Blame lang/python/tests/t-verify.py

Packit Service 672cf4
#!/usr/bin/env python
Packit Service 672cf4
Packit Service 672cf4
# Copyright (C) 2016 g10 Code GmbH
Packit Service 672cf4
#
Packit Service 672cf4
# This file is part of GPGME.
Packit Service 672cf4
#
Packit Service 672cf4
# GPGME is free software; you can redistribute it and/or modify it
Packit Service 672cf4
# under the terms of the GNU General Public License as published by
Packit Service 672cf4
# the Free Software Foundation; either version 2 of the License, or
Packit Service 672cf4
# (at your option) any later version.
Packit Service 672cf4
#
Packit Service 672cf4
# GPGME is distributed in the hope that it will be useful, but WITHOUT
Packit Service 672cf4
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
Packit Service 672cf4
# or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General
Packit Service 672cf4
# Public License for more details.
Packit Service 672cf4
#
Packit Service 672cf4
# You should have received a copy of the GNU Lesser General Public
Packit Service 6c01f9
# License along with this program; if not, see <http://www.gnu.org/licenses/>.
Packit Service 672cf4
Packit Service 672cf4
from __future__ import absolute_import, print_function, unicode_literals
Packit Service 6c01f9
del absolute_import, print_function, unicode_literals
Packit Service 672cf4
Packit Service 672cf4
import sys
Packit Service 672cf4
import os
Packit Service 672cf4
import gpg
Packit Service 672cf4
import support
Packit Service 6c01f9
_ = support # to appease pyflakes.
Packit Service 672cf4
Packit Service 672cf4
test_text1 = b"Just GNU it!\n"
Packit Service 6c01f9
test_text1f= b"Just GNU it?\n"
Packit Service 672cf4
test_sig1 = b"""-----BEGIN PGP SIGNATURE-----
Packit Service 672cf4
Packit Service 672cf4
iN0EABECAJ0FAjoS+i9FFIAAAAAAAwA5YmFyw7bDpMO8w58gZGFzIHdhcmVuIFVt
Packit Service 672cf4
bGF1dGUgdW5kIGpldHp0IGVpbiBwcm96ZW50JS1aZWljaGVuNRSAAAAAAAgAJGZv
Packit Service 672cf4
b2Jhci4xdGhpcyBpcyBhIG5vdGF0aW9uIGRhdGEgd2l0aCAyIGxpbmVzGhpodHRw
Packit Service 672cf4
Oi8vd3d3Lmd1Lm9yZy9wb2xpY3kvAAoJEC1yfMdoaXc0JBIAoIiLlUsvpMDOyGEc
Packit Service 672cf4
dADGKXF/Hcb+AKCJWPphZCphduxSvrzH0hgzHdeQaA==
Packit Service 672cf4
=nts1
Packit Service 672cf4
-----END PGP SIGNATURE-----
Packit Service 672cf4
"""
Packit Service 672cf4
Packit Service 672cf4
test_sig2 = b"""-----BEGIN PGP MESSAGE-----
Packit Service 672cf4
Packit Service 672cf4
owGbwMvMwCSoW1RzPCOz3IRxjXQSR0lqcYleSUWJTZOvjVdpcYmCu1+oQmaJIleH
Packit Service 672cf4
GwuDIBMDGysTSIqBi1MApi+nlGGuwDeHao53HBr+FoVGP3xX+kvuu9fCMJvl6IOf
Packit Service 672cf4
y1kvP4y+8D5a11ang0udywsA
Packit Service 672cf4
=Crq6
Packit Service 672cf4
-----END PGP MESSAGE-----
Packit Service 672cf4
"""
Packit Service 672cf4
Packit Service 672cf4
# A message with a prepended but unsigned plaintext packet.
Packit Service 672cf4
double_plaintext_sig = b"""-----BEGIN PGP MESSAGE-----
Packit Service 672cf4
Packit Service 672cf4
rDRiCmZvb2Jhci50eHRF4pxNVGhpcyBpcyBteSBzbmVha3kgcGxhaW50ZXh0IG1l
Packit Service 672cf4
c3NhZ2UKowGbwMvMwCSoW1RzPCOz3IRxTWISa6JebnG666MFD1wzSzJSixQ81XMV
Packit Service 672cf4
UlITUxTyixRyKxXKE0uSMxQyEosVikvyCwpSU/S4FNCArq6Ce1F+aXJGvoJvYlGF
Packit Service 672cf4
erFCTmJxiUJ5flFKMVeHGwuDIBMDGysTyA4GLk4BmO036xgWzMgzt9V85jCtfDFn
Packit Service 672cf4
UqVooWlGXHwNw/xg/fVzt9VNbtjtJ/fhUqYo0/LyCGEA
Packit Service 672cf4
=6+AK
Packit Service 672cf4
-----END PGP MESSAGE-----
Packit Service 672cf4
"""
Packit Service 672cf4
Packit Service 672cf4
def check_result(result, summary, validity, fpr, status, notation):
Packit Service 672cf4
    assert len(result.signatures) == 1, "Unexpected number of signatures"
