def test_mime(): decoded = loads(unhexlify( 'd824787b436f6e74656e742d547970653a20746578742f706c61696e3b20636861727365743d2269736f2d38' '3835392d3135220a4d494d452d56657273696f6e3a20312e300a436f6e74656e742d5472616e736665722d45' '6e636f64696e673a2071756f7465642d7072696e7461626c650a0a48656c6c6f203d413475726f')) assert isinstance(decoded, Message) assert decoded.get_payload() == 'Hello =A4uro'
def test_custom_decoder(): def reverse(decoder, value, fp, shareable_index): return value[::-1] decoded = loads(unhexlify('d917706548656c6c6f'), semantic_decoders={6000: reverse}) assert decoded == u'olleH'
def test_unhandled_tag(): """ Test that a tag is simply ignored and its associated value returned if there is no special handling available for it. """ decoded = loads(unhexlify('d917706548656c6c6f')) assert decoded == CBORTag(6000, u'Hello')
def test_object_hook(): class DummyType(object): def __init__(self, state): self.state = state payload = unhexlify('A2616103616205') decoded = loads(payload, object_hook=lambda decoder, value: DummyType(value)) assert isinstance(decoded, DummyType) assert decoded.state == {'a': 3, 'b': 5}
def test_tag_hook_cyclic(): class DummyType(object): def __init__(self, value): self.value = value def unmarshal_dummy(decoder, tag, shareable_index=None): instance = DummyType.__new__(DummyType) decoder.set_shareable(shareable_index, instance) instance.value = decoder.decode_from_bytes(tag.value) return instance decoded = loads(unhexlify('D81CD90BB849D81CD90BB843D81D00'), tag_hook=unmarshal_dummy) assert isinstance(decoded, DummyType) assert decoded.value.value is decoded
def test_streaming(payload, expected): decoded = loads(unhexlify(payload)) assert decoded == expected
def test_special(payload, expected): decoded = loads(unhexlify(payload)) assert decoded is expected
def test_integer(payload, expected): decoded = loads(unhexlify(payload)) assert decoded == expected
def test_rational(): decoded = loads(unhexlify('d81e820205')) assert decoded == Fraction(2, 5)
def test_fraction(): decoded = loads(unhexlify('c48221196ab3')) assert decoded == Decimal('273.15')
def test_simple_value(payload, expected): decoded = loads(unhexlify(payload)) assert decoded == expected
def test_mixed_array_map(payload, expected): decoded = loads(unhexlify(payload)) assert decoded == expected
def test_tag_hook(): def reverse(decoder, tag, fp, shareable_index=None): return tag.value[::-1] decoded = loads(unhexlify('d917706548656c6c6f'), tag_hook=reverse) assert decoded == u'olleH'
def test_bigfloat(): decoded = loads(unhexlify('c5822003')) assert decoded == Decimal('1.5')
def test_cyclic_map(): decoded = loads(unhexlify('d81ca100d81d00')) assert decoded == {0: decoded}
def test_cyclic_array(): decoded = loads(unhexlify('d81c81d81d00')) assert decoded == [decoded]
def test_uuid(): decoded = loads(unhexlify('d825505eaffac8b51e480581277fdcc7842faf')) assert decoded == UUID(hex='5eaffac8b51e480581277fdcc7842faf')
def decode_credential_public_key( key: bytes, ) -> Union[DecodedOKPPublicKey, DecodedEC2PublicKey, DecodedRSAPublicKey]: """ Decode a CBOR-encoded public key and turn it into a data structure. Supports OKP, EC2, and RSA public keys """ # Occassionally we might be given a public key in an "uncompressed" format, # typically from older U2F security keys. As per the FIDO spec this is indicated by # a leading 0x04 "uncompressed point compression method" format byte. In that case # we need to fill in some blanks to turn it into a full EC2 key for signature # verification # # See https://fidoalliance.org/specs/fido-v2.0-id-20180227/fido-registry-v2.0-id-20180227.html#public-key-representation-formats if key[0] == 0x04: return DecodedEC2PublicKey( kty=COSEKTY.EC2, alg=COSEAlgorithmIdentifier.ECDSA_SHA_256, crv=COSECRV.P256, x=key[1:33], y=key[33:65], ) decoded_key: dict = decoder.loads(key) kty = decoded_key[COSEKey.KTY] alg = decoded_key[COSEKey.ALG] if not kty: raise InvalidPublicKeyStructure("Credential public key missing kty") if not alg: raise InvalidPublicKeyStructure("Credential public key missing alg") if kty == COSEKTY.OKP: crv = decoded_key[COSEKey.CRV] x = decoded_key[COSEKey.X] if not crv: raise InvalidPublicKeyStructure( "OKP credential public key missing crv") if not x: raise InvalidPublicKeyStructure( "OKP credential public key missing x") return DecodedOKPPublicKey( kty=kty, alg=alg, crv=crv, x=x, ) elif kty == COSEKTY.EC2: crv = decoded_key[COSEKey.CRV] x = decoded_key[COSEKey.X] y = decoded_key[COSEKey.Y] if not crv: raise InvalidPublicKeyStructure( "EC2 credential public key missing crv") if not x: raise InvalidPublicKeyStructure( "EC2 credential public key missing x") if not y: raise InvalidPublicKeyStructure( "EC2 credential public key missing y") return DecodedEC2PublicKey( kty=kty, alg=alg, crv=crv, x=x, y=y, ) elif kty == COSEKTY.RSA: n = decoded_key[COSEKey.N] e = decoded_key[COSEKey.E] if not n: raise InvalidPublicKeyStructure( "RSA credential public key missing n") if not e: raise InvalidPublicKeyStructure( "RSA credential public key missing e") return DecodedRSAPublicKey( kty=kty, alg=alg, n=n, e=e, ) raise UnsupportedPublicKeyType( f'Unsupported credential public key type "{kty}"')
def test_datetime(payload, expected): decoded = loads(unhexlify(payload)) assert decoded == expected
def test_regex(): decoded = loads(unhexlify('d8236d68656c6c6f2028776f726c6429')) expr = re.compile(u'hello (world)') assert decoded == expr
def test_set(): payload = unhexlify('d9010283616361626161') value = loads(payload) assert type(value) is set assert value == set([u'a', u'b', u'c'])
def test_immutable_keys(payload, expected): value = loads(unhexlify(payload)) assert value == expected
def test_float_nan(payload): decoded = loads(unhexlify(payload)) assert math.isnan(decoded)
def test_binary(payload, expected): decoded = loads(unhexlify(payload)) assert decoded == expected