2 * Simple Client/Server connection test
4 * Based on OpenSSL example code.
5 * Copyright (C) 2019 vt@altlinux.org. All Rights Reserved.
7 * Contents licensed under the terms of the OpenSSL license
8 * See https://www.openssl.org/source/license.html for details
11 #include "e_gost_err.h"
13 #include <openssl/evp.h>
14 #include <openssl/ssl.h>
15 #include <openssl/bio.h>
16 #include <openssl/rand.h>
17 #include <openssl/err.h>
18 #include <openssl/asn1.h>
19 #include <openssl/obj_mac.h>
20 #include <openssl/x509v3.h>
21 #include <openssl/ec.h>
22 #include <openssl/bn.h>
27 #include <sys/types.h>
29 #include <sys/socket.h>
30 #include <netinet/in.h>
31 #include <arpa/inet.h>
34 /* For X509_NAME_add_entry_by_txt */
35 #pragma GCC diagnostic ignored "-Wpointer-sign"
37 #define T(e) ({ if (!(e)) { \
38 ERR_print_errors_fp(stderr); \
39 OpenSSLDie(__FILE__, __LINE__, #e); \
42 #define TE(e) ({ if (!(e)) { \
43 ERR_print_errors_fp(stderr); \
44 fprintf(stderr, "Error at %s:%d %s\n", __FILE__, __LINE__, #e); \
49 #define cRED "\033[1;31m"
50 #define cDRED "\033[0;31m"
51 #define cGREEN "\033[1;32m"
52 #define cDGREEN "\033[0;32m"
53 #define cBLUE "\033[1;34m"
54 #define cDBLUE "\033[0;34m"
55 #define cNORM "\033[m"
56 #define TEST_ASSERT(e) {if ((test = (e))) \
57 printf(cRED " Test FAILED\n" cNORM); \
59 printf(cGREEN " Test passed\n" cNORM);}
66 /* How much K to transfer between client and server. */
67 #define KTRANSFER (1 * 1024)
70 * Simple TLS Server code is based on
71 * https://wiki.openssl.org/index.php/Simple_TLS_Server
73 static int s_server(EVP_PKEY *pkey, X509 *cert, int client)
76 T(ctx = SSL_CTX_new(TLS_server_method()));
77 T(SSL_CTX_use_certificate(ctx, cert));
78 T(SSL_CTX_use_PrivateKey(ctx, pkey));
79 T(SSL_CTX_check_private_key(ctx));
82 T(ssl = SSL_new(ctx));
83 T(SSL_set_fd(ssl, client));
84 T(SSL_accept(ssl) == 1);
86 /* Receive data from client */
89 for (i = 0; i < KTRANSFER; i++) {
92 T(SSL_read(ssl, buf, sizeof(buf)) == sizeof(buf));
93 for (k = 0; k < sizeof(buf); k++)
95 err(1, "corruption from client");
97 /* Send data to client. */
98 memset(buf, 's', sizeof(buf));
99 for (i = 0; i < KTRANSFER; i++) {
100 T(SSL_write(ssl, buf, sizeof(buf)) == sizeof(buf));
111 * Simple TLC Client code is based on man BIO_f_ssl and
112 * https://wiki.openssl.org/index.php/SSL/TLS_Client
114 static int s_client(int server)
117 T(ctx = SSL_CTX_new(TLS_client_method()));
120 T(sbio = BIO_new_ssl_connect(ctx));
122 T(BIO_get_ssl(sbio, &ssl));
123 T(SSL_set_mode(ssl, SSL_MODE_AUTO_RETRY));
125 /* Does not work with reneg. */
126 BIO_set_ssl_renegotiate_bytes(sbio, 100 * 1024);
128 T(SSL_set_fd(ssl, server));
129 T(BIO_do_handshake(sbio) == 1);
131 printf("Protocol: %s\n", SSL_get_version(ssl));
132 printf("Cipher: %s\n", SSL_get_cipher_name(ssl));
134 SSL_SESSION *sess = SSL_get0_session(ssl);
135 SSL_SESSION_print_fp(stdout, sess);
139 T(cert = SSL_get_peer_certificate(ssl));
141 int verify = SSL_get_verify_result(ssl);
142 printf("Verify: %s\n", X509_verify_cert_error_string(verify));
143 if (verify != X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT)
144 err(1, "invalid SSL_get_verify_result");
146 /* Send data to server. */
149 memset(buf, 'c', sizeof(buf));
150 for (i = 0; i < KTRANSFER; i++) {
151 T(BIO_write(sbio, buf, sizeof(buf)) == sizeof(buf));
153 (void)BIO_shutdown_wr(sbio);
155 /* Receive data from server. */
156 for (i = 0; i < KTRANSFER; i++) {
158 int n = BIO_read(sbio, buf, sizeof(buf));
160 if (n != sizeof(buf)) {
161 printf("i:%d BIO_read:%d SSL_get_error:%d\n", i, n,
162 SSL_get_error(ssl, n));
163 ERR_print_errors_fp(stderr);
167 for (k = 0; k < sizeof(buf); k++)
169 err(1, "corruption from server");
172 i = BIO_get_num_renegotiates(sbio);
174 printf("Renegs: %d\n", i);
181 /* Generate simple cert+key pair. Based on req.c */
182 static struct certkey certgen(const char *algname, const char *paramset)
186 T(tkey = EVP_PKEY_new());
187 T(EVP_PKEY_set_type_str(tkey, algname, strlen(algname)));
189 T(ctx = EVP_PKEY_CTX_new(tkey, NULL));
190 T(EVP_PKEY_keygen_init(ctx));
192 T(EVP_PKEY_CTX_ctrl_str(ctx, "paramset", paramset));
193 EVP_PKEY *pkey = NULL;
194 T((EVP_PKEY_keygen(ctx, &pkey)) == 1);
195 EVP_PKEY_CTX_free(ctx);
199 X509_REQ *req = NULL;
200 T(req = X509_REQ_new());
201 T(X509_REQ_set_version(req, 0L));
203 T(name = X509_NAME_new());
204 T(X509_NAME_add_entry_by_txt(name, "O", MBSTRING_ASC, "Test CA", -1, -1, 0));
205 T(X509_NAME_add_entry_by_txt(name, "CN", MBSTRING_ASC, "Test Key", -1, -1, 0));
206 T(X509_REQ_set_subject_name(req, name));
207 T(X509_REQ_set_pubkey(req, pkey));
208 X509_NAME_free(name);
212 T(x509ss = X509_new());
213 T(X509_set_version(x509ss, 2));
214 BIGNUM *brnd = BN_new();
215 T(BN_rand(brnd, 20 * 8 - 1, -1, 0));
216 T(BN_to_ASN1_INTEGER(brnd, X509_get_serialNumber(x509ss)));
217 T(X509_set_issuer_name(x509ss, X509_REQ_get_subject_name(req)));
218 T(X509_gmtime_adj(X509_getm_notBefore(x509ss), 0));
219 T(X509_time_adj_ex(X509_getm_notAfter(x509ss), 1, 0, NULL));
220 T(X509_set_subject_name(x509ss, X509_REQ_get_subject_name(req)));
221 T(X509_set_pubkey(x509ss, X509_REQ_get0_pubkey(req)));
226 X509V3_set_ctx_nodb(&v3ctx);
227 X509V3_set_ctx(&v3ctx, x509ss, x509ss, NULL, NULL, 0);
229 T(ext = X509V3_EXT_conf_nid(NULL, &v3ctx, NID_basic_constraints, "critical,CA:TRUE"));
230 T(X509_add_ext(x509ss, ext, 0));
231 X509_EXTENSION_free(ext);
232 T(ext = X509V3_EXT_conf_nid(NULL, &v3ctx, NID_subject_key_identifier, "hash"));
233 T(X509_add_ext(x509ss, ext, 1));
234 X509_EXTENSION_free(ext);
235 T(ext = X509V3_EXT_conf_nid(NULL, &v3ctx, NID_authority_key_identifier, "keyid:always,issuer"));
236 T(X509_add_ext(x509ss, ext, 2));
237 X509_EXTENSION_free(ext);
240 T(mctx = EVP_MD_CTX_new());
241 T(EVP_DigestSignInit(mctx, NULL, NULL, NULL, pkey));
242 T(X509_sign_ctx(x509ss, mctx));
243 EVP_MD_CTX_free(mctx);
245 /* Print cert in text format. */
246 X509_print_fp(stdout, x509ss);
249 /* Print cert in PEM format. */
250 BIO *out = BIO_new_fp(stdout, BIO_NOCLOSE | BIO_FP_TEXT);
251 PEM_write_bio_X509(out, x509ss);
254 return (struct certkey){ .pkey = pkey, .cert = x509ss };
257 int test(const char *algname, const char *paramset)
261 printf(cBLUE "Test %s", algname);
263 printf(cBLUE ":%s", paramset);
267 ck = certgen(algname, paramset);
270 if (socketpair(AF_LOCAL, SOCK_STREAM, 0, sockfd) == -1)
271 err(1, "socketpair");
275 /* Run server in separate process. */
276 pid_t server_pid = fork();
278 err(1, "fork server");
279 if (server_pid == 0) {
280 ret = s_server(ck.pkey, ck.cert, sockfd[1]);
282 EVP_PKEY_free(ck.pkey);
286 /* Run client in separate process. */
287 pid_t client_pid = fork();
289 err(1, "fork client");
290 if (client_pid == 0) {
291 ret = s_client(sockfd[0]);
293 EVP_PKEY_free(ck.pkey);
297 /* Wait for first child to exit. */
299 pid_t exited_pid = wait(&status);
300 ret = (WIFEXITED(status) && WEXITSTATUS(status)) ||
301 (WIFSIGNALED(status) && WTERMSIG(status));
303 warnx(cRED "%s child %s with %d %s" cNORM,
304 exited_pid == server_pid? "server" : "client",
305 WIFSIGNALED(status)? "killed" : "exited",
306 WIFSIGNALED(status)? WTERMSIG(status) : WEXITSTATUS(status),
307 WIFSIGNALED(status)? strsignal(WTERMSIG(status)) : "");
309 /* If first child exited with error, kill other. */
310 warnx("terminating %s by force",
311 exited_pid == server_pid? "client" : "server");
312 kill(exited_pid == server_pid? client_pid : server_pid, SIGTERM);
315 exited_pid = wait(&status);
316 /* Report error unless we killed it. */
317 if (!ret && (!WIFEXITED(status) || WEXITSTATUS(status)))
318 warnx(cRED "%s child %s with %d %s" cNORM,
319 exited_pid == server_pid? "server" : "client",
320 WIFSIGNALED(status)? "killed" : "exited",
321 WIFSIGNALED(status)? WTERMSIG(status) : WEXITSTATUS(status),
322 WIFSIGNALED(status)? strsignal(WTERMSIG(status)) : "");
323 ret |= (WIFEXITED(status) && WEXITSTATUS(status)) ||
324 (WIFSIGNALED(status) && WTERMSIG(status));
326 /* Every responsible process should free this. */
328 EVP_PKEY_free(ck.pkey);
329 #ifdef __SANITIZE_ADDRESS__
330 /* Abort on the first (hopefully) ASan error. */
337 int main(int argc, char **argv)
341 setenv("OPENSSL_ENGINES", ENGINE_DIR, 0);
342 OPENSSL_add_all_algorithms_conf();
343 ERR_load_crypto_strings();
345 T(eng = ENGINE_by_id("gost"));
347 T(ENGINE_set_default(eng, ENGINE_METHOD_ALL));
349 /* ret |= test("rsa", NULL); */
350 ret |= test("gost2012_256", "A");
351 ret |= test("gost2012_256", "B");
352 ret |= test("gost2012_256", "C");
353 ret |= test("gost2012_256", "TCA");
354 ret |= test("gost2012_512", "A");
355 ret |= test("gost2012_512", "B");
356 ret |= test("gost2012_512", "C");
362 printf(cDRED "= Some tests FAILED!\n" cNORM);
364 printf(cDGREEN "= All tests passed!\n" cNORM);