def __init__(self, config, secrets): try: dataKeyBytes = config.get("key", None) except AttributeError as e: raise InvalidConfig(original=e) if not dataKeyBytes or dataKeyBytes == "": msg = ("crypto.key is not a string, " "Remove the crypto property if encryption is not needed") raise InvalidConfig(message=msg) privateKey = secrets.get("privateKey", None) if not privateKey or privateKey == "": msg = ("No gpg private key provided for decryption. " "Remove the crypto property if encryption is not needed") raise InvalidConfig(message=msg) gpgKey = PGPKey() gpgKey.parse(privateKey) password = secrets.get("privateKeyPassword", None) if password: try: gpgKey.unlock(password) except PGPDecryptionError as err: raise BadGPGKeyPasswordError(gpgKey.userids[0]) with warnings.catch_warnings(): # prevents warning of type `UserWarning: Message was encrypted with this key's subkey: ...` warnings.simplefilter("ignore", category=UserWarning) dataKey = gpgKey.decrypt( PGPMessage.from_blob(dataKeyBytes)).message DataKeyService.__init__(self, dataKey)
def test_parse_wrong_crc24(self, recwarn): keytext = _read('tests/testdata/keys/rsa.1.sec.asc').splitlines() keytext[-2] = "=abcd" keytext = '\n'.join(keytext) key = PGPKey() key.parse(keytext) w = recwarn.pop(UserWarning) assert str(w.message) == "Incorrect crc24" assert w.filename == __file__
def test_parse_wrong_magic(self): keytext = _read('tests/testdata/keys/rsa.1.sec.asc').replace( 'KEY', 'EKY') key = PGPKey() with pytest.raises(ValueError): key.parse(keytext)
def test_parse_wrong_magic(self): keytext = _read('tests/testdata/keys/rsa.1.sec.asc').replace('KEY', 'EKY') key = PGPKey() with pytest.raises(ValueError): key.parse(keytext)
def parse_key(key_str): key = PGPKey() key.parse(key_str) return key
def test_reg_bug_56(): # some imports only used by this regression test import hashlib from datetime import datetime from pgpy.pgp import PGPSignature from pgpy.constants import HashAlgorithm from pgpy.constants import PubKeyAlgorithm from pgpy.constants import SignatureType from cryptography.hazmat.backends import default_backend from cryptography.hazmat.primitives import hashes from cryptography.hazmat.primitives.asymmetric import padding # do a regression test on issue #56 # re-create a signature that would have been encoded improperly as with issue #56 # and see if it fails to verify or not # this was the old seckeys/TestRSA-2048.key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6mx4eHUd0MSnYud1IzRx\n" \ "D8cPxh9fCdoW0OpddqKNczAvO4bl5wwDafrEa7HpIX/sMVMZXo2h6TkitdLCdEfk\n" \ "tgEjS0hTsFtfwsXt9TKi1x3HJIbcm8t78ubpWXepB/iNKVzv4punFHhKiz54ZFyN\n" \ "dQ==\n" \ "=lqIH\n" \ "-----END PGP PUBLIC KEY BLOCK-----\n" # load the keypair above sk = PGPKey() sk.parse(sec) pk = PGPKey() pk.parse(pub) sigsubject = bytearray(b"Hello!I'm a test document.I'm going to get signed a bunch of times.KBYE!") sig = PGPSignature.new(SignatureType.BinaryDocument, PubKeyAlgorithm.RSAEncryptOrSign, HashAlgorithm.SHA512, sk.fingerprint.keyid) sig._signature.subpackets['h_CreationTime'][-1].created = datetime(2014, 8, 6, 23, 28, 51) sig._signature.subpackets.update_hlen() hdata = sig.hashdata(sigsubject) sig._signature.hash2 = hashlib.new('sha512', hdata).digest()[:2] # create the signature signer = sk.__key__.__privkey__().signer(padding.PKCS1v15(), hashes.SHA512()) signer.update(hdata) sig._signature.signature.from_signer(signer.finalize()) sig._signature.update_hlen() # check encoding assert sig._signature.signature.md_mod_n.to_mpibytes()[2:3] != b'\x00' # with PGPy assert pk.verify(sigsubject, sig) # with GnuPG with gpg.Context(armor=True, offline=True) as c: c.set_engine_info(gpg.constants.PROTOCOL_OpenPGP, home_dir=gnupghome) # import the key key_data = gpg.Data(string=pub) gpg.core.gpgme.gpgme_op_import(c.wrapped, key_data) _, vres = c.verify(gpg.Data(string=sigsubject.decode('latin-1')), gpg.Data(string=str(sig))) assert vres