diff options
author | bluhm <> | 2018-11-07 01:08:49 +0000 |
---|---|---|
committer | bluhm <> | 2018-11-07 01:08:49 +0000 |
commit | 72b3bb10734114d4b5795542387a8aeef8e24d10 (patch) | |
tree | a8266eebb496a361bb45e73920260a5f766bd201 | |
parent | 85ff90e3521b98704f2fee667927fa140f56dd14 (diff) | |
download | openbsd-72b3bb10734114d4b5795542387a8aeef8e24d10.tar.gz openbsd-72b3bb10734114d4b5795542387a8aeef8e24d10.tar.bz2 openbsd-72b3bb10734114d4b5795542387a8aeef8e24d10.zip |
Test TLS interoperability between LibreSSL and OpenSSL.
Implement simple SSL client and server in C. Create four binaries
by linking them with LibreSSL or OpenSSL. This way API compatibility
is tested. Connect and accept with netcat to test protocol
compatibility with libtls.
Currently OpenSSL 1.0.2p from ports is used. Plan is to move to
OpenSSL 1.1 and and test TLS 1.3.
idea from beck@; help from jsing@
-rw-r--r-- | src/regress/lib/libssl/interop/LICENSE | 15 | ||||
-rw-r--r-- | src/regress/lib/libssl/interop/Makefile | 5 | ||||
-rw-r--r-- | src/regress/lib/libssl/interop/Makefile.inc | 62 | ||||
-rw-r--r-- | src/regress/lib/libssl/interop/README | 9 | ||||
-rw-r--r-- | src/regress/lib/libssl/interop/client.c | 136 | ||||
-rw-r--r-- | src/regress/lib/libssl/interop/libressl/Makefile | 20 | ||||
-rw-r--r-- | src/regress/lib/libssl/interop/openssl/Makefile | 27 | ||||
-rw-r--r-- | src/regress/lib/libssl/interop/server.c | 161 | ||||
-rw-r--r-- | src/regress/lib/libssl/interop/util.c | 93 | ||||
-rw-r--r-- | src/regress/lib/libssl/interop/util.h | 21 |
10 files changed, 549 insertions, 0 deletions
diff --git a/src/regress/lib/libssl/interop/LICENSE b/src/regress/lib/libssl/interop/LICENSE new file mode 100644 index 0000000000..8695620495 --- /dev/null +++ b/src/regress/lib/libssl/interop/LICENSE | |||
@@ -0,0 +1,15 @@ | |||
1 | /* | ||
2 | * Copyright (c) 2018 Alexander Bluhm <bluhm@openbsd.org> | ||
3 | * | ||
4 | * Permission to use, copy, modify, and distribute this software for any | ||
5 | * purpose with or without fee is hereby granted, provided that the above | ||
6 | * copyright notice and this permission notice appear in all copies. | ||
7 | * | ||
8 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | ||
9 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | ||
10 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | ||
11 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | ||
12 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | ||
13 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | ||
14 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | ||
15 | */ | ||
diff --git a/src/regress/lib/libssl/interop/Makefile b/src/regress/lib/libssl/interop/Makefile new file mode 100644 index 0000000000..997cad2949 --- /dev/null +++ b/src/regress/lib/libssl/interop/Makefile | |||
@@ -0,0 +1,5 @@ | |||
1 | # $OpenBSD: Makefile,v 1.1.1.1 2018/11/07 01:08:49 bluhm Exp $ | ||
2 | |||
3 | SUBDIR = libressl openssl | ||
4 | |||
5 | .include <bsd.subdir.mk> | ||
diff --git a/src/regress/lib/libssl/interop/Makefile.inc b/src/regress/lib/libssl/interop/Makefile.inc new file mode 100644 index 0000000000..fc282bea88 --- /dev/null +++ b/src/regress/lib/libssl/interop/Makefile.inc | |||
@@ -0,0 +1,62 @@ | |||
1 | # $OpenBSD: Makefile.inc,v 1.1.1.1 2018/11/07 01:08:49 bluhm Exp $ | ||
2 | |||
3 | .PATH: ${.CURDIR}/.. | ||
4 | |||
5 | SRCS_client = client.c util.c | ||
6 | SRCS_server = server.c util.c | ||
7 | WARNINGS = yes | ||
8 | REGRESS_TARGETS = | ||
9 | |||
10 | # check that program is linked with correct libraries | ||
11 | |||
12 | .for p in ${PROGS} | ||
13 | CLEANFILES += ldd-$p.out | ||
14 | REGRESS_TARGETS += run-ldd-$p | ||
15 | ldd-$p.out: $p | ||
16 | LD_LIBRARY_PATH=${LD_LIBRARY_PATH} ldd $p >$@ | ||
17 | .endfor | ||
18 | |||
19 | # run netcat server and connect with test client | ||
20 | |||
21 | CLEANFILES += client.out netcat-l.out netcat-l.fstat | ||
22 | REGRESS_TARGETS += run-client | ||
23 | run-client: client 127.0.0.1.crt | ||
24 | @echo '\n======== $@ ========' | ||
25 | echo "greeting" | nc -l -c -C 127.0.0.1.crt -K 127.0.0.1.key \ | ||
26 | 127.0.0.1 0 >netcat-l.out & \ | ||
27 | sleep 1; fstat -p $$! >netcat-l.fstat | ||
28 | LD_LIBRARY_PATH=${LD_LIBRARY_PATH} ./client \ | ||
29 | `sed -n 's/.* stream tcp .*:/127.0.0.1 /p' netcat-l.fstat` \ | ||
30 | >client.out | ||
31 | # check that the client run successfully to the end | ||
32 | grep -q '^success$$' client.out | ||
33 | # client must have read server greeting | ||
34 | grep -q '^<<< greeting$$' client.out | ||
35 | # netstat server must have read client hello | ||
36 | grep -q '^hello$$' netcat-l.out | ||
37 | |||
38 | # run test server and connect with netcat client | ||
39 | |||
40 | CLEANFILES += server.out netcat.out | ||
41 | REGRESS_TARGETS += run-server | ||
42 | run-server: server 127.0.0.1.crt | ||
43 | @echo '\n======== $@ ========' | ||
44 | LD_LIBRARY_PATH=${LD_LIBRARY_PATH} ./server 127.0.0.1 0 >server.out | ||
45 | echo "hello" | nc -c -T noverify \ | ||
46 | `sed -n 's/listen sock: //p' server.out` \ | ||
47 | >netcat.out | ||
48 | # check that the server child run successfully to the end | ||
49 | grep -q '^success$$' server.out | ||
50 | # server must have read client hello | ||
51 | grep -q '^<<< hello$$' server.out | ||
52 | # client must have read server greeting | ||
53 | grep -q '^greeting$$' netcat.out | ||
54 | |||
55 | # create certificates for TLS | ||
56 | |||
57 | CLEANFILES += 127.0.0.1.crt 127.0.0.1.key | ||
58 | |||
59 | 127.0.0.1.crt: | ||
60 | openssl req -batch -new \ | ||
61 | -subj /L=OpenBSD/O=tls-regress/OU=server/CN=127.0.0.1/ \ | ||
62 | -nodes -newkey rsa -keyout 127.0.0.1.key -x509 -out $@ | ||
diff --git a/src/regress/lib/libssl/interop/README b/src/regress/lib/libssl/interop/README new file mode 100644 index 0000000000..d1ecc7e683 --- /dev/null +++ b/src/regress/lib/libssl/interop/README | |||
@@ -0,0 +1,9 @@ | |||
1 | Test TLS interoperability between LibreSSL and OpenSSL. | ||
2 | |||
3 | Implement simple SSL client and server in C. Create four binaries | ||
4 | by linking them with LibreSSL or OpenSSL. This way API compatibility | ||
5 | is tested. Connect and accept with netcat to test protocol | ||
6 | compatibility with libtls. | ||
7 | |||
8 | Currently OpenSSL 1.0.2p from ports is used. Plan is to move to | ||
9 | OpenSSL 1.1 and and test TLS 1.3. | ||
diff --git a/src/regress/lib/libssl/interop/client.c b/src/regress/lib/libssl/interop/client.c new file mode 100644 index 0000000000..d4d4f1e94d --- /dev/null +++ b/src/regress/lib/libssl/interop/client.c | |||
@@ -0,0 +1,136 @@ | |||
1 | /* $OpenBSD: client.c,v 1.1.1.1 2018/11/07 01:08:49 bluhm Exp $ */ | ||
2 | /* | ||
3 | * Copyright (c) 2018 Alexander Bluhm <bluhm@openbsd.org> | ||
4 | * | ||
5 | * Permission to use, copy, modify, and distribute this software for any | ||
6 | * purpose with or without fee is hereby granted, provided that the above | ||
7 | * copyright notice and this permission notice appear in all copies. | ||
8 | * | ||
9 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | ||
10 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | ||
11 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | ||
12 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | ||
13 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | ||
14 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | ||
15 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | ||
16 | */ | ||
17 | |||
18 | #include <sys/types.h> | ||
19 | #include <sys/socket.h> | ||
20 | |||
21 | #include <err.h> | ||
22 | #include <netdb.h> | ||
23 | #include <stdio.h> | ||
24 | #include <unistd.h> | ||
25 | |||
26 | #include <openssl/err.h> | ||
27 | #include <openssl/ssl.h> | ||
28 | |||
29 | #include "util.h" | ||
30 | |||
31 | void __dead usage(void); | ||
32 | |||
33 | void __dead | ||
34 | usage(void) | ||
35 | { | ||
36 | fprintf(stderr, "usage: client host port"); | ||
37 | exit(2); | ||
38 | } | ||
39 | |||
40 | int | ||
41 | main(int argc, char *argv[]) | ||
42 | { | ||
43 | const SSL_METHOD *method; | ||
44 | SSL_CTX *ctx; | ||
45 | SSL *ssl; | ||
46 | BIO *bio; | ||
47 | SSL_SESSION *session; | ||
48 | int error; | ||
49 | char buf[256]; | ||
50 | char *host_port, *host, *port; | ||
51 | |||
52 | if (argc == 3) { | ||
53 | host = argv[1]; | ||
54 | port = argv[2]; | ||
55 | } else { | ||
56 | usage(); | ||
57 | } | ||
58 | if (asprintf(&host_port, strchr(host, ':') ? "[%s]:%s" : "%s:%s", | ||
59 | host, port) == -1) | ||
60 | err(1, "asprintf host port"); | ||
61 | |||
62 | SSL_library_init(); | ||
63 | SSL_load_error_strings(); | ||
64 | |||
65 | /* setup method and context */ | ||
66 | method = SSLv23_client_method(); | ||
67 | if (method == NULL) | ||
68 | err_ssl(1, "SSLv23_client_method"); | ||
69 | ctx = SSL_CTX_new(method); | ||
70 | if (ctx == NULL) | ||
71 | err_ssl(1, "SSL_CTX_new"); | ||
72 | |||
73 | /* setup ssl and bio for socket operations */ | ||
74 | ssl = SSL_new(ctx); | ||
75 | if (ssl == NULL) | ||
76 | err_ssl(1, "SSL_new"); | ||
77 | bio = BIO_new_connect(host_port); | ||
78 | if (bio == NULL) | ||
79 | err_ssl(1, "BIO_new_connect"); | ||
80 | |||
81 | print_ciphers(SSL_get_ciphers(ssl)); | ||
82 | |||
83 | /* connect */ | ||
84 | if (BIO_do_connect(bio) <= 0) | ||
85 | err_ssl(1, "BIO_do_connect"); | ||
86 | printf("connect "); | ||
87 | print_sockname(bio); | ||
88 | printf("connect "); | ||
89 | print_peername(bio); | ||
90 | |||
91 | /* do ssl client handshake */ | ||
92 | SSL_set_bio(ssl, bio, bio); | ||
93 | if ((error = SSL_connect(ssl)) <= 0) | ||
94 | err_ssl(1, "SSL_connect %d", error); | ||
95 | |||
96 | /* print session statistics */ | ||
97 | session = SSL_get_session(ssl); | ||
98 | if (session == NULL) | ||
99 | err_ssl(1, "SSL_get_session"); | ||
100 | if (SSL_SESSION_print_fp(stdout, session) <= 0) | ||
101 | err_ssl(1, "SSL_SESSION_print_fp"); | ||
102 | |||
103 | /* read server greeting and write client hello over TLS connection */ | ||
104 | if ((error = SSL_read(ssl, buf, 9)) <= 0) | ||
105 | err_ssl(1, "SSL_read %d", error); | ||
106 | if (error != 9) | ||
107 | errx(1, "read not 9 bytes greeting: %d", error); | ||
108 | buf[9] = '\0'; | ||
109 | printf("<<< %s", buf); | ||
110 | if (fflush(stdout) != 0) | ||
111 | err(1, "fflush stdout"); | ||
112 | strlcpy(buf, "hello\n", sizeof(buf)); | ||
113 | printf(">>> %s", buf); | ||
114 | if (fflush(stdout) != 0) | ||
115 | err(1, "fflush stdout"); | ||
116 | if ((error = SSL_write(ssl, buf, 6)) <= 0) | ||
117 | err_ssl(1, "SSL_write %d", error); | ||
118 | if (error != 6) | ||
119 | errx(1, "write not 6 bytes hello: %d", error); | ||
120 | |||
121 | /* shutdown connection */ | ||
122 | if ((error = SSL_shutdown(ssl)) < 0) | ||
123 | err_ssl(1, "SSL_shutdown unidirectional %d", error); | ||
124 | if (error <= 0) { | ||
125 | if ((error = SSL_shutdown(ssl)) <= 0) | ||
126 | err_ssl(1, "SSL_shutdown bidirectional %d", error); | ||
127 | } | ||
128 | |||
129 | /* cleanup and free resources */ | ||
130 | SSL_free(ssl); | ||
131 | SSL_CTX_free(ctx); | ||
132 | |||
133 | printf("success\n"); | ||
134 | |||
135 | return 0; | ||
136 | } | ||
diff --git a/src/regress/lib/libssl/interop/libressl/Makefile b/src/regress/lib/libssl/interop/libressl/Makefile new file mode 100644 index 0000000000..2d8ef78922 --- /dev/null +++ b/src/regress/lib/libssl/interop/libressl/Makefile | |||
@@ -0,0 +1,20 @@ | |||
1 | # $OpenBSD: Makefile,v 1.1.1.1 2018/11/07 01:08:49 bluhm Exp $ | ||
2 | |||
3 | PROGS = client server | ||
4 | CPPFLAGS = | ||
5 | LDFLAGS = | ||
6 | LDADD = -lssl -lcrypto | ||
7 | DPADD = ${LIBSSL} ${LIBCRYPTO} | ||
8 | LD_LIBRARY_PATH = | ||
9 | |||
10 | .for p in ${PROGS} | ||
11 | run-ldd-$p: ldd-$p.out | ||
12 | @echo '\n======== $@ ========' | ||
13 | # check that $p is linked with LibreSSL | ||
14 | grep -q /usr/lib/libcrypto.so ldd-$p.out | ||
15 | grep -q /usr/lib/libssl.so ldd-$p.out | ||
16 | # check that $p is not linked with OpenSSL | ||
17 | ! grep /usr/local/lib/ ldd-$p.out | ||
18 | .endfor | ||
19 | |||
20 | .include <bsd.regress.mk> | ||
diff --git a/src/regress/lib/libssl/interop/openssl/Makefile b/src/regress/lib/libssl/interop/openssl/Makefile new file mode 100644 index 0000000000..26095d0019 --- /dev/null +++ b/src/regress/lib/libssl/interop/openssl/Makefile | |||
@@ -0,0 +1,27 @@ | |||
1 | # $OpenBSD: Makefile,v 1.1.1.1 2018/11/07 01:08:49 bluhm Exp $ | ||
2 | |||
3 | .if ! exists(/usr/local/bin/eopenssl) | ||
4 | regress: | ||
5 | # install openssl-1.0.2p from ports for interop tests | ||
6 | @echo SKIPPED | ||
7 | .endif | ||
8 | |||
9 | PROGS = client server | ||
10 | CPPFLAGS = -I /usr/local/include/eopenssl | ||
11 | LDFLAGS = -L /usr/local/lib/eopenssl | ||
12 | LDADD = -lssl -lcrypto | ||
13 | DPADD = /usr/local/lib/eopenssl/libssl.a \ | ||
14 | /usr/local/lib/eopenssl/libcrypto.a | ||
15 | LD_LIBRARY_PATH = /usr/local/lib/eopenssl | ||
16 | |||
17 | .for p in ${PROGS} | ||
18 | run-ldd-$p: ldd-$p.out | ||
19 | @echo '\n======== $@ ========' | ||
20 | # check that $p is linked with OpenSSL | ||
21 | grep -q /usr/local/lib/eopenssl/libcrypto.so ldd-$p.out | ||
22 | grep -q /usr/local/lib/eopenssl/libssl.so ldd-$p.out | ||
23 | # check that $p is not linked with LibreSSL | ||
24 | ! grep -v libc.so ldd-$p.out | grep /usr/lib/ | ||
25 | .endfor | ||
26 | |||
27 | .include <bsd.regress.mk> | ||
diff --git a/src/regress/lib/libssl/interop/server.c b/src/regress/lib/libssl/interop/server.c new file mode 100644 index 0000000000..862ca21fcb --- /dev/null +++ b/src/regress/lib/libssl/interop/server.c | |||
@@ -0,0 +1,161 @@ | |||
1 | /* $OpenBSD: server.c,v 1.1.1.1 2018/11/07 01:08:49 bluhm Exp $ */ | ||
2 | /* | ||
3 | * Copyright (c) 2018 Alexander Bluhm <bluhm@openbsd.org> | ||
4 | * | ||
5 | * Permission to use, copy, modify, and distribute this software for any | ||
6 | * purpose with or without fee is hereby granted, provided that the above | ||
7 | * copyright notice and this permission notice appear in all copies. | ||
8 | * | ||
9 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | ||
10 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | ||
11 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | ||
12 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | ||
13 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | ||
14 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | ||
15 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | ||
16 | */ | ||
17 | |||
18 | #include <sys/types.h> | ||
19 | #include <sys/socket.h> | ||
20 | |||
21 | #include <err.h> | ||
22 | #include <netdb.h> | ||
23 | #include <stdio.h> | ||
24 | #include <unistd.h> | ||
25 | |||
26 | #include <openssl/err.h> | ||
27 | #include <openssl/ssl.h> | ||
28 | |||
29 | #include "util.h" | ||
30 | |||
31 | void __dead usage(void); | ||
32 | |||
33 | void __dead | ||
34 | usage(void) | ||
35 | { | ||
36 | fprintf(stderr, "usage: server [host port]"); | ||
37 | exit(2); | ||
38 | } | ||
39 | |||
40 | int | ||
41 | main(int argc, char *argv[]) | ||
42 | { | ||
43 | const SSL_METHOD *method; | ||
44 | SSL_CTX *ctx; | ||
45 | SSL *ssl; | ||
46 | BIO *bio; | ||
47 | SSL_SESSION *session; | ||
48 | int error; | ||
49 | char buf[256]; | ||
50 | char *crt, *key, *host_port, *host = "127.0.0.1", *port = "0"; | ||
51 | |||
52 | if (argc == 3) { | ||
53 | host = argv[1]; | ||
54 | port = argv[2]; | ||
55 | } else if (argc != 1) { | ||
56 | usage(); | ||
57 | } | ||
58 | if (asprintf(&host_port, strchr(host, ':') ? "[%s]:%s" : "%s:%s", | ||
59 | host, port) == -1) | ||
60 | err(1, "asprintf host port"); | ||
61 | if (asprintf(&crt, "%s.crt", host) == -1) | ||
62 | err(1, "asprintf crt"); | ||
63 | if (asprintf(&key, "%s.key", host) == -1) | ||
64 | err(1, "asprintf key"); | ||
65 | |||
66 | SSL_library_init(); | ||
67 | SSL_load_error_strings(); | ||
68 | |||
69 | /* setup method and context */ | ||
70 | method = SSLv23_server_method(); | ||
71 | if (method == NULL) | ||
72 | err_ssl(1, "SSLv23_server_method"); | ||
73 | ctx = SSL_CTX_new(method); | ||
74 | if (ctx == NULL) | ||
75 | err_ssl(1, "SSL_CTX_new"); | ||
76 | |||
77 | /* needed when linking with OpenSSL 1.0.2p */ | ||
78 | if (SSL_CTX_set_ecdh_auto(ctx, 1) <= 0) | ||
79 | err_ssl(1, "SSL_CTX_set_ecdh_auto"); | ||
80 | |||
81 | /* load server certificate */ | ||
82 | if (SSL_CTX_use_certificate_file(ctx, crt, SSL_FILETYPE_PEM) <= 0) | ||
83 | err_ssl(1, "SSL_CTX_use_certificate_file"); | ||
84 | if (SSL_CTX_use_PrivateKey_file(ctx, key, SSL_FILETYPE_PEM) <= 0) | ||
85 | err_ssl(1, "SSL_CTX_use_PrivateKey_file"); | ||
86 | if (SSL_CTX_check_private_key(ctx) <= 0) | ||
87 | err_ssl(1, "SSL_CTX_check_private_key"); | ||
88 | |||
89 | /* setup ssl and bio for socket operations */ | ||
90 | ssl = SSL_new(ctx); | ||
91 | if (ssl == NULL) | ||
92 | err_ssl(1, "SSL_new"); | ||
93 | bio = BIO_new_accept(host_port); | ||
94 | if (bio == NULL) | ||
95 | err_ssl(1, "BIO_new_accept"); | ||
96 | |||
97 | print_ciphers(SSL_get_ciphers(ssl)); | ||
98 | |||
99 | /* bind, listen */ | ||
100 | if (BIO_do_accept(bio) <= 0) | ||
101 | err_ssl(1, "BIO_do_accept setup"); | ||
102 | printf("listen "); | ||
103 | print_sockname(bio); | ||
104 | |||
105 | /* fork to background and accept */ | ||
106 | if (daemon(1, 1) == -1) | ||
107 | err(1, "daemon"); | ||
108 | if (BIO_do_accept(bio) <= 0) | ||
109 | err_ssl(1, "BIO_do_accept wait"); | ||
110 | bio = BIO_pop(bio); | ||
111 | printf("accept "); | ||
112 | print_sockname(bio); | ||
113 | printf("accept "); | ||
114 | print_peername(bio); | ||
115 | |||
116 | /* do ssl server handshake */ | ||
117 | SSL_set_bio(ssl, bio, bio); | ||
118 | if ((error = SSL_accept(ssl)) <= 0) | ||
119 | err_ssl(1, "SSL_accept %d", error); | ||
120 | |||
121 | /* print session statistics */ | ||
122 | session = SSL_get_session(ssl); | ||
123 | if (session == NULL) | ||
124 | err_ssl(1, "SSL_get_session"); | ||
125 | if (SSL_SESSION_print_fp(stdout, session) <= 0) | ||
126 | err_ssl(1, "SSL_SESSION_print_fp"); | ||
127 | |||
128 | /* write server greeting and read client hello over TLS connection */ | ||
129 | strlcpy(buf, "greeting\n", sizeof(buf)); | ||
130 | printf(">>> %s", buf); | ||
131 | if (fflush(stdout) != 0) | ||
132 | err(1, "fflush stdout"); | ||
133 | if ((error = SSL_write(ssl, buf, 9)) <= 0) | ||
134 | err_ssl(1, "SSL_write %d", error); | ||
135 | if (error != 9) | ||
136 | errx(1, "write not 9 bytes greeting: %d", error); | ||
137 | if ((error = SSL_read(ssl, buf, 6)) <= 0) | ||
138 | err_ssl(1, "SSL_read %d", error); | ||
139 | if (error != 6) | ||
140 | errx(1, "read not 6 bytes hello: %d", error); | ||
141 | buf[6] = '\0'; | ||
142 | printf("<<< %s", buf); | ||
143 | if (fflush(stdout) != 0) | ||
144 | err(1, "fflush stdout"); | ||
145 | |||
146 | /* shutdown connection */ | ||
147 | if ((error = SSL_shutdown(ssl)) < 0) | ||
148 | err_ssl(1, "SSL_shutdown unidirectional %d", error); | ||
149 | if (error <= 0) { | ||
150 | if ((error = SSL_shutdown(ssl)) <= 0) | ||
151 | err_ssl(1, "SSL_shutdown bidirectional %d", error); | ||
152 | } | ||
153 | |||
154 | /* cleanup and free resources */ | ||
155 | SSL_free(ssl); | ||
156 | SSL_CTX_free(ctx); | ||
157 | |||
158 | printf("success\n"); | ||
159 | |||
160 | return 0; | ||
161 | } | ||
diff --git a/src/regress/lib/libssl/interop/util.c b/src/regress/lib/libssl/interop/util.c new file mode 100644 index 0000000000..3f1c221d51 --- /dev/null +++ b/src/regress/lib/libssl/interop/util.c | |||
@@ -0,0 +1,93 @@ | |||
1 | /* $OpenBSD: util.c,v 1.1.1.1 2018/11/07 01:08:49 bluhm Exp $ */ | ||
2 | /* | ||
3 | * Copyright (c) 2018 Alexander Bluhm <bluhm@openbsd.org> | ||
4 | * | ||
5 | * Permission to use, copy, modify, and distribute this software for any | ||
6 | * purpose with or without fee is hereby granted, provided that the above | ||
7 | * copyright notice and this permission notice appear in all copies. | ||
8 | * | ||
9 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | ||
10 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | ||
11 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | ||
12 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | ||
13 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | ||
14 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | ||
15 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | ||
16 | */ | ||
17 | |||
18 | #include <sys/types.h> | ||
19 | #include <sys/socket.h> | ||
20 | |||
21 | #include <err.h> | ||
22 | #include <netdb.h> | ||
23 | #include <stdio.h> | ||
24 | |||
25 | #include <openssl/err.h> | ||
26 | #include <openssl/ssl.h> | ||
27 | |||
28 | #include "util.h" | ||
29 | |||
30 | void | ||
31 | print_ciphers(STACK_OF(SSL_CIPHER) *cstack) | ||
32 | { | ||
33 | SSL_CIPHER *cipher; | ||
34 | int i; | ||
35 | |||
36 | for (i = 0; (cipher = sk_SSL_CIPHER_value(cstack, i)) != NULL; i++) | ||
37 | printf("cipher %s\n", SSL_CIPHER_get_name(cipher)); | ||
38 | if (fflush(stdout) != 0) | ||
39 | err(1, "fflush stdout"); | ||
40 | } | ||
41 | |||
42 | void | ||
43 | print_sockname(BIO *bio) | ||
44 | { | ||
45 | struct sockaddr_storage ss; | ||
46 | socklen_t slen; | ||
47 | char host[NI_MAXHOST], port[NI_MAXSERV]; | ||
48 | int fd; | ||
49 | |||
50 | if (BIO_get_fd(bio, &fd) <= 0) | ||
51 | err_ssl(1, "BIO_get_fd"); | ||
52 | slen = sizeof(ss); | ||
53 | if (getsockname(fd, (struct sockaddr *)&ss, &slen) == -1) | ||
54 | err(1, "getsockname"); | ||
55 | if (getnameinfo((struct sockaddr *)&ss, ss.ss_len, host, | ||
56 | sizeof(host), port, sizeof(port), NI_NUMERICHOST | NI_NUMERICSERV)) | ||
57 | errx(1, "getnameinfo"); | ||
58 | printf("sock: %s %s\n", host, port); | ||
59 | if (fflush(stdout) != 0) | ||
60 | err(1, "fflush stdout"); | ||
61 | } | ||
62 | |||
63 | void | ||
64 | print_peername(BIO *bio) | ||
65 | { | ||
66 | struct sockaddr_storage ss; | ||
67 | socklen_t slen; | ||
68 | char host[NI_MAXHOST], port[NI_MAXSERV]; | ||
69 | int fd; | ||
70 | |||
71 | if (BIO_get_fd(bio, &fd) <= 0) | ||
72 | err_ssl(1, "BIO_get_fd"); | ||
73 | slen = sizeof(ss); | ||
74 | if (getpeername(fd, (struct sockaddr *)&ss, &slen) == -1) | ||
75 | err(1, "getpeername"); | ||
76 | if (getnameinfo((struct sockaddr *)&ss, ss.ss_len, host, | ||
77 | sizeof(host), port, sizeof(port), NI_NUMERICHOST | NI_NUMERICSERV)) | ||
78 | errx(1, "getnameinfo"); | ||
79 | printf("peer: %s %s\n", host, port); | ||
80 | if (fflush(stdout) != 0) | ||
81 | err(1, "fflush stdout"); | ||
82 | } | ||
83 | |||
84 | void | ||
85 | err_ssl(int eval, const char *fmt, ...) | ||
86 | { | ||
87 | va_list ap; | ||
88 | |||
89 | ERR_print_errors_fp(stderr); | ||
90 | va_start(ap, fmt); | ||
91 | verrx(eval, fmt, ap); | ||
92 | va_end(ap); | ||
93 | } | ||
diff --git a/src/regress/lib/libssl/interop/util.h b/src/regress/lib/libssl/interop/util.h new file mode 100644 index 0000000000..2fdebf34b3 --- /dev/null +++ b/src/regress/lib/libssl/interop/util.h | |||
@@ -0,0 +1,21 @@ | |||
1 | /* $OpenBSD: util.h,v 1.1.1.1 2018/11/07 01:08:49 bluhm Exp $ */ | ||
2 | /* | ||
3 | * Copyright (c) 2018 Alexander Bluhm <bluhm@openbsd.org> | ||
4 | * | ||
5 | * Permission to use, copy, modify, and distribute this software for any | ||
6 | * purpose with or without fee is hereby granted, provided that the above | ||
7 | * copyright notice and this permission notice appear in all copies. | ||
8 | * | ||
9 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | ||
10 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | ||
11 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | ||
12 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | ||
13 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | ||
14 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | ||
15 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | ||
16 | */ | ||
17 | |||
18 | void print_ciphers(STACK_OF(SSL_CIPHER) *); | ||
19 | void print_sockname(BIO *); | ||
20 | void print_peername(BIO *); | ||
21 | void err_ssl(int, const char *, ...); | ||