rubysl-openssl 2.10 → 2.11

Sign up to get free protection for your applications and to get access to all the features.
Files changed (55) hide show
  1. checksums.yaml +5 -5
  2. data/ext/rubysl/openssl/deprecation.rb +7 -3
  3. data/ext/rubysl/openssl/extconf.rb +148 -103
  4. data/ext/rubysl/openssl/openssl_missing.c +94 -275
  5. data/ext/rubysl/openssl/openssl_missing.h +167 -98
  6. data/ext/rubysl/openssl/ossl.c +266 -212
  7. data/ext/rubysl/openssl/ossl.h +27 -89
  8. data/ext/rubysl/openssl/ossl_asn1.c +157 -221
  9. data/ext/rubysl/openssl/ossl_asn1.h +11 -3
  10. data/ext/rubysl/openssl/ossl_bio.c +10 -40
  11. data/ext/rubysl/openssl/ossl_bio.h +1 -2
  12. data/ext/rubysl/openssl/ossl_bn.c +144 -100
  13. data/ext/rubysl/openssl/ossl_bn.h +3 -1
  14. data/ext/rubysl/openssl/ossl_cipher.c +270 -195
  15. data/ext/rubysl/openssl/ossl_config.c +7 -1
  16. data/ext/rubysl/openssl/ossl_config.h +0 -1
  17. data/ext/rubysl/openssl/ossl_digest.c +40 -29
  18. data/ext/rubysl/openssl/ossl_engine.c +23 -62
  19. data/ext/rubysl/openssl/ossl_hmac.c +82 -55
  20. data/ext/rubysl/openssl/ossl_ns_spki.c +22 -22
  21. data/ext/rubysl/openssl/ossl_ocsp.c +894 -144
  22. data/ext/rubysl/openssl/ossl_ocsp.h +1 -1
  23. data/ext/rubysl/openssl/ossl_pkcs12.c +47 -19
  24. data/ext/rubysl/openssl/ossl_pkcs5.c +7 -15
  25. data/ext/rubysl/openssl/ossl_pkcs7.c +38 -15
  26. data/ext/rubysl/openssl/ossl_pkey.c +151 -99
  27. data/ext/rubysl/openssl/ossl_pkey.h +123 -29
  28. data/ext/rubysl/openssl/ossl_pkey_dh.c +143 -92
  29. data/ext/rubysl/openssl/ossl_pkey_dsa.c +149 -104
  30. data/ext/rubysl/openssl/ossl_pkey_ec.c +646 -524
  31. data/ext/rubysl/openssl/ossl_pkey_rsa.c +180 -121
  32. data/ext/rubysl/openssl/ossl_rand.c +25 -21
  33. data/ext/rubysl/openssl/ossl_ssl.c +795 -413
  34. data/ext/rubysl/openssl/ossl_ssl.h +3 -0
  35. data/ext/rubysl/openssl/ossl_ssl_session.c +83 -77
  36. data/ext/rubysl/openssl/ossl_version.h +1 -1
  37. data/ext/rubysl/openssl/ossl_x509.c +92 -8
  38. data/ext/rubysl/openssl/ossl_x509.h +14 -5
  39. data/ext/rubysl/openssl/ossl_x509attr.c +77 -41
  40. data/ext/rubysl/openssl/ossl_x509cert.c +45 -46
  41. data/ext/rubysl/openssl/ossl_x509crl.c +51 -57
  42. data/ext/rubysl/openssl/ossl_x509ext.c +39 -33
  43. data/ext/rubysl/openssl/ossl_x509name.c +68 -45
  44. data/ext/rubysl/openssl/ossl_x509req.c +32 -38
  45. data/ext/rubysl/openssl/ossl_x509revoked.c +43 -9
  46. data/ext/rubysl/openssl/ossl_x509store.c +309 -104
  47. data/ext/rubysl/openssl/ruby_missing.h +8 -6
  48. data/lib/openssl/buffering.rb +11 -5
  49. data/lib/openssl/cipher.rb +23 -15
  50. data/lib/openssl/digest.rb +7 -10
  51. data/lib/openssl/pkey.rb +15 -8
  52. data/lib/openssl/ssl.rb +81 -105
  53. data/lib/rubysl/openssl.rb +1 -4
  54. data/lib/rubysl/openssl/version.rb +1 -1
  55. metadata +3 -4
@@ -13,18 +13,26 @@
13
13
  /*
14
14
  * ASN1_DATE conversions
15
15
  */
