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