예제 #1
0
 def testF02EncryptPublicArmor(self):
     "encrypt_str()/decrypt_str() ElGamal via user ID (armored)"
     pubkey_d = read_test_file(['pgpfiles','key','DSAELG1.pub.asc'])
     seckey_d = read_test_file(['pgpfiles','key','DSAELG1.sec.asc'])
     cphtxt = encrypt_str(self.lit_data,keys=pubkey_d,
                          use_userid=[(None,"Tester")], armor=True)
     self.assertEqual(True, looks_armored(cphtxt))
예제 #2
0
 def testD03RSA(self):
     """crypto.cipher: decrypt_symmetric() v3 RSA secret key (RSA_d,p,q,u)"""
     rsasec_armored = read_test_file([
         'pgpfiles', 'interop', 'pgp6.5.3', 'RSA1',
         'key.pgp6.5.3.RSA1.sec.asc'
     ])
     rsanopass_armored = read_test_file([
         'pgpfiles', 'interop', 'pgp6.5.3', 'RSA1',
         'key.pgp6.5.3.RSA1.sec.nopass.asc'
     ])
     rsasec_d, rsanopass_d = list_armored(
         rsasec_armored)[0].data, list_armored(rsanopass_armored)[0].data
     pkts, nopasspkts = list_pkts(rsasec_d), list_pkts(rsanopass_d)
     secretkey_pkt, nopasskey_pkt = pkts[0], nopasspkts[0]
     secretkey, nopasskey = secretkey_pkt.body, nopasskey_pkt.body
     passphrase = 'test'
     # for the future..
     if secretkey.s2k_usg in [254, 255]:  # we have an s2k
         key = string2key(secretkey.s2k, secretkey.alg_sym, passphrase)
     else:
         import md5
         key = md5.new(passphrase).digest()
     # Just comparing bytes, not integer values. The funky notation
     # 'enc_RSA_d_d', 'enc_RSA_p_d' means "the encrypted *integer*
     # data from the RSA d and p # MPIs, respectively.
     # 'len_RSA_d_d' means "the octet length of the integer portion
     # of the RSA d MPI."
     idx = 0
     # RSA_d
     len_RSA_d_d = mpilen2int(secretkey._enc_d[idx:idx + 2])
     idx = idx + 2
     enc_RSA_d_d = secretkey._enc_d[idx:idx + len_RSA_d_d]
     idx = idx + len_RSA_d_d
     iv = secretkey.iv  # iv provided
     RSA_d_d = decrypt_symmetric(secretkey.alg_sym, key, enc_RSA_d_d, iv)
     self.assertEqual(RSA_d_d, nopasskey.RSA_d._int_d)
     # RSA_p
     len_RSA_p_d = mpilen2int(secretkey._enc_d[idx:idx + 2])
     idx = idx + 2
     enc_RSA_p_d = secretkey._enc_d[idx:idx + len_RSA_p_d]
     idx = idx + len_RSA_p_d
     iv = enc_RSA_d_d[-8:]  # last 8 octets from "pre-sync" instream
     RSA_p_d = decrypt_symmetric(secretkey.alg_sym, key, enc_RSA_p_d, iv)
     self.assertEqual(RSA_p_d, nopasskey.RSA_p._int_d)
     # RSA_q
     len_RSA_q_d = mpilen2int(secretkey._enc_d[idx:idx + 2])
     idx = idx + 2
     enc_RSA_q_d = secretkey._enc_d[idx:idx + len_RSA_q_d]
     idx = idx + len_RSA_q_d
     iv = enc_RSA_p_d[-8:]  # last 8 octets from "pre-sync" instream
     RSA_q_d = decrypt_symmetric(secretkey.alg_sym, key, enc_RSA_q_d, iv)
     self.assertEqual(RSA_q_d, nopasskey.RSA_q._int_d)
     # RSA_u
     len_RSA_u_d = mpilen2int(secretkey._enc_d[idx:idx + 2])
     idx = idx + 2
     enc_RSA_u_d = secretkey._enc_d[idx:idx + len_RSA_u_d]
     idx = idx + len_RSA_u_d
     iv = enc_RSA_q_d[-8:]  # last 8 octets from "pre-sync" instream
     RSA_u_d = decrypt_symmetric(secretkey.alg_sym, key, enc_RSA_u_d, iv)
     self.assertEqual(RSA_u_d, nopasskey.RSA_u._int_d)
예제 #3
0
 def testI01EncryptPublicElGamalDES3ZIP(self):
     """crypto.cipher: encrypt()/decrypt() ElGamal, DES3 w/ integrity, & ZIP"""
     key_d = read_test_file(['pgpfiles', 'key', 'DSAELG3.pub.gpg'])
     keypkt = list_pkts(key_d)[3]  # ElGamal encrypting key
     d = "My secret message."
     # literal data packet
     litbody = create_LiteralDataBody(data=d,
                                      modified=0,
                                      format='b',
                                      filename='outfile')
     litpkt = create_Packet(PKT_LITERAL, litbody._d)
     # compressed data packet
     compbody = create_CompressedDataBody(COMP_ZIP, litpkt.rawstr())
     comppkt = create_Packet(PKT_COMPRESSED, compbody._d)
     # session key
     key = gen_random(_keysize(SYM_DES3))
     sespkt = encrypt_public_session(keypkt, key, SYM_DES3)
     # encrypted data
     encpkt = encrypt_integrity(SYM_DES3, key, comppkt.rawstr())
     # decryption
     seckey_d = read_test_file(
         ['pgpfiles', 'key', 'DSAELG3.sec.nopass.gpg'])
     seckeypkt = list_pkts(seckey_d)[2]
     clrtxt = decrypt(encpkt, None, sespkt, seckeypkt)
     # got compressed
     comppkt_out = list_pkts(clrtxt)[0]
     # got literal
     litpkt_out = list_pkts(comppkt_out.body.data)[0]
     self.assertEqual(d, litpkt_out.body.data)
     self.assertEqual('outfile', litpkt_out.body.filename)
     self.assertEqual(0, litpkt_out.body.modified)
     self.assertEqual('b', litpkt_out.body.format)
예제 #4
0
 def testI01EncryptPublicElGamalDES3ZIP(self):
     """crypto.cipher: encrypt()/decrypt() ElGamal, DES3 w/ integrity, & ZIP"""
     key_d = read_test_file(['pgpfiles','key','DSAELG3.pub.gpg'])
     keypkt = list_pkts(key_d)[3] # ElGamal encrypting key
     d = "My secret message."
     # literal data packet
     litbody = create_LiteralDataBody(data=d, modified=0, format='b',
                                          filename='outfile')
     litpkt = create_Packet(PKT_LITERAL, litbody._d)
     # compressed data packet
     compbody = create_CompressedDataBody(COMP_ZIP, litpkt.rawstr())
     comppkt = create_Packet(PKT_COMPRESSED, compbody._d)
     # session key
     key = gen_random(_keysize(SYM_DES3))
     sespkt = encrypt_public_session(keypkt, key, SYM_DES3)
     # encrypted data
     encpkt = encrypt_integrity(SYM_DES3, key, comppkt.rawstr())
     # decryption
     seckey_d = read_test_file(['pgpfiles','key','DSAELG3.sec.nopass.gpg'])
     seckeypkt = list_pkts(seckey_d)[2]
     clrtxt = decrypt(encpkt, None, sespkt, seckeypkt)
     # got compressed
     comppkt_out = list_pkts(clrtxt)[0]
     # got literal
     litpkt_out = list_pkts(comppkt_out.body.data)[0]
     self.assertEqual(d, litpkt_out.body.data)
     self.assertEqual('outfile', litpkt_out.body.filename)
     self.assertEqual(0, litpkt_out.body.modified)
     self.assertEqual('b', litpkt_out.body.format)
