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.
Files changed (55) hide show
  1. checksums.yaml +4 -4
  2. data/CONTRIBUTING.md +3 -0
  3. data/History.md +85 -0
  4. data/README.md +12 -11
  5. data/ext/openssl/extconf.rb +30 -69
  6. data/ext/openssl/openssl_missing.h +0 -206
  7. data/ext/openssl/ossl.c +280 -301
  8. data/ext/openssl/ossl.h +15 -10
  9. data/ext/openssl/ossl_asn1.c +598 -406
  10. data/ext/openssl/ossl_asn1.h +15 -1
  11. data/ext/openssl/ossl_bio.c +3 -3
  12. data/ext/openssl/ossl_bn.c +286 -291
  13. data/ext/openssl/ossl_cipher.c +252 -203
  14. data/ext/openssl/ossl_cipher.h +10 -1
  15. data/ext/openssl/ossl_config.c +1 -6
  16. data/ext/openssl/ossl_digest.c +74 -43
  17. data/ext/openssl/ossl_digest.h +9 -1
  18. data/ext/openssl/ossl_engine.c +39 -103
  19. data/ext/openssl/ossl_hmac.c +30 -36
  20. data/ext/openssl/ossl_kdf.c +42 -53
  21. data/ext/openssl/ossl_ns_spki.c +31 -37
  22. data/ext/openssl/ossl_ocsp.c +214 -241
  23. data/ext/openssl/ossl_pkcs12.c +26 -26
  24. data/ext/openssl/ossl_pkcs7.c +175 -145
  25. data/ext/openssl/ossl_pkey.c +162 -178
  26. data/ext/openssl/ossl_pkey.h +99 -99
  27. data/ext/openssl/ossl_pkey_dh.c +31 -68
  28. data/ext/openssl/ossl_pkey_dsa.c +15 -54
  29. data/ext/openssl/ossl_pkey_ec.c +179 -237
  30. data/ext/openssl/ossl_pkey_rsa.c +56 -103
  31. data/ext/openssl/ossl_provider.c +0 -7
  32. data/ext/openssl/ossl_rand.c +7 -14
  33. data/ext/openssl/ossl_ssl.c +478 -353
  34. data/ext/openssl/ossl_ssl.h +8 -8
  35. data/ext/openssl/ossl_ssl_session.c +93 -97
  36. data/ext/openssl/ossl_ts.c +81 -127
  37. data/ext/openssl/ossl_x509.c +9 -28
  38. data/ext/openssl/ossl_x509attr.c +33 -54
  39. data/ext/openssl/ossl_x509cert.c +69 -100
  40. data/ext/openssl/ossl_x509crl.c +78 -89
  41. data/ext/openssl/ossl_x509ext.c +45 -66
  42. data/ext/openssl/ossl_x509name.c +63 -88
  43. data/ext/openssl/ossl_x509req.c +55 -62
  44. data/ext/openssl/ossl_x509revoked.c +27 -41
  45. data/ext/openssl/ossl_x509store.c +38 -56
  46. data/lib/openssl/buffering.rb +30 -24
  47. data/lib/openssl/digest.rb +1 -1
  48. data/lib/openssl/pkey.rb +71 -49
  49. data/lib/openssl/ssl.rb +12 -79
  50. data/lib/openssl/version.rb +2 -1
  51. data/lib/openssl/x509.rb +9 -0
  52. data/lib/openssl.rb +9 -6
  53. metadata +1 -3
  54. data/ext/openssl/openssl_missing.c +0 -40
  55. data/lib/openssl/asn1.rb +0 -188
@@ -13,14 +13,14 @@
13
13
  TypedData_Wrap_Struct((klass), &ossl_x509_type, 0)
14
14
  #define SetX509(obj, x509) do { \
15
15
  if (!(x509)) { \
16
- ossl_raise(rb_eRuntimeError, "CERT wasn't initialized!"); \
16
+ ossl_raise(rb_eRuntimeError, "CERT wasn't initialized!"); \
17
17
  } \
18
18
  RTYPEDDATA_DATA(obj) = (x509); \
19
19
  } while (0)
20
20
  #define GetX509(obj, x509) do { \
