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
@@ -103,7 +103,7 @@ static const rb_data_type_t ossl_ts_resp_type = {
103
103
  static void
104
104
  ossl_ts_token_info_free(void *ptr)
105
105
  {
106
- TS_TST_INFO_free(ptr);
106
+ TS_TST_INFO_free(ptr);
107
107
  }
108
108
 
109
109
  static const rb_data_type_t ossl_ts_token_info_type = {
@@ -132,44 +132,10 @@ asn1_to_der(void *template, int (*i2d)(void *template, unsigned char **pp))
132
132
  return str;
133
133
  }
134
134
 
135
- static ASN1_OBJECT*
136
- obj_to_asn1obj(VALUE obj)
137
- {
138
- ASN1_OBJECT *a1obj;
139
-
140
- StringValue(obj);
141
- a1obj = OBJ_txt2obj(RSTRING_PTR(obj), 0);
142
- if(!a1obj) a1obj = OBJ_txt2obj(RSTRING_PTR(obj), 1);
143
- if(!a1obj) ossl_raise(eASN1Error, "invalid OBJECT ID");
144
-
145
- return a1obj;
146
- }
147
-
148
135
  static VALUE
149
136
  obj_to_asn1obj_i(VALUE obj)
150
137
  {
151
- return (VALUE)obj_to_asn1obj(obj);
152
- }
153
-
154
- static VALUE
155
- get_asn1obj(ASN1_OBJECT *obj)
156
- {
157
- BIO *out;
158
- VALUE ret;
159
- int nid;
160
- if ((nid = OBJ_obj2nid(obj)) != NID_undef)
161
- ret = rb_str_new2(OBJ_nid2sn(nid));
162
- else{
163
- if (!(out = BIO_new(BIO_s_mem())))
164
- ossl_raise(eTimestampError, "BIO_new(BIO_s_mem())");
165
- if (i2a_ASN1_OBJECT(out, obj) <= 0) {
166
- BIO_free(out);
167
- ossl_raise(eTimestampError, "i2a_ASN1_OBJECT");
168
- }
169
- ret = ossl_membio2str(out);
170
- }
171
-
172
- return ret;
138
+ return (VALUE)ossl_to_asn1obj(obj);
173
139
  }
174
140
 
175
141
  static VALUE
@@ -236,11 +202,13 @@ ossl_ts_req_get_algorithm(VALUE self)
236
202
  TS_REQ *req;
237
203
  TS_MSG_IMPRINT *mi;
238
204
  X509_ALGOR *algor;
205
+ const ASN1_OBJECT *obj;
239
206
 
240
207
  GetTSRequest(self, req);
241
208
  mi = TS_REQ_get_msg_imprint(req);
242
209
  algor = TS_MSG_IMPRINT_get_algo(mi);
243
- return get_asn1obj(algor->algorithm);
210
+ X509_ALGOR_get0(&obj, NULL, NULL, algor);
211
+ return ossl_asn1obj_to_string(obj);
244
212
  }
245
213
 