예제 #5
0
 def testC02DecryptDSA_decompression(self):
     "decrypt_str() DSA AES256 w/integrity (decompressed)"
     enc_d = read_test_file(['pgpfiles','enc','pub.elg.aes256.clrtxt.gpg'])
     key_d = read_test_file(['pgpfiles','key','DSAELG1.sec.asc'])
     clrtxt = decrypt_str(enc_d, passphrase='test', keys=key_d,
                                  decompress=True)
     litmsg = list_as_signed(clrtxt)[0] # no compressed msg to go through
     self.assertEqual(self.lit_data, litmsg.literals[0].body.data)
예제 #6
0
 def testC01DecryptDSA(self):
     "decrypt_str() DSA AES256 w/integrity"
     enc_d = read_test_file(['pgpfiles','enc','pub.elg.aes256.clrtxt.gpg'])
     key_d = read_test_file(['pgpfiles','key','DSAELG1.sec.asc'])
     clrtxt = decrypt_str(enc_d, passphrase='test', keys=key_d)
     compmsg = list_as_signed(clrtxt)[0]
     literal_pkt = list_pkts(compmsg.compressed.body.data)[0]
     self.assertEqual(self.lit_data, literal_pkt.body.data)
예제 #7
0
 def testF03EncryptPublicKeyID(self):
     "encrypt_str()/decrypt_str() ElGamal via key ID"
     pubkey_d = read_test_file(['pgpfiles','key','DSAELG1.pub.asc'])
     seckey_d = read_test_file(['pgpfiles','key','DSAELG1.sec.asc'])
     cphtxt = encrypt_str(self.lit_data, keys=pubkey_d, use_key=[(None,"CB7D6980A1F2BEF6")])
     clrtxt = decrypt_str(cphtxt, passphrase="test", keys=seckey_d, decompress=True)
     litmsg = list_as_signed(clrtxt)[0] # auto-decompressed above
     self.assertEqual(self.lit_data, litmsg.literals[0].body.data)
예제 #8
0
 def testC03DecryptDSA_armor(self):
     "decrypt_str() DSA AES256 w/integrity (armored, decompressed)"
     enc_d = read_test_file(['pgpfiles','enc','pub.elg.aes256.clrtxt.gpg'])
     key_d = read_test_file(['pgpfiles','key','DSAELG1.sec.asc'])
     clrtxt = decrypt_str(enc_d, passphrase='test', keys=key_d,
                                  decompress=True, armor=True)
     self.assertEqual(True, looks_armored(clrtxt)) # check armoring
     litmsg = list_as_signed(clrtxt)[0]
     self.assertEqual(self.lit_data, litmsg.literals[0].body.data)
예제 #9
0
 def testC04DecryptRSA(self):
     "decrypt_str() RSA w/integrity"
     lit_data = "My secret message."
     enc_d = read_test_file(['pgpfiles','enc','sap.2DSAELG1RSA.nocomp.gpg'])
     key_d = read_test_file(['pgpfiles','key','RSA1.sec.asc'])
     clrtxt = decrypt_str(enc_d, passphrase='test', keys=key_d)
     clrmsgs = list_as_signed(clrtxt)
     literal_pkt = clrmsgs[0].literals[0]
     self.assertEqual(lit_data, literal_pkt.body.data)
예제 #10
0
 def testC06DecryptSymmetricCASTMessageFailure(self):
     "decrypt_str() symmetric decryption failure (wrong password)"
     lit_d = read_test_file(['pgpfiles', 'cleartext.txt'])
     enc_d = read_test_file(
         ['pgpfiles', 'enc', 'sym.cast.cleartext.txt.gpg'])
     self.assertRaises(PGPCryptoError,
                       decrypt_str,
                       enc_d,
                       passphrase='badpassword')
예제 #11
0
 def testA05DSADetached(self):
     "list: list_as_signed() DSA detached signature"
     d = read_test_file(['pgpfiles', 'sig', 'sig.DSAELG1.detached.gpg'])
     det_d = read_test_file(['pgpfiles', 'cleartext.txt'])
     players = list_as_signed(d, detached=det_d)
     # looking for players = [([sig], msg)]
     self.assertEqual(Signature, players[0][0][0].__class__)
     self.assertEqual(True, isinstance(players[0][1], str))
     self.assertEqual(1, len(players))
예제 #12
0
 def testC01DecryptDSA(self):
     "decrypt_str() DSA AES256 w/integrity"
     enc_d = read_test_file(
         ['pgpfiles', 'enc', 'pub.elg.aes256.clrtxt.gpg'])
     key_d = read_test_file(['pgpfiles', 'key', 'DSAELG1.sec.asc'])
     clrtxt = decrypt_str(enc_d, passphrase='test', keys=key_d)
     compmsg = list_as_signed(clrtxt)[0]
     literal_pkt = list_pkts(compmsg.compressed.body.data)[0]
     self.assertEqual(self.lit_data, literal_pkt.body.data)
예제 #13
0
 def testF02EncryptPublicArmor(self):
     "encrypt_str()/decrypt_str() ElGamal via user ID (armored)"
     pubkey_d = read_test_file(['pgpfiles', 'key', 'DSAELG1.pub.asc'])
     seckey_d = read_test_file(['pgpfiles', 'key', 'DSAELG1.sec.asc'])
     cphtxt = encrypt_str(self.lit_data,
                          keys=pubkey_d,
                          use_userid=[(None, "Tester")],
                          armor=True)
     self.assertEqual(True, looks_armored(cphtxt))
예제 #14
0
 def testC04DecryptRSA(self):
     "decrypt_str() RSA w/integrity"
     lit_data = "My secret message."
     enc_d = read_test_file(
         ['pgpfiles', 'enc', 'sap.2DSAELG1RSA.nocomp.gpg'])
     key_d = read_test_file(['pgpfiles', 'key', 'RSA1.sec.asc'])
     clrtxt = decrypt_str(enc_d, passphrase='test', keys=key_d)
     clrmsgs = list_as_signed(clrtxt)
     literal_pkt = clrmsgs[0].literals[0]
     self.assertEqual(lit_data, literal_pkt.body.data)
예제 #15
0
 def testF04EncryptPublicKeyIDNoTarget(self):
     "encrypt_str()/decrypt_str() no specified encryption key"
     pubkey_d = read_test_file(['pgpfiles','key','DSAELG1.pub.asc'])
     seckey_d = read_test_file(['pgpfiles','key','DSAELG1.sec.asc'])
     try:
         cphtxt = encrypt_str(self.lit_data, keys=pubkey_d) # missing target
     except PGPError:
         pass
     else:
         self.fail()