21
21
  TypedData_Get_Struct((obj), X509, &ossl_x509_type, (x509)); \
22
22
  if (!(x509)) { \
23
- ossl_raise(rb_eRuntimeError, "CERT wasn't initialized!"); \
23
+ ossl_raise(rb_eRuntimeError, "CERT wasn't initialized!"); \
24
24
  } \
25
25
  } while (0)
26
26
 
@@ -39,7 +39,7 @@ ossl_x509_free(void *ptr)
39
39
  static const rb_data_type_t ossl_x509_type = {
40
40
  "OpenSSL/X509",
41
41
  {
42
- 0, ossl_x509_free,
42
+ 0, ossl_x509_free,
43
43
  },
44
44
  0, 0, RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_WB_PROTECTED,
45
45
  };
@@ -54,14 +54,9 @@ ossl_x509_new(X509 *x509)
54
54
  VALUE obj;
55
55
 
56
56
  obj = NewX509(cX509Cert);
57
- if (!x509) {
58
- new = X509_new();
59
- } else {
60
- new = X509_dup(x509);
61
- }
62
- if (!new) {
63
- ossl_raise(eX509CertError, NULL);
64
- }
57
+ new = X509_dup(x509);
58
+ if (!new)
59
+ ossl_raise(eX509CertError, "X509_dup");
65
60
  SetX509(obj, new);
66
61
 
67
62
  return obj;
@@ -120,8 +115,8 @@ ossl_x509_initialize(int argc, VALUE *argv, VALUE self)
120
115
 
121
116
  rb_check_frozen(self);
122
117
  if (rb_scan_args(argc, argv, "01", &arg) == 0) {
123
- /* create just empty X509Cert */
124
- return self;
118
+ /* create just empty X509Cert */
119
+ return self;
125
120
  }
126
121
  arg = ossl_to_der_if_possible(arg);
127
122
  in = ossl_obj2bio(&arg);
@@ -140,6 +135,7 @@ ossl_x509_initialize(int argc, VALUE *argv, VALUE self)
140
135
  return self;
141
136
  }
142
137
 
138
+ /* :nodoc: */
143
139
  static VALUE
144
140
  ossl_x509_copy(VALUE self, VALUE other)
