gpg: rename load_from_gpg -> get_public_key

This commit is contained in:
Roman Zeyde 2016-05-21 20:23:48 +03:00
parent db6903eab7
commit 0b0f60dd89
4 changed files with 19 additions and 24 deletions

View File

@ -309,17 +309,6 @@ def load_signature(stream, original_data):
return signature, digest
def load_from_gpg(user_id, use_custom=False):
"""Load existing GPG public key for `user_id` from local keyring."""
args = ['gpg2', '--export'] + ([user_id] if user_id else [])
pubkey_bytes = subprocess.check_output(args=args)
if pubkey_bytes:
return load_public_key(io.BytesIO(pubkey_bytes), use_custom=use_custom)
else:
log.error('could not find public key %r in local GPG keyring', user_id)
raise KeyError(user_id)
def verify_digest(pubkey, digest, signature, label):
"""Verify a digest signature from a specified public key."""
verifier = pubkey['verifier']

View File

@ -89,12 +89,7 @@ class Factory(object):
@classmethod
def from_public_key(cls, pubkey, user_id):
"""
Create from an existing GPG public key.
`pubkey` should be loaded via `decode.load_from_gpg(user_id)`
from the local GPG keyring.
"""
"""Create from an existing GPG public key."""
s = cls(user_id=user_id,
created=pubkey['created'],
curve_name=proto.find_curve_by_algo_id(pubkey['algo']))
@ -144,7 +139,7 @@ class Factory(object):
def create_subkey(self):
"""Export new subkey to `self.user_id` GPG primary key."""
subkey_packet = proto.packet(tag=14, blob=self.pubkey.data())
primary = decode.load_from_gpg(self.user_id)
primary = keyring.get_public_key(self.user_id)
log.info('adding subkey to primary GPG key "%s" (%s)',
self.user_id, util.hexlify(primary['key_id']))
data_to_sign = primary['_to_hash'] + self.pubkey.data_to_hash()

View File

@ -7,7 +7,7 @@ import logging
import os
import re
import socket
import subprocess as sp
import subprocess
from . import decode
from .. import util
@ -18,7 +18,7 @@ log = logging.getLogger(__name__)
def connect_to_agent(sock_path='~/.gnupg/S.gpg-agent'):
"""Connect to GPG agent's UNIX socket."""
sock_path = os.path.expanduser(sock_path)
sp.check_call(['gpg-connect-agent', '/bye'])
subprocess.check_call(['gpg-connect-agent', '/bye'])
sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
sock.connect(sock_path)
return sock
@ -109,7 +109,7 @@ def sign_digest(sock, keygrip, digest):
assert _communicate(sock, 'RESET').startswith('OK')
ttyname = sp.check_output('tty').strip()
ttyname = subprocess.check_output('tty').strip()
options = ['ttyname={}'.format(ttyname)] # set TTY for passphrase entry
for opt in options:
assert _communicate(sock, 'OPTION {}'.format(opt)) == 'OK'
@ -138,5 +138,16 @@ def sign_digest(sock, keygrip, digest):
def get_keygrip(user_id):
"""Get a keygrip of the primary GPG key of the specified user."""
args = ['gpg2', '--list-keys', '--with-keygrip', user_id]
output = sp.check_output(args)
output = subprocess.check_output(args)
return re.findall(r'Keygrip = (\w+)', output)[0]
def get_public_key(user_id, use_custom=False):
"""Load existing GPG public key for `user_id` from local keyring."""
args = ['gpg2', '--export'] + ([user_id] if user_id else [])
pubkey_bytes = subprocess.check_output(args=args)
if pubkey_bytes:
return decode.load_public_key(io.BytesIO(pubkey_bytes), use_custom=use_custom)
else:
log.error('could not find public key %r in local GPG keyring', user_id)
raise KeyError(user_id)

View File

@ -8,7 +8,7 @@ import sys
import time
import os
from . import decode, encode, proto
from . import decode, encode, keyring, proto
log = logging.getLogger(__name__)
@ -32,7 +32,7 @@ def run_create(args):
def run_sign(args):
"""Generate a GPG signature using hardware-based device."""
pubkey = decode.load_from_gpg(user_id=None, use_custom=True)
pubkey = keyring.get_public_key(user_id=None, use_custom=True)
f = encode.Factory.from_public_key(pubkey=pubkey,
user_id=pubkey['user_id'])
with contextlib.closing(f):