openssl 2.2.0 → 3.2.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 (54) hide show
  1. checksums.yaml +4 -4
  2. data/CONTRIBUTING.md +33 -45
  3. data/History.md +300 -0
  4. data/README.md +36 -19
  5. data/ext/openssl/extconf.rb +119 -79
  6. data/ext/openssl/openssl_missing.c +0 -66
  7. data/ext/openssl/openssl_missing.h +26 -45
  8. data/ext/openssl/ossl.c +131 -233
  9. data/ext/openssl/ossl.h +31 -12
  10. data/ext/openssl/ossl_asn1.c +26 -13
  11. data/ext/openssl/ossl_bn.c +279 -143
  12. data/ext/openssl/ossl_bn.h +2 -1
  13. data/ext/openssl/ossl_cipher.c +13 -14
  14. data/ext/openssl/ossl_config.c +412 -41
  15. data/ext/openssl/ossl_config.h +4 -7
  16. data/ext/openssl/ossl_digest.c +16 -12
  17. data/ext/openssl/ossl_engine.c +17 -16
  18. data/ext/openssl/ossl_hmac.c +57 -136
  19. data/ext/openssl/ossl_kdf.c +12 -4
  20. data/ext/openssl/ossl_ns_spki.c +1 -1
  21. data/ext/openssl/ossl_ocsp.c +11 -59
  22. data/ext/openssl/ossl_pkcs12.c +22 -4
  23. data/ext/openssl/ossl_pkcs7.c +45 -62
  24. data/ext/openssl/ossl_pkey.c +1320 -196
  25. data/ext/openssl/ossl_pkey.h +36 -73
  26. data/ext/openssl/ossl_pkey_dh.c +152 -347
  27. data/ext/openssl/ossl_pkey_dsa.c +157 -413
  28. data/ext/openssl/ossl_pkey_ec.c +227 -343
  29. data/ext/openssl/ossl_pkey_rsa.c +159 -491
  30. data/ext/openssl/ossl_provider.c +211 -0
  31. data/ext/openssl/ossl_provider.h +5 -0
  32. data/ext/openssl/ossl_ssl.c +593 -467
  33. data/ext/openssl/ossl_ssl_session.c +29 -30
  34. data/ext/openssl/ossl_ts.c +67 -42
  35. data/ext/openssl/ossl_x509.c +0 -6
  36. data/ext/openssl/ossl_x509attr.c +1 -1
  37. data/ext/openssl/ossl_x509cert.c +168 -12
  38. data/ext/openssl/ossl_x509crl.c +14 -11
  39. data/ext/openssl/ossl_x509ext.c +14 -9
  40. data/ext/openssl/ossl_x509name.c +10 -3
  41. data/ext/openssl/ossl_x509req.c +14 -11
  42. data/ext/openssl/ossl_x509revoked.c +4 -4
  43. data/ext/openssl/ossl_x509store.c +204 -94
  44. data/lib/openssl/buffering.rb +10 -4
  45. data/lib/openssl/digest.rb +1 -5
  46. data/lib/openssl/hmac.rb +65 -0
  47. data/lib/openssl/pkey.rb +429 -0
  48. data/lib/openssl/ssl.rb +23 -18
  49. data/lib/openssl/version.rb +1 -1
  50. data/lib/openssl/x509.rb +22 -0
  51. data/lib/openssl.rb +0 -1
  52. metadata +13 -68
  53. data/ext/openssl/ruby_missing.h +0 -24
  54. data/lib/openssl/config.rb +0 -501
data/ext/openssl/ossl.c CHANGED
@@ -9,13 +9,19 @@
9
9
  */
10
10
  #include "ossl.h"
11
11
  #include <stdarg.h> /* for ossl_raise */
12
- #include <ruby/thread_native.h> /* for OpenSSL < 1.1.0 locks */
12
+
13
+ /* OpenSSL >= 1.1.0 and LibreSSL >= 2.9.0 */
14
+ #if defined(LIBRESSL_VERSION_NUMBER) || OPENSSL_VERSION_NUMBER >= 0x10100000
15
+ # define HAVE_OPENSSL_110_THREADING_API
16
+ #else
17
+ # include <ruby/thread_native.h>
18
+ #endif
13
19
 
14
20
  /*
15
21
  * Data Conversion
16
22
  */
17
23
  #define OSSL_IMPL_ARY2SK(name, type, expected_class, dup) \
