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