예제 #16
0
 def testE01SignBinaryAsMsgViaUID(self):
     "sign_str()/verify_str() binary (0x00) using user ID"
     lit_d = read_test_file(['pgpfiles','cleartext.txt'])
     seckey_d = read_test_file(['pgpfiles','key','DSAELG1.sec.asc'])
     pubkey_d = read_test_file(['pgpfiles','key','DSAELG1.pub.asc'])
     signed = sign_str(0x00, seckey_d, target=lit_d,
                       use_userid=(None,'Tester'),
                       passphrase='test')
     verified = verify_str(signed, pubkey_d)
     self.assertEqual(lit_d, list_pkts(verified)[0].body.data)
예제 #17
0
 def testD08CertificationRevocation(self):
     "crypto.signature: verify() foreign user ID certification"
     key_d = read_test_file(['pgpfiles','key','DSAELG2.pub.foreign_uid_cert.gpg'])
     sigkey_d = read_test_file(['pgpfiles','key','RSA1.pub.gpg'])
     sigpkt = list_pkts(sigkey_d)[0]
     pkts = list_pkts(key_d)
     primary_key, uid, cert = pkts[0], pkts[1], pkts[3]
     opts = {'primary':primary_key}
     verified = verify(cert, uid, sigpkt, **opts)
     self.assertEqual(True, verified)
예제 #18
0
 def testB03MultiplePreferencesSameValues(self):
     """sap.api: find_key_prefs() two keys, same values"""
     key1_d = read_test_file(['pgpfiles', 'key', 'DSAELG1.pub.gpg'])
     keymsg1 = list_msgs(list_pkts(key1_d))[0]
     key2_d = read_test_file(['pgpfiles', 'key', 'RSA1.pub.gpg'])
     keymsg2 = list_msgs(list_pkts(key2_d))[0]
     preferred = find_key_prefs([keymsg1, keymsg2])
     self.assertEqual(preferred['sym'], [9, 8, 7, 3, 2])
     self.assertEqual(preferred['hash'], [2, 3])
     self.assertEqual(preferred['comp'], [2, 1])
예제 #19
0
 def testE02SignTextKeyID(self):
     "sign_str()/verify_str() text (0x01) using key ID"
     lit_d = read_test_file(['pgpfiles','cleartext.txt'])
     seckey_d = read_test_file(['pgpfiles','key','DSAELG1.sec.asc'])
     pubkey_d = read_test_file(['pgpfiles','key','DSAELG1.pub.asc'])
     signed = sign_str(0x01, seckey_d, target=lit_d,
                       use_key=(None, '0CFC2B6DCC079DF3'),
                       passphrase='test')
     verified = verify_str(signed, pubkey_d)
     self.assertEqual(lit_d, list_pkts(verified)[0].body.data)
예제 #20
0
 def testG05Decrypt5(self):
     """crypto.cipher: decrypt() symmetric (CAST) compressed"""
     enc_d = read_test_file(['pgpfiles','enc','sym.cast.cleartext.txt.gpg'])
     clr_d = read_test_file(['pgpfiles','cleartext.txt'])
     sespkt, encpkt = list_pkts(enc_d)
     passphrase = 'test'
     msg_d = decrypt(encpkt, passphrase, sespkt)
     compmsg = list_msgs(list_pkts(msg_d))[0]
     litpkt = list_pkts(compmsg.compressed.body.data)[0]
     self.assertEqual(clr_d, litpkt.body.data)
예제 #21
0
 def testG05Decrypt5(self):
     """crypto.cipher: decrypt() symmetric (CAST) compressed"""
     enc_d = read_test_file(
         ['pgpfiles', 'enc', 'sym.cast.cleartext.txt.gpg'])
     clr_d = read_test_file(['pgpfiles', 'cleartext.txt'])
     sespkt, encpkt = list_pkts(enc_d)
     passphrase = 'test'
     msg_d = decrypt(encpkt, passphrase, sespkt)
     compmsg = list_msgs(list_pkts(msg_d))[0]
     litpkt = list_pkts(compmsg.compressed.body.data)[0]
     self.assertEqual(clr_d, litpkt.body.data)
예제 #22
0
 def testF04EncryptPublicKeyIDNoTarget(self):
     "encrypt_str()/decrypt_str() no specified encryption key"
     pubkey_d = read_test_file(['pgpfiles', 'key', 'DSAELG1.pub.asc'])
     seckey_d = read_test_file(['pgpfiles', 'key', 'DSAELG1.sec.asc'])
     try:
         cphtxt = encrypt_str(self.lit_data,
                              keys=pubkey_d)  # missing target
     except PGPError:
         pass
     else:
         self.fail()
예제 #23
0
 def testC07DecryptPublicSessionFailure(self):
     "decrypt_str() public session decryption failure (wrong password)"
     lit_d = read_test_file(['pgpfiles', 'cleartext.txt'])
     enc_d = read_test_file(
         ['pgpfiles', 'enc', 'pub.elg.aes256.clrtxt.gpg'])
     key_d = read_test_file(['pgpfiles', 'key', 'DSAELG1.sec.asc'])
     self.assertRaises(PGPCryptoError,
                       decrypt_str,
                       enc_d,
                       keys=key_d,
                       passphrase='badpassword')
예제 #24
0
 def testC02DecryptDSA_decompression(self):
     "decrypt_str() DSA AES256 w/integrity (decompressed)"
     enc_d = read_test_file(
         ['pgpfiles', 'enc', 'pub.elg.aes256.clrtxt.gpg'])
     key_d = read_test_file(['pgpfiles', 'key', 'DSAELG1.sec.asc'])
     clrtxt = decrypt_str(enc_d,
                          passphrase='test',
                          keys=key_d,
                          decompress=True)
     litmsg = list_as_signed(clrtxt)[0]  # no compressed msg to go through
     self.assertEqual(self.lit_data, litmsg.literals[0].body.data)
