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