openssl 2.2.0 → 3.1.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.
Files changed (48) hide show
  1. checksums.yaml +4 -4
  2. data/CONTRIBUTING.md +33 -45
  3. data/History.md +260 -0
  4. data/ext/openssl/extconf.rb +85 -72
  5. data/ext/openssl/openssl_missing.c +0 -66
  6. data/ext/openssl/openssl_missing.h +26 -45
  7. data/ext/openssl/ossl.c +67 -47
  8. data/ext/openssl/ossl.h +26 -6
  9. data/ext/openssl/ossl_asn1.c +26 -13
  10. data/ext/openssl/ossl_bn.c +278 -142
  11. data/ext/openssl/ossl_bn.h +2 -1
  12. data/ext/openssl/ossl_cipher.c +12 -13
  13. data/ext/openssl/ossl_config.c +412 -41
  14. data/ext/openssl/ossl_config.h +4 -7
  15. data/ext/openssl/ossl_digest.c +15 -11
  16. data/ext/openssl/ossl_engine.c +16 -15
  17. data/ext/openssl/ossl_hmac.c +56 -135
  18. data/ext/openssl/ossl_kdf.c +11 -3
  19. data/ext/openssl/ossl_ocsp.c +5 -53
  20. data/ext/openssl/ossl_pkcs12.c +21 -3
  21. data/ext/openssl/ossl_pkcs7.c +42 -59
  22. data/ext/openssl/ossl_pkey.c +1142 -191
  23. data/ext/openssl/ossl_pkey.h +36 -73
  24. data/ext/openssl/ossl_pkey_dh.c +130 -340
  25. data/ext/openssl/ossl_pkey_dsa.c +100 -405
  26. data/ext/openssl/ossl_pkey_ec.c +163 -335
  27. data/ext/openssl/ossl_pkey_rsa.c +106 -493
  28. data/ext/openssl/ossl_ssl.c +529 -421
  29. data/ext/openssl/ossl_ssl_session.c +28 -29
  30. data/ext/openssl/ossl_ts.c +64 -39
  31. data/ext/openssl/ossl_x509.c +0 -6
  32. data/ext/openssl/ossl_x509cert.c +167 -11
  33. data/ext/openssl/ossl_x509crl.c +13 -10
  34. data/ext/openssl/ossl_x509ext.c +1 -2
  35. data/ext/openssl/ossl_x509name.c +9 -2
  36. data/ext/openssl/ossl_x509req.c +13 -10
  37. data/ext/openssl/ossl_x509revoked.c +3 -3
  38. data/ext/openssl/ossl_x509store.c +193 -90
  39. data/lib/openssl/buffering.rb +10 -1
  40. data/lib/openssl/hmac.rb +65 -0
  41. data/lib/openssl/pkey.rb +429 -0
  42. data/lib/openssl/ssl.rb +13 -8
  43. data/lib/openssl/version.rb +1 -1
  44. data/lib/openssl/x509.rb +22 -0
  45. data/lib/openssl.rb +0 -1
  46. metadata +8 -66
  47. data/ext/openssl/ruby_missing.h +0 -24
  48. data/lib/openssl/config.rb +0 -501
@@ -10,6 +10,10 @@
10
10
  /* modified by Michal Rokos <m.rokos@sh.cvut.cz> */
11
11
  #include "ossl.h"
12
12
 
13
+ #ifdef HAVE_RB_EXT_RACTOR_SAFE
14
+ #include <ruby/ractor.h>
15
+ #endif
16
+
13
17
  #define NewBN(klass) \
14
18
  TypedData_Wrap_Struct((klass), &ossl_bn_type, 0)
15
19
  #define SetBN(obj, bn) do { \
@@ -150,12 +154,58 @@ ossl_bn_value_ptr(volatile VALUE *ptr)
150
154
  /*
151
155
  * Private
152
156
  */
