diff options
| -rw-r--r-- | src/openssl.c | 117 |
1 files changed, 117 insertions, 0 deletions
diff --git a/src/openssl.c b/src/openssl.c index 01bf2c8..c3f8bbb 100644 --- a/src/openssl.c +++ b/src/openssl.c | |||
| @@ -1558,6 +1558,16 @@ static BIGNUM *bn_push(lua_State *L) { | |||
| 1558 | } /* bn_push() */ | 1558 | } /* bn_push() */ |
| 1559 | 1559 | ||
| 1560 | 1560 | ||
| 1561 | static BIGNUM *bn_dup(lua_State *L, const BIGNUM *src) { | ||
| 1562 | BIGNUM **ud = prepsimple(L, BIGNUM_CLASS); | ||
| 1563 | |||
| 1564 | if (!(*ud = BN_dup(src))) | ||
| 1565 | auxL_error(L, auxL_EOPENSSL, "bignum.new"); | ||
| 1566 | |||
| 1567 | return *ud; | ||
| 1568 | } /* bn_dup() */ | ||
| 1569 | |||
| 1570 | |||
| 1561 | #define checkbig_(a, b, c, ...) checkbig((a), (b), (c)) | 1571 | #define checkbig_(a, b, c, ...) checkbig((a), (b), (c)) |
| 1562 | #define checkbig(...) checkbig_(__VA_ARGS__, &(_Bool){ 0 }, 0) | 1572 | #define checkbig(...) checkbig_(__VA_ARGS__, &(_Bool){ 0 }, 0) |
| 1563 | 1573 | ||
| @@ -2498,6 +2508,112 @@ static int pk_toPEM(lua_State *L) { | |||
| 2498 | } /* pk_toPEM() */ | 2508 | } /* pk_toPEM() */ |
| 2499 | 2509 | ||
| 2500 | 2510 | ||
| 2511 | static int pk_getParameters(lua_State *L) { | ||
| 2512 | EVP_PKEY *key = checksimple(L, 1, PKEY_CLASS); | ||
| 2513 | _Bool public_only = lua_toboolean(L, 2); | ||
| 2514 | |||
| 2515 | void *tmp; | ||
| 2516 | const EC_GROUP *group; | ||
| 2517 | const EC_POINT *public_key; | ||
| 2518 | |||
| 2519 | if (!(tmp = EVP_PKEY_get0(key))) | ||
| 2520 | return auxL_error(L, auxL_EOPENSSL, "pkey:getParameters"); | ||
| 2521 | |||
| 2522 | lua_newtable(L); | ||
| 2523 | |||
| 2524 | switch (EVP_PKEY_base_id(key)) { | ||
| 2525 | case EVP_PKEY_RSA: | ||
| 2526 | /* RSA public modulus n */ | ||
| 2527 | if (!bn_dup(L, ((RSA*)tmp)->n)) | ||
| 2528 | return auxL_error(L, auxL_EOPENSSL, "pkey:getParameters"); | ||
| 2529 | lua_setfield(L, -2, "n"); | ||
| 2530 | |||
| 2531 | /* RSA public exponent e */ | ||
| 2532 | if (!bn_dup(L, ((RSA*)tmp)->e)) | ||
| 2533 | return auxL_error(L, auxL_EOPENSSL, "pkey:getParameters"); | ||
| 2534 | lua_setfield(L, -2, "e"); | ||
| 2535 | |||
| 2536 | if (public_only) break; | ||
| 2537 | |||
| 2538 | /* RSA secret exponent d */ | ||
| 2539 | if (!bn_dup(L, ((RSA*)tmp)->d)) | ||
| 2540 | return auxL_error(L, auxL_EOPENSSL, "pkey:getParameters"); | ||
| 2541 | lua_setfield(L, -2, "d"); | ||
| 2542 | |||
| 2543 | /* RSA secret prime p */ | ||
| 2544 | if (!bn_dup(L, ((RSA*)tmp)->p)) | ||
| 2545 | return auxL_error(L, auxL_EOPENSSL, "pkey:getParameters"); | ||
| 2546 | lua_setfield(L, -2, "p"); | ||
| 2547 | |||
| 2548 | /* RSA secret prime q with p < q */ | ||
| 2549 | if (!bn_dup(L, ((RSA*)tmp)->q)) | ||
| 2550 | return auxL_error(L, auxL_EOPENSSL, "pkey:getParameters"); | ||
| 2551 | lua_setfield(L, -2, "q"); | ||
| 2552 | |||
| 2553 | /* exponent1 */ | ||
| 2554 | if (!bn_dup(L, ((RSA*)tmp)->dmp1)) | ||
| 2555 | return auxL_error(L, auxL_EOPENSSL, "pkey:getParameters"); | ||
| 2556 | lua_setfield(L, -2, "dmp1"); | ||
| 2557 | |||
| 2558 | /* exponent2 */ | ||
| 2559 | if (!bn_dup(L, ((RSA*)tmp)->dmq1)) | ||
| 2560 | return auxL_error(L, auxL_EOPENSSL, "pkey:getParameters"); | ||
| 2561 | lua_setfield(L, -2, "dmq1"); | ||
| 2562 | |||
| 2563 | /* coefficient */ | ||
| 2564 | if (!bn_dup(L, ((RSA*)tmp)->iqmp)) | ||
| 2565 | return auxL_error(L, auxL_EOPENSSL, "pkey:getParameters"); | ||
| 2566 | lua_setfield(L, -2, "iqmp"); | ||
| 2567 | |||
| 2568 | break; | ||
| 2569 | case EVP_PKEY_DH: | ||
| 2570 | /* prime */ | ||
| 2571 | if (!bn_dup(L, ((DH*)tmp)->p)) | ||
| 2572 | return auxL_error(L, auxL_EOPENSSL, "pkey:getParameters"); | ||
| 2573 | lua_setfield(L, -2, "p"); | ||
| 2574 | |||
| 2575 | /* generator */ | ||
| 2576 | if (!bn_dup(L, ((DH*)tmp)->g)) | ||
| 2577 | return auxL_error(L, auxL_EOPENSSL, "pkey:getParameters"); | ||
| 2578 | lua_setfield(L, -2, "g"); | ||
| 2579 | |||
| 2580 | /* pub_key */ | ||
| 2581 | if (!bn_dup(L, ((DH*)tmp)->pub_key)) | ||
| 2582 | return auxL_error(L, auxL_EOPENSSL, "pkey:getParameters"); | ||
| 2583 | lua_setfield(L, -2, "pub_key"); | ||
| 2584 | |||
| 2585 | if (public_only) break; | ||
| 2586 | |||
| 2587 | /* priv_key */ | ||
| 2588 | if (!bn_dup(L, ((DH*)tmp)->priv_key)) | ||
| 2589 | return auxL_error(L, auxL_EOPENSSL, "pkey:getParameters"); | ||
| 2590 | lua_setfield(L, -2, "priv_key"); | ||
| 2591 | |||
| 2592 | break; | ||
| 2593 | case EVP_PKEY_EC: | ||
| 2594 | /* pub_key */ | ||
| 2595 | if (!(group = EC_KEY_get0_group(tmp)) || !(public_key = EC_KEY_get0_public_key(tmp))) | ||
| 2596 | return auxL_error(L, auxL_EOPENSSL, "pkey:getParameters"); | ||
| 2597 | if (!bn_dup(L, EC_POINT_point2bn(group, public_key, EC_KEY_get_conv_form(tmp), NULL, getctx(L)))) | ||
| 2598 | return auxL_error(L, auxL_EOPENSSL, "pkey:getParameters"); | ||
| 2599 | lua_setfield(L, -2, "pub_key"); | ||
| 2600 | |||
| 2601 | if (public_only) break; | ||
| 2602 | |||
| 2603 | /* priv_key */ | ||
| 2604 | if (!bn_dup(L, EC_KEY_get0_private_key(tmp))) | ||
| 2605 | return auxL_error(L, auxL_EOPENSSL, "pkey:getParameters"); | ||
| 2606 | lua_setfield(L, -2, "priv_key"); | ||
| 2607 | |||
| 2608 | break; | ||
| 2609 | default: | ||
| 2610 | return luaL_error(L, "%d: unsupported EVP base type", EVP_PKEY_base_id(key)); | ||
| 2611 | } /* switch() */ | ||
| 2612 | |||
| 2613 | return 1; | ||
| 2614 | } | ||
| 2615 | |||
| 2616 | |||
| 2501 | static int pk__tostring(lua_State *L) { | 2617 | static int pk__tostring(lua_State *L) { |
| 2502 | EVP_PKEY *key = checksimple(L, 1, PKEY_CLASS); | 2618 | EVP_PKEY *key = checksimple(L, 1, PKEY_CLASS); |
| 2503 | int type = optencoding(L, 2, "pem", X509_PEM|X509_DER); | 2619 | int type = optencoding(L, 2, "pem", X509_PEM|X509_DER); |
| @@ -2543,6 +2659,7 @@ static const luaL_Reg pk_methods[] = { | |||
| 2543 | { "sign", &pk_sign }, | 2659 | { "sign", &pk_sign }, |
| 2544 | { "verify", &pk_verify }, | 2660 | { "verify", &pk_verify }, |
| 2545 | { "toPEM", &pk_toPEM }, | 2661 | { "toPEM", &pk_toPEM }, |
| 2662 | { "getParameters", &pk_getParameters }, | ||
| 2546 | { NULL, NULL }, | 2663 | { NULL, NULL }, |
| 2547 | }; | 2664 | }; |
| 2548 | 2665 | ||
