File: s2n_certificate_parsing_test.c

package info (click to toggle)
aws-crt-python 0.24.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 75,932 kB
  • sloc: ansic: 418,984; python: 23,626; makefile: 6,035; sh: 4,075; ruby: 208; java: 82; perl: 73; cpp: 25; xml: 11
file content (569 lines) | stat: -rw-r--r-- 20,618 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
/*
 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License").
 * You may not use this file except in compliance with the License.
 * A copy of the License is located at
 *
 *  http://aws.amazon.com/apache2.0
 *
 * or in the "license" file accompanying this file. This file is distributed
 * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
 * express or implied. See the License for the specific language governing
 * permissions and limitations under the License.
 */

#include "api/s2n.h"
#include "crypto/s2n_crypto.h"
#include "crypto/s2n_openssl_x509.h"
#include "s2n_test.h"
#include "testlib/s2n_testlib.h"
#include "tls/s2n_tls.h"
#include "utils/s2n_safety.h"

#define CERTIFICATE_1                                                  \
    "MIIBljCCATygAwIBAgIUHxKbtYzLM4Bct5v5Sagb8aZU/BcwCgYIKoZIzj0EAwIw" \
    "HjELMAkGA1UEBhMCVVMxDzANBgNVBAMMBmJyYW5jaDAgFw0yNDAxMjMwMDU3MTha" \
    "GA8yMjAzMDYzMDAwNTcxOFowHDELMAkGA1UEBhMCVVMxDTALBgNVBAMMBGxlYWYw" \
    "WTATBgcqhkjOPQIBBggqhkjOPQMBBwNCAASAmJKXt9P8hSz9ubntEokFn06+Rexr" \
    "lEujwWUIq5Kl6QwvtfDCzrJN/sUmM5mssjq7pF6XVr6zFcQ6G4BfnwGio1gwVjAU" \
    "BgNVHREEDTALgglsb2NhbGhvc3QwHQYDVR0OBBYEFNlOWJn7XfzC6xORzzjrdnqK" \
    "UlJwMB8GA1UdIwQYMBaAFFkQhpDCJ3bAbXw1tXpmk5Fi7YIGMAoGCCqGSM49BAMC" \
    "A0gAMEUCIB58OBwIruTJIy1f3tUgM/wXoO7fCoU25sMcioHBV9dYAiEA7Ufxa2JF" \
    "I5LP6RGyllsjjnh0MJy1ZMXhw7X6GqFn4Rw="

#define CERTIFICATE_2                                                  \
    "MIIBoDCCAUegAwIBAgIUQud1+tNUPAEBnJLb2Lyl3/vuA4EwCgYIKoZIzj0EAwIw" \
    "HDELMAkGA1UEBhMCVVMxDTALBgNVBAMMBHJvb3QwIBcNMjQwMTIzMDA1NzE4WhgP" \
    "MjIwMzA2MzAwMDU3MThaMB4xCzAJBgNVBAYTAlVTMQ8wDQYDVQQDDAZicmFuY2gw" \
    "WTATBgcqhkjOPQIBBggqhkjOPQMBBwNCAATfScB9w/LkHBAVXiyKN941555oyBpv" \
    "IZeCNXX+gbSvnS0pNRr35BalgFmij86DaXLl68RrHQsnhZByJvnIplN+o2MwYTAP" \
    "BgNVHRMBAf8EBTADAQH/MA4GA1UdDwEB/wQEAwICBDAdBgNVHQ4EFgQUWRCGkMIn" \
    "dsBtfDW1emaTkWLtggYwHwYDVR0jBBgwFoAUfCtcQDvXYwkR37fHwe/mi7JyQIQw" \
    "CgYIKoZIzj0EAwIDRwAwRAIgaK6aOuCfMwgAASavkZpoxWag49yco4d9AlxIU+rt" \
    "U2UCIHRieWdQICIWSEHdRTXWPPEnOd7A3UmTgoqbMl+Imhy8"

