Ruby  1.9.3p448(2013-06-27revision41675)
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  } else if (id == s_GF2m_simple) {
761  method = EC_GF2m_simple_method();
762  }
763 
764  if (method) {
765  if ((group = EC_GROUP_new(method)) == NULL)
766  ossl_raise(eEC_GROUP, "EC_GROUP_new");
767  } else {
768  ossl_raise(rb_eArgError, "unknown symbol, must be :GFp_simple, :GFp_mont, :GFp_nist or :GF2m_simple");
769  }
770  } else if (rb_obj_is_kind_of(arg1, cEC_GROUP)) {
771  const EC_GROUP *arg1_group;
772 
773  SafeRequire_EC_GROUP(arg1, arg1_group);
774  if ((group = EC_GROUP_dup(arg1_group)) == NULL)
775  ossl_raise(eEC_GROUP, "EC_GROUP_dup");
776  } else {
777  BIO *in = ossl_obj2bio(arg1);
778 
779  group = PEM_read_bio_ECPKParameters(in, NULL, NULL, NULL);
780  if (!group) {
781  OSSL_BIO_reset(in);
782  group = d2i_ECPKParameters_bio(in, NULL);
783  }
784 
785  BIO_free(in);
786 
787  if (!group) {
788  const char *name = StringValueCStr(arg1);
789  int nid = OBJ_sn2nid(name);
790 
791  (void)ERR_get_error();
792  if (nid == NID_undef)
793  ossl_raise(eEC_GROUP, "unknown curve name (%s)", name);
794 
795  group = EC_GROUP_new_by_curve_name(nid);
796  if (group == NULL)
797  ossl_raise(eEC_GROUP, "unable to create curve (%s)", name);
798 
799  EC_GROUP_set_asn1_flag(group, OPENSSL_EC_NAMED_CURVE);
800  EC_GROUP_set_point_conversion_form(group, POINT_CONVERSION_UNCOMPRESSED);
801  }
802  }
803 
804  break;
805  case 4:
806  if (SYMBOL_P(arg1)) {
807  ID id = SYM2ID(arg1);
808  EC_GROUP *(*new_curve)(const BIGNUM *, const BIGNUM *, const BIGNUM *, BN_CTX *) = NULL;
809  const BIGNUM *p = GetBNPtr(arg2);
810  const BIGNUM *a = GetBNPtr(arg3);
811  const BIGNUM *b = GetBNPtr(arg4);
812 
813  if (id == s_GFp) {
814  new_curve = EC_GROUP_new_curve_GFp;
815  } else if (id == s_GF2m) {
816  new_curve = EC_GROUP_new_curve_GF2m;
817  } else {
818  ossl_raise(rb_eArgError, "unknown symbol, must be :GFp or :GF2m");
819  }
820 
821  if ((group = new_curve(p, a, b, ossl_bn_ctx)) == NULL)
822  ossl_raise(eEC_GROUP, "EC_GROUP_new_by_GF*");
823  } else {
824  ossl_raise(rb_eArgError, "unknown argument, must be :GFp or :GF2m");
825  }
826 
827  break;
828  default:
829  ossl_raise(rb_eArgError, "wrong number of arguments");
830  }
831 
832  if (group == NULL)
833  ossl_raise(eEC_GROUP, "");
834 
835  ec_group->group = group;
836 
837  return self;
838 }
839 
840 /* call-seq:
841  * group1 == group2 => true | false
842  *
843  */
844 static VALUE ossl_ec_group_eql(VALUE a, VALUE b)
845 {
846  EC_GROUP *group1 = NULL, *group2 = NULL;
847 
848  Require_EC_GROUP(a, group1);
849  SafeRequire_EC_GROUP(b, group2);
850 
851  if (EC_GROUP_cmp(group1, group2, ossl_bn_ctx) == 1)
852  return Qfalse;
853 
854  return Qtrue;
855 }
856 
857 /* call-seq:
858  * group.generator => ec_point
859  *
860  * See the OpenSSL documentation for EC_GROUP_get0_generator()
861  */
862 static VALUE ossl_ec_group_get_generator(VALUE self)
863 {
864  VALUE point_obj;
865  EC_GROUP *group = NULL;
866 
867  Require_EC_GROUP(self, group);
868 
869  point_obj = ossl_ec_point_dup(EC_GROUP_get0_generator(group), self);
870 
871  return point_obj;
872 }
873 
874 /* call-seq:
875  * group.set_generator(generator, order, cofactor) => self
876  *
877  * See the OpenSSL documentation for EC_GROUP_set_generator()
878  */
879 static VALUE ossl_ec_group_set_generator(VALUE self, VALUE generator, VALUE order, VALUE cofactor)
880 {
881  EC_GROUP *group = NULL;
882  const EC_POINT *point;
883  const BIGNUM *o, *co;
884 
885  Require_EC_GROUP(self, group);
886  SafeRequire_EC_POINT(generator, point);
887  o = GetBNPtr(order);
888  co = GetBNPtr(cofactor);
889 
890  if (EC_GROUP_set_generator(group, point, o, co) != 1)
891  ossl_raise(eEC_GROUP, "EC_GROUP_set_generator");
892 
893  return self;
894 }
895 
896 /* call-seq:
897  * group.get_order => order_bn
898  *
899  * See the OpenSSL documentation for EC_GROUP_get_order()
900  */
901 static VALUE ossl_ec_group_get_order(VALUE self)
902 {
903  VALUE bn_obj;
904  BIGNUM *bn;
905  EC_GROUP *group = NULL;
906 
907  Require_EC_GROUP(self, group);
908 
909  bn_obj = ossl_bn_new(NULL);
910  bn = GetBNPtr(bn_obj);
911 
912  if (EC_GROUP_get_order(group, bn, ossl_bn_ctx) != 1)
913  ossl_raise(eEC_GROUP, "EC_GROUP_get_order");
914 
915  return bn_obj;
916 }
917 
918 /* call-seq:
919  * group.get_cofactor => cofactor_bn
920  *
921  * See the OpenSSL documentation for EC_GROUP_get_cofactor()
922  */
923 static VALUE ossl_ec_group_get_cofactor(VALUE self)
924 {
925  VALUE bn_obj;
926  BIGNUM *bn;
927  EC_GROUP *group = NULL;
928 
929  Require_EC_GROUP(self, group);
930 
931  bn_obj = ossl_bn_new(NULL);
932  bn = GetBNPtr(bn_obj);
933 
934  if (EC_GROUP_get_cofactor(group, bn, ossl_bn_ctx) != 1)
935  ossl_raise(eEC_GROUP, "EC_GROUP_get_cofactor");
936 
937  return bn_obj;
938 }
939 
940 /* call-seq:
941  * group.curve_name => String
942  *
943  * See the OpenSSL documentation for EC_GROUP_get_curve_name()
944  */
945 static VALUE ossl_ec_group_get_curve_name(VALUE self)
946 {
947  EC_GROUP *group = NULL;
948  int nid;
949 
950  Get_EC_GROUP(self, group);
951  if (group == NULL)
952  return Qnil;
953 
954  nid = EC_GROUP_get_curve_name(group);
955 
956 /* BUG: an nid or asn1 object should be returned, maybe. */
957  return rb_str_new2(OBJ_nid2sn(nid));
958 }
959 
960 /* call-seq:
961  * EC.builtin_curves => [[name, comment], ...]
962  *
963  * See the OpenSSL documentation for EC_builtin_curves()
964  */
965 static VALUE ossl_s_builtin_curves(VALUE self)
966 {
967  EC_builtin_curve *curves = NULL;
968  int n;
969  int crv_len = rb_long2int(EC_get_builtin_curves(NULL, 0));
970  VALUE ary, ret;
971 
972  curves = ALLOCA_N(EC_builtin_curve, crv_len);
973  if (curves == NULL)
974  return Qnil;
975  if (!EC_get_builtin_curves(curves, crv_len))
976  ossl_raise(rb_eRuntimeError, "EC_get_builtin_curves");
977 
978  ret = rb_ary_new2(crv_len);
979 
980  for (n = 0; n < crv_len; n++) {
981  const char *sname = OBJ_nid2sn(curves[n].nid);
982  const char *comment = curves[n].comment;
983 
984  ary = rb_ary_new2(2);
985  rb_ary_push(ary, rb_str_new2(sname));
986  rb_ary_push(ary, comment ? rb_str_new2(comment) : Qnil);
987  rb_ary_push(ret, ary);
988  }
989 
990  return ret;
991 }
992 
993 /* call-seq:
994  * group.asn1_flag => Fixnum
995  *
996  * See the OpenSSL documentation for EC_GROUP_get_asn1_flag()
997  */
998 static VALUE ossl_ec_group_get_asn1_flag(VALUE self)
999 {
1000  EC_GROUP *group = NULL;
1001  int flag;
1002 
1003  Require_EC_GROUP(self, group);
1004 
1005  flag = EC_GROUP_get_asn1_flag(group);
1006 
1007  return INT2FIX(flag);
1008 }
1009 
1010 /* call-seq:
1011  * group.asn1_flag = Fixnum => Fixnum
1012  *
1013  * See the OpenSSL documentation for EC_GROUP_set_asn1_flag()
1014  */
1015 static VALUE ossl_ec_group_set_asn1_flag(VALUE self, VALUE flag_v)
1016 {
1017  EC_GROUP *group = NULL;
1018 
1019  Require_EC_GROUP(self, group);
1020 
1021  EC_GROUP_set_asn1_flag(group, NUM2INT(flag_v));
1022 
1023  return flag_v;
1024 }
1025 
1026 /* call-seq:
1027  * group.point_conversion_form => :uncompressed | :compressed | :hybrid
1028  *
1029  * See the OpenSSL documentation for EC_GROUP_get_point_conversion_form()
1030  */
1031 static VALUE ossl_ec_group_get_point_conversion_form(VALUE self)
1032 {
1033  EC_GROUP *group = NULL;
1034  point_conversion_form_t form;
1035  VALUE ret;
1036 
1037  Require_EC_GROUP(self, group);
1038 
1039  form = EC_GROUP_get_point_conversion_form(group);
1040 
1041  switch (form) {
1042  case POINT_CONVERSION_UNCOMPRESSED: ret = ID_uncompressed; break;
1043  case POINT_CONVERSION_COMPRESSED: ret = ID_compressed; break;
1044  case POINT_CONVERSION_HYBRID: ret = ID_hybrid; break;
1045  default: ossl_raise(eEC_GROUP, "unsupported point conversion form: %d, this module should be updated", form);
1046  }
1047 
1048  return ID2SYM(ret);
1049 }
1050 
1051 /* call-seq:
1052  * group.point_conversion_form = form => form
1053  *
1054  * See the OpenSSL documentation for EC_GROUP_set_point_conversion_form()
1055  */
1056 static VALUE ossl_ec_group_set_point_conversion_form(VALUE self, VALUE form_v)
1057 {
1058  EC_GROUP *group = NULL;
1059  point_conversion_form_t form;
1060  ID form_id = SYM2ID(form_v);
1061 
1062  Require_EC_GROUP(self, group);
1063 
1064  if (form_id == ID_uncompressed) {
1065  form = POINT_CONVERSION_UNCOMPRESSED;
1066  } else if (form_id == ID_compressed) {
1067  form = POINT_CONVERSION_COMPRESSED;
1068  } else if (form_id == ID_hybrid) {
1069  form = POINT_CONVERSION_HYBRID;
1070  } else {
1071  ossl_raise(rb_eArgError, "form must be :compressed, :uncompressed, or :hybrid");
1072  }
1073 
1074  EC_GROUP_set_point_conversion_form(group, form);
1075 
1076  return form_v;
1077 }
1078 
1079 /* call-seq:
1080  * group.seed => String or nil
1081  *
1082  * See the OpenSSL documentation for EC_GROUP_get0_seed()
1083  */
1084 static VALUE ossl_ec_group_get_seed(VALUE self)
1085 {
1086  EC_GROUP *group = NULL;
1087  size_t seed_len;
1088 
1089  Require_EC_GROUP(self, group);
1090 
1091  seed_len = EC_GROUP_get_seed_len(group);
1092 
1093  if (seed_len == 0)
1094  return Qnil;
1095 
1096  return rb_str_new((const char *)EC_GROUP_get0_seed(group), seed_len);
1097 }
1098 
1099 /* call-seq:
1100  * group.seed = seed => seed
1101  *
1102  * See the OpenSSL documentation for EC_GROUP_set_seed()
1103  */
1104 static VALUE ossl_ec_group_set_seed(VALUE self, VALUE seed)
1105 {
1106  EC_GROUP *group = NULL;
1107 
1108  Require_EC_GROUP(self, group);
1109  StringValue(seed);
1110 
1111  if (EC_GROUP_set_seed(group, (unsigned char *)RSTRING_PTR(seed), RSTRING_LEN(seed)) != (size_t)RSTRING_LEN(seed))
1112  ossl_raise(eEC_GROUP, "EC_GROUP_set_seed");
1113 
1114  return seed;
1115 }
1116 
1117 /* get/set curve GFp, GF2m */
1118 
1119 /* call-seq:
1120  * group.degree => Fixnum
1121  *
1122  * See the OpenSSL documentation for EC_GROUP_get_degree()
1123  */
1124 static VALUE ossl_ec_group_get_degree(VALUE self)
1125 {
1126  EC_GROUP *group = NULL;
1127 
1128  Require_EC_GROUP(self, group);
1129 
1130  return INT2NUM(EC_GROUP_get_degree(group));
1131 }
1132 
1133 static VALUE ossl_ec_group_to_string(VALUE self, int format)
1134 {
1135  EC_GROUP *group;
1136  BIO *out;
1137  int i = -1;
1138  VALUE str;
1139 
1140  Get_EC_GROUP(self, group);
1141 
1142  if (!(out = BIO_new(BIO_s_mem())))
1143  ossl_raise(eEC_GROUP, "BIO_new(BIO_s_mem())");
1144 
1145  switch(format) {
1146  case EXPORT_PEM:
1147  i = PEM_write_bio_ECPKParameters(out, group);
1148  break;
1149  case EXPORT_DER:
1150  i = i2d_ECPKParameters_bio(out, group);
1151  break;
1152  default:
1153  BIO_free(out);
1154  ossl_raise(rb_eRuntimeError, "unknown format (internal error)");
1155  }
1156 
1157  if (i != 1) {
1158  BIO_free(out);
1160  }
1161 
1162  str = ossl_membio2str(out);
1163 
1164  return str;
1165 }
1166 
1167 /* call-seq:
1168  * group.to_pem => String
1169  *
1170  * See the OpenSSL documentation for PEM_write_bio_ECPKParameters()
1171  */
1172 static VALUE ossl_ec_group_to_pem(VALUE self)
1173 {
1174  return ossl_ec_group_to_string(self, EXPORT_PEM);
1175 }
1176 
1177 /* call-seq:
1178  * group.to_der => String
1179  *
1180  * See the OpenSSL documentation for i2d_ECPKParameters_bio()
1181  */
1182 static VALUE ossl_ec_group_to_der(VALUE self)
1183 {
1184  return ossl_ec_group_to_string(self, EXPORT_DER);
1185 }
1186 
1187 /* call-seq:
1188  * group.to_text => String
1189  *
1190  * See the OpenSSL documentation for ECPKParameters_print()
1191  */
1192 static VALUE ossl_ec_group_to_text(VALUE self)
1193 {
1194  EC_GROUP *group;
1195  BIO *out;
1196  VALUE str;
1197 
1198  Require_EC_GROUP(self, group);
1199  if (!(out = BIO_new(BIO_s_mem()))) {
1200  ossl_raise(eEC_GROUP, "BIO_new(BIO_s_mem())");
1201  }
1202  if (!ECPKParameters_print(out, group, 0)) {
1203  BIO_free(out);
1205  }
1206  str = ossl_membio2str(out);
1207 
1208  return str;
1209 }
1210 
1211 
1212 static void ossl_ec_point_free(ossl_ec_point *ec_point)
1213 {
1214  if (!ec_point->dont_free && ec_point->point)
1215  EC_POINT_clear_free(ec_point->point);
1216  ruby_xfree(ec_point);
1217 }
1218 
1219 static VALUE ossl_ec_point_alloc(VALUE klass)
1220 {
1221  ossl_ec_point *ec_point;
1222  VALUE obj;
1223 
1224  obj = Data_Make_Struct(klass, ossl_ec_point, 0, ossl_ec_point_free, ec_point);
1225 
1226  return obj;
1227 }
1228 
1229 /*
1230  * call-seq:
1231  * OpenSSL::PKey::EC::Point.new(point)
1232  * OpenSSL::PKey::EC::Point.new(group)
1233  * OpenSSL::PKey::EC::Point.new(group, bn)
1234  *
1235  * See the OpenSSL documentation for EC_POINT_*
1236  */
1237 static VALUE ossl_ec_point_initialize(int argc, VALUE *argv, VALUE self)
1238 {
1239  ossl_ec_point *ec_point;
1240  EC_POINT *point = NULL;
1241  VALUE arg1, arg2;
1242  VALUE group_v = Qnil;
1243  const EC_GROUP *group = NULL;
1244 
1245  Data_Get_Struct(self, ossl_ec_point, ec_point);
1246  if (ec_point->point)
1247  ossl_raise(eEC_POINT, "EC_POINT already initialized");
1248 
1249  switch (rb_scan_args(argc, argv, "11", &arg1, &arg2)) {
1250  case 1:
1251  if (rb_obj_is_kind_of(arg1, cEC_POINT)) {
1252  const EC_POINT *arg_point;
1253 
1254  group_v = rb_iv_get(arg1, "@group");
1255  SafeRequire_EC_GROUP(group_v, group);
1256  SafeRequire_EC_POINT(arg1, arg_point);
1257 
1258  point = EC_POINT_dup(arg_point, group);
1259  } else if (rb_obj_is_kind_of(arg1, cEC_GROUP)) {
1260  group_v = arg1;
1261  SafeRequire_EC_GROUP(group_v, group);
1262 
1263  point = EC_POINT_new(group);
1264  } else {
1265  ossl_raise(eEC_POINT, "wrong argument type: must be OpenSSL::PKey::EC::Point or OpenSSL::Pkey::EC::Group");
1266  }
1267 
1268  break;
1269  case 2:
1270  if (!rb_obj_is_kind_of(arg1, cEC_GROUP))
1271  ossl_raise(rb_eArgError, "1st argument must be OpenSSL::PKey::EC::Group");
1272  group_v = arg1;
1273  SafeRequire_EC_GROUP(group_v, group);
1274 
1275  if (rb_obj_is_kind_of(arg2, cBN)) {
1276  const BIGNUM *bn = GetBNPtr(arg2);
1277 
1278  point = EC_POINT_bn2point(group, bn, NULL, ossl_bn_ctx);
1279  } else {
1280  BIO *in = ossl_obj2bio(arg1);
1281 
1282 /* BUG: finish me */
1283 
1284  BIO_free(in);
1285 
1286  if (point == NULL) {
1287  ossl_raise(eEC_POINT, "unknown type for 2nd arg");
1288  }
1289  }
1290  break;
1291  default:
1292  ossl_raise(rb_eArgError, "wrong number of arguments");
1293  }
1294 
1295  if (point == NULL)
1297 
1298  if (NIL_P(group_v))
1299  ossl_raise(rb_eRuntimeError, "missing group (internal error)");
1300 
1301  ec_point->point = point;
1302 
1303  rb_iv_set(self, "@group", group_v);
1304 
1305  return self;
1306 }
1307 
1308 /*
1309  * call-seq:
1310  * point1 == point2 => true | false
1311  *
1312  */
1313 static VALUE ossl_ec_point_eql(VALUE a, VALUE b)
1314 {
1315  EC_POINT *point1, *point2;
1316  VALUE group_v1 = rb_iv_get(a, "@group");
1317  VALUE group_v2 = rb_iv_get(b, "@group");
1318  const EC_GROUP *group;
1319 
1320  if (ossl_ec_group_eql(group_v1, group_v2) == Qfalse)
1321  return Qfalse;
1322 
1323  Require_EC_POINT(a, point1);
1324  SafeRequire_EC_POINT(b, point2);
1325  SafeRequire_EC_GROUP(group_v1, group);
1326 
1327  if (EC_POINT_cmp(group, point1, point2, ossl_bn_ctx) == 1)
1328  return Qfalse;
1329 
1330  return Qtrue;
1331 }
1332 
1333 /*
1334  * call-seq:
1335  * point.infinity? => true | false
1336  *
1337  */
1338 static VALUE ossl_ec_point_is_at_infinity(VALUE self)
1339 {
1340  EC_POINT *point;
1341  VALUE group_v = rb_iv_get(self, "@group");
1342  const EC_GROUP *group;
1343 
1344  Require_EC_POINT(self, point);
1345  SafeRequire_EC_GROUP(group_v, group);
1346 
1347  switch (EC_POINT_is_at_infinity(group, point)) {
1348  case 1: return Qtrue;
1349  case 0: return Qfalse;
1350  default: ossl_raise(cEC_POINT, "EC_POINT_is_at_infinity");
1351  }
1352 }
1353 
1354 /*
1355  * call-seq:
1356  * point.on_curve? => true | false
1357  *
1358  */
1359 static VALUE ossl_ec_point_is_on_curve(VALUE self)
1360 {
1361  EC_POINT *point;
1362  VALUE group_v = rb_iv_get(self, "@group");
1363  const EC_GROUP *group;
1364 
1365  Require_EC_POINT(self, point);
1366  SafeRequire_EC_GROUP(group_v, group);
1367 
1368  switch (EC_POINT_is_on_curve(group, point, ossl_bn_ctx)) {
1369  case 1: return Qtrue;
1370  case 0: return Qfalse;
1371  default: ossl_raise(cEC_POINT, "EC_POINT_is_on_curve");
1372  }
1373 }
1374 
1375 /*
1376  * call-seq:
1377  * point.make_affine! => self
1378  *
1379  */
1380 static VALUE ossl_ec_point_make_affine(VALUE self)
1381 {
1382  EC_POINT *point;
1383  VALUE group_v = rb_iv_get(self, "@group");
1384  const EC_GROUP *group;
1385 
1386  Require_EC_POINT(self, point);
1387  SafeRequire_EC_GROUP(group_v, group);
1388 
1389  if (EC_POINT_make_affine(group, point, ossl_bn_ctx) != 1)
1390  ossl_raise(cEC_POINT, "EC_POINT_make_affine");
1391 
1392  return self;
1393 }
1394 
1395 /*
1396  * call-seq:
1397  * point.invert! => self
1398  *
1399  */
1400 static VALUE ossl_ec_point_invert(VALUE self)
1401 {
1402  EC_POINT *point;
1403  VALUE group_v = rb_iv_get(self, "@group");
1404  const EC_GROUP *group;
1405 
1406  Require_EC_POINT(self, point);
1407  SafeRequire_EC_GROUP(group_v, group);
1408 
1409  if (EC_POINT_invert(group, point, ossl_bn_ctx) != 1)
1410  ossl_raise(cEC_POINT, "EC_POINT_invert");
1411 
1412  return self;
1413 }
1414 
1415 /*
1416  * call-seq:
1417  * point.set_to_infinity! => self
1418  *
1419  */
1420 static VALUE ossl_ec_point_set_to_infinity(VALUE self)
1421 {
1422  EC_POINT *point;
1423  VALUE group_v = rb_iv_get(self, "@group");
1424  const EC_GROUP *group;
1425 
1426  Require_EC_POINT(self, point);
1427  SafeRequire_EC_GROUP(group_v, group);
1428 
1429  if (EC_POINT_set_to_infinity(group, point) != 1)
1430  ossl_raise(cEC_POINT, "EC_POINT_set_to_infinity");
1431 
1432  return self;
1433 }
1434 
1435 /*
1436  * call-seq:
1437  * point.to_bn => OpenSSL::BN
1438  *
1439  * See the OpenSSL documentation for EC_POINT_point2bn()
1440  */
1441 static VALUE ossl_ec_point_to_bn(VALUE self)
1442 {
1443  EC_POINT *point;
1444  VALUE bn_obj;
1445  VALUE group_v = rb_iv_get(self, "@group");
1446  const EC_GROUP *group;
1447  point_conversion_form_t form;
1448  BIGNUM *bn;
1449 
1450  Require_EC_POINT(self, point);
1451  SafeRequire_EC_GROUP(group_v, group);
1452 
1453  form = EC_GROUP_get_point_conversion_form(group);
1454 
1455  bn_obj = rb_obj_alloc(cBN);
1456  bn = GetBNPtr(bn_obj);
1457 
1458  if (EC_POINT_point2bn(group, point, form, bn, ossl_bn_ctx) == NULL)
1459  ossl_raise(eEC_POINT, "EC_POINT_point2bn");
1460 
1461  return bn_obj;
1462 }
1463 
1464 static void no_copy(VALUE klass)
1465 {
1466  rb_undef_method(klass, "copy");
1467  rb_undef_method(klass, "clone");
1468  rb_undef_method(klass, "dup");
1469  rb_undef_method(klass, "initialize_copy");
1470 }
1471 
1472 void Init_ossl_ec()
1473 {
1474 #ifdef DONT_NEED_RDOC_WORKAROUND
1475  mOSSL = rb_define_module("OpenSSL");
1476  mPKey = rb_define_module_under(mOSSL, "PKey");
1477 #endif
1478 
1480 
1486 
1487  s_GFp = rb_intern("GFp");
1488  s_GF2m = rb_intern("GF2m");
1489  s_GFp_simple = rb_intern("GFp_simple");
1490  s_GFp_mont = rb_intern("GFp_mont");
1491  s_GFp_nist = rb_intern("GFp_nist");
1492  s_GF2m_simple = rb_intern("GF2m_simple");
1493 
1494  ID_uncompressed = rb_intern("uncompressed");
1495  ID_compressed = rb_intern("compressed");
1496  ID_hybrid = rb_intern("hybrid");
1497 
1498 #ifdef OPENSSL_EC_NAMED_CURVE
1499  rb_define_const(cEC, "NAMED_CURVE", ULONG2NUM(OPENSSL_EC_NAMED_CURVE));
1500 #endif
1501 
1502  rb_define_singleton_method(cEC, "builtin_curves", ossl_s_builtin_curves, 0);
1503 
1504  rb_define_method(cEC, "initialize", ossl_ec_key_initialize, -1);
1505 /* copy/dup/cmp */
1506 
1507  rb_define_method(cEC, "group", ossl_ec_key_get_group, 0);
1508  rb_define_method(cEC, "group=", ossl_ec_key_set_group, 1);
1509  rb_define_method(cEC, "private_key", ossl_ec_key_get_private_key, 0);
1510  rb_define_method(cEC, "private_key=", ossl_ec_key_set_private_key, 1);
1511  rb_define_method(cEC, "public_key", ossl_ec_key_get_public_key, 0);
1512  rb_define_method(cEC, "public_key=", ossl_ec_key_set_public_key, 1);
1513  rb_define_method(cEC, "private_key?", ossl_ec_key_is_private_key, 0);
1514  rb_define_method(cEC, "public_key?", ossl_ec_key_is_public_key, 0);
1515 /* rb_define_method(cEC, "", ossl_ec_key_get_, 0);
1516  rb_define_method(cEC, "=", ossl_ec_key_set_ 1);
1517  set/get enc_flags
1518  set/get _conv_from
1519  set/get asn1_flag (can use ruby to call self.group.asn1_flag)
1520  set/get precompute_mult
1521 */
1522  rb_define_method(cEC, "generate_key", ossl_ec_key_generate_key, 0);
1523  rb_define_method(cEC, "check_key", ossl_ec_key_check_key, 0);
1524 
1525  rb_define_method(cEC, "dh_compute_key", ossl_ec_key_dh_compute_key, 1);
1526  rb_define_method(cEC, "dsa_sign_asn1", ossl_ec_key_dsa_sign_asn1, 1);
1527  rb_define_method(cEC, "dsa_verify_asn1", ossl_ec_key_dsa_verify_asn1, 2);
1528 /* do_sign/do_verify */
1529 
1530  rb_define_method(cEC, "to_pem", ossl_ec_key_to_pem, -1);
1531  rb_define_method(cEC, "to_der", ossl_ec_key_to_der, 0);
1532  rb_define_method(cEC, "to_text", ossl_ec_key_to_text, 0);
1533 
1534 
1535  rb_define_alloc_func(cEC_GROUP, ossl_ec_group_alloc);
1536  rb_define_method(cEC_GROUP, "initialize", ossl_ec_group_initialize, -1);
1537  rb_define_method(cEC_GROUP, "eql?", ossl_ec_group_eql, 1);
1538  rb_define_alias(cEC_GROUP, "==", "eql?");
1539 /* copy/dup/cmp */
1540 
1541  rb_define_method(cEC_GROUP, "generator", ossl_ec_group_get_generator, 0);
1542  rb_define_method(cEC_GROUP, "set_generator", ossl_ec_group_set_generator, 3);
1543  rb_define_method(cEC_GROUP, "order", ossl_ec_group_get_order, 0);
1544  rb_define_method(cEC_GROUP, "cofactor", ossl_ec_group_get_cofactor, 0);
1545 
1546  rb_define_method(cEC_GROUP, "curve_name", ossl_ec_group_get_curve_name, 0);
1547 /* rb_define_method(cEC_GROUP, "curve_name=", ossl_ec_group_set_curve_name, 1); */
1548 
1549  rb_define_method(cEC_GROUP, "asn1_flag", ossl_ec_group_get_asn1_flag, 0);
1550  rb_define_method(cEC_GROUP, "asn1_flag=", ossl_ec_group_set_asn1_flag, 1);
1551 
1552  rb_define_method(cEC_GROUP, "point_conversion_form", ossl_ec_group_get_point_conversion_form, 0);
1553  rb_define_method(cEC_GROUP, "point_conversion_form=", ossl_ec_group_set_point_conversion_form, 1);
1554 
1555  rb_define_method(cEC_GROUP, "seed", ossl_ec_group_get_seed, 0);
1556  rb_define_method(cEC_GROUP, "seed=", ossl_ec_group_set_seed, 1);
1557 
1558 /* get/set GFp, GF2m */
1559 
1560  rb_define_method(cEC_GROUP, "degree", ossl_ec_group_get_degree, 0);
1561 
1562 /* check* */
1563 
1564 
1565  rb_define_method(cEC_GROUP, "to_pem", ossl_ec_group_to_pem, 0);
1566  rb_define_method(cEC_GROUP, "to_der", ossl_ec_group_to_der, 0);
1567  rb_define_method(cEC_GROUP, "to_text", ossl_ec_group_to_text, 0);
1568 
1569 
1570  rb_define_alloc_func(cEC_POINT, ossl_ec_point_alloc);
1571  rb_define_method(cEC_POINT, "initialize", ossl_ec_point_initialize, -1);
1572  rb_attr(cEC_POINT, rb_intern("group"), 1, 0, 0);
1573  rb_define_method(cEC_POINT, "eql?", ossl_ec_point_eql, 1);
1574  rb_define_alias(cEC_POINT, "==", "eql?");
1575 
1576  rb_define_method(cEC_POINT, "infinity?", ossl_ec_point_is_at_infinity, 0);
1577  rb_define_method(cEC_POINT, "on_curve?", ossl_ec_point_is_on_curve, 0);
1578  rb_define_method(cEC_POINT, "make_affine!", ossl_ec_point_make_affine, 0);
1579  rb_define_method(cEC_POINT, "invert!", ossl_ec_point_invert, 0);
1580  rb_define_method(cEC_POINT, "set_to_infinity!", ossl_ec_point_set_to_infinity, 0);
1581 /* all the other methods */
1582 
1583  rb_define_method(cEC_POINT, "to_bn", ossl_ec_point_to_bn, 0);
1584 
1585  no_copy(cEC);
1586  no_copy(cEC_GROUP);
1587  no_copy(cEC_POINT);
1588 }
1589 
1590 #else /* defined NO_EC */
1592 {
1593 }
1594 #endif /* NO_EC */
#define RSTRING_LEN(string)
Definition: generator.h:45
VALUE mOSSL
Definition: ossl.c:250
int i
Definition: win32ole.c:776
#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:1342
VALUE mPKey
Definition: ossl_pkey.c:16
VALUE ePKeyError
Definition: ossl_pkey.c:18
#define Qtrue
Definition: ruby.h:366
VALUE rb_eTypeError
Definition: error.c:467
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
VALUE rb_funcall(VALUE, ID, int,...)
Calls a method.
Definition: vm_eval.c:638
#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:514
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
VALUE ossl_membio2str(BIO *bio)
Definition: ossl_bio.c:77
void rb_undef_method(VALUE klass, const char *name)
Definition: class.c:1227
#define ID2SYM(i)
Definition: cparse.c:63
Win32OLEIDispatch * p
Definition: win32ole.c:778
RUBY_EXTERN VALUE rb_cObject
Definition: ruby.h:1246
VALUE rb_eRuntimeError
Definition: error.c:466
#define SYM2ID(v)
Definition: cparse.c:66
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
void rb_define_const(VALUE, const char *, VALUE)
Definition: variable.c:1923
VALUE eOSSLError
Definition: ossl.c:255
int argc
Definition: ruby.c:120
#define Qfalse
Definition: ruby.h:365
#define ALLOCA_N(type, n)
Definition: ruby.h:1038
VALUE rb_obj_alloc(VALUE)
Definition: object.c:1600
arg
Definition: ripper.y:1283
const EVP_CIPHER * GetCipherPtr(VALUE obj)
Definition: ossl_cipher.c:45
VALUE rb_str_resize(VALUE, long)
Definition: string.c:1771
void Init_ossl_ec()
void rb_define_alias(VALUE klass, const char *name1, const char *name2)
Defines an alias of a method.
Definition: class.c:1384
#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:914
int rb_scan_args(int argc, const VALUE *argv, const char *fmt,...)
Definition: class.c:1415
VALUE cEC_GROUP
unsigned long ID
Definition: ruby.h:89
#define NULL
#define Qnil
Definition: ruby.h:367
unsigned long VALUE
Definition: ruby.h:88
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:606
#define StringValueCStr(v)
Definition: ruby.h:468
VALUE cEC_POINT
#define SYMBOL_P(v)
Definition: cparse.c:69
#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
void ossl_raise(VALUE exc, const char *fmt,...)
Definition: ossl.c:319
VALUE rb_ary_new2(long capa)
Definition: array.c:332
const char * name
Definition: nkf.c:208
VALUE ossl_bn_new(const BIGNUM *bn)
Definition: ossl_bn.c:43
#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
VALUE rb_define_module(const char *name)
Definition: class.c:586
VALUE cEC
VALUE cPKey
Definition: ossl_pkey.c:17
#define rb_intern(str)
void rb_define_method(VALUE klass, const char *name, VALUE(*func)(ANYARGS), int argc)
Definition: class.c:1209
VALUE rb_str_new2(const char *)
VALUE ossl_ec_new(EVP_PKEY *)
VALUE rb_eArgError
Definition: error.c:468
char ** argv
Definition: ruby.c:121
#define StringValue(v)
Definition: ruby.h:466
VALUE rb_str_new(const char *, long)
Definition: string.c:410