@@ -266,96 +266,45 @@ ossl_dh_get_params(VALUE self)
266
266
return hash ;
267
267
}
268
268
269
- /*
270
- * call-seq:
271
- * dh.to_text -> aString
272
- *
273
- * Prints all parameters of key to buffer
274
- * INSECURE: PRIVATE INFORMATIONS CAN LEAK OUT!!!
275
- * Don't use :-)) (I's up to you)
276
- */
277
- static VALUE
278
- ossl_dh_to_text (VALUE self )
279
- {
280
- DH * dh ;
281
- BIO * out ;
282
- VALUE str ;
283
-
284
- GetDH (self , dh );
285
- if (!(out = BIO_new (BIO_s_mem ()))) {
286
- ossl_raise (eDHError , NULL );
287
- }
288
- if (!DHparams_print (out , dh )) {
289
- BIO_free (out );
290
- ossl_raise (eDHError , NULL );
291
- }
292
- str = ossl_membio2str (out );
293
-
294
- return str ;
295
- }
296
-
297
- /*
298
- * call-seq:
299
- * dh.public_key -> aDH
300
- *
301
- * Returns a new DH instance that carries just the public information, i.e.
302
- * the prime _p_ and the generator _g_, but no public/private key yet. Such
303
- * a pair may be generated using DH#generate_key!. The "public key" needed
304
- * for a key exchange with DH#compute_key is considered as per-session
305
- * information and may be retrieved with DH#pub_key once a key pair has
306
- * been generated.
307
- * If the current instance already contains private information (and thus a
308
- * valid public/private key pair), this information will no longer be present
309
- * in the new instance generated by DH#public_key. This feature is helpful for
310
- * publishing the Diffie-Hellman parameters without leaking any of the private
311
- * per-session information.
312
- *
313
- * === Example
314
- * dh = OpenSSL::PKey::DH.new(2048) # has public and private key set
315
- * public_key = dh.public_key # contains only prime and generator
316
- * parameters = public_key.to_der # it's safe to publish this
317
- */
318
- static VALUE
319
- ossl_dh_to_public_key (VALUE self )
320
- {
321
- EVP_PKEY * pkey ;
322
- DH * orig_dh , * dh ;
323
- VALUE obj ;
324
-
325
- obj = rb_obj_alloc (rb_obj_class (self ));
326
- GetPKey (obj , pkey );
327
-
328
- GetDH (self , orig_dh );
329
- dh = DHparams_dup (orig_dh );
330
- if (!dh )
331
- ossl_raise (eDHError , "DHparams_dup" );
332
- if (!EVP_PKEY_assign_DH (pkey , dh )) {
333
- DH_free (dh );
334
- ossl_raise (eDHError , "EVP_PKEY_assign_DH" );
335
- }
336
- return obj ;
337
- }
338
-
339
269
/*
340
270
* call-seq:
341
271
* dh.params_ok? -> true | false
342
272
*
343
273
* Validates the Diffie-Hellman parameters associated with this instance.
344
274
* It checks whether a safe prime and a suitable generator are used. If this
345
275
* is not the case, +false+ is returned.
276
+ *
277
+ * See also the man page EVP_PKEY_param_check(3).
346
278
*/
347
279
static VALUE
348
280
ossl_dh_check_params (VALUE self )
349
281
{
282
+ int ret ;
283
+ #ifdef HAVE_EVP_PKEY_CHECK
284
+ EVP_PKEY * pkey ;
285
+ EVP_PKEY_CTX * pctx ;
286
+
287
+ GetPKey (self , pkey );
288
+ pctx = EVP_PKEY_CTX_new (pkey , /* engine */ NULL );
289
+ if (!pctx )
290
+ ossl_raise (eDHError , "EVP_PKEY_CTX_new" );
291
+ ret = EVP_PKEY_param_check (pctx );
292
+ EVP_PKEY_CTX_free (pctx );
293
+ #else
350
294
DH * dh ;
351
295
int codes ;
352
296
353
297
GetDH (self , dh );
354
- if (!DH_check (dh , & codes )) {
355
- return Qfalse ;
356
- }
298
+ ret = DH_check (dh , & codes ) == 1 && codes == 0 ;
299
+ #endif
357
300
358
- return codes == 0 ? Qtrue : Qfalse ;
301
+ if (ret == 1 )
302
+ return Qtrue ;
303
+ else {
304
+ /* DH_check_ex() will put error entry on failure */
305
+ ossl_clear_error ();
306
+ return Qfalse ;
307
+ }
359
308
}
360
309
361
310
/*
@@ -412,26 +361,30 @@ Init_ossl_dh(void)
412
361
* The per-session private key, an OpenSSL::BN.
413
362
*
414
363
* === Example of a key exchange
415
- * dh1 = OpenSSL::PKey::DH.new(2048)
416
- * der = dh1.public_key.to_der #you may send this publicly to the participating party
417
- * dh2 = OpenSSL::PKey::DH.new(der)
418
- * dh2.generate_key! #generate the per-session key pair
419
- * symm_key1 = dh1.compute_key(dh2.pub_key)
420
- * symm_key2 = dh2.compute_key(dh1.pub_key)
364
+ * # you may send the parameters (der) and own public key (pub1) publicly
365
+ * # to the participating party
366
+ * dh1 = OpenSSL::PKey::DH.new(2048)
367
+ * der = dh1.to_der
368
+ * pub1 = dh1.pub_key
369
+ *
370
+ * # the other party generates its per-session key pair
371
+ * dhparams = OpenSSL::PKey::DH.new(der)
372
+ * dh2 = OpenSSL::PKey.generate_key(dhparams)
373
+ * pub2 = dh2.pub_key
421
374
*
422
- * puts symm_key1 == symm_key2 # => true
375
+ * symm_key1 = dh1.compute_key(pub2)
376
+ * symm_key2 = dh2.compute_key(pub1)
377
+ * puts symm_key1 == symm_key2 # => true
423
378
*/
424
379
cDH = rb_define_class_under (mPKey , "DH" , cPKey );
425
380
rb_define_method (cDH , "initialize" , ossl_dh_initialize , -1 );
426
381
rb_define_method (cDH , "initialize_copy" , ossl_dh_initialize_copy , 1 );
427
382
rb_define_method (cDH , "public?" , ossl_dh_is_public , 0 );
428
383
rb_define_method (cDH , "private?" , ossl_dh_is_private , 0 );
429
- rb_define_method (cDH , "to_text" , ossl_dh_to_text , 0 );
430
384
rb_define_method (cDH , "export" , ossl_dh_export , 0 );
431
385
rb_define_alias (cDH , "to_pem" , "export" );
432
386
rb_define_alias (cDH , "to_s" , "export" );
433
387
rb_define_method (cDH , "to_der" , ossl_dh_to_der , 0 );
434
- rb_define_method (cDH , "public_key" , ossl_dh_to_public_key , 0 );
435
388
rb_define_method (cDH , "params_ok?" , ossl_dh_check_params , 0 );
436
389
437
390
DEF_OSSL_PKEY_BN (cDH , dh , p );
0 commit comments