| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
#include <fcntl.h> |
| 17 |
#include <string.h> |
| 18 |
#include <unistd.h> |
| 19 |
|
| 20 |
#include "s2n_test.h" |
| 21 |
#include "testlib/s2n_testlib.h" |
| 22 |
#include "tls/policy/s2n_policy_feature.h" |
| 23 |
|
| 24 |
static S2N_RESULT s2n_verify_format_v1_output(const char *output) |
| 25 |
{ |
| 26 |
RESULT_ENSURE_REF(output); |
| 27 |
|
| 28 |
|
| 29 |
RESULT_ENSURE(strstr(output, "min version: ") != NULL, S2N_ERR_TEST_ASSERTION); |
| 30 |
RESULT_ENSURE(strstr(output, "rules:\n") != NULL, S2N_ERR_TEST_ASSERTION); |
| 31 |
RESULT_ENSURE(strstr(output, "cipher suites:\n") != NULL, S2N_ERR_TEST_ASSERTION); |
| 32 |
RESULT_ENSURE(strstr(output, "signature schemes:\n") != NULL, S2N_ERR_TEST_ASSERTION); |
| 33 |
RESULT_ENSURE(strstr(output, "curves:\n") != NULL, S2N_ERR_TEST_ASSERTION); |
| 34 |
|
| 35 |
return S2N_RESULT_OK; |
| 36 |
} |
| 37 |
|
| 38 |
int main(int argc, char **argv) |
| 39 |
{ |
| 40 |
BEGIN_TEST(); |
| 41 |
|
| 42 |
|
| 43 |
{ |
| 44 |
|
| 45 |
{ |
| 46 |
uint32_t length = 0; |
| 47 |
EXPECT_FAILURE_WITH_ERRNO( |
| 48 |
s2n_security_policy_write_length(NULL, S2N_POLICY_FORMAT_DEBUG_V1, &length), |
| 49 |
S2N_ERR_NULL); |
| 50 |
}; |
| 51 |
|
| 52 |
|
| 53 |
{ |
| 54 |
const struct s2n_security_policy *policy = NULL; |
| 55 |
EXPECT_SUCCESS(s2n_find_security_policy_from_version("default", &policy)); |
| 56 |
EXPECT_NOT_NULL(policy); |
| 57 |
|
| 58 |
EXPECT_FAILURE_WITH_ERRNO( |
| 59 |
s2n_security_policy_write_length(policy, S2N_POLICY_FORMAT_DEBUG_V1, NULL), |
| 60 |
S2N_ERR_NULL); |
| 61 |
}; |
| 62 |
|
| 63 |
|
| 64 |
{ |
| 65 |
const struct s2n_security_policy *policy = NULL; |
| 66 |
EXPECT_SUCCESS(s2n_find_security_policy_from_version("default", &policy)); |
| 67 |
EXPECT_NOT_NULL(policy); |
| 68 |
|
| 69 |
uint32_t length = 0; |
| 70 |
EXPECT_FAILURE_WITH_ERRNO( |
| 71 |
s2n_security_policy_write_length(policy, 999, &length), |
| 72 |
S2N_ERR_INVALID_ARGUMENT); |
| 73 |
}; |
| 74 |
|
| 75 |
|
| 76 |
{ |
| 77 |
const struct s2n_security_policy *policy = NULL; |
| 78 |
EXPECT_SUCCESS(s2n_find_security_policy_from_version("default", &policy)); |
| 79 |
EXPECT_NOT_NULL(policy); |
| 80 |
|
| 81 |
uint32_t required_length = 0; |
| 82 |
EXPECT_SUCCESS(s2n_security_policy_write_length(policy, S2N_POLICY_FORMAT_DEBUG_V1, &required_length)); |
| 83 |
EXPECT_TRUE(required_length > 0); |
| 84 |
|
| 85 |
DEFER_CLEANUP(struct s2n_blob exact_buffer = { 0 }, s2n_free); |
| 86 |
EXPECT_SUCCESS(s2n_alloc(&exact_buffer, required_length + 1)); |
| 87 |
uint32_t output_size = 0; |
| 88 |
EXPECT_SUCCESS(s2n_security_policy_write_bytes(policy, S2N_POLICY_FORMAT_DEBUG_V1, exact_buffer.data, required_length, &output_size)); |
| 89 |
exact_buffer.data[required_length] = '\0'; |
| 90 |
|
| 91 |
EXPECT_OK(s2n_verify_format_v1_output((const char *) exact_buffer.data)); |
| 92 |
|
| 93 |
|
| 94 |
EXPECT_TRUE(required_length > 1); |
| 95 |
DEFER_CLEANUP(struct s2n_blob small_buffer = { 0 }, s2n_free); |
| 96 |
EXPECT_SUCCESS(s2n_alloc(&small_buffer, required_length - 1)); |
| 97 |
output_size = 0; |
| 98 |
EXPECT_FAILURE_WITH_ERRNO( |
| 99 |
s2n_security_policy_write_bytes(policy, S2N_POLICY_FORMAT_DEBUG_V1, small_buffer.data, required_length - 1, &output_size), |
| 100 |
S2N_ERR_INSUFFICIENT_MEM_SIZE); |
| 101 |
}; |
| 102 |
}; |
| 103 |
|
| 104 |
|
| 105 |
{ |
| 106 |
|
| 107 |
{ |
| 108 |
uint8_t buffer[1024] = { 0 }; |
| 109 |
uint32_t output_size = 0; |
| 110 |
EXPECT_FAILURE_WITH_ERRNO( |
| 111 |
s2n_security_policy_write_bytes(NULL, S2N_POLICY_FORMAT_DEBUG_V1, buffer, sizeof(buffer), &output_size), |
| 112 |
S2N_ERR_NULL); |
| 113 |
}; |
| 114 |
|
| 115 |
|
| 116 |
{ |
| 117 |
const struct s2n_security_policy *policy = NULL; |
| 118 |
EXPECT_SUCCESS(s2n_find_security_policy_from_version("default", &policy)); |
| 119 |
EXPECT_NOT_NULL(policy); |
| 120 |
|
| 121 |
uint32_t output_size = 0; |
| 122 |
EXPECT_FAILURE_WITH_ERRNO( |
| 123 |
s2n_security_policy_write_bytes(policy, S2N_POLICY_FORMAT_DEBUG_V1, NULL, 1024, &output_size), |
| 124 |
S2N_ERR_NULL); |
| 125 |
}; |
| 126 |
|
| 127 |
|
| 128 |
{ |
| 129 |
const struct s2n_security_policy *policy = NULL; |
| 130 |
EXPECT_SUCCESS(s2n_find_security_policy_from_version("default", &policy)); |
| 131 |
EXPECT_NOT_NULL(policy); |
| 132 |
|
| 133 |
uint8_t buffer[1024] = { 0 }; |
| 134 |
EXPECT_FAILURE_WITH_ERRNO( |
| 135 |
s2n_security_policy_write_bytes(policy, S2N_POLICY_FORMAT_DEBUG_V1, buffer, sizeof(buffer), NULL), |
| 136 |
S2N_ERR_NULL); |
| 137 |
}; |
| 138 |
|
| 139 |
|
| 140 |
{ |
| 141 |
const struct s2n_security_policy *policy = NULL; |
| 142 |
EXPECT_SUCCESS(s2n_find_security_policy_from_version("default", &policy)); |
| 143 |
EXPECT_NOT_NULL(policy); |
| 144 |
|
| 145 |
uint8_t buffer[1024] = { 0 }; |
| 146 |
uint32_t output_size = 0; |
| 147 |
EXPECT_FAILURE_WITH_ERRNO( |
| 148 |
s2n_security_policy_write_bytes(policy, 999, buffer, sizeof(buffer), &output_size), |
| 149 |
S2N_ERR_INVALID_ARGUMENT); |
| 150 |
}; |
| 151 |
|
| 152 |
|
| 153 |
{ |
| 154 |
const struct s2n_security_policy *policy = NULL; |
| 155 |
EXPECT_SUCCESS(s2n_find_security_policy_from_version("default", &policy)); |
| 156 |
EXPECT_NOT_NULL(policy); |
| 157 |
|
| 158 |
uint8_t small_buffer[10] = { 0 }; |
| 159 |
uint32_t output_size = 0; |
| 160 |
EXPECT_FAILURE_WITH_ERRNO( |
| 161 |
s2n_security_policy_write_bytes(policy, S2N_POLICY_FORMAT_DEBUG_V1, small_buffer, sizeof(small_buffer), &output_size), |
| 162 |
S2N_ERR_INSUFFICIENT_MEM_SIZE); |
| 163 |
}; |
| 164 |
|
| 165 |
|
| 166 |
{ |
| 167 |
const struct s2n_security_policy *policy = NULL; |
| 168 |
EXPECT_SUCCESS(s2n_find_security_policy_from_version("default", &policy)); |
| 169 |
EXPECT_NOT_NULL(policy); |
| 170 |
|
| 171 |
uint32_t required_length = 0; |
| 172 |
EXPECT_SUCCESS(s2n_security_policy_write_length(policy, S2N_POLICY_FORMAT_DEBUG_V1, &required_length)); |
| 173 |
|
| 174 |
DEFER_CLEANUP(struct s2n_blob buffer = { 0 }, s2n_free); |
| 175 |
EXPECT_SUCCESS(s2n_alloc(&buffer, required_length + 1)); |
| 176 |
uint32_t output_size = 0; |
| 177 |
EXPECT_SUCCESS(s2n_security_policy_write_bytes(policy, S2N_POLICY_FORMAT_DEBUG_V1, buffer.data, required_length, &output_size)); |
| 178 |
EXPECT_EQUAL(output_size, required_length); |
| 179 |
buffer.data[required_length] = '\0'; |
| 180 |
|
| 181 |
EXPECT_OK(s2n_verify_format_v1_output((const char *) buffer.data)); |
| 182 |
}; |
| 183 |
}; |
| 184 |
|
| 185 |
|
| 186 |
{ |
| 187 |
|
| 188 |
{ |
| 189 |
uint32_t output_size = 0; |
| 190 |
EXPECT_FAILURE_WITH_ERRNO( |
| 191 |
s2n_security_policy_write_fd(NULL, S2N_POLICY_FORMAT_DEBUG_V1, STDOUT_FILENO, &output_size), |
| 192 |
S2N_ERR_NULL); |
| 193 |
}; |
| 194 |
|
| 195 |
|
| 196 |
{ |
| 197 |
const struct s2n_security_policy *policy = NULL; |
| 198 |
EXPECT_SUCCESS(s2n_find_security_policy_from_version("default", &policy)); |
| 199 |
EXPECT_NOT_NULL(policy); |
| 200 |
|
| 201 |
EXPECT_FAILURE_WITH_ERRNO( |
| 202 |
s2n_security_policy_write_fd(policy, S2N_POLICY_FORMAT_DEBUG_V1, STDOUT_FILENO, NULL), |
| 203 |
S2N_ERR_NULL); |
| 204 |
}; |
| 205 |
|
| 206 |
|
| 207 |
{ |
| 208 |
const struct s2n_security_policy *policy = NULL; |
| 209 |
EXPECT_SUCCESS(s2n_find_security_policy_from_version("default", &policy)); |
| 210 |
EXPECT_NOT_NULL(policy); |
| 211 |
|
| 212 |
uint32_t output_size = 0; |
| 213 |
EXPECT_FAILURE_WITH_ERRNO( |
| 214 |
s2n_security_policy_write_fd(policy, 999, STDOUT_FILENO, &output_size), |
| 215 |
S2N_ERR_INVALID_ARGUMENT); |
| 216 |
}; |
| 217 |
|
| 218 |
|
| 219 |
{ |
| 220 |
const struct s2n_security_policy *policy = NULL; |
| 221 |
EXPECT_SUCCESS(s2n_find_security_policy_from_version("default", &policy)); |
| 222 |
EXPECT_NOT_NULL(policy); |
| 223 |
|
| 224 |
uint32_t output_size = 0; |
| 225 |
EXPECT_FAILURE_WITH_ERRNO( |
| 226 |
s2n_security_policy_write_fd(policy, S2N_POLICY_FORMAT_DEBUG_V1, -1, &output_size), |
| 227 |
S2N_ERR_INVALID_ARGUMENT); |
| 228 |
}; |
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
#ifndef _WIN32 |
| 236 |
{ |
| 237 |
const struct s2n_security_policy *policy = NULL; |
| 238 |
EXPECT_SUCCESS(s2n_find_security_policy_from_version("default", &policy)); |
| 239 |
EXPECT_NOT_NULL(policy); |
| 240 |
|
| 241 |
uint32_t expected_length = 0; |
| 242 |
EXPECT_SUCCESS(s2n_security_policy_write_length(policy, S2N_POLICY_FORMAT_DEBUG_V1, &expected_length)); |
| 243 |
|
| 244 |
|
| 245 |
DEFER_CLEANUP(struct s2n_test_io_pair io_pair = { 0 }, s2n_io_pair_close); |
| 246 |
EXPECT_SUCCESS(s2n_io_pair_init(&io_pair)); |
| 247 |
|
| 248 |
uint32_t output_size = 0; |
| 249 |
EXPECT_SUCCESS(s2n_security_policy_write_fd(policy, S2N_POLICY_FORMAT_DEBUG_V1, io_pair.client, &output_size)); |
| 250 |
EXPECT_EQUAL(output_size, expected_length); |
| 251 |
|
| 252 |
DEFER_CLEANUP(struct s2n_blob file_buffer = { 0 }, s2n_free); |
| 253 |
EXPECT_SUCCESS(s2n_alloc(&file_buffer, expected_length + 1)); |
| 254 |
|
| 255 |
ssize_t bytes_read = s2n_test_recv(io_pair.server, file_buffer.data, expected_length); |
| 256 |
EXPECT_TRUE(bytes_read >= 0); |
| 257 |
EXPECT_EQUAL((size_t) bytes_read, expected_length); |
| 258 |
file_buffer.data[expected_length] = '\0'; |
| 259 |
|
| 260 |
EXPECT_OK(s2n_verify_format_v1_output((const char *) file_buffer.data)); |
| 261 |
}; |
| 262 |
#endif |
| 263 |
}; |
| 264 |
|
| 265 |
END_TEST(); |
| 266 |
} |