|
Lines 23-28
Link Here
|
| 23 |
#include <string.h> |
23 |
#include <string.h> |
| 24 |
#include <dlfcn.h> |
24 |
#include <dlfcn.h> |
| 25 |
|
25 |
|
|
|
26 |
#include <openssl/x509.h> |
| 27 |
|
| 26 |
#define CRYPTOKI_COMPAT |
28 |
#define CRYPTOKI_COMPAT |
| 27 |
#include "pkcs11.h" |
29 |
#include "pkcs11.h" |
| 28 |
|
30 |
|
|
Lines 373-404
pkcs11_open_session(struct pkcs11_provider *p, CK_ULONG slotidx, char *pin)
Link Here
|
| 373 |
* add 'wrapped' public keys to the 'keysp' array and increment nkeys. |
375 |
* add 'wrapped' public keys to the 'keysp' array and increment nkeys. |
| 374 |
* keysp points to an (possibly empty) array with *nkeys keys. |
376 |
* keysp points to an (possibly empty) array with *nkeys keys. |
| 375 |
*/ |
377 |
*/ |
|
|
378 |
static int pkcs11_fetch_keys_filter(struct pkcs11_provider *, CK_ULONG, |
| 379 |
CK_ATTRIBUTE [], CK_ATTRIBUTE [3], Key ***, int *) |
| 380 |
__attribute__((__bounded__(__minbytes__,4, 3 * sizeof(CK_ATTRIBUTE)))); |
| 381 |
|
| 376 |
static int |
382 |
static int |
| 377 |
pkcs11_fetch_keys(struct pkcs11_provider *p, CK_ULONG slotidx, Key ***keysp, |
383 |
pkcs11_fetch_keys(struct pkcs11_provider *p, CK_ULONG slotidx, |
| 378 |
int *nkeys) |
384 |
Key ***keysp, int *nkeys) |
| 379 |
{ |
385 |
{ |
| 380 |
Key *key; |
|
|
| 381 |
RSA *rsa; |
| 382 |
int i; |
| 383 |
CK_RV rv; |
| 384 |
CK_OBJECT_HANDLE obj; |
| 385 |
CK_ULONG nfound; |
| 386 |
CK_SESSION_HANDLE session; |
| 387 |
CK_FUNCTION_LIST *f; |
| 388 |
CK_OBJECT_CLASS pubkey_class = CKO_PUBLIC_KEY; |
386 |
CK_OBJECT_CLASS pubkey_class = CKO_PUBLIC_KEY; |
|
|
387 |
CK_OBJECT_CLASS cert_class = CKO_CERTIFICATE; |
| 389 |
CK_ATTRIBUTE pubkey_filter[] = { |
388 |
CK_ATTRIBUTE pubkey_filter[] = { |
| 390 |
{ CKA_CLASS, &pubkey_class, sizeof(pubkey_class) } |
389 |
{ CKA_CLASS, &pubkey_class, sizeof(pubkey_class) } |
| 391 |
}; |
390 |
}; |
| 392 |
CK_ATTRIBUTE attribs[] = { |
391 |
CK_ATTRIBUTE cert_filter[] = { |
|
|
392 |
{ CKA_CLASS, &cert_class, sizeof(cert_class) } |
| 393 |
}; |
| 394 |
CK_ATTRIBUTE pubkey_attribs[] = { |
| 393 |
{ CKA_ID, NULL, 0 }, |
395 |
{ CKA_ID, NULL, 0 }, |
| 394 |
{ CKA_MODULUS, NULL, 0 }, |
396 |
{ CKA_MODULUS, NULL, 0 }, |
| 395 |
{ CKA_PUBLIC_EXPONENT, NULL, 0 } |
397 |
{ CKA_PUBLIC_EXPONENT, NULL, 0 } |
| 396 |
}; |
398 |
}; |
|
|
399 |
CK_ATTRIBUTE cert_attribs[] = { |
| 400 |
{ CKA_ID, NULL, 0 }, |
| 401 |
{ CKA_SUBJECT, NULL, 0 }, |
| 402 |
{ CKA_VALUE, NULL, 0 } |
| 403 |
}; |
| 404 |
|
| 405 |
if (pkcs11_fetch_keys_filter(p, slotidx, pubkey_filter, pubkey_attribs, |
| 406 |
keysp, nkeys) < 0 || |
| 407 |
pkcs11_fetch_keys_filter(p, slotidx, cert_filter, cert_attribs, |
| 408 |
keysp, nkeys) < 0) |
| 409 |
return (-1); |
| 410 |
return (0); |
| 411 |
} |
| 412 |
|
| 413 |
static int |
| 414 |
pkcs11_fetch_keys_filter(struct pkcs11_provider *p, CK_ULONG slotidx, |
| 415 |
CK_ATTRIBUTE filter[], CK_ATTRIBUTE attribs[3], |
| 416 |
Key ***keysp, int *nkeys) |
| 417 |
{ |
| 418 |
Key *key; |
| 419 |
RSA *rsa; |
| 420 |
X509 *x509; |
| 421 |
EVP_PKEY *evp; |
| 422 |
int i; |
| 423 |
const u_char *cp; |
| 424 |
CK_RV rv; |
| 425 |
CK_OBJECT_HANDLE obj; |
| 426 |
CK_ULONG nfound; |
| 427 |
CK_SESSION_HANDLE session; |
| 428 |
CK_FUNCTION_LIST *f; |
| 397 |
|
429 |
|
| 398 |
f = p->function_list; |
430 |
f = p->function_list; |
| 399 |
session = p->slotinfo[slotidx].session; |
431 |
session = p->slotinfo[slotidx].session; |
| 400 |
/* setup a filter the looks for public keys */ |
432 |
/* setup a filter the looks for public keys */ |
| 401 |
if ((rv = f->C_FindObjectsInit(session, pubkey_filter, 1)) != CKR_OK) { |
433 |
if ((rv = f->C_FindObjectsInit(session, filter, 1)) != CKR_OK) { |
| 402 |
error("C_FindObjectsInit failed: %lu", rv); |
434 |
error("C_FindObjectsInit failed: %lu", rv); |
| 403 |
return (-1); |
435 |
return (-1); |
| 404 |
} |
436 |
} |
|
Lines 426-457
pkcs11_fetch_keys(struct pkcs11_provider *p, CK_ULONG slotidx, Key ***keysp,
Link Here
|
| 426 |
/* allocate buffers for attributes */ |
458 |
/* allocate buffers for attributes */ |
| 427 |
for (i = 0; i < 3; i++) |
459 |
for (i = 0; i < 3; i++) |
| 428 |
attribs[i].pValue = xmalloc(attribs[i].ulValueLen); |
460 |
attribs[i].pValue = xmalloc(attribs[i].ulValueLen); |
| 429 |
/* retrieve ID, modulus and public exponent of RSA key */ |
461 |
/* |
|
|
462 |
* retrieve ID, modulus and public exponent of RSA key, |
| 463 |
* or ID, subject and value for certificates. |
| 464 |
*/ |
| 465 |
rsa = NULL; |
| 430 |
if ((rv = f->C_GetAttributeValue(session, obj, attribs, 3)) |
466 |
if ((rv = f->C_GetAttributeValue(session, obj, attribs, 3)) |
| 431 |
!= CKR_OK) { |
467 |
!= CKR_OK) { |
| 432 |
error("C_GetAttributeValue failed: %lu", rv); |
468 |
error("C_GetAttributeValue failed: %lu", rv); |
| 433 |
} else if ((rsa = RSA_new()) == NULL) { |
469 |
} else if (attribs[1].type == CKA_MODULUS ) { |
| 434 |
error("RSA_new failed"); |
470 |
if ((rsa = RSA_new()) == NULL) { |
| 435 |
} else { |
471 |
error("RSA_new failed"); |
| 436 |
rsa->n = BN_bin2bn(attribs[1].pValue, |
|
|
| 437 |
attribs[1].ulValueLen, NULL); |
| 438 |
rsa->e = BN_bin2bn(attribs[2].pValue, |
| 439 |
attribs[2].ulValueLen, NULL); |
| 440 |
if (rsa->n && rsa->e && |
| 441 |
pkcs11_rsa_wrap(p, slotidx, &attribs[0], rsa) == 0) { |
| 442 |
key = key_new(KEY_UNSPEC); |
| 443 |
key->rsa = rsa; |
| 444 |
key->type = KEY_RSA; |
| 445 |
key->flags |= KEY_FLAG_EXT; |
| 446 |
/* expand key array and add key */ |
| 447 |
*keysp = xrealloc(*keysp, *nkeys + 1, |
| 448 |
sizeof(Key *)); |
| 449 |
(*keysp)[*nkeys] = key; |
| 450 |
*nkeys = *nkeys + 1; |
| 451 |
debug("have %d keys", *nkeys); |
| 452 |
} else { |
472 |
} else { |
| 453 |
RSA_free(rsa); |
473 |
rsa->n = BN_bin2bn(attribs[1].pValue, |
|
|
474 |
attribs[1].ulValueLen, NULL); |
| 475 |
rsa->e = BN_bin2bn(attribs[2].pValue, |
| 476 |
attribs[2].ulValueLen, NULL); |
| 477 |
} |
| 478 |
} else { |
| 479 |
cp = attribs[2].pValue; |
| 480 |
if ((x509 = X509_new()) == NULL) { |
| 481 |
error("X509_new failed"); |
| 482 |
} else if (d2i_X509(&x509, &cp, attribs[2].ulValueLen) |
| 483 |
== NULL) { |
| 484 |
error("d2i_X509 failed"); |
| 485 |
} else if ((evp = X509_get_pubkey(x509)) == NULL || |
| 486 |
evp->type != EVP_PKEY_RSA || |
| 487 |
evp->pkey.rsa == NULL) { |
| 488 |
debug("X509_get_pubkey failed or no rsa"); |
| 489 |
} else if ((rsa = RSAPublicKey_dup(evp->pkey.rsa)) |
| 490 |
== NULL) { |
| 491 |
error("RSAPublicKey_dup"); |
| 454 |
} |
492 |
} |
|
|
493 |
if (x509) |
| 494 |
X509_free(x509); |
| 495 |
} |
| 496 |
if (rsa && rsa->n && rsa->e && |
| 497 |
pkcs11_rsa_wrap(p, slotidx, &attribs[0], rsa) == 0) { |
| 498 |
key = key_new(KEY_UNSPEC); |
| 499 |
key->rsa = rsa; |
| 500 |
key->type = KEY_RSA; |
| 501 |
key->flags |= KEY_FLAG_EXT; |
| 502 |
/* expand key array and add key */ |
| 503 |
*keysp = xrealloc(*keysp, *nkeys + 1, |
| 504 |
sizeof(Key *)); |
| 505 |
(*keysp)[*nkeys] = key; |
| 506 |
*nkeys = *nkeys + 1; |
| 507 |
debug("have %d keys", *nkeys); |
| 508 |
} else if (rsa) { |
| 509 |
RSA_free(rsa); |
| 455 |
} |
510 |
} |
| 456 |
for (i = 0; i < 3; i++) |
511 |
for (i = 0; i < 3; i++) |
| 457 |
free(attribs[i].pValue); |
512 |
free(attribs[i].pValue); |