Skip to content

Commit

Permalink
pkey: change PKey::{RSA,DSA,DH}#params to use nil for missing parameters
Browse files Browse the repository at this point in the history
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"=>#<OpenSSL::BN 286934673421[...snip]>,
	#  "e"=>#<OpenSSL::BN 65537>,
	#  "d"=>#<OpenSSL::BN 0>,
	#  "p"=>#<OpenSSL::BN 0>,
	#  "q"=>#<OpenSSL::BN 0>,
	#  "dmp1"=>#<OpenSSL::BN 0>,
	#  "dmq1"=>#<OpenSSL::BN 0>,
	#  "iqmp"=>#<OpenSSL::BN 0>}

Let's use nil instead, which is more appropriate for indicating a
missing value.
  • Loading branch information
rhenium committed Jul 3, 2024
1 parent 60a32e4 commit 1f5bdaf
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 7 deletions.
6 changes: 3 additions & 3 deletions lib/openssl/pkey.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand Down
4 changes: 2 additions & 2 deletions test/openssl/test_pkey_dh.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down
2 changes: 1 addition & 1 deletion test/openssl/test_pkey_dsa.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion test/openssl/test_pkey_rsa.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 1f5bdaf

Please sign in to comment.