Skip to content

Commit f6bb911

Browse files
committed
pkey/dsa: refactor DSA#sys{sign,verify} with PKey#{sign,verify}_raw
With the newly added OpenSSL::PKey::PKey#{sign,verify}_raw, OpenSSL::PKey::DSA's low level signing operation methods can be implemented in Ruby. The definitions are now in lib/openssl/pkey.rb.
1 parent 342e844 commit f6bb911

File tree

2 files changed

+54
-88
lines changed

2 files changed

+54
-88
lines changed

ext/openssl/ossl_pkey_dsa.c

Lines changed: 0 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -333,92 +333,6 @@ ossl_dsa_to_public_key(VALUE self)
333333
return obj;
334334
}
335335

336-
/*
337-
* call-seq:
338-
* dsa.syssign(string) -> aString
339-
*
340-
* Computes and returns the DSA signature of _string_, where _string_ is
341-
* expected to be an already-computed message digest of the original input
342-
* data. The signature is issued using the private key of this DSA instance.
343-
*
344-
* === Parameters
345-
* * _string_ is a message digest of the original input data to be signed.
346-
*
347-
* === Example
348-
* dsa = OpenSSL::PKey::DSA.new(2048)
349-
* doc = "Sign me"
350-
* digest = OpenSSL::Digest.digest('SHA1', doc)
351-
* sig = dsa.syssign(digest)
352-
*
353-
*
354-
*/
355-
static VALUE
356-
ossl_dsa_sign(VALUE self, VALUE data)
357-
{
358-
DSA *dsa;
359-
const BIGNUM *dsa_q;
360-
unsigned int buf_len;
361-
VALUE str;
362-
363-
GetDSA(self, dsa);
364-
DSA_get0_pqg(dsa, NULL, &dsa_q, NULL);
365-
if (!dsa_q)
366-
ossl_raise(eDSAError, "incomplete DSA");
367-
if (!DSA_PRIVATE(self, dsa))
368-
ossl_raise(eDSAError, "Private DSA key needed!");
369-
StringValue(data);
370-
str = rb_str_new(0, DSA_size(dsa));
371-
if (!DSA_sign(0, (unsigned char *)RSTRING_PTR(data), RSTRING_LENINT(data),
372-
(unsigned char *)RSTRING_PTR(str),
373-
&buf_len, dsa)) { /* type is ignored (0) */
374-
ossl_raise(eDSAError, NULL);
375-
}
376-
rb_str_set_len(str, buf_len);
377-
378-
return str;
379-
}
380-
381-
/*
382-
* call-seq:
383-
* dsa.sysverify(digest, sig) -> true | false
384-
*
385-
* Verifies whether the signature is valid given the message digest input. It
386-
* does so by validating _sig_ using the public key of this DSA instance.
387-
*
388-
* === Parameters
389-
* * _digest_ is a message digest of the original input data to be signed
390-
* * _sig_ is a DSA signature value
391-
*
392-
* === Example
393-
* dsa = OpenSSL::PKey::DSA.new(2048)
394-
* doc = "Sign me"
395-
* digest = OpenSSL::Digest.digest('SHA1', doc)
396-
* sig = dsa.syssign(digest)
397-
* puts dsa.sysverify(digest, sig) # => true
398-
*
399-
*/
400-
static VALUE
401-
ossl_dsa_verify(VALUE self, VALUE digest, VALUE sig)
402-
{
403-
DSA *dsa;
404-
int ret;
405-
406-
GetDSA(self, dsa);
407-
StringValue(digest);
408-
StringValue(sig);
409-
/* type is ignored (0) */
410-
ret = DSA_verify(0, (unsigned char *)RSTRING_PTR(digest), RSTRING_LENINT(digest),
411-
(unsigned char *)RSTRING_PTR(sig), RSTRING_LENINT(sig), dsa);
412-
if (ret < 0) {
413-
ossl_raise(eDSAError, NULL);
414-
}
415-
else if (ret == 1) {
416-
return Qtrue;
417-
}
418-
419-
return Qfalse;
420-
}
421-
422336
/*
423337
* Document-method: OpenSSL::PKey::DSA#set_pqg
424338
* call-seq:
@@ -475,8 +389,6 @@ Init_ossl_dsa(void)
475389
rb_define_alias(cDSA, "to_s", "export");
476390
rb_define_method(cDSA, "to_der", ossl_dsa_to_der, 0);
477391
rb_define_method(cDSA, "public_key", ossl_dsa_to_public_key, 0);
478-
rb_define_method(cDSA, "syssign", ossl_dsa_sign, 1);
479-
rb_define_method(cDSA, "sysverify", ossl_dsa_verify, 2);
480392

481393
DEF_OSSL_PKEY_BN(cDSA, dsa, p);
482394
DEF_OSSL_PKEY_BN(cDSA, dsa, q);

lib/openssl/pkey.rb

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,60 @@ def new(*args, &blk) # :nodoc:
118118
end
119119
end
120120
end
121+
122+
# :call-seq:
123+
# dsa.syssign(string) -> string
124+
#
125+
# Computes and returns the \DSA signature of +string+, where +string+ is
126+
# expected to be an already-computed message digest of the original input
127+
# data. The signature is issued using the private key of this DSA instance.
128+
#
129+
# <b>Deprecated in version 2.3</b>.
130+
# Consider using PKey::PKey#sign_raw and PKey::PKey#verify_raw instead.
131+
#
132+
# +string+::
133+
# A message digest of the original input data to be signed.
134+
#
135+
# Example:
136+
# dsa = OpenSSL::PKey::DSA.new(2048)
137+
# doc = "Sign me"
138+
# digest = OpenSSL::Digest.digest('SHA1', doc)
139+
#
140+
# # With legacy #syssign and #sysverify:
141+
# sig = dsa.syssign(digest)
142+
# p dsa.sysverify(digest, sig) #=> true
143+
#
144+
# # With #sign_raw and #verify_raw:
145+
# sig = dsa.sign_raw(nil, digest)
146+
# p dsa.verify_raw(nil, sig, digest) #=> true
147+
def syssign(string)
148+
q or raise OpenSSL::PKey::DSAError, "incomplete DSA"
149+
private? or raise OpenSSL::PKey::DSAError, "Private DSA key needed!"
150+
begin
151+
sign_raw(nil, string)
152+
rescue OpenSSL::PKey::PKeyError
153+
raise OpenSSL::PKey::DSAError, $!.message
154+
end
155+
end
156+
157+
# :call-seq:
158+
# dsa.sysverify(digest, sig) -> true | false
159+
#
160+
# Verifies whether the signature is valid given the message digest input.
161+
# It does so by validating +sig+ using the public key of this DSA instance.
162+
#
163+
# <b>Deprecated in version 2.3</b>.
164+
# Consider using PKey::PKey#sign_raw and PKey::PKey#verify_raw instead.
165+
#
166+
# +digest+::
167+
# A message digest of the original input data to be signed.
168+
# +sig+::
169+
# A \DSA signature value.
170+
def sysverify(digest, sig)
171+
verify_raw(nil, sig, digest)
172+
rescue OpenSSL::PKey::PKeyError
173+
raise OpenSSL::PKey::DSAError, $!.message
174+
end
121175
end
122176

123177
if defined?(EC)

0 commit comments

Comments
 (0)