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 {
|
446
|
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);
|
542
|
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);
|
446
|
42 static char *ngx_openssl_engine(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
|
120
|
43 static void ngx_openssl_exit(ngx_cycle_t *cycle);
|
90
|
44
|
|
45
|
|
46 static ngx_command_t ngx_openssl_commands[] = {
|
|
47
|
|
48 { ngx_string("ssl_engine"),
|
|
49 NGX_MAIN_CONF|NGX_DIRECT_CONF|NGX_CONF_TAKE1,
|
446
|
50 ngx_openssl_engine,
|
90
|
51 0,
|
446
|
52 0,
|
90
|
53 NULL },
|
|
54
|
|
55 ngx_null_command
|
|
56 };
|
|
57
|
|
58
|
|
59 static ngx_core_module_t ngx_openssl_module_ctx = {
|
|
60 ngx_string("openssl"),
|
|
61 ngx_openssl_create_conf,
|
446
|
62 NULL
|
126
|
63 };
|
90
|
64
|
|
65
|
|
66 ngx_module_t ngx_openssl_module = {
|
|
67 NGX_MODULE_V1,
|
|
68 &ngx_openssl_module_ctx, /* module context */
|
|
69 ngx_openssl_commands, /* module directives */
|
|
70 NGX_CORE_MODULE, /* module type */
|
|
71 NULL, /* init master */
|
|
72 NULL, /* init module */
|
|
73 NULL, /* init process */
|
|
74 NULL, /* init thread */
|
|
75 NULL, /* exit thread */
|
|
76 NULL, /* exit process */
|
120
|
77 ngx_openssl_exit, /* exit master */
|
90
|
78 NGX_MODULE_V1_PADDING
|
96
|
79 };
|
|
80
|
|
81
|
|
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
|
564
|
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
|
542
|
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 {
|
454
|
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
|
454
|
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
|
454
|
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
|
510
|
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
|
542
|
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
|
454
|
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
|
430
|
553 if (ngx_handle_read_event(c->read, 0) != NGX_OK) {
|
96
|
554 return NGX_ERROR;
|
|
555 }
|
|
556
|
430
|
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;
|
|
564 SSL_CIPHER *cipher;
|
|
565
|
|
566 cipher = SSL_get_current_cipher(c->ssl->connection);
|
|
567
|
|
568 if (cipher) {
|
|
569 SSL_CIPHER_description(cipher, &buf[1], 128);
|
|
570
|
|
571 for (s = &buf[1], d = buf; *s; s++) {
|
|
572 if (*s == ' ' && *d == ' ') {
|
|
573 continue;
|
|
574 }
|
|
575
|
|
576 if (*s == LF || *s == CR) {
|
|
577 continue;
|
|
578 }
|
|
579
|
|
580 *++d = *s;
|
|
581 }
|
|
582
|
|
583 if (*d != ' ') {
|
|
584 d++;
|
|
585 }
|
|
586
|
|
587 *d = '\0';
|
|
588
|
132
|
589 ngx_log_debug2(NGX_LOG_DEBUG_EVENT, c->log, 0,
|
96
|
590 "SSL: %s, cipher: \"%s\"",
|
126
|
591 SSL_get_version(c->ssl->connection), &buf[1]);
|
96
|
592
|
|
593 if (SSL_session_reused(c->ssl->connection)) {
|
132
|
594 ngx_log_debug0(NGX_LOG_DEBUG_EVENT, c->log, 0,
|
96
|
595 "SSL reused session");
|
|
596 }
|
|
597
|
|
598 } else {
|
|
599 ngx_log_debug0(NGX_LOG_DEBUG_EVENT, c->log, 0,
|
126
|
600 "SSL no shared ciphers");
|
96
|
601 }
|
|
602 }
|
|
603 #endif
|
|
604
|
|
605 c->ssl->handshaked = 1;
|
|
606
|
|
607 c->recv = ngx_ssl_recv;
|
|
608 c->send = ngx_ssl_write;
|
126
|
609 c->recv_chain = ngx_ssl_recv_chain;
|
|
610 c->send_chain = ngx_ssl_send_chain;
|
96
|
611
|
542
|
612 /* initial handshake done, disable renegotiation (CVE-2009-3555) */
|
|
613 if (c->ssl->connection->s3) {
|
|
614 c->ssl->connection->s3->flags |= SSL3_FLAGS_NO_RENEGOTIATE_CIPHERS;
|
|
615 }
|
|
616
|
96
|
617 return NGX_OK;
|
|
618 }
|
|
619
|
|
620 sslerr = SSL_get_error(c->ssl->connection, n);
|
|
621
|
|
622 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, c->log, 0, "SSL_get_error: %d", sslerr);
|
|
623
|
|
624 if (sslerr == SSL_ERROR_WANT_READ) {
|
|
625 c->read->ready = 0;
|
|
626 c->read->handler = ngx_ssl_handshake_handler;
|
140
|
627 c->write->handler = ngx_ssl_handshake_handler;
|
96
|
628
|
430
|
629 if (ngx_handle_read_event(c->read, 0) != NGX_OK) {
|
96
|
630 return NGX_ERROR;
|
|
631 }
|
|
632
|
|
633 return NGX_AGAIN;
|
|
634 }
|
|
635
|
|
636 if (sslerr == SSL_ERROR_WANT_WRITE) {
|
|
637 c->write->ready = 0;
|
140
|
638 c->read->handler = ngx_ssl_handshake_handler;
|
96
|
639 c->write->handler = ngx_ssl_handshake_handler;
|
|
640
|
430
|
641 if (ngx_handle_write_event(c->write, 0) != NGX_OK) {
|
96
|
642 return NGX_ERROR;
|
|
643 }
|
|
644
|
|
645 return NGX_AGAIN;
|
|
646 }
|
|
647
|
|
648 err = (sslerr == SSL_ERROR_SYSCALL) ? ngx_errno : 0;
|
|
649
|
|
650 c->ssl->no_wait_shutdown = 1;
|
|
651 c->ssl->no_send_shutdown = 1;
|
140
|
652 c->read->eof = 1;
|
96
|
653
|
|
654 if (sslerr == SSL_ERROR_ZERO_RETURN || ERR_peek_error() == 0) {
|
|
655 ngx_log_error(NGX_LOG_INFO, c->log, err,
|
126
|
656 "peer closed connection in SSL handshake");
|
96
|
657
|
|
658 return NGX_ERROR;
|
|
659 }
|
|
660
|
140
|
661 c->read->error = 1;
|
|
662
|
96
|
663 ngx_ssl_connection_error(c, sslerr, err, "SSL_do_handshake() failed");
|
|
664
|
|
665 return NGX_ERROR;
|
|
666 }
|
|
667
|
|
668
|
|
669 static void
|
|
670 ngx_ssl_handshake_handler(ngx_event_t *ev)
|
|
671 {
|
|
672 ngx_connection_t *c;
|
|
673
|
|
674 c = ev->data;
|
|
675
|
98
|
676 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, c->log, 0,
|
126
|
677 "SSL handshake handler: %d", ev->write);
|
96
|
678
|
140
|
679 if (ev->timedout) {
|
|
680 c->ssl->handler(c);
|
|
681 return;
|
|
682 }
|
|
683
|
96
|
684 if (ngx_ssl_handshake(c) == NGX_AGAIN) {
|
|
685 return;
|
|
686 }
|
|
687
|
|
688 c->ssl->handler(c);
|
|
689 }
|
|
690
|
|
691
|
38
|
692 ssize_t
|
126
|
693 ngx_ssl_recv_chain(ngx_connection_t *c, ngx_chain_t *cl)
|
|
694 {
|
294
|
695 u_char *last;
|
126
|
696 ssize_t n, bytes;
|
|
697 ngx_buf_t *b;
|
|
698
|
|
699 bytes = 0;
|
|
700
|
294
|
701 b = cl->buf;
|
|
702 last = b->last;
|
|
703
|
|
704 for ( ;; ) {
|
|
705
|
|
706 n = ngx_ssl_recv(c, last, b->end - last);
|
126
|
707
|
|
708 if (n > 0) {
|
294
|
709 last += n;
|
126
|
710 bytes += n;
|
|
711
|
294
|
712 if (last == b->end) {
|
126
|
713 cl = cl->next;
|
294
|
714
|
|
715 if (cl == NULL) {
|
|
716 return bytes;
|
|
717 }
|
|
718
|
|
719 b = cl->buf;
|
|
720 last = b->last;
|
126
|
721 }
|
|
722
|
|
723 continue;
|
|
724 }
|
|
725
|
|
726 if (bytes) {
|
382
|
727
|
|
728 if (n == 0 || n == NGX_ERROR) {
|
|
729 c->read->ready = 1;
|
|
730 }
|
|
731
|
126
|
732 return bytes;
|
|
733 }
|
|
734
|
|
735 return n;
|
|
736 }
|
|
737 }
|
|
738
|
|
739
|
|
740 ssize_t
|
38
|
741 ngx_ssl_recv(ngx_connection_t *c, u_char *buf, size_t size)
|
0
|
742 {
|
38
|
743 int n, bytes;
|
|
744
|
|
745 if (c->ssl->last == NGX_ERROR) {
|
330
|
746 c->read->error = 1;
|
38
|
747 return NGX_ERROR;
|
|
748 }
|
|
749
|
126
|
750 if (c->ssl->last == NGX_DONE) {
|
330
|
751 c->read->ready = 0;
|
|
752 c->read->eof = 1;
|
126
|
753 return 0;
|
|
754 }
|
|
755
|
38
|
756 bytes = 0;
|
0
|
757
|
358
|
758 ngx_ssl_clear_error(c->log);
|
|
759
|
38
|
760 /*
|
|
761 * SSL_read() may return data in parts, so try to read
|
|
762 * until SSL_read() would return no data
|
|
763 */
|
|
764
|
|
765 for ( ;; ) {
|
0
|
766
|
92
|
767 n = SSL_read(c->ssl->connection, buf, size);
|
38
|
768
|
126
|
769 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, c->log, 0, "SSL_read: %d", n);
|
0
|
770
|
38
|
771 if (n > 0) {
|
|
772 bytes += n;
|
|
773 }
|
|
774
|
|
775 c->ssl->last = ngx_ssl_handle_recv(c, n);
|
|
776
|
330
|
777 if (c->ssl->last == NGX_OK) {
|
|
778
|
|
779 size -= n;
|
|
780
|
|
781 if (size == 0) {
|
38
|
782 return bytes;
|
126
|
783 }
|
38
|
784
|
330
|
785 buf += n;
|
|
786
|
|
787 continue;
|
|
788 }
|
|
789
|
|
790 if (bytes) {
|
|
791 return bytes;
|
|
792 }
|
|
793
|
|
794 switch (c->ssl->last) {
|
|
795
|
|
796 case NGX_DONE:
|
|
797 c->read->ready = 0;
|
|
798 c->read->eof = 1;
|
|
799 return 0;
|
|
800
|
|
801 case NGX_ERROR:
|
|
802 c->read->error = 1;
|
|
803
|
|
804 /* fall thruogh */
|
|
805
|
|
806 case NGX_AGAIN:
|
126
|
807 return c->ssl->last;
|
28
|
808 }
|
38
|
809 }
|
|
810 }
|
|
811
|
|
812
|
|
813 static ngx_int_t
|
|
814 ngx_ssl_handle_recv(ngx_connection_t *c, int n)
|
|
815 {
|
96
|
816 int sslerr;
|
|
817 ngx_err_t err;
|
38
|
818
|
542
|
819 if (c->ssl->renegotiation) {
|
|
820 /*
|
|
821 * disable renegotiation (CVE-2009-3555):
|
|
822 * OpenSSL (at least up to 0.9.8l) does not handle disabled
|
|
823 * renegotiation gracefully, so drop connection here
|
|
824 */
|
|
825
|
|
826 ngx_log_error(NGX_LOG_NOTICE, c->log, 0, "SSL renegotiation disabled");
|
|
827
|
|
828 c->ssl->no_wait_shutdown = 1;
|
|
829 c->ssl->no_send_shutdown = 1;
|
|
830
|
|
831 return NGX_ERROR;
|
|
832 }
|
|
833
|
38
|
834 if (n > 0) {
|
28
|
835
|
22
|
836 if (c->ssl->saved_write_handler) {
|
|
837
|
58
|
838 c->write->handler = c->ssl->saved_write_handler;
|
22
|
839 c->ssl->saved_write_handler = NULL;
|
|
840 c->write->ready = 1;
|
|
841
|
430
|
842 if (ngx_handle_write_event(c->write, 0) != NGX_OK) {
|
22
|
843 return NGX_ERROR;
|
|
844 }
|
|
845
|
112
|
846 ngx_post_event(c->write, &ngx_posted_events);
|
22
|
847 }
|
|
848
|
38
|
849 return NGX_OK;
|
0
|
850 }
|
|
851
|
92
|
852 sslerr = SSL_get_error(c->ssl->connection, n);
|
0
|
853
|
|
854 err = (sslerr == SSL_ERROR_SYSCALL) ? ngx_errno : 0;
|
|
855
|
|
856 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, c->log, 0, "SSL_get_error: %d", sslerr);
|
|
857
|
|
858 if (sslerr == SSL_ERROR_WANT_READ) {
|
4
|
859 c->read->ready = 0;
|
0
|
860 return NGX_AGAIN;
|
|
861 }
|
|
862
|
|
863 if (sslerr == SSL_ERROR_WANT_WRITE) {
|
88
|
864
|
96
|
865 ngx_log_error(NGX_LOG_INFO, c->log, 0,
|
126
|
866 "peer started SSL renegotiation");
|
22
|
867
|
|
868 c->write->ready = 0;
|
|
869
|
430
|
870 if (ngx_handle_write_event(c->write, 0) != NGX_OK) {
|
22
|
871 return NGX_ERROR;
|
|
872 }
|
|
873
|
|
874 /*
|
|
875 * we do not set the timer because there is already the read event timer
|
|
876 */
|
|
877
|
|
878 if (c->ssl->saved_write_handler == NULL) {
|
58
|
879 c->ssl->saved_write_handler = c->write->handler;
|
|
880 c->write->handler = ngx_ssl_write_handler;
|
22
|
881 }
|
|
882
|
0
|
883 return NGX_AGAIN;
|
|
884 }
|
|
885
|
96
|
886 c->ssl->no_wait_shutdown = 1;
|
|
887 c->ssl->no_send_shutdown = 1;
|
0
|
888
|
|
889 if (sslerr == SSL_ERROR_ZERO_RETURN || ERR_peek_error() == 0) {
|
126
|
890 ngx_log_debug0(NGX_LOG_DEBUG_EVENT, c->log, 0,
|
|
891 "peer shutdown SSL cleanly");
|
|
892 return NGX_DONE;
|
0
|
893 }
|
|
894
|
96
|
895 ngx_ssl_connection_error(c, sslerr, err, "SSL_read() failed");
|
0
|
896
|
|
897 return NGX_ERROR;
|
|
898 }
|
|
899
|
|
900
|
38
|
901 static void
|
|
902 ngx_ssl_write_handler(ngx_event_t *wev)
|
22
|
903 {
|
|
904 ngx_connection_t *c;
|
|
905
|
|
906 c = wev->data;
|
96
|
907
|
58
|
908 c->read->handler(c->read);
|
22
|
909 }
|
|
910
|
|
911
|
0
|
912 /*
|
|
913 * OpenSSL has no SSL_writev() so we copy several bufs into our 16K buffer
|
22
|
914 * before the SSL_write() call to decrease a SSL overhead.
|
0
|
915 *
|
|
916 * Besides for protocols such as HTTP it is possible to always buffer
|
|
917 * the output to decrease a SSL overhead some more.
|
|
918 */
|
|
919
|
38
|
920 ngx_chain_t *
|
|
921 ngx_ssl_send_chain(ngx_connection_t *c, ngx_chain_t *in, off_t limit)
|
0
|
922 {
|
|
923 int n;
|
|
924 ngx_uint_t flush;
|
|
925 ssize_t send, size;
|
|
926 ngx_buf_t *buf;
|
|
927
|
416
|
928 if (!c->ssl->buffer) {
|
0
|
929
|
126
|
930 while (in) {
|
|
931 if (ngx_buf_special(in->buf)) {
|
|
932 in = in->next;
|
|
933 continue;
|
|
934 }
|
0
|
935
|
126
|
936 n = ngx_ssl_write(c, in->buf->pos, in->buf->last - in->buf->pos);
|
|
937
|
|
938 if (n == NGX_ERROR) {
|
|
939 return NGX_CHAIN_ERROR;
|
|
940 }
|
0
|
941
|
126
|
942 if (n == NGX_AGAIN) {
|
146
|
943 c->buffered |= NGX_SSL_BUFFERED;
|
126
|
944 return in;
|
|
945 }
|
|
946
|
|
947 in->buf->pos += n;
|
|
948
|
|
949 if (in->buf->pos == in->buf->last) {
|
|
950 in = in->next;
|
|
951 }
|
0
|
952 }
|
|
953
|
|
954 return in;
|
|
955 }
|
|
956
|
22
|
957
|
|
958 /* the maximum limit size is the maximum uint32_t value - the page size */
|
|
959
|
324
|
960 if (limit == 0 || limit > (off_t) (NGX_MAX_UINT32_VALUE - ngx_pagesize)) {
|
22
|
961 limit = NGX_MAX_UINT32_VALUE - ngx_pagesize;
|
|
962 }
|
|
963
|
126
|
964 buf = c->ssl->buf;
|
358
|
965
|
|
966 if (buf == NULL) {
|
|
967 buf = ngx_create_temp_buf(c->pool, NGX_SSL_BUFSIZE);
|
|
968 if (buf == NULL) {
|
|
969 return NGX_CHAIN_ERROR;
|
|
970 }
|
|
971
|
|
972 c->ssl->buf = buf;
|
|
973 }
|
|
974
|
|
975 if (buf->start == NULL) {
|
|
976 buf->start = ngx_palloc(c->pool, NGX_SSL_BUFSIZE);
|
|
977 if (buf->start == NULL) {
|
|
978 return NGX_CHAIN_ERROR;
|
|
979 }
|
|
980
|
|
981 buf->pos = buf->start;
|
|
982 buf->last = buf->start;
|
|
983 buf->end = buf->start + NGX_SSL_BUFSIZE;
|
|
984 }
|
|
985
|
0
|
986 send = 0;
|
|
987 flush = (in == NULL) ? 1 : 0;
|
|
988
|
|
989 for ( ;; ) {
|
|
990
|
540
|
991 while (in && buf->last < buf->end && send < limit) {
|
132
|
992 if (in->buf->last_buf || in->buf->flush) {
|
0
|
993 flush = 1;
|
|
994 }
|
|
995
|
|
996 if (ngx_buf_special(in->buf)) {
|
|
997 in = in->next;
|
|
998 continue;
|
|
999 }
|
|
1000
|
|
1001 size = in->buf->last - in->buf->pos;
|
|
1002
|
|
1003 if (size > buf->end - buf->last) {
|
|
1004 size = buf->end - buf->last;
|
|
1005 }
|
|
1006
|
|
1007 if (send + size > limit) {
|
126
|
1008 size = (ssize_t) (limit - send);
|
0
|
1009 flush = 1;
|
|
1010 }
|
|
1011
|
|
1012 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, c->log, 0,
|
|
1013 "SSL buf copy: %d", size);
|
|
1014
|
|
1015 ngx_memcpy(buf->last, in->buf->pos, size);
|
|
1016
|
|
1017 buf->last += size;
|
|
1018 in->buf->pos += size;
|
540
|
1019 send += size;
|
126
|
1020
|
0
|
1021 if (in->buf->pos == in->buf->last) {
|
|
1022 in = in->next;
|
|
1023 }
|
|
1024 }
|
|
1025
|
|
1026 size = buf->last - buf->pos;
|
|
1027
|
|
1028 if (!flush && buf->last < buf->end && c->ssl->buffer) {
|
|
1029 break;
|
|
1030 }
|
|
1031
|
|
1032 n = ngx_ssl_write(c, buf->pos, size);
|
|
1033
|
|
1034 if (n == NGX_ERROR) {
|
|
1035 return NGX_CHAIN_ERROR;
|
|
1036 }
|
|
1037
|
60
|
1038 if (n == NGX_AGAIN) {
|
146
|
1039 c->buffered |= NGX_SSL_BUFFERED;
|
60
|
1040 return in;
|
0
|
1041 }
|
|
1042
|
|
1043 buf->pos += n;
|
|
1044 c->sent += n;
|
|
1045
|
|
1046 if (n < size) {
|
|
1047 break;
|
|
1048 }
|
|
1049
|
|
1050 if (buf->pos == buf->last) {
|
|
1051 buf->pos = buf->start;
|
|
1052 buf->last = buf->start;
|
|
1053 }
|
|
1054
|
|
1055 if (in == NULL || send == limit) {
|
|
1056 break;
|
|
1057 }
|
|
1058 }
|
|
1059
|
146
|
1060 if (buf->pos < buf->last) {
|
|
1061 c->buffered |= NGX_SSL_BUFFERED;
|
|
1062
|
|
1063 } else {
|
|
1064 c->buffered &= ~NGX_SSL_BUFFERED;
|
|
1065 }
|
0
|
1066
|
|
1067 return in;
|
|
1068 }
|
|
1069
|
|
1070
|
88
|
1071 ssize_t
|
38
|
1072 ngx_ssl_write(ngx_connection_t *c, u_char *data, size_t size)
|
0
|
1073 {
|
96
|
1074 int n, sslerr;
|
|
1075 ngx_err_t err;
|
0
|
1076
|
358
|
1077 ngx_ssl_clear_error(c->log);
|
|
1078
|
0
|
1079 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, c->log, 0, "SSL to write: %d", size);
|
|
1080
|
92
|
1081 n = SSL_write(c->ssl->connection, data, size);
|
0
|
1082
|
|
1083 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, c->log, 0, "SSL_write: %d", n);
|
|
1084
|
|
1085 if (n > 0) {
|
88
|
1086
|
22
|
1087 if (c->ssl->saved_read_handler) {
|
|
1088
|
58
|
1089 c->read->handler = c->ssl->saved_read_handler;
|
22
|
1090 c->ssl->saved_read_handler = NULL;
|
|
1091 c->read->ready = 1;
|
|
1092
|
430
|
1093 if (ngx_handle_read_event(c->read, 0) != NGX_OK) {
|
22
|
1094 return NGX_ERROR;
|
|
1095 }
|
|
1096
|
112
|
1097 ngx_post_event(c->read, &ngx_posted_events);
|
22
|
1098 }
|
|
1099
|
0
|
1100 return n;
|
|
1101 }
|
|
1102
|
92
|
1103 sslerr = SSL_get_error(c->ssl->connection, n);
|
0
|
1104
|
|
1105 err = (sslerr == SSL_ERROR_SYSCALL) ? ngx_errno : 0;
|
|
1106
|
|
1107 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, c->log, 0, "SSL_get_error: %d", sslerr);
|
|
1108
|
|
1109 if (sslerr == SSL_ERROR_WANT_WRITE) {
|
|
1110 c->write->ready = 0;
|
|
1111 return NGX_AGAIN;
|
|
1112 }
|
|
1113
|
|
1114 if (sslerr == SSL_ERROR_WANT_READ) {
|
2
|
1115
|
96
|
1116 ngx_log_error(NGX_LOG_INFO, c->log, 0,
|
126
|
1117 "peer started SSL renegotiation");
|
22
|
1118
|
|
1119 c->read->ready = 0;
|
|
1120
|
430
|
1121 if (ngx_handle_read_event(c->read, 0) != NGX_OK) {
|
22
|
1122 return NGX_ERROR;
|
|
1123 }
|
|
1124
|
|
1125 /*
|
|
1126 * we do not set the timer because there is already
|
|
1127 * the write event timer
|
|
1128 */
|
|
1129
|
|
1130 if (c->ssl->saved_read_handler == NULL) {
|
58
|
1131 c->ssl->saved_read_handler = c->read->handler;
|
|
1132 c->read->handler = ngx_ssl_read_handler;
|
22
|
1133 }
|
|
1134
|
0
|
1135 return NGX_AGAIN;
|
|
1136 }
|
|
1137
|
96
|
1138 c->ssl->no_wait_shutdown = 1;
|
|
1139 c->ssl->no_send_shutdown = 1;
|
140
|
1140 c->write->error = 1;
|
92
|
1141
|
96
|
1142 ngx_ssl_connection_error(c, sslerr, err, "SSL_write() failed");
|
0
|
1143
|
|
1144 return NGX_ERROR;
|
|
1145 }
|
|
1146
|
|
1147
|
38
|
1148 static void
|
|
1149 ngx_ssl_read_handler(ngx_event_t *rev)
|
22
|
1150 {
|
|
1151 ngx_connection_t *c;
|
|
1152
|
|
1153 c = rev->data;
|
96
|
1154
|
58
|
1155 c->write->handler(c->write);
|
22
|
1156 }
|
|
1157
|
|
1158
|
358
|
1159 void
|
|
1160 ngx_ssl_free_buffer(ngx_connection_t *c)
|
|
1161 {
|
360
|
1162 if (c->ssl->buf && c->ssl->buf->start) {
|
|
1163 if (ngx_pfree(c->pool, c->ssl->buf->start) == NGX_OK) {
|
|
1164 c->ssl->buf->start = NULL;
|
|
1165 }
|
358
|
1166 }
|
|
1167 }
|
|
1168
|
|
1169
|
38
|
1170 ngx_int_t
|
|
1171 ngx_ssl_shutdown(ngx_connection_t *c)
|
0
|
1172 {
|
358
|
1173 int n, sslerr, mode;
|
|
1174 ngx_err_t err;
|
0
|
1175
|
126
|
1176 if (c->timedout) {
|
96
|
1177 mode = SSL_RECEIVED_SHUTDOWN|SSL_SENT_SHUTDOWN;
|
0
|
1178
|
96
|
1179 } else {
|
|
1180 mode = SSL_get_shutdown(c->ssl->connection);
|
22
|
1181
|
96
|
1182 if (c->ssl->no_wait_shutdown) {
|
|
1183 mode |= SSL_RECEIVED_SHUTDOWN;
|
0
|
1184 }
|
|
1185
|
96
|
1186 if (c->ssl->no_send_shutdown) {
|
|
1187 mode |= SSL_SENT_SHUTDOWN;
|
0
|
1188 }
|
|
1189 }
|
|
1190
|
96
|
1191 SSL_set_shutdown(c->ssl->connection, mode);
|
|
1192
|
358
|
1193 ngx_ssl_clear_error(c->log);
|
|
1194
|
|
1195 n = SSL_shutdown(c->ssl->connection);
|
|
1196
|
|
1197 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, c->log, 0, "SSL_shutdown: %d", n);
|
|
1198
|
10
|
1199 sslerr = 0;
|
0
|
1200
|
364
|
1201 /* SSL_shutdown() never returns -1, on error it returns 0 */
|
|
1202
|
|
1203 if (n != 1 && ERR_peek_error()) {
|
92
|
1204 sslerr = SSL_get_error(c->ssl->connection, n);
|
0
|
1205
|
|
1206 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, c->log, 0,
|
|
1207 "SSL_get_error: %d", sslerr);
|
|
1208 }
|
|
1209
|
364
|
1210 if (n == 1 || sslerr == 0 || sslerr == SSL_ERROR_ZERO_RETURN) {
|
358
|
1211 SSL_free(c->ssl->connection);
|
|
1212 c->ssl = NULL;
|
|
1213
|
|
1214 return NGX_OK;
|
|
1215 }
|
|
1216
|
|
1217 if (sslerr == SSL_ERROR_WANT_READ || sslerr == SSL_ERROR_WANT_WRITE) {
|
126
|
1218 c->read->handler = ngx_ssl_shutdown_handler;
|
138
|
1219 c->write->handler = ngx_ssl_shutdown_handler;
|
126
|
1220
|
430
|
1221 if (ngx_handle_read_event(c->read, 0) != NGX_OK) {
|
0
|
1222 return NGX_ERROR;
|
|
1223 }
|
|
1224
|
430
|
1225 if (ngx_handle_write_event(c->write, 0) != NGX_OK) {
|
0
|
1226 return NGX_ERROR;
|
|
1227 }
|
|
1228
|
358
|
1229 if (sslerr == SSL_ERROR_WANT_READ) {
|
138
|
1230 ngx_add_timer(c->read, 30000);
|
|
1231 }
|
|
1232
|
0
|
1233 return NGX_AGAIN;
|
|
1234 }
|
|
1235
|
140
|
1236 err = (sslerr == SSL_ERROR_SYSCALL) ? ngx_errno : 0;
|
|
1237
|
|
1238 ngx_ssl_connection_error(c, sslerr, err, "SSL_shutdown() failed");
|
0
|
1239
|
92
|
1240 SSL_free(c->ssl->connection);
|
|
1241 c->ssl = NULL;
|
|
1242
|
0
|
1243 return NGX_ERROR;
|
|
1244 }
|
|
1245
|
|
1246
|
96
|
1247 static void
|
126
|
1248 ngx_ssl_shutdown_handler(ngx_event_t *ev)
|
|
1249 {
|
|
1250 ngx_connection_t *c;
|
|
1251 ngx_connection_handler_pt handler;
|
|
1252
|
|
1253 c = ev->data;
|
|
1254 handler = c->ssl->handler;
|
|
1255
|
|
1256 if (ev->timedout) {
|
|
1257 c->timedout = 1;
|
|
1258 }
|
|
1259
|
272
|
1260 ngx_log_debug0(NGX_LOG_DEBUG_EVENT, ev->log, 0, "SSL shutdown handler");
|
126
|
1261
|
|
1262 if (ngx_ssl_shutdown(c) == NGX_AGAIN) {
|
|
1263 return;
|
|
1264 }
|
|
1265
|
|
1266 handler(c);
|
|
1267 }
|
|
1268
|
|
1269
|
|
1270 static void
|
96
|
1271 ngx_ssl_connection_error(ngx_connection_t *c, int sslerr, ngx_err_t err,
|
|
1272 char *text)
|
|
1273 {
|
364
|
1274 int n;
|
96
|
1275 ngx_uint_t level;
|
|
1276
|
|
1277 level = NGX_LOG_CRIT;
|
|
1278
|
|
1279 if (sslerr == SSL_ERROR_SYSCALL) {
|
|
1280
|
|
1281 if (err == NGX_ECONNRESET
|
|
1282 || err == NGX_EPIPE
|
|
1283 || err == NGX_ENOTCONN
|
138
|
1284 || err == NGX_ETIMEDOUT
|
96
|
1285 || err == NGX_ECONNREFUSED
|
364
|
1286 || err == NGX_ENETDOWN
|
|
1287 || err == NGX_ENETUNREACH
|
|
1288 || err == NGX_EHOSTDOWN
|
96
|
1289 || err == NGX_EHOSTUNREACH)
|
|
1290 {
|
|
1291 switch (c->log_error) {
|
|
1292
|
|
1293 case NGX_ERROR_IGNORE_ECONNRESET:
|
|
1294 case NGX_ERROR_INFO:
|
|
1295 level = NGX_LOG_INFO;
|
|
1296 break;
|
|
1297
|
|
1298 case NGX_ERROR_ERR:
|
|
1299 level = NGX_LOG_ERR;
|
|
1300 break;
|
|
1301
|
|
1302 default:
|
|
1303 break;
|
|
1304 }
|
|
1305 }
|
364
|
1306
|
|
1307 } else if (sslerr == SSL_ERROR_SSL) {
|
|
1308
|
|
1309 n = ERR_GET_REASON(ERR_peek_error());
|
|
1310
|
|
1311 /* handshake failures */
|
420
|
1312 if (n == SSL_R_DIGEST_CHECK_FAILED /* 149 */
|
564
|
1313 || n == SSL_R_LENGTH_MISMATCH /* 159 */
|
420
|
1314 || n == SSL_R_NO_CIPHERS_PASSED /* 182 */
|
564
|
1315 || n == SSL_R_NO_CIPHERS_SPECIFIED /* 183 */
|
420
|
1316 || n == SSL_R_NO_SHARED_CIPHER /* 193 */
|
564
|
1317 || n == SSL_R_RECORD_LENGTH_MISMATCH /* 213 */
|
420
|
1318 || n == SSL_R_UNEXPECTED_MESSAGE /* 244 */
|
|
1319 || n == SSL_R_UNEXPECTED_RECORD /* 245 */
|
564
|
1320 || n == SSL_R_UNKNOWN_ALERT_TYPE /* 246 */
|
552
|
1321 || n == SSL_R_UNKNOWN_PROTOCOL /* 252 */
|
420
|
1322 || n == SSL_R_WRONG_VERSION_NUMBER /* 267 */
|
|
1323 || n == SSL_R_DECRYPTION_FAILED_OR_BAD_RECORD_MAC /* 281 */
|
364
|
1324 || n == 1000 /* SSL_R_SSLV3_ALERT_CLOSE_NOTIFY */
|
420
|
1325 || n == SSL_R_SSLV3_ALERT_UNEXPECTED_MESSAGE /* 1010 */
|
|
1326 || n == SSL_R_SSLV3_ALERT_BAD_RECORD_MAC /* 1020 */
|
|
1327 || n == SSL_R_TLSV1_ALERT_DECRYPTION_FAILED /* 1021 */
|
|
1328 || n == SSL_R_TLSV1_ALERT_RECORD_OVERFLOW /* 1022 */
|
|
1329 || n == SSL_R_SSLV3_ALERT_DECOMPRESSION_FAILURE /* 1030 */
|
|
1330 || n == SSL_R_SSLV3_ALERT_HANDSHAKE_FAILURE /* 1040 */
|
|
1331 || n == SSL_R_SSLV3_ALERT_NO_CERTIFICATE /* 1041 */
|
|
1332 || n == SSL_R_SSLV3_ALERT_BAD_CERTIFICATE /* 1042 */
|
|
1333 || n == SSL_R_SSLV3_ALERT_UNSUPPORTED_CERTIFICATE /* 1043 */
|
|
1334 || n == SSL_R_SSLV3_ALERT_CERTIFICATE_REVOKED /* 1044 */
|
|
1335 || n == SSL_R_SSLV3_ALERT_CERTIFICATE_EXPIRED /* 1045 */
|
|
1336 || n == SSL_R_SSLV3_ALERT_CERTIFICATE_UNKNOWN /* 1046 */
|
|
1337 || n == SSL_R_SSLV3_ALERT_ILLEGAL_PARAMETER /* 1047 */
|
|
1338 || n == SSL_R_TLSV1_ALERT_UNKNOWN_CA /* 1048 */
|
|
1339 || n == SSL_R_TLSV1_ALERT_ACCESS_DENIED /* 1049 */
|
|
1340 || n == SSL_R_TLSV1_ALERT_DECODE_ERROR /* 1050 */
|
|
1341 || n == SSL_R_TLSV1_ALERT_DECRYPT_ERROR /* 1051 */
|
|
1342 || n == SSL_R_TLSV1_ALERT_EXPORT_RESTRICTION /* 1060 */
|
|
1343 || n == SSL_R_TLSV1_ALERT_PROTOCOL_VERSION /* 1070 */
|
|
1344 || n == SSL_R_TLSV1_ALERT_INSUFFICIENT_SECURITY /* 1071 */
|
|
1345 || n == SSL_R_TLSV1_ALERT_INTERNAL_ERROR /* 1080 */
|
|
1346 || n == SSL_R_TLSV1_ALERT_USER_CANCELLED /* 1090 */
|
|
1347 || n == SSL_R_TLSV1_ALERT_NO_RENEGOTIATION) /* 1100 */
|
364
|
1348 {
|
|
1349 switch (c->log_error) {
|
|
1350
|
|
1351 case NGX_ERROR_IGNORE_ECONNRESET:
|
|
1352 case NGX_ERROR_INFO:
|
|
1353 level = NGX_LOG_INFO;
|
|
1354 break;
|
|
1355
|
|
1356 case NGX_ERROR_ERR:
|
|
1357 level = NGX_LOG_ERR;
|
|
1358 break;
|
|
1359
|
|
1360 default:
|
|
1361 break;
|
|
1362 }
|
|
1363 }
|
96
|
1364 }
|
|
1365
|
|
1366 ngx_ssl_error(level, c->log, err, text);
|
|
1367 }
|
|
1368
|
|
1369
|
358
|
1370 static void
|
|
1371 ngx_ssl_clear_error(ngx_log_t *log)
|
|
1372 {
|
364
|
1373 while (ERR_peek_error()) {
|
358
|
1374 ngx_ssl_error(NGX_LOG_ALERT, log, 0, "ignoring stale global SSL error");
|
|
1375 }
|
364
|
1376
|
|
1377 ERR_clear_error();
|
358
|
1378 }
|
|
1379
|
|
1380
|
132
|
1381 void ngx_cdecl
|
38
|
1382 ngx_ssl_error(ngx_uint_t level, ngx_log_t *log, ngx_err_t err, char *fmt, ...)
|
126
|
1383 {
|
364
|
1384 u_long n;
|
|
1385 va_list args;
|
|
1386 u_char *p, *last;
|
|
1387 u_char errstr[NGX_MAX_CONF_ERRSTR];
|
10
|
1388
|
|
1389 last = errstr + NGX_MAX_CONF_ERRSTR;
|
0
|
1390
|
|
1391 va_start(args, fmt);
|
484
|
1392 p = ngx_vslprintf(errstr, last - 1, fmt, args);
|
0
|
1393 va_end(args);
|
|
1394
|
96
|
1395 p = ngx_cpystrn(p, (u_char *) " (SSL:", last - p);
|
|
1396
|
364
|
1397 for ( ;; ) {
|
132
|
1398
|
|
1399 n = ERR_get_error();
|
|
1400
|
|
1401 if (n == 0) {
|
|
1402 break;
|
|
1403 }
|
96
|
1404
|
364
|
1405 if (p >= last) {
|
|
1406 continue;
|
|
1407 }
|
|
1408
|
96
|
1409 *p++ = ' ';
|
0
|
1410
|
96
|
1411 ERR_error_string_n(n, (char *) p, last - p);
|
|
1412
|
|
1413 while (p < last && *p) {
|
|
1414 p++;
|
|
1415 }
|
|
1416 }
|
0
|
1417
|
|
1418 ngx_log_error(level, log, err, "%s)", errstr);
|
|
1419 }
|
58
|
1420
|
|
1421
|
272
|
1422 ngx_int_t
|
|
1423 ngx_ssl_session_cache(ngx_ssl_t *ssl, ngx_str_t *sess_ctx,
|
|
1424 ssize_t builtin_session_cache, ngx_shm_zone_t *shm_zone, time_t timeout)
|
|
1425 {
|
|
1426 long cache_mode;
|
|
1427
|
358
|
1428 if (builtin_session_cache == NGX_SSL_NO_SCACHE) {
|
|
1429 SSL_CTX_set_session_cache_mode(ssl->ctx, SSL_SESS_CACHE_OFF);
|
|
1430 return NGX_OK;
|
|
1431 }
|
|
1432
|
564
|
1433 SSL_CTX_set_session_id_context(ssl->ctx, sess_ctx->data, sess_ctx->len);
|
|
1434
|
378
|
1435 if (builtin_session_cache == NGX_SSL_NONE_SCACHE) {
|
|
1436
|
|
1437 /*
|
|
1438 * If the server explicitly says that it does not support
|
|
1439 * session reuse (see SSL_SESS_CACHE_OFF above), then
|
|
1440 * Outlook Express fails to upload a sent email to
|
|
1441 * the Sent Items folder on the IMAP server via a separate IMAP
|
|
1442 * connection in the background. Therefore we have a special
|
|
1443 * mode (SSL_SESS_CACHE_SERVER|SSL_SESS_CACHE_NO_INTERNAL_STORE)
|
|
1444 * where the server pretends that it supports session reuse,
|
|
1445 * but it does not actually store any session.
|
|
1446 */
|
|
1447
|
|
1448 SSL_CTX_set_session_cache_mode(ssl->ctx,
|
|
1449 SSL_SESS_CACHE_SERVER
|
|
1450 |SSL_SESS_CACHE_NO_AUTO_CLEAR
|
|
1451 |SSL_SESS_CACHE_NO_INTERNAL_STORE);
|
|
1452
|
|
1453 SSL_CTX_sess_set_cache_size(ssl->ctx, 1);
|
|
1454
|
|
1455 return NGX_OK;
|
|
1456 }
|
|
1457
|
272
|
1458 cache_mode = SSL_SESS_CACHE_SERVER;
|
|
1459
|
|
1460 if (shm_zone && builtin_session_cache == NGX_SSL_NO_BUILTIN_SCACHE) {
|
|
1461 cache_mode |= SSL_SESS_CACHE_NO_INTERNAL;
|
|
1462 }
|
|
1463
|
|
1464 SSL_CTX_set_session_cache_mode(ssl->ctx, cache_mode);
|
|
1465
|
|
1466 if (builtin_session_cache != NGX_SSL_NO_BUILTIN_SCACHE) {
|
|
1467
|
|
1468 if (builtin_session_cache != NGX_SSL_DFLT_BUILTIN_SCACHE) {
|
|
1469 SSL_CTX_sess_set_cache_size(ssl->ctx, builtin_session_cache);
|
|
1470 }
|
|
1471 }
|
|
1472
|
480
|
1473 SSL_CTX_set_timeout(ssl->ctx, (long) timeout);
|
274
|
1474
|
272
|
1475 if (shm_zone) {
|
|
1476 shm_zone->init = ngx_ssl_session_cache_init;
|
|
1477
|
|
1478 SSL_CTX_sess_set_new_cb(ssl->ctx, ngx_ssl_new_session);
|
|
1479 SSL_CTX_sess_set_get_cb(ssl->ctx, ngx_ssl_get_cached_session);
|
|
1480 SSL_CTX_sess_set_remove_cb(ssl->ctx, ngx_ssl_remove_session);
|
|
1481
|
|
1482 if (SSL_CTX_set_ex_data(ssl->ctx, ngx_ssl_session_cache_index, shm_zone)
|
|
1483 == 0)
|
|
1484 {
|
|
1485 ngx_ssl_error(NGX_LOG_EMERG, ssl->log, 0,
|
|
1486 "SSL_CTX_set_ex_data() failed");
|
|
1487 return NGX_ERROR;
|
|
1488 }
|
|
1489 }
|
|
1490
|
|
1491 return NGX_OK;
|
|
1492 }
|
|
1493
|
|
1494
|
|
1495 static ngx_int_t
|
|
1496 ngx_ssl_session_cache_init(ngx_shm_zone_t *shm_zone, void *data)
|
|
1497 {
|
468
|
1498 size_t len;
|
272
|
1499 ngx_slab_pool_t *shpool;
|
|
1500 ngx_ssl_session_cache_t *cache;
|
|
1501
|
|
1502 if (data) {
|
|
1503 shm_zone->data = data;
|
|
1504 return NGX_OK;
|
|
1505 }
|
|
1506
|
480
|
1507 if (shm_zone->shm.exists) {
|
|
1508 shm_zone->data = data;
|
|
1509 return NGX_OK;
|
|
1510 }
|
|
1511
|
272
|
1512 shpool = (ngx_slab_pool_t *) shm_zone->shm.addr;
|
|
1513
|
|
1514 cache = ngx_slab_alloc(shpool, sizeof(ngx_ssl_session_cache_t));
|
|
1515 if (cache == NULL) {
|
|
1516 return NGX_ERROR;
|
|
1517 }
|
|
1518
|
480
|
1519 shpool->data = cache;
|
|
1520 shm_zone->data = cache;
|
|
1521
|
358
|
1522 ngx_rbtree_init(&cache->session_rbtree, &cache->sentinel,
|
356
|
1523 ngx_ssl_session_rbtree_insert_value);
|
272
|
1524
|
358
|
1525 ngx_queue_init(&cache->expire_queue);
|
|
1526
|
480
|
1527 len = sizeof(" in SSL session shared cache \"\"") + shm_zone->shm.name.len;
|
468
|
1528
|
|
1529 shpool->log_ctx = ngx_slab_alloc(shpool, len);
|
|
1530 if (shpool->log_ctx == NULL) {
|
|
1531 return NGX_ERROR;
|
|
1532 }
|
|
1533
|
|
1534 ngx_sprintf(shpool->log_ctx, " in SSL session shared cache \"%V\"%Z",
|
480
|
1535 &shm_zone->shm.name);
|
272
|
1536
|
|
1537 return NGX_OK;
|
|
1538 }
|
|
1539
|
|
1540
|
|
1541 /*
|
274
|
1542 * The length of the session id is 16 bytes for SSLv2 sessions and
|
|
1543 * between 1 and 32 bytes for SSLv3/TLSv1, typically 32 bytes.
|
|
1544 * It seems that the typical length of the external ASN1 representation
|
|
1545 * of a session is 118 or 119 bytes for SSLv3/TSLv1.
|
|
1546 *
|
|
1547 * Thus on 32-bit platforms we allocate separately an rbtree node,
|
|
1548 * a session id, and an ASN1 representation, they take accordingly
|
|
1549 * 64, 32, and 128 bytes.
|
|
1550 *
|
|
1551 * On 64-bit platforms we allocate separately an rbtree node + session_id,
|
|
1552 * and an ASN1 representation, they take accordingly 128 and 128 bytes.
|
|
1553 *
|
272
|
1554 * OpenSSL's i2d_SSL_SESSION() and d2i_SSL_SESSION are slow,
|
|
1555 * so they are outside the code locked by shared pool mutex
|
|
1556 */
|
|
1557
|
|
1558 static int
|
|
1559 ngx_ssl_new_session(ngx_ssl_conn_t *ssl_conn, ngx_ssl_session_t *sess)
|
|
1560 {
|
|
1561 int len;
|
274
|
1562 u_char *p, *id, *cached_sess;
|
272
|
1563 uint32_t hash;
|
|
1564 SSL_CTX *ssl_ctx;
|
|
1565 ngx_shm_zone_t *shm_zone;
|
|
1566 ngx_connection_t *c;
|
|
1567 ngx_slab_pool_t *shpool;
|
|
1568 ngx_ssl_sess_id_t *sess_id;
|
|
1569 ngx_ssl_session_cache_t *cache;
|
|
1570 u_char buf[NGX_SSL_MAX_SESSION_SIZE];
|
|
1571
|
|
1572 len = i2d_SSL_SESSION(sess, NULL);
|
|
1573
|
|
1574 /* do not cache too big session */
|
|
1575
|
|
1576 if (len > (int) NGX_SSL_MAX_SESSION_SIZE) {
|
|
1577 return 0;
|
|
1578 }
|
|
1579
|
|
1580 p = buf;
|
|
1581 i2d_SSL_SESSION(sess, &p);
|
|
1582
|
|
1583 c = ngx_ssl_get_connection(ssl_conn);
|
|
1584
|
|
1585 ssl_ctx = SSL_get_SSL_CTX(ssl_conn);
|
|
1586 shm_zone = SSL_CTX_get_ex_data(ssl_ctx, ngx_ssl_session_cache_index);
|
|
1587
|
|
1588 cache = shm_zone->data;
|
|
1589 shpool = (ngx_slab_pool_t *) shm_zone->shm.addr;
|
|
1590
|
|
1591 ngx_shmtx_lock(&shpool->mutex);
|
|
1592
|
|
1593 /* drop one or two expired sessions */
|
|
1594 ngx_ssl_expire_sessions(cache, shpool, 1);
|
|
1595
|
274
|
1596 cached_sess = ngx_slab_alloc_locked(shpool, len);
|
272
|
1597
|
|
1598 if (cached_sess == NULL) {
|
|
1599
|
|
1600 /* drop the oldest non-expired session and try once more */
|
|
1601
|
|
1602 ngx_ssl_expire_sessions(cache, shpool, 0);
|
|
1603
|
274
|
1604 cached_sess = ngx_slab_alloc_locked(shpool, len);
|
272
|
1605
|
|
1606 if (cached_sess == NULL) {
|
274
|
1607 sess_id = NULL;
|
272
|
1608 goto failed;
|
|
1609 }
|
|
1610 }
|
|
1611
|
|
1612 sess_id = ngx_slab_alloc_locked(shpool, sizeof(ngx_ssl_sess_id_t));
|
|
1613 if (sess_id == NULL) {
|
|
1614 goto failed;
|
|
1615 }
|
|
1616
|
274
|
1617 #if (NGX_PTR_SIZE == 8)
|
|
1618
|
|
1619 id = sess_id->sess_id;
|
|
1620
|
|
1621 #else
|
|
1622
|
|
1623 id = ngx_slab_alloc_locked(shpool, sess->session_id_length);
|
|
1624 if (id == NULL) {
|
|
1625 goto failed;
|
|
1626 }
|
|
1627
|
|
1628 #endif
|
|
1629
|
|
1630 ngx_memcpy(cached_sess, buf, len);
|
272
|
1631
|
|
1632 ngx_memcpy(id, sess->session_id, sess->session_id_length);
|
|
1633
|
|
1634 hash = ngx_crc32_short(sess->session_id, sess->session_id_length);
|
|
1635
|
|
1636 ngx_log_debug3(NGX_LOG_DEBUG_EVENT, c->log, 0,
|
530
|
1637 "ssl new session: %08XD:%d:%d",
|
272
|
1638 hash, sess->session_id_length, len);
|
|
1639
|
|
1640 sess_id->node.key = hash;
|
|
1641 sess_id->node.data = (u_char) sess->session_id_length;
|
|
1642 sess_id->id = id;
|
|
1643 sess_id->len = len;
|
|
1644 sess_id->session = cached_sess;
|
|
1645
|
358
|
1646 sess_id->expire = ngx_time() + SSL_CTX_get_timeout(ssl_ctx);
|
|
1647
|
|
1648 ngx_queue_insert_head(&cache->expire_queue, &sess_id->queue);
|
|
1649
|
|
1650 ngx_rbtree_insert(&cache->session_rbtree, &sess_id->node);
|
272
|
1651
|
|
1652 ngx_shmtx_unlock(&shpool->mutex);
|
|
1653
|
|
1654 return 0;
|
|
1655
|
|
1656 failed:
|
|
1657
|
|
1658 if (cached_sess) {
|
|
1659 ngx_slab_free_locked(shpool, cached_sess);
|
|
1660 }
|
|
1661
|
274
|
1662 if (sess_id) {
|
|
1663 ngx_slab_free_locked(shpool, sess_id);
|
272
|
1664 }
|
|
1665
|
|
1666 ngx_shmtx_unlock(&shpool->mutex);
|
|
1667
|
|
1668 ngx_log_error(NGX_LOG_ALERT, c->log, 0,
|
|
1669 "could not add new SSL session to the session cache");
|
|
1670
|
|
1671 return 0;
|
|
1672 }
|
|
1673
|
|
1674
|
|
1675 static ngx_ssl_session_t *
|
|
1676 ngx_ssl_get_cached_session(ngx_ssl_conn_t *ssl_conn, u_char *id, int len,
|
|
1677 int *copy)
|
|
1678 {
|
|
1679 #if OPENSSL_VERSION_NUMBER >= 0x0090707fL
|
|
1680 const
|
|
1681 #endif
|
|
1682 u_char *p;
|
|
1683 uint32_t hash;
|
274
|
1684 ngx_int_t rc;
|
272
|
1685 ngx_shm_zone_t *shm_zone;
|
|
1686 ngx_slab_pool_t *shpool;
|
|
1687 ngx_connection_t *c;
|
|
1688 ngx_rbtree_node_t *node, *sentinel;
|
|
1689 ngx_ssl_session_t *sess;
|
|
1690 ngx_ssl_sess_id_t *sess_id;
|
|
1691 ngx_ssl_session_cache_t *cache;
|
|
1692 u_char buf[NGX_SSL_MAX_SESSION_SIZE];
|
|
1693
|
|
1694 c = ngx_ssl_get_connection(ssl_conn);
|
|
1695
|
274
|
1696 hash = ngx_crc32_short(id, (size_t) len);
|
272
|
1697 *copy = 0;
|
|
1698
|
|
1699 ngx_log_debug2(NGX_LOG_DEBUG_EVENT, c->log, 0,
|
530
|
1700 "ssl get session: %08XD:%d", hash, len);
|
272
|
1701
|
|
1702 shm_zone = SSL_CTX_get_ex_data(SSL_get_SSL_CTX(ssl_conn),
|
|
1703 ngx_ssl_session_cache_index);
|
|
1704
|
|
1705 cache = shm_zone->data;
|
|
1706
|
|
1707 sess = NULL;
|
|
1708
|
|
1709 shpool = (ngx_slab_pool_t *) shm_zone->shm.addr;
|
|
1710
|
|
1711 ngx_shmtx_lock(&shpool->mutex);
|
|
1712
|
358
|
1713 node = cache->session_rbtree.root;
|
|
1714 sentinel = cache->session_rbtree.sentinel;
|
272
|
1715
|
|
1716 while (node != sentinel) {
|
|
1717
|
|
1718 if (hash < node->key) {
|
|
1719 node = node->left;
|
|
1720 continue;
|
|
1721 }
|
|
1722
|
|
1723 if (hash > node->key) {
|
|
1724 node = node->right;
|
|
1725 continue;
|
|
1726 }
|
|
1727
|
274
|
1728 /* hash == node->key */
|
|
1729
|
|
1730 do {
|
272
|
1731 sess_id = (ngx_ssl_sess_id_t *) node;
|
|
1732
|
274
|
1733 rc = ngx_memn2cmp(id, sess_id->id,
|
|
1734 (size_t) len, (size_t) node->data);
|
|
1735 if (rc == 0) {
|
272
|
1736
|
358
|
1737 if (sess_id->expire > ngx_time()) {
|
274
|
1738 ngx_memcpy(buf, sess_id->session, sess_id->len);
|
272
|
1739
|
|
1740 ngx_shmtx_unlock(&shpool->mutex);
|
|
1741
|
|
1742 p = buf;
|
|
1743 sess = d2i_SSL_SESSION(NULL, &p, sess_id->len);
|
|
1744
|
|
1745 return sess;
|
|
1746 }
|
|
1747
|
358
|
1748 ngx_queue_remove(&sess_id->queue);
|
|
1749
|
|
1750 ngx_rbtree_delete(&cache->session_rbtree, node);
|
272
|
1751
|
274
|
1752 ngx_slab_free_locked(shpool, sess_id->session);
|
|
1753 #if (NGX_PTR_SIZE == 4)
|
272
|
1754 ngx_slab_free_locked(shpool, sess_id->id);
|
274
|
1755 #endif
|
272
|
1756 ngx_slab_free_locked(shpool, sess_id);
|
|
1757
|
|
1758 sess = NULL;
|
|
1759
|
274
|
1760 goto done;
|
272
|
1761 }
|
274
|
1762
|
|
1763 node = (rc < 0) ? node->left : node->right;
|
|
1764
|
|
1765 } while (node != sentinel && hash == node->key);
|
|
1766
|
|
1767 break;
|
272
|
1768 }
|
|
1769
|
274
|
1770 done:
|
|
1771
|
272
|
1772 ngx_shmtx_unlock(&shpool->mutex);
|
|
1773
|
|
1774 return sess;
|
|
1775 }
|
|
1776
|
|
1777
|
366
|
1778 void
|
|
1779 ngx_ssl_remove_cached_session(SSL_CTX *ssl, ngx_ssl_session_t *sess)
|
|
1780 {
|
|
1781 SSL_CTX_remove_session(ssl, sess);
|
|
1782
|
|
1783 ngx_ssl_remove_session(ssl, sess);
|
|
1784 }
|
|
1785
|
|
1786
|
272
|
1787 static void
|
|
1788 ngx_ssl_remove_session(SSL_CTX *ssl, ngx_ssl_session_t *sess)
|
|
1789 {
|
274
|
1790 size_t len;
|
|
1791 u_char *id;
|
272
|
1792 uint32_t hash;
|
274
|
1793 ngx_int_t rc;
|
272
|
1794 ngx_shm_zone_t *shm_zone;
|
|
1795 ngx_slab_pool_t *shpool;
|
|
1796 ngx_rbtree_node_t *node, *sentinel;
|
|
1797 ngx_ssl_sess_id_t *sess_id;
|
|
1798 ngx_ssl_session_cache_t *cache;
|
|
1799
|
|
1800 shm_zone = SSL_CTX_get_ex_data(ssl, ngx_ssl_session_cache_index);
|
|
1801
|
366
|
1802 if (shm_zone == NULL) {
|
|
1803 return;
|
|
1804 }
|
|
1805
|
272
|
1806 cache = shm_zone->data;
|
|
1807
|
|
1808 id = sess->session_id;
|
274
|
1809 len = (size_t) sess->session_id_length;
|
|
1810
|
|
1811 hash = ngx_crc32_short(id, len);
|
272
|
1812
|
|
1813 ngx_log_debug2(NGX_LOG_DEBUG_EVENT, ngx_cycle->log, 0,
|
530
|
1814 "ssl remove session: %08XD:%uz", hash, len);
|
272
|
1815
|
|
1816 shpool = (ngx_slab_pool_t *) shm_zone->shm.addr;
|
|
1817
|
|
1818 ngx_shmtx_lock(&shpool->mutex);
|
|
1819
|
358
|
1820 node = cache->session_rbtree.root;
|
|
1821 sentinel = cache->session_rbtree.sentinel;
|
272
|
1822
|
|
1823 while (node != sentinel) {
|
|
1824
|
|
1825 if (hash < node->key) {
|
|
1826 node = node->left;
|
|
1827 continue;
|
|
1828 }
|
|
1829
|
|
1830 if (hash > node->key) {
|
|
1831 node = node->right;
|
|
1832 continue;
|
|
1833 }
|
|
1834
|
274
|
1835 /* hash == node->key */
|
|
1836
|
|
1837 do {
|
272
|
1838 sess_id = (ngx_ssl_sess_id_t *) node;
|
|
1839
|
274
|
1840 rc = ngx_memn2cmp(id, sess_id->id, len, (size_t) node->data);
|
|
1841
|
|
1842 if (rc == 0) {
|
358
|
1843
|
|
1844 ngx_queue_remove(&sess_id->queue);
|
|
1845
|
|
1846 ngx_rbtree_delete(&cache->session_rbtree, node);
|
272
|
1847
|
274
|
1848 ngx_slab_free_locked(shpool, sess_id->session);
|
|
1849 #if (NGX_PTR_SIZE == 4)
|
272
|
1850 ngx_slab_free_locked(shpool, sess_id->id);
|
274
|
1851 #endif
|
272
|
1852 ngx_slab_free_locked(shpool, sess_id);
|
|
1853
|
274
|
1854 goto done;
|
272
|
1855 }
|
274
|
1856
|
|
1857 node = (rc < 0) ? node->left : node->right;
|
|
1858
|
|
1859 } while (node != sentinel && hash == node->key);
|
|
1860
|
|
1861 break;
|
272
|
1862 }
|
|
1863
|
274
|
1864 done:
|
|
1865
|
272
|
1866 ngx_shmtx_unlock(&shpool->mutex);
|
|
1867 }
|
|
1868
|
|
1869
|
|
1870 static void
|
|
1871 ngx_ssl_expire_sessions(ngx_ssl_session_cache_t *cache,
|
|
1872 ngx_slab_pool_t *shpool, ngx_uint_t n)
|
|
1873 {
|
358
|
1874 time_t now;
|
|
1875 ngx_queue_t *q;
|
274
|
1876 ngx_ssl_sess_id_t *sess_id;
|
272
|
1877
|
358
|
1878 now = ngx_time();
|
272
|
1879
|
|
1880 while (n < 3) {
|
|
1881
|
358
|
1882 if (ngx_queue_empty(&cache->expire_queue)) {
|
272
|
1883 return;
|
|
1884 }
|
|
1885
|
358
|
1886 q = ngx_queue_last(&cache->expire_queue);
|
|
1887
|
|
1888 sess_id = ngx_queue_data(q, ngx_ssl_sess_id_t, queue);
|
|
1889
|
|
1890 if (n++ != 0 && sess_id->expire > now) {
|
332
|
1891 return;
|
272
|
1892 }
|
|
1893
|
358
|
1894 ngx_queue_remove(q);
|
272
|
1895
|
|
1896 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, ngx_cycle->log, 0,
|
|
1897 "expire session: %08Xi", sess_id->node.key);
|
|
1898
|
358
|
1899 ngx_rbtree_delete(&cache->session_rbtree, &sess_id->node);
|
|
1900
|
274
|
1901 ngx_slab_free_locked(shpool, sess_id->session);
|
|
1902 #if (NGX_PTR_SIZE == 4)
|
272
|
1903 ngx_slab_free_locked(shpool, sess_id->id);
|
274
|
1904 #endif
|
272
|
1905 ngx_slab_free_locked(shpool, sess_id);
|
|
1906 }
|
|
1907 }
|
|
1908
|
|
1909
|
274
|
1910 static void
|
|
1911 ngx_ssl_session_rbtree_insert_value(ngx_rbtree_node_t *temp,
|
|
1912 ngx_rbtree_node_t *node, ngx_rbtree_node_t *sentinel)
|
|
1913 {
|
356
|
1914 ngx_rbtree_node_t **p;
|
|
1915 ngx_ssl_sess_id_t *sess_id, *sess_id_temp;
|
274
|
1916
|
|
1917 for ( ;; ) {
|
|
1918
|
|
1919 if (node->key < temp->key) {
|
|
1920
|
356
|
1921 p = &temp->left;
|
274
|
1922
|
|
1923 } else if (node->key > temp->key) {
|
|
1924
|
356
|
1925 p = &temp->right;
|
274
|
1926
|
|
1927 } else { /* node->key == temp->key */
|
|
1928
|
|
1929 sess_id = (ngx_ssl_sess_id_t *) node;
|
|
1930 sess_id_temp = (ngx_ssl_sess_id_t *) temp;
|
|
1931
|
356
|
1932 p = (ngx_memn2cmp(sess_id->id, sess_id_temp->id,
|
|
1933 (size_t) node->data, (size_t) temp->data)
|
|
1934 < 0) ? &temp->left : &temp->right;
|
274
|
1935 }
|
356
|
1936
|
|
1937 if (*p == sentinel) {
|
|
1938 break;
|
|
1939 }
|
|
1940
|
|
1941 temp = *p;
|
274
|
1942 }
|
|
1943
|
356
|
1944 *p = node;
|
274
|
1945 node->parent = temp;
|
|
1946 node->left = sentinel;
|
|
1947 node->right = sentinel;
|
|
1948 ngx_rbt_red(node);
|
276
|
1949 }
|
274
|
1950
|
|
1951
|
58
|
1952 void
|
|
1953 ngx_ssl_cleanup_ctx(void *data)
|
|
1954 {
|
138
|
1955 ngx_ssl_t *ssl = data;
|
58
|
1956
|
138
|
1957 SSL_CTX_free(ssl->ctx);
|
58
|
1958 }
|
90
|
1959
|
|
1960
|
220
|
1961 ngx_int_t
|
|
1962 ngx_ssl_get_protocol(ngx_connection_t *c, ngx_pool_t *pool, ngx_str_t *s)
|
160
|
1963 {
|
220
|
1964 s->data = (u_char *) SSL_get_version(c->ssl->connection);
|
|
1965 return NGX_OK;
|
160
|
1966 }
|
|
1967
|
|
1968
|
220
|
1969 ngx_int_t
|
|
1970 ngx_ssl_get_cipher_name(ngx_connection_t *c, ngx_pool_t *pool, ngx_str_t *s)
|
160
|
1971 {
|
220
|
1972 s->data = (u_char *) SSL_get_cipher_name(c->ssl->connection);
|
|
1973 return NGX_OK;
|
160
|
1974 }
|
|
1975
|
|
1976
|
196
|
1977 ngx_int_t
|
530
|
1978 ngx_ssl_get_session_id(ngx_connection_t *c, ngx_pool_t *pool, ngx_str_t *s)
|
|
1979 {
|
|
1980 int len;
|
|
1981 u_char *p, *buf;
|
|
1982 SSL_SESSION *sess;
|
|
1983
|
|
1984 sess = SSL_get0_session(c->ssl->connection);
|
|
1985
|
|
1986 len = i2d_SSL_SESSION(sess, NULL);
|
|
1987
|
|
1988 buf = ngx_alloc(len, c->log);
|
|
1989 if (buf == NULL) {
|
|
1990 return NGX_ERROR;
|
|
1991 }
|
|
1992
|
|
1993 s->len = 2 * len;
|
|
1994 s->data = ngx_pnalloc(pool, 2 * len);
|
|
1995 if (s->data == NULL) {
|
|
1996 ngx_free(buf);
|
|
1997 return NGX_ERROR;
|
|
1998 }
|
|
1999
|
|
2000 p = buf;
|
|
2001 i2d_SSL_SESSION(sess, &p);
|
|
2002
|
|
2003 ngx_hex_dump(s->data, buf, len);
|
|
2004
|
|
2005 ngx_free(buf);
|
|
2006
|
|
2007 return NGX_OK;
|
|
2008 }
|
|
2009
|
|
2010
|
|
2011 ngx_int_t
|
390
|
2012 ngx_ssl_get_raw_certificate(ngx_connection_t *c, ngx_pool_t *pool, ngx_str_t *s)
|
380
|
2013 {
|
|
2014 size_t len;
|
|
2015 BIO *bio;
|
|
2016 X509 *cert;
|
|
2017
|
|
2018 s->len = 0;
|
|
2019
|
|
2020 cert = SSL_get_peer_certificate(c->ssl->connection);
|
|
2021 if (cert == NULL) {
|
|
2022 return NGX_OK;
|
|
2023 }
|
|
2024
|
|
2025 bio = BIO_new(BIO_s_mem());
|
|
2026 if (bio == NULL) {
|
|
2027 ngx_ssl_error(NGX_LOG_ALERT, c->log, 0, "BIO_new() failed");
|
|
2028 X509_free(cert);
|
|
2029 return NGX_ERROR;
|
|
2030 }
|
|
2031
|
|
2032 if (PEM_write_bio_X509(bio, cert) == 0) {
|
|
2033 ngx_ssl_error(NGX_LOG_ALERT, c->log, 0, "PEM_write_bio_X509() failed");
|
|
2034 goto failed;
|
|
2035 }
|
|
2036
|
|
2037 len = BIO_pending(bio);
|
|
2038 s->len = len;
|
|
2039
|
382
|
2040 s->data = ngx_pnalloc(pool, len);
|
380
|
2041 if (s->data == NULL) {
|
|
2042 goto failed;
|
|
2043 }
|
|
2044
|
|
2045 BIO_read(bio, s->data, len);
|
|
2046
|
|
2047 BIO_free(bio);
|
|
2048 X509_free(cert);
|
|
2049
|
|
2050 return NGX_OK;
|
|
2051
|
|
2052 failed:
|
|
2053
|
|
2054 BIO_free(bio);
|
|
2055 X509_free(cert);
|
|
2056
|
|
2057 return NGX_ERROR;
|
|
2058 }
|
|
2059
|
|
2060
|
|
2061 ngx_int_t
|
390
|
2062 ngx_ssl_get_certificate(ngx_connection_t *c, ngx_pool_t *pool, ngx_str_t *s)
|
|
2063 {
|
|
2064 u_char *p;
|
|
2065 size_t len;
|
|
2066 ngx_uint_t i;
|
|
2067 ngx_str_t cert;
|
|
2068
|
|
2069 if (ngx_ssl_get_raw_certificate(c, pool, &cert) != NGX_OK) {
|
|
2070 return NGX_ERROR;
|
|
2071 }
|
|
2072
|
|
2073 if (cert.len == 0) {
|
|
2074 s->len = 0;
|
|
2075 return NGX_OK;
|
|
2076 }
|
|
2077
|
|
2078 len = cert.len - 1;
|
|
2079
|
|
2080 for (i = 0; i < cert.len - 1; i++) {
|
|
2081 if (cert.data[i] == LF) {
|
|
2082 len++;
|
|
2083 }
|
|
2084 }
|
|
2085
|
|
2086 s->len = len;
|
|
2087 s->data = ngx_pnalloc(pool, len);
|
|
2088 if (s->data == NULL) {
|
|
2089 return NGX_ERROR;
|
|
2090 }
|
|
2091
|
|
2092 p = s->data;
|
|
2093
|
510
|
2094 for (i = 0; i < cert.len - 1; i++) {
|
390
|
2095 *p++ = cert.data[i];
|
|
2096 if (cert.data[i] == LF) {
|
|
2097 *p++ = '\t';
|
|
2098 }
|
|
2099 }
|
|
2100
|
|
2101 return NGX_OK;
|
|
2102 }
|
|
2103
|
|
2104
|
|
2105 ngx_int_t
|
196
|
2106 ngx_ssl_get_subject_dn(ngx_connection_t *c, ngx_pool_t *pool, ngx_str_t *s)
|
|
2107 {
|
|
2108 char *p;
|
|
2109 size_t len;
|
|
2110 X509 *cert;
|
|
2111 X509_NAME *name;
|
|
2112
|
|
2113 s->len = 0;
|
|
2114
|
|
2115 cert = SSL_get_peer_certificate(c->ssl->connection);
|
|
2116 if (cert == NULL) {
|
|
2117 return NGX_OK;
|
|
2118 }
|
|
2119
|
|
2120 name = X509_get_subject_name(cert);
|
|
2121 if (name == NULL) {
|
372
|
2122 X509_free(cert);
|
196
|
2123 return NGX_ERROR;
|
|
2124 }
|
|
2125
|
|
2126 p = X509_NAME_oneline(name, NULL, 0);
|
|
2127
|
|
2128 for (len = 0; p[len]; len++) { /* void */ }
|
|
2129
|
|
2130 s->len = len;
|
382
|
2131 s->data = ngx_pnalloc(pool, len);
|
196
|
2132 if (s->data == NULL) {
|
|
2133 OPENSSL_free(p);
|
372
|
2134 X509_free(cert);
|
196
|
2135 return NGX_ERROR;
|
|
2136 }
|
|
2137
|
|
2138 ngx_memcpy(s->data, p, len);
|
|
2139
|
|
2140 OPENSSL_free(p);
|
372
|
2141 X509_free(cert);
|
196
|
2142
|
|
2143 return NGX_OK;
|
|
2144 }
|
|
2145
|
|
2146
|
|
2147 ngx_int_t
|
|
2148 ngx_ssl_get_issuer_dn(ngx_connection_t *c, ngx_pool_t *pool, ngx_str_t *s)
|
|
2149 {
|
|
2150 char *p;
|
|
2151 size_t len;
|
|
2152 X509 *cert;
|
|
2153 X509_NAME *name;
|
|
2154
|
|
2155 s->len = 0;
|
|
2156
|
|
2157 cert = SSL_get_peer_certificate(c->ssl->connection);
|
|
2158 if (cert == NULL) {
|
|
2159 return NGX_OK;
|
|
2160 }
|
|
2161
|
|
2162 name = X509_get_issuer_name(cert);
|
|
2163 if (name == NULL) {
|
372
|
2164 X509_free(cert);
|
196
|
2165 return NGX_ERROR;
|
|
2166 }
|
|
2167
|
|
2168 p = X509_NAME_oneline(name, NULL, 0);
|
|
2169
|
|
2170 for (len = 0; p[len]; len++) { /* void */ }
|
|
2171
|
|
2172 s->len = len;
|
382
|
2173 s->data = ngx_pnalloc(pool, len);
|
196
|
2174 if (s->data == NULL) {
|
|
2175 OPENSSL_free(p);
|
372
|
2176 X509_free(cert);
|
196
|
2177 return NGX_ERROR;
|
|
2178 }
|
|
2179
|
|
2180 ngx_memcpy(s->data, p, len);
|
|
2181
|
|
2182 OPENSSL_free(p);
|
372
|
2183 X509_free(cert);
|
196
|
2184
|
|
2185 return NGX_OK;
|
|
2186 }
|
|
2187
|
|
2188
|
220
|
2189 ngx_int_t
|
|
2190 ngx_ssl_get_serial_number(ngx_connection_t *c, ngx_pool_t *pool, ngx_str_t *s)
|
|
2191 {
|
|
2192 size_t len;
|
|
2193 X509 *cert;
|
|
2194 BIO *bio;
|
|
2195
|
|
2196 s->len = 0;
|
|
2197
|
|
2198 cert = SSL_get_peer_certificate(c->ssl->connection);
|
|
2199 if (cert == NULL) {
|
|
2200 return NGX_OK;
|
|
2201 }
|
|
2202
|
|
2203 bio = BIO_new(BIO_s_mem());
|
|
2204 if (bio == NULL) {
|
372
|
2205 X509_free(cert);
|
220
|
2206 return NGX_ERROR;
|
|
2207 }
|
|
2208
|
|
2209 i2a_ASN1_INTEGER(bio, X509_get_serialNumber(cert));
|
|
2210 len = BIO_pending(bio);
|
|
2211
|
|
2212 s->len = len;
|
382
|
2213 s->data = ngx_pnalloc(pool, len);
|
220
|
2214 if (s->data == NULL) {
|
|
2215 BIO_free(bio);
|
372
|
2216 X509_free(cert);
|
220
|
2217 return NGX_ERROR;
|
|
2218 }
|
|
2219
|
|
2220 BIO_read(bio, s->data, len);
|
|
2221 BIO_free(bio);
|
372
|
2222 X509_free(cert);
|
220
|
2223
|
|
2224 return NGX_OK;
|
|
2225 }
|
|
2226
|
|
2227
|
510
|
2228 ngx_int_t
|
|
2229 ngx_ssl_get_client_verify(ngx_connection_t *c, ngx_pool_t *pool, ngx_str_t *s)
|
|
2230 {
|
|
2231 X509 *cert;
|
|
2232
|
|
2233 if (SSL_get_verify_result(c->ssl->connection) != X509_V_OK) {
|
|
2234 s->len = sizeof("FAILED") - 1;
|
|
2235 s->data = (u_char *) "FAILED";
|
|
2236
|
|
2237 return NGX_OK;
|
|
2238 }
|
|
2239
|
|
2240 cert = SSL_get_peer_certificate(c->ssl->connection);
|
|
2241
|
|
2242 if (cert) {
|
|
2243 s->len = sizeof("SUCCESS") - 1;
|
|
2244 s->data = (u_char *) "SUCCESS";
|
|
2245
|
|
2246 } else {
|
|
2247 s->len = sizeof("NONE") - 1;
|
|
2248 s->data = (u_char *) "NONE";
|
|
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) {
|
496
|
2264 return NULL;
|
90
|
2265 }
|
126
|
2266
|
90
|
2267 /*
|
|
2268 * set by ngx_pcalloc():
|
126
|
2269 *
|
446
|
2270 * oscf->engine = 0;
|
126
|
2271 */
|
90
|
2272
|
|
2273 return oscf;
|
|
2274 }
|
|
2275
|
|
2276
|
|
2277 static char *
|
446
|
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
|
446
|
2282 ENGINE *engine;
|
|
2283 ngx_str_t *value;
|
|
2284
|
|
2285 if (oscf->engine) {
|
|
2286 return "is duplicate";
|
90
|
2287 }
|
126
|
2288
|
446
|
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) {
|
446
|
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) {
|
446
|
2302 ngx_ssl_error(NGX_LOG_WARN, cf->log, 0,
|
90
|
2303 "ENGINE_set_default(\"%V\", ENGINE_METHOD_ALL) failed",
|
446
|
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;
|
446
|
2314 }
|
120
|
2315
|
|
2316
|
|
2317 static void
|
|
2318 ngx_openssl_exit(ngx_cycle_t *cycle)
|
|
2319 {
|
564
|
2320 EVP_cleanup();
|
120
|
2321 ENGINE_cleanup();
|
|
2322 }
|