#define CERTIFICATE_3                                                  \
    "MIIBnzCCAUWgAwIBAgIUe0/XdFLyc4+Sj1NMkvbagE8DFaUwCgYIKoZIzj0EAwIw" \
    "HDELMAkGA1UEBhMCVVMxDTALBgNVBAMMBHJvb3QwIBcNMjQwMTIzMDA1NzE4WhgP" \
    "MjIwMzA2MzAwMDU3MThaMBwxCzAJBgNVBAYTAlVTMQ0wCwYDVQQDDARyb290MFkw" \
    "EwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEPPtDvucc4UGOdIjED2P/vDxVYDhO5P8s" \
    "7lyys3QZpKapMuc9wOV0cQ6tN9h4kVY+FJocYgqDAl2vv6Rg/wSbl6NjMGEwHQYD" \
    "VR0OBBYEFHwrXEA712MJEd+3x8Hv5ouyckCEMB8GA1UdIwQYMBaAFHwrXEA712MJ" \
    "Ed+3x8Hv5ouyckCEMA8GA1UdEwEB/wQFMAMBAf8wDgYDVR0PAQH/BAQDAgIEMAoG" \
    "CCqGSM49BAMCA0gAMEUCIQCcbhKRAsqQCj2pCXh+9og3sLw9q8nU4xAB9xuV3vPA" \
    "FAIgBxFWfRdu09dtE0IUkLTY0WPxiWaYYrKexlD4wUquqJE="

#define BEGIN_CERT      "BEGIN CERTIFICATE"
#define BEGIN_CERT_LINE "-----" BEGIN_CERT "-----"
#define END_CERT        "END CERTIFICATE"
#define END_CERT_LINE   "-----" END_CERT "-----"

static S2N_RESULT s2n_test_validate_cert(struct s2n_cert *cert, struct s2n_blob *expected)
{
    RESULT_ENSURE_REF(cert);
    RESULT_ENSURE_REF(expected);
    RESULT_ENSURE_EQ(cert->raw.size, expected->size);
    RESULT_ENSURE_EQ(memcmp(cert->raw.data, expected->data, expected->size), 0);
    return S2N_RESULT_OK;
}

