xref: /freebsd/crypto/openssh/regress/unittests/kex/test_proposal.c (revision 59c8e88e72633afbc47a4ace0d2170d00d51f7dc)
1 /* 	$OpenBSD: test_proposal.c,v 1.2 2023/03/06 12:15:47 dtucker Exp $ */
2 /*
3  * Regress test KEX
4  *
5  * Placed in the public domain
6  */
7 
8 #include "includes.h"
9 
10 #include <sys/types.h>
11 #include <signal.h>
12 #include <stdio.h>
13 #ifdef HAVE_STDINT_H
14 #include <stdint.h>
15 #endif
16 #include <stdlib.h>
17 #include <string.h>
18 
19 #include "../test_helper/test_helper.h"
20 
21 #include "cipher.h"
22 #include "compat.h"
23 #include "ssherr.h"
24 #include "sshbuf.h"
25 #include "kex.h"
26 #include "myproposal.h"
27 #include "packet.h"
28 #include "xmalloc.h"
29 
30 void kex_proposal_tests(void);
31 void kex_proposal_populate_tests(void);
32 
33 #define CURVE25519 "curve25519-sha256@libssh.org"
34 #define DHGEX1 "diffie-hellman-group-exchange-sha1"
35 #define DHGEX256 "diffie-hellman-group-exchange-sha256"
36 #define KEXALGOS CURVE25519","DHGEX256","DHGEX1
37 void
38 kex_proposal_tests(void)
39 {
40 	size_t i;
41 	struct ssh ssh;
42 	char *result, *out, *in;
43 	struct {
44 		char *in;	/* TODO: make this const */
45 		char *out;
46 		int compat;
47 	} tests[] = {
48 		{ KEXALGOS, KEXALGOS, 0},
49 		{ KEXALGOS, DHGEX256","DHGEX1, SSH_BUG_CURVE25519PAD },
50 		{ KEXALGOS, CURVE25519, SSH_OLD_DHGEX },
51 		{ "a,"KEXALGOS, "a", SSH_BUG_CURVE25519PAD|SSH_OLD_DHGEX },
52 		/* TODO: enable once compat_kex_proposal doesn't fatal() */
53 		/* { KEXALGOS, "", SSH_BUG_CURVE25519PAD|SSH_OLD_DHGEX }, */
54 	};
55 
56 	TEST_START("compat_kex_proposal");
57 	for (i = 0; i < sizeof(tests) / sizeof(*tests); i++) {
58 		ssh.compat = tests[i].compat;
59 		/* match entire string */
60 		result = compat_kex_proposal(&ssh, tests[i].in);
61 		ASSERT_STRING_EQ(result, tests[i].out);
62 		free(result);
63 		/* match at end */
64 		in = kex_names_cat("a", tests[i].in);
65 		out = kex_names_cat("a", tests[i].out);
66 		result = compat_kex_proposal(&ssh, in);
67 		ASSERT_STRING_EQ(result, out);
68 		free(result); free(in); free(out);
69 		/* match at start */
70 		in = kex_names_cat(tests[i].in, "a");
71 		out = kex_names_cat(tests[i].out, "a");
72 		result = compat_kex_proposal(&ssh, in);
73 		ASSERT_STRING_EQ(result, out);
74 		free(result); free(in); free(out);
75 		/* match in middle */
76 		xasprintf(&in, "a,%s,b", tests[i].in);
77 		if (*(tests[i].out) == '\0')
78 			out = xstrdup("a,b");
79 		else
80 			xasprintf(&out, "a,%s,b", tests[i].out);
81 		result = compat_kex_proposal(&ssh, in);
82 		ASSERT_STRING_EQ(result, out);
83 		free(result); free(in); free(out);
84 	}
85 	TEST_DONE();
86 }
87 
88 void
89 kex_proposal_populate_tests(void)
90 {
91 	char *prop[PROPOSAL_MAX], *kexalgs, *ciphers, *macs, *hkalgs;
92 	const char *comp = compression_alg_list(0);
93 	int i;
94 	struct ssh ssh;
95 	struct kex kex;
96 
97 	kexalgs = kex_alg_list(',');
98 	ciphers = cipher_alg_list(',', 0);
99 	macs = mac_alg_list(',');
100 	hkalgs = kex_alg_list(',');
101 
102 	ssh.kex = &kex;
103 	TEST_START("compat_kex_proposal_populate");
104 	for (i = 0; i <= 1; i++) {
105 		kex.server = i;
106 		for (ssh.compat = 0; ssh.compat < 0x40000000; ) {
107 			kex_proposal_populate_entries(&ssh, prop, NULL, NULL,
108 			    NULL, NULL, NULL);
109 			kex_proposal_free_entries(prop);
110 			kex_proposal_populate_entries(&ssh, prop, kexalgs,
111 			    ciphers, macs, hkalgs, comp);
112 			kex_proposal_free_entries(prop);
113 			if (ssh.compat == 0)
114 				ssh.compat = 1;
115 			else
116 				ssh.compat <<= 1;
117 		}
118 	}
119 
120 	free(kexalgs);
121 	free(ciphers);
122 	free(macs);
123 	free(hkalgs);
124 }
125