1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
|
#!/usr/bin/env python
# Copyright 2017 Ludovico de Nittis <aasonykk+gnome@gmail.com>
#
# This file is part of GNOME Keysign.
#
# GNOME Keysign is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# GNOME Keysign is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with GNOME Keysign. If not, see <http://www.gnu.org/licenses/>.
import os
import logging
from subprocess import check_call
import tempfile
import gi
gi.require_version('Gtk', '3.0')
from nose.twistedtools import deferred
from nose.tools import *
from wormhole.errors import WrongPasswordError, LonelyError
from twisted.internet.defer import inlineCallbacks
from keysign.gpgmeh import openpgpkey_from_data
from keysign.gpgmeh import get_public_key_data
from keysign.offer import Offer
from keysign.util import mac_generate
from keysign.wormholeoffer import WormholeOffer
from keysign.wormholereceive import WormholeReceive
log = logging.getLogger(__name__)
thisdir = os.path.dirname(os.path.realpath(__file__))
parentdir = os.path.join(thisdir, "..")
def get_fixture_dir(fixture=""):
dname = os.path.join(thisdir, "fixtures", fixture)
return dname
def get_fixture_file(fixture):
fname = os.path.join(get_fixture_dir(), fixture)
return fname
def import_key_from_file(fixture, homedir):
fname = get_fixture_file(fixture)
original = open(fname, 'rb').read()
gpgcmd = ["gpg", "--homedir={}".format(homedir)]
# Now we import a single key
check_call(gpgcmd + ["--import", fname])
return openpgpkey_from_data(original)
@deferred(timeout=10)
@inlineCallbacks
def test_wrmhl():
# This should be a new, empty directory
homedir = tempfile.mkdtemp()
os.environ["GNUPGHOME"] = homedir
key = import_key_from_file("seckey-no-pw-1.asc", homedir)
file_key_data = get_public_key_data(key.fingerprint)
log.info("Running with key %r", key)
# Start offering the key
offer = WormholeOffer(key)
info = yield offer.allocate_code()
code, _ = info
offer.start()
receive = WormholeReceive(code)
msg_tuple = yield receive.start()
downloaded_key_data, success, _ = msg_tuple
assert_true(success)
log.info("Checking with key: %r", downloaded_key_data)
assert_equal(downloaded_key_data, file_key_data)
@deferred(timeout=10)
@inlineCallbacks
def test_wrmhl_offline_code():
# This should be a new, empty directory
homedir = tempfile.mkdtemp()
os.environ["GNUPGHOME"] = homedir
key = import_key_from_file("seckey-no-pw-1.asc", homedir)
file_key_data = get_public_key_data(key.fingerprint)
# We assume that this channel, at execution time, is free
code = u"5556-penguin-paw-print"
# Start offering the key
offer = WormholeOffer(key)
offer.allocate_code(code)
offer.start()
# Start receiving the key
receive = WormholeReceive(code)
msg_tuple = yield receive.start()
downloaded_key_data, success, _ = msg_tuple
assert_true(success)
log.info("Checking with key: %r", downloaded_key_data)
assert_equal(downloaded_key_data, file_key_data)
@deferred(timeout=10)
@inlineCallbacks
def test_wrmhl_wrong_code():
# This should be a new, empty directory
homedir = tempfile.mkdtemp()
os.environ["GNUPGHOME"] = homedir
key = import_key_from_file("seckey-no-pw-1.asc", homedir)
log.info("Running with key %r", key)
# Start offering the key
offer = WormholeOffer(key)
info = yield offer.allocate_code()
code, _ = info
offer.start()
receive = WormholeReceive(code+"-wrong")
msg_tuple = yield receive.start()
downloaded_key_data, success, message = msg_tuple
assert_false(success)
assert_is_not_none(message)
assert_equal(message, WrongPasswordError)
@deferred(timeout=10)
@inlineCallbacks
def test_wrmhl_wrong_hmac():
# This should be a new, empty directory
homedir = tempfile.mkdtemp()
os.environ["GNUPGHOME"] = homedir
key = import_key_from_file("seckey-no-pw-1.asc", homedir)
log.info("Running with key %r", key)
hmac = "wrong_hmac_eg_tampered_key"
# Start offering the key
offer = WormholeOffer(key)
info = yield offer.allocate_code()
code, _ = info
offer.start()
receive = WormholeReceive(code, mac=hmac)
msg_tuple = yield receive.start()
downloaded_key_data, success, message = msg_tuple
assert_false(success)
assert_is_not_none(message)
assert_equal(message, WrongPasswordError)
@deferred(timeout=10)
@inlineCallbacks
def test_wrmhl_with_hmac():
# This should be a new, empty directory
homedir = tempfile.mkdtemp()
os.environ["GNUPGHOME"] = homedir
key = import_key_from_file("seckey-no-pw-1.asc", homedir)
file_key_data = get_public_key_data(key.fingerprint)
log.info("Running with key %r", key)
hmac = mac_generate(key.fingerprint.encode('ascii'), file_key_data)
# Start offering the key
offer = WormholeOffer(key)
info = yield offer.allocate_code()
code, _ = info
offer.start()
receive = WormholeReceive(code, mac=hmac)
msg_tuple = yield receive.start()
downloaded_key_data, success, _ = msg_tuple
assert_true(success)
log.info("Checking with key: %r", downloaded_key_data)
assert_equal(downloaded_key_data, file_key_data)
@deferred(timeout=10)
@inlineCallbacks
def _test_offer_cancel():
def _received(start_data):
success, message = start_data
assert_is_not_none(message)
assert_equal(type(message), LonelyError)
# This should be a new, empty directory
homedir = tempfile.mkdtemp()
os.environ["GNUPGHOME"] = homedir
key = import_key_from_file("seckey-no-pw-1.asc", homedir)
log.info("Running with key %r", key)
# Start offering the key
offer = Offer(key)
_ = yield offer.allocate_code(worm=True)
defers = offer.start()
for de in defers:
de.addCallback(_received)
offer.stop()
|