Skip to content

Commit 66b53ef

Browse files
committed
Merge pull request #47 from talnikov/master
Don't choose DH ciphers if DH is not configured
2 parents a744dff + 1541cc6 commit 66b53ef

File tree

2 files changed

+77
-60
lines changed

2 files changed

+77
-60
lines changed

tests/unit/s2n_self_talk_test.c

Lines changed: 68 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,9 @@ static char certificate[] =
4242
"WbyxPJNtSlA9GfKBz1INR5cFsOL27VrBoMYHMaolveeslc1AW2HfBtXWXeWSEF7F\n"
4343
"QNgye8ZDPNzeSWSI0VyK2762wsTgTuUhHAaJ45660eX57+e8IvaM7xOEfBPDKYtU\n"
4444
"0a28ZuhvSr2akJtGCwcs2J6rs6I+rV84UktDxFC9LUezBo8D9FkMPLoPKKNH1dXR\n"
45-
"6LO8GOkqWUrhPIEmfy9KYes3q2ZX6svk4rwBtommHRv30kPxnnU1YXt52Ri+XczO\n" "wEs=\n" "-----END CERTIFICATE-----\n";
45+
"6LO8GOkqWUrhPIEmfy9KYes3q2ZX6svk4rwBtommHRv30kPxnnU1YXt52Ri+XczO\n"
46+
"wEs=\n"
47+
"-----END CERTIFICATE-----\n";
4648

4749
static char private_key[] =
4850
"-----BEGIN RSA PRIVATE KEY-----\n"
@@ -69,17 +71,21 @@ static char private_key[] =
6971
"pRsovQKpiHQNgHizkwM861GqqrfisZZSyKfFlcynkACoVmyu7fv9VoD2VCMiqdUq\n"
7072
"IvjNmfE5RnXVQwja+668AS+MHi+GF77DTFBxoC5VHDAnXfLyIL9WWh9GEBoNLnKT\n"
7173
"hVm8RQKBgQCB9Skzdftc+14a4Vj3NCgdHZHz9mcdPhzJXUiQyZ3tYhaytX9E8mWq\n"
72-
"pm/OFqahbxw6EQd86mgANBMKayD6B1Id1INqtXN1XYI50bSs1D2nOGsBM7MK9aWD\n" "JXlJ2hwsIc4q9En/LR3GtBaL84xTHGfznNylNhXi7GbO1wNMJuAukA==\n" "-----END RSA PRIVATE KEY-----\n";
74+
"pm/OFqahbxw6EQd86mgANBMKayD6B1Id1INqtXN1XYI50bSs1D2nOGsBM7MK9aWD\n"
75+
"JXlJ2hwsIc4q9En/LR3GtBaL84xTHGfznNylNhXi7GbO1wNMJuAukA==\n"
76+
"-----END RSA PRIVATE KEY-----\n";
7377

7478
static char dhparams[] =
7579
"-----BEGIN DH PARAMETERS-----\n"
7680
"MIIBCAKCAQEAy1+hVWCfNQoPB+NA733IVOONl8fCumiz9zdRRu1hzVa2yvGseUSq\n"
7781
"Bbn6k0FQ7yMED6w5XWQKDC0z2m0FI/BPE3AjUfuPzEYGqTDf9zQZ2Lz4oAN90Sud\n"
7882
"luOoEhYR99cEbCn0T4eBvEf9IUtczXUZ/wj7gzGbGG07dLfT+CmCRJxCjhrosenJ\n"
7983
"gzucyS7jt1bobgU66JKkgMNm7hJY4/nhR5LWTCzZyzYQh2HM2Vk4K5ZqILpj/n0S\n"
80-
"5JYTQ2PVhxP+Uu8+hICs/8VvM72DznjPZzufADipjC7CsQ4S6x/ecZluFtbb+ZTv\n" "HI5CnYmkAwJ6+FSWGaZQDi8bgerFk9RWwwIBAg==\n" "-----END DH PARAMETERS-----\n";
84+
"5JYTQ2PVhxP+Uu8+hICs/8VvM72DznjPZzufADipjC7CsQ4S6x/ecZluFtbb+ZTv\n"
85+
"HI5CnYmkAwJ6+FSWGaZQDi8bgerFk9RWwwIBAg==\n"
86+
"-----END DH PARAMETERS-----\n";
8187

