Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Returns keysize in bits #691

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
31 changes: 31 additions & 0 deletions ext/openssl/ossl_pkey.c
Original file line number Diff line number Diff line change
Expand Up @@ -737,6 +737,32 @@ ossl_pkey_inspect(VALUE self)
OBJ_nid2sn(nid));
}

/*
* call-seq:
* pkey.keysize_in_bits -> integer
*
* Returns an integer representing cryptographic length of the cryptosystem to which the key
* in _pkey_ belongs, in bits.
*
* See man page EVP_PKEY_get_bits(3)
chrisliaw marked this conversation as resolved.
Show resolved Hide resolved
*/
static VALUE
ossl_pkey_length_in_bits(VALUE self)
{
EVP_PKEY * pkey;
int bits;

GetPKey(self, pkey);

#if OSSL_OPENSSL_PREREQ(3, 0, 0)
bits = EVP_PKEY_get_bits(pkey);
#else
bits = EVP_PKEY_bits(pkey);
#endif
return INT2NUM(bits);

}
chrisliaw marked this conversation as resolved.
Show resolved Hide resolved

/*
* call-seq:
* pkey.to_text -> string
Expand Down Expand Up @@ -1666,6 +1692,8 @@ ossl_pkey_decrypt(int argc, VALUE *argv, VALUE self)
return str;
}



chrisliaw marked this conversation as resolved.
Show resolved Hide resolved
/*
* INIT
*/
Expand Down Expand Up @@ -1765,6 +1793,7 @@ Init_ossl_pkey(void)
#endif
rb_define_method(cPKey, "oid", ossl_pkey_oid, 0);
rb_define_method(cPKey, "inspect", ossl_pkey_inspect, 0);
rb_define_method(cPKey, "keysize_in_bits", ossl_pkey_length_in_bits, 0);
rb_define_method(cPKey, "to_text", ossl_pkey_to_text, 0);
rb_define_method(cPKey, "private_to_der", ossl_pkey_private_to_der, -1);
rb_define_method(cPKey, "private_to_pem", ossl_pkey_private_to_pem, -1);
Expand All @@ -1785,6 +1814,8 @@ Init_ossl_pkey(void)
rb_define_method(cPKey, "encrypt", ossl_pkey_encrypt, -1);
rb_define_method(cPKey, "decrypt", ossl_pkey_decrypt, -1);



id_private_q = rb_intern("private?");

/*
Expand Down
Loading
Loading