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