| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
#include "api/s2n.h" |
| 17 |
#include "crypto/s2n_crypto.h" |
| 18 |
#include "crypto/s2n_openssl_x509.h" |
| 19 |
#include "s2n_test.h" |
| 20 |
#include "testlib/s2n_testlib.h" |
| 21 |
#include "tls/s2n_tls.h" |
| 22 |
#include "utils/s2n_safety.h" |
| 23 |
|
| 24 |
#define CERTIFICATE_1 \ |
| 25 |
"MIIBljCCATygAwIBAgIUHxKbtYzLM4Bct5v5Sagb8aZU/BcwCgYIKoZIzj0EAwIw" \ |
| 26 |
"HjELMAkGA1UEBhMCVVMxDzANBgNVBAMMBmJyYW5jaDAgFw0yNDAxMjMwMDU3MTha" \ |
| 27 |
"GA8yMjAzMDYzMDAwNTcxOFowHDELMAkGA1UEBhMCVVMxDTALBgNVBAMMBGxlYWYw" \ |
| 28 |
"WTATBgcqhkjOPQIBBggqhkjOPQMBBwNCAASAmJKXt9P8hSz9ubntEokFn06+Rexr" \ |
| 29 |
"lEujwWUIq5Kl6QwvtfDCzrJN/sUmM5mssjq7pF6XVr6zFcQ6G4BfnwGio1gwVjAU" \ |
| 30 |
"BgNVHREEDTALgglsb2NhbGhvc3QwHQYDVR0OBBYEFNlOWJn7XfzC6xORzzjrdnqK" \ |
| 31 |
"UlJwMB8GA1UdIwQYMBaAFFkQhpDCJ3bAbXw1tXpmk5Fi7YIGMAoGCCqGSM49BAMC" \ |
| 32 |
"A0gAMEUCIB58OBwIruTJIy1f3tUgM/wXoO7fCoU25sMcioHBV9dYAiEA7Ufxa2JF" \ |
| 33 |
"I5LP6RGyllsjjnh0MJy1ZMXhw7X6GqFn4Rw=" |
| 34 |
|
| 35 |
#define CERTIFICATE_2 \ |
| 36 |
"MIIBoDCCAUegAwIBAgIUQud1+tNUPAEBnJLb2Lyl3/vuA4EwCgYIKoZIzj0EAwIw" \ |
| 37 |
"HDELMAkGA1UEBhMCVVMxDTALBgNVBAMMBHJvb3QwIBcNMjQwMTIzMDA1NzE4WhgP" \ |
| 38 |
"MjIwMzA2MzAwMDU3MThaMB4xCzAJBgNVBAYTAlVTMQ8wDQYDVQQDDAZicmFuY2gw" \ |
| 39 |
"WTATBgcqhkjOPQIBBggqhkjOPQMBBwNCAATfScB9w/LkHBAVXiyKN941555oyBpv" \ |
| 40 |
"IZeCNXX+gbSvnS0pNRr35BalgFmij86DaXLl68RrHQsnhZByJvnIplN+o2MwYTAP" \ |
| 41 |
"BgNVHRMBAf8EBTADAQH/MA4GA1UdDwEB/wQEAwICBDAdBgNVHQ4EFgQUWRCGkMIn" \ |
| 42 |
"dsBtfDW1emaTkWLtggYwHwYDVR0jBBgwFoAUfCtcQDvXYwkR37fHwe/mi7JyQIQw" \ |
| 43 |
"CgYIKoZIzj0EAwIDRwAwRAIgaK6aOuCfMwgAASavkZpoxWag49yco4d9AlxIU+rt" \ |
| 44 |
"U2UCIHRieWdQICIWSEHdRTXWPPEnOd7A3UmTgoqbMl+Imhy8" |
| 45 |
|
| 46 |
#define CERTIFICATE_3 \ |
| 47 |
"MIIBnzCCAUWgAwIBAgIUe0/XdFLyc4+Sj1NMkvbagE8DFaUwCgYIKoZIzj0EAwIw" \ |
| 48 |
"HDELMAkGA1UEBhMCVVMxDTALBgNVBAMMBHJvb3QwIBcNMjQwMTIzMDA1NzE4WhgP" \ |
| 49 |
"MjIwMzA2MzAwMDU3MThaMBwxCzAJBgNVBAYTAlVTMQ0wCwYDVQQDDARyb290MFkw" \ |
| 50 |
"EwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEPPtDvucc4UGOdIjED2P/vDxVYDhO5P8s" \ |
| 51 |
"7lyys3QZpKapMuc9wOV0cQ6tN9h4kVY+FJocYgqDAl2vv6Rg/wSbl6NjMGEwHQYD" \ |
| 52 |
"VR0OBBYEFHwrXEA712MJEd+3x8Hv5ouyckCEMB8GA1UdIwQYMBaAFHwrXEA712MJ" \ |
| 53 |
"Ed+3x8Hv5ouyckCEMA8GA1UdEwEB/wQFMAMBAf8wDgYDVR0PAQH/BAQDAgIEMAoG" \ |
| 54 |
"CCqGSM49BAMCA0gAMEUCIQCcbhKRAsqQCj2pCXh+9og3sLw9q8nU4xAB9xuV3vPA" \ |
| 55 |
"FAIgBxFWfRdu09dtE0IUkLTY0WPxiWaYYrKexlD4wUquqJE=" |
| 56 |
|
| 57 |
#define BEGIN_CERT "BEGIN CERTIFICATE" |
| 58 |
#define BEGIN_CERT_LINE "-----" BEGIN_CERT "-----" |
| 59 |
#define END_CERT "END CERTIFICATE" |
| 60 |
#define END_CERT_LINE "-----" END_CERT "-----" |
| 61 |
|
| 62 |
static S2N_RESULT s2n_test_validate_cert(struct s2n_cert *cert, struct s2n_blob *expected) |
| 63 |
{ |
| 64 |
RESULT_ENSURE_REF(cert); |
| 65 |
RESULT_ENSURE_REF(expected); |
| 66 |
RESULT_ENSURE_EQ(cert->raw.size, expected->size); |
| 67 |
RESULT_ENSURE_EQ(memcmp(cert->raw.data, expected->data, expected->size), 0); |
| 68 |
return S2N_RESULT_OK; |
| 69 |
} |
| 70 |
|
| 71 |
int main(int argc, char **argv) |
| 72 |
{ |
| 73 |
BEGIN_TEST(); |
| 74 |
|
| 75 |
const char *expected_cert_strs[] = { |
| 76 |
CERTIFICATE_1, |
| 77 |
CERTIFICATE_2, |
| 78 |
CERTIFICATE_3, |
| 79 |
}; |
| 80 |
struct s2n_blob expected_certs[s2n_array_len(expected_cert_strs)] = { 0 }; |
| 81 |
EXPECT_EQUAL(s2n_array_len(expected_certs), s2n_array_len(expected_cert_strs)); |
| 82 |
|
| 83 |
for (size_t i = 0; i < s2n_array_len(expected_certs); i++) { |
| 84 |
const char *base64_cert = expected_cert_strs[i]; |
| 85 |
struct s2n_blob *out = &expected_certs[i]; |
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
EXPECT_SUCCESS(s2n_alloc(out, strlen(base64_cert))); |
| 91 |
|
| 92 |
struct s2n_stuffer out_stuffer = { 0 }; |
| 93 |
EXPECT_SUCCESS(s2n_stuffer_init(&out_stuffer, out)); |
| 94 |
|
| 95 |
DEFER_CLEANUP(struct s2n_stuffer in_stuffer = { 0 }, s2n_stuffer_free); |
| 96 |
EXPECT_SUCCESS(s2n_stuffer_alloc(&in_stuffer, strlen(base64_cert))); |
| 97 |
EXPECT_SUCCESS(s2n_stuffer_write_str(&in_stuffer, base64_cert)); |
| 98 |
|
| 99 |
EXPECT_SUCCESS(s2n_stuffer_read_base64(&in_stuffer, &out_stuffer)); |
| 100 |
out->size = s2n_stuffer_data_available(&out_stuffer); |
| 101 |
} |
| 102 |
|
| 103 |
struct s2n_test_case { |
| 104 |
const char *name; |
| 105 |
const char *input; |
| 106 |
}; |
| 107 |
|
| 108 |
|
| 109 |
const struct s2n_test_case test_cases[] = { |
| 110 |
{ |
| 111 |
.name = "basic format", |
| 112 |
.input = |
| 113 |
BEGIN_CERT_LINE "\n" |
| 114 |
CERTIFICATE_1 "\n" |
| 115 |
END_CERT_LINE "\n" |
| 116 |
BEGIN_CERT_LINE "\n" |
| 117 |
CERTIFICATE_2 "\n" |
| 118 |
END_CERT_LINE "\n" |
| 119 |
BEGIN_CERT_LINE "\n" |
| 120 |
CERTIFICATE_3 "\n" |
| 121 |
END_CERT_LINE "\n", |
| 122 |
}, |
| 123 |
{ |
| 124 |
.name = "no newlines", |
| 125 |
.input = |
| 126 |
BEGIN_CERT_LINE CERTIFICATE_1 END_CERT_LINE |
| 127 |
BEGIN_CERT_LINE CERTIFICATE_2 END_CERT_LINE |
| 128 |
BEGIN_CERT_LINE CERTIFICATE_3 END_CERT_LINE, |
| 129 |
}, |
| 130 |
{ |
| 131 |
.name = "empty lines", |
| 132 |
.input = |
| 133 |
"\n" |
| 134 |
BEGIN_CERT_LINE "\n" |
| 135 |
CERTIFICATE_1 "\n" |
| 136 |
END_CERT_LINE "\n" |
| 137 |
"\n" |
| 138 |
BEGIN_CERT_LINE "\n" |
| 139 |
CERTIFICATE_2 "\n" |
| 140 |
END_CERT_LINE "\n" |
| 141 |
"\n" |
| 142 |
BEGIN_CERT_LINE "\n" |
| 143 |
CERTIFICATE_3 "\n" |
| 144 |
END_CERT_LINE "\n" |
| 145 |
"\n", |
| 146 |
}, |
| 147 |
{ |
| 148 |
.name = "double empty lines", |
| 149 |
.input = |
| 150 |
"\n" |
| 151 |
"\n" |
| 152 |
BEGIN_CERT_LINE "\n" |
| 153 |
CERTIFICATE_1 "\n" |
| 154 |
END_CERT_LINE "\n" |
| 155 |
"\n" |
| 156 |
"\n" |
| 157 |
BEGIN_CERT_LINE "\n" |
| 158 |
CERTIFICATE_2 "\n" |
| 159 |
END_CERT_LINE "\n" |
| 160 |
"\n" |
| 161 |
"\n" |
| 162 |
BEGIN_CERT_LINE "\n" |
| 163 |
CERTIFICATE_3 "\n" |
| 164 |
END_CERT_LINE "\n" |
| 165 |
"\n" |
| 166 |
"\n", |
| 167 |
}, |
| 168 |
{ |
| 169 |
.name = "variable number of newlines", |
| 170 |
.input = |
| 171 |
"\n" |
| 172 |
BEGIN_CERT_LINE "\n" |
| 173 |
CERTIFICATE_1 "\n" |
| 174 |
END_CERT_LINE "\n" |
| 175 |
"\n\n\n" |
| 176 |
BEGIN_CERT_LINE "\n" |
| 177 |
CERTIFICATE_2 "\n" |
| 178 |
END_CERT_LINE "\n" |
| 179 |
BEGIN_CERT_LINE "\n" |
| 180 |
CERTIFICATE_3 "\n" |
| 181 |
END_CERT_LINE "\n" |
| 182 |
"\n\n" |
| 183 |
}, |
| 184 |
{ |
| 185 |
.name = "whitespace", |
| 186 |
.input = |
| 187 |
" " BEGIN_CERT_LINE "\n" |
| 188 |
CERTIFICATE_1 "\n" |
| 189 |
END_CERT_LINE " \n" |
| 190 |
" " |
| 191 |
BEGIN_CERT_LINE "\n" |
| 192 |
CERTIFICATE_2 "\n" |
| 193 |
END_CERT_LINE " \n" |
| 194 |
BEGIN_CERT_LINE "\n" |
| 195 |
CERTIFICATE_3 "\n" |
| 196 |
END_CERT_LINE " \n", |
| 197 |
}, |
| 198 |
{ |
| 199 |
.name = "extra dashes", |
| 200 |
.input = |
| 201 |
"-" BEGIN_CERT_LINE "--\n" |
| 202 |
CERTIFICATE_1 "\n" |
| 203 |
"---" END_CERT_LINE "----\n" |
| 204 |
"-----" BEGIN_CERT_LINE "------\n" |
| 205 |
CERTIFICATE_2 "\n" |
| 206 |
END_CERT_LINE "---------------\n" |
| 207 |
"--" BEGIN_CERT_LINE "---\n" |
| 208 |
CERTIFICATE_3 "\n" |
| 209 |
"-" END_CERT_LINE "-\n", |
| 210 |
}, |
| 211 |
{ |
| 212 |
.name = "64 dashes", |
| 213 |
.input = |
| 214 |
"-----" "-----" "-----" "-----" |
| 215 |
"-----" "-----" "-----" "-----" |
| 216 |
"-----" "-----" "-----" "-----" |
| 217 |
"----" |
| 218 |
BEGIN_CERT |
| 219 |
"-----" "-----" "-----" "-----" |
| 220 |
"-----" "-----" "-----" "-----" |
| 221 |
"-----" "-----" "-----" "-----" |
| 222 |
"----" |
| 223 |
"\n" |
| 224 |
CERTIFICATE_1 "\n" |
| 225 |
END_CERT_LINE "\n" |
| 226 |
BEGIN_CERT_LINE "\n" |
| 227 |
CERTIFICATE_2 "\n" |
| 228 |
END_CERT_LINE "\n" |
| 229 |
BEGIN_CERT_LINE "\n" |
| 230 |
CERTIFICATE_3 "\n" |
| 231 |
END_CERT_LINE "\n", |
| 232 |
}, |
| 233 |
{ |
| 234 |
.name = "missing dashes", |
| 235 |
.input = |
| 236 |
"--" BEGIN_CERT "--\n" |
| 237 |
CERTIFICATE_1 "\n" |
| 238 |
"---" END_CERT "---\n" |
| 239 |
"----" BEGIN_CERT "----\n" |
| 240 |
CERTIFICATE_2 "\n" |
| 241 |
"--" END_CERT "---\n" |
| 242 |
"---" BEGIN_CERT "----\n" |
| 243 |
CERTIFICATE_3 "\n" |
| 244 |
"----" END_CERT "--\n", |
| 245 |
}, |
| 246 |
{ |
| 247 |
.name = "delimiters share dashes", |
| 248 |
.input = |
| 249 |
"--" BEGIN_CERT "--" CERTIFICATE_1 "--" END_CERT |
| 250 |
"--" BEGIN_CERT "--" CERTIFICATE_2 "--" END_CERT |
| 251 |
"--" BEGIN_CERT "--" CERTIFICATE_3 "--" END_CERT "--" , |
| 252 |
}, |
| 253 |
{ |
| 254 |
.name = "non-certificate data", |
| 255 |
.input = |
| 256 |
"this is not a certificate\n" |
| 257 |
BEGIN_CERT_LINE "\n" |
| 258 |
CERTIFICATE_1 "\n" |
| 259 |
END_CERT_LINE "\n" |
| 260 |
"\n" |
| 261 |
"this is not a certificate either\n" |
| 262 |
"\n" |
| 263 |
BEGIN_CERT_LINE "\n" |
| 264 |
CERTIFICATE_2 "\n" |
| 265 |
END_CERT_LINE "not a certificate\n" |
| 266 |
"not a certificate" BEGIN_CERT_LINE "\n" |
| 267 |
CERTIFICATE_3 "\n" |
| 268 |
END_CERT_LINE "\n", |
| 269 |
}, |
| 270 |
{ |
| 271 |
.name = "comments", |
| 272 |
.input = |
| 273 |
"# cert1" |
| 274 |
BEGIN_CERT_LINE "\n" |
| 275 |
CERTIFICATE_1 "\n" |
| 276 |
END_CERT_LINE "\n" |
| 277 |
"\n" |
| 278 |
"# cert2" |
| 279 |
BEGIN_CERT_LINE "\n" |
| 280 |
CERTIFICATE_2 "\n" |
| 281 |
END_CERT_LINE "\n" |
| 282 |
"\n" |
| 283 |
"# cert3" |
| 284 |
BEGIN_CERT_LINE "\n" |
| 285 |
CERTIFICATE_3 "\n" |
| 286 |
END_CERT_LINE "\n", |
| 287 |
}, |
| 288 |
{ |
| 289 |
.name = "comments containing dashes", |
| 290 |
.input = |
| 291 |
"# cert-1\n" |
| 292 |
BEGIN_CERT_LINE "\n" |
| 293 |
CERTIFICATE_1 "\n" |
| 294 |
END_CERT_LINE "\n" |
| 295 |
"\n" |
| 296 |
"# -c-e-r-t-2-\n" |
| 297 |
BEGIN_CERT_LINE "\n" |
| 298 |
CERTIFICATE_2 "\n" |
| 299 |
END_CERT_LINE "\n" |
| 300 |
"\n" |
| 301 |
"# -\n" |
| 302 |
BEGIN_CERT_LINE "\n" |
| 303 |
CERTIFICATE_3 "\n" |
| 304 |
END_CERT_LINE "\n", |
| 305 |
}, |
| 306 |
{ |
| 307 |
.name = "comments containing other special characters", |
| 308 |
.input = |
| 309 |
"# cert_1\n" |
| 310 |
BEGIN_CERT_LINE "\n" |
| 311 |
CERTIFICATE_1 "\n" |
| 312 |
END_CERT_LINE "\n" |
| 313 |
"\n" |
| 314 |
"# cert #2\n" |
| 315 |
BEGIN_CERT_LINE "\n" |
| 316 |
CERTIFICATE_2 "\n" |
| 317 |
END_CERT_LINE "\n" |
| 318 |
"\n" |
| 319 |
"# cert !@$%! \n" |
| 320 |
BEGIN_CERT_LINE "\n" |
| 321 |
CERTIFICATE_3 "\n" |
| 322 |
END_CERT_LINE "\n", |
| 323 |
}, |
| 324 |
{ |
| 325 |
.name = "trailing comment", |
| 326 |
.input = |
| 327 |
BEGIN_CERT_LINE "\n" |
| 328 |
CERTIFICATE_1 "\n" |
| 329 |
END_CERT_LINE "\n" |
| 330 |
BEGIN_CERT_LINE "\n" |
| 331 |
CERTIFICATE_2 "\n" |
| 332 |
END_CERT_LINE "\n" |
| 333 |
BEGIN_CERT_LINE "\n" |
| 334 |
CERTIFICATE_3 "\n" |
| 335 |
END_CERT_LINE "\n" |
| 336 |
"# trailing comment \n" |
| 337 |
}, |
| 338 |
{ |
| 339 |
.name = "trailing whitespace", |
| 340 |
.input = |
| 341 |
BEGIN_CERT_LINE "\n" |
| 342 |
CERTIFICATE_1 "\n" |
| 343 |
END_CERT_LINE "\n" |
| 344 |
BEGIN_CERT_LINE "\n" |
| 345 |
CERTIFICATE_2 "\n" |
| 346 |
END_CERT_LINE "\n" |
| 347 |
BEGIN_CERT_LINE "\n" |
| 348 |
CERTIFICATE_3 "\n" |
| 349 |
END_CERT_LINE "\n" |
| 350 |
"\n" |
| 351 |
"\n", |
| 352 |
}, |
| 353 |
{ |
| 354 |
.name = "trailing non-certificate data", |
| 355 |
.input = |
| 356 |
BEGIN_CERT_LINE "\n" |
| 357 |
CERTIFICATE_1 "\n" |
| 358 |
END_CERT_LINE "\n" |
| 359 |
BEGIN_CERT_LINE "\n" |
| 360 |
CERTIFICATE_2 "\n" |
| 361 |
END_CERT_LINE "\n" |
| 362 |
BEGIN_CERT_LINE "\n" |
| 363 |
CERTIFICATE_3 "\n" |
| 364 |
END_CERT_LINE "\n" |
| 365 |
"this is not a certificate\n" |
| 366 |
"neither is this", |
| 367 |
}, |
| 368 |
}; |
| 369 |
|
| 370 |
|
| 371 |
for (size_t i = 0; i < s2n_array_len(test_cases); i++) { |
| 372 |
DEFER_CLEANUP(struct s2n_cert_chain_and_key *chain_and_key = s2n_cert_chain_and_key_new(), |
| 373 |
s2n_cert_chain_and_key_ptr_free); |
| 374 |
const struct s2n_test_case *test_case = &test_cases[i]; |
| 375 |
|
| 376 |
DEFER_CLEANUP(struct s2n_stuffer input = { 0 }, s2n_stuffer_free); |
| 377 |
EXPECT_SUCCESS(s2n_stuffer_alloc(&input, strlen(test_case->input))); |
| 378 |
EXPECT_SUCCESS(s2n_stuffer_write_str(&input, test_case->input)); |
| 379 |
|
| 380 |
struct s2n_cert_chain *cert_chain = chain_and_key->cert_chain; |
| 381 |
if (s2n_create_cert_chain_from_stuffer(cert_chain, &input) != S2N_SUCCESS) { |
| 382 |
fprintf(stderr, "Failed to parse \"%s\"\n", test_case->name); |
| 383 |
FAIL_MSG("Failed to parse certificate chain input"); |
| 384 |
} |
| 385 |
|
| 386 |
struct s2n_cert *cert = cert_chain->head; |
| 387 |
for (size_t j = 0; j < s2n_array_len(expected_certs); j++) { |
| 388 |
if (s2n_result_is_error(s2n_test_validate_cert(cert, &expected_certs[j]))) { |
| 389 |
fprintf(stderr, "\"%s\" failed to parse cert %zu\n", test_case->name, j); |
| 390 |
FAIL_MSG("Did not correctly read all certificates"); |
| 391 |
} |
| 392 |
cert = cert->next; |
| 393 |
} |
| 394 |
} |
| 395 |
|
| 396 |
|
| 397 |
const struct s2n_test_case bad_test_cases[] = { |
| 398 |
{ |
| 399 |
.name = "double begin marker", |
| 400 |
.input = |
| 401 |
BEGIN_CERT_LINE "\n" |
| 402 |
BEGIN_CERT_LINE "\n" |
| 403 |
CERTIFICATE_1 "\n" |
| 404 |
END_CERT_LINE "\n" |
| 405 |
}, |
| 406 |
{ |
| 407 |
.name = "double end marker", |
| 408 |
.input = |
| 409 |
BEGIN_CERT_LINE "\n" |
| 410 |
CERTIFICATE_1 "\n" |
| 411 |
END_CERT_LINE "\n" |
| 412 |
END_CERT_LINE "\n" |
| 413 |
}, |
| 414 |
{ |
| 415 |
.name = "missing begin marker", |
| 416 |
.input = |
| 417 |
CERTIFICATE_1 "\n" |
| 418 |
END_CERT_LINE "\n" |
| 419 |
}, |
| 420 |
{ |
| 421 |
.name = "missing end marker", |
| 422 |
.input = |
| 423 |
BEGIN_CERT_LINE "\n" |
| 424 |
CERTIFICATE_1 "\n" |
| 425 |
}, |
| 426 |
{ |
| 427 |
.name = "no dashes before marker", |
| 428 |
.input = |
| 429 |
BEGIN_CERT "-----\n" |
| 430 |
CERTIFICATE_1 "\n" |
| 431 |
END_CERT_LINE "\n" |
| 432 |
}, |
| 433 |
{ |
| 434 |
.name = "no dashes after marker", |
| 435 |
.input = |
| 436 |
"-----" BEGIN_CERT "\n" |
| 437 |
CERTIFICATE_1 "\n" |
| 438 |
END_CERT_LINE "\n" |
| 439 |
}, |
| 440 |
{ |
| 441 |
.name = "single dash before marker", |
| 442 |
.input = |
| 443 |
"-" BEGIN_CERT "-----\n" |
| 444 |
CERTIFICATE_1 "\n" |
| 445 |
END_CERT_LINE "\n" |
| 446 |
}, |
| 447 |
{ |
| 448 |
.name = "single dash after marker", |
| 449 |
.input = |
| 450 |
"-----" BEGIN_CERT "-\n" |
| 451 |
CERTIFICATE_1 "\n" |
| 452 |
END_CERT_LINE "\n" |
| 453 |
}, |
| 454 |
{ |
| 455 |
.name = "65 dashes before marker", |
| 456 |
.input = |
| 457 |
"-----" "-----" "-----" "-----" |
| 458 |
"-----" "-----" "-----" "-----" |
| 459 |
"-----" "-----" "-----" "-----" |
| 460 |
"-----" |
| 461 |
BEGIN_CERT "-----\n" |
| 462 |
CERTIFICATE_1 "\n" |
| 463 |
END_CERT_LINE "\n" |
| 464 |
}, |
| 465 |
{ |
| 466 |
.name = "65 dashes after marker", |
| 467 |
.input = |
| 468 |
"-----" BEGIN_CERT |
| 469 |
"-----" "-----" "-----" "-----" |
| 470 |
"-----" "-----" "-----" "-----" |
| 471 |
"-----" "-----" "-----" "-----" |
| 472 |
"-----" "\n" |
| 473 |
CERTIFICATE_1 "\n" |
| 474 |
END_CERT_LINE "\n" |
| 475 |
}, |
| 476 |
{ |
| 477 |
.name = "dashes in comment", |
| 478 |
.input = |
| 479 |
"# --cert--\n" |
| 480 |
BEGIN_CERT_LINE "\n" |
| 481 |
CERTIFICATE_1 "\n" |
| 482 |
END_CERT_LINE "\n" |
| 483 |
}, |
| 484 |
{ |
| 485 |
.name = "multiple certs: trailing marker", |
| 486 |
.input = |
| 487 |
BEGIN_CERT_LINE "\n" |
| 488 |
CERTIFICATE_1 "\n" |
| 489 |
END_CERT_LINE "\n" |
| 490 |
BEGIN_CERT_LINE "\n" |
| 491 |
}, |
| 492 |
{ |
| 493 |
.name = "multiple certs: trailing partial certificate", |
| 494 |
.input = |
| 495 |
BEGIN_CERT_LINE "\n" |
| 496 |
CERTIFICATE_1 "\n" |
| 497 |
END_CERT_LINE "\n" |
| 498 |
BEGIN_CERT_LINE "\n" |
| 499 |
"MIIBljCCATygAwIBAg\n" |
| 500 |
}, |
| 501 |
{ |
| 502 |
.name = "multiple certs: missing end marker", |
| 503 |
.input = |
| 504 |
BEGIN_CERT_LINE "\n" |
| 505 |
CERTIFICATE_1 "\n" |
| 506 |
END_CERT_LINE "\n" |
| 507 |
BEGIN_CERT_LINE "\n" |
| 508 |
CERTIFICATE_1 "\n" |
| 509 |
}, |
| 510 |
}; |
| 511 |
|
| 512 |
|
| 513 |
for (size_t i = 0; i < s2n_array_len(bad_test_cases); i++) { |
| 514 |
DEFER_CLEANUP(struct s2n_cert_chain_and_key *chain_and_key = s2n_cert_chain_and_key_new(), |
| 515 |
s2n_cert_chain_and_key_ptr_free); |
| 516 |
const struct s2n_test_case *test_case = &bad_test_cases[i]; |
| 517 |
|
| 518 |
DEFER_CLEANUP(struct s2n_stuffer test_input = { 0 }, s2n_stuffer_free); |
| 519 |
EXPECT_SUCCESS(s2n_stuffer_alloc(&test_input, strlen(test_case->input))); |
| 520 |
EXPECT_SUCCESS(s2n_stuffer_write_str(&test_input, test_case->input)); |
| 521 |
|
| 522 |
struct s2n_cert_chain *cert_chain = chain_and_key->cert_chain; |
| 523 |
if (s2n_create_cert_chain_from_stuffer(cert_chain, &test_input) == S2N_SUCCESS) { |
| 524 |
fprintf(stderr, "Successfully parsed invalid cert chain \"%s\"\n", test_case->name); |
| 525 |
FAIL_MSG("Successfully parsed invalid cert chain"); |
| 526 |
}; |
| 527 |
} |
| 528 |
|
| 529 |
|
| 530 |
|
| 531 |
|
| 532 |
|
| 533 |
const char *valid_cert_chain = test_cases[0].input; |
| 534 |
for (size_t i = 0; i < s2n_array_len(bad_test_cases); i++) { |
| 535 |
const struct s2n_test_case *test_case = &bad_test_cases[i]; |
| 536 |
|
| 537 |
|
| 538 |
size_t test_input_size = strlen(test_case->input) + strlen(valid_cert_chain) + 1; |
| 539 |
|
| 540 |
DEFER_CLEANUP(struct s2n_stuffer test_input = { 0 }, s2n_stuffer_free); |
| 541 |
EXPECT_SUCCESS(s2n_stuffer_alloc(&test_input, test_input_size)); |
| 542 |
EXPECT_SUCCESS(s2n_stuffer_write_str(&test_input, valid_cert_chain)); |
| 543 |
|
| 544 |
|
| 545 |
DEFER_CLEANUP(struct s2n_cert_chain_and_key *good_chain_and_key = s2n_cert_chain_and_key_new(), |
| 546 |
s2n_cert_chain_and_key_ptr_free); |
| 547 |
struct s2n_cert_chain *good_chain = good_chain_and_key->cert_chain; |
| 548 |
EXPECT_SUCCESS(s2n_create_cert_chain_from_stuffer(good_chain, &test_input)); |
| 549 |
|
| 550 |
|
| 551 |
EXPECT_SUCCESS(s2n_stuffer_write_char(&test_input, '\n')); |
| 552 |
EXPECT_SUCCESS(s2n_stuffer_write_str(&test_input, test_case->input)); |
| 553 |
EXPECT_SUCCESS(s2n_stuffer_reread(&test_input)); |
| 554 |
|
| 555 |
|
| 556 |
DEFER_CLEANUP(struct s2n_cert_chain_and_key *bad_chain_and_key = s2n_cert_chain_and_key_new(), |
| 557 |
s2n_cert_chain_and_key_ptr_free); |
| 558 |
struct s2n_cert_chain *bad_chain = bad_chain_and_key->cert_chain; |
| 559 |
if (s2n_create_cert_chain_from_stuffer(bad_chain, &test_input) == S2N_SUCCESS) { |
| 560 |
fprintf(stderr, "Successfully parsed invalid cert chain \"%s\"\n", test_case->name); |
| 561 |
FAIL_MSG("Successfully parsed invalid cert chain"); |
| 562 |
}; |
| 563 |
} |
| 564 |
|
| 565 |
for (size_t i = 0; i < s2n_array_len(expected_certs); i++) { |
| 566 |
EXPECT_SUCCESS(s2n_free(&expected_certs[i])); |
| 567 |
} |
| 568 |
END_TEST(); |
| 569 |
} |