153
- /*
154
- * BN_CTX - is used in more difficult math. ops
155
- * (Why just 1? Because Ruby itself isn't thread safe,
156
- * we don't need to care about threads)
157
- */
158
- BN_CTX *ossl_bn_ctx;
157
+
158
+ #ifdef HAVE_RB_EXT_RACTOR_SAFE
159
+ void
160
+ ossl_bn_ctx_free(void *ptr)
161
+ {
162
+ BN_CTX *ctx = (BN_CTX *)ptr;
163
+ BN_CTX_free(ctx);
164
+ }
165
+
166
+ struct rb_ractor_local_storage_type ossl_bn_ctx_key_type = {
167
+ NULL, // mark
168
+ ossl_bn_ctx_free,
169
+ };
170
+
171
+ rb_ractor_local_key_t ossl_bn_ctx_key;
172
+
173
+ BN_CTX *
174
+ ossl_bn_ctx_get(void)
175
+ {
176
+ // stored in ractor local storage
177
+
178
+ BN_CTX *ctx = rb_ractor_local_storage_ptr(ossl_bn_ctx_key);
179
+ if (!ctx) {
180
+ if (!(ctx = BN_CTX_new())) {
181
+ ossl_raise(rb_eRuntimeError, "Cannot init BN_CTX");
182
+ }
183
+ rb_ractor_local_storage_ptr_set(ossl_bn_ctx_key, ctx);
184
+ }
185
+ return ctx;
186
+ }
187
+ #else
188
+ // for ruby 2.x
189
+ static BN_CTX *gv_ossl_bn_ctx;
190
+
191
+ BN_CTX *
192
+ ossl_bn_ctx_get(void)
193
+ {
194
+ if (gv_ossl_bn_ctx == NULL) {
195
+ if (!(gv_ossl_bn_ctx = BN_CTX_new())) {
196
+ ossl_raise(rb_eRuntimeError, "Cannot init BN_CTX");
197
+ }
198
+ }
199
+ return gv_ossl_bn_ctx;
200
+ }
201
+
202
+ void
203
+ ossl_bn_ctx_free(void)
204
+ {
205
+ BN_CTX_free(gv_ossl_bn_ctx);
206
+ gv_ossl_bn_ctx = NULL;
207
+ }
208
+ #endif
159
209
 
160
210
  static VALUE
161
211
  ossl_bn_alloc(VALUE klass)
@@ -173,12 +223,29 @@ ossl_bn_alloc(VALUE klass)
173
223
 
174
224
  /*
175
225
  * call-seq:
176
- * OpenSSL::BN.new(bn) => aBN
177
- * OpenSSL::BN.new(integer) => aBN
178
- * OpenSSL::BN.new(string) => aBN
179
- * OpenSSL::BN.new(string, 0 | 2 | 10 | 16) => aBN
226
+ * OpenSSL::BN.new(bn) -> aBN
227
+ * OpenSSL::BN.new(integer) -> aBN
228
+ * OpenSSL::BN.new(string, base = 10) -> aBN
229
+ *
230
+ * Construct a new \OpenSSL BIGNUM object.
231
+ *
232
+ * If +bn+ is an Integer or OpenSSL::BN, a new instance of OpenSSL::BN
233
+ * representing the same value is returned. See also Integer#to_bn for the
234
+ * short-hand.
235
+ *
236
+ * If a String is given, the content will be parsed according to +base+.
180
237
  *
181
- * Construct a new OpenSSL BIGNUM object.
238
+ * +string+::
239
+ * The string to be parsed.
240
+ * +base+::
241
+ * The format. Must be one of the following:
242
+ * - +0+ - MPI format. See the man page BN_mpi2bn(3) for details.
243
+ * - +2+ - Variable-length and big-endian binary encoding of a positive
244
+ * number.
245
+ * - +10+ - Decimal number representation, with a leading '-' for a negative
246
+ * number.
247
+ * - +16+ - Hexadeciaml number representation, with a leading '-' for a
248
+ * negative number.
182
249
  */
183
250
  static VALUE
184
251
  ossl_bn_initialize(int argc, VALUE *argv, VALUE self)
@@ -246,16 +313,21 @@ ossl_bn_initialize(int argc, VALUE *argv, VALUE self)
246
313
 
247
314
  /*
248
315
  * call-seq:
249
- * bn.to_s => string
250
- * bn.to_s(base) => string
316
+ * bn.to_s(base = 10) -> string
251
317
  *
252
- * === Parameters
253
- * * _base_ - Integer
254
- * Valid values:
255
- * * 0 - MPI
256
- * * 2 - binary
257
- * * 10 - the default
258
- * * 16 - hex
318
+ * Returns the string representation of the bignum.
319
+ *
320
+ * BN.new can parse the encoded string to convert back into an OpenSSL::BN.
321
+ *
322
+ * +base+::
323
+ * The format. Must be one of the following:
324
+ * - +0+ - MPI format. See the man page BN_bn2mpi(3) for details.
325
+ * - +2+ - Variable-length and big-endian binary encoding. The sign of
326
+ * the bignum is ignored.
327
+ * - +10+ - Decimal number representation, with a leading '-' for a negative
328
+ * bignum.
329
+ * - +16+ - Hexadeciaml number representation, with a leading '-' for a
330
+ * negative bignum.
259
331
  */
