Ejemplo n.º 1
0
 def test_reraise_errors(self):
     with pytest.raises(SecurityError):
         with reraise_errors(errors=(KeyError, )):
             raise KeyError('foo')
     with pytest.raises(KeyError):
         with reraise_errors(errors=(ValueError, )):
             raise KeyError('bar')
Ejemplo n.º 2
0
 def test_reraise_errors(self):
     with pytest.raises(SecurityError):
         with reraise_errors(errors=(KeyError,)):
             raise KeyError('foo')
     with pytest.raises(KeyError):
         with reraise_errors(errors=(ValueError,)):
             raise KeyError('bar')
Ejemplo n.º 3
0
 def test_reraise_errors(self):
     with self.assertRaises(SecurityError):
         with reraise_errors(errors=(KeyError,)):
             raise KeyError("foo")
     with self.assertRaises(KeyError):
         with reraise_errors(errors=(ValueError,)):
             raise KeyError("bar")
Ejemplo n.º 4
0
 def deserialize(self, data):
     """Deserialize data structure from string."""
     assert self._cert_store is not None
     with reraise_errors('Unable to deserialize: {0!r}', (Exception, )):
         payload = self._unpack(data)
         signature, signer, body = (payload['signature'], payload['signer'],
                                    payload['body'])
         self._cert_store[signer].verify(body, signature, self._digest)
     return loads(bytes_to_str(body),
                  payload['content_type'],
                  payload['content_encoding'],
                  force=True)
Ejemplo n.º 5
0
 def serialize(self, data):
     """Serialize data structure into string."""
     assert self._key is not None
     assert self._cert is not None
     with reraise_errors('Unable to serialize: {0!r}', (Exception, )):
         content_type, content_encoding, body = dumps(
             bytes_to_str(data), serializer=self._serializer)
         # What we sign is the serialized body, not the body itself.
         # this way the receiver doesn't have to decode the contents
         # to verify the signature (and thus avoiding potential flaws
         # in the decoding step).
         body = ensure_bytes(body)
         return self._pack(body,
                           content_type,
                           content_encoding,
                           signature=self._key.sign(body, self._digest),
                           signer=self._cert.get_id())
Ejemplo n.º 6
0
 def sign(self, data, digest):
     """Sign string containing data."""
     with reraise_errors('Unable to sign data: {0!r}'):
         return crypto.sign(self._key, ensure_bytes(data), digest)
Ejemplo n.º 7
0
 def __init__(self, key):
     with reraise_errors('Invalid private key: {0!r}'):
         self._key = crypto.load_privatekey(crypto.FILETYPE_PEM, key)
Ejemplo n.º 8
0
 def verify(self, data, signature, digest):
     """Verify signature for string containing data."""
     with reraise_errors('Bad signature: {0!r}'):
         crypto.verify(self._cert, signature, data, digest)
Ejemplo n.º 9
0
 def __init__(self, cert):
     assert crypto is not None
     with reraise_errors('Invalid certificate: {0!r}'):
         self._cert = crypto.load_certificate(crypto.FILETYPE_PEM, cert)