Beispiel #1
0
    def read_xml(self,xml,private_key):
        
        # Parse Salmon document
        tree = ElementTree.fromstring(xml)
        
        encrypted_header = tree.findtext('.//{http://www.w3.org/2005/Atom}encrypted_header')
        envelope_data_encrypted = tree.findtext('.//{http://salmon-protocol.org/ns/magic-env}data')
        envelope_signature = tree.findtext('.//{http://salmon-protocol.org/ns/magic-env}data')
        envelope_encoding = tree.findtext('.//{http://salmon-protocol.org/ns/magic-env}encoding')
        envelope_alg = tree.findtext('.//{http://salmon-protocol.org/ns/magic-env}alg')

        # Validate stuff
        if envelope_encoding != 'base64url':
            # Wrong encoding in salmon
            return None
        
        if envelope_alg != 'RSA-SHA256':
            # Wrong algorithm used for signature
            return None
        
        # Check signature
        # TODO: IMPORTANT! Verify signature

        # B64decode and unfold encrypted header
        encrypted_header = simplejson.loads(base64.b64decode(encrypted_header))
        encrypted_header_key = base64.b64decode(encrypted_header['aes_key'])
        encrypted_header_cipher = base64.b64decode(encrypted_header['ciphertext'])
        
        # Extract the key json (diaspora adds some random padding before json data, so we'll filter that out)
        test_dec = re.search('\{\".*\"}',rsa_helper.decrypt(encrypted_header_key,private_key))
        if test_dec.group(0) == None:
            test_dec = re.search('\{\'.*\'}',rsa_helper.decrypt(encrypted_header_key,private_key))

        # Save key and iv and decrypt header
        encrypted_header_decrypted_key = [  base64.b64decode(simplejson.loads(test_dec.group(0))[u'key']),
                                            base64.b64decode(simplejson.loads(test_dec.group(0))[u'iv'])]

        decrypted_header = self.filter_printable(aes_helper.decrypt(encrypted_header_cipher,encrypted_header_decrypted_key))
        
        # Extract AES iv and key from decrypted header
        tree_header = ElementTree.fromstring(decrypted_header)
        header_iv = base64.urlsafe_b64decode(tree_header.findtext('.//iv'))
        header_key = base64.urlsafe_b64decode(tree_header.findtext('.//aes_key'))
        header_author = tree_header.findtext('.//author/name')
        header_author_handle = tree_header.findtext('.//author/uri')

        # Decrypt Salmon message
        envelope_data = self.filter_printable(aes_helper.decrypt(base64.urlsafe_b64decode(base64.urlsafe_b64decode(envelope_data_encrypted)),[header_key,header_iv]))

        return [header_author_handle,envelope_data]
Beispiel #2
0
        plain = "Testing encryption"
        cipher = rsa_helper.encrypt(plain,res[0])
        if cipher and cipher != plain:
            print " - Success"
            success += 1
        else:
            print " - Fail"
            fail +=1
    except:
        print " - Epic fail"
        fail +=1

if test_rsa or test_salmon:
    print "Decrypting message with private key using rsa_helper..."
    try:
        new_plain = rsa_helper.decrypt(cipher,res[1])
        if new_plain == plain:
            print " - Success"
            success += 1
        else:
            print " - Fail"
            fail +=1
    except:
        print " - Epic fail"
        fail +=1

if test_salmon:
    print "Creating Salmon XML..."
    try:
        salmon_obj = salmon.Salmon("Robin Nilsson","*****@*****.**",res[1],res[0],"Testing testing")
        if salmon_obj: