Skip to content

Commit

Permalink
Added EC public key sanity checks
Browse files Browse the repository at this point in the history
Require returned EC public keys to contain the params and the point.
  • Loading branch information
mtrojnar committed Apr 21, 2016
1 parent 874154b commit 80c5a94
Showing 1 changed file with 13 additions and 10 deletions.
23 changes: 13 additions & 10 deletions src/p11_ec.c
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,9 @@ static void free_ec_ex_index()

static EC_KEY *pkcs11_get_ec(PKCS11_KEY *key)
{
EC_KEY *ec;
size_t params_len = 0;
CK_BYTE *params;
size_t point_len = 0;
CK_BYTE *point;
EC_KEY *ec, *found_params = NULL, *found_point = NULL;
CK_BYTE *params, *point;
size_t params_len = 0, point_len = 0;
PKCS11_KEY *pubkey;

ec = EC_KEY_new();
Expand All @@ -108,7 +106,7 @@ static EC_KEY *pkcs11_get_ec(PKCS11_KEY *key)
const unsigned char *a = params;

/* Convert to OpenSSL parmas */
d2i_ECParameters(&ec, &a, (long)params_len);
found_params = d2i_ECParameters(&ec, &a, (long)params_len);
OPENSSL_free(params);
}

Expand All @@ -119,23 +117,28 @@ static EC_KEY *pkcs11_get_ec(PKCS11_KEY *key)
if (!key_getattr_alloc(pubkey, CKA_EC_POINT, &point, &point_len)) {
const unsigned char *a;
ASN1_OCTET_STRING *os;
EC_KEY *success = NULL;

/* PKCS#11-compliant modules should return ASN1_OCTET_STRING */
a = point;
os = d2i_ASN1_OCTET_STRING(NULL, &a, (long)point_len);
if (os) {
a = os->data;
success = o2i_ECPublicKey(&ec, &a, os->length);
found_point = o2i_ECPublicKey(&ec, &a, os->length);
ASN1_STRING_free(os);
}
if (success == NULL) { /* Workaround for broken PKCS#11 modules */
if (found_point == NULL) { /* Workaround for broken PKCS#11 modules */
a = point;
o2i_ECPublicKey(&ec, &a, point_len);
found_point = o2i_ECPublicKey(&ec, &a, point_len);
}
OPENSSL_free(point);
}

/* A public keys requires both the params and the point to be present */
if (!key->isPrivate && (found_params == NULL || found_point == NULL)) {
EC_KEY_free(ec);
return NULL;
}

return ec;
}

Expand Down

0 comments on commit 80c5a94

Please sign in to comment.