0
|
1
|
|
2 /*
|
|
3 * Copyright (C) Igor Sysoev
|
660
|
4 * Copyright (C) Nginx, Inc.
|
0
|
5 */
|
|
6
|
|
7
|
|
8 #include <ngx_config.h>
|
|
9 #include <ngx_core.h>
|
|
10 #include <ngx_event.h>
|
|
11
|
90
|
12
|
|
13 typedef struct {
|
446
|
14 ngx_uint_t engine; /* unsigned engine:1; */
|
90
|
15 } ngx_openssl_conf_t;
|
28
|
16
|
0
|
17
|
220
|
18 static int ngx_http_ssl_verify_callback(int ok, X509_STORE_CTX *x509_store);
|
542
|
19 static void ngx_ssl_info_callback(const ngx_ssl_conn_t *ssl_conn, int where,
|
|
20 int ret);
|
96
|
21 static void ngx_ssl_handshake_handler(ngx_event_t *ev);
|
38
|
22 static ngx_int_t ngx_ssl_handle_recv(ngx_connection_t *c, int n);
|
22
|
23 static void ngx_ssl_write_handler(ngx_event_t *wev);
|
|
24 static void ngx_ssl_read_handler(ngx_event_t *rev);
|
126
|
25 static void ngx_ssl_shutdown_handler(ngx_event_t *ev);
|
96
|
26 static void ngx_ssl_connection_error(ngx_connection_t *c, int sslerr,
|
|
27 ngx_err_t err, char *text);
|
358
|
28 static void ngx_ssl_clear_error(ngx_log_t *log);
|
272
|
29
|
634
|
30 ngx_int_t ngx_ssl_session_cache_init(ngx_shm_zone_t *shm_zone, void *data);
|
272
|
31 static int ngx_ssl_new_session(ngx_ssl_conn_t *ssl_conn,
|
|
32 ngx_ssl_session_t *sess);
|
|
33 static ngx_ssl_session_t *ngx_ssl_get_cached_session(ngx_ssl_conn_t *ssl_conn,
|
|
34 u_char *id, int len, int *copy);
|
|
35 static void ngx_ssl_remove_session(SSL_CTX *ssl, ngx_ssl_session_t *sess);
|
|
36 static void ngx_ssl_expire_sessions(ngx_ssl_session_cache_t *cache,
|
|
37 ngx_slab_pool_t *shpool, ngx_uint_t n);
|
274
|
38 static void ngx_ssl_session_rbtree_insert_value(ngx_rbtree_node_t *temp,
|
|
39 ngx_rbtree_node_t *node, ngx_rbtree_node_t *sentinel);
|
272
|
40
|
90
|
41 static void *ngx_openssl_create_conf(ngx_cycle_t *cycle);
|
446
|
42 static char *ngx_openssl_engine(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
|
120
|
43 static void ngx_openssl_exit(ngx_cycle_t *cycle);
|
90
|
44
|
|
45
|
|
46 static ngx_command_t ngx_openssl_commands[] = {
|
|
47
|
|
48 { ngx_string("ssl_engine"),
|
|
49 NGX_MAIN_CONF|NGX_DIRECT_CONF|NGX_CONF_TAKE1,
|
446
|
50 ngx_openssl_engine,
|
90
|
51 0,
|
446
|
52 0,
|
90
|
53 NULL },
|
|
54
|
|
55 ngx_null_command
|
|
56 };
|
|
57
|
|
58
|
|
59 static ngx_core_module_t ngx_openssl_module_ctx = {
|
|
60 ngx_string("openssl"),
|
|
61 ngx_openssl_create_conf,
|
446
|
62 NULL
|
126
|
63 };
|
90
|
64
|
|
65
|
|
66 ngx_module_t ngx_openssl_module = {
|
|
67 NGX_MODULE_V1,
|
|
68 &ngx_openssl_module_ctx, /* module context */
|
|
69 ngx_openssl_commands, /* module directives */
|
|
70 NGX_CORE_MODULE, /* module type */
|
|
71 NULL, /* init master */
|
|
72 NULL, /* init module */
|
|
73 NULL, /* init process */
|
|
74 NULL, /* init thread */
|
|
75 NULL, /* exit thread */
|
|
76 NULL, /* exit process */
|
120
|
77 ngx_openssl_exit, /* exit master */
|
90
|
78 NGX_MODULE_V1_PADDING
|
96
|
79 };
|
|
80
|
|
81
|
272
|
82 int ngx_ssl_connection_index;
|
|
83 int ngx_ssl_server_conf_index;
|
|
84 int ngx_ssl_session_cache_index;
|
688
|
85 int ngx_ssl_certificate_index;
|
|
86 int ngx_ssl_stapling_index;
|
220
|
87
|
|
88
|
38
|
89 ngx_int_t
|
|
90 ngx_ssl_init(ngx_log_t *log)
|
0
|
91 {
|
272
|
92 OPENSSL_config(NULL);
|
|
93
|
0
|
94 SSL_library_init();
|
|
95 SSL_load_error_strings();
|
90
|
96
|
564
|
97 OpenSSL_add_all_algorithms();
|
|
98
|
688
|
99 #if OPENSSL_VERSION_NUMBER >= 0x0090800fL
|
678
|
100 #ifndef SSL_OP_NO_COMPRESSION
|
|
101 {
|
|
102 /*
|
|
103 * Disable gzip compression in OpenSSL prior to 1.0.0 version,
|
|
104 * this saves about 522K per connection.
|
|
105 */
|
688
|
106 int n;
|
678
|
107 STACK_OF(SSL_COMP) *ssl_comp_methods;
|
|
108
|
|
109 ssl_comp_methods = SSL_COMP_get_compression_methods();
|
|
110 n = sk_SSL_COMP_num(ssl_comp_methods);
|
|
111
|
688
|
112 while (n--) {
|
|
113 (void) sk_SSL_COMP_pop(ssl_comp_methods);
|
678
|
114 }
|
|
115 }
|
|
116 #endif
|
688
|
117 #endif
|
678
|
118
|
272
|
119 ngx_ssl_connection_index = SSL_get_ex_new_index(0, NULL, NULL, NULL, NULL);
|
|
120
|
|
121 if (ngx_ssl_connection_index == -1) {
|
|
122 ngx_ssl_error(NGX_LOG_ALERT, log, 0, "SSL_get_ex_new_index() failed");
|
|
123 return NGX_ERROR;
|
|
124 }
|
220
|
125
|
272
|
126 ngx_ssl_server_conf_index = SSL_CTX_get_ex_new_index(0, NULL, NULL, NULL,
|
|
127 NULL);
|
|
128 if (ngx_ssl_server_conf_index == -1) {
|
|
129 ngx_ssl_error(NGX_LOG_ALERT, log, 0,
|
|
130 "SSL_CTX_get_ex_new_index() failed");
|
|
131 return NGX_ERROR;
|
|
132 }
|
|
133
|
|
134 ngx_ssl_session_cache_index = SSL_CTX_get_ex_new_index(0, NULL, NULL, NULL,
|
|
135 NULL);
|
|
136 if (ngx_ssl_session_cache_index == -1) {
|
|
137 ngx_ssl_error(NGX_LOG_ALERT, log, 0,
|
|
138 "SSL_CTX_get_ex_new_index() failed");
|
220
|
139 return NGX_ERROR;
|
|
140 }
|
|
141
|
688
|
142 ngx_ssl_certificate_index = SSL_CTX_get_ex_new_index(0, NULL, NULL, NULL,
|
|
143 NULL);
|
|
144 if (ngx_ssl_certificate_index == -1) {
|
|
145 ngx_ssl_error(NGX_LOG_ALERT, log, 0,
|
|
146 "SSL_CTX_get_ex_new_index() failed");
|
|
147 return NGX_ERROR;
|
|
148 }
|
|
149
|
|
150 ngx_ssl_stapling_index = SSL_CTX_get_ex_new_index(0, NULL, NULL, NULL,
|
|
151 NULL);
|
|
152 if (ngx_ssl_stapling_index == -1) {
|
|
153 ngx_ssl_error(NGX_LOG_ALERT, log, 0,
|
|
154 "SSL_CTX_get_ex_new_index() failed");
|
|
155 return NGX_ERROR;
|
|
156 }
|
|
157
|
0
|
158 return NGX_OK;
|
|
159 }
|
|
160
|
|
161
|
38
|
162 ngx_int_t
|
272
|
163 ngx_ssl_create(ngx_ssl_t *ssl, ngx_uint_t protocols, void *data)
|
96
|
164 {
|
126
|
165 ssl->ctx = SSL_CTX_new(SSLv23_method());
|
96
|
166
|
|
167 if (ssl->ctx == NULL) {
|
|
168 ngx_ssl_error(NGX_LOG_EMERG, ssl->log, 0, "SSL_CTX_new() failed");
|
|
169 return NGX_ERROR;
|
|
170 }
|
|
171
|
272
|
172 if (SSL_CTX_set_ex_data(ssl->ctx, ngx_ssl_server_conf_index, data) == 0) {
|
|
173 ngx_ssl_error(NGX_LOG_EMERG, ssl->log, 0,
|
|
174 "SSL_CTX_set_ex_data() failed");
|
|
175 return NGX_ERROR;
|
|
176 }
|
|
177
|
126
|
178 /* client side options */
|
|
179
|
|
180 SSL_CTX_set_options(ssl->ctx, SSL_OP_MICROSOFT_SESS_ID_BUG);
|
|
181 SSL_CTX_set_options(ssl->ctx, SSL_OP_NETSCAPE_CHALLENGE_BUG);
|
|
182
|
|
183 /* server side options */
|
112
|
184
|
|
185 SSL_CTX_set_options(ssl->ctx, SSL_OP_SSLREF2_REUSE_CERT_TYPE_BUG);
|
|
186 SSL_CTX_set_options(ssl->ctx, SSL_OP_MICROSOFT_BIG_SSLV3_BUFFER);
|
|
187
|
|
188 /* this option allow a potential SSL 2.0 rollback (CAN-2005-2969) */
|
|
189 SSL_CTX_set_options(ssl->ctx, SSL_OP_MSIE_SSLV2_RSA_PADDING);
|
|
190
|
|
191 SSL_CTX_set_options(ssl->ctx, SSL_OP_SSLEAY_080_CLIENT_DH_BUG);
|
|
192 SSL_CTX_set_options(ssl->ctx, SSL_OP_TLS_D5_BUG);
|
|
193 SSL_CTX_set_options(ssl->ctx, SSL_OP_TLS_BLOCK_PADDING_BUG);
|
|
194
|
|
195 SSL_CTX_set_options(ssl->ctx, SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS);
|
|
196
|
380
|
197 SSL_CTX_set_options(ssl->ctx, SSL_OP_SINGLE_DH_USE);
|
96
|
198
|
658
|
199 if (!(protocols & NGX_SSL_SSLv2)) {
|
|
200 SSL_CTX_set_options(ssl->ctx, SSL_OP_NO_SSLv2);
|
|
201 }
|
|
202 if (!(protocols & NGX_SSL_SSLv3)) {
|
|
203 SSL_CTX_set_options(ssl->ctx, SSL_OP_NO_SSLv3);
|
|
204 }
|
|
205 if (!(protocols & NGX_SSL_TLSv1)) {
|
|
206 SSL_CTX_set_options(ssl->ctx, SSL_OP_NO_TLSv1);
|
96
|
207 }
|
658
|
208 #ifdef SSL_OP_NO_TLSv1_1
|
|
209 if (!(protocols & NGX_SSL_TLSv1_1)) {
|
|
210 SSL_CTX_set_options(ssl->ctx, SSL_OP_NO_TLSv1_1);
|
|
211 }
|
|
212 #endif
|
|
213 #ifdef SSL_OP_NO_TLSv1_2
|
|
214 if (!(protocols & NGX_SSL_TLSv1_2)) {
|
|
215 SSL_CTX_set_options(ssl->ctx, SSL_OP_NO_TLSv1_2);
|
|
216 }
|
|
217 #endif
|
96
|
218
|
644
|
219 #ifdef SSL_OP_NO_COMPRESSION
|
|
220 SSL_CTX_set_options(ssl->ctx, SSL_OP_NO_COMPRESSION);
|
|
221 #endif
|
|
222
|
|
223 #ifdef SSL_MODE_RELEASE_BUFFERS
|
|
224 SSL_CTX_set_mode(ssl->ctx, SSL_MODE_RELEASE_BUFFERS);
|
|
225 #endif
|
|
226
|
96
|
227 SSL_CTX_set_read_ahead(ssl->ctx, 1);
|
|
228
|
542
|
229 SSL_CTX_set_info_callback(ssl->ctx, ngx_ssl_info_callback);
|
|
230
|
96
|
231 return NGX_OK;
|
|
232 }
|
|
233
|
|
234
|
|
235 ngx_int_t
|
112
|
236 ngx_ssl_certificate(ngx_conf_t *cf, ngx_ssl_t *ssl, ngx_str_t *cert,
|
|
237 ngx_str_t *key)
|
96
|
238 {
|
688
|
239 BIO *bio;
|
|
240 X509 *x509;
|
|
241 u_long n;
|
|
242
|
454
|
243 if (ngx_conf_full_name(cf->cycle, cert, 1) != NGX_OK) {
|
96
|
244 return NGX_ERROR;
|
|
245 }
|
|
246
|
688
|
247 /*
|
|
248 * we can't use SSL_CTX_use_certificate_chain_file() as it doesn't
|
|
249 * allow to access certificate later from SSL_CTX, so we reimplement
|
|
250 * it here
|
|
251 */
|
|
252
|
|
253 bio = BIO_new_file((char *) cert->data, "r");
|
|
254 if (bio == NULL) {
|
|
255 ngx_ssl_error(NGX_LOG_EMERG, ssl->log, 0,
|
|
256 "BIO_new_file(\"%s\") failed", cert->data);
|
|
257 return NGX_ERROR;
|
|
258 }
|
|
259
|
|
260 x509 = PEM_read_bio_X509_AUX(bio, NULL, NULL, NULL);
|
|
261 if (x509 == NULL) {
|
|
262 ngx_ssl_error(NGX_LOG_EMERG, ssl->log, 0,
|
|
263 "PEM_read_bio_X509_AUX(\"%s\") failed", cert->data);
|
|
264 BIO_free(bio);
|
|
265 return NGX_ERROR;
|
|
266 }
|
|
267
|
|
268 if (SSL_CTX_use_certificate(ssl->ctx, x509) == 0) {
|
|
269 ngx_ssl_error(NGX_LOG_EMERG, ssl->log, 0,
|
|
270 "SSL_CTX_use_certificate(\"%s\") failed", cert->data);
|
|
271 X509_free(x509);
|
|
272 BIO_free(bio);
|
|
273 return NGX_ERROR;
|
|
274 }
|
|
275
|
|
276 if (SSL_CTX_set_ex_data(ssl->ctx, ngx_ssl_certificate_index, x509)
|
96
|
277 == 0)
|
|
278 {
|
|
279 ngx_ssl_error(NGX_LOG_EMERG, ssl->log, 0,
|
688
|
280 "SSL_CTX_set_ex_data() failed");
|
112
|
281 return NGX_ERROR;
|
|
282 }
|
|
283
|
688
|
284 X509_free(x509);
|
|
285
|
|
286 /* read rest of the chain */
|
|
287
|
|
288 for ( ;; ) {
|
|
289
|
|
290 x509 = PEM_read_bio_X509(bio, NULL, NULL, NULL);
|
|
291 if (x509 == NULL) {
|
|
292 n = ERR_peek_last_error();
|
|
293
|
|
294 if (ERR_GET_LIB(n) == ERR_LIB_PEM
|
|
295 && ERR_GET_REASON(n) == PEM_R_NO_START_LINE)
|
|
296 {
|
|
297 /* end of file */
|
|
298 ERR_clear_error();
|
|
299 break;
|
|
300 }
|
|
301
|
|
302 /* some real error */
|
|
303
|
|
304 ngx_ssl_error(NGX_LOG_EMERG, ssl->log, 0,
|
|
305 "PEM_read_bio_X509(\"%s\") failed", cert->data);
|
|
306 BIO_free(bio);
|
|
307 return NGX_ERROR;
|
|
308 }
|
|
309
|
|
310 if (SSL_CTX_add_extra_chain_cert(ssl->ctx, x509) == 0) {
|
|
311 ngx_ssl_error(NGX_LOG_EMERG, ssl->log, 0,
|
|
312 "SSL_CTX_add_extra_chain_cert(\"%s\") failed",
|
|
313 cert->data);
|
|
314 X509_free(x509);
|
|
315 BIO_free(bio);
|
|
316 return NGX_ERROR;
|
|
317 }
|
|
318 }
|
|
319
|
|
320 BIO_free(bio);
|
|
321
|
454
|
322 if (ngx_conf_full_name(cf->cycle, key, 1) != NGX_OK) {
|
112
|
323 return NGX_ERROR;
|
|
324 }
|
|
325
|
|
326 if (SSL_CTX_use_PrivateKey_file(ssl->ctx, (char *) key->data,
|
196
|
327 SSL_FILETYPE_PEM)
|
|
328 == 0)
|
112
|
329 {
|
|
330 ngx_ssl_error(NGX_LOG_EMERG, ssl->log, 0,
|
|
331 "SSL_CTX_use_PrivateKey_file(\"%s\") failed", key->data);
|
96
|
332 return NGX_ERROR;
|
|
333 }
|
|
334
|
|
335 return NGX_OK;
|
|
336 }
|
|
337
|
|
338
|
|
339 ngx_int_t
|
220
|
340 ngx_ssl_client_certificate(ngx_conf_t *cf, ngx_ssl_t *ssl, ngx_str_t *cert,
|
|
341 ngx_int_t depth)
|
196
|
342 {
|
220
|
343 STACK_OF(X509_NAME) *list;
|
|
344
|
|
345 SSL_CTX_set_verify(ssl->ctx, SSL_VERIFY_PEER, ngx_http_ssl_verify_callback);
|
|
346
|
|
347 SSL_CTX_set_verify_depth(ssl->ctx, depth);
|
|
348
|
|
349 if (cert->len == 0) {
|
|
350 return NGX_OK;
|
|
351 }
|
|
352
|
454
|
353 if (ngx_conf_full_name(cf->cycle, cert, 1) != NGX_OK) {
|
196
|
354 return NGX_ERROR;
|
|
355 }
|
|
356
|
|
357 if (SSL_CTX_load_verify_locations(ssl->ctx, (char *) cert->data, NULL)
|
|
358 == 0)
|
|
359 {
|
|
360 ngx_ssl_error(NGX_LOG_EMERG, ssl->log, 0,
|
|
361 "SSL_CTX_load_verify_locations(\"%s\") failed",
|
|
362 cert->data);
|
|
363 return NGX_ERROR;
|
|
364 }
|
|
365
|
220
|
366 list = SSL_load_client_CA_file((char *) cert->data);
|
|
367
|
|
368 if (list == NULL) {
|
|
369 ngx_ssl_error(NGX_LOG_EMERG, ssl->log, 0,
|
|
370 "SSL_load_client_CA_file(\"%s\") failed", cert->data);
|
|
371 return NGX_ERROR;
|
|
372 }
|
|
373
|
|
374 /*
|
|
375 * before 0.9.7h and 0.9.8 SSL_load_client_CA_file()
|
|
376 * always leaved an error in the error queue
|
|
377 */
|
|
378
|
|
379 ERR_clear_error();
|
|
380
|
|
381 SSL_CTX_set_client_CA_list(ssl->ctx, list);
|
|
382
|
196
|
383 return NGX_OK;
|
|
384 }
|
|
385
|
|
386
|
510
|
387 ngx_int_t
|
688
|
388 ngx_ssl_trusted_certificate(ngx_conf_t *cf, ngx_ssl_t *ssl, ngx_str_t *cert,
|
|
389 ngx_int_t depth)
|
|
390 {
|
|
391 SSL_CTX_set_verify_depth(ssl->ctx, depth);
|
|
392
|
|
393 if (cert->len == 0) {
|
|
394 return NGX_OK;
|
|
395 }
|
|
396
|
|
397 if (ngx_conf_full_name(cf->cycle, cert, 1) != NGX_OK) {
|
|
398 return NGX_ERROR;
|
|
399 }
|
|
400
|
|
401 if (SSL_CTX_load_verify_locations(ssl->ctx, (char *) cert->data, NULL)
|
|
402 == 0)
|
|
403 {
|
|
404 ngx_ssl_error(NGX_LOG_EMERG, ssl->log, 0,
|
|
405 "SSL_CTX_load_verify_locations(\"%s\") failed",
|
|
406 cert->data);
|
|
407 return NGX_ERROR;
|
|
408 }
|
|
409
|
|
410 return NGX_OK;
|
|
411 }
|
|
412
|
|
413
|
|
414 ngx_int_t
|
510
|
415 ngx_ssl_crl(ngx_conf_t *cf, ngx_ssl_t *ssl, ngx_str_t *crl)
|
|
416 {
|
|
417 X509_STORE *store;
|
|
418 X509_LOOKUP *lookup;
|
|
419
|
|
420 if (crl->len == 0) {
|
|
421 return NGX_OK;
|
|
422 }
|
|
423
|
|
424 if (ngx_conf_full_name(cf->cycle, crl, 1) != NGX_OK) {
|
|
425 return NGX_ERROR;
|
|
426 }
|
|
427
|
|
428 store = SSL_CTX_get_cert_store(ssl->ctx);
|
|
429
|
|
430 if (store == NULL) {
|
|
431 ngx_ssl_error(NGX_LOG_EMERG, ssl->log, 0,
|
|
432 "SSL_CTX_get_cert_store() failed");
|
|
433 return NGX_ERROR;
|
|
434 }
|
|
435
|
|
436 lookup = X509_STORE_add_lookup(store, X509_LOOKUP_file());
|
|
437
|
|
438 if (lookup == NULL) {
|
|
439 ngx_ssl_error(NGX_LOG_EMERG, ssl->log, 0,
|
|
440 "X509_STORE_add_lookup() failed");
|
|
441 return NGX_ERROR;
|
|
442 }
|
|
443
|
|
444 if (X509_LOOKUP_load_file(lookup, (char *) crl->data, X509_FILETYPE_PEM)
|
|
445 == 0)
|
|
446 {
|
|
447 ngx_ssl_error(NGX_LOG_EMERG, ssl->log, 0,
|
|
448 "X509_LOOKUP_load_file(\"%s\") failed", crl->data);
|
|
449 return NGX_ERROR;
|
|
450 }
|
|
451
|
|
452 X509_STORE_set_flags(store,
|
|
453 X509_V_FLAG_CRL_CHECK|X509_V_FLAG_CRL_CHECK_ALL);
|
|
454
|
|
455 return NGX_OK;
|
|
456 }
|
|
457
|
|
458
|
220
|
459 static int
|
|
460 ngx_http_ssl_verify_callback(int ok, X509_STORE_CTX *x509_store)
|
|
461 {
|
372
|
462 #if (NGX_DEBUG)
|
220
|
463 char *subject, *issuer;
|
|
464 int err, depth;
|
|
465 X509 *cert;
|
372
|
466 X509_NAME *sname, *iname;
|
220
|
467 ngx_connection_t *c;
|
|
468 ngx_ssl_conn_t *ssl_conn;
|
|
469
|
|
470 ssl_conn = X509_STORE_CTX_get_ex_data(x509_store,
|
|
471 SSL_get_ex_data_X509_STORE_CTX_idx());
|
|
472
|
|
473 c = ngx_ssl_get_connection(ssl_conn);
|
|
474
|
|
475 cert = X509_STORE_CTX_get_current_cert(x509_store);
|
|
476 err = X509_STORE_CTX_get_error(x509_store);
|
|
477 depth = X509_STORE_CTX_get_error_depth(x509_store);
|
|
478
|
372
|
479 sname = X509_get_subject_name(cert);
|
|
480 subject = sname ? X509_NAME_oneline(sname, NULL, 0) : "(none)";
|
|
481
|
|
482 iname = X509_get_issuer_name(cert);
|
|
483 issuer = iname ? X509_NAME_oneline(iname, NULL, 0) : "(none)";
|
220
|
484
|
272
|
485 ngx_log_debug5(NGX_LOG_DEBUG_EVENT, c->log, 0,
|
220
|
486 "verify:%d, error:%d, depth:%d, "
|
|
487 "subject:\"%s\",issuer: \"%s\"",
|
|
488 ok, err, depth, subject, issuer);
|
|
489
|
372
|
490 if (sname) {
|
|
491 OPENSSL_free(subject);
|
|
492 }
|
|
493
|
|
494 if (iname) {
|
|
495 OPENSSL_free(issuer);
|
|
496 }
|
|
497 #endif
|
|
498
|
220
|
499 return 1;
|
|
500 }
|
|
501
|
|
502
|
542
|
503 static void
|
|
504 ngx_ssl_info_callback(const ngx_ssl_conn_t *ssl_conn, int where, int ret)
|
|
505 {
|
|
506 ngx_connection_t *c;
|
|
507
|
|
508 if (where & SSL_CB_HANDSHAKE_START) {
|
|
509 c = ngx_ssl_get_connection((ngx_ssl_conn_t *) ssl_conn);
|
|
510
|
|
511 if (c->ssl->handshaked) {
|
|
512 c->ssl->renegotiation = 1;
|
|
513 ngx_log_debug0(NGX_LOG_DEBUG_EVENT, c->log, 0, "SSL renegotiation");
|
|
514 }
|
|
515 }
|
|
516 }
|
|
517
|
|
518
|
632
|
519 RSA *
|
|
520 ngx_ssl_rsa512_key_callback(SSL *ssl, int is_export, int key_length)
|
96
|
521 {
|
632
|
522 static RSA *key;
|
|
523
|
|
524 if (key_length == 512) {
|
|
525 if (key == NULL) {
|
|
526 key = RSA_generate_key(512, RSA_F4, NULL, NULL);
|
|
527 }
|
108
|
528 }
|
|
529
|
632
|
530 return key;
|
96
|
531 }
|
|
532
|
|
533
|
|
534 ngx_int_t
|
380
|
535 ngx_ssl_dhparam(ngx_conf_t *cf, ngx_ssl_t *ssl, ngx_str_t *file)
|
|
536 {
|
|
537 DH *dh;
|
|
538 BIO *bio;
|
|
539
|
|
540 /*
|
|
541 * -----BEGIN DH PARAMETERS-----
|
|
542 * MIGHAoGBALu8LcrYRnSQfEP89YDpz9vZWKP1aLQtSwju1OsPs1BMbAMCducQgAxc
|
|
543 * y7qokiYUxb7spWWl/fHSh6K8BJvmd4Bg6RqSp1fjBI9osHb302zI8pul34HcLKcl
|
|
544 * 7OZicMyaUDXYzs7vnqAnSmOrHlj6/UmI0PZdFGdX2gcd8EXP4WubAgEC
|
|
545 * -----END DH PARAMETERS-----
|
|
546 */
|
|
547
|
|
548 static unsigned char dh1024_p[] = {
|
|
549 0xBB, 0xBC, 0x2D, 0xCA, 0xD8, 0x46, 0x74, 0x90, 0x7C, 0x43, 0xFC, 0xF5,
|
|
550 0x80, 0xE9, 0xCF, 0xDB, 0xD9, 0x58, 0xA3, 0xF5, 0x68, 0xB4, 0x2D, 0x4B,
|
|
551 0x08, 0xEE, 0xD4, 0xEB, 0x0F, 0xB3, 0x50, 0x4C, 0x6C, 0x03, 0x02, 0x76,
|
|
552 0xE7, 0x10, 0x80, 0x0C, 0x5C, 0xCB, 0xBA, 0xA8, 0x92, 0x26, 0x14, 0xC5,
|
|
553 0xBE, 0xEC, 0xA5, 0x65, 0xA5, 0xFD, 0xF1, 0xD2, 0x87, 0xA2, 0xBC, 0x04,
|
|
554 0x9B, 0xE6, 0x77, 0x80, 0x60, 0xE9, 0x1A, 0x92, 0xA7, 0x57, 0xE3, 0x04,
|
|
555 0x8F, 0x68, 0xB0, 0x76, 0xF7, 0xD3, 0x6C, 0xC8, 0xF2, 0x9B, 0xA5, 0xDF,
|
|
556 0x81, 0xDC, 0x2C, 0xA7, 0x25, 0xEC, 0xE6, 0x62, 0x70, 0xCC, 0x9A, 0x50,
|
|
557 0x35, 0xD8, 0xCE, 0xCE, 0xEF, 0x9E, 0xA0, 0x27, 0x4A, 0x63, 0xAB, 0x1E,
|
|
558 0x58, 0xFA, 0xFD, 0x49, 0x88, 0xD0, 0xF6, 0x5D, 0x14, 0x67, 0x57, 0xDA,
|
|
559 0x07, 0x1D, 0xF0, 0x45, 0xCF, 0xE1, 0x6B, 0x9B
|
|
560 };
|
|
561
|
|
562 static unsigned char dh1024_g[] = { 0x02 };
|
|
563
|
|
564
|
|
565 if (file->len == 0) {
|
|
566
|
|
567 dh = DH_new();
|
|
568 if (dh == NULL) {
|
|
569 ngx_ssl_error(NGX_LOG_EMERG, ssl->log, 0, "DH_new() failed");
|
|
570 return NGX_ERROR;
|
|
571 }
|
|
572
|
|
573 dh->p = BN_bin2bn(dh1024_p, sizeof(dh1024_p), NULL);
|
|
574 dh->g = BN_bin2bn(dh1024_g, sizeof(dh1024_g), NULL);
|
|
575
|
|
576 if (dh->p == NULL || dh->g == NULL) {
|
|
577 ngx_ssl_error(NGX_LOG_EMERG, ssl->log, 0, "BN_bin2bn() failed");
|
|
578 DH_free(dh);
|
|
579 return NGX_ERROR;
|
|
580 }
|
|
581
|
|
582 SSL_CTX_set_tmp_dh(ssl->ctx, dh);
|
|
583
|
|
584 DH_free(dh);
|
|
585
|
|
586 return NGX_OK;
|
|
587 }
|
|
588
|
454
|
589 if (ngx_conf_full_name(cf->cycle, file, 1) != NGX_OK) {
|
380
|
590 return NGX_ERROR;
|
|
591 }
|
|
592
|
|
593 bio = BIO_new_file((char *) file->data, "r");
|
|
594 if (bio == NULL) {
|
|
595 ngx_ssl_error(NGX_LOG_EMERG, ssl->log, 0,
|
|
596 "BIO_new_file(\"%s\") failed", file->data);
|
|
597 return NGX_ERROR;
|
|
598 }
|
|
599
|
|
600 dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL);
|
|
601 if (dh == NULL) {
|
|
602 ngx_ssl_error(NGX_LOG_EMERG, ssl->log, 0,
|
|
603 "PEM_read_bio_DHparams(\"%s\") failed", file->data);
|
|
604 BIO_free(bio);
|
|
605 return NGX_ERROR;
|
|
606 }
|
|
607
|
|
608 SSL_CTX_set_tmp_dh(ssl->ctx, dh);
|
|
609
|
|
610 DH_free(dh);
|
|
611 BIO_free(bio);
|
|
612
|
|
613 return NGX_OK;
|
|
614 }
|
|
615
|
666
|
616
|
632
|
617 ngx_int_t
|
|
618 ngx_ssl_ecdh_curve(ngx_conf_t *cf, ngx_ssl_t *ssl, ngx_str_t *name)
|
|
619 {
|
|
620 #if OPENSSL_VERSION_NUMBER >= 0x0090800fL
|
|
621 #ifndef OPENSSL_NO_ECDH
|
|
622 int nid;
|
|
623 EC_KEY *ecdh;
|
|
624
|
|
625 /*
|
|
626 * Elliptic-Curve Diffie-Hellman parameters are either "named curves"
|
670
|
627 * from RFC 4492 section 5.1.1, or explicitly described curves over
|
632
|
628 * binary fields. OpenSSL only supports the "named curves", which provide
|
|
629 * maximum interoperability.
|
|
630 */
|
|
631
|
|
632 nid = OBJ_sn2nid((const char *) name->data);
|
|
633 if (nid == 0) {
|
|
634 ngx_ssl_error(NGX_LOG_EMERG, ssl->log, 0,
|
|
635 "Unknown curve name \"%s\"", name->data);
|
|
636 return NGX_ERROR;
|
|
637 }
|
|
638
|
|
639 ecdh = EC_KEY_new_by_curve_name(nid);
|
|
640 if (ecdh == NULL) {
|
|
641 ngx_ssl_error(NGX_LOG_EMERG, ssl->log, 0,
|
|
642 "Unable to create curve \"%s\"", name->data);
|
|
643 return NGX_ERROR;
|
|
644 }
|
|
645
|
|
646 SSL_CTX_set_tmp_ecdh(ssl->ctx, ecdh);
|
|
647
|
|
648 SSL_CTX_set_options(ssl->ctx, SSL_OP_SINGLE_ECDH_USE);
|
|
649
|
|
650 EC_KEY_free(ecdh);
|
|
651 #endif
|
|
652 #endif
|
|
653
|
|
654 return NGX_OK;
|
|
655 }
|
380
|
656
|
666
|
657
|
380
|
658 ngx_int_t
|
96
|
659 ngx_ssl_create_connection(ngx_ssl_t *ssl, ngx_connection_t *c, ngx_uint_t flags)
|
126
|
660 {
|
96
|
661 ngx_ssl_connection_t *sc;
|
0
|
662
|
96
|
663 sc = ngx_pcalloc(c->pool, sizeof(ngx_ssl_connection_t));
|
|
664 if (sc == NULL) {
|
0
|
665 return NGX_ERROR;
|
|
666 }
|
|
667
|
358
|
668 sc->buffer = ((flags & NGX_SSL_BUFFER) != 0);
|
0
|
669
|
96
|
670 sc->connection = SSL_new(ssl->ctx);
|
0
|
671
|
96
|
672 if (sc->connection == NULL) {
|
0
|
673 ngx_ssl_error(NGX_LOG_ALERT, c->log, 0, "SSL_new() failed");
|
|
674 return NGX_ERROR;
|
|
675 }
|
|
676
|
96
|
677 if (SSL_set_fd(sc->connection, c->fd) == 0) {
|
0
|
678 ngx_ssl_error(NGX_LOG_ALERT, c->log, 0, "SSL_set_fd() failed");
|
|
679 return NGX_ERROR;
|
|
680 }
|
|
681
|
126
|
682 if (flags & NGX_SSL_CLIENT) {
|
|
683 SSL_set_connect_state(sc->connection);
|
|
684
|
|
685 } else {
|
|
686 SSL_set_accept_state(sc->connection);
|
|
687 }
|
0
|
688
|
272
|
689 if (SSL_set_ex_data(sc->connection, ngx_ssl_connection_index, c) == 0) {
|
220
|
690 ngx_ssl_error(NGX_LOG_ALERT, c->log, 0, "SSL_set_ex_data() failed");
|
|
691 return NGX_ERROR;
|
|
692 }
|
|
693
|
96
|
694 c->ssl = sc;
|
0
|
695
|
|
696 return NGX_OK;
|
|
697 }
|
|
698
|
|
699
|
96
|
700 ngx_int_t
|
126
|
701 ngx_ssl_set_session(ngx_connection_t *c, ngx_ssl_session_t *session)
|
|
702 {
|
|
703 if (session) {
|
|
704 if (SSL_set_session(c->ssl->connection, session) == 0) {
|
|
705 ngx_ssl_error(NGX_LOG_ALERT, c->log, 0, "SSL_set_session() failed");
|
|
706 return NGX_ERROR;
|
|
707 }
|
|
708 }
|
|
709
|
|
710 return NGX_OK;
|
|
711 }
|
|
712
|
|
713
|
|
714 ngx_int_t
|
96
|
715 ngx_ssl_handshake(ngx_connection_t *c)
|
|
716 {
|
|
717 int n, sslerr;
|
|
718 ngx_err_t err;
|
|
719
|
358
|
720 ngx_ssl_clear_error(c->log);
|
|
721
|
96
|
722 n = SSL_do_handshake(c->ssl->connection);
|
|
723
|
126
|
724 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, c->log, 0, "SSL_do_handshake: %d", n);
|
96
|
725
|
|
726 if (n == 1) {
|
|
727
|
430
|
728 if (ngx_handle_read_event(c->read, 0) != NGX_OK) {
|
96
|
729 return NGX_ERROR;
|
|
730 }
|
|
731
|
430
|
732 if (ngx_handle_write_event(c->write, 0) != NGX_OK) {
|
96
|
733 return NGX_ERROR;
|
|
734 }
|
|
735
|
|
736 #if (NGX_DEBUG)
|
|
737 {
|
|
738 char buf[129], *s, *d;
|
612
|
739 #if OPENSSL_VERSION_NUMBER >= 0x10000000L
|
566
|
740 const
|
|
741 #endif
|
96
|
742 SSL_CIPHER *cipher;
|
|
743
|
|
744 cipher = SSL_get_current_cipher(c->ssl->connection);
|
|
745
|
|
746 if (cipher) {
|
|
747 SSL_CIPHER_description(cipher, &buf[1], 128);
|
|
748
|
|
749 for (s = &buf[1], d = buf; *s; s++) {
|
|
750 if (*s == ' ' && *d == ' ') {
|
|
751 continue;
|
|
752 }
|
|
753
|
|
754 if (*s == LF || *s == CR) {
|
|
755 continue;
|
|
756 }
|
|
757
|
|
758 *++d = *s;
|
|
759 }
|
|
760
|
|
761 if (*d != ' ') {
|
|
762 d++;
|
|
763 }
|
|
764
|
|
765 *d = '\0';
|
|
766
|
132
|
767 ngx_log_debug2(NGX_LOG_DEBUG_EVENT, c->log, 0,
|
96
|
768 "SSL: %s, cipher: \"%s\"",
|
126
|
769 SSL_get_version(c->ssl->connection), &buf[1]);
|
96
|
770
|
|
771 if (SSL_session_reused(c->ssl->connection)) {
|
132
|
772 ngx_log_debug0(NGX_LOG_DEBUG_EVENT, c->log, 0,
|
96
|
773 "SSL reused session");
|
|
774 }
|
|
775
|
|
776 } else {
|
|
777 ngx_log_debug0(NGX_LOG_DEBUG_EVENT, c->log, 0,
|
126
|
778 "SSL no shared ciphers");
|
96
|
779 }
|
|
780 }
|
|
781 #endif
|
|
782
|
|
783 c->ssl->handshaked = 1;
|
|
784
|
|
785 c->recv = ngx_ssl_recv;
|
|
786 c->send = ngx_ssl_write;
|
126
|
787 c->recv_chain = ngx_ssl_recv_chain;
|
|
788 c->send_chain = ngx_ssl_send_chain;
|
96
|
789
|
542
|
790 /* initial handshake done, disable renegotiation (CVE-2009-3555) */
|
|
791 if (c->ssl->connection->s3) {
|
|
792 c->ssl->connection->s3->flags |= SSL3_FLAGS_NO_RENEGOTIATE_CIPHERS;
|
|
793 }
|
|
794
|
96
|
795 return NGX_OK;
|
|
796 }
|
|
797
|
|
798 sslerr = SSL_get_error(c->ssl->connection, n);
|
|
799
|
|
800 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, c->log, 0, "SSL_get_error: %d", sslerr);
|
|
801
|
|
802 if (sslerr == SSL_ERROR_WANT_READ) {
|
|
803 c->read->ready = 0;
|
|
804 c->read->handler = ngx_ssl_handshake_handler;
|
140
|
805 c->write->handler = ngx_ssl_handshake_handler;
|
96
|
806
|
430
|
807 if (ngx_handle_read_event(c->read, 0) != NGX_OK) {
|
96
|
808 return NGX_ERROR;
|
|
809 }
|
|
810
|
|
811 return NGX_AGAIN;
|
|
812 }
|
|
813
|
|
814 if (sslerr == SSL_ERROR_WANT_WRITE) {
|
|
815 c->write->ready = 0;
|
140
|
816 c->read->handler = ngx_ssl_handshake_handler;
|
96
|
817 c->write->handler = ngx_ssl_handshake_handler;
|
|
818
|
430
|
819 if (ngx_handle_write_event(c->write, 0) != NGX_OK) {
|
96
|
820 return NGX_ERROR;
|
|
821 }
|
|
822
|
|
823 return NGX_AGAIN;
|
|
824 }
|
|
825
|
|
826 err = (sslerr == SSL_ERROR_SYSCALL) ? ngx_errno : 0;
|
|
827
|
|
828 c->ssl->no_wait_shutdown = 1;
|
|
829 c->ssl->no_send_shutdown = 1;
|
140
|
830 c->read->eof = 1;
|
96
|
831
|
|
832 if (sslerr == SSL_ERROR_ZERO_RETURN || ERR_peek_error() == 0) {
|
|
833 ngx_log_error(NGX_LOG_INFO, c->log, err,
|
126
|
834 "peer closed connection in SSL handshake");
|
96
|
835
|
|
836 return NGX_ERROR;
|
|
837 }
|
|
838
|
140
|
839 c->read->error = 1;
|
|
840
|
96
|
841 ngx_ssl_connection_error(c, sslerr, err, "SSL_do_handshake() failed");
|
|
842
|
|
843 return NGX_ERROR;
|
|
844 }
|
|
845
|
|
846
|
|
847 static void
|
|
848 ngx_ssl_handshake_handler(ngx_event_t *ev)
|
|
849 {
|
|
850 ngx_connection_t *c;
|
|
851
|
|
852 c = ev->data;
|
|
853
|
98
|
854 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, c->log, 0,
|
126
|
855 "SSL handshake handler: %d", ev->write);
|
96
|
856
|
140
|
857 if (ev->timedout) {
|
|
858 c->ssl->handler(c);
|
|
859 return;
|
|
860 }
|
|
861
|
96
|
862 if (ngx_ssl_handshake(c) == NGX_AGAIN) {
|
|
863 return;
|
|
864 }
|
|
865
|
|
866 c->ssl->handler(c);
|
|
867 }
|
|
868
|
|
869
|
38
|
870 ssize_t
|
126
|
871 ngx_ssl_recv_chain(ngx_connection_t *c, ngx_chain_t *cl)
|
|
872 {
|
294
|
873 u_char *last;
|
126
|
874 ssize_t n, bytes;
|
|
875 ngx_buf_t *b;
|
|
876
|
|
877 bytes = 0;
|
|
878
|
294
|
879 b = cl->buf;
|
|
880 last = b->last;
|
|
881
|
|
882 for ( ;; ) {
|
|
883
|
|
884 n = ngx_ssl_recv(c, last, b->end - last);
|
126
|
885
|
|
886 if (n > 0) {
|
294
|
887 last += n;
|
126
|
888 bytes += n;
|
|
889
|
294
|
890 if (last == b->end) {
|
126
|
891 cl = cl->next;
|
294
|
892
|
|
893 if (cl == NULL) {
|
|
894 return bytes;
|
|
895 }
|
|
896
|
|
897 b = cl->buf;
|
|
898 last = b->last;
|
126
|
899 }
|
|
900
|
|
901 continue;
|
|
902 }
|
|
903
|
|
904 if (bytes) {
|
382
|
905
|
|
906 if (n == 0 || n == NGX_ERROR) {
|
|
907 c->read->ready = 1;
|
|
908 }
|
|
909
|
126
|
910 return bytes;
|
|
911 }
|
|
912
|
|
913 return n;
|
|
914 }
|
|
915 }
|
|
916
|
|
917
|
|
918 ssize_t
|
38
|
919 ngx_ssl_recv(ngx_connection_t *c, u_char *buf, size_t size)
|
0
|
920 {
|
38
|
921 int n, bytes;
|
|
922
|
|
923 if (c->ssl->last == NGX_ERROR) {
|
330
|
924 c->read->error = 1;
|
38
|
925 return NGX_ERROR;
|
|
926 }
|
|
927
|
126
|
928 if (c->ssl->last == NGX_DONE) {
|
330
|
929 c->read->ready = 0;
|
|
930 c->read->eof = 1;
|
126
|
931 return 0;
|
|
932 }
|
|
933
|
38
|
934 bytes = 0;
|
0
|
935
|
358
|
936 ngx_ssl_clear_error(c->log);
|
|
937
|
38
|
938 /*
|
|
939 * SSL_read() may return data in parts, so try to read
|
|
940 * until SSL_read() would return no data
|
|
941 */
|
|
942
|
|
943 for ( ;; ) {
|
0
|
944
|
92
|
945 n = SSL_read(c->ssl->connection, buf, size);
|
38
|
946
|
126
|
947 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, c->log, 0, "SSL_read: %d", n);
|
0
|
948
|
38
|
949 if (n > 0) {
|
|
950 bytes += n;
|
|
951 }
|
|
952
|
|
953 c->ssl->last = ngx_ssl_handle_recv(c, n);
|
|
954
|
330
|
955 if (c->ssl->last == NGX_OK) {
|
|
956
|
|
957 size -= n;
|
|
958
|
|
959 if (size == 0) {
|
38
|
960 return bytes;
|
126
|
961 }
|
38
|
962
|
330
|
963 buf += n;
|
|
964
|
|
965 continue;
|
|
966 }
|
|
967
|
|
968 if (bytes) {
|
|
969 return bytes;
|
|
970 }
|
|
971
|
|
972 switch (c->ssl->last) {
|
|
973
|
|
974 case NGX_DONE:
|
|
975 c->read->ready = 0;
|
|
976 c->read->eof = 1;
|
|
977 return 0;
|
|
978
|
|
979 case NGX_ERROR:
|
|
980 c->read->error = 1;
|
|
981
|
664
|
982 /* fall through */
|
330
|
983
|
|
984 case NGX_AGAIN:
|
126
|
985 return c->ssl->last;
|
28
|
986 }
|
38
|
987 }
|
|
988 }
|
|
989
|
|
990
|
|
991 static ngx_int_t
|
|
992 ngx_ssl_handle_recv(ngx_connection_t *c, int n)
|
|
993 {
|
96
|
994 int sslerr;
|
|
995 ngx_err_t err;
|
38
|
996
|
542
|
997 if (c->ssl->renegotiation) {
|
|
998 /*
|
|
999 * disable renegotiation (CVE-2009-3555):
|
|
1000 * OpenSSL (at least up to 0.9.8l) does not handle disabled
|
|
1001 * renegotiation gracefully, so drop connection here
|
|
1002 */
|
|
1003
|
|
1004 ngx_log_error(NGX_LOG_NOTICE, c->log, 0, "SSL renegotiation disabled");
|
|
1005
|
646
|
1006 while (ERR_peek_error()) {
|
|
1007 ngx_ssl_error(NGX_LOG_DEBUG, c->log, 0,
|
|
1008 "ignoring stale global SSL error");
|
|
1009 }
|
|
1010
|
|
1011 ERR_clear_error();
|
|
1012
|
542
|
1013 c->ssl->no_wait_shutdown = 1;
|
|
1014 c->ssl->no_send_shutdown = 1;
|
|
1015
|
|
1016 return NGX_ERROR;
|
|
1017 }
|
|
1018
|
38
|
1019 if (n > 0) {
|
28
|
1020
|
22
|
1021 if (c->ssl->saved_write_handler) {
|
|
1022
|
58
|
1023 c->write->handler = c->ssl->saved_write_handler;
|
22
|
1024 c->ssl->saved_write_handler = NULL;
|
|
1025 c->write->ready = 1;
|
|
1026
|
430
|
1027 if (ngx_handle_write_event(c->write, 0) != NGX_OK) {
|
22
|
1028 return NGX_ERROR;
|
|
1029 }
|
|
1030
|
112
|
1031 ngx_post_event(c->write, &ngx_posted_events);
|
22
|
1032 }
|
|
1033
|
38
|
1034 return NGX_OK;
|
0
|
1035 }
|
|
1036
|
92
|
1037 sslerr = SSL_get_error(c->ssl->connection, n);
|
0
|
1038
|
|
1039 err = (sslerr == SSL_ERROR_SYSCALL) ? ngx_errno : 0;
|
|
1040
|
|
1041 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, c->log, 0, "SSL_get_error: %d", sslerr);
|
|
1042
|
|
1043 if (sslerr == SSL_ERROR_WANT_READ) {
|
4
|
1044 c->read->ready = 0;
|
0
|
1045 return NGX_AGAIN;
|
|
1046 }
|
|
1047
|
|
1048 if (sslerr == SSL_ERROR_WANT_WRITE) {
|
88
|
1049
|
96
|
1050 ngx_log_error(NGX_LOG_INFO, c->log, 0,
|
126
|
1051 "peer started SSL renegotiation");
|
22
|
1052
|
|
1053 c->write->ready = 0;
|
|
1054
|
430
|
1055 if (ngx_handle_write_event(c->write, 0) != NGX_OK) {
|
22
|
1056 return NGX_ERROR;
|
|
1057 }
|
|
1058
|
|
1059 /*
|
|
1060 * we do not set the timer because there is already the read event timer
|
|
1061 */
|
|
1062
|
|
1063 if (c->ssl->saved_write_handler == NULL) {
|
58
|
1064 c->ssl->saved_write_handler = c->write->handler;
|
|
1065 c->write->handler = ngx_ssl_write_handler;
|
22
|
1066 }
|
|
1067
|
0
|
1068 return NGX_AGAIN;
|
|
1069 }
|
|
1070
|
96
|
1071 c->ssl->no_wait_shutdown = 1;
|
|
1072 c->ssl->no_send_shutdown = 1;
|
0
|
1073
|
|
1074 if (sslerr == SSL_ERROR_ZERO_RETURN || ERR_peek_error() == 0) {
|
126
|
1075 ngx_log_debug0(NGX_LOG_DEBUG_EVENT, c->log, 0,
|
|
1076 "peer shutdown SSL cleanly");
|
|
1077 return NGX_DONE;
|
0
|
1078 }
|
|
1079
|
96
|
1080 ngx_ssl_connection_error(c, sslerr, err, "SSL_read() failed");
|
0
|
1081
|
|
1082 return NGX_ERROR;
|
|
1083 }
|
|
1084
|
|
1085
|
38
|
1086 static void
|
|
1087 ngx_ssl_write_handler(ngx_event_t *wev)
|
22
|
1088 {
|
|
1089 ngx_connection_t *c;
|
|
1090
|
|
1091 c = wev->data;
|
96
|
1092
|
58
|
1093 c->read->handler(c->read);
|
22
|
1094 }
|
|
1095
|
|
1096
|
0
|
1097 /*
|
|
1098 * OpenSSL has no SSL_writev() so we copy several bufs into our 16K buffer
|
22
|
1099 * before the SSL_write() call to decrease a SSL overhead.
|
0
|
1100 *
|
|
1101 * Besides for protocols such as HTTP it is possible to always buffer
|
|
1102 * the output to decrease a SSL overhead some more.
|
|
1103 */
|
|
1104
|
38
|
1105 ngx_chain_t *
|
|
1106 ngx_ssl_send_chain(ngx_connection_t *c, ngx_chain_t *in, off_t limit)
|
0
|
1107 {
|
|
1108 int n;
|
|
1109 ngx_uint_t flush;
|
|
1110 ssize_t send, size;
|
|
1111 ngx_buf_t *buf;
|
|
1112
|
416
|
1113 if (!c->ssl->buffer) {
|
0
|
1114
|
126
|
1115 while (in) {
|
|
1116 if (ngx_buf_special(in->buf)) {
|
|
1117 in = in->next;
|
|
1118 continue;
|
|
1119 }
|
0
|
1120
|
126
|
1121 n = ngx_ssl_write(c, in->buf->pos, in->buf->last - in->buf->pos);
|
|
1122
|
|
1123 if (n == NGX_ERROR) {
|
|
1124 return NGX_CHAIN_ERROR;
|
|
1125 }
|
0
|
1126
|
126
|
1127 if (n == NGX_AGAIN) {
|
|
1128 return in;
|
|
1129 }
|
|
1130
|
|
1131 in->buf->pos += n;
|
674
|
1132 c->sent += n;
|
126
|
1133
|
|
1134 if (in->buf->pos == in->buf->last) {
|
|
1135 in = in->next;
|
|
1136 }
|
0
|
1137 }
|
|
1138
|
|
1139 return in;
|
|
1140 }
|
|
1141
|
22
|
1142
|
632
|
1143 /* the maximum limit size is the maximum int32_t value - the page size */
|
|
1144
|
|
1145 if (limit == 0 || limit > (off_t) (NGX_MAX_INT32_VALUE - ngx_pagesize)) {
|
|
1146 limit = NGX_MAX_INT32_VALUE - ngx_pagesize;
|
22
|
1147 }
|
|
1148
|
126
|
1149 buf = c->ssl->buf;
|
358
|
1150
|
|
1151 if (buf == NULL) {
|
|
1152 buf = ngx_create_temp_buf(c->pool, NGX_SSL_BUFSIZE);
|
|
1153 if (buf == NULL) {
|
|
1154 return NGX_CHAIN_ERROR;
|
|
1155 }
|
|
1156
|
|
1157 c->ssl->buf = buf;
|
|
1158 }
|
|
1159
|
|
1160 if (buf->start == NULL) {
|
|
1161 buf->start = ngx_palloc(c->pool, NGX_SSL_BUFSIZE);
|
|
1162 if (buf->start == NULL) {
|
|
1163 return NGX_CHAIN_ERROR;
|
|
1164 }
|
|
1165
|
|
1166 buf->pos = buf->start;
|
|
1167 buf->last = buf->start;
|
|
1168 buf->end = buf->start + NGX_SSL_BUFSIZE;
|
|
1169 }
|
|
1170
|
0
|
1171 send = 0;
|
|
1172 flush = (in == NULL) ? 1 : 0;
|
|
1173
|
|
1174 for ( ;; ) {
|
|
1175
|
540
|
1176 while (in && buf->last < buf->end && send < limit) {
|
132
|
1177 if (in->buf->last_buf || in->buf->flush) {
|
0
|
1178 flush = 1;
|
|
1179 }
|
|
1180
|
|
1181 if (ngx_buf_special(in->buf)) {
|
|
1182 in = in->next;
|
|
1183 continue;
|
|
1184 }
|
|
1185
|
|
1186 size = in->buf->last - in->buf->pos;
|
|
1187
|
|
1188 if (size > buf->end - buf->last) {
|
|
1189 size = buf->end - buf->last;
|
|
1190 }
|
|
1191
|
|
1192 if (send + size > limit) {
|
126
|
1193 size = (ssize_t) (limit - send);
|
0
|
1194 flush = 1;
|
|
1195 }
|
|
1196
|
|
1197 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, c->log, 0,
|
|
1198 "SSL buf copy: %d", size);
|
|
1199
|
|
1200 ngx_memcpy(buf->last, in->buf->pos, size);
|
|
1201
|
|
1202 buf->last += size;
|
|
1203 in->buf->pos += size;
|
540
|
1204 send += size;
|
126
|
1205
|
0
|
1206 if (in->buf->pos == in->buf->last) {
|
|
1207 in = in->next;
|
|
1208 }
|
|
1209 }
|
|
1210
|
|
1211 size = buf->last - buf->pos;
|
|
1212
|
|
1213 if (!flush && buf->last < buf->end && c->ssl->buffer) {
|
|
1214 break;
|
|
1215 }
|
|
1216
|
|
1217 n = ngx_ssl_write(c, buf->pos, size);
|
|
1218
|
|
1219 if (n == NGX_ERROR) {
|
|
1220 return NGX_CHAIN_ERROR;
|
|
1221 }
|
|
1222
|
60
|
1223 if (n == NGX_AGAIN) {
|
146
|
1224 c->buffered |= NGX_SSL_BUFFERED;
|
60
|
1225 return in;
|
0
|
1226 }
|
|
1227
|
|
1228 buf->pos += n;
|
|
1229 c->sent += n;
|
|
1230
|
|
1231 if (n < size) {
|
|
1232 break;
|
|
1233 }
|
|
1234
|
|
1235 if (buf->pos == buf->last) {
|
|
1236 buf->pos = buf->start;
|
|
1237 buf->last = buf->start;
|
|
1238 }
|
|
1239
|
|
1240 if (in == NULL || send == limit) {
|
|
1241 break;
|
|
1242 }
|
|
1243 }
|
|
1244
|
146
|
1245 if (buf->pos < buf->last) {
|
|
1246 c->buffered |= NGX_SSL_BUFFERED;
|
|
1247
|
|
1248 } else {
|
|
1249 c->buffered &= ~NGX_SSL_BUFFERED;
|
|
1250 }
|
0
|
1251
|
|
1252 return in;
|
|
1253 }
|
|
1254
|
|
1255
|
88
|
1256 ssize_t
|
38
|
1257 ngx_ssl_write(ngx_connection_t *c, u_char *data, size_t size)
|
0
|
1258 {
|
96
|
1259 int n, sslerr;
|
|
1260 ngx_err_t err;
|
0
|
1261
|
358
|
1262 ngx_ssl_clear_error(c->log);
|
|
1263
|
0
|
1264 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, c->log, 0, "SSL to write: %d", size);
|
|
1265
|
92
|
1266 n = SSL_write(c->ssl->connection, data, size);
|
0
|
1267
|
|
1268 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, c->log, 0, "SSL_write: %d", n);
|
|
1269
|
|
1270 if (n > 0) {
|
88
|
1271
|
22
|
1272 if (c->ssl->saved_read_handler) {
|
|
1273
|
58
|
1274 c->read->handler = c->ssl->saved_read_handler;
|
22
|
1275 c->ssl->saved_read_handler = NULL;
|
|
1276 c->read->ready = 1;
|
|
1277
|
430
|
1278 if (ngx_handle_read_event(c->read, 0) != NGX_OK) {
|
22
|
1279 return NGX_ERROR;
|
|
1280 }
|
|
1281
|
112
|
1282 ngx_post_event(c->read, &ngx_posted_events);
|
22
|
1283 }
|
|
1284
|
0
|
1285 return n;
|
|
1286 }
|
|
1287
|
92
|
1288 sslerr = SSL_get_error(c->ssl->connection, n);
|
0
|
1289
|
|
1290 err = (sslerr == SSL_ERROR_SYSCALL) ? ngx_errno : 0;
|
|
1291
|
|
1292 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, c->log, 0, "SSL_get_error: %d", sslerr);
|
|
1293
|
|
1294 if (sslerr == SSL_ERROR_WANT_WRITE) {
|
|
1295 c->write->ready = 0;
|
|
1296 return NGX_AGAIN;
|
|
1297 }
|
|
1298
|
|
1299 if (sslerr == SSL_ERROR_WANT_READ) {
|
2
|
1300
|
96
|
1301 ngx_log_error(NGX_LOG_INFO, c->log, 0,
|
126
|
1302 "peer started SSL renegotiation");
|
22
|
1303
|
|
1304 c->read->ready = 0;
|
|
1305
|
430
|
1306 if (ngx_handle_read_event(c->read, 0) != NGX_OK) {
|
22
|
1307 return NGX_ERROR;
|
|
1308 }
|
|
1309
|
|
1310 /*
|
|
1311 * we do not set the timer because there is already
|
|
1312 * the write event timer
|
|
1313 */
|
|
1314
|
|
1315 if (c->ssl->saved_read_handler == NULL) {
|
58
|
1316 c->ssl->saved_read_handler = c->read->handler;
|
|
1317 c->read->handler = ngx_ssl_read_handler;
|
22
|
1318 }
|
|
1319
|
0
|
1320 return NGX_AGAIN;
|
|
1321 }
|
|
1322
|
96
|
1323 c->ssl->no_wait_shutdown = 1;
|
|
1324 c->ssl->no_send_shutdown = 1;
|
140
|
1325 c->write->error = 1;
|
92
|
1326
|
96
|
1327 ngx_ssl_connection_error(c, sslerr, err, "SSL_write() failed");
|
0
|
1328
|
|
1329 return NGX_ERROR;
|
|
1330 }
|
|
1331
|
|
1332
|
38
|
1333 static void
|
|
1334 ngx_ssl_read_handler(ngx_event_t *rev)
|
22
|
1335 {
|
|
1336 ngx_connection_t *c;
|
|
1337
|
|
1338 c = rev->data;
|
96
|
1339
|
58
|
1340 c->write->handler(c->write);
|
22
|
1341 }
|
|
1342
|
|
1343
|
358
|
1344 void
|
|
1345 ngx_ssl_free_buffer(ngx_connection_t *c)
|
|
1346 {
|
360
|
1347 if (c->ssl->buf && c->ssl->buf->start) {
|
|
1348 if (ngx_pfree(c->pool, c->ssl->buf->start) == NGX_OK) {
|
|
1349 c->ssl->buf->start = NULL;
|
|
1350 }
|
358
|
1351 }
|
|
1352 }
|
|
1353
|
|
1354
|
38
|
1355 ngx_int_t
|
|
1356 ngx_ssl_shutdown(ngx_connection_t *c)
|
0
|
1357 {
|
358
|
1358 int n, sslerr, mode;
|
|
1359 ngx_err_t err;
|
0
|
1360
|
126
|
1361 if (c->timedout) {
|
96
|
1362 mode = SSL_RECEIVED_SHUTDOWN|SSL_SENT_SHUTDOWN;
|
636
|
1363 SSL_set_quiet_shutdown(c->ssl->connection, 1);
|
0
|
1364
|
96
|
1365 } else {
|
|
1366 mode = SSL_get_shutdown(c->ssl->connection);
|
22
|
1367
|
96
|
1368 if (c->ssl->no_wait_shutdown) {
|
|
1369 mode |= SSL_RECEIVED_SHUTDOWN;
|
0
|
1370 }
|
|
1371
|
96
|
1372 if (c->ssl->no_send_shutdown) {
|
|
1373 mode |= SSL_SENT_SHUTDOWN;
|
0
|
1374 }
|
636
|
1375
|
|
1376 if (c->ssl->no_wait_shutdown && c->ssl->no_send_shutdown) {
|
|
1377 SSL_set_quiet_shutdown(c->ssl->connection, 1);
|
|
1378 }
|
0
|
1379 }
|
|
1380
|
96
|
1381 SSL_set_shutdown(c->ssl->connection, mode);
|
|
1382
|
358
|
1383 ngx_ssl_clear_error(c->log);
|
|
1384
|
|
1385 n = SSL_shutdown(c->ssl->connection);
|
|
1386
|
|
1387 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, c->log, 0, "SSL_shutdown: %d", n);
|
|
1388
|
10
|
1389 sslerr = 0;
|
0
|
1390
|
364
|
1391 /* SSL_shutdown() never returns -1, on error it returns 0 */
|
|
1392
|
|
1393 if (n != 1 && ERR_peek_error()) {
|
92
|
1394 sslerr = SSL_get_error(c->ssl->connection, n);
|
0
|
1395
|
|
1396 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, c->log, 0,
|
|
1397 "SSL_get_error: %d", sslerr);
|
|
1398 }
|
|
1399
|
364
|
1400 if (n == 1 || sslerr == 0 || sslerr == SSL_ERROR_ZERO_RETURN) {
|
358
|
1401 SSL_free(c->ssl->connection);
|
|
1402 c->ssl = NULL;
|
|
1403
|
|
1404 return NGX_OK;
|
|
1405 }
|
|
1406
|
|
1407 if (sslerr == SSL_ERROR_WANT_READ || sslerr == SSL_ERROR_WANT_WRITE) {
|
126
|
1408 c->read->handler = ngx_ssl_shutdown_handler;
|
138
|
1409 c->write->handler = ngx_ssl_shutdown_handler;
|
126
|
1410
|
430
|
1411 if (ngx_handle_read_event(c->read, 0) != NGX_OK) {
|
0
|
1412 return NGX_ERROR;
|
|
1413 }
|
|
1414
|
430
|
1415 if (ngx_handle_write_event(c->write, 0) != NGX_OK) {
|
0
|
1416 return NGX_ERROR;
|
|
1417 }
|
|
1418
|
358
|
1419 if (sslerr == SSL_ERROR_WANT_READ) {
|
138
|
1420 ngx_add_timer(c->read, 30000);
|
|
1421 }
|
|
1422
|
0
|
1423 return NGX_AGAIN;
|
|
1424 }
|
|
1425
|
140
|
1426 err = (sslerr == SSL_ERROR_SYSCALL) ? ngx_errno : 0;
|
|
1427
|
|
1428 ngx_ssl_connection_error(c, sslerr, err, "SSL_shutdown() failed");
|
0
|
1429
|
92
|
1430 SSL_free(c->ssl->connection);
|
|
1431 c->ssl = NULL;
|
|
1432
|
0
|
1433 return NGX_ERROR;
|
|
1434 }
|
|
1435
|
|
1436
|
96
|
1437 static void
|
126
|
1438 ngx_ssl_shutdown_handler(ngx_event_t *ev)
|
|
1439 {
|
|
1440 ngx_connection_t *c;
|
|
1441 ngx_connection_handler_pt handler;
|
|
1442
|
|
1443 c = ev->data;
|
|
1444 handler = c->ssl->handler;
|
|
1445
|
|
1446 if (ev->timedout) {
|
|
1447 c->timedout = 1;
|
|
1448 }
|
|
1449
|
272
|
1450 ngx_log_debug0(NGX_LOG_DEBUG_EVENT, ev->log, 0, "SSL shutdown handler");
|
126
|
1451
|
|
1452 if (ngx_ssl_shutdown(c) == NGX_AGAIN) {
|
|
1453 return;
|
|
1454 }
|
|
1455
|
|
1456 handler(c);
|
|
1457 }
|
|
1458
|
|
1459
|
|
1460 static void
|
96
|
1461 ngx_ssl_connection_error(ngx_connection_t *c, int sslerr, ngx_err_t err,
|
|
1462 char *text)
|
|
1463 {
|
364
|
1464 int n;
|
96
|
1465 ngx_uint_t level;
|
|
1466
|
|
1467 level = NGX_LOG_CRIT;
|
|
1468
|
|
1469 if (sslerr == SSL_ERROR_SYSCALL) {
|
|
1470
|
|
1471 if (err == NGX_ECONNRESET
|
|
1472 || err == NGX_EPIPE
|
|
1473 || err == NGX_ENOTCONN
|
138
|
1474 || err == NGX_ETIMEDOUT
|
96
|
1475 || err == NGX_ECONNREFUSED
|
364
|
1476 || err == NGX_ENETDOWN
|
|
1477 || err == NGX_ENETUNREACH
|
|
1478 || err == NGX_EHOSTDOWN
|
96
|
1479 || err == NGX_EHOSTUNREACH)
|
|
1480 {
|
|
1481 switch (c->log_error) {
|
|
1482
|
|
1483 case NGX_ERROR_IGNORE_ECONNRESET:
|
|
1484 case NGX_ERROR_INFO:
|
|
1485 level = NGX_LOG_INFO;
|
|
1486 break;
|
|
1487
|
|
1488 case NGX_ERROR_ERR:
|
|
1489 level = NGX_LOG_ERR;
|
|
1490 break;
|
|
1491
|
|
1492 default:
|
|
1493 break;
|
|
1494 }
|
|
1495 }
|
364
|
1496
|
|
1497 } else if (sslerr == SSL_ERROR_SSL) {
|
|
1498
|
|
1499 n = ERR_GET_REASON(ERR_peek_error());
|
|
1500
|
|
1501 /* handshake failures */
|
646
|
1502 if (n == SSL_R_BAD_CHANGE_CIPHER_SPEC /* 103 */
|
|
1503 || n == SSL_R_BLOCK_CIPHER_PAD_IS_WRONG /* 129 */
|
592
|
1504 || n == SSL_R_DIGEST_CHECK_FAILED /* 149 */
|
646
|
1505 || n == SSL_R_ERROR_IN_RECEIVED_CIPHER_LIST /* 151 */
|
|
1506 || n == SSL_R_EXCESSIVE_MESSAGE_SIZE /* 152 */
|
564
|
1507 || n == SSL_R_LENGTH_MISMATCH /* 159 */
|
420
|
1508 || n == SSL_R_NO_CIPHERS_PASSED /* 182 */
|
564
|
1509 || n == SSL_R_NO_CIPHERS_SPECIFIED /* 183 */
|
646
|
1510 || n == SSL_R_NO_COMPRESSION_SPECIFIED /* 187 */
|
420
|
1511 || n == SSL_R_NO_SHARED_CIPHER /* 193 */
|
564
|
1512 || n == SSL_R_RECORD_LENGTH_MISMATCH /* 213 */
|
646
|
1513 #ifdef SSL_R_PARSE_TLSEXT
|
|
1514 || n == SSL_R_PARSE_TLSEXT /* 227 */
|
|
1515 #endif
|
420
|
1516 || n == SSL_R_UNEXPECTED_MESSAGE /* 244 */
|
|
1517 || n == SSL_R_UNEXPECTED_RECORD /* 245 */
|
564
|
1518 || n == SSL_R_UNKNOWN_ALERT_TYPE /* 246 */
|
552
|
1519 || n == SSL_R_UNKNOWN_PROTOCOL /* 252 */
|
420
|
1520 || n == SSL_R_WRONG_VERSION_NUMBER /* 267 */
|
|
1521 || n == SSL_R_DECRYPTION_FAILED_OR_BAD_RECORD_MAC /* 281 */
|
646
|
1522 #ifdef SSL_R_RENEGOTIATE_EXT_TOO_LONG
|
|
1523 || n == SSL_R_RENEGOTIATE_EXT_TOO_LONG /* 335 */
|
|
1524 || n == SSL_R_RENEGOTIATION_ENCODING_ERR /* 336 */
|
|
1525 || n == SSL_R_RENEGOTIATION_MISMATCH /* 337 */
|
|
1526 #endif
|
|
1527 #ifdef SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED
|
|
1528 || n == SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED /* 338 */
|
|
1529 #endif
|
|
1530 #ifdef SSL_R_SCSV_RECEIVED_WHEN_RENEGOTIATING
|
|
1531 || n == SSL_R_SCSV_RECEIVED_WHEN_RENEGOTIATING /* 345 */
|
|
1532 #endif
|
364
|
1533 || n == 1000 /* SSL_R_SSLV3_ALERT_CLOSE_NOTIFY */
|
420
|
1534 || n == SSL_R_SSLV3_ALERT_UNEXPECTED_MESSAGE /* 1010 */
|
|
1535 || n == SSL_R_SSLV3_ALERT_BAD_RECORD_MAC /* 1020 */
|
|
1536 || n == SSL_R_TLSV1_ALERT_DECRYPTION_FAILED /* 1021 */
|
|
1537 || n == SSL_R_TLSV1_ALERT_RECORD_OVERFLOW /* 1022 */
|
|
1538 || n == SSL_R_SSLV3_ALERT_DECOMPRESSION_FAILURE /* 1030 */
|
|
1539 || n == SSL_R_SSLV3_ALERT_HANDSHAKE_FAILURE /* 1040 */
|
|
1540 || n == SSL_R_SSLV3_ALERT_NO_CERTIFICATE /* 1041 */
|
|
1541 || n == SSL_R_SSLV3_ALERT_BAD_CERTIFICATE /* 1042 */
|
|
1542 || n == SSL_R_SSLV3_ALERT_UNSUPPORTED_CERTIFICATE /* 1043 */
|
|
1543 || n == SSL_R_SSLV3_ALERT_CERTIFICATE_REVOKED /* 1044 */
|
|
1544 || n == SSL_R_SSLV3_ALERT_CERTIFICATE_EXPIRED /* 1045 */
|
|
1545 || n == SSL_R_SSLV3_ALERT_CERTIFICATE_UNKNOWN /* 1046 */
|
|
1546 || n == SSL_R_SSLV3_ALERT_ILLEGAL_PARAMETER /* 1047 */
|
|
1547 || n == SSL_R_TLSV1_ALERT_UNKNOWN_CA /* 1048 */
|
|
1548 || n == SSL_R_TLSV1_ALERT_ACCESS_DENIED /* 1049 */
|
|
1549 || n == SSL_R_TLSV1_ALERT_DECODE_ERROR /* 1050 */
|
|
1550 || n == SSL_R_TLSV1_ALERT_DECRYPT_ERROR /* 1051 */
|
|
1551 || n == SSL_R_TLSV1_ALERT_EXPORT_RESTRICTION /* 1060 */
|
|
1552 || n == SSL_R_TLSV1_ALERT_PROTOCOL_VERSION /* 1070 */
|
|
1553 || n == SSL_R_TLSV1_ALERT_INSUFFICIENT_SECURITY /* 1071 */
|
|
1554 || n == SSL_R_TLSV1_ALERT_INTERNAL_ERROR /* 1080 */
|
|
1555 || n == SSL_R_TLSV1_ALERT_USER_CANCELLED /* 1090 */
|
|
1556 || n == SSL_R_TLSV1_ALERT_NO_RENEGOTIATION) /* 1100 */
|
364
|
1557 {
|
|
1558 switch (c->log_error) {
|
|
1559
|
|
1560 case NGX_ERROR_IGNORE_ECONNRESET:
|
|
1561 case NGX_ERROR_INFO:
|
|
1562 level = NGX_LOG_INFO;
|
|
1563 break;
|
|
1564
|
|
1565 case NGX_ERROR_ERR:
|
|
1566 level = NGX_LOG_ERR;
|
|
1567 break;
|
|
1568
|
|
1569 default:
|
|
1570 break;
|
|
1571 }
|
|
1572 }
|
96
|
1573 }
|
|
1574
|
|
1575 ngx_ssl_error(level, c->log, err, text);
|
|
1576 }
|
|
1577
|
|
1578
|
358
|
1579 static void
|
|
1580 ngx_ssl_clear_error(ngx_log_t *log)
|
|
1581 {
|
364
|
1582 while (ERR_peek_error()) {
|
358
|
1583 ngx_ssl_error(NGX_LOG_ALERT, log, 0, "ignoring stale global SSL error");
|
|
1584 }
|
364
|
1585
|
|
1586 ERR_clear_error();
|
358
|
1587 }
|
|
1588
|
|
1589
|
132
|
1590 void ngx_cdecl
|
38
|
1591 ngx_ssl_error(ngx_uint_t level, ngx_log_t *log, ngx_err_t err, char *fmt, ...)
|
126
|
1592 {
|
688
|
1593 int flags;
|
|
1594 u_long n;
|
|
1595 va_list args;
|
|
1596 u_char *p, *last;
|
|
1597 u_char errstr[NGX_MAX_CONF_ERRSTR];
|
|
1598 const char *data;
|
10
|
1599
|
|
1600 last = errstr + NGX_MAX_CONF_ERRSTR;
|
0
|
1601
|
|
1602 va_start(args, fmt);
|
484
|
1603 p = ngx_vslprintf(errstr, last - 1, fmt, args);
|
0
|
1604 va_end(args);
|
|
1605
|
96
|
1606 p = ngx_cpystrn(p, (u_char *) " (SSL:", last - p);
|
|
1607
|
364
|
1608 for ( ;; ) {
|
132
|
1609
|
688
|
1610 n = ERR_peek_error_line_data(NULL, NULL, &data, &flags);
|
132
|
1611
|
|
1612 if (n == 0) {
|
|
1613 break;
|
|
1614 }
|
96
|
1615
|
364
|
1616 if (p >= last) {
|
688
|
1617 goto next;
|
364
|
1618 }
|
|
1619
|
96
|
1620 *p++ = ' ';
|
0
|
1621
|
96
|
1622 ERR_error_string_n(n, (char *) p, last - p);
|
|
1623
|
|
1624 while (p < last && *p) {
|
|
1625 p++;
|
|
1626 }
|
688
|
1627
|
|
1628 if (p < last && *data && (flags & ERR_TXT_STRING)) {
|
|
1629 *p++ = ':';
|
|
1630 p = ngx_cpystrn(p, (u_char *) data, last - p);
|
|
1631 }
|
|
1632
|
|
1633 next:
|
|
1634
|
|
1635 (void) ERR_get_error();
|
96
|
1636 }
|
0
|
1637
|
|
1638 ngx_log_error(level, log, err, "%s)", errstr);
|
|
1639 }
|
58
|
1640
|
|
1641
|
272
|
1642 ngx_int_t
|
|
1643 ngx_ssl_session_cache(ngx_ssl_t *ssl, ngx_str_t *sess_ctx,
|
|
1644 ssize_t builtin_session_cache, ngx_shm_zone_t *shm_zone, time_t timeout)
|
|
1645 {
|
|
1646 long cache_mode;
|
|
1647
|
358
|
1648 if (builtin_session_cache == NGX_SSL_NO_SCACHE) {
|
|
1649 SSL_CTX_set_session_cache_mode(ssl->ctx, SSL_SESS_CACHE_OFF);
|
|
1650 return NGX_OK;
|
|
1651 }
|
|
1652
|
564
|
1653 SSL_CTX_set_session_id_context(ssl->ctx, sess_ctx->data, sess_ctx->len);
|
|
1654
|
378
|
1655 if (builtin_session_cache == NGX_SSL_NONE_SCACHE) {
|
|
1656
|
|
1657 /*
|
|
1658 * If the server explicitly says that it does not support
|
|
1659 * session reuse (see SSL_SESS_CACHE_OFF above), then
|
|
1660 * Outlook Express fails to upload a sent email to
|
|
1661 * the Sent Items folder on the IMAP server via a separate IMAP
|
|
1662 * connection in the background. Therefore we have a special
|
|
1663 * mode (SSL_SESS_CACHE_SERVER|SSL_SESS_CACHE_NO_INTERNAL_STORE)
|
|
1664 * where the server pretends that it supports session reuse,
|
|
1665 * but it does not actually store any session.
|
|
1666 */
|
|
1667
|
|
1668 SSL_CTX_set_session_cache_mode(ssl->ctx,
|
|
1669 SSL_SESS_CACHE_SERVER
|
|
1670 |SSL_SESS_CACHE_NO_AUTO_CLEAR
|
|
1671 |SSL_SESS_CACHE_NO_INTERNAL_STORE);
|
|
1672
|
|
1673 SSL_CTX_sess_set_cache_size(ssl->ctx, 1);
|
|
1674
|
|
1675 return NGX_OK;
|
|
1676 }
|
|
1677
|
272
|
1678 cache_mode = SSL_SESS_CACHE_SERVER;
|
|
1679
|
|
1680 if (shm_zone && builtin_session_cache == NGX_SSL_NO_BUILTIN_SCACHE) {
|
|
1681 cache_mode |= SSL_SESS_CACHE_NO_INTERNAL;
|
|
1682 }
|
|
1683
|
|
1684 SSL_CTX_set_session_cache_mode(ssl->ctx, cache_mode);
|
|
1685
|
|
1686 if (builtin_session_cache != NGX_SSL_NO_BUILTIN_SCACHE) {
|
|
1687
|
|
1688 if (builtin_session_cache != NGX_SSL_DFLT_BUILTIN_SCACHE) {
|
|
1689 SSL_CTX_sess_set_cache_size(ssl->ctx, builtin_session_cache);
|
|
1690 }
|
|
1691 }
|
|
1692
|
480
|
1693 SSL_CTX_set_timeout(ssl->ctx, (long) timeout);
|
274
|
1694
|
272
|
1695 if (shm_zone) {
|
|
1696 SSL_CTX_sess_set_new_cb(ssl->ctx, ngx_ssl_new_session);
|
|
1697 SSL_CTX_sess_set_get_cb(ssl->ctx, ngx_ssl_get_cached_session);
|
|
1698 SSL_CTX_sess_set_remove_cb(ssl->ctx, ngx_ssl_remove_session);
|
|
1699
|
|
1700 if (SSL_CTX_set_ex_data(ssl->ctx, ngx_ssl_session_cache_index, shm_zone)
|
|
1701 == 0)
|
|
1702 {
|
|
1703 ngx_ssl_error(NGX_LOG_EMERG, ssl->log, 0,
|
|
1704 "SSL_CTX_set_ex_data() failed");
|
|
1705 return NGX_ERROR;
|
|
1706 }
|
|
1707 }
|
|
1708
|
|
1709 return NGX_OK;
|
|
1710 }
|
|
1711
|
|
1712
|
634
|
1713 ngx_int_t
|
272
|
1714 ngx_ssl_session_cache_init(ngx_shm_zone_t *shm_zone, void *data)
|
|
1715 {
|
468
|
1716 size_t len;
|
272
|
1717 ngx_slab_pool_t *shpool;
|
|
1718 ngx_ssl_session_cache_t *cache;
|
|
1719
|
|
1720 if (data) {
|
|
1721 shm_zone->data = data;
|
|
1722 return NGX_OK;
|
|
1723 }
|
|
1724
|
480
|
1725 if (shm_zone->shm.exists) {
|
|
1726 shm_zone->data = data;
|
|
1727 return NGX_OK;
|
|
1728 }
|
|
1729
|
272
|
1730 shpool = (ngx_slab_pool_t *) shm_zone->shm.addr;
|
|
1731
|
|
1732 cache = ngx_slab_alloc(shpool, sizeof(ngx_ssl_session_cache_t));
|
|
1733 if (cache == NULL) {
|
|
1734 return NGX_ERROR;
|
|
1735 }
|
|
1736
|
480
|
1737 shpool->data = cache;
|
|
1738 shm_zone->data = cache;
|
|
1739
|
358
|
1740 ngx_rbtree_init(&cache->session_rbtree, &cache->sentinel,
|
356
|
1741 ngx_ssl_session_rbtree_insert_value);
|
272
|
1742
|
358
|
1743 ngx_queue_init(&cache->expire_queue);
|
|
1744
|
480
|
1745 len = sizeof(" in SSL session shared cache \"\"") + shm_zone->shm.name.len;
|
468
|
1746
|
|
1747 shpool->log_ctx = ngx_slab_alloc(shpool, len);
|
|
1748 if (shpool->log_ctx == NULL) {
|
|
1749 return NGX_ERROR;
|
|
1750 }
|
|
1751
|
|
1752 ngx_sprintf(shpool->log_ctx, " in SSL session shared cache \"%V\"%Z",
|
480
|
1753 &shm_zone->shm.name);
|
272
|
1754
|
|
1755 return NGX_OK;
|
|
1756 }
|
|
1757
|
|
1758
|
|
1759 /*
|
274
|
1760 * The length of the session id is 16 bytes for SSLv2 sessions and
|
|
1761 * between 1 and 32 bytes for SSLv3/TLSv1, typically 32 bytes.
|
|
1762 * It seems that the typical length of the external ASN1 representation
|
|
1763 * of a session is 118 or 119 bytes for SSLv3/TSLv1.
|
|
1764 *
|
|
1765 * Thus on 32-bit platforms we allocate separately an rbtree node,
|
|
1766 * a session id, and an ASN1 representation, they take accordingly
|
|
1767 * 64, 32, and 128 bytes.
|
|
1768 *
|
|
1769 * On 64-bit platforms we allocate separately an rbtree node + session_id,
|
|
1770 * and an ASN1 representation, they take accordingly 128 and 128 bytes.
|
|
1771 *
|
272
|
1772 * OpenSSL's i2d_SSL_SESSION() and d2i_SSL_SESSION are slow,
|
|
1773 * so they are outside the code locked by shared pool mutex
|
|
1774 */
|
|
1775
|
|
1776 static int
|
|
1777 ngx_ssl_new_session(ngx_ssl_conn_t *ssl_conn, ngx_ssl_session_t *sess)
|
|
1778 {
|
|
1779 int len;
|
274
|
1780 u_char *p, *id, *cached_sess;
|
272
|
1781 uint32_t hash;
|
|
1782 SSL_CTX *ssl_ctx;
|
|
1783 ngx_shm_zone_t *shm_zone;
|
|
1784 ngx_connection_t *c;
|
|
1785 ngx_slab_pool_t *shpool;
|
|
1786 ngx_ssl_sess_id_t *sess_id;
|
|
1787 ngx_ssl_session_cache_t *cache;
|
|
1788 u_char buf[NGX_SSL_MAX_SESSION_SIZE];
|
|
1789
|
|
1790 len = i2d_SSL_SESSION(sess, NULL);
|
|
1791
|
|
1792 /* do not cache too big session */
|
|
1793
|
|
1794 if (len > (int) NGX_SSL_MAX_SESSION_SIZE) {
|
|
1795 return 0;
|
|
1796 }
|
|
1797
|
|
1798 p = buf;
|
|
1799 i2d_SSL_SESSION(sess, &p);
|
|
1800
|
|
1801 c = ngx_ssl_get_connection(ssl_conn);
|
|
1802
|
|
1803 ssl_ctx = SSL_get_SSL_CTX(ssl_conn);
|
|
1804 shm_zone = SSL_CTX_get_ex_data(ssl_ctx, ngx_ssl_session_cache_index);
|
|
1805
|
|
1806 cache = shm_zone->data;
|
|
1807 shpool = (ngx_slab_pool_t *) shm_zone->shm.addr;
|
|
1808
|
|
1809 ngx_shmtx_lock(&shpool->mutex);
|
|
1810
|
|
1811 /* drop one or two expired sessions */
|
|
1812 ngx_ssl_expire_sessions(cache, shpool, 1);
|
|
1813
|
274
|
1814 cached_sess = ngx_slab_alloc_locked(shpool, len);
|
272
|
1815
|
|
1816 if (cached_sess == NULL) {
|
|
1817
|
|
1818 /* drop the oldest non-expired session and try once more */
|
|
1819
|
|
1820 ngx_ssl_expire_sessions(cache, shpool, 0);
|
|
1821
|
274
|
1822 cached_sess = ngx_slab_alloc_locked(shpool, len);
|
272
|
1823
|
|
1824 if (cached_sess == NULL) {
|
274
|
1825 sess_id = NULL;
|
272
|
1826 goto failed;
|
|
1827 }
|
|
1828 }
|
|
1829
|
|
1830 sess_id = ngx_slab_alloc_locked(shpool, sizeof(ngx_ssl_sess_id_t));
|
|
1831 if (sess_id == NULL) {
|
|
1832 goto failed;
|
|
1833 }
|
|
1834
|
274
|
1835 #if (NGX_PTR_SIZE == 8)
|
|
1836
|
|
1837 id = sess_id->sess_id;
|
|
1838
|
|
1839 #else
|
|
1840
|
|
1841 id = ngx_slab_alloc_locked(shpool, sess->session_id_length);
|
|
1842 if (id == NULL) {
|
|
1843 goto failed;
|
|
1844 }
|
|
1845
|
|
1846 #endif
|
|
1847
|
|
1848 ngx_memcpy(cached_sess, buf, len);
|
272
|
1849
|
|
1850 ngx_memcpy(id, sess->session_id, sess->session_id_length);
|
|
1851
|
|
1852 hash = ngx_crc32_short(sess->session_id, sess->session_id_length);
|
|
1853
|
|
1854 ngx_log_debug3(NGX_LOG_DEBUG_EVENT, c->log, 0,
|
530
|
1855 "ssl new session: %08XD:%d:%d",
|
272
|
1856 hash, sess->session_id_length, len);
|
|
1857
|
|
1858 sess_id->node.key = hash;
|
|
1859 sess_id->node.data = (u_char) sess->session_id_length;
|
|
1860 sess_id->id = id;
|
|
1861 sess_id->len = len;
|
|
1862 sess_id->session = cached_sess;
|
|
1863
|
358
|
1864 sess_id->expire = ngx_time() + SSL_CTX_get_timeout(ssl_ctx);
|
|
1865
|
|
1866 ngx_queue_insert_head(&cache->expire_queue, &sess_id->queue);
|
|
1867
|
|
1868 ngx_rbtree_insert(&cache->session_rbtree, &sess_id->node);
|
272
|
1869
|
|
1870 ngx_shmtx_unlock(&shpool->mutex);
|
|
1871
|
|
1872 return 0;
|
|
1873
|
|
1874 failed:
|
|
1875
|
|
1876 if (cached_sess) {
|
|
1877 ngx_slab_free_locked(shpool, cached_sess);
|
|
1878 }
|
|
1879
|
274
|
1880 if (sess_id) {
|
|
1881 ngx_slab_free_locked(shpool, sess_id);
|
272
|
1882 }
|
|
1883
|
|
1884 ngx_shmtx_unlock(&shpool->mutex);
|
|
1885
|
|
1886 ngx_log_error(NGX_LOG_ALERT, c->log, 0,
|
|
1887 "could not add new SSL session to the session cache");
|
|
1888
|
|
1889 return 0;
|
|
1890 }
|
|
1891
|
|
1892
|
|
1893 static ngx_ssl_session_t *
|
|
1894 ngx_ssl_get_cached_session(ngx_ssl_conn_t *ssl_conn, u_char *id, int len,
|
|
1895 int *copy)
|
|
1896 {
|
|
1897 #if OPENSSL_VERSION_NUMBER >= 0x0090707fL
|
|
1898 const
|
|
1899 #endif
|
|
1900 u_char *p;
|
|
1901 uint32_t hash;
|
274
|
1902 ngx_int_t rc;
|
272
|
1903 ngx_shm_zone_t *shm_zone;
|
|
1904 ngx_slab_pool_t *shpool;
|
|
1905 ngx_rbtree_node_t *node, *sentinel;
|
|
1906 ngx_ssl_session_t *sess;
|
|
1907 ngx_ssl_sess_id_t *sess_id;
|
|
1908 ngx_ssl_session_cache_t *cache;
|
|
1909 u_char buf[NGX_SSL_MAX_SESSION_SIZE];
|
632
|
1910 #if (NGX_DEBUG)
|
|
1911 ngx_connection_t *c;
|
|
1912 #endif
|
272
|
1913
|
274
|
1914 hash = ngx_crc32_short(id, (size_t) len);
|
272
|
1915 *copy = 0;
|
|
1916
|
632
|
1917 #if (NGX_DEBUG)
|
|
1918 c = ngx_ssl_get_connection(ssl_conn);
|
|
1919
|
272
|
1920 ngx_log_debug2(NGX_LOG_DEBUG_EVENT, c->log, 0,
|
530
|
1921 "ssl get session: %08XD:%d", hash, len);
|
632
|
1922 #endif
|
272
|
1923
|
|
1924 shm_zone = SSL_CTX_get_ex_data(SSL_get_SSL_CTX(ssl_conn),
|
|
1925 ngx_ssl_session_cache_index);
|
|
1926
|
|
1927 cache = shm_zone->data;
|
|
1928
|
|
1929 sess = NULL;
|
|
1930
|
|
1931 shpool = (ngx_slab_pool_t *) shm_zone->shm.addr;
|
|
1932
|
|
1933 ngx_shmtx_lock(&shpool->mutex);
|
|
1934
|
358
|
1935 node = cache->session_rbtree.root;
|
|
1936 sentinel = cache->session_rbtree.sentinel;
|
272
|
1937
|
|
1938 while (node != sentinel) {
|
|
1939
|
|
1940 if (hash < node->key) {
|
|
1941 node = node->left;
|
|
1942 continue;
|
|
1943 }
|
|
1944
|
|
1945 if (hash > node->key) {
|
|
1946 node = node->right;
|
|
1947 continue;
|
|
1948 }
|
|
1949
|
274
|
1950 /* hash == node->key */
|
|
1951
|
664
|
1952 sess_id = (ngx_ssl_sess_id_t *) node;
|
|
1953
|
|
1954 rc = ngx_memn2cmp(id, sess_id->id, (size_t) len, (size_t) node->data);
|
|
1955
|
|
1956 if (rc == 0) {
|
|
1957
|
|
1958 if (sess_id->expire > ngx_time()) {
|
|
1959 ngx_memcpy(buf, sess_id->session, sess_id->len);
|
|
1960
|
|
1961 ngx_shmtx_unlock(&shpool->mutex);
|
|
1962
|
|
1963 p = buf;
|
|
1964 sess = d2i_SSL_SESSION(NULL, &p, sess_id->len);
|
|
1965
|
|
1966 return sess;
|
|
1967 }
|
|
1968
|
|
1969 ngx_queue_remove(&sess_id->queue);
|
|
1970
|
|
1971 ngx_rbtree_delete(&cache->session_rbtree, node);
|
|
1972
|
|
1973 ngx_slab_free_locked(shpool, sess_id->session);
|
274
|
1974 #if (NGX_PTR_SIZE == 4)
|
664
|
1975 ngx_slab_free_locked(shpool, sess_id->id);
|
274
|
1976 #endif
|
664
|
1977 ngx_slab_free_locked(shpool, sess_id);
|
|
1978
|
|
1979 sess = NULL;
|
|
1980
|
|
1981 goto done;
|
|
1982 }
|
|
1983
|
|
1984 node = (rc < 0) ? node->left : node->right;
|
272
|
1985 }
|
|
1986
|
274
|
1987 done:
|
|
1988
|
272
|
1989 ngx_shmtx_unlock(&shpool->mutex);
|
|
1990
|
|
1991 return sess;
|
|
1992 }
|
|
1993
|
|
1994
|
366
|
1995 void
|
|
1996 ngx_ssl_remove_cached_session(SSL_CTX *ssl, ngx_ssl_session_t *sess)
|
|
1997 {
|
|
1998 SSL_CTX_remove_session(ssl, sess);
|
|
1999
|
|
2000 ngx_ssl_remove_session(ssl, sess);
|
|
2001 }
|
|
2002
|
|
2003
|
272
|
2004 static void
|
|
2005 ngx_ssl_remove_session(SSL_CTX *ssl, ngx_ssl_session_t *sess)
|
|
2006 {
|
274
|
2007 size_t len;
|
|
2008 u_char *id;
|
272
|
2009 uint32_t hash;
|
274
|
2010 ngx_int_t rc;
|
272
|
2011 ngx_shm_zone_t *shm_zone;
|
|
2012 ngx_slab_pool_t *shpool;
|
|
2013 ngx_rbtree_node_t *node, *sentinel;
|
|
2014 ngx_ssl_sess_id_t *sess_id;
|
|
2015 ngx_ssl_session_cache_t *cache;
|
|
2016
|
|
2017 shm_zone = SSL_CTX_get_ex_data(ssl, ngx_ssl_session_cache_index);
|
|
2018
|
366
|
2019 if (shm_zone == NULL) {
|
|
2020 return;
|
|
2021 }
|
|
2022
|
272
|
2023 cache = shm_zone->data;
|
|
2024
|
|
2025 id = sess->session_id;
|
274
|
2026 len = (size_t) sess->session_id_length;
|
|
2027
|
|
2028 hash = ngx_crc32_short(id, len);
|
272
|
2029
|
|
2030 ngx_log_debug2(NGX_LOG_DEBUG_EVENT, ngx_cycle->log, 0,
|
530
|
2031 "ssl remove session: %08XD:%uz", hash, len);
|
272
|
2032
|
|
2033 shpool = (ngx_slab_pool_t *) shm_zone->shm.addr;
|
|
2034
|
|
2035 ngx_shmtx_lock(&shpool->mutex);
|
|
2036
|
358
|
2037 node = cache->session_rbtree.root;
|
|
2038 sentinel = cache->session_rbtree.sentinel;
|
272
|
2039
|
|
2040 while (node != sentinel) {
|
|
2041
|
|
2042 if (hash < node->key) {
|
|
2043 node = node->left;
|
|
2044 continue;
|
|
2045 }
|
|
2046
|
|
2047 if (hash > node->key) {
|
|
2048 node = node->right;
|
|
2049 continue;
|
|
2050 }
|
|
2051
|
274
|
2052 /* hash == node->key */
|
|
2053
|
664
|
2054 sess_id = (ngx_ssl_sess_id_t *) node;
|
|
2055
|
|
2056 rc = ngx_memn2cmp(id, sess_id->id, len, (size_t) node->data);
|
|
2057
|
|
2058 if (rc == 0) {
|
|
2059
|
|
2060 ngx_queue_remove(&sess_id->queue);
|
|
2061
|
|
2062 ngx_rbtree_delete(&cache->session_rbtree, node);
|
|
2063
|
|
2064 ngx_slab_free_locked(shpool, sess_id->session);
|
274
|
2065 #if (NGX_PTR_SIZE == 4)
|
664
|
2066 ngx_slab_free_locked(shpool, sess_id->id);
|
274
|
2067 #endif
|
664
|
2068 ngx_slab_free_locked(shpool, sess_id);
|
|
2069
|
|
2070 goto done;
|
|
2071 }
|
|
2072
|
|
2073 node = (rc < 0) ? node->left : node->right;
|
272
|
2074 }
|
|
2075
|
274
|
2076 done:
|
|
2077
|
272
|
2078 ngx_shmtx_unlock(&shpool->mutex);
|
|
2079 }
|
|
2080
|
|
2081
|
|
2082 static void
|
|
2083 ngx_ssl_expire_sessions(ngx_ssl_session_cache_t *cache,
|
|
2084 ngx_slab_pool_t *shpool, ngx_uint_t n)
|
|
2085 {
|
358
|
2086 time_t now;
|
|
2087 ngx_queue_t *q;
|
274
|
2088 ngx_ssl_sess_id_t *sess_id;
|
272
|
2089
|
358
|
2090 now = ngx_time();
|
272
|
2091
|
|
2092 while (n < 3) {
|
|
2093
|
358
|
2094 if (ngx_queue_empty(&cache->expire_queue)) {
|
272
|
2095 return;
|
|
2096 }
|
|
2097
|
358
|
2098 q = ngx_queue_last(&cache->expire_queue);
|
|
2099
|
|
2100 sess_id = ngx_queue_data(q, ngx_ssl_sess_id_t, queue);
|
|
2101
|
|
2102 if (n++ != 0 && sess_id->expire > now) {
|
332
|
2103 return;
|
272
|
2104 }
|
|
2105
|
358
|
2106 ngx_queue_remove(q);
|
272
|
2107
|
|
2108 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, ngx_cycle->log, 0,
|
|
2109 "expire session: %08Xi", sess_id->node.key);
|
|
2110
|
358
|
2111 ngx_rbtree_delete(&cache->session_rbtree, &sess_id->node);
|
|
2112
|
274
|
2113 ngx_slab_free_locked(shpool, sess_id->session);
|
|
2114 #if (NGX_PTR_SIZE == 4)
|
272
|
2115 ngx_slab_free_locked(shpool, sess_id->id);
|
274
|
2116 #endif
|
272
|
2117 ngx_slab_free_locked(shpool, sess_id);
|
|
2118 }
|
|
2119 }
|
|
2120
|
|
2121
|
274
|
2122 static void
|
|
2123 ngx_ssl_session_rbtree_insert_value(ngx_rbtree_node_t *temp,
|
|
2124 ngx_rbtree_node_t *node, ngx_rbtree_node_t *sentinel)
|
|
2125 {
|
356
|
2126 ngx_rbtree_node_t **p;
|
|
2127 ngx_ssl_sess_id_t *sess_id, *sess_id_temp;
|
274
|
2128
|
|
2129 for ( ;; ) {
|
|
2130
|
|
2131 if (node->key < temp->key) {
|
|
2132
|
356
|
2133 p = &temp->left;
|
274
|
2134
|
|
2135 } else if (node->key > temp->key) {
|
|
2136
|
356
|
2137 p = &temp->right;
|
274
|
2138
|
|
2139 } else { /* node->key == temp->key */
|
|
2140
|
|
2141 sess_id = (ngx_ssl_sess_id_t *) node;
|
|
2142 sess_id_temp = (ngx_ssl_sess_id_t *) temp;
|
|
2143
|
356
|
2144 p = (ngx_memn2cmp(sess_id->id, sess_id_temp->id,
|
|
2145 (size_t) node->data, (size_t) temp->data)
|
|
2146 < 0) ? &temp->left : &temp->right;
|
274
|
2147 }
|
356
|
2148
|
|
2149 if (*p == sentinel) {
|
|
2150 break;
|
|
2151 }
|
|
2152
|
|
2153 temp = *p;
|
274
|
2154 }
|
|
2155
|
356
|
2156 *p = node;
|
274
|
2157 node->parent = temp;
|
|
2158 node->left = sentinel;
|
|
2159 node->right = sentinel;
|
|
2160 ngx_rbt_red(node);
|
276
|
2161 }
|
274
|
2162
|
|
2163
|
58
|
2164 void
|
|
2165 ngx_ssl_cleanup_ctx(void *data)
|
|
2166 {
|
138
|
2167 ngx_ssl_t *ssl = data;
|
58
|
2168
|
138
|
2169 SSL_CTX_free(ssl->ctx);
|
58
|
2170 }
|
90
|
2171
|
|
2172
|
220
|
2173 ngx_int_t
|
|
2174 ngx_ssl_get_protocol(ngx_connection_t *c, ngx_pool_t *pool, ngx_str_t *s)
|
160
|
2175 {
|
220
|
2176 s->data = (u_char *) SSL_get_version(c->ssl->connection);
|
|
2177 return NGX_OK;
|
160
|
2178 }
|
|
2179
|
|
2180
|
220
|
2181 ngx_int_t
|
|
2182 ngx_ssl_get_cipher_name(ngx_connection_t *c, ngx_pool_t *pool, ngx_str_t *s)
|
160
|
2183 {
|
220
|
2184 s->data = (u_char *) SSL_get_cipher_name(c->ssl->connection);
|
|
2185 return NGX_OK;
|
160
|
2186 }
|
|
2187
|
|
2188
|
196
|
2189 ngx_int_t
|
530
|
2190 ngx_ssl_get_session_id(ngx_connection_t *c, ngx_pool_t *pool, ngx_str_t *s)
|
|
2191 {
|
|
2192 int len;
|
|
2193 u_char *p, *buf;
|
|
2194 SSL_SESSION *sess;
|
|
2195
|
|
2196 sess = SSL_get0_session(c->ssl->connection);
|
|
2197
|
|
2198 len = i2d_SSL_SESSION(sess, NULL);
|
|
2199
|
|
2200 buf = ngx_alloc(len, c->log);
|
|
2201 if (buf == NULL) {
|
|
2202 return NGX_ERROR;
|
|
2203 }
|
|
2204
|
|
2205 s->len = 2 * len;
|
|
2206 s->data = ngx_pnalloc(pool, 2 * len);
|
|
2207 if (s->data == NULL) {
|
|
2208 ngx_free(buf);
|
|
2209 return NGX_ERROR;
|
|
2210 }
|
|
2211
|
|
2212 p = buf;
|
|
2213 i2d_SSL_SESSION(sess, &p);
|
|
2214
|
|
2215 ngx_hex_dump(s->data, buf, len);
|
|
2216
|
|
2217 ngx_free(buf);
|
|
2218
|
|
2219 return NGX_OK;
|
|
2220 }
|
|
2221
|
|
2222
|
|
2223 ngx_int_t
|
390
|
2224 ngx_ssl_get_raw_certificate(ngx_connection_t *c, ngx_pool_t *pool, ngx_str_t *s)
|
380
|
2225 {
|
|
2226 size_t len;
|
|
2227 BIO *bio;
|
|
2228 X509 *cert;
|
|
2229
|
|
2230 s->len = 0;
|
|
2231
|
|
2232 cert = SSL_get_peer_certificate(c->ssl->connection);
|
|
2233 if (cert == NULL) {
|
|
2234 return NGX_OK;
|
|
2235 }
|
|
2236
|
|
2237 bio = BIO_new(BIO_s_mem());
|
|
2238 if (bio == NULL) {
|
|
2239 ngx_ssl_error(NGX_LOG_ALERT, c->log, 0, "BIO_new() failed");
|
|
2240 X509_free(cert);
|
|
2241 return NGX_ERROR;
|
|
2242 }
|
|
2243
|
|
2244 if (PEM_write_bio_X509(bio, cert) == 0) {
|
|
2245 ngx_ssl_error(NGX_LOG_ALERT, c->log, 0, "PEM_write_bio_X509() failed");
|
|
2246 goto failed;
|
|
2247 }
|
|
2248
|
|
2249 len = BIO_pending(bio);
|
|
2250 s->len = len;
|
|
2251
|
382
|
2252 s->data = ngx_pnalloc(pool, len);
|
380
|
2253 if (s->data == NULL) {
|
|
2254 goto failed;
|
|
2255 }
|
|
2256
|
|
2257 BIO_read(bio, s->data, len);
|
|
2258
|
|
2259 BIO_free(bio);
|
|
2260 X509_free(cert);
|
|
2261
|
|
2262 return NGX_OK;
|
|
2263
|
|
2264 failed:
|
|
2265
|
|
2266 BIO_free(bio);
|
|
2267 X509_free(cert);
|
|
2268
|
|
2269 return NGX_ERROR;
|
|
2270 }
|
|
2271
|
|
2272
|
|
2273 ngx_int_t
|
390
|
2274 ngx_ssl_get_certificate(ngx_connection_t *c, ngx_pool_t *pool, ngx_str_t *s)
|
|
2275 {
|
|
2276 u_char *p;
|
|
2277 size_t len;
|
|
2278 ngx_uint_t i;
|
|
2279 ngx_str_t cert;
|
|
2280
|
|
2281 if (ngx_ssl_get_raw_certificate(c, pool, &cert) != NGX_OK) {
|
|
2282 return NGX_ERROR;
|
|
2283 }
|
|
2284
|
|
2285 if (cert.len == 0) {
|
|
2286 s->len = 0;
|
|
2287 return NGX_OK;
|
|
2288 }
|
|
2289
|
|
2290 len = cert.len - 1;
|
|
2291
|
|
2292 for (i = 0; i < cert.len - 1; i++) {
|
|
2293 if (cert.data[i] == LF) {
|
|
2294 len++;
|
|
2295 }
|
|
2296 }
|
|
2297
|
|
2298 s->len = len;
|
|
2299 s->data = ngx_pnalloc(pool, len);
|
|
2300 if (s->data == NULL) {
|
|
2301 return NGX_ERROR;
|
|
2302 }
|
|
2303
|
|
2304 p = s->data;
|
|
2305
|
510
|
2306 for (i = 0; i < cert.len - 1; i++) {
|
390
|
2307 *p++ = cert.data[i];
|
|
2308 if (cert.data[i] == LF) {
|
|
2309 *p++ = '\t';
|
|
2310 }
|
|
2311 }
|
|
2312
|
|
2313 return NGX_OK;
|
|
2314 }
|
|
2315
|
|
2316
|
|
2317 ngx_int_t
|
196
|
2318 ngx_ssl_get_subject_dn(ngx_connection_t *c, ngx_pool_t *pool, ngx_str_t *s)
|
|
2319 {
|
|
2320 char *p;
|
|
2321 size_t len;
|
|
2322 X509 *cert;
|
|
2323 X509_NAME *name;
|
|
2324
|
|
2325 s->len = 0;
|
|
2326
|
|
2327 cert = SSL_get_peer_certificate(c->ssl->connection);
|
|
2328 if (cert == NULL) {
|
|
2329 return NGX_OK;
|
|
2330 }
|
|
2331
|
|
2332 name = X509_get_subject_name(cert);
|
|
2333 if (name == NULL) {
|
372
|
2334 X509_free(cert);
|
196
|
2335 return NGX_ERROR;
|
|
2336 }
|
|
2337
|
|
2338 p = X509_NAME_oneline(name, NULL, 0);
|
|
2339
|
|
2340 for (len = 0; p[len]; len++) { /* void */ }
|
|
2341
|
|
2342 s->len = len;
|
382
|
2343 s->data = ngx_pnalloc(pool, len);
|
196
|
2344 if (s->data == NULL) {
|
|
2345 OPENSSL_free(p);
|
372
|
2346 X509_free(cert);
|
196
|
2347 return NGX_ERROR;
|
|
2348 }
|
|
2349
|
|
2350 ngx_memcpy(s->data, p, len);
|
|
2351
|
|
2352 OPENSSL_free(p);
|
372
|
2353 X509_free(cert);
|
196
|
2354
|
|
2355 return NGX_OK;
|
|
2356 }
|
|
2357
|
|
2358
|
|
2359 ngx_int_t
|
|
2360 ngx_ssl_get_issuer_dn(ngx_connection_t *c, ngx_pool_t *pool, ngx_str_t *s)
|
|
2361 {
|
|
2362 char *p;
|
|
2363 size_t len;
|
|
2364 X509 *cert;
|
|
2365 X509_NAME *name;
|
|
2366
|
|
2367 s->len = 0;
|
|
2368
|
|
2369 cert = SSL_get_peer_certificate(c->ssl->connection);
|
|
2370 if (cert == NULL) {
|
|
2371 return NGX_OK;
|
|
2372 }
|
|
2373
|
|
2374 name = X509_get_issuer_name(cert);
|
|
2375 if (name == NULL) {
|
372
|
2376 X509_free(cert);
|
196
|
2377 return NGX_ERROR;
|
|
2378 }
|
|
2379
|
|
2380 p = X509_NAME_oneline(name, NULL, 0);
|
|
2381
|
|
2382 for (len = 0; p[len]; len++) { /* void */ }
|
|
2383
|
|
2384 s->len = len;
|
382
|
2385 s->data = ngx_pnalloc(pool, len);
|
196
|
2386 if (s->data == NULL) {
|
|
2387 OPENSSL_free(p);
|
372
|
2388 X509_free(cert);
|
196
|
2389 return NGX_ERROR;
|
|
2390 }
|
|
2391
|
|
2392 ngx_memcpy(s->data, p, len);
|
|
2393
|
|
2394 OPENSSL_free(p);
|
372
|
2395 X509_free(cert);
|
196
|
2396
|
|
2397 return NGX_OK;
|
|
2398 }
|
|
2399
|
|
2400
|
220
|
2401 ngx_int_t
|
|
2402 ngx_ssl_get_serial_number(ngx_connection_t *c, ngx_pool_t *pool, ngx_str_t *s)
|
|
2403 {
|
|
2404 size_t len;
|
|
2405 X509 *cert;
|
|
2406 BIO *bio;
|
|
2407
|
|
2408 s->len = 0;
|
|
2409
|
|
2410 cert = SSL_get_peer_certificate(c->ssl->connection);
|
|
2411 if (cert == NULL) {
|
|
2412 return NGX_OK;
|
|
2413 }
|
|
2414
|
|
2415 bio = BIO_new(BIO_s_mem());
|
|
2416 if (bio == NULL) {
|
372
|
2417 X509_free(cert);
|
220
|
2418 return NGX_ERROR;
|
|
2419 }
|
|
2420
|
|
2421 i2a_ASN1_INTEGER(bio, X509_get_serialNumber(cert));
|
|
2422 len = BIO_pending(bio);
|
|
2423
|
|
2424 s->len = len;
|
382
|
2425 s->data = ngx_pnalloc(pool, len);
|
220
|
2426 if (s->data == NULL) {
|
|
2427 BIO_free(bio);
|
372
|
2428 X509_free(cert);
|
220
|
2429 return NGX_ERROR;
|
|
2430 }
|
|
2431
|
|
2432 BIO_read(bio, s->data, len);
|
|
2433 BIO_free(bio);
|
372
|
2434 X509_free(cert);
|
220
|
2435
|
|
2436 return NGX_OK;
|
|
2437 }
|
|
2438
|
|
2439
|
510
|
2440 ngx_int_t
|
|
2441 ngx_ssl_get_client_verify(ngx_connection_t *c, ngx_pool_t *pool, ngx_str_t *s)
|
|
2442 {
|
|
2443 X509 *cert;
|
|
2444
|
|
2445 if (SSL_get_verify_result(c->ssl->connection) != X509_V_OK) {
|
570
|
2446 ngx_str_set(s, "FAILED");
|
510
|
2447 return NGX_OK;
|
|
2448 }
|
|
2449
|
|
2450 cert = SSL_get_peer_certificate(c->ssl->connection);
|
|
2451
|
|
2452 if (cert) {
|
570
|
2453 ngx_str_set(s, "SUCCESS");
|
510
|
2454
|
|
2455 } else {
|
570
|
2456 ngx_str_set(s, "NONE");
|
510
|
2457 }
|
|
2458
|
|
2459 X509_free(cert);
|
|
2460
|
|
2461 return NGX_OK;
|
|
2462 }
|
|
2463
|
|
2464
|
90
|
2465 static void *
|
|
2466 ngx_openssl_create_conf(ngx_cycle_t *cycle)
|
|
2467 {
|
|
2468 ngx_openssl_conf_t *oscf;
|
126
|
2469
|
90
|
2470 oscf = ngx_pcalloc(cycle->pool, sizeof(ngx_openssl_conf_t));
|
|
2471 if (oscf == NULL) {
|
496
|
2472 return NULL;
|
90
|
2473 }
|
126
|
2474
|
90
|
2475 /*
|
|
2476 * set by ngx_pcalloc():
|
126
|
2477 *
|
446
|
2478 * oscf->engine = 0;
|
126
|
2479 */
|
90
|
2480
|
|
2481 return oscf;
|
|
2482 }
|
|
2483
|
|
2484
|
|
2485 static char *
|
446
|
2486 ngx_openssl_engine(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
|
90
|
2487 {
|
|
2488 ngx_openssl_conf_t *oscf = conf;
|
120
|
2489
|
446
|
2490 ENGINE *engine;
|
|
2491 ngx_str_t *value;
|
|
2492
|
|
2493 if (oscf->engine) {
|
|
2494 return "is duplicate";
|
90
|
2495 }
|
126
|
2496
|
446
|
2497 oscf->engine = 1;
|
|
2498
|
|
2499 value = cf->args->elts;
|
|
2500
|
|
2501 engine = ENGINE_by_id((const char *) value[1].data);
|
90
|
2502
|
|
2503 if (engine == NULL) {
|
446
|
2504 ngx_ssl_error(NGX_LOG_WARN, cf->log, 0,
|
|
2505 "ENGINE_by_id(\"%V\") failed", &value[1]);
|
90
|
2506 return NGX_CONF_ERROR;
|
|
2507 }
|
|
2508
|
|
2509 if (ENGINE_set_default(engine, ENGINE_METHOD_ALL) == 0) {
|
446
|
2510 ngx_ssl_error(NGX_LOG_WARN, cf->log, 0,
|
90
|
2511 "ENGINE_set_default(\"%V\", ENGINE_METHOD_ALL) failed",
|
446
|
2512 &value[1]);
|
|
2513
|
|
2514 ENGINE_free(engine);
|
|
2515
|
90
|
2516 return NGX_CONF_ERROR;
|
|
2517 }
|
|
2518
|
|
2519 ENGINE_free(engine);
|
|
2520
|
|
2521 return NGX_CONF_OK;
|
446
|
2522 }
|
120
|
2523
|
|
2524
|
|
2525 static void
|
|
2526 ngx_openssl_exit(ngx_cycle_t *cycle)
|
|
2527 {
|
564
|
2528 EVP_cleanup();
|
120
|
2529 ENGINE_cleanup();
|
|
2530 }
|