Skip to content

Commit d74bab4

Browse files
committed
Added a little documentation and finetuning for cases where parameters cannot be fetched from the underlying library
1 parent 8401aee commit d74bab4

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

ext/openssl/ossl_pkey.c

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -544,11 +544,18 @@ pkey_from_parameters(int argc, VALUE *argv, VALUE self)
544544

545545
from_params_args.param_bld = OSSL_PARAM_BLD_new();
546546

547-
if (from_params_args.param_bld == NULL)
547+
if (from_params_args.param_bld == NULL) {
548+
EVP_PKEY_CTX_free(ctx);
548549
ossl_raise(ePKeyError, "OSSL_PARAM_BLD_new");
550+
}
549551

550552
from_params_args.settable_params = EVP_PKEY_fromdata_settable(ctx, EVP_PKEY_KEYPAIR);
551553

554+
if (from_params_args.settable_params == NULL) {
555+
EVP_PKEY_CTX_free(ctx);
556+
ossl_raise(ePKeyError, "EVP_PKEY_fromdata_settable");
557+
}
558+
552559
if (strcmp("RSA", algorithm) == 0) {
553560
from_params_args.aliases = rsa_aliases;
554561
from_params_args.nAliases = sizeof(rsa_aliases)/sizeof((rsa_aliases)[0]);
@@ -638,6 +645,22 @@ ossl_pkey_s_generate_key(int argc, VALUE *argv, VALUE self)
638645
return pkey_generate(argc, argv, self, 0);
639646
}
640647

648+
/*
649+
* call-seq:
650+
* OpenSSL::PKey.from_parameters(algo_name, parameters) -> pkey
651+
*
652+
* Generates a new key based on given key parameters.
653+
*
654+
* The first parameter is the type of the key to create, given as a String, for example RSA, DSA, EC etc.
655+
* Second parameter is the parameters to be used for the key.
656+
*
657+
* For details algorithms and parameters see https://www.openssl.org/docs/man3.0/man3/EVP_PKEY_fromdata.html
658+
*
659+
* == Example
660+
* pkey = OpenSSL::PKey.from_parameters("RSA", n: 3161751493, e: 65537, d: 2064855961)
661+
* pkey.private? #=> true
662+
* pkey.public_key #=> #<OpenSSL::PKey::RSA...
663+
*/
641664
static VALUE
642665
ossl_pkey_s_from_parameters(int argc, VALUE *argv, VALUE self)
643666
{

test/openssl/test_pkey.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,11 @@ def test_s_from_parameters_ec_with_invalid_parameter
324324
assert_match(/Invalid parameter "invalid"/, e.message)
325325
end
326326

327+
def test_s_from_parameters_scrypt
328+
e = assert_raise(OpenSSL::PKey::PKeyError) { OpenSSL::PKey.from_parameters("SCRYPT", {}) }
329+
assert_match("EVP_PKEY_fromdata_settable", e.message)
330+
end
331+
327332
def test_s_from_parameters_dsa_with_all_supported_parameters
328333
source = OpenSSL::PKey::DSA.generate(2048)
329334

0 commit comments

Comments
 (0)