-
Notifications
You must be signed in to change notification settings - Fork 122
/
openpgp.py
591 lines (484 loc) · 18.3 KB
/
openpgp.py
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
# Copyright (c) 2015 Yubico AB
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or
# without modification, are permitted provided that the following
# conditions are met:
#
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above
# copyright notice, this list of conditions and the following
# disclaimer in the documentation and/or other materials provided
# with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
# COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
from yubikit.core.smartcard import ApduError, SW, SmartCardConnection
from yubikit.openpgp import OpenPgpSession, UIF, PIN_POLICY, KEY_REF as _KEY_REF
from yubikit.management import CAPABILITY
from ..util import parse_certificates, parse_private_key
from ..openpgp import get_openpgp_info
from .util import (
CliFail,
click_force_option,
click_format_option,
click_postpone_execution,
click_prompt,
click_group,
EnumChoice,
pretty_print,
get_scp_params,
log_or_echo,
)
from enum import IntEnum
import logging
import click
logger = logging.getLogger(__name__)
class KEY_REF(IntEnum):
SIG = 0x01
DEC = 0x02
AUT = 0x03
ATT = 0x81
ENC = 0x02 # Alias for backwards compatibility, will be removed in ykman 6
def __getattribute__(self, name: str):
return _KEY_REF(self).__getattribute__(name)
def _fname(fobj):
return getattr(fobj, "name", fobj)
@click_group(connections=[SmartCardConnection])
@click.pass_context
@click_postpone_execution
def openpgp(ctx):
"""
Manage the OpenPGP application.
Examples:
\b
Set the retries for PIN, Reset Code and Admin PIN to 10:
$ ykman openpgp access set-retries 10 10 10
\b
Require touch to use the authentication key:
$ ykman openpgp keys set-touch aut on
"""
dev = ctx.obj["device"]
conn = dev.open_connection(SmartCardConnection)
ctx.call_on_close(conn.close)
scp_params = get_scp_params(ctx, CAPABILITY.OPENPGP, conn)
ctx.obj["session"] = OpenPgpSession(conn, scp_params)
@openpgp.command()
@click.pass_context
def info(ctx):
"""
Display general status of the OpenPGP application.
"""
info = ctx.obj["info"]
data = get_openpgp_info(ctx.obj["session"])
if CAPABILITY.OPENPGP in info.fips_capable:
# This is a bit ugly as it makes assumptions about the structure of data
data["FIPS approved"] = CAPABILITY.OPENPGP in info.fips_approved
click.echo("\n".join(pretty_print(data)))
@openpgp.command()
@click_force_option
@click.pass_context
def reset(ctx, force):
"""
Reset all OpenPGP data.
This action will wipe all OpenPGP data, and set all PINs to their default
values.
"""
force or click.confirm(
"WARNING! This will delete all stored OpenPGP keys and data and restore "
"factory settings. Proceed?",
abort=True,
err=True,
)
click.echo("Resetting OpenPGP data, don't remove the YubiKey...")
ctx.obj["session"].reset()
logger.info("OpenPGP application data reset")
click.echo(
"Reset complete. OpenPGP data has been cleared and default PINs are set."
)
echo_default_pins()
def echo_default_pins():
click.echo("PIN: 123456")
click.echo("Reset code: NOT SET")
click.echo("Admin PIN: 12345678")
@openpgp.group("access")
def access():
"""Manage PIN, Reset Code, and Admin PIN."""
@access.command("set-retries")
@click.argument("user-pin-retries", type=click.IntRange(1, 99), metavar="PIN-RETRIES")
@click.argument(
"reset-code-retries", type=click.IntRange(1, 99), metavar="RESET-CODE-RETRIES"
)
@click.argument(
"admin-pin-retries", type=click.IntRange(1, 99), metavar="ADMIN-PIN-RETRIES"
)
@click.option("-a", "--admin-pin", help="admin PIN for OpenPGP")
@click_force_option
@click.pass_context
def set_pin_retries(
ctx, admin_pin, user_pin_retries, reset_code_retries, admin_pin_retries, force
):
"""
Set the number of retry attempts for the User PIN, Reset Code, and Admin PIN.
"""
session = ctx.obj["session"]
if admin_pin is None:
admin_pin = click_prompt("Enter Admin PIN", hide_input=True)
resets_pins = session.version < (4, 0, 0)
if resets_pins:
click.echo("WARNING: Setting PIN retries will reset the values for all 3 PINs!")
if force or click.confirm(
f"Set PIN retry counters to: {user_pin_retries} {reset_code_retries} "
f"{admin_pin_retries}?",
abort=True,
err=True,
):
session.verify_admin(admin_pin)
session.set_pin_attempts(
user_pin_retries, reset_code_retries, admin_pin_retries
)
click.echo("Number of PIN/Reset Code/Admin PIN retries set.")
if resets_pins:
click.echo("Default values have been restored:")
echo_default_pins()
@access.command("change-pin")
@click.option("-P", "--pin", help="current PIN code")
@click.option("-n", "--new-pin", help="a new PIN")
@click.pass_context
def change_pin(ctx, pin, new_pin):
"""
Change the User PIN.
The PIN has a minimum length of 6 (or 8, for YubiKey 5.7+ FIPS when not using KDF),
and supports any type of alphanumeric characters.
"""
session = ctx.obj["session"]
if pin is None:
pin = click_prompt("Enter PIN", hide_input=True)
if new_pin is None:
new_pin = click_prompt(
"New PIN",
hide_input=True,
confirmation_prompt=True,
)
try:
session.change_pin(pin, new_pin)
click.echo("User PIN has been changed.")
except ApduError as e:
if e.sw == SW.CONDITIONS_NOT_SATISFIED:
raise CliFail("PIN does not meet complexity requirement.")
raise
@access.command("change-reset-code")
@click.option("-a", "--admin-pin", help="Admin PIN")
@click.option("-r", "--reset-code", help="a new Reset Code")
@click.pass_context
def change_reset_code(ctx, admin_pin, reset_code):
"""
Change the Reset Code.
The Reset Code has a minimum length of 6, and supports any type of
alphanumeric characters.
"""
session = ctx.obj["session"]
if admin_pin is None:
admin_pin = click_prompt("Enter Admin PIN", hide_input=True)
if reset_code is None:
reset_code = click_prompt(
"New Reset Code",
hide_input=True,
confirmation_prompt=True,
)
session.verify_admin(admin_pin)
try:
session.set_reset_code(reset_code)
click.echo("Reset Code has been changed.")
except ApduError as e:
if e.sw == SW.CONDITIONS_NOT_SATISFIED:
raise CliFail("Reset Code does not meet complexity requirement.")
raise
@access.command("change-admin-pin")
@click.option("-a", "--admin-pin", help="current Admin PIN")
@click.option("-n", "--new-admin-pin", help="new Admin PIN")
@click.pass_context
def change_admin(ctx, admin_pin, new_admin_pin):
"""
Change the Admin PIN.
The Admin PIN has a minimum length of 8, and supports any type of
alphanumeric characters.
"""
session = ctx.obj["session"]
if admin_pin is None:
admin_pin = click_prompt("Enter Admin PIN", hide_input=True)
if new_admin_pin is None:
new_admin_pin = click_prompt(
"New Admin PIN",
hide_input=True,
confirmation_prompt=True,
)
try:
session.change_admin(admin_pin, new_admin_pin)
click.echo("Admin PIN has been changed.")
except ApduError as e:
if e.sw == SW.CONDITIONS_NOT_SATISFIED:
raise CliFail("Admin PIN does not meet complexity requirement.")
raise
@access.command("unblock-pin")
@click.option(
"-a", "--admin-pin", help='Admin PIN (use "-" as a value to prompt for input)'
)
@click.option("-r", "--reset-code", help="Reset Code")
@click.option("-n", "--new-pin", help="a new PIN")
@click.pass_context
def unblock_pin(ctx, admin_pin, reset_code, new_pin):
"""
Unblock the PIN (using Reset Code or Admin PIN).
If the PIN is lost or blocked you can reset it to a new value using the Reset Code.
Alternatively, the Admin PIN can be used (using the "-a, --admin-pin" option)
instead of the Reset Code.
The new PIN has a minimum length of 6, and supports any type of
alphanumeric characters.
"""
session = ctx.obj["session"]
if reset_code is not None and admin_pin is not None:
raise CliFail(
"Invalid options: Only one of --reset-code and --admin-pin may be used."
)
if admin_pin == "-":
admin_pin = click_prompt("Enter Admin PIN", hide_input=True)
if reset_code is None and admin_pin is None:
reset_code = click_prompt("Enter Reset Code", hide_input=True)
if new_pin is None:
new_pin = click_prompt(
"New PIN",
hide_input=True,
confirmation_prompt=True,
)
if admin_pin:
session.verify_admin(admin_pin)
try:
session.reset_pin(new_pin, reset_code)
click.echo("User PIN has been changed.")
except ApduError as e:
if e.sw == SW.CONDITIONS_NOT_SATISFIED:
raise CliFail("New PIN does not meet complexity requirement.")
raise
@access.command("set-signature-policy")
@click.argument("policy", metavar="POLICY", type=EnumChoice(PIN_POLICY))
@click.option("-a", "--admin-pin", help="Admin PIN for OpenPGP")
@click.pass_context
def set_signature_policy(ctx, policy, admin_pin):
"""
Set the Signature PIN policy.
The Signature PIN policy is used to control whether the PIN is
always required when using the Signature key, or if it is required
only once per session.
\b
POLICY signature PIN policy to set (always, once)
"""
session = ctx.obj["session"]
if admin_pin is None:
admin_pin = click_prompt("Enter Admin PIN", hide_input=True)
try:
session.verify_admin(admin_pin)
session.set_signature_pin_policy(policy)
click.echo("Signature PIN policy has been set.")
except Exception:
raise CliFail("Failed to set new Signature PIN policy.")
@openpgp.group("keys")
def keys():
"""Manage private keys."""
@keys.command("set-touch")
@click.argument("key", metavar="KEY", type=EnumChoice(KEY_REF))
@click.argument("policy", metavar="POLICY", type=EnumChoice(UIF))
@click.option("-a", "--admin-pin", help="Admin PIN for OpenPGP")
@click_force_option
@click.pass_context
def set_touch(ctx, key, policy, admin_pin, force):
"""
Set the touch policy for OpenPGP keys.
The touch policy is used to require user interaction for all operations using the
private key on the YubiKey. The touch policy is set individually for each key slot.
To see the current touch policy, run the "openpgp info" subcommand.
Touch policies:
\b
Off (default) no touch required
On touch required
Fixed touch required, can't be disabled without deleting the private key
Cached touch required, cached for 15s after use
Cached-Fixed touch required, cached for 15s after use, can't be disabled
without deleting the private key
\b
KEY key slot to set (sig, dec, aut or att)
POLICY touch policy to set (on, off, fixed, cached or cached-fixed)
"""
session = ctx.obj["session"]
policy_name = policy.name.lower().replace("_", "-")
if admin_pin is None:
admin_pin = click_prompt("Enter Admin PIN", hide_input=True)
prompt = f"Set touch policy of {key.name} key to {policy_name}?"
if policy.is_fixed:
prompt = (
"WARNING: This touch policy cannot be changed without deleting the "
+ "corresponding key slot!\n"
+ prompt
)
if force or click.confirm(prompt, abort=True, err=True):
try:
session.verify_admin(admin_pin)
session.set_uif(key, policy)
click.echo(f"Touch policy for slot {key.name} set.")
except ApduError as e:
if e.sw == SW.SECURITY_CONDITION_NOT_SATISFIED:
raise CliFail("Touch policy not allowed.")
raise CliFail("Failed to set touch policy.")
@keys.command("import")
@click.option("-a", "--admin-pin", help="Admin PIN for OpenPGP")
@click.pass_context
@click.argument("key", metavar="KEY", type=EnumChoice(KEY_REF))
@click.argument("private-key", type=click.File("rb"), metavar="PRIVATE-KEY")
def import_key(ctx, key, private_key, admin_pin):
"""
Import a private key (ONLY SUPPORTS ATTESTATION KEY).
Import a private key for OpenPGP attestation.
\b
PRIVATE-KEY file containing the private key (use '-' to use stdin)
"""
session = ctx.obj["session"]
if key != KEY_REF.ATT:
ctx.fail("Importing keys is only supported for the Attestation slot.")
if admin_pin is None:
admin_pin = click_prompt("Enter Admin PIN", hide_input=True)
try:
private_key = parse_private_key(private_key.read(), password=None)
except Exception:
raise CliFail("Failed to parse private key.")
try:
session.verify_admin(admin_pin)
session.put_key(key, private_key)
click.echo(f"Private key imported for slot {key.name}.")
except Exception:
raise CliFail("Failed to import attestation key.")
@keys.command()
@click.pass_context
@click.option("-P", "--pin", help="PIN code")
@click_format_option
@click.argument("key", metavar="KEY", type=EnumChoice(KEY_REF, hidden=[KEY_REF.ATT]))
@click.argument("certificate", type=click.File("wb"), metavar="CERTIFICATE")
def attest(ctx, key, certificate, pin, format):
"""
Generate an attestation certificate for a key.
Attestation is used to show that an asymmetric key was generated on the
YubiKey and therefore doesn't exist outside the device.
\b
KEY key slot to attest (sig, dec, aut)
CERTIFICATE file to write attestation certificate to (use '-' to use stdout)
"""
session = ctx.obj["session"]
if not pin:
pin = click_prompt("Enter PIN", hide_input=True)
try:
cert = session.get_certificate(key)
except ValueError:
cert = None
if not cert or click.confirm(
f"There is already data stored in the certificate slot for {key.value}, "
"do you want to overwrite it?"
):
touch_policy = session.get_uif(KEY_REF.ATT)
if touch_policy in [UIF.ON, UIF.FIXED]:
click.echo("Touch the YubiKey sensor...")
try:
session.verify_pin(pin)
cert = session.attest_key(key)
certificate.write(cert.public_bytes(encoding=format))
log_or_echo(
f"Attestation certificate for slot {key.name} written to "
f"{_fname(certificate)}",
logger,
certificate,
)
except Exception:
raise CliFail("Attestation failed.")
@openpgp.group("certificates")
def certificates():
"""
Manage certificates.
"""
@certificates.command("export")
@click.pass_context
@click.argument("key", metavar="KEY", type=EnumChoice(KEY_REF))
@click_format_option
@click.argument("certificate", type=click.File("wb"), metavar="CERTIFICATE")
def export_certificate(ctx, key, format, certificate):
"""
Export an OpenPGP certificate.
\b
KEY key slot to read from (sig, dec, aut, or att)
CERTIFICATE file to write certificate to (use '-' to use stdout)
"""
session = ctx.obj["session"]
try:
cert = session.get_certificate(key)
except ValueError:
raise CliFail(f"Failed to read certificate from slot {key.name}.")
certificate.write(cert.public_bytes(encoding=format))
log_or_echo(
f"Certificate for slot {key.name} exported to {_fname(certificate)}",
logger,
certificate,
)
@certificates.command("delete")
@click.option("-a", "--admin-pin", help="Admin PIN for OpenPGP")
@click.pass_context
@click.argument("key", metavar="KEY", type=EnumChoice(KEY_REF))
def delete_certificate(ctx, key, admin_pin):
"""
Delete an OpenPGP certificate.
\b
KEY Key slot to delete certificate from (sig, dec, aut, or att).
"""
session = ctx.obj["session"]
if admin_pin is None:
admin_pin = click_prompt("Enter Admin PIN", hide_input=True)
try:
session.verify_admin(admin_pin)
session.delete_certificate(key)
click.echo(f"Certificate for slot {key.name} deleted.")
except Exception:
raise CliFail("Failed to delete certificate.")
@certificates.command("import")
@click.option("-a", "--admin-pin", help="Admin PIN for OpenPGP")
@click.pass_context
@click.argument("key", metavar="KEY", type=EnumChoice(KEY_REF))
@click.argument("cert", type=click.File("rb"), metavar="CERTIFICATE")
def import_certificate(ctx, key, cert, admin_pin):
"""
Import an OpenPGP certificate.
\b
KEY key slot to import certificate to (sig, dec, aut, or att)
CERTIFICATE file containing the certificate (use '-' to use stdin)
"""
session = ctx.obj["session"]
if admin_pin is None:
admin_pin = click_prompt("Enter Admin PIN", hide_input=True)
try:
certs = parse_certificates(cert.read(), password=None)
except Exception:
raise CliFail("Failed to parse certificate.")
if len(certs) != 1:
raise CliFail("Can only import one certificate.")
try:
session.verify_admin(admin_pin)
session.put_certificate(key, certs[0])
click.echo(f"Certificate imported into slot {key.name}")
except Exception:
raise CliFail("Failed to import certificate.")