Mercurial > hg > nginx-quic
annotate src/event/ngx_event_openssl.c @ 635:18268abd340c release-0.3.39
nginx-0.3.39-RELEASE import
*) Feature: the "uninitialized_variable_warn" directive; the logging
level of the "uninitialized variable" message was lowered from
"alert" to "warn".
*) Feature: the "override_charset" directive.
*) Change: now if the unknown variable is used in the "echo" and "if
expr='$name'" SSI-commands, then the "unknown variable" message is
not logged.
*) Bugfix: the active connection counter increased on the exceeding of
the connection limit specified by the "worker_connections"
directive; the bug had appeared in 0.2.0.
*) Bugfix: the limit rate might not work on some condition; the bug had
appeared in 0.3.38.
author | Igor Sysoev <igor@sysoev.ru> |
---|---|
date | Mon, 17 Apr 2006 19:55:41 +0000 |
parents | 3f8a2132b93d |
children | 95d7da23ea53 |
rev | line source |
---|---|
441
da8c5707af39
nginx-0.1.0-2004-09-28-12:34:51 import; set copyright and remove unused files
Igor Sysoev <igor@sysoev.ru>
parents:
399
diff
changeset
|
1 |
da8c5707af39
nginx-0.1.0-2004-09-28-12:34:51 import; set copyright and remove unused files
Igor Sysoev <igor@sysoev.ru>
parents:
399
diff
changeset
|
2 /* |
444
42d11f017717
nginx-0.1.0-2004-09-29-20:00:49 import; remove years from copyright
Igor Sysoev <igor@sysoev.ru>
parents:
441
diff
changeset
|
3 * Copyright (C) Igor Sysoev |
441
da8c5707af39
nginx-0.1.0-2004-09-28-12:34:51 import; set copyright and remove unused files
Igor Sysoev <igor@sysoev.ru>
parents:
399
diff
changeset
|
4 */ |
da8c5707af39
nginx-0.1.0-2004-09-28-12:34:51 import; set copyright and remove unused files
Igor Sysoev <igor@sysoev.ru>
parents:
399
diff
changeset
|
5 |
394
e7a68e14ccd3
nginx-0.0.7-2004-07-16-10:33:35 import
Igor Sysoev <igor@sysoev.ru>
parents:
393
diff
changeset
|
6 |
393
5659d773cfa8
nginx-0.0.7-2004-07-15-20:35:51 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
7 #include <ngx_config.h> |
5659d773cfa8
nginx-0.0.7-2004-07-15-20:35:51 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
8 #include <ngx_core.h> |
394
e7a68e14ccd3
nginx-0.0.7-2004-07-16-10:33:35 import
Igor Sysoev <igor@sysoev.ru>
parents:
393
diff
changeset
|
9 #include <ngx_event.h> |
393
5659d773cfa8
nginx-0.0.7-2004-07-15-20:35:51 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
10 |
541 | 11 |
12 typedef struct { | |
13 ngx_str_t engine; | |
14 } ngx_openssl_conf_t; | |
479 | 15 |
393
5659d773cfa8
nginx-0.0.7-2004-07-15-20:35:51 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
16 |
547 | 17 static void ngx_ssl_handshake_handler(ngx_event_t *ev); |
489 | 18 static ngx_int_t ngx_ssl_handle_recv(ngx_connection_t *c, int n); |
473 | 19 static void ngx_ssl_write_handler(ngx_event_t *wev); |
20 static void ngx_ssl_read_handler(ngx_event_t *rev); | |
577 | 21 static void ngx_ssl_shutdown_handler(ngx_event_t *ev); |
547 | 22 static void ngx_ssl_connection_error(ngx_connection_t *c, int sslerr, |
23 ngx_err_t err, char *text); | |
541 | 24 static void *ngx_openssl_create_conf(ngx_cycle_t *cycle); |
25 static char *ngx_openssl_init_conf(ngx_cycle_t *cycle, void *conf); | |
571 | 26 static void ngx_openssl_exit(ngx_cycle_t *cycle); |
541 | 27 |
28 #if !(NGX_SSL_ENGINE) | |
29 static char *ngx_openssl_noengine(ngx_conf_t *cf, ngx_command_t *cmd, | |
30 void *conf); | |
31 #endif | |
32 | |
33 | |
34 static ngx_command_t ngx_openssl_commands[] = { | |
35 | |
36 { ngx_string("ssl_engine"), | |
37 NGX_MAIN_CONF|NGX_DIRECT_CONF|NGX_CONF_TAKE1, | |
38 #if (NGX_SSL_ENGINE) | |
39 ngx_conf_set_str_slot, | |
40 #else | |
41 ngx_openssl_noengine, | |
42 #endif | |
43 0, | |
44 offsetof(ngx_openssl_conf_t, engine), | |
45 NULL }, | |
46 | |
47 ngx_null_command | |
48 }; | |
49 | |
50 | |
51 static ngx_core_module_t ngx_openssl_module_ctx = { | |
52 ngx_string("openssl"), | |
53 ngx_openssl_create_conf, | |
54 ngx_openssl_init_conf | |
577 | 55 }; |
541 | 56 |
57 | |
58 ngx_module_t ngx_openssl_module = { | |
59 NGX_MODULE_V1, | |
60 &ngx_openssl_module_ctx, /* module context */ | |
61 ngx_openssl_commands, /* module directives */ | |
62 NGX_CORE_MODULE, /* module type */ | |
63 NULL, /* init master */ | |
64 NULL, /* init module */ | |
65 NULL, /* init process */ | |
66 NULL, /* init thread */ | |
67 NULL, /* exit thread */ | |
68 NULL, /* exit process */ | |
571 | 69 ngx_openssl_exit, /* exit master */ |
541 | 70 NGX_MODULE_V1_PADDING |
547 | 71 }; |
72 | |
73 | |
74 static long ngx_ssl_protocols[] = { | |
75 SSL_OP_NO_SSLv2|SSL_OP_NO_SSLv3|SSL_OP_NO_TLSv1, | |
76 SSL_OP_NO_SSLv3|SSL_OP_NO_TLSv1, | |
77 SSL_OP_NO_SSLv2|SSL_OP_NO_TLSv1, | |
78 SSL_OP_NO_TLSv1, | |
79 SSL_OP_NO_SSLv2|SSL_OP_NO_SSLv3, | |
80 SSL_OP_NO_SSLv3, | |
81 SSL_OP_NO_SSLv2, | |
82 0, | |
83 }; | |
397
de797f3b4c27
nginx-0.0.7-2004-07-23-09:37:29 import
Igor Sysoev <igor@sysoev.ru>
parents:
396
diff
changeset
|
84 |
de797f3b4c27
nginx-0.0.7-2004-07-23-09:37:29 import
Igor Sysoev <igor@sysoev.ru>
parents:
396
diff
changeset
|
85 |
489 | 86 ngx_int_t |
87 ngx_ssl_init(ngx_log_t *log) | |
393
5659d773cfa8
nginx-0.0.7-2004-07-15-20:35:51 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
88 { |
5659d773cfa8
nginx-0.0.7-2004-07-15-20:35:51 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
89 SSL_library_init(); |
5659d773cfa8
nginx-0.0.7-2004-07-15-20:35:51 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
90 SSL_load_error_strings(); |
541 | 91 |
92 #if (NGX_SSL_ENGINE) | |
479 | 93 ENGINE_load_builtin_engines(); |
541 | 94 #endif |
393
5659d773cfa8
nginx-0.0.7-2004-07-15-20:35:51 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
95 |
5659d773cfa8
nginx-0.0.7-2004-07-15-20:35:51 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
96 return NGX_OK; |
5659d773cfa8
nginx-0.0.7-2004-07-15-20:35:51 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
97 } |
5659d773cfa8
nginx-0.0.7-2004-07-15-20:35:51 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
98 |
5659d773cfa8
nginx-0.0.7-2004-07-15-20:35:51 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
99 |
489 | 100 ngx_int_t |
547 | 101 ngx_ssl_create(ngx_ssl_t *ssl, ngx_uint_t protocols) |
102 { | |
577 | 103 ssl->ctx = SSL_CTX_new(SSLv23_method()); |
547 | 104 |
105 if (ssl->ctx == NULL) { | |
106 ngx_ssl_error(NGX_LOG_EMERG, ssl->log, 0, "SSL_CTX_new() failed"); | |
107 return NGX_ERROR; | |
108 } | |
109 | |
577 | 110 /* client side options */ |
111 | |
112 SSL_CTX_set_options(ssl->ctx, SSL_OP_MICROSOFT_SESS_ID_BUG); | |
113 SSL_CTX_set_options(ssl->ctx, SSL_OP_NETSCAPE_CHALLENGE_BUG); | |
114 SSL_CTX_set_options(ssl->ctx, SSL_OP_NETSCAPE_REUSE_CIPHER_CHANGE_BUG); | |
115 | |
116 /* server side options */ | |
563 | 117 |
118 SSL_CTX_set_options(ssl->ctx, SSL_OP_SSLREF2_REUSE_CERT_TYPE_BUG); | |
119 SSL_CTX_set_options(ssl->ctx, SSL_OP_MICROSOFT_BIG_SSLV3_BUFFER); | |
120 | |
121 /* this option allow a potential SSL 2.0 rollback (CAN-2005-2969) */ | |
122 SSL_CTX_set_options(ssl->ctx, SSL_OP_MSIE_SSLV2_RSA_PADDING); | |
123 | |
124 SSL_CTX_set_options(ssl->ctx, SSL_OP_SSLEAY_080_CLIENT_DH_BUG); | |
125 SSL_CTX_set_options(ssl->ctx, SSL_OP_TLS_D5_BUG); | |
126 SSL_CTX_set_options(ssl->ctx, SSL_OP_TLS_BLOCK_PADDING_BUG); | |
127 | |
128 #ifdef SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS | |
129 SSL_CTX_set_options(ssl->ctx, SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS); | |
130 #endif | |
131 | |
547 | 132 |
133 if (ngx_ssl_protocols[protocols >> 1] != 0) { | |
134 SSL_CTX_set_options(ssl->ctx, ngx_ssl_protocols[protocols >> 1]); | |
135 } | |
136 | |
137 SSL_CTX_set_mode(ssl->ctx, SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER); | |
138 | |
139 SSL_CTX_set_read_ahead(ssl->ctx, 1); | |
140 | |
141 return NGX_OK; | |
142 } | |
143 | |
144 | |
145 ngx_int_t | |
563 | 146 ngx_ssl_certificate(ngx_conf_t *cf, ngx_ssl_t *ssl, ngx_str_t *cert, |
147 ngx_str_t *key) | |
547 | 148 { |
563 | 149 if (ngx_conf_full_name(cf->cycle, cert) == NGX_ERROR) { |
547 | 150 return NGX_ERROR; |
151 } | |
152 | |
563 | 153 if (SSL_CTX_use_certificate_chain_file(ssl->ctx, (char *) cert->data) |
547 | 154 == 0) |
155 { | |
156 ngx_ssl_error(NGX_LOG_EMERG, ssl->log, 0, | |
563 | 157 "SSL_CTX_use_certificate_chain_file(\"%s\") failed", |
158 cert->data); | |
159 return NGX_ERROR; | |
160 } | |
161 | |
162 if (ngx_conf_full_name(cf->cycle, key) == NGX_ERROR) { | |
163 return NGX_ERROR; | |
164 } | |
165 | |
166 if (SSL_CTX_use_PrivateKey_file(ssl->ctx, (char *) key->data, | |
167 SSL_FILETYPE_PEM) == 0) | |
168 { | |
169 ngx_ssl_error(NGX_LOG_EMERG, ssl->log, 0, | |
170 "SSL_CTX_use_PrivateKey_file(\"%s\") failed", key->data); | |
547 | 171 return NGX_ERROR; |
172 } | |
173 | |
174 return NGX_OK; | |
175 } | |
176 | |
177 | |
178 ngx_int_t | |
179 ngx_ssl_generate_rsa512_key(ngx_ssl_t *ssl) | |
180 { | |
559 | 181 if (SSL_CTX_need_tmp_RSA(ssl->ctx) == 0) { |
182 return NGX_OK; | |
183 } | |
184 | |
547 | 185 ssl->rsa512_key = RSA_generate_key(512, RSA_F4, NULL, NULL); |
186 | |
187 if (ssl->rsa512_key) { | |
188 SSL_CTX_set_tmp_rsa(ssl->ctx, ssl->rsa512_key); | |
189 return NGX_OK; | |
190 } | |
191 | |
192 ngx_ssl_error(NGX_LOG_EMERG, ssl->log, 0, "RSA_generate_key(512) failed"); | |
193 | |
194 return NGX_ERROR; | |
195 } | |
196 | |
197 | |
198 ngx_int_t | |
199 ngx_ssl_create_connection(ngx_ssl_t *ssl, ngx_connection_t *c, ngx_uint_t flags) | |
577 | 200 { |
547 | 201 ngx_ssl_connection_t *sc; |
393
5659d773cfa8
nginx-0.0.7-2004-07-15-20:35:51 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
202 |
547 | 203 sc = ngx_pcalloc(c->pool, sizeof(ngx_ssl_connection_t)); |
204 if (sc == NULL) { | |
393
5659d773cfa8
nginx-0.0.7-2004-07-15-20:35:51 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
205 return NGX_ERROR; |
5659d773cfa8
nginx-0.0.7-2004-07-15-20:35:51 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
206 } |
5659d773cfa8
nginx-0.0.7-2004-07-15-20:35:51 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
207 |
543 | 208 if (flags & NGX_SSL_BUFFER) { |
547 | 209 sc->buffer = 1; |
543 | 210 |
547 | 211 sc->buf = ngx_create_temp_buf(c->pool, NGX_SSL_BUFSIZE); |
212 if (sc->buf == NULL) { | |
543 | 213 return NGX_ERROR; |
214 } | |
397
de797f3b4c27
nginx-0.0.7-2004-07-23-09:37:29 import
Igor Sysoev <igor@sysoev.ru>
parents:
396
diff
changeset
|
215 } |
de797f3b4c27
nginx-0.0.7-2004-07-23-09:37:29 import
Igor Sysoev <igor@sysoev.ru>
parents:
396
diff
changeset
|
216 |
547 | 217 sc->connection = SSL_new(ssl->ctx); |
395
f8f0f1834266
nginx-0.0.7-2004-07-16-21:11:43 import
Igor Sysoev <igor@sysoev.ru>
parents:
394
diff
changeset
|
218 |
547 | 219 if (sc->connection == NULL) { |
395
f8f0f1834266
nginx-0.0.7-2004-07-16-21:11:43 import
Igor Sysoev <igor@sysoev.ru>
parents:
394
diff
changeset
|
220 ngx_ssl_error(NGX_LOG_ALERT, c->log, 0, "SSL_new() failed"); |
393
5659d773cfa8
nginx-0.0.7-2004-07-15-20:35:51 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
221 return NGX_ERROR; |
5659d773cfa8
nginx-0.0.7-2004-07-15-20:35:51 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
222 } |
5659d773cfa8
nginx-0.0.7-2004-07-15-20:35:51 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
223 |
547 | 224 if (SSL_set_fd(sc->connection, c->fd) == 0) { |
395
f8f0f1834266
nginx-0.0.7-2004-07-16-21:11:43 import
Igor Sysoev <igor@sysoev.ru>
parents:
394
diff
changeset
|
225 ngx_ssl_error(NGX_LOG_ALERT, c->log, 0, "SSL_set_fd() failed"); |
f8f0f1834266
nginx-0.0.7-2004-07-16-21:11:43 import
Igor Sysoev <igor@sysoev.ru>
parents:
394
diff
changeset
|
226 return NGX_ERROR; |
f8f0f1834266
nginx-0.0.7-2004-07-16-21:11:43 import
Igor Sysoev <igor@sysoev.ru>
parents:
394
diff
changeset
|
227 } |
f8f0f1834266
nginx-0.0.7-2004-07-16-21:11:43 import
Igor Sysoev <igor@sysoev.ru>
parents:
394
diff
changeset
|
228 |
577 | 229 if (flags & NGX_SSL_CLIENT) { |
230 SSL_set_connect_state(sc->connection); | |
231 | |
232 } else { | |
233 SSL_set_accept_state(sc->connection); | |
234 } | |
393
5659d773cfa8
nginx-0.0.7-2004-07-15-20:35:51 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
235 |
547 | 236 c->ssl = sc; |
393
5659d773cfa8
nginx-0.0.7-2004-07-15-20:35:51 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
237 |
5659d773cfa8
nginx-0.0.7-2004-07-15-20:35:51 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
238 return NGX_OK; |
5659d773cfa8
nginx-0.0.7-2004-07-15-20:35:51 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
239 } |
5659d773cfa8
nginx-0.0.7-2004-07-15-20:35:51 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
240 |
5659d773cfa8
nginx-0.0.7-2004-07-15-20:35:51 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
241 |
547 | 242 ngx_int_t |
577 | 243 ngx_ssl_set_session(ngx_connection_t *c, ngx_ssl_session_t *session) |
244 { | |
245 if (session) { | |
246 if (SSL_set_session(c->ssl->connection, session) == 0) { | |
247 ngx_ssl_error(NGX_LOG_ALERT, c->log, 0, "SSL_set_session() failed"); | |
248 return NGX_ERROR; | |
249 } | |
250 } | |
251 | |
252 return NGX_OK; | |
253 } | |
254 | |
255 | |
256 ngx_int_t | |
547 | 257 ngx_ssl_handshake(ngx_connection_t *c) |
258 { | |
259 int n, sslerr; | |
260 ngx_err_t err; | |
261 | |
262 n = SSL_do_handshake(c->ssl->connection); | |
263 | |
577 | 264 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, c->log, 0, "SSL_do_handshake: %d", n); |
547 | 265 |
266 if (n == 1) { | |
267 | |
268 if (ngx_handle_read_event(c->read, 0) == NGX_ERROR) { | |
269 return NGX_ERROR; | |
270 } | |
271 | |
272 if (ngx_handle_write_event(c->write, 0) == NGX_ERROR) { | |
273 return NGX_ERROR; | |
274 } | |
275 | |
276 #if (NGX_DEBUG) | |
277 { | |
278 char buf[129], *s, *d; | |
279 SSL_CIPHER *cipher; | |
280 | |
281 cipher = SSL_get_current_cipher(c->ssl->connection); | |
282 | |
283 if (cipher) { | |
284 SSL_CIPHER_description(cipher, &buf[1], 128); | |
285 | |
286 for (s = &buf[1], d = buf; *s; s++) { | |
287 if (*s == ' ' && *d == ' ') { | |
288 continue; | |
289 } | |
290 | |
291 if (*s == LF || *s == CR) { | |
292 continue; | |
293 } | |
294 | |
295 *++d = *s; | |
296 } | |
297 | |
298 if (*d != ' ') { | |
299 d++; | |
300 } | |
301 | |
302 *d = '\0'; | |
303 | |
583 | 304 ngx_log_debug2(NGX_LOG_DEBUG_EVENT, c->log, 0, |
547 | 305 "SSL: %s, cipher: \"%s\"", |
577 | 306 SSL_get_version(c->ssl->connection), &buf[1]); |
547 | 307 |
308 if (SSL_session_reused(c->ssl->connection)) { | |
583 | 309 ngx_log_debug0(NGX_LOG_DEBUG_EVENT, c->log, 0, |
547 | 310 "SSL reused session"); |
311 } | |
312 | |
313 } else { | |
314 ngx_log_debug0(NGX_LOG_DEBUG_EVENT, c->log, 0, | |
577 | 315 "SSL no shared ciphers"); |
547 | 316 } |
317 } | |
318 #endif | |
319 | |
320 c->ssl->handshaked = 1; | |
321 | |
322 c->recv = ngx_ssl_recv; | |
323 c->send = ngx_ssl_write; | |
577 | 324 c->recv_chain = ngx_ssl_recv_chain; |
325 c->send_chain = ngx_ssl_send_chain; | |
547 | 326 |
327 return NGX_OK; | |
328 } | |
329 | |
330 sslerr = SSL_get_error(c->ssl->connection, n); | |
331 | |
332 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, c->log, 0, "SSL_get_error: %d", sslerr); | |
333 | |
334 if (sslerr == SSL_ERROR_WANT_READ) { | |
335 c->read->ready = 0; | |
336 c->read->handler = ngx_ssl_handshake_handler; | |
591 | 337 c->write->handler = ngx_ssl_handshake_handler; |
547 | 338 |
339 if (ngx_handle_read_event(c->read, 0) == NGX_ERROR) { | |
340 return NGX_ERROR; | |
341 } | |
342 | |
343 return NGX_AGAIN; | |
344 } | |
345 | |
346 if (sslerr == SSL_ERROR_WANT_WRITE) { | |
347 c->write->ready = 0; | |
591 | 348 c->read->handler = ngx_ssl_handshake_handler; |
547 | 349 c->write->handler = ngx_ssl_handshake_handler; |
350 | |
351 if (ngx_handle_write_event(c->write, 0) == NGX_ERROR) { | |
352 return NGX_ERROR; | |
353 } | |
354 | |
355 return NGX_AGAIN; | |
356 } | |
357 | |
358 err = (sslerr == SSL_ERROR_SYSCALL) ? ngx_errno : 0; | |
359 | |
360 c->ssl->no_wait_shutdown = 1; | |
361 c->ssl->no_send_shutdown = 1; | |
591 | 362 c->read->eof = 1; |
547 | 363 |
364 if (sslerr == SSL_ERROR_ZERO_RETURN || ERR_peek_error() == 0) { | |
365 ngx_log_error(NGX_LOG_INFO, c->log, err, | |
577 | 366 "peer closed connection in SSL handshake"); |
547 | 367 |
368 return NGX_ERROR; | |
369 } | |
370 | |
591 | 371 c->read->error = 1; |
372 | |
547 | 373 ngx_ssl_connection_error(c, sslerr, err, "SSL_do_handshake() failed"); |
374 | |
375 return NGX_ERROR; | |
376 } | |
377 | |
378 | |
379 static void | |
380 ngx_ssl_handshake_handler(ngx_event_t *ev) | |
381 { | |
382 ngx_connection_t *c; | |
383 | |
384 c = ev->data; | |
385 | |
549 | 386 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, c->log, 0, |
577 | 387 "SSL handshake handler: %d", ev->write); |
547 | 388 |
591 | 389 if (ev->timedout) { |
390 c->ssl->handler(c); | |
391 return; | |
392 } | |
393 | |
547 | 394 if (ngx_ssl_handshake(c) == NGX_AGAIN) { |
395 return; | |
396 } | |
397 | |
398 c->ssl->handler(c); | |
399 } | |
400 | |
401 | |
489 | 402 ssize_t |
577 | 403 ngx_ssl_recv_chain(ngx_connection_t *c, ngx_chain_t *cl) |
404 { | |
405 ssize_t n, bytes; | |
406 ngx_buf_t *b; | |
407 | |
408 bytes = 0; | |
409 | |
410 while (cl) { | |
411 b = cl->buf; | |
412 | |
413 n = ngx_ssl_recv(c, b->last, b->end - b->last); | |
414 | |
415 if (n > 0) { | |
416 b->last += n; | |
417 bytes += n; | |
418 | |
419 if (b->last == b->end) { | |
420 cl = cl->next; | |
421 } | |
422 | |
423 continue; | |
424 } | |
425 | |
426 if (bytes) { | |
427 return bytes; | |
428 } | |
429 | |
430 return n; | |
431 } | |
432 | |
433 return bytes; | |
434 } | |
435 | |
436 | |
437 ssize_t | |
489 | 438 ngx_ssl_recv(ngx_connection_t *c, u_char *buf, size_t size) |
393
5659d773cfa8
nginx-0.0.7-2004-07-15-20:35:51 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
439 { |
489 | 440 int n, bytes; |
441 | |
442 if (c->ssl->last == NGX_ERROR) { | |
443 return NGX_ERROR; | |
444 } | |
445 | |
577 | 446 if (c->ssl->last == NGX_DONE) { |
447 return 0; | |
448 } | |
449 | |
489 | 450 bytes = 0; |
393
5659d773cfa8
nginx-0.0.7-2004-07-15-20:35:51 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
451 |
489 | 452 /* |
453 * SSL_read() may return data in parts, so try to read | |
454 * until SSL_read() would return no data | |
455 */ | |
456 | |
457 for ( ;; ) { | |
393
5659d773cfa8
nginx-0.0.7-2004-07-15-20:35:51 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
458 |
543 | 459 n = SSL_read(c->ssl->connection, buf, size); |
489 | 460 |
577 | 461 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, c->log, 0, "SSL_read: %d", n); |
393
5659d773cfa8
nginx-0.0.7-2004-07-15-20:35:51 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
462 |
489 | 463 if (n > 0) { |
464 bytes += n; | |
465 } | |
466 | |
467 c->ssl->last = ngx_ssl_handle_recv(c, n); | |
468 | |
469 if (c->ssl->last != NGX_OK) { | |
479 | 470 |
489 | 471 if (bytes) { |
472 return bytes; | |
577 | 473 } |
489 | 474 |
577 | 475 if (c->ssl->last == NGX_DONE) { |
476 return 0; | |
479 | 477 } |
577 | 478 |
479 return c->ssl->last; | |
479 | 480 } |
481 | |
489 | 482 size -= n; |
483 | |
484 if (size == 0) { | |
485 return bytes; | |
486 } | |
487 | |
488 buf += n; | |
489 } | |
490 } | |
491 | |
492 | |
493 static ngx_int_t | |
494 ngx_ssl_handle_recv(ngx_connection_t *c, int n) | |
495 { | |
547 | 496 int sslerr; |
497 ngx_err_t err; | |
489 | 498 |
499 if (n > 0) { | |
479 | 500 |
473 | 501 if (c->ssl->saved_write_handler) { |
502 | |
509 | 503 c->write->handler = c->ssl->saved_write_handler; |
473 | 504 c->ssl->saved_write_handler = NULL; |
505 c->write->ready = 1; | |
506 | |
507 if (ngx_handle_write_event(c->write, 0) == NGX_ERROR) { | |
508 return NGX_ERROR; | |
509 } | |
510 | |
563 | 511 ngx_post_event(c->write, &ngx_posted_events); |
473 | 512 } |
513 | |
489 | 514 return NGX_OK; |
393
5659d773cfa8
nginx-0.0.7-2004-07-15-20:35:51 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
515 } |
5659d773cfa8
nginx-0.0.7-2004-07-15-20:35:51 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
516 |
543 | 517 sslerr = SSL_get_error(c->ssl->connection, n); |
395
f8f0f1834266
nginx-0.0.7-2004-07-16-21:11:43 import
Igor Sysoev <igor@sysoev.ru>
parents:
394
diff
changeset
|
518 |
396
6f3b20c1ac50
nginx-0.0.7-2004-07-18-23:11:20 import
Igor Sysoev <igor@sysoev.ru>
parents:
395
diff
changeset
|
519 err = (sslerr == SSL_ERROR_SYSCALL) ? ngx_errno : 0; |
393
5659d773cfa8
nginx-0.0.7-2004-07-15-20:35:51 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
520 |
396
6f3b20c1ac50
nginx-0.0.7-2004-07-18-23:11:20 import
Igor Sysoev <igor@sysoev.ru>
parents:
395
diff
changeset
|
521 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, c->log, 0, "SSL_get_error: %d", sslerr); |
393
5659d773cfa8
nginx-0.0.7-2004-07-15-20:35:51 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
522 |
395
f8f0f1834266
nginx-0.0.7-2004-07-16-21:11:43 import
Igor Sysoev <igor@sysoev.ru>
parents:
394
diff
changeset
|
523 if (sslerr == SSL_ERROR_WANT_READ) { |
455 | 524 c->read->ready = 0; |
393
5659d773cfa8
nginx-0.0.7-2004-07-15-20:35:51 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
525 return NGX_AGAIN; |
5659d773cfa8
nginx-0.0.7-2004-07-15-20:35:51 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
526 } |
394
e7a68e14ccd3
nginx-0.0.7-2004-07-16-10:33:35 import
Igor Sysoev <igor@sysoev.ru>
parents:
393
diff
changeset
|
527 |
445
f26432a1935a
nginx-0.1.0-2004-09-30-10:38:49 import
Igor Sysoev <igor@sysoev.ru>
parents:
444
diff
changeset
|
528 if (sslerr == SSL_ERROR_WANT_WRITE) { |
539 | 529 |
547 | 530 ngx_log_error(NGX_LOG_INFO, c->log, 0, |
577 | 531 "peer started SSL renegotiation"); |
473 | 532 |
533 c->write->ready = 0; | |
534 | |
535 if (ngx_handle_write_event(c->write, 0) == NGX_ERROR) { | |
536 return NGX_ERROR; | |
537 } | |
538 | |
539 /* | |
540 * we do not set the timer because there is already the read event timer | |
541 */ | |
542 | |
543 if (c->ssl->saved_write_handler == NULL) { | |
509 | 544 c->ssl->saved_write_handler = c->write->handler; |
545 c->write->handler = ngx_ssl_write_handler; | |
473 | 546 } |
547 | |
393
5659d773cfa8
nginx-0.0.7-2004-07-15-20:35:51 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
548 return NGX_AGAIN; |
5659d773cfa8
nginx-0.0.7-2004-07-15-20:35:51 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
549 } |
5659d773cfa8
nginx-0.0.7-2004-07-15-20:35:51 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
550 |
547 | 551 c->ssl->no_wait_shutdown = 1; |
552 c->ssl->no_send_shutdown = 1; | |
589 | 553 c->read->eof = 1; |
396
6f3b20c1ac50
nginx-0.0.7-2004-07-18-23:11:20 import
Igor Sysoev <igor@sysoev.ru>
parents:
395
diff
changeset
|
554 |
395
f8f0f1834266
nginx-0.0.7-2004-07-16-21:11:43 import
Igor Sysoev <igor@sysoev.ru>
parents:
394
diff
changeset
|
555 if (sslerr == SSL_ERROR_ZERO_RETURN || ERR_peek_error() == 0) { |
577 | 556 ngx_log_debug0(NGX_LOG_DEBUG_EVENT, c->log, 0, |
557 "peer shutdown SSL cleanly"); | |
558 return NGX_DONE; | |
393
5659d773cfa8
nginx-0.0.7-2004-07-15-20:35:51 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
559 } |
5659d773cfa8
nginx-0.0.7-2004-07-15-20:35:51 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
560 |
591 | 561 c->read->error = 1; |
547 | 562 ngx_ssl_connection_error(c, sslerr, err, "SSL_read() failed"); |
393
5659d773cfa8
nginx-0.0.7-2004-07-15-20:35:51 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
563 |
5659d773cfa8
nginx-0.0.7-2004-07-15-20:35:51 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
564 return NGX_ERROR; |
5659d773cfa8
nginx-0.0.7-2004-07-15-20:35:51 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
565 } |
5659d773cfa8
nginx-0.0.7-2004-07-15-20:35:51 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
566 |
5659d773cfa8
nginx-0.0.7-2004-07-15-20:35:51 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
567 |
489 | 568 static void |
569 ngx_ssl_write_handler(ngx_event_t *wev) | |
473 | 570 { |
571 ngx_connection_t *c; | |
572 | |
573 c = wev->data; | |
547 | 574 |
509 | 575 c->read->handler(c->read); |
473 | 576 } |
577 | |
578 | |
398
201b5f68b59f
nginx-0.0.7-2004-07-23-21:05:37 import
Igor Sysoev <igor@sysoev.ru>
parents:
397
diff
changeset
|
579 /* |
201b5f68b59f
nginx-0.0.7-2004-07-23-21:05:37 import
Igor Sysoev <igor@sysoev.ru>
parents:
397
diff
changeset
|
580 * OpenSSL has no SSL_writev() so we copy several bufs into our 16K buffer |
473 | 581 * before the SSL_write() call to decrease a SSL overhead. |
398
201b5f68b59f
nginx-0.0.7-2004-07-23-21:05:37 import
Igor Sysoev <igor@sysoev.ru>
parents:
397
diff
changeset
|
582 * |
201b5f68b59f
nginx-0.0.7-2004-07-23-21:05:37 import
Igor Sysoev <igor@sysoev.ru>
parents:
397
diff
changeset
|
583 * Besides for protocols such as HTTP it is possible to always buffer |
201b5f68b59f
nginx-0.0.7-2004-07-23-21:05:37 import
Igor Sysoev <igor@sysoev.ru>
parents:
397
diff
changeset
|
584 * the output to decrease a SSL overhead some more. |
201b5f68b59f
nginx-0.0.7-2004-07-23-21:05:37 import
Igor Sysoev <igor@sysoev.ru>
parents:
397
diff
changeset
|
585 */ |
201b5f68b59f
nginx-0.0.7-2004-07-23-21:05:37 import
Igor Sysoev <igor@sysoev.ru>
parents:
397
diff
changeset
|
586 |
489 | 587 ngx_chain_t * |
588 ngx_ssl_send_chain(ngx_connection_t *c, ngx_chain_t *in, off_t limit) | |
394
e7a68e14ccd3
nginx-0.0.7-2004-07-16-10:33:35 import
Igor Sysoev <igor@sysoev.ru>
parents:
393
diff
changeset
|
589 { |
397
de797f3b4c27
nginx-0.0.7-2004-07-23-09:37:29 import
Igor Sysoev <igor@sysoev.ru>
parents:
396
diff
changeset
|
590 int n; |
399
4e21d1291a14
nginx-0.0.7-2004-07-25-22:34:14 import
Igor Sysoev <igor@sysoev.ru>
parents:
398
diff
changeset
|
591 ngx_uint_t flush; |
397
de797f3b4c27
nginx-0.0.7-2004-07-23-09:37:29 import
Igor Sysoev <igor@sysoev.ru>
parents:
396
diff
changeset
|
592 ssize_t send, size; |
de797f3b4c27
nginx-0.0.7-2004-07-23-09:37:29 import
Igor Sysoev <igor@sysoev.ru>
parents:
396
diff
changeset
|
593 ngx_buf_t *buf; |
394
e7a68e14ccd3
nginx-0.0.7-2004-07-16-10:33:35 import
Igor Sysoev <igor@sysoev.ru>
parents:
393
diff
changeset
|
594 |
597 | 595 if (!c->ssl->buffer |
596 || (in && in->next == NULL && !(c->buffered & NGX_SSL_BUFFERED))) | |
597 { | |
395
f8f0f1834266
nginx-0.0.7-2004-07-16-21:11:43 import
Igor Sysoev <igor@sysoev.ru>
parents:
394
diff
changeset
|
598 |
397
de797f3b4c27
nginx-0.0.7-2004-07-23-09:37:29 import
Igor Sysoev <igor@sysoev.ru>
parents:
396
diff
changeset
|
599 /* |
577 | 600 * we avoid a buffer copy if |
601 * we do not need to buffer the output | |
602 * or the incoming buf is a single and our buffer is empty | |
397
de797f3b4c27
nginx-0.0.7-2004-07-23-09:37:29 import
Igor Sysoev <igor@sysoev.ru>
parents:
396
diff
changeset
|
603 */ |
395
f8f0f1834266
nginx-0.0.7-2004-07-16-21:11:43 import
Igor Sysoev <igor@sysoev.ru>
parents:
394
diff
changeset
|
604 |
577 | 605 while (in) { |
606 if (ngx_buf_special(in->buf)) { | |
607 in = in->next; | |
608 continue; | |
609 } | |
397
de797f3b4c27
nginx-0.0.7-2004-07-23-09:37:29 import
Igor Sysoev <igor@sysoev.ru>
parents:
396
diff
changeset
|
610 |
577 | 611 n = ngx_ssl_write(c, in->buf->pos, in->buf->last - in->buf->pos); |
612 | |
613 if (n == NGX_ERROR) { | |
614 return NGX_CHAIN_ERROR; | |
615 } | |
398
201b5f68b59f
nginx-0.0.7-2004-07-23-21:05:37 import
Igor Sysoev <igor@sysoev.ru>
parents:
397
diff
changeset
|
616 |
577 | 617 if (n == NGX_AGAIN) { |
597 | 618 c->buffered |= NGX_SSL_BUFFERED; |
577 | 619 return in; |
620 } | |
621 | |
622 in->buf->pos += n; | |
623 | |
624 if (in->buf->pos == in->buf->last) { | |
625 in = in->next; | |
626 } | |
397
de797f3b4c27
nginx-0.0.7-2004-07-23-09:37:29 import
Igor Sysoev <igor@sysoev.ru>
parents:
396
diff
changeset
|
627 } |
395
f8f0f1834266
nginx-0.0.7-2004-07-16-21:11:43 import
Igor Sysoev <igor@sysoev.ru>
parents:
394
diff
changeset
|
628 |
397
de797f3b4c27
nginx-0.0.7-2004-07-23-09:37:29 import
Igor Sysoev <igor@sysoev.ru>
parents:
396
diff
changeset
|
629 return in; |
de797f3b4c27
nginx-0.0.7-2004-07-23-09:37:29 import
Igor Sysoev <igor@sysoev.ru>
parents:
396
diff
changeset
|
630 } |
395
f8f0f1834266
nginx-0.0.7-2004-07-16-21:11:43 import
Igor Sysoev <igor@sysoev.ru>
parents:
394
diff
changeset
|
631 |
473 | 632 |
633 /* the maximum limit size is the maximum uint32_t value - the page size */ | |
634 | |
635 if (limit == 0 || limit > NGX_MAX_UINT32_VALUE - ngx_pagesize) { | |
636 limit = NGX_MAX_UINT32_VALUE - ngx_pagesize; | |
637 } | |
638 | |
639 | |
577 | 640 buf = c->ssl->buf; |
397
de797f3b4c27
nginx-0.0.7-2004-07-23-09:37:29 import
Igor Sysoev <igor@sysoev.ru>
parents:
396
diff
changeset
|
641 send = 0; |
de797f3b4c27
nginx-0.0.7-2004-07-23-09:37:29 import
Igor Sysoev <igor@sysoev.ru>
parents:
396
diff
changeset
|
642 flush = (in == NULL) ? 1 : 0; |
395
f8f0f1834266
nginx-0.0.7-2004-07-16-21:11:43 import
Igor Sysoev <igor@sysoev.ru>
parents:
394
diff
changeset
|
643 |
397
de797f3b4c27
nginx-0.0.7-2004-07-23-09:37:29 import
Igor Sysoev <igor@sysoev.ru>
parents:
396
diff
changeset
|
644 for ( ;; ) { |
395
f8f0f1834266
nginx-0.0.7-2004-07-16-21:11:43 import
Igor Sysoev <igor@sysoev.ru>
parents:
394
diff
changeset
|
645 |
397
de797f3b4c27
nginx-0.0.7-2004-07-23-09:37:29 import
Igor Sysoev <igor@sysoev.ru>
parents:
396
diff
changeset
|
646 while (in && buf->last < buf->end) { |
583 | 647 if (in->buf->last_buf || in->buf->flush) { |
397
de797f3b4c27
nginx-0.0.7-2004-07-23-09:37:29 import
Igor Sysoev <igor@sysoev.ru>
parents:
396
diff
changeset
|
648 flush = 1; |
395
f8f0f1834266
nginx-0.0.7-2004-07-16-21:11:43 import
Igor Sysoev <igor@sysoev.ru>
parents:
394
diff
changeset
|
649 } |
f8f0f1834266
nginx-0.0.7-2004-07-16-21:11:43 import
Igor Sysoev <igor@sysoev.ru>
parents:
394
diff
changeset
|
650 |
397
de797f3b4c27
nginx-0.0.7-2004-07-23-09:37:29 import
Igor Sysoev <igor@sysoev.ru>
parents:
396
diff
changeset
|
651 if (ngx_buf_special(in->buf)) { |
398
201b5f68b59f
nginx-0.0.7-2004-07-23-21:05:37 import
Igor Sysoev <igor@sysoev.ru>
parents:
397
diff
changeset
|
652 in = in->next; |
397
de797f3b4c27
nginx-0.0.7-2004-07-23-09:37:29 import
Igor Sysoev <igor@sysoev.ru>
parents:
396
diff
changeset
|
653 continue; |
de797f3b4c27
nginx-0.0.7-2004-07-23-09:37:29 import
Igor Sysoev <igor@sysoev.ru>
parents:
396
diff
changeset
|
654 } |
de797f3b4c27
nginx-0.0.7-2004-07-23-09:37:29 import
Igor Sysoev <igor@sysoev.ru>
parents:
396
diff
changeset
|
655 |
de797f3b4c27
nginx-0.0.7-2004-07-23-09:37:29 import
Igor Sysoev <igor@sysoev.ru>
parents:
396
diff
changeset
|
656 size = in->buf->last - in->buf->pos; |
de797f3b4c27
nginx-0.0.7-2004-07-23-09:37:29 import
Igor Sysoev <igor@sysoev.ru>
parents:
396
diff
changeset
|
657 |
de797f3b4c27
nginx-0.0.7-2004-07-23-09:37:29 import
Igor Sysoev <igor@sysoev.ru>
parents:
396
diff
changeset
|
658 if (size > buf->end - buf->last) { |
de797f3b4c27
nginx-0.0.7-2004-07-23-09:37:29 import
Igor Sysoev <igor@sysoev.ru>
parents:
396
diff
changeset
|
659 size = buf->end - buf->last; |
de797f3b4c27
nginx-0.0.7-2004-07-23-09:37:29 import
Igor Sysoev <igor@sysoev.ru>
parents:
396
diff
changeset
|
660 } |
de797f3b4c27
nginx-0.0.7-2004-07-23-09:37:29 import
Igor Sysoev <igor@sysoev.ru>
parents:
396
diff
changeset
|
661 |
395
f8f0f1834266
nginx-0.0.7-2004-07-16-21:11:43 import
Igor Sysoev <igor@sysoev.ru>
parents:
394
diff
changeset
|
662 if (send + size > limit) { |
577 | 663 size = (ssize_t) (limit - send); |
397
de797f3b4c27
nginx-0.0.7-2004-07-23-09:37:29 import
Igor Sysoev <igor@sysoev.ru>
parents:
396
diff
changeset
|
664 flush = 1; |
395
f8f0f1834266
nginx-0.0.7-2004-07-16-21:11:43 import
Igor Sysoev <igor@sysoev.ru>
parents:
394
diff
changeset
|
665 } |
f8f0f1834266
nginx-0.0.7-2004-07-16-21:11:43 import
Igor Sysoev <igor@sysoev.ru>
parents:
394
diff
changeset
|
666 |
f8f0f1834266
nginx-0.0.7-2004-07-16-21:11:43 import
Igor Sysoev <igor@sysoev.ru>
parents:
394
diff
changeset
|
667 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, c->log, 0, |
397
de797f3b4c27
nginx-0.0.7-2004-07-23-09:37:29 import
Igor Sysoev <igor@sysoev.ru>
parents:
396
diff
changeset
|
668 "SSL buf copy: %d", size); |
395
f8f0f1834266
nginx-0.0.7-2004-07-16-21:11:43 import
Igor Sysoev <igor@sysoev.ru>
parents:
394
diff
changeset
|
669 |
397
de797f3b4c27
nginx-0.0.7-2004-07-23-09:37:29 import
Igor Sysoev <igor@sysoev.ru>
parents:
396
diff
changeset
|
670 ngx_memcpy(buf->last, in->buf->pos, size); |
395
f8f0f1834266
nginx-0.0.7-2004-07-16-21:11:43 import
Igor Sysoev <igor@sysoev.ru>
parents:
394
diff
changeset
|
671 |
397
de797f3b4c27
nginx-0.0.7-2004-07-23-09:37:29 import
Igor Sysoev <igor@sysoev.ru>
parents:
396
diff
changeset
|
672 buf->last += size; |
395
f8f0f1834266
nginx-0.0.7-2004-07-16-21:11:43 import
Igor Sysoev <igor@sysoev.ru>
parents:
394
diff
changeset
|
673 |
397
de797f3b4c27
nginx-0.0.7-2004-07-23-09:37:29 import
Igor Sysoev <igor@sysoev.ru>
parents:
396
diff
changeset
|
674 in->buf->pos += size; |
577 | 675 |
397
de797f3b4c27
nginx-0.0.7-2004-07-23-09:37:29 import
Igor Sysoev <igor@sysoev.ru>
parents:
396
diff
changeset
|
676 if (in->buf->pos == in->buf->last) { |
de797f3b4c27
nginx-0.0.7-2004-07-23-09:37:29 import
Igor Sysoev <igor@sysoev.ru>
parents:
396
diff
changeset
|
677 in = in->next; |
395
f8f0f1834266
nginx-0.0.7-2004-07-16-21:11:43 import
Igor Sysoev <igor@sysoev.ru>
parents:
394
diff
changeset
|
678 } |
f8f0f1834266
nginx-0.0.7-2004-07-16-21:11:43 import
Igor Sysoev <igor@sysoev.ru>
parents:
394
diff
changeset
|
679 } |
f8f0f1834266
nginx-0.0.7-2004-07-16-21:11:43 import
Igor Sysoev <igor@sysoev.ru>
parents:
394
diff
changeset
|
680 |
397
de797f3b4c27
nginx-0.0.7-2004-07-23-09:37:29 import
Igor Sysoev <igor@sysoev.ru>
parents:
396
diff
changeset
|
681 size = buf->last - buf->pos; |
de797f3b4c27
nginx-0.0.7-2004-07-23-09:37:29 import
Igor Sysoev <igor@sysoev.ru>
parents:
396
diff
changeset
|
682 |
398
201b5f68b59f
nginx-0.0.7-2004-07-23-21:05:37 import
Igor Sysoev <igor@sysoev.ru>
parents:
397
diff
changeset
|
683 if (!flush && buf->last < buf->end && c->ssl->buffer) { |
201b5f68b59f
nginx-0.0.7-2004-07-23-21:05:37 import
Igor Sysoev <igor@sysoev.ru>
parents:
397
diff
changeset
|
684 break; |
201b5f68b59f
nginx-0.0.7-2004-07-23-21:05:37 import
Igor Sysoev <igor@sysoev.ru>
parents:
397
diff
changeset
|
685 } |
397
de797f3b4c27
nginx-0.0.7-2004-07-23-09:37:29 import
Igor Sysoev <igor@sysoev.ru>
parents:
396
diff
changeset
|
686 |
398
201b5f68b59f
nginx-0.0.7-2004-07-23-21:05:37 import
Igor Sysoev <igor@sysoev.ru>
parents:
397
diff
changeset
|
687 n = ngx_ssl_write(c, buf->pos, size); |
201b5f68b59f
nginx-0.0.7-2004-07-23-21:05:37 import
Igor Sysoev <igor@sysoev.ru>
parents:
397
diff
changeset
|
688 |
201b5f68b59f
nginx-0.0.7-2004-07-23-21:05:37 import
Igor Sysoev <igor@sysoev.ru>
parents:
397
diff
changeset
|
689 if (n == NGX_ERROR) { |
201b5f68b59f
nginx-0.0.7-2004-07-23-21:05:37 import
Igor Sysoev <igor@sysoev.ru>
parents:
397
diff
changeset
|
690 return NGX_CHAIN_ERROR; |
397
de797f3b4c27
nginx-0.0.7-2004-07-23-09:37:29 import
Igor Sysoev <igor@sysoev.ru>
parents:
396
diff
changeset
|
691 } |
de797f3b4c27
nginx-0.0.7-2004-07-23-09:37:29 import
Igor Sysoev <igor@sysoev.ru>
parents:
396
diff
changeset
|
692 |
511 | 693 if (n == NGX_AGAIN) { |
597 | 694 c->buffered |= NGX_SSL_BUFFERED; |
511 | 695 return in; |
397
de797f3b4c27
nginx-0.0.7-2004-07-23-09:37:29 import
Igor Sysoev <igor@sysoev.ru>
parents:
396
diff
changeset
|
696 } |
de797f3b4c27
nginx-0.0.7-2004-07-23-09:37:29 import
Igor Sysoev <igor@sysoev.ru>
parents:
396
diff
changeset
|
697 |
de797f3b4c27
nginx-0.0.7-2004-07-23-09:37:29 import
Igor Sysoev <igor@sysoev.ru>
parents:
396
diff
changeset
|
698 buf->pos += n; |
de797f3b4c27
nginx-0.0.7-2004-07-23-09:37:29 import
Igor Sysoev <igor@sysoev.ru>
parents:
396
diff
changeset
|
699 send += n; |
de797f3b4c27
nginx-0.0.7-2004-07-23-09:37:29 import
Igor Sysoev <igor@sysoev.ru>
parents:
396
diff
changeset
|
700 c->sent += n; |
de797f3b4c27
nginx-0.0.7-2004-07-23-09:37:29 import
Igor Sysoev <igor@sysoev.ru>
parents:
396
diff
changeset
|
701 |
de797f3b4c27
nginx-0.0.7-2004-07-23-09:37:29 import
Igor Sysoev <igor@sysoev.ru>
parents:
396
diff
changeset
|
702 if (n < size) { |
de797f3b4c27
nginx-0.0.7-2004-07-23-09:37:29 import
Igor Sysoev <igor@sysoev.ru>
parents:
396
diff
changeset
|
703 break; |
395
f8f0f1834266
nginx-0.0.7-2004-07-16-21:11:43 import
Igor Sysoev <igor@sysoev.ru>
parents:
394
diff
changeset
|
704 } |
f8f0f1834266
nginx-0.0.7-2004-07-16-21:11:43 import
Igor Sysoev <igor@sysoev.ru>
parents:
394
diff
changeset
|
705 |
f8f0f1834266
nginx-0.0.7-2004-07-16-21:11:43 import
Igor Sysoev <igor@sysoev.ru>
parents:
394
diff
changeset
|
706 if (buf->pos == buf->last) { |
397
de797f3b4c27
nginx-0.0.7-2004-07-23-09:37:29 import
Igor Sysoev <igor@sysoev.ru>
parents:
396
diff
changeset
|
707 buf->pos = buf->start; |
de797f3b4c27
nginx-0.0.7-2004-07-23-09:37:29 import
Igor Sysoev <igor@sysoev.ru>
parents:
396
diff
changeset
|
708 buf->last = buf->start; |
de797f3b4c27
nginx-0.0.7-2004-07-23-09:37:29 import
Igor Sysoev <igor@sysoev.ru>
parents:
396
diff
changeset
|
709 } |
395
f8f0f1834266
nginx-0.0.7-2004-07-16-21:11:43 import
Igor Sysoev <igor@sysoev.ru>
parents:
394
diff
changeset
|
710 |
397
de797f3b4c27
nginx-0.0.7-2004-07-23-09:37:29 import
Igor Sysoev <igor@sysoev.ru>
parents:
396
diff
changeset
|
711 if (in == NULL || send == limit) { |
de797f3b4c27
nginx-0.0.7-2004-07-23-09:37:29 import
Igor Sysoev <igor@sysoev.ru>
parents:
396
diff
changeset
|
712 break; |
395
f8f0f1834266
nginx-0.0.7-2004-07-16-21:11:43 import
Igor Sysoev <igor@sysoev.ru>
parents:
394
diff
changeset
|
713 } |
f8f0f1834266
nginx-0.0.7-2004-07-16-21:11:43 import
Igor Sysoev <igor@sysoev.ru>
parents:
394
diff
changeset
|
714 } |
f8f0f1834266
nginx-0.0.7-2004-07-16-21:11:43 import
Igor Sysoev <igor@sysoev.ru>
parents:
394
diff
changeset
|
715 |
597 | 716 if (buf->pos < buf->last) { |
717 c->buffered |= NGX_SSL_BUFFERED; | |
718 | |
719 } else { | |
720 c->buffered &= ~NGX_SSL_BUFFERED; | |
721 } | |
397
de797f3b4c27
nginx-0.0.7-2004-07-23-09:37:29 import
Igor Sysoev <igor@sysoev.ru>
parents:
396
diff
changeset
|
722 |
399
4e21d1291a14
nginx-0.0.7-2004-07-25-22:34:14 import
Igor Sysoev <igor@sysoev.ru>
parents:
398
diff
changeset
|
723 return in; |
397
de797f3b4c27
nginx-0.0.7-2004-07-23-09:37:29 import
Igor Sysoev <igor@sysoev.ru>
parents:
396
diff
changeset
|
724 } |
de797f3b4c27
nginx-0.0.7-2004-07-23-09:37:29 import
Igor Sysoev <igor@sysoev.ru>
parents:
396
diff
changeset
|
725 |
de797f3b4c27
nginx-0.0.7-2004-07-23-09:37:29 import
Igor Sysoev <igor@sysoev.ru>
parents:
396
diff
changeset
|
726 |
539 | 727 ssize_t |
489 | 728 ngx_ssl_write(ngx_connection_t *c, u_char *data, size_t size) |
397
de797f3b4c27
nginx-0.0.7-2004-07-23-09:37:29 import
Igor Sysoev <igor@sysoev.ru>
parents:
396
diff
changeset
|
729 { |
547 | 730 int n, sslerr; |
731 ngx_err_t err; | |
397
de797f3b4c27
nginx-0.0.7-2004-07-23-09:37:29 import
Igor Sysoev <igor@sysoev.ru>
parents:
396
diff
changeset
|
732 |
de797f3b4c27
nginx-0.0.7-2004-07-23-09:37:29 import
Igor Sysoev <igor@sysoev.ru>
parents:
396
diff
changeset
|
733 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, c->log, 0, "SSL to write: %d", size); |
de797f3b4c27
nginx-0.0.7-2004-07-23-09:37:29 import
Igor Sysoev <igor@sysoev.ru>
parents:
396
diff
changeset
|
734 |
543 | 735 n = SSL_write(c->ssl->connection, data, size); |
397
de797f3b4c27
nginx-0.0.7-2004-07-23-09:37:29 import
Igor Sysoev <igor@sysoev.ru>
parents:
396
diff
changeset
|
736 |
de797f3b4c27
nginx-0.0.7-2004-07-23-09:37:29 import
Igor Sysoev <igor@sysoev.ru>
parents:
396
diff
changeset
|
737 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, c->log, 0, "SSL_write: %d", n); |
de797f3b4c27
nginx-0.0.7-2004-07-23-09:37:29 import
Igor Sysoev <igor@sysoev.ru>
parents:
396
diff
changeset
|
738 |
de797f3b4c27
nginx-0.0.7-2004-07-23-09:37:29 import
Igor Sysoev <igor@sysoev.ru>
parents:
396
diff
changeset
|
739 if (n > 0) { |
539 | 740 |
473 | 741 if (c->ssl->saved_read_handler) { |
742 | |
509 | 743 c->read->handler = c->ssl->saved_read_handler; |
473 | 744 c->ssl->saved_read_handler = NULL; |
745 c->read->ready = 1; | |
746 | |
747 if (ngx_handle_read_event(c->read, 0) == NGX_ERROR) { | |
748 return NGX_ERROR; | |
749 } | |
750 | |
563 | 751 ngx_post_event(c->read, &ngx_posted_events); |
473 | 752 } |
753 | |
397
de797f3b4c27
nginx-0.0.7-2004-07-23-09:37:29 import
Igor Sysoev <igor@sysoev.ru>
parents:
396
diff
changeset
|
754 return n; |
de797f3b4c27
nginx-0.0.7-2004-07-23-09:37:29 import
Igor Sysoev <igor@sysoev.ru>
parents:
396
diff
changeset
|
755 } |
de797f3b4c27
nginx-0.0.7-2004-07-23-09:37:29 import
Igor Sysoev <igor@sysoev.ru>
parents:
396
diff
changeset
|
756 |
543 | 757 sslerr = SSL_get_error(c->ssl->connection, n); |
397
de797f3b4c27
nginx-0.0.7-2004-07-23-09:37:29 import
Igor Sysoev <igor@sysoev.ru>
parents:
396
diff
changeset
|
758 |
de797f3b4c27
nginx-0.0.7-2004-07-23-09:37:29 import
Igor Sysoev <igor@sysoev.ru>
parents:
396
diff
changeset
|
759 err = (sslerr == SSL_ERROR_SYSCALL) ? ngx_errno : 0; |
de797f3b4c27
nginx-0.0.7-2004-07-23-09:37:29 import
Igor Sysoev <igor@sysoev.ru>
parents:
396
diff
changeset
|
760 |
de797f3b4c27
nginx-0.0.7-2004-07-23-09:37:29 import
Igor Sysoev <igor@sysoev.ru>
parents:
396
diff
changeset
|
761 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, c->log, 0, "SSL_get_error: %d", sslerr); |
de797f3b4c27
nginx-0.0.7-2004-07-23-09:37:29 import
Igor Sysoev <igor@sysoev.ru>
parents:
396
diff
changeset
|
762 |
de797f3b4c27
nginx-0.0.7-2004-07-23-09:37:29 import
Igor Sysoev <igor@sysoev.ru>
parents:
396
diff
changeset
|
763 if (sslerr == SSL_ERROR_WANT_WRITE) { |
de797f3b4c27
nginx-0.0.7-2004-07-23-09:37:29 import
Igor Sysoev <igor@sysoev.ru>
parents:
396
diff
changeset
|
764 c->write->ready = 0; |
de797f3b4c27
nginx-0.0.7-2004-07-23-09:37:29 import
Igor Sysoev <igor@sysoev.ru>
parents:
396
diff
changeset
|
765 return NGX_AGAIN; |
de797f3b4c27
nginx-0.0.7-2004-07-23-09:37:29 import
Igor Sysoev <igor@sysoev.ru>
parents:
396
diff
changeset
|
766 } |
de797f3b4c27
nginx-0.0.7-2004-07-23-09:37:29 import
Igor Sysoev <igor@sysoev.ru>
parents:
396
diff
changeset
|
767 |
445
f26432a1935a
nginx-0.1.0-2004-09-30-10:38:49 import
Igor Sysoev <igor@sysoev.ru>
parents:
444
diff
changeset
|
768 if (sslerr == SSL_ERROR_WANT_READ) { |
452 | 769 |
547 | 770 ngx_log_error(NGX_LOG_INFO, c->log, 0, |
577 | 771 "peer started SSL renegotiation"); |
473 | 772 |
773 c->read->ready = 0; | |
774 | |
775 if (ngx_handle_read_event(c->read, 0) == NGX_ERROR) { | |
776 return NGX_ERROR; | |
777 } | |
778 | |
779 /* | |
780 * we do not set the timer because there is already | |
781 * the write event timer | |
782 */ | |
783 | |
784 if (c->ssl->saved_read_handler == NULL) { | |
509 | 785 c->ssl->saved_read_handler = c->read->handler; |
786 c->read->handler = ngx_ssl_read_handler; | |
473 | 787 } |
788 | |
397
de797f3b4c27
nginx-0.0.7-2004-07-23-09:37:29 import
Igor Sysoev <igor@sysoev.ru>
parents:
396
diff
changeset
|
789 return NGX_AGAIN; |
de797f3b4c27
nginx-0.0.7-2004-07-23-09:37:29 import
Igor Sysoev <igor@sysoev.ru>
parents:
396
diff
changeset
|
790 } |
395
f8f0f1834266
nginx-0.0.7-2004-07-16-21:11:43 import
Igor Sysoev <igor@sysoev.ru>
parents:
394
diff
changeset
|
791 |
547 | 792 c->ssl->no_wait_shutdown = 1; |
793 c->ssl->no_send_shutdown = 1; | |
591 | 794 c->write->error = 1; |
543 | 795 |
547 | 796 ngx_ssl_connection_error(c, sslerr, err, "SSL_write() failed"); |
394
e7a68e14ccd3
nginx-0.0.7-2004-07-16-10:33:35 import
Igor Sysoev <igor@sysoev.ru>
parents:
393
diff
changeset
|
797 |
397
de797f3b4c27
nginx-0.0.7-2004-07-23-09:37:29 import
Igor Sysoev <igor@sysoev.ru>
parents:
396
diff
changeset
|
798 return NGX_ERROR; |
394
e7a68e14ccd3
nginx-0.0.7-2004-07-16-10:33:35 import
Igor Sysoev <igor@sysoev.ru>
parents:
393
diff
changeset
|
799 } |
e7a68e14ccd3
nginx-0.0.7-2004-07-16-10:33:35 import
Igor Sysoev <igor@sysoev.ru>
parents:
393
diff
changeset
|
800 |
e7a68e14ccd3
nginx-0.0.7-2004-07-16-10:33:35 import
Igor Sysoev <igor@sysoev.ru>
parents:
393
diff
changeset
|
801 |
489 | 802 static void |
803 ngx_ssl_read_handler(ngx_event_t *rev) | |
473 | 804 { |
805 ngx_connection_t *c; | |
806 | |
807 c = rev->data; | |
547 | 808 |
509 | 809 c->write->handler(c->write); |
473 | 810 } |
811 | |
812 | |
489 | 813 ngx_int_t |
814 ngx_ssl_shutdown(ngx_connection_t *c) | |
394
e7a68e14ccd3
nginx-0.0.7-2004-07-16-10:33:35 import
Igor Sysoev <igor@sysoev.ru>
parents:
393
diff
changeset
|
815 { |
473 | 816 int n, sslerr, mode; |
591 | 817 ngx_err_t err; |
394
e7a68e14ccd3
nginx-0.0.7-2004-07-16-10:33:35 import
Igor Sysoev <igor@sysoev.ru>
parents:
393
diff
changeset
|
818 ngx_uint_t again; |
e7a68e14ccd3
nginx-0.0.7-2004-07-16-10:33:35 import
Igor Sysoev <igor@sysoev.ru>
parents:
393
diff
changeset
|
819 |
577 | 820 if (c->timedout) { |
547 | 821 mode = SSL_RECEIVED_SHUTDOWN|SSL_SENT_SHUTDOWN; |
396
6f3b20c1ac50
nginx-0.0.7-2004-07-18-23:11:20 import
Igor Sysoev <igor@sysoev.ru>
parents:
395
diff
changeset
|
822 |
547 | 823 } else { |
824 mode = SSL_get_shutdown(c->ssl->connection); | |
473 | 825 |
547 | 826 if (c->ssl->no_wait_shutdown) { |
827 mode |= SSL_RECEIVED_SHUTDOWN; | |
396
6f3b20c1ac50
nginx-0.0.7-2004-07-18-23:11:20 import
Igor Sysoev <igor@sysoev.ru>
parents:
395
diff
changeset
|
828 } |
6f3b20c1ac50
nginx-0.0.7-2004-07-18-23:11:20 import
Igor Sysoev <igor@sysoev.ru>
parents:
395
diff
changeset
|
829 |
547 | 830 if (c->ssl->no_send_shutdown) { |
831 mode |= SSL_SENT_SHUTDOWN; | |
396
6f3b20c1ac50
nginx-0.0.7-2004-07-18-23:11:20 import
Igor Sysoev <igor@sysoev.ru>
parents:
395
diff
changeset
|
832 } |
394
e7a68e14ccd3
nginx-0.0.7-2004-07-16-10:33:35 import
Igor Sysoev <igor@sysoev.ru>
parents:
393
diff
changeset
|
833 } |
e7a68e14ccd3
nginx-0.0.7-2004-07-16-10:33:35 import
Igor Sysoev <igor@sysoev.ru>
parents:
393
diff
changeset
|
834 |
547 | 835 SSL_set_shutdown(c->ssl->connection, mode); |
836 | |
394
e7a68e14ccd3
nginx-0.0.7-2004-07-16-10:33:35 import
Igor Sysoev <igor@sysoev.ru>
parents:
393
diff
changeset
|
837 again = 0; |
461 | 838 sslerr = 0; |
394
e7a68e14ccd3
nginx-0.0.7-2004-07-16-10:33:35 import
Igor Sysoev <igor@sysoev.ru>
parents:
393
diff
changeset
|
839 |
e7a68e14ccd3
nginx-0.0.7-2004-07-16-10:33:35 import
Igor Sysoev <igor@sysoev.ru>
parents:
393
diff
changeset
|
840 for ( ;; ) { |
543 | 841 n = SSL_shutdown(c->ssl->connection); |
394
e7a68e14ccd3
nginx-0.0.7-2004-07-16-10:33:35 import
Igor Sysoev <igor@sysoev.ru>
parents:
393
diff
changeset
|
842 |
e7a68e14ccd3
nginx-0.0.7-2004-07-16-10:33:35 import
Igor Sysoev <igor@sysoev.ru>
parents:
393
diff
changeset
|
843 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, c->log, 0, "SSL_shutdown: %d", n); |
e7a68e14ccd3
nginx-0.0.7-2004-07-16-10:33:35 import
Igor Sysoev <igor@sysoev.ru>
parents:
393
diff
changeset
|
844 |
577 | 845 if (n == 1 || (n == 0 && c->timedout)) { |
543 | 846 SSL_free(c->ssl->connection); |
473 | 847 c->ssl = NULL; |
543 | 848 |
473 | 849 return NGX_OK; |
850 } | |
851 | |
394
e7a68e14ccd3
nginx-0.0.7-2004-07-16-10:33:35 import
Igor Sysoev <igor@sysoev.ru>
parents:
393
diff
changeset
|
852 if (n == 0) { |
e7a68e14ccd3
nginx-0.0.7-2004-07-16-10:33:35 import
Igor Sysoev <igor@sysoev.ru>
parents:
393
diff
changeset
|
853 again = 1; |
e7a68e14ccd3
nginx-0.0.7-2004-07-16-10:33:35 import
Igor Sysoev <igor@sysoev.ru>
parents:
393
diff
changeset
|
854 break; |
e7a68e14ccd3
nginx-0.0.7-2004-07-16-10:33:35 import
Igor Sysoev <igor@sysoev.ru>
parents:
393
diff
changeset
|
855 } |
e7a68e14ccd3
nginx-0.0.7-2004-07-16-10:33:35 import
Igor Sysoev <igor@sysoev.ru>
parents:
393
diff
changeset
|
856 |
e7a68e14ccd3
nginx-0.0.7-2004-07-16-10:33:35 import
Igor Sysoev <igor@sysoev.ru>
parents:
393
diff
changeset
|
857 break; |
e7a68e14ccd3
nginx-0.0.7-2004-07-16-10:33:35 import
Igor Sysoev <igor@sysoev.ru>
parents:
393
diff
changeset
|
858 } |
e7a68e14ccd3
nginx-0.0.7-2004-07-16-10:33:35 import
Igor Sysoev <igor@sysoev.ru>
parents:
393
diff
changeset
|
859 |
e7a68e14ccd3
nginx-0.0.7-2004-07-16-10:33:35 import
Igor Sysoev <igor@sysoev.ru>
parents:
393
diff
changeset
|
860 if (!again) { |
543 | 861 sslerr = SSL_get_error(c->ssl->connection, n); |
394
e7a68e14ccd3
nginx-0.0.7-2004-07-16-10:33:35 import
Igor Sysoev <igor@sysoev.ru>
parents:
393
diff
changeset
|
862 |
396
6f3b20c1ac50
nginx-0.0.7-2004-07-18-23:11:20 import
Igor Sysoev <igor@sysoev.ru>
parents:
395
diff
changeset
|
863 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, c->log, 0, |
6f3b20c1ac50
nginx-0.0.7-2004-07-18-23:11:20 import
Igor Sysoev <igor@sysoev.ru>
parents:
395
diff
changeset
|
864 "SSL_get_error: %d", sslerr); |
394
e7a68e14ccd3
nginx-0.0.7-2004-07-16-10:33:35 import
Igor Sysoev <igor@sysoev.ru>
parents:
393
diff
changeset
|
865 } |
e7a68e14ccd3
nginx-0.0.7-2004-07-16-10:33:35 import
Igor Sysoev <igor@sysoev.ru>
parents:
393
diff
changeset
|
866 |
589 | 867 if (again |
868 || sslerr == SSL_ERROR_WANT_READ | |
869 || sslerr == SSL_ERROR_WANT_WRITE) | |
870 { | |
577 | 871 c->read->handler = ngx_ssl_shutdown_handler; |
589 | 872 c->write->handler = ngx_ssl_shutdown_handler; |
577 | 873 |
394
e7a68e14ccd3
nginx-0.0.7-2004-07-16-10:33:35 import
Igor Sysoev <igor@sysoev.ru>
parents:
393
diff
changeset
|
874 if (ngx_handle_read_event(c->read, 0) == NGX_ERROR) { |
e7a68e14ccd3
nginx-0.0.7-2004-07-16-10:33:35 import
Igor Sysoev <igor@sysoev.ru>
parents:
393
diff
changeset
|
875 return NGX_ERROR; |
e7a68e14ccd3
nginx-0.0.7-2004-07-16-10:33:35 import
Igor Sysoev <igor@sysoev.ru>
parents:
393
diff
changeset
|
876 } |
e7a68e14ccd3
nginx-0.0.7-2004-07-16-10:33:35 import
Igor Sysoev <igor@sysoev.ru>
parents:
393
diff
changeset
|
877 |
e7a68e14ccd3
nginx-0.0.7-2004-07-16-10:33:35 import
Igor Sysoev <igor@sysoev.ru>
parents:
393
diff
changeset
|
878 if (ngx_handle_write_event(c->write, 0) == NGX_ERROR) { |
e7a68e14ccd3
nginx-0.0.7-2004-07-16-10:33:35 import
Igor Sysoev <igor@sysoev.ru>
parents:
393
diff
changeset
|
879 return NGX_ERROR; |
e7a68e14ccd3
nginx-0.0.7-2004-07-16-10:33:35 import
Igor Sysoev <igor@sysoev.ru>
parents:
393
diff
changeset
|
880 } |
e7a68e14ccd3
nginx-0.0.7-2004-07-16-10:33:35 import
Igor Sysoev <igor@sysoev.ru>
parents:
393
diff
changeset
|
881 |
589 | 882 if (again || sslerr == SSL_ERROR_WANT_READ) { |
883 ngx_add_timer(c->read, 30000); | |
884 } | |
885 | |
394
e7a68e14ccd3
nginx-0.0.7-2004-07-16-10:33:35 import
Igor Sysoev <igor@sysoev.ru>
parents:
393
diff
changeset
|
886 return NGX_AGAIN; |
e7a68e14ccd3
nginx-0.0.7-2004-07-16-10:33:35 import
Igor Sysoev <igor@sysoev.ru>
parents:
393
diff
changeset
|
887 } |
e7a68e14ccd3
nginx-0.0.7-2004-07-16-10:33:35 import
Igor Sysoev <igor@sysoev.ru>
parents:
393
diff
changeset
|
888 |
591 | 889 err = (sslerr == SSL_ERROR_SYSCALL) ? ngx_errno : 0; |
890 | |
891 ngx_ssl_connection_error(c, sslerr, err, "SSL_shutdown() failed"); | |
394
e7a68e14ccd3
nginx-0.0.7-2004-07-16-10:33:35 import
Igor Sysoev <igor@sysoev.ru>
parents:
393
diff
changeset
|
892 |
543 | 893 SSL_free(c->ssl->connection); |
894 c->ssl = NULL; | |
895 | |
394
e7a68e14ccd3
nginx-0.0.7-2004-07-16-10:33:35 import
Igor Sysoev <igor@sysoev.ru>
parents:
393
diff
changeset
|
896 return NGX_ERROR; |
e7a68e14ccd3
nginx-0.0.7-2004-07-16-10:33:35 import
Igor Sysoev <igor@sysoev.ru>
parents:
393
diff
changeset
|
897 } |
e7a68e14ccd3
nginx-0.0.7-2004-07-16-10:33:35 import
Igor Sysoev <igor@sysoev.ru>
parents:
393
diff
changeset
|
898 |
e7a68e14ccd3
nginx-0.0.7-2004-07-16-10:33:35 import
Igor Sysoev <igor@sysoev.ru>
parents:
393
diff
changeset
|
899 |
547 | 900 static void |
577 | 901 ngx_ssl_shutdown_handler(ngx_event_t *ev) |
902 { | |
903 ngx_connection_t *c; | |
904 ngx_connection_handler_pt handler; | |
905 | |
906 c = ev->data; | |
907 handler = c->ssl->handler; | |
908 | |
909 if (ev->timedout) { | |
910 c->timedout = 1; | |
911 } | |
912 | |
913 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, ev->log, 0, "SSL shutdown handler"); | |
914 | |
915 if (ngx_ssl_shutdown(c) == NGX_AGAIN) { | |
916 return; | |
917 } | |
918 | |
919 handler(c); | |
920 } | |
921 | |
922 | |
923 static void | |
547 | 924 ngx_ssl_connection_error(ngx_connection_t *c, int sslerr, ngx_err_t err, |
925 char *text) | |
926 { | |
927 ngx_uint_t level; | |
928 | |
929 level = NGX_LOG_CRIT; | |
930 | |
931 if (sslerr == SSL_ERROR_SYSCALL) { | |
932 | |
933 if (err == NGX_ECONNRESET | |
934 || err == NGX_EPIPE | |
935 || err == NGX_ENOTCONN | |
589 | 936 #if !(NGX_CRIT_ETIMEDOUT) |
937 || err == NGX_ETIMEDOUT | |
938 #endif | |
547 | 939 || err == NGX_ECONNREFUSED |
940 || err == NGX_EHOSTUNREACH) | |
941 { | |
942 switch (c->log_error) { | |
943 | |
944 case NGX_ERROR_IGNORE_ECONNRESET: | |
945 case NGX_ERROR_INFO: | |
946 level = NGX_LOG_INFO; | |
947 break; | |
948 | |
949 case NGX_ERROR_ERR: | |
950 level = NGX_LOG_ERR; | |
951 break; | |
952 | |
953 default: | |
954 break; | |
955 } | |
956 } | |
957 } | |
958 | |
959 ngx_ssl_error(level, c->log, err, text); | |
960 } | |
961 | |
962 | |
583 | 963 void ngx_cdecl |
489 | 964 ngx_ssl_error(ngx_uint_t level, ngx_log_t *log, ngx_err_t err, char *fmt, ...) |
577 | 965 { |
547 | 966 u_long n; |
967 va_list args; | |
461 | 968 u_char errstr[NGX_MAX_CONF_ERRSTR], *p, *last; |
969 | |
970 last = errstr + NGX_MAX_CONF_ERRSTR; | |
393
5659d773cfa8
nginx-0.0.7-2004-07-15-20:35:51 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
971 |
5659d773cfa8
nginx-0.0.7-2004-07-15-20:35:51 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
972 va_start(args, fmt); |
461 | 973 p = ngx_vsnprintf(errstr, sizeof(errstr) - 1, fmt, args); |
393
5659d773cfa8
nginx-0.0.7-2004-07-15-20:35:51 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
974 va_end(args); |
5659d773cfa8
nginx-0.0.7-2004-07-15-20:35:51 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
975 |
547 | 976 p = ngx_cpystrn(p, (u_char *) " (SSL:", last - p); |
977 | |
583 | 978 while (p < last) { |
979 | |
980 n = ERR_get_error(); | |
981 | |
982 if (n == 0) { | |
983 break; | |
984 } | |
547 | 985 |
986 *p++ = ' '; | |
393
5659d773cfa8
nginx-0.0.7-2004-07-15-20:35:51 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
987 |
547 | 988 ERR_error_string_n(n, (char *) p, last - p); |
989 | |
990 while (p < last && *p) { | |
991 p++; | |
992 } | |
993 } | |
393
5659d773cfa8
nginx-0.0.7-2004-07-15-20:35:51 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
994 |
395
f8f0f1834266
nginx-0.0.7-2004-07-16-21:11:43 import
Igor Sysoev <igor@sysoev.ru>
parents:
394
diff
changeset
|
995 ngx_log_error(level, log, err, "%s)", errstr); |
393
5659d773cfa8
nginx-0.0.7-2004-07-15-20:35:51 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
996 } |
509 | 997 |
998 | |
999 void | |
1000 ngx_ssl_cleanup_ctx(void *data) | |
1001 { | |
589 | 1002 ngx_ssl_t *ssl = data; |
509 | 1003 |
589 | 1004 if (ssl->rsa512_key) { |
1005 RSA_free(ssl->rsa512_key); | |
1006 } | |
559 | 1007 |
589 | 1008 SSL_CTX_free(ssl->ctx); |
509 | 1009 } |
541 | 1010 |
1011 | |
611 | 1012 u_char * |
1013 ngx_ssl_get_protocol(ngx_connection_t *c) | |
1014 { | |
1015 return (u_char *) SSL_get_version(c->ssl->connection); | |
1016 } | |
1017 | |
1018 | |
1019 u_char * | |
1020 ngx_ssl_get_cipher_name(ngx_connection_t *c) | |
1021 { | |
1022 return (u_char *) SSL_get_cipher_name(c->ssl->connection); | |
1023 } | |
1024 | |
1025 | |
541 | 1026 static void * |
1027 ngx_openssl_create_conf(ngx_cycle_t *cycle) | |
1028 { | |
1029 ngx_openssl_conf_t *oscf; | |
577 | 1030 |
541 | 1031 oscf = ngx_pcalloc(cycle->pool, sizeof(ngx_openssl_conf_t)); |
1032 if (oscf == NULL) { | |
1033 return NGX_CONF_ERROR; | |
1034 } | |
577 | 1035 |
541 | 1036 /* |
1037 * set by ngx_pcalloc(): | |
577 | 1038 * |
541 | 1039 * oscf->engine.len = 0; |
1040 * oscf->engine.data = NULL; | |
577 | 1041 */ |
541 | 1042 |
1043 return oscf; | |
1044 } | |
1045 | |
1046 | |
1047 static char * | |
1048 ngx_openssl_init_conf(ngx_cycle_t *cycle, void *conf) | |
1049 { | |
1050 #if (NGX_SSL_ENGINE) | |
1051 ngx_openssl_conf_t *oscf = conf; | |
571 | 1052 |
541 | 1053 ENGINE *engine; |
1054 | |
1055 if (oscf->engine.len == 0) { | |
1056 return NGX_CONF_OK; | |
1057 } | |
577 | 1058 |
541 | 1059 engine = ENGINE_by_id((const char *) oscf->engine.data); |
1060 | |
1061 if (engine == NULL) { | |
1062 ngx_ssl_error(NGX_LOG_WARN, cycle->log, 0, | |
1063 "ENGINE_by_id(\"%V\") failed", &oscf->engine); | |
1064 return NGX_CONF_ERROR; | |
1065 } | |
1066 | |
1067 if (ENGINE_set_default(engine, ENGINE_METHOD_ALL) == 0) { | |
1068 ngx_ssl_error(NGX_LOG_WARN, cycle->log, 0, | |
1069 "ENGINE_set_default(\"%V\", ENGINE_METHOD_ALL) failed", | |
1070 &oscf->engine); | |
1071 return NGX_CONF_ERROR; | |
1072 } | |
1073 | |
1074 ENGINE_free(engine); | |
1075 | |
1076 #endif | |
1077 | |
1078 return NGX_CONF_OK; | |
1079 } | |
1080 | |
1081 | |
1082 #if !(NGX_SSL_ENGINE) | |
1083 | |
1084 static char * | |
1085 ngx_openssl_noengine(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) | |
1086 { | |
1087 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, | |
563 | 1088 "\"ssl_engine\" directive is available only in " |
1089 "OpenSSL 0.9.7 and higher,"); | |
541 | 1090 |
1091 return NGX_CONF_ERROR; | |
1092 } | |
1093 | |
1094 #endif | |
571 | 1095 |
1096 | |
1097 static void | |
1098 ngx_openssl_exit(ngx_cycle_t *cycle) | |
1099 { | |
1100 #if (NGX_SSL_ENGINE) | |
1101 ENGINE_cleanup(); | |
1102 #endif | |
1103 } |