openssl 2.1.1 → 2.1.4

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.
@@ -13,6 +13,12 @@
13
13
 
14
14
  #define numberof(ary) (int)(sizeof(ary)/sizeof((ary)[0]))
15
15
 
16
+ #if !defined(TLS1_3_VERSION) && \
17
+ defined(LIBRESSL_VERSION_NUMBER) && \
18
+ LIBRESSL_VERSION_NUMBER >= 0x3020000fL
19
+ # define TLS1_3_VERSION 0x0304
20
+ #endif
21
+
16
22
  #ifdef _WIN32
17
23
  # define TO_SOCKET(s) _get_osfhandle(s)
18
24
  #else
@@ -33,7 +39,7 @@ static VALUE eSSLErrorWaitReadable;
33
39
  static VALUE eSSLErrorWaitWritable;
34
40
 
35
41
  static ID id_call, ID_callback_state, id_tmp_dh_callback, id_tmp_ecdh_callback,
36
- id_npn_protocols_encoded;
42
+ id_npn_protocols_encoded, id_each;
37
43
  static VALUE sym_exception, sym_wait_readable, sym_wait_writable;
38
44
 
39
45
  static ID id_i_cert_store, id_i_ca_file, id_i_ca_path, id_i_verify_mode,
@@ -53,6 +59,13 @@ static int ossl_sslctx_ex_ptr_idx;
53
59
  static int ossl_sslctx_ex_store_p;
54
60
  #endif
55
61
 
62
+ static void
63
+ ossl_sslctx_mark(void *ptr)
64
+ {
65
+ SSL_CTX *ctx = ptr;
66
+ rb_gc_mark((VALUE)SSL_CTX_get_ex_data(ctx, ossl_sslctx_ex_ptr_idx));
67
+ }
68
+
56
69
  static void
57
70
  ossl_sslctx_free(void *ptr)
58
71
  {
@@ -67,7 +80,7 @@ ossl_sslctx_free(void *ptr)
67
80
  static const rb_data_type_t ossl_sslctx_type = {
68
81
  "OpenSSL/SSL/CTX",
69
82
  {
70
- 0, ossl_sslctx_free,
83
+ ossl_sslctx_mark, ossl_sslctx_free,
71
84
  },
72
85
  0, 0, RUBY_TYPED_FREE_IMMEDIATELY,
73
86
  };
@@ -184,8 +197,10 @@ ossl_sslctx_set_minmax_proto_version(VALUE self, VALUE min_v, VALUE max_v)
184
197
 
185
198
  for (i = 0; i < numberof(options_map); i++) {
186
199
  sum |= options_map[i].opts;
187
- if (min && min > options_map[i].ver || max && max < options_map[i].ver)
200
+ if ((min && min > options_map[i].ver) ||
201
+ (max && max < options_map[i].ver)) {
188
202
  opts |= options_map[i].opts;
203
+ }
189
204
  }
190
205
  SSL_CTX_clear_options(ctx, sum);
191
206
  SSL_CTX_set_options(ctx, opts);
@@ -357,7 +372,14 @@ ossl_ssl_verify_callback(int preverify_ok, X509_STORE_CTX *ctx)
357
372
  rb_ivar_set(ssl_obj, ID_callback_state, INT2NUM(status));
358
373
  return 0;
359
374
  }
360
- preverify_ok = ret == Qtrue;
375
+ if (ret != Qtrue) {
376
+ preverify_ok = 0;
377
+ #if defined(X509_V_ERR_HOSTNAME_MISMATCH)
378
+ X509_STORE_CTX_set_error(ctx, X509_V_ERR_HOSTNAME_MISMATCH);
379
+ #else
380
+ X509_STORE_CTX_set_error(ctx, X509_V_ERR_CERT_REJECTED);
381
+ #endif
382
+ }
361
383
  }
362
384
 
363
385
  return ossl_verify_cb_call(cb, preverify_ok, ctx);
@@ -378,7 +400,7 @@ ossl_call_session_get_cb(VALUE ary)
378
400
  }
379
401
 
380
402
  static SSL_SESSION *
381
- #if OPENSSL_VERSION_NUMBER >= 0x10100000 && !defined(LIBRESSL_VERSION_NUMBER)
403
+ #if (!defined(LIBRESSL_VERSION_NUMBER) ? OPENSSL_VERSION_NUMBER >= 0x10100000 : LIBRESSL_VERSION_NUMBER >= 0x2080000f)
382
404
  ossl_sslctx_session_get_cb(SSL *ssl, const unsigned char *buf, int len, int *copy)
