예제 #1
0
    def _parse_header(cls, hdr) -> dict:
        decoded_hdr = {}
        for k, v in hdr.items():
            attr = CoseHeaderAttribute.from_id(k)
            decoded_hdr[attr] = attr.value_parser(v)

        return decoded_hdr
예제 #2
0
def print_header(hdr):
    if isinstance(hdr, dict):
        for k, v in hdr.items():
            attr = CoseHeaderAttribute.from_id(k)
            if isinstance(v, bytes):
                v = "{hex}" + binascii.hexlify(v).decode()
            print(f"  {k} ({attr.fullname}) = {v}")
예제 #3
0
    def _parse_header(cls, hdr, allow_unknown_attributes: bool = True) -> dict:
        decoded_hdr = {}
        for k, v in hdr.items():
            attr = CoseHeaderAttribute.from_id(
                k, allow_unknown_attributes=allow_unknown_attributes)

            if hasattr(attr, 'value_parser'):
                decoded_hdr[attr] = attr.value_parser(v)
            else:
                decoded_hdr[attr] = v

        return decoded_hdr
예제 #4
0
    def _transform_headers(output_header, base_header: dict):
        for _header_attribute, _value in base_header.items():
            try:
                # translate the header attribute
                hp = CoseHeaderAttribute.from_id(_header_attribute)

                # parse the value of the key attribute if possible
                if hasattr(hp.value_parser, '__call__'):
                    _value = hp.value_parser(_value)

                # store in new dict
                output_header[hp] = _value
            except ValueError:
                output_header[_header_attribute] = _value
예제 #5
0
def type_conv(d: dict) -> dict:
    if "__header__" in d:
        header = {}
        if 'ephemeral_key' in d:
            header[EphemeralKey] = d['ephemeral_key']
        if 'static_key' in d:
            header[StaticKey] = d['static_key']
        if 'alg' in d:
            header[Algorithm] = CoseAlgorithm.from_id(d['alg'])
        if 'kid' in d:
            header[KID] = d['kid'].encode('utf-8')
        if 'iv' in d:
            header[IV] = unhexlify(d['iv'])
        if 'party_u_nonce' in d:
            header[PartyUNonce] = unhexlify(d['party_u_nonce'])
        if 'ctype' in d:
            header[ContentType] = d['ctype']
        return header

    elif "__input__" in d or "__recipient__" in d:
        if 'alg' in d:
            d['alg'] = CoseAlgorithm.from_id(d['alg'])
        if 'plaintext' in d:
            d['plaintext'] = d['plaintext'].encode('utf-8')
        if 'external_aad' in d:
            d['external_aad'] = unhexlify(d['external_aad'])
        if 'iv' in d:
            d['iv'] = unhexlify(d['iv'])
        return d

    elif "__key__" in d:
        key = {KpKty: KTY.from_id(d['kty'])}
        if 'alg' in d:
            key[KpAlg] = CoseAlgorithm.from_id(d['alg'])
        if 'kid' in d:
            key[KpKid] = d["kid"].encode("utf-8")
        if 'crv' in d and d['kty'] == "EC2":
            key[EC2KpCurve] = CoseCurve.from_id(d['crv'])
        if 'crv' in d and d['kty'] == "OKP":
            key[OKPKpCurve] = CoseCurve.from_id(d['crv'])
        if 'k' in d:
            key[SymKpK] = unhexlify(d['k'])
        if 'x' in d and d['kty'] == "EC2":
            key[EC2KpX] = unhexlify(d['x'])
        if 'x' in d and d['kty'] == "OKP":
            key[OKPKpX] = unhexlify(d['x'])
        if 'y' in d and d['kty'] == "EC2":
            key[EC2KpY] = unhexlify(d['y'])
        if 'd' in d and d['kty'] == "EC2":
            key[EC2KpD] = unhexlify(d['d'])
        if 'd' in d and d['kty'] == "OKP":
            key[OKPKpD] = unhexlify(d['d'])

        key = cosekey.CoseKey.from_dict(key)
        return key

    elif '__encode_rcpt__' in d or '__encode_signer__' in d:
        d['protected'] = unhexlify(d['protected'])
        d['unprotected'] = {
            CoseHeaderAttribute.from_id(k1): cosekey.CoseKey.from_dict({
                k2: (unhexlify(v2) if k2 == -2 or k2 == -3 else v2)
                for k2, v2 in v1.items()
            }) if isinstance(v1, dict) else v1
            for k1, v1 in eval(d['unprotected']).items()
        }
        if PartyUNonce in d['unprotected']:
            d['unprotected'][PartyUNonce] = unhexlify(
                d['unprotected'][PartyUNonce])
        elif Algorithm in d['unprotected']:
            d['unprotected'][Algorithm] = CoseAlgorithm.from_id(
                d['unprotected'][Algorithm])

        if 'structure' in d:
            d['structure'] = unhexlify(d['structure'])
        if 'signature' in d:
            d['signature'] = unhexlify(d['signature'])
        if 'ciphertext' in d:
            d['ciphertext'] = unhexlify(d['ciphertext'])
        if 'secret' in d:
            d['secret'] = unhexlify(d['secret'])
        if "context" in d:
            d['context'] = unhexlify(d['context'])
        if 'kek' in d:
            d['kek'] = unhexlify(d['kek'])
        return d

    elif '__output__' in d:
        d['result'] = cbor2.loads(unhexlify(d['result']))
        d['protected'] = unhexlify(d['protected'])
        d['unprotected'] = {
            CoseHeaderAttribute.from_id(k): v
            for k, v in eval(d['unprotected']).items()
        }

        for k in d['unprotected'].keys():
            if k == Algorithm:
                d['unprotected'][k] = CoseAlgorithm.from_id(
                    d['unprotected'][k])
            if k == IV:
                d['unprotected'][k] = unhexlify(d['unprotected'][k])
            if k == KID:
                d['unprotected'][k] = d['unprotected'][k]
            if k == PartyUNonce:
                d['unprotected'][k] = unhexlify(d['unprotected'][k])

        if "cek" in d:
            d['cek'] = unhexlify(d["cek"])

        if 'structure' in d:
            d['structure'] = unhexlify(d['structure'])
        d['ciphertext'] = unhexlify(d['ciphertext'])
        if 'tag' in d:
            d['tag'] = unhexlify(d['tag'])
        if 'signature' in d:
            d['signature'] = unhexlify(d['signature'])

        return d

    else:
        return d