summaryrefslogtreecommitdiff
path: root/src/regress/lib/libssl/interop/client.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/regress/lib/libssl/interop/client.c')
-rw-r--r--src/regress/lib/libssl/interop/client.c232
1 files changed, 0 insertions, 232 deletions
diff --git a/src/regress/lib/libssl/interop/client.c b/src/regress/lib/libssl/interop/client.c
deleted file mode 100644
index 27ad9a0ade..0000000000
--- a/src/regress/lib/libssl/interop/client.c
+++ /dev/null
@@ -1,232 +0,0 @@
1/* $OpenBSD: client.c,v 1.8 2019/03/21 17:52:26 bluhm Exp $ */
2/*
3 * Copyright (c) 2018-2019 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 <string.h>
25#include <unistd.h>
26
27#include <openssl/err.h>
28#include <openssl/ssl.h>
29
30#include "util.h"
31
32void __dead usage(void);
33
34void __dead
35usage(void)
36{
37 fprintf(stderr, "usage: client [-Lsv] [-C CA] [-c crt -k key] "
38 "[-l ciphers] host port\n");
39 exit(2);
40}
41
42int
43main(int argc, char *argv[])
44{
45 const SSL_METHOD *method;
46 SSL_CTX *ctx;
47 SSL *ssl;
48 BIO *bio;
49 SSL_SESSION *session = NULL;
50 int ch, error, listciphers = 0, sessionreuse = 0, verify = 0;
51 char buf[256];
52 char *ca = NULL, *crt = NULL, *key = NULL, *ciphers = NULL;
53 char *host_port, *host = "127.0.0.1", *port = "0";
54
55
56 while ((ch = getopt(argc, argv, "C:c:k:Ll:sv")) != -1) {
57 switch (ch) {
58 case 'C':
59 ca = optarg;
60 break;
61 case 'c':
62 crt = optarg;
63 break;
64 case 'k':
65 key = optarg;
66 break;
67 case 'L':
68 listciphers = 1;
69 break;
70 case 'l':
71 ciphers = optarg;
72 break;
73 case 's':
74 /* multiple reueses are possible */
75 sessionreuse++;
76 break;
77 case 'v':
78 verify = 1;
79 break;
80 default:
81 usage();
82 }
83 }
84 argc -= optind;
85 argv += optind;
86 if (argc == 2) {
87 host = argv[0];
88 port = argv[1];
89 } else if (!listciphers) {
90 usage();
91 }
92 if (asprintf(&host_port, strchr(host, ':') ? "[%s]:%s" : "%s:%s",
93 host, port) == -1)
94 err(1, "asprintf host port");
95 if ((crt == NULL && key != NULL) || (crt != NULL && key == NULL))
96 errx(1, "certificate and private key must be used together");
97
98 SSL_library_init();
99 SSL_load_error_strings();
100 print_version();
101
102 /* setup method and context */
103#if OPENSSL_VERSION_NUMBER >= 0x1010000f
104 method = TLS_client_method();
105 if (method == NULL)
106 err_ssl(1, "TLS_client_method");
107#else
108 method = SSLv23_client_method();
109 if (method == NULL)
110 err_ssl(1, "SSLv23_client_method");
111#endif
112 ctx = SSL_CTX_new(method);
113 if (ctx == NULL)
114 err_ssl(1, "SSL_CTX_new");
115
116 /* load client certificate */
117 if (crt != NULL) {
118 if (SSL_CTX_use_certificate_file(ctx, crt,
119 SSL_FILETYPE_PEM) <= 0)
120 err_ssl(1, "SSL_CTX_use_certificate_file");
121 if (SSL_CTX_use_PrivateKey_file(ctx, key,
122 SSL_FILETYPE_PEM) <= 0)
123 err_ssl(1, "SSL_CTX_use_PrivateKey_file");
124 if (SSL_CTX_check_private_key(ctx) <= 0)
125 err_ssl(1, "SSL_CTX_check_private_key");
126 }
127
128 /* verify server certificate */
129 if (ca != NULL) {
130 if (SSL_CTX_load_verify_locations(ctx, ca, NULL) <= 0)
131 err_ssl(1, "SSL_CTX_load_verify_locations");
132 }
133 SSL_CTX_set_verify(ctx, verify ? SSL_VERIFY_PEER : SSL_VERIFY_NONE,
134 verify_callback);
135
136 if (sessionreuse) {
137 SSL_CTX_set_session_cache_mode(ctx, SSL_SESS_CACHE_CLIENT);
138 }
139
140 if (ciphers) {
141 if (SSL_CTX_set_cipher_list(ctx, ciphers) <= 0)
142 err_ssl(1, "SSL_CTX_set_cipher_list");
143 }
144
145 if (listciphers) {
146 ssl = SSL_new(ctx);
147 if (ssl == NULL)
148 err_ssl(1, "SSL_new");
149 print_ciphers(SSL_get_ciphers(ssl));
150 return 0;
151 }
152
153 do {
154 /* setup bio for socket operations */
155 bio = BIO_new_connect(host_port);
156 if (bio == NULL)
157 err_ssl(1, "BIO_new_connect");
158
159 /* connect */
160 if (BIO_do_connect(bio) <= 0)
161 err_ssl(1, "BIO_do_connect");
162 printf("connect ");
163 print_sockname(bio);
164 printf("connect ");
165 print_peername(bio);
166
167 /* do ssl client handshake */
168 ssl = SSL_new(ctx);
169 if (ssl == NULL)
170 err_ssl(1, "SSL_new");
171 SSL_set_bio(ssl, bio, bio);
172 /* resuse session if possible */
173 if (session != NULL) {
174 if (SSL_set_session(ssl, session) <= 0)
175 err_ssl(1, "SSL_set_session");
176 }
177 if ((error = SSL_connect(ssl)) <= 0)
178 err_ssl(1, "SSL_connect %d", error);
179 printf("session %d: %s\n", sessionreuse,
180 SSL_session_reused(ssl) ? "reuse" : "new");
181 if (fflush(stdout) != 0)
182 err(1, "fflush stdout");
183
184 /* print session statistics */
185 if (sessionreuse) {
186 session = SSL_get1_session(ssl);
187 if (session == NULL)
188 err_ssl(1, "SSL1_get_session");
189 } else {
190 session = SSL_get_session(ssl);
191 if (session == NULL)
192 err_ssl(1, "SSL_get_session");
193 }
194 if (SSL_SESSION_print_fp(stdout, session) <= 0)
195 err_ssl(1, "SSL_SESSION_print_fp");
196
197 /* read server greeting and write client hello over TLS */
198 if ((error = SSL_read(ssl, buf, 9)) <= 0)
199 err_ssl(1, "SSL_read %d", error);
200 if (error != 9)
201 errx(1, "read not 9 bytes greeting: %d", error);
202 buf[9] = '\0';
203 printf("<<< %s", buf);
204 if (fflush(stdout) != 0)
205 err(1, "fflush stdout");
206 strlcpy(buf, "hello\n", sizeof(buf));
207 printf(">>> %s", buf);
208 if (fflush(stdout) != 0)
209 err(1, "fflush stdout");
210 if ((error = SSL_write(ssl, buf, 6)) <= 0)
211 err_ssl(1, "SSL_write %d", error);
212 if (error != 6)
213 errx(1, "write not 6 bytes hello: %d", error);
214
215 /* shutdown connection */
216 if ((error = SSL_shutdown(ssl)) < 0)
217 err_ssl(1, "SSL_shutdown unidirectional %d", error);
218 if (error <= 0) {
219 if ((error = SSL_shutdown(ssl)) <= 0)
220 err_ssl(1, "SSL_shutdown bidirectional %d",
221 error);
222 }
223
224 SSL_free(ssl);
225 } while (sessionreuse--);
226
227 SSL_CTX_free(ctx);
228
229 printf("success\n");
230
231 return 0;
232}