246
214
  /*
@@ -262,7 +230,7 @@ ossl_ts_req_set_algorithm(VALUE self, VALUE algo)
262
230
  X509_ALGOR *algor;
263
231
 
264
232
  GetTSRequest(self, req);
265
- obj = obj_to_asn1obj(algo);
233
+ obj = ossl_to_asn1obj(algo);
266
234
  mi = TS_REQ_get_msg_imprint(req);
267
235
  algor = TS_MSG_IMPRINT_get_algo(mi);
268
236
  if (!X509_ALGOR_set0(algor, obj, V_ASN1_NULL, NULL)) {
@@ -291,7 +259,7 @@ ossl_ts_req_get_msg_imprint(VALUE self)
291
259
  mi = TS_REQ_get_msg_imprint(req);
292
260
  hashed_msg = TS_MSG_IMPRINT_get_msg(mi);
293
261
 
294
- ret = rb_str_new((const char *)hashed_msg->data, hashed_msg->length);
262
+ ret = asn1str_to_str(hashed_msg);
295
263
 
296
264
  return ret;
297
265
  }
@@ -369,7 +337,7 @@ ossl_ts_req_get_policy_id(VALUE self)
369
337
  GetTSRequest(self, req);
370
338
  if (!TS_REQ_get_policy_id(req))
371
339
  return Qnil;
372
- return get_asn1obj(TS_REQ_get_policy_id(req));
340
+ return ossl_asn1obj_to_string(TS_REQ_get_policy_id(req));
373
341
  }
374
342
 
375
343
  /*
@@ -392,7 +360,7 @@ ossl_ts_req_set_policy_id(VALUE self, VALUE oid)
392
360
  int ok;
393
361
 
394
362
  GetTSRequest(self, req);
395
- obj = obj_to_asn1obj(oid);
363
+ obj = ossl_to_asn1obj(oid);
396
364
  ok = TS_REQ_set_policy_id(req, obj);
397
365
  ASN1_OBJECT_free(obj);
398
366
  if (!ok)
@@ -490,17 +458,19 @@ ossl_ts_req_to_der(VALUE self)
490
458
  TS_REQ *req;
491
459
  TS_MSG_IMPRINT *mi;
492
460
  X509_ALGOR *algo;
461
+ const ASN1_OBJECT *obj;
493
462
  ASN1_OCTET_STRING *hashed_msg;
494
463
 
495
464
  GetTSRequest(self, req);
496
465
  mi = TS_REQ_get_msg_imprint(req);
497
466
 
498
467
  algo = TS_MSG_IMPRINT_get_algo(mi);
499
- if (OBJ_obj2nid(algo->algorithm) == NID_undef)
468
+ X509_ALGOR_get0(&obj, NULL, NULL, algo);
469
+ if (OBJ_obj2nid(obj) == NID_undef)
500
470
  ossl_raise(eTimestampError, "Message imprint missing algorithm");
501
471
 
502
472
  hashed_msg = TS_MSG_IMPRINT_get_msg(mi);
503
- if (!hashed_msg->length)
473
+ if (!ASN1_STRING_length(hashed_msg))
504
474
  ossl_raise(eTimestampError, "Message imprint missing hashed message");
505
475
 
506
476
  return asn1_to_der((void *)req, (int (*)(void *, unsigned char **))i2d_TS_REQ);
@@ -620,14 +590,7 @@ ossl_ts_resp_get_failure_info(VALUE self)
620
590
  {
621
591
  TS_RESP *resp;
622
592
  TS_STATUS_INFO *si;
623
-
624
- /* The ASN1_BIT_STRING_get_bit changed from 1.0.0. to 1.1.0, making this
625
- * const. */
626
- #if defined(HAVE_TS_STATUS_INFO_GET0_FAILURE_INFO)
627
593
  const ASN1_BIT_STRING *fi;
628
- #else
629
- ASN1_BIT_STRING *fi;
630
- #endif
631
594
 
632
595
  GetTSResponse(self, resp);
633
596
  si = TS_RESP_get_status_info(resp);
@@ -858,16 +821,26 @@ ossl_ts_resp_verify(int argc, VALUE *argv, VALUE self)
858
821
  X509_up_ref(cert);
859
822
  }
860
823
 
824
+ if (!X509_STORE_up_ref(x509st)) {
825
+ sk_X509_pop_free(x509inter, X509_free);
826
+ TS_VERIFY_CTX_free(ctx);
827
+ ossl_raise(eTimestampError, "X509_STORE_up_ref");
828
+ }
829
+
830
+ #ifdef HAVE_TS_VERIFY_CTX_SET0_CERTS
831
+ TS_VERIFY_CTX_set0_certs(ctx, x509inter);
832
+ TS_VERIFY_CTX_set0_store(ctx, x509st);
833
+ #else
834
+ # if OSSL_OPENSSL_PREREQ(3, 0, 0) || OSSL_IS_LIBRESSL
861
835
  TS_VERIFY_CTX_set_certs(ctx, x509inter);
862
- TS_VERIFY_CTX_add_flags(ctx, TS_VFY_SIGNATURE);
836
+ # else
837
+ TS_VERIFY_CTS_set_certs(ctx, x509inter);
838
+ # endif
863
839
  TS_VERIFY_CTX_set_store(ctx, x509st);
840
+ #endif
841
+ TS_VERIFY_CTX_add_flags(ctx, TS_VFY_SIGNATURE);
864
842
 
865
843
  ok = TS_RESP_verify_response(ctx, resp);
866
- /*
867
- * TS_VERIFY_CTX_set_store() call above does not increment the reference
868
- * counter, so it must be unset before TS_VERIFY_CTX_free() is called.
869
- */
870
- TS_VERIFY_CTX_set_store(ctx, NULL);
871
844
  TS_VERIFY_CTX_free(ctx);
872
845
 
873
846
  if (!ok)
@@ -954,7 +927,7 @@ ossl_ts_token_info_get_policy_id(VALUE self)
954
927
  TS_TST_INFO *info;
955
928
 
956
929
  GetTSTokenInfo(self, info);
957
- return get_asn1obj(TS_TST_INFO_get_policy_id(info));
930
+ return ossl_asn1obj_to_string(TS_TST_INFO_get_policy_id(info));
958
931
  }
959
932
 