예제 #25
0
 def testF01ElGamal(self):
     """crypto.cipher: decrypt_public() ElGamal-encrypted session key"""
     #encmsg_d = read_test_file(['pgpfiles','enc','pub.elg.aes256.clrtxt.gpg'])
     encmsg_d = read_test_file(
         ['pgpfiles', 'enc', 'pub.elg.cast.clrtxt.gpg'])
     encmsg = list_msgs(list_pkts(encmsg_d))[0]
     seskeypkt = encmsg.targets[0]  # target = session key
     # get target secret key (in this case, a subkey)
     keymsg_d = read_test_file(
         ['pgpfiles', 'key', 'DSAELG1.sec.nopass.gpg'])
     keymsg = list_msgs(list_pkts(keymsg_d))[0]
     for block in keymsg._b_subkeys.list():
         subkey = block.leader
         if seskeypkt.body.keyid == subkey.body.id:
             break  # leave with the appropriate subkey
     # I know the target in question is an ElGamal key
     key_tuple = (subkey.body.ELGAMAL_p.value, subkey.body.ELGAMAL_x.value)
     cipher_tuple = (seskeypkt.body.ELGAMAL_gk_modp.value,
                     seskeypkt.body.ELGAMAL_myk_modp.value)
     # retrieving the PKCS, etc., padded session key
     padded_message = decrypt_public(seskeypkt.body.alg_pubkey, key_tuple,
                                     cipher_tuple)
     # Rules from rfc 2437 9.1.2.2:
     # 1. Padding starts with 0x02.
     # 2. Padding continues with >= 8 octets non-zero gibberish.
     # 3. Gibberish concludes with 0x00.
     # 4. Remaining data is the message.
     if '\x02' == padded_message[0]:
         idx = padded_message.find('\x00')
         if -1 != idx and 8 <= idx:
             message = padded_message[idx + 1:]
             chksum = str2int(message[-2:])
             ciphertype = str2int(message[0])
             key = message[1:len(message) - 2]
             i = 0
             for e in key:
                 i += str2int(e)
             # TODO check chksum
             cleartext = decrypt_symmetric(ciphertype, key,
                                           encmsg.encrypted.body.data)
         else:
             errmsg = "Misplaced \\x00 in session key padding, located at index->(%s)" % idx
     else:
         errmsg = "Session key padding must start with \\x02, received->()" % hex(
             ord(padded_message[0]))
     cleartext = cleartext[10:]  # ditching the prefix + 2
     # result is a compressed message..
     compressedmsg = list_msgs(list_pkts(cleartext))[0]
     newmsgs = list_msgs(list_pkts(compressedmsg.compressed.body.data))
     # ..with only one literal data packet
     literal_data = newmsgs[0].literals[0].body.data
     # compare against original file
     clrtxt = read_test_file(['pgpfiles', 'cleartext.txt'])
     self.assertEqual(literal_data, clrtxt)
예제 #26
0
 def testD08CertificationRevocation(self):
     "crypto.signature: verify() foreign user ID certification"
     key_d = read_test_file(
         ['pgpfiles', 'key', 'DSAELG2.pub.foreign_uid_cert.gpg'])
     sigkey_d = read_test_file(['pgpfiles', 'key', 'RSA1.pub.gpg'])
     sigpkt = list_pkts(sigkey_d)[0]
     pkts = list_pkts(key_d)
     primary_key, uid, cert = pkts[0], pkts[1], pkts[3]
     opts = {'primary': primary_key}
     verified = verify(cert, uid, sigpkt, **opts)
     self.assertEqual(True, verified)
예제 #27
0
 def testD01DSASecretKey(self):
     """crypto.cipher: decrypt_symmetric() v4 DSA secret key (DSA_x)"""
     dsasec_d = read_test_file(['pgpfiles','key','DSAELG1.sec.gpg'])
     dsasecnopass_d = read_test_file(['pgpfiles','key','DSAELG1.sec.nopass.gpg'])
     pkts, nopasspkts = list_pkts(dsasec_d), list_pkts(dsasecnopass_d)
     secretkey_pkt, nopasskey_pkt = pkts[0], nopasspkts[0]
     secretkey, nopasskey = secretkey_pkt.body, nopasskey_pkt.body
     passphrase = 'test'
     key = string2key(secretkey.s2k, secretkey.alg_sym, passphrase)
     result = decrypt_symmetric(secretkey.alg_sym, key, secretkey._enc_d, secretkey.iv)
     self.assertEqual(nopasskey.DSA_x._d, result[:22])
예제 #28
0
 def testE01SignBinaryAsMsgViaUID(self):
     "sign_str()/verify_str() binary (0x00) using user ID"
     lit_d = read_test_file(['pgpfiles', 'cleartext.txt'])
     seckey_d = read_test_file(['pgpfiles', 'key', 'DSAELG1.sec.asc'])
     pubkey_d = read_test_file(['pgpfiles', 'key', 'DSAELG1.pub.asc'])
     signed = sign_str(0x00,
                       seckey_d,
                       target=lit_d,
                       use_userid=(None, 'Tester'),
                       passphrase='test')
     verified = verify_str(signed, pubkey_d)
     self.assertEqual(lit_d, list_pkts(verified)[0].body.data)
예제 #29
0
 def testC08DecryptWrongKey(self):
     "decrypt_str() public decryption failure (wrong decryption key)"
     lit_d = read_test_file(['pgpfiles', 'cleartext.txt'])
     enc_d = read_test_file(
         ['pgpfiles', 'enc', 'pub.elg.aes256.clrtxt.gpg'])
     key_d = read_test_file(['pgpfiles', 'key',
                             'DSAELG3.sec.asc'])  # wrong key
     self.assertRaises(PGPCryptoError,
                       decrypt_str,
                       enc_d,
                       keys=key_d,
                       passphrase='badpassword')
예제 #30
0
 def testE02SignTextKeyID(self):
     "sign_str()/verify_str() text (0x01) using key ID"
     lit_d = read_test_file(['pgpfiles', 'cleartext.txt'])
     seckey_d = read_test_file(['pgpfiles', 'key', 'DSAELG1.sec.asc'])
     pubkey_d = read_test_file(['pgpfiles', 'key', 'DSAELG1.pub.asc'])
     signed = sign_str(0x01,
                       seckey_d,
                       target=lit_d,
                       use_key=(None, '0CFC2B6DCC079DF3'),
                       passphrase='test')
     verified = verify_str(signed, pubkey_d)
     self.assertEqual(lit_d, list_pkts(verified)[0].body.data)
예제 #31
0
 def testD03RSA(self):
     """crypto.cipher: decrypt_symmetric() v3 RSA secret key (RSA_d,p,q,u)"""
     rsasec_armored = read_test_file(['pgpfiles','interop','pgp6.5.3','RSA1','key.pgp6.5.3.RSA1.sec.asc'])
     rsanopass_armored = read_test_file(['pgpfiles','interop','pgp6.5.3','RSA1','key.pgp6.5.3.RSA1.sec.nopass.asc'])
     rsasec_d, rsanopass_d = list_armored(rsasec_armored)[0].data, list_armored(rsanopass_armored)[0].data
     pkts, nopasspkts = list_pkts(rsasec_d), list_pkts(rsanopass_d)
     secretkey_pkt, nopasskey_pkt = pkts[0], nopasspkts[0]
     secretkey, nopasskey = secretkey_pkt.body, nopasskey_pkt.body
     passphrase = 'test'
     # for the future..
     if secretkey.s2k_usg in [254, 255]: # we have an s2k
         key = string2key(secretkey.s2k, secretkey.alg_sym, passphrase)
     else:
         import md5
         key = md5.new(passphrase).digest()
     # Just comparing bytes, not integer values. The funky notation
     # 'enc_RSA_d_d', 'enc_RSA_p_d' means "the encrypted *integer*
     # data from the RSA d and p # MPIs, respectively.
     # 'len_RSA_d_d' means "the octet length of the integer portion
     # of the RSA d MPI."
     idx = 0
     # RSA_d
     len_RSA_d_d = mpilen2int(secretkey._enc_d[idx:idx+2])
     idx = idx + 2
     enc_RSA_d_d = secretkey._enc_d[idx:idx+len_RSA_d_d]
     idx = idx + len_RSA_d_d
     iv = secretkey.iv # iv provided
     RSA_d_d = decrypt_symmetric(secretkey.alg_sym, key, enc_RSA_d_d, iv)
     self.assertEqual(RSA_d_d, nopasskey.RSA_d._int_d)
     # RSA_p
     len_RSA_p_d = mpilen2int(secretkey._enc_d[idx:idx+2])
     idx = idx + 2
     enc_RSA_p_d = secretkey._enc_d[idx:idx+len_RSA_p_d]
     idx = idx + len_RSA_p_d
     iv = enc_RSA_d_d[-8:] # last 8 octets from "pre-sync" instream
     RSA_p_d = decrypt_symmetric(secretkey.alg_sym, key, enc_RSA_p_d, iv)
     self.assertEqual(RSA_p_d, nopasskey.RSA_p._int_d)
     # RSA_q
     len_RSA_q_d = mpilen2int(secretkey._enc_d[idx:idx+2])
     idx = idx + 2
     enc_RSA_q_d = secretkey._enc_d[idx:idx+len_RSA_q_d]
     idx = idx + len_RSA_q_d
     iv = enc_RSA_p_d[-8:] # last 8 octets from "pre-sync" instream
     RSA_q_d = decrypt_symmetric(secretkey.alg_sym, key, enc_RSA_q_d, iv)
     self.assertEqual(RSA_q_d, nopasskey.RSA_q._int_d)
     # RSA_u
     len_RSA_u_d = mpilen2int(secretkey._enc_d[idx:idx+2])
     idx = idx + 2
     enc_RSA_u_d = secretkey._enc_d[idx:idx+len_RSA_u_d]
     idx = idx + len_RSA_u_d
     iv = enc_RSA_q_d[-8:] # last 8 octets from "pre-sync" instream
     RSA_u_d = decrypt_symmetric(secretkey.alg_sym, key, enc_RSA_u_d, iv)
     self.assertEqual(RSA_u_d, nopasskey.RSA_u._int_d)
