Skip to content

Commit 714ba16

Browse files
committed
pkey/ec: refactor EC#dsa_{sign,verify}_asn1 with PKey#{sign,verify}_raw
With the newly added OpenSSL::PKey::PKey#{sign,verify}_raw, OpenSSL::PKey::EC's low level signing operation methods can be implemented in Ruby. The definitions are now in lib/openssl/pkey.rb.
1 parent 71c8040 commit 714ba16

File tree

2 files changed

+22
-55
lines changed

2 files changed

+22
-55
lines changed

ext/openssl/ossl_pkey_ec.c

-55
Original file line numberDiff line numberDiff line change
@@ -471,57 +471,6 @@ static VALUE ossl_ec_key_check_key(VALUE self)
471471
return Qtrue;
472472
}
473473

474-
/*
475-
* call-seq:
476-
* key.dsa_sign_asn1(data) => String
477-
*
478-
* See the OpenSSL documentation for ECDSA_sign()
479-
*/
480-
static VALUE ossl_ec_key_dsa_sign_asn1(VALUE self, VALUE data)
481-
{
482-
EC_KEY *ec;
483-
unsigned int buf_len;
484-
VALUE str;
485-
486-
GetEC(self, ec);
487-
StringValue(data);
488-
489-
if (EC_KEY_get0_private_key(ec) == NULL)
490-
ossl_raise(eECError, "Private EC key needed!");
491-
492-
str = rb_str_new(0, ECDSA_size(ec));
493-
if (ECDSA_sign(0, (unsigned char *) RSTRING_PTR(data), RSTRING_LENINT(data), (unsigned char *) RSTRING_PTR(str), &buf_len, ec) != 1)
494-
ossl_raise(eECError, "ECDSA_sign");
495-
rb_str_set_len(str, buf_len);
496-
497-
return str;
498-
}
499-
500-
/*
501-
* call-seq:
502-
* key.dsa_verify_asn1(data, sig) => true or false
503-
*
504-
* See the OpenSSL documentation for ECDSA_verify()
505-
*/
506-
static VALUE ossl_ec_key_dsa_verify_asn1(VALUE self, VALUE data, VALUE sig)
507-
{
508-
EC_KEY *ec;
509-
510-
GetEC(self, ec);
511-
StringValue(data);
512-
StringValue(sig);
513-
514-
switch (ECDSA_verify(0, (unsigned char *) RSTRING_PTR(data), RSTRING_LENINT(data), (unsigned char *) RSTRING_PTR(sig), (int)RSTRING_LEN(sig), ec)) {
515-
case 1: return Qtrue;
516-
case 0: return Qfalse;
517-
default: break;
518-
}
519-
520-
ossl_raise(eECError, "ECDSA_verify");
521-
522-
UNREACHABLE;
523-
}
524-
525474
/*
526475
* OpenSSL::PKey::EC::Group
527476
*/
@@ -1583,10 +1532,6 @@ void Init_ossl_ec(void)
15831532
rb_define_alias(cEC, "generate_key", "generate_key!");
15841533
rb_define_method(cEC, "check_key", ossl_ec_key_check_key, 0);
15851534

1586-
rb_define_method(cEC, "dsa_sign_asn1", ossl_ec_key_dsa_sign_asn1, 1);
1587-
rb_define_method(cEC, "dsa_verify_asn1", ossl_ec_key_dsa_verify_asn1, 2);
1588-
/* do_sign/do_verify */
1589-
15901535
rb_define_method(cEC, "export", ossl_ec_key_export, -1);
15911536
rb_define_alias(cEC, "to_pem", "export");
15921537
rb_define_method(cEC, "to_der", ossl_ec_key_to_der, 0);

lib/openssl/pkey.rb

+22
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,28 @@ def new(*args, &blk) # :nodoc:
164164
class EC
165165
include OpenSSL::Marshal
166166

167+
# :call-seq:
168+
# key.dsa_sign_asn1(data) -> String
169+
#
170+
# <b>Deprecated in version 3.0</b>.
171+
# Consider using PKey::PKey#sign_raw and PKey::PKey#verify_raw instead.
172+
def dsa_sign_asn1(data)
173+
sign_raw(nil, data)
174+
rescue OpenSSL::PKey::PKeyError
175+
raise OpenSSL::PKey::ECError, $!.message
176+
end
177+
178+
# :call-seq:
179+
# key.dsa_verify_asn1(data, sig) -> true | false
180+
#
181+
# <b>Deprecated in version 3.0</b>.
182+
# Consider using PKey::PKey#sign_raw and PKey::PKey#verify_raw instead.
183+
def dsa_verify_asn1(data, sig)
184+
verify_raw(nil, sig, data)
185+
rescue OpenSSL::PKey::PKeyError
186+
raise OpenSSL::PKey::ECError, $!.message
187+
end
188+
167189
# :call-seq:
168190
# ec.dh_compute_key(pubkey) -> string
169191
#

0 commit comments

Comments
 (0)