Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sc-hsm-tool: Add options for public key authentication #1711

Closed
wants to merge 23 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
198 changes: 197 additions & 1 deletion src/libopensc/card-sc-hsm.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* Driver for the SmartCard-HSM, a light-weight hardware security module
*
* Copyright (C) 2012 Andreas Schwier, CardContact, Minden, Germany, and others
* Copyright (C) 2018-2019 GSMK - Gesellschaft für Sichere Mobile Kommunikation mbH
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
Expand Down Expand Up @@ -1235,7 +1236,7 @@ static int sc_hsm_initialize(sc_card_t *card, sc_cardctl_sc_hsm_init_param_t *pa
int r;
size_t tilen;
sc_apdu_t apdu;
u8 ibuff[64+0xFF], *p;
u8 ibuff[68+0xFF], *p;

LOG_FUNC_CALLED(card->ctx);

Expand Down Expand Up @@ -1268,6 +1269,13 @@ static int sc_hsm_initialize(sc_card_t *card, sc_cardctl_sc_hsm_init_param_t *pa
*p++ = (u8)params->dkek_shares;
}

if (params->num_of_pub_keys > 0) {
*p++ = 0x93; // Use public key authentication
*p++ = 0x02;
*p++ = params->num_of_pub_keys; // Total number of public keys used for public authentication
*p++ = params->required_pub_keys; // Number of public keys required for authentication
}

if (params->bio1.len) {
*p++ = 0x95;
*p++ = params->bio1.len;
Expand Down Expand Up @@ -1424,10 +1432,194 @@ static int sc_hsm_unwrap_key(sc_card_t *card, sc_cardctl_sc_hsm_wrapped_key_t *p
r = sc_transmit_apdu(card, &apdu);
LOG_TEST_RET(ctx, r, "APDU transmit failed");

r = sc_check_sw(card, apdu.sw1, apdu.sw2);
LOG_TEST_RET(ctx, r, "Check SW error");

LOG_FUNC_RETURN(card->ctx, SC_SUCCESS);
}



static int get_CAR(u8 *carstr, size_t carstr_len, sc_context_t *ctx, const u8 *buf, size_t buflen)
{
const u8 *cb = NULL, *car = NULL;
size_t taglen;

LOG_FUNC_CALLED(ctx);

/* find embedded Certificate Body (should be right at the start) */
if (!(cb = sc_asn1_find_tag(ctx, buf, buflen, 0x7F4E, &taglen))) {
LOG_FUNC_RETURN(ctx, SC_ERROR_UNKNOWN);
}
buf = cb;
buflen = taglen;

/* find embedded Certification Authority Reference (CAR) */
if (!(car = sc_asn1_find_tag(ctx, buf, buflen, 0x42, &taglen))) {
LOG_FUNC_RETURN(ctx, SC_ERROR_UNKNOWN);
}

/* return CAR */
if (taglen > carstr_len) {
fprintf(stderr, "Output buffer too small.\n");
LOG_FUNC_RETURN(ctx, SC_ERROR_BUFFER_TOO_SMALL);
}
memcpy(carstr, car, taglen);
frankbraun marked this conversation as resolved.
Show resolved Hide resolved
LOG_FUNC_RETURN(ctx, SC_SUCCESS);
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe you also want to look at OpenPACE for more extended parsing/verification of CVCs. If you want to keep this self sustained within OpenSC, you should use sc_pkcs15emu_sc_hsm_decode_cvc()

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried to parse this with sc_pkcs15emu_sc_hsm_decode_cvc(), but wasn't able to. I got the following error:

Required ASN.1 object not found

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The idea is not to parse ASN.1 by hand. Please check what object is missing and why.



static int verify_certificate(sc_card_t *card, const u8 *cert, size_t cert_len, const u8 *chr, size_t chr_len)
{
u8 tag = SC_ASN1_TAG_CONTEXT | SC_ASN1_TAG_BIT_STRING; /* 0x83 */
size_t pukref_len, car_len;
u8 car[BUFSIZ] = { 0 };
u8 pukref[BUFSIZ];
sc_apdu_t apdu;
u8 *ptr;
int r;

LOG_FUNC_CALLED(card->ctx);

/* check if public key is already known */
if ((r = sc_asn1_put_tag(tag, chr, chr_len, pukref, sizeof(pukref), &ptr)) < 0) {
fprintf(stderr, "Error formatting ASN.1 sequence: %s\n", sc_strerror(r));
LOG_FUNC_RETURN(card->ctx, SC_ERROR_UNKNOWN);
}
pukref_len = ptr - pukref;

sc_format_apdu_ex(&apdu, 0x00, 0x22, 0x81, 0xB6, pukref, pukref_len, NULL, 0);

r = sc_transmit_apdu(card, &apdu);
LOG_TEST_RET(card->ctx, r, "APDU transmit failed");

r = sc_check_sw(card, apdu.sw1, apdu.sw2);
if (!r) {
/* already known */
LOG_FUNC_RETURN(card->ctx, SC_SUCCESS);
}
if (apdu.sw1 != 0x6A && apdu.sw2 != 0x88) {
LOG_TEST_RET(card->ctx, SC_ERROR_UNKNOWN, "Check SW error");
}

/* select public key for verification */
r = get_CAR(car, sizeof car, card->ctx, cert, cert_len);
LOG_TEST_RET(card->ctx, r, "cannot parse CAR");
car_len = strnlen((const char*) car, sizeof car);

if ((r = sc_asn1_put_tag(tag, car, car_len, pukref, sizeof(pukref), &ptr)) < 0) {
fprintf(stderr, "Error formatting ASN.1 sequence: %s\n", sc_strerror(r));
LOG_FUNC_RETURN(card->ctx, SC_ERROR_UNKNOWN);
}
pukref_len = ptr - pukref;

sc_format_apdu_ex(&apdu, 0x00, 0x22, 0x81, 0xB6, pukref, pukref_len, NULL, 0);

r = sc_transmit_apdu(card, &apdu);
LOG_TEST_RET(card->ctx, r, "APDU transmit failed");
r = sc_check_sw(card, apdu.sw1, apdu.sw2);
LOG_TEST_RET(card->ctx, r, "Check SW error");

/* verify certificate */
sc_format_apdu_ex(&apdu, 0x00, 0x2A, 0x00, 0xBE, cert, cert_len, NULL, 0);

r = sc_transmit_apdu(card, &apdu);
LOG_TEST_RET(card->ctx, r, "APDU transmit failed");
r = sc_check_sw(card, apdu.sw1, apdu.sw2);
LOG_TEST_RET(card->ctx, r, "Check SW error");

LOG_FUNC_RETURN(card->ctx, SC_SUCCESS);
}



static void print_public_key_auth_status(u8 *recvbuf)
{
printf("Number of public keys: %d\n", recvbuf[0]);
printf("Missing public keys: %d\n", recvbuf[1]);
printf("Required pubkeys for auth: %d\n", recvbuf[2]);
printf("Authenticated public keys: %d\n", recvbuf[3]);
}



static int sc_hsm_register_public_key(sc_card_t *card, sc_cardctl_sc_hsm_public_key_t *params)
{
u8 tag = SC_ASN1_TAG_CONTEXT | SC_ASN1_TAG_BIT_STRING; /* 0x83 */
size_t outer_car_len, pukref_len;
u8 recvbuf[4];
sc_context_t *ctx = card->ctx;
const u8 *outer_car = NULL;
u8 pukref[BUFSIZ];
sc_apdu_t apdu;
u8 *ptr;
int r;

LOG_FUNC_CALLED(card->ctx);

/* verify dicacert */
r = verify_certificate(card, params->dicacert, params->dicacert_length, params->dicacert_chr, params->dicacert_chr_length);
LOG_TEST_RET(ctx, r, "device issuer certificate verification failed");

/* verify devcert */
r = verify_certificate(card, params->devcert, params->devcert_length, params->devcert_chr, params->devcert_chr_length);
LOG_TEST_RET(ctx, r, "device certificate verification failed");

/* manage SE */
if (!(outer_car = sc_asn1_find_tag(ctx, params->pk, params->pk_length, 0x42, &outer_car_len))) {
LOG_FUNC_RETURN(ctx, SC_ERROR_UNKNOWN);
}

if ((r = sc_asn1_put_tag(tag, outer_car, outer_car_len, pukref, sizeof(pukref), &ptr)) < 0) {
fprintf(stderr, "Error formatting ASN.1 sequence: %s\n", sc_strerror(r));
LOG_FUNC_RETURN(card->ctx, SC_ERROR_UNKNOWN);
}
pukref_len = ptr - pukref;

sc_format_apdu_ex(&apdu, 0x00, 0x22, 0x81, 0xB6, pukref, pukref_len, NULL, 0);

r = sc_transmit_apdu(card, &apdu);
LOG_TEST_RET(card->ctx, r, "APDU transmit failed");
r = sc_check_sw(card, apdu.sw1, apdu.sw2);
LOG_TEST_RET(card->ctx, r, "Check SW error");

/* manage public key authentication */
sc_format_apdu_ex(&apdu, 0x00, 0x54, 0x00, 0x00, params->pk, params->pk_length, recvbuf, sizeof recvbuf);
apdu.cla = 0x80;

r = sc_transmit_apdu(card, &apdu);
LOG_TEST_RET(ctx, r, "APDU transmit failed");
r = sc_check_sw(card, apdu.sw1, apdu.sw2);
LOG_TEST_RET(ctx, r, "Check SW error");

print_public_key_auth_status(recvbuf);

LOG_FUNC_RETURN(card->ctx, SC_SUCCESS);
}



static int sc_hsm_public_key_auth_status(sc_card_t *card)
{
u8 recvbuf[4];
sc_context_t *ctx = card->ctx;
sc_apdu_t apdu;
int r;

LOG_FUNC_CALLED(card->ctx);

/* get status */
sc_format_apdu_ex(&apdu, 0x00, 0x54, 0x00, 0x00, NULL, 0, recvbuf, sizeof recvbuf);
apdu.cla = 0x80;

r = sc_transmit_apdu(card, &apdu);
LOG_TEST_RET(ctx, r, "APDU transmit failed");

r = sc_check_sw(card, apdu.sw1, apdu.sw2);
LOG_TEST_RET(ctx, r, "Check SW error");

print_public_key_auth_status(recvbuf);

LOG_FUNC_RETURN(card->ctx, SC_SUCCESS);
}

Expand Down Expand Up @@ -1597,6 +1789,10 @@ static int sc_hsm_card_ctl(sc_card_t *card, unsigned long cmd, void *ptr)
return sc_hsm_wrap_key(card, (sc_cardctl_sc_hsm_wrapped_key_t *)ptr);
case SC_CARDCTL_SC_HSM_UNWRAP_KEY:
return sc_hsm_unwrap_key(card, (sc_cardctl_sc_hsm_wrapped_key_t *)ptr);
case SC_CARDCTL_SC_HSM_REGISTER_PUBLIC_KEY:
return sc_hsm_register_public_key(card, (sc_cardctl_sc_hsm_public_key_t *)ptr);
case SC_CARDCTL_SC_HSM_PUBLIC_KEY_AUTH_STATUS:
return sc_hsm_public_key_auth_status(card);
}
return SC_ERROR_NOT_SUPPORTED;
}
Expand Down
18 changes: 18 additions & 0 deletions src/libopensc/cardctl.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* cardctl.h: card_ctl command numbers
*
* Copyright (C) 2003 Olaf Kirch <[email protected]>
* Copyright (C) 2018-2019 GSMK - Gesellschaft für Sichere Mobile Kommunikation mbH
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
Expand Down Expand Up @@ -272,6 +273,8 @@ enum {
SC_CARDCTL_SC_HSM_IMPORT_DKEK_SHARE,
SC_CARDCTL_SC_HSM_WRAP_KEY,
SC_CARDCTL_SC_HSM_UNWRAP_KEY,
SC_CARDCTL_SC_HSM_REGISTER_PUBLIC_KEY,
SC_CARDCTL_SC_HSM_PUBLIC_KEY_AUTH_STATUS,

/*
* DNIe specific calls
Expand Down Expand Up @@ -1021,6 +1024,8 @@ typedef struct sc_cardctl_sc_hsm_init_param {
struct sc_aid bio2; /* AID of biometric server for template 2 */
u8 options[2]; /* Initialization options */
signed char dkek_shares; /* Number of DKEK shares, 0 for card generated, -1 for none */
signed char num_of_pub_keys; /* Total number of public keys used for public authentication (if > 0) */
u8 required_pub_keys; /* Number of public keys required for authentication (if public auth. is used) */
char *label; /* Token label to be set in EF.TokenInfo (2F03) */
} sc_cardctl_sc_hsm_init_param_t;

Expand All @@ -1038,6 +1043,19 @@ typedef struct sc_cardctl_sc_hsm_wrapped_key {
size_t wrapped_key_length; /* Length of key blob */
} sc_cardctl_sc_hsm_wrapped_key_t;

typedef struct sc_cardctl_sc_hsm_public_key {
const u8 *pk; /* Public key */
size_t pk_length; /* Length of key */
const u8 *devcert; /* Device certificate */
size_t devcert_length; /* Length of device certificate */
const u8 *dicacert; /* Device issuer certificate */
size_t dicacert_length; /* Length of device issuer certificate */
const u8 *devcert_chr; /* Device certificate CHR */
size_t devcert_chr_length; /* Length of device certificate CHR */
const u8 *dicacert_chr; /* Device issuer certificate CHR */
size_t dicacert_chr_length; /* Device issuer certificate CHR length */
} sc_cardctl_sc_hsm_public_key_t;

/*
* isoApplet
*/
Expand Down
2 changes: 2 additions & 0 deletions src/tools/pkcs11-tool.c
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ static struct ec_curve_info {
{"prime384v1", "1.3.132.0.34", "06052B81040022", 384},
{"ansiX9p384r1", "1.3.132.0.34", "06052B81040022", 384},

{"prime521v1", "1.3.132.0.35", "06052B81040023", 521},
{"secp521r1", "1.3.132.0.35", "06052B81040023", 521},
{"nistp521", "1.3.132.0.35", "06052B81040023", 521},

Expand All @@ -114,6 +115,7 @@ static struct ec_curve_info {

{"secp192k1", "1.3.132.0.31", "06052B8104001F", 192},
{"secp256k1", "1.3.132.0.10", "06052B8104000A", 256},
{"secp521k1", "1.3.132.0.35", "06052B81040023", 521},
{NULL, NULL, NULL, 0},
};

Expand Down
Loading