예제 #32
0
 def testH01EncryptPublicElGamalDES3(self):
     """crypto.cipher: encrypt()/decrypt() ElGamal, DES3 w/ integrity"""
     key_d = read_test_file(['pgpfiles','key','DSAELG3.pub.gpg'])
     keypkt = list_pkts(key_d)[3] # ElGamal encrypting key
     msg = "My secret message."
     key = gen_random(_keysize(SYM_DES3))
     sespkt = encrypt_public_session(keypkt, key, SYM_DES3)
     encpkt = encrypt_integrity(SYM_DES3, key, msg)
     seckey_d = read_test_file(['pgpfiles','key','DSAELG3.sec.nopass.gpg'])
     seckeypkt = list_pkts(seckey_d)[2]
     clrtxt = decrypt(encpkt, None, sespkt, seckeypkt)
     self.assertEqual(msg, clrtxt)
예제 #33
0
 def testD02RSASecretKey(self):
     """crypto.cipher: decrypt_symmetric() v4 RSA secret key (RSA_d,p,q,u)"""
     rsasec_d = read_test_file(['pgpfiles','key','RSA1.sec.gpg'])
     rsasecnopass_d = read_test_file(['pgpfiles','key','RSA1.sec.nopass.gpg'])
     pkts, nopasspkts = list_pkts(rsasec_d), list_pkts(rsasecnopass_d)
     secretkey_pkt, nopasskey_pkt = pkts[0], nopasspkts[0]
     secretkey, nopasskey = secretkey_pkt.body, nopasskey_pkt.body
     passphrase = 'test'
     key = string2key(secretkey.s2k, secretkey.alg_sym, passphrase)
     result = decrypt_symmetric(secretkey.alg_sym, key, secretkey._enc_d, secretkey.iv)
     good_mpi_d = nopasskey.RSA_d._d + nopasskey.RSA_p._d + nopasskey.RSA_q._d + nopasskey.RSA_u._d
     self.assertEqual(good_mpi_d, result[:328])
예제 #34
0
 def testF03DSADirectForeign(self):
     "crypto.signature: sign() v4 DSA direct on foreign primary key"
     targetkey_d = read_test_file(['pgpfiles','key','DSAELG3.pub.gpg'])
     seckey_d = read_test_file(['pgpfiles','key','DSAELG1.sec.gpg'])
     targetkeypkt = list_pkts(targetkey_d)[0]
     seckeypkt = list_pkts(seckey_d)[0]
     sigtype = SIG_DIRECT
     opts = {'passphrase':'test', 'primary':targetkeypkt}
     sigpkt = sign(sigtype, targetkeypkt, seckeypkt, **opts)
     verified = verify(sigpkt, targetkeypkt, seckeypkt,
                           primary=targetkeypkt)
     self.assertEqual(1, verified)
예제 #35
0
 def testF03EncryptPublicKeyID(self):
     "encrypt_str()/decrypt_str() ElGamal via key ID"
     pubkey_d = read_test_file(['pgpfiles', 'key', 'DSAELG1.pub.asc'])
     seckey_d = read_test_file(['pgpfiles', 'key', 'DSAELG1.sec.asc'])
     cphtxt = encrypt_str(self.lit_data,
                          keys=pubkey_d,
                          use_key=[(None, "CB7D6980A1F2BEF6")])
     clrtxt = decrypt_str(cphtxt,
                          passphrase="test",
                          keys=seckey_d,
                          decompress=True)
     litmsg = list_as_signed(clrtxt)[0]  # auto-decompressed above
     self.assertEqual(self.lit_data, litmsg.literals[0].body.data)
예제 #36
0
 def testC03DecryptDSA_armor(self):
     "decrypt_str() DSA AES256 w/integrity (armored, decompressed)"
     enc_d = read_test_file(
         ['pgpfiles', 'enc', 'pub.elg.aes256.clrtxt.gpg'])
     key_d = read_test_file(['pgpfiles', 'key', 'DSAELG1.sec.asc'])
     clrtxt = decrypt_str(enc_d,
                          passphrase='test',
                          keys=key_d,
                          decompress=True,
                          armor=True)
     self.assertEqual(True, looks_armored(clrtxt))  # check armoring
     litmsg = list_as_signed(clrtxt)[0]
     self.assertEqual(self.lit_data, litmsg.literals[0].body.data)
예제 #37
0
 def testG03Decrypt3(self):
     """crypto.cipher: decrypt() v3 public (RSA nopass) 3DES compressed"""
     enc_d = read_test_file(['pgpfiles','enc','pub.pgp653rsa.clrtxt.gpg'])
     key_d = read_test_file(['pgpfiles','key','pgp653rsa.sec.nopass.gpg'])
     clr_d = read_test_file(['pgpfiles','cleartext.txt'])
     sespkt, encpkt = list_pkts(enc_d)
     passphrase = 'test'
     sespkt, encpkt = list_pkts(enc_d)
     keypkt = list_pkts(key_d)[0]
     msg_d = decrypt(encpkt, passphrase, sespkt, keypkt)
     compmsg = list_msgs(list_pkts(msg_d))[0]
     litpkt = list_pkts(compmsg.compressed.body.data)[0]
     self.assertEqual(clr_d, litpkt.body.data)
