lib-agent 1.0.9__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- lib_agent-1.0.9.dist-info/METADATA +44 -0
- lib_agent-1.0.9.dist-info/RECORD +35 -0
- lib_agent-1.0.9.dist-info/WHEEL +5 -0
- lib_agent-1.0.9.dist-info/licenses/LICENSE +166 -0
- lib_agent-1.0.9.dist-info/top_level.txt +1 -0
- libagent/__init__.py +1 -0
- libagent/age/__init__.py +180 -0
- libagent/age/client.py +48 -0
- libagent/device/__init__.py +3 -0
- libagent/device/fake_device.py +74 -0
- libagent/device/interface.py +155 -0
- libagent/device/jade.py +143 -0
- libagent/device/keepkey.py +45 -0
- libagent/device/keepkey_defs.py +24 -0
- libagent/device/ledger.py +178 -0
- libagent/device/onlykey.py +613 -0
- libagent/device/onlykey_defs.py +10 -0
- libagent/device/onlykey_pqc.py +153 -0
- libagent/device/trezor.py +154 -0
- libagent/device/trezor_defs.py +30 -0
- libagent/device/ui.py +159 -0
- libagent/formats.py +274 -0
- libagent/gpg/__init__.py +439 -0
- libagent/gpg/agent.py +385 -0
- libagent/gpg/client.py +55 -0
- libagent/gpg/decode.py +346 -0
- libagent/gpg/encode.py +103 -0
- libagent/gpg/keyring.py +270 -0
- libagent/gpg/protocol.py +291 -0
- libagent/server.py +400 -0
- libagent/signify/__init__.py +127 -0
- libagent/ssh/__init__.py +379 -0
- libagent/ssh/client.py +92 -0
- libagent/ssh/protocol.py +179 -0
- libagent/util.py +292 -0
libagent/gpg/decode.py
ADDED
|
@@ -0,0 +1,346 @@
|
|
|
1
|
+
"""Decoders for GPG v2 data structures."""
|
|
2
|
+
import base64
|
|
3
|
+
import functools
|
|
4
|
+
import hashlib
|
|
5
|
+
import io
|
|
6
|
+
import logging
|
|
7
|
+
import struct
|
|
8
|
+
|
|
9
|
+
import ecdsa
|
|
10
|
+
import nacl.signing
|
|
11
|
+
|
|
12
|
+
from .. import util
|
|
13
|
+
from . import protocol
|
|
14
|
+
|
|
15
|
+
log = logging.getLogger(__name__)
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
def parse_subpackets(s):
|
|
19
|
+
"""See https://tools.ietf.org/html/rfc4880#section-5.2.3.1 for details."""
|
|
20
|
+
subpackets = []
|
|
21
|
+
total_size = s.readfmt('>H')
|
|
22
|
+
data = s.read(total_size)
|
|
23
|
+
s = util.Reader(io.BytesIO(data))
|
|
24
|
+
|
|
25
|
+
while True:
|
|
26
|
+
try:
|
|
27
|
+
first = s.readfmt('B')
|
|
28
|
+
except EOFError:
|
|
29
|
+
break
|
|
30
|
+
|
|
31
|
+
if first < 192:
|
|
32
|
+
subpacket_len = first
|
|
33
|
+
elif first < 255:
|
|
34
|
+
subpacket_len = ((first - 192) << 8) + s.readfmt('B') + 192
|
|
35
|
+
else: # first == 255
|
|
36
|
+
subpacket_len = s.readfmt('>L')
|
|
37
|
+
|
|
38
|
+
subpackets.append(s.read(subpacket_len))
|
|
39
|
+
|
|
40
|
+
return subpackets
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
def parse_mpi(s):
|
|
44
|
+
"""See https://tools.ietf.org/html/rfc4880#section-3.2 for details."""
|
|
45
|
+
bits = s.readfmt('>H')
|
|
46
|
+
blob = bytearray(s.read(int((bits + 7) // 8)))
|
|
47
|
+
return sum(v << (8 * i) for i, v in enumerate(reversed(blob)))
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
def parse_mpis(s, n):
|
|
51
|
+
"""Parse multiple MPIs from stream."""
|
|
52
|
+
return [parse_mpi(s) for _ in range(n)]
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
def _parse_nist256p1_pubkey(mpi):
|
|
56
|
+
prefix, x, y = util.split_bits(mpi, 4, 256, 256)
|
|
57
|
+
if prefix != 4:
|
|
58
|
+
raise ValueError('Invalid MPI prefix: {}'.format(prefix))
|
|
59
|
+
point = ecdsa.ellipticcurve.Point(curve=ecdsa.NIST256p.curve,
|
|
60
|
+
x=x, y=y)
|
|
61
|
+
return ecdsa.VerifyingKey.from_public_point(
|
|
62
|
+
point=point, curve=ecdsa.curves.NIST256p,
|
|
63
|
+
hashfunc=hashlib.sha256)
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
def _parse_ed25519_pubkey(mpi):
|
|
67
|
+
prefix, value = util.split_bits(mpi, 8, 256)
|
|
68
|
+
if prefix != 0x40:
|
|
69
|
+
raise ValueError('Invalid MPI prefix: {}'.format(prefix))
|
|
70
|
+
vk = nacl.signing.VerifyKey(util.num2bytes(value, size=32), encoder=nacl.encoding.RawEncoder)
|
|
71
|
+
return vk
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
SUPPORTED_CURVES = {
|
|
75
|
+
b'\x2A\x86\x48\xCE\x3D\x03\x01\x07':
|
|
76
|
+
(_parse_nist256p1_pubkey, protocol.keygrip_nist256),
|
|
77
|
+
b'\x2B\x06\x01\x04\x01\xDA\x47\x0F\x01':
|
|
78
|
+
(_parse_ed25519_pubkey, protocol.keygrip_ed25519),
|
|
79
|
+
b'\x2B\x06\x01\x04\x01\x97\x55\x01\x05\x01':
|
|
80
|
+
(_parse_ed25519_pubkey, protocol.keygrip_curve25519),
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
RSA_ALGO_IDS = {1, 2, 3}
|
|
84
|
+
ELGAMAL_ALGO_ID = 16
|
|
85
|
+
DSA_ALGO_ID = 17
|
|
86
|
+
ECDSA_ALGO_IDS = {18, 19, 22} # {ecdsa, nist256, ed25519}
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
def _parse_embedded_signatures(subpackets):
|
|
90
|
+
for packet in subpackets:
|
|
91
|
+
data = bytearray(packet)
|
|
92
|
+
if data[0] == 32:
|
|
93
|
+
# https://tools.ietf.org/html/rfc4880#section-5.2.3.26
|
|
94
|
+
stream = io.BytesIO(data[1:])
|
|
95
|
+
yield _parse_signature(util.Reader(stream))
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
def has_custom_subpacket(signature_packet):
|
|
99
|
+
"""Detect our custom public keys by matching subpacket data."""
|
|
100
|
+
return any(protocol.CUSTOM_KEY_LABEL == subpacket[1:]
|
|
101
|
+
for subpacket in signature_packet['unhashed_subpackets'])
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+
def _parse_signature(stream):
|
|
105
|
+
"""See https://tools.ietf.org/html/rfc4880#section-5.2 for details."""
|
|
106
|
+
p = {'type': 'signature'}
|
|
107
|
+
|
|
108
|
+
to_hash = io.BytesIO()
|
|
109
|
+
with stream.capture(to_hash):
|
|
110
|
+
p['version'] = stream.readfmt('B')
|
|
111
|
+
p['sig_type'] = stream.readfmt('B')
|
|
112
|
+
p['pubkey_alg'] = stream.readfmt('B')
|
|
113
|
+
p['hash_alg'] = stream.readfmt('B')
|
|
114
|
+
p['hashed_subpackets'] = parse_subpackets(stream)
|
|
115
|
+
|
|
116
|
+
# https://tools.ietf.org/html/rfc4880#section-5.2.4
|
|
117
|
+
tail_to_hash = b'\x04\xff' + struct.pack('>L', to_hash.tell())
|
|
118
|
+
|
|
119
|
+
p['_to_hash'] = to_hash.getvalue() + tail_to_hash
|
|
120
|
+
|
|
121
|
+
p['unhashed_subpackets'] = parse_subpackets(stream)
|
|
122
|
+
embedded = list(_parse_embedded_signatures(p['unhashed_subpackets']))
|
|
123
|
+
if embedded:
|
|
124
|
+
log.debug('embedded sigs: %s', embedded)
|
|
125
|
+
p['embedded'] = embedded
|
|
126
|
+
|
|
127
|
+
p['hash_prefix'] = stream.readfmt('2s')
|
|
128
|
+
if p['pubkey_alg'] in ECDSA_ALGO_IDS:
|
|
129
|
+
p['sig'] = (parse_mpi(stream), parse_mpi(stream))
|
|
130
|
+
elif p['pubkey_alg'] in RSA_ALGO_IDS: # RSA
|
|
131
|
+
p['sig'] = (parse_mpi(stream),)
|
|
132
|
+
elif p['pubkey_alg'] == DSA_ALGO_ID:
|
|
133
|
+
p['sig'] = (parse_mpi(stream), parse_mpi(stream))
|
|
134
|
+
else:
|
|
135
|
+
log.error('unsupported public key algo: %d', p['pubkey_alg'])
|
|
136
|
+
|
|
137
|
+
assert not stream.read()
|
|
138
|
+
return p
|
|
139
|
+
|
|
140
|
+
|
|
141
|
+
def _parse_pubkey(stream, packet_type='pubkey'):
|
|
142
|
+
"""See https://tools.ietf.org/html/rfc4880#section-5.5 for details."""
|
|
143
|
+
p = {'type': packet_type}
|
|
144
|
+
packet = io.BytesIO()
|
|
145
|
+
with stream.capture(packet):
|
|
146
|
+
p['version'] = stream.readfmt('B')
|
|
147
|
+
p['created'] = stream.readfmt('>L')
|
|
148
|
+
p['algo'] = stream.readfmt('B')
|
|
149
|
+
if p['algo'] in ECDSA_ALGO_IDS:
|
|
150
|
+
log.debug('parsing elliptic curve key')
|
|
151
|
+
# https://tools.ietf.org/html/rfc6637#section-11
|
|
152
|
+
oid_size = stream.readfmt('B')
|
|
153
|
+
oid = stream.read(oid_size)
|
|
154
|
+
assert oid in SUPPORTED_CURVES, util.hexlify(oid)
|
|
155
|
+
p['curve_oid'] = oid
|
|
156
|
+
|
|
157
|
+
mpi = parse_mpi(stream)
|
|
158
|
+
log.debug('mpi: %x (%d bits)', mpi, mpi.bit_length())
|
|
159
|
+
leftover = stream.read()
|
|
160
|
+
if leftover:
|
|
161
|
+
leftover = io.BytesIO(leftover)
|
|
162
|
+
# https://tools.ietf.org/html/rfc6637#section-8
|
|
163
|
+
# should be b'\x03\x01\x08\x07': SHA256 + AES128
|
|
164
|
+
size, = util.readfmt(leftover, 'B')
|
|
165
|
+
p['kdf'] = leftover.read(size)
|
|
166
|
+
p['secret'] = leftover.read()
|
|
167
|
+
|
|
168
|
+
parse_func, keygrip_func = SUPPORTED_CURVES[oid]
|
|
169
|
+
keygrip = keygrip_func(parse_func(mpi))
|
|
170
|
+
log.debug('keygrip: %s', util.hexlify(keygrip))
|
|
171
|
+
p['keygrip'] = keygrip
|
|
172
|
+
|
|
173
|
+
elif p['algo'] == DSA_ALGO_ID:
|
|
174
|
+
parse_mpis(stream, n=4) # DSA keys are not supported
|
|
175
|
+
elif p['algo'] == ELGAMAL_ALGO_ID:
|
|
176
|
+
parse_mpis(stream, n=3) # ElGamal keys are not supported
|
|
177
|
+
elif p['algo'] in RSA_ALGO_IDS:
|
|
178
|
+
log.debug('parsing rsa key')
|
|
179
|
+
mpi = parse_mpi(stream) # RSA key
|
|
180
|
+
log.debug('mpi: %d (%d bits)', mpi, mpi.bit_length())
|
|
181
|
+
keygrip = protocol.keygrip_rsa(mpi, mpi.bit_length())
|
|
182
|
+
log.debug('keygrip: %s', util.hexlify(keygrip))
|
|
183
|
+
p['keygrip'] = keygrip
|
|
184
|
+
leftover = stream.read()
|
|
185
|
+
if leftover:
|
|
186
|
+
leftover = io.BytesIO(leftover)
|
|
187
|
+
# https://tools.ietf.org/html/rfc6637#section-8
|
|
188
|
+
# should be b'\x03\x01\x08\x07': SHA256 + AES128
|
|
189
|
+
size, = util.readfmt(leftover, 'B')
|
|
190
|
+
p['kdf'] = leftover.read(size)
|
|
191
|
+
p['secret'] = leftover.read()
|
|
192
|
+
|
|
193
|
+
assert not stream.read()
|
|
194
|
+
|
|
195
|
+
# https://tools.ietf.org/html/rfc4880#section-12.2
|
|
196
|
+
packet_data = packet.getvalue()
|
|
197
|
+
data_to_hash = (b'\x99' + struct.pack('>H', len(packet_data)) +
|
|
198
|
+
packet_data)
|
|
199
|
+
p['key_id'] = hashlib.sha1(data_to_hash).digest()[-8:]
|
|
200
|
+
p['_to_hash'] = data_to_hash
|
|
201
|
+
log.debug('key ID: %s', util.hexlify(p['key_id']))
|
|
202
|
+
return p
|
|
203
|
+
|
|
204
|
+
|
|
205
|
+
_parse_subkey = functools.partial(_parse_pubkey, packet_type='subkey')
|
|
206
|
+
|
|
207
|
+
|
|
208
|
+
def _parse_user_id(stream, packet_type='user_id'):
|
|
209
|
+
"""See https://tools.ietf.org/html/rfc4880#section-5.11 for details."""
|
|
210
|
+
value = stream.read()
|
|
211
|
+
to_hash = b'\xb4' + util.prefix_len('>L', value)
|
|
212
|
+
return {'type': packet_type, 'value': value, '_to_hash': to_hash}
|
|
213
|
+
|
|
214
|
+
|
|
215
|
+
# User attribute is handled as an opaque user ID
|
|
216
|
+
_parse_attribute = functools.partial(_parse_user_id,
|
|
217
|
+
packet_type='user_attribute')
|
|
218
|
+
|
|
219
|
+
PACKET_TYPES = {
|
|
220
|
+
2: _parse_signature,
|
|
221
|
+
5: _parse_pubkey,
|
|
222
|
+
6: _parse_pubkey,
|
|
223
|
+
7: _parse_subkey,
|
|
224
|
+
13: _parse_user_id,
|
|
225
|
+
14: _parse_subkey,
|
|
226
|
+
17: _parse_attribute,
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
|
|
230
|
+
def parse_packets(stream):
|
|
231
|
+
"""
|
|
232
|
+
Support iterative parsing of available GPG packets.
|
|
233
|
+
|
|
234
|
+
See https://tools.ietf.org/html/rfc4880#section-4.2 for details.
|
|
235
|
+
"""
|
|
236
|
+
reader = util.Reader(stream)
|
|
237
|
+
while True:
|
|
238
|
+
try:
|
|
239
|
+
value = reader.readfmt('B')
|
|
240
|
+
except EOFError:
|
|
241
|
+
return
|
|
242
|
+
|
|
243
|
+
log.debug('prefix byte: %s', bin(value))
|
|
244
|
+
assert util.bit(value, 7) == 1
|
|
245
|
+
|
|
246
|
+
tag = util.low_bits(value, 6)
|
|
247
|
+
if util.bit(value, 6) == 0:
|
|
248
|
+
length_type = util.low_bits(tag, 2)
|
|
249
|
+
tag = tag >> 2
|
|
250
|
+
fmt = {0: '>B', 1: '>H', 2: '>L'}[length_type]
|
|
251
|
+
packet_size = reader.readfmt(fmt)
|
|
252
|
+
else:
|
|
253
|
+
first = reader.readfmt('B')
|
|
254
|
+
if first < 192:
|
|
255
|
+
packet_size = first
|
|
256
|
+
elif first < 224:
|
|
257
|
+
packet_size = ((first - 192) << 8) + reader.readfmt('B') + 192
|
|
258
|
+
elif first == 255:
|
|
259
|
+
packet_size = reader.readfmt('>L')
|
|
260
|
+
else:
|
|
261
|
+
log.error('Partial Body Lengths unsupported')
|
|
262
|
+
|
|
263
|
+
log.debug('packet length: %d', packet_size)
|
|
264
|
+
packet_data = reader.read(packet_size)
|
|
265
|
+
packet_type = PACKET_TYPES.get(tag)
|
|
266
|
+
|
|
267
|
+
p = {'type': 'unknown', 'tag': tag, 'raw': packet_data}
|
|
268
|
+
if packet_type is not None:
|
|
269
|
+
try:
|
|
270
|
+
p = packet_type(util.Reader(io.BytesIO(packet_data)))
|
|
271
|
+
p['tag'] = tag
|
|
272
|
+
except ValueError:
|
|
273
|
+
log.exception('Skipping packet: %s', util.hexlify(packet_data))
|
|
274
|
+
|
|
275
|
+
log.debug('packet "%s": %s', p['type'], p)
|
|
276
|
+
yield p
|
|
277
|
+
|
|
278
|
+
|
|
279
|
+
def digest_packets(packets, hasher):
|
|
280
|
+
"""Compute digest on specified packets, according to '_to_hash' field."""
|
|
281
|
+
data_to_hash = io.BytesIO()
|
|
282
|
+
for p in packets:
|
|
283
|
+
data_to_hash.write(p['_to_hash'])
|
|
284
|
+
hasher.update(data_to_hash.getvalue())
|
|
285
|
+
return hasher.digest()
|
|
286
|
+
|
|
287
|
+
|
|
288
|
+
HASH_ALGORITHMS = {
|
|
289
|
+
1: 'md5',
|
|
290
|
+
2: 'sha1',
|
|
291
|
+
3: 'ripemd160',
|
|
292
|
+
8: 'sha256',
|
|
293
|
+
9: 'sha384',
|
|
294
|
+
10: 'sha512',
|
|
295
|
+
11: 'sha224',
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
|
|
299
|
+
def _parse_pubkey_packets(pubkey_bytes):
|
|
300
|
+
stream = io.BytesIO(pubkey_bytes)
|
|
301
|
+
packets_per_pubkey = []
|
|
302
|
+
for p in parse_packets(stream):
|
|
303
|
+
if p['type'] == 'pubkey':
|
|
304
|
+
# Add a new packet list for each pubkey.
|
|
305
|
+
packets_per_pubkey.append([])
|
|
306
|
+
packets_per_pubkey[-1].append(p)
|
|
307
|
+
return packets_per_pubkey
|
|
308
|
+
|
|
309
|
+
|
|
310
|
+
def load_by_keygrip(pubkey_bytes, keygrip):
|
|
311
|
+
"""Return public key and first user ID for specified keygrip."""
|
|
312
|
+
for packets in _parse_pubkey_packets(pubkey_bytes):
|
|
313
|
+
user_ids = [p for p in packets if p['type'] == 'user_id']
|
|
314
|
+
for p in packets:
|
|
315
|
+
if p.get('keygrip') == keygrip:
|
|
316
|
+
return p, user_ids
|
|
317
|
+
raise KeyError('{} keygrip not found'.format(util.hexlify(keygrip)))
|
|
318
|
+
|
|
319
|
+
|
|
320
|
+
def iter_keygrips(pubkey_bytes):
|
|
321
|
+
"""Iterate over all keygrips in this pubkey."""
|
|
322
|
+
for packets in _parse_pubkey_packets(pubkey_bytes):
|
|
323
|
+
for p in packets:
|
|
324
|
+
keygrip = p.get('keygrip')
|
|
325
|
+
if keygrip:
|
|
326
|
+
yield keygrip
|
|
327
|
+
|
|
328
|
+
|
|
329
|
+
def load_signature(stream, original_data):
|
|
330
|
+
"""Load signature from stream, and compute GPG digest for verification."""
|
|
331
|
+
signature, = list(parse_packets((stream)))
|
|
332
|
+
hash_alg = HASH_ALGORITHMS[signature['hash_alg']]
|
|
333
|
+
digest = digest_packets([{'_to_hash': original_data}, signature],
|
|
334
|
+
hasher=hashlib.new(hash_alg))
|
|
335
|
+
assert signature['hash_prefix'] == digest[:2]
|
|
336
|
+
return signature, digest
|
|
337
|
+
|
|
338
|
+
|
|
339
|
+
def remove_armor(armored_data):
|
|
340
|
+
"""Decode armored data into its binary form."""
|
|
341
|
+
stream = io.BytesIO(armored_data)
|
|
342
|
+
lines = stream.readlines()[3:-1]
|
|
343
|
+
data = base64.b64decode(b''.join(lines))
|
|
344
|
+
payload, checksum = data[:-3], data[-3:]
|
|
345
|
+
assert util.crc24(payload) == checksum
|
|
346
|
+
return payload
|
libagent/gpg/encode.py
ADDED
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
"""Create GPG ECDSA signatures and public keys using TREZOR device."""
|
|
2
|
+
import io
|
|
3
|
+
import logging
|
|
4
|
+
|
|
5
|
+
from .. import util
|
|
6
|
+
from . import decode, keyring, protocol
|
|
7
|
+
|
|
8
|
+
log = logging.getLogger(__name__)
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
def create_primary(user_id, pubkey, signer_func, secret_bytes=b''):
|
|
12
|
+
"""Export new primary GPG public key, ready for "gpg2 --import"."""
|
|
13
|
+
pubkey_packet = protocol.packet(tag=(5 if secret_bytes else 6),
|
|
14
|
+
blob=pubkey.data() + secret_bytes)
|
|
15
|
+
user_id_bytes = user_id.encode('utf-8')
|
|
16
|
+
user_id_packet = protocol.packet(tag=13, blob=user_id_bytes)
|
|
17
|
+
data_to_sign = (pubkey.data_to_hash() + user_id_packet[:1] +
|
|
18
|
+
util.prefix_len('>L', user_id_bytes))
|
|
19
|
+
hashed_subpackets = [
|
|
20
|
+
protocol.subpacket_time(pubkey.created), # signature time
|
|
21
|
+
# https://tools.ietf.org/html/rfc4880#section-5.2.3.7
|
|
22
|
+
protocol.subpacket_byte(0x0B, 9), # preferred symmetric algo (AES-256)
|
|
23
|
+
# https://tools.ietf.org/html/rfc4880#section-5.2.3.4
|
|
24
|
+
protocol.subpacket_byte(0x1B, 1 | 2), # key flags (certify & sign)
|
|
25
|
+
# https://tools.ietf.org/html/rfc4880#section-5.2.3.21
|
|
26
|
+
protocol.subpacket_bytes(0x15, [8, 9, 10]), # preferred hash
|
|
27
|
+
# https://tools.ietf.org/html/rfc4880#section-5.2.3.8
|
|
28
|
+
protocol.subpacket_bytes(0x16, [2, 3, 1]), # preferred compression
|
|
29
|
+
# https://tools.ietf.org/html/rfc4880#section-5.2.3.9
|
|
30
|
+
protocol.subpacket_byte(0x17, 0x80), # key server prefs (no-modify)
|
|
31
|
+
# https://tools.ietf.org/html/rfc4880#section-5.2.3.17
|
|
32
|
+
protocol.subpacket_byte(0x1E, 0x01), # advanced features (MDC)
|
|
33
|
+
# https://tools.ietf.org/html/rfc4880#section-5.2.3.24
|
|
34
|
+
]
|
|
35
|
+
unhashed_subpackets = [
|
|
36
|
+
protocol.subpacket(16, pubkey.key_id()), # issuer key id
|
|
37
|
+
protocol.CUSTOM_SUBPACKET]
|
|
38
|
+
|
|
39
|
+
signature = protocol.make_signature(
|
|
40
|
+
signer_func=signer_func,
|
|
41
|
+
public_algo=pubkey.algo_id,
|
|
42
|
+
data_to_sign=data_to_sign,
|
|
43
|
+
sig_type=0x13, # user id & public key
|
|
44
|
+
hashed_subpackets=hashed_subpackets,
|
|
45
|
+
unhashed_subpackets=unhashed_subpackets)
|
|
46
|
+
|
|
47
|
+
sign_packet = protocol.packet(tag=2, blob=signature)
|
|
48
|
+
return pubkey_packet + user_id_packet + sign_packet
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
def create_subkey(primary_bytes, subkey, signer_func, secret_bytes=b''):
|
|
52
|
+
"""Export new subkey to GPG primary key."""
|
|
53
|
+
subkey_packet = protocol.packet(tag=(7 if secret_bytes else 14),
|
|
54
|
+
blob=subkey.data() + secret_bytes)
|
|
55
|
+
packets = list(decode.parse_packets(io.BytesIO(primary_bytes)))
|
|
56
|
+
primary, user_id, signature = packets[:3]
|
|
57
|
+
|
|
58
|
+
data_to_sign = primary['_to_hash'] + subkey.data_to_hash()
|
|
59
|
+
|
|
60
|
+
if subkey.ecdh:
|
|
61
|
+
embedded_sig = None
|
|
62
|
+
else:
|
|
63
|
+
# Primary Key Binding Signature
|
|
64
|
+
hashed_subpackets = [
|
|
65
|
+
protocol.subpacket_time(subkey.created)] # signature time
|
|
66
|
+
unhashed_subpackets = [
|
|
67
|
+
protocol.subpacket(16, subkey.key_id())] # issuer key id
|
|
68
|
+
embedded_sig = protocol.make_signature(
|
|
69
|
+
signer_func=signer_func,
|
|
70
|
+
data_to_sign=data_to_sign,
|
|
71
|
+
public_algo=subkey.algo_id,
|
|
72
|
+
sig_type=0x19,
|
|
73
|
+
hashed_subpackets=hashed_subpackets,
|
|
74
|
+
unhashed_subpackets=unhashed_subpackets)
|
|
75
|
+
|
|
76
|
+
# Subkey Binding Signature
|
|
77
|
+
|
|
78
|
+
# Key flags: https://tools.ietf.org/html/rfc4880#section-5.2.3.21
|
|
79
|
+
# (certify & sign) (encrypt)
|
|
80
|
+
flags = (2) if (not subkey.ecdh) else (4 | 8)
|
|
81
|
+
|
|
82
|
+
hashed_subpackets = [
|
|
83
|
+
protocol.subpacket_time(subkey.created), # signature time
|
|
84
|
+
protocol.subpacket_byte(0x1B, flags)]
|
|
85
|
+
|
|
86
|
+
unhashed_subpackets = []
|
|
87
|
+
unhashed_subpackets.append(protocol.subpacket(16, primary['key_id']))
|
|
88
|
+
if embedded_sig is not None:
|
|
89
|
+
unhashed_subpackets.append(protocol.subpacket(32, embedded_sig))
|
|
90
|
+
unhashed_subpackets.append(protocol.CUSTOM_SUBPACKET)
|
|
91
|
+
|
|
92
|
+
if not decode.has_custom_subpacket(signature):
|
|
93
|
+
signer_func = keyring.create_agent_signer(user_id['value'])
|
|
94
|
+
|
|
95
|
+
signature = protocol.make_signature(
|
|
96
|
+
signer_func=signer_func,
|
|
97
|
+
data_to_sign=data_to_sign,
|
|
98
|
+
public_algo=primary['algo'],
|
|
99
|
+
sig_type=0x18,
|
|
100
|
+
hashed_subpackets=hashed_subpackets,
|
|
101
|
+
unhashed_subpackets=unhashed_subpackets)
|
|
102
|
+
sign_packet = protocol.packet(tag=2, blob=signature)
|
|
103
|
+
return primary_bytes + subkey_packet + sign_packet
|