openssl
openssl copied to clipboard
Feature request: OpenSSL::PKey::RSA#size
It would be nice to be able to introspect on the sizes of RSA keys:
pry(main)> key.size
NoMethodError: undefined method `size' for #<OpenSSL::PKey::RSA:0x007fa4292cafe0>
Here is an ugly way to implement it in pure Ruby using the existing API:
Integer(key.to_text[/\((\d+) bit/, 1], 10)
Patches welcome :)
Awesome! We found a better implementation:
key.n.num_bits
Would you like to update the documentation?
I can send you a PR that adds the method in pure Ruby! :open_mouth:
@tarcieri Ahh! I misunderstood, i think for this key we would need to patch the capi.
btw what is n
?
n
is the RSA modulus, which should match the key length.
Also I know C, so I'd be happy to add this to the C API. But what about jruby-openssl
?
Let's decide on a Ruby api we like for it before we write the C, but for merging into the gem we will need to write the capi.
We can hopefully use a similar patch for jruby-openssl
I can give it a try on the C level. Iirc OpenSSL exposes something that would work for DSA and EC keys, too. Definitely a useful feature to have!
I ran into this, too and realized that OpenSSL::PKey::DSA and OpenSSL::PKey::EC need a different strategy each. Should we add these, too?
Yes please. And I'd love to know when this would be released.
Out of curiosity, is #n
— and its friends, #d
, #e
, #p
, and #q
— documented anywhere?
Any movement on this? I took a stab at writing a proxy object to provide this functionality. Would appreciate some feedback, especially wrt EC keys. https://github.com/trailofbits/ruby-sslyze/blob/d2cffe53764ad6e48bef65553730716336dd0a4a/lib/sslyze/x509/public_key.rb#L39-L49
@aryeh-looker feel free to make a PR to improve docuemntation
@postmodern do you want to take a stab at making a PR regarding this functionality?
Regarding EC keys, it is correct do the following: private_key.group.degree
which will return bit size of the curve that was used for private key generation