Skip to content

Commit afffbc0

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 f653d59 commit afffbc0

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
@@ -482,57 +482,6 @@ static VALUE ossl_ec_key_check_key(VALUE self)
482482
return Qtrue;
483483
}
484484

485-
/*
486-
* call-seq:
487-
* key.dsa_sign_asn1(data) => String
488-
*
489-
* See the OpenSSL documentation for ECDSA_sign()
490-
*/
491-
static VALUE ossl_ec_key_dsa_sign_asn1(VALUE self, VALUE data)
492-
{
493-
EC_KEY *ec;
494-
unsigned int buf_len;
495-
VALUE str;
496-
497-
GetEC(self, ec);
498-
StringValue(data);
499-
500-
if (EC_KEY_get0_private_key(ec) == NULL)
501-
ossl_raise(eECError, "Private EC key needed!");
502-
503-
str = rb_str_new(0, ECDSA_size(ec));
504-
if (ECDSA_sign(0, (unsigned char *) RSTRING_PTR(data), RSTRING_LENINT(data), (unsigned char *) RSTRING_PTR(str), &buf_len, ec) != 1)
505-
ossl_raise(eECError, "ECDSA_sign");
506-
rb_str_set_len(str, buf_len);
507-
508-
return str;
509-
}
510-
511-
/*
512-
* call-seq:
513-
* key.dsa_verify_asn1(data, sig) => true or false
514-
*
515-
* See the OpenSSL documentation for ECDSA_verify()
516-
*/
517-
static VALUE ossl_ec_key_dsa_verify_asn1(VALUE self, VALUE data, VALUE sig)
518-
{
519-
EC_KEY *ec;
520-
521-
GetEC(self, ec);
522-
StringValue(data);
523-
StringValue(sig);
524-
525-
switch (ECDSA_verify(0, (unsigned char *) RSTRING_PTR(data), RSTRING_LENINT(data), (unsigned char *) RSTRING_PTR(sig), (int)RSTRING_LEN(sig), ec)) {
526-
case 1: return Qtrue;
527-
case 0: return Qfalse;
528-
default: break;
529-
}
530-
531-
ossl_raise(eECError, "ECDSA_verify");
532-
533-
UNREACHABLE;
534-
}
535-
536485
/*
537486
* OpenSSL::PKey::EC::Group
538487
*/
@@ -1594,10 +1543,6 @@ void Init_ossl_ec(void)
15941543
rb_define_alias(cEC, "generate_key", "generate_key!");
15951544
rb_define_method(cEC, "check_key", ossl_ec_key_check_key, 0);
15961545

1597-
rb_define_method(cEC, "dsa_sign_asn1", ossl_ec_key_dsa_sign_asn1, 1);
1598-
rb_define_method(cEC, "dsa_verify_asn1", ossl_ec_key_dsa_verify_asn1, 2);
1599-
/* do_sign/do_verify */
1600-
16011546
rb_define_method(cEC, "export", ossl_ec_key_export, -1);
16021547
rb_define_alias(cEC, "to_pem", "export");
16031548
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
@@ -37,6 +37,28 @@ class DSA
3737
class EC
3838
include OpenSSL::Marshal
3939

40+
# :call-seq:
41+
# key.dsa_sign_asn1(data) -> String
42+
#
43+
# <b>Deprecated in version 2.3</b>.
44+
# Consider using PKey::PKey#sign_raw and PKey::PKey#verify_raw instead.
45+
def dsa_sign_asn1(data)
46+
sign_raw(nil, data)
47+
rescue OpenSSL::PKey::PKeyError
48+
raise OpenSSL::PKey::ECError, $!.message
49+
end
50+
51+
# :call-seq:
52+
# key.dsa_verify_asn1(data, sig) -> true | false
53+
#
54+
# <b>Deprecated in version 2.3</b>.
55+
# Consider using PKey::PKey#sign_raw and PKey::PKey#verify_raw instead.
56+
def dsa_verify_asn1(data, sig)
57+
verify_raw(nil, sig, data)
58+
rescue OpenSSL::PKey::PKeyError
59+
raise OpenSSL::PKey::ECError, $!.message
60+
end
61+
4062
# :call-seq:
4163
# ec.dh_compute_key(pubkey) -> string
4264
#

0 commit comments

Comments
 (0)