383
405
  #else
384
406
  ossl_sslctx_session_get_cb(SSL *ssl, unsigned char *buf, int len, int *copy)
@@ -590,7 +612,7 @@ ssl_renegotiation_cb(const SSL *ssl)
590
612
  #if !defined(OPENSSL_NO_NEXTPROTONEG) || \
591
613
  defined(HAVE_SSL_CTX_SET_ALPN_SELECT_CB)
592
614
  static VALUE
593
- ssl_npn_encode_protocol_i(VALUE cur, VALUE encoded)
615
+ ssl_npn_encode_protocol_i(RB_BLOCK_CALL_FUNC_ARGLIST(cur, encoded))
594
616
  {
595
617
  int len = RSTRING_LENINT(cur);
596
618
  char len_byte;
@@ -607,7 +629,7 @@ static VALUE
607
629
  ssl_encode_npn_protocols(VALUE protocols)
608
630
  {
609
631
  VALUE encoded = rb_str_new(NULL, 0);
610
- rb_iterate(rb_each, protocols, ssl_npn_encode_protocol_i, encoded);
632
+ rb_block_call(protocols, id_each, 0, 0, ssl_npn_encode_protocol_i, encoded);
611
633
  return encoded;
612
634
  }
613
635
 
@@ -677,7 +699,7 @@ static int
677
699
  ssl_npn_advertise_cb(SSL *ssl, const unsigned char **out, unsigned int *outlen,
678
700
  void *arg)
679
701
  {
680
- VALUE protocols = (VALUE)arg;
702
+ VALUE protocols = rb_attr_get((VALUE)arg, id_npn_protocols_encoded);
681
703
 
682
704
  *out = (const unsigned char *) RSTRING_PTR(protocols);
683
705
  *outlen = RSTRING_LENINT(protocols);
@@ -895,7 +917,7 @@ ossl_sslctx_setup(VALUE self)
895
917
  if (!NIL_P(val)) {
896
918
  VALUE encoded = ssl_encode_npn_protocols(val);
897
919
  rb_ivar_set(self, id_npn_protocols_encoded, encoded);
898
- SSL_CTX_set_next_protos_advertised_cb(ctx, ssl_npn_advertise_cb, (void *)encoded);
920
+ SSL_CTX_set_next_protos_advertised_cb(ctx, ssl_npn_advertise_cb, (void *)self);
899
921
  OSSL_Debug("SSL NPN advertise callback added");
900
922
  }
901
923
  if (RTEST(rb_attr_get(self, id_i_npn_select_cb))) {
@@ -1513,6 +1535,14 @@ ssl_started(SSL *ssl)
1513
1535
  return SSL_get_fd(ssl) >= 0;
1514
1536
  }
1515
1537
 
1538
+ static void
1539
+ ossl_ssl_mark(void *ptr)
1540
+ {
1541
+ SSL *ssl = ptr;
1542
+ rb_gc_mark((VALUE)SSL_get_ex_data(ssl, ossl_ssl_ex_ptr_idx));
1543
+ rb_gc_mark((VALUE)SSL_get_ex_data(ssl, ossl_ssl_ex_vcb_idx));
1544
+ }
1545
+
1516
1546
  static void
1517
1547
  ossl_ssl_free(void *ssl)
1518
1548
  {
@@ -1522,7 +1552,7 @@ ossl_ssl_free(void *ssl)
1522
1552
  const rb_data_type_t ossl_ssl_type = {
1523
1553
  "OpenSSL/SSL",
1524
1554
  {
1525
- 0, ossl_ssl_free,
1555
+ ossl_ssl_mark, ossl_ssl_free,
1526
1556
  },
1527
1557
  0, 0, RUBY_TYPED_FREE_IMMEDIATELY,
1528
1558
  };
@@ -1678,6 +1708,11 @@ ossl_start_ssl(VALUE self, int (*func)(), const char *funcname, VALUE opts)
1678
1708
  rb_io_wait_readable(fptr->fd);
1679
1709
  continue;
1680
1710
  case SSL_ERROR_SYSCALL:
1711
+ #ifdef __APPLE__
1712
+ /* See ossl_ssl_write_internal() */
1713
+ if (errno == EPROTOTYPE)
1714
+ continue;
1715
+ #endif
1681
1716
  if (errno) rb_sys_fail(funcname);
1682
1717
  ossl_raise(eSSLError, "%s SYSCALL returned=%d errno=%d state=%s", funcname, ret2, errno, SSL_state_string_long(ssl));
1683
1718
  #if defined(SSL_R_CERTIFICATE_VERIFY_FAILED)
@@ -1826,7 +1861,6 @@ ossl_ssl_read_internal(int argc, VALUE *argv, VALUE self, int nonblock)
1826
1861
  else
1827
1862
  rb_str_modify_expand(str, ilen - RSTRING_LEN(str));
1828
1863
  }
1829
- OBJ_TAINT(str);
1830
1864
  rb_str_set_len(str, 0);
1831
1865
  if (ilen == 0)
1832
1866
  return str;
@@ -1835,26 +1869,36 @@ ossl_ssl_read_internal(int argc, VALUE *argv, VALUE self, int nonblock)
1835
1869
  io = rb_attr_get(self, id_i_io);
1836
1870
  GetOpenFile(io, fptr);
1837
1871
  if (ssl_started(ssl)) {
1838
- for (;;){
1872
+ rb_str_locktmp(str);
1873
+ for (;;) {
1839
1874
  nread = SSL_read(ssl, RSTRING_PTR(str), ilen);
1840
1875
  switch(ssl_get_error(ssl, nread)){
1841
1876
  case SSL_ERROR_NONE:
1877
+ rb_str_unlocktmp(str);
1842
1878
  goto end;
1843
1879
  case SSL_ERROR_ZERO_RETURN:
1880
+ rb_str_unlocktmp(str);
1844
1881
  if (no_exception_p(opts)) { return Qnil; }
1845
1882
  rb_eof_error();
1846
1883
  case SSL_ERROR_WANT_WRITE:
1847
- if (no_exception_p(opts)) { return sym_wait_writable; }
1848
- write_would_block(nonblock);
1884
+ if (nonblock) {
1885
+ rb_str_unlocktmp(str);
1886
+ if (no_exception_p(opts)) { return sym_wait_writable; }
1887
+ write_would_block(nonblock);
1888
+ }
1849
1889
  rb_io_wait_writable(fptr->fd);
1850
1890
  continue;
1851
1891
  case SSL_ERROR_WANT_READ:
1852
- if (no_exception_p(opts)) { return sym_wait_readable; }
1853
- read_would_block(nonblock);
1892
+ if (nonblock) {
1893
+ rb_str_unlocktmp(str);
1894
+ if (no_exception_p(opts)) { return sym_wait_readable; }
1895
+ read_would_block(nonblock);
1896
+ }
1854
1897
  rb_io_wait_readable(fptr->fd);
1855
1898
  continue;
1856
1899
  case SSL_ERROR_SYSCALL:
1857
1900
  if (!ERR_peek_error()) {
1901
+ rb_str_unlocktmp(str);
1858
1902
  if (errno)
1859
1903
  rb_sys_fail(0);
1860
1904
  else {
@@ -1869,19 +1913,32 @@ ossl_ssl_read_internal(int argc, VALUE *argv, VALUE self, int nonblock)
1869
1913
  rb_eof_error();
1870
1914
  }
1871
1915
  }
1916
+ /* fall through */
1872
1917
  default:
1918
+ rb_str_unlocktmp(str);
1873
1919
  ossl_raise(eSSLError, "SSL_read");
1874
1920
  }
1875
1921
  }
1876
1922
  }
1877
1923
  else {
1878
- ID meth = nonblock ? rb_intern("read_nonblock") : rb_intern("sysread");
1879
-
1880
- rb_warning("SSL session is not started yet.");
1881
- if (nonblock)
1882
- return rb_funcall(io, meth, 3, len, str, opts);
1883
- else
1884
- return rb_funcall(io, meth, 2, len, str);
1924
+ ID meth = nonblock ? rb_intern("read_nonblock") : rb_intern("sysread");
1925
+
1926
+ rb_warning("SSL session is not started yet.");
1927
+ #if defined(RB_PASS_KEYWORDS)
1928
+ if (nonblock) {
1929
+ VALUE argv[3];
1930
+ argv[0] = len;
1931
+ argv[1] = str;
1932
+ argv[2] = opts;
1933
+ return rb_funcallv_kw(io, meth, 3, argv, RB_PASS_KEYWORDS);
1934
+ }
1935
+ #else
1936
+ if (nonblock) {
1937
+ return rb_funcall(io, meth, 3, len, str, opts);
1938
+ }
1939
+ #endif
1940
+ else
1941
+ return rb_funcall(io, meth, 2, len, str);
1885
1942
  }
1886
1943
 
1887
1944
  end:
@@ -1929,21 +1986,21 @@ ossl_ssl_write_internal(VALUE self, VALUE str, VALUE opts)
1929
1986
  int nwrite = 0;
1930
1987
  rb_io_t *fptr;
1931
1988
  int nonblock = opts != Qfalse;
1932
- VALUE io;
1989
+ VALUE tmp, io;
1933
1990
 
1934
- StringValue(str);
1991
+ tmp = rb_str_new_frozen(StringValue(str));
1935
1992
  GetSSL(self, ssl);
1936
1993
  io = rb_attr_get(self, id_i_io);
1937
1994
  GetOpenFile(io, fptr);
1938
1995
  if (ssl_started(ssl)) {
1939
- for (;;){
1940
- int num = RSTRING_LENINT(str);
1996
+ for (;;) {
1997
+ int num = RSTRING_LENINT(tmp);
1941
1998
 
1942
1999
  /* SSL_write(3ssl) manpage states num == 0 is undefined */
1943
2000
  if (num == 0)
1944
2001
  goto end;
1945
2002
 
1946
- nwrite = SSL_write(ssl, RSTRING_PTR(str), num);
2003
+ nwrite = SSL_write(ssl, RSTRING_PTR(tmp), num);
1947
2004
  switch(ssl_get_error(ssl, nwrite)){
1948
2005
  case SSL_ERROR_NONE:
1949
2006
  goto end;
@@ -1958,6 +2015,16 @@ ossl_ssl_write_internal(VALUE self, VALUE str, VALUE opts)
1958
2015
  rb_io_wait_readable(fptr->fd);
1959
2016
  continue;
1960
2017
  case SSL_ERROR_SYSCALL:
2018
+ #ifdef __APPLE__
2019
+ /*
2020
+ * It appears that send syscall can return EPROTOTYPE if the
2021
+ * socket is being torn down. Retry to get a proper errno to
2022
+ * make the error handling in line with the socket library.
2023
+ * [Bug #14713] https://bugs.ruby-lang.org/issues/14713
2024
+ */
2025
+ if (errno == EPROTOTYPE)
2026
+ continue;
2027
+ #endif
1961
2028
  if (errno) rb_sys_fail(0);
1962
2029
  default:
1963
2030
  ossl_raise(eSSLError, "SSL_write");
@@ -1968,11 +2035,21 @@ ossl_ssl_write_internal(VALUE self, VALUE str, VALUE opts)
1968
2035
  ID meth = nonblock ?
1969
2036
  rb_intern("write_nonblock") : rb_intern("syswrite");
1970
2037
 
1971
- rb_warning("SSL session is not started yet.");
1972
- if (nonblock)
1973
- return rb_funcall(io, meth, 2, str, opts);
1974
- else
1975
- return rb_funcall(io, meth, 1, str);
2038
+ rb_warning("SSL session is not started yet.");
2039
+ #if defined(RB_PASS_KEYWORDS)
2040
+ if (nonblock) {
2041
+ VALUE argv[2];
2042
+ argv[0] = str;
2043
+ argv[1] = opts;
2044
+ return rb_funcallv_kw(io, meth, 2, argv, RB_PASS_KEYWORDS);
2045
+ }
2046
+ #else
2047
+ if (nonblock) {
2048
+ return rb_funcall(io, meth, 2, str, opts);
2049
+ }
2050
+ #endif
2051
+ else
2052
+ return rb_funcall(io, meth, 1, str);
1976
2053
  }
1977
2054
 
1978
2055
  end:
@@ -2915,6 +2992,7 @@ Init_ossl_ssl(void)
2915
2992
  id_tmp_dh_callback = rb_intern("tmp_dh_callback");
2916
2993
  id_tmp_ecdh_callback = rb_intern("tmp_ecdh_callback");
2917
2994
  id_npn_protocols_encoded = rb_intern("npn_protocols_encoded");
2995
+ id_each = rb_intern_const("each");
2918
2996
 
2919
2997
  #define DefIVarID(name) do \
2920
2998
  id_i_##name = rb_intern("@"#name); while (0)
@@ -10,6 +10,6 @@
10
10
  #if !defined(_OSSL_VERSION_H_)
11
11
  #define _OSSL_VERSION_H_
12
12
 
13
- #define OSSL_VERSION "2.1.1"
13
+ #define OSSL_VERSION "2.1.4"
14
14
 
15
15
  #endif /* _OSSL_VERSION_H_ */
@@ -44,7 +44,13 @@ Init_ossl_x509(void)
44
44
  Init_ossl_x509revoked();
45
45
  Init_ossl_x509store();
46
46
 
47
+ /* Constants are up-to-date with 1.1.1. */
48
+
49
+ /* Certificate verification error code */
47
50
  DefX509Const(V_OK);
51
+ #if defined(X509_V_ERR_UNSPECIFIED) /* 1.0.1r, 1.0.2f, 1.1.0 */
52
+ DefX509Const(V_ERR_UNSPECIFIED);
53
+ #endif
48
54
  DefX509Const(V_ERR_UNABLE_TO_GET_ISSUER_CERT);
49
55
  DefX509Const(V_ERR_UNABLE_TO_GET_CRL);
50
56
  DefX509Const(V_ERR_UNABLE_TO_DECRYPT_CERT_SIGNATURE);
@@ -76,8 +82,73 @@ Init_ossl_x509(void)
76
82
  DefX509Const(V_ERR_AKID_SKID_MISMATCH);
77
83
  DefX509Const(V_ERR_AKID_ISSUER_SERIAL_MISMATCH);
78
84
  DefX509Const(V_ERR_KEYUSAGE_NO_CERTSIGN);
85
+ DefX509Const(V_ERR_UNABLE_TO_GET_CRL_ISSUER);
86
+ DefX509Const(V_ERR_UNHANDLED_CRITICAL_EXTENSION);
87
+ DefX509Const(V_ERR_KEYUSAGE_NO_CRL_SIGN);
88
+ DefX509Const(V_ERR_UNHANDLED_CRITICAL_CRL_EXTENSION);
89
+ DefX509Const(V_ERR_INVALID_NON_CA);
90
+ DefX509Const(V_ERR_PROXY_PATH_LENGTH_EXCEEDED);
91
+ DefX509Const(V_ERR_KEYUSAGE_NO_DIGITAL_SIGNATURE);
92
+ DefX509Const(V_ERR_PROXY_CERTIFICATES_NOT_ALLOWED);
93
+ DefX509Const(V_ERR_INVALID_EXTENSION);
94
+ DefX509Const(V_ERR_INVALID_POLICY_EXTENSION);
95
+ DefX509Const(V_ERR_NO_EXPLICIT_POLICY);
96
+ DefX509Const(V_ERR_DIFFERENT_CRL_SCOPE);
97
+ DefX509Const(V_ERR_UNSUPPORTED_EXTENSION_FEATURE);
98
+ DefX509Const(V_ERR_UNNESTED_RESOURCE);
99
+ DefX509Const(V_ERR_PERMITTED_VIOLATION);
100
+ DefX509Const(V_ERR_EXCLUDED_VIOLATION);
101
+ DefX509Const(V_ERR_SUBTREE_MINMAX);
79
102
  DefX509Const(V_ERR_APPLICATION_VERIFICATION);
103
+ DefX509Const(V_ERR_UNSUPPORTED_CONSTRAINT_TYPE);
104
+ DefX509Const(V_ERR_UNSUPPORTED_CONSTRAINT_SYNTAX);
105
+ DefX509Const(V_ERR_UNSUPPORTED_NAME_SYNTAX);
106
+ DefX509Const(V_ERR_CRL_PATH_VALIDATION_ERROR);
107
+ #if defined(X509_V_ERR_PATH_LOOP)
108
+ DefX509Const(V_ERR_PATH_LOOP);
109
+ #endif
110
+ #if defined(X509_V_ERR_SUITE_B_INVALID_VERSION)
111
+ DefX509Const(V_ERR_SUITE_B_INVALID_VERSION);
112
+ DefX509Const(V_ERR_SUITE_B_INVALID_ALGORITHM);
113
+ DefX509Const(V_ERR_SUITE_B_INVALID_CURVE);
114
+ DefX509Const(V_ERR_SUITE_B_INVALID_SIGNATURE_ALGORITHM);
115
+ DefX509Const(V_ERR_SUITE_B_LOS_NOT_ALLOWED);
116
+ DefX509Const(V_ERR_SUITE_B_CANNOT_SIGN_P_384_WITH_P_256);
117
+ #endif
118
+ #if defined(X509_V_ERR_HOSTNAME_MISMATCH)
119
+ DefX509Const(V_ERR_HOSTNAME_MISMATCH);
120
+ DefX509Const(V_ERR_EMAIL_MISMATCH);
121
+ DefX509Const(V_ERR_IP_ADDRESS_MISMATCH);
122
+ #endif
123
+ #if defined(X509_V_ERR_DANE_NO_MATCH)
124
+ DefX509Const(V_ERR_DANE_NO_MATCH);
125
+ #endif
126
+ #if defined(X509_V_ERR_EE_KEY_TOO_SMALL)
127
+ DefX509Const(V_ERR_EE_KEY_TOO_SMALL);
128
+ DefX509Const(V_ERR_CA_KEY_TOO_SMALL);
129
+ DefX509Const(V_ERR_CA_MD_TOO_WEAK);
130
+ #endif
131
+ #if defined(X509_V_ERR_INVALID_CALL)
132
+ DefX509Const(V_ERR_INVALID_CALL);
133
+ #endif
134
+ #if defined(X509_V_ERR_STORE_LOOKUP)
135
+ DefX509Const(V_ERR_STORE_LOOKUP);
136
+ #endif
137
+ #if defined(X509_V_ERR_NO_VALID_SCTS)
138
+ DefX509Const(V_ERR_NO_VALID_SCTS);
139
+ #endif
140
+ #if defined(X509_V_ERR_PROXY_SUBJECT_NAME_VIOLATION)
141
+ DefX509Const(V_ERR_PROXY_SUBJECT_NAME_VIOLATION);
142
+ #endif
143
+ #if defined(X509_V_ERR_OCSP_VERIFY_NEEDED)
144
+ DefX509Const(V_ERR_OCSP_VERIFY_NEEDED);
145
+ DefX509Const(V_ERR_OCSP_VERIFY_FAILED);
146
+ DefX509Const(V_ERR_OCSP_CERT_UNKNOWN);
147
+ #endif
80
148
 
149
+ /* Certificate verify flags */
150
+ /* Set by Store#flags= and StoreContext#flags=. */
151
+ DefX509Const(V_FLAG_USE_CHECK_TIME);
81
152
  /* Set by Store#flags= and StoreContext#flags=. Enables CRL checking for the
82
153
  * certificate chain leaf. */
83
154
  DefX509Const(V_FLAG_CRL_CHECK);
@@ -122,6 +193,26 @@ Init_ossl_x509(void)
122
193
  * Enabled by default in OpenSSL >= 1.1.0. */
123
194
  DefX509Const(V_FLAG_TRUSTED_FIRST);
124
195
  #endif
196
+ #if defined(X509_V_FLAG_SUITEB_128_LOS_ONLY)
197
+ /* Set by Store#flags= and StoreContext#flags=.
198
+ * Enables Suite B 128 bit only mode. */
199
+ DefX509Const(V_FLAG_SUITEB_128_LOS_ONLY);
200
+ #endif
201
+ #if defined(X509_V_FLAG_SUITEB_192_LOS)
202
+ /* Set by Store#flags= and StoreContext#flags=.
203
+ * Enables Suite B 192 bit only mode. */
204
+ DefX509Const(V_FLAG_SUITEB_192_LOS);
205
+ #endif
206
+ #if defined(X509_V_FLAG_SUITEB_128_LOS)
207
+ /* Set by Store#flags= and StoreContext#flags=.
208
+ * Enables Suite B 128 bit mode allowing 192 bit algorithms. */
209
+ DefX509Const(V_FLAG_SUITEB_128_LOS);
210
+ #endif
211
+ #if defined(X509_V_FLAG_PARTIAL_CHAIN)
212
+ /* Set by Store#flags= and StoreContext#flags=.
213
+ * Allows partial chains if at least one certificate is in trusted store. */
214
+ DefX509Const(V_FLAG_PARTIAL_CHAIN);
215
+ #endif
125
216
  #if defined(X509_V_FLAG_NO_ALT_CHAINS)
126
217
  /* Set by Store#flags= and StoreContext#flags=. Suppresses searching for
127
218
  * a alternative chain. No effect in OpenSSL >= 1.1.0. */
@@ -437,6 +437,7 @@ ossl_x509ext_to_der(VALUE obj)
437
437
  void
438
438
  Init_ossl_x509ext(void)
439
439
  {
440
+ #undef rb_intern
440
441
  #if 0
441
442
  mOSSL = rb_define_module("OpenSSL");
442
443
  eOSSLError = rb_define_class_under(mOSSL, "OpenSSLError", rb_eStandardError);
@@ -250,14 +250,12 @@ ossl_x509name_to_s_old(VALUE self)
250
250
  {
251
251
  X509_NAME *name;
252
252
  char *buf;
253
- VALUE str;
254
253
 
255
254
  GetX509Name(self, name);
256
255
  buf = X509_NAME_oneline(name, NULL, 0);
257
- str = rb_str_new2(buf);
258
- OPENSSL_free(buf);
259
-
260
- return str;
256
+ if (!buf)
257
+ ossl_raise(eX509NameError, "X509_NAME_oneline");
258
+ return ossl_buf2str(buf, rb_long2int(strlen(buf)));
261
259
  }
262
260
 
263
261
  static VALUE
@@ -265,12 +263,14 @@ x509name_print(VALUE self, unsigned long iflag)
265
263
  {
266
264
  X509_NAME *name;
267
265
  BIO *out;
266
+ int ret;
268
267
 
269
268
  GetX509Name(self, name);
270
269
  out = BIO_new(BIO_s_mem());
271
270
  if (!out)
272
271
  ossl_raise(eX509NameError, NULL);
273
- if (!X509_NAME_print_ex(out, name, 0, iflag)) {
272
+ ret = X509_NAME_print_ex(out, name, 0, iflag);
273
+ if (ret < 0 || (iflag == XN_FLAG_COMPAT && ret == 0)) {
274
274
  BIO_free(out);
275
275
  ossl_raise(eX509NameError, "X509_NAME_print_ex");
276
276
  }
@@ -400,7 +400,7 @@ ossl_x509name_cmp(VALUE self, VALUE other)
400
400
 
401
401
  result = ossl_x509name_cmp0(self, other);
402
402
  if (result < 0) return INT2FIX(-1);
403
- if (result > 1) return INT2FIX(1);
403
+ if (result > 0) return INT2FIX(1);
404
404
 
405
405
  return INT2FIX(0);
406
406
  }
@@ -502,6 +502,7 @@ ossl_x509name_to_der(VALUE self)
502
502
  void
503
503
  Init_ossl_x509name(void)
504
504
  {
505
+ #undef rb_intern
505
506
  VALUE utf8str, ptrstr, ia5str, hash;
506
507
 
507
508
  #if 0
@@ -105,6 +105,13 @@ VALUE cX509Store;
105
105
  VALUE cX509StoreContext;
106
106
  VALUE eX509StoreError;
107
107
 
108
+ static void
109
+ ossl_x509store_mark(void *ptr)
110
+ {
111
+ X509_STORE *store = ptr;
112
+ rb_gc_mark((VALUE)X509_STORE_get_ex_data(store, store_ex_verify_cb_idx));
113
+ }
114
+
108
115
  static void
109
116
  ossl_x509store_free(void *ptr)
110
117
  {
@@ -114,7 +121,7 @@ ossl_x509store_free(void *ptr)
114
121
  static const rb_data_type_t ossl_x509store_type = {
115
122
  "OpenSSL/X509/STORE",
116
123
  {
117
- 0, ossl_x509store_free,
124
+ ossl_x509store_mark, ossl_x509store_free,
118
125
  },
119
126
  0, 0, RUBY_TYPED_FREE_IMMEDIATELY,
120
127
  };
@@ -304,7 +311,6 @@ ossl_x509store_add_file(VALUE self, VALUE file)
304
311
  char *path = NULL;
305
312
 
306
313
  if(file != Qnil){
307
- rb_check_safe_obj(file);
308
314
  path = StringValueCStr(file);
309
315
  }
310
316
  GetX509Store(self, store);
@@ -340,7 +346,6 @@ ossl_x509store_add_path(VALUE self, VALUE dir)
340
346
  char *path = NULL;
341
347
 
342
348
  if(dir != Qnil){
343
- rb_check_safe_obj(dir);
344
349
  path = StringValueCStr(dir);
345
350
  }
346
351
  GetX509Store(self, store);
@@ -458,23 +463,16 @@ ossl_x509store_verify(int argc, VALUE *argv, VALUE self)
458
463
  return result;
459
464
  }
460
465
 
461
- /*
462
- * Public Functions
463
- */
464
- static void ossl_x509stctx_free(void*);
465
-
466
-
467
- static const rb_data_type_t ossl_x509stctx_type = {
468
- "OpenSSL/X509/STORE_CTX",
469
- {
470
- 0, ossl_x509stctx_free,
471
- },
472
- 0, 0, RUBY_TYPED_FREE_IMMEDIATELY,
473
- };
474
-
475
466
  /*
476
467
  * Private functions
477
468
  */
469
+ static void
470
+ ossl_x509stctx_mark(void *ptr)
471
+ {
472
+ X509_STORE_CTX *ctx = ptr;
473
+ rb_gc_mark((VALUE)X509_STORE_CTX_get_ex_data(ctx, stctx_ex_verify_cb_idx));
474
+ }
475
+
478
476
  static void
479
477
  ossl_x509stctx_free(void *ptr)
480
478
  {
@@ -486,6 +484,14 @@ ossl_x509stctx_free(void *ptr)
486
484
  X509_STORE_CTX_free(ctx);
487
485
  }
488
486
 
487
+ static const rb_data_type_t ossl_x509stctx_type = {
488
+ "OpenSSL/X509/STORE_CTX",
489
+ {
490
+ ossl_x509stctx_mark, ossl_x509stctx_free,
491
+ },
492
+ 0, 0, RUBY_TYPED_FREE_IMMEDIATELY,
493
+ };
494
+
489
495
  static VALUE
490
496
  ossl_x509stctx_alloc(VALUE klass)
491
497
  {
@@ -519,7 +525,9 @@ static VALUE ossl_x509stctx_set_time(VALUE, VALUE);
519
525
 
520
526
  /*
521
527
  * call-seq:
522
- * StoreContext.new(store, cert = nil, chain = nil)
528
+ * StoreContext.new(store, cert = nil, untrusted = nil)
529
+ *
530
+ * Sets up a StoreContext for a verification of the X.509 certificate _cert_.
523
531
  */
524
532
  static VALUE
525
533
  ossl_x509stctx_initialize(int argc, VALUE *argv, VALUE self)
@@ -529,15 +537,24 @@ ossl_x509stctx_initialize(int argc, VALUE *argv, VALUE self)
529
537
  X509_STORE *x509st;
530
538
  X509 *x509 = NULL;
531
539
  STACK_OF(X509) *x509s = NULL;
540
+ int state;
532
541
 
533
542
  rb_scan_args(argc, argv, "12", &store, &cert, &chain);
534
543
  GetX509StCtx(self, ctx);
535
544
  GetX509Store(store, x509st);
536
- if(!NIL_P(cert)) x509 = DupX509CertPtr(cert); /* NEED TO DUP */
537
- if(!NIL_P(chain)) x509s = ossl_x509_ary2sk(chain);
538
- if(X509_STORE_CTX_init(ctx, x509st, x509, x509s) != 1){
545
+ if (!NIL_P(cert))
546
+ x509 = DupX509CertPtr(cert); /* NEED TO DUP */
547
+ if (!NIL_P(chain)) {
548
+ x509s = ossl_protect_x509_ary2sk(chain, &state);
549
+ if (state) {
550
+ X509_free(x509);
551
+ rb_jump_tag(state);
552
+ }
553
+ }
554
+ if (X509_STORE_CTX_init(ctx, x509st, x509, x509s) != 1){
555
+ X509_free(x509);
539
556
  sk_X509_pop_free(x509s, X509_free);
540
- ossl_raise(eX509StoreError, NULL);
557
+ ossl_raise(eX509StoreError, "X509_STORE_CTX_init");
541
558
  }
542
559
  if (!NIL_P(t = rb_iv_get(store, "@time")))
543
560
  ossl_x509stctx_set_time(self, t);
@@ -771,6 +788,7 @@ ossl_x509stctx_set_time(VALUE self, VALUE time)
771
788
  void
772
789
  Init_ossl_x509store(void)
773
790
  {
791
+ #undef rb_intern
774
792
  #if 0
775
793
  mOSSL = rb_define_module("OpenSSL");
776
794
  eOSSLError = rb_define_class_under(mOSSL, "OpenSSLError", rb_eStandardError);
@@ -316,20 +316,15 @@ module OpenSSL::Buffering
316
316
  @wbuffer << s
317
317
  @wbuffer.force_encoding(Encoding::BINARY)
318
318
  @sync ||= false
319
- if @sync or @wbuffer.size > BLOCK_SIZE or idx = @wbuffer.rindex($/)
320
- remain = idx ? idx + $/.size : @wbuffer.length
321
- nwritten = 0
322
- while remain > 0
323
- str = @wbuffer[nwritten,remain]
319
+ if @sync or @wbuffer.size > BLOCK_SIZE
320
+ until @wbuffer.empty?
324
321
  begin
325
- nwrote = syswrite(str)
322
+ nwrote = syswrite(@wbuffer)
326
323
  rescue Errno::EAGAIN
327
324
  retry
328
325
  end
329
- remain -= nwrote
330
- nwritten += nwrote
326
+ @wbuffer[0, nwrote] = ""
331
327
  end
332
- @wbuffer[0,nwritten] = ""
333
328
  end
334
329
  end
335
330
 
@@ -409,9 +404,7 @@ module OpenSSL::Buffering
409
404
  end
410
405
  args.each{|arg|
411
406
  s << arg.to_s
412
- if $/ && /\n\z/ !~ s
413
- s << "\n"
414
- end
407
+ s.sub!(/(?<!\n)\z/, "\n")
415
408
  }
416
409
  do_write(s)
417
410
  nil