Ejemplo n.º 1
0
    def decrypt_stanza(self, stanza):
        # delete the unencrypted explanation body, if it exists
        orig_body = stanza.getTag("body")
        if orig_body:
            stanza.delChild(orig_body)

        c = stanza.getTag(name="c", namespace="http://www.xmpp.org/extensions/xep-0200.html#ns")

        stanza.delChild(c)

        # contents of <c>, minus <mac>, minus whitespace
        macable = "".join(map(str, filter(lambda x: x.getName() != "mac", c.getChildren())))

        received_mac = base64.b64decode(c.getTagData("mac"))
        calculated_mac = self.hmac(self.km_o, macable + crypto.encode_mpi_with_padding(self.c_o))

        if not calculated_mac == received_mac:
            raise exceptions.DecryptionError, "bad signature"

        m_final = base64.b64decode(c.getTagData("data"))
        m_compressed = self.decrypt(m_final)
        plaintext = self.decompress(m_compressed)

        try:
            parsed = xmpp.Node(node="<node>" + plaintext + "</node>")
        except Exception:
            raise exceptions.DecryptionError, "decrypted <data/> not parseable as XML"

        for child in parsed.getChildren():
            stanza.addChild(node=child)

        return stanza
Ejemplo n.º 2
0
    def decrypt_stanza(self, stanza):
        """
        Delete the unencrypted explanation body, if it exists
        """
        orig_body = stanza.getTag('body')
        if orig_body:
            stanza.delChild(orig_body)

        c = stanza.getTag(
            name='c',
            namespace='http://www.xmpp.org/extensions/xep-0200.html#ns')

        stanza.delChild(c)

        # contents of <c>, minus <mac>, minus whitespace
        macable = ''.join(
            str(x) for x in c.getChildren() if x.getName() != 'mac')
        macable = macable.encode('utf-8')

        received_mac = base64.b64decode(c.getTagData('mac'))
        calculated_mac = self.hmac(self.km_o, macable + \
                crypto.encode_mpi_with_padding(self.c_o))

        if not calculated_mac == received_mac:
            raise DecryptionError('bad signature')

        m_final = base64.b64decode(c.getTagData('data'))
        m_compressed = self.decrypt(m_final)
        plaintext = self.decompress(m_compressed).decode('utf-8')

        try:
            parsed = nbxmpp.Node(node='<node>' + plaintext + '</node>')
        except Exception:
            raise DecryptionError('decrypted <data/> not parseable as XML')

        for child in parsed.getChildren():
            stanza.addChild(node=child)

        # replace non-character unicode
        body = stanza.getBody()
        if body:
            stanza.setBody(
                self.conn.connection.Dispatcher.replace_non_character(body))

        return stanza
Ejemplo n.º 3
0
    def decrypt_stanza(self, stanza):
        """
        Delete the unencrypted explanation body, if it exists
        """
        orig_body = stanza.getTag('body')
        if orig_body:
            stanza.delChild(orig_body)

        c = stanza.getTag(name='c',
                namespace='http://www.xmpp.org/extensions/xep-0200.html#ns')

        stanza.delChild(c)

        # contents of <c>, minus <mac>, minus whitespace
        macable = ''.join(str(x) for x in c.getChildren() if x.getName() != 'mac')
        macable = macable.encode('utf-8')

        received_mac = base64.b64decode(c.getTagData('mac'))
        calculated_mac = self.hmac(self.km_o, macable + \
                crypto.encode_mpi_with_padding(self.c_o))

        if not calculated_mac == received_mac:
            raise DecryptionError('bad signature')

        m_final = base64.b64decode(c.getTagData('data'))
        m_compressed = self.decrypt(m_final)
        plaintext = self.decompress(m_compressed).decode('utf-8')

        try:
            parsed = nbxmpp.Node(node='<node>' + plaintext + '</node>')
        except Exception:
            raise DecryptionError('decrypted <data/> not parseable as XML')

        for child in parsed.getChildren():
            stanza.addChild(node=child)

        # replace non-character unicode
        body = stanza.getBody()
        if body:
            stanza.setBody(
                self.conn.connection.Dispatcher.replace_non_character(body))

        return stanza
Ejemplo n.º 4
0
    def decrypt_stanza(self, stanza):
        # delete the unencrypted explanation body, if it exists
        orig_body = stanza.getTag('body')
        if orig_body:
            stanza.delChild(orig_body)

        c = stanza.getTag(
            name='c',
            namespace='http://www.xmpp.org/extensions/xep-0200.html#ns')

        stanza.delChild(c)

        # contents of <c>, minus <mac>, minus whitespace
        macable = ''.join(
            map(str, filter(lambda x: x.getName() != 'mac', c.getChildren())))

        received_mac = base64.b64decode(c.getTagData('mac'))
        calculated_mac = self.hmac(self.km_o, macable + \
         crypto.encode_mpi_with_padding(self.c_o))

        if not calculated_mac == received_mac:
            raise exceptions.DecryptionError, 'bad signature'

        m_final = base64.b64decode(c.getTagData('data'))
        m_compressed = self.decrypt(m_final)
        plaintext = self.decompress(m_compressed)

        try:
            parsed = xmpp.Node(node='<node>' + plaintext + '</node>')
        except Exception:
            raise exceptions.DecryptionError, 'decrypted <data/> not parseable as XML'

        for child in parsed.getChildren():
            stanza.addChild(node=child)

        return stanza
Ejemplo n.º 5
0
 def decryptcounter(self):
     self.c_o = (self.c_o + 1) % (2**self.n)
     return crypto.encode_mpi_with_padding(self.c_o)
Ejemplo n.º 6
0
 def encryptcounter(self):
     self.c_s = (self.c_s + 1) % (2**self.n)
     return crypto.encode_mpi_with_padding(self.c_s)
Ejemplo n.º 7
0
 def decryptcounter(self):
     self.c_o = (self.c_o + 1) % (2 ** self.n)
     return crypto.encode_mpi_with_padding(self.c_o)
Ejemplo n.º 8
0
 def encryptcounter(self):
     self.c_s = (self.c_s + 1) % (2 ** self.n)
     return crypto.encode_mpi_with_padding(self.c_s)