145
141
  {
@@ -174,11 +170,11 @@ ossl_x509_to_der(VALUE self)
174
170
 
175
171
  GetX509(self, x509);
176
172
  if ((len = i2d_X509(x509, NULL)) <= 0)
177
- ossl_raise(eX509CertError, NULL);
173
+ ossl_raise(eX509CertError, NULL);
178
174
  str = rb_str_new(0, len);
179
175
  p = (unsigned char *)RSTRING_PTR(str);
180
176
  if (i2d_X509(x509, &p) <= 0)
181
- ossl_raise(eX509CertError, NULL);
177
+ ossl_raise(eX509CertError, NULL);
182
178
  ossl_str_adjust(str, p);
183
179
 
184
180
  return str;
@@ -200,8 +196,8 @@ ossl_x509_to_pem(VALUE self)
200
196
  if (!out) ossl_raise(eX509CertError, NULL);
201
197
 
202
198
  if (!PEM_write_bio_X509(out, x509)) {
203
- BIO_free(out);
204
- ossl_raise(eX509CertError, NULL);
199
+ BIO_free(out);
200
+ ossl_raise(eX509CertError, NULL);
205
201
  }
206
202
  str = ossl_membio2str(out);
207
203
 
@@ -225,8 +221,8 @@ ossl_x509_to_text(VALUE self)
225
221
  if (!out) ossl_raise(eX509CertError, NULL);
226
222
 
227
223
  if (!X509_print(out, x509)) {
228
- BIO_free(out);
229
- ossl_raise(eX509CertError, NULL);
224
+ BIO_free(out);
225
+ ossl_raise(eX509CertError, NULL);
230
226
  }
231
227
  str = ossl_membio2str(out);
232
228
 
@@ -246,7 +242,7 @@ ossl_x509_to_req(VALUE self)
246
242
 
247
243
  GetX509(self, x509);
248
244
  if (!(req = X509_to_X509_REQ(x509, NULL, EVP_md5()))) {
249
- ossl_raise(eX509CertError, NULL);
245
+ ossl_raise(eX509CertError, NULL);
250
246
  }
251
247
  obj = ossl_x509req_new(req);
252
248
  X509_REQ_free(req);
@@ -280,11 +276,11 @@ ossl_x509_set_version(VALUE self, VALUE version)
280
276
  long ver;
281
277
 
282
278
  if ((ver = NUM2LONG(version)) < 0) {
283
- ossl_raise(eX509CertError, "version must be >= 0!");
279
+ ossl_raise(eX509CertError, "version must be >= 0!");
284
280
  }
285
281
  GetX509(self, x509);
286
282
  if (!X509_set_version(x509, ver)) {
287
- ossl_raise(eX509CertError, NULL);
283
+ ossl_raise(eX509CertError, NULL);
288
284
  }
289
285
 
290
286
  return version;
@@ -322,25 +318,23 @@ ossl_x509_set_serial(VALUE self, VALUE num)
322
318
  /*
323
319
  * call-seq:
324
320
  * cert.signature_algorithm => string
321
+ *
322
+ * Returns the signature algorithm used to sign this certificate. This returns
323
+ * the algorithm name found in the TBSCertificate structure, not the outer
324
+ * \Certificate structure.
325
+ *
326
+ * Returns the long name of the signature algorithm, or the dotted decimal
327
+ * notation if \OpenSSL does not define a long name for it.
325
328
  */
326
329
  static VALUE
327
330
  ossl_x509_get_signature_algorithm(VALUE self)
328
331
  {
329
332
  X509 *x509;
330
- BIO *out;
331
- VALUE str;
333
+ const ASN1_OBJECT *obj;
332
334
 
333
335
  GetX509(self, x509);
334
- out = BIO_new(BIO_s_mem());
335
- if (!out) ossl_raise(eX509CertError, NULL);
336
-
337
- if (!i2a_ASN1_OBJECT(out, X509_get0_tbs_sigalg(x509)->algorithm)) {
338
- BIO_free(out);
339
- ossl_raise(eX509CertError, NULL);
340
- }
341
- str = ossl_membio2str(out);
342
-
343
- return str;
336
+ X509_ALGOR_get0(&obj, NULL, NULL, X509_get0_tbs_sigalg(x509));
337
+ return ossl_asn1obj_to_string_long_name(obj);
344
338
  }
345
339
 
346
340
  /*
@@ -355,7 +349,7 @@ ossl_x509_get_subject(VALUE self)
355
349
 
356
350
  GetX509(self, x509);
357
351
  if (!(name = X509_get_subject_name(x509))) { /* NO DUP - don't free! */
358
- ossl_raise(eX509CertError, NULL);
352
+ ossl_raise(eX509CertError, NULL);
359
353
  }
360
354
 
361
355
  return ossl_x509name_new(name);
@@ -372,7 +366,7 @@ ossl_x509_set_subject(VALUE self, VALUE subject)
372
366
 
373
367
  GetX509(self, x509);
374
368
  if (!X509_set_subject_name(x509, GetX509NamePtr(subject))) { /* DUPs name */
375
- ossl_raise(eX509CertError, NULL);
369
+ ossl_raise(eX509CertError, NULL);
376
370
  }
377
371
 
378
372
  return subject;
@@ -390,7 +384,7 @@ ossl_x509_get_issuer(VALUE self)
390
384
 
391
385
  GetX509(self, x509);
392
386
  if(!(name = X509_get_issuer_name(x509))) { /* NO DUP - don't free! */
393
- ossl_raise(eX509CertError, NULL);
387
+ ossl_raise(eX509CertError, NULL);
394
388
  }
395
389
 
396
390
  return ossl_x509name_new(name);
@@ -407,7 +401,7 @@ ossl_x509_set_issuer(VALUE self, VALUE issuer)
407
401
 
408
402
  GetX509(self, x509);
409
403
  if (!X509_set_issuer_name(x509, GetX509NamePtr(issuer))) { /* DUPs name */
410
- ossl_raise(eX509CertError, NULL);
404
+ ossl_raise(eX509CertError, NULL);
411
405
  }
