openssl 3.3.2 → 4.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CONTRIBUTING.md +3 -0
- data/History.md +85 -0
- data/README.md +12 -11
- data/ext/openssl/extconf.rb +30 -69
- data/ext/openssl/openssl_missing.h +0 -206
- data/ext/openssl/ossl.c +280 -301
- data/ext/openssl/ossl.h +15 -10
- data/ext/openssl/ossl_asn1.c +598 -406
- data/ext/openssl/ossl_asn1.h +15 -1
- data/ext/openssl/ossl_bio.c +3 -3
- data/ext/openssl/ossl_bn.c +286 -291
- data/ext/openssl/ossl_cipher.c +252 -203
- data/ext/openssl/ossl_cipher.h +10 -1
- data/ext/openssl/ossl_config.c +1 -6
- data/ext/openssl/ossl_digest.c +74 -43
- data/ext/openssl/ossl_digest.h +9 -1
- data/ext/openssl/ossl_engine.c +39 -103
- data/ext/openssl/ossl_hmac.c +30 -36
- data/ext/openssl/ossl_kdf.c +42 -53
- data/ext/openssl/ossl_ns_spki.c +31 -37
- data/ext/openssl/ossl_ocsp.c +214 -241
- data/ext/openssl/ossl_pkcs12.c +26 -26
- data/ext/openssl/ossl_pkcs7.c +175 -145
- data/ext/openssl/ossl_pkey.c +162 -178
- data/ext/openssl/ossl_pkey.h +99 -99
- data/ext/openssl/ossl_pkey_dh.c +31 -68
- data/ext/openssl/ossl_pkey_dsa.c +15 -54
- data/ext/openssl/ossl_pkey_ec.c +179 -237
- data/ext/openssl/ossl_pkey_rsa.c +56 -103
- data/ext/openssl/ossl_provider.c +0 -7
- data/ext/openssl/ossl_rand.c +7 -14
- data/ext/openssl/ossl_ssl.c +478 -353
- data/ext/openssl/ossl_ssl.h +8 -8
- data/ext/openssl/ossl_ssl_session.c +93 -97
- data/ext/openssl/ossl_ts.c +81 -127
- data/ext/openssl/ossl_x509.c +9 -28
- data/ext/openssl/ossl_x509attr.c +33 -54
- data/ext/openssl/ossl_x509cert.c +69 -100
- data/ext/openssl/ossl_x509crl.c +78 -89
- data/ext/openssl/ossl_x509ext.c +45 -66
- data/ext/openssl/ossl_x509name.c +63 -88
- data/ext/openssl/ossl_x509req.c +55 -62
- data/ext/openssl/ossl_x509revoked.c +27 -41
- data/ext/openssl/ossl_x509store.c +38 -56
- data/lib/openssl/buffering.rb +30 -24
- data/lib/openssl/digest.rb +1 -1
- data/lib/openssl/pkey.rb +71 -49
- data/lib/openssl/ssl.rb +12 -79
- data/lib/openssl/version.rb +2 -1
- data/lib/openssl/x509.rb +9 -0
- data/lib/openssl.rb +9 -6
- metadata +1 -3
- data/ext/openssl/openssl_missing.c +0 -40
- data/lib/openssl/asn1.rb +0 -188
data/ext/openssl/ossl_bn.c
CHANGED
|
@@ -11,19 +11,19 @@
|
|
|
11
11
|
#include "ossl.h"
|
|
12
12
|
|
|
13
13
|
#define NewBN(klass) \
|
|
14
|
-
|
|
14
|
+
TypedData_Wrap_Struct((klass), &ossl_bn_type, 0)
|
|
15
15
|
#define SetBN(obj, bn) do { \
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
16
|
+
if (!(bn)) { \
|
|
17
|
+
ossl_raise(rb_eRuntimeError, "BN wasn't initialized!"); \
|
|
18
|
+
} \
|
|
19
|
+
RTYPEDDATA_DATA(obj) = (bn); \
|
|
20
20
|
} while (0)
|
|
21
21
|
|
|
22
22
|
#define GetBN(obj, bn) do { \
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
23
|
+
TypedData_Get_Struct((obj), BIGNUM, &ossl_bn_type, (bn)); \
|
|
24
|
+
if (!(bn)) { \
|
|
25
|
+
ossl_raise(rb_eRuntimeError, "BN wasn't initialized!"); \
|
|
26
|
+
} \
|
|
27
27
|
} while (0)
|
|
28
28
|
|
|
29
29
|
static void
|
|
@@ -35,7 +35,7 @@ ossl_bn_free(void *ptr)
|
|
|
35
35
|
static const rb_data_type_t ossl_bn_type = {
|
|
36
36
|
"OpenSSL/BN",
|
|
37
37
|
{
|
|
38
|
-
|
|
38
|
+
0, ossl_bn_free,
|
|
39
39
|
},
|
|
40
40
|
0, 0, RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_WB_PROTECTED | RUBY_TYPED_FROZEN_SHAREABLE,
|
|
41
41
|
};
|
|
@@ -61,10 +61,9 @@ ossl_bn_new(const BIGNUM *bn)
|
|
|
61
61
|
VALUE obj;
|
|
62
62
|
|
|
63
63
|
obj = NewBN(cBN);
|
|
64
|
-
newbn =
|
|
65
|
-
if (!newbn)
|
|
66
|
-
|
|
67
|
-
}
|
|
64
|
+
newbn = BN_dup(bn);
|
|
65
|
+
if (!newbn)
|
|
66
|
+
ossl_raise(eBNError, "BN_dup");
|
|
68
67
|
SetBN(obj, newbn);
|
|
69
68
|
|
|
70
69
|
return obj;
|
|
@@ -76,40 +75,40 @@ integer_to_bnptr(VALUE obj, BIGNUM *orig)
|
|
|
76
75
|
BIGNUM *bn;
|
|
77
76
|
|
|
78
77
|
if (FIXNUM_P(obj)) {
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
78
|
+
long i;
|
|
79
|
+
unsigned char bin[sizeof(long)];
|
|
80
|
+
long n = FIX2LONG(obj);
|
|
81
|
+
unsigned long un = labs(n);
|
|
82
|
+
|
|
83
|
+
for (i = sizeof(long) - 1; 0 <= i; i--) {
|
|
84
|
+
bin[i] = un & 0xff;
|
|
85
|
+
un >>= 8;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
bn = BN_bin2bn(bin, sizeof(bin), orig);
|
|
89
|
+
if (!bn)
|
|
90
|
+
ossl_raise(eBNError, "BN_bin2bn");
|
|
91
|
+
if (n < 0)
|
|
92
|
+
BN_set_negative(bn, 1);
|
|
94
93
|
}
|
|
95
94
|
else { /* assuming Bignum */
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
95
|
+
size_t len = rb_absint_size(obj, NULL);
|
|
96
|
+
unsigned char *bin;
|
|
97
|
+
VALUE buf;
|
|
98
|
+
int sign;
|
|
99
|
+
|
|
100
|
+
if (INT_MAX < len) {
|
|
101
|
+
rb_raise(eBNError, "bignum too long");
|
|
102
|
+
}
|
|
103
|
+
bin = (unsigned char*)ALLOCV_N(unsigned char, buf, len);
|
|
104
|
+
sign = rb_integer_pack(obj, bin, len, 1, 0, INTEGER_PACK_BIG_ENDIAN);
|
|
105
|
+
|
|
106
|
+
bn = BN_bin2bn(bin, (int)len, orig);
|
|
107
|
+
ALLOCV_END(buf);
|
|
108
|
+
if (!bn)
|
|
109
|
+
ossl_raise(eBNError, "BN_bin2bn");
|
|
110
|
+
if (sign < 0)
|
|
111
|
+
BN_set_negative(bn, 1);
|
|
113
112
|
}
|
|
114
113
|
|
|
115
114
|
return bn;
|
|
@@ -122,11 +121,11 @@ try_convert_to_bn(VALUE obj)
|
|
|
122
121
|
VALUE newobj = Qnil;
|
|
123
122
|
|
|
124
123
|
if (rb_obj_is_kind_of(obj, cBN))
|
|
125
|
-
|
|
124
|
+
return obj;
|
|
126
125
|
if (RB_INTEGER_TYPE_P(obj)) {
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
126
|
+
newobj = NewBN(cBN); /* Handle potential mem leaks */
|
|
127
|
+
bn = integer_to_bnptr(obj, NULL);
|
|
128
|
+
SetBN(newobj, bn);
|
|
130
129
|
}
|
|
131
130
|
|
|
132
131
|
return newobj;
|
|
@@ -140,7 +139,7 @@ ossl_bn_value_ptr(volatile VALUE *ptr)
|
|
|
140
139
|
|
|
141
140
|
tmp = try_convert_to_bn(*ptr);
|
|
142
141
|
if (NIL_P(tmp))
|
|
143
|
-
|
|
142
|
+
ossl_raise(rb_eTypeError, "Cannot convert into OpenSSL::BN");
|
|
144
143
|
GetBN(tmp, bn);
|
|
145
144
|
*ptr = tmp;
|
|
146
145
|
|
|
@@ -210,7 +209,7 @@ ossl_bn_alloc(VALUE klass)
|
|
|
210
209
|
VALUE obj = NewBN(klass);
|
|
211
210
|
|
|
212
211
|
if (!(bn = BN_new())) {
|
|
213
|
-
|
|
212
|
+
ossl_raise(eBNError, NULL);
|
|
214
213
|
}
|
|
215
214
|
SetBN(obj, bn);
|
|
216
215
|
|
|
@@ -252,7 +251,7 @@ ossl_bn_initialize(int argc, VALUE *argv, VALUE self)
|
|
|
252
251
|
char *ptr;
|
|
253
252
|
|
|
254
253
|
if (rb_scan_args(argc, argv, "11", &str, &bs) == 2) {
|
|
255
|
-
|
|
254
|
+
base = NUM2INT(bs);
|
|
256
255
|
}
|
|
257
256
|
|
|
258
257
|
if (NIL_P(str)) {
|
|
@@ -261,49 +260,49 @@ ossl_bn_initialize(int argc, VALUE *argv, VALUE self)
|
|
|
261
260
|
|
|
262
261
|
rb_check_frozen(self);
|
|
263
262
|
if (RB_INTEGER_TYPE_P(str)) {
|
|
264
|
-
|
|
265
|
-
|
|
263
|
+
GetBN(self, bn);
|
|
264
|
+
integer_to_bnptr(str, bn);
|
|
266
265
|
|
|
267
|
-
|
|
266
|
+
return self;
|
|
268
267
|
}
|
|
269
268
|
|
|
270
269
|
if (RTEST(rb_obj_is_kind_of(str, cBN))) {
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
270
|
+
BIGNUM *other;
|
|
271
|
+
|
|
272
|
+
GetBN(self, bn);
|
|
273
|
+
GetBN(str, other); /* Safe - we checked kind_of? above */
|
|
274
|
+
if (!BN_copy(bn, other)) {
|
|
275
|
+
ossl_raise(eBNError, NULL);
|
|
276
|
+
}
|
|
277
|
+
return self;
|
|
279
278
|
}
|
|
280
279
|
|
|
281
280
|
GetBN(self, bn);
|
|
282
281
|
switch (base) {
|
|
283
|
-
|
|
282
|
+
case 0:
|
|
284
283
|
ptr = StringValuePtr(str);
|
|
285
284
|
if (!BN_mpi2bn((unsigned char *)ptr, RSTRING_LENINT(str), bn)) {
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
285
|
+
ossl_raise(eBNError, NULL);
|
|
286
|
+
}
|
|
287
|
+
break;
|
|
288
|
+
case 2:
|
|
290
289
|
ptr = StringValuePtr(str);
|
|
291
290
|
if (!BN_bin2bn((unsigned char *)ptr, RSTRING_LENINT(str), bn)) {
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
291
|
+
ossl_raise(eBNError, NULL);
|
|
292
|
+
}
|
|
293
|
+
break;
|
|
294
|
+
case 10:
|
|
295
|
+
if (!BN_dec2bn(&bn, StringValueCStr(str))) {
|
|
296
|
+
ossl_raise(eBNError, NULL);
|
|
297
|
+
}
|
|
298
|
+
break;
|
|
299
|
+
case 16:
|
|
300
|
+
if (!BN_hex2bn(&bn, StringValueCStr(str))) {
|
|
301
|
+
ossl_raise(eBNError, NULL);
|
|
302
|
+
}
|
|
303
|
+
break;
|
|
304
|
+
default:
|
|
305
|
+
ossl_raise(rb_eArgError, "invalid radix %d", base);
|
|
307
306
|
}
|
|
308
307
|
return self;
|
|
309
308
|
}
|
|
@@ -335,32 +334,32 @@ ossl_bn_to_s(int argc, VALUE *argv, VALUE self)
|
|
|
335
334
|
char *buf;
|
|
336
335
|
|
|
337
336
|
if (rb_scan_args(argc, argv, "01", &bs) == 1) {
|
|
338
|
-
|
|
337
|
+
base = NUM2INT(bs);
|
|
339
338
|
}
|
|
340
339
|
GetBN(self, bn);
|
|
341
340
|
switch (base) {
|
|
342
|
-
|
|
343
|
-
|
|
341
|
+
case 0:
|
|
342
|
+
len = BN_bn2mpi(bn, NULL);
|
|
344
343
|
str = rb_str_new(0, len);
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
344
|
+
if (BN_bn2mpi(bn, (unsigned char *)RSTRING_PTR(str)) != len)
|
|
345
|
+
ossl_raise(eBNError, NULL);
|
|
346
|
+
break;
|
|
347
|
+
case 2:
|
|
348
|
+
len = BN_num_bytes(bn);
|
|
350
349
|
str = rb_str_new(0, len);
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
350
|
+
if (BN_bn2bin(bn, (unsigned char *)RSTRING_PTR(str)) != len)
|
|
351
|
+
ossl_raise(eBNError, NULL);
|
|
352
|
+
break;
|
|
353
|
+
case 10:
|
|
354
|
+
if (!(buf = BN_bn2dec(bn))) ossl_raise(eBNError, NULL);
|
|
355
|
+
str = ossl_buf2str(buf, rb_long2int(strlen(buf)));
|
|
356
|
+
break;
|
|
357
|
+
case 16:
|
|
358
|
+
if (!(buf = BN_bn2hex(bn))) ossl_raise(eBNError, NULL);
|
|
359
|
+
str = ossl_buf2str(buf, rb_long2int(strlen(buf)));
|
|
360
|
+
break;
|
|
361
|
+
default:
|
|
362
|
+
ossl_raise(rb_eArgError, "invalid radix %d", base);
|
|
364
363
|
}
|
|
365
364
|
|
|
366
365
|
return str;
|
|
@@ -380,7 +379,7 @@ ossl_bn_to_i(VALUE self)
|
|
|
380
379
|
GetBN(self, bn);
|
|
381
380
|
|
|
382
381
|
if (!(txt = BN_bn2hex(bn))) {
|
|
383
|
-
|
|
382
|
+
ossl_raise(eBNError, NULL);
|
|
384
383
|
}
|
|
385
384
|
num = rb_cstr_to_inum(txt, 16, Qtrue);
|
|
386
385
|
OPENSSL_free(txt);
|
|
@@ -398,31 +397,31 @@ static VALUE
|
|
|
398
397
|
ossl_bn_coerce(VALUE self, VALUE other)
|
|
399
398
|
{
|
|
400
399
|
switch(TYPE(other)) {
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
400
|
+
case T_STRING:
|
|
401
|
+
self = ossl_bn_to_s(0, NULL, self);
|
|
402
|
+
break;
|
|
403
|
+
case T_FIXNUM:
|
|
404
|
+
case T_BIGNUM:
|
|
405
|
+
self = ossl_bn_to_i(self);
|
|
406
|
+
break;
|
|
407
|
+
default:
|
|
408
|
+
if (!RTEST(rb_obj_is_kind_of(other, cBN))) {
|
|
409
|
+
ossl_raise(rb_eTypeError, "Don't know how to coerce");
|
|
410
|
+
}
|
|
412
411
|
}
|
|
413
412
|
return rb_assoc_new(other, self);
|
|
414
413
|
}
|
|
415
414
|
|
|
416
|
-
#define BIGNUM_BOOL1(func)
|
|
417
|
-
static VALUE
|
|
418
|
-
ossl_bn_##func(VALUE self)
|
|
419
|
-
{
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
415
|
+
#define BIGNUM_BOOL1(func) \
|
|
416
|
+
static VALUE \
|
|
417
|
+
ossl_bn_##func(VALUE self) \
|
|
418
|
+
{ \
|
|
419
|
+
BIGNUM *bn; \
|
|
420
|
+
GetBN(self, bn); \
|
|
421
|
+
if (BN_##func(bn)) { \
|
|
422
|
+
return Qtrue; \
|
|
423
|
+
} \
|
|
424
|
+
return Qfalse; \
|
|
426
425
|
}
|
|
427
426
|
|
|
428
427
|
/*
|
|
@@ -457,27 +456,27 @@ ossl_bn_is_negative(VALUE self)
|
|
|
457
456
|
|
|
458
457
|
GetBN(self, bn);
|
|
459
458
|
if (BN_is_zero(bn))
|
|
460
|
-
|
|
459
|
+
return Qfalse;
|
|
461
460
|
return BN_is_negative(bn) ? Qtrue : Qfalse;
|
|
462
461
|
}
|
|
463
462
|
|
|
464
|
-
#define BIGNUM_1c(func)
|
|
465
|
-
static VALUE
|
|
466
|
-
ossl_bn_##func(VALUE self)
|
|
467
|
-
{
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
463
|
+
#define BIGNUM_1c(func) \
|
|
464
|
+
static VALUE \
|
|
465
|
+
ossl_bn_##func(VALUE self) \
|
|
466
|
+
{ \
|
|
467
|
+
BIGNUM *bn, *result; \
|
|
468
|
+
VALUE obj; \
|
|
469
|
+
GetBN(self, bn); \
|
|
470
|
+
obj = NewBN(rb_obj_class(self)); \
|
|
471
|
+
if (!(result = BN_new())) { \
|
|
472
|
+
ossl_raise(eBNError, NULL); \
|
|
473
|
+
} \
|
|
474
|
+
if (BN_##func(result, bn, ossl_bn_ctx) <= 0) { \
|
|
475
|
+
BN_free(result); \
|
|
476
|
+
ossl_raise(eBNError, NULL); \
|
|
477
|
+
} \
|
|
478
|
+
SetBN(obj, result); \
|
|
479
|
+
return obj; \
|
|
481
480
|
}
|
|
482
481
|
|
|
483
482
|
/*
|
|
@@ -487,23 +486,23 @@ ossl_bn_is_negative(VALUE self)
|
|
|
487
486
|
*/
|
|
488
487
|
BIGNUM_1c(sqr)
|
|
489
488
|
|
|
490
|
-
#define BIGNUM_2(func)
|
|
491
|
-
static VALUE
|
|
492
|
-
ossl_bn_##func(VALUE self, VALUE other)
|
|
493
|
-
{
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
489
|
+
#define BIGNUM_2(func) \
|
|
490
|
+
static VALUE \
|
|
491
|
+
ossl_bn_##func(VALUE self, VALUE other) \
|
|
492
|
+
{ \
|
|
493
|
+
BIGNUM *bn1, *bn2 = GetBNPtr(other), *result; \
|
|
494
|
+
VALUE obj; \
|
|
495
|
+
GetBN(self, bn1); \
|
|
496
|
+
obj = NewBN(rb_obj_class(self)); \
|
|
497
|
+
if (!(result = BN_new())) { \
|
|
498
|
+
ossl_raise(eBNError, NULL); \
|
|
499
|
+
} \
|
|
500
|
+
if (BN_##func(result, bn1, bn2) <= 0) { \
|
|
501
|
+
BN_free(result); \
|
|
502
|
+
ossl_raise(eBNError, NULL); \
|
|
503
|
+
} \
|
|
504
|
+
SetBN(obj, result); \
|
|
505
|
+
return obj; \
|
|
507
506
|
}
|
|
508
507
|
|
|
509
508
|
/*
|
|
@@ -520,23 +519,23 @@ BIGNUM_2(add)
|
|
|
520
519
|
*/
|
|
521
520
|
BIGNUM_2(sub)
|
|
522
521
|
|
|
523
|
-
#define BIGNUM_2c(func)
|
|
524
|
-
static VALUE
|
|
525
|
-
ossl_bn_##func(VALUE self, VALUE other)
|
|
526
|
-
{
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
522
|
+
#define BIGNUM_2c(func) \
|
|
523
|
+
static VALUE \
|
|
524
|
+
ossl_bn_##func(VALUE self, VALUE other) \
|
|
525
|
+
{ \
|
|
526
|
+
BIGNUM *bn1, *bn2 = GetBNPtr(other), *result; \
|
|
527
|
+
VALUE obj; \
|
|
528
|
+
GetBN(self, bn1); \
|
|
529
|
+
obj = NewBN(rb_obj_class(self)); \
|
|
530
|
+
if (!(result = BN_new())) { \
|
|
531
|
+
ossl_raise(eBNError, NULL); \
|
|
532
|
+
} \
|
|
533
|
+
if (BN_##func(result, bn1, bn2, ossl_bn_ctx) <= 0) { \
|
|
534
|
+
BN_free(result); \
|
|
535
|
+
ossl_raise(eBNError, NULL); \
|
|
536
|
+
} \
|
|
537
|
+
SetBN(obj, result); \
|
|
538
|
+
return obj; \
|
|
540
539
|
}
|
|
541
540
|
|
|
542
541
|
/*
|
|
@@ -574,18 +573,18 @@ BIGNUM_2c(gcd)
|
|
|
574
573
|
*/
|
|
575
574
|
BIGNUM_2c(mod_sqr)
|
|
576
575
|
|
|
577
|
-
#define BIGNUM_2cr(func)
|
|
578
|
-
static VALUE
|
|
579
|
-
ossl_bn_##func(VALUE self, VALUE other)
|
|
580
|
-
{
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
576
|
+
#define BIGNUM_2cr(func) \
|
|
577
|
+
static VALUE \
|
|
578
|
+
ossl_bn_##func(VALUE self, VALUE other) \
|
|
579
|
+
{ \
|
|
580
|
+
BIGNUM *bn1, *bn2 = GetBNPtr(other), *result; \
|
|
581
|
+
VALUE obj; \
|
|
582
|
+
GetBN(self, bn1); \
|
|
583
|
+
obj = NewBN(rb_obj_class(self)); \
|
|
584
|
+
if (!(result = BN_##func(NULL, bn1, bn2, ossl_bn_ctx))) \
|
|
585
|
+
ossl_raise(eBNError, NULL); \
|
|
586
|
+
SetBN(obj, result); \
|
|
587
|
+
return obj; \
|
|
589
588
|
}
|
|
590
589
|
|
|
591
590
|
/*
|
|
@@ -620,16 +619,16 @@ ossl_bn_div(VALUE self, VALUE other)
|
|
|
620
619
|
obj1 = NewBN(klass);
|
|
621
620
|
obj2 = NewBN(klass);
|
|
622
621
|
if (!(r1 = BN_new())) {
|
|
623
|
-
|
|
622
|
+
ossl_raise(eBNError, NULL);
|
|
624
623
|
}
|
|
625
624
|
if (!(r2 = BN_new())) {
|
|
626
|
-
|
|
627
|
-
|
|
625
|
+
BN_free(r1);
|
|
626
|
+
ossl_raise(eBNError, NULL);
|
|
628
627
|
}
|
|
629
628
|
if (!BN_div(r1, r2, bn1, bn2, ossl_bn_ctx)) {
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
629
|
+
BN_free(r1);
|
|
630
|
+
BN_free(r2);
|
|
631
|
+
ossl_raise(eBNError, NULL);
|
|
633
632
|
}
|
|
634
633
|
SetBN(obj1, r1);
|
|
635
634
|
SetBN(obj2, r2);
|
|
@@ -637,24 +636,24 @@ ossl_bn_div(VALUE self, VALUE other)
|
|
|
637
636
|
return rb_ary_new3(2, obj1, obj2);
|
|
638
637
|
}
|
|
639
638
|
|
|
640
|
-
#define BIGNUM_3c(func)
|
|
641
|
-
static VALUE
|
|
642
|
-
ossl_bn_##func(VALUE self, VALUE other1, VALUE other2)
|
|
643
|
-
{
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
639
|
+
#define BIGNUM_3c(func) \
|
|
640
|
+
static VALUE \
|
|
641
|
+
ossl_bn_##func(VALUE self, VALUE other1, VALUE other2) \
|
|
642
|
+
{ \
|
|
643
|
+
BIGNUM *bn1, *bn2 = GetBNPtr(other1); \
|
|
644
|
+
BIGNUM *bn3 = GetBNPtr(other2), *result; \
|
|
645
|
+
VALUE obj; \
|
|
646
|
+
GetBN(self, bn1); \
|
|
647
|
+
obj = NewBN(rb_obj_class(self)); \
|
|
648
|
+
if (!(result = BN_new())) { \
|
|
649
|
+
ossl_raise(eBNError, NULL); \
|
|
650
|
+
} \
|
|
651
|
+
if (BN_##func(result, bn1, bn2, bn3, ossl_bn_ctx) <= 0) { \
|
|
652
|
+
BN_free(result); \
|
|
653
|
+
ossl_raise(eBNError, NULL); \
|
|
654
|
+
} \
|
|
655
|
+
SetBN(obj, result); \
|
|
656
|
+
return obj; \
|
|
658
657
|
}
|
|
659
658
|
|
|
660
659
|
/*
|
|
@@ -685,17 +684,17 @@ BIGNUM_3c(mod_mul)
|
|
|
685
684
|
*/
|
|
686
685
|
BIGNUM_3c(mod_exp)
|
|
687
686
|
|
|
688
|
-
#define BIGNUM_BIT(func)
|
|
689
|
-
static VALUE
|
|
690
|
-
ossl_bn_##func(VALUE self, VALUE bit)
|
|
691
|
-
{
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
687
|
+
#define BIGNUM_BIT(func) \
|
|
688
|
+
static VALUE \
|
|
689
|
+
ossl_bn_##func(VALUE self, VALUE bit) \
|
|
690
|
+
{ \
|
|
691
|
+
BIGNUM *bn; \
|
|
692
|
+
rb_check_frozen(self); \
|
|
693
|
+
GetBN(self, bn); \
|
|
694
|
+
if (BN_##func(bn, NUM2INT(bit)) <= 0) { \
|
|
695
|
+
ossl_raise(eBNError, NULL); \
|
|
696
|
+
} \
|
|
697
|
+
return self; \
|
|
699
698
|
}
|
|
700
699
|
|
|
701
700
|
/*
|
|
@@ -734,30 +733,30 @@ ossl_bn_is_bit_set(VALUE self, VALUE bit)
|
|
|
734
733
|
b = NUM2INT(bit);
|
|
735
734
|
GetBN(self, bn);
|
|
736
735
|
if (BN_is_bit_set(bn, b)) {
|
|
737
|
-
|
|
736
|
+
return Qtrue;
|
|
738
737
|
}
|
|
739
738
|
return Qfalse;
|
|
740
739
|
}
|
|
741
740
|
|
|
742
|
-
#define BIGNUM_SHIFT(func)
|
|
743
|
-
static VALUE
|
|
744
|
-
ossl_bn_##func(VALUE self, VALUE bits)
|
|
745
|
-
{
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
741
|
+
#define BIGNUM_SHIFT(func) \
|
|
742
|
+
static VALUE \
|
|
743
|
+
ossl_bn_##func(VALUE self, VALUE bits) \
|
|
744
|
+
{ \
|
|
745
|
+
BIGNUM *bn, *result; \
|
|
746
|
+
int b; \
|
|
747
|
+
VALUE obj; \
|
|
748
|
+
b = NUM2INT(bits); \
|
|
749
|
+
GetBN(self, bn); \
|
|
750
|
+
obj = NewBN(rb_obj_class(self)); \
|
|
751
|
+
if (!(result = BN_new())) { \
|
|
752
|
+
ossl_raise(eBNError, NULL); \
|
|
753
|
+
} \
|
|
754
|
+
if (BN_##func(result, bn, b) <= 0) { \
|
|
755
|
+
BN_free(result); \
|
|
756
|
+
ossl_raise(eBNError, NULL); \
|
|
757
|
+
} \
|
|
758
|
+
SetBN(obj, result); \
|
|
759
|
+
return obj; \
|
|
761
760
|
}
|
|
762
761
|
|
|
763
762
|
/*
|
|
@@ -774,18 +773,18 @@ BIGNUM_SHIFT(lshift)
|
|
|
774
773
|
*/
|
|
775
774
|
BIGNUM_SHIFT(rshift)
|
|
776
775
|
|
|
777
|
-
#define BIGNUM_SELF_SHIFT(func)
|
|
778
|
-
static VALUE
|
|
779
|
-
ossl_bn_self_##func(VALUE self, VALUE bits)
|
|
780
|
-
{
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
776
|
+
#define BIGNUM_SELF_SHIFT(func) \
|
|
777
|
+
static VALUE \
|
|
778
|
+
ossl_bn_self_##func(VALUE self, VALUE bits) \
|
|
779
|
+
{ \
|
|
780
|
+
BIGNUM *bn; \
|
|
781
|
+
int b; \
|
|
782
|
+
rb_check_frozen(self); \
|
|
783
|
+
b = NUM2INT(bits); \
|
|
784
|
+
GetBN(self, bn); \
|
|
785
|
+
if (BN_##func(bn, bn, b) <= 0) \
|
|
786
|
+
ossl_raise(eBNError, NULL); \
|
|
787
|
+
return self; \
|
|
789
788
|
}
|
|
790
789
|
|
|
791
790
|
/*
|
|
@@ -887,32 +886,32 @@ ossl_bn_s_generate_prime(int argc, VALUE *argv, VALUE klass)
|
|
|
887
886
|
num = NUM2INT(vnum);
|
|
888
887
|
|
|
889
888
|
if (vsafe == Qfalse) {
|
|
890
|
-
|
|
889
|
+
safe = 0;
|
|
891
890
|
}
|
|
892
891
|
if (!NIL_P(vadd)) {
|
|
893
|
-
|
|
894
|
-
|
|
892
|
+
add = GetBNPtr(vadd);
|
|
893
|
+
rem = NIL_P(vrem) ? NULL : GetBNPtr(vrem);
|
|
895
894
|
}
|
|
896
895
|
obj = NewBN(klass);
|
|
897
896
|
if (!(result = BN_new())) {
|
|
898
|
-
|
|
897
|
+
ossl_raise(eBNError, NULL);
|
|
899
898
|
}
|
|
900
899
|
if (!BN_generate_prime_ex(result, num, safe, add, rem, NULL)) {
|
|
901
|
-
|
|
902
|
-
|
|
900
|
+
BN_free(result);
|
|
901
|
+
ossl_raise(eBNError, NULL);
|
|
903
902
|
}
|
|
904
903
|
SetBN(obj, result);
|
|
905
904
|
|
|
906
905
|
return obj;
|
|
907
906
|
}
|
|
908
907
|
|
|
909
|
-
#define BIGNUM_NUM(func)
|
|
910
|
-
static VALUE
|
|
911
|
-
ossl_bn_##func(VALUE self)
|
|
912
|
-
{
|
|
913
|
-
|
|
914
|
-
|
|
915
|
-
|
|
908
|
+
#define BIGNUM_NUM(func) \
|
|
909
|
+
static VALUE \
|
|
910
|
+
ossl_bn_##func(VALUE self) \
|
|
911
|
+
{ \
|
|
912
|
+
BIGNUM *bn; \
|
|
913
|
+
GetBN(self, bn); \
|
|
914
|
+
return INT2NUM(BN_##func(bn)); \
|
|
916
915
|
}
|
|
917
916
|
|
|
918
917
|
/*
|
|
@@ -929,6 +928,7 @@ BIGNUM_NUM(num_bytes)
|
|
|
929
928
|
*/
|
|
930
929
|
BIGNUM_NUM(num_bits)
|
|
931
930
|
|
|
931
|
+
/* :nodoc: */
|
|
932
932
|
static VALUE
|
|
933
933
|
ossl_bn_copy(VALUE self, VALUE other)
|
|
934
934
|
{
|
|
@@ -942,7 +942,7 @@ ossl_bn_copy(VALUE self, VALUE other)
|
|
|
942
942
|
bn2 = GetBNPtr(other);
|
|
943
943
|
|
|
944
944
|
if (!BN_copy(bn1, bn2)) {
|
|
945
|
-
|
|
945
|
+
ossl_raise(eBNError, NULL);
|
|
946
946
|
}
|
|
947
947
|
return self;
|
|
948
948
|
}
|
|
@@ -961,7 +961,7 @@ ossl_bn_uplus(VALUE self)
|
|
|
961
961
|
obj = NewBN(cBN);
|
|
962
962
|
bn2 = BN_dup(bn1);
|
|
963
963
|
if (!bn2)
|
|
964
|
-
|
|
964
|
+
ossl_raise(eBNError, "BN_dup");
|
|
965
965
|
SetBN(obj, bn2);
|
|
966
966
|
|
|
967
967
|
return obj;
|
|
@@ -981,7 +981,7 @@ ossl_bn_uminus(VALUE self)
|
|
|
981
981
|
obj = NewBN(cBN);
|
|
982
982
|
bn2 = BN_dup(bn1);
|
|
983
983
|
if (!bn2)
|
|
984
|
-
|
|
984
|
+
ossl_raise(eBNError, "BN_dup");
|
|
985
985
|
SetBN(obj, bn2);
|
|
986
986
|
BN_set_negative(bn2, !BN_is_negative(bn2));
|
|
987
987
|
|
|
@@ -1006,13 +1006,13 @@ ossl_bn_abs(VALUE self)
|
|
|
1006
1006
|
}
|
|
1007
1007
|
}
|
|
1008
1008
|
|
|
1009
|
-
#define BIGNUM_CMP(func)
|
|
1010
|
-
static VALUE
|
|
1011
|
-
ossl_bn_##func(VALUE self, VALUE other)
|
|
1012
|
-
{
|
|
1013
|
-
|
|
1014
|
-
|
|
1015
|
-
|
|
1009
|
+
#define BIGNUM_CMP(func) \
|
|
1010
|
+
static VALUE \
|
|
1011
|
+
ossl_bn_##func(VALUE self, VALUE other) \
|
|
1012
|
+
{ \
|
|
1013
|
+
BIGNUM *bn1, *bn2 = GetBNPtr(other); \
|
|
1014
|
+
GetBN(self, bn1); \
|
|
1015
|
+
return INT2NUM(BN_##func(bn1, bn2)); \
|
|
1016
1016
|
}
|
|
1017
1017
|
|
|
1018
1018
|
/*
|
|
@@ -1049,11 +1049,11 @@ ossl_bn_eq(VALUE self, VALUE other)
|
|
|
1049
1049
|
GetBN(self, bn1);
|
|
1050
1050
|
other = try_convert_to_bn(other);
|
|
1051
1051
|
if (NIL_P(other))
|
|
1052
|
-
|
|
1052
|
+
return Qfalse;
|
|
1053
1053
|
GetBN(other, bn2);
|
|
1054
1054
|
|
|
1055
1055
|
if (!BN_cmp(bn1, bn2)) {
|
|
1056
|
-
|
|
1056
|
+
return Qtrue;
|
|
1057
1057
|
}
|
|
1058
1058
|
return Qfalse;
|
|
1059
1059
|
}
|
|
@@ -1072,7 +1072,7 @@ ossl_bn_eql(VALUE self, VALUE other)
|
|
|
1072
1072
|
BIGNUM *bn1, *bn2;
|
|
1073
1073
|
|
|
1074
1074
|
if (!rb_obj_is_kind_of(other, cBN))
|
|
1075
|
-
|
|
1075
|
+
return Qfalse;
|
|
1076
1076
|
GetBN(self, bn1);
|
|
1077
1077
|
GetBN(other, bn2);
|
|
1078
1078
|
|
|
@@ -1099,8 +1099,8 @@ ossl_bn_hash(VALUE self)
|
|
|
1099
1099
|
len = BN_num_bytes(bn);
|
|
1100
1100
|
buf = ALLOCV(tmp, len);
|
|
1101
1101
|
if (BN_bn2bin(bn, buf) != len) {
|
|
1102
|
-
|
|
1103
|
-
|
|
1102
|
+
ALLOCV_END(tmp);
|
|
1103
|
+
ossl_raise(eBNError, "BN_bn2bin");
|
|
1104
1104
|
}
|
|
1105
1105
|
|
|
1106
1106
|
hash = ST2FIX(rb_memhash(buf, len));
|
|
@@ -1202,11 +1202,6 @@ ossl_bn_set_flags(VALUE self, VALUE arg)
|
|
|
1202
1202
|
void
|
|
1203
1203
|
Init_ossl_bn(void)
|
|
1204
1204
|
{
|
|
1205
|
-
#if 0
|
|
1206
|
-
mOSSL = rb_define_module("OpenSSL");
|
|
1207
|
-
eOSSLError = rb_define_class_under(mOSSL, "OpenSSLError", rb_eStandardError);
|
|
1208
|
-
#endif
|
|
1209
|
-
|
|
1210
1205
|
#ifdef HAVE_RB_EXT_RACTOR_SAFE
|
|
1211
1206
|
ossl_bn_ctx_key = rb_ractor_local_storage_ptr_newkey(&ossl_bn_ctx_key_type);
|
|
1212
1207
|
#else
|