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