diff options
author | Kazuki Yamaguchi <k@rhe.jp> | 2017-03-20 23:18:26 +0900 |
---|---|---|
committer | Kazuki Yamaguchi <k@rhe.jp> | 2021-03-16 19:16:11 +0900 |
commit | 1e3590fe221c9631259f6876bfaa896398ac7cfa (patch) | |
tree | 7ca91d2c5fffaeb864df9fec668608536ccbc57d /test | |
parent | d60ab95398ecdb2ae9a78e12244f5019d335b356 (diff) | |
download | ruby-1e3590fe221c9631259f6876bfaa896398ac7cfa.tar.gz |
[ruby/openssl] pkey: add PKey.generate_parameters and .generate_key
Add two methods to create a PKey using the generic EVP interface. This
is useful for the PKey types we don't have a dedicated class.
https://github.com/ruby/openssl/commit/d8e8e57de9
Diffstat (limited to 'test')
-rw-r--r-- | test/openssl/test_pkey.rb | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/test/openssl/test_pkey.rb b/test/openssl/test_pkey.rb index 0bdc9795cc..a325a1ea2b 100644 --- a/test/openssl/test_pkey.rb +++ b/test/openssl/test_pkey.rb @@ -25,4 +25,47 @@ class OpenSSL::TestPKey < OpenSSL::PKeyTestCase assert_equal "X25519", x25519.oid assert_match %r{oid=X25519}, x25519.inspect end + + def test_s_generate_parameters + # 512 is non-default; 1024 is used if 'dsa_paramgen_bits' is not specified + # with OpenSSL 1.1.0. + pkey = OpenSSL::PKey.generate_parameters("DSA", { + "dsa_paramgen_bits" => 512, + "dsa_paramgen_q_bits" => 256, + }) + assert_instance_of OpenSSL::PKey::DSA, pkey + assert_equal 512, pkey.p.num_bits + assert_equal 256, pkey.q.num_bits + assert_equal nil, pkey.priv_key + + # Invalid options are checked + assert_raise(OpenSSL::PKey::PKeyError) { + OpenSSL::PKey.generate_parameters("DSA", "invalid" => "option") + } + + # Parameter generation callback is called + cb_called = [] + assert_raise(RuntimeError) { + OpenSSL::PKey.generate_parameters("DSA") { |*args| + cb_called << args + raise "exit!" if cb_called.size == 3 + } + } + assert_not_empty cb_called + end + + def test_s_generate_key + assert_raise(OpenSSL::PKey::PKeyError) { + # DSA key pair cannot be generated without parameters + OpenSSL::PKey.generate_key("DSA") + } + pkey_params = OpenSSL::PKey.generate_parameters("DSA", { + "dsa_paramgen_bits" => 512, + "dsa_paramgen_q_bits" => 256, + }) + pkey = OpenSSL::PKey.generate_key(pkey_params) + assert_instance_of OpenSSL::PKey::DSA, pkey + assert_equal 512, pkey.p.num_bits + assert_not_equal nil, pkey.priv_key + end end |