82-
int mock_client(int writefd, int readfd)
88+
void mock_client(int writefd, int readfd)
8389
{
8490
char buffer[0xffff];
8591
struct s2n_connection *conn;
@@ -99,10 +105,10 @@ int mock_client(int writefd, int readfd)
99105
for (int j = 0; j < i; j++) {
100106
buffer[j] = 33;
101107
}
102-
108+
103109
s2n_send(conn, buffer, i, &more);
104110
}
105-
111+
106112
s2n_shutdown(conn, &more);
107113
s2n_connection_free(conn);
108114

@@ -127,71 +133,75 @@ int main(int argc, char **argv)
127133

128134
/* Create a pipe */
129135
EXPECT_SUCCESS(s2n_init());
130-
EXPECT_SUCCESS(pipe(server_to_client));
131-
EXPECT_SUCCESS(pipe(client_to_server));
132-
133-
/* Create a child process */
134-
pid = fork();
135-
if (pid == 0) {
136-
/* This is the child process, close the read end of the pipe */
137-
EXPECT_SUCCESS(close(client_to_server[0]));
138-
EXPECT_SUCCESS(close(server_to_client[1]));
139-
140-
/* Write the fragmented hello message */
141-
mock_client(client_to_server[1], server_to_client[0]);
142-
}
143136

144-
/* This is the parent */
145-
EXPECT_SUCCESS(close(client_to_server[1]));
146-
EXPECT_SUCCESS(close(server_to_client[0]));
137+
for (int is_dh_key_exchange = 0; is_dh_key_exchange <= 1; is_dh_key_exchange++) {
138+
EXPECT_SUCCESS(pipe(server_to_client));
139+
EXPECT_SUCCESS(pipe(client_to_server));
147140

148-
EXPECT_NOT_NULL(conn = s2n_connection_new(S2N_SERVER));
149-
EXPECT_NOT_NULL(config = s2n_config_new());
141+
/* Create a child process */
142+
pid = fork();
143+
if (pid == 0) {
144+
/* This is the child process, close the read end of the pipe */
145+
EXPECT_SUCCESS(close(client_to_server[0]));
146+
EXPECT_SUCCESS(close(server_to_client[1]));
150147

151-
EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key(config, certificate, private_key));
152-
EXPECT_SUCCESS(s2n_config_add_dhparams(config, dhparams));
153-
154-
EXPECT_SUCCESS(s2n_connection_set_config(conn, config));
148+
/* Write the fragmented hello message */
149+
mock_client(client_to_server[1], server_to_client[0]);
150+
}
155151

156-
/* Set up the connection to read from the fd */
157-
EXPECT_SUCCESS(s2n_connection_set_read_fd(conn, client_to_server[0]));
158-
EXPECT_SUCCESS(s2n_connection_set_write_fd(conn, server_to_client[1]));
152+
/* This is the parent */
153+
EXPECT_SUCCESS(close(client_to_server[1]));
154+
EXPECT_SUCCESS(close(server_to_client[0]));
159155

160-
/* Negotiate the handshake. */
161-
EXPECT_SUCCESS(s2n_negotiate(conn, &status));
156+
EXPECT_NOT_NULL(conn = s2n_connection_new(S2N_SERVER));
157+
EXPECT_NOT_NULL(config = s2n_config_new());
162158