260
332
  static VALUE
261
333
  ossl_bn_to_s(int argc, VALUE *argv, VALUE self)
@@ -403,7 +475,7 @@ ossl_bn_is_negative(VALUE self)
403
475
  if (!(result = BN_new())) { \
404
476
  ossl_raise(eBNError, NULL); \
405
477
  } \
406
- if (!BN_##func(result, bn, ossl_bn_ctx)) { \
478
+ if (BN_##func(result, bn, ossl_bn_ctx) <= 0) { \
407
479
  BN_free(result); \
408
480
  ossl_raise(eBNError, NULL); \
409
481
  } \
@@ -429,7 +501,7 @@ BIGNUM_1c(sqr)
429
501
  if (!(result = BN_new())) { \
430
502
  ossl_raise(eBNError, NULL); \
431
503
  } \
432
- if (!BN_##func(result, bn1, bn2)) { \
504
+ if (BN_##func(result, bn1, bn2) <= 0) { \
433
505
  BN_free(result); \
434
506
  ossl_raise(eBNError, NULL); \
435
507
  } \
@@ -462,7 +534,7 @@ BIGNUM_2(sub)
462
534
  if (!(result = BN_new())) { \
463
535
  ossl_raise(eBNError, NULL); \
464
536
  } \
465
- if (!BN_##func(result, bn1, bn2, ossl_bn_ctx)) { \
537
+ if (BN_##func(result, bn1, bn2, ossl_bn_ctx) <= 0) { \
466
538
  BN_free(result); \
467
539
  ossl_raise(eBNError, NULL); \
468
540
  } \
@@ -505,12 +577,33 @@ BIGNUM_2c(gcd)
505
577
  */
506
578
  BIGNUM_2c(mod_sqr)
507
579
 
580
+ #define BIGNUM_2cr(func) \
581
+ static VALUE \
582
+ ossl_bn_##func(VALUE self, VALUE other) \
583
+ { \
584
+ BIGNUM *bn1, *bn2 = GetBNPtr(other), *result; \
585
+ VALUE obj; \
586
+ GetBN(self, bn1); \
587
+ obj = NewBN(rb_obj_class(self)); \
588
+ if (!(result = BN_##func(NULL, bn1, bn2, ossl_bn_ctx))) \
589
+ ossl_raise(eBNError, NULL); \
590
+ SetBN(obj, result); \
591
+ return obj; \
592
+ }
593
+
594
+ /*
595
+ * Document-method: OpenSSL::BN#mod_sqrt
596
+ * call-seq:
597
+ * bn.mod_sqrt(bn2) => aBN
598
+ */
599
+ BIGNUM_2cr(mod_sqrt)
600
+
508
601
  /*
509
602
  * Document-method: OpenSSL::BN#mod_inverse
510
603
  * call-seq:
511
- * bn.mod_inverse(bn2) => aBN
604
+ * bn.mod_inverse(bn2) => aBN
512
605
  */
513
- BIGNUM_2c(mod_inverse)
606
+ BIGNUM_2cr(mod_inverse)
514
607
 
515
608
  /*
516
609
  * call-seq:
@@ -559,7 +652,7 @@ ossl_bn_div(VALUE self, VALUE other)
559
652
  if (!(result = BN_new())) { \
560
653
  ossl_raise(eBNError, NULL); \
561
654
  } \
562
- if (!BN_##func(result, bn1, bn2, bn3, ossl_bn_ctx)) { \
655
+ if (BN_##func(result, bn1, bn2, bn3, ossl_bn_ctx) <= 0) { \
563
656
  BN_free(result); \
564
657
  ossl_raise(eBNError, NULL); \
565
658
  } \
@@ -601,7 +694,7 @@ BIGNUM_3c(mod_exp)
601
694
  { \
602
695
  BIGNUM *bn; \
603
696
  GetBN(self, bn); \
604
- if (!BN_##func(bn, NUM2INT(bit))) { \
697
+ if (BN_##func(bn, NUM2INT(bit)) <= 0) { \
605
698
  ossl_raise(eBNError, NULL); \
606
699
  } \
607
700
  return self; \
@@ -661,7 +754,7 @@ ossl_bn_is_bit_set(VALUE self, VALUE bit)
661
754
  if (!(result = BN_new())) { \
662
755
  ossl_raise(eBNError, NULL); \
663
756
  } \
664
- if (!BN_##func(result, bn, b)) { \
757
+ if (BN_##func(result, bn, b) <= 0) { \
665
758
  BN_free(result); \
666
759
  ossl_raise(eBNError, NULL); \
667
760
  } \
@@ -691,7 +784,7 @@ BIGNUM_SHIFT(rshift)
691
784
  int b; \
692
785
  b = NUM2INT(bits); \
693
786
  GetBN(self, bn); \
694
- if (!BN_##func(bn, bn, b)) \
787
+ if (BN_##func(bn, bn, b) <= 0) \
695
788
  ossl_raise(eBNError, NULL); \
696
789
  return self; \
697
790
  }
@@ -710,78 +803,64 @@ BIGNUM_SELF_SHIFT(lshift)
710
803
  */
711
804
  BIGNUM_SELF_SHIFT(rshift)
712
805
 
713
- #define BIGNUM_RAND(func) \
714
- static VALUE \
715
- ossl_bn_s_##func(int argc, VALUE *argv, VALUE klass) \
716
- { \
717
- BIGNUM *result; \
718
- int bottom = 0, top = 0, b; \
719
- VALUE bits, fill, odd, obj; \
720
- \
721
- switch (rb_scan_args(argc, argv, "12", &bits, &fill, &odd)) { \
722
- case 3: \
723
- bottom = (odd == Qtrue) ? 1 : 0; \
724
- /* FALLTHROUGH */ \
725
- case 2: \
726
- top = NUM2INT(fill); \
727
- } \
728
- b = NUM2INT(bits); \
729
- obj = NewBN(klass); \
730
- if (!(result = BN_new())) { \
731
- ossl_raise(eBNError, NULL); \
732
- } \
733
- if (!BN_##func(result, b, top, bottom)) { \
734
- BN_free(result); \
735
- ossl_raise(eBNError, NULL); \
736
- } \
737
- SetBN(obj, result); \
738
- return obj; \
739
- }
740
-
741
- /*
742
- * Document-method: OpenSSL::BN.rand
743
- * BN.rand(bits [, fill [, odd]]) -> aBN
744
- */
745
- BIGNUM_RAND(rand)
746
-
747
- /*
748
- * Document-method: OpenSSL::BN.pseudo_rand
749
- * BN.pseudo_rand(bits [, fill [, odd]]) -> aBN
750
- */
751
- BIGNUM_RAND(pseudo_rand)
752
-
753
- #define BIGNUM_RAND_RANGE(func) \
754
- static VALUE \
755
- ossl_bn_s_##func##_range(VALUE klass, VALUE range) \
756
- { \
757
- BIGNUM *bn = GetBNPtr(range), *result; \
758
- VALUE obj = NewBN(klass); \
759
- if (!(result = BN_new())) { \
760
- ossl_raise(eBNError, NULL); \
761
- } \
762
- if (!BN_##func##_range(result, bn)) { \
763
- BN_free(result); \
764
- ossl_raise(eBNError, NULL); \
765
- } \
766
- SetBN(obj, result); \
767
- return obj; \
768
- }
769
-
770
806
  /*
771
- * Document-method: OpenSSL::BN.rand_range
772
807
  * call-seq:
773
- * BN.rand_range(range) -> aBN
808
+ * BN.rand(bits [, fill [, odd]]) -> aBN
774
809
  *
810
+ * Generates a cryptographically strong pseudo-random number of +bits+.
811
+ *
812
+ * See also the man page BN_rand(3).
775
813
  */
776
- BIGNUM_RAND_RANGE(rand)
814
+ static VALUE
815
+ ossl_bn_s_rand(int argc, VALUE *argv, VALUE klass)
816
+ {
817
+ BIGNUM *result;
818
+ int bottom = 0, top = 0, b;
819
+ VALUE bits, fill, odd, obj;
820
+
821
+ switch (rb_scan_args(argc, argv, "12", &bits, &fill, &odd)) {
822
+ case 3:
823
+ bottom = (odd == Qtrue) ? 1 : 0;
824
+ /* FALLTHROUGH */
825
+ case 2:
826
+ top = NUM2INT(fill);
827
+ }
828
+ b = NUM2INT(bits);
829
+ obj = NewBN(klass);
830
+ if (!(result = BN_new())) {
831
+ ossl_raise(eBNError, "BN_new");
832
+ }
833
+ if (BN_rand(result, b, top, bottom) <= 0) {
834
+ BN_free(result);
835
+ ossl_raise(eBNError, "BN_rand");
836
+ }
837
+ SetBN(obj, result);
838
+ return obj;
839
+ }
777
840
 
778
841
  /*
779
- * Document-method: OpenSSL::BN.pseudo_rand_range
780
842
  * call-seq:
781
- * BN.pseudo_rand_range(range) -> aBN
843
+ * BN.rand_range(range) -> aBN
844
+ *
845
+ * Generates a cryptographically strong pseudo-random number in the range
846
+ * 0...+range+.
782
847
  *
848
+ * See also the man page BN_rand_range(3).
783
849
  */
784
- BIGNUM_RAND_RANGE(pseudo_rand)
850
+ static VALUE
851
+ ossl_bn_s_rand_range(VALUE klass, VALUE range)
852
+ {
853
+ BIGNUM *bn = GetBNPtr(range), *result;
854
+ VALUE obj = NewBN(klass);
855
+ if (!(result = BN_new()))
856
+ ossl_raise(eBNError, "BN_new");
857
+ if (BN_rand_range(result, bn) <= 0) {
858
+ BN_free(result);
859
+ ossl_raise(eBNError, "BN_rand_range");
860
+ }
861
+ SetBN(obj, result);
862
+ return obj;
863
+ }
785
864
 
786
865
  /*
787
866
  * call-seq:
@@ -876,7 +955,17 @@ ossl_bn_copy(VALUE self, VALUE other)
876
955
  static VALUE
877
956
  ossl_bn_uplus(VALUE self)
878
957
  {
879
- return self;
958
+ VALUE obj;
959
+ BIGNUM *bn1, *bn2;
960
+
961
+ GetBN(self, bn1);
962
+ obj = NewBN(cBN);
963
+ bn2 = BN_dup(bn1);
964
+ if (!bn2)
965
+ ossl_raise(eBNError, "BN_dup");
966
+ SetBN(obj, bn2);
967
+
968
+ return obj;
880
969
  }
881
970
 
882
971
  /*
@@ -900,6 +989,24 @@ ossl_bn_uminus(VALUE self)
900
989
  return obj;
901
990
  }
902
991
 
992
+ /*
993
+ * call-seq:
994
+ * bn.abs -> aBN
995
+ */
996
+ static VALUE
997
+ ossl_bn_abs(VALUE self)
998
+ {
999
+ BIGNUM *bn1;
1000
+
1001
+ GetBN(self, bn1);
1002
+ if (BN_is_negative(bn1)) {
1003
+ return ossl_bn_uminus(self);
1004
+ }
1005
+ else {
1006
+ return ossl_bn_uplus(self);
1007
+ }
1008
+ }
1009
+
903
1010
  #define BIGNUM_CMP(func) \
904
1011
  static VALUE \
905
1012
  ossl_bn_##func(VALUE self, VALUE other) \
@@ -1008,34 +1115,29 @@ ossl_bn_hash(VALUE self)
1008
1115
  * bn.prime? => true | false
1009
1116
  * bn.prime?(checks) => true | false
1010
1117
  *
1011
- * Performs a Miller-Rabin probabilistic primality test with _checks_
1012
- * iterations. If _checks_ is not specified, a number of iterations is used
1013
- * that yields a false positive rate of at most 2^-80 for random input.
1118
+ * Performs a Miller-Rabin probabilistic primality test for +bn+.
1014
1119
  *
1015
- * === Parameters
1016
- * * _checks_ - integer
1120
+ * <b>+checks+ parameter is deprecated in version 3.0.</b> It has no effect.
1017
1121
  */
1018
1122
  static VALUE
1019
1123
  ossl_bn_is_prime(int argc, VALUE *argv, VALUE self)
1020
1124
  {
1021
1125
  BIGNUM *bn;
1022
- VALUE vchecks;
1023
- int checks = BN_prime_checks;
1126
+ int ret;
1024
1127
 
1025
- if (rb_scan_args(argc, argv, "01", &vchecks) == 1) {
1026
- checks = NUM2INT(vchecks);
1027
- }
1128
+ rb_check_arity(argc, 0, 1);
1028
1129
  GetBN(self, bn);
1029
- switch (BN_is_prime_ex(bn, checks, ossl_bn_ctx, NULL)) {
1030
- case 1:
1031
- return Qtrue;
1032
- case 0:
1033
- return Qfalse;
1034
- default:
1035
- ossl_raise(eBNError, NULL);
1036
- }
1037
- /* not reachable */
1038
- return Qnil;
1130
+
1131
+ #ifdef HAVE_BN_CHECK_PRIME
1132
+ ret = BN_check_prime(bn, ossl_bn_ctx, NULL);
1133
+ if (ret < 0)
1134
+ ossl_raise(eBNError, "BN_check_prime");
1135
+ #else
1136
+ ret = BN_is_prime_fasttest_ex(bn, BN_prime_checks, ossl_bn_ctx, 1, NULL);
1137
+ if (ret < 0)
1138
+ ossl_raise(eBNError, "BN_is_prime_fasttest_ex");
1139
+ #endif
1140
+ return ret ? Qtrue : Qfalse;
1039
1141
  }
1040
1142
 
1041
1143
  /*
@@ -1044,39 +1146,52 @@ ossl_bn_is_prime(int argc, VALUE *argv, VALUE self)
1044
1146
  * bn.prime_fasttest?(checks) => true | false
1045
1147
  * bn.prime_fasttest?(checks, trial_div) => true | false
1046
1148
  *
1047
- * Performs a Miller-Rabin primality test. This is same as #prime? except this
1048
- * first attempts trial divisions with some small primes.
1149
+ * Performs a Miller-Rabin probabilistic primality test for +bn+.
1049
1150
  *
1050
- * === Parameters
1051
- * * _checks_ - integer
1052
- * * _trial_div_ - boolean
1151
+ * <b>Deprecated in version 3.0.</b> Use #prime? instead.
1152
+ *
1153
+ * +checks+ and +trial_div+ parameters no longer have any effect.
1053
1154
  */
1054
1155
  static VALUE
1055
1156
  ossl_bn_is_prime_fasttest(int argc, VALUE *argv, VALUE self)
1157
+ {
1158
+ rb_check_arity(argc, 0, 2);
1159
+ return ossl_bn_is_prime(0, argv, self);
1160
+ }
1161
+
1162
+ /*
1163
+ * call-seq:
1164
+ * bn.get_flags(flags) => flags
1165
+ *
1166
+ * Returns the flags on the BN object.
1167
+ * The argument is used as a bit mask.
1168
+ *
1169
+ * === Parameters
1170
+ * * _flags_ - integer
1171
+ */
1172
+ static VALUE
1173
+ ossl_bn_get_flags(VALUE self, VALUE arg)
1056
1174
  {
1057
1175
  BIGNUM *bn;
1058
- VALUE vchecks, vtrivdiv;
1059
- int checks = BN_prime_checks, do_trial_division = 1;
1176
+ GetBN(self, bn);
1060
1177
 
1061
- rb_scan_args(argc, argv, "02", &vchecks, &vtrivdiv);
1178
+ return INT2NUM(BN_get_flags(bn, NUM2INT(arg)));
1179
+ }
1062
1180
 
1063
- if (!NIL_P(vchecks)) {
1064
- checks = NUM2INT(vchecks);
1065
- }
1181
+ /*
1182
+ * call-seq:
1183
+ * bn.set_flags(flags) => nil
1184
+ *
1185
+ * Enables the flags on the BN object.
1186
+ * Currently, the flags argument can contain zero of OpenSSL::BN::CONSTTIME.
1187
+ */
1188
+ static VALUE
1189
+ ossl_bn_set_flags(VALUE self, VALUE arg)
1190
+ {
1191
+ BIGNUM *bn;
1066
1192
  GetBN(self, bn);
1067
- /* handle true/false */
1068
- if (vtrivdiv == Qfalse) {
1069
- do_trial_division = 0;
1070
- }
1071
- switch (BN_is_prime_fasttest_ex(bn, checks, ossl_bn_ctx, do_trial_division, NULL)) {
1072
- case 1:
1073
- return Qtrue;
1074
- case 0:
1075
- return Qfalse;
1076
- default:
1077
- ossl_raise(eBNError, NULL);
1078
- }
1079
- /* not reachable */
1193
+
1194
+ BN_set_flags(bn, NUM2INT(arg));
1080
1195
  return Qnil;
1081
1196
  }
1082
1197
 
@@ -1092,9 +1207,11 @@ Init_ossl_bn(void)
1092
1207
  eOSSLError = rb_define_class_under(mOSSL, "OpenSSLError", rb_eStandardError);
1093
1208
  #endif
1094
1209
 
1095
- if (!(ossl_bn_ctx = BN_CTX_new())) {
1096
- ossl_raise(rb_eRuntimeError, "Cannot init BN_CTX");
1097
- }
1210
+ #ifdef HAVE_RB_EXT_RACTOR_SAFE
1211
+ ossl_bn_ctx_key = rb_ractor_local_storage_ptr_newkey(&ossl_bn_ctx_key_type);
1212
+ #else
1213
+ ossl_bn_ctx_get();
1214
+ #endif
1098
1215
 
1099
1216
  eBNError = rb_define_class_under(mOSSL, "BNError", eOSSLError);
1100
1217
 
@@ -1114,6 +1231,7 @@ Init_ossl_bn(void)
1114
1231
 
1115
1232
  rb_define_method(cBN, "+@", ossl_bn_uplus, 0);
1116
1233
  rb_define_method(cBN, "-@", ossl_bn_uminus, 0);
1234
+ rb_define_method(cBN, "abs", ossl_bn_abs, 0);
1117
1235
 
1118
1236
  rb_define_method(cBN, "+", ossl_bn_add, 1);
1119
1237
  rb_define_method(cBN, "-", ossl_bn_sub, 1);
@@ -1127,6 +1245,7 @@ Init_ossl_bn(void)
1127
1245
  rb_define_method(cBN, "mod_sub", ossl_bn_mod_sub, 2);
1128
1246
  rb_define_method(cBN, "mod_mul", ossl_bn_mod_mul, 2);
1129
1247
  rb_define_method(cBN, "mod_sqr", ossl_bn_mod_sqr, 1);
1248
+ rb_define_method(cBN, "mod_sqrt", ossl_bn_mod_sqrt, 1);
1130
1249
  rb_define_method(cBN, "**", ossl_bn_exp, 1);
1131
1250
  rb_define_method(cBN, "mod_exp", ossl_bn_mod_exp, 2);
1132
1251
  rb_define_method(cBN, "gcd", ossl_bn_gcd, 1);
@@ -1157,9 +1276,9 @@ Init_ossl_bn(void)
1157
1276
  * get_word */
1158
1277
 
1159
1278
  rb_define_singleton_method(cBN, "rand", ossl_bn_s_rand, -1);
1160
- rb_define_singleton_method(cBN, "pseudo_rand", ossl_bn_s_pseudo_rand, -1);
1161
1279
  rb_define_singleton_method(cBN, "rand_range", ossl_bn_s_rand_range, 1);
1162
- rb_define_singleton_method(cBN, "pseudo_rand_range", ossl_bn_s_pseudo_rand_range, 1);
1280
+ rb_define_alias(rb_singleton_class(cBN), "pseudo_rand", "rand");
1281
+ rb_define_alias(rb_singleton_class(cBN), "pseudo_rand_range", "rand_range");
1163
1282
 
1164
1283
  rb_define_singleton_method(cBN, "generate_prime", ossl_bn_s_generate_prime, -1);
1165
1284
  rb_define_method(cBN, "prime?", ossl_bn_is_prime, -1);
@@ -1176,6 +1295,23 @@ Init_ossl_bn(void)
1176
1295
  /* lshift1 - DON'T IMPL. */
1177
1296
  /* rshift1 - DON'T IMPL. */
1178
1297
 
1298
+ rb_define_method(cBN, "get_flags", ossl_bn_get_flags, 1);
1299
+ rb_define_method(cBN, "set_flags", ossl_bn_set_flags, 1);
1300
+
1301
+ #ifdef BN_FLG_CONSTTIME
1302
+ rb_define_const(cBN, "CONSTTIME", INT2NUM(BN_FLG_CONSTTIME));
1303
+ #endif
1304
+ /* BN_FLG_MALLOCED and BN_FLG_STATIC_DATA seems for C programming.
1305
+ * Allowing them leads to memory leak.
1306
+ * So, for now, they are not exported
1307
+ #ifdef BN_FLG_MALLOCED
1308
+ rb_define_const(cBN, "MALLOCED", INT2NUM(BN_FLG_MALLOCED));
1309
+ #endif
1310
+ #ifdef BN_FLG_STATIC_DATA
1311
+ rb_define_const(cBN, "STATIC_DATA", INT2NUM(BN_FLG_STATIC_DATA));
1312
+ #endif
1313
+ */
1314
+
1179
1315
  /*
1180
1316
  * bn2bin
1181
1317
  * bin2bn
@@ -13,7 +13,8 @@
13
13
  extern VALUE cBN;
14
14
  extern VALUE eBNError;
15
15
 
16
- extern BN_CTX *ossl_bn_ctx;
16
+ BN_CTX *ossl_bn_ctx_get(void);
17
+ #define ossl_bn_ctx ossl_bn_ctx_get()
17
18
 
18
19
  #define GetBNPtr(obj) ossl_bn_value_ptr(&(obj))
19
20
 
@@ -104,7 +104,7 @@ ossl_cipher_alloc(VALUE klass)
104
104
  * call-seq:
105
105
  * Cipher.new(string) -> cipher
106
106
  *
107
- * The string must contain a valid cipher name like "AES-256-CBC".
107
+ * The string must contain a valid cipher name like "aes-256-cbc".
108
108
  *
109
109
  * A list of cipher names is available by calling OpenSSL::Cipher.ciphers.
110
110
  */
@@ -149,11 +149,11 @@ ossl_cipher_copy(VALUE self, VALUE other)
149
149
  return self;
150
150
  }
151
151
 
152
- static void*
153
- add_cipher_name_to_ary(const OBJ_NAME *name, VALUE ary)
152
+ static void
153
+ add_cipher_name_to_ary(const OBJ_NAME *name, void *arg)
154
154
  {
155
+ VALUE ary = (VALUE)arg;
155
156
  rb_ary_push(ary, rb_str_new2(name->name));
156
- return NULL;
157
157
  }
158
158
 
159
159
  /*
@@ -169,7 +169,7 @@ ossl_s_ciphers(VALUE self)
169
169
 
170
170
  ary = rb_ary_new();
171
171
  OBJ_NAME_do_all_sorted(OBJ_NAME_TYPE_CIPHER_METH,
172
- (void(*)(const OBJ_NAME*,void*))add_cipher_name_to_ary,
172
+ add_cipher_name_to_ary,
173
173
  (void*)ary);
174
174
 
175
175
  return ary;
@@ -384,8 +384,7 @@ ossl_cipher_update(int argc, VALUE *argv, VALUE self)
384
384
 
385
385
  StringValue(data);
386
386
  in = (unsigned char *)RSTRING_PTR(data);
387
- if ((in_len = RSTRING_LEN(data)) == 0)
388
- ossl_raise(rb_eArgError, "data must not be empty");
387
+ in_len = RSTRING_LEN(data);
389
388
  GetCipher(self, ctx);
390
389
  out_len = in_len+EVP_CIPHER_CTX_block_size(ctx);
391
390
  if (out_len <= 0) {
@@ -874,7 +873,7 @@ Init_ossl_cipher(void)
874
873
  * individual components name, key length and mode. Either all uppercase
875
874
  * or all lowercase strings may be used, for example:
876
875
  *
877
- * cipher = OpenSSL::Cipher.new('AES-128-CBC')
876
+ * cipher = OpenSSL::Cipher.new('aes-128-cbc')
878
877
  *
879
878
  * === Choosing either encryption or decryption mode
880
879
  *
@@ -904,7 +903,7 @@ Init_ossl_cipher(void)
904
903
  * without processing the password further. A simple and secure way to
905
904
  * create a key for a particular Cipher is
906
905
  *
907
- * cipher = OpenSSL::Cipher.new('AES-256-CFB')
906
+ * cipher = OpenSSL::Cipher.new('aes-256-cfb')
908
907
  * cipher.encrypt
909
908
  * key = cipher.random_key # also sets the generated key on the Cipher
910
909
  *
@@ -972,14 +971,14 @@ Init_ossl_cipher(void)
972
971
  *
973
972
  * data = "Very, very confidential data"
974
973
  *
975
- * cipher = OpenSSL::Cipher.new('AES-128-CBC')
974
+ * cipher = OpenSSL::Cipher.new('aes-128-cbc')
976
975
  * cipher.encrypt
977
976
  * key = cipher.random_key
978
977
  * iv = cipher.random_iv
979
978
  *
980
979
  * encrypted = cipher.update(data) + cipher.final
981
980
  * ...
982
- * decipher = OpenSSL::Cipher.new('AES-128-CBC')
981
+ * decipher = OpenSSL::Cipher.new('aes-128-cbc')
983
982
  * decipher.decrypt
984
983
  * decipher.key = key
985
984
  * decipher.iv = iv
@@ -1015,7 +1014,7 @@ Init_ossl_cipher(void)
1015
1014
  * not to reuse the _key_ and _nonce_ pair. Reusing an nonce ruins the
1016
1015
  * security guarantees of GCM mode.
1017
1016
  *
1018
- * cipher = OpenSSL::Cipher.new('AES-128-GCM').encrypt
1017
+ * cipher = OpenSSL::Cipher.new('aes-128-gcm').encrypt
1019
1018
  * cipher.key = key
1020
1019
  * cipher.iv = nonce
1021
1020
  * cipher.auth_data = auth_data
@@ -1031,7 +1030,7 @@ Init_ossl_cipher(void)
1031
1030
  * ciphertext with a probability of 1/256.
1032
1031
  *
1033
1032
  * raise "tag is truncated!" unless tag.bytesize == 16
1034
- * decipher = OpenSSL::Cipher.new('AES-128-GCM').decrypt
1033
+ * decipher = OpenSSL::Cipher.new('aes-128-gcm').decrypt
1035
1034
  * decipher.key = key
1036
1035
  * decipher.iv = nonce
1037
1036
  * decipher.auth_tag = tag