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;
|
0
|
1208
|
96
|
1209 } else {
|
|
1210 mode = SSL_get_shutdown(c->ssl->connection);
|
22
|
1211
|
96
|
1212 if (c->ssl->no_wait_shutdown) {
|
|
1213 mode |= SSL_RECEIVED_SHUTDOWN;
|
0
|
1214 }
|
|
1215
|
96
|
1216 if (c->ssl->no_send_shutdown) {
|
|
1217 mode |= SSL_SENT_SHUTDOWN;
|
0
|
1218 }
|
|
1219 }
|
|
1220
|
96
|
1221 SSL_set_shutdown(c->ssl->connection, mode);
|
|
1222
|
358
|
1223 ngx_ssl_clear_error(c->log);
|
|
1224
|
|
1225 n = SSL_shutdown(c->ssl->connection);
|
|
1226
|
|
1227 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, c->log, 0, "SSL_shutdown: %d", n);
|
|
1228
|
10
|
1229 sslerr = 0;
|
0
|
1230
|
364
|
1231 /* SSL_shutdown() never returns -1, on error it returns 0 */
|
|
1232
|
|
1233 if (n != 1 && ERR_peek_error()) {
|
92
|
1234 sslerr = SSL_get_error(c->ssl->connection, n);
|
0
|
1235
|
|
1236 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, c->log, 0,
|
|
1237 "SSL_get_error: %d", sslerr);
|
|
1238 }
|
|
1239
|
364
|
1240 if (n == 1 || sslerr == 0 || sslerr == SSL_ERROR_ZERO_RETURN) {
|
358
|
1241 SSL_free(c->ssl->connection);
|
|
1242 c->ssl = NULL;
|
|
1243
|
|
1244 return NGX_OK;
|
|
1245 }
|
|
1246
|
|
1247 if (sslerr == SSL_ERROR_WANT_READ || sslerr == SSL_ERROR_WANT_WRITE) {
|
126
|
1248 c->read->handler = ngx_ssl_shutdown_handler;
|
138
|
1249 c->write->handler = ngx_ssl_shutdown_handler;
|
126
|
1250
|
430
|
1251 if (ngx_handle_read_event(c->read, 0) != NGX_OK) {
|
0
|
1252 return NGX_ERROR;
|
|
1253 }
|
|
1254
|
430
|
1255 if (ngx_handle_write_event(c->write, 0) != NGX_OK) {
|
0
|
1256 return NGX_ERROR;
|
|
1257 }
|
|
1258
|
358
|
1259 if (sslerr == SSL_ERROR_WANT_READ) {
|
138
|
1260 ngx_add_timer(c->read, 30000);
|
|
1261 }
|
|
1262
|
0
|
1263 return NGX_AGAIN;
|
|
1264 }
|
|
1265
|
140
|
1266 err = (sslerr == SSL_ERROR_SYSCALL) ? ngx_errno : 0;
|
|
1267
|
|
1268 ngx_ssl_connection_error(c, sslerr, err, "SSL_shutdown() failed");
|
0
|
1269
|
92
|
1270 SSL_free(c->ssl->connection);
|
|
1271 c->ssl = NULL;
|
|
1272
|
0
|
1273 return NGX_ERROR;
|
|
1274 }
|
|
1275
|
|
1276
|
96
|
1277 static void
|
126
|
1278 ngx_ssl_shutdown_handler(ngx_event_t *ev)
|
|
1279 {
|
|
1280 ngx_connection_t *c;
|
|
1281 ngx_connection_handler_pt handler;
|
|
1282
|
|
1283 c = ev->data;
|
|
1284 handler = c->ssl->handler;
|
|
1285
|
|
1286 if (ev->timedout) {
|
|
1287 c->timedout = 1;
|
|
1288 }
|
|
1289
|
272
|
1290 ngx_log_debug0(NGX_LOG_DEBUG_EVENT, ev->log, 0, "SSL shutdown handler");
|
126
|
1291
|
|
1292 if (ngx_ssl_shutdown(c) == NGX_AGAIN) {
|
|
1293 return;
|
|
1294 }
|
|
1295
|
|
1296 handler(c);
|
|
1297 }
|
|
1298
|
|
1299
|
|
1300 static void
|
96
|
1301 ngx_ssl_connection_error(ngx_connection_t *c, int sslerr, ngx_err_t err,
|
|
1302 char *text)
|
|
1303 {
|
364
|
1304 int n;
|
96
|
1305 ngx_uint_t level;
|
|
1306
|
|
1307 level = NGX_LOG_CRIT;
|
|
1308
|
|
1309 if (sslerr == SSL_ERROR_SYSCALL) {
|
|
1310
|
|
1311 if (err == NGX_ECONNRESET
|
|
1312 || err == NGX_EPIPE
|
|
1313 || err == NGX_ENOTCONN
|
138
|
1314 || err == NGX_ETIMEDOUT
|
96
|
1315 || err == NGX_ECONNREFUSED
|
364
|
1316 || err == NGX_ENETDOWN
|
|
1317 || err == NGX_ENETUNREACH
|
|
1318 || err == NGX_EHOSTDOWN
|
96
|
1319 || err == NGX_EHOSTUNREACH)
|
|
1320 {
|
|
1321 switch (c->log_error) {
|
|
1322
|
|
1323 case NGX_ERROR_IGNORE_ECONNRESET:
|
|
1324 case NGX_ERROR_INFO:
|
|
1325 level = NGX_LOG_INFO;
|
|
1326 break;
|
|
1327
|
|
1328 case NGX_ERROR_ERR:
|
|
1329 level = NGX_LOG_ERR;
|
|
1330 break;
|
|
1331
|
|
1332 default:
|
|
1333 break;
|
|
1334 }
|
|
1335 }
|
364
|
1336
|
|
1337 } else if (sslerr == SSL_ERROR_SSL) {
|
|
1338
|
|
1339 n = ERR_GET_REASON(ERR_peek_error());
|
|
1340
|
|
1341 /* handshake failures */
|
592
|
1342 if (n == SSL_R_BLOCK_CIPHER_PAD_IS_WRONG /* 129 */
|
|
1343 || n == SSL_R_DIGEST_CHECK_FAILED /* 149 */
|
564
|
1344 || n == SSL_R_LENGTH_MISMATCH /* 159 */
|
420
|
1345 || n == SSL_R_NO_CIPHERS_PASSED /* 182 */
|
564
|
1346 || n == SSL_R_NO_CIPHERS_SPECIFIED /* 183 */
|
420
|
1347 || n == SSL_R_NO_SHARED_CIPHER /* 193 */
|
564
|
1348 || n == SSL_R_RECORD_LENGTH_MISMATCH /* 213 */
|
420
|
1349 || n == SSL_R_UNEXPECTED_MESSAGE /* 244 */
|
|
1350 || n == SSL_R_UNEXPECTED_RECORD /* 245 */
|
564
|
1351 || n == SSL_R_UNKNOWN_ALERT_TYPE /* 246 */
|
552
|
1352 || n == SSL_R_UNKNOWN_PROTOCOL /* 252 */
|
420
|
1353 || n == SSL_R_WRONG_VERSION_NUMBER /* 267 */
|
|
1354 || n == SSL_R_DECRYPTION_FAILED_OR_BAD_RECORD_MAC /* 281 */
|
364
|
1355 || n == 1000 /* SSL_R_SSLV3_ALERT_CLOSE_NOTIFY */
|
420
|
1356 || n == SSL_R_SSLV3_ALERT_UNEXPECTED_MESSAGE /* 1010 */
|
|
1357 || n == SSL_R_SSLV3_ALERT_BAD_RECORD_MAC /* 1020 */
|
|
1358 || n == SSL_R_TLSV1_ALERT_DECRYPTION_FAILED /* 1021 */
|
|
1359 || n == SSL_R_TLSV1_ALERT_RECORD_OVERFLOW /* 1022 */
|
|
1360 || n == SSL_R_SSLV3_ALERT_DECOMPRESSION_FAILURE /* 1030 */
|
|
1361 || n == SSL_R_SSLV3_ALERT_HANDSHAKE_FAILURE /* 1040 */
|
|
1362 || n == SSL_R_SSLV3_ALERT_NO_CERTIFICATE /* 1041 */
|
|
1363 || n == SSL_R_SSLV3_ALERT_BAD_CERTIFICATE /* 1042 */
|
|
1364 || n == SSL_R_SSLV3_ALERT_UNSUPPORTED_CERTIFICATE /* 1043 */
|
|
1365 || n == SSL_R_SSLV3_ALERT_CERTIFICATE_REVOKED /* 1044 */
|
|
1366 || n == SSL_R_SSLV3_ALERT_CERTIFICATE_EXPIRED /* 1045 */
|
|
1367 || n == SSL_R_SSLV3_ALERT_CERTIFICATE_UNKNOWN /* 1046 */
|
|
1368 || n == SSL_R_SSLV3_ALERT_ILLEGAL_PARAMETER /* 1047 */
|
|
1369 || n == SSL_R_TLSV1_ALERT_UNKNOWN_CA /* 1048 */
|
|
1370 || n == SSL_R_TLSV1_ALERT_ACCESS_DENIED /* 1049 */
|
|
1371 || n == SSL_R_TLSV1_ALERT_DECODE_ERROR /* 1050 */
|
|
1372 || n == SSL_R_TLSV1_ALERT_DECRYPT_ERROR /* 1051 */
|
|
1373 || n == SSL_R_TLSV1_ALERT_EXPORT_RESTRICTION /* 1060 */
|
|
1374 || n == SSL_R_TLSV1_ALERT_PROTOCOL_VERSION /* 1070 */
|
|
1375 || n == SSL_R_TLSV1_ALERT_INSUFFICIENT_SECURITY /* 1071 */
|
|
1376 || n == SSL_R_TLSV1_ALERT_INTERNAL_ERROR /* 1080 */
|
|
1377 || n == SSL_R_TLSV1_ALERT_USER_CANCELLED /* 1090 */
|
|
1378 || n == SSL_R_TLSV1_ALERT_NO_RENEGOTIATION) /* 1100 */
|
364
|
1379 {
|
|
1380 switch (c->log_error) {
|
|
1381
|
|
1382 case NGX_ERROR_IGNORE_ECONNRESET:
|
|
1383 case NGX_ERROR_INFO:
|
|
1384 level = NGX_LOG_INFO;
|
|
1385 break;
|
|
1386
|
|
1387 case NGX_ERROR_ERR:
|
|
1388 level = NGX_LOG_ERR;
|
|
1389 break;
|
|
1390
|
|
1391 default:
|
|
1392 break;
|
|
1393 }
|
|
1394 }
|
96
|
1395 }
|
|
1396
|
|
1397 ngx_ssl_error(level, c->log, err, text);
|
|
1398 }
|
|
1399
|
|
1400
|
358
|
1401 static void
|
|
1402 ngx_ssl_clear_error(ngx_log_t *log)
|
|
1403 {
|
364
|
1404 while (ERR_peek_error()) {
|
358
|
1405 ngx_ssl_error(NGX_LOG_ALERT, log, 0, "ignoring stale global SSL error");
|
|
1406 }
|
364
|
1407
|
|
1408 ERR_clear_error();
|
358
|
1409 }
|
|
1410
|
|
1411
|
132
|
1412 void ngx_cdecl
|
38
|
1413 ngx_ssl_error(ngx_uint_t level, ngx_log_t *log, ngx_err_t err, char *fmt, ...)
|
126
|
1414 {
|
364
|
1415 u_long n;
|
|
1416 va_list args;
|
|
1417 u_char *p, *last;
|
|
1418 u_char errstr[NGX_MAX_CONF_ERRSTR];
|
10
|
1419
|
|
1420 last = errstr + NGX_MAX_CONF_ERRSTR;
|
0
|
1421
|
|
1422 va_start(args, fmt);
|
484
|
1423 p = ngx_vslprintf(errstr, last - 1, fmt, args);
|
0
|
1424 va_end(args);
|
|
1425
|
96
|
1426 p = ngx_cpystrn(p, (u_char *) " (SSL:", last - p);
|
|
1427
|
364
|
1428 for ( ;; ) {
|
132
|
1429
|
|
1430 n = ERR_get_error();
|
|
1431
|
|
1432 if (n == 0) {
|
|
1433 break;
|
|
1434 }
|
96
|
1435
|
364
|
1436 if (p >= last) {
|
|
1437 continue;
|
|
1438 }
|
|
1439
|
96
|
1440 *p++ = ' ';
|
0
|
1441
|
96
|
1442 ERR_error_string_n(n, (char *) p, last - p);
|
|
1443
|
|
1444 while (p < last && *p) {
|
|
1445 p++;
|
|
1446 }
|
|
1447 }
|
0
|
1448
|
|
1449 ngx_log_error(level, log, err, "%s)", errstr);
|
|
1450 }
|
58
|
1451
|
|
1452
|
272
|
1453 ngx_int_t
|
|
1454 ngx_ssl_session_cache(ngx_ssl_t *ssl, ngx_str_t *sess_ctx,
|
|
1455 ssize_t builtin_session_cache, ngx_shm_zone_t *shm_zone, time_t timeout)
|
|
1456 {
|
|
1457 long cache_mode;
|
|
1458
|
358
|
1459 if (builtin_session_cache == NGX_SSL_NO_SCACHE) {
|
|
1460 SSL_CTX_set_session_cache_mode(ssl->ctx, SSL_SESS_CACHE_OFF);
|
|
1461 return NGX_OK;
|
|
1462 }
|
|
1463
|
564
|
1464 SSL_CTX_set_session_id_context(ssl->ctx, sess_ctx->data, sess_ctx->len);
|
|
1465
|
378
|
1466 if (builtin_session_cache == NGX_SSL_NONE_SCACHE) {
|
|
1467
|
|
1468 /*
|
|
1469 * If the server explicitly says that it does not support
|
|
1470 * session reuse (see SSL_SESS_CACHE_OFF above), then
|
|
1471 * Outlook Express fails to upload a sent email to
|
|
1472 * the Sent Items folder on the IMAP server via a separate IMAP
|
|
1473 * connection in the background. Therefore we have a special
|
|
1474 * mode (SSL_SESS_CACHE_SERVER|SSL_SESS_CACHE_NO_INTERNAL_STORE)
|
|
1475 * where the server pretends that it supports session reuse,
|
|
1476 * but it does not actually store any session.
|
|
1477 */
|
|
1478
|
|
1479 SSL_CTX_set_session_cache_mode(ssl->ctx,
|
|
1480 SSL_SESS_CACHE_SERVER
|
|
1481 |SSL_SESS_CACHE_NO_AUTO_CLEAR
|
|
1482 |SSL_SESS_CACHE_NO_INTERNAL_STORE);
|
|
1483
|
|
1484 SSL_CTX_sess_set_cache_size(ssl->ctx, 1);
|
|
1485
|
|
1486 return NGX_OK;
|
|
1487 }
|
|
1488
|
272
|
1489 cache_mode = SSL_SESS_CACHE_SERVER;
|
|
1490
|
|
1491 if (shm_zone && builtin_session_cache == NGX_SSL_NO_BUILTIN_SCACHE) {
|
|
1492 cache_mode |= SSL_SESS_CACHE_NO_INTERNAL;
|
|
1493 }
|
|
1494
|
|
1495 SSL_CTX_set_session_cache_mode(ssl->ctx, cache_mode);
|
|
1496
|
|
1497 if (builtin_session_cache != NGX_SSL_NO_BUILTIN_SCACHE) {
|
|
1498
|
|
1499 if (builtin_session_cache != NGX_SSL_DFLT_BUILTIN_SCACHE) {
|
|
1500 SSL_CTX_sess_set_cache_size(ssl->ctx, builtin_session_cache);
|
|
1501 }
|
|
1502 }
|
|
1503
|
480
|
1504 SSL_CTX_set_timeout(ssl->ctx, (long) timeout);
|
274
|
1505
|
272
|
1506 if (shm_zone) {
|
|
1507 SSL_CTX_sess_set_new_cb(ssl->ctx, ngx_ssl_new_session);
|
|
1508 SSL_CTX_sess_set_get_cb(ssl->ctx, ngx_ssl_get_cached_session);
|
|
1509 SSL_CTX_sess_set_remove_cb(ssl->ctx, ngx_ssl_remove_session);
|
|
1510
|
|
1511 if (SSL_CTX_set_ex_data(ssl->ctx, ngx_ssl_session_cache_index, shm_zone)
|
|
1512 == 0)
|
|
1513 {
|
|
1514 ngx_ssl_error(NGX_LOG_EMERG, ssl->log, 0,
|
|
1515 "SSL_CTX_set_ex_data() failed");
|
|
1516 return NGX_ERROR;
|
|
1517 }
|
|
1518 }
|
|
1519
|
|
1520 return NGX_OK;
|
|
1521 }
|
|
1522
|
|
1523
|
634
|
1524 ngx_int_t
|
272
|
1525 ngx_ssl_session_cache_init(ngx_shm_zone_t *shm_zone, void *data)
|
|
1526 {
|
468
|
1527 size_t len;
|
272
|
1528 ngx_slab_pool_t *shpool;
|
|
1529 ngx_ssl_session_cache_t *cache;
|
|
1530
|
|
1531 if (data) {
|
|
1532 shm_zone->data = data;
|
|
1533 return NGX_OK;
|
|
1534 }
|
|
1535
|
480
|
1536 if (shm_zone->shm.exists) {
|
|
1537 shm_zone->data = data;
|
|
1538 return NGX_OK;
|
|
1539 }
|
|
1540
|
272
|
1541 shpool = (ngx_slab_pool_t *) shm_zone->shm.addr;
|
|
1542
|
|
1543 cache = ngx_slab_alloc(shpool, sizeof(ngx_ssl_session_cache_t));
|
|
1544 if (cache == NULL) {
|
|
1545 return NGX_ERROR;
|
|
1546 }
|
|
1547
|
480
|
1548 shpool->data = cache;
|
|
1549 shm_zone->data = cache;
|
|
1550
|
358
|
1551 ngx_rbtree_init(&cache->session_rbtree, &cache->sentinel,
|
356
|
1552 ngx_ssl_session_rbtree_insert_value);
|
272
|
1553
|
358
|
1554 ngx_queue_init(&cache->expire_queue);
|
|
1555
|
480
|
1556 len = sizeof(" in SSL session shared cache \"\"") + shm_zone->shm.name.len;
|
468
|
1557
|
|
1558 shpool->log_ctx = ngx_slab_alloc(shpool, len);
|
|
1559 if (shpool->log_ctx == NULL) {
|
|
1560 return NGX_ERROR;
|
|
1561 }
|
|
1562
|
|
1563 ngx_sprintf(shpool->log_ctx, " in SSL session shared cache \"%V\"%Z",
|
480
|
1564 &shm_zone->shm.name);
|
272
|
1565
|
|
1566 return NGX_OK;
|
|
1567 }
|
|
1568
|
|
1569
|
|
1570 /*
|
274
|
1571 * The length of the session id is 16 bytes for SSLv2 sessions and
|
|
1572 * between 1 and 32 bytes for SSLv3/TLSv1, typically 32 bytes.
|
|
1573 * It seems that the typical length of the external ASN1 representation
|
|
1574 * of a session is 118 or 119 bytes for SSLv3/TSLv1.
|
|
1575 *
|
|
1576 * Thus on 32-bit platforms we allocate separately an rbtree node,
|
|
1577 * a session id, and an ASN1 representation, they take accordingly
|
|
1578 * 64, 32, and 128 bytes.
|
|
1579 *
|
|
1580 * On 64-bit platforms we allocate separately an rbtree node + session_id,
|
|
1581 * and an ASN1 representation, they take accordingly 128 and 128 bytes.
|
|
1582 *
|
272
|
1583 * OpenSSL's i2d_SSL_SESSION() and d2i_SSL_SESSION are slow,
|
|
1584 * so they are outside the code locked by shared pool mutex
|
|
1585 */
|
|
1586
|
|
1587 static int
|
|
1588 ngx_ssl_new_session(ngx_ssl_conn_t *ssl_conn, ngx_ssl_session_t *sess)
|
|
1589 {
|
|
1590 int len;
|
274
|
1591 u_char *p, *id, *cached_sess;
|
272
|
1592 uint32_t hash;
|
|
1593 SSL_CTX *ssl_ctx;
|
|
1594 ngx_shm_zone_t *shm_zone;
|
|
1595 ngx_connection_t *c;
|
|
1596 ngx_slab_pool_t *shpool;
|
|
1597 ngx_ssl_sess_id_t *sess_id;
|
|
1598 ngx_ssl_session_cache_t *cache;
|
|
1599 u_char buf[NGX_SSL_MAX_SESSION_SIZE];
|
|
1600
|
|
1601 len = i2d_SSL_SESSION(sess, NULL);
|
|
1602
|
|
1603 /* do not cache too big session */
|
|
1604
|
|
1605 if (len > (int) NGX_SSL_MAX_SESSION_SIZE) {
|
|
1606 return 0;
|
|
1607 }
|
|
1608
|
|
1609 p = buf;
|
|
1610 i2d_SSL_SESSION(sess, &p);
|
|
1611
|
|
1612 c = ngx_ssl_get_connection(ssl_conn);
|
|
1613
|
|
1614 ssl_ctx = SSL_get_SSL_CTX(ssl_conn);
|
|
1615 shm_zone = SSL_CTX_get_ex_data(ssl_ctx, ngx_ssl_session_cache_index);
|
|
1616
|
|
1617 cache = shm_zone->data;
|
|
1618 shpool = (ngx_slab_pool_t *) shm_zone->shm.addr;
|
|
1619
|
|
1620 ngx_shmtx_lock(&shpool->mutex);
|
|
1621
|
|
1622 /* drop one or two expired sessions */
|
|
1623 ngx_ssl_expire_sessions(cache, shpool, 1);
|
|
1624
|
274
|
1625 cached_sess = ngx_slab_alloc_locked(shpool, len);
|
272
|
1626
|
|
1627 if (cached_sess == NULL) {
|
|
1628
|
|
1629 /* drop the oldest non-expired session and try once more */
|
|
1630
|
|
1631 ngx_ssl_expire_sessions(cache, shpool, 0);
|
|
1632
|
274
|
1633 cached_sess = ngx_slab_alloc_locked(shpool, len);
|
272
|
1634
|
|
1635 if (cached_sess == NULL) {
|
274
|
1636 sess_id = NULL;
|
272
|
1637 goto failed;
|
|
1638 }
|
|
1639 }
|
|
1640
|
|
1641 sess_id = ngx_slab_alloc_locked(shpool, sizeof(ngx_ssl_sess_id_t));
|
|
1642 if (sess_id == NULL) {
|
|
1643 goto failed;
|
|
1644 }
|
|
1645
|
274
|
1646 #if (NGX_PTR_SIZE == 8)
|
|
1647
|
|
1648 id = sess_id->sess_id;
|
|
1649
|
|
1650 #else
|
|
1651
|
|
1652 id = ngx_slab_alloc_locked(shpool, sess->session_id_length);
|
|
1653 if (id == NULL) {
|
|
1654 goto failed;
|
|
1655 }
|
|
1656
|
|
1657 #endif
|
|
1658
|
|
1659 ngx_memcpy(cached_sess, buf, len);
|
272
|
1660
|
|
1661 ngx_memcpy(id, sess->session_id, sess->session_id_length);
|
|
1662
|
|
1663 hash = ngx_crc32_short(sess->session_id, sess->session_id_length);
|
|
1664
|
|
1665 ngx_log_debug3(NGX_LOG_DEBUG_EVENT, c->log, 0,
|
530
|
1666 "ssl new session: %08XD:%d:%d",
|
272
|
1667 hash, sess->session_id_length, len);
|
|
1668
|
|
1669 sess_id->node.key = hash;
|
|
1670 sess_id->node.data = (u_char) sess->session_id_length;
|
|
1671 sess_id->id = id;
|
|
1672 sess_id->len = len;
|
|
1673 sess_id->session = cached_sess;
|
|
1674
|
358
|
1675 sess_id->expire = ngx_time() + SSL_CTX_get_timeout(ssl_ctx);
|
|
1676
|
|
1677 ngx_queue_insert_head(&cache->expire_queue, &sess_id->queue);
|
|
1678
|
|
1679 ngx_rbtree_insert(&cache->session_rbtree, &sess_id->node);
|
272
|
1680
|
|
1681 ngx_shmtx_unlock(&shpool->mutex);
|
|
1682
|
|
1683 return 0;
|
|
1684
|
|
1685 failed:
|
|
1686
|
|
1687 if (cached_sess) {
|
|
1688 ngx_slab_free_locked(shpool, cached_sess);
|
|
1689 }
|
|
1690
|
274
|
1691 if (sess_id) {
|
|
1692 ngx_slab_free_locked(shpool, sess_id);
|
272
|
1693 }
|
|
1694
|
|
1695 ngx_shmtx_unlock(&shpool->mutex);
|
|
1696
|
|
1697 ngx_log_error(NGX_LOG_ALERT, c->log, 0,
|
|
1698 "could not add new SSL session to the session cache");
|
|
1699
|
|
1700 return 0;
|
|
1701 }
|
|
1702
|
|
1703
|
|
1704 static ngx_ssl_session_t *
|
|
1705 ngx_ssl_get_cached_session(ngx_ssl_conn_t *ssl_conn, u_char *id, int len,
|
|
1706 int *copy)
|
|
1707 {
|
|
1708 #if OPENSSL_VERSION_NUMBER >= 0x0090707fL
|
|
1709 const
|
|
1710 #endif
|
|
1711 u_char *p;
|
|
1712 uint32_t hash;
|
274
|
1713 ngx_int_t rc;
|
272
|
1714 ngx_shm_zone_t *shm_zone;
|
|
1715 ngx_slab_pool_t *shpool;
|
|
1716 ngx_rbtree_node_t *node, *sentinel;
|
|
1717 ngx_ssl_session_t *sess;
|
|
1718 ngx_ssl_sess_id_t *sess_id;
|
|
1719 ngx_ssl_session_cache_t *cache;
|
|
1720 u_char buf[NGX_SSL_MAX_SESSION_SIZE];
|
632
|
1721 #if (NGX_DEBUG)
|
|
1722 ngx_connection_t *c;
|
|
1723 #endif
|
272
|
1724
|
274
|
1725 hash = ngx_crc32_short(id, (size_t) len);
|
272
|
1726 *copy = 0;
|
|
1727
|
632
|
1728 #if (NGX_DEBUG)
|
|
1729 c = ngx_ssl_get_connection(ssl_conn);
|
|
1730
|
272
|
1731 ngx_log_debug2(NGX_LOG_DEBUG_EVENT, c->log, 0,
|
530
|
1732 "ssl get session: %08XD:%d", hash, len);
|
632
|
1733 #endif
|
272
|
1734
|
|
1735 shm_zone = SSL_CTX_get_ex_data(SSL_get_SSL_CTX(ssl_conn),
|
|
1736 ngx_ssl_session_cache_index);
|
|
1737
|
|
1738 cache = shm_zone->data;
|
|
1739
|
|
1740 sess = NULL;
|
|
1741
|
|
1742 shpool = (ngx_slab_pool_t *) shm_zone->shm.addr;
|
|
1743
|
|
1744 ngx_shmtx_lock(&shpool->mutex);
|
|
1745
|
358
|
1746 node = cache->session_rbtree.root;
|
|
1747 sentinel = cache->session_rbtree.sentinel;
|
272
|
1748
|
|
1749 while (node != sentinel) {
|
|
1750
|
|
1751 if (hash < node->key) {
|
|
1752 node = node->left;
|
|
1753 continue;
|
|
1754 }
|
|
1755
|
|
1756 if (hash > node->key) {
|
|
1757 node = node->right;
|
|
1758 continue;
|
|
1759 }
|
|
1760
|
274
|
1761 /* hash == node->key */
|
|
1762
|
|
1763 do {
|
272
|
1764 sess_id = (ngx_ssl_sess_id_t *) node;
|
|
1765
|
274
|
1766 rc = ngx_memn2cmp(id, sess_id->id,
|
|
1767 (size_t) len, (size_t) node->data);
|
|
1768 if (rc == 0) {
|
272
|
1769
|
358
|
1770 if (sess_id->expire > ngx_time()) {
|
274
|
1771 ngx_memcpy(buf, sess_id->session, sess_id->len);
|
272
|
1772
|
|
1773 ngx_shmtx_unlock(&shpool->mutex);
|
|
1774
|
|
1775 p = buf;
|
|
1776 sess = d2i_SSL_SESSION(NULL, &p, sess_id->len);
|
|
1777
|
|
1778 return sess;
|
|
1779 }
|
|
1780
|
358
|
1781 ngx_queue_remove(&sess_id->queue);
|
|
1782
|
|
1783 ngx_rbtree_delete(&cache->session_rbtree, node);
|
272
|
1784
|
274
|
1785 ngx_slab_free_locked(shpool, sess_id->session);
|
|
1786 #if (NGX_PTR_SIZE == 4)
|
272
|
1787 ngx_slab_free_locked(shpool, sess_id->id);
|
274
|
1788 #endif
|
272
|
1789 ngx_slab_free_locked(shpool, sess_id);
|
|
1790
|
|
1791 sess = NULL;
|
|
1792
|
274
|
1793 goto done;
|
272
|
1794 }
|
274
|
1795
|
|
1796 node = (rc < 0) ? node->left : node->right;
|
|
1797
|
|
1798 } while (node != sentinel && hash == node->key);
|
|
1799
|
|
1800 break;
|
272
|
1801 }
|
|
1802
|
274
|
1803 done:
|
|
1804
|
272
|
1805 ngx_shmtx_unlock(&shpool->mutex);
|
|
1806
|
|
1807 return sess;
|
|
1808 }
|
|
1809
|
|
1810
|
366
|
1811 void
|
|
1812 ngx_ssl_remove_cached_session(SSL_CTX *ssl, ngx_ssl_session_t *sess)
|
|
1813 {
|
|
1814 SSL_CTX_remove_session(ssl, sess);
|
|
1815
|
|
1816 ngx_ssl_remove_session(ssl, sess);
|
|
1817 }
|
|
1818
|
|
1819
|
272
|
1820 static void
|
|
1821 ngx_ssl_remove_session(SSL_CTX *ssl, ngx_ssl_session_t *sess)
|
|
1822 {
|
274
|
1823 size_t len;
|
|
1824 u_char *id;
|
272
|
1825 uint32_t hash;
|
274
|
1826 ngx_int_t rc;
|
272
|
1827 ngx_shm_zone_t *shm_zone;
|
|
1828 ngx_slab_pool_t *shpool;
|
|
1829 ngx_rbtree_node_t *node, *sentinel;
|
|
1830 ngx_ssl_sess_id_t *sess_id;
|
|
1831 ngx_ssl_session_cache_t *cache;
|
|
1832
|
|
1833 shm_zone = SSL_CTX_get_ex_data(ssl, ngx_ssl_session_cache_index);
|
|
1834
|
366
|
1835 if (shm_zone == NULL) {
|
|
1836 return;
|
|
1837 }
|
|
1838
|
272
|
1839 cache = shm_zone->data;
|
|
1840
|
|
1841 id = sess->session_id;
|
274
|
1842 len = (size_t) sess->session_id_length;
|
|
1843
|
|
1844 hash = ngx_crc32_short(id, len);
|
272
|
1845
|
|
1846 ngx_log_debug2(NGX_LOG_DEBUG_EVENT, ngx_cycle->log, 0,
|
530
|
1847 "ssl remove session: %08XD:%uz", hash, len);
|
272
|
1848
|
|
1849 shpool = (ngx_slab_pool_t *) shm_zone->shm.addr;
|
|
1850
|
|
1851 ngx_shmtx_lock(&shpool->mutex);
|
|
1852
|
358
|
1853 node = cache->session_rbtree.root;
|
|
1854 sentinel = cache->session_rbtree.sentinel;
|
272
|
1855
|
|
1856 while (node != sentinel) {
|
|
1857
|
|
1858 if (hash < node->key) {
|
|
1859 node = node->left;
|
|
1860 continue;
|
|
1861 }
|
|
1862
|
|
1863 if (hash > node->key) {
|
|
1864 node = node->right;
|
|
1865 continue;
|
|
1866 }
|
|
1867
|
274
|
1868 /* hash == node->key */
|
|
1869
|
|
1870 do {
|
272
|
1871 sess_id = (ngx_ssl_sess_id_t *) node;
|
|
1872
|
274
|
1873 rc = ngx_memn2cmp(id, sess_id->id, len, (size_t) node->data);
|
|
1874
|
|
1875 if (rc == 0) {
|
358
|
1876
|
|
1877 ngx_queue_remove(&sess_id->queue);
|
|
1878
|
|
1879 ngx_rbtree_delete(&cache->session_rbtree, node);
|
272
|
1880
|
274
|
1881 ngx_slab_free_locked(shpool, sess_id->session);
|
|
1882 #if (NGX_PTR_SIZE == 4)
|
272
|
1883 ngx_slab_free_locked(shpool, sess_id->id);
|
274
|
1884 #endif
|
272
|
1885 ngx_slab_free_locked(shpool, sess_id);
|
|
1886
|
274
|
1887 goto done;
|
272
|
1888 }
|
274
|
1889
|
|
1890 node = (rc < 0) ? node->left : node->right;
|
|
1891
|
|
1892 } while (node != sentinel && hash == node->key);
|
|
1893
|
|
1894 break;
|
272
|
1895 }
|
|
1896
|
274
|
1897 done:
|
|
1898
|
272
|
1899 ngx_shmtx_unlock(&shpool->mutex);
|
|
1900 }
|
|
1901
|
|
1902
|
|
1903 static void
|
|
1904 ngx_ssl_expire_sessions(ngx_ssl_session_cache_t *cache,
|
|
1905 ngx_slab_pool_t *shpool, ngx_uint_t n)
|
|
1906 {
|
358
|
1907 time_t now;
|
|
1908 ngx_queue_t *q;
|
274
|
1909 ngx_ssl_sess_id_t *sess_id;
|
272
|
1910
|
358
|
1911 now = ngx_time();
|
272
|
1912
|
|
1913 while (n < 3) {
|
|
1914
|
358
|
1915 if (ngx_queue_empty(&cache->expire_queue)) {
|
272
|
1916 return;
|
|
1917 }
|
|
1918
|
358
|
1919 q = ngx_queue_last(&cache->expire_queue);
|
|
1920
|
|
1921 sess_id = ngx_queue_data(q, ngx_ssl_sess_id_t, queue);
|
|
1922
|
|
1923 if (n++ != 0 && sess_id->expire > now) {
|
332
|
1924 return;
|
272
|
1925 }
|
|
1926
|
358
|
1927 ngx_queue_remove(q);
|
272
|
1928
|
|
1929 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, ngx_cycle->log, 0,
|
|
1930 "expire session: %08Xi", sess_id->node.key);
|
|
1931
|
358
|
1932 ngx_rbtree_delete(&cache->session_rbtree, &sess_id->node);
|
|
1933
|
274
|
1934 ngx_slab_free_locked(shpool, sess_id->session);
|
|
1935 #if (NGX_PTR_SIZE == 4)
|
272
|
1936 ngx_slab_free_locked(shpool, sess_id->id);
|
274
|
1937 #endif
|
272
|
1938 ngx_slab_free_locked(shpool, sess_id);
|
|
1939 }
|
|
1940 }
|
|
1941
|
|
1942
|
274
|
1943 static void
|
|
1944 ngx_ssl_session_rbtree_insert_value(ngx_rbtree_node_t *temp,
|
|
1945 ngx_rbtree_node_t *node, ngx_rbtree_node_t *sentinel)
|
|
1946 {
|
356
|
1947 ngx_rbtree_node_t **p;
|
|
1948 ngx_ssl_sess_id_t *sess_id, *sess_id_temp;
|
274
|
1949
|
|
1950 for ( ;; ) {
|
|
1951
|
|
1952 if (node->key < temp->key) {
|
|
1953
|
356
|
1954 p = &temp->left;
|
274
|
1955
|
|
1956 } else if (node->key > temp->key) {
|
|
1957
|
356
|
1958 p = &temp->right;
|
274
|
1959
|
|
1960 } else { /* node->key == temp->key */
|
|
1961
|
|
1962 sess_id = (ngx_ssl_sess_id_t *) node;
|
|
1963 sess_id_temp = (ngx_ssl_sess_id_t *) temp;
|
|
1964
|
356
|
1965 p = (ngx_memn2cmp(sess_id->id, sess_id_temp->id,
|
|
1966 (size_t) node->data, (size_t) temp->data)
|
|
1967 < 0) ? &temp->left : &temp->right;
|
274
|
1968 }
|
356
|
1969
|
|
1970 if (*p == sentinel) {
|
|
1971 break;
|
|
1972 }
|
|
1973
|
|
1974 temp = *p;
|
274
|
1975 }
|
|
1976
|
356
|
1977 *p = node;
|
274
|
1978 node->parent = temp;
|
|
1979 node->left = sentinel;
|
|
1980 node->right = sentinel;
|
|
1981 ngx_rbt_red(node);
|
276
|
1982 }
|
274
|
1983
|
|
1984
|
58
|
1985 void
|
|
1986 ngx_ssl_cleanup_ctx(void *data)
|
|
1987 {
|
138
|
1988 ngx_ssl_t *ssl = data;
|
58
|
1989
|
138
|
1990 SSL_CTX_free(ssl->ctx);
|
58
|
1991 }
|
90
|
1992
|
|
1993
|
220
|
1994 ngx_int_t
|
|
1995 ngx_ssl_get_protocol(ngx_connection_t *c, ngx_pool_t *pool, ngx_str_t *s)
|
160
|
1996 {
|
220
|
1997 s->data = (u_char *) SSL_get_version(c->ssl->connection);
|
|
1998 return NGX_OK;
|
160
|
1999 }
|
|
2000
|
|
2001
|
220
|
2002 ngx_int_t
|
|
2003 ngx_ssl_get_cipher_name(ngx_connection_t *c, ngx_pool_t *pool, ngx_str_t *s)
|
160
|
2004 {
|
220
|
2005 s->data = (u_char *) SSL_get_cipher_name(c->ssl->connection);
|
|
2006 return NGX_OK;
|
160
|
2007 }
|
|
2008
|
|
2009
|
196
|
2010 ngx_int_t
|
530
|
2011 ngx_ssl_get_session_id(ngx_connection_t *c, ngx_pool_t *pool, ngx_str_t *s)
|
|
2012 {
|
|
2013 int len;
|
|
2014 u_char *p, *buf;
|
|
2015 SSL_SESSION *sess;
|
|
2016
|
|
2017 sess = SSL_get0_session(c->ssl->connection);
|
|
2018
|
|
2019 len = i2d_SSL_SESSION(sess, NULL);
|
|
2020
|
|
2021 buf = ngx_alloc(len, c->log);
|
|
2022 if (buf == NULL) {
|
|
2023 return NGX_ERROR;
|
|
2024 }
|
|
2025
|
|
2026 s->len = 2 * len;
|
|
2027 s->data = ngx_pnalloc(pool, 2 * len);
|
|
2028 if (s->data == NULL) {
|
|
2029 ngx_free(buf);
|
|
2030 return NGX_ERROR;
|
|
2031 }
|
|
2032
|
|
2033 p = buf;
|
|
2034 i2d_SSL_SESSION(sess, &p);
|
|
2035
|
|
2036 ngx_hex_dump(s->data, buf, len);
|
|
2037
|
|
2038 ngx_free(buf);
|
|
2039
|
|
2040 return NGX_OK;
|
|
2041 }
|
|
2042
|
|
2043
|
|
2044 ngx_int_t
|
390
|
2045 ngx_ssl_get_raw_certificate(ngx_connection_t *c, ngx_pool_t *pool, ngx_str_t *s)
|
380
|
2046 {
|
|
2047 size_t len;
|
|
2048 BIO *bio;
|
|
2049 X509 *cert;
|
|
2050
|
|
2051 s->len = 0;
|
|
2052
|
|
2053 cert = SSL_get_peer_certificate(c->ssl->connection);
|
|
2054 if (cert == NULL) {
|
|
2055 return NGX_OK;
|
|
2056 }
|
|
2057
|
|
2058 bio = BIO_new(BIO_s_mem());
|
|
2059 if (bio == NULL) {
|
|
2060 ngx_ssl_error(NGX_LOG_ALERT, c->log, 0, "BIO_new() failed");
|
|
2061 X509_free(cert);
|
|
2062 return NGX_ERROR;
|
|
2063 }
|
|
2064
|
|
2065 if (PEM_write_bio_X509(bio, cert) == 0) {
|
|
2066 ngx_ssl_error(NGX_LOG_ALERT, c->log, 0, "PEM_write_bio_X509() failed");
|
|
2067 goto failed;
|
|
2068 }
|
|
2069
|
|
2070 len = BIO_pending(bio);
|
|
2071 s->len = len;
|
|
2072
|
382
|
2073 s->data = ngx_pnalloc(pool, len);
|
380
|
2074 if (s->data == NULL) {
|
|
2075 goto failed;
|
|
2076 }
|
|
2077
|
|
2078 BIO_read(bio, s->data, len);
|
|
2079
|
|
2080 BIO_free(bio);
|
|
2081 X509_free(cert);
|
|
2082
|
|
2083 return NGX_OK;
|
|
2084
|
|
2085 failed:
|
|
2086
|
|
2087 BIO_free(bio);
|
|
2088 X509_free(cert);
|
|
2089
|
|
2090 return NGX_ERROR;
|
|
2091 }
|
|
2092
|
|
2093
|
|
2094 ngx_int_t
|
390
|
2095 ngx_ssl_get_certificate(ngx_connection_t *c, ngx_pool_t *pool, ngx_str_t *s)
|
|
2096 {
|
|
2097 u_char *p;
|
|
2098 size_t len;
|
|
2099 ngx_uint_t i;
|
|
2100 ngx_str_t cert;
|
|
2101
|
|
2102 if (ngx_ssl_get_raw_certificate(c, pool, &cert) != NGX_OK) {
|
|
2103 return NGX_ERROR;
|
|
2104 }
|
|
2105
|
|
2106 if (cert.len == 0) {
|
|
2107 s->len = 0;
|
|
2108 return NGX_OK;
|
|
2109 }
|
|
2110
|
|
2111 len = cert.len - 1;
|
|
2112
|
|
2113 for (i = 0; i < cert.len - 1; i++) {
|
|
2114 if (cert.data[i] == LF) {
|
|
2115 len++;
|
|
2116 }
|
|
2117 }
|
|
2118
|
|
2119 s->len = len;
|
|
2120 s->data = ngx_pnalloc(pool, len);
|
|
2121 if (s->data == NULL) {
|
|
2122 return NGX_ERROR;
|
|
2123 }
|
|
2124
|
|
2125 p = s->data;
|
|
2126
|
510
|
2127 for (i = 0; i < cert.len - 1; i++) {
|
390
|
2128 *p++ = cert.data[i];
|
|
2129 if (cert.data[i] == LF) {
|
|
2130 *p++ = '\t';
|
|
2131 }
|
|
2132 }
|
|
2133
|
|
2134 return NGX_OK;
|
|
2135 }
|
|
2136
|
|
2137
|
|
2138 ngx_int_t
|
196
|
2139 ngx_ssl_get_subject_dn(ngx_connection_t *c, ngx_pool_t *pool, ngx_str_t *s)
|
|
2140 {
|
|
2141 char *p;
|
|
2142 size_t len;
|
|
2143 X509 *cert;
|
|
2144 X509_NAME *name;
|
|
2145
|
|
2146 s->len = 0;
|
|
2147
|
|
2148 cert = SSL_get_peer_certificate(c->ssl->connection);
|
|
2149 if (cert == NULL) {
|
|
2150 return NGX_OK;
|
|
2151 }
|
|
2152
|
|
2153 name = X509_get_subject_name(cert);
|
|
2154 if (name == NULL) {
|
372
|
2155 X509_free(cert);
|
196
|
2156 return NGX_ERROR;
|
|
2157 }
|
|
2158
|
|
2159 p = X509_NAME_oneline(name, NULL, 0);
|
|
2160
|
|
2161 for (len = 0; p[len]; len++) { /* void */ }
|
|
2162
|
|
2163 s->len = len;
|
382
|
2164 s->data = ngx_pnalloc(pool, len);
|
196
|
2165 if (s->data == NULL) {
|
|
2166 OPENSSL_free(p);
|
372
|
2167 X509_free(cert);
|
196
|
2168 return NGX_ERROR;
|
|
2169 }
|
|
2170
|
|
2171 ngx_memcpy(s->data, p, len);
|
|
2172
|
|
2173 OPENSSL_free(p);
|
372
|
2174 X509_free(cert);
|
196
|
2175
|
|
2176 return NGX_OK;
|
|
2177 }
|
|
2178
|
|
2179
|
|
2180 ngx_int_t
|
|
2181 ngx_ssl_get_issuer_dn(ngx_connection_t *c, ngx_pool_t *pool, ngx_str_t *s)
|
|
2182 {
|
|
2183 char *p;
|
|
2184 size_t len;
|
|
2185 X509 *cert;
|
|
2186 X509_NAME *name;
|
|
2187
|
|
2188 s->len = 0;
|
|
2189
|
|
2190 cert = SSL_get_peer_certificate(c->ssl->connection);
|
|
2191 if (cert == NULL) {
|
|
2192 return NGX_OK;
|
|
2193 }
|
|
2194
|
|
2195 name = X509_get_issuer_name(cert);
|
|
2196 if (name == NULL) {
|
372
|
2197 X509_free(cert);
|
196
|
2198 return NGX_ERROR;
|
|
2199 }
|
|
2200
|
|
2201 p = X509_NAME_oneline(name, NULL, 0);
|
|
2202
|
|
2203 for (len = 0; p[len]; len++) { /* void */ }
|
|
2204
|
|
2205 s->len = len;
|
382
|
2206 s->data = ngx_pnalloc(pool, len);
|
196
|
2207 if (s->data == NULL) {
|
|
2208 OPENSSL_free(p);
|
372
|
2209 X509_free(cert);
|
196
|
2210 return NGX_ERROR;
|
|
2211 }
|
|
2212
|
|
2213 ngx_memcpy(s->data, p, len);
|
|
2214
|
|
2215 OPENSSL_free(p);
|
372
|
2216 X509_free(cert);
|
196
|
2217
|
|
2218 return NGX_OK;
|
|
2219 }
|
|
2220
|
|
2221
|
220
|
2222 ngx_int_t
|
|
2223 ngx_ssl_get_serial_number(ngx_connection_t *c, ngx_pool_t *pool, ngx_str_t *s)
|
|
2224 {
|
|
2225 size_t len;
|
|
2226 X509 *cert;
|
|
2227 BIO *bio;
|
|
2228
|
|
2229 s->len = 0;
|
|
2230
|
|
2231 cert = SSL_get_peer_certificate(c->ssl->connection);
|
|
2232 if (cert == NULL) {
|
|
2233 return NGX_OK;
|
|
2234 }
|
|
2235
|
|
2236 bio = BIO_new(BIO_s_mem());
|
|
2237 if (bio == NULL) {
|
372
|
2238 X509_free(cert);
|
220
|
2239 return NGX_ERROR;
|
|
2240 }
|
|
2241
|
|
2242 i2a_ASN1_INTEGER(bio, X509_get_serialNumber(cert));
|
|
2243 len = BIO_pending(bio);
|
|
2244
|
|
2245 s->len = len;
|
382
|
2246 s->data = ngx_pnalloc(pool, len);
|
220
|
2247 if (s->data == NULL) {
|
|
2248 BIO_free(bio);
|
372
|
2249 X509_free(cert);
|
220
|
2250 return NGX_ERROR;
|
|
2251 }
|
|
2252
|
|
2253 BIO_read(bio, s->data, len);
|
|
2254 BIO_free(bio);
|
372
|
2255 X509_free(cert);
|
220
|
2256
|
|
2257 return NGX_OK;
|
|
2258 }
|
|
2259
|
|
2260
|
510
|
2261 ngx_int_t
|
|
2262 ngx_ssl_get_client_verify(ngx_connection_t *c, ngx_pool_t *pool, ngx_str_t *s)
|
|
2263 {
|
|
2264 X509 *cert;
|
|
2265
|
|
2266 if (SSL_get_verify_result(c->ssl->connection) != X509_V_OK) {
|
570
|
2267 ngx_str_set(s, "FAILED");
|
510
|
2268 return NGX_OK;
|
|
2269 }
|
|
2270
|
|
2271 cert = SSL_get_peer_certificate(c->ssl->connection);
|
|
2272
|
|
2273 if (cert) {
|
570
|
2274 ngx_str_set(s, "SUCCESS");
|
510
|
2275
|
|
2276 } else {
|
570
|
2277 ngx_str_set(s, "NONE");
|
510
|
2278 }
|
|
2279
|
|
2280 X509_free(cert);
|
|
2281
|
|
2282 return NGX_OK;
|
|
2283 }
|
|
2284
|
|
2285
|
90
|
2286 static void *
|
|
2287 ngx_openssl_create_conf(ngx_cycle_t *cycle)
|
|
2288 {
|
|
2289 ngx_openssl_conf_t *oscf;
|
126
|
2290
|
90
|
2291 oscf = ngx_pcalloc(cycle->pool, sizeof(ngx_openssl_conf_t));
|
|
2292 if (oscf == NULL) {
|
496
|
2293 return NULL;
|
90
|
2294 }
|
126
|
2295
|
90
|
2296 /*
|
|
2297 * set by ngx_pcalloc():
|
126
|
2298 *
|
446
|
2299 * oscf->engine = 0;
|
126
|
2300 */
|
90
|
2301
|
|
2302 return oscf;
|
|
2303 }
|
|
2304
|
|
2305
|
|
2306 static char *
|
446
|
2307 ngx_openssl_engine(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
|
90
|
2308 {
|
|
2309 ngx_openssl_conf_t *oscf = conf;
|
120
|
2310
|
446
|
2311 ENGINE *engine;
|
|
2312 ngx_str_t *value;
|
|
2313
|
|
2314 if (oscf->engine) {
|
|
2315 return "is duplicate";
|
90
|
2316 }
|
126
|
2317
|
446
|
2318 oscf->engine = 1;
|
|
2319
|
|
2320 value = cf->args->elts;
|
|
2321
|
|
2322 engine = ENGINE_by_id((const char *) value[1].data);
|
90
|
2323
|
|
2324 if (engine == NULL) {
|
446
|
2325 ngx_ssl_error(NGX_LOG_WARN, cf->log, 0,
|
|
2326 "ENGINE_by_id(\"%V\") failed", &value[1]);
|
90
|
2327 return NGX_CONF_ERROR;
|
|
2328 }
|
|
2329
|
|
2330 if (ENGINE_set_default(engine, ENGINE_METHOD_ALL) == 0) {
|
446
|
2331 ngx_ssl_error(NGX_LOG_WARN, cf->log, 0,
|
90
|
2332 "ENGINE_set_default(\"%V\", ENGINE_METHOD_ALL) failed",
|
446
|
2333 &value[1]);
|
|
2334
|
|
2335 ENGINE_free(engine);
|
|
2336
|
90
|
2337 return NGX_CONF_ERROR;
|
|
2338 }
|
|
2339
|
|
2340 ENGINE_free(engine);
|
|
2341
|
|
2342 return NGX_CONF_OK;
|
446
|
2343 }
|
120
|
2344
|
|
2345
|
|
2346 static void
|
|
2347 ngx_openssl_exit(ngx_cycle_t *cycle)
|
|
2348 {
|
564
|
2349 EVP_cleanup();
|
120
|
2350 ENGINE_cleanup();
|
|
2351 }
|