Moves appointment to common and creates serialize

Appointment serialization used to be part of the cryptographer (signature_format) but it makes more sense to be an appointment method. Therefore cli also need Appointment

Also fixes comments based on reviews
This commit is contained in:
Sergi Delgado Segura
2019-12-17 15:09:49 +01:00
parent 5c01bec89c
commit a69f1ba28b
3 changed files with 25 additions and 22 deletions

View File

@@ -1,4 +1,6 @@
import json import json
import struct
from binascii import unhexlify
from pisa.encrypted_blob import EncryptedBlob from pisa.encrypted_blob import EncryptedBlob
@@ -101,3 +103,23 @@ class Appointment:
appointment["triggered"] = triggered appointment["triggered"] = triggered
return json.dumps(appointment, sort_keys=True, separators=(",", ":")) return json.dumps(appointment, sort_keys=True, separators=(",", ":"))
def serialize(self):
"""
Serializes an appointment to be signed.
The serialization follows the same ordering as the fields in the appointment:
locator:start_time:end_time:to_self_delay:encrypted_blob
All values are big endian.
Returns:
:mod:`bytes`: The serialized data to be signed.
"""
return (
unhexlify(self.locator)
+ struct.pack(">I", self.start_time)
+ struct.pack(">I", self.end_time)
+ struct.pack(">I", self.to_self_delay)
+ unhexlify(self.encrypted_blob.data)
)

View File

@@ -1,4 +1,3 @@
import json
from hashlib import sha256 from hashlib import sha256
from binascii import unhexlify, hexlify from binascii import unhexlify, hexlify
@@ -146,23 +145,6 @@ class Cryptographer:
return blob return blob
# NOTCOVERED
@staticmethod
def signature_format(data):
"""
Serializes a given ``data`` in the right format to be signed.
Args:
data(:mod:`str`): the data to be formatted.
Returns:
:mod:`str`: The serialized data to be signed.
"""
# FIXME: This is temporary serialization. A proper one is required. Data need to be unhexlified too (can't atm)
return json.dumps(data, sort_keys=True, separators=(",", ":")).encode("utf-8")
# Deserialize public key from der data.
@staticmethod @staticmethod
def load_public_key_der(pk_der): def load_public_key_der(pk_der):
""" """
@@ -195,7 +177,6 @@ class Cryptographer:
return None return None
# Deserialize private key from der data.
@staticmethod @staticmethod
def load_private_key_der(sk_der): def load_private_key_der(sk_der):
""" """
@@ -231,7 +212,7 @@ class Cryptographer:
Signs a given data using a given secret key using ECDSA. Signs a given data using a given secret key using ECDSA.
Args: Args:
data(:mod:`str`): the data to be signed. data(:mod:`bytes`): the data to be signed.
sk(:mod:`EllipticCurvePrivateKey`): the ECDSA secret key used to signed the data. sk(:mod:`EllipticCurvePrivateKey`): the ECDSA secret key used to signed the data.
rtype: the return type for the encrypted value. Can be either ``'str'`` or ``'bytes'``. rtype: the return type for the encrypted value. Can be either ``'str'`` or ``'bytes'``.
@@ -263,7 +244,7 @@ class Cryptographer:
Verifies if a signature is valid for a given public key and message. Verifies if a signature is valid for a given public key and message.
Args: Args:
message(:mod:`str`): the message that is supposed have been signed. message(:mod:`bytes`): the message that is supposed have been signed.
signature(:mod:`str`): the potential signature of the message. signature(:mod:`str`): the potential signature of the message.
pk(:mod:`EllipticCurvePublicKey`): the public key that is used to try to verify the signature. pk(:mod:`EllipticCurvePublicKey`): the public key that is used to try to verify the signature.

View File

@@ -2,7 +2,7 @@ import json
from pytest import fixture from pytest import fixture
from pisa import c_logger from pisa import c_logger
from pisa.appointment import Appointment from common.appointment import Appointment
from pisa.encrypted_blob import EncryptedBlob from pisa.encrypted_blob import EncryptedBlob
from test.pisa.unit.conftest import get_random_value_hex from test.pisa.unit.conftest import get_random_value_hex