960
933
  /*
@@ -976,11 +949,13 @@ ossl_ts_token_info_get_algorithm(VALUE self)
976
949
  TS_TST_INFO *info;
977
950
  TS_MSG_IMPRINT *mi;
978
951
  X509_ALGOR *algo;
952
+ const ASN1_OBJECT *obj;
979
953
 
980
954
  GetTSTokenInfo(self, info);
981
955
  mi = TS_TST_INFO_get_msg_imprint(info);
982
956
  algo = TS_MSG_IMPRINT_get_algo(mi);
983
- return get_asn1obj(algo->algorithm);
957
+ X509_ALGOR_get0(&obj, NULL, NULL, algo);
958
+ return ossl_asn1obj_to_string(obj);
984
959
  }
985
960
 
986
961
  /*
@@ -1006,7 +981,7 @@ ossl_ts_token_info_get_msg_imprint(VALUE self)
1006
981
  GetTSTokenInfo(self, info);
1007
982
  mi = TS_TST_INFO_get_msg_imprint(info);
1008
983
  hashed_msg = TS_MSG_IMPRINT_get_msg(mi);
1009
- ret = rb_str_new((const char *)hashed_msg->data, hashed_msg->length);
984
+ ret = asn1str_to_str(hashed_msg);
1010
985
 
1011
986
  return ret;
1012
987
  }
@@ -1146,9 +1121,14 @@ ossl_tsfac_time_cb(struct TS_resp_ctx *ctx, void *data, time_t *sec, long *usec)
1146
1121
  }
1147
1122
 
1148
1123
  static VALUE
1149
- ossl_evp_get_digestbyname_i(VALUE arg)
1124
+ ossl_evp_md_fetch_i(VALUE args_)
1150
1125
  {
1151
- return (VALUE)ossl_evp_get_digestbyname(arg);
1126
+ VALUE *args = (VALUE *)args_, md_holder;
1127
+ const EVP_MD *md;
1128
+
1129
+ md = ossl_evp_md_fetch(args[1], &md_holder);
1130
+ rb_ary_push(args[0], md_holder);
1131
+ return (VALUE)md;
1152
1132
  }
1153
1133
 
1154
1134
  static VALUE
@@ -1184,7 +1164,8 @@ ossl_obj2bio_i(VALUE arg)
1184
1164
  static VALUE
1185
1165
  ossl_tsfac_create_ts(VALUE self, VALUE key, VALUE certificate, VALUE request)
1186
1166
  {
1187
- VALUE serial_number, def_policy_id, gen_time, additional_certs, allowed_digests;
1167
+ VALUE serial_number, def_policy_id, gen_time, additional_certs,
1168
+ allowed_digests, allowed_digests_tmp = Qnil;
1188
1169
  VALUE str;
1189
1170
  STACK_OF(X509) *inter_certs;
1190
1171
  VALUE tsresp, ret = Qnil;
@@ -1245,7 +1226,7 @@ ossl_tsfac_create_ts(VALUE self, VALUE key, VALUE certificate, VALUE request)
1245
1226
  if (rb_obj_is_kind_of(additional_certs, rb_cArray)) {
1246
1227
  inter_certs = ossl_protect_x509_ary2sk(additional_certs, &status);
1247
1228
  if (status)
1248
- goto end;
1229
+ goto end;
1249
1230
 
1250
1231
  /* this dups the sk_X509 and ups each cert's ref count */
1251
1232
  TS_RESP_CTX_set_certs(ctx, inter_certs);
@@ -1261,16 +1242,18 @@ ossl_tsfac_create_ts(VALUE self, VALUE key, VALUE certificate, VALUE request)
1261
1242
 
1262
1243
  allowed_digests = ossl_tsfac_get_allowed_digests(self);