Packit Service 672cf4
    sig = result.signatures[0]
Packit Service 672cf4
    assert sig.summary == summary, \
Packit Service 672cf4
        "Unexpected signature summary: {}, want: {}".format(sig.summary,
Packit Service 672cf4
                                                            summary)
Packit Service 672cf4
    assert sig.fpr == fpr
Packit Service 672cf4
    assert gpg.errors.GPGMEError(sig.status).getcode() == status
Packit Service 672cf4
Packit Service 672cf4
    if notation:
Packit Service 672cf4
        expected_notations = {
Packit Service 672cf4
            "bar": (b"\xc3\xb6\xc3\xa4\xc3\xbc\xc3\x9f" +
Packit Service 672cf4
                    b" das waren Umlaute und jetzt ein prozent%-Zeichen"
Packit Service 672cf4
                    if sys.version_info[0] < 3 else
Packit Service 672cf4
                    b"\xc3\xb6\xc3\xa4\xc3\xbc\xc3\x9f".decode() +
Packit Service 672cf4
                    " das waren Umlaute und jetzt ein prozent%-Zeichen"),
Packit Service 6c01f9
            "foobar.1":  "this is a notation data with 2 lines",
Packit Service 6c01f9
            None: "http://www.gu.org/policy/",
Packit Service 672cf4
        }
Packit Service 672cf4
        assert len(sig.notations) == len(expected_notations)
Packit Service 672cf4
Packit Service 672cf4
        for r in sig.notations:
Packit Service 6c01f9
            assert not 'name_len' in dir(r)
Packit Service 6c01f9
            assert not 'value_len' in dir(r)
Packit Service 672cf4
            assert r.name in expected_notations
Packit Service 672cf4
            assert r.value == expected_notations[r.name], \
Packit Service 672cf4
                "Expected {!r}, got {!r}".format(expected_notations[r.name],
Packit Service 672cf4
                                                 r.value)
Packit Service 672cf4
            expected_notations.pop(r.name)
Packit Service 672cf4
Packit Service 672cf4
        assert len(expected_notations) == 0
Packit Service 672cf4
Packit Service 672cf4
    assert not sig.wrong_key_usage
Packit Service 672cf4
    assert sig.validity == validity, \
Packit Service 672cf4
        "Unexpected signature validity: {}, want: {}".format(
Packit Service 672cf4
            sig.validity, validity)
Packit Service 6c01f9
    assert gpg.errors.GPGMEError(sig.validity_reason).getcode() == gpg.errors.NO_ERROR
Packit Service 672cf4
Packit Service 672cf4
c = gpg.Context()
Packit Service 672cf4
c.set_armor(True)
Packit Service 672cf4
Packit Service 672cf4
# Checking a valid message.
Packit Service 672cf4
text = gpg.Data(test_text1)
Packit Service 672cf4
sig = gpg.Data(test_sig1)
Packit Service 672cf4
c.op_verify(sig, text, None)
Packit Service 672cf4
result = c.op_verify_result()
Packit Service 672cf4
check_result(result, gpg.constants.sigsum.VALID | gpg.constants.sigsum.GREEN,
Packit Service 672cf4
             gpg.constants.validity.FULL,
Packit Service 6c01f9
             "A0FF4590BB6122EDEF6E3C542D727CC768697734",
Packit Service 6c01f9
             gpg.errors.NO_ERROR, True)
Packit Service 6c01f9
Packit Service 672cf4
Packit Service 672cf4
# Checking a manipulated message.
Packit Service 672cf4
text = gpg.Data(test_text1f)
Packit Service 672cf4
sig.seek(0, os.SEEK_SET)
Packit Service 672cf4
c.op_verify(sig, text, None)
Packit Service 672cf4
result = c.op_verify_result()
Packit Service 672cf4
check_result(result, gpg.constants.sigsum.RED, gpg.constants.validity.UNKNOWN,
Packit Service 672cf4
             "2D727CC768697734", gpg.errors.BAD_SIGNATURE, False)
