@@ -46,126 +46,39 @@ VALUE eDSAError;
46
46
/*
47
47
* Private
48
48
*/
49
- struct dsa_blocking_gen_arg {
50
- DSA * dsa ;
51
- int size ;
52
- int * counter ;
53
- unsigned long * h ;
54
- BN_GENCB * cb ;
55
- int result ;
56
- };
57
-
58
- static void *
59
- dsa_blocking_gen (void * arg )
60
- {
61
- struct dsa_blocking_gen_arg * gen = (struct dsa_blocking_gen_arg * )arg ;
62
- gen -> result = DSA_generate_parameters_ex (gen -> dsa , gen -> size , NULL , 0 ,
63
- gen -> counter , gen -> h , gen -> cb );
64
- return 0 ;
65
- }
66
-
67
- static DSA *
68
- dsa_generate (int size )
69
- {
70
- struct ossl_generate_cb_arg cb_arg = { 0 };
71
- struct dsa_blocking_gen_arg gen_arg ;
72
- DSA * dsa = DSA_new ();
73
- BN_GENCB * cb = BN_GENCB_new ();
74
- int counter ;
75
- unsigned long h ;
76
-
77
- if (!dsa || !cb ) {
78
- DSA_free (dsa );
79
- BN_GENCB_free (cb );
80
- ossl_raise (eDSAError , "malloc failure" );
81
- }
82
-
83
- if (rb_block_given_p ())
84
- cb_arg .yield = 1 ;
85
- BN_GENCB_set (cb , ossl_generate_cb_2 , & cb_arg );
86
- gen_arg .dsa = dsa ;
87
- gen_arg .size = size ;
88
- gen_arg .counter = & counter ;
89
- gen_arg .h = & h ;
90
- gen_arg .cb = cb ;
91
- if (cb_arg .yield == 1 ) {
92
- /* we cannot release GVL when callback proc is supplied */
93
- dsa_blocking_gen (& gen_arg );
94
- } else {
95
- /* there's a chance to unblock */
96
- rb_thread_call_without_gvl (dsa_blocking_gen , & gen_arg , ossl_generate_cb_stop , & cb_arg );
97
- }
98
-
99
- BN_GENCB_free (cb );
100
- if (!gen_arg .result ) {
101
- DSA_free (dsa );
102
- if (cb_arg .state ) {
103
- /* Clear OpenSSL error queue before re-raising. By the way, the
104
- * documentation of DSA_generate_parameters_ex() says the error code
105
- * can be obtained by ERR_get_error(), but the default
106
- * implementation, dsa_builtin_paramgen() doesn't put any error... */
107
- ossl_clear_error ();
108
- rb_jump_tag (cb_arg .state );
109
- }
110
- ossl_raise (eDSAError , "DSA_generate_parameters_ex" );
111
- }
112
-
113
- if (!DSA_generate_key (dsa )) {
114
- DSA_free (dsa );
115
- ossl_raise (eDSAError , "DSA_generate_key" );
116
- }
117
-
118
- return dsa ;
119
- }
120
-
121
- /*
122
- * call-seq:
123
- * DSA.generate(size) -> dsa
124
- *
125
- * Creates a new DSA instance by generating a private/public key pair
126
- * from scratch.
127
- *
128
- * === Parameters
129
- * * _size_ is an integer representing the desired key size.
130
- *
131
- */
132
- static VALUE
133
- ossl_dsa_s_generate (VALUE klass , VALUE size )
134
- {
135
- EVP_PKEY * pkey ;
136
- DSA * dsa ;
137
- VALUE obj ;
138
-
139
- obj = rb_obj_alloc (klass );
140
- GetPKey (obj , pkey );
141
-
142
- dsa = dsa_generate (NUM2INT (size ));
143
- if (!EVP_PKEY_assign_DSA (pkey , dsa )) {
144
- DSA_free (dsa );
145
- ossl_raise (eDSAError , "EVP_PKEY_assign_DSA" );
146
- }
147
- return obj ;
148
- }
149
-
150
49
/*
151
50
* call-seq:
152
51
* DSA.new -> dsa
153
- * DSA.new(size) -> dsa
154
52
* DSA.new(string [, pass]) -> dsa
53
+ * DSA.new(size) -> dsa
155
54
*
156
55
* Creates a new DSA instance by reading an existing key from _string_.
157
56
*
158
- * === Parameters
159
- * * _size_ is an integer representing the desired key size.
160
- * * _string_ contains a DER or PEM encoded key.
161
- * * _pass_ is a string that contains an optional password.
57
+ * If called without arguments, creates a new instance with no key components
58
+ * set. They can be set individually by #set_pqg and #set_key.
162
59
*
163
- * === Examples
164
- * DSA.new -> dsa
165
- * DSA.new(1024) -> dsa
166
- * DSA.new(File.read('dsa.pem')) -> dsa
167
- * DSA.new(File.read('dsa.pem'), 'mypassword') -> dsa
60
+ * If called with a String, tries to parse as DER or PEM encoding of a \DSA key.
61
+ * See also OpenSSL::PKey.read which can parse keys of any kinds.
62
+ *
63
+ * If called with a number, generates random parameters and a key pair. This
64
+ * form works as an alias of DSA.generate.
65
+ *
66
+ * +string+::
67
+ * A String that contains a DER or PEM encoded key.
68
+ * +pass+::
69
+ * A String that contains an optional password.
70
+ * +size+::
71
+ * See DSA.generate.
168
72
*
73
+ * Examples:
74
+ * p OpenSSL::PKey::DSA.new(1024)
75
+ * #=> #<OpenSSL::PKey::DSA:0x000055a8d6025bf0 oid=DSA>
76
+ *
77
+ * p OpenSSL::PKey::DSA.new(File.read('dsa.pem'))
78
+ * #=> #<OpenSSL::PKey::DSA:0x000055555d6b8110 oid=DSA>
79
+ *
80
+ * p OpenSSL::PKey::DSA.new(File.read('dsa.pem'), 'mypassword')
81
+ * #=> #<OpenSSL::PKey::DSA:0x0000556f973c40b8 oid=DSA>
169
82
*/
170
83
static VALUE
171
84
ossl_dsa_initialize (int argc , VALUE * argv , VALUE self )
@@ -176,15 +89,13 @@ ossl_dsa_initialize(int argc, VALUE *argv, VALUE self)
176
89
VALUE arg , pass ;
177
90
178
91
GetPKey (self , pkey );
92
+ /* The DSA.new(size, generator) form is handled by lib/openssl/pkey.rb */
179
93
rb_scan_args (argc , argv , "02" , & arg , & pass );
180
94
if (argc == 0 ) {
181
95
dsa = DSA_new ();
182
96
if (!dsa )
183
97
ossl_raise (eDSAError , "DSA_new" );
184
98
}
185
- else if (argc == 1 && RB_INTEGER_TYPE_P (arg )) {
186
- dsa = dsa_generate (NUM2INT (arg ));
187
- }
188
99
else {
189
100
pass = ossl_pem_passwd_value (pass );
190
101
arg = ossl_to_der_if_possible (arg );
@@ -553,7 +464,6 @@ Init_ossl_dsa(void)
553
464
*/
554
465
cDSA = rb_define_class_under (mPKey , "DSA" , cPKey );
555
466
556
- rb_define_singleton_method (cDSA , "generate" , ossl_dsa_s_generate , 1 );
557
467
rb_define_method (cDSA , "initialize" , ossl_dsa_initialize , -1 );
558
468
rb_define_method (cDSA , "initialize_copy" , ossl_dsa_initialize_copy , 1 );
559
469
0 commit comments