From 1f5bdaf5d08297a42fcdc51d293f80073e21a1ff Mon Sep 17 00:00:00 2001 From: Kazuki Yamaguchi Date: Fri, 14 Jun 2024 14:45:56 +0900 Subject: [PATCH] pkey: change PKey::{RSA,DSA,DH}#params to use nil for missing parameters The returned Hash from these methods contain 0 in place of a missing parameter in the key, for example: pkey = OpenSSL::PKey.read(OpenSSL::PKey::RSA.new(2048).public_to_pem) pp pkey.params #=> # {"n"=>#, # "e"=>#, # "d"=>#, # "p"=>#, # "q"=>#, # "dmp1"=>#, # "dmq1"=>#, # "iqmp"=>#} Let's use nil instead, which is more appropriate for indicating a missing value. --- lib/openssl/pkey.rb | 6 +++--- test/openssl/test_pkey_dh.rb | 4 ++-- test/openssl/test_pkey_dsa.rb | 2 +- test/openssl/test_pkey_rsa.rb | 2 +- 4 files changed, 7 insertions(+), 7 deletions(-) diff --git a/lib/openssl/pkey.rb b/lib/openssl/pkey.rb index 1aa22bfee..3d1e8885c 100644 --- a/lib/openssl/pkey.rb +++ b/lib/openssl/pkey.rb @@ -42,7 +42,7 @@ def public_key # The hash has keys 'p', 'q', 'g', 'pub_key', and 'priv_key'. def params %w{p q g pub_key priv_key}.map { |name| - [name, send(name) || 0.to_bn] + [name, send(name)] }.to_h end @@ -174,7 +174,7 @@ def public_key # The hash has keys 'p', 'q', 'g', 'pub_key', and 'priv_key'. def params %w{p q g pub_key priv_key}.map { |name| - [name, send(name) || 0.to_bn] + [name, send(name)] }.to_h end @@ -360,7 +360,7 @@ def public_key # The hash has keys 'n', 'e', 'd', 'p', 'q', 'dmp1', 'dmq1', and 'iqmp'. def params %w{n e d p q dmp1 dmq1 iqmp}.map { |name| - [name, send(name) || 0.to_bn] + [name, send(name)] }.to_h end diff --git a/test/openssl/test_pkey_dh.rb b/test/openssl/test_pkey_dh.rb index 88c3d674e..cec1e6301 100644 --- a/test/openssl/test_pkey_dh.rb +++ b/test/openssl/test_pkey_dh.rb @@ -138,9 +138,9 @@ def test_params assert_not_nil dh.g assert_equal dh.g, dh.params["g"] assert_nil dh.pub_key - assert_equal 0, dh.params["pub_key"] + assert_nil dh.params["pub_key"] assert_nil dh.priv_key - assert_equal 0, dh.params["priv_key"] + assert_nil dh.params["priv_key"] dhkey = OpenSSL::PKey.generate_key(dh) assert_equal dh.params["p"], dhkey.params["p"] diff --git a/test/openssl/test_pkey_dsa.rb b/test/openssl/test_pkey_dsa.rb index b47a20dfd..c120d74aa 100644 --- a/test/openssl/test_pkey_dsa.rb +++ b/test/openssl/test_pkey_dsa.rb @@ -251,7 +251,7 @@ def test_params assert_equal key.pub_key, pubkey.pub_key assert_equal key.pub_key, pubkey.params["pub_key"] assert_nil pubkey.priv_key - assert_equal 0, pubkey.params["priv_key"] + assert_nil pubkey.params["priv_key"] end def test_dup diff --git a/test/openssl/test_pkey_rsa.rb b/test/openssl/test_pkey_rsa.rb index 516d651ae..f47261ccd 100644 --- a/test/openssl/test_pkey_rsa.rb +++ b/test/openssl/test_pkey_rsa.rb @@ -535,7 +535,7 @@ def test_params pubkey = OpenSSL::PKey.read(key.public_to_der) [:d, :p, :q, :dmp1, :dmq1, :iqmp].each do |name| assert_nil pubkey.send(name) - assert_equal 0, pubkey.params[name.to_s] + assert_nil pubkey.params[name.to_s] end end