1263
1244
  if (rb_obj_is_kind_of(allowed_digests, rb_cArray)) {
1264
- int i;
1265
- VALUE rbmd;
1266
- const EVP_MD *md;
1267
-
1268
- for (i = 0; i < RARRAY_LEN(allowed_digests); i++) {
1269
- rbmd = rb_ary_entry(allowed_digests, i);
1270
- md = (const EVP_MD *)rb_protect(ossl_evp_get_digestbyname_i, rbmd, &status);
1245
+ allowed_digests_tmp = rb_ary_new_capa(RARRAY_LEN(allowed_digests));
1246
+ for (long i = 0; i < RARRAY_LEN(allowed_digests); i++) {
1247
+ VALUE args[] = {
1248
+ allowed_digests_tmp,
1249
+ rb_ary_entry(allowed_digests, i),
1250
+ };
1251
+ const EVP_MD *md = (const EVP_MD *)rb_protect(ossl_evp_md_fetch_i,
1252
+ (VALUE)args, &status);
1271
1253
  if (status)
1272
1254
  goto end;
1273
- TS_RESP_CTX_add_md(ctx, md);
1255
+ if (!TS_RESP_CTX_add_md(ctx, md))
1256
+ goto end;
1274
1257
  }
1275
1258
  }
1276
1259
 
@@ -1284,6 +1267,7 @@ ossl_tsfac_create_ts(VALUE self, VALUE key, VALUE certificate, VALUE request)
1284
1267
 
1285
1268
  response = TS_RESP_create_response(ctx, req_bio);
1286
1269
  BIO_free(req_bio);
1270
+ RB_GC_GUARD(allowed_digests_tmp);
1287
1271
 
1288
1272
  if (!response) {
1289
1273
  err_msg = "Error during response generation";
@@ -1297,7 +1281,7 @@ ossl_tsfac_create_ts(VALUE self, VALUE key, VALUE certificate, VALUE request)
1297
1281
  SetTSResponse(tsresp, response);
1298
1282
  ret = tsresp;
1299
1283
 
1300
- end:
1284
+ end:
1301
1285
  ASN1_INTEGER_free(asn1_serial);
1302
1286
  ASN1_OBJECT_free(def_policy_id_obj);
1303
1287
  TS_RESP_CTX_free(ctx);
@@ -1314,10 +1298,6 @@ end:
1314
1298
  void
1315
1299
  Init_ossl_ts(void)
1316
1300
  {
1317
- #if 0
1318
- mOSSL = rb_define_module("OpenSSL"); /* let rdoc know about mOSSL */
1319
- #endif
1320
-
1321
1301
  /*
1322
1302
  * Possible return value for +Response#failure_info+. Indicates that the
1323
1303
  * timestamp server rejects the message imprint algorithm used in the
@@ -1527,65 +1507,39 @@ Init_ossl_ts(void)
1527
1507
  * fac.default_policy_id = '1.2.3.4.5'
1528
1508
  * fac.additional_certificates = [ inter1, inter2 ]
1529
1509
  * timestamp = fac.create_timestamp(p12.key, p12.certificate, req)
1530
- *
1531
- * ==Attributes
1532
- *
1533
- * ===default_policy_id
1510
+ */
1511
+ cTimestampFactory = rb_define_class_under(mTimestamp, "Factory", rb_cObject);
1512
+ /*
1513
+ * The list of digest algorithms that the factory is allowed
1514
+ * create timestamps for. Known vulnerable or weak algorithms should not be
1515
+ * allowed where possible. Must be an Array of String or OpenSSL::Digest
1516
+ * subclass instances.
1517
+ */
1518
+ rb_attr(cTimestampFactory, rb_intern_const("allowed_digests"), 1, 1, 0);
1519
+ /*
1520
+ * A String representing the default policy object identifier, or +nil+.
1534
1521
  *
1535
1522
  * Request#policy_id will always be preferred over this if present in the
1536
- * Request, only if Request#policy_id is nil default_policy will be used.
1523
+ * Request, only if Request#policy_id is +nil+ default_policy will be used.
1537
1524
  * If none of both is present, a TimestampError will be raised when trying
1538
1525
  * to create a Response.
1539
- *
1540
- * call-seq:
1541
- * factory.default_policy_id = "string" -> string
1542
- * factory.default_policy_id -> string or nil
1543
- *
1544
- * ===serial_number
1545
- *
1546
- * Sets or retrieves the serial number to be used for timestamp creation.
1547
- * Must be present for timestamp creation.
1548
- *
1549
- * call-seq:
1550
- * factory.serial_number = number -> number
1551
- * factory.serial_number -> number or nil
1552
- *
1553
- * ===gen_time
1554
- *
1555
- * Sets or retrieves the Time value to be used in the Response. Must be
1556
- * present for timestamp creation.
1557
- *
1558
- * call-seq:
1559
- * factory.gen_time = Time -> Time
1560
- * factory.gen_time -> Time or nil
1561
- *
1562
- * ===additional_certs
1563
- *
1564
- * Sets or retrieves additional certificates apart from the timestamp
1565
- * certificate (e.g. intermediate certificates) to be added to the Response.
1566
- * Must be an Array of OpenSSL::X509::Certificate.
1567
- *
1568
- * call-seq:
1569
- * factory.additional_certs = [cert1, cert2] -> [ cert1, cert2 ]
1570
- * factory.additional_certs -> array or nil
1571
- *
1572
- * ===allowed_digests
1573
- *
1574
- * Sets or retrieves the digest algorithms that the factory is allowed
1575
- * create timestamps for. Known vulnerable or weak algorithms should not be
1576
- * allowed where possible.
1577
- * Must be an Array of String or OpenSSL::Digest subclass instances.
1578
- *
1579
- * call-seq:
1580
- * factory.allowed_digests = ["sha1", OpenSSL::Digest.new('SHA256').new] -> [ "sha1", OpenSSL::Digest) ]
1581
- * factory.allowed_digests -> array or nil
1582
- *
1583
1526
  */
1584
- cTimestampFactory = rb_define_class_under(mTimestamp, "Factory", rb_cObject);
1585
- rb_attr(cTimestampFactory, rb_intern_const("allowed_digests"), 1, 1, 0);
1586
1527
  rb_attr(cTimestampFactory, rb_intern_const("default_policy_id"), 1, 1, 0);
1528
+ /*
1529
+ * The serial number to be used for timestamp creation. Must be present for
1530
+ * timestamp creation. Must be an instance of OpenSSL::BN or Integer.
1531
+ */
1587
1532
  rb_attr(cTimestampFactory, rb_intern_const("serial_number"), 1, 1, 0);
1533
+ /*
1534
+ * The Time value to be used in the Response. Must be present for timestamp
1535
+ * creation.
1536
+ */
1588
1537
  rb_attr(cTimestampFactory, rb_intern_const("gen_time"), 1, 1, 0);
1538
+ /*
1539
+ * Additional certificates apart from the timestamp certificate (e.g.
1540
+ * intermediate certificates) to be added to the Response.
1541
+ * Must be an Array of OpenSSL::X509::Certificate, or +nil+.
1542
+ */
1589
1543
  rb_attr(cTimestampFactory, rb_intern_const("additional_certs"), 1, 1, 0);
1590
1544
  rb_define_method(cTimestampFactory, "create_timestamp", ossl_tsfac_create_ts, 3);
1591
1545
  }
@@ -13,7 +13,8 @@ VALUE mX509;
13
13
 
14
14
  #define DefX509Const(x) rb_define_const(mX509, #x, INT2NUM(X509_##x))
15
15
  #define DefX509Default(x,i) \
16
- rb_define_const(mX509, "DEFAULT_" #x, rb_str_new2(X509_get_default_##i()))
16
+ rb_define_const(mX509, "DEFAULT_" #x, \
17
+ rb_obj_freeze(rb_str_new_cstr(X509_get_default_##i())))
17
18
 
18
19
  ASN1_TIME *
19
20
  ossl_x509_time_adjust(ASN1_TIME *s, VALUE time)
@@ -29,10 +30,6 @@ ossl_x509_time_adjust(ASN1_TIME *s, VALUE time)
29
30
  void
30
31
  Init_ossl_x509(void)
31
32
  {
32
- #if 0
33
- mOSSL = rb_define_module("OpenSSL");
34
- #endif
35
-
36
33
  mX509 = rb_define_module_under(mOSSL, "X509");
37
34
 
38
35
  Init_ossl_x509attr();
@@ -48,9 +45,7 @@ Init_ossl_x509(void)
48
45
 
49
46
  /* Certificate verification error code */
50
47
  DefX509Const(V_OK);
51
- #if defined(X509_V_ERR_UNSPECIFIED) /* 1.0.1r, 1.0.2f, 1.1.0 */
52
48
  DefX509Const(V_ERR_UNSPECIFIED);
53
- #endif
54
49
  DefX509Const(V_ERR_UNABLE_TO_GET_ISSUER_CERT);
55
50
  DefX509Const(V_ERR_UNABLE_TO_GET_CRL);
56
51
  DefX509Const(V_ERR_UNABLE_TO_DECRYPT_CERT_SIGNATURE);
@@ -104,10 +99,10 @@ Init_ossl_x509(void)
104
99
  DefX509Const(V_ERR_UNSUPPORTED_CONSTRAINT_SYNTAX);
105
100
  DefX509Const(V_ERR_UNSUPPORTED_NAME_SYNTAX);
106
101
  DefX509Const(V_ERR_CRL_PATH_VALIDATION_ERROR);
107
- #if defined(X509_V_ERR_PATH_LOOP)
102
+ #if defined(X509_V_ERR_PATH_LOOP) /* OpenSSL 1.1.0, missing in LibreSSL */
108
103
  DefX509Const(V_ERR_PATH_LOOP);
109
104
  #endif
110
- #if defined(X509_V_ERR_SUITE_B_INVALID_VERSION)
105
+ #if defined(X509_V_ERR_SUITE_B_INVALID_VERSION) /* OpenSSL 1.1.0, missing in LibreSSL */
111
106
  DefX509Const(V_ERR_SUITE_B_INVALID_VERSION);
112
107
  DefX509Const(V_ERR_SUITE_B_INVALID_ALGORITHM);
113
108
  DefX509Const(V_ERR_SUITE_B_INVALID_CURVE);
@@ -118,27 +113,21 @@ Init_ossl_x509(void)
118
113
  DefX509Const(V_ERR_HOSTNAME_MISMATCH);
119
114
  DefX509Const(V_ERR_EMAIL_MISMATCH);
120
115
  DefX509Const(V_ERR_IP_ADDRESS_MISMATCH);
121
- #if defined(X509_V_ERR_DANE_NO_MATCH)
116
+ #if defined(X509_V_ERR_DANE_NO_MATCH) /* OpenSSL 1.1.0, missing in LibreSSL */
122
117
  DefX509Const(V_ERR_DANE_NO_MATCH);
123
118
  #endif
124
- #if defined(X509_V_ERR_EE_KEY_TOO_SMALL)
125
119
  DefX509Const(V_ERR_EE_KEY_TOO_SMALL);
126
120
  DefX509Const(V_ERR_CA_KEY_TOO_SMALL);
127
121
  DefX509Const(V_ERR_CA_MD_TOO_WEAK);
128
- #endif
129
- #if defined(X509_V_ERR_INVALID_CALL)
130
122
  DefX509Const(V_ERR_INVALID_CALL);
131
- #endif
132
- #if defined(X509_V_ERR_STORE_LOOKUP)
133
123
  DefX509Const(V_ERR_STORE_LOOKUP);
134
- #endif
135
- #if defined(X509_V_ERR_NO_VALID_SCTS)
124
+ #if defined(X509_V_ERR_NO_VALID_SCTS) /* OpenSSL 1.1.0, missing in LibreSSL */
136
125
  DefX509Const(V_ERR_NO_VALID_SCTS);
137
126
  #endif
138
- #if defined(X509_V_ERR_PROXY_SUBJECT_NAME_VIOLATION)
127
+ #if defined(X509_V_ERR_PROXY_SUBJECT_NAME_VIOLATION) /* OpenSSL 1.1.0, missing in LibreSSL */
139
128
  DefX509Const(V_ERR_PROXY_SUBJECT_NAME_VIOLATION);
140
129
  #endif
141
- #if defined(X509_V_ERR_OCSP_VERIFY_NEEDED)
130
+ #if defined(X509_V_ERR_OCSP_VERIFY_NEEDED) /* OpenSSL 1.1.1, missing in LibreSSL */
142
131
  DefX509Const(V_ERR_OCSP_VERIFY_NEEDED);
143
132
  DefX509Const(V_ERR_OCSP_VERIFY_FAILED);
144
133
  DefX509Const(V_ERR_OCSP_CERT_UNKNOWN);
@@ -189,17 +178,13 @@ Init_ossl_x509(void)
189
178
  * certificate chain, search the Store first for the issuer certificate.
190
179
  * Enabled by default in OpenSSL >= 1.1.0. */
191
180
  DefX509Const(V_FLAG_TRUSTED_FIRST);
192
- #if defined(X509_V_FLAG_SUITEB_128_LOS_ONLY)
181
+ #if defined(X509_V_FLAG_SUITEB_128_LOS_ONLY) /* OpenSSL 1.1.0, missing in LibreSSL */
193
182
  /* Set by Store#flags= and StoreContext#flags=.
194
183
  * Enables Suite B 128 bit only mode. */
195
184
  DefX509Const(V_FLAG_SUITEB_128_LOS_ONLY);
196
- #endif
197
- #if defined(X509_V_FLAG_SUITEB_192_LOS)
198
185
  /* Set by Store#flags= and StoreContext#flags=.
199
186
  * Enables Suite B 192 bit only mode. */
200
187
  DefX509Const(V_FLAG_SUITEB_192_LOS);
201
- #endif
202
- #if defined(X509_V_FLAG_SUITEB_128_LOS)
203
188
  /* Set by Store#flags= and StoreContext#flags=.
204
189
  * Enables Suite B 128 bit mode allowing 192 bit algorithms. */
205
190
  DefX509Const(V_FLAG_SUITEB_128_LOS);
@@ -207,17 +192,13 @@ Init_ossl_x509(void)
207
192
  /* Set by Store#flags= and StoreContext#flags=.
208
193
  * Allows partial chains if at least one certificate is in trusted store. */
209
194
  DefX509Const(V_FLAG_PARTIAL_CHAIN);
210
- #if defined(X509_V_FLAG_NO_ALT_CHAINS)
211
195
  /* Set by Store#flags= and StoreContext#flags=. Suppresses searching for
212
196
  * a alternative chain. No effect in OpenSSL >= 1.1.0. */
213
197
  DefX509Const(V_FLAG_NO_ALT_CHAINS);
214
- #endif
215
- #if defined(X509_V_FLAG_NO_CHECK_TIME)
216
198
  /* Set by Store#flags= and StoreContext#flags=. Suppresses checking the
217
199
  * validity period of certificates and CRLs. No effect when the current
218
200
  * time is explicitly set by Store#time= or StoreContext#time=. */
219
201
  DefX509Const(V_FLAG_NO_CHECK_TIME);
220
- #endif
221
202
 
222
203
  /* Set by Store#purpose=. SSL/TLS client. */
223
204
  DefX509Const(PURPOSE_SSL_CLIENT);
@@ -13,14 +13,14 @@
13
13
  TypedData_Wrap_Struct((klass), &ossl_x509attr_type, 0)
14
14
  #define SetX509Attr(obj, attr) do { \
15
15
  if (!(attr)) { \
16
- ossl_raise(rb_eRuntimeError, "ATTR wasn't initialized!"); \
16
+ ossl_raise(rb_eRuntimeError, "ATTR wasn't initialized!"); \
17
17
  } \
18
18
  RTYPEDDATA_DATA(obj) = (attr); \
19
19
  } while (0)
20
20
  #define GetX509Attr(obj, attr) do { \
21
21
  TypedData_Get_Struct((obj), X509_ATTRIBUTE, &ossl_x509attr_type, (attr)); \
22
22
  if (!(attr)) { \
23
- ossl_raise(rb_eRuntimeError, "ATTR wasn't initialized!"); \
23
+ ossl_raise(rb_eRuntimeError, "ATTR wasn't initialized!"); \
24
24
  } \
25
25
  } while (0)
26
26
 
@@ -39,7 +39,7 @@ ossl_x509attr_free(void *ptr)
39
39
  static const rb_data_type_t ossl_x509attr_type = {
40
40
  "OpenSSL/X509/ATTRIBUTE",
41
41
  {
42
- 0, ossl_x509attr_free,
42
+ 0, ossl_x509attr_free,
43
43
  },
44
44
  0, 0, RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_WB_PROTECTED,
45
45
  };
@@ -54,14 +54,9 @@ ossl_x509attr_new(X509_ATTRIBUTE *attr)
54
54
  VALUE obj;
55
55
 
56
56
  obj = NewX509Attr(cX509Attr);
57
- if (!attr) {
58
- new = X509_ATTRIBUTE_new();
59
- } else {
60
- new = X509_ATTRIBUTE_dup(attr);
61
- }
62
- if (!new) {
63
- ossl_raise(eX509AttrError, NULL);
64
- }
57
+ new = X509_ATTRIBUTE_dup(attr);
58
+ if (!new)
59
+ ossl_raise(eX509AttrError, "X509_ATTRIBUTE_dup");
65
60
  SetX509Attr(obj, new);
66
61
 
67
62
  return obj;
@@ -88,7 +83,7 @@ ossl_x509attr_alloc(VALUE klass)
88
83
 
89
84
  obj = NewX509Attr(klass);
90
85
  if (!(attr = X509_ATTRIBUTE_new()))
91
- ossl_raise(eX509AttrError, NULL);
86
+ ossl_raise(eX509AttrError, NULL);
92
87
  SetX509Attr(obj, attr);
93
88
 
94
89
  return obj;
@@ -107,15 +102,15 @@ ossl_x509attr_initialize(int argc, VALUE *argv, VALUE self)
107
102
 
108
103
  GetX509Attr(self, attr);
109
104
  if(rb_scan_args(argc, argv, "11", &oid, &value) == 1){
110
- oid = ossl_to_der_if_possible(oid);
111
- StringValue(oid);
112
- p = (unsigned char *)RSTRING_PTR(oid);
113
- x = d2i_X509_ATTRIBUTE(&attr, &p, RSTRING_LEN(oid));
114
- DATA_PTR(self) = attr;
115
- if(!x){
116
- ossl_raise(eX509AttrError, NULL);
117
- }
118
- return self;
105
+ oid = ossl_to_der_if_possible(oid);
106
+ StringValue(oid);
107
+ p = (unsigned char *)RSTRING_PTR(oid);
108
+ x = d2i_X509_ATTRIBUTE(&attr, &p, RSTRING_LEN(oid));
109
+ DATA_PTR(self) = attr;
110
+ if(!x){
111
+ ossl_raise(eX509AttrError, NULL);
112
+ }
113
+ return self;
119
114
  }
120
115
  rb_funcall(self, rb_intern("oid="), 1, oid);
121
116
  rb_funcall(self, rb_intern("value="), 1, value);
@@ -123,6 +118,7 @@ ossl_x509attr_initialize(int argc, VALUE *argv, VALUE self)
123
118
  return self;
124
119
  }
125
120
 
121
+ /* :nodoc: */
126
122
  static VALUE
127
123
  ossl_x509attr_initialize_copy(VALUE self, VALUE other)
128
124
  {
@@ -134,7 +130,7 @@ ossl_x509attr_initialize_copy(VALUE self, VALUE other)
134
130
 
135
131
  attr_new = X509_ATTRIBUTE_dup(attr_other);
136
132
  if (!attr_new)
137
- ossl_raise(eX509AttrError, "X509_ATTRIBUTE_dup");
133
+ ossl_raise(eX509AttrError, "X509_ATTRIBUTE_dup");
138
134
 
139
135
  SetX509Attr(self, attr_new);
140
136
  X509_ATTRIBUTE_free(attr);
@@ -158,8 +154,8 @@ ossl_x509attr_set_oid(VALUE self, VALUE oid)
158
154
  obj = OBJ_txt2obj(s, 0);
159
155
  if(!obj) ossl_raise(eX509AttrError, NULL);
160
156
  if (!X509_ATTRIBUTE_set1_object(attr, obj)) {
161
- ASN1_OBJECT_free(obj);
162
- ossl_raise(eX509AttrError, "X509_ATTRIBUTE_set1_object");
157
+ ASN1_OBJECT_free(obj);
158
+ ossl_raise(eX509AttrError, "X509_ATTRIBUTE_set1_object");
163
159
  }
164
160
  ASN1_OBJECT_free(obj);
165
161
 
@@ -168,29 +164,18 @@ ossl_x509attr_set_oid(VALUE self, VALUE oid)
168
164
 
169
165
  /*
170
166
  * call-seq:
171
- * attr.oid => string
167
+ * attr.oid -> string
168
+ *
169
+ * Returns the OID of the attribute. Returns the short name or the dotted
170
+ * decimal notation.
172
171
  */
173
172
  static VALUE
174
173
  ossl_x509attr_get_oid(VALUE self)
175
174
  {
176
175
  X509_ATTRIBUTE *attr;
177
- ASN1_OBJECT *oid;
178
- BIO *out;
179
- VALUE ret;
180
- int nid;
181
176
 
182
177
  GetX509Attr(self, attr);
183
- oid = X509_ATTRIBUTE_get0_object(attr);
184
- if ((nid = OBJ_obj2nid(oid)) != NID_undef)
185
- ret = rb_str_new2(OBJ_nid2sn(nid));
186
- else{
187
- if (!(out = BIO_new(BIO_s_mem())))
188
- ossl_raise(eX509AttrError, NULL);
189
- i2a_ASN1_OBJECT(out, oid);
190
- ret = ossl_membio2str(out);
191
- }
192
-
193
- return ret;
178
+ return ossl_asn1obj_to_string(X509_ATTRIBUTE_get0_object(attr));
194
179
  }
195
180
 
196
181
  /*
@@ -251,21 +236,21 @@ ossl_x509attr_get_value(VALUE self)
251
236
  GetX509Attr(self, attr);
252
237
  /* there is no X509_ATTRIBUTE_get0_set() :( */
253
238
  if (!(sk = sk_ASN1_TYPE_new_null()))
254
- ossl_raise(eX509AttrError, "sk_new");
239
+ ossl_raise(eX509AttrError, "sk_new");
255
240
 
256
241
  count = X509_ATTRIBUTE_count(attr);
257
242
  for (i = 0; i < count; i++)
258
- sk_ASN1_TYPE_push(sk, X509_ATTRIBUTE_get0_type(attr, i));
243
+ sk_ASN1_TYPE_push(sk, X509_ATTRIBUTE_get0_type(attr, i));
259
244
 
260
245
  if ((len = i2d_ASN1_SET_ANY(sk, NULL)) <= 0) {
261
- sk_ASN1_TYPE_free(sk);
262
- ossl_raise(eX509AttrError, NULL);
246
+ sk_ASN1_TYPE_free(sk);
247
+ ossl_raise(eX509AttrError, NULL);
263
248
  }
264
249
  str = rb_str_new(0, len);
265
250
  p = (unsigned char *)RSTRING_PTR(str);
266
251
  if (i2d_ASN1_SET_ANY(sk, &p) <= 0) {
267
- sk_ASN1_TYPE_free(sk);
268
- ossl_raise(eX509AttrError, NULL);
252
+ sk_ASN1_TYPE_free(sk);
253
+ ossl_raise(eX509AttrError, NULL);
269
254
  }
270
255
  ossl_str_adjust(str, p);
271
256
  sk_ASN1_TYPE_free(sk);
@@ -287,11 +272,11 @@ ossl_x509attr_to_der(VALUE self)
287
272
 
288
273
  GetX509Attr(self, attr);
289
274
  if((len = i2d_X509_ATTRIBUTE(attr, NULL)) <= 0)
290
- ossl_raise(eX509AttrError, NULL);
275
+ ossl_raise(eX509AttrError, NULL);
291
276
  str = rb_str_new(0, len);
292
277
  p = (unsigned char *)RSTRING_PTR(str);
293
278
  if(i2d_X509_ATTRIBUTE(attr, &p) <= 0)
294
- ossl_raise(eX509AttrError, NULL);
279
+ ossl_raise(eX509AttrError, NULL);
295
280
  ossl_str_adjust(str, p);
296
281
 
297
282
  return str;
@@ -303,12 +288,6 @@ ossl_x509attr_to_der(VALUE self)
303
288
  void
304
289
  Init_ossl_x509attr(void)
305
290
  {
306
- #if 0
307
- mOSSL = rb_define_module("OpenSSL");
308
- eOSSLError = rb_define_class_under(mOSSL, "OpenSSLError", rb_eStandardError);
309
- mX509 = rb_define_module_under(mOSSL, "X509");
310
- #endif
311
-
312
291
  eX509AttrError = rb_define_class_under(mX509, "AttributeError", eOSSLError);
313
292
 
314
293
  cX509Attr = rb_define_class_under(mX509, "Attribute", rb_cObject);