diff options
author | daurnimator <quae@daurnimator.com> | 2015-12-07 00:06:02 +1100 |
---|---|---|
committer | daurnimator <quae@daurnimator.com> | 2015-12-07 00:38:16 +1100 |
commit | bda7ea7cdc0a5a76a5ed2ed5979d101930918696 (patch) | |
tree | a7cacd71834d3413c77197fef90a467b351132f6 | |
parent | 60fc10973eb348cb3d99d27f083437ddeab03f14 (diff) | |
download | luaossl-bda7ea7cdc0a5a76a5ed2ed5979d101930918696.tar.gz luaossl-bda7ea7cdc0a5a76a5ed2ed5979d101930918696.tar.bz2 luaossl-bda7ea7cdc0a5a76a5ed2ed5979d101930918696.zip |
Add 'pkey:getParameters()' function that returns private key parameters as a table of bignums
-rw-r--r-- | src/openssl.c | 99 |
1 files changed, 99 insertions, 0 deletions
diff --git a/src/openssl.c b/src/openssl.c index 9dbcda7..e05fcc4 100644 --- a/src/openssl.c +++ b/src/openssl.c | |||
@@ -2458,6 +2458,104 @@ static int pk_toPEM(lua_State *L) { | |||
2458 | } /* pk_toPEM() */ | 2458 | } /* pk_toPEM() */ |
2459 | 2459 | ||
2460 | 2460 | ||
2461 | static int pk_getParameters(lua_State *L) { | ||
2462 | EVP_PKEY *key = checksimple(L, 1, PKEY_CLASS); | ||
2463 | void *tmp; | ||
2464 | const EC_GROUP *group; | ||
2465 | const EC_POINT *public_key; | ||
2466 | |||
2467 | if (!(tmp = EVP_PKEY_get0(key))) | ||
2468 | return auxL_error(L, auxL_EOPENSSL, "pkey:getParameters"); | ||
2469 | |||
2470 | lua_newtable(L); | ||
2471 | |||
2472 | switch (EVP_PKEY_base_id(key)) { | ||
2473 | case EVP_PKEY_RSA: | ||
2474 | /* RSA public modulus n */ | ||
2475 | if (!BN_copy(bn_push(L), ((RSA*)tmp)->n)) | ||
2476 | return auxL_error(L, auxL_EOPENSSL, "pkey:getParameters"); | ||
2477 | lua_setfield(L, -2, "n"); | ||
2478 | |||
2479 | /* RSA public exponent e */ | ||
2480 | if (!BN_copy(bn_push(L), ((RSA*)tmp)->e)) | ||
2481 | return auxL_error(L, auxL_EOPENSSL, "pkey:getParameters"); | ||
2482 | lua_setfield(L, -2, "e"); | ||
2483 | |||
2484 | /* RSA secret exponent d */ | ||
2485 | if (!BN_copy(bn_push(L), ((RSA*)tmp)->d)) | ||
2486 | return auxL_error(L, auxL_EOPENSSL, "pkey:getParameters"); | ||
2487 | lua_setfield(L, -2, "d"); | ||
2488 | |||
2489 | /* RSA secret prime p */ | ||
2490 | if (!BN_copy(bn_push(L), ((RSA*)tmp)->p)) | ||
2491 | return auxL_error(L, auxL_EOPENSSL, "pkey:getParameters"); | ||
2492 | lua_setfield(L, -2, "p"); | ||
2493 | |||
2494 | /* RSA secret prime q with p < q */ | ||
2495 | if (!BN_copy(bn_push(L), ((RSA*)tmp)->q)) | ||
2496 | return auxL_error(L, auxL_EOPENSSL, "pkey:getParameters"); | ||
2497 | lua_setfield(L, -2, "q"); | ||
2498 | |||
2499 | /* exponent1 */ | ||
2500 | if (!BN_copy(bn_push(L), ((RSA*)tmp)->dmp1)) | ||
2501 | return auxL_error(L, auxL_EOPENSSL, "pkey:getParameters"); | ||
2502 | lua_setfield(L, -2, "dmp1"); | ||
2503 | |||
2504 | /* exponent2 */ | ||
2505 | if (!BN_copy(bn_push(L), ((RSA*)tmp)->dmq1)) | ||
2506 | return auxL_error(L, auxL_EOPENSSL, "pkey:getParameters"); | ||
2507 | lua_setfield(L, -2, "dmq1"); | ||
2508 | |||
2509 | /* coefficient */ | ||
2510 | if (!BN_copy(bn_push(L), ((RSA*)tmp)->iqmp)) | ||
2511 | return auxL_error(L, auxL_EOPENSSL, "pkey:getParameters"); | ||
2512 | lua_setfield(L, -2, "iqmp"); | ||
2513 | |||
2514 | break; | ||
2515 | case EVP_PKEY_DH: | ||
2516 | /* prime */ | ||
2517 | if (!BN_copy(bn_push(L), ((DH*)tmp)->p)) | ||
2518 | return auxL_error(L, auxL_EOPENSSL, "pkey:getParameters"); | ||
2519 | lua_setfield(L, -2, "p"); | ||
2520 | |||
2521 | /* generator */ | ||
2522 | if (!BN_copy(bn_push(L), ((DH*)tmp)->g)) | ||
2523 | return auxL_error(L, auxL_EOPENSSL, "pkey:getParameters"); | ||
2524 | lua_setfield(L, -2, "g"); | ||
2525 | |||
2526 | /* pub_key */ | ||
2527 | if (!BN_copy(bn_push(L), ((DH*)tmp)->pub_key)) | ||
2528 | return auxL_error(L, auxL_EOPENSSL, "pkey:getParameters"); | ||
2529 | lua_setfield(L, -2, "pub_key"); | ||
2530 | |||
2531 | /* priv_key */ | ||
2532 | if (!BN_copy(bn_push(L), ((DH*)tmp)->priv_key)) | ||
2533 | return auxL_error(L, auxL_EOPENSSL, "pkey:getParameters"); | ||
2534 | lua_setfield(L, -2, "priv_key"); | ||
2535 | |||
2536 | break; | ||
2537 | case EVP_PKEY_EC: | ||
2538 | /* pub_key */ | ||
2539 | if (!(group = EC_KEY_get0_group(tmp)) || !(public_key = EC_KEY_get0_public_key(tmp))) | ||
2540 | return auxL_error(L, auxL_EOPENSSL, "pkey:getParameters"); | ||
2541 | if (!BN_copy(bn_push(L), EC_POINT_point2bn(group, public_key, EC_KEY_get_conv_form(tmp), NULL, getctx(L)))) | ||
2542 | return auxL_error(L, auxL_EOPENSSL, "pkey:getParameters"); | ||
2543 | lua_setfield(L, -2, "pub_key"); | ||
2544 | |||
2545 | /* priv_key */ | ||
2546 | if (!BN_copy(bn_push(L), EC_KEY_get0_private_key(tmp))) | ||
2547 | return auxL_error(L, auxL_EOPENSSL, "pkey:getParameters"); | ||
2548 | lua_setfield(L, -2, "priv_key"); | ||
2549 | |||
2550 | break; | ||
2551 | default: | ||
2552 | return luaL_error(L, "%d: unsupported EVP base type", EVP_PKEY_base_id(key)); | ||
2553 | } /* switch() */ | ||
2554 | |||
2555 | return 1; | ||
2556 | } | ||
2557 | |||
2558 | |||
2461 | static int pk__tostring(lua_State *L) { | 2559 | static int pk__tostring(lua_State *L) { |
2462 | EVP_PKEY *key = checksimple(L, 1, PKEY_CLASS); | 2560 | EVP_PKEY *key = checksimple(L, 1, PKEY_CLASS); |
2463 | int type = optencoding(L, 2, "pem", X509_PEM|X509_DER); | 2561 | int type = optencoding(L, 2, "pem", X509_PEM|X509_DER); |
@@ -2503,6 +2601,7 @@ static const luaL_Reg pk_methods[] = { | |||
2503 | { "sign", &pk_sign }, | 2601 | { "sign", &pk_sign }, |
2504 | { "verify", &pk_verify }, | 2602 | { "verify", &pk_verify }, |
2505 | { "toPEM", &pk_toPEM }, | 2603 | { "toPEM", &pk_toPEM }, |
2604 | { "getParameters", &pk_getParameters }, | ||
2506 | { NULL, NULL }, | 2605 | { NULL, NULL }, |
2507 | }; | 2606 | }; |
2508 | 2607 | ||