163-
char buffer[0xffff];
164-
for (int i = 1; i < 0xffff; i += 100) {
165-
char * ptr = buffer;
166-
int bytes_read = 0;
167-
int size = i;
159+
EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key(config, certificate, private_key));
160+
if (is_dh_key_exchange) {
161+
EXPECT_SUCCESS(s2n_config_add_dhparams(config, dhparams));
162+
}
168163

169-
do {
170-
EXPECT_SUCCESS(bytes_read = s2n_recv(conn, ptr, size, &status));
164+
EXPECT_SUCCESS(s2n_connection_set_config(conn, config));
171165

172-
size -= bytes_read;
173-
ptr += bytes_read;
174-
} while(size);
166+
/* Set up the connection to read from the fd */
167+
EXPECT_SUCCESS(s2n_connection_set_read_fd(conn, client_to_server[0]));
168+
EXPECT_SUCCESS(s2n_connection_set_write_fd(conn, server_to_client[1]));
175169

176-
for (int j = 0; j < i; j++) {
177-
EXPECT_EQUAL(buffer[j], 33);
170+
/* Negotiate the handshake. */
171+
EXPECT_SUCCESS(s2n_negotiate(conn, &status));
172+
173+
char buffer[0xffff];
174+
for (int i = 1; i < 0xffff; i += 100) {
175+
char * ptr = buffer;
176+
int bytes_read = 0;
177+
int size = i;
178+
179+
do {
180+
EXPECT_SUCCESS(bytes_read = s2n_recv(conn, ptr, size, &status));
181+
182+
size -= bytes_read;
183+
ptr += bytes_read;
184+
} while(size);
185+
186+
for (int j = 0; j < i; j++) {
187+
EXPECT_EQUAL(buffer[j], 33);
188+
}
178189
}
179-
}
180-
181-
/* Verify that read() returns EOF */
182-
EXPECT_SUCCESS(s2n_recv(conn, buffer, 1, &status));
183-
184-
EXPECT_SUCCESS(s2n_shutdown(conn, &status));
185-
186-
EXPECT_SUCCESS(s2n_connection_free(conn));
187190

188-
EXPECT_SUCCESS(s2n_config_free(config));
191+
/* Verify that read() returns EOF */
192+
EXPECT_SUCCESS(s2n_recv(conn, buffer, 1, &status));
189193

190-
/* Clean up */
191-
EXPECT_EQUAL(waitpid(-1, &status, 0), pid);
192-
EXPECT_EQUAL(status, 0);
194+
EXPECT_SUCCESS(s2n_shutdown(conn, &status));
193195

194-
END_TEST();
196+
EXPECT_SUCCESS(s2n_connection_free(conn));
195197

198+
EXPECT_SUCCESS(s2n_config_free(config));
199+
200+
/* Clean up */
201+
EXPECT_EQUAL(waitpid(-1, &status, 0), pid);
202+
EXPECT_EQUAL(status, 0);
203+
}
204+
205+
END_TEST();
196206
return 0;
197207
}

tls/s2n_cipher_suites.c

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,12 +95,19 @@ static int s2n_set_cipher_as_server(struct s2n_connection *conn, uint8_t *wire,
9595

9696
if (!memcmp(ours, theirs, S2N_TLS_CIPHER_SUITE_LEN)) {
9797
/* We have a match */
98-
conn->pending.cipher_suite = s2n_cipher_suite_match(ours);
98+
struct s2n_cipher_suite *match = s2n_cipher_suite_match(ours);
9999

100100
/* This should never happen */
101-
if (conn->pending.cipher_suite == NULL) {
101+
if (match == NULL) {
102102
S2N_ERROR(S2N_ERR_CIPHER_NOT_SUPPORTED);
103103
}
104+
105+
/* Don't choose DH key exchange if it's not configured. */
106+
if (conn->config->dhparams == NULL && match->key_exchange_alg == S2N_DHE) {
107+
continue;
108+
}
109+
110+
conn->pending.cipher_suite = match;
104111
return 0;
105112
}
106113
}

0 commit comments

Comments
 (0)