예제 #38
0
 def testD01DSASecretKey(self):
     """crypto.cipher: decrypt_symmetric() v4 DSA secret key (DSA_x)"""
     dsasec_d = read_test_file(['pgpfiles', 'key', 'DSAELG1.sec.gpg'])
     dsasecnopass_d = read_test_file(
         ['pgpfiles', 'key', 'DSAELG1.sec.nopass.gpg'])
     pkts, nopasspkts = list_pkts(dsasec_d), list_pkts(dsasecnopass_d)
     secretkey_pkt, nopasskey_pkt = pkts[0], nopasspkts[0]
     secretkey, nopasskey = secretkey_pkt.body, nopasskey_pkt.body
     passphrase = 'test'
     key = string2key(secretkey.s2k, secretkey.alg_sym, passphrase)
     result = decrypt_symmetric(secretkey.alg_sym, key, secretkey._enc_d,
                                secretkey.iv)
     self.assertEqual(nopasskey.DSA_x._d, result[:22])
예제 #39
0
 def testH01EncryptPublicElGamalDES3(self):
     """crypto.cipher: encrypt()/decrypt() ElGamal, DES3 w/ integrity"""
     key_d = read_test_file(['pgpfiles', 'key', 'DSAELG3.pub.gpg'])
     keypkt = list_pkts(key_d)[3]  # ElGamal encrypting key
     msg = "My secret message."
     key = gen_random(_keysize(SYM_DES3))
     sespkt = encrypt_public_session(keypkt, key, SYM_DES3)
     encpkt = encrypt_integrity(SYM_DES3, key, msg)
     seckey_d = read_test_file(
         ['pgpfiles', 'key', 'DSAELG3.sec.nopass.gpg'])
     seckeypkt = list_pkts(seckey_d)[2]
     clrtxt = decrypt(encpkt, None, sespkt, seckeypkt)
     self.assertEqual(msg, clrtxt)
예제 #40
0
 def testE08SignSubkeyBadSigner(self):
     "sign_str() subkey direct (0x1F) target not found"
     seckey_d = read_test_file(['pgpfiles','key','DSAELG1.sec.asc'])
     pubkey_d = read_test_file(['pgpfiles','key','DSAELG1.pub.asc'])
     try:
         sigpkt = sign_str(0x1F, seckey_d, target=pubkey_d,
                           use_key=(None, "0CFC2B6DCC079xxx"), # munged
                           target_key=(None,"CB7D6980A1F2BEF6"),
                           passphrase='test')
     except PGPError: # signing key is specified so it must be used
         pass
     else:
         self.fail()
예제 #41
0
 def testG03Decrypt3(self):
     """crypto.cipher: decrypt() v3 public (RSA nopass) 3DES compressed"""
     enc_d = read_test_file(['pgpfiles', 'enc', 'pub.pgp653rsa.clrtxt.gpg'])
     key_d = read_test_file(['pgpfiles', 'key', 'pgp653rsa.sec.nopass.gpg'])
     clr_d = read_test_file(['pgpfiles', 'cleartext.txt'])
     sespkt, encpkt = list_pkts(enc_d)
     passphrase = 'test'
     sespkt, encpkt = list_pkts(enc_d)
     keypkt = list_pkts(key_d)[0]
     msg_d = decrypt(encpkt, passphrase, sespkt, keypkt)
     compmsg = list_msgs(list_pkts(msg_d))[0]
     litpkt = list_pkts(compmsg.compressed.body.data)[0]
     self.assertEqual(clr_d, litpkt.body.data)
예제 #42
0
 def testB04MultiplePreferencesDifferentValues(self):
     """sap.api: find_key_prefs() two keys, different values"""
     key1_d = read_test_file(['pgpfiles', 'key', 'DSAELG1.pub.gpg'])
     # sym[9, 8, 7, 3, 2], hash[2, 3], comp[2, 1]
     keymsg1 = list_msgs(list_pkts(key1_d))[0]
     key2_d = read_test_file(
         ['pgpfiles', 'interop', 'pgp8.0.2', 'key.pgp8.0.2.DHDSS1.pub.asc'])
     d = list_armored(key2_d)[0].data
     # sym[9, 8, 7, 3, 2, 10], empty hash, empty comp
     keymsg2 = list_msgs(list_pkts(d))[0]
     preferred = find_key_prefs([keymsg1, keymsg2])
     self.assertEqual(preferred['sym'], [9, 8, 7, 3, 2])
     self.assertEqual(preferred['hash'], [])
     self.assertEqual(preferred['comp'], [])
예제 #43
0
 def testG02Decrypt2(self):
     """crypto.cipher: decrypt() v4 public (ElGamal) MDC AES256 compressed"""
     # This message is known to have a compressed packet containing a literal
     # packet containing the text from cleartext.txt.
     enc_d = read_test_file(['pgpfiles','enc','pub.elg.aes256.clrtxt.gpg'])
     key_d = read_test_file(['pgpfiles','key','DSAELG1.sec.gpg'])
     clr_d = read_test_file(['pgpfiles','cleartext.txt'])
     sespkt, encpkt = list_pkts(enc_d)
     keypkt = list_pkts(key_d)[3] # first encryption subkey (known)
     passphrase = 'test'
     msg_d = decrypt(encpkt, passphrase, sespkt, keypkt)
     compmsg = list_msgs(list_pkts(msg_d))[0]
     litpkt = list_pkts(compmsg.compressed.body.data)[0]
     self.assertEqual(clr_d, litpkt.body.data)
예제 #44
0
 def testD02RSASecretKey(self):
     """crypto.cipher: decrypt_symmetric() v4 RSA secret key (RSA_d,p,q,u)"""
     rsasec_d = read_test_file(['pgpfiles', 'key', 'RSA1.sec.gpg'])
     rsasecnopass_d = read_test_file(
         ['pgpfiles', 'key', 'RSA1.sec.nopass.gpg'])
     pkts, nopasspkts = list_pkts(rsasec_d), list_pkts(rsasecnopass_d)
     secretkey_pkt, nopasskey_pkt = pkts[0], nopasspkts[0]
     secretkey, nopasskey = secretkey_pkt.body, nopasskey_pkt.body
     passphrase = 'test'
     key = string2key(secretkey.s2k, secretkey.alg_sym, passphrase)
     result = decrypt_symmetric(secretkey.alg_sym, key, secretkey._enc_d,
                                secretkey.iv)
     good_mpi_d = nopasskey.RSA_d._d + nopasskey.RSA_p._d + nopasskey.RSA_q._d + nopasskey.RSA_u._d
     self.assertEqual(good_mpi_d, result[:328])
예제 #45
0
 def testH02EncryptPublicRSAAES256(self):
     """crypto.cipher: encrypt()/decrypt() RSA, AES256 w/ integrity"""
     key_d = read_test_file(['pgpfiles','key','RSA1.pub.gpg'])
     keypkt = list_pkts(key_d)[0] # use RSA for encrypting
     alg = SYM_AES256
     msg = "My secret message."
     passphrase = 'test'
     key = gen_random(_keysize(alg))
     sespkt = encrypt_public_session(keypkt, key, alg)
     encpkt = encrypt_integrity(alg, key, msg)
     seckey_d = read_test_file(['pgpfiles','key','RSA1.sec.gpg'])
     seckeypkt = list_pkts(seckey_d)[0]
     clrtxt = decrypt(encpkt, passphrase, sespkt, seckeypkt)
     self.assertEqual(msg, clrtxt)
