diff options
author | Simon Chopin <simon.chopin@canonical.com> | 2022-04-08 09:32:24 +0200 |
---|---|---|
committer | Florian Wininger <fw.centrale@gmail.com> | 2022-04-29 14:42:49 +0200 |
commit | 406063de2852cabe7d123c9dd72a72c4cfff8215 (patch) | |
tree | ee7c7daf619c60e8f453d822cc50d84a72ee6f70 | |
parent | e4ffdc07b1f0f01ebeab359c1001984912d87437 (diff) | |
download | net-ssh-406063de2852cabe7d123c9dd72a72c4cfff8215.tar.gz |
buffer: create RSA keys by loading PEM data directly
The OpenSSL 3.0 changes don't allow for us to modify the private key
details directly, and there are no dedicated constructors as of Ruby
3.0, so we need to actually create a PEM certificate in-memory and load
that instead.
Co-authored-by: Lucas Kanashiro <lucas.kanashiro@canonical.com>
-rw-r--r-- | lib/net/ssh/buffer.rb | 18 | ||||
-rw-r--r-- | test/test_buffer.rb | 16 | ||||
-rw-r--r-- | test/test_known_hosts.rb | 15 |
3 files changed, 25 insertions, 24 deletions
diff --git a/lib/net/ssh/buffer.rb b/lib/net/ssh/buffer.rb index 6ed5789..8a67d3e 100644 --- a/lib/net/ssh/buffer.rb +++ b/lib/net/ssh/buffer.rb @@ -315,15 +315,15 @@ module Net key.pub_key = read_bignum end when /^ssh-rsa$/ - key = OpenSSL::PKey::RSA.new - if key.respond_to?(:set_key) - e = read_bignum - n = read_bignum - key.set_key(n, e, nil) - else - key.e = read_bignum - key.n = read_bignum - end + e = read_bignum + n = read_bignum + + asn1 = OpenSSL::ASN1::Sequence([ + OpenSSL::ASN1::Integer(n), + OpenSSL::ASN1::Integer(e) + ]) + + key = OpenSSL::PKey::RSA.new(asn1.to_der) when /^ssh-ed25519$/ Net::SSH::Authentication::ED25519Loader.raiseUnlessLoaded("unsupported key type `#{type}'") key = Net::SSH::Authentication::ED25519::PubKey.read_keyblob(self) diff --git a/test/test_buffer.rb b/test/test_buffer.rb index c4fc9f2..b6968bb 100644 --- a/test/test_buffer.rb +++ b/test/test_buffer.rb @@ -337,13 +337,15 @@ class TestBuffer < NetSSHTest def test_write_rsa_key_should_write_argument_to_end_of_buffer buffer = new("start") - key = OpenSSL::PKey::RSA.new - if key.respond_to?(:set_key) - key.set_key(0x7766554433221100, 0xffeeddccbbaa9988, nil) - else - key.e = 0xffeeddccbbaa9988 - key.n = 0x7766554433221100 - end + n = 0x7766554433221100 + e = 0xffeeddccbbaa9988 + + asn1 = OpenSSL::ASN1::Sequence([ + OpenSSL::ASN1::Integer(n), + OpenSSL::ASN1::Integer(e) + ]) + + key = OpenSSL::PKey::RSA.new(asn1.to_der) buffer.write_key(key) assert_equal "start\0\0\0\7ssh-rsa\0\0\0\011\0\xff\xee\xdd\xcc\xbb\xaa\x99\x88\0\0\0\010\x77\x66\x55\x44\x33\x22\x11\x00", buffer.to_s diff --git a/test/test_known_hosts.rb b/test/test_known_hosts.rb index f960351..e47e75b 100644 --- a/test/test_known_hosts.rb +++ b/test/test_known_hosts.rb @@ -166,13 +166,12 @@ class TestKnownHosts < NetSSHTest end def rsa_key - key = OpenSSL::PKey::RSA.new - if key.respond_to?(:set_key) - key.set_key(0x7766554433221100, 0xffeeddccbbaa9988, nil) - else - key.e = 0xffeeddccbbaa9988 - key.n = 0x7766554433221100 - end - key + n = 0x7766554433221100 + e = 0xffeeddccbbaa9988 + asn1 = OpenSSL::ASN1::Sequence([ + OpenSSL::ASN1::Integer(n), + OpenSSL::ASN1::Integer(e) + ]) + OpenSSL::PKey::RSA.new(asn1.to_der) end end |