16
- VALUE asn1time_to_time(ASN1_TIME *);
16
+ VALUE asn1time_to_time(const ASN1_TIME *);
17
+ #if defined(HAVE_ASN1_TIME_ADJ)
18
+ /* Splits VALUE to seconds and offset days. VALUE is typically a Time or an
19
+ * Integer. This is used when updating ASN1_*TIME with ASN1_TIME_adj() or
20
+ * X509_time_adj_ex(). We can't use ASN1_TIME_set() and X509_time_adj() because
21
+ * they have the Year 2038 issue on sizeof(time_t) == 4 environment */
22
+ void ossl_time_split(VALUE, time_t *, int *);
23
+ #else
17
24
  time_t time_to_time_t(VALUE);
25
+ #endif
18
26
 
19
27
  /*
20
28
  * ASN1_STRING conversions
21
29
  */
22
- VALUE asn1str_to_str(ASN1_STRING *);
30
+ VALUE asn1str_to_str(const ASN1_STRING *);
23
31
 
24
32
  /*
25
33
  * ASN1_INTEGER conversions
26
34
  */
27
- VALUE asn1integer_to_num(ASN1_INTEGER *);
35
+ VALUE asn1integer_to_num(const ASN1_INTEGER *);
28
36
  ASN1_INTEGER *num_to_asn1integer(VALUE, ASN1_INTEGER *);
29
37
 
30
38
  /*
@@ -8,53 +8,23 @@
8
8
  * (See the file 'LICENCE'.)
9
9
  */
10
10
  #include "ossl.h"
11
- #ifdef HAVE_UNISTD_H
12
- #include <unistd.h>
13
- #endif
14
11
 
15
12
  BIO *
16
- ossl_obj2bio(VALUE obj)
13
+ ossl_obj2bio(volatile VALUE *pobj)
17
14
  {
15
+ VALUE obj = *pobj;
18
16
  BIO *bio;
19
17
 
20
- if (RB_TYPE_P(obj, T_FILE)) {
21
- rb_io_t *fptr;
22
- FILE *fp;
23
- int fd;
24
-
25
- GetOpenFile(obj, fptr);
26
- rb_io_check_readable(fptr);
27
- if ((fd = rb_cloexec_dup(FPTR_TO_FD(fptr))) < 0){
28
- rb_sys_fail(0);
29
- }
30
- rb_update_max_fd(fd);
31
- if (!(fp = fdopen(fd, "r"))){
32
- int e = errno;
33
- close(fd);
34
- rb_syserr_fail(e, 0);
35
- }
36
- if (!(bio = BIO_new_fp(fp, BIO_CLOSE))){
37
- fclose(fp);
38
- ossl_raise(eOSSLError, NULL);
39
- }
40
- }
41
- else {
42
- StringValue(obj);
43
- bio = BIO_new_mem_buf(RSTRING_PTR(obj), RSTRING_LENINT(obj));
44
- if (!bio) ossl_raise(eOSSLError, NULL);
45
- }
46
-
18
+ if (RB_TYPE_P(obj, T_FILE))
19
+ obj = rb_funcallv(obj, rb_intern("read"), 0, NULL);
20
+ StringValue(obj);
21
+ bio = BIO_new_mem_buf(RSTRING_PTR(obj), RSTRING_LENINT(obj));
22
+ if (!bio)
23
+ ossl_raise(eOSSLError, "BIO_new_mem_buf");
24
+ *pobj = obj;
47
25
  return bio;
48
26
  }
49
27
 
50
- BIO *
51
- ossl_protect_obj2bio(VALUE obj, int *status)
52
- {
53
- BIO *ret = NULL;
54
- ret = (BIO*)rb_protect((VALUE(*)_((VALUE)))ossl_obj2bio, obj, status);
55
- return ret;
56
- }
57
-
58
28
  VALUE
59
29
  ossl_membio2str0(BIO *bio)
60
30
  {
@@ -70,7 +40,7 @@ ossl_membio2str0(BIO *bio)
70
40
  VALUE
71
41
  ossl_protect_membio2str(BIO *bio, int *status)
72
42
  {
73
- return rb_protect((VALUE(*)_((VALUE)))ossl_membio2str0, (VALUE)bio, status);
43
+ return rb_protect((VALUE (*)(VALUE))ossl_membio2str0, (VALUE)bio, status);
74
44
  }
75
45
 
76
46
  VALUE
@@ -10,8 +10,7 @@
10
10
  #if !defined(_OSSL_BIO_H_)
11
11
  #define _OSSL_BIO_H_
12
12
 
13
- BIO *ossl_obj2bio(VALUE);
14
- BIO *ossl_protect_obj2bio(VALUE,int*);
13
+ BIO *ossl_obj2bio(volatile VALUE *);
15
14
  VALUE ossl_membio2str0(BIO*);
16
15
  VALUE ossl_membio2str(BIO*);
17
16
  VALUE ossl_protect_membio2str(BIO*,int*);
@@ -37,17 +37,12 @@ ossl_bn_free(void *ptr)
37
37
  BN_clear_free(ptr);
38
38
  }
