def build_signed_envelope(signed_envelope, recipient, sender_id, request_id, encrypted_payload, sign): """ Build signed envelope application message :param signed_envelope: :param recipient: :param sender_id: :param request_id: :param encrypted_payload: :param sign: :return: """ application_message = websocket_pb2.ApplicationMessage() application_message.version = websocket_pb2.ApplicationMessage.MAJOR_VERSION_V1 application_message.id = request_id application_message.to = recipient application_message.sender = sender_id application_message.payload = encrypted_payload serialized = application_message.SerializeToString() signature = sign(serialized) signed_envelope.messageType = sb_common_pb2.SignedEnvelope.MESSAGE_TYPE_APPLICATION_MESSAGE signed_envelope.signature = signature signed_envelope.serialized = serialized
def build_envelope(signed_response, message, recipient, encryption_context, request_id, sign, generichash, logger): """Takes the Server Application Response, encrypts it and constructs top level Signed Envelope which is sent back to Spacebridge. Arguments: :param message: Encrypted and processed message :param recipient: Who the message will be sent to :param encryption_context: Encryption context object :param request_id: The id we associate with the request on the server side :param sign: A function that takes in a message and returns a digital signature :param generichash: hash function for encryption :param logger: Logger object for logging purposes Returns: SignedEnvelope Proto """ # First construct application level message application_message = websocket_pb2.ApplicationMessage() application_message.version = websocket_pb2.ApplicationMessage.MAJOR_VERSION_V1 application_message.id = request_id application_message.to = recipient application_message.sender = encryption_context.sign_public_key( transform=generichash) application_message.payload = message serialized = application_message.SerializeToString() # Construct Signed Envelope signed_response.serialized = serialized signed_response.messageType = sb_common_pb2.SignedEnvelope.MESSAGE_TYPE_APPLICATION_MESSAGE signed_response.signature = sign(serialized) logger.info("Finished Signing envelope request_id={}".format(request_id)) return signed_response
def parse_application_message(serialized_message): """Deserialize a serialized Application Message object Arguments: serialized_message {bytes} Returns: ApplicationMessage Proto """ application_message = websocket_pb2.ApplicationMessage() try: application_message.ParseFromString(serialized_message) except: LOGGER.exception("Exception deserializing protobuf") return application_message
def build_envelope(message, recipient, websocket_protocol, request_id, sign, generichash): """Takes the Server Application Response, encrypts it and constructs top level Signed Envelope which is sent back to Spacebridge. Arguments: :param message: Encrypted and processed message :param recipient: Who the message will be sent to :param websocket_protocol: The protocol used to send the method :param request_id: The id we associate with the request on the server side :param sign: A function that takes in a message and returns a digital signature Returns: SignedEnvelope Proto """ sodium_client = websocket_protocol.encryption_context.sodium_client # First construct application level message application_message = websocket_pb2.ApplicationMessage() application_message.version = websocket_pb2.ApplicationMessage.MAJOR_VERSION_V1 application_message.id = request_id application_message.to = recipient application_message.sender = websocket_protocol.encryption_context.sign_public_key( transform=generichash) application_message.payload = message serialized = application_message.SerializeToString() # Construct Signed Envelope signed_response = sb_common_pb2.SignedEnvelope() signed_response.serialized = serialized signed_response.messageType = sb_common_pb2.SignedEnvelope.MESSAGE_TYPE_APPLICATION_MESSAGE signed_response.signature = sign(serialized) LOGGER.info("Finished Signing envelope") return signed_response