def decode_hex16(value, size=None, minimum=False): """Decode hexlified field. :param int size: Required length (after decoding). :param bool minimum: If ``True``, then `size` will be treated as minimum required length, as opposed to exact equality. """ if size is not None and ((not minimum and len(value) != size * 2) or (minimum and len(value) < size * 2)): raise errors.DeserializationError() try: return binascii.unhexlify(value) except TypeError as error: raise errors.DeserializationError(error)
def decode_csr(b64der): """Decode JOSE Base-64 DER-encoded CSR.""" try: return util.ComparableX509( M2Crypto.X509.load_request_der_string(decode_b64jose(b64der))) except M2Crypto.X509.X509Error as error: raise errors.DeserializationError(error)
def decode(cls, value): """Decoder.""" # 4.1.10 if '/' not in value: if ';' in value: raise errors.DeserializationError('Unexpected semi-colon') return cls.PREFIX + value return value
def decode_b64jose(data, size=None, minimum=False): """Decode JOSE Base-64 field. :param int size: Required length (after decoding). :param bool minimum: If ``True``, then `size` will be treated as minimum required length, as opposed to exact equality. """ try: decoded = b64.b64decode(data) except TypeError as error: raise errors.DeserializationError(error) if size is not None and ((not minimum and len(decoded) != size) or (minimum and len(decoded) < size)): raise errors.DeserializationError() return decoded
def _check_required(cls, jobj): missing = set() for _, field in cls._fields.iteritems(): if not field.omitempty and field.json_name not in jobj: missing.add(field.json_name) if missing: raise errors.DeserializationError( 'The following field are required: {0}'.format( ','.join(missing)))
def from_json(cls, jobj): if 'signature' in jobj and 'signatures' in jobj: raise errors.DeserializationError('Flat mixed with non-flat') elif 'signature' in jobj: # flat return cls(payload=json_util.decode_b64jose(jobj.pop('payload')), signatures=(Signature.from_json(jobj),)) else: return cls(payload=json_util.decode_b64jose(jobj['payload']), signatures=tuple(Signature.from_json(sig) for sig in jobj['signatures']))
def from_compact(cls, compact): """Compact deserialization.""" try: protected, payload, signature = compact.split('.') except ValueError: raise errors.DeserializationError( 'Compact JWS serialization should comprise of exactly' ' 3 dot-separated components') sig = Signature(protected=json_util.decode_b64jose(protected), signature=json_util.decode_b64jose(signature)) return cls(payload=json_util.decode_b64jose(payload), signatures=(sig,))
def get_type_cls(cls, jobj): """Get the registered class for ``jobj``.""" if cls in cls.TYPES.itervalues(): assert jobj[cls.type_field_name] # cls is already registered type_cls, force to use it # so that, e.g Revocation.from_json(jobj) fails if # jobj["type"] != "revocation". return cls if not isinstance(jobj, dict): raise errors.DeserializationError( "{0} is not a dictionary object".format(jobj)) try: typ = jobj[cls.type_field_name] except KeyError: raise errors.DeserializationError("missing type field") try: return cls.TYPES[typ] except KeyError: raise errors.UnrecognizedTypeError(typ, jobj)
def fields_from_json(cls, jobj): """Deserialize fields from JSON.""" cls._check_required(jobj) fields = {} for slot, field in cls._fields.iteritems(): if field.json_name not in jobj and field.omitempty: fields[slot] = field.default else: value = jobj[field.json_name] try: fields[slot] = field.decode(value) except errors.DeserializationError as error: raise errors.DeserializationError( 'Could not decode {0!r} ({1!r}): {2}'.format( slot, value, error)) return fields
def y(value): if value == 500: raise errors.DeserializationError() return value
def _decode_param(cls, data): try: return long(binascii.hexlify(json_util.decode_b64jose(data)), 16) except ValueError: # invalid literal for long() with base 16 raise errors.DeserializationError()
def fields_from_json(cls, jobj): fields = super(Signature, cls).fields_from_json(jobj) fields_with_combined = cls._with_combined(fields) if 'alg' not in fields_with_combined['combined'].not_omitted(): raise errors.DeserializationError('alg not present') return fields_with_combined
def x5c(value): # pylint: disable=missing-docstring,no-self-argument try: return tuple(util.ComparableX509(M2Crypto.X509.load_cert_der_string( base64.b64decode(cert))) for cert in value) except M2Crypto.X509.X509Error as error: raise errors.DeserializationError(error)
def crit(unused_value): # pylint: disable=missing-docstring,no-self-argument,no-self-use raise errors.DeserializationError( '"crit" is not supported, please subclass')