39
39
 
40
- static size_t
41
- ossl_bn_size(const void *ptr)
42
- {
43
- return sizeof(BIGNUM);
44
- }
45
-
46
40
  static const rb_data_type_t ossl_bn_type = {
47
41
  "OpenSSL/BN",
48
- {0, ossl_bn_free, ossl_bn_size,},
49
- 0, 0,
50
- RUBY_TYPED_FREE_IMMEDIATELY,
42
+ {
43
+ 0, ossl_bn_free,
44
+ },
45
+ 0, 0, RUBY_TYPED_FREE_IMMEDIATELY,
51
46
  };
52
47
 
53
48
  /*
@@ -56,8 +51,6 @@ static const rb_data_type_t ossl_bn_type = {
56
51
  VALUE cBN;
57
52
 
58
53
  /* Document-class: OpenSSL::BNError
59
- *
60
- * BNError < OpenSSLError
61
54
  *
62
55
  * Generic Error for all of OpenSSL::BN (big num)
63
56
  */
@@ -83,33 +76,78 @@ ossl_bn_new(const BIGNUM *bn)
83
76
  }
84
77
 
85
78
  static BIGNUM *
86
- try_convert_to_bnptr(VALUE obj)
79
+ integer_to_bnptr(VALUE obj, BIGNUM *orig)
87
80
  {
88
- BIGNUM *bn = NULL;
89
- VALUE newobj;
81
+ BIGNUM *bn;
90
82
 
91
- if (RTEST(rb_obj_is_kind_of(obj, cBN))) {
92
- GetBN(obj, bn);
93
- } else switch (TYPE(obj)) {
94
- case T_FIXNUM:
95
- case T_BIGNUM:
96
- obj = rb_String(obj);
97
- newobj = NewBN(cBN); /* GC bug */
98
- if (!BN_dec2bn(&bn, StringValuePtr(obj))) {
99
- ossl_raise(eBNError, NULL);
83
+ if (FIXNUM_P(obj)) {
84
+ long i;
85
+ unsigned char bin[sizeof(long)];
86
+ long n = FIX2LONG(obj);
87
+ unsigned long un = labs(n);
88
+
89
+ for (i = sizeof(long) - 1; 0 <= i; i--) {
90
+ bin[i] = un & 0xff;
91
+ un >>= 8;
100
92
  }
101
- SetBN(newobj, bn); /* Handle potencial mem leaks */
102
- break;
93
+
94
+ bn = BN_bin2bn(bin, sizeof(bin), orig);
95
+ if (!bn)
96
+ ossl_raise(eBNError, "BN_bin2bn");
97
+ if (n < 0)
98
+ BN_set_negative(bn, 1);
99
+ }
100
+ else { /* assuming Bignum */
101
+ size_t len = rb_absint_size(obj, NULL);
102
+ unsigned char *bin;
103
+ VALUE buf;
104
+ int sign;
105
+
106
+ if (INT_MAX < len) {
107
+ rb_raise(eBNError, "bignum too long");
108
+ }
109
+ bin = (unsigned char*)ALLOCV_N(unsigned char, buf, len);
110
+ sign = rb_integer_pack(obj, bin, len, 1, 0, INTEGER_PACK_BIG_ENDIAN);
111
+
112
+ bn = BN_bin2bn(bin, (int)len, orig);
113
+ ALLOCV_END(buf);
114
+ if (!bn)
115
+ ossl_raise(eBNError, "BN_bin2bn");
116
+ if (sign < 0)
117
+ BN_set_negative(bn, 1);
103
118
  }
119
+
104
120
  return bn;
105
121
  }
106
122
 
123
+ static VALUE
124
+ try_convert_to_bn(VALUE obj)
125
+ {
126
+ BIGNUM *bn;
127
+ VALUE newobj = Qnil;
128
+
129
+ if (rb_obj_is_kind_of(obj, cBN))
130
+ return obj;
131
+ if (RB_INTEGER_TYPE_P(obj)) {
132
+ newobj = NewBN(cBN); /* Handle potential mem leaks */
133
+ bn = integer_to_bnptr(obj, NULL);
134
+ SetBN(newobj, bn);
135
+ }
136
+
137
+ return newobj;
138
+ }
139
+
107
140
  BIGNUM *
108
- GetBNPtr(VALUE obj)
141
+ ossl_bn_value_ptr(volatile VALUE *ptr)
109
142
  {
110
- BIGNUM *bn = try_convert_to_bnptr(obj);
111
- if (!bn)
143
+ VALUE tmp;
144
+ BIGNUM *bn;
145
+
146
+ tmp = try_convert_to_bn(*ptr);
147
+ if (NIL_P(tmp))
112
148
  ossl_raise(rb_eTypeError, "Cannot convert into OpenSSL::BN");
149
+ GetBN(tmp, bn);
150
+ *ptr = tmp;
113
151
 
114
152
  return bn;
115
153
  }
@@ -140,6 +178,7 @@ ossl_bn_alloc(VALUE klass)
140
178
 
141
179
  /* Document-method: OpenSSL::BN.new
142
180
  *
181
+ * call-seq:
143
182
  * OpenSSL::BN.new => aBN
144
183
  * OpenSSL::BN.new(bn) => aBN
145
184
  * OpenSSL::BN.new(integer) => aBN
@@ -159,45 +198,13 @@ ossl_bn_initialize(int argc, VALUE *argv, VALUE self)
159
198
  base = NUM2INT(bs);
160
199
  }
161
200
 
162
- if (RB_TYPE_P(str, T_FIXNUM)) {
163
- long i;
164
- unsigned char bin[sizeof(long)];
165
- long n = FIX2LONG(str);
166
- unsigned long un = labs(n);
167
-
168
- for (i = sizeof(long) - 1; 0 <= i; i--) {
169
- bin[i] = un&0xff;
170
- un >>= 8;
171
- }
172
-
201
+ if (RB_INTEGER_TYPE_P(str)) {
173
202
  GetBN(self, bn);
174
- if (!BN_bin2bn(bin, sizeof(bin), bn)) {
175
- ossl_raise(eBNError, NULL);
176
- }
177
- if (n < 0) BN_set_negative(bn, 1);
178
- return self;
179
- }
180
- else if (RB_TYPE_P(str, T_BIGNUM)) {
181
- size_t len = rb_absint_size(str, NULL);
182
- unsigned char *bin;
183
- VALUE buf;
184
- int sign;
185
-
186
- if (INT_MAX < len) {
187
- rb_raise(eBNError, "bignum too long");
188
- }
189
- bin = (unsigned char*)ALLOCV_N(unsigned char, buf, len);
190
- sign = rb_integer_pack(str, bin, len, 1, 0, INTEGER_PACK_BIG_ENDIAN);
203
+ integer_to_bnptr(str, bn);
191
204
 
192
- GetBN(self, bn);
193
- if (!BN_bin2bn(bin, (int)len, bn)) {
194
- ALLOCV_END(buf);
195
- ossl_raise(eBNError, NULL);
196
- }
197
- ALLOCV_END(buf);
198
- if (sign < 0) BN_set_negative(bn, 1);
199
205
  return self;
200
206
  }
207
+
201
208
  if (RTEST(rb_obj_is_kind_of(str, cBN))) {
202
209
  BIGNUM *other;
203
210
 
@@ -209,26 +216,25 @@ ossl_bn_initialize(int argc, VALUE *argv, VALUE self)
209
216
  return self;
210
217
  }
211
218
 
212
- StringValue(str);
213
219
  GetBN(self, bn);
214
220
  switch (base) {
215
221
  case 0:
216
- if (!BN_mpi2bn((unsigned char *)RSTRING_PTR(str), RSTRING_LENINT(str), bn)) {
222
+ if (!BN_mpi2bn((unsigned char *)StringValuePtr(str), RSTRING_LENINT(str), bn)) {
217
223
  ossl_raise(eBNError, NULL);
218
224
  }
219
225
  break;
220
226
  case 2:
221
- if (!BN_bin2bn((unsigned char *)RSTRING_PTR(str), RSTRING_LENINT(str), bn)) {
227
+ if (!BN_bin2bn((unsigned char *)StringValuePtr(str), RSTRING_LENINT(str), bn)) {
222
228
  ossl_raise(eBNError, NULL);
223
229
  }
224
230
  break;
225
231
  case 10:
226
- if (!BN_dec2bn(&bn, RSTRING_PTR(str))) {
232
+ if (!BN_dec2bn(&bn, StringValueCStr(str))) {
227
233
  ossl_raise(eBNError, NULL);
228
234
  }
229
235
  break;
230
236
  case 16:
231
- if (!BN_hex2bn(&bn, RSTRING_PTR(str))) {
237
+ if (!BN_hex2bn(&bn, StringValueCStr(str))) {
232
238
  ossl_raise(eBNError, NULL);
233
239
  }
234
240
  break;
@@ -245,11 +251,11 @@ ossl_bn_initialize(int argc, VALUE *argv, VALUE self)
245
251
  *
246
252
  * === Parameters
247
253
  * * +base+ - integer
248
- * * * Valid values:
249
- * * * * 0 - MPI
250
- * * * * 2 - binary
251
- * * * * 10 - the default
252
- * * * * 16 - hex
254
+ * Valid values:
255
+ * * 0 - MPI
256
+ * * 2 - binary
257
+ * * 10 - the default
258
+ * * 16 - hex
253
259
  */
254
260
  static VALUE
255
261
  ossl_bn_to_s(int argc, VALUE *argv, VALUE self)
@@ -352,18 +358,21 @@ ossl_bn_coerce(VALUE self, VALUE other)
352
358
 
353
359
  /*
354
360
  * Document-method: OpenSSL::BN#zero?
361
+ * call-seq:
355
362
  * bn.zero? => true | false
356
363
  */
357
364
  BIGNUM_BOOL1(is_zero)
358
365
 
359
366
  /*
360
367
  * Document-method: OpenSSL::BN#one?
368
+ * call-seq:
361
369
  * bn.one? => true | false
362
370
  */
363
371
  BIGNUM_BOOL1(is_one)
364
372
 
365
373
  /*
366
374
  * Document-method: OpenSSL::BN#odd?
375
+ * call-seq:
367
376
  * bn.odd? => true | false
368
377
  */
369
378
  BIGNUM_BOOL1(is_odd)
@@ -375,7 +384,7 @@ BIGNUM_BOOL1(is_odd)
375
384
  BIGNUM *bn, *result; \
376
385
  VALUE obj; \
377
386
  GetBN(self, bn); \
378
- obj = NewBN(CLASS_OF(self)); \
387
+ obj = NewBN(rb_obj_class(self)); \
379
388
  if (!(result = BN_new())) { \
380
389
  ossl_raise(eBNError, NULL); \
381
390
  } \
@@ -389,6 +398,7 @@ BIGNUM_BOOL1(is_odd)
389
398
 
390
399
  /*
391
400
  * Document-method: OpenSSL::BN#sqr
401
+ * call-seq:
392
402
  * bn.sqr => aBN
393
403
  */
394
404
  BIGNUM_1c(sqr)
@@ -400,7 +410,7 @@ BIGNUM_1c(sqr)
400
410
  BIGNUM *bn1, *bn2 = GetBNPtr(other), *result; \
401
411
  VALUE obj; \
402
412
  GetBN(self, bn1); \
403
- obj = NewBN(CLASS_OF(self)); \
413
+ obj = NewBN(rb_obj_class(self)); \
404
414
  if (!(result = BN_new())) { \
405
415
  ossl_raise(eBNError, NULL); \
406
416
  } \
@@ -414,12 +424,14 @@ BIGNUM_1c(sqr)
414
424
 
415
425
  /*
416
426
  * Document-method: OpenSSL::BN#+
427
+ * call-seq:
417
428
  * bn + bn2 => aBN
418
429
  */
419
430
  BIGNUM_2(add)
420
431
 
421
432
  /*
422
433
  * Document-method: OpenSSL::BN#-
434
+ * call-seq:
423
435
  * bn - bn2 => aBN
424
436
  */
425
437
  BIGNUM_2(sub)
@@ -431,7 +443,7 @@ BIGNUM_2(sub)
431
443
  BIGNUM *bn1, *bn2 = GetBNPtr(other), *result; \
432
444
  VALUE obj; \
433
445
  GetBN(self, bn1); \
434
- obj = NewBN(CLASS_OF(self)); \
446
+ obj = NewBN(rb_obj_class(self)); \
435
447
  if (!(result = BN_new())) { \
436
448
  ossl_raise(eBNError, NULL); \
437
449
  } \
@@ -445,42 +457,49 @@ BIGNUM_2(sub)
445
457
 
446
458
  /*
447
459
  * Document-method: OpenSSL::BN#*
460
+ * call-seq:
448
461
  * bn * bn2 => aBN
449
462
  */
450
463
  BIGNUM_2c(mul)
451
464
 
452
465
  /*
453
466
  * Document-method: OpenSSL::BN#%
467
+ * call-seq:
454
468
  * bn % bn2 => aBN
455
469
  */
456
470
  BIGNUM_2c(mod)
457
471
 
458
472
  /*
459
473
  * Document-method: OpenSSL::BN#**
474
+ * call-seq:
460
475
  * bn ** bn2 => aBN
461
476
  */
462
477
  BIGNUM_2c(exp)
463
478
 
464
479
  /*
465
480
  * Document-method: OpenSSL::BN#gcd
481
+ * call-seq:
466
482
  * bn.gcd(bn2) => aBN
467
483
  */
468
484
  BIGNUM_2c(gcd)
469
485
 
470
486
  /*
471
487
  * Document-method: OpenSSL::BN#mod_sqr
488
+ * call-seq:
472
489
  * bn.mod_sqr(bn2) => aBN
473
490
  */
474
491
  BIGNUM_2c(mod_sqr)
475
492
 
476
493
  /*
477
494
  * Document-method: OpenSSL::BN#mod_inverse
495
+ * call-seq:
478
496
  * bn.mod_inverse(bn2) => aBN
479
497
  */
480
498
  BIGNUM_2c(mod_inverse)
481
499
 
482
500
  /*
483
501
  * Document-method: OpenSSL::BN#/
502
+ * call-seq:
484
503
  * bn1 / bn2 => [result, remainder]
485
504
  *
486
505
  * Division of OpenSSL::BN instances
@@ -489,12 +508,13 @@ static VALUE
489
508
  ossl_bn_div(VALUE self, VALUE other)
490
509
  {
491
510
  BIGNUM *bn1, *bn2 = GetBNPtr(other), *r1, *r2;
492
- VALUE obj1, obj2;
511
+ VALUE klass, obj1, obj2;
493
512
 
494
513
  GetBN(self, bn1);
495
514
 
496
- obj1 = NewBN(CLASS_OF(self));
497
- obj2 = NewBN(CLASS_OF(self));
515
+ klass = rb_obj_class(self);
516
+ obj1 = NewBN(klass);
517
+ obj2 = NewBN(klass);
498
518
  if (!(r1 = BN_new())) {
499
519
  ossl_raise(eBNError, NULL);
500
520
  }
@@ -521,7 +541,7 @@ ossl_bn_div(VALUE self, VALUE other)
521
541
  BIGNUM *bn3 = GetBNPtr(other2), *result; \
522
542
  VALUE obj; \
523
543
  GetBN(self, bn1); \
524
- obj = NewBN(CLASS_OF(self)); \
544
+ obj = NewBN(rb_obj_class(self)); \
525
545
  if (!(result = BN_new())) { \
526
546
  ossl_raise(eBNError, NULL); \
527
547
  } \
@@ -535,24 +555,28 @@ ossl_bn_div(VALUE self, VALUE other)
535
555
 
536
556
  /*
537
557
  * Document-method: OpenSSL::BN#mod_add
558
+ * call-seq:
538
559
  * bn.mod_add(bn1, bn2) -> aBN
539
560
  */
540
561
  BIGNUM_3c(mod_add)
541
562
 
542
563
  /*
543
564
  * Document-method: OpenSSL::BN#mod_sub
565
+ * call-seq:
544
566
  * bn.mod_sub(bn1, bn2) -> aBN
545
567
  */
546
568
  BIGNUM_3c(mod_sub)
547
569
 
548
570
  /*
549
571
  * Document-method: OpenSSL::BN#mod_mul
572
+ * call-seq:
550
573
  * bn.mod_mul(bn1, bn2) -> aBN
551
574
  */
552
575
  BIGNUM_3c(mod_mul)
553
576
 
554
577
  /*
555
578
  * Document-method: OpenSSL::BN#mod_exp
579
+ * call-seq:
556
580
  * bn.mod_exp(bn1, bn2) -> aBN
557
581
  */
558
582
  BIGNUM_3c(mod_exp)
@@ -571,29 +595,31 @@ BIGNUM_3c(mod_exp)
571
595
 
572
596
  /*
573
597
  * Document-method: OpenSSL::BN#set_bit!
598
+ * call-seq:
574
599
  * bn.set_bit!(bit) -> self
575
600
  */
576
601
  BIGNUM_BIT(set_bit)
577
602
 
578
603
  /*
579
604
  * Document-method: OpenSSL::BN#clear_bit!
605
+ * call-seq:
580
606
  * bn.clear_bit!(bit) -> self
581
607
  */
582
608
  BIGNUM_BIT(clear_bit)
583
609
 
584
610
  /*
585
611
  * Document-method: OpenSSL::BN#mask_bit!
612
+ * call-seq:
586
613
  * bn.mask_bit!(bit) -> self
587
614
  */
588
615
  BIGNUM_BIT(mask_bits)
589
616
 
590
617
  /* Document-method: OpenSSL::BN#bit_set?
618
+ * call-seq:
619
+ * bn.bit_set?(bit) => true | false
591
620
  *
592
621
  * Returns boolean of whether +bit+ is set.
593
622
  * Bitwise operations for openssl BIGNUMs.
594
- *
595
- * bn.bit_set?(bit) => true | false
596
- *
597
623
  */
598
624
  static VALUE
599
625
  ossl_bn_is_bit_set(VALUE self, VALUE bit)
@@ -618,7 +644,7 @@ ossl_bn_is_bit_set(VALUE self, VALUE bit)
618
644
  VALUE obj; \
619
645
  b = NUM2INT(bits); \
620
646
  GetBN(self, bn); \
621
- obj = NewBN(CLASS_OF(self)); \
647
+ obj = NewBN(rb_obj_class(self)); \
622
648
  if (!(result = BN_new())) { \
623
649
  ossl_raise(eBNError, NULL); \
624
650
  } \
@@ -659,12 +685,14 @@ BIGNUM_SHIFT(rshift)
659
685
 
660
686
  /*
661
687
  * Document-method: OpenSSL::BN#lshift!
688
+ * call-seq:
662
689
  * bn.lshift!(bits) -> self
663
690
  */
664
691
  BIGNUM_SELF_SHIFT(lshift)
665
692
 
666
693
  /*
667
694
  * Document-method: OpenSSL::BN#rshift!
695
+ * call-seq:
668
696
  * bn.rshift!(bits) -> self
669
697
  */
670
698
  BIGNUM_SELF_SHIFT(rshift)
@@ -728,6 +756,7 @@ BIGNUM_RAND(pseudo_rand)
728
756
 
729
757
  /*
730
758
  * Document-method: OpenSSL::BN.rand_range
759
+ * call-seq:
731
760
  * BN.rand_range(range) -> aBN
732
761
  *
733
762
  */
@@ -735,6 +764,7 @@ BIGNUM_RAND_RANGE(rand)
735
764
 
736
765
  /*
737
766
  * Document-method: OpenSSL::BN.pseudo_rand_range
767
+ * call-seq:
738
768
  * BN.pseudo_rand_range(range) -> aBN
739
769
  *
740
770
  */
@@ -744,6 +774,10 @@ BIGNUM_RAND_RANGE(pseudo_rand)
744
774
  * call-seq:
745
775
  * BN.generate_prime(bits, [, safe [, add [, rem]]]) => bn
746
776
  *
777
+ * Generates a random prime number of bit length +bits+. If +safe+ is true,
778
+ * generates a safe prime. If +add+ is specified, generates a prime that
779
+ * fulfills condition <tt>p % add = rem</tt>.
780
+ *
747
781
  * === Parameters
748
782
  * * +bits+ - integer
749
783
  * * +safe+ - boolean
@@ -772,7 +806,7 @@ ossl_bn_s_generate_prime(int argc, VALUE *argv, VALUE klass)
772
806
  if (!(result = BN_new())) {
773
807
  ossl_raise(eBNError, NULL);
774
808
  }
775
- if (!BN_generate_prime(result, num, safe, add, rem, NULL, NULL)) {
809
+ if (!BN_generate_prime_ex(result, num, safe, add, rem, NULL)) {
776
810
  BN_free(result);
777
811
  ossl_raise(eBNError, NULL);
778
812
  }
@@ -787,17 +821,19 @@ ossl_bn_s_generate_prime(int argc, VALUE *argv, VALUE klass)
787
821
  { \
788
822
  BIGNUM *bn; \
789
823
  GetBN(self, bn); \
790
- return INT2FIX(BN_##func(bn)); \
824
+ return INT2NUM(BN_##func(bn)); \
791
825
  }
792
826
 
793
827
  /*
794
828
  * Document-method: OpenSSL::BN#num_bytes
829
+ * call-seq:
795
830
  * bn.num_bytes => integer
796
831
  */
797
832
  BIGNUM_NUM(num_bytes)
798
833
 
799
834
  /*
800
835
  * Document-method: OpenSSL::BN#num_bits
836
+ * call-seq:
801
837
  * bn.num_bits => integer
802
838
  */
803
839
  BIGNUM_NUM(num_bits)
@@ -826,21 +862,24 @@ ossl_bn_copy(VALUE self, VALUE other)
826
862
  { \
827
863
  BIGNUM *bn1, *bn2 = GetBNPtr(other); \
828
864
  GetBN(self, bn1); \
829
- return INT2FIX(BN_##func(bn1, bn2)); \
865
+ return INT2NUM(BN_##func(bn1, bn2)); \
830
866
  }
831
867
 
832
868
  /*
833
869
  * Document-method: OpenSSL::BN#cmp
870
+ * call-seq:
834
871
  * bn.cmp(bn2) => integer
835
872
  */
836
873
  /*
837
874
  * Document-method: OpenSSL::BN#<=>
875
+ * call-seq:
838
876
  * bn <=> bn2 => integer
839
877
  */
840
878
  BIGNUM_CMP(cmp)
841
879
 
842
880
  /*
843
881
  * Document-method: OpenSSL::BN#ucmp
882
+ * call-seq:
844
883
  * bn.ucmp(bn2) => integer
845
884
  */
846
885
  BIGNUM_CMP(ucmp)
@@ -858,10 +897,12 @@ ossl_bn_eq(VALUE self, VALUE other)
858
897
  BIGNUM *bn1, *bn2;
859
898
 
860
899
  GetBN(self, bn1);
861
- /* BNPtr may raise, so we can't use here */
862
- bn2 = try_convert_to_bnptr(other);
900
+ other = try_convert_to_bn(other);
901
+ if (NIL_P(other))
902
+ return Qfalse;
903
+ GetBN(other, bn2);
863
904
 
864
- if (bn2 && !BN_cmp(bn1, bn2)) {
905
+ if (!BN_cmp(bn1, bn2)) {
865
906
  return Qtrue;
866
907
  }
867
908
  return Qfalse;
@@ -912,7 +953,7 @@ ossl_bn_hash(VALUE self)
912
953
  ossl_raise(eBNError, NULL);
913
954
  }
914
955
 
915
- hash = INT2FIX(rb_memhash(buf, len));
956
+ hash = ST2FIX(rb_memhash(buf, len));
916
957
  xfree(buf);
917
958
 
918
959
  return hash;
@@ -923,6 +964,10 @@ ossl_bn_hash(VALUE self)
923
964
  * bn.prime? => true | false
924
965
  * bn.prime?(checks) => true | false
925
966
  *
967
+ * Performs a Miller-Rabin probabilistic primality test with +checks+
968
+ * iterations. If +nchecks+ is not specified, a number of iterations is used
969
+ * that yields a false positive rate of at most 2^-80 for random input.
970
+ *
926
971
  * === Parameters
927
972
  * * +checks+ - integer
928
973
  */
@@ -937,7 +982,7 @@ ossl_bn_is_prime(int argc, VALUE *argv, VALUE self)
937
982
  checks = NUM2INT(vchecks);
938
983
  }
939
984
  GetBN(self, bn);
940
- switch (BN_is_prime(bn, checks, NULL, ossl_bn_ctx, NULL)) {
985
+ switch (BN_is_prime_ex(bn, checks, ossl_bn_ctx, NULL)) {
941
986
  case 1:
942
987
  return Qtrue;
943
988
  case 0:
@@ -955,6 +1000,9 @@ ossl_bn_is_prime(int argc, VALUE *argv, VALUE self)
955
1000
  * bn.prime_fasttest?(checks) => true | false
956
1001
  * bn.prime_fasttest?(checks, trial_div) => true | false
957
1002
  *
1003
+ * Performs a Miller-Rabin primality test. This is same as #prime? except this
1004
+ * first attempts trial divisions with some small primes.
1005
+ *
958
1006
  * === Parameters
959
1007
  * * +checks+ - integer
960
1008
  * * +trial_div+ - boolean
@@ -976,7 +1024,7 @@ ossl_bn_is_prime_fasttest(int argc, VALUE *argv, VALUE self)
976
1024
  if (vtrivdiv == Qfalse) {
977
1025
  do_trial_division = 0;
978
1026
  }
979
- switch (BN_is_prime_fasttest(bn, checks, NULL, ossl_bn_ctx, NULL, do_trial_division)) {
1027
+ switch (BN_is_prime_fasttest_ex(bn, checks, ossl_bn_ctx, do_trial_division, NULL)) {
980
1028
  case 1:
981
1029
  return Qtrue;
982
1030
  case 0:
@@ -996,7 +1044,8 @@ void
996
1044
  Init_ossl_bn(void)
997
1045
  {
998
1046
  #if 0
999
- mOSSL = rb_define_module("OpenSSL"); /* let rdoc know about mOSSL */
1047
+ mOSSL = rb_define_module("OpenSSL");
1048
+ eOSSLError = rb_define_class_under(mOSSL, "OpenSSLError", rb_eStandardError);
1000
1049
  #endif
1001
1050
 
1002
1051
  if (!(ossl_bn_ctx = BN_CTX_new())) {
@@ -1066,6 +1115,7 @@ Init_ossl_bn(void)
1066
1115
 
1067
1116
  rb_define_singleton_method(cBN, "generate_prime", ossl_bn_s_generate_prime, -1);
1068
1117
  rb_define_method(cBN, "prime?", ossl_bn_is_prime, -1);
1118
+ rb_define_method(cBN, "prime_fasttest?", ossl_bn_is_prime_fasttest, -1);
1069
1119
 
1070
1120
  rb_define_method(cBN, "set_bit!", ossl_bn_set_bit, 1);
1071
1121
  rb_define_method(cBN, "clear_bit!", ossl_bn_clear_bit, 1);
@@ -1107,10 +1157,4 @@ Init_ossl_bn(void)
1107
1157
 
1108
1158
  /* RECiProcal
1109
1159
  * MONTgomery */
1110
-
1111
- /*
1112
- * TODO:
1113
- * Where to belong these?
1114
- */
1115
- rb_define_method(cBN, "prime_fasttest?", ossl_bn_is_prime_fasttest, -1);
1116
1160
  }