Skip to content

Commit

Permalink
pkey: implement PKey::{RSA,DSA,DH}#params in Ruby
Browse files Browse the repository at this point in the history
Move the definitions to lib/openssl/pkey.rb.

They don't have to be implemented in the native extension and can be
simplified by using existing methods.
  • Loading branch information
rhenium committed Jun 14, 2024
1 parent bc61a93 commit 60a32e4
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 100 deletions.
31 changes: 0 additions & 31 deletions ext/openssl/ossl_pkey_dh.c
Original file line number Diff line number Diff line change
Expand Up @@ -284,35 +284,6 @@ ossl_dh_to_der(VALUE self)
return str;
}

/*
* call-seq:
* dh.params -> hash
*
* Stores all parameters of key to the hash
* INSECURE: PRIVATE INFORMATIONS CAN LEAK OUT!!!
* Don't use :-)) (I's up to you)
*/
static VALUE
ossl_dh_get_params(VALUE self)
{
OSSL_3_const DH *dh;
VALUE hash;
const BIGNUM *p, *q, *g, *pub_key, *priv_key;

GetDH(self, dh);
DH_get0_pqg(dh, &p, &q, &g);
DH_get0_key(dh, &pub_key, &priv_key);

hash = rb_hash_new();
rb_hash_aset(hash, rb_str_new2("p"), ossl_bn_new(p));
rb_hash_aset(hash, rb_str_new2("q"), ossl_bn_new(q));
rb_hash_aset(hash, rb_str_new2("g"), ossl_bn_new(g));
rb_hash_aset(hash, rb_str_new2("pub_key"), ossl_bn_new(pub_key));
rb_hash_aset(hash, rb_str_new2("priv_key"), ossl_bn_new(priv_key));

return hash;
}

/*
* call-seq:
* dh.params_ok? -> true | false
Expand Down Expand Up @@ -443,8 +414,6 @@ Init_ossl_dh(void)
DEF_OSSL_PKEY_BN(cDH, dh, priv_key);
rb_define_method(cDH, "set_pqg", ossl_dh_set_pqg, 3);
rb_define_method(cDH, "set_key", ossl_dh_set_key, 2);

rb_define_method(cDH, "params", ossl_dh_get_params, 0);
}

#else /* defined NO_DH */
Expand Down
31 changes: 0 additions & 31 deletions ext/openssl/ossl_pkey_dsa.c
Original file line number Diff line number Diff line change
Expand Up @@ -303,35 +303,6 @@ ossl_dsa_to_der(VALUE self)
}


/*
* call-seq:
* dsa.params -> hash
*
* Stores all parameters of key to the hash
* INSECURE: PRIVATE INFORMATIONS CAN LEAK OUT!!!
* Don't use :-)) (I's up to you)
*/
static VALUE
ossl_dsa_get_params(VALUE self)
{
OSSL_3_const DSA *dsa;
VALUE hash;
const BIGNUM *p, *q, *g, *pub_key, *priv_key;

GetDSA(self, dsa);
DSA_get0_pqg(dsa, &p, &q, &g);
DSA_get0_key(dsa, &pub_key, &priv_key);

hash = rb_hash_new();
rb_hash_aset(hash, rb_str_new2("p"), ossl_bn_new(p));
rb_hash_aset(hash, rb_str_new2("q"), ossl_bn_new(q));
rb_hash_aset(hash, rb_str_new2("g"), ossl_bn_new(g));
rb_hash_aset(hash, rb_str_new2("pub_key"), ossl_bn_new(pub_key));
rb_hash_aset(hash, rb_str_new2("priv_key"), ossl_bn_new(priv_key));

return hash;
}

