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