예제 #46
0
 def testH02EncryptPublicRSAAES256(self):
     """crypto.cipher: encrypt()/decrypt() RSA, AES256 w/ integrity"""
     key_d = read_test_file(['pgpfiles', 'key', 'RSA1.pub.gpg'])
     keypkt = list_pkts(key_d)[0]  # use RSA for encrypting
     alg = SYM_AES256
     msg = "My secret message."
     passphrase = 'test'
     key = gen_random(_keysize(alg))
     sespkt = encrypt_public_session(keypkt, key, alg)
     encpkt = encrypt_integrity(alg, key, msg)
     seckey_d = read_test_file(['pgpfiles', 'key', 'RSA1.sec.gpg'])
     seckeypkt = list_pkts(seckey_d)[0]
     clrtxt = decrypt(encpkt, passphrase, sespkt, seckeypkt)
     self.assertEqual(msg, clrtxt)
예제 #47
0
 def testF03DSADirectForeign(self):
     "crypto.signature: sign() v4 DSA direct on foreign primary key"
     targetkey_d = read_test_file(['pgpfiles', 'key', 'DSAELG3.pub.gpg'])
     seckey_d = read_test_file(['pgpfiles', 'key', 'DSAELG1.sec.gpg'])
     targetkeypkt = list_pkts(targetkey_d)[0]
     seckeypkt = list_pkts(seckey_d)[0]
     sigtype = SIG_DIRECT
     opts = {'passphrase': 'test', 'primary': targetkeypkt}
     sigpkt = sign(sigtype, targetkeypkt, seckeypkt, **opts)
     verified = verify(sigpkt,
                       targetkeypkt,
                       seckeypkt,
                       primary=targetkeypkt)
     self.assertEqual(1, verified)
예제 #48
0
 def testG01Decrypt1(self):
     """crypto.cipher: decrypt() v4 public (ElGamal) MDC CAST5 compressed"""
     # This message is known to have a compressed packet containing a literal
     # packet containing the text from cleartext.txt.
     enc_d = read_test_file(['pgpfiles', 'enc', 'pub.elg.cast.clrtxt.gpg'])
     key_d = read_test_file(['pgpfiles', 'key', 'DSAELG1.sec.gpg'])
     clr_d = read_test_file(['pgpfiles', 'cleartext.txt'])
     sespkt, encpkt = list_pkts(enc_d)
     keypkt = list_pkts(key_d)[3]  # first encryption subkey (known)
     passphrase = 'test'
     msg_d = decrypt(encpkt, passphrase, sespkt, keypkt)
     compmsg = list_msgs(list_pkts(msg_d))[0]
     litpkt = list_pkts(compmsg.compressed.body.data)[0]
     self.assertEqual(clr_d, litpkt.body.data)
예제 #49
0
 def testE03SignDetached(self):
     "sign_str()/verify_str() detached binary (0x00) using key ID"
     lit_d = read_test_file(['pgpfiles','cleartext.txt'])
     seckey_d = read_test_file(['pgpfiles','key','DSAELG1.sec.asc'])
     pubkey_d = read_test_file(['pgpfiles','key','DSAELG1.pub.asc'])
     sigtype = 0x01
     keyid_d = "0CFC2B6DCC079DF3"
     passphrase = "test"
     sigpkt = sign_str(0x01, seckey_d, target=lit_d,
                       use_key=(None, '0CFC2B6DCC079DF3'),
                       passphrase='test',
                       detach=True)
     verified = verify_str(sigpkt, pubkey_d, detached=lit_d)
     self.assertEqual(lit_d, verified)
예제 #50
0
 def testG04Decrypt4(self):
     """crypto.cipher: decrypt() v3 public (RSA) 3DES compressed"""
     prefix = ['pgpfiles','interop','pgp6.5.3','RSA1']
     enc_d = read_test_file(prefix+['encrypted.cleartext.notepad.pgp6.5.3.RSA1.pgp'])
     key_d = read_test_file(prefix+['key.pgp6.5.3.RSA1.sec.asc'])
     clr_d = read_test_file(prefix+['cleartext.notepad.txt'])
     sespkt, encpkt = list_pkts(enc_d)
     passphrase = 'test'
     # first (totally detached) armored block has [secret key pkt, uid pkt]
     arms = list_armored(key_d)
     keypkt = list_pkts(arms[0].data)[0]
     msg_d = decrypt(encpkt, passphrase, sespkt, keypkt)
     compmsg = list_msgs(list_pkts(msg_d))[0]
     litpkt = list_pkts(compmsg.compressed.body.data)[0]
     self.assertEqual(clr_d, litpkt.body.data)
예제 #51
0
 def testF01ElGamal(self):
     """crypto.cipher: decrypt_public() ElGamal-encrypted session key"""
     #encmsg_d = read_test_file(['pgpfiles','enc','pub.elg.aes256.clrtxt.gpg'])
     encmsg_d = read_test_file(['pgpfiles','enc','pub.elg.cast.clrtxt.gpg'])
     encmsg = list_msgs(list_pkts(encmsg_d))[0]
     seskeypkt = encmsg.targets[0] # target = session key 
     # get target secret key (in this case, a subkey)
     keymsg_d = read_test_file(['pgpfiles','key','DSAELG1.sec.nopass.gpg'])
     keymsg = list_msgs(list_pkts(keymsg_d))[0]
     for block in keymsg._b_subkeys.list():
         subkey = block.leader
         if seskeypkt.body.keyid == subkey.body.id:
             break # leave with the appropriate subkey
     # I know the target in question is an ElGamal key
     key_tuple = (subkey.body.ELGAMAL_p.value, subkey.body.ELGAMAL_x.value)
     cipher_tuple = (seskeypkt.body.ELGAMAL_gk_modp.value, seskeypkt.body.ELGAMAL_myk_modp.value)
     # retrieving the PKCS, etc., padded session key
     padded_message = decrypt_public(seskeypkt.body.alg_pubkey, key_tuple, cipher_tuple)
     # Rules from rfc 2437 9.1.2.2:
     # 1. Padding starts with 0x02.
     # 2. Padding continues with >= 8 octets non-zero gibberish.
     # 3. Gibberish concludes with 0x00.
     # 4. Remaining data is the message.
     if '\x02' == padded_message[0]:
         idx = padded_message.find('\x00')
         if -1 != idx and 8 <= idx:
             message = padded_message[idx+1:]
             chksum = str2int(message[-2:])
             ciphertype = str2int(message[0])
             key = message[1:len(message)-2]
             i = 0
             for e in key:
                 i += str2int(e)
             # TODO check chksum
             cleartext = decrypt_symmetric(ciphertype, key, encmsg.encrypted.body.data)
         else:
             errmsg = "Misplaced \\x00 in session key padding, located at index->(%s)" % idx
     else:
         errmsg = "Session key padding must start with \\x02, received->()" % hex(ord(padded_message[0]))
     cleartext = cleartext[10:] # ditching the prefix + 2
     # result is a compressed message..
     compressedmsg = list_msgs(list_pkts(cleartext))[0]
     newmsgs = list_msgs(list_pkts(compressedmsg.compressed.body.data))
     # ..with only one literal data packet
     literal_data = newmsgs[0].literals[0].body.data
     # compare against original file
     clrtxt = read_test_file(['pgpfiles','cleartext.txt'])
     self.assertEqual(literal_data, clrtxt)
