1 /* $OpenBSD: test_kex.c,v 1.2 2015/07/10 06:23:25 markus 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 <sys/param.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 "ssherr.h" 22 #include "ssh_api.h" 23 #include "sshbuf.h" 24 #include "packet.h" 25 #include "myproposal.h" 26 27 struct ssh *active_state = NULL; /* XXX - needed for linking */ 28 29 void kex_tests(void); 30 static int do_debug = 0; 31 32 static int 33 do_send_and_receive(struct ssh *from, struct ssh *to) 34 { 35 u_char type; 36 size_t len; 37 const u_char *buf; 38 int r; 39 40 for (;;) { 41 if ((r = ssh_packet_next(from, &type)) != 0) { 42 fprintf(stderr, "ssh_packet_next: %s\n", ssh_err(r)); 43 return r; 44 } 45 if (type != 0) 46 return 0; 47 buf = ssh_output_ptr(from, &len); 48 if (do_debug) 49 printf("%zu", len); 50 if (len == 0) 51 return 0; 52 if ((r = ssh_output_consume(from, len)) != 0 || 53 (r = ssh_input_append(to, buf, len)) != 0) 54 return r; 55 } 56 } 57 58 static void 59 run_kex(struct ssh *client, struct ssh *server) 60 { 61 int r = 0; 62 63 while (!server->kex->done || !client->kex->done) { 64 if (do_debug) 65 printf(" S:"); 66 if ((r = do_send_and_receive(server, client))) 67 break; 68 if (do_debug) 69 printf(" C:"); 70 if ((r = do_send_and_receive(client, server))) 71 break; 72 } 73 if (do_debug) 74 printf("done: %s\n", ssh_err(r)); 75 ASSERT_INT_EQ(r, 0); 76 ASSERT_INT_EQ(server->kex->done, 1); 77 ASSERT_INT_EQ(client->kex->done, 1); 78 } 79 80 static void 81 do_kex_with_key(char *kex, int keytype, int bits) 82 { 83 struct ssh *client = NULL, *server = NULL, *server2 = NULL; 84 struct sshkey *private, *public; 85 struct sshbuf *state; 86 struct kex_params kex_params; 87 char *myproposal[PROPOSAL_MAX] = { KEX_CLIENT }; 88 char *keyname = NULL; 89 90 TEST_START("sshkey_generate"); 91 ASSERT_INT_EQ(sshkey_generate(keytype, bits, &private), 0); 92 TEST_DONE(); 93 94 TEST_START("sshkey_from_private"); 95 ASSERT_INT_EQ(sshkey_from_private(private, &public), 0); 96 TEST_DONE(); 97 98 TEST_START("ssh_init"); 99 memcpy(kex_params.proposal, myproposal, sizeof(myproposal)); 100 if (kex != NULL) 101 kex_params.proposal[PROPOSAL_KEX_ALGS] = kex; 102 keyname = strdup(sshkey_ssh_name(private)); 103 ASSERT_PTR_NE(keyname, NULL); 104 kex_params.proposal[PROPOSAL_SERVER_HOST_KEY_ALGS] = keyname; 105 ASSERT_INT_EQ(ssh_init(&client, 0, &kex_params), 0); 106 ASSERT_INT_EQ(ssh_init(&server, 1, &kex_params), 0); 107 ASSERT_PTR_NE(client, NULL); 108 ASSERT_PTR_NE(server, NULL); 109 TEST_DONE(); 110 111 TEST_START("ssh_add_hostkey"); 112 ASSERT_INT_EQ(ssh_add_hostkey(server, private), 0); 113 ASSERT_INT_EQ(ssh_add_hostkey(client, public), 0); 114 TEST_DONE(); 115 116 TEST_START("kex"); 117 run_kex(client, server); 118 TEST_DONE(); 119 120 TEST_START("rekeying client"); 121 ASSERT_INT_EQ(kex_send_kexinit(client), 0); 122 run_kex(client, server); 123 TEST_DONE(); 124 125 TEST_START("rekeying server"); 126 ASSERT_INT_EQ(kex_send_kexinit(server), 0); 127 run_kex(client, server); 128 TEST_DONE(); 129 130 TEST_START("ssh_packet_get_state"); 131 state = sshbuf_new(); 132 ASSERT_PTR_NE(state, NULL); 133 ASSERT_INT_EQ(ssh_packet_get_state(server, state), 0); 134 ASSERT_INT_GE(sshbuf_len(state), 1); 135 TEST_DONE(); 136 137 TEST_START("ssh_packet_set_state"); 138 server2 = NULL; 139 ASSERT_INT_EQ(ssh_init(&server2, 1, NULL), 0); 140 ASSERT_PTR_NE(server2, NULL); 141 ASSERT_INT_EQ(ssh_add_hostkey(server2, private), 0); 142 kex_free(server2->kex); /* XXX or should ssh_packet_set_state()? */ 143 ASSERT_INT_EQ(ssh_packet_set_state(server2, state), 0); 144 ASSERT_INT_EQ(sshbuf_len(state), 0); 145 sshbuf_free(state); 146 ASSERT_PTR_NE(server2->kex, NULL); 147 /* XXX we need to set the callbacks */ 148 server2->kex->kex[KEX_DH_GRP1_SHA1] = kexdh_server; 149 server2->kex->kex[KEX_DH_GRP14_SHA1] = kexdh_server; 150 server2->kex->kex[KEX_DH_GEX_SHA1] = kexgex_server; 151 server2->kex->kex[KEX_DH_GEX_SHA256] = kexgex_server; 152 #ifdef OPENSSL_HAS_ECC 153 server2->kex->kex[KEX_ECDH_SHA2] = kexecdh_server; 154 #endif 155 server2->kex->kex[KEX_C25519_SHA256] = kexc25519_server; 156 server2->kex->load_host_public_key = server->kex->load_host_public_key; 157 server2->kex->load_host_private_key = server->kex->load_host_private_key; 158 server2->kex->sign = server->kex->sign; 159 TEST_DONE(); 160 161 TEST_START("rekeying server2"); 162 ASSERT_INT_EQ(kex_send_kexinit(server2), 0); 163 run_kex(client, server2); 164 ASSERT_INT_EQ(kex_send_kexinit(client), 0); 165 run_kex(client, server2); 166 TEST_DONE(); 167 168 TEST_START("cleanup"); 169 sshkey_free(private); 170 sshkey_free(public); 171 ssh_free(client); 172 ssh_free(server); 173 ssh_free(server2); 174 free(keyname); 175 TEST_DONE(); 176 } 177 178 static void 179 do_kex(char *kex) 180 { 181 do_kex_with_key(kex, KEY_RSA, 2048); 182 do_kex_with_key(kex, KEY_DSA, 1024); 183 #ifdef OPENSSL_HAS_ECC 184 do_kex_with_key(kex, KEY_ECDSA, 256); 185 #endif 186 do_kex_with_key(kex, KEY_ED25519, 256); 187 } 188 189 void 190 kex_tests(void) 191 { 192 do_kex("curve25519-sha256@libssh.org"); 193 #ifdef OPENSSL_HAS_ECC 194 do_kex("ecdh-sha2-nistp256"); 195 do_kex("ecdh-sha2-nistp384"); 196 do_kex("ecdh-sha2-nistp521"); 197 #endif 198 do_kex("diffie-hellman-group-exchange-sha256"); 199 do_kex("diffie-hellman-group-exchange-sha1"); 200 do_kex("diffie-hellman-group14-sha1"); 201 do_kex("diffie-hellman-group1-sha1"); 202 } 203