mirror of
https://github.com/romanz/amodem.git
synced 2026-04-21 05:36:42 +08:00
gpg: rename load_from_gpg -> get_public_key
This commit is contained in:
@@ -309,17 +309,6 @@ def load_signature(stream, original_data):
|
|||||||
return signature, digest
|
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):
|
def verify_digest(pubkey, digest, signature, label):
|
||||||
"""Verify a digest signature from a specified public key."""
|
"""Verify a digest signature from a specified public key."""
|
||||||
verifier = pubkey['verifier']
|
verifier = pubkey['verifier']
|
||||||
|
|||||||
@@ -89,12 +89,7 @@ class Factory(object):
|
|||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def from_public_key(cls, pubkey, user_id):
|
def from_public_key(cls, pubkey, user_id):
|
||||||
"""
|
"""Create from an existing GPG public key."""
|
||||||
Create from an existing GPG public key.
|
|
||||||
|
|
||||||
`pubkey` should be loaded via `decode.load_from_gpg(user_id)`
|
|
||||||
from the local GPG keyring.
|
|
||||||
"""
|
|
||||||
s = cls(user_id=user_id,
|
s = cls(user_id=user_id,
|
||||||
created=pubkey['created'],
|
created=pubkey['created'],
|
||||||
curve_name=proto.find_curve_by_algo_id(pubkey['algo']))
|
curve_name=proto.find_curve_by_algo_id(pubkey['algo']))
|
||||||
@@ -144,7 +139,7 @@ class Factory(object):
|
|||||||
def create_subkey(self):
|
def create_subkey(self):
|
||||||
"""Export new subkey to `self.user_id` GPG primary key."""
|
"""Export new subkey to `self.user_id` GPG primary key."""
|
||||||
subkey_packet = proto.packet(tag=14, blob=self.pubkey.data())
|
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)',
|
log.info('adding subkey to primary GPG key "%s" (%s)',
|
||||||
self.user_id, util.hexlify(primary['key_id']))
|
self.user_id, util.hexlify(primary['key_id']))
|
||||||
data_to_sign = primary['_to_hash'] + self.pubkey.data_to_hash()
|
data_to_sign = primary['_to_hash'] + self.pubkey.data_to_hash()
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ import logging
|
|||||||
import os
|
import os
|
||||||
import re
|
import re
|
||||||
import socket
|
import socket
|
||||||
import subprocess as sp
|
import subprocess
|
||||||
|
|
||||||
from . import decode
|
from . import decode
|
||||||
from .. import util
|
from .. import util
|
||||||
@@ -18,7 +18,7 @@ log = logging.getLogger(__name__)
|
|||||||
def connect_to_agent(sock_path='~/.gnupg/S.gpg-agent'):
|
def connect_to_agent(sock_path='~/.gnupg/S.gpg-agent'):
|
||||||
"""Connect to GPG agent's UNIX socket."""
|
"""Connect to GPG agent's UNIX socket."""
|
||||||
sock_path = os.path.expanduser(sock_path)
|
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 = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
|
||||||
sock.connect(sock_path)
|
sock.connect(sock_path)
|
||||||
return sock
|
return sock
|
||||||
@@ -109,7 +109,7 @@ def sign_digest(sock, keygrip, digest):
|
|||||||
|
|
||||||
assert _communicate(sock, 'RESET').startswith('OK')
|
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
|
options = ['ttyname={}'.format(ttyname)] # set TTY for passphrase entry
|
||||||
for opt in options:
|
for opt in options:
|
||||||
assert _communicate(sock, 'OPTION {}'.format(opt)) == 'OK'
|
assert _communicate(sock, 'OPTION {}'.format(opt)) == 'OK'
|
||||||
@@ -138,5 +138,16 @@ def sign_digest(sock, keygrip, digest):
|
|||||||
def get_keygrip(user_id):
|
def get_keygrip(user_id):
|
||||||
"""Get a keygrip of the primary GPG key of the specified user."""
|
"""Get a keygrip of the primary GPG key of the specified user."""
|
||||||
args = ['gpg2', '--list-keys', '--with-keygrip', user_id]
|
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]
|
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)
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ import sys
|
|||||||
import time
|
import time
|
||||||
import os
|
import os
|
||||||
|
|
||||||
from . import decode, encode, proto
|
from . import decode, encode, keyring, proto
|
||||||
|
|
||||||
log = logging.getLogger(__name__)
|
log = logging.getLogger(__name__)
|
||||||
|
|
||||||
@@ -32,7 +32,7 @@ def run_create(args):
|
|||||||
|
|
||||||
def run_sign(args):
|
def run_sign(args):
|
||||||
"""Generate a GPG signature using hardware-based device."""
|
"""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,
|
f = encode.Factory.from_public_key(pubkey=pubkey,
|
||||||
user_id=pubkey['user_id'])
|
user_id=pubkey['user_id'])
|
||||||
with contextlib.closing(f):
|
with contextlib.closing(f):
|
||||||
|
|||||||
Reference in New Issue
Block a user