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