Skip to content

Commit 1b82bcb

Browse files
authored
Merge pull request #418 from rhenium/ky/bn-check-negative-error-returns
bn: check -1 return from BIGNUM functions
2 parents b3972a7 + 9b59f34 commit 1b82bcb

File tree

1 file changed

+22
-12
lines changed

1 file changed

+22
-12
lines changed

ext/openssl/ossl_bn.c

+22-12
Original file line numberDiff line numberDiff line change
@@ -397,7 +397,7 @@ ossl_bn_is_negative(VALUE self)
397397
if (!(result = BN_new())) { \
398398
ossl_raise(eBNError, NULL); \
399399
} \
400-
if (!BN_##func(result, bn, ossl_bn_ctx)) { \
400+
if (BN_##func(result, bn, ossl_bn_ctx) <= 0) { \
401401
BN_free(result); \
402402
ossl_raise(eBNError, NULL); \
403403
} \
@@ -423,7 +423,7 @@ BIGNUM_1c(sqr)
423423
if (!(result = BN_new())) { \
424424
ossl_raise(eBNError, NULL); \
425425
} \
426-
if (!BN_##func(result, bn1, bn2)) { \
426+
if (BN_##func(result, bn1, bn2) <= 0) { \
427427
BN_free(result); \
428428
ossl_raise(eBNError, NULL); \
429429
} \
@@ -456,7 +456,7 @@ BIGNUM_2(sub)
456456
if (!(result = BN_new())) { \
457457
ossl_raise(eBNError, NULL); \
458458
} \
459-
if (!BN_##func(result, bn1, bn2, ossl_bn_ctx)) { \
459+
if (BN_##func(result, bn1, bn2, ossl_bn_ctx) <= 0) { \
460460
BN_free(result); \
461461
ossl_raise(eBNError, NULL); \
462462
} \
@@ -500,11 +500,21 @@ BIGNUM_2c(gcd)
500500
BIGNUM_2c(mod_sqr)
501501

502502
/*
503-
* Document-method: OpenSSL::BN#mod_inverse
504503
* call-seq:
505-
* bn.mod_inverse(bn2) => aBN
504+
* bn.mod_inverse(bn2) => aBN
506505
*/
507-
BIGNUM_2c(mod_inverse)
506+
static VALUE
507+
ossl_bn_mod_inverse(VALUE self, VALUE other)
508+
{
509+
BIGNUM *bn1, *bn2 = GetBNPtr(other), *result;
510+
VALUE obj;
511+
GetBN(self, bn1);
512+
obj = NewBN(rb_obj_class(self));
513+
if (!(result = BN_mod_inverse(NULL, bn1, bn2, ossl_bn_ctx)))
514+
ossl_raise(eBNError, "BN_mod_inverse");
515+
SetBN(obj, result);
516+
return obj;
517+
}
508518

509519
/*
510520
* call-seq:
@@ -553,7 +563,7 @@ ossl_bn_div(VALUE self, VALUE other)
553563
if (!(result = BN_new())) { \
554564
ossl_raise(eBNError, NULL); \
555565
} \
556-
if (!BN_##func(result, bn1, bn2, bn3, ossl_bn_ctx)) { \
566+
if (BN_##func(result, bn1, bn2, bn3, ossl_bn_ctx) <= 0) { \
557567
BN_free(result); \
558568
ossl_raise(eBNError, NULL); \
559569
} \
@@ -595,7 +605,7 @@ BIGNUM_3c(mod_exp)
595605
{ \
596606
BIGNUM *bn; \
597607
GetBN(self, bn); \
598-
if (!BN_##func(bn, NUM2INT(bit))) { \
608+
if (BN_##func(bn, NUM2INT(bit)) <= 0) { \
599609
ossl_raise(eBNError, NULL); \
600610
} \
601611
return self; \
@@ -655,7 +665,7 @@ ossl_bn_is_bit_set(VALUE self, VALUE bit)
655665
if (!(result = BN_new())) { \
656666
ossl_raise(eBNError, NULL); \
657667
} \
658-
if (!BN_##func(result, bn, b)) { \
668+
if (BN_##func(result, bn, b) <= 0) { \
659669
BN_free(result); \
660670
ossl_raise(eBNError, NULL); \
661671
} \
@@ -685,7 +695,7 @@ BIGNUM_SHIFT(rshift)
685695
int b; \
686696
b = NUM2INT(bits); \
687697
GetBN(self, bn); \
688-
if (!BN_##func(bn, bn, b)) \
698+
if (BN_##func(bn, bn, b) <= 0) \
689699
ossl_raise(eBNError, NULL); \
690700
return self; \
691701
}
@@ -724,7 +734,7 @@ BIGNUM_SELF_SHIFT(rshift)
724734
if (!(result = BN_new())) { \
725735
ossl_raise(eBNError, NULL); \
726736
} \
727-
if (!BN_##func(result, b, top, bottom)) { \
737+
if (BN_##func(result, b, top, bottom) <= 0) { \
728738
BN_free(result); \
729739
ossl_raise(eBNError, NULL); \
730740
} \
@@ -753,7 +763,7 @@ BIGNUM_RAND(pseudo_rand)
753763
if (!(result = BN_new())) { \
754764
ossl_raise(eBNError, NULL); \
755765
} \
756-
if (!BN_##func##_range(result, bn)) { \
766+
if (BN_##func##_range(result, bn) <= 0) { \
757767
BN_free(result); \
758768
ossl_raise(eBNError, NULL); \
759769
} \

0 commit comments

Comments
 (0)