412
406
 
413
407
  return issuer;
@@ -425,7 +419,7 @@ ossl_x509_get_not_before(VALUE self)
425
419
 
426
420
  GetX509(self, x509);
427
421
  if (!(asn1time = X509_get0_notBefore(x509))) {
428
- ossl_raise(eX509CertError, NULL);
422
+ ossl_raise(eX509CertError, NULL);
429
423
  }
430
424
 
431
425
  return asn1time_to_time(asn1time);
@@ -444,8 +438,8 @@ ossl_x509_set_not_before(VALUE self, VALUE time)
444
438
  GetX509(self, x509);
445
439
  asn1time = ossl_x509_time_adjust(NULL, time);
446
440
  if (!X509_set1_notBefore(x509, asn1time)) {
447
- ASN1_TIME_free(asn1time);
448
- ossl_raise(eX509CertError, "X509_set_notBefore");
441
+ ASN1_TIME_free(asn1time);
442
+ ossl_raise(eX509CertError, "X509_set_notBefore");
449
443
  }
450
444
  ASN1_TIME_free(asn1time);
451
445
 
@@ -464,7 +458,7 @@ ossl_x509_get_not_after(VALUE self)
464
458
 
465
459
  GetX509(self, x509);
466
460
  if (!(asn1time = X509_get0_notAfter(x509))) {
467
- ossl_raise(eX509CertError, NULL);
461
+ ossl_raise(eX509CertError, NULL);
468
462
  }
469
463
 
470
464
  return asn1time_to_time(asn1time);
@@ -483,8 +477,8 @@ ossl_x509_set_not_after(VALUE self, VALUE time)
483
477
  GetX509(self, x509);
484
478
  asn1time = ossl_x509_time_adjust(NULL, time);
485
479
  if (!X509_set1_notAfter(x509, asn1time)) {
486
- ASN1_TIME_free(asn1time);
487
- ossl_raise(eX509CertError, "X509_set_notAfter");
480
+ ASN1_TIME_free(asn1time);
481
+ ossl_raise(eX509CertError, "X509_set_notAfter");
488
482
  }
489
483
  ASN1_TIME_free(asn1time);
490
484
 
@@ -503,10 +497,10 @@ ossl_x509_get_public_key(VALUE self)
503
497
 
504
498
  GetX509(self, x509);
505
499
  if (!(pkey = X509_get_pubkey(x509))) { /* adds an reference */
506
- ossl_raise(eX509CertError, NULL);
500
+ ossl_raise(eX509CertError, NULL);
507
501
  }
508
502
 
509
- return ossl_pkey_new(pkey); /* NO DUP - OK */
503
+ return ossl_pkey_wrap(pkey);
510
504
  }
511
505
 
512
506
  /*
@@ -523,7 +517,7 @@ ossl_x509_set_public_key(VALUE self, VALUE key)
523
517
  pkey = GetPKeyPtr(key);
524
518
  ossl_pkey_check_public_key(pkey);
525
519
  if (!X509_set_pubkey(x509, pkey))
526
- ossl_raise(eX509CertError, "X509_set_pubkey");
520
+ ossl_raise(eX509CertError, "X509_set_pubkey");
527
521
  return key;
528
522
  }
529
523
 
@@ -537,17 +531,14 @@ ossl_x509_sign(VALUE self, VALUE key, VALUE digest)
537
531
  X509 *x509;
538
532
  EVP_PKEY *pkey;
539
533
  const EVP_MD *md;
534
+ VALUE md_holder;
540
535
 
541
536
  pkey = GetPrivPKeyPtr(key); /* NO NEED TO DUP */
542
- if (NIL_P(digest)) {
543
- md = NULL; /* needed for some key types, e.g. Ed25519 */
544
- } else {
545
- md = ossl_evp_get_digestbyname(digest);
546
- }
537
+ /* NULL needed for some key types, e.g. Ed25519 */
538
+ md = NIL_P(digest) ? NULL : ossl_evp_md_fetch(digest, &md_holder);
547
539
  GetX509(self, x509);
