|
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 os
|
|
Packit Service |
672cf4 |
import gpg
|
|
Packit Service |
672cf4 |
import support
|
|
Packit Service |
6c01f9 |
_ = support # to appease pyflakes.
|
|
Packit Service |
672cf4 |
|
|
Packit Service |
672cf4 |
for passphrase in ("abc", b"abc"):
|
|
Packit Service |
672cf4 |
c = gpg.Context()
|
|
Packit Service |
672cf4 |
c.set_armor(True)
|
|
Packit Service |
672cf4 |
c.set_pinentry_mode(gpg.constants.PINENTRY_MODE_LOOPBACK)
|
|
Packit Service |
672cf4 |
|
|
Packit Service |
672cf4 |
source = gpg.Data("Hallo Leute\n")
|
|
Packit Service |
672cf4 |
cipher = gpg.Data()
|
|
Packit Service |
672cf4 |
|
|
Packit Service |
672cf4 |
passphrase_cb_called = 0
|
|
Packit Service |
672cf4 |
def passphrase_cb(hint, desc, prev_bad, hook=None):
|
|
Packit Service |
672cf4 |
global passphrase_cb_called
|
|
Packit Service |
672cf4 |
passphrase_cb_called += 1
|
|
Packit Service |
672cf4 |
return passphrase
|
|
Packit Service |
672cf4 |
|
|
Packit Service |
672cf4 |
c.set_passphrase_cb(passphrase_cb, None)
|
|
Packit Service |
672cf4 |
|
|
Packit Service |
672cf4 |
c.op_encrypt([], 0, source, cipher)
|
|
Packit Service |
672cf4 |
assert passphrase_cb_called == 1, \
|
|
Packit Service |
672cf4 |
"Callback called {} times".format(passphrase_cb_called)
|
|
Packit Service |
672cf4 |
support.print_data(cipher)
|
|
Packit Service |
672cf4 |
|
|
Packit Service |
672cf4 |
c = gpg.Context()
|
|
Packit Service |
672cf4 |
c.set_armor(True)
|
|
Packit Service |
672cf4 |
c.set_pinentry_mode(gpg.constants.PINENTRY_MODE_LOOPBACK)
|
|
Packit Service |
672cf4 |
c.set_passphrase_cb(passphrase_cb, None)
|
|
Packit Service |
672cf4 |
plain = gpg.Data()
|
|
Packit Service |
672cf4 |
cipher.seek(0, os.SEEK_SET)
|
|
Packit Service |
672cf4 |
|
|
Packit Service |
672cf4 |
c.op_decrypt(cipher, plain)
|
|
Packit Service |
672cf4 |
# Seems like the passphrase is cached.
|
|
Packit Service |
6c01f9 |
#assert passphrase_cb_called == 2, \
|
|
Packit Service |
672cf4 |
# "Callback called {} times".format(passphrase_cb_called)
|
|
Packit Service |
672cf4 |
support.print_data(plain)
|
|
Packit Service |
672cf4 |
|
|
Packit Service |
672cf4 |
plain.seek(0, os.SEEK_SET)
|
|
Packit Service |
672cf4 |
plaintext = plain.read()
|
|
Packit Service |
672cf4 |
assert plaintext == b"Hallo Leute\n", \
|
|
Packit Service |
672cf4 |
"Wrong plaintext {!r}".format(plaintext)
|
|
Packit Service |
672cf4 |
|
|
Packit Service |
672cf4 |
# Idiomatic interface.
|
|
Packit Service |
672cf4 |
for passphrase in ("abc", b"abc"):
|
|
Packit Service |
672cf4 |
with gpg.Context(armor=True) as c:
|
|
Packit Service |
672cf4 |
# Check that the passphrase callback is not altered.
|
|
Packit Service |
672cf4 |
def f(*args):
|
|
Packit Service |
672cf4 |
assert False
|
|
Packit Service |
672cf4 |
c.set_passphrase_cb(f)
|
|
Packit Service |
672cf4 |
|
|
Packit Service |
672cf4 |
message = "Hallo Leute\n".encode()
|
|
Packit Service |
6c01f9 |
ciphertext, _, _ = c.encrypt(message,
|
|
Packit Service |
6c01f9 |
passphrase=passphrase,
|
|
Packit Service |
6c01f9 |
sign=False)
|
|
Packit Service |
672cf4 |
assert ciphertext.find(b'BEGIN PGP MESSAGE') > 0, 'Marker not found'
|
|
Packit Service |
672cf4 |
|
|
Packit Service |
672cf4 |
plaintext, _, _ = c.decrypt(ciphertext, passphrase=passphrase)
|
|
Packit Service |
672cf4 |
assert plaintext == message, 'Message body not recovered'
|
|
Packit Service |
672cf4 |
|
|
Packit Service |
672cf4 |
assert c._passphrase_cb[1] == f, "Passphrase callback not restored"
|