summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--regress/unittests/kex/Makefile13
-rw-r--r--regress/unittests/kex/test_kex.c185
-rw-r--r--regress/unittests/kex/tests.c14
3 files changed, 212 insertions, 0 deletions
diff --git a/regress/unittests/kex/Makefile b/regress/unittests/kex/Makefile
new file mode 100644
index 000000000..6502c36db
--- /dev/null
+++ b/regress/unittests/kex/Makefile
@@ -0,0 +1,13 @@
1# $OpenBSD: Makefile,v 1.1 2015/01/15 23:41:29 markus Exp $
2
3TEST_ENV= "MALLOC_OPTIONS=AFGJPRX"
4
5PROG=test_kex
6SRCS=tests.c test_kex.c
7LDADD=-lz
8REGRESS_TARGETS=run-regress-${PROG}
9
10run-regress-${PROG}: ${PROG}
11 env ${TEST_ENV} ./${PROG}
12
13.include <bsd.regress.mk>
diff --git a/regress/unittests/kex/test_kex.c b/regress/unittests/kex/test_kex.c
new file mode 100644
index 000000000..e90647763
--- /dev/null
+++ b/regress/unittests/kex/test_kex.c
@@ -0,0 +1,185 @@
1/* $OpenBSD: test_kex.c,v 1.1 2015/01/15 23:41:29 markus Exp $ */
2/*
3 * Regress test KEX
4 *
5 * Placed in the public domain
6 */
7
8#include <sys/types.h>
9#include <sys/param.h>
10#include <stdio.h>
11#include <stdint.h>
12#include <stdlib.h>
13#include <string.h>
14
15#include "test_helper.h"
16
17#include "ssherr.h"
18#include "ssh_api.h"
19#include "sshbuf.h"
20#include "packet.h"
21#include "myproposal.h"
22
23void kex_tests(void);
24static int do_debug = 0;
25
26static int
27do_send_and_receive(struct ssh *from, struct ssh *to)
28{
29 u_char type;
30 size_t len;
31 const u_char *buf;
32 int r;
33
34 for (;;) {
35 if ((r = ssh_packet_next(from, &type)) != 0) {
36 fprintf(stderr, "ssh_packet_next: %s\n", ssh_err(r));
37 return r;
38 }
39 if (type != 0)
40 return 0;
41 buf = ssh_output_ptr(from, &len);
42 if (do_debug)
43 printf("%zu", len);
44 if (len == 0)
45 return 0;
46 if ((r = ssh_output_consume(from, len)) != 0 ||
47 (r = ssh_input_append(to, buf, len)) != 0)
48 return r;
49 }
50}
51
52static void
53run_kex(struct ssh *client, struct ssh *server)
54{
55 int r = 0;
56
57 while (!server->kex->done || !client->kex->done) {
58 if (do_debug)
59 printf(" S:");
60 if ((r = do_send_and_receive(server, client)))
61 break;
62 if (do_debug)
63 printf(" C:");
64 if ((r = do_send_and_receive(client, server)))
65 break;
66 }
67 if (do_debug)
68 printf("done: %s\n", ssh_err(r));
69 ASSERT_INT_EQ(r, 0);
70 ASSERT_INT_EQ(server->kex->done, 1);
71 ASSERT_INT_EQ(client->kex->done, 1);
72}
73
74static void
75do_kex_with_key(char *kex, int keytype, int bits)
76{
77 struct ssh *client = NULL, *server = NULL, *server2 = NULL;
78 struct sshkey *private, *public;
79 struct sshbuf *state;
80 struct kex_params kex_params;
81 char *myproposal[PROPOSAL_MAX] = { KEX_CLIENT };
82
83 TEST_START("sshkey_generate");
84 ASSERT_INT_EQ(sshkey_generate(keytype, bits, &private), 0);
85 TEST_DONE();
86
87 TEST_START("sshkey_from_private");
88 ASSERT_INT_EQ(sshkey_from_private(private, &public), 0);
89 TEST_DONE();
90
91 TEST_START("ssh_init");
92 memcpy(kex_params.proposal, myproposal, sizeof(myproposal));
93 if (kex != NULL)
94 kex_params.proposal[PROPOSAL_KEX_ALGS] = kex;
95 ASSERT_INT_EQ(ssh_init(&client, 0, &kex_params), 0);
96 ASSERT_INT_EQ(ssh_init(&server, 1, &kex_params), 0);
97 ASSERT_PTR_NE(client, NULL);
98 ASSERT_PTR_NE(server, NULL);
99 TEST_DONE();
100
101 TEST_START("ssh_add_hostkey");
102 ASSERT_INT_EQ(ssh_add_hostkey(server, private), 0);
103 ASSERT_INT_EQ(ssh_add_hostkey(client, public), 0);
104 TEST_DONE();
105
106 TEST_START("kex");
107 run_kex(client, server);
108 TEST_DONE();
109
110 TEST_START("rekeying client");
111 ASSERT_INT_EQ(kex_send_kexinit(client), 0);
112 run_kex(client, server);
113 TEST_DONE();
114
115 TEST_START("rekeying server");
116 ASSERT_INT_EQ(kex_send_kexinit(server), 0);
117 run_kex(client, server);
118 TEST_DONE();
119
120 TEST_START("ssh_packet_get_state");
121 state = sshbuf_new();
122 ASSERT_PTR_NE(state, NULL);
123 ASSERT_INT_EQ(ssh_packet_get_state(server, state), 0);
124 ASSERT_INT_GE(sshbuf_len(state), 1);
125 TEST_DONE();
126
127 TEST_START("ssh_packet_set_state");
128 server2 = NULL;
129 ASSERT_INT_EQ(ssh_init(&server2, 1, NULL), 0);
130 ASSERT_PTR_NE(server2, NULL);
131 ASSERT_INT_EQ(ssh_add_hostkey(server2, private), 0);
132 kex_free(server2->kex); /* XXX or should ssh_packet_set_state()? */
133 ASSERT_INT_EQ(ssh_packet_set_state(server2, state), 0);
134 ASSERT_INT_EQ(sshbuf_len(state), 0);
135 sshbuf_free(state);
136 ASSERT_PTR_NE(server2->kex, NULL);
137 /* XXX we need to set the callbacks */
138 server2->kex->kex[KEX_DH_GRP1_SHA1] = kexdh_server;
139 server2->kex->kex[KEX_DH_GRP14_SHA1] = kexdh_server;
140 server2->kex->kex[KEX_DH_GEX_SHA1] = kexgex_server;
141 server2->kex->kex[KEX_DH_GEX_SHA256] = kexgex_server;
142 server2->kex->kex[KEX_ECDH_SHA2] = kexecdh_server;
143 server2->kex->kex[KEX_C25519_SHA256] = kexc25519_server;
144 server2->kex->load_host_public_key = server->kex->load_host_public_key;
145 server2->kex->load_host_private_key = server->kex->load_host_private_key;
146 server2->kex->sign = server->kex->sign;
147 TEST_DONE();
148
149 TEST_START("rekeying server2");
150 ASSERT_INT_EQ(kex_send_kexinit(server2), 0);
151 run_kex(client, server2);
152 ASSERT_INT_EQ(kex_send_kexinit(client), 0);
153 run_kex(client, server2);
154 TEST_DONE();
155
156 TEST_START("cleanup");
157 sshkey_free(private);
158 sshkey_free(public);
159 ssh_free(client);
160 ssh_free(server);
161 ssh_free(server2);
162 TEST_DONE();
163}
164
165static void
166do_kex(char *kex)
167{
168 do_kex_with_key(kex, KEY_RSA, 2048);
169 do_kex_with_key(kex, KEY_DSA, 1024);
170 do_kex_with_key(kex, KEY_ECDSA, 256);
171 do_kex_with_key(kex, KEY_ED25519, 256);
172}
173
174void
175kex_tests(void)
176{
177 do_kex("curve25519-sha256@libssh.org");
178 do_kex("ecdh-sha2-nistp256");
179 do_kex("ecdh-sha2-nistp384");
180 do_kex("ecdh-sha2-nistp521");
181 do_kex("diffie-hellman-group-exchange-sha256");
182 do_kex("diffie-hellman-group-exchange-sha1");
183 do_kex("diffie-hellman-group14-sha1");
184 do_kex("diffie-hellman-group1-sha1");
185}
diff --git a/regress/unittests/kex/tests.c b/regress/unittests/kex/tests.c
new file mode 100644
index 000000000..1e519fe4b
--- /dev/null
+++ b/regress/unittests/kex/tests.c
@@ -0,0 +1,14 @@
1/* $OpenBSD: tests.c,v 1.1 2015/01/15 23:41:29 markus Exp $ */
2/*
3 * Placed in the public domain
4 */
5
6#include "test_helper.h"
7
8void kex_tests(void);
9
10void
11tests(void)
12{
13 kex_tests();
14}