548
- if (!X509_sign(x509, pkey, md)) {
549
- ossl_raise(eX509CertError, NULL);
550
- }
540
+ if (!X509_sign(x509, pkey, md))
541
+ ossl_raise(eX509CertError, "X509_sign");
551
542
 
552
543
  return self;
553
544
  }
@@ -570,12 +561,12 @@ ossl_x509_verify(VALUE self, VALUE key)
570
561
  ossl_pkey_check_public_key(pkey);
571
562
  switch (X509_verify(x509, pkey)) {
572
563
  case 1:
573
- return Qtrue;
564
+ return Qtrue;
574
565
  case 0:
575
- ossl_clear_error();
576
- return Qfalse;
566
+ ossl_clear_error();
567
+ return Qfalse;
577
568
  default:
578
- ossl_raise(eX509CertError, NULL);
569
+ ossl_raise(eX509CertError, NULL);
579
570
  }
580
571
  }
581
572
 
@@ -596,8 +587,8 @@ ossl_x509_check_private_key(VALUE self, VALUE key)
596
587
  pkey = GetPrivPKeyPtr(key); /* NO NEED TO DUP */
597
588
  GetX509(self, x509);
598
589
  if (!X509_check_private_key(x509, pkey)) {
599
- ossl_clear_error();
600
- return Qfalse;
590
+ ossl_clear_error();
591
+ return Qfalse;
601
592
  }
602
593
 
603
594
  return Qtrue;
@@ -617,13 +608,10 @@ ossl_x509_get_extensions(VALUE self)
617
608
 
618
609
  GetX509(self, x509);
619
610
  count = X509_get_ext_count(x509);
620
- if (count < 0) {
621
- return rb_ary_new();
622
- }
623
- ary = rb_ary_new2(count);
611
+ ary = rb_ary_new_capa(count);
624
612
  for (i=0; i<count; i++) {
625
- ext = X509_get_ext(x509, i); /* NO DUP - don't free! */
626
- rb_ary_push(ary, ossl_x509ext_new(ext));
613
+ ext = X509_get_ext(x509, i); /* NO DUP - don't free! */
614
+ rb_ary_push(ary, ossl_x509ext_new(ext));
627
615
  }
628
616
 
629
617
  return ary;
@@ -643,16 +631,16 @@ ossl_x509_set_extensions(VALUE self, VALUE ary)
643
631
  Check_Type(ary, T_ARRAY);
644
632
  /* All ary's members should be X509Extension */
645
633
  for (i=0; i<RARRAY_LEN(ary); i++) {
646
- OSSL_Check_Kind(RARRAY_AREF(ary, i), cX509Ext);
634
+ OSSL_Check_Kind(RARRAY_AREF(ary, i), cX509Ext);
647
635
  }
648
636
  GetX509(self, x509);
649
637
  for (i = X509_get_ext_count(x509); i > 0; i--)
650
638
  X509_EXTENSION_free(X509_delete_ext(x509, 0));
651
639
  for (i=0; i<RARRAY_LEN(ary); i++) {
652
- ext = GetX509ExtPtr(RARRAY_AREF(ary, i));
653
- if (!X509_add_ext(x509, ext, -1)) { /* DUPs ext */
654
- ossl_raise(eX509CertError, "X509_add_ext");
655
- }
640
+ ext = GetX509ExtPtr(RARRAY_AREF(ary, i));
641
+ if (!X509_add_ext(x509, ext, -1)) { /* DUPs ext */
642
+ ossl_raise(eX509CertError, "X509_add_ext");
643
+ }
656
644
  }
657
645
 
658
646
  return ary;
@@ -671,32 +659,24 @@ ossl_x509_add_extension(VALUE self, VALUE extension)
671
659
  GetX509(self, x509);
672
660
  ext = GetX509ExtPtr(extension);
673
661
  if (!X509_add_ext(x509, ext, -1)) { /* DUPs ext - FREE it */
674
- ossl_raise(eX509CertError, NULL);
662
+ ossl_raise(eX509CertError, NULL);
675
663
  }
676
664
 
677
665
  return extension;
678
666
  }
679
667
 