Packit Service 672cf4
Packit Service 672cf4
# Checking a normal signature.
Packit Service 672cf4
text = gpg.Data()
Packit Service 672cf4
sig = gpg.Data(test_sig2)
Packit Service 672cf4
c.op_verify(sig, None, text)
Packit Service 672cf4
result = c.op_verify_result()
Packit Service 672cf4
check_result(result, gpg.constants.sigsum.VALID | gpg.constants.sigsum.GREEN,
Packit Service 672cf4
             gpg.constants.validity.FULL,
Packit Service 6c01f9
             "A0FF4590BB6122EDEF6E3C542D727CC768697734",
Packit Service 6c01f9
             gpg.errors.NO_ERROR, False)
Packit Service 672cf4
Packit Service 672cf4
# Checking an invalid message.
Packit Service 672cf4
text = gpg.Data()
Packit Service 672cf4
sig = gpg.Data(double_plaintext_sig)
Packit Service 672cf4
try:
Packit Service 672cf4
    c.op_verify(sig, None, text)
Packit Service 672cf4
except Exception as e:
Packit Service 672cf4
    assert type(e) == gpg.errors.GPGMEError
Packit Service 672cf4
    assert e.getcode() == gpg.errors.BAD_DATA
Packit Service 672cf4
else:
Packit Service 672cf4
    assert False, "Expected an error but got none."
Packit Service 672cf4
Packit Service 6c01f9
Packit Service 672cf4
# Idiomatic interface.
Packit Service 672cf4
with gpg.Context(armor=True) as c:
Packit Service 672cf4
    # Checking a valid message.
Packit Service 672cf4
    _, result = c.verify(test_text1, test_sig1)
Packit Service 6c01f9
    check_result(result, gpg.constants.sigsum.VALID | gpg.constants.sigsum.GREEN,
Packit Service 6c01f9
                 gpg.constants.validity.FULL,
Packit Service 6c01f9
                 "A0FF4590BB6122EDEF6E3C542D727CC768697734",
Packit Service 6c01f9
                 gpg.errors.NO_ERROR, True)
Packit Service 672cf4
Packit Service 672cf4
    # Checking a manipulated message.
Packit Service 672cf4
    try:
Packit Service 672cf4
        c.verify(test_text1f, test_sig1)
Packit Service 672cf4
    except gpg.errors.BadSignatures as e:
Packit Service 672cf4
        check_result(e.result, gpg.constants.sigsum.RED,
Packit Service 6c01f9
                     gpg.constants.validity.UNKNOWN,
Packit Service 6c01f9
                     "2D727CC768697734", gpg.errors.BAD_SIGNATURE, False)
Packit Service 672cf4
    else:
Packit Service 672cf4
        assert False, "Expected an error but got none."
Packit Service 672cf4
Packit Service 672cf4
    # Checking a normal signature.
Packit Service 672cf4
    sig = gpg.Data(test_sig2)
Packit Service 672cf4
    data, result = c.verify(test_sig2)
Packit Service 6c01f9
    check_result(result, gpg.constants.sigsum.VALID | gpg.constants.sigsum.GREEN,
Packit Service 6c01f9
                 gpg.constants.validity.FULL,
Packit Service 6c01f9
                 "A0FF4590BB6122EDEF6E3C542D727CC768697734",
Packit Service 6c01f9
                 gpg.errors.NO_ERROR, False)
Packit Service 672cf4
    assert data == test_text1
Packit Service 672cf4
Packit Service 672cf4
    # Checking an invalid message.
Packit Service 672cf4
    try:
Packit Service 672cf4
        c.verify(double_plaintext_sig)
Packit Service 672cf4
    except gpg.errors.GPGMEError as e:
Packit Service 672cf4
        assert e.getcode() == gpg.errors.BAD_DATA
Packit Service 672cf4
    else:
Packit Service 672cf4
        assert False, "Expected an error but got none."
Packit Service 672cf4
Packit Service 672cf4
    alpha = c.get_key("A0FF4590BB6122EDEF6E3C542D727CC768697734", False)
Packit Service 672cf4
    bob = c.get_key("D695676BDCEDCC2CDD6152BCFE180B1DA9E3B0B2", False)
Packit Service 672cf4
Packit Service 672cf4
    # Checking a valid message.
Packit Service 672cf4
    c.verify(test_text1, test_sig1, verify=[alpha])
Packit Service 672cf4
Packit Service 672cf4
    try:
Packit Service 672cf4
        c.verify(test_text1, test_sig1, verify=[alpha, bob])
Packit Service 672cf4
    except gpg.errors.MissingSignatures as e:
Packit Service 672cf4
        assert len(e.missing) == 1
Packit Service 672cf4
        assert e.missing[0] == bob
Packit Service 672cf4
    else:
Packit Service 672cf4
        assert False, "Expected an error, got none"