Skip to content

Commit ce805ad

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 1f9da0c commit ce805ad

File tree

2 files changed

+54
-88
lines changed

2 files changed

+54
-88
lines changed

ext/openssl/ossl_pkey_dsa.c

-88
Original file line numberDiff line numberDiff line change
@@ -264,92 +264,6 @@ ossl_dsa_get_params(VALUE self)
264264
return hash;
265265
}
266266

267-
/*
268-
* call-seq:
269-
* dsa.syssign(string) -> aString
270-
*
271-
* Computes and returns the DSA signature of _string_, where _string_ is
272-
* expected to be an already-computed message digest of the original input
273-
* data. The signature is issued using the private key of this DSA instance.
274-
*
275-
* === Parameters
276-
* * _string_ is a message digest of the original input data to be signed.
277-
*
278-
* === Example
279-
* dsa = OpenSSL::PKey::DSA.new(2048)
280-
* doc = "Sign me"
281-
* digest = OpenSSL::Digest.digest('SHA1', doc)
282-
* sig = dsa.syssign(digest)
283-
*
284-
*
285-
*/
286-
static VALUE
287-
ossl_dsa_sign(VALUE self, VALUE data)
288-
{
289-
DSA *dsa;
290-
const BIGNUM *dsa_q;
291-
unsigned int buf_len;
292-
VALUE str;
293-
294-
GetDSA(self, dsa);
295-
DSA_get0_pqg(dsa, NULL, &dsa_q, NULL);
296-
if (!dsa_q)
297-
ossl_raise(eDSAError, "incomplete DSA");
298-
if (!DSA_PRIVATE(self, dsa))
299-
ossl_raise(eDSAError, "Private DSA key needed!");
300-
StringValue(data);
301-
str = rb_str_new(0, DSA_size(dsa));
302-
if (!DSA_sign(0, (unsigned char *)RSTRING_PTR(data), RSTRING_LENINT(data),
303-
(unsigned char *)RSTRING_PTR(str),
304-
&buf_len, dsa)) { /* type is ignored (0) */
305-
ossl_raise(eDSAError, NULL);
306-
}
307-
rb_str_set_len(str, buf_len);
308-
309-
return str;
310-
}
311-
312-
/*
313-
* call-seq:
314-
* dsa.sysverify(digest, sig) -> true | false
315-
*
316-
* Verifies whether the signature is valid given the message digest input. It
317-
* does so by validating _sig_ using the public key of this DSA instance.
318-
*
319-
* === Parameters
320-
* * _digest_ is a message digest of the original input data to be signed
321-
* * _sig_ is a DSA signature value
322-
*
323-
* === Example
324-
* dsa = OpenSSL::PKey::DSA.new(2048)
325-
* doc = "Sign me"
326-
* digest = OpenSSL::Digest.digest('SHA1', doc)
327-
* sig = dsa.syssign(digest)
328-
* puts dsa.sysverify(digest, sig) # => true
329-
*
330-
*/
331-
static VALUE
332-
ossl_dsa_verify(VALUE self, VALUE digest, VALUE sig)
333-
{
334-
DSA *dsa;
335-
int ret;
336-
337-
GetDSA(self, dsa);
338-
StringValue(digest);
339-
StringValue(sig);
340-
/* type is ignored (0) */
341-
ret = DSA_verify(0, (unsigned char *)RSTRING_PTR(digest), RSTRING_LENINT(digest),
342-
(unsigned char *)RSTRING_PTR(sig), RSTRING_LENINT(sig), dsa);
343-
if (ret < 0) {
344-
ossl_raise(eDSAError, NULL);
345-
}
346-
else if (ret == 1) {
347-
return Qtrue;
348-
}
349-
350-
return Qfalse;
351-
}
352-
353267
/*
354268
* Document-method: OpenSSL::PKey::DSA#set_pqg
355269
* call-seq:
@@ -404,8 +318,6 @@ Init_ossl_dsa(void)
404318
rb_define_alias(cDSA, "to_pem", "export");
405319
rb_define_alias(cDSA, "to_s", "export");
406320
rb_define_method(cDSA, "to_der", ossl_dsa_to_der, 0);
407-
rb_define_method(cDSA, "syssign", ossl_dsa_sign, 1);
408-
rb_define_method(cDSA, "sysverify", ossl_dsa_verify, 2);
409321

410322
DEF_OSSL_PKEY_BN(cDSA, dsa, p);
411323
DEF_OSSL_PKEY_BN(cDSA, dsa, q);

lib/openssl/pkey.rb

+54
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,60 @@ def new(*args, &blk) # :nodoc:
158158
end
159159
end
160160
end
161+
162+
# :call-seq:
163+
# dsa.syssign(string) -> string
164+
#
165+
# Computes and returns the \DSA signature of +string+, where +string+ is
166+
# expected to be an already-computed message digest of the original input
167+
# data. The signature is issued using the private key of this DSA instance.
168+
#
169+
# <b>Deprecated in version 3.0</b>.
170+
# Consider using PKey::PKey#sign_raw and PKey::PKey#verify_raw instead.
171+
#
172+
# +string+::
173+
# A message digest of the original input data to be signed.
174+
#
175+
# Example:
176+
# dsa = OpenSSL::PKey::DSA.new(2048)
177+
# doc = "Sign me"
178+
# digest = OpenSSL::Digest.digest('SHA1', doc)
179+
#
180+
# # With legacy #syssign and #sysverify:
181+
# sig = dsa.syssign(digest)
182+
# p dsa.sysverify(digest, sig) #=> true
183+
#
184+
# # With #sign_raw and #verify_raw:
185+
# sig = dsa.sign_raw(nil, digest)
186+
# p dsa.verify_raw(nil, sig, digest) #=> true
187+
def syssign(string)
188+
q or raise OpenSSL::PKey::DSAError, "incomplete DSA"
189+
private? or raise OpenSSL::PKey::DSAError, "Private DSA key needed!"
190+
begin
191+
sign_raw(nil, string)
192+
rescue OpenSSL::PKey::PKeyError
193+
raise OpenSSL::PKey::DSAError, $!.message
194+
end
195+
end
196+
197+
# :call-seq:
198+
# dsa.sysverify(digest, sig) -> true | false
199+
#
200+
# Verifies whether the signature is valid given the message digest input.
201+
# It does so by validating +sig+ using the public key of this DSA instance.
202+
#
203+
# <b>Deprecated in version 3.0</b>.
204+
# Consider using PKey::PKey#sign_raw and PKey::PKey#verify_raw instead.
205+
#
206+
# +digest+::
207+
# A message digest of the original input data to be signed.
208+
# +sig+::
209+
# A \DSA signature value.
210+
def sysverify(digest, sig)
211+
verify_raw(nil, sig, digest)
212+
rescue OpenSSL::PKey::PKeyError
213+
raise OpenSSL::PKey::DSAError, $!.message
214+
end
161215
end
162216

163217
if defined?(EC)

0 commit comments

Comments
 (0)