680
- static VALUE
681
- ossl_x509_inspect(VALUE self)
682
- {
683
- return rb_sprintf("#<%"PRIsVALUE": subject=%+"PRIsVALUE", "
684
- "issuer=%+"PRIsVALUE", serial=%+"PRIsVALUE", "
685
- "not_before=%+"PRIsVALUE", not_after=%+"PRIsVALUE">",
686
- rb_obj_class(self),
687
- ossl_x509_get_subject(self),
688
- ossl_x509_get_issuer(self),
689
- ossl_x509_get_serial(self),
690
- ossl_x509_get_not_before(self),
691
- ossl_x509_get_not_after(self));
692
- }
693
-
694
668
  /*
695
669
  * call-seq:
696
670
  * cert1 == cert2 -> true | false
697
671
  *
698
672
  * Compares the two certificates. Note that this takes into account all fields,
699
673
  * not just the issuer name and the serial number.
674
+ *
675
+ * This method uses X509_cmp() from OpenSSL, which compares certificates based
676
+ * on their cached DER encodings. The comparison can be unreliable if a
677
+ * certificate is incomplete.
678
+ *
679
+ * See also the man page X509_cmp(3).
700
680
  */
701
681
  static VALUE
702
682
  ossl_x509_eq(VALUE self, VALUE other)
@@ -705,13 +685,12 @@ ossl_x509_eq(VALUE self, VALUE other)
705
685
 
706
686
  GetX509(self, a);
707
687
  if (!rb_obj_is_kind_of(other, cX509Cert))
708
- return Qfalse;
688
+ return Qfalse;
709
689
  GetX509(other, b);
710
690
 
711
691
  return !X509_cmp(a, b) ? Qtrue : Qfalse;
712
692
  }
713
693
 
714
- #ifdef HAVE_I2D_RE_X509_TBS
715
694
  /*
716
695
  * call-seq:
717
696
  * cert.tbs_bytes => string
@@ -741,7 +720,6 @@ ossl_x509_tbs_bytes(VALUE self)
741
720
 
742
721
  return str;
743
722
  }
744
- #endif
745
723
 
746
724
  struct load_chained_certificates_arguments {
747
725
  VALUE certificates;
@@ -802,7 +780,7 @@ load_chained_certificates_PEM(BIO *in) {
802
780
  certificates = load_chained_certificates_append(Qnil, certificate);
803
781
 
804
782
  while ((certificate = PEM_read_bio_X509(in, NULL, NULL, NULL))) {
805
- load_chained_certificates_append(certificates, certificate);
783
+ load_chained_certificates_append(certificates, certificate);
806
784
  }
807
785
 
808
786
  /* We tried to read one more certificate but could not read start line: */
@@ -900,12 +878,6 @@ ossl_x509_load(VALUE klass, VALUE buffer)
900
878
  void
901
879
  Init_ossl_x509cert(void)
902
880
  {
903
- #if 0
904
- mOSSL = rb_define_module("OpenSSL");
905
- eOSSLError = rb_define_class_under(mOSSL, "OpenSSLError", rb_eStandardError);
906
- mX509 = rb_define_module_under(mOSSL, "X509");
907
- #endif
908
-
909
881
  eX509CertError = rb_define_class_under(mX509, "CertificateError", eOSSLError);
910
882
 
911
883
  /* Document-class: OpenSSL::X509::Certificate
@@ -1033,9 +1005,6 @@ Init_ossl_x509cert(void)
1033
1005
  rb_define_method(cX509Cert, "extensions", ossl_x509_get_extensions, 0);
1034
1006
  rb_define_method(cX509Cert, "extensions=", ossl_x509_set_extensions, 1);
1035
1007
  rb_define_method(cX509Cert, "add_extension", ossl_x509_add_extension, 1);
1036
- rb_define_method(cX509Cert, "inspect", ossl_x509_inspect, 0);
1037
1008
  rb_define_method(cX509Cert, "==", ossl_x509_eq, 1);
1038
- #ifdef HAVE_I2D_RE_X509_TBS
1039
1009
  rb_define_method(cX509Cert, "tbs_bytes", ossl_x509_tbs_bytes, 0);
1040
- #endif
1041
1010
  }