Skip to content

Commit 95ad3ee

Browse files
committed
pivy-tool: add delete-cert command
1 parent d1b9678 commit 95ad3ee

File tree

2 files changed

+92
-9
lines changed

2 files changed

+92
-9
lines changed

piv.c

+13-9
Original file line numberDiff line numberDiff line change
@@ -3135,17 +3135,21 @@ piv_write_cert(struct piv_token *pk, enum piv_slotid slotid,
31353135
"%02x", slotid));
31363136
}
31373137

3138-
tlv = tlv_init_write();
3139-
tlv_push(tlv, 0x70);
3140-
tlv_write(tlv, data, datalen);
3141-
tlv_pop(tlv);
3142-
tlv_push(tlv, 0x71);
3143-
tlv_write_byte(tlv, (uint8_t)flags);
3144-
tlv_pop(tlv);
3138+
if (data != NULL && datalen > 0) {
3139+
tlv = tlv_init_write();
3140+
tlv_push(tlv, 0x70);
3141+
tlv_write(tlv, data, datalen);
3142+
tlv_pop(tlv);
3143+
tlv_push(tlv, 0x71);
3144+
tlv_write_byte(tlv, (uint8_t)flags);
3145+
tlv_pop(tlv);
31453146

3146-
err = piv_write_file(pk, tag, tlv_buf(tlv), tlv_len(tlv));
3147+
err = piv_write_file(pk, tag, tlv_buf(tlv), tlv_len(tlv));
31473148

3148-
tlv_free(tlv);
3149+
tlv_free(tlv);
3150+
} else {
3151+
err = piv_write_file(pk, tag, NULL, 0);
3152+
}
31493153

31503154
return (err);
31513155
}

pivy-tool.c

+79
Original file line numberDiff line numberDiff line change
@@ -1734,6 +1734,62 @@ cmd_cert(uint slotid)
17341734
return (ERRF_OK);
17351735
}
17361736

1737+
static errf_t *
1738+
cmd_delete_cert(uint slotid)
1739+
{
1740+
errf_t *err;
1741+
1742+
assert_slotid(slotid);
1743+
1744+
if ((err = piv_txn_begin(selk)))
1745+
errfx(1, err, "failed to open transaction");
1746+
assert_select(selk);
1747+
admin_again:
1748+
err = piv_auth_admin(selk, admin_key, key_length, key_alg);
1749+
if (err && (errf_caused_by(err, "PermissionError") ||
1750+
errf_caused_by(err, "ArgumentError")) &&
1751+
admin_key == DEFAULT_ADMIN_KEY) {
1752+
errf_free(err);
1753+
err = try_pinfo_admin_key(selk);
1754+
if (err == ERRF_OK)
1755+
goto admin_again;
1756+
}
1757+
1758+
if (err == ERRF_OK)
1759+
err = piv_write_cert(selk, slotid, NULL, 0, PIV_COMP_NONE);
1760+
1761+
if (err == ERRF_OK && slotid >= 0x82 && slotid <= 0x95 &&
1762+
piv_token_keyhistory_oncard(selk) >= slotid - 0x82) {
1763+
uint oncard, offcard;
1764+
const char *url;
1765+
1766+
oncard = piv_token_keyhistory_oncard(selk);
1767+
offcard = piv_token_keyhistory_offcard(selk);
1768+
url = piv_token_offcard_url(selk);
1769+
1770+
if (oncard > 0)
1771+
--oncard;
1772+
1773+
err = piv_write_keyhistory(selk, oncard, offcard, url);
1774+
1775+
if (err) {
1776+
warnfx(err, "failed to update key "
1777+
"history object with new cert, trying to "
1778+
"continue anyway...");
1779+
err = ERRF_OK;
1780+
}
1781+
}
1782+
1783+
piv_txn_end(selk);
1784+
1785+
if (err) {
1786+
err = errf("write_cert", err, "failed to delete cert");
1787+
return (err);
1788+
}
1789+
1790+
return (ERRF_OK);
1791+
}
1792+
17371793
static errf_t *
17381794
cmd_write_cert(uint slotid)
17391795
{
@@ -2784,6 +2840,7 @@ usage(void)
27842840
" and replaces the cert in the given slot\n"
27852841
" req-cert <slot> Generates an X.509 CSR for the key in\n"
27862842
" the given slot (for user auth)\n"
2843+
" delete-cert <slot> Clears the certificate from the given slot\n"
27872844
" change-pin Changes the PIV PIN\n"
27882845
" change-puk Changes the PIV PUK\n"
27892846
" reset-pin Resets the PIN using the PUK\n"
@@ -3430,6 +3487,28 @@ main(int argc, char *argv[])
34303487
override = piv_force_slot(selk, slotid, overalg);
34313488
err = cmd_write_cert(slotid);
34323489

3490+
} else if (strcmp(op, "delete-cert") == 0) {
3491+
enum piv_slotid slotid;
3492+
3493+
if (optind >= argc) {
3494+
warnx("not enough arguments for %s (slot required)",
3495+
op);
3496+
usage();
3497+
}
3498+
err = piv_slotid_from_string(argv[optind++], &slotid);
3499+
if (err != ERRF_OK)
3500+
errfx(EXIT_BAD_ARGS, err, "failed to parse slot id");
3501+
3502+
if (optind < argc) {
3503+
warnx("too many arguments for %s", op);
3504+
usage();
3505+
}
3506+
3507+
check_select_key();
3508+
if (hasover)
3509+
override = piv_force_slot(selk, slotid, overalg);
3510+
err = cmd_delete_cert(slotid);
3511+
34333512
} else if (strcmp(op, "req-cert") == 0) {
34343513
enum piv_slotid slotid;
34353514

0 commit comments

Comments
 (0)