Blame lang/python/tests/t-quick-key-creation.py

Packit Service 672cf4
#!/usr/bin/env python
Packit Service 672cf4
Packit Service 672cf4
# Copyright (C) 2017 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 gpg
Packit Service 672cf4
import itertools
Packit Service 672cf4
import time
Packit Service 672cf4
Packit Service 672cf4
import support
Packit Service 672cf4
support.assert_gpg_version((2, 1, 2))
Packit Service 672cf4
Packit Service 672cf4
alpha = "Alpha <alpha@invalid.example.net>"
Packit Service 672cf4
Packit Service 672cf4
with support.EphemeralContext() as ctx:
Packit Service 672cf4
    res = ctx.create_key(alpha)
Packit Service 672cf4
Packit Service 672cf4
    keys = list(ctx.keylist())
Packit Service 672cf4
    assert len(keys) == 1, "Weird number of keys created"
Packit Service 672cf4
Packit Service 672cf4
    key = keys[0]
Packit Service 672cf4
    assert key.fpr == res.fpr
Packit Service 672cf4
    assert len(key.subkeys) == 2, "Expected one primary key and one subkey"
Packit Service 672cf4
    assert key.subkeys[0].expires > 0, "Expected primary key to expire"
Packit Service 672cf4
Packit Service 672cf4
    # Try to create a key with the same UID
Packit Service 672cf4
    try:
Packit Service 672cf4
        ctx.create_key(alpha)
Packit Service 672cf4
        assert False, "Expected an error but got none"
Packit Service 672cf4
    except gpg.errors.GpgError as e:
Packit Service 672cf4
        pass
Packit Service 672cf4
Packit Service 672cf4
    # Try to create a key with the same UID, now with force!
Packit Service 672cf4
    res2 = ctx.create_key(alpha, force=True)
Packit Service 672cf4
    assert res.fpr != res2.fpr
Packit Service 672cf4
Packit Service 6c01f9
Packit Service 672cf4
# From here on, we use one context, and create unique UIDs
Packit Service 672cf4
uid_counter = 0
Packit Service 672cf4
def make_uid():
Packit Service 672cf4
    global uid_counter
Packit Service 672cf4
    uid_counter += 1
Packit Service 672cf4
    return "user{0}@invalid.example.org".format(uid_counter)
Packit Service 672cf4
Packit Service 672cf4
with support.EphemeralContext() as ctx:
Packit Service 672cf4
    # Check gpg.constants.create.NOEXPIRE...
Packit Service 672cf4
    res = ctx.create_key(make_uid(), expires=False)
Packit Service 672cf4
    key = ctx.get_key(res.fpr, secret=True)
Packit Service 672cf4
    assert key.fpr == res.fpr
Packit Service 672cf4
    assert len(key.subkeys) == 2, "Expected one primary key and one subkey"
Packit Service 672cf4
    assert key.subkeys[0].expires == 0, "Expected primary key not to expire"
Packit Service 672cf4
Packit Service 672cf4
    t = 2 * 24 * 60 * 60
Packit Service 672cf4
    slack = 5 * 60
Packit Service 672cf4
    res = ctx.create_key(make_uid(), expires_in=t)
Packit Service 672cf4
    key = ctx.get_key(res.fpr, secret=True)
Packit Service 672cf4
    assert key.fpr == res.fpr
Packit Service 672cf4
    assert len(key.subkeys) == 2, "Expected one primary key and one subkey"
Packit Service 672cf4
    assert abs(time.time() + t - key.subkeys[0].expires) < slack, \
Packit Service 672cf4
        "Primary keys expiration time is off"
Packit Service 672cf4
Packit Service 672cf4
    # Check capabilities
Packit Service 6c01f9
    for sign, encrypt, certify, authenticate in itertools.product([False, True],
Packit Service 6c01f9
                                                                  [False, True],
Packit Service 6c01f9
                                                                  [False, True],
Packit Service 6c01f9
                                                                  [False, True]):
Packit Service 672cf4
        # Filter some out
Packit Service 672cf4
        if not (sign or encrypt or certify or authenticate):
Packit Service 672cf4
            # This triggers the default capabilities tested before.
Packit Service 672cf4
            continue
Packit Service 672cf4
        if (sign or encrypt or authenticate) and not certify:
Packit Service 672cf4
            # The primary key always certifies.
Packit Service 672cf4
            continue
Packit Service 672cf4
Packit Service 6c01f9
        res = ctx.create_key(make_uid(), algorithm="rsa",
Packit Service 6c01f9
                             sign=sign, encrypt=encrypt, certify=certify,
Packit Service 6c01f9
                             authenticate=authenticate)
Packit Service 672cf4
        key = ctx.get_key(res.fpr, secret=True)
Packit Service 672cf4
        assert key.fpr == res.fpr
Packit Service 672cf4
        assert len(key.subkeys) == 1, \
Packit Service 672cf4
            "Expected no subkey for non-default capabilities"
Packit Service 672cf4
Packit Service 672cf4
        p = key.subkeys[0]
Packit Service 672cf4
        assert sign == p.can_sign
Packit Service 672cf4
        assert encrypt == p.can_encrypt
Packit Service 672cf4
        assert certify == p.can_certify
Packit Service 672cf4
        assert authenticate == p.can_authenticate
Packit Service 672cf4
Packit Service 672cf4
    # Check algorithm
Packit Service 672cf4
    res = ctx.create_key(make_uid(), algorithm="rsa")
Packit Service 672cf4
    key = ctx.get_key(res.fpr, secret=True)
Packit Service 672cf4
    assert key.fpr == res.fpr
Packit Service 672cf4
    for k in key.subkeys:
Packit Service 672cf4
        assert k.pubkey_algo == 1
Packit Service 672cf4
Packit Service 672cf4
    # Check algorithm with size
Packit Service 672cf4
    res = ctx.create_key(make_uid(), algorithm="rsa1024")
Packit Service 672cf4
    key = ctx.get_key(res.fpr, secret=True)
Packit Service 672cf4
    assert key.fpr == res.fpr
Packit Service 672cf4
    for k in key.subkeys:
Packit Service 672cf4
        assert k.pubkey_algo == 1
Packit Service 672cf4
        assert k.length == 1024
Packit Service 672cf4
Packit Service 672cf4
    # Check algorithm future-default
Packit Service 672cf4
    ctx.create_key(make_uid(), algorithm="future-default")
Packit Service 672cf4
Packit Service 672cf4
    # Check passphrase protection
Packit Service 672cf4
    recipient = make_uid()
Packit Service 672cf4
    passphrase = "streng geheim"
Packit Service 672cf4
    res = ctx.create_key(recipient, passphrase=passphrase)
Packit Service 6c01f9
    ciphertext, _, _ = ctx.encrypt(b"hello there", recipients=[ctx.get_key(res.fpr)])
Packit Service 672cf4
Packit Service 672cf4
    cb_called = False
Packit Service 672cf4
    def cb(*args):
Packit Service 672cf4
        global cb_called
Packit Service 672cf4
        cb_called = True
Packit Service 672cf4
        return passphrase
Packit Service 672cf4
    ctx.pinentry_mode = gpg.constants.PINENTRY_MODE_LOOPBACK
Packit Service 672cf4
    ctx.set_passphrase_cb(cb)
Packit Service 672cf4
Packit Service 672cf4
    plaintext, _, _ = ctx.decrypt(ciphertext)
Packit Service 672cf4
    assert plaintext == b"hello there"
Packit Service 672cf4
    assert cb_called