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