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