예제 #52
0
 def testC05DecryptSymmetricCAST(self):
     "decrypt_str() symmetric CAST w/integrity"
     enc_d = read_test_file(['pgpfiles','enc','sym.cast.cleartext.txt.gpg'])
     clrtxt = decrypt_str(enc_d, passphrase='test')
     clrmsgs = list_as_signed(clrtxt)
     literal_pkt = list_pkts(clrmsgs[0].compressed.body.data)[0]
     self.assertEqual(self.lit_data, literal_pkt.body.data)
예제 #53
0
 def testA01PublicKeyAttributes(self):
     "PublicKeyMsg: attribute order check"
     key_d = read_test_file(['pgpfiles','key','DSAELG2.subkeyrevoc.gpg'])
     pkts = list_pkts(key_d) # for sequence test
     keymsg = list_msgs(pkts)[0]
     # check type
     self.assertEqual(keymsg.type, MSG_PUBLICKEY)
     # user ids
     self.assertEqual(keymsg._b_userids[0].leader.body.value,
                      'test2 (test many subkeys) <[email protected]>')
     self.assertEqual(keymsg._b_userids[0], 
                      keymsg._b_userids['test2 (test many subkeys) <[email protected]>'])
     # account for all the subkey IDs, *ordering is important*
     known_subkey_ids = ['1BEA996EB8246578',
                         'A4E57E4D8A3BAAE8',
                         'B45D57C94A595CEE',
                         '90AFB828686B6E9A',
                         '52E7E945372C2D26',
                         'A6EA514C4F7232B2']
     self.assertEqual(known_subkey_ids, keymsg._b_subkeys.keylist)
     # check that block list matches up with dictionary values
     for i  in  enumerate(keymsg._b_subkeys.keylist):
         self.assertEqual(keymsg._b_subkeys[i[0]],
                          keymsg._b_subkeys[i[1]])
     # check that primary key is represented
     self.assertEqual('6246EF319AC13CFC', keymsg.primary_id)
     self.assertEqual('6246EF319AC13CFC', keymsg._b_primary.leader.body.id)
     # user ID access
     good_uid = "test2 (test many subkeys) <[email protected]>"
     test_uid = keymsg._b_userids[0].leader.body.value
     self.assertEqual(good_uid, test_uid)
예제 #54
0
 def testG04Decrypt4(self):
     """crypto.cipher: decrypt() v3 public (RSA) 3DES compressed"""
     prefix = ['pgpfiles', 'interop', 'pgp6.5.3', 'RSA1']
     enc_d = read_test_file(
         prefix + ['encrypted.cleartext.notepad.pgp6.5.3.RSA1.pgp'])
     key_d = read_test_file(prefix + ['key.pgp6.5.3.RSA1.sec.asc'])
     clr_d = read_test_file(prefix + ['cleartext.notepad.txt'])
     sespkt, encpkt = list_pkts(enc_d)
     passphrase = 'test'
     # first (totally detached) armored block has [secret key pkt, uid pkt]
     arms = list_armored(key_d)
     keypkt = list_pkts(arms[0].data)[0]
     msg_d = decrypt(encpkt, passphrase, sespkt, keypkt)
     compmsg = list_msgs(list_pkts(msg_d))[0]
     litpkt = list_pkts(compmsg.compressed.body.data)[0]
     self.assertEqual(clr_d, litpkt.body.data)
예제 #55
0
 def testE05SignBinaryAsStringUsingUID(self):
     "sign_str()/verify_str() check literal data options"
     lit_d = read_test_file(['pgpfiles','cleartext.txt'])
     seckey_d = read_test_file(['pgpfiles','key','DSAELG1.sec.asc'])
     pubkey_d = read_test_file(['pgpfiles','key','DSAELG1.pub.asc'])
     signed = sign_str(0x00, seckey_d, target=lit_d,
                       use_userid=(None,'Tester'),
                       passphrase='test',
                       lit_filename='testfilename',
                       lit_modified=100)
     verified = verify_str(signed, pubkey_d) # verified strips sigs..
     literal_body = list_as_signed(verified)[0].literals[0].body
     self.assertEqual('b', literal_body.format)
     self.assertEqual(lit_d, literal_body.data)
     self.assertEqual('testfilename', literal_body.filename)
     self.assertEqual(100, literal_body.modified)
예제 #56
0
 def testA06DSACompressed(self):
     "list: list_as_signed() DSA compressed one-pass signature"
     d = read_test_file(['pgpfiles', 'sig', 'sig.DSAELG1.comp.gpg'])
     players = list_as_signed(d, decompress=True)
     # looking for players = [signed_msg], since decompression is automatic
     self.assertEqual(SignedMsg, players[0].__class__)
     self.assertEqual(1, len(players))
예제 #57
0
 def testA04DSANativeOnePass(self):
     "list: list_as_signed() DSA one-pass signature"
     d = read_test_file(['pgpfiles', 'sig', 'sig.DSAELG1.onepass.gpg'])
     players = list_as_signed(d)
     # looking for players = [signed_msg]
     self.assertEqual(SignedMsg, players[0].__class__)
     self.assertEqual(1, len(players))
예제 #58
0
 def testC06PrimaryRevocation(self):
     "crypto.signature: verify_DSA() primary key revocation by hand"
     d = read_test_file(['pgpfiles','key','DSAELG1.pub.revoked.gpg'])
     keypkt, revocpkt = list_pkts(d)[:2]
     context = sha.new(keypkt.rawstr()+revocpkt.body.hashed_data).digest()
     sigtup = (revocpkt.body.DSA_r.value, revocpkt.body.DSA_s.value)
     keytup = (keypkt.body.DSA_y.value, keypkt.body.DSA_g.value, keypkt.body.DSA_p.value, keypkt.body.DSA_q.value)
     self.assertEqual(1, verify_DSA(context, sigtup, keytup))
예제 #59
0
 def testD07CertificationRevocation(self):
     "crypto.signature: verify() certification revocation"
     key_d = read_test_file(['pgpfiles','key','DSAELG2.revoked_uid.gpg'])
     pkts = list_pkts(key_d)
     keymsg = list_msgs(pkts)[0]
     primary_key, uid, revoker = pkts[0], pkts[3], pkts[4]
     verified = verify(revoker, uid, primary_key)
     self.assertEqual(True, verify(revoker, uid, primary_key))
예제 #60
0
 def testF01DSADirectPrimary(self):
     "crypto.signature: sign() v4 DSA direct (self signature on primary)"
     seckey_d = read_test_file(['pgpfiles','key','DSAELG1.sec.gpg'])
     seckeypkt = list_pkts(seckey_d)[0]
     sigtype = SIG_DIRECT
     sigpkt = sign(sigtype, seckeypkt, seckeypkt, passphrase="test")
     verified = verify(sigpkt,  seckeypkt, seckeypkt)
     self.assertEqual(1, verified)