|
15 | 15 | * License along with this library; if not, write to the Free Software |
16 | 16 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
17 | 17 | */ |
| 18 | +#include <openssl/objects.h> |
18 | 19 |
|
19 | 20 | #include "libp11-int.h" |
20 | 21 |
|
| 22 | +/* The maximum length of PIN */ |
| 23 | +#define MAX_PIN_LENGTH 256 |
| 24 | + |
21 | 25 | /* The following exported functions are *not* implemented here: |
22 | 26 | * PKCS11_get_rsa_method |
23 | 27 | * PKCS11_get_ecdsa_method |
@@ -401,14 +405,63 @@ int PKCS11_set_ui_method(PKCS11_CTX *pctx, UI_METHOD *ui_method, void *ui_user_d |
401 | 405 |
|
402 | 406 | /* External interface to the deprecated features */ |
403 | 407 |
|
404 | | -int PKCS11_generate_key(PKCS11_TOKEN *token, |
405 | | - int algorithm, unsigned int bits, |
406 | | - char *label, unsigned char *id, size_t id_len) |
| 408 | +int PKCS11_keygen(PKCS11_TOKEN *token, PKCS11_KGEN_ATTRS *kg) |
407 | 409 | { |
| 410 | + if (token == NULL || kg == NULL || kg->id_len > MAX_PIN_LENGTH) |
| 411 | + return -1; |
408 | 412 | PKCS11_SLOT_private *slot = PRIVSLOT(token->slot); |
409 | 413 | if (check_slot_fork(slot) < 0) |
410 | 414 | return -1; |
411 | | - return pkcs11_generate_key(slot, algorithm, bits, label, id, id_len); |
| 415 | + |
| 416 | + switch(kg->type) { |
| 417 | + case EVP_PKEY_RSA: |
| 418 | + return pkcs11_rsa_keygen(slot, kg->kgen.rsa->bits, |
| 419 | + kg->key_label, kg->key_id, kg->id_len, kg->key_params); |
| 420 | + case EVP_PKEY_EC: |
| 421 | + return pkcs11_ec_keygen(slot, kg->kgen.ec->curve, |
| 422 | + kg->key_label, kg->key_id, kg->id_len, kg->key_params); |
| 423 | + default: |
| 424 | + return -1; |
| 425 | + } |
| 426 | +} |
| 427 | + |
| 428 | +int PKCS11_generate_key(PKCS11_TOKEN *token, |
| 429 | + int algorithm, unsigned int bits_or_nid, |
| 430 | + char *label, unsigned char *id, size_t id_len) |
| 431 | +{ |
| 432 | + PKCS11_params key_params = { .extractable = 0, .sensitive = 1 }; |
| 433 | + PKCS11_EC_KGEN ec_kgen; |
| 434 | + PKCS11_RSA_KGEN rsa_kgen; |
| 435 | + PKCS11_KGEN_ATTRS kgen_attrs = { 0 }; |
| 436 | + |
| 437 | + switch (algorithm) { |
| 438 | + case EVP_PKEY_EC: |
| 439 | + ec_kgen.curve = OBJ_nid2sn(bits_or_nid); |
| 440 | + kgen_attrs = (PKCS11_KGEN_ATTRS){ |
| 441 | + .type = EVP_PKEY_EC, |
| 442 | + .kgen.ec = &ec_kgen, |
| 443 | + .token_label = (const char *)token->label, |
| 444 | + .key_label = label, |
| 445 | + .key_id = (const unsigned char *)id, |
| 446 | + .id_len = id_len, |
| 447 | + .key_params = &key_params |
| 448 | + }; |
| 449 | + break; |
| 450 | + |
| 451 | + default: |
| 452 | + rsa_kgen.bits = bits_or_nid; |
| 453 | + kgen_attrs = (PKCS11_KGEN_ATTRS){ |
| 454 | + .type = EVP_PKEY_RSA, |
| 455 | + .kgen.rsa = &rsa_kgen, |
| 456 | + .token_label = (const char *)token->label, |
| 457 | + .key_label = label, |
| 458 | + .key_id = (const unsigned char *)id, |
| 459 | + .id_len = id_len, |
| 460 | + .key_params = &key_params |
| 461 | + }; |
| 462 | + } |
| 463 | + |
| 464 | + return PKCS11_keygen(token, &kgen_attrs); |
412 | 465 | } |
413 | 466 |
|
414 | 467 | int PKCS11_get_key_size(PKCS11_KEY *pkey) |
|
0 commit comments