Ruby 2.1.5p273(2014-11-13revision48405)

- Main Page
- Modules
- Data Structures
- Files

- File List
- Globals

ossl_pkey_dh.c
Go to the documentation of this file.
1 /*
2 * $Id: ossl_pkey_dh.c 45113 2014-02-22 05:56:02Z naruse $
3 * 'OpenSSL for Ruby' project
4 * Copyright (C) 2001-2002 Michal Rokos <m.rokos@sh.cvut.cz>
5 * All rights reserved.
6 */
7 /*
8 * This program is licenced under the same licence as Ruby.
9 * (See the file 'LICENCE'.)
10 */
11 #if !defined(OPENSSL_NO_DH)
12
13 #include "ossl.h"
14
15 #define GetPKeyDH(obj, pkey) do { \
16 GetPKey((obj), (pkey)); \
17 if (EVP_PKEY_type((pkey)->type) != EVP_PKEY_DH) { /* PARANOIA? */ \
18 ossl_raise(rb_eRuntimeError, "THIS IS NOT A DH!") ; \
19 } \
20 } while (0)
21
22 #define DH_HAS_PRIVATE(dh) ((dh)->priv_key)
23
24 #ifdef OSSL_ENGINE_ENABLED
25 # define DH_PRIVATE(dh) (DH_HAS_PRIVATE(dh) || (dh)->engine)
26 #else
27 # define DH_PRIVATE(dh) DH_HAS_PRIVATE(dh)
28 #endif
29
30
31 /*
32 * Classes
33 */
34 VALUE cDH;
35 VALUE eDHError;
36
37 /*
38 * Public
39 */
40 static VALUE
41 dh_instance(VALUE klass, DH *dh)
42 {
43 EVP_PKEY *pkey;
44 VALUE obj;
45
46 if (!dh) {
47 return Qfalse;
48 }
49 if (!(pkey = EVP_PKEY_new())) {
50 return Qfalse;
51 }
52 if (!EVP_PKEY_assign_DH(pkey, dh)) {
53 EVP_PKEY_free(pkey);
54 return Qfalse;
55 }
56 WrapPKey(klass, obj, pkey);
57
58 return obj;
59 }
60
61 VALUE
62 ossl_dh_new(EVP_PKEY *pkey)
63 {
64 VALUE obj;
65
66 if (!pkey) {
67 obj = dh_instance(cDH, DH_new());
68 } else {
69 if (EVP_PKEY_type(pkey->type) != EVP_PKEY_DH) {
70 ossl_raise(rb_eTypeError, "Not a DH key!");
71 }
72 WrapPKey(cDH, obj, pkey);
73 }
74 if (obj == Qfalse) {
75 ossl_raise(eDHError, NULL);
76 }
77
78 return obj;
79 }
80
81 /*
82 * Private
83 */
84 #if defined(HAVE_DH_GENERATE_PARAMETERS_EX) && HAVE_BN_GENCB
85 struct dh_blocking_gen_arg {
86 DH *dh;
87 int size;
88 int gen;
89 BN_GENCB *cb;
90 int result;
91 };
92
93 static void *
94 dh_blocking_gen(void *arg)
95 {
96 struct dh_blocking_gen_arg *gen = (struct dh_blocking_gen_arg *)arg;
97 gen->result = DH_generate_parameters_ex(gen->dh, gen->size, gen->gen, gen->cb);
98 return 0;
99 }
100 #endif
101
102 static DH *
103 dh_generate(int size, int gen)
104 {
105 #if defined(HAVE_DH_GENERATE_PARAMETERS_EX) && HAVE_BN_GENCB
106 BN_GENCB cb;
107 struct ossl_generate_cb_arg cb_arg;
108 struct dh_blocking_gen_arg gen_arg;
109 DH *dh = DH_new();
110
111 if (!dh) return 0;
112
113 memset(&cb_arg, 0, sizeof(struct ossl_generate_cb_arg));
114 if (rb_block_given_p())
115 cb_arg.yield = 1;
116 BN_GENCB_set(&cb, ossl_generate_cb_2, &cb_arg);
117 gen_arg.dh = dh;
118 gen_arg.size = size;
119 gen_arg.gen = gen;
120 gen_arg.cb = &cb;
121 if (cb_arg.yield == 1) {
122 /* we cannot release GVL when callback proc is supplied */
123 dh_blocking_gen(&gen_arg);
124 } else {
125 /* there's a chance to unblock */
126 rb_thread_call_without_gvl(dh_blocking_gen, &gen_arg, ossl_generate_cb_stop, &cb_arg);
127 }
128
129 if (!gen_arg.result) {
130 DH_free(dh);
131 if (cb_arg.state) rb_jump_tag(cb_arg.state);
132 return 0;
133 }
134 #else
135 DH *dh;
136
137 dh = DH_generate_parameters(size, gen, rb_block_given_p() ? ossl_generate_cb : NULL, NULL);
138 if (!dh) return 0;
139 #endif
140
141 if (!DH_generate_key(dh)) {
142 DH_free(dh);
143 return 0;
144 }
145
146 return dh;
147 }
148
149 /*
150 * call-seq:
151 * DH.generate(size [, generator]) -> dh
152 *
153 * Creates a new DH instance from scratch by generating the private and public
154 * components alike.
155 *
156 * === Parameters
157 * * +size+ is an integer representing the desired key size. Keys smaller than 1024 bits should be considered insecure.
158 * * +generator+ is a small number > 1, typically 2 or 5.
159 *
160 */
161 static VALUE
162 ossl_dh_s_generate(int argc, VALUE *argv, VALUE klass)
163 {
164 DH *dh ;
165 int g = 2;
166 VALUE size, gen, obj;
167
168 if (rb_scan_args(argc, argv, "11", &size, &gen) == 2) {
169 g = NUM2INT(gen);
170 }
171 dh = dh_generate(NUM2INT(size), g);
172 obj = dh_instance(klass, dh);
173 if (obj == Qfalse) {
174 DH_free(dh);
175 ossl_raise(eDHError, NULL);
176 }
177
178 return obj;
179 }
180
181 /*
182 * call-seq:
183 * DH.new([size [, generator] | string]) -> dh
184 *
185 * Either generates a DH instance from scratch or by reading already existing
186 * DH parameters from +string+. Note that when reading a DH instance from
187 * data that was encoded from a DH instance by using DH#to_pem or DH#to_der
188 * the result will *not* contain a public/private key pair yet. This needs to
189 * be generated using DH#generate_key! first.
190 *
191 * === Parameters
192 * * +size+ is an integer representing the desired key size. Keys smaller than 1024 bits should be considered insecure.
193 * * +generator+ is a small number > 1, typically 2 or 5.
194 * * +string+ contains the DER or PEM encoded key.
195 *
196 * === Examples
197 * DH.new # -> dh
198 * DH.new(1024) # -> dh
199 * DH.new(1024, 5) # -> dh
200 * #Reading DH parameters
201 * dh = DH.new(File.read('parameters.pem')) # -> dh, but no public/private key yet
202 * dh.generate_key! # -> dh with public and private key
203 */
204 static VALUE
205 ossl_dh_initialize(int argc, VALUE *argv, VALUE self)
206 {
207 EVP_PKEY *pkey;
208 DH *dh;
209 int g = 2;
210 BIO *in;
211 VALUE arg, gen;
212
213 GetPKey(self, pkey);
214 if(rb_scan_args(argc, argv, "02", &arg, &gen) == 0) {
215 dh = DH_new();
216 }
217 else if (FIXNUM_P(arg)) {
218 if (!NIL_P(gen)) {
219 g = NUM2INT(gen);
220 }
221 if (!(dh = dh_generate(FIX2INT(arg), g))) {
222 ossl_raise(eDHError, NULL);
223 }
224 }
225 else {
226 arg = ossl_to_der_if_possible(arg);
227 in = ossl_obj2bio(arg);
228 dh = PEM_read_bio_DHparams(in, NULL, NULL, NULL);
229 if (!dh){
230 OSSL_BIO_reset(in);
231 dh = d2i_DHparams_bio(in, NULL);
232 }
233 BIO_free(in);
234 if (!dh) {
235 ossl_raise(eDHError, NULL);
236 }
237 }
238 if (!EVP_PKEY_assign_DH(pkey, dh)) {
239 DH_free(dh);
240 ossl_raise(eDHError, NULL);
241 }
242 return self;
243 }
244
245 /*
246 * call-seq:
247 * dh.public? -> true | false
248 *
249 * Indicates whether this DH instance has a public key associated with it or
250 * not. The public key may be retrieved with DH#pub_key.
251 */
252 static VALUE
253 ossl_dh_is_public(VALUE self)
254 {
255 EVP_PKEY *pkey;
256
257 GetPKeyDH(self, pkey);
258
259 return (pkey->pkey.dh->pub_key) ? Qtrue : Qfalse;
260 }
261
262 /*
263 * call-seq:
264 * dh.private? -> true | false
265 *
266 * Indicates whether this DH instance has a private key associated with it or
267 * not. The private key may be retrieved with DH#priv_key.
268 */
269 static VALUE
270 ossl_dh_is_private(VALUE self)
271 {
272 EVP_PKEY *pkey;
273
274 GetPKeyDH(self, pkey);
275
276 return (DH_PRIVATE(pkey->pkey.dh)) ? Qtrue : Qfalse;
277 }
278
279 /*
280 * call-seq:
281 * dh.export -> aString
282 * dh.to_pem -> aString
283 * dh.to_s -> aString
284 *
285 * Encodes this DH to its PEM encoding. Note that any existing per-session
286 * public/private keys will *not* get encoded, just the Diffie-Hellman
287 * parameters will be encoded.
288 */
289 static VALUE
290 ossl_dh_export(VALUE self)
291 {
292 EVP_PKEY *pkey;
293 BIO *out;
294 VALUE str;
295
296 GetPKeyDH(self, pkey);
297 if (!(out = BIO_new(BIO_s_mem()))) {
298 ossl_raise(eDHError, NULL);
299 }
300 if (!PEM_write_bio_DHparams(out, pkey->pkey.dh)) {
301 BIO_free(out);
302 ossl_raise(eDHError, NULL);
303 }
304 str = ossl_membio2str(out);
305
306 return str;
307 }
308
309 /*
310 * call-seq:
311 * dh.to_der -> aString
312 *
313 * Encodes this DH to its DER encoding. Note that any existing per-session
314 * public/private keys will *not* get encoded, just the Diffie-Hellman
315 * parameters will be encoded.
316
317 */
318 static VALUE
319 ossl_dh_to_der(VALUE self)
320 {
321 EVP_PKEY *pkey;
322 unsigned char *p;
323 long len;
324 VALUE str;
325
326 GetPKeyDH(self, pkey);
327 if((len = i2d_DHparams(pkey->pkey.dh, NULL)) <= 0)
328 ossl_raise(eDHError, NULL);
329 str = rb_str_new(0, len);
330 p = (unsigned char *)RSTRING_PTR(str);
331 if(i2d_DHparams(pkey->pkey.dh, &p) < 0)
332 ossl_raise(eDHError, NULL);
333 ossl_str_adjust(str, p);
334
335 return str;
336 }
337
338 /*
339 * call-seq:
340 * dh.params -> hash
341 *
342 * Stores all parameters of key to the hash
343 * INSECURE: PRIVATE INFORMATIONS CAN LEAK OUT!!!
344 * Don't use :-)) (I's up to you)
345 */
346 static VALUE
347 ossl_dh_get_params(VALUE self)
348 {
349 EVP_PKEY *pkey;
350 VALUE hash;
351
352 GetPKeyDH(self, pkey);
353
354 hash = rb_hash_new();
355
356 rb_hash_aset(hash, rb_str_new2("p"), ossl_bn_new(pkey->pkey.dh->p));
357 rb_hash_aset(hash, rb_str_new2("g"), ossl_bn_new(pkey->pkey.dh->g));
358 rb_hash_aset(hash, rb_str_new2("pub_key"), ossl_bn_new(pkey->pkey.dh->pub_key));
359 rb_hash_aset(hash, rb_str_new2("priv_key"), ossl_bn_new(pkey->pkey.dh->priv_key));
360
361 return hash;
362 }
363
364 /*
365 * call-seq:
366 * dh.to_text -> aString
367 *
368 * Prints all parameters of key to buffer
369 * INSECURE: PRIVATE INFORMATIONS CAN LEAK OUT!!!
370 * Don't use :-)) (I's up to you)
371 */
372 static VALUE
373 ossl_dh_to_text(VALUE self)
374 {
375 EVP_PKEY *pkey;
376 BIO *out;
377 VALUE str;
378
379 GetPKeyDH(self, pkey);
380 if (!(out = BIO_new(BIO_s_mem()))) {
381 ossl_raise(eDHError, NULL);
382 }
383 if (!DHparams_print(out, pkey->pkey.dh)) {
384 BIO_free(out);
385 ossl_raise(eDHError, NULL);
386 }
387 str = ossl_membio2str(out);
388
389 return str;
390 }
391
392 /*
393 * call-seq:
394 * dh.public_key -> aDH
395 *
396 * Returns a new DH instance that carries just the public information, i.e.
397 * the prime +p+ and the generator +g+, but no public/private key yet. Such
398 * a pair may be generated using DH#generate_key!. The "public key" needed
399 * for a key exchange with DH#compute_key is considered as per-session
400 * information and may be retrieved with DH#pub_key once a key pair has
401 * been generated.
402 * If the current instance already contains private information (and thus a
403 * valid public/private key pair), this information will no longer be present
404 * in the new instance generated by DH#public_key. This feature is helpful for
405 * publishing the Diffie-Hellman parameters without leaking any of the private
406 * per-session information.
407 *
408 * === Example
409 * dh = OpenSSL::PKey::DH.new(2048) # has public and private key set
410 * public_key = dh.public_key # contains only prime and generator
411 * parameters = public_key.to_der # it's safe to publish this
412 */
413 static VALUE
414 ossl_dh_to_public_key(VALUE self)
415 {
416 EVP_PKEY *pkey;
417 DH *dh;
418 VALUE obj;
419
420 GetPKeyDH(self, pkey);
421 dh = DHparams_dup(pkey->pkey.dh); /* err check perfomed by dh_instance */
422 obj = dh_instance(CLASS_OF(self), dh);
423 if (obj == Qfalse) {
424 DH_free(dh);
425 ossl_raise(eDHError, NULL);
426 }
427
428 return obj;
429 }
430
431 /*
432 * call-seq:
433 * dh.params_ok? -> true | false
434 *
435 * Validates the Diffie-Hellman parameters associated with this instance.
436 * It checks whether a safe prime and a suitable generator are used. If this
437 * is not the case, +false+ is returned.
438 */
439 static VALUE
440 ossl_dh_check_params(VALUE self)
441 {
442 DH *dh;
443 EVP_PKEY *pkey;
444 int codes;
445
446 GetPKeyDH(self, pkey);
447 dh = pkey->pkey.dh;
448
449 if (!DH_check(dh, &codes)) {
450 return Qfalse;
451 }
452
453 return codes == 0 ? Qtrue : Qfalse;
454 }
455
456 /*
457 * call-seq:
458 * dh.generate_key! -> self
459 *
460 * Generates a private and public key unless a private key already exists.
461 * If this DH instance was generated from public DH parameters (e.g. by
462 * encoding the result of DH#public_key), then this method needs to be
463 * called first in order to generate the per-session keys before performing
464 * the actual key exchange.
465 *
466 * === Example
467 * dh = OpenSSL::PKey::DH.new(2048)
468 * public_key = dh.public_key #contains no private/public key yet
469 * public_key.generate_key!
470 * puts public_key.private? # => true
471 */
472 static VALUE
473 ossl_dh_generate_key(VALUE self)
474 {
475 DH *dh;
476 EVP_PKEY *pkey;
477
478 GetPKeyDH(self, pkey);
479 dh = pkey->pkey.dh;
480
481 if (!DH_generate_key(dh))
482 ossl_raise(eDHError, "Failed to generate key");
483 return self;
484 }
485
486 /*
487 * call-seq:
488 * dh.compute_key(pub_bn) -> aString
489 *
490 * Returns a String containing a shared secret computed from the other party's public value.
491 * See DH_compute_key() for further information.
492 *
493 * === Parameters
494 * * +pub_bn+ is a OpenSSL::BN, *not* the DH instance returned by
495 * DH#public_key as that contains the DH parameters only.
496 */
497 static VALUE
498 ossl_dh_compute_key(VALUE self, VALUE pub)
499 {
500 DH *dh;
501 EVP_PKEY *pkey;
502 BIGNUM *pub_key;
503 VALUE str;
504 int len;
505
506 GetPKeyDH(self, pkey);
507 dh = pkey->pkey.dh;
508 pub_key = GetBNPtr(pub);
509 len = DH_size(dh);
510 str = rb_str_new(0, len);
511 if ((len = DH_compute_key((unsigned char *)RSTRING_PTR(str), pub_key, dh)) < 0) {
512 ossl_raise(eDHError, NULL);
513 }
514 rb_str_set_len(str, len);
515
516 return str;
517 }
518
519 OSSL_PKEY_BN(dh, p)
520 OSSL_PKEY_BN(dh, g)
521 OSSL_PKEY_BN(dh, pub_key)
522 OSSL_PKEY_BN(dh, priv_key)
523
524 /*
525 * -----BEGIN DH PARAMETERS-----
526 * MEYCQQD0zXHljRg/mJ9PYLACLv58Cd8VxBxxY7oEuCeURMiTqEhMym16rhhKgZG2
527 * zk2O9uUIBIxSj+NKMURHGaFKyIvLAgEC
528 * -----END DH PARAMETERS-----
529 */
530 static unsigned char DEFAULT_DH_512_PRIM[] = {
531 0xf4, 0xcd, 0x71, 0xe5, 0x8d, 0x18, 0x3f, 0x98,
532 0x9f, 0x4f, 0x60, 0xb0, 0x02, 0x2e, 0xfe, 0x7c,
533 0x09, 0xdf, 0x15, 0xc4, 0x1c, 0x71, 0x63, 0xba,
534 0x04, 0xb8, 0x27, 0x94, 0x44, 0xc8, 0x93, 0xa8,
535 0x48, 0x4c, 0xca, 0x6d, 0x7a, 0xae, 0x18, 0x4a,
536 0x81, 0x91, 0xb6, 0xce, 0x4d, 0x8e, 0xf6, 0xe5,
537 0x08, 0x04, 0x8c, 0x52, 0x8f, 0xe3, 0x4a, 0x31,
538 0x44, 0x47, 0x19, 0xa1, 0x4a, 0xc8, 0x8b, 0xcb,
539 };
540 static unsigned char DEFAULT_DH_512_GEN[] = { 0x02 };
541 DH *OSSL_DEFAULT_DH_512 = NULL;
542
543 /*
544 * -----BEGIN DH PARAMETERS-----
545 * MIGHAoGBAJ0lOVy0VIr/JebWn0zDwY2h+rqITFOpdNr6ugsgvkDXuucdcChhYExJ
546 * AV/ZD2AWPbrTqV76mGRgJg4EddgT1zG0jq3rnFdMj2XzkBYx3BVvfR0Arnby0RHR
547 * T4h7KZ/2zmjvV+eF8kBUHBJAojUlzxKj4QeO2x20FP9X5xmNUXeDAgEC
548 * -----END DH PARAMETERS-----
549 */
550 static unsigned char DEFAULT_DH_1024_PRIM[] = {
551 0x9d, 0x25, 0x39, 0x5c, 0xb4, 0x54, 0x8a, 0xff,
552 0x25, 0xe6, 0xd6, 0x9f, 0x4c, 0xc3, 0xc1, 0x8d,
553 0xa1, 0xfa, 0xba, 0x88, 0x4c, 0x53, 0xa9, 0x74,
554 0xda, 0xfa, 0xba, 0x0b, 0x20, 0xbe, 0x40, 0xd7,
555 0xba, 0xe7, 0x1d, 0x70, 0x28, 0x61, 0x60, 0x4c,
556 0x49, 0x01, 0x5f, 0xd9, 0x0f, 0x60, 0x16, 0x3d,
557 0xba, 0xd3, 0xa9, 0x5e, 0xfa, 0x98, 0x64, 0x60,
558 0x26, 0x0e, 0x04, 0x75, 0xd8, 0x13, 0xd7, 0x31,
559 0xb4, 0x8e, 0xad, 0xeb, 0x9c, 0x57, 0x4c, 0x8f,
560 0x65, 0xf3, 0x90, 0x16, 0x31, 0xdc, 0x15, 0x6f,
561 0x7d, 0x1d, 0x00, 0xae, 0x76, 0xf2, 0xd1, 0x11,
562 0xd1, 0x4f, 0x88, 0x7b, 0x29, 0x9f, 0xf6, 0xce,
563 0x68, 0xef, 0x57, 0xe7, 0x85, 0xf2, 0x40, 0x54,
564 0x1c, 0x12, 0x40, 0xa2, 0x35, 0x25, 0xcf, 0x12,
565 0xa3, 0xe1, 0x07, 0x8e, 0xdb, 0x1d, 0xb4, 0x14,
566 0xff, 0x57, 0xe7, 0x19, 0x8d, 0x51, 0x77, 0x83
567 };
568 static unsigned char DEFAULT_DH_1024_GEN[] = { 0x02 };
569 DH *OSSL_DEFAULT_DH_1024 = NULL;
570
571 static DH*
572 ossl_create_dh(unsigned char *p, size_t plen, unsigned char *g, size_t glen)
573 {
574 DH *dh;
575
576 if ((dh = DH_new()) == NULL) ossl_raise(eDHError, NULL);
577 dh->p = BN_bin2bn(p, rb_long2int(plen), NULL);
578 dh->g = BN_bin2bn(g, rb_long2int(glen), NULL);
579 if (dh->p == NULL || dh->g == NULL){
580 DH_free(dh);
581 ossl_raise(eDHError, NULL);
582 }
583
584 return dh;
585 }
586
587 /*
588 * INIT
589 */
590 void
591 Init_ossl_dh()
592 {
593 #if 0
594 mOSSL = rb_define_module("OpenSSL"); /* let rdoc know about mOSSL and mPKey */
595 mPKey = rb_define_module_under(mOSSL, "PKey");
596 #endif
597
598 /* Document-class: OpenSSL::PKey::DHError
599 *
600 * Generic exception that is raised if an operation on a DH PKey
601 * fails unexpectedly or in case an instantiation of an instance of DH
602 * fails due to non-conformant input data.
603 */
604 eDHError = rb_define_class_under(mPKey, "DHError", ePKeyError);
605 /* Document-class: OpenSSL::PKey::DH
606 *
607 * An implementation of the Diffie-Hellman key exchange protocol based on
608 * discrete logarithms in finite fields, the same basis that DSA is built
609 * on.
610 *
611 * === Accessor methods for the Diffie-Hellman parameters
612 * * DH#p
613 * The prime (an OpenSSL::BN) of the Diffie-Hellman parameters.
614 * * DH#g
615 * The generator (an OpenSSL::BN) g of the Diffie-Hellman parameters.
616 * * DH#pub_key
617 * The per-session public key (an OpenSSL::BN) matching the private key.
618 * This needs to be passed to DH#compute_key.
619 * * DH#priv_key
620 * The per-session private key, an OpenSSL::BN.
621 *
622 * === Example of a key exchange
623 * dh1 = OpenSSL::PKey::DH.new(2048)
624 * der = dh1.public_key.to_der #you may send this publicly to the participating party
625 * dh2 = OpenSSL::PKey::DH.new(der)
626 * dh2.generate_key! #generate the per-session key pair
627 * symm_key1 = dh1.compute_key(dh2.pub_key)
628 * symm_key2 = dh2.compute_key(dh1.pub_key)
629 *
630 * puts symm_key1 == symm_key2 # => true
631 */
632 cDH = rb_define_class_under(mPKey, "DH", cPKey);
633 rb_define_singleton_method(cDH, "generate", ossl_dh_s_generate, -1);
634 rb_define_method(cDH, "initialize", ossl_dh_initialize, -1);
635 rb_define_method(cDH, "public?", ossl_dh_is_public, 0);
636 rb_define_method(cDH, "private?", ossl_dh_is_private, 0);
637 rb_define_method(cDH, "to_text", ossl_dh_to_text, 0);
638 rb_define_method(cDH, "export", ossl_dh_export, 0);
639 rb_define_alias(cDH, "to_pem", "export");
640 rb_define_alias(cDH, "to_s", "export");
641 rb_define_method(cDH, "to_der", ossl_dh_to_der, 0);
642 rb_define_method(cDH, "public_key", ossl_dh_to_public_key, 0);
643 rb_define_method(cDH, "params_ok?", ossl_dh_check_params, 0);
644 rb_define_method(cDH, "generate_key!", ossl_dh_generate_key, 0);
645 rb_define_method(cDH, "compute_key", ossl_dh_compute_key, 1);
646
647 DEF_OSSL_PKEY_BN(cDH, dh, p);
648 DEF_OSSL_PKEY_BN(cDH, dh, g);
649 DEF_OSSL_PKEY_BN(cDH, dh, pub_key);
650 DEF_OSSL_PKEY_BN(cDH, dh, priv_key);
651 rb_define_method(cDH, "params", ossl_dh_get_params, 0);
652
653 OSSL_DEFAULT_DH_512 = ossl_create_dh(
654 DEFAULT_DH_512_PRIM, sizeof(DEFAULT_DH_512_PRIM),
655 DEFAULT_DH_512_GEN, sizeof(DEFAULT_DH_512_GEN));
656 OSSL_DEFAULT_DH_1024 = ossl_create_dh(
657 DEFAULT_DH_1024_PRIM, sizeof(DEFAULT_DH_1024_PRIM),
658 DEFAULT_DH_1024_GEN, sizeof(DEFAULT_DH_1024_GEN));
659 }
660
661 #else /* defined NO_DH */
662 void
663 Init_ossl_dh()
664 {
665 }
666 #endif /* NO_DH */
GetPKeyDH
#define GetPKeyDH(obj, pkey)
Definition: ossl_pkey_dh.c:15
mOSSL
VALUE mOSSL
Definition: ossl.c:259
DEFAULT_DH_1024_PRIM
static unsigned char DEFAULT_DH_1024_PRIM[]
Definition: ossl_pkey_dh.c:550
dh_instance
static VALUE dh_instance(VALUE klass, DH *dh)
Definition: ossl_pkey_dh.c:41
ossl_dh_generate_key
static VALUE ossl_dh_generate_key(VALUE self)
Definition: ossl_pkey_dh.c:473
NUM2INT
#define NUM2INT(x)
Definition: ruby.h:630
rb_define_singleton_method
void rb_define_singleton_method(VALUE obj, const char *name, VALUE(*func)(ANYARGS), int argc)
Defines a singleton method for obj.
Definition: class.c:1655
mPKey
VALUE mPKey
Definition: ossl_pkey.c:16
CLASS_OF
#define CLASS_OF(v)
Definition: ruby.h:440
ePKeyError
VALUE ePKeyError
Definition: ossl_pkey.c:18
OSSL_PKEY_BN
#define OSSL_PKEY_BN(keytype, name)
Definition: ossl_pkey.h:103
Qtrue
#define Qtrue
Definition: ruby.h:426
ossl_str_adjust
#define ossl_str_adjust(str, p)
Definition: ossl.h:138
OSSL_DEFAULT_DH_512
DH * OSSL_DEFAULT_DH_512
Definition: ossl_pkey_dh.c:541
rb_eTypeError
VALUE rb_eTypeError
Definition: error.c:548
rb_long2int
#define rb_long2int(n)
Definition: ruby.h:317
rb_str_set_len
void rb_str_set_len(VALUE, long)
Definition: string.c:2008
rb_define_class_under
VALUE rb_define_class_under(VALUE outer, const char *name, VALUE super)
Defines a class under the namespace of outer.
Definition: class.c:676
GetPKey
#define GetPKey(obj, pkey)
Definition: ossl_pkey.h:30
DH_PRIVATE
#define DH_PRIVATE(dh)
Definition: ossl_pkey_dh.c:27
ossl_dh_compute_key
static VALUE ossl_dh_compute_key(VALUE self, VALUE pub)
Definition: ossl_pkey_dh.c:498
ossl_dh_to_text
static VALUE ossl_dh_to_text(VALUE self)
Definition: ossl_pkey_dh.c:373
OSSL_DEFAULT_DH_1024
DH * OSSL_DEFAULT_DH_1024
Definition: ossl_pkey_dh.c:569
ossl_membio2str
VALUE ossl_membio2str(BIO *bio)
Definition: ossl_bio.c:77
FIXNUM_P
#define FIXNUM_P(f)
Definition: ruby.h:347
eDHError
VALUE eDHError
Definition: ossl_pkey_dh.c:35
ossl_dh_new
VALUE ossl_dh_new(EVP_PKEY *pkey)
Definition: ossl_pkey_dh.c:62
ossl_dh_check_params
static VALUE ossl_dh_check_params(VALUE self)
Definition: ossl_pkey_dh.c:440
ossl_generate_cb
void ossl_generate_cb(int p, int n, void *arg)
Definition: ossl_pkey.c:25
ossl_to_der_if_possible
VALUE ossl_to_der_if_possible(VALUE obj)
Definition: ossl.c:283
rb_block_given_p
int rb_block_given_p(void)
Definition: eval.c:712
rb_hash_aset
VALUE rb_hash_aset(VALUE hash, VALUE key, VALUE val)
Definition: hash.c:1393
OSSL_BIO_reset
#define OSSL_BIO_reset(bio)
Definition: ossl.h:155
ossl_dh_s_generate
static VALUE ossl_dh_s_generate(int argc, VALUE *argv, VALUE klass)
Definition: ossl_pkey_dh.c:162
NIL_P
#define NIL_P(v)
Definition: ruby.h:438
ossl_dh_initialize
static VALUE ossl_dh_initialize(int argc, VALUE *argv, VALUE self)
Definition: ossl_pkey_dh.c:205
DEF_OSSL_PKEY_BN
#define DEF_OSSL_PKEY_BN(class, keytype, name)
Definition: ossl_pkey.h:145
ossl_dh_to_public_key
static VALUE ossl_dh_to_public_key(VALUE self)
Definition: ossl_pkey_dh.c:414
argc
int argc
Definition: ruby.c:131
Qfalse
#define Qfalse
Definition: ruby.h:425
rb_str_new2
#define rb_str_new2
Definition: intern.h:840
rb_thread_call_without_gvl
void * rb_thread_call_without_gvl(void *(*func)(void *), void *data1, rb_unblock_function_t *ubf, void *data2)
DEFAULT_DH_1024_GEN
static unsigned char DEFAULT_DH_1024_GEN[]
Definition: ossl_pkey_dh.c:568
rb_define_alias
void rb_define_alias(VALUE klass, const char *name1, const char *name2)
Defines an alias of a method.
Definition: class.c:1697
DEFAULT_DH_512_GEN
static unsigned char DEFAULT_DH_512_GEN[]
Definition: ossl_pkey_dh.c:540
WrapPKey
#define WrapPKey(klass, obj, pkey)
Definition: ossl_pkey.h:23
ossl_obj2bio
BIO * ossl_obj2bio(VALUE obj)
Definition: ossl_bio.c:17
rb_hash_new
VALUE rb_hash_new(void)
Definition: hash.c:298
ossl_dh_to_der
static VALUE ossl_dh_to_der(VALUE self)
Definition: ossl_pkey_dh.c:319
rb_scan_args
int rb_scan_args(int argc, const VALUE *argv, const char *fmt,...)
Definition: class.c:1728
ossl_create_dh
static DH * ossl_create_dh(unsigned char *p, size_t plen, unsigned char *g, size_t glen)
Definition: ossl_pkey_dh.c:572
VALUE
unsigned long VALUE
Definition: ruby.h:88
result
static VALUE result
Definition: nkf.c:40
DEFAULT_DH_512_PRIM
static unsigned char DEFAULT_DH_512_PRIM[]
Definition: ossl_pkey_dh.c:530
FIX2INT
#define FIX2INT(x)
Definition: ruby.h:632
ossl_dh_is_public
static VALUE ossl_dh_is_public(VALUE self)
Definition: ossl_pkey_dh.c:253
Init_ossl_dh
void Init_ossl_dh()
Definition: ossl_pkey_dh.c:591
rb_jump_tag
void rb_jump_tag(int tag)
Definition: eval.c:706
ossl_dh_export
static VALUE ossl_dh_export(VALUE self)
Definition: ossl_pkey_dh.c:290
rb_define_module_under
VALUE rb_define_module_under(VALUE outer, const char *name)
Definition: class.c:766
RSTRING_PTR
#define RSTRING_PTR(str)
Definition: ruby.h:845
size
int size
Definition: encoding.c:49
dh_generate
static DH * dh_generate(int size, int gen)
Definition: ossl_pkey_dh.c:103
ossl_raise
void ossl_raise(VALUE exc, const char *fmt,...)
Definition: ossl.c:333
hash
static unsigned int hash(const char *str, unsigned int len)
Definition: lex.c:56
ossl_bn_new
VALUE ossl_bn_new(const BIGNUM *bn)
Definition: ossl_bn.c:43
ossl.h
GetBNPtr
BIGNUM * GetBNPtr(VALUE obj)
Definition: ossl_bn.c:58
rb_define_module
VALUE rb_define_module(const char *name)
Definition: class.c:746
cPKey
VALUE cPKey
Definition: ossl_pkey.c:17
NULL
#define NULL
Definition: _sdbm.c:103
rb_define_method
void rb_define_method(VALUE klass, const char *name, VALUE(*func)(ANYARGS), int argc)
Definition: class.c:1488
ossl_dh_is_private
static VALUE ossl_dh_is_private(VALUE self)
Definition: ossl_pkey_dh.c:270
ossl_dh_get_params
static VALUE ossl_dh_get_params(VALUE self)
Definition: ossl_pkey_dh.c:347
cDH
VALUE cDH
Definition: ossl_pkey_dh.c:34
argv
char ** argv
Definition: ruby.c:132
rb_str_new
VALUE rb_str_new(const char *, long)
Definition: string.c:534