Ruby  1.9.3p551(2014-11-13revision48407)
ossl_pkey_ec.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006-2007 Technorama Ltd. <oss-ruby@technorama.net>
3  */
4 
5 #include "ossl.h"
6 
7 #if !defined(OPENSSL_NO_EC) && (OPENSSL_VERSION_NUMBER >= 0x0090802fL)
8 
9 typedef struct {
10  EC_GROUP *group;
11  int dont_free;
12 } ossl_ec_group;
13 
14 typedef struct {
15  EC_POINT *point;
16  int dont_free;
17 } ossl_ec_point;
18 
19 
20 #define EXPORT_PEM 0
21 #define EXPORT_DER 1
22 
23 
24 #define GetPKeyEC(obj, pkey) do { \
25  GetPKey((obj), (pkey)); \
26  if (EVP_PKEY_type((pkey)->type) != EVP_PKEY_EC) { \
27  ossl_raise(rb_eRuntimeError, "THIS IS NOT A EC PKEY!"); \
28  } \
29 } while (0)
30 
31 #define SafeGet_ec_group(obj, group) do { \
32  OSSL_Check_Kind((obj), cEC_GROUP); \
33  Data_Get_Struct((obj), ossl_ec_group, (group)); \
34 } while(0)
35 
36 #define Get_EC_KEY(obj, key) do { \
37  EVP_PKEY *pkey; \
38  GetPKeyEC((obj), pkey); \
39  (key) = pkey->pkey.ec; \
40 } while(0)
41 
42 #define Require_EC_KEY(obj, key) do { \
43  Get_EC_KEY((obj), (key)); \
44  if ((key) == NULL) \
45  ossl_raise(eECError, "EC_KEY is not initialized"); \
46 } while(0)
47 
48 #define SafeRequire_EC_KEY(obj, key) do { \
49  OSSL_Check_Kind((obj), cEC); \
50  Require_EC_KEY((obj), (key)); \
51 } while (0)
52 
53 #define Get_EC_GROUP(obj, g) do { \
54  ossl_ec_group *ec_group; \
55  Data_Get_Struct((obj), ossl_ec_group, ec_group); \
56  if (ec_group == NULL) \
57  ossl_raise(eEC_GROUP, "missing ossl_ec_group structure"); \
58  (g) = ec_group->group; \
59 } while(0)
60 
61 #define Require_EC_GROUP(obj, group) do { \
62  Get_EC_GROUP((obj), (group)); \
63  if ((group) == NULL) \
64  ossl_raise(eEC_GROUP, "EC_GROUP is not initialized"); \
65 } while(0)
66 
67 #define SafeRequire_EC_GROUP(obj, group) do { \
68  OSSL_Check_Kind((obj), cEC_GROUP); \
69  Require_EC_GROUP((obj), (group)); \
70 } while(0)
71 
72 #define Get_EC_POINT(obj, p) do { \
73  ossl_ec_point *ec_point; \
74  Data_Get_Struct((obj), ossl_ec_point, ec_point); \
75  if (ec_point == NULL) \
76  ossl_raise(eEC_POINT, "missing ossl_ec_point structure"); \
77  (p) = ec_point->point; \
78 } while(0)
79 
80 #define Require_EC_POINT(obj, point) do { \
81  Get_EC_POINT((obj), (point)); \
82  if ((point) == NULL) \
83  ossl_raise(eEC_POINT, "EC_POINT is not initialized"); \
84 } while(0)
85 
86 #define SafeRequire_EC_POINT(obj, point) do { \
87  OSSL_Check_Kind((obj), cEC_POINT); \
88  Require_EC_POINT((obj), (point)); \
89 } while(0)
90 
91 VALUE cEC;
97 
98 static ID s_GFp;
99 static ID s_GFp_simple;
100 static ID s_GFp_mont;
101 static ID s_GFp_nist;
102 static ID s_GF2m;
103 static ID s_GF2m_simple;
104 
105 static ID ID_uncompressed;
106 static ID ID_compressed;
107 static ID ID_hybrid;
108 
109 static VALUE ec_instance(VALUE klass, EC_KEY *ec)
110 {
111  EVP_PKEY *pkey;
112  VALUE obj;
113 
114  if (!ec) {
115  return Qfalse;
116  }
117  if (!(pkey = EVP_PKEY_new())) {
118  return Qfalse;
119  }
120  if (!EVP_PKEY_assign_EC_KEY(pkey, ec)) {
121  EVP_PKEY_free(pkey);
122  return Qfalse;
123  }
124  WrapPKey(klass, obj, pkey);
125 
126  return obj;
127 }
128 
129 VALUE ossl_ec_new(EVP_PKEY *pkey)
130 {
131  VALUE obj;
132 
133  if (!pkey) {
134  obj = ec_instance(cEC, EC_KEY_new());
135  } else {
136  if (EVP_PKEY_type(pkey->type) != EVP_PKEY_EC) {
137  ossl_raise(rb_eTypeError, "Not a EC key!");
138  }
139  WrapPKey(cEC, obj, pkey);
140  }
141  if (obj == Qfalse) {
143  }
144 
145  return obj;
146 }
147 
148 
149 /* call-seq:
150  * OpenSSL::PKey::EC.new()
151  * OpenSSL::PKey::EC.new(ec_key)
152  * OpenSSL::PKey::EC.new(ec_group)
153  * OpenSSL::PKey::EC.new("secp112r1")
154  * OpenSSL::PKey::EC.new(pem_string)
155  * OpenSSL::PKey::EC.new(pem_string [, pwd])
156  * OpenSSL::PKey::EC.new(der_string)
157  *
158  * See the OpenSSL documentation for:
159  * EC_KEY_*
160  */
161 static VALUE ossl_ec_key_initialize(int argc, VALUE *argv, VALUE self)
162 {
163  EVP_PKEY *pkey;
164  EC_KEY *ec = NULL;
165  VALUE arg, pass;
166  VALUE group = Qnil;
167  char *passwd = NULL;
168 
169  GetPKey(self, pkey);
170  if (pkey->pkey.ec)
171  ossl_raise(eECError, "EC_KEY already initialized");
172 
173  rb_scan_args(argc, argv, "02", &arg, &pass);
174 
175  if (NIL_P(arg)) {
176  ec = EC_KEY_new();
177  } else {
178  if (rb_obj_is_kind_of(arg, cEC)) {
179  EC_KEY *other_ec = NULL;
180 
181  SafeRequire_EC_KEY(arg, other_ec);
182  ec = EC_KEY_dup(other_ec);
183  } else if (rb_obj_is_kind_of(arg, cEC_GROUP)) {
184  ec = EC_KEY_new();
185  group = arg;
186  } else {
187  BIO *in = ossl_obj2bio(arg);
188 
189  if (!NIL_P(pass)) {
190  passwd = StringValuePtr(pass);
191  }
192  ec = PEM_read_bio_ECPrivateKey(in, NULL, ossl_pem_passwd_cb, passwd);
193  if (!ec) {
194  OSSL_BIO_reset(in);
195  ec = PEM_read_bio_EC_PUBKEY(in, NULL, ossl_pem_passwd_cb, passwd);
196  }
197  if (!ec) {
198  OSSL_BIO_reset(in);
199  ec = d2i_ECPrivateKey_bio(in, NULL);
200  }
201  if (!ec) {
202  OSSL_BIO_reset(in);
203  ec = d2i_EC_PUBKEY_bio(in, NULL);
204  }
205 
206  BIO_free(in);
207 
208  if (ec == NULL) {
209  const char *name = StringValueCStr(arg);
210  int nid = OBJ_sn2nid(name);
211 
212  (void)ERR_get_error();
213  if (nid == NID_undef)
214  ossl_raise(eECError, "unknown curve name (%s)\n", name);
215 
216  if ((ec = EC_KEY_new_by_curve_name(nid)) == NULL)
217  ossl_raise(eECError, "unable to create curve (%s)\n", name);
218 
219  EC_KEY_set_asn1_flag(ec, OPENSSL_EC_NAMED_CURVE);
220  EC_KEY_set_conv_form(ec, POINT_CONVERSION_UNCOMPRESSED);
221  }
222  }
223  }
224 
225  if (ec == NULL)
227 
228  if (!EVP_PKEY_assign_EC_KEY(pkey, ec)) {
229  EC_KEY_free(ec);
230  ossl_raise(eECError, "EVP_PKEY_assign_EC_KEY");
231  }
232 
233  rb_iv_set(self, "@group", Qnil);
234 
235  if (!NIL_P(group))
236  rb_funcall(self, rb_intern("group="), 1, arg);
237 
238  return self;
239 }
240 
241 /*
242  * call-seq:
243  * key.group => group
244  *
245  * Returns a constant <code>OpenSSL::EC::Group</code> that is tied to the key.
246  * Modifying the returned group can make the key invalid.
247  */
248 static VALUE ossl_ec_key_get_group(VALUE self)
249 {
250  VALUE group_v;
251  EC_KEY *ec;
252  ossl_ec_group *ec_group;
253  EC_GROUP *group;
254 
255  Require_EC_KEY(self, ec);
256 
257  group_v = rb_iv_get(self, "@group");
258  if (!NIL_P(group_v))
259  return group_v;
260 
261  if ((group = (EC_GROUP *)EC_KEY_get0_group(ec)) != NULL) {
262  group_v = rb_obj_alloc(cEC_GROUP);
263  SafeGet_ec_group(group_v, ec_group);
264  ec_group->group = group;
265  ec_group->dont_free = 1;
266  rb_iv_set(group_v, "@key", self);
267  rb_iv_set(self, "@group", group_v);
268  return group_v;
269  }
270 
271  return Qnil;
272 }
273 
274 /*
275  * call-seq:
276  * key.group = group => group
277  *
278  * Returns the same object passed, not the group object associated with the key.
279  * If you wish to access the group object tied to the key call key.group after setting
280  * the group.
281  *
282  * Setting the group will immediately destroy any previously assigned group object.
283  * The group is internally copied by OpenSSL. Modifying the original group after
284  * assignment will not effect the internal key structure.
285  * (your changes may be lost). BE CAREFUL.
286  *
287  * EC_KEY_set_group calls EC_GROUP_free(key->group) then EC_GROUP_dup(), not EC_GROUP_copy.
288  * This documentation is accurate for OpenSSL 0.9.8b.
289  */
290 static VALUE ossl_ec_key_set_group(VALUE self, VALUE group_v)
291 {
292  VALUE old_group_v;
293  EC_KEY *ec;
294  EC_GROUP *group;
295 
296  Require_EC_KEY(self, ec);
297  SafeRequire_EC_GROUP(group_v, group);
298 
299  old_group_v = rb_iv_get(self, "@group");
300  if (!NIL_P(old_group_v)) {
301  ossl_ec_group *old_ec_group;
302  SafeGet_ec_group(old_group_v, old_ec_group);
303 
304  old_ec_group->group = NULL;
305  old_ec_group->dont_free = 0;
306  rb_iv_set(old_group_v, "@key", Qnil);
307  }
308 
309  rb_iv_set(self, "@group", Qnil);
310 
311  if (EC_KEY_set_group(ec, group) != 1)
312  ossl_raise(eECError, "EC_KEY_set_group");
313 
314  return group_v;
315 }
316 
317 /*
318  * call-seq:
319  * key.private_key => OpenSSL::BN
320  *
321  * See the OpenSSL documentation for EC_KEY_get0_private_key()
322  */
323 static VALUE ossl_ec_key_get_private_key(VALUE self)
324 {
325  EC_KEY *ec;
326  const BIGNUM *bn;
327 
328  Require_EC_KEY(self, ec);
329 
330  if ((bn = EC_KEY_get0_private_key(ec)) == NULL)
331  return Qnil;
332 
333  return ossl_bn_new(bn);
334 }
335 
336 /*
337  * call-seq:
338  * key.private_key = openssl_bn
339  *
340  * See the OpenSSL documentation for EC_KEY_set_private_key()
341  */
342 static VALUE ossl_ec_key_set_private_key(VALUE self, VALUE private_key)
343 {
344  EC_KEY *ec;
345  BIGNUM *bn = NULL;
346 
347  Require_EC_KEY(self, ec);
348  if (!NIL_P(private_key))
349  bn = GetBNPtr(private_key);
350 
351  switch (EC_KEY_set_private_key(ec, bn)) {
352  case 1:
353  break;
354  case 0:
355  if (bn == NULL)
356  break;
357  default:
358  ossl_raise(eECError, "EC_KEY_set_private_key");
359  }
360 
361  return private_key;
362 }
363 
364 
365 static VALUE ossl_ec_point_dup(const EC_POINT *point, VALUE group_v)
366 {
367  VALUE obj;
368  const EC_GROUP *group;
369  ossl_ec_point *new_point;
370 
371  obj = rb_obj_alloc(cEC_POINT);
372  Data_Get_Struct(obj, ossl_ec_point, new_point);
373 
374  SafeRequire_EC_GROUP(group_v, group);
375 
376  new_point->point = EC_POINT_dup(point, group);
377  if (new_point->point == NULL)
378  ossl_raise(eEC_POINT, "EC_POINT_dup");
379  rb_iv_set(obj, "@group", group_v);
380 
381  return obj;
382 }
383 
384 /*
385  * call-seq:
386  * key.public_key => OpenSSL::PKey::EC::Point
387  *
388  * See the OpenSSL documentation for EC_KEY_get0_public_key()
389  */
390 static VALUE ossl_ec_key_get_public_key(VALUE self)
391 {
392  EC_KEY *ec;
393  const EC_POINT *point;
394  VALUE group;
395 
396  Require_EC_KEY(self, ec);
397 
398  if ((point = EC_KEY_get0_public_key(ec)) == NULL)
399  return Qnil;
400 
401  group = rb_funcall(self, rb_intern("group"), 0);
402  if (NIL_P(group))
403  ossl_raise(eECError, "EC_KEY_get0_get0_group (has public_key but no group???");
404 
405  return ossl_ec_point_dup(point, group);
406 }
407 
408 /*
409  * call-seq:
410  * key.public_key = ec_point
411  *
412  * See the OpenSSL documentation for EC_KEY_set_public_key()
413  */
414 static VALUE ossl_ec_key_set_public_key(VALUE self, VALUE public_key)
415 {
416  EC_KEY *ec;
417  EC_POINT *point = NULL;
418 
419  Require_EC_KEY(self, ec);
420  if (!NIL_P(public_key))
421  SafeRequire_EC_POINT(public_key, point);
422 
423  switch (EC_KEY_set_public_key(ec, point)) {
424  case 1:
425  break;
426  case 0:
427  if (point == NULL)
428  break;
429  default:
430  ossl_raise(eECError, "EC_KEY_set_public_key");
431  }
432 
433  return public_key;
434 }
435 
436 /*
437  * call-seq:
438  * key.public_key? => true or false
439  *
440  * Both public_key? and private_key? may return false at the same time unlike other PKey classes.
441  */
442 static VALUE ossl_ec_key_is_public_key(VALUE self)
443 {
444  EC_KEY *ec;
445 
446  Require_EC_KEY(self, ec);
447 
448  return (EC_KEY_get0_public_key(ec) ? Qtrue : Qfalse);
449 }
450 
451 /*
452  * call-seq:
453  * key.private_key? => true or false
454  *
455  * Both public_key? and private_key? may return false at the same time unlike other PKey classes.
456  */
457 static VALUE ossl_ec_key_is_private_key(VALUE self)
458 {
459  EC_KEY *ec;
460 
461  Require_EC_KEY(self, ec);
462 
463  return (EC_KEY_get0_private_key(ec) ? Qtrue : Qfalse);
464 }
465 
466 static VALUE ossl_ec_key_to_string(VALUE self, VALUE ciph, VALUE pass, int format)
467 {
468  EC_KEY *ec;
469  BIO *out;
470  int i = -1;
471  int private = 0;
472  char *password = NULL;
473  VALUE str;
474 
475  Require_EC_KEY(self, ec);
476 
477  if (EC_KEY_get0_public_key(ec) == NULL)
478  ossl_raise(eECError, "can't export - no public key set");
479 
480  if (EC_KEY_check_key(ec) != 1)
481  ossl_raise(eECError, "can't export - EC_KEY_check_key failed");
482 
483  if (EC_KEY_get0_private_key(ec))
484  private = 1;
485 
486  if (!(out = BIO_new(BIO_s_mem())))
487  ossl_raise(eECError, "BIO_new(BIO_s_mem())");
488 
489  switch(format) {
490  case EXPORT_PEM:
491  if (private) {
492  const EVP_CIPHER *cipher;
493  if (!NIL_P(ciph)) {
494  cipher = GetCipherPtr(ciph);
495  if (!NIL_P(pass)) {
496  password = StringValuePtr(pass);
497  }
498  }
499  else {
500  cipher = NULL;
501  }
502  i = PEM_write_bio_ECPrivateKey(out, ec, cipher, NULL, 0, NULL, password);
503  } else {
504  i = PEM_write_bio_EC_PUBKEY(out, ec);
505  }
506 
507  break;
508  case EXPORT_DER:
509  if (private) {
510  i = i2d_ECPrivateKey_bio(out, ec);
511  } else {
512  i = i2d_EC_PUBKEY_bio(out, ec);
513  }
514 
515  break;
516  default:
517  BIO_free(out);
518  ossl_raise(rb_eRuntimeError, "unknown format (internal error)");
519  }
520 
521  if (i != 1) {
522  BIO_free(out);
523  ossl_raise(eECError, "outlen=%d", i);
524  }
525 
526  str = ossl_membio2str(out);
527 
528  return str;
529 }
530 
531 /*
532  * call-seq:
533  * key.to_pem => String
534  * key.to_pem(cipher, pass_phrase) => String
535  *
536  * Outputs the EC key in PEM encoding. If +cipher+ and +pass_phrase+ are
537  * given they will be used to encrypt the key. +cipher+ must be an
538  * OpenSSL::Cipher::Cipher instance. Note that encryption will only be
539  * effective for a private key, public keys will always be encoded in plain
540  * text.
541  *
542  */
543 static VALUE ossl_ec_key_to_pem(int argc, VALUE *argv, VALUE self)
544 {
545  VALUE cipher, passwd;
546  rb_scan_args(argc, argv, "02", &cipher, &passwd);
547  return ossl_ec_key_to_string(self, cipher, passwd, EXPORT_PEM);
548 }
549 
550 /*
551  * call-seq:
552  * key.to_der => String
553  *
554  * See the OpenSSL documentation for i2d_ECPrivateKey_bio()
555  */
556 static VALUE ossl_ec_key_to_der(VALUE self)
557 {
558  return ossl_ec_key_to_string(self, Qnil, Qnil, EXPORT_DER);
559 }
560 
561 /*
562  * call-seq:
563  * key.to_text => String
564  *
565  * See the OpenSSL documentation for EC_KEY_print()
566  */
567 static VALUE ossl_ec_key_to_text(VALUE self)
568 {
569  EC_KEY *ec;
570  BIO *out;
571  VALUE str;
572 
573  Require_EC_KEY(self, ec);
574  if (!(out = BIO_new(BIO_s_mem()))) {
575  ossl_raise(eECError, "BIO_new(BIO_s_mem())");
576  }
577  if (!EC_KEY_print(out, ec, 0)) {
578  BIO_free(out);
579  ossl_raise(eECError, "EC_KEY_print");
580  }
581  str = ossl_membio2str(out);
582 
583  return str;
584 }
585 
586 /*
587  * call-seq:
588  * key.generate_key => self
589  *
590  * See the OpenSSL documentation for EC_KEY_generate_key()
591  */
592 static VALUE ossl_ec_key_generate_key(VALUE self)
593 {
594  EC_KEY *ec;
595 
596  Require_EC_KEY(self, ec);
597 
598  if (EC_KEY_generate_key(ec) != 1)
599  ossl_raise(eECError, "EC_KEY_generate_key");
600 
601  return self;
602 }
603 
604 /*
605  * call-seq:
606  * key.check_key => true
607  *
608  * Raises an exception if the key is invalid.
609  *
610  * See the OpenSSL documentation for EC_KEY_check_key()
611  */
612 static VALUE ossl_ec_key_check_key(VALUE self)
613 {
614  EC_KEY *ec;
615 
616  Require_EC_KEY(self, ec);
617 
618  if (EC_KEY_check_key(ec) != 1)
619  ossl_raise(eECError, "EC_KEY_check_key");
620 
621  return Qtrue;
622 }
623 
624 /*
625  * call-seq:
626  * key.dh_compute_key(pubkey) => String
627  *
628  * See the OpenSSL documentation for ECDH_compute_key()
629  */
630 static VALUE ossl_ec_key_dh_compute_key(VALUE self, VALUE pubkey)
631 {
632  EC_KEY *ec;
633  EC_POINT *point;
634  int buf_len;
635  VALUE str;
636 
637  Require_EC_KEY(self, ec);
638  SafeRequire_EC_POINT(pubkey, point);
639 
640 /* BUG: need a way to figure out the maximum string size */
641  buf_len = 1024;
642  str = rb_str_new(0, buf_len);
643 /* BUG: take KDF as a block */
644  buf_len = ECDH_compute_key(RSTRING_PTR(str), buf_len, point, ec, NULL);
645  if (buf_len < 0)
646  ossl_raise(eECError, "ECDH_compute_key");
647 
648  rb_str_resize(str, buf_len);
649 
650  return str;
651 }
652 
653 /* sign_setup */
654 
655 /*
656  * call-seq:
657  * key.dsa_sign_asn1(data) => String
658  *
659  * See the OpenSSL documentation for ECDSA_sign()
660  */
661 static VALUE ossl_ec_key_dsa_sign_asn1(VALUE self, VALUE data)
662 {
663  EC_KEY *ec;
664  unsigned int buf_len;
665  VALUE str;
666 
667  Require_EC_KEY(self, ec);
668  StringValue(data);
669 
670  if (EC_KEY_get0_private_key(ec) == NULL)
671  ossl_raise(eECError, "Private EC key needed!");
672 
673  str = rb_str_new(0, ECDSA_size(ec) + 16);
674  if (ECDSA_sign(0, (unsigned char *) RSTRING_PTR(data), RSTRING_LENINT(data), (unsigned char *) RSTRING_PTR(str), &buf_len, ec) != 1)
675  ossl_raise(eECError, "ECDSA_sign");
676 
677  rb_str_resize(str, buf_len);
678 
679  return str;
680 }
681 
682 /*
683  * call-seq:
684  * key.dsa_verify_asn1(data, sig) => true or false
685  *
686  * See the OpenSSL documentation for ECDSA_verify()
687  */
688 static VALUE ossl_ec_key_dsa_verify_asn1(VALUE self, VALUE data, VALUE sig)
689 {
690  EC_KEY *ec;
691 
692  Require_EC_KEY(self, ec);
693  StringValue(data);
694  StringValue(sig);
695 
696  switch (ECDSA_verify(0, (unsigned char *) RSTRING_PTR(data), RSTRING_LENINT(data), (unsigned char *) RSTRING_PTR(sig), (int)RSTRING_LEN(sig), ec)) {
697  case 1: return Qtrue;
698  case 0: return Qfalse;
699  default: break;
700  }
701 
702  ossl_raise(eECError, "ECDSA_verify");
703 }
704 
705 static void ossl_ec_group_free(ossl_ec_group *ec_group)
706 {
707  if (!ec_group->dont_free && ec_group->group)
708  EC_GROUP_clear_free(ec_group->group);
709  ruby_xfree(ec_group);
710 }
711 
712 static VALUE ossl_ec_group_alloc(VALUE klass)
713 {
714  ossl_ec_group *ec_group;
715  VALUE obj;
716 
717  obj = Data_Make_Struct(klass, ossl_ec_group, 0, ossl_ec_group_free, ec_group);
718 
719  return obj;
720 }
721 
722 /* call-seq:
723  * OpenSSL::PKey::EC::Group.new("secp112r1")
724  * OpenSSL::PKey::EC::Group.new(ec_group)
725  * OpenSSL::PKey::EC::Group.new(pem_string)
726  * OpenSSL::PKey::EC::Group.new(der_string)
727  * OpenSSL::PKey::EC::Group.new(pem_file)
728  * OpenSSL::PKey::EC::Group.new(der_file)
729  * OpenSSL::PKey::EC::Group.new(:GFp_simple)
730  * OpenSSL::PKey::EC::Group.new(:GFp_mult)
731  * OpenSSL::PKey::EC::Group.new(:GFp_nist)
732  * OpenSSL::PKey::EC::Group.new(:GF2m_simple)
733  * OpenSSL::PKey::EC::Group.new(:GFp, bignum_p, bignum_a, bignum_b)
734  * OpenSSL::PKey::EC::Group.new(:GF2m, bignum_p, bignum_a, bignum_b)
735  *
736  * See the OpenSSL documentation for EC_GROUP_*
737  */
738 static VALUE ossl_ec_group_initialize(int argc, VALUE *argv, VALUE self)
739 {
740  VALUE arg1, arg2, arg3, arg4;
741  ossl_ec_group *ec_group;
742  EC_GROUP *group = NULL;
743 
744  Data_Get_Struct(self, ossl_ec_group, ec_group);
745  if (ec_group->group != NULL)
746  ossl_raise(rb_eRuntimeError, "EC_GROUP is already initialized");
747 
748  switch (rb_scan_args(argc, argv, "13", &arg1, &arg2, &arg3, &arg4)) {
749  case 1:
750  if (SYMBOL_P(arg1)) {
751  const EC_METHOD *method = NULL;
752  ID id = SYM2ID(arg1);
753 
754  if (id == s_GFp_simple) {
755  method = EC_GFp_simple_method();
756  } else if (id == s_GFp_mont) {
757  method = EC_GFp_mont_method();
758  } else if (id == s_GFp_nist) {
759  method = EC_GFp_nist_method();
760 #if !defined(OPENSSL_NO_EC2M)
761  } else if (id == s_GF2m_simple) {
762  method = EC_GF2m_simple_method();
763 #endif
764  }
765 
766  if (method) {
767  if ((group = EC_GROUP_new(method)) == NULL)
768  ossl_raise(eEC_GROUP, "EC_GROUP_new");
769  } else {
770  ossl_raise(rb_eArgError, "unknown symbol, must be :GFp_simple, :GFp_mont, :GFp_nist or :GF2m_simple");
771  }
772  } else if (rb_obj_is_kind_of(arg1, cEC_GROUP)) {
773  const EC_GROUP *arg1_group;
774 
775  SafeRequire_EC_GROUP(arg1, arg1_group);
776  if ((group = EC_GROUP_dup(arg1_group)) == NULL)
777  ossl_raise(eEC_GROUP, "EC_GROUP_dup");
778  } else {
779  BIO *in = ossl_obj2bio(arg1);
780 
781  group = PEM_read_bio_ECPKParameters(in, NULL, NULL, NULL);
782  if (!group) {
783  OSSL_BIO_reset(in);
784  group = d2i_ECPKParameters_bio(in, NULL);
785  }
786 
787  BIO_free(in);
788 
789  if (!group) {
790  const char *name = StringValueCStr(arg1);
791  int nid = OBJ_sn2nid(name);
792 
793  (void)ERR_get_error();
794  if (nid == NID_undef)
795  ossl_raise(eEC_GROUP, "unknown curve name (%s)", name);
796 
797  group = EC_GROUP_new_by_curve_name(nid);
798  if (group == NULL)
799  ossl_raise(eEC_GROUP, "unable to create curve (%s)", name);
800 
801  EC_GROUP_set_asn1_flag(group, OPENSSL_EC_NAMED_CURVE);
802  EC_GROUP_set_point_conversion_form(group, POINT_CONVERSION_UNCOMPRESSED);
803  }
804  }
805 
806  break;
807  case 4:
808  if (SYMBOL_P(arg1)) {
809  ID id = SYM2ID(arg1);
810  EC_GROUP *(*new_curve)(const BIGNUM *, const BIGNUM *, const BIGNUM *, BN_CTX *) = NULL;
811  const BIGNUM *p = GetBNPtr(arg2);
812  const BIGNUM *a = GetBNPtr(arg3);
813  const BIGNUM *b = GetBNPtr(arg4);
814 
815  if (id == s_GFp) {
816  new_curve = EC_GROUP_new_curve_GFp;
817 #if !defined(OPENSSL_NO_EC2M)
818  } else if (id == s_GF2m) {
819  new_curve = EC_GROUP_new_curve_GF2m;
820 #endif
821  } else {
822  ossl_raise(rb_eArgError, "unknown symbol, must be :GFp or :GF2m");
823  }
824 
825  if ((group = new_curve(p, a, b, ossl_bn_ctx)) == NULL)
826  ossl_raise(eEC_GROUP, "EC_GROUP_new_by_GF*");
827  } else {
828  ossl_raise(rb_eArgError, "unknown argument, must be :GFp or :GF2m");
829  }
830 
831  break;
832  default:
833  ossl_raise(rb_eArgError, "wrong number of arguments");
834  }
835 
836  if (group == NULL)
837  ossl_raise(eEC_GROUP, "");
838 
839  ec_group->group = group;
840 
841  return self;
842 }
843 
844 /* call-seq:
845  * group1 == group2 => true | false
846  *
847  */
848 static VALUE ossl_ec_group_eql(VALUE a, VALUE b)
849 {
850  EC_GROUP *group1 = NULL, *group2 = NULL;
851 
852  Require_EC_GROUP(a, group1);
853  SafeRequire_EC_GROUP(b, group2);
854 
855  if (EC_GROUP_cmp(group1, group2, ossl_bn_ctx) == 1)
856  return Qfalse;
857 
858  return Qtrue;
859 }
860 
861 /* call-seq:
862  * group.generator => ec_point
863  *
864  * See the OpenSSL documentation for EC_GROUP_get0_generator()
865  */
866 static VALUE ossl_ec_group_get_generator(VALUE self)
867 {
868  VALUE point_obj;
869  EC_GROUP *group = NULL;
870 
871  Require_EC_GROUP(self, group);
872 
873  point_obj = ossl_ec_point_dup(EC_GROUP_get0_generator(group), self);
874 
875  return point_obj;
876 }
877 
878 /* call-seq:
879  * group.set_generator(generator, order, cofactor) => self
880  *
881  * See the OpenSSL documentation for EC_GROUP_set_generator()
882  */
883 static VALUE ossl_ec_group_set_generator(VALUE self, VALUE generator, VALUE order, VALUE cofactor)
884 {
885  EC_GROUP *group = NULL;
886  const EC_POINT *point;
887  const BIGNUM *o, *co;
888 
889  Require_EC_GROUP(self, group);
890  SafeRequire_EC_POINT(generator, point);
891  o = GetBNPtr(order);
892  co = GetBNPtr(cofactor);
893 
894  if (EC_GROUP_set_generator(group, point, o, co) != 1)
895  ossl_raise(eEC_GROUP, "EC_GROUP_set_generator");
896 
897  return self;
898 }
899 
900 /* call-seq:
901  * group.get_order => order_bn
902  *
903  * See the OpenSSL documentation for EC_GROUP_get_order()
904  */
905 static VALUE ossl_ec_group_get_order(VALUE self)
906 {
907  VALUE bn_obj;
908  BIGNUM *bn;
909  EC_GROUP *group = NULL;
910 
911  Require_EC_GROUP(self, group);
912 
913  bn_obj = ossl_bn_new(NULL);
914  bn = GetBNPtr(bn_obj);
915 
916  if (EC_GROUP_get_order(group, bn, ossl_bn_ctx) != 1)
917  ossl_raise(eEC_GROUP, "EC_GROUP_get_order");
918 
919  return bn_obj;
920 }
921 
922 /* call-seq:
923  * group.get_cofactor => cofactor_bn
924  *
925  * See the OpenSSL documentation for EC_GROUP_get_cofactor()
926  */
927 static VALUE ossl_ec_group_get_cofactor(VALUE self)
928 {
929  VALUE bn_obj;
930  BIGNUM *bn;
931  EC_GROUP *group = NULL;
932 
933  Require_EC_GROUP(self, group);
934 
935  bn_obj = ossl_bn_new(NULL);
936  bn = GetBNPtr(bn_obj);
937 
938  if (EC_GROUP_get_cofactor(group, bn, ossl_bn_ctx) != 1)
939  ossl_raise(eEC_GROUP, "EC_GROUP_get_cofactor");
940 
941  return bn_obj;
942 }
943 
944 /* call-seq:
945  * group.curve_name => String
946  *
947  * See the OpenSSL documentation for EC_GROUP_get_curve_name()
948  */
949 static VALUE ossl_ec_group_get_curve_name(VALUE self)
950 {
951  EC_GROUP *group = NULL;
952  int nid;
953 
954  Get_EC_GROUP(self, group);
955  if (group == NULL)
956  return Qnil;
957 
958  nid = EC_GROUP_get_curve_name(group);
959 
960 /* BUG: an nid or asn1 object should be returned, maybe. */
961  return rb_str_new2(OBJ_nid2sn(nid));
962 }
963 
964 /* call-seq:
965  * EC.builtin_curves => [[name, comment], ...]
966  *
967  * See the OpenSSL documentation for EC_builtin_curves()
968  */
969 static VALUE ossl_s_builtin_curves(VALUE self)
970 {
971  EC_builtin_curve *curves = NULL;
972  int n;
973  int crv_len = rb_long2int(EC_get_builtin_curves(NULL, 0));
974  VALUE ary, ret;
975 
976  curves = ALLOCA_N(EC_builtin_curve, crv_len);
977  if (curves == NULL)
978  return Qnil;
979  if (!EC_get_builtin_curves(curves, crv_len))
980  ossl_raise(rb_eRuntimeError, "EC_get_builtin_curves");
981 
982  ret = rb_ary_new2(crv_len);
983 
984  for (n = 0; n < crv_len; n++) {
985  const char *sname = OBJ_nid2sn(curves[n].nid);
986  const char *comment = curves[n].comment;
987 
988  ary = rb_ary_new2(2);
989  rb_ary_push(ary, rb_str_new2(sname));
990  rb_ary_push(ary, comment ? rb_str_new2(comment) : Qnil);
991  rb_ary_push(ret, ary);
992  }
993 
994  return ret;
995 }
996 
997 /* call-seq:
998  * group.asn1_flag => Fixnum
999  *
1000  * See the OpenSSL documentation for EC_GROUP_get_asn1_flag()
1001  */
1002 static VALUE ossl_ec_group_get_asn1_flag(VALUE self)
1003 {
1004  EC_GROUP *group = NULL;
1005  int flag;
1006 
1007  Require_EC_GROUP(self, group);
1008 
1009  flag = EC_GROUP_get_asn1_flag(group);
1010 
1011  return INT2FIX(flag);
1012 }
1013 
1014 /* call-seq:
1015  * group.asn1_flag = Fixnum => Fixnum
1016  *
1017  * See the OpenSSL documentation for EC_GROUP_set_asn1_flag()
1018  */
1019 static VALUE ossl_ec_group_set_asn1_flag(VALUE self, VALUE flag_v)
1020 {
1021  EC_GROUP *group = NULL;
1022 
1023  Require_EC_GROUP(self, group);
1024 
1025  EC_GROUP_set_asn1_flag(group, NUM2INT(flag_v));
1026 
1027  return flag_v;
1028 }
1029 
1030 /* call-seq:
1031  * group.point_conversion_form => :uncompressed | :compressed | :hybrid
1032  *
1033  * See the OpenSSL documentation for EC_GROUP_get_point_conversion_form()
1034  */
1035 static VALUE ossl_ec_group_get_point_conversion_form(VALUE self)
1036 {
1037  EC_GROUP *group = NULL;
1038  point_conversion_form_t form;
1039  VALUE ret;
1040 
1041  Require_EC_GROUP(self, group);
1042 
1043  form = EC_GROUP_get_point_conversion_form(group);
1044 
1045  switch (form) {
1046  case POINT_CONVERSION_UNCOMPRESSED: ret = ID_uncompressed; break;
1047  case POINT_CONVERSION_COMPRESSED: ret = ID_compressed; break;
1048  case POINT_CONVERSION_HYBRID: ret = ID_hybrid; break;
1049  default: ossl_raise(eEC_GROUP, "unsupported point conversion form: %d, this module should be updated", form);
1050  }
1051 
1052  return ID2SYM(ret);
1053 }
1054 
1055 /* call-seq:
1056  * group.point_conversion_form = form => form
1057  *
1058  * See the OpenSSL documentation for EC_GROUP_set_point_conversion_form()
1059  */
1060 static VALUE ossl_ec_group_set_point_conversion_form(VALUE self, VALUE form_v)
1061 {
1062  EC_GROUP *group = NULL;
1063  point_conversion_form_t form;
1064  ID form_id = SYM2ID(form_v);
1065 
1066  Require_EC_GROUP(self, group);
1067 
1068  if (form_id == ID_uncompressed) {
1069  form = POINT_CONVERSION_UNCOMPRESSED;
1070  } else if (form_id == ID_compressed) {
1071  form = POINT_CONVERSION_COMPRESSED;
1072  } else if (form_id == ID_hybrid) {
1073  form = POINT_CONVERSION_HYBRID;
1074  } else {
1075  ossl_raise(rb_eArgError, "form must be :compressed, :uncompressed, or :hybrid");
1076  }
1077 
1078  EC_GROUP_set_point_conversion_form(group, form);
1079 
1080  return form_v;
1081 }
1082 
1083 /* call-seq:
1084  * group.seed => String or nil
1085  *
1086  * See the OpenSSL documentation for EC_GROUP_get0_seed()
1087  */
1088 static VALUE ossl_ec_group_get_seed(VALUE self)
1089 {
1090  EC_GROUP *group = NULL;
1091  size_t seed_len;
1092 
1093  Require_EC_GROUP(self, group);
1094 
1095  seed_len = EC_GROUP_get_seed_len(group);
1096 
1097  if (seed_len == 0)
1098  return Qnil;
1099 
1100  return rb_str_new((const char *)EC_GROUP_get0_seed(group), seed_len);
1101 }
1102 
1103 /* call-seq:
1104  * group.seed = seed => seed
1105  *
1106  * See the OpenSSL documentation for EC_GROUP_set_seed()
1107  */
1108 static VALUE ossl_ec_group_set_seed(VALUE self, VALUE seed)
1109 {
1110  EC_GROUP *group = NULL;
1111 
1112  Require_EC_GROUP(self, group);
1113  StringValue(seed);
1114 
1115  if (EC_GROUP_set_seed(group, (unsigned char *)RSTRING_PTR(seed), RSTRING_LEN(seed)) != (size_t)RSTRING_LEN(seed))
1116  ossl_raise(eEC_GROUP, "EC_GROUP_set_seed");
1117 
1118  return seed;
1119 }
1120 
1121 /* get/set curve GFp, GF2m */
1122 
1123 /* call-seq:
1124  * group.degree => Fixnum
1125  *
1126  * See the OpenSSL documentation for EC_GROUP_get_degree()
1127  */
1128 static VALUE ossl_ec_group_get_degree(VALUE self)
1129 {
1130  EC_GROUP *group = NULL;
1131 
1132  Require_EC_GROUP(self, group);
1133 
1134  return INT2NUM(EC_GROUP_get_degree(group));
1135 }
1136 
1137 static VALUE ossl_ec_group_to_string(VALUE self, int format)
1138 {
1139  EC_GROUP *group;
1140  BIO *out;
1141  int i = -1;
1142  VALUE str;
1143 
1144  Get_EC_GROUP(self, group);
1145 
1146  if (!(out = BIO_new(BIO_s_mem())))
1147  ossl_raise(eEC_GROUP, "BIO_new(BIO_s_mem())");
1148 
1149  switch(format) {
1150  case EXPORT_PEM:
1151  i = PEM_write_bio_ECPKParameters(out, group);
1152  break;
1153  case EXPORT_DER:
1154  i = i2d_ECPKParameters_bio(out, group);
1155  break;
1156  default:
1157  BIO_free(out);
1158  ossl_raise(rb_eRuntimeError, "unknown format (internal error)");
1159  }
1160 
1161  if (i != 1) {
1162  BIO_free(out);
1164  }
1165 
1166  str = ossl_membio2str(out);
1167 
1168  return str;
1169 }
1170 
1171 /* call-seq:
1172  * group.to_pem => String
1173  *
1174  * See the OpenSSL documentation for PEM_write_bio_ECPKParameters()
1175  */
1176 static VALUE ossl_ec_group_to_pem(VALUE self)
1177 {
1178  return ossl_ec_group_to_string(self, EXPORT_PEM);
1179 }
1180 
1181 /* call-seq:
1182  * group.to_der => String
1183  *
1184  * See the OpenSSL documentation for i2d_ECPKParameters_bio()
1185  */
1186 static VALUE ossl_ec_group_to_der(VALUE self)
1187 {
1188  return ossl_ec_group_to_string(self, EXPORT_DER);
1189 }
1190 
1191 /* call-seq:
1192  * group.to_text => String
1193  *
1194  * See the OpenSSL documentation for ECPKParameters_print()
1195  */
1196 static VALUE ossl_ec_group_to_text(VALUE self)
1197 {
1198  EC_GROUP *group;
1199  BIO *out;
1200  VALUE str;
1201 
1202  Require_EC_GROUP(self, group);
1203  if (!(out = BIO_new(BIO_s_mem()))) {
1204  ossl_raise(eEC_GROUP, "BIO_new(BIO_s_mem())");
1205  }
1206  if (!ECPKParameters_print(out, group, 0)) {
1207  BIO_free(out);
1209  }
1210  str = ossl_membio2str(out);
1211 
1212  return str;
1213 }
1214 
1215 
1216 static void ossl_ec_point_free(ossl_ec_point *ec_point)
1217 {
1218  if (!ec_point->dont_free && ec_point->point)
1219  EC_POINT_clear_free(ec_point->point);
1220  ruby_xfree(ec_point);
1221 }
1222 
1223 static VALUE ossl_ec_point_alloc(VALUE klass)
1224 {
1225  ossl_ec_point *ec_point;
1226  VALUE obj;
1227 
1228  obj = Data_Make_Struct(klass, ossl_ec_point, 0, ossl_ec_point_free, ec_point);
1229 
1230  return obj;
1231 }
1232 
1233 /*
1234  * call-seq:
1235  * OpenSSL::PKey::EC::Point.new(point)
1236  * OpenSSL::PKey::EC::Point.new(group)
1237  * OpenSSL::PKey::EC::Point.new(group, bn)
1238  *
1239  * See the OpenSSL documentation for EC_POINT_*
1240  */
1241 static VALUE ossl_ec_point_initialize(int argc, VALUE *argv, VALUE self)
1242 {
1243  ossl_ec_point *ec_point;
1244  EC_POINT *point = NULL;
1245  VALUE arg1, arg2;
1246  VALUE group_v = Qnil;
1247  const EC_GROUP *group = NULL;
1248 
1249  Data_Get_Struct(self, ossl_ec_point, ec_point);
1250  if (ec_point->point)
1251  ossl_raise(eEC_POINT, "EC_POINT already initialized");
1252 
1253  switch (rb_scan_args(argc, argv, "11", &arg1, &arg2)) {
1254  case 1:
1255  if (rb_obj_is_kind_of(arg1, cEC_POINT)) {
1256  const EC_POINT *arg_point;
1257 
1258  group_v = rb_iv_get(arg1, "@group");
1259  SafeRequire_EC_GROUP(group_v, group);
1260  SafeRequire_EC_POINT(arg1, arg_point);
1261 
1262  point = EC_POINT_dup(arg_point, group);
1263  } else if (rb_obj_is_kind_of(arg1, cEC_GROUP)) {
1264  group_v = arg1;
1265  SafeRequire_EC_GROUP(group_v, group);
1266 
1267  point = EC_POINT_new(group);
1268  } else {
1269  ossl_raise(eEC_POINT, "wrong argument type: must be OpenSSL::PKey::EC::Point or OpenSSL::Pkey::EC::Group");
1270  }
1271 
1272  break;
1273  case 2:
1274  if (!rb_obj_is_kind_of(arg1, cEC_GROUP))
1275  ossl_raise(rb_eArgError, "1st argument must be OpenSSL::PKey::EC::Group");
1276  group_v = arg1;
1277  SafeRequire_EC_GROUP(group_v, group);
1278 
1279  if (rb_obj_is_kind_of(arg2, cBN)) {
1280  const BIGNUM *bn = GetBNPtr(arg2);
1281 
1282  point = EC_POINT_bn2point(group, bn, NULL, ossl_bn_ctx);
1283  } else {
1284  BIO *in = ossl_obj2bio(arg1);
1285 
1286 /* BUG: finish me */
1287 
1288  BIO_free(in);
1289 
1290  if (point == NULL) {
1291  ossl_raise(eEC_POINT, "unknown type for 2nd arg");
1292  }
1293  }
1294  break;
1295  default:
1296  ossl_raise(rb_eArgError, "wrong number of arguments");
1297  }
1298 
1299  if (point == NULL)
1301 
1302  if (NIL_P(group_v))
1303  ossl_raise(rb_eRuntimeError, "missing group (internal error)");
1304 
1305  ec_point->point = point;
1306 
1307  rb_iv_set(self, "@group", group_v);
1308 
1309  return self;
1310 }
1311 
1312 /*
1313  * call-seq:
1314  * point1 == point2 => true | false
1315  *
1316  */
1317 static VALUE ossl_ec_point_eql(VALUE a, VALUE b)
1318 {
1319  EC_POINT *point1, *point2;
1320  VALUE group_v1 = rb_iv_get(a, "@group");
1321  VALUE group_v2 = rb_iv_get(b, "@group");
1322  const EC_GROUP *group;
1323 
1324  if (ossl_ec_group_eql(group_v1, group_v2) == Qfalse)
1325  return Qfalse;
1326 
1327  Require_EC_POINT(a, point1);
1328  SafeRequire_EC_POINT(b, point2);
1329  SafeRequire_EC_GROUP(group_v1, group);
1330 
1331  if (EC_POINT_cmp(group, point1, point2, ossl_bn_ctx) == 1)
1332  return Qfalse;
1333 
1334  return Qtrue;
1335 }
1336 
1337 /*
1338  * call-seq:
1339  * point.infinity? => true | false
1340  *
1341  */
1342 static VALUE ossl_ec_point_is_at_infinity(VALUE self)
1343 {
1344  EC_POINT *point;
1345  VALUE group_v = rb_iv_get(self, "@group");
1346  const EC_GROUP *group;
1347 
1348  Require_EC_POINT(self, point);
1349  SafeRequire_EC_GROUP(group_v, group);
1350 
1351  switch (EC_POINT_is_at_infinity(group, point)) {
1352  case 1: return Qtrue;
1353  case 0: return Qfalse;
1354  default: ossl_raise(cEC_POINT, "EC_POINT_is_at_infinity");
1355  }
1356 }
1357 
1358 /*
1359  * call-seq:
1360  * point.on_curve? => true | false
1361  *
1362  */
1363 static VALUE ossl_ec_point_is_on_curve(VALUE self)
1364 {
1365  EC_POINT *point;
1366  VALUE group_v = rb_iv_get(self, "@group");
1367  const EC_GROUP *group;
1368 
1369  Require_EC_POINT(self, point);
1370  SafeRequire_EC_GROUP(group_v, group);
1371 
1372  switch (EC_POINT_is_on_curve(group, point, ossl_bn_ctx)) {
1373  case 1: return Qtrue;
1374  case 0: return Qfalse;
1375  default: ossl_raise(cEC_POINT, "EC_POINT_is_on_curve");
1376  }
1377 }
1378 
1379 /*
1380  * call-seq:
1381  * point.make_affine! => self
1382  *
1383  */
1384 static VALUE ossl_ec_point_make_affine(VALUE self)
1385 {
1386  EC_POINT *point;
1387  VALUE group_v = rb_iv_get(self, "@group");
1388  const EC_GROUP *group;
1389 
1390  Require_EC_POINT(self, point);
1391  SafeRequire_EC_GROUP(group_v, group);
1392 
1393  if (EC_POINT_make_affine(group, point, ossl_bn_ctx) != 1)
1394  ossl_raise(cEC_POINT, "EC_POINT_make_affine");
1395 
1396  return self;
1397 }
1398 
1399 /*
1400  * call-seq:
1401  * point.invert! => self
1402  *
1403  */
1404 static VALUE ossl_ec_point_invert(VALUE self)
1405 {
1406  EC_POINT *point;
1407  VALUE group_v = rb_iv_get(self, "@group");
1408  const EC_GROUP *group;
1409 
1410  Require_EC_POINT(self, point);
1411  SafeRequire_EC_GROUP(group_v, group);
1412 
1413  if (EC_POINT_invert(group, point, ossl_bn_ctx) != 1)
1414  ossl_raise(cEC_POINT, "EC_POINT_invert");
1415 
1416  return self;
1417 }
1418 
1419 /*
1420  * call-seq:
1421  * point.set_to_infinity! => self
1422  *
1423  */
1424 static VALUE ossl_ec_point_set_to_infinity(VALUE self)
1425 {
1426  EC_POINT *point;
1427  VALUE group_v = rb_iv_get(self, "@group");
1428  const EC_GROUP *group;
1429 
1430  Require_EC_POINT(self, point);
1431  SafeRequire_EC_GROUP(group_v, group);
1432 
1433  if (EC_POINT_set_to_infinity(group, point) != 1)
1434  ossl_raise(cEC_POINT, "EC_POINT_set_to_infinity");
1435 
1436  return self;
1437 }
1438 
1439 /*
1440  * call-seq:
1441  * point.to_bn => OpenSSL::BN
1442  *
1443  * See the OpenSSL documentation for EC_POINT_point2bn()
1444  */
1445 static VALUE ossl_ec_point_to_bn(VALUE self)
1446 {
1447  EC_POINT *point;
1448  VALUE bn_obj;
1449  VALUE group_v = rb_iv_get(self, "@group");
1450  const EC_GROUP *group;
1451  point_conversion_form_t form;
1452  BIGNUM *bn;
1453 
1454  Require_EC_POINT(self, point);
1455  SafeRequire_EC_GROUP(group_v, group);
1456 
1457  form = EC_GROUP_get_point_conversion_form(group);
1458 
1459  bn_obj = rb_obj_alloc(cBN);
1460  bn = GetBNPtr(bn_obj);
1461 
1462  if (EC_POINT_point2bn(group, point, form, bn, ossl_bn_ctx) == NULL)
1463  ossl_raise(eEC_POINT, "EC_POINT_point2bn");
1464 
1465  return bn_obj;
1466 }
1467 
1468 static void no_copy(VALUE klass)
1469 {
1470  rb_undef_method(klass, "copy");
1471  rb_undef_method(klass, "clone");
1472  rb_undef_method(klass, "dup");
1473  rb_undef_method(klass, "initialize_copy");
1474 }
1475 
1476 void Init_ossl_ec()
1477 {
1478 #ifdef DONT_NEED_RDOC_WORKAROUND
1479  mOSSL = rb_define_module("OpenSSL");
1480  mPKey = rb_define_module_under(mOSSL, "PKey");
1481 #endif
1482 
1484 
1490 
1491  s_GFp = rb_intern("GFp");
1492  s_GF2m = rb_intern("GF2m");
1493  s_GFp_simple = rb_intern("GFp_simple");
1494  s_GFp_mont = rb_intern("GFp_mont");
1495  s_GFp_nist = rb_intern("GFp_nist");
1496  s_GF2m_simple = rb_intern("GF2m_simple");
1497 
1498  ID_uncompressed = rb_intern("uncompressed");
1499  ID_compressed = rb_intern("compressed");
1500  ID_hybrid = rb_intern("hybrid");
1501 
1502 #ifdef OPENSSL_EC_NAMED_CURVE
1503  rb_define_const(cEC, "NAMED_CURVE", ULONG2NUM(OPENSSL_EC_NAMED_CURVE));
1504 #endif
1505 
1506  rb_define_singleton_method(cEC, "builtin_curves", ossl_s_builtin_curves, 0);
1507 
1508  rb_define_method(cEC, "initialize", ossl_ec_key_initialize, -1);
1509 /* copy/dup/cmp */
1510 
1511  rb_define_method(cEC, "group", ossl_ec_key_get_group, 0);
1512  rb_define_method(cEC, "group=", ossl_ec_key_set_group, 1);
1513  rb_define_method(cEC, "private_key", ossl_ec_key_get_private_key, 0);
1514  rb_define_method(cEC, "private_key=", ossl_ec_key_set_private_key, 1);
1515  rb_define_method(cEC, "public_key", ossl_ec_key_get_public_key, 0);
1516  rb_define_method(cEC, "public_key=", ossl_ec_key_set_public_key, 1);
1517  rb_define_method(cEC, "private_key?", ossl_ec_key_is_private_key, 0);
1518  rb_define_method(cEC, "public_key?", ossl_ec_key_is_public_key, 0);
1519 /* rb_define_method(cEC, "", ossl_ec_key_get_, 0);
1520  rb_define_method(cEC, "=", ossl_ec_key_set_ 1);
1521  set/get enc_flags
1522  set/get _conv_from
1523  set/get asn1_flag (can use ruby to call self.group.asn1_flag)
1524  set/get precompute_mult
1525 */
1526  rb_define_method(cEC, "generate_key", ossl_ec_key_generate_key, 0);
1527  rb_define_method(cEC, "check_key", ossl_ec_key_check_key, 0);
1528 
1529  rb_define_method(cEC, "dh_compute_key", ossl_ec_key_dh_compute_key, 1);
1530  rb_define_method(cEC, "dsa_sign_asn1", ossl_ec_key_dsa_sign_asn1, 1);
1531  rb_define_method(cEC, "dsa_verify_asn1", ossl_ec_key_dsa_verify_asn1, 2);
1532 /* do_sign/do_verify */
1533 
1534  rb_define_method(cEC, "to_pem", ossl_ec_key_to_pem, -1);
1535  rb_define_method(cEC, "to_der", ossl_ec_key_to_der, 0);
1536  rb_define_method(cEC, "to_text", ossl_ec_key_to_text, 0);
1537 
1538 
1539  rb_define_alloc_func(cEC_GROUP, ossl_ec_group_alloc);
1540  rb_define_method(cEC_GROUP, "initialize", ossl_ec_group_initialize, -1);
1541  rb_define_method(cEC_GROUP, "eql?", ossl_ec_group_eql, 1);
1542  rb_define_alias(cEC_GROUP, "==", "eql?");
1543 /* copy/dup/cmp */
1544 
1545  rb_define_method(cEC_GROUP, "generator", ossl_ec_group_get_generator, 0);
1546  rb_define_method(cEC_GROUP, "set_generator", ossl_ec_group_set_generator, 3);
1547  rb_define_method(cEC_GROUP, "order", ossl_ec_group_get_order, 0);
1548  rb_define_method(cEC_GROUP, "cofactor", ossl_ec_group_get_cofactor, 0);
1549 
1550  rb_define_method(cEC_GROUP, "curve_name", ossl_ec_group_get_curve_name, 0);
1551 /* rb_define_method(cEC_GROUP, "curve_name=", ossl_ec_group_set_curve_name, 1); */
1552 
1553  rb_define_method(cEC_GROUP, "asn1_flag", ossl_ec_group_get_asn1_flag, 0);
1554  rb_define_method(cEC_GROUP, "asn1_flag=", ossl_ec_group_set_asn1_flag, 1);
1555 
1556  rb_define_method(cEC_GROUP, "point_conversion_form", ossl_ec_group_get_point_conversion_form, 0);
1557  rb_define_method(cEC_GROUP, "point_conversion_form=", ossl_ec_group_set_point_conversion_form, 1);
1558 
1559  rb_define_method(cEC_GROUP, "seed", ossl_ec_group_get_seed, 0);
1560  rb_define_method(cEC_GROUP, "seed=", ossl_ec_group_set_seed, 1);
1561 
1562 /* get/set GFp, GF2m */
1563 
1564  rb_define_method(cEC_GROUP, "degree", ossl_ec_group_get_degree, 0);
1565 
1566 /* check* */
1567 
1568 
1569  rb_define_method(cEC_GROUP, "to_pem", ossl_ec_group_to_pem, 0);
1570  rb_define_method(cEC_GROUP, "to_der", ossl_ec_group_to_der, 0);
1571  rb_define_method(cEC_GROUP, "to_text", ossl_ec_group_to_text, 0);
1572 
1573 
1574  rb_define_alloc_func(cEC_POINT, ossl_ec_point_alloc);
1575  rb_define_method(cEC_POINT, "initialize", ossl_ec_point_initialize, -1);
1576  rb_attr(cEC_POINT, rb_intern("group"), 1, 0, 0);
1577  rb_define_method(cEC_POINT, "eql?", ossl_ec_point_eql, 1);
1578  rb_define_alias(cEC_POINT, "==", "eql?");
1579 
1580  rb_define_method(cEC_POINT, "infinity?", ossl_ec_point_is_at_infinity, 0);
1581  rb_define_method(cEC_POINT, "on_curve?", ossl_ec_point_is_on_curve, 0);
1582  rb_define_method(cEC_POINT, "make_affine!", ossl_ec_point_make_affine, 0);
1583  rb_define_method(cEC_POINT, "invert!", ossl_ec_point_invert, 0);
1584  rb_define_method(cEC_POINT, "set_to_infinity!", ossl_ec_point_set_to_infinity, 0);
1585 /* all the other methods */
1586 
1587  rb_define_method(cEC_POINT, "to_bn", ossl_ec_point_to_bn, 0);
1588 
1589  no_copy(cEC);
1590  no_copy(cEC_GROUP);
1591  no_copy(cEC_POINT);
1592 }
1593 
1594 #else /* defined NO_EC */
1596 {
1597 }
1598 #endif /* NO_EC */
#define RSTRING_LEN(string)
Definition: generator.h:45
VALUE mOSSL
Definition: ossl.c:250
ssize_t n
Definition: bigdecimal.c:5519
volatile VALUE ary
Definition: tcltklib.c:9700
gz ec
Definition: zlib.c:2036
#define NUM2INT(x)
Definition: ruby.h:536
VALUE eEC_GROUP
#define Data_Get_Struct(obj, type, sval)
Definition: ruby.h:835
void rb_define_singleton_method(VALUE obj, const char *name, VALUE(*func)(ANYARGS), int argc)
Defines a singleton method for obj.
Definition: class.c:1343
VALUE mPKey
Definition: ossl_pkey.c:16
C_block * out
Definition: crypt.c:308
VALUE ePKeyError
Definition: ossl_pkey.c:18
int ret
Definition: tcltklib.c:276
SYMID SyckParser * p
Definition: yaml2byte.c:119
VALUE rb_eTypeError
Definition: error.c:467
unsigned long VALUE
Definition: ruby.h:88
VALUE rb_ary_push(VALUE ary, VALUE item)
Definition: array.c:740
#define rb_long2int(n)
Definition: ruby.h:308
static VALUE INT2NUM(int v)
Definition: ruby.h:981
int const char * in
Definition: crypt.c:639
#define RSTRING_PTR(string)
Definition: generator.h:42
VALUE rb_define_class_under(VALUE outer, const char *name, VALUE super)
Defines a class under the namespace of outer.
Definition: class.c:515
return Qtrue
Definition: tcltklib.c:9597
void rb_define_alloc_func(VALUE, rb_alloc_func_t)
VALUE rb_obj_is_kind_of(VALUE, VALUE)
Definition: object.c:525
VALUE eEC_POINT
#define GetPKey(obj, pkey)
Definition: ossl_pkey.h:30
return str
Definition: ruby.c:1183
VALUE ossl_membio2str(BIO *bio)
Definition: ossl_bio.c:77
void rb_undef_method(VALUE klass, const char *name)
Definition: class.c:1228
#define ID2SYM(i)
Definition: cparse.c:63
flag
Definition: tcltklib.c:2039
Real * b
Definition: bigdecimal.c:1140
#define Qnil
Definition: ruby.h:367
n NULL
Definition: yaml2byte.c:134
void * data
Definition: yaml2byte.c:131
return Qfalse
Definition: tcltklib.c:6768
RUBY_EXTERN VALUE rb_cObject
Definition: ruby.h:1246
VALUE rb_eRuntimeError
Definition: error.c:466
#define SYM2ID(v)
Definition: cparse.c:66
gz comment
Definition: zlib.c:2029
void rb_attr(VALUE, ID, int, int, int)
Definition: vm_method.c:558
#define OSSL_BIO_reset(bio)
Definition: ossl.h:149
VALUE rb_iv_get(VALUE, const char *)
Definition: variable.c:2220
#define NIL_P(v)
Definition: ruby.h:374
static VALUE VALUE obj
Definition: tcltklib.c:3147
void rb_define_const(VALUE, const char *, VALUE)
Definition: variable.c:1923
VALUE eOSSLError
Definition: ossl.c:255
volatile ID method
Definition: tcltklib.c:3588
#define ALLOCA_N(type, n)
Definition: ruby.h:1038
VALUE rb_obj_alloc(VALUE)
Definition: object.c:1601
const EVP_CIPHER * GetCipherPtr(VALUE obj)
Definition: ossl_cipher.c:45
VALUE rb_str_resize(VALUE, long)
Definition: string.c:1779
VALUE * argv
Definition: tcltklib.c:1962
void rb_define_alias(VALUE klass, const char *name1, const char *name2)
Defines an alias of a method.
Definition: class.c:1385
#define WrapPKey(klass, obj, pkey)
Definition: ossl_pkey.h:23
BIO * ossl_obj2bio(VALUE obj)
Definition: ossl_bio.c:17
VALUE rb_iv_set(VALUE, const char *, VALUE)
Definition: variable.c:2228
void ruby_xfree(void *x)
Definition: gc.c:916
VP_EXPORT void
Definition: bigdecimal.c:4944
int rb_scan_args(int argc, const VALUE *argv, const char *fmt,...)
Definition: class.c:1416
void Init_ossl_ec(void)
VALUE cEC_GROUP
unsigned long ID
Definition: ruby.h:89
int argc
Definition: tcltklib.c:1961
VALUE cBN
Definition: ossl_bn.c:36
#define Data_Make_Struct(klass, type, mark, free, sval)
Definition: ruby.h:820
VALUE rb_define_module_under(VALUE outer, const char *name)
Definition: class.c:607
#define StringValueCStr(v)
Definition: ruby.h:468
VALUE cEC_POINT
#define SYMBOL_P(v)
Definition: cparse.c:69
arg
Definition: ripper.y:1287
#define INT2FIX(i)
Definition: ruby.h:225
static VALUE ULONG2NUM(unsigned long v)
Definition: ruby.h:1015
VALUE eECError
BN_CTX * ossl_bn_ctx
Definition: ossl_bn.c:89
klass
Definition: tcltklib.c:3493
void ossl_raise(VALUE exc, const char *fmt,...)
Definition: ossl.c:324
VALUE rb_ary_new2(long capa)
Definition: array.c:332
VALUE ossl_bn_new(const BIGNUM *bn)
Definition: ossl_bn.c:43
return rb_funcall(q->proc, ID_call, 0)
#define StringValuePtr(v)
Definition: ruby.h:467
int ossl_pem_passwd_cb(char *buf, int max_len, int flag, void *pwd)
Definition: ossl.c:162
#define RSTRING_LENINT(str)
Definition: ruby.h:684
BIGNUM * GetBNPtr(VALUE obj)
Definition: ossl_bn.c:58
ssize_t i
Definition: bigdecimal.c:5519
VALUE rb_define_module(const char *name)
Definition: class.c:587
VALUE cEC
VALUE cPKey
Definition: ossl_pkey.c:17
#define rb_intern(str)
const char * name
Definition: nkf.c:208
Real * a
Definition: bigdecimal.c:1140
void rb_define_method(VALUE klass, const char *name, VALUE(*func)(ANYARGS), int argc)
Definition: class.c:1210
VALUE rb_str_new2(const char *)
VALUE ossl_ec_new(EVP_PKEY *)
VALUE rb_eArgError
Definition: error.c:468
#define StringValue(v)
Definition: ruby.h:466
VALUE rb_str_new(const char *, long)
Definition: string.c:410