# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General # Public License for more details. # # You should have received a copy of the GNU Lesser General Public # License along with this program; if not, see <http://www.gnu.org/licenses/>. from __future__ import absolute_import, print_function, unicode_literals import gpg import support del absolute_import, print_function, unicode_literals c = gpg.Context() source = gpg.Data(file=support.make_filename("cipher-1.asc")) sink = gpg.Data() c.op_decrypt(source, sink) result = c.op_decrypt_result() assert not result.unsupported_algorithm, \ "Unsupported algorithm: {}".format(result.unsupported_algorithm) support.print_data(sink) # Idiomatic interface. with gpg.Context() as c: plaintext, _, _ = c.decrypt(open(support.make_filename("cipher-1.asc"))) assert len(plaintext) > 0 assert plaintext.find(b'Wenn Sie dies lesen k') >= 0, \ 'Plaintext not found'
def check_verify_result(result, summary, fpr, status): assert len(result.signatures) == 1, "Unexpected number of signatures" sig = result.signatures[0] assert sig.summary == summary, "Unexpected signature summary" assert sig.fpr == fpr assert gpg.errors.GPGMEError(sig.status).getcode() == status assert len(sig.notations) == 0 assert not sig.wrong_key_usage assert sig.validity == gpg.constants.validity.FULL assert gpg.errors.GPGMEError( sig.validity_reason).getcode() == gpg.errors.NO_ERROR c = gpg.Context() source = gpg.Data(file=support.make_filename("cipher-2.asc")) sink = gpg.Data() c.op_decrypt_verify(source, sink) result = c.op_decrypt_result() assert not result.unsupported_algorithm, \ "Unsupported algorithm: {}".format(result.unsupported_algorithm) support.print_data(sink) verify_result = c.op_verify_result() check_verify_result(verify_result, gpg.constants.sigsum.VALID | gpg.constants.sigsum.GREEN, "A0FF4590BB6122EDEF6E3C542D727CC768697734", gpg.errors.NO_ERROR)
(not secret and result.secret_imported != 0)) assert not ((secret and ( (result.secret_imported == 0 and result.secret_unchanged != 1 and result.secret_unchanged != 2) or (result.secret_imported == 1 and result.secret_unchanged != 0))) or (not secret and result.secret_unchanged != 0)) assert result.not_imported == 0 if secret: assert not (len(result.imports) in (0, 3)) else: assert not (len(result.imports) in (0, 2)) assert fpr == result.imports[0].fpr assert len(result.imports) == 1 or fpr == result.imports[1].fpr assert result.imports[0].result == 0 c = gpg.Context() result = c.key_import(open(support.make_filename("pubkey-1.asc"), 'rb').read()) check_result(result, "ADAB7FCC1F4DE2616ECFA402AF82244F9CD9FD55", False) result = c.key_import(open(support.make_filename("seckey-1.asc"), 'rb').read()) check_result(result, "ADAB7FCC1F4DE2616ECFA402AF82244F9CD9FD55", True) try: result = c.key_import(b"thisisnotakey") except ValueError: pass assert result == "IMPORT_PROBLEM"
import support def check_verify_result(result, summary, fpr, status): assert len(result.signatures) == 1, "Unexpected number of signatures" sig = result.signatures[0] assert sig.summary == summary, "Unexpected signature summary" assert sig.fpr == fpr assert errors.GPGMEError(sig.status).getcode() == status assert len(sig.notations) == 0 assert not sig.wrong_key_usage assert sig.validity == constants.VALIDITY_UNKNOWN assert errors.GPGMEError(sig.validity_reason).getcode() == errors.NO_ERROR support.init_gpgme(constants.PROTOCOL_OpenPGP) c = core.Context() source = core.Data(file=support.make_filename("cipher-2.asc")) sink = core.Data() c.op_decrypt_verify(source, sink) result = c.op_decrypt_result() assert not result.unsupported_algorithm, \ "Unsupported algorithm: {}".format(result.unsupported_algorithm) support.print_data(sink) verify_result = c.op_verify_result() check_verify_result(verify_result, 0, "A0FF4590BB6122EDEF6E3C542D727CC768697734", errors.NO_ERROR)
(not secret and result.secret_read != 0)) assert not ( (secret and result.secret_imported != 0 and result.secret_imported != 1 and result.secret_imported != 2) or (not secret and result.secret_imported != 0)) assert not ((secret and ( (result.secret_imported == 0 and result.secret_unchanged != 1 and result.secret_unchanged != 2) or (result.secret_imported == 1 and result.secret_unchanged != 0))) or (not secret and result.secret_unchanged != 0)) assert result.not_imported == 0 if secret: assert not (len(result.imports) in (0, 3)) else: assert not (len(result.imports) in (0, 2)) assert fpr == result.imports[0].fpr assert len(result.imports) == 1 or fpr == result.imports[1].fpr assert result.imports[0].result == 0 c = gpg.Context() c.op_import(gpg.Data(file=support.make_filename("pubkey-1.asc"))) result = c.op_import_result() check_result(result, "ADAB7FCC1F4DE2616ECFA402AF82244F9CD9FD55", False) c.op_import(gpg.Data(file=support.make_filename("seckey-1.asc"))) result = c.op_import_result() check_result(result, "ADAB7FCC1F4DE2616ECFA402AF82244F9CD9FD55", True)