| 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 |
#include <stdint.h> |
| 14 |
#include <stdlib.h> |
| 15 |
#include <string.h> |
| 16 |
|
| 17 |
#include "../test_helper/test_helper.h" |
| 18 |
|
| 19 |
#include "cipher.h" |
| 20 |
#include "compat.h" |
| 21 |
#include "ssherr.h" |
| 22 |
#include "sshbuf.h" |
| 23 |
#include "kex.h" |
| 24 |
#include "myproposal.h" |
| 25 |
#include "packet.h" |
| 26 |
#include "xmalloc.h" |
| 27 |
|
| 28 |
void kex_proposal_tests(void); |
| 29 |
void kex_proposal_populate_tests(void); |
| 30 |
|
| 31 |
#define CURVE25519 "[email protected]" |
| 32 |
#define DHGEX1 "diffie-hellman-group-exchange-sha1" |
| 33 |
#define DHGEX256 "diffie-hellman-group-exchange-sha256" |
| 34 |
#define KEXALGOS CURVE25519","DHGEX256","DHGEX1 |
| 35 |
void |
| 36 |
kex_proposal_tests(void) |
| 37 |
{ |
| 38 |
size_t i; |
| 39 |
struct ssh ssh; |
| 40 |
char *result, *out, *in; |
| 41 |
struct { |
| 42 |
char *in; /* TODO: make this const */ |
| 43 |
char *out; |
| 44 |
int compat; |
| 45 |
} tests[] = { |
| 46 |
{ KEXALGOS, KEXALGOS, 0}, |
| 47 |
{ KEXALGOS, DHGEX256","DHGEX1, SSH_BUG_CURVE25519PAD }, |
| 48 |
{ KEXALGOS, CURVE25519, SSH_OLD_DHGEX }, |
| 49 |
{ "a,"KEXALGOS, "a", SSH_BUG_CURVE25519PAD|SSH_OLD_DHGEX }, |
| 50 |
/* TODO: enable once compat_kex_proposal doesn't fatal() */ |
| 51 |
/* { KEXALGOS, "", SSH_BUG_CURVE25519PAD|SSH_OLD_DHGEX }, */ |
| 52 |
}; |
| 53 |
|
| 54 |
TEST_START("compat_kex_proposal"); |
| 55 |
for (i = 0; i < sizeof(tests) / sizeof(*tests); i++) { |
| 56 |
ssh.compat = tests[i].compat; |
| 57 |
/* match entire string */ |
| 58 |
result = compat_kex_proposal(&ssh, tests[i].in); |
| 59 |
ASSERT_STRING_EQ(result, tests[i].out); |
| 60 |
free(result); |
| 61 |
/* match at end */ |
| 62 |
in = kex_names_cat("a", tests[i].in); |
| 63 |
out = kex_names_cat("a", tests[i].out); |
| 64 |
result = compat_kex_proposal(&ssh, in); |
| 65 |
ASSERT_STRING_EQ(result, out); |
| 66 |
free(result); free(in); free(out); |
| 67 |
/* match at start */ |
| 68 |
in = kex_names_cat(tests[i].in, "a"); |
| 69 |
out = kex_names_cat(tests[i].out, "a"); |
| 70 |
result = compat_kex_proposal(&ssh, in); |
| 71 |
ASSERT_STRING_EQ(result, out); |
| 72 |
free(result); free(in); free(out); |
| 73 |
/* match in middle */ |
| 74 |
xasprintf(&in, "a,%s,b", tests[i].in); |
| 75 |
if (*(tests[i].out) == '\0') |
| 76 |
out = xstrdup("a,b"); |
| 77 |
else |
| 78 |
xasprintf(&out, "a,%s,b", tests[i].out); |
| 79 |
result = compat_kex_proposal(&ssh, in); |
| 80 |
ASSERT_STRING_EQ(result, out); |
| 81 |
free(result); free(in); free(out); |
| 82 |
} |
| 83 |
TEST_DONE(); |
| 84 |
} |
| 85 |
|
| 86 |
void |
| 87 |
kex_proposal_populate_tests(void) |
| 88 |
{ |
| 89 |
char *prop[PROPOSAL_MAX], *kexalgs, *ciphers, *macs, *hkalgs; |
| 90 |
const char *comp = compression_alg_list(0); |
| 91 |
int i; |
| 92 |
struct ssh ssh; |
| 93 |
struct kex kex; |
| 94 |
|
| 95 |
kexalgs = kex_alg_list(','); |
| 96 |
ciphers = cipher_alg_list(',', 0); |
| 97 |
macs = mac_alg_list(','); |
| 98 |
hkalgs = kex_alg_list(','); |
| 99 |
|
| 100 |
ssh.kex = &kex; |
| 101 |
TEST_START("compat_kex_proposal_populate"); |
| 102 |
for (i = 0; i <= 1; i++) { |
| 103 |
kex.server = i; |
| 104 |
for (ssh.compat = 0; ssh.compat < 0x40000000; ) { |
| 105 |
kex_proposal_populate_entries(&ssh, prop, NULL, NULL, |
| 106 |
NULL, NULL, NULL); |
| 107 |
kex_proposal_free_entries(prop); |
| 108 |
kex_proposal_populate_entries(&ssh, prop, kexalgs, |
| 109 |
ciphers, macs, hkalgs, comp); |
| 110 |
kex_proposal_free_entries(prop); |
| 111 |
if (ssh.compat == 0) |
| 112 |
ssh.compat = 1; |
| 113 |
else |
| 114 |
ssh.compat <<= 1; |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
free(kexalgs); |
| 119 |
free(ciphers); |
| 120 |
free(macs); |
| 121 |
free(hkalgs); |
| 122 |
} |