diff --git a/spec/rsa_spec.cr b/spec/rsa_spec.cr index 4d23adb..93188be 100644 --- a/spec/rsa_spec.cr +++ b/spec/rsa_spec.cr @@ -137,6 +137,22 @@ k0LaJjYM2ycehinmuLHgY3qdDJgtEbt4WG5XNQzhyfaN rsa.verify(new_digest, signature[0, 10], data).should be_false end end + + describe "can get parameters for serialization into other formats" do + it "can get p and q" do + rsa = OpenSSL::RSA.new(1024) + rsa.p + rsa.q + end + + it "can get n, e and d" do + rsa = OpenSSL::RSA.new(1024) + rsa.n + rsa.e + rsa.d + end + end + describe "can set parameters for more efficient decryption" do it "can set dmp1, dmq1, iqmp" do end diff --git a/src/openssl_ext/lib_crypto.cr b/src/openssl_ext/lib_crypto.cr index b067e73..c0c6d31 100644 --- a/src/openssl_ext/lib_crypto.cr +++ b/src/openssl_ext/lib_crypto.cr @@ -196,6 +196,8 @@ lib LibCrypto fun bignum_new = BN_new : Bignum* fun set_bignum_from_decimal = BN_dec2bn(a : Bignum**, str : LibC::Char*) : LibC::Int + fun bignum_to_bytes = BN_bn2bin(a : Bignum*, str : LibC::Char*) : LibC::Int + fun bignum_bits = BN_num_bits(a : Bignum*) : LibC::Int fun evp_pkey_new = EVP_PKEY_new : EvpPKey* fun evp_pkey_free = EVP_PKEY_free(pkey : EvpPKey*) @@ -212,7 +214,9 @@ lib LibCrypto fun rsa_public_encrypt = RSA_public_encrypt(flen : LibC::Int, from : UInt8*, to : UInt8*, rsa : Rsa*, padding : LibC::Int) : LibC::Int fun rsa_private_decrypt = RSA_private_decrypt(flen : LibC::Int, from : UInt8*, to : UInt8*, rsa : Rsa*, padding : LibC::Int) : LibC::Int fun rsa_public_decrypt = RSA_public_decrypt(flen : LibC::Int, from : UInt8*, to : UInt8*, rsa : Rsa*, padding : LibC::Int) : LibC::Int - + {% for param in %i[n e d p q] %} + fun rsa_get_{{param.id}} = RSA_get0_{{param.id}}(rsa : Rsa*) : Bignum* + {% end %} fun pem_read_bio_private_key = PEM_read_bio_PrivateKey(bp : Bio*, x : EvpPKey**, cb : (LibC::Char*, LibC::Int, LibC::Int, Void* -> LibC::Int), u : Void*) : EvpPKey* fun pem_read_bio_public_key = PEM_read_bio_PUBKEY(bp : Bio*, x : EvpPKey**, cb : (LibC::Char*, LibC::Int, LibC::Int, Void* -> LibC::Int), u : Void*) : EvpPKey* fun pem_write_bio_rsa_private_key = PEM_write_bio_RSAPrivateKey(bp : Bio*, x : Rsa*, enc : EVP_MD*, kstr : UInt8*, klen : LibC::Int, cb : (LibC::Char*, LibC::Int, LibC::Int, Void* -> LibC::Int), u : Void*) : LibC::Int diff --git a/src/openssl_ext/rsa.cr b/src/openssl_ext/rsa.cr index 72c4076..bedd95e 100644 --- a/src/openssl_ext/rsa.cr +++ b/src/openssl_ext/rsa.cr @@ -94,5 +94,14 @@ module OpenSSL LibCrypto.rsa_blinding_off(rsa) @blinding_on = false end + + {% for param in %i[n e d p q] %} + def {{param.id}} + bignum = LibCrypto.rsa_get_{{param.id}}(rsa) + Bytes.new((LibCrypto.bignum_bits(bignum) + 7) / 8).tap do |bytes| + LibCrypto.bignum_to_bytes(bignum, bytes) + end + end + {% end %} end end