]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved. | |
3 | * | |
4 | * Licensed under the OpenSSL license (the "License"). You may not use | |
5 | * this file except in compliance with the License. You can obtain a copy | |
6 | * in the file LICENSE in the source distribution or at | |
7 | * https://www.openssl.org/source/license.html | |
8 | */ | |
9 | ||
10 | #include <stdio.h> | |
11 | #include <stdlib.h> | |
12 | #include <string.h> | |
13 | #include <errno.h> | |
14 | #include <openssl/crypto.h> | |
15 | #include "internal/bio.h" | |
16 | #include <openssl/err.h> | |
17 | #include "ssl_locl.h" | |
18 | ||
19 | static int ssl_write(BIO *h, const char *buf, int num); | |
20 | static int ssl_read(BIO *h, char *buf, int size); | |
21 | static int ssl_puts(BIO *h, const char *str); | |
22 | static long ssl_ctrl(BIO *h, int cmd, long arg1, void *arg2); | |
23 | static int ssl_new(BIO *h); | |
24 | static int ssl_free(BIO *data); | |
25 | static long ssl_callback_ctrl(BIO *h, int cmd, bio_info_cb *fp); | |
26 | typedef struct bio_ssl_st { | |
27 | SSL *ssl; /* The ssl handle :-) */ | |
28 | /* re-negotiate every time the total number of bytes is this size */ | |
29 | int num_renegotiates; | |
30 | unsigned long renegotiate_count; | |
31 | unsigned long byte_count; | |
32 | unsigned long renegotiate_timeout; | |
33 | unsigned long last_time; | |
34 | } BIO_SSL; | |
35 | ||
36 | static const BIO_METHOD methods_sslp = { | |
37 | BIO_TYPE_SSL, "ssl", | |
38 | ssl_write, | |
39 | ssl_read, | |
40 | ssl_puts, | |
41 | NULL, /* ssl_gets, */ | |
42 | ssl_ctrl, | |
43 | ssl_new, | |
44 | ssl_free, | |
45 | ssl_callback_ctrl, | |
46 | }; | |
47 | ||
48 | const BIO_METHOD *BIO_f_ssl(void) | |
49 | { | |
50 | return (&methods_sslp); | |
51 | } | |
52 | ||
53 | static int ssl_new(BIO *bi) | |
54 | { | |
55 | BIO_SSL *bs = OPENSSL_zalloc(sizeof(*bs)); | |
56 | ||
57 | if (bs == NULL) { | |
58 | BIOerr(BIO_F_SSL_NEW, ERR_R_MALLOC_FAILURE); | |
59 | return (0); | |
60 | } | |
61 | BIO_set_init(bi, 0); | |
62 | BIO_set_data(bi, bs); | |
63 | /* Clear all flags */ | |
64 | BIO_clear_flags(bi, ~0); | |
65 | ||
66 | return 1; | |
67 | } | |
68 | ||
69 | static int ssl_free(BIO *a) | |
70 | { | |
71 | BIO_SSL *bs; | |
72 | ||
73 | if (a == NULL) | |
74 | return (0); | |
75 | bs = BIO_get_data(a); | |
76 | if (bs->ssl != NULL) | |
77 | SSL_shutdown(bs->ssl); | |
78 | if (BIO_get_shutdown(a)) { | |
79 | if (BIO_get_init(a)) | |
80 | SSL_free(bs->ssl); | |
81 | /* Clear all flags */ | |
82 | BIO_clear_flags(a, ~0); | |
83 | BIO_set_init(a, 0); | |
84 | } | |
85 | OPENSSL_free(bs); | |
86 | return 1; | |
87 | } | |
88 | ||
89 | static int ssl_read(BIO *b, char *out, int outl) | |
90 | { | |
91 | int ret = 1; | |
92 | BIO_SSL *sb; | |
93 | SSL *ssl; | |
94 | int retry_reason = 0; | |
95 | int r = 0; | |
96 | ||
97 | if (out == NULL) | |
98 | return (0); | |
99 | sb = BIO_get_data(b); | |
100 | ssl = sb->ssl; | |
101 | ||
102 | BIO_clear_retry_flags(b); | |
103 | ||
104 | ret = SSL_read(ssl, out, outl); | |
105 | ||
106 | switch (SSL_get_error(ssl, ret)) { | |
107 | case SSL_ERROR_NONE: | |
108 | if (ret <= 0) | |
109 | break; | |
110 | if (sb->renegotiate_count > 0) { | |
111 | sb->byte_count += ret; | |
112 | if (sb->byte_count > sb->renegotiate_count) { | |
113 | sb->byte_count = 0; | |
114 | sb->num_renegotiates++; | |
115 | SSL_renegotiate(ssl); | |
116 | r = 1; | |
117 | } | |
118 | } | |
119 | if ((sb->renegotiate_timeout > 0) && (!r)) { | |
120 | unsigned long tm; | |
121 | ||
122 | tm = (unsigned long)time(NULL); | |
123 | if (tm > sb->last_time + sb->renegotiate_timeout) { | |
124 | sb->last_time = tm; | |
125 | sb->num_renegotiates++; | |
126 | SSL_renegotiate(ssl); | |
127 | } | |
128 | } | |
129 | ||
130 | break; | |
131 | case SSL_ERROR_WANT_READ: | |
132 | BIO_set_retry_read(b); | |
133 | break; | |
134 | case SSL_ERROR_WANT_WRITE: | |
135 | BIO_set_retry_write(b); | |
136 | break; | |
137 | case SSL_ERROR_WANT_X509_LOOKUP: | |
138 | BIO_set_retry_special(b); | |
139 | retry_reason = BIO_RR_SSL_X509_LOOKUP; | |
140 | break; | |
141 | case SSL_ERROR_WANT_ACCEPT: | |
142 | BIO_set_retry_special(b); | |
143 | retry_reason = BIO_RR_ACCEPT; | |
144 | break; | |
145 | case SSL_ERROR_WANT_CONNECT: | |
146 | BIO_set_retry_special(b); | |
147 | retry_reason = BIO_RR_CONNECT; | |
148 | break; | |
149 | case SSL_ERROR_SYSCALL: | |
150 | case SSL_ERROR_SSL: | |
151 | case SSL_ERROR_ZERO_RETURN: | |
152 | default: | |
153 | break; | |
154 | } | |
155 | ||
156 | BIO_set_retry_reason(b, retry_reason); | |
157 | return (ret); | |
158 | } | |
159 | ||
160 | static int ssl_write(BIO *b, const char *out, int outl) | |
161 | { | |
162 | int ret, r = 0; | |
163 | int retry_reason = 0; | |
164 | SSL *ssl; | |
165 | BIO_SSL *bs; | |
166 | ||
167 | if (out == NULL) | |
168 | return (0); | |
169 | bs = BIO_get_data(b); | |
170 | ssl = bs->ssl; | |
171 | ||
172 | BIO_clear_retry_flags(b); | |
173 | ||
174 | /* | |
175 | * ret=SSL_do_handshake(ssl); if (ret > 0) | |
176 | */ | |
177 | ret = SSL_write(ssl, out, outl); | |
178 | ||
179 | switch (SSL_get_error(ssl, ret)) { | |
180 | case SSL_ERROR_NONE: | |
181 | if (ret <= 0) | |
182 | break; | |
183 | if (bs->renegotiate_count > 0) { | |
184 | bs->byte_count += ret; | |
185 | if (bs->byte_count > bs->renegotiate_count) { | |
186 | bs->byte_count = 0; | |
187 | bs->num_renegotiates++; | |
188 | SSL_renegotiate(ssl); | |
189 | r = 1; | |
190 | } | |
191 | } | |
192 | if ((bs->renegotiate_timeout > 0) && (!r)) { | |
193 | unsigned long tm; | |
194 | ||
195 | tm = (unsigned long)time(NULL); | |
196 | if (tm > bs->last_time + bs->renegotiate_timeout) { | |
197 | bs->last_time = tm; | |
198 | bs->num_renegotiates++; | |
199 | SSL_renegotiate(ssl); | |
200 | } | |
201 | } | |
202 | break; | |
203 | case SSL_ERROR_WANT_WRITE: | |
204 | BIO_set_retry_write(b); | |
205 | break; | |
206 | case SSL_ERROR_WANT_READ: | |
207 | BIO_set_retry_read(b); | |
208 | break; | |
209 | case SSL_ERROR_WANT_X509_LOOKUP: | |
210 | BIO_set_retry_special(b); | |
211 | retry_reason = BIO_RR_SSL_X509_LOOKUP; | |
212 | break; | |
213 | case SSL_ERROR_WANT_CONNECT: | |
214 | BIO_set_retry_special(b); | |
215 | retry_reason = BIO_RR_CONNECT; | |
216 | case SSL_ERROR_SYSCALL: | |
217 | case SSL_ERROR_SSL: | |
218 | default: | |
219 | break; | |
220 | } | |
221 | ||
222 | BIO_set_retry_reason(b, retry_reason); | |
223 | return ret; | |
224 | } | |
225 | ||
226 | static long ssl_ctrl(BIO *b, int cmd, long num, void *ptr) | |
227 | { | |
228 | SSL **sslp, *ssl; | |
229 | BIO_SSL *bs, *dbs; | |
230 | BIO *dbio, *bio; | |
231 | long ret = 1; | |
232 | BIO *next; | |
233 | ||
234 | bs = BIO_get_data(b); | |
235 | next = BIO_next(b); | |
236 | ssl = bs->ssl; | |
237 | if ((ssl == NULL) && (cmd != BIO_C_SET_SSL)) | |
238 | return (0); | |
239 | switch (cmd) { | |
240 | case BIO_CTRL_RESET: | |
241 | SSL_shutdown(ssl); | |
242 | ||
243 | if (ssl->handshake_func == ssl->method->ssl_connect) | |
244 | SSL_set_connect_state(ssl); | |
245 | else if (ssl->handshake_func == ssl->method->ssl_accept) | |
246 | SSL_set_accept_state(ssl); | |
247 | ||
248 | if (!SSL_clear(ssl)) { | |
249 | ret = 0; | |
250 | break; | |
251 | } | |
252 | ||
253 | if (next != NULL) | |
254 | ret = BIO_ctrl(next, cmd, num, ptr); | |
255 | else if (ssl->rbio != NULL) | |
256 | ret = BIO_ctrl(ssl->rbio, cmd, num, ptr); | |
257 | else | |
258 | ret = 1; | |
259 | break; | |
260 | case BIO_CTRL_INFO: | |
261 | ret = 0; | |
262 | break; | |
263 | case BIO_C_SSL_MODE: | |
264 | if (num) /* client mode */ | |
265 | SSL_set_connect_state(ssl); | |
266 | else | |
267 | SSL_set_accept_state(ssl); | |
268 | break; | |
269 | case BIO_C_SET_SSL_RENEGOTIATE_TIMEOUT: | |
270 | ret = bs->renegotiate_timeout; | |
271 | if (num < 60) | |
272 | num = 5; | |
273 | bs->renegotiate_timeout = (unsigned long)num; | |
274 | bs->last_time = (unsigned long)time(NULL); | |
275 | break; | |
276 | case BIO_C_SET_SSL_RENEGOTIATE_BYTES: | |
277 | ret = bs->renegotiate_count; | |
278 | if ((long)num >= 512) | |
279 | bs->renegotiate_count = (unsigned long)num; | |
280 | break; | |
281 | case BIO_C_GET_SSL_NUM_RENEGOTIATES: | |
282 | ret = bs->num_renegotiates; | |
283 | break; | |
284 | case BIO_C_SET_SSL: | |
285 | if (ssl != NULL) { | |
286 | ssl_free(b); | |
287 | if (!ssl_new(b)) | |
288 | return 0; | |
289 | } | |
290 | BIO_set_shutdown(b, num); | |
291 | ssl = (SSL *)ptr; | |
292 | bs->ssl = ssl; | |
293 | bio = SSL_get_rbio(ssl); | |
294 | if (bio != NULL) { | |
295 | if (next != NULL) | |
296 | BIO_push(bio, next); | |
297 | BIO_set_next(b, bio); | |
298 | BIO_up_ref(bio); | |
299 | } | |
300 | BIO_set_init(b, 1); | |
301 | break; | |
302 | case BIO_C_GET_SSL: | |
303 | if (ptr != NULL) { | |
304 | sslp = (SSL **)ptr; | |
305 | *sslp = ssl; | |
306 | } else | |
307 | ret = 0; | |
308 | break; | |
309 | case BIO_CTRL_GET_CLOSE: | |
310 | ret = BIO_get_shutdown(b); | |
311 | break; | |
312 | case BIO_CTRL_SET_CLOSE: | |
313 | BIO_set_shutdown(b, (int)num); | |
314 | break; | |
315 | case BIO_CTRL_WPENDING: | |
316 | ret = BIO_ctrl(ssl->wbio, cmd, num, ptr); | |
317 | break; | |
318 | case BIO_CTRL_PENDING: | |
319 | ret = SSL_pending(ssl); | |
320 | if (ret == 0) | |
321 | ret = BIO_pending(ssl->rbio); | |
322 | break; | |
323 | case BIO_CTRL_FLUSH: | |
324 | BIO_clear_retry_flags(b); | |
325 | ret = BIO_ctrl(ssl->wbio, cmd, num, ptr); | |
326 | BIO_copy_next_retry(b); | |
327 | break; | |
328 | case BIO_CTRL_PUSH: | |
329 | if ((next != NULL) && (next != ssl->rbio)) { | |
330 | /* | |
331 | * We are going to pass ownership of next to the SSL object...but | |
332 | * we don't own a reference to pass yet - so up ref | |
333 | */ | |
334 | BIO_up_ref(next); | |
335 | SSL_set_bio(ssl, next, next); | |
336 | } | |
337 | break; | |
338 | case BIO_CTRL_POP: | |
339 | /* Only detach if we are the BIO explicitly being popped */ | |
340 | if (b == ptr) { | |
341 | /* | |
342 | * Shouldn't happen in practice because the rbio and wbio are the | |
343 | * same when pushed. | |
344 | */ | |
345 | if (ssl->rbio != ssl->wbio) | |
346 | BIO_free_all(ssl->wbio); | |
347 | if (next != NULL) | |
348 | BIO_free(next); | |
349 | ssl->wbio = NULL; | |
350 | ssl->rbio = NULL; | |
351 | } | |
352 | break; | |
353 | case BIO_C_DO_STATE_MACHINE: | |
354 | BIO_clear_retry_flags(b); | |
355 | ||
356 | BIO_set_retry_reason(b, 0); | |
357 | ret = (int)SSL_do_handshake(ssl); | |
358 | ||
359 | switch (SSL_get_error(ssl, (int)ret)) { | |
360 | case SSL_ERROR_WANT_READ: | |
361 | BIO_set_flags(b, BIO_FLAGS_READ | BIO_FLAGS_SHOULD_RETRY); | |
362 | break; | |
363 | case SSL_ERROR_WANT_WRITE: | |
364 | BIO_set_flags(b, BIO_FLAGS_WRITE | BIO_FLAGS_SHOULD_RETRY); | |
365 | break; | |
366 | case SSL_ERROR_WANT_CONNECT: | |
367 | BIO_set_flags(b, BIO_FLAGS_IO_SPECIAL | BIO_FLAGS_SHOULD_RETRY); | |
368 | BIO_set_retry_reason(b, BIO_get_retry_reason(next)); | |
369 | break; | |
370 | case SSL_ERROR_WANT_X509_LOOKUP: | |
371 | BIO_set_retry_special(b); | |
372 | BIO_set_retry_reason(b, BIO_RR_SSL_X509_LOOKUP); | |
373 | break; | |
374 | default: | |
375 | break; | |
376 | } | |
377 | break; | |
378 | case BIO_CTRL_DUP: | |
379 | dbio = (BIO *)ptr; | |
380 | dbs = BIO_get_data(dbio); | |
381 | SSL_free(dbs->ssl); | |
382 | dbs->ssl = SSL_dup(ssl); | |
383 | dbs->num_renegotiates = bs->num_renegotiates; | |
384 | dbs->renegotiate_count = bs->renegotiate_count; | |
385 | dbs->byte_count = bs->byte_count; | |
386 | dbs->renegotiate_timeout = bs->renegotiate_timeout; | |
387 | dbs->last_time = bs->last_time; | |
388 | ret = (dbs->ssl != NULL); | |
389 | break; | |
390 | case BIO_C_GET_FD: | |
391 | ret = BIO_ctrl(ssl->rbio, cmd, num, ptr); | |
392 | break; | |
393 | case BIO_CTRL_SET_CALLBACK: | |
394 | { | |
395 | #if 0 /* FIXME: Should this be used? -- Richard | |
396 | * Levitte */ | |
397 | SSLerr(SSL_F_SSL_CTRL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); | |
398 | ret = -1; | |
399 | #else | |
400 | ret = 0; | |
401 | #endif | |
402 | } | |
403 | break; | |
404 | case BIO_CTRL_GET_CALLBACK: | |
405 | { | |
406 | void (**fptr) (const SSL *xssl, int type, int val); | |
407 | ||
408 | fptr = (void (**)(const SSL *xssl, int type, int val))ptr; | |
409 | *fptr = SSL_get_info_callback(ssl); | |
410 | } | |
411 | break; | |
412 | default: | |
413 | ret = BIO_ctrl(ssl->rbio, cmd, num, ptr); | |
414 | break; | |
415 | } | |
416 | return (ret); | |
417 | } | |
418 | ||
419 | static long ssl_callback_ctrl(BIO *b, int cmd, bio_info_cb *fp) | |
420 | { | |
421 | SSL *ssl; | |
422 | BIO_SSL *bs; | |
423 | long ret = 1; | |
424 | ||
425 | bs = BIO_get_data(b); | |
426 | ssl = bs->ssl; | |
427 | switch (cmd) { | |
428 | case BIO_CTRL_SET_CALLBACK: | |
429 | { | |
430 | /* | |
431 | * FIXME: setting this via a completely different prototype seems | |
432 | * like a crap idea | |
433 | */ | |
434 | SSL_set_info_callback(ssl, (void (*)(const SSL *, int, int))fp); | |
435 | } | |
436 | break; | |
437 | default: | |
438 | ret = BIO_callback_ctrl(ssl->rbio, cmd, fp); | |
439 | break; | |
440 | } | |
441 | return (ret); | |
442 | } | |
443 | ||
444 | static int ssl_puts(BIO *bp, const char *str) | |
445 | { | |
446 | int n, ret; | |
447 | ||
448 | n = strlen(str); | |
449 | ret = BIO_write(bp, str, n); | |
450 | return (ret); | |
451 | } | |
452 | ||
453 | BIO *BIO_new_buffer_ssl_connect(SSL_CTX *ctx) | |
454 | { | |
455 | #ifndef OPENSSL_NO_SOCK | |
456 | BIO *ret = NULL, *buf = NULL, *ssl = NULL; | |
457 | ||
458 | if ((buf = BIO_new(BIO_f_buffer())) == NULL) | |
459 | return (NULL); | |
460 | if ((ssl = BIO_new_ssl_connect(ctx)) == NULL) | |
461 | goto err; | |
462 | if ((ret = BIO_push(buf, ssl)) == NULL) | |
463 | goto err; | |
464 | return (ret); | |
465 | err: | |
466 | BIO_free(buf); | |
467 | BIO_free(ssl); | |
468 | #endif | |
469 | return (NULL); | |
470 | } | |
471 | ||
472 | BIO *BIO_new_ssl_connect(SSL_CTX *ctx) | |
473 | { | |
474 | #ifndef OPENSSL_NO_SOCK | |
475 | BIO *ret = NULL, *con = NULL, *ssl = NULL; | |
476 | ||
477 | if ((con = BIO_new(BIO_s_connect())) == NULL) | |
478 | return (NULL); | |
479 | if ((ssl = BIO_new_ssl(ctx, 1)) == NULL) | |
480 | goto err; | |
481 | if ((ret = BIO_push(ssl, con)) == NULL) | |
482 | goto err; | |
483 | return (ret); | |
484 | err: | |
485 | BIO_free(con); | |
486 | #endif | |
487 | return (NULL); | |
488 | } | |
489 | ||
490 | BIO *BIO_new_ssl(SSL_CTX *ctx, int client) | |
491 | { | |
492 | BIO *ret; | |
493 | SSL *ssl; | |
494 | ||
495 | if ((ret = BIO_new(BIO_f_ssl())) == NULL) | |
496 | return (NULL); | |
497 | if ((ssl = SSL_new(ctx)) == NULL) { | |
498 | BIO_free(ret); | |
499 | return (NULL); | |
500 | } | |
501 | if (client) | |
502 | SSL_set_connect_state(ssl); | |
503 | else | |
504 | SSL_set_accept_state(ssl); | |
505 | ||
506 | BIO_set_ssl(ret, ssl, BIO_CLOSE); | |
507 | return (ret); | |
508 | } | |
509 | ||
510 | int BIO_ssl_copy_session_id(BIO *t, BIO *f) | |
511 | { | |
512 | BIO_SSL *tdata, *fdata; | |
513 | t = BIO_find_type(t, BIO_TYPE_SSL); | |
514 | f = BIO_find_type(f, BIO_TYPE_SSL); | |
515 | if ((t == NULL) || (f == NULL)) | |
516 | return 0; | |
517 | tdata = BIO_get_data(t); | |
518 | fdata = BIO_get_data(f); | |
519 | if ((tdata->ssl == NULL) || (fdata->ssl == NULL)) | |
520 | return (0); | |
521 | if (!SSL_copy_session_id(tdata->ssl, (fdata->ssl))) | |
522 | return 0; | |
523 | return (1); | |
524 | } | |
525 | ||
526 | void BIO_ssl_shutdown(BIO *b) | |
527 | { | |
528 | SSL *s; | |
529 | ||
530 | b = BIO_find_type(b, BIO_TYPE_SSL); | |
531 | if (b == NULL) | |
532 | return; | |
533 | ||
534 | s = BIO_get_data(b); | |
535 | SSL_shutdown(s); | |
536 | } |