75 static char const rcsid[] =
76 "$Id: easy-tls.c,v 1.4 2002/03/05 09:07:16 bodo Exp $";
85 #include <sys/select.h>
86 #include <sys/socket.h>
89 #include <sys/types.h>
90 #include <sys/utsname.h>
108 #if OPENSSL_VERSION_NUMBER < 0x00904000L
109 # error "This program needs OpenSSL 0.9.4 or later."
114 #if TLS_INFO_SIZE > PIPE_BUF
116 # error "PIPE_BUF < 512"
118 # error "TLS_INFO_SIZE > PIPE_BUF"
134 #ifndef TLS_APP_PROCESS_INIT
135 # define TLS_APP_PROCESS_INIT(fd, client_p, apparg) ((void) 0)
138 #ifndef TLS_ERROR_BUFSIZ
139 # define TLS_ERROR_BUFSIZ (10*160)
141 #if TLS_ERROR_BUFSIZ < 2
142 # error "TLS_ERROR_BUFSIZE is too small."
145 #ifndef TLS_APP_ERRFLUSH
146 # define TLS_APP_ERRFLUSH tls_app_errflush
148 tls_app_errflush(
int child_p,
char *errbuf,
size_t num,
void *apparg)
150 fputs(errbuf, stderr);
157 # define DEBUG_MSG(x) fprintf(stderr," %s\n",x)
158 # define DEBUG_MSG2(x,y) fprintf(stderr, " %s: %d\n",x,y)
159 static int tls_loop_count = 0;
160 static int tls_select_count = 0;
162 # define DEBUG_MSG(x) (void)0
163 # define DEBUG_MSG2(x,y) (void)0
166 static void tls_rand_seed_uniquely(
void);
167 static void tls_proxy(
int clear_fd,
int tls_fd,
int info_fd,
SSL_CTX *ctx,
int client_p);
168 static int tls_socket_nonblocking(
int fd);
170 static int tls_child_p = 0;
171 static void *tls_child_apparg;
199 int fds[2] = {-1, -1};
200 int infofds[2] = {-1, -1};
217 r = socketpair(AF_UNIX, SOCK_STREAM, 0, fds);
240 tls_child_apparg = apparg;
242 if (infofds[0] != -1)
251 if (infofds[1] != -1) {
258 getfd = fcntl(a.
fd, F_GETFD);
259 getfl = fcntl(a.
fd, F_GETFL);
260 r = dup2(fds[1], a.
fd);
268 fcntl(a.
fd, F_SETFD, getfd);
269 if (getfl & O_NONBLOCK)
270 (
void)tls_socket_nonblocking(a.
fd);
280 if (infofds[0] != -1)
282 if (infofds[1] != -1)
290 static size_t errbuf_i = 0;
293 tls_errflush(
void *apparg)
298 assert(errbuf_i <
sizeof errbuf);
299 assert(errbuf[errbuf_i] == 0);
300 if (errbuf_i ==
sizeof errbuf - 1) {
302 errbuf[errbuf_i - 1] =
'\n';
313 tls_errprintf(
int flush,
void *apparg,
const char *fmt, ...)
318 if (errbuf_i <
sizeof errbuf - 1) {
322 n = (
sizeof errbuf) - errbuf_i;
323 r = vsnprintf(errbuf + errbuf_i, n, fmt, args);
329 errbuf_i =
sizeof errbuf - 1;
330 errbuf[errbuf_i] =
'\0';
332 assert(errbuf_i <
sizeof errbuf);
333 assert(errbuf[errbuf_i] == 0);
335 #ifndef TLS_CUMULATE_ERRORS
336 tls_errflush(apparg);
339 tls_errflush(apparg);
347 tls_openssl_errors(
const char *app_prefix_1,
const char *app_prefix_2,
const char *default_text,
void *apparg)
349 static char reasons[255];
357 int printed_something = 0;
361 assert(app_prefix_1 != NULL);
362 assert(app_prefix_2 != NULL);
364 if (default_text == NULL)
365 default_text =
"?""?""?";
368 if (reasons_i <
sizeof reasons) {
372 n = (
sizeof reasons) - reasons_i;
379 reasons_i =
sizeof reasons;
381 assert(reasons_i <=
sizeof reasons);
385 assert(errstring != NULL);
386 tls_errprintf(0, apparg,
"OpenSSL error%s%s: %s:%s:%d:%s\n", app_prefix_1, app_prefix_2, errstring, file, line, (flags &
ERR_TXT_STRING) ? data :
"");
387 printed_something = 1;
390 if (!printed_something) {
391 assert(reasons_i == 0);
392 snprintf(reasons,
sizeof reasons,
"%s", default_text);
393 tls_errprintf(0, apparg,
"OpenSSL error%s%s: %s\n", app_prefix_1, app_prefix_2, default_text);
396 #ifdef TLS_CUMULATE_ERRORS
397 tls_errflush(apparg);
399 assert(errbuf_i == 0);
406 static int tls_init_done = 0;
409 tls_init(
void *apparg)
416 tls_errprintf(1, apparg,
"SSL_library_init failed.\n");
427 tls_rand_seed_uniquely(
void)
436 data.time = time(NULL);
437 data.stack = (
void *)&data;
439 RAND_seed((
const void *)&data,
sizeof data);
446 struct utsname uname;
455 data.uname_1 = uname(&data.uname);
456 data.uname_2 =
errno;
459 data.euid = geteuid();
461 data.egid = getegid();
463 RAND_seed((
const void *)&data,
sizeof data);
464 tls_rand_seed_uniquely();
467 static int tls_rand_seeded_p = 0;
469 #define my_MIN_SEED_BYTES 256
478 if (tls_init(apparg) == -1)
482 r =
RAND_load_file(filename, (n > 0 && n < LONG_MAX) ? (
long)n : LONG_MAX);
493 tls_errprintf(1, apparg,
"rand_seed_from_file: could not read %d bytes from %s.\n", n, filename);
496 tls_rand_seeded_p = 1;
508 int chunk = rest < INT_MAX ? (int)rest : INT_MAX;
512 tls_rand_seeded_p = 1;
528 namestring->
str[0] =
'\0';
534 assert(
sizeof namestring->
str >= 4);
537 namestring->
str[0] =
'?';
538 namestring->
str[1] = 0;
543 len = strlen(namestring->
str);
544 assert(namestring->
str[len] == 0);
545 assert(len < sizeof namestring->str);
547 if (len+1 ==
sizeof namestring->
str) {
553 assert(namestring->
str[len] == 0);
554 namestring->
str[--
len] =
'.';
555 namestring->
str[--
len] =
'.';
556 namestring->
str[--
len] =
'.';
565 no_passphrase_callback(
char *buf,
int num,
int w,
void *
arg)
570 #if OPENSSL_VERSION_NUMBER >= 0x00907000L
581 #if OPENSSL_VERSION_NUMBER >= 0x00905000L
593 static DH *tls_dhe1024 = NULL;
600 const char *
seed[] = {
";-) :-( :-) :-( ",
602 "Random String no. 12",
604 "hackers have even mo",
606 unsigned char seedbuf[20];
610 i %=
sizeof seed /
sizeof seed[0];
611 assert(strlen(seed[i]) == 20);
612 memcpy(seedbuf, seed[i], 20);
619 if (dsaparams == NULL) {
620 tls_openssl_errors(
"",
"", NULL, apparg);
625 if (dhparams == NULL) {
626 tls_openssl_errors(
"",
"", NULL, apparg);
629 if (tls_dhe1024 != NULL)
631 tls_dhe1024 = dhparams;
654 static long context_num = 0;
656 const char *err_pref_1 =
"", *err_pref_2 =
"";
658 if (tls_init(apparg) == -1)
671 tls_errprintf(1, apparg,
"Need a key file.\n");
675 tls_errprintf(1, apparg,
"Need a certificate chain file.\n");
681 if (!tls_rand_seeded_p) {
690 tls_errprintf(1, apparg,
"Private key \"%s\" does not match certificate \"%s\".\n", a.
key_file, a.
certificate_file);
711 err_pref_1 =
" while processing certificate file ";
722 if (SSL_CTX_get_client_CA_list(ret) == NULL) {
723 tls_errprintf(1, apparg,
"Could not set client CA list from \"%s\".\n", a.
ca_file);
731 if (tls_dhe1024 == NULL) {
742 if (tls_dhe1024 == NULL)
774 tls_openssl_errors(err_pref_1, err_pref_2, NULL, apparg);
785 tls_socket_nonblocking(
int fd)
789 v = fcntl(fd, F_GETFL, 0);
795 r = fcntl(fd, F_SETFL, v | O_NONBLOCK);
807 return a > b ? a :
b;
811 tls_sockets_select(
int read_select_1,
int read_select_2,
int write_select_1,
int write_select_2,
int seconds )
814 fd_set reads, writes;
815 struct timeval timeout;
816 struct timeval *timeout_p;
818 assert(read_select_1 >= -1 && read_select_2 >= -1 && write_select_1 >= -1 && write_select_2 >= -1);
821 maxfd =
max(
max(read_select_1, read_select_2),
max(write_select_1, write_select_2));
827 for(n = 0; n < 4; ++n) {
833 if (i == 0 && w == 0)
835 else if (i == 1 && w == 0)
837 else if (i == 0 && w == 1)
840 assert(i == 1 && w == 1);
853 timeout.tv_sec = seconds;
855 timeout_p = &timeout;
860 select(maxfd + 1, &reads, &writes, (fd_set *) NULL, timeout_p);
866 #define TUNNELBUFSIZE (16*1024)
873 static int tls_connect_attempt(
SSL *,
int *write_select,
int *read_select,
int *closed,
int *progress,
const char **err_pref);
875 static int tls_accept_attempt(
SSL *,
int *write_select,
int *read_select,
int *closed,
int *progress,
const char **err_pref);
877 static int tls_write_attempt(
SSL *,
struct tunnelbuf *,
int *write_select,
int *read_select,
int *closed,
int *progress,
const char **err_pref);
879 static int tls_read_attempt(
SSL *,
struct tunnelbuf *,
int *write_select,
int *read_select,
int *closed,
int *progress,
const char **err_pref);
881 static int write_attempt(
int fd,
struct tunnelbuf *,
int *select,
int *closed,
int *progress);
883 static int read_attempt(
int fd,
struct tunnelbuf *,
int *select,
int *closed,
int *progress);
885 static void write_info(
SSL *ssl,
int *info_fd)
887 if (*info_fd != -1) {
901 tls_get_x509_subject_name_oneline(peercert, &peer);
902 if (peercert != NULL)
905 if (peer.str[0] ==
'\0')
908 if (strchr(peer.str,
'\n')) {
910 *strchr(peer.str,
'\n') =
'\0';
914 if (r == -1 || r >=
sizeof infobuf)
915 r =
sizeof infobuf - 1;
916 write(*info_fd, infobuf, r);
925 tls_proxy(
int clear_fd,
int tls_fd,
int info_fd,
SSL_CTX *ctx,
int client_p)
927 struct tunnelbuf clear_to_tls, tls_to_clear;
930 int closed, in_handshake;
931 const char *err_pref_1 =
"", *err_pref_2 =
"";
932 const char *err_def = NULL;
934 assert(clear_fd != -1);
935 assert(tls_fd != -1);
941 tls_rand_seed_uniquely();
943 tls_socket_nonblocking(clear_fd);
945 tls_socket_nonblocking(tls_fd);
956 assert(rbio != NULL);
957 assert(wbio != NULL);
965 tls_to_clear.len = 0;
966 tls_to_clear.offset = 0;
967 clear_to_tls.len = 0;
968 clear_to_tls.offset = 0;
970 err_def =
"I/O error";
977 int clear_read_select = 0, clear_write_select = 0,
978 tls_read_select = 0, tls_write_select = 0,
984 DEBUG_MSG2(
"loop iteration", ++tls_loop_count);
989 r = tls_connect_attempt(ssl, &tls_write_select, &tls_read_select, &closed, &progress, &err_pref_1);
991 r = tls_accept_attempt(ssl, &tls_write_select, &tls_read_select, &closed, &progress, &err_pref_1);
993 write_info(ssl, &info_fd);
1000 write_info(ssl, &info_fd);
1004 if (clear_to_tls.len != 0 && !in_handshake) {
1007 r = tls_write_attempt(ssl, &clear_to_tls, &tls_write_select, &tls_read_select, &closed, &progress, &err_pref_1);
1012 tls_to_clear.offset = 0;
1013 tls_to_clear.len = 0;
1017 if (tls_to_clear.len != 0) {
1020 r = write_attempt(clear_fd, &tls_to_clear, &clear_write_select, &closed, &progress);
1025 clear_to_tls.offset = 0;
1026 clear_to_tls.len = 0;
1031 if (clear_to_tls.offset + clear_to_tls.len <
sizeof clear_to_tls.buf) {
1032 r = read_attempt(clear_fd, &clear_to_tls, &clear_read_select, &closed, &progress);
1042 if (!closed && !in_handshake) {
1043 if (tls_to_clear.offset + tls_to_clear.len <
sizeof tls_to_clear.buf) {
1044 r = tls_read_attempt(ssl, &tls_to_clear, &tls_write_select, &tls_read_select, &closed, &progress, &err_pref_1);
1061 assert(clear_read_select || tls_read_select || clear_write_select || tls_write_select);
1062 tls_sockets_select(clear_read_select ? clear_fd : -1, tls_read_select ? tls_fd : -1, clear_write_select ? clear_fd : -1, tls_write_select ? tls_fd : -1, -1);
1069 tls_openssl_errors(err_pref_1, err_pref_2, err_def, tls_child_apparg);
1076 tls_get_error(
SSL *ssl,
int r,
int *write_select,
int *read_select,
int *closed,
int *progress)
1080 if (err == SSL_ERROR_NONE) {
1108 tls_connect_attempt(
SSL *ssl,
int *write_select,
int *read_select,
int *closed,
int *progress,
const char **err_pref)
1115 r = tls_get_error(ssl, n, write_select, read_select, closed, progress);
1117 *err_pref =
" during SSL_connect";
1122 tls_accept_attempt(
SSL *ssl,
int *write_select,
int *read_select,
int *closed,
int *progress,
const char **err_pref)
1129 r = tls_get_error(ssl, n, write_select, read_select, closed, progress);
1131 *err_pref =
" during SSL_accept";
1136 tls_write_attempt(
SSL *ssl,
struct tunnelbuf *
buf,
int *write_select,
int *read_select,
int *closed,
int *progress,
const char **err_pref)
1143 r = tls_get_error(ssl, n, write_select, read_select, closed, progress);
1146 assert(buf->
len >= 0);
1153 *err_pref =
" during SSL_write";
1158 tls_read_attempt(
SSL *ssl,
struct tunnelbuf *buf,
int *write_select,
int *read_select,
int *closed,
int *progress,
const char **err_pref)
1165 assert(total < sizeof buf->buf);
1168 r = tls_get_error(ssl, n, write_select, read_select, closed, progress);
1174 *err_pref =
" during SSL_read";
1179 get_error(
int r,
int *select,
int *closed,
int *progress)
1188 if (errno == EAGAIN || errno == EWOULDBLOCK) {
1191 }
else if (errno == EPIPE) {
1200 static int write_attempt(
int fd,
struct tunnelbuf *buf,
int *select,
int *closed,
int *progress)
1207 r = get_error(n, select, closed, progress);
1210 assert(buf->
len >= 0);
1217 tls_errprintf(1, tls_child_apparg,
"write error: %s\n", strerror(errno));
1222 read_attempt(
int fd,
struct tunnelbuf *buf,
int *select,
int *closed,
int *progress)
1229 assert(total < sizeof buf->buf);
1230 n =
read(fd, buf->
buf + total, (
sizeof buf->
buf) - total);
1232 r = get_error(n, select, closed, progress);
1238 tls_errprintf(1, tls_child_apparg,
"read error: %s\n", strerror(errno));