18
- STACK_OF(type) * \
24
+ VALUE \
19
25
  ossl_##name##_ary2sk0(VALUE ary) \
20
26
  { \
21
27
  STACK_OF(type) *sk; \
@@ -37,7 +43,7 @@ ossl_##name##_ary2sk0(VALUE ary) \
37
43
  x = dup(val); /* NEED TO DUP */ \
38
44
  sk_##type##_push(sk, x); \
39
45
  } \
40
- return sk; \
46
+ return (VALUE)sk; \
41
47
  } \
42
48
  \
43
49
  STACK_OF(type) * \
@@ -201,7 +207,7 @@ ossl_pem_passwd_cb(char *buf, int max_len, int flag, void *pwd_)
201
207
 
202
208
  while (1) {
203
209
  /*
204
- * when the flag is nonzero, this passphrase
210
+ * when the flag is nonzero, this password
205
211
  * will be used to perform encryption; otherwise it will
206
212
  * be used to perform decryption.
207
213
  */
@@ -262,31 +268,32 @@ ossl_to_der_if_possible(VALUE obj)
262
268
  /*
263
269
  * Errors
264
270
  */
265
- static VALUE
266
- ossl_make_error(VALUE exc, const char *fmt, va_list args)
271
+ VALUE
272
+ ossl_make_error(VALUE exc, VALUE str)
267
273
  {
268
- VALUE str = Qnil;
269
274
  unsigned long e;
275
+ const char *data;
276
+ int flags;
270
277
 
271
- if (fmt) {
272
- str = rb_vsprintf(fmt, args);
273
- }
274
- e = ERR_peek_last_error();
278
+ if (NIL_P(str))
279
+ str = rb_str_new(NULL, 0);
280
+
281
+ #ifdef HAVE_ERR_GET_ERROR_ALL
282
+ e = ERR_peek_last_error_all(NULL, NULL, NULL, &data, &flags);
283
+ #else
284
+ e = ERR_peek_last_error_line_data(NULL, NULL, &data, &flags);
285
+ #endif
275
286
  if (e) {
276
- const char *msg = ERR_reason_error_string(e);
287
+ const char *msg = ERR_reason_error_string(e);
277
288
 
278
- if (NIL_P(str)) {
279
- if (msg) str = rb_str_new_cstr(msg);
280
- }
281
- else {
282
- if (RSTRING_LEN(str)) rb_str_cat2(str, ": ");
283
- rb_str_cat2(str, msg ? msg : "(null)");
284
- }
285
- ossl_clear_error();
289
+ if (RSTRING_LEN(str)) rb_str_cat_cstr(str, ": ");
290
+ rb_str_cat_cstr(str, msg ? msg : "(null)");
291
+ if (flags & ERR_TXT_STRING && data)
292
+ rb_str_catf(str, " (%s)", data);
293
+ ossl_clear_error();
286
294
  }
287
295
 
288
- if (NIL_P(str)) str = rb_str_new(0, 0);
289
- return rb_exc_new3(exc, str);
296
+ return rb_exc_new_str(exc, str);
290
297
  }
291
298
 
292
299
  void
@@ -294,37 +301,48 @@ ossl_raise(VALUE exc, const char *fmt, ...)
294
301
  {
295
302
  va_list args;
296
303
  VALUE err;
297
- va_start(args, fmt);
298
- err = ossl_make_error(exc, fmt, args);
299
- va_end(args);
300
- rb_exc_raise(err);
304
+
305
+ if (fmt) {
306
+ va_start(args, fmt);
307
+ err = rb_vsprintf(fmt, args);
308
+ va_end(args);
309
+ }
310
+ else {
311
+ err = Qnil;
312
+ }
313
+
314
+ rb_exc_raise(ossl_make_error(exc, err));
301
315
  }
302
316
 
303
317
  void
304
318
  ossl_clear_error(void)
305
319
  {
306
320
  if (dOSSL == Qtrue) {
307
- unsigned long e;
308
- const char *file, *data, *errstr;
309
- int line, flags;
310
-
311
- while ((e = ERR_get_error_line_data(&file, &line, &data, &flags))) {
312
- errstr = ERR_error_string(e, NULL);
313
- if (!errstr)
314
- errstr = "(null)";
315
-
316
- if (flags & ERR_TXT_STRING) {
317
- if (!data)
318
- data = "(null)";
319
- rb_warn("error on stack: %s (%s)", errstr, data);
320
- }
321
- else {
322
- rb_warn("error on stack: %s", errstr);
323
- }
324
- }
321
+ unsigned long e;
322
+ const char *file, *data, *func, *lib, *reason;
323
+ char append[256] = "";
324
+ int line, flags;
325
+
326
+ #ifdef HAVE_ERR_GET_ERROR_ALL
327
+ while ((e = ERR_get_error_all(&file, &line, &func, &data, &flags))) {
328
+ #else
329
+ while ((e = ERR_get_error_line_data(&file, &line, &data, &flags))) {
330
+ func = ERR_func_error_string(e);
331
+ #endif
332
+ lib = ERR_lib_error_string(e);
333
+ reason = ERR_reason_error_string(e);
334
+
335
+ if (flags & ERR_TXT_STRING) {
336
+ if (!data)
337
+ data = "(null)";
338
+ snprintf(append, sizeof(append), " (%s)", data);
339
+ }
340
+ rb_warn("error on stack: error:%08lX:%s:%s:%s%s", e, lib ? lib : "",
341
+ func ? func : "", reason ? reason : "", append);
342
+ }
325
343
  }
326
344
  else {
327
- ERR_clear_error();
345
+ ERR_clear_error();
328
346
  }
329
347
  }
330
348
 
@@ -356,22 +374,6 @@ ossl_get_errors(VALUE _)
356
374
  */
357
375
  VALUE dOSSL;
358
376
 
359
- #if !defined(HAVE_VA_ARGS_MACRO)
360
- void
361
- ossl_debug(const char *fmt, ...)
362
- {
363
- va_list args;
364
-
365
- if (dOSSL == Qtrue) {
366
- fprintf(stderr, "OSSL_DEBUG: ");
367
- va_start(args, fmt);
368
- vfprintf(stderr, fmt, args);
369
- va_end(args);
370
- fprintf(stderr, " [CONTEXT N/A]\n");
371
- }
372
- }
373
- #endif
374
-
375
377
  /*
376
378
  * call-seq:
377
379
  * OpenSSL.debug -> true | false
@@ -386,7 +388,7 @@ ossl_debug_get(VALUE self)
386
388
  * call-seq:
387
389
  * OpenSSL.debug = boolean -> boolean
388
390
  *
389
- * Turns on or off debug mode. With debug mode, all erros added to the OpenSSL
391
+ * Turns on or off debug mode. With debug mode, all errors added to the OpenSSL
390
392
  * error queue will be printed to stderr.
391
393
  */
392
394
  static VALUE
@@ -405,7 +407,11 @@ static VALUE
405
407
  ossl_fips_mode_get(VALUE self)
406
408
  {
407
409
 
408
- #ifdef OPENSSL_FIPS
410
+ #if OSSL_OPENSSL_PREREQ(3, 0, 0)
411
+ VALUE enabled;
412
+ enabled = EVP_default_properties_is_fips_enabled(NULL) ? Qtrue : Qfalse;
413
+ return enabled;
414
+ #elif defined(OPENSSL_FIPS)
409
415
  VALUE enabled;
410
416
  enabled = FIPS_mode() ? Qtrue : Qfalse;
411
417
  return enabled;
@@ -429,8 +435,18 @@ ossl_fips_mode_get(VALUE self)
429
435
  static VALUE
430
436
  ossl_fips_mode_set(VALUE self, VALUE enabled)
431
437
  {
432
-
433
- #ifdef OPENSSL_FIPS
438
+ #if OSSL_OPENSSL_PREREQ(3, 0, 0)
439
+ if (RTEST(enabled)) {
440
+ if (!EVP_default_properties_enable_fips(NULL, 1)) {
441
+ ossl_raise(eOSSLError, "Turning on FIPS mode failed");
442
+ }
443
+ } else {
444
+ if (!EVP_default_properties_enable_fips(NULL, 0)) {
445
+ ossl_raise(eOSSLError, "Turning off FIPS mode failed");
446
+ }
447
+ }
448
+ return enabled;
449
+ #elif defined(OPENSSL_FIPS)
434
450
  if (RTEST(enabled)) {
435
451
  int mode = FIPS_mode();
436
452
  if(!mode && !FIPS_mode_set(1)) /* turning on twice leads to an error */
@@ -447,72 +463,6 @@ ossl_fips_mode_set(VALUE self, VALUE enabled)
447
463
  #endif
448
464
  }
449
465
 
450
- #if defined(OSSL_DEBUG)
451
- #if !defined(LIBRESSL_VERSION_NUMBER) && \
452
- (OPENSSL_VERSION_NUMBER >= 0x10100000 && !defined(OPENSSL_NO_CRYPTO_MDEBUG) || \
453
- defined(CRYPTO_malloc_debug_init))
454
- /*
455
- * call-seq:
456
- * OpenSSL.mem_check_start -> nil
457
- *
458
- * Calls CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON). Starts tracking memory
459
- * allocations. See also OpenSSL.print_mem_leaks.
460
- *
461
- * This is available only when built with a capable OpenSSL and --enable-debug
462
- * configure option.
463
- */
464
- static VALUE
465
- mem_check_start(VALUE self)
466
- {
467
- CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
468
- return Qnil;
469
- }
470
-
471
- /*
472
- * call-seq:
473
- * OpenSSL.print_mem_leaks -> true | false
474
- *
475
- * For debugging the Ruby/OpenSSL library. Calls CRYPTO_mem_leaks_fp(stderr).
476
- * Prints detected memory leaks to standard error. This cleans the global state
477
- * up thus you cannot use any methods of the library after calling this.
478
- *
479
- * Returns +true+ if leaks detected, +false+ otherwise.
480
- *
481
- * This is available only when built with a capable OpenSSL and --enable-debug
482
- * configure option.
483
- *
484
- * === Example
485
- * OpenSSL.mem_check_start
486
- * NOT_GCED = OpenSSL::PKey::RSA.new(256)
487
- *
488
- * END {
489
- * GC.start
490
- * OpenSSL.print_mem_leaks # will print the leakage
491
- * }
492
- */
493
- static VALUE
494
- print_mem_leaks(VALUE self)
495
- {
496
- #if OPENSSL_VERSION_NUMBER >= 0x10100000
497
- int ret;
498
- #endif
499
-
500
- BN_CTX_free(ossl_bn_ctx);
501
- ossl_bn_ctx = NULL;
502
-
503
- #if OPENSSL_VERSION_NUMBER >= 0x10100000
504
- ret = CRYPTO_mem_leaks_fp(stderr);
505
- if (ret < 0)
506
- ossl_raise(eOSSLError, "CRYPTO_mem_leaks_fp");
507
- return ret ? Qfalse : Qtrue;
508
- #else
509
- CRYPTO_mem_leaks_fp(stderr);
510
- return Qnil;
511
- #endif
512
- }
513
- #endif
514
- #endif
515
-
516
466
  #if !defined(HAVE_OPENSSL_110_THREADING_API)
517
467
  /**
518
468
  * Stores locks needed for OpenSSL thread safety
@@ -655,23 +605,21 @@ ossl_crypto_fixed_length_secure_compare(VALUE dummy, VALUE str1, VALUE str2)
655
605
  *
656
606
  * key = OpenSSL::PKey::RSA.new 2048
657
607
  *
658
- * open 'private_key.pem', 'w' do |io| io.write key.to_pem end
659
- * open 'public_key.pem', 'w' do |io| io.write key.public_key.to_pem end
608
+ * File.write 'private_key.pem', key.private_to_pem
609
+ * File.write 'public_key.pem', key.public_to_pem
660
610
  *
661
611
  * === Exporting a Key
662
612
  *
663
613
  * Keys saved to disk without encryption are not secure as anyone who gets
664
614
  * ahold of the key may use it unless it is encrypted. In order to securely
665
- * export a key you may export it with a pass phrase.
615
+ * export a key you may export it with a password.
666
616
  *
667
- * cipher = OpenSSL::Cipher.new 'AES-256-CBC'
668
- * pass_phrase = 'my secure pass phrase goes here'
617
+ * cipher = OpenSSL::Cipher.new 'aes-256-cbc'
618
+ * password = 'my secure password goes here'
669
619
  *
670
- * key_secure = key.export cipher, pass_phrase
620
+ * key_secure = key.private_to_pem cipher, password
671
621
  *
672
- * open 'private.secure.pem', 'w' do |io|
673
- * io.write key_secure
674
- * end
622
+ * File.write 'private.secure.pem', key_secure
675
623
  *
676
624
  * OpenSSL::Cipher.ciphers returns a list of available ciphers.
677
625
  *
@@ -679,25 +627,25 @@ ossl_crypto_fixed_length_secure_compare(VALUE dummy, VALUE str1, VALUE str2)
679
627
  *
680
628
  * A key can also be loaded from a file.
681
629
  *
682
- * key2 = OpenSSL::PKey::RSA.new File.read 'private_key.pem'
630
+ * key2 = OpenSSL::PKey.read File.read 'private_key.pem'
683
631
  * key2.public? # => true
684
632
  * key2.private? # => true
685
633
  *
686
634
  * or
687
635
  *
688
- * key3 = OpenSSL::PKey::RSA.new File.read 'public_key.pem'
636
+ * key3 = OpenSSL::PKey.read File.read 'public_key.pem'
689
637
  * key3.public? # => true
690
638
  * key3.private? # => false
691
639
  *
692
640
  * === Loading an Encrypted Key
693
641
  *
694
- * OpenSSL will prompt you for your pass phrase when loading an encrypted key.
695
- * If you will not be able to type in the pass phrase you may provide it when
642
+ * OpenSSL will prompt you for your password when loading an encrypted key.
643
+ * If you will not be able to type in the password you may provide it when
696
644
  * loading the key:
697
645
  *
698
646
  * key4_pem = File.read 'private.secure.pem'
699
- * pass_phrase = 'my secure pass phrase goes here'
700
- * key4 = OpenSSL::PKey::RSA.new key4_pem, pass_phrase
647
+ * password = 'my secure password goes here'
648
+ * key4 = OpenSSL::PKey.read key4_pem, password
701
649
  *
702
650
  * == RSA Encryption
703
651
  *
@@ -772,7 +720,7 @@ ossl_crypto_fixed_length_secure_compare(VALUE dummy, VALUE str1, VALUE str2)
772
720
  * using PBKDF2. PKCS #5 v2.0 recommends at least 8 bytes for the salt,
773
721
  * the number of iterations largely depends on the hardware being used.
774
722
  *
775
- * cipher = OpenSSL::Cipher.new 'AES-256-CBC'
723
+ * cipher = OpenSSL::Cipher.new 'aes-256-cbc'
776
724
  * cipher.encrypt
777
725
  * iv = cipher.random_iv
778
726
  *
@@ -795,7 +743,7 @@ ossl_crypto_fixed_length_secure_compare(VALUE dummy, VALUE str1, VALUE str2)
795
743
  * Use the same steps as before to derive the symmetric AES key, this time
796
744
  * setting the Cipher up for decryption.
797
745
  *
798
- * cipher = OpenSSL::Cipher.new 'AES-256-CBC'
746
+ * cipher = OpenSSL::Cipher.new 'aes-256-cbc'
799
747
  * cipher.decrypt
800
748
  * cipher.iv = iv # the one generated with #random_iv
801
749
  *
@@ -813,45 +761,6 @@ ossl_crypto_fixed_length_secure_compare(VALUE dummy, VALUE str1, VALUE str2)
813
761
  * decrypted = cipher.update encrypted
814
762
  * decrypted << cipher.final
815
763
  *
816
- * == PKCS #5 Password-based Encryption
817
- *
818
- * PKCS #5 is a password-based encryption standard documented at
819
- * RFC2898[http://www.ietf.org/rfc/rfc2898.txt]. It allows a short password or
820
- * passphrase to be used to create a secure encryption key. If possible, PBKDF2
821
- * as described above should be used if the circumstances allow it.
822
- *
823
- * PKCS #5 uses a Cipher, a pass phrase and a salt to generate an encryption
824
- * key.
825
- *
826
- * pass_phrase = 'my secure pass phrase goes here'
827
- * salt = '8 octets'
828
- *
829
- * === Encryption
830
- *
831
- * First set up the cipher for encryption
832
- *
833
- * encryptor = OpenSSL::Cipher.new 'AES-256-CBC'
834
- * encryptor.encrypt
835
- * encryptor.pkcs5_keyivgen pass_phrase, salt
836
- *
837
- * Then pass the data you want to encrypt through
838
- *
839
- * encrypted = encryptor.update 'top secret document'
840
- * encrypted << encryptor.final
841
- *
842
- * === Decryption
843
- *
844
- * Use a new Cipher instance set up for decryption
845
- *
846
- * decryptor = OpenSSL::Cipher.new 'AES-256-CBC'
847
- * decryptor.decrypt
848
- * decryptor.pkcs5_keyivgen pass_phrase, salt
849
- *
850
- * Then pass the data you want to decrypt through
851
- *
852
- * plain = decryptor.update encrypted
853
- * plain << decryptor.final
854
- *
855
764
  * == X509 Certificates
856
765
  *
857
766
  * === Creating a Certificate
@@ -929,12 +838,12 @@ ossl_crypto_fixed_length_secure_compare(VALUE dummy, VALUE str1, VALUE str2)
929
838
  * not readable by other users.
930
839
  *
931
840
  * ca_key = OpenSSL::PKey::RSA.new 2048
932
- * pass_phrase = 'my secure pass phrase goes here'
841
+ * password = 'my secure password goes here'
933
842
  *
934
- * cipher = OpenSSL::Cipher.new 'AES-256-CBC'
843
+ * cipher = 'aes-256-cbc'
935
844
  *
936
845
  * open 'ca_key.pem', 'w', 0400 do |io|
937
- * io.write ca_key.export(cipher, pass_phrase)
846
+ * io.write ca_key.private_to_pem(cipher, password)
938
847
  * end
939
848
  *
940
849
  * === CA Certificate
@@ -1069,13 +978,13 @@ ossl_crypto_fixed_length_secure_compare(VALUE dummy, VALUE str1, VALUE str2)
1069
978
  * loop do
1070
979
  * ssl_connection = ssl_server.accept
1071
980
  *
1072
- * data = connection.gets
981
+ * data = ssl_connection.gets
1073
982
  *
1074
983
  * response = "I got #{data.dump}"
1075
984
  * puts response
1076
985
  *
1077
- * connection.puts "I got #{data.dump}"
1078
- * connection.close
986
+ * ssl_connection.puts "I got #{data.dump}"
987
+ * ssl_connection.close
1079
988
  * end
1080
989
  *
1081
990
  * === SSL client
@@ -1126,6 +1035,10 @@ ossl_crypto_fixed_length_secure_compare(VALUE dummy, VALUE str1, VALUE str2)
1126
1035
  void
1127
1036
  Init_openssl(void)
1128
1037
  {
1038
+ #ifdef HAVE_RB_EXT_RACTOR_SAFE
1039
+ rb_ext_ractor_safe(true);
1040
+ #endif
1041
+
1129
1042
  #undef rb_intern
1130
1043
  /*
1131
1044
  * Init timezone info
@@ -1150,8 +1063,8 @@ Init_openssl(void)
1150
1063
  /*
1151
1064
  * Init main module
1152
1065
  */
1153
- mOSSL = rb_define_module("OpenSSL");
1154
1066
  rb_global_variable(&mOSSL);
1067
+ mOSSL = rb_define_module("OpenSSL");
1155
1068
  rb_define_singleton_method(mOSSL, "fixed_length_secure_compare", ossl_crypto_fixed_length_secure_compare, 2);
1156
1069
 
1157
1070
  /*
@@ -1170,15 +1083,35 @@ Init_openssl(void)
1170
1083
 
1171
1084
  /*
1172
1085
  * Version number of OpenSSL the ruby OpenSSL extension was built with
1173
- * (base 16)
1086
+ * (base 16). The formats are below.
1087
+ *
1088
+ * [OpenSSL 3] <tt>0xMNN00PP0 (major minor 00 patch 0)</tt>
1089
+ * [OpenSSL before 3] <tt>0xMNNFFPPS (major minor fix patch status)</tt>
1090
+ * [LibreSSL] <tt>0x20000000 (fixed value)</tt>
1091
+ *
1092
+ * See also the man page OPENSSL_VERSION_NUMBER(3).
1174
1093
  */
1175
1094
  rb_define_const(mOSSL, "OPENSSL_VERSION_NUMBER", INT2NUM(OPENSSL_VERSION_NUMBER));
1176
1095
 
1096
+ #if defined(LIBRESSL_VERSION_NUMBER)
1097
+ /*
1098
+ * Version number of LibreSSL the ruby OpenSSL extension was built with
1099
+ * (base 16). The format is <tt>0xMNNFF00f (major minor fix 00
1100
+ * status)</tt>. This constant is only defined in LibreSSL cases.
1101
+ *
1102
+ * See also the man page LIBRESSL_VERSION_NUMBER(3).
1103
+ */
1104
+ rb_define_const(mOSSL, "LIBRESSL_VERSION_NUMBER", INT2NUM(LIBRESSL_VERSION_NUMBER));
1105
+ #endif
1106
+
1177
1107
  /*
1178
1108
  * Boolean indicating whether OpenSSL is FIPS-capable or not
1179
1109
  */
1180
1110
  rb_define_const(mOSSL, "OPENSSL_FIPS",
1181
- #ifdef OPENSSL_FIPS
1111
+ /* OpenSSL 3 is FIPS-capable even when it is installed without fips option */
1112
+ #if OSSL_OPENSSL_PREREQ(3, 0, 0)
1113
+ Qtrue
1114
+ #elif defined(OPENSSL_FIPS)
1182
1115
  Qtrue
1183
1116
  #else
1184
1117
  Qfalse
@@ -1188,12 +1121,12 @@ Init_openssl(void)
1188
1121
  rb_define_module_function(mOSSL, "fips_mode", ossl_fips_mode_get, 0);
1189
1122
  rb_define_module_function(mOSSL, "fips_mode=", ossl_fips_mode_set, 1);
1190
1123
 
1124
+ rb_global_variable(&eOSSLError);
1191
1125
  /*
1192
1126
  * Generic error,
1193
1127
  * common for all classes under OpenSSL module
1194
1128
  */
1195
1129
  eOSSLError = rb_define_class_under(mOSSL,"OpenSSLError",rb_eStandardError);
1196
- rb_global_variable(&eOSSLError);
1197
1130
 
1198
1131
  /*
1199
1132
  * Init debug core
@@ -1234,42 +1167,7 @@ Init_openssl(void)
1234
1167
  Init_ossl_x509();
1235
1168
  Init_ossl_ocsp();
1236
1169
  Init_ossl_engine();
1170
+ Init_ossl_provider();
1237
1171
  Init_ossl_asn1();
1238
1172
  Init_ossl_kdf();
1239
-
1240
- #if defined(OSSL_DEBUG)
1241
- /*
1242
- * For debugging Ruby/OpenSSL. Enable only when built with --enable-debug
1243
- */
1244
- #if !defined(LIBRESSL_VERSION_NUMBER) && \
1245
- (OPENSSL_VERSION_NUMBER >= 0x10100000 && !defined(OPENSSL_NO_CRYPTO_MDEBUG) || \
1246
- defined(CRYPTO_malloc_debug_init))
1247
- rb_define_module_function(mOSSL, "mem_check_start", mem_check_start, 0);
1248
- rb_define_module_function(mOSSL, "print_mem_leaks", print_mem_leaks, 0);
1249
-
1250
- #if defined(CRYPTO_malloc_debug_init) /* <= 1.0.2 */
1251
- CRYPTO_malloc_debug_init();
1252
- #endif
1253
-
1254
- #if defined(V_CRYPTO_MDEBUG_ALL) /* <= 1.0.2 */
1255
- CRYPTO_set_mem_debug_options(V_CRYPTO_MDEBUG_ALL);
1256
- #endif
1257
-
1258
- #if OPENSSL_VERSION_NUMBER < 0x10100000 /* <= 1.0.2 */
1259
- {
1260
- int i;
1261
- /*
1262
- * See crypto/ex_data.c; call def_get_class() immediately to avoid
1263
- * allocations. 15 is the maximum number that is used as the class index
1264
- * in OpenSSL 1.0.2.
1265
- */
1266
- for (i = 0; i <= 15; i++) {
1267
- if (CRYPTO_get_ex_new_index(i, 0, (void *)"ossl-mdebug-dummy", 0, 0, 0) < 0)
1268
- rb_raise(rb_eRuntimeError, "CRYPTO_get_ex_new_index for "
1269
- "class index %d failed", i);
1270
- }
1271
- }
1272
- #endif
1273
- #endif
1274
- #endif
1275
1173
  }
data/ext/openssl/ossl.h CHANGED
@@ -18,22 +18,19 @@
18
18
  #include <ruby/io.h>
19
19
  #include <ruby/thread.h>
20
20
  #include <openssl/opensslv.h>
21
+
21
22
  #include <openssl/err.h>
22
23
  #include <openssl/asn1.h>
23
24
  #include <openssl/x509v3.h>
24
25
  #include <openssl/ssl.h>
25
26
  #include <openssl/pkcs12.h>
26
27
  #include <openssl/pkcs7.h>
27
- #include <openssl/hmac.h>
28
28
  #include <openssl/rand.h>
29
29
  #include <openssl/conf.h>
30
30
  #ifndef OPENSSL_NO_TS
31
31
  #include <openssl/ts.h>
32
32
  #endif
33
33
  #include <openssl/crypto.h>
34
- #if !defined(OPENSSL_NO_ENGINE)
35
- # include <openssl/engine.h>
36
- #endif
37
34
  #if !defined(OPENSSL_NO_OCSP)
38
35
  # include <openssl/ocsp.h>
39
36
  #endif
@@ -43,6 +40,32 @@
43
40
  #include <openssl/evp.h>
44
41
  #include <openssl/dh.h>
45
42
 
43
+ #ifndef LIBRESSL_VERSION_NUMBER
44
+ # define OSSL_IS_LIBRESSL 0
45
+ # define OSSL_OPENSSL_PREREQ(maj, min, pat) \
46
+ (OPENSSL_VERSION_NUMBER >= ((maj << 28) | (min << 20) | (pat << 12)))
47
+ # define OSSL_LIBRESSL_PREREQ(maj, min, pat) 0
48
+ #else
49
+ # define OSSL_IS_LIBRESSL 1
50
+ # define OSSL_OPENSSL_PREREQ(maj, min, pat) 0
51
+ # define OSSL_LIBRESSL_PREREQ(maj, min, pat) \
52
+ (LIBRESSL_VERSION_NUMBER >= ((maj << 28) | (min << 20) | (pat << 12)))
53
+ #endif
54
+
55
+ #if OSSL_OPENSSL_PREREQ(3, 0, 0)
56
+ # define OSSL_3_const const
57
+ #else
58
+ # define OSSL_3_const /* const */
59
+ #endif
60
+
61
+ #if !defined(OPENSSL_NO_ENGINE) && !OSSL_OPENSSL_PREREQ(3, 0, 0)
62
+ # define OSSL_USE_ENGINE
63
+ #endif
64
+
65
+ #if OSSL_OPENSSL_PREREQ(3, 0, 0)
66
+ # define OSSL_USE_PROVIDER
67
+ #endif
68
+
46
69
  /*
47
70
  * Common Module
48
71
  */
@@ -121,7 +144,9 @@ int ossl_pem_passwd_cb(char *, int, int, void *);
121
144
  /*
122
145
  * ERRor messages
123
146
  */
124
- NORETURN(void ossl_raise(VALUE, const char *, ...));
147
+ PRINTF_ARGS(NORETURN(void ossl_raise(VALUE, const char *, ...)), 2, 3);
148
+ /* Make exception instance from str and OpenSSL error reason string. */
149
+ VALUE ossl_make_error(VALUE exc, VALUE str);
125
150
  /* Clear OpenSSL error queue. If dOSSL is set, rb_warn() them. */
126
151
  void ossl_clear_error(void);
127
152
 
@@ -136,7 +161,6 @@ VALUE ossl_to_der_if_possible(VALUE);
136
161
  */
137
162
  extern VALUE dOSSL;
138
163
 
139
- #if defined(HAVE_VA_ARGS_MACRO)
140
164
  #define OSSL_Debug(...) do { \
141
165
  if (dOSSL == Qtrue) { \
142
166
  fprintf(stderr, "OSSL_DEBUG: "); \
@@ -145,16 +169,10 @@ extern VALUE dOSSL;
145
169
  } \
146
170
  } while (0)
147
171
 
148
- #else
149
- void ossl_debug(const char *, ...);
150
- #define OSSL_Debug ossl_debug
151
- #endif
152
-
153
172
  /*
154
173
  * Include all parts
155
174
  */
156
175
  #include "openssl_missing.h"
157
- #include "ruby_missing.h"
158
176
  #include "ossl_asn1.h"
159
177
  #include "ossl_bio.h"
160
178
  #include "ossl_bn.h"
@@ -174,6 +192,7 @@ void ossl_debug(const char *, ...);
174
192
  #endif
175
193
  #include "ossl_x509.h"
176
194
  #include "ossl_engine.h"
195
+ #include "ossl_provider.h"
177
196
  #include "ossl_kdf.h"
178
197
 
179
198
  void Init_openssl(void);