Exemplo n.º 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]
Exemplo n.º 2
0
Arquivo: test.py Projeto: Hexagon/Oden
        plain = "Testing encryption"
        cipher = aes_helper.encrypt(plain,res)
        if cipher and cipher != plain:
            print " - Success"
            success += 1
        else:
            print " - Fail"
            fail +=1
    except:
        print " - Epic fail"
        fail +=1

if test_aes:
    print "Decrypting message using aes_helper..."
    try:
        new_plain = aes_helper.decrypt(cipher,res)
        if new_plain.strip() == plain.strip():
            print " - Success"
            success += 1
        else:
            print " - Fail"
            fail +=1
    except:
        print " - Epic fail"
        fail +=1

# MongoDB People object add
if test_new_people or test_get_people_by_id:
    print "Creating a new people object..."
    try:
        tmp = people.People()