| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
#include <stdint.h> |
| 17 |
#include <string.h> |
| 18 |
|
| 19 |
#include "s2n_test.h" |
| 20 |
#include "stuffer/s2n_stuffer.h" |
| 21 |
#include "testlib/s2n_testlib.h" |
| 22 |
#include "tests/s2n_test.h" |
| 23 |
#include "tls/extensions/s2n_client_alpn.h" |
| 24 |
#include "tls/extensions/s2n_npn.h" |
| 25 |
#include "tls/s2n_tls.h" |
| 26 |
#include "utils/s2n_safety.h" |
| 27 |
|
| 28 |
#define HTTP11 0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31 |
| 29 |
#define SPDY1 0x73, 0x70, 0x64, 0x79, 0x2f, 0x31 |
| 30 |
#define SPDY2 0x73, 0x70, 0x64, 0x79, 0x2f, 0x32 |
| 31 |
#define SPDY3 0x73, 0x70, 0x64, 0x79, 0x2f, 0x33 |
| 32 |
|
| 33 |
S2N_RESULT s2n_calculate_padding(uint8_t protocol_len, uint8_t *padding_len); |
| 34 |
|
| 35 |
int main(int argc, char **argv) |
| 36 |
{ |
| 37 |
BEGIN_TEST(); |
| 38 |
|
| 39 |
const char *protocols[] = { "http/1.1", "spdy/1", "spdy/2" }; |
| 40 |
const uint8_t protocols_count = s2n_array_len(protocols); |
| 41 |
|
| 42 |
|
| 43 |
{ |
| 44 |
|
| 45 |
EXPECT_FALSE(s2n_client_npn_extension.should_send(NULL)); |
| 46 |
|
| 47 |
|
| 48 |
DEFER_CLEANUP(struct s2n_connection *client_conn = s2n_connection_new(S2N_CLIENT), s2n_connection_ptr_free); |
| 49 |
EXPECT_NOT_NULL(client_conn); |
| 50 |
EXPECT_FALSE(s2n_client_npn_extension.should_send(client_conn)); |
| 51 |
|
| 52 |
|
| 53 |
DEFER_CLEANUP(struct s2n_config *config = s2n_config_new(), s2n_config_ptr_free); |
| 54 |
EXPECT_NOT_NULL(config); |
| 55 |
EXPECT_SUCCESS(s2n_connection_set_config(client_conn, config)); |
| 56 |
EXPECT_FALSE(s2n_client_npn_extension.should_send(client_conn)); |
| 57 |
|
| 58 |
|
| 59 |
EXPECT_SUCCESS(s2n_config_set_protocol_preferences(config, protocols, protocols_count)); |
| 60 |
EXPECT_FALSE(s2n_client_npn_extension.should_send(client_conn)); |
| 61 |
EXPECT_TRUE(s2n_client_alpn_extension.should_send(client_conn)); |
| 62 |
|
| 63 |
|
| 64 |
client_conn->config->npn_supported = true; |
| 65 |
EXPECT_TRUE(s2n_client_npn_extension.should_send(client_conn)); |
| 66 |
EXPECT_TRUE(s2n_client_alpn_extension.should_send(client_conn)); |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
client_conn->handshake.renegotiation = true; |
| 76 |
EXPECT_FALSE(s2n_client_npn_extension.should_send(client_conn)); |
| 77 |
EXPECT_TRUE(s2n_client_alpn_extension.should_send(client_conn)); |
| 78 |
}; |
| 79 |
|
| 80 |
|
| 81 |
{ |
| 82 |
DEFER_CLEANUP(struct s2n_connection *server_conn = s2n_connection_new(S2N_SERVER), s2n_connection_ptr_free); |
| 83 |
EXPECT_NOT_NULL(server_conn); |
| 84 |
DEFER_CLEANUP(struct s2n_config *config = s2n_config_new(), s2n_config_ptr_free); |
| 85 |
EXPECT_NOT_NULL(config); |
| 86 |
EXPECT_SUCCESS(s2n_config_set_protocol_preferences(config, protocols, protocols_count)); |
| 87 |
EXPECT_SUCCESS(s2n_connection_set_config(server_conn, config)); |
| 88 |
|
| 89 |
DEFER_CLEANUP(struct s2n_stuffer extension = { 0 }, s2n_stuffer_free); |
| 90 |
EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&extension, 0)); |
| 91 |
|
| 92 |
|
| 93 |
EXPECT_SUCCESS(s2n_client_npn_extension.recv(server_conn, &extension)); |
| 94 |
EXPECT_FALSE(server_conn->npn_negotiated); |
| 95 |
|
| 96 |
|
| 97 |
server_conn->config->npn_supported = true; |
| 98 |
EXPECT_SUCCESS(s2n_client_npn_extension.recv(server_conn, &extension)); |
| 99 |
EXPECT_TRUE(server_conn->npn_negotiated); |
| 100 |
|
| 101 |
|
| 102 |
uint8_t first_protocol_len = strlen(protocols[0]); |
| 103 |
EXPECT_MEMCPY_SUCCESS(server_conn->application_protocol, protocols[0], first_protocol_len + 1); |
| 104 |
server_conn->npn_negotiated = false; |
| 105 |
EXPECT_SUCCESS(s2n_client_npn_extension.recv(server_conn, &extension)); |
| 106 |
EXPECT_FALSE(server_conn->npn_negotiated); |
| 107 |
}; |
| 108 |
|
| 109 |
|
| 110 |
{ |
| 111 |
DEFER_CLEANUP(struct s2n_connection *server_conn = s2n_connection_new(S2N_SERVER), s2n_connection_ptr_free); |
| 112 |
EXPECT_NOT_NULL(server_conn); |
| 113 |
DEFER_CLEANUP(struct s2n_config *config = s2n_config_new(), s2n_config_ptr_free); |
| 114 |
EXPECT_NOT_NULL(config); |
| 115 |
EXPECT_SUCCESS(s2n_config_set_protocol_preferences(config, protocols, protocols_count)); |
| 116 |
EXPECT_SUCCESS(s2n_connection_set_config(server_conn, config)); |
| 117 |
|
| 118 |
|
| 119 |
EXPECT_FALSE(s2n_server_npn_extension.should_send(server_conn)); |
| 120 |
|
| 121 |
|
| 122 |
server_conn->npn_negotiated = true; |
| 123 |
EXPECT_TRUE(s2n_server_npn_extension.should_send(server_conn)); |
| 124 |
}; |
| 125 |
|
| 126 |
|
| 127 |
{ |
| 128 |
DEFER_CLEANUP(struct s2n_connection *server_conn = s2n_connection_new(S2N_SERVER), s2n_connection_ptr_free); |
| 129 |
EXPECT_NOT_NULL(server_conn); |
| 130 |
DEFER_CLEANUP(struct s2n_config *config = s2n_config_new(), s2n_config_ptr_free); |
| 131 |
EXPECT_NOT_NULL(config); |
| 132 |
EXPECT_SUCCESS(s2n_config_set_protocol_preferences(config, protocols, protocols_count)); |
| 133 |
EXPECT_SUCCESS(s2n_connection_set_config(server_conn, config)); |
| 134 |
DEFER_CLEANUP(struct s2n_stuffer out = { 0 }, s2n_stuffer_free); |
| 135 |
EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&out, 0)); |
| 136 |
|
| 137 |
EXPECT_SUCCESS(s2n_server_npn_extension.send(server_conn, &out)); |
| 138 |
|
| 139 |
uint8_t protocol_len = 0; |
| 140 |
uint8_t protocol[UINT8_MAX] = { 0 }; |
| 141 |
for (size_t i = 0; i < protocols_count; i++) { |
| 142 |
EXPECT_SUCCESS(s2n_stuffer_read_uint8(&out, &protocol_len)); |
| 143 |
EXPECT_EQUAL(protocol_len, strlen(protocols[i])); |
| 144 |
|
| 145 |
EXPECT_SUCCESS(s2n_stuffer_read_bytes(&out, protocol, protocol_len)); |
| 146 |
EXPECT_BYTEARRAY_EQUAL(protocol, protocols[i], protocol_len); |
| 147 |
} |
| 148 |
|
| 149 |
EXPECT_EQUAL(s2n_stuffer_data_available(&out), 0); |
| 150 |
}; |
| 151 |
|
| 152 |
|
| 153 |
{ |
| 154 |
|
| 155 |
|
| 156 |
{ |
| 157 |
DEFER_CLEANUP(struct s2n_connection *client_conn = s2n_connection_new(S2N_CLIENT), s2n_connection_ptr_free); |
| 158 |
EXPECT_NOT_NULL(client_conn); |
| 159 |
DEFER_CLEANUP(struct s2n_stuffer extension = { 0 }, s2n_stuffer_free); |
| 160 |
EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&extension, 0)); |
| 161 |
|
| 162 |
EXPECT_SUCCESS(s2n_server_npn_extension.recv(client_conn, &extension)); |
| 163 |
EXPECT_NULL(s2n_get_application_protocol(client_conn)); |
| 164 |
}; |
| 165 |
|
| 166 |
|
| 167 |
{ |
| 168 |
DEFER_CLEANUP(struct s2n_connection *client_conn = s2n_connection_new(S2N_CLIENT), s2n_connection_ptr_free); |
| 169 |
EXPECT_NOT_NULL(client_conn); |
| 170 |
DEFER_CLEANUP(struct s2n_config *config = s2n_config_new(), s2n_config_ptr_free); |
| 171 |
EXPECT_NOT_NULL(config); |
| 172 |
EXPECT_SUCCESS(s2n_config_set_protocol_preferences(config, protocols, protocols_count)); |
| 173 |
EXPECT_SUCCESS(s2n_connection_set_config(client_conn, config)); |
| 174 |
|
| 175 |
DEFER_CLEANUP(struct s2n_stuffer extension = { 0 }, s2n_stuffer_free); |
| 176 |
EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&extension, 0)); |
| 177 |
|
| 178 |
EXPECT_SUCCESS(s2n_server_npn_extension.send(client_conn, &extension)); |
| 179 |
EXPECT_SUCCESS(s2n_server_npn_extension.recv(client_conn, &extension)); |
| 180 |
|
| 181 |
|
| 182 |
EXPECT_NOT_NULL(s2n_get_application_protocol(client_conn)); |
| 183 |
EXPECT_BYTEARRAY_EQUAL(s2n_get_application_protocol(client_conn), protocols[0], strlen(protocols[0])); |
| 184 |
}; |
| 185 |
|
| 186 |
|
| 187 |
{ |
| 188 |
DEFER_CLEANUP(struct s2n_connection *client_conn = s2n_connection_new(S2N_CLIENT), s2n_connection_ptr_free); |
| 189 |
EXPECT_NOT_NULL(client_conn); |
| 190 |
DEFER_CLEANUP(struct s2n_config *config = s2n_config_new(), s2n_config_ptr_free); |
| 191 |
EXPECT_NOT_NULL(config); |
| 192 |
EXPECT_SUCCESS(s2n_config_set_protocol_preferences(config, protocols, protocols_count)); |
| 193 |
EXPECT_SUCCESS(s2n_connection_set_config(client_conn, config)); |
| 194 |
|
| 195 |
DEFER_CLEANUP(struct s2n_stuffer extension = { 0 }, s2n_stuffer_free); |
| 196 |
EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&extension, 0)); |
| 197 |
|
| 198 |
uint8_t protocol[] = { SPDY3 }; |
| 199 |
EXPECT_SUCCESS(s2n_stuffer_write_uint8(&extension, sizeof(protocol))); |
| 200 |
EXPECT_SUCCESS(s2n_stuffer_write_bytes(&extension, protocol, sizeof(protocol))); |
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
EXPECT_SUCCESS(s2n_server_npn_extension.recv(client_conn, &extension)); |
| 210 |
EXPECT_NOT_NULL(s2n_get_application_protocol(client_conn)); |
| 211 |
EXPECT_BYTEARRAY_EQUAL(s2n_get_application_protocol(client_conn), protocols[0], strlen(protocols[0])); |
| 212 |
}; |
| 213 |
|
| 214 |
|
| 215 |
{ |
| 216 |
DEFER_CLEANUP(struct s2n_connection *client_conn = s2n_connection_new(S2N_CLIENT), s2n_connection_ptr_free); |
| 217 |
EXPECT_NOT_NULL(client_conn); |
| 218 |
DEFER_CLEANUP(struct s2n_config *config = s2n_config_new(), s2n_config_ptr_free); |
| 219 |
EXPECT_NOT_NULL(config); |
| 220 |
EXPECT_SUCCESS(s2n_config_set_protocol_preferences(config, protocols, protocols_count)); |
| 221 |
EXPECT_SUCCESS(s2n_connection_set_config(client_conn, config)); |
| 222 |
|
| 223 |
DEFER_CLEANUP(struct s2n_stuffer extension = { 0 }, s2n_stuffer_free); |
| 224 |
EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&extension, 0)); |
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
EXPECT_SUCCESS(s2n_server_npn_extension.recv(client_conn, &extension)); |
| 234 |
EXPECT_NOT_NULL(s2n_get_application_protocol(client_conn)); |
| 235 |
EXPECT_BYTEARRAY_EQUAL(s2n_get_application_protocol(client_conn), protocols[0], strlen(protocols[0])); |
| 236 |
}; |
| 237 |
|
| 238 |
|
| 239 |
{ |
| 240 |
DEFER_CLEANUP(struct s2n_connection *client_conn = s2n_connection_new(S2N_CLIENT), s2n_connection_ptr_free); |
| 241 |
EXPECT_NOT_NULL(client_conn); |
| 242 |
DEFER_CLEANUP(struct s2n_config *config = s2n_config_new(), s2n_config_ptr_free); |
| 243 |
EXPECT_NOT_NULL(config); |
| 244 |
EXPECT_SUCCESS(s2n_config_set_protocol_preferences(config, protocols, protocols_count)); |
| 245 |
EXPECT_SUCCESS(s2n_connection_set_config(client_conn, config)); |
| 246 |
|
| 247 |
DEFER_CLEANUP(struct s2n_stuffer extension = { 0 }, s2n_stuffer_free); |
| 248 |
EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&extension, 0)); |
| 249 |
|
| 250 |
uint8_t wire_bytes[] = { |
| 251 |
|
| 252 |
0x06, |
| 253 |
SPDY1, |
| 254 |
|
| 255 |
0x08, |
| 256 |
HTTP11, |
| 257 |
|
| 258 |
0x06, |
| 259 |
SPDY2, |
| 260 |
}; |
| 261 |
|
| 262 |
EXPECT_SUCCESS(s2n_stuffer_write_bytes(&extension, wire_bytes, sizeof(wire_bytes))); |
| 263 |
EXPECT_SUCCESS(s2n_server_npn_extension.recv(client_conn, &extension)); |
| 264 |
|
| 265 |
EXPECT_NOT_NULL(s2n_get_application_protocol(client_conn)); |
| 266 |
|
| 267 |
|
| 268 |
EXPECT_BYTEARRAY_EQUAL(s2n_get_application_protocol(client_conn), protocols[1], strlen(protocols[1])); |
| 269 |
}; |
| 270 |
}; |
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
{ |
| 281 |
DEFER_CLEANUP(struct s2n_connection *server_conn = s2n_connection_new(S2N_SERVER), s2n_connection_ptr_free); |
| 282 |
EXPECT_NOT_NULL(server_conn); |
| 283 |
|
| 284 |
|
| 285 |
EXPECT_TRUE(UINT8_MAX < sizeof(server_conn->application_protocol)); |
| 286 |
}; |
| 287 |
|
| 288 |
END_TEST(); |
| 289 |
} |