int main(int argc, char **argv)
{
    BEGIN_TEST();

    const char *expected_cert_strs[] = {
        CERTIFICATE_1,
        CERTIFICATE_2,
        CERTIFICATE_3,
    };
    struct s2n_blob expected_certs[s2n_array_len(expected_cert_strs)] = { 0 };
    EXPECT_EQUAL(s2n_array_len(expected_certs), s2n_array_len(expected_cert_strs));

    for (size_t i = 0; i < s2n_array_len(expected_certs); i++) {
        const char *base64_cert = expected_cert_strs[i];
        struct s2n_blob *out = &expected_certs[i];

        /* base64 requires more than one character per byte, so the binary
         * output will be smaller than the base64 input.
         */
        EXPECT_SUCCESS(s2n_alloc(out, strlen(base64_cert)));

        struct s2n_stuffer out_stuffer = { 0 };
        EXPECT_SUCCESS(s2n_stuffer_init(&out_stuffer, out));

        DEFER_CLEANUP(struct s2n_stuffer in_stuffer = { 0 }, s2n_stuffer_free);
        EXPECT_SUCCESS(s2n_stuffer_alloc(&in_stuffer, strlen(base64_cert)));
        EXPECT_SUCCESS(s2n_stuffer_write_str(&in_stuffer, base64_cert));

        EXPECT_SUCCESS(s2n_stuffer_read_base64(&in_stuffer, &out_stuffer));
        out->size = s2n_stuffer_data_available(&out_stuffer);
    }

    struct s2n_test_case {
        const char *name;
        const char *input;
    };

    /* clang-format off */
    const struct s2n_test_case test_cases[] = {
        {
            .name = "basic format",
            .input =
                BEGIN_CERT_LINE "\n"
                CERTIFICATE_1 "\n"
                END_CERT_LINE "\n"
                BEGIN_CERT_LINE "\n"
                CERTIFICATE_2 "\n"
                END_CERT_LINE "\n"
                BEGIN_CERT_LINE "\n"
                CERTIFICATE_3 "\n"
                END_CERT_LINE "\n",
        },
        {
            .name = "no newlines",
            .input =
                BEGIN_CERT_LINE CERTIFICATE_1 END_CERT_LINE
                BEGIN_CERT_LINE CERTIFICATE_2 END_CERT_LINE
                BEGIN_CERT_LINE CERTIFICATE_3 END_CERT_LINE,
        },
        {
            .name = "empty lines",
            .input =
                "\n"
                BEGIN_CERT_LINE "\n"
                CERTIFICATE_1 "\n"
                END_CERT_LINE "\n"
                "\n"
                BEGIN_CERT_LINE "\n"
                CERTIFICATE_2 "\n"
                END_CERT_LINE "\n"
                "\n"
                BEGIN_CERT_LINE "\n"
                CERTIFICATE_3 "\n"
                END_CERT_LINE "\n"
                "\n",
        },
        {
            .name = "double empty lines",
            .input =
                "\n"
                "\n"
                BEGIN_CERT_LINE "\n"
                CERTIFICATE_1 "\n"
                END_CERT_LINE "\n"
                "\n"
                "\n"
                BEGIN_CERT_LINE "\n"
                CERTIFICATE_2 "\n"
                END_CERT_LINE "\n"
                "\n"
                "\n"
                BEGIN_CERT_LINE "\n"
                CERTIFICATE_3 "\n"
                END_CERT_LINE "\n"
                "\n"
                "\n",
        },
        {
            .name = "variable number of newlines",
            .input =
                "\n"
                BEGIN_CERT_LINE "\n"
                CERTIFICATE_1 "\n"
                END_CERT_LINE "\n"
                "\n\n\n"
                BEGIN_CERT_LINE "\n"
                CERTIFICATE_2 "\n"
                END_CERT_LINE "\n"
                BEGIN_CERT_LINE "\n"
                CERTIFICATE_3 "\n"
                END_CERT_LINE "\n"
                "\n\n"
        },
        {
            .name = "whitespace",
            .input =
                "   " BEGIN_CERT_LINE "\n"
                CERTIFICATE_1 "\n"
                END_CERT_LINE "  \n"
                "                 "
                BEGIN_CERT_LINE "\n"
                CERTIFICATE_2 "\n"
                END_CERT_LINE "    \n"
                BEGIN_CERT_LINE "\n"
                CERTIFICATE_3 "\n"
                END_CERT_LINE "  \n",
        },
        {
            .name = "extra dashes",
            .input =
                "-" BEGIN_CERT_LINE "--\n"
                CERTIFICATE_1 "\n"
                "---" END_CERT_LINE "----\n"
                "-----" BEGIN_CERT_LINE "------\n"
                CERTIFICATE_2 "\n"
                END_CERT_LINE "---------------\n"
                "--" BEGIN_CERT_LINE "---\n"
                CERTIFICATE_3 "\n"
                "-" END_CERT_LINE "-\n",
        },
        {
            .name = "64 dashes",
            .input =
                "-----" "-----" "-----" "-----"
                "-----" "-----" "-----" "-----"
                "-----" "-----" "-----" "-----"
                "----"
                BEGIN_CERT
                "-----" "-----" "-----" "-----"
                "-----" "-----" "-----" "-----"
                "-----" "-----" "-----" "-----"
                "----"
                "\n"
                CERTIFICATE_1 "\n"
                END_CERT_LINE "\n"
                BEGIN_CERT_LINE "\n"
                CERTIFICATE_2 "\n"
                END_CERT_LINE "\n"
                BEGIN_CERT_LINE "\n"
                CERTIFICATE_3 "\n"
                END_CERT_LINE "\n",
        },
        {
            .name = "missing dashes",
            .input =
                "--" BEGIN_CERT "--\n"
                CERTIFICATE_1 "\n"
                "---" END_CERT "---\n"
                "----" BEGIN_CERT "----\n"
                CERTIFICATE_2 "\n"
                "--" END_CERT "---\n"
                "---" BEGIN_CERT "----\n"
                CERTIFICATE_3 "\n"
                "----" END_CERT "--\n",
        },
        {
            .name = "delimiters share dashes",
            .input =
                "--" BEGIN_CERT "--" CERTIFICATE_1 "--" END_CERT
                "--" BEGIN_CERT "--" CERTIFICATE_2 "--" END_CERT
                "--" BEGIN_CERT "--" CERTIFICATE_3 "--" END_CERT "--" ,
        },
        {
            .name = "non-certificate data",
            .input =
                "this is not a certificate\n"
                BEGIN_CERT_LINE "\n"
                CERTIFICATE_1 "\n"
                END_CERT_LINE "\n"
                "\n"
                "this is not a certificate either\n"
                "\n"
                BEGIN_CERT_LINE "\n"
                CERTIFICATE_2 "\n"
                END_CERT_LINE "not a certificate\n"
                "not a certificate" BEGIN_CERT_LINE "\n"
                CERTIFICATE_3 "\n"
                END_CERT_LINE "\n",
        },
        {
            .name = "comments",
            .input =
                "# cert1"
                BEGIN_CERT_LINE "\n"
                CERTIFICATE_1 "\n"
                END_CERT_LINE "\n"
                "\n"
                "# cert2"
                BEGIN_CERT_LINE "\n"
                CERTIFICATE_2 "\n"
                END_CERT_LINE "\n"
                "\n"
                "# cert3"
                BEGIN_CERT_LINE "\n"
                CERTIFICATE_3 "\n"
                END_CERT_LINE "\n",
        },
        {
            .name = "comments containing dashes",
            .input =
                "# cert-1\n"
                BEGIN_CERT_LINE "\n"
                CERTIFICATE_1 "\n"
                END_CERT_LINE "\n"
                "\n"
                "# -c-e-r-t-2-\n"
                BEGIN_CERT_LINE "\n"
                CERTIFICATE_2 "\n"
                END_CERT_LINE "\n"
                "\n"
                "# -\n"
                BEGIN_CERT_LINE "\n"
                CERTIFICATE_3 "\n"
                END_CERT_LINE "\n",
        },
        {
            .name = "comments containing other special characters",
            .input =
                "# cert_1\n"
                BEGIN_CERT_LINE "\n"
                CERTIFICATE_1 "\n"
                END_CERT_LINE "\n"
                "\n"
                "# cert #2\n"
                BEGIN_CERT_LINE "\n"
                CERTIFICATE_2 "\n"
                END_CERT_LINE "\n"
                "\n"
                "# cert !@$%! \n"
                BEGIN_CERT_LINE "\n"
                CERTIFICATE_3 "\n"
                END_CERT_LINE "\n",
        },
        {
            .name = "trailing comment",
            .input =
                BEGIN_CERT_LINE "\n"
                CERTIFICATE_1 "\n"
                END_CERT_LINE "\n"
                BEGIN_CERT_LINE "\n"
                CERTIFICATE_2 "\n"
                END_CERT_LINE "\n"
                BEGIN_CERT_LINE "\n"
                CERTIFICATE_3 "\n"
                END_CERT_LINE "\n"
                "# trailing comment \n"
        },
        {
            .name = "trailing whitespace",
            .input =
                BEGIN_CERT_LINE "\n"
                CERTIFICATE_1 "\n"
                END_CERT_LINE "\n"
                BEGIN_CERT_LINE "\n"
                CERTIFICATE_2 "\n"
                END_CERT_LINE "\n"
                BEGIN_CERT_LINE "\n"
                CERTIFICATE_3 "\n"
                END_CERT_LINE "\n"
                "\n"
                "\n",
        },
        {
            .name = "trailing non-certificate data",
            .input =
                BEGIN_CERT_LINE "\n"
                CERTIFICATE_1 "\n"
                END_CERT_LINE "\n"
                BEGIN_CERT_LINE "\n"
                CERTIFICATE_2 "\n"
                END_CERT_LINE "\n"
                BEGIN_CERT_LINE "\n"
                CERTIFICATE_3 "\n"
                END_CERT_LINE "\n"
                "this is not a certificate\n"
                "neither is this",
        },
    };
    /* clang-format on */

    for (size_t i = 0; i < s2n_array_len(test_cases); i++) {
        DEFER_CLEANUP(struct s2n_cert_chain_and_key *chain_and_key = s2n_cert_chain_and_key_new(),
                s2n_cert_chain_and_key_ptr_free);
        const struct s2n_test_case *test_case = &test_cases[i];

        DEFER_CLEANUP(struct s2n_stuffer input = { 0 }, s2n_stuffer_free);
        EXPECT_SUCCESS(s2n_stuffer_alloc(&input, strlen(test_case->input)));
        EXPECT_SUCCESS(s2n_stuffer_write_str(&input, test_case->input));

        struct s2n_cert_chain *cert_chain = chain_and_key->cert_chain;
        if (s2n_create_cert_chain_from_stuffer(cert_chain, &input) != S2N_SUCCESS) {
            fprintf(stderr, "Failed to parse \"%s\"\n", test_case->name);
            FAIL_MSG("Failed to parse certificate chain input");
        }

        struct s2n_cert *cert = cert_chain->head;
        for (size_t j = 0; j < s2n_array_len(expected_certs); j++) {
            if (s2n_result_is_error(s2n_test_validate_cert(cert, &expected_certs[j]))) {
                fprintf(stderr, "\"%s\" failed to parse cert %zu\n", test_case->name, j);
                FAIL_MSG("Did not correctly read all certificates");
            }
            cert = cert->next;
        }
    }

    /* clang-format off */
    const struct s2n_test_case bad_test_cases[] = {
        {
            .name = "double begin marker",
            .input =
                BEGIN_CERT_LINE "\n"
                BEGIN_CERT_LINE "\n"
                CERTIFICATE_1 "\n"
                END_CERT_LINE "\n"
        },
        {
            .name = "double end marker",
            .input =
                BEGIN_CERT_LINE "\n"
                CERTIFICATE_1 "\n"
                END_CERT_LINE "\n"
                END_CERT_LINE "\n"
        },
        {
            .name = "missing begin marker",
            .input =
                CERTIFICATE_1 "\n"
                END_CERT_LINE "\n"
        },
        {
            .name = "missing end marker",
            .input =
                BEGIN_CERT_LINE "\n"
                CERTIFICATE_1 "\n"
        },
        {
            .name = "no dashes before marker",
            .input =
                BEGIN_CERT "-----\n"
                CERTIFICATE_1 "\n"
                END_CERT_LINE "\n"
        },
        {
            .name = "no dashes after marker",
            .input =
                "-----" BEGIN_CERT "\n"
                CERTIFICATE_1 "\n"
                END_CERT_LINE "\n"
        },
        {
            .name = "single dash before marker",
            .input =
                "-" BEGIN_CERT "-----\n"
                CERTIFICATE_1 "\n"
                END_CERT_LINE "\n"
        },
        {
            .name = "single dash after marker",
            .input =
                "-----" BEGIN_CERT "-\n"
                CERTIFICATE_1 "\n"
                END_CERT_LINE "\n"
        },
        {
            .name = "65 dashes before marker",
            .input =
                "-----" "-----" "-----" "-----"
                "-----" "-----" "-----" "-----"
                "-----" "-----" "-----" "-----"
                "-----"
                BEGIN_CERT "-----\n"
                CERTIFICATE_1 "\n"
                END_CERT_LINE "\n"
        },
        {
            .name = "65 dashes after marker",
            .input =
                "-----" BEGIN_CERT
                "-----" "-----" "-----" "-----"
                "-----" "-----" "-----" "-----"
                "-----" "-----" "-----" "-----"
                "-----" "\n"
                CERTIFICATE_1 "\n"
                END_CERT_LINE "\n"
        },
        {
            .name = "dashes in comment",
            .input =
                "# --cert--\n"
                BEGIN_CERT_LINE "\n"
                CERTIFICATE_1 "\n"
                END_CERT_LINE "\n"
        },
        {
            .name = "multiple certs: trailing marker",
            .input =
                BEGIN_CERT_LINE "\n"
                CERTIFICATE_1 "\n"
                END_CERT_LINE "\n"
                BEGIN_CERT_LINE "\n"
        },
        {
            .name = "multiple certs: trailing partial certificate",
            .input =
                BEGIN_CERT_LINE "\n"
                CERTIFICATE_1 "\n"
                END_CERT_LINE "\n"
                BEGIN_CERT_LINE "\n"
                "MIIBljCCATygAwIBAg\n"
        },
        {
            .name = "multiple certs: missing end marker",
            .input =
                BEGIN_CERT_LINE "\n"
                CERTIFICATE_1 "\n"
                END_CERT_LINE "\n"
                BEGIN_CERT_LINE "\n"
                CERTIFICATE_1 "\n"
        },
    };
    /* clang-format on */

    for (size_t i = 0; i < s2n_array_len(bad_test_cases); i++) {
        DEFER_CLEANUP(struct s2n_cert_chain_and_key *chain_and_key = s2n_cert_chain_and_key_new(),
                s2n_cert_chain_and_key_ptr_free);
        const struct s2n_test_case *test_case = &bad_test_cases[i];

        DEFER_CLEANUP(struct s2n_stuffer test_input = { 0 }, s2n_stuffer_free);
        EXPECT_SUCCESS(s2n_stuffer_alloc(&test_input, strlen(test_case->input)));
        EXPECT_SUCCESS(s2n_stuffer_write_str(&test_input, test_case->input));

        struct s2n_cert_chain *cert_chain = chain_and_key->cert_chain;
        if (s2n_create_cert_chain_from_stuffer(cert_chain, &test_input) == S2N_SUCCESS) {
            fprintf(stderr, "Successfully parsed invalid cert chain \"%s\"\n", test_case->name);
            FAIL_MSG("Successfully parsed invalid cert chain");
        };
    }

    /* Any case that is invalid for a single certificate should also be invalid
     * for a certificate chain.
     * We do not want to rely solely on our test for 0-length chains.
     */
    const char *valid_cert_chain = test_cases[0].input;
    for (size_t i = 0; i < s2n_array_len(bad_test_cases); i++) {
        const struct s2n_test_case *test_case = &bad_test_cases[i];

        /* The extra character is for an extra newline */
        size_t test_input_size = strlen(test_case->input) + strlen(valid_cert_chain) + 1;

        DEFER_CLEANUP(struct s2n_stuffer test_input = { 0 }, s2n_stuffer_free);
        EXPECT_SUCCESS(s2n_stuffer_alloc(&test_input, test_input_size));
        EXPECT_SUCCESS(s2n_stuffer_write_str(&test_input, valid_cert_chain));

        /* Sanity check: valid chain is valid */
        DEFER_CLEANUP(struct s2n_cert_chain_and_key *good_chain_and_key = s2n_cert_chain_and_key_new(),
                s2n_cert_chain_and_key_ptr_free);
        struct s2n_cert_chain *good_chain = good_chain_and_key->cert_chain;
        EXPECT_SUCCESS(s2n_create_cert_chain_from_stuffer(good_chain, &test_input));

        /* Add the invalid input to the end of the proven valid chain */
        EXPECT_SUCCESS(s2n_stuffer_write_char(&test_input, '\n'));
        EXPECT_SUCCESS(s2n_stuffer_write_str(&test_input, test_case->input));
        EXPECT_SUCCESS(s2n_stuffer_reread(&test_input));

        /* Test: valid chain + invalid test case is sill invalid */
        DEFER_CLEANUP(struct s2n_cert_chain_and_key *bad_chain_and_key = s2n_cert_chain_and_key_new(),
                s2n_cert_chain_and_key_ptr_free);
        struct s2n_cert_chain *bad_chain = bad_chain_and_key->cert_chain;
        if (s2n_create_cert_chain_from_stuffer(bad_chain, &test_input) == S2N_SUCCESS) {
            fprintf(stderr, "Successfully parsed invalid cert chain \"%s\"\n", test_case->name);
            FAIL_MSG("Successfully parsed invalid cert chain");
        };
    }

    for (size_t i = 0; i < s2n_array_len(expected_certs); i++) {
        EXPECT_SUCCESS(s2n_free(&expected_certs[i]));
    }
    END_TEST();
}