/*
* Document-method: OpenSSL::PKey::DSA#set_pqg
* call-seq:
Expand Down Expand Up @@ -396,8 +367,6 @@ Init_ossl_dsa(void)
DEF_OSSL_PKEY_BN(cDSA, dsa, priv_key);
rb_define_method(cDSA, "set_pqg", ossl_dsa_set_pqg, 3);
rb_define_method(cDSA, "set_key", ossl_dsa_set_key, 2);

rb_define_method(cDSA, "params", ossl_dsa_get_params, 0);
}

#else /* defined NO_DSA */
Expand Down
38 changes: 0 additions & 38 deletions ext/openssl/ossl_pkey_rsa.c
Original file line number Diff line number Diff line change
Expand Up @@ -494,42 +494,6 @@ ossl_rsa_verify_pss(int argc, VALUE *argv, VALUE self)
ossl_raise(eRSAError, NULL);
}

/*
* call-seq:
* rsa.params => hash
*
* THIS METHOD IS INSECURE, PRIVATE INFORMATION CAN LEAK OUT!!!
*
* Stores all parameters of key to the hash. The hash has keys 'n', 'e', 'd',
* 'p', 'q', 'dmp1', 'dmq1', 'iqmp'.
*
* Don't use :-)) (It's up to you)
*/
static VALUE
ossl_rsa_get_params(VALUE self)
{
OSSL_3_const RSA *rsa;
VALUE hash;
const BIGNUM *n, *e, *d, *p, *q, *dmp1, *dmq1, *iqmp;

GetRSA(self, rsa);
RSA_get0_key(rsa, &n, &e, &d);
RSA_get0_factors(rsa, &p, &q);
RSA_get0_crt_params(rsa, &dmp1, &dmq1, &iqmp);

hash = rb_hash_new();
rb_hash_aset(hash, rb_str_new2("n"), ossl_bn_new(n));
rb_hash_aset(hash, rb_str_new2("e"), ossl_bn_new(e));
rb_hash_aset(hash, rb_str_new2("d"), ossl_bn_new(d));
rb_hash_aset(hash, rb_str_new2("p"), ossl_bn_new(p));
rb_hash_aset(hash, rb_str_new2("q"), ossl_bn_new(q));
rb_hash_aset(hash, rb_str_new2("dmp1"), ossl_bn_new(dmp1));
rb_hash_aset(hash, rb_str_new2("dmq1"), ossl_bn_new(dmq1));
rb_hash_aset(hash, rb_str_new2("iqmp"), ossl_bn_new(iqmp));

return hash;
}

/*
* Document-method: OpenSSL::PKey::RSA#set_key
* call-seq:
Expand Down Expand Up @@ -617,8 +581,6 @@ Init_ossl_rsa(void)
rb_define_method(cRSA, "set_factors", ossl_rsa_set_factors, 2);
rb_define_method(cRSA, "set_crt_params", ossl_rsa_set_crt_params, 3);

rb_define_method(cRSA, "params", ossl_rsa_get_params, 0);

/*
* TODO: Test it
rb_define_method(cRSA, "blinding_on!", ossl_rsa_blinding_on, 0);
Expand Down
36 changes: 36 additions & 0 deletions lib/openssl/pkey.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,18 @@ def public_key
DH.new(to_der)
end

# :call-seq:
# dh.params -> hash
#
# Stores all parameters of key to a Hash.
#
# 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]
}.to_h
end

# :call-seq:
# dh.compute_key(pub_bn) -> string
#
Expand Down Expand Up @@ -154,6 +166,18 @@ def public_key
OpenSSL::PKey.read(public_to_der)
end

# :call-seq:
# dsa.params -> hash
#
# Stores all parameters of key to a Hash.
#
# 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]
}.to_h
end

class << self
# :call-seq:
# DSA.generate(size) -> dsa
Expand Down Expand Up @@ -328,6 +352,18 @@ def public_key
OpenSSL::PKey.read(public_to_der)
end

# :call-seq:
# rsa.params -> hash
#
# Stores all parameters of key to a Hash.
#
# 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]
}.to_h
end

class << self
# :call-seq:
# RSA.generate(size, exponent = 65537) -> RSA
Expand Down

0 comments on commit 60a32e4

Please sign in to comment.