Beispiel #1
0
 def init_key_exchange(self, recipient):
     """Send initial key exchange message to recipient"""
     logger.info(
         'Sending initial key exchange message to {}...'.format(recipient))
     sessionBuilder = self.getSessionBuilder(recipient)
     keyExchangeMessage = sessionBuilder.processInitKeyExchangeMessage()
     return Message(recipient, keyExchangeMessage.serialize(),
                    MessageType.KEY)
Beispiel #2
0
    def receive(self, message=""):
        """Receive email message (read it from stdin)"""
        if not message:
            message = sys.stdin.read()

        email_message = parse_email(message)
        sender = email_message["from"][1]
        body = email_message["body"][0]
        if sender and body:
            return Message.from_raw(sender, self.encoder.decode(body.encode("ascii")))
Beispiel #3
0
    def receive(self, message=''):
        """Receive email message (read it from stdin)"""
        if not message:
            message = sys.stdin.read()

        email_message = parse_email(message)
        sender = email_message['from'][1]
        body = email_message['body'][0]
        if sender and body:
            return Message.from_raw(sender,
                                    self.encoder.decode(body.encode('ascii')))
Beispiel #4
0
    def receive(self, message=""):
        """Receive message (read it from terminal)"""
        if not message:
            print("RECEIVE:")
            sender = input("From: ")
            body = input("Encrypted message: ")
            print()
        else:
            sender, body = message.strip().split(None, maxsplit=1)

        if sender and body:
            return Message.from_raw(sender, self.encoder.decode(body.encode("ascii")))
Beispiel #5
0
    def receive(self, message=''):
        """Receive message (read it from terminal)"""
        if not message:
            print('RECEIVE:')
            sender = input('From: ')
            body = input('Encrypted message: ')
            print()
        else:
            sender, body = message.strip().split(None, maxsplit=1)

        if sender and body:
            return Message.from_raw(sender,
                                    self.encoder.decode(body.encode('ascii')))
Beispiel #6
0
 def send(self, recipient, plaintext):
     """Send encrypted message to recipient"""
     if self.store.containsSession(recipient, self.DEFAULT_DEVICE_ID):
         if not self.store.sessionStore.hasPendingKeyExchange(
                 recipient, self.DEFAULT_DEVICE_ID):
             logger.info(
                 'Sending encrypted message to {}...'.format(recipient))
             sessionCipher = self.getSessionCipher(recipient)
             whisperMessage = sessionCipher.encrypt(plaintext)
             return Message(recipient, whisperMessage.serialize(),
                            MessageType.SECURE)
         else:
             raise PendingKeyExchangeException(
                 'Session is in pending key exchange state, '
                 'wait for KeyExchangeMessage reply!')
     else:
         raise NoSessionException('Session doesn\'t exists, '
                                  'send initial KeyExchangeMessage first!')
Beispiel #7
0
    def handle_KeyExchangeMessage(self, message):
        """Handle received key exchange message"""
        #if self.store.containsSession(message.identity, self.DEFAULT_DEVICE_ID):
        #    raise RuntimeError('KeyExchangeMessage received, but session already exists!')

        sessionBuilder = self.getSessionBuilder(message.identity)
        keyExchangeMessage = sessionBuilder.processKeyExchangeMessage(
            KeyExchangeMessage(serialized=message.message))

        if keyExchangeMessage:
            logger.info('Received initial KeyExchangeMessage from {}, '
                        'sending response...'.format(message.identity))
            return Message(message.identity, keyExchangeMessage.serialize(),
                           MessageType.KEY)
        else:
            logger.info(
                'Received response from {} to initial KeyExchangeMessage, '
                'key exchange completed.'.format(message.identity))
Beispiel #8
0
    def end_session(self, recipient):
        """Send end session message to recipient and delete session"""
        if self.store.containsSession(recipient, self.DEFAULT_DEVICE_ID):
            endSessionMessage = None
            if not self.store.sessionStore.hasPendingKeyExchange(
                    recipient, self.DEFAULT_DEVICE_ID):
                logger.info(
                    'Sending end session message to {}...'.format(recipient))
                sessionCipher = self.getSessionCipher(recipient)
                endSessionMessage = sessionCipher.encrypt('TERMINATE')

            logger.info(
                'Deleting session for recipient {}...'.format(recipient))
            self.store.deleteSession(recipient, self.DEFAULT_DEVICE_ID)

            if endSessionMessage:
                return Message(recipient, endSessionMessage.serialize(),
                               MessageType.END_SESSION)
        else:
            raise NoSessionException('Session doesn\'t exists!')