aboutsummaryrefslogtreecommitdiffstats
path: root/test/cmsapitest.c
blob: e583b33f2c7e27addf445136b92f4aa9a3278735 (plain)
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
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
/*
 * Copyright 2018-2026 The OpenSSL Project Authors. All Rights Reserved.
 *
 * Licensed under the Apache License 2.0 (the "License").  You may not use
 * this file except in compliance with the License.  You can obtain a copy
 * in the file LICENSE in the source distribution or at
 * https://www.openssl.org/source/license.html
 */

#include <string.h>

#include <openssl/pem.h>
#include <openssl/cms.h>
#include <openssl/bio.h>
#include <openssl/x509.h>
#include "../crypto/cms/cms_local.h" /* for d.signedData and d.envelopedData */

#include "testutil.h"

static X509 *cert = NULL;
static EVP_PKEY *privkey = NULL;
static char *derin = NULL;
static char *too_long_iv_cms_in = NULL;
static char *pwri_kek_oob_der_in = NULL;

/*
 * This is our bad cms data, it contains an AuthEnvelopedData field
 * with a CIPHER OID set to AES-256-OFB
 */
static const unsigned char bad_cms_der[452] = {
    0x30, 0x82, 0x01, 0xc0, 0x06, 0x0b, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d,
    0x01, 0x09, 0x10, 0x01, 0x17, 0xa0, 0x82, 0x01, 0xaf, 0x30, 0x82, 0x01,
    0xab, 0x02, 0x01, 0x00, 0x31, 0x82, 0x01, 0x44, 0x30, 0x82, 0x01, 0x40,
    0x02, 0x01, 0x00, 0x30, 0x28, 0x30, 0x10, 0x31, 0x0e, 0x30, 0x0c, 0x06,
    0x03, 0x55, 0x04, 0x03, 0x0c, 0x05, 0x52, 0x65, 0x63, 0x69, 0x70, 0x02,
    0x14, 0x1a, 0x5c, 0x04, 0x9b, 0x3a, 0x64, 0xff, 0xd4, 0x63, 0xde, 0x4f,
    0x90, 0xe5, 0x76, 0xe2, 0x18, 0xe8, 0x5c, 0x9e, 0xd7, 0x30, 0x0d, 0x06,
    0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01, 0x05, 0x00,
    0x04, 0x82, 0x01, 0x00, 0x18, 0xcf, 0x9f, 0x44, 0x95, 0x79, 0xe9, 0x96,
    0x7d, 0x0f, 0xd1, 0xb4, 0xc2, 0x38, 0xb0, 0xc9, 0x76, 0xd5, 0xba, 0x08,
    0x5c, 0xbf, 0xc3, 0x30, 0xea, 0x3a, 0x68, 0xa4, 0xba, 0x99, 0x4c, 0x70,
    0x97, 0xb8, 0xa9, 0xce, 0x71, 0x4c, 0x54, 0xa3, 0xfd, 0x81, 0x9e, 0x15,
    0x63, 0xb7, 0x23, 0x46, 0x17, 0x69, 0xaf, 0x8f, 0xbd, 0xa3, 0x54, 0x23,
    0xf3, 0xf5, 0x35, 0xa8, 0xd4, 0x9c, 0xec, 0xe1, 0x17, 0x2c, 0x6d, 0x0b,
    0xad, 0xc0, 0xe9, 0x1d, 0xd1, 0x8d, 0x59, 0xd5, 0x29, 0xc6, 0x40, 0xc4,
    0xcd, 0x4e, 0x87, 0x70, 0x19, 0x5d, 0x88, 0x50, 0xbd, 0x4a, 0x13, 0xb3,
    0xef, 0x0c, 0x6d, 0x6a, 0xc5, 0x51, 0xbb, 0x5c, 0x39, 0x17, 0xda, 0xb1,
    0x71, 0x17, 0x88, 0xfb, 0x6a, 0xef, 0x7f, 0x85, 0xa7, 0x04, 0x71, 0xc7,
    0x83, 0x91, 0xb3, 0x30, 0x1b, 0x3d, 0x18, 0x7f, 0x63, 0xbf, 0x42, 0x7c,
    0xae, 0x6f, 0xae, 0xa1, 0x17, 0x84, 0xfd, 0x67, 0x2a, 0x4f, 0x4c, 0xe9,
    0x05, 0x26, 0x2c, 0xd5, 0xab, 0x0c, 0xcf, 0xdc, 0x3f, 0x24, 0xcf, 0x71,
    0x26, 0x7a, 0x1f, 0xf7, 0xc9, 0x92, 0x5e, 0xb6, 0x3d, 0x7f, 0xc3, 0x08,
    0xd3, 0xad, 0xc0, 0xc8, 0x4f, 0x42, 0x0c, 0xf3, 0xac, 0x23, 0x11, 0xdf,
    0x75, 0x84, 0x69, 0x8c, 0xa6, 0x59, 0x43, 0xfb, 0xf7, 0x6b, 0x62, 0xf0,
    0xf7, 0x35, 0x07, 0xc4, 0xf8, 0xd5, 0x12, 0x4a, 0x16, 0x62, 0xbc, 0x04,
    0xaa, 0x9a, 0x2e, 0xb2, 0x1a, 0xfa, 0x4c, 0x82, 0xce, 0x9e, 0xa8, 0x6d,
    0xc1, 0x29, 0x59, 0xe0, 0x33, 0xb5, 0xa6, 0x47, 0x09, 0x2e, 0xbf, 0x60,
    0xa6, 0xb3, 0x21, 0xa0, 0x15, 0xac, 0x92, 0x29, 0xb5, 0xe6, 0xe0, 0xd4,
    0x8b, 0xd8, 0x21, 0xe2, 0x17, 0x98, 0xd1, 0x11, 0x5d, 0xc5, 0xae, 0x24,
    0xe8, 0x92, 0xdb, 0x96, 0xa3, 0x5b, 0x58, 0xa7, 0x30, 0x4c, 0x06, 0x09,
    0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x07, 0x01, 0x30, 0x1d, 0x06,
    0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x01, 0x2b, 0x04, 0x10,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x80, 0x20, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41,
    0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41,
    0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41,
    0x41, 0x41, 0x04, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};

/*
 * This array represents a der encoded contentinfo structure addressed to
 * servercert.pem, with the tag value of the aes-256-gcm cipher used to encrypt
 * the contents of the mssages down to 1 byte.  Decoding it should fail
 */
static const unsigned char one_byte_mac_cms_der[423] = {
    0x30, 0x82, 0x01, 0xa3, 0x06, 0x0b, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d,
    0x01, 0x09, 0x10, 0x01, 0x17, 0xa0, 0x82, 0x01, 0x92, 0x30, 0x82, 0x01,
    0x8e, 0x02, 0x01, 0x00, 0x31, 0x82, 0x01, 0x33, 0x30, 0x82, 0x01, 0x2f,
    0x02, 0x01, 0x00, 0x30, 0x17, 0x30, 0x12, 0x31, 0x10, 0x30, 0x0e, 0x06,
    0x03, 0x55, 0x04, 0x03, 0x0c, 0x07, 0x52, 0x6f, 0x6f, 0x74, 0x20, 0x43,
    0x41, 0x02, 0x01, 0x02, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86,
    0xf7, 0x0d, 0x01, 0x01, 0x01, 0x05, 0x00, 0x04, 0x82, 0x01, 0x00, 0x10,
    0x3a, 0x8c, 0xee, 0x4e, 0xe2, 0x1f, 0xfe, 0xcc, 0x28, 0x39, 0x9e, 0x46,
    0xbe, 0xa7, 0xd5, 0x02, 0x2a, 0x53, 0x06, 0x5f, 0x94, 0x6b, 0x69, 0x6d,
    0x2d, 0xe8, 0x44, 0xa6, 0x43, 0x52, 0x82, 0x89, 0x2d, 0xf1, 0x9b, 0xb9,
    0x9e, 0xa4, 0x8d, 0x77, 0xf1, 0xd2, 0x8e, 0x86, 0x79, 0x06, 0x3e, 0x90,
    0xf0, 0xca, 0x9e, 0xb5, 0x35, 0xd5, 0x89, 0xf0, 0x7c, 0x06, 0xa0, 0x91,
    0xbf, 0xf4, 0x61, 0xaa, 0x5c, 0x99, 0xa3, 0x64, 0x15, 0xfd, 0xf9, 0x90,
    0xf0, 0xf3, 0x25, 0x5b, 0x48, 0xa1, 0xfb, 0x7a, 0xce, 0x63, 0xdc, 0xa9,
    0xfe, 0x7c, 0xbe, 0x9c, 0xaa, 0xd3, 0x42, 0x0e, 0x4a, 0xc3, 0x4b, 0x4e,
    0x76, 0x6d, 0x52, 0x54, 0x85, 0x4e, 0xab, 0x50, 0x2c, 0x5f, 0xc2, 0x8b,
    0x9f, 0x1f, 0x0f, 0x8a, 0x7c, 0xb3, 0x0a, 0xde, 0x50, 0x9b, 0xef, 0x89,
    0xf2, 0xea, 0x07, 0xca, 0x11, 0x76, 0x29, 0xaf, 0xe4, 0x59, 0x28, 0x19,
    0x48, 0x96, 0x67, 0xdd, 0xdd, 0x01, 0xf0, 0x14, 0xbe, 0x3d, 0xa5, 0xa3,
    0x83, 0x21, 0x39, 0x29, 0xb7, 0x8f, 0xb7, 0xf4, 0x85, 0x05, 0xee, 0xca,
    0xbb, 0xbd, 0xc0, 0xaf, 0x0d, 0xf1, 0xef, 0x5f, 0x06, 0x05, 0xeb, 0x0e,
    0x55, 0xf0, 0x7e, 0x13, 0x1a, 0x2a, 0x37, 0xd4, 0xba, 0x26, 0xc8, 0x2e,
    0x6b, 0xc3, 0xe1, 0xcf, 0x28, 0xab, 0x0d, 0xab, 0xdd, 0xa7, 0xf4, 0xd3,
    0x59, 0xcd, 0xc7, 0x2d, 0xa1, 0x56, 0x5f, 0x47, 0x77, 0x27, 0x17, 0x71,
    0xae, 0x75, 0xc8, 0x71, 0x58, 0xf9, 0xab, 0x67, 0xda, 0x23, 0x62, 0xa0,
    0x6d, 0xe5, 0x2d, 0x06, 0xb8, 0xc0, 0xac, 0xaa, 0x38, 0xa4, 0x0d, 0xb5,
    0xb2, 0xce, 0xa7, 0x26, 0x0d, 0x3a, 0x88, 0x2f, 0x8d, 0x6c, 0xa0, 0xf6,
    0x94, 0xf2, 0x2c, 0x37, 0x03, 0xaf, 0x67, 0x5c, 0xf3, 0x2c, 0xfb, 0xe8,
    0x16, 0x9e, 0x55, 0x30, 0x4f, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7,
    0x0d, 0x01, 0x07, 0x01, 0x30, 0x1e, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01,
    0x65, 0x03, 0x04, 0x01, 0x2e, 0x30, 0x11, 0x04, 0x0c, 0x6b, 0x1d, 0xe5,
    0xb2, 0x38, 0x0e, 0x17, 0x91, 0x9c, 0x9c, 0x40, 0x35, 0x02, 0x01, 0x10,
    0x80, 0x22, 0xa0, 0x90, 0x75, 0x74, 0xdf, 0x2d, 0xba, 0x4f, 0xce, 0x4e,
    0x7e, 0x52, 0xb0, 0x2e, 0x5f, 0xe0, 0x84, 0x01, 0xb1, 0x49, 0x0b, 0x69,
    0xc7, 0x61, 0x63, 0x84, 0x3a, 0xfc, 0xaa, 0x86, 0xfc, 0x96, 0x4e, 0x6c,
    0x04, 0x01, 0x92
};

static int test_short_mac_on_auth_envelope_data(void)
{
    int ret = 0;
    const unsigned char *derptr = one_byte_mac_cms_der;
    BIO *outmsgbio = BIO_new(BIO_s_mem());
    CMS_ContentInfo *content = d2i_CMS_ContentInfo(NULL, &derptr, OSSL_NELEM(one_byte_mac_cms_der));

    if (!TEST_ptr(content))
        goto end;

    /*
     * We expect this to fail, as the tag value in the authEnvelopedData parameter is
     * a single byte
     */
    if (!TEST_false(CMS_decrypt(content, privkey, cert, NULL, outmsgbio, CMS_TEXT)))
        goto end;

    ret = 1;
end:
    BIO_free(outmsgbio);
    CMS_ContentInfo_free(content);
    return ret;
}

static int test_non_aead_on_auth_envelope_dec(void)
{
    int ret = 0;
    const unsigned char *derptr = bad_cms_der;
    BIO *outmsgbio = BIO_new(BIO_s_mem());
    CMS_ContentInfo *content = d2i_CMS_ContentInfo(NULL, &derptr, OSSL_NELEM(bad_cms_der));

    if (!TEST_ptr(content))
        goto end;

    /*
     * We expect this to fail
     */
    if (!TEST_false(CMS_decrypt(content, privkey, cert, NULL, outmsgbio,
            CMS_TEXT)))
        goto end;

    ret = 1;
end:
    BIO_free(outmsgbio);
    CMS_ContentInfo_free(content);
    return ret;
}

static int test_non_aead_on_auth_envelope_enc(void)
{
    CMS_ContentInfo *content = NULL;
    STACK_OF(X509) *certstack = sk_X509_new_null();
    const EVP_CIPHER *cipher = EVP_aes_128_cbc();
    const char *msg = "Hello world";
    BIO *msgbio = BIO_new_mem_buf(msg, (int)strlen(msg));
    BIO *outmsgbio = BIO_new(BIO_s_mem());
    X509 *recip;
    int i;
    int ret = 0;

    if (!TEST_ptr(certstack) || !TEST_ptr(msgbio) || !TEST_ptr(outmsgbio))
        goto end;

    if (!TEST_int_gt(sk_X509_push(certstack, cert), 0))
        goto end;

    /*
     * Emulate CMS_encrypt here, but use a non AEAD cipher
     */
    content = CMS_AuthEnvelopedData_create_ex(cipher, NULL, NULL);

    if (!TEST_ptr(content))
        goto end;

    for (i = 0; i < sk_X509_num(certstack); i++) {
        recip = sk_X509_value(certstack, i);
        if (!TEST_ptr(CMS_add1_recipient_cert(content, recip, CMS_TEXT)))
            goto end;
    }

    /*
     * We expect this to fail as we are using a non-AEAD cipher on
     * AuthEnvelopedData
     */
    if (!TEST_int_eq(CMS_final(content, msgbio, NULL, CMS_TEXT), 0))
        goto end;

    ret = 1;
end:
    sk_X509_free(certstack);
    BIO_free(msgbio);
    BIO_free(outmsgbio);
    CMS_ContentInfo_free(content);
    return ret;
}

static int test_encrypt_decrypt(const EVP_CIPHER *cipher)
{
    int testresult = 0;
    STACK_OF(X509) *certstack = sk_X509_new_null();
    const char *msg = "Hello world";
    BIO *msgbio = BIO_new_mem_buf(msg, (int)strlen(msg));
    BIO *outmsgbio = BIO_new(BIO_s_mem());
    CMS_ContentInfo *content = NULL;
    BIO *contentbio = NULL;
    char buf[80];

    if (!TEST_ptr(certstack) || !TEST_ptr(msgbio) || !TEST_ptr(outmsgbio))
        goto end;

    if (!TEST_int_gt(sk_X509_push(certstack, cert), 0))
        goto end;

    content = CMS_encrypt(certstack, msgbio, cipher, CMS_TEXT);
    if (!TEST_ptr(content))
        goto end;

    if (!TEST_true(CMS_decrypt(content, privkey, cert, NULL, outmsgbio,
            CMS_TEXT)))
        goto end;

    if (!(EVP_CIPHER_get_flags(cipher) & EVP_CIPH_FLAG_AEAD_CIPHER)
        && !TEST_ptr(contentbio = CMS_EnvelopedData_decrypt(content->d.envelopedData,
                         NULL, privkey, cert, NULL,
                         CMS_TEXT, NULL, NULL)))
        goto end;

    /* Check we got the message we first started with */
    if (!TEST_int_eq(BIO_gets(outmsgbio, buf, sizeof(buf)), (int)strlen(msg))
        || !TEST_int_eq(strcmp(buf, msg), 0))
        goto end;

    testresult = 1;
end:
    BIO_free(contentbio);
    sk_X509_free(certstack);
    BIO_free(msgbio);
    BIO_free(outmsgbio);
    CMS_ContentInfo_free(content);

    return testresult && TEST_int_eq(ERR_peek_error(), 0);
}

static int test_encrypt_decrypt_aes_cbc(void)
{
    return test_encrypt_decrypt(EVP_aes_128_cbc());
}

static int test_encrypt_decrypt_aes_128_gcm(void)
{
    return test_encrypt_decrypt(EVP_aes_128_gcm());
}

static int test_encrypt_decrypt_aes_192_gcm(void)
{
    return test_encrypt_decrypt(EVP_aes_192_gcm());
}

static int test_encrypt_decrypt_aes_256_gcm(void)
{
    return test_encrypt_decrypt(EVP_aes_256_gcm());
}

static int test_CMS_add1_cert(void)
{
    CMS_ContentInfo *cms = NULL;
    int ret = 0;

    ret = TEST_ptr(cms = CMS_ContentInfo_new())
        && TEST_ptr(CMS_add1_signer(cms, cert, privkey, NULL, 0))
        && TEST_true(CMS_add1_cert(cms, cert)); /* add cert again */

    CMS_ContentInfo_free(cms);
    return ret;
}

static int test_d2i_CMS_bio_NULL(void)
{
    BIO *bio, *content = NULL;
    CMS_ContentInfo *cms = NULL;
    unsigned int flags = CMS_NO_SIGNER_CERT_VERIFY;
    int ret = 0;

    /*
     * Test data generated using:
     * openssl cms -sign -md sha256 -signer ./test/certs/rootCA.pem -inkey \
     * ./test/certs/rootCA.key -nodetach -outform DER -in ./in.txt -out out.der \
     * -nosmimecap
     */
    static const unsigned char cms_data[] = {
        0x30, 0x82, 0x05, 0xc5, 0x06, 0x09, 0x2a, 0x86,
        0x48, 0x86, 0xf7, 0x0d, 0x01, 0x07, 0x02, 0xa0,
        0x82, 0x05, 0xb6, 0x30, 0x82, 0x05, 0xb2, 0x02,
        0x01, 0x01, 0x31, 0x0d, 0x30, 0x0b, 0x06, 0x09,
        0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02,
        0x01, 0x30, 0x1c, 0x06, 0x09, 0x2a, 0x86, 0x48,
        0x86, 0xf7, 0x0d, 0x01, 0x07, 0x01, 0xa0, 0x0f,
        0x04, 0x0d, 0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x20,
        0x57, 0x6f, 0x72, 0x6c, 0x64, 0x0d, 0x0a, 0xa0,
        0x82, 0x03, 0x83, 0x30, 0x82, 0x03, 0x7f, 0x30,
        0x82, 0x02, 0x67, 0xa0, 0x03, 0x02, 0x01, 0x02,
        0x02, 0x09, 0x00, 0x88, 0x43, 0x29, 0xcb, 0xc2,
        0xeb, 0x15, 0x9a, 0x30, 0x0d, 0x06, 0x09, 0x2a,
        0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x0b,
        0x05, 0x00, 0x30, 0x56, 0x31, 0x0b, 0x30, 0x09,
        0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x41,
        0x55, 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55,
        0x04, 0x08, 0x0c, 0x0a, 0x53, 0x6f, 0x6d, 0x65,
        0x2d, 0x53, 0x74, 0x61, 0x74, 0x65, 0x31, 0x21,
        0x30, 0x1f, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x0c,
        0x18, 0x49, 0x6e, 0x74, 0x65, 0x72, 0x6e, 0x65,
        0x74, 0x20, 0x57, 0x69, 0x64, 0x67, 0x69, 0x74,
        0x73, 0x20, 0x50, 0x74, 0x79, 0x20, 0x4c, 0x74,
        0x64, 0x31, 0x0f, 0x30, 0x0d, 0x06, 0x03, 0x55,
        0x04, 0x03, 0x0c, 0x06, 0x72, 0x6f, 0x6f, 0x74,
        0x43, 0x41, 0x30, 0x1e, 0x17, 0x0d, 0x31, 0x35,
        0x30, 0x37, 0x30, 0x32, 0x31, 0x33, 0x31, 0x35,
        0x31, 0x31, 0x5a, 0x17, 0x0d, 0x33, 0x35, 0x30,
        0x37, 0x30, 0x32, 0x31, 0x33, 0x31, 0x35, 0x31,
        0x31, 0x5a, 0x30, 0x56, 0x31, 0x0b, 0x30, 0x09,
        0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x41,
        0x55, 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55,
        0x04, 0x08, 0x0c, 0x0a, 0x53, 0x6f, 0x6d, 0x65,
        0x2d, 0x53, 0x74, 0x61, 0x74, 0x65, 0x31, 0x21,
        0x30, 0x1f, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x0c,
        0x18, 0x49, 0x6e, 0x74, 0x65, 0x72, 0x6e, 0x65,
        0x74, 0x20, 0x57, 0x69, 0x64, 0x67, 0x69, 0x74,
        0x73, 0x20, 0x50, 0x74, 0x79, 0x20, 0x4c, 0x74,
        0x64, 0x31, 0x0f, 0x30, 0x0d, 0x06, 0x03, 0x55,
        0x04, 0x03, 0x0c, 0x06, 0x72, 0x6f, 0x6f, 0x74,
        0x43, 0x41, 0x30, 0x82, 0x01, 0x22, 0x30, 0x0d,
        0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d,
        0x01, 0x01, 0x01, 0x05, 0x00, 0x03, 0x82, 0x01,
        0x0f, 0x00, 0x30, 0x82, 0x01, 0x0a, 0x02, 0x82,
        0x01, 0x01, 0x00, 0xc0, 0xf1, 0x6b, 0x77, 0x88,
        0xac, 0x35, 0xdf, 0xfb, 0x73, 0x53, 0x2f, 0x92,
        0x80, 0x2f, 0x74, 0x16, 0x32, 0x4d, 0xf5, 0x10,
        0x20, 0x6f, 0x6c, 0x3a, 0x8e, 0xd1, 0xdc, 0x6b,
        0xe1, 0x2e, 0x3e, 0xc3, 0x04, 0x0f, 0xbf, 0x9b,
        0xc4, 0xc9, 0x12, 0xd1, 0xe4, 0x0b, 0x45, 0x97,
        0xe5, 0x06, 0xcd, 0x66, 0x3a, 0xe1, 0xe0, 0xe2,
        0x2b, 0xdf, 0xa2, 0xc4, 0xec, 0x7b, 0xd3, 0x3d,
        0x3c, 0x8a, 0xff, 0x5e, 0x74, 0xa0, 0xab, 0xa7,
        0x03, 0x6a, 0x16, 0x5b, 0x5e, 0x92, 0xc4, 0x7e,
        0x5b, 0x79, 0x8a, 0x69, 0xd4, 0xbc, 0x83, 0x5e,
        0xae, 0x42, 0x92, 0x74, 0xa5, 0x2b, 0xe7, 0x00,
        0xc1, 0xa9, 0xdc, 0xd5, 0xb1, 0x53, 0x07, 0x0f,
        0x73, 0xf7, 0x8e, 0xad, 0x14, 0x3e, 0x25, 0x9e,
        0xe5, 0x1e, 0xe6, 0xcc, 0x91, 0xcd, 0x95, 0x0c,
        0x80, 0x44, 0x20, 0xc3, 0xfd, 0x17, 0xcf, 0x91,
        0x3d, 0x63, 0x10, 0x1c, 0x14, 0x5b, 0xfb, 0xc3,
        0xa8, 0xc1, 0x88, 0xb2, 0x77, 0xff, 0x9c, 0xdb,
        0xfc, 0x6a, 0x44, 0x44, 0x44, 0xf7, 0x85, 0xec,
        0x08, 0x2c, 0xd4, 0xdf, 0x81, 0xa3, 0x79, 0xc9,
        0xfe, 0x1e, 0x9b, 0x93, 0x16, 0x53, 0xb7, 0x97,
        0xab, 0xbe, 0x4f, 0x1a, 0xa5, 0xe2, 0xfa, 0x46,
        0x05, 0xe4, 0x0d, 0x9c, 0x2a, 0xa4, 0xcc, 0xb9,
        0x1e, 0x21, 0xa0, 0x6c, 0xc4, 0xab, 0x59, 0xb0,
        0x40, 0x39, 0xbb, 0xf9, 0x88, 0xad, 0xfd, 0xdf,
        0x8d, 0xb4, 0x0b, 0xaf, 0x7e, 0x41, 0xe0, 0x21,
        0x3c, 0xc8, 0x33, 0x45, 0x49, 0x84, 0x2f, 0x93,
        0x06, 0xee, 0xfd, 0x4f, 0xed, 0x4f, 0xf3, 0xbc,
        0x9b, 0xde, 0xfc, 0x25, 0x5e, 0x55, 0xd5, 0x75,
        0xd4, 0xc5, 0x7b, 0x3a, 0x40, 0x35, 0x06, 0x9f,
        0xc4, 0x84, 0xb4, 0x6c, 0x93, 0x0c, 0xaf, 0x37,
        0x5a, 0xaf, 0xb6, 0x41, 0x4d, 0x26, 0x23, 0x1c,
        0xb8, 0x02, 0xb3, 0x02, 0x03, 0x01, 0x00, 0x01,
        0xa3, 0x50, 0x30, 0x4e, 0x30, 0x0c, 0x06, 0x03,
        0x55, 0x1d, 0x13, 0x04, 0x05, 0x30, 0x03, 0x01,
        0x01, 0xff, 0x30, 0x1d, 0x06, 0x03, 0x55, 0x1d,
        0x0e, 0x04, 0x16, 0x04, 0x14, 0x85, 0x56, 0x89,
        0x35, 0xe2, 0x9f, 0x00, 0x1a, 0xe1, 0x86, 0x03,
        0x0b, 0x4b, 0xaf, 0x76, 0x12, 0x6b, 0x33, 0x6d,
        0xfd, 0x30, 0x1f, 0x06, 0x03, 0x55, 0x1d, 0x23,
        0x04, 0x18, 0x30, 0x16, 0x80, 0x14, 0x85, 0x56,
        0x89, 0x35, 0xe2, 0x9f, 0x00, 0x1a, 0xe1, 0x86,
        0x03, 0x0b, 0x4b, 0xaf, 0x76, 0x12, 0x6b, 0x33,
        0x6d, 0xfd, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86,
        0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x0b, 0x05,
        0x00, 0x03, 0x82, 0x01, 0x01, 0x00, 0x32, 0x0a,
        0xbf, 0x2a, 0x0a, 0xe2, 0xbb, 0x4f, 0x43, 0xce,
        0x88, 0xda, 0x5a, 0x39, 0x10, 0x37, 0x80, 0xbb,
        0x37, 0x2d, 0x5e, 0x2d, 0x88, 0xdd, 0x26, 0x69,
        0x9c, 0xe7, 0xb4, 0x98, 0x20, 0xb1, 0x25, 0xe6,
        0x61, 0x59, 0x6d, 0x12, 0xec, 0x9b, 0x87, 0xbe,
        0x57, 0xe1, 0x12, 0x05, 0xc5, 0x04, 0xf1, 0x17,
        0xce, 0x14, 0xb8, 0x1c, 0x92, 0xd4, 0x95, 0x95,
        0x2c, 0x5b, 0x28, 0x89, 0xfb, 0x72, 0x9c, 0x20,
        0xd3, 0x32, 0x81, 0xa8, 0x85, 0xec, 0xc8, 0x08,
        0x7b, 0xa8, 0x59, 0x5b, 0x3a, 0x6c, 0x31, 0xab,
        0x52, 0xe2, 0x66, 0xcd, 0x14, 0x49, 0x5c, 0xf3,
        0xd3, 0x3e, 0x62, 0xbc, 0x91, 0x16, 0xb4, 0x1c,
        0xf5, 0xdd, 0x54, 0xaa, 0x3c, 0x61, 0x97, 0x79,
        0xac, 0xe4, 0xc8, 0x43, 0x35, 0xc3, 0x0f, 0xfc,
        0xf3, 0x70, 0x1d, 0xaf, 0xf0, 0x9c, 0x8a, 0x2a,
        0x92, 0x93, 0x48, 0xaa, 0xd0, 0xe8, 0x47, 0xbe,
        0x35, 0xc1, 0xc6, 0x7b, 0x6d, 0xda, 0xfa, 0x5d,
        0x57, 0x45, 0xf3, 0xea, 0x41, 0x8f, 0x36, 0xc1,
        0x3c, 0xf4, 0x52, 0x7f, 0x6e, 0x31, 0xdd, 0xba,
        0x9a, 0xbc, 0x70, 0x56, 0x71, 0x38, 0xdc, 0x49,
        0x57, 0x0c, 0xfd, 0x91, 0x17, 0xc5, 0xea, 0x87,
        0xe5, 0x23, 0x74, 0x19, 0xb2, 0xb6, 0x99, 0x0c,
        0x6b, 0xa2, 0x05, 0xf8, 0x51, 0x68, 0xed, 0x97,
        0xe0, 0xdf, 0x62, 0xf9, 0x7e, 0x7a, 0x3a, 0x44,
        0x71, 0x83, 0x57, 0x28, 0x49, 0x88, 0x69, 0xb5,
        0x14, 0x1e, 0xda, 0x46, 0xe3, 0x6e, 0x78, 0xe1,
        0xcb, 0x8f, 0xb5, 0x98, 0xb3, 0x2d, 0x6e, 0x5b,
        0xb7, 0xf6, 0x93, 0x24, 0x14, 0x1f, 0xa4, 0xf6,
        0x69, 0xbd, 0xff, 0x4c, 0x52, 0x50, 0x02, 0xc5,
        0x43, 0x8d, 0x14, 0xe2, 0xd0, 0x75, 0x9f, 0x12,
        0x5e, 0x94, 0x89, 0xd1, 0xef, 0x77, 0x89, 0x7d,
        0x89, 0xd9, 0x9e, 0x76, 0x99, 0x24, 0x31, 0x82,
        0x01, 0xf7, 0x30, 0x82, 0x01, 0xf3, 0x02, 0x01,
        0x01, 0x30, 0x63, 0x30, 0x56, 0x31, 0x0b, 0x30,
        0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02,
        0x41, 0x55, 0x31, 0x13, 0x30, 0x11, 0x06, 0x03,
        0x55, 0x04, 0x08, 0x0c, 0x0a, 0x53, 0x6f, 0x6d,
        0x65, 0x2d, 0x53, 0x74, 0x61, 0x74, 0x65, 0x31,
        0x21, 0x30, 0x1f, 0x06, 0x03, 0x55, 0x04, 0x0a,
        0x0c, 0x18, 0x49, 0x6e, 0x74, 0x65, 0x72, 0x6e,
        0x65, 0x74, 0x20, 0x57, 0x69, 0x64, 0x67, 0x69,
        0x74, 0x73, 0x20, 0x50, 0x74, 0x79, 0x20, 0x4c,
        0x74, 0x64, 0x31, 0x0f, 0x30, 0x0d, 0x06, 0x03,
        0x55, 0x04, 0x03, 0x0c, 0x06, 0x72, 0x6f, 0x6f,
        0x74, 0x43, 0x41, 0x02, 0x09, 0x00, 0x88, 0x43,
        0x29, 0xcb, 0xc2, 0xeb, 0x15, 0x9a, 0x30, 0x0b,
        0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03,
        0x04, 0x02, 0x01, 0xa0, 0x69, 0x30, 0x18, 0x06,
        0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01,
        0x09, 0x03, 0x31, 0x0b, 0x06, 0x09, 0x2a, 0x86,
        0x48, 0x86, 0xf7, 0x0d, 0x01, 0x07, 0x01, 0x30,
        0x1c, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7,
        0x0d, 0x01, 0x09, 0x05, 0x31, 0x0f, 0x17, 0x0d,
        0x32, 0x30, 0x31, 0x32, 0x31, 0x31, 0x30, 0x39,
        0x30, 0x30, 0x31, 0x33, 0x5a, 0x30, 0x2f, 0x06,
        0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01,
        0x09, 0x04, 0x31, 0x22, 0x04, 0x20, 0xb0, 0x80,
        0x22, 0xd3, 0x15, 0xcf, 0x1e, 0xb1, 0x2d, 0x26,
        0x65, 0xbd, 0xed, 0x0e, 0x6a, 0xf4, 0x06, 0x53,
        0xc0, 0xa0, 0xbe, 0x97, 0x52, 0x32, 0xfb, 0x49,
        0xbc, 0xbd, 0x02, 0x1c, 0xfc, 0x36, 0x30, 0x0d,
        0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d,
        0x01, 0x01, 0x01, 0x05, 0x00, 0x04, 0x82, 0x01,
        0x00, 0x37, 0x44, 0x39, 0x08, 0xb2, 0x19, 0x52,
        0x35, 0x9c, 0xd0, 0x67, 0x87, 0xae, 0xb8, 0x1c,
        0x80, 0xf4, 0x03, 0x29, 0x2e, 0xe3, 0x76, 0x4a,
        0xb0, 0x98, 0x10, 0x00, 0x9a, 0x30, 0xdb, 0x05,
        0x28, 0x53, 0x34, 0x31, 0x14, 0xbd, 0x87, 0xb9,
        0x4d, 0x45, 0x07, 0x97, 0xa3, 0x57, 0x0b, 0x7e,
        0xd1, 0x67, 0xfb, 0x4e, 0x0f, 0x5b, 0x90, 0xb2,
        0x6f, 0xe6, 0xce, 0x49, 0xdd, 0x72, 0x46, 0x71,
        0x26, 0xa1, 0x1b, 0x98, 0x23, 0x7d, 0x69, 0x73,
        0x84, 0xdc, 0xf9, 0xd2, 0x1c, 0x6d, 0xf6, 0xf5,
        0x17, 0x49, 0x6e, 0x9d, 0x4d, 0xf1, 0xe2, 0x43,
        0x29, 0x53, 0x55, 0xa5, 0x22, 0x1e, 0x89, 0x2c,
        0xaf, 0xf2, 0x43, 0x47, 0xd5, 0xfa, 0xad, 0xe7,
        0x89, 0x60, 0xbf, 0x96, 0x35, 0x6f, 0xc2, 0x99,
        0xb7, 0x55, 0xc5, 0xe3, 0x04, 0x25, 0x1b, 0xf6,
        0x7e, 0xf2, 0x2b, 0x14, 0xa9, 0x57, 0x96, 0xbe,
        0xbd, 0x6e, 0x95, 0x44, 0x94, 0xbd, 0xaf, 0x9a,
        0x6d, 0x77, 0x55, 0x5e, 0x6c, 0xf6, 0x32, 0x37,
        0xec, 0xef, 0xe5, 0x81, 0xb0, 0xe3, 0x35, 0xc7,
        0x86, 0xea, 0x47, 0x59, 0x38, 0xb6, 0x16, 0xfb,
        0x1d, 0x10, 0x55, 0x48, 0xb1, 0x44, 0x33, 0xde,
        0xf6, 0x29, 0xbe, 0xbf, 0xbc, 0x71, 0x3e, 0x49,
        0xba, 0xe7, 0x9f, 0x4d, 0x6c, 0xfb, 0xec, 0xd2,
        0xe0, 0x12, 0xa9, 0x7c, 0xc9, 0x9a, 0x7b, 0x85,
        0x83, 0xb8, 0xca, 0xdd, 0xf6, 0xb7, 0x15, 0x75,
        0x7b, 0x4a, 0x69, 0xcf, 0x0a, 0xc7, 0x80, 0x01,
        0xe7, 0x94, 0x16, 0x7f, 0x8d, 0x3c, 0xfa, 0x1f,
        0x05, 0x71, 0x76, 0x15, 0xb0, 0xf6, 0x61, 0x30,
        0x58, 0x16, 0xbe, 0x1b, 0xd1, 0x93, 0xc4, 0x1a,
        0x91, 0x0c, 0x48, 0xe2, 0x1c, 0x8e, 0xa5, 0xc5,
        0xa7, 0x81, 0x44, 0x48, 0x3b, 0x10, 0xc2, 0x74,
        0x07, 0xdf, 0xa8, 0xae, 0x57, 0xee, 0x7f, 0xe3,
        0x6a
    };

    ret = TEST_ptr(bio = BIO_new_mem_buf(cms_data, sizeof(cms_data)))
        && TEST_ptr(cms = d2i_CMS_bio(bio, NULL))
        && TEST_true(CMS_verify(cms, NULL, NULL, NULL, NULL, flags))
        && TEST_ptr(content = CMS_SignedData_verify(cms->d.signedData, NULL, NULL, NULL,
                        NULL, NULL, flags, NULL, NULL));
    BIO_free(content);
    CMS_ContentInfo_free(cms);
    BIO_free(bio);
    return ret && TEST_int_eq(ERR_peek_error(), 0);
}

static unsigned char *read_all(BIO *bio, long *p_len)
{
    const int step = 256;
    unsigned char *buf = NULL;
    unsigned char *tmp = NULL;
    int ret;

    *p_len = 0;
    for (;;) {
        tmp = OPENSSL_realloc(buf, *p_len + step);
        if (tmp == NULL)
            break;
        buf = tmp;
        ret = BIO_read(bio, buf + *p_len, step);
        if (ret < 0)
            break;

        if (LONG_MAX - ret < *p_len)
            break;

        *p_len += ret;

        if (ret < step)
            return buf;
    }

    /* Error */
    OPENSSL_free(buf);
    *p_len = 0;
    return NULL;
}

static int test_d2i_CMS_decode(const int idx)
{
    BIO *bio = NULL;
    CMS_ContentInfo *cms = NULL;
    unsigned char *buf = NULL;
    const unsigned char *tmp = NULL;
    long buf_len = 0;
    int ret = 0;

    if (!TEST_ptr(bio = BIO_new_file(derin, "r")))
        goto end;

    switch (idx) {
    case 0:
        if (!TEST_ptr(cms = d2i_CMS_bio(bio, NULL)))
            goto end;
        break;
    case 1:
        if (!TEST_ptr(buf = read_all(bio, &buf_len)))
            goto end;
        tmp = buf;
        if (!TEST_ptr(cms = d2i_CMS_ContentInfo(NULL, &tmp, buf_len)))
            goto end;
        break;
    }

    if (!TEST_int_eq(ERR_peek_error(), 0))
        goto end;

    ret = 1;
end:
    CMS_ContentInfo_free(cms);
    BIO_free(bio);
    OPENSSL_free(buf);

    return ret;
}

static int test_CMS_set1_key_mem_leak(void)
{
    CMS_ContentInfo *cms;
    unsigned char key[32] = { 0 };
    int ret = 0;

    if (!TEST_ptr(cms = CMS_ContentInfo_new()))
        return 0;

    if (!TEST_true(CMS_EncryptedData_set1_key(cms, EVP_aes_256_cbc(),
            key, 32)))
        goto end;

    if (!TEST_true(CMS_EncryptedData_set1_key(cms, EVP_aes_128_cbc(),
            key, 16)))
        goto end;

    ret = 1;
end:
    CMS_ContentInfo_free(cms);
    return ret;
}

static int test_encrypted_data(void)
{
    const char *msg = "Hello world";
    BIO *msgbio = BIO_new_mem_buf(msg, (int)strlen(msg));
    uint8_t key[16] = { 0 };
    size_t keylen = 16;
    CMS_ContentInfo *cms;
    BIO *decryptbio = BIO_new(BIO_s_mem());
    char buf[80];
    int ret = 0;

    cms = CMS_EncryptedData_encrypt(msgbio, EVP_aes_128_cbc(), key, keylen, SMIME_BINARY);
    if (!TEST_ptr(cms))
        goto end;

    if (!TEST_true(CMS_EncryptedData_decrypt(cms, key, keylen, NULL, decryptbio, SMIME_BINARY)))
        goto end;

    /* Check we got the message we first started with */
    if (!TEST_int_eq(BIO_gets(decryptbio, buf, sizeof(buf)), (int)strlen(msg))
        || !TEST_int_eq(strcmp(buf, msg), 0))
        goto end;

    ret = 1;
end:
    CMS_ContentInfo_free(cms);
    BIO_free(msgbio);
    BIO_free(decryptbio);
    return ret;
}

static int test_encrypted_data_aead(void)
{
    const char *msg = "Hello world";
    BIO *msgbio = BIO_new_mem_buf(msg, (int)strlen(msg));
    uint8_t key[16] = { 0 };
    size_t keylen = 16;
    CMS_ContentInfo *cms;
    BIO *decryptbio = BIO_new(BIO_s_mem());
    int ret = 0;

    cms = CMS_ContentInfo_new();
    if (!TEST_ptr(cms))
        goto end;

    /*
     * AEAD algorithms are not supported by the CMS EncryptedData so setting
     * the cipher to AES GCM 128 will result in a failure
     */
    if (!TEST_false(CMS_EncryptedData_set1_key(cms, EVP_aes_128_gcm(), key, keylen)))
        goto end;

    CMS_ContentInfo_free(cms);
    cms = NULL;

    /*
     * AEAD algorithms are not supported by the CMS EncryptedData so setting
     * the cipher to AES GCM 128 will result in a failure
     */
    cms = CMS_EncryptedData_encrypt(msgbio, EVP_aes_128_gcm(), key, keylen, SMIME_BINARY);
    if (!TEST_ptr_null(cms))
        goto end;

    ret = 1;

end:
    CMS_ContentInfo_free(cms);
    BIO_free(msgbio);
    BIO_free(decryptbio);
    return ret;
}

static int test_cms_aesgcm_iv_too_long(void)
{
    int ret = 0;
    BIO *cmsbio = NULL, *out = NULL;
    CMS_ContentInfo *cms = NULL;
    unsigned long err = 0;

    if (!TEST_ptr(cmsbio = BIO_new_file(too_long_iv_cms_in, "r")))
        goto end;

    if (!TEST_ptr(cms = PEM_read_bio_CMS(cmsbio, NULL, NULL, NULL)))
        goto end;

    /* Must fail cleanly (no crash) */
    if (!TEST_false(CMS_decrypt(cms, privkey, cert, NULL, out, 0)))
        goto end;
    err = ERR_peek_last_error();
    if (!TEST_ulong_ne(err, 0))
        goto end;
    if (!TEST_int_eq(ERR_GET_LIB(err), ERR_LIB_CMS))
        goto end;
    if (!TEST_int_eq(ERR_GET_REASON(err), CMS_R_CIPHER_PARAMETER_INITIALISATION_ERROR))
        goto end;

    ret = 1;
end:
    CMS_ContentInfo_free(cms);
    BIO_free(cmsbio);
    BIO_free(out);
    return ret;
}

/*
 * CMS EnvelopedData with a single PasswordRecipientInfo using
 * id-alg-PWRI-KEK and an AES-128-CFB key encryption cipher
 * (1-byte effective block size).  The encryptedKey OCTET STRING is
 * only two bytes long, so the wrapped key buffer is shorter than
 * the seven octets read by the check-byte test in kek_unwrap_key().
 * Prior to CVE-2026-9076 this triggered an out-of-bounds heap read;
 * CMS_decrypt() must now fail cleanly.
 */
static int test_pwri_kek_unwrap_short_encrypted_key(void)
{
    BIO *in = NULL;
    CMS_ContentInfo *cms = NULL;
    unsigned long err = 0;
    int ret = 0;

    if (!TEST_ptr(in = BIO_new_file(pwri_kek_oob_der_in, "rb"))
        || !TEST_ptr(cms = d2i_CMS_bio(in, NULL)))
        goto end;

    /*
     * The unwrap is attempted eagerly inside CMS_decrypt_set1_password().
     * It must fail cleanly (no OOB read) and report CMS_R_UNWRAP_FAILURE.
     */
    if (!TEST_false(CMS_decrypt_set1_password(cms,
            (unsigned char *)"password", -1)))
        goto end;

    err = ERR_peek_last_error();
    if (!TEST_int_eq(ERR_GET_LIB(err), ERR_LIB_CMS)
        || !TEST_int_eq(ERR_GET_REASON(err), CMS_R_UNWRAP_FAILURE))
        goto end;

    ERR_clear_error();
    ret = 1;
end:
    CMS_ContentInfo_free(cms);
    BIO_free(in);
    return ret;
}

OPT_TEST_DECLARE_USAGE("certfile privkeyfile derfile tooLongIVpem pwriKekOobDer\n")

int setup_tests(void)
{
    char *certin = NULL, *privkeyin = NULL;
    BIO *certbio = NULL, *privkeybio = NULL;

    if (!test_skip_common_options()) {
        TEST_error("Error parsing test options\n");
        return 0;
    }

    if (!TEST_ptr(certin = test_get_argument(0))
        || !TEST_ptr(privkeyin = test_get_argument(1))
        || !TEST_ptr(derin = test_get_argument(2))
        || !TEST_ptr(too_long_iv_cms_in = test_get_argument(3))
        || !TEST_ptr(pwri_kek_oob_der_in = test_get_argument(4)))
        return 0;

    certbio = BIO_new_file(certin, "r");
    if (!TEST_ptr(certbio))
        return 0;
    if (!TEST_true(PEM_read_bio_X509(certbio, &cert, NULL, NULL))) {
        BIO_free(certbio);
        return 0;
    }
    BIO_free(certbio);

    privkeybio = BIO_new_file(privkeyin, "r");
    if (!TEST_ptr(privkeybio)) {
        X509_free(cert);
        cert = NULL;
        return 0;
    }
    if (!TEST_true(PEM_read_bio_PrivateKey(privkeybio, &privkey, NULL, NULL))) {
        BIO_free(privkeybio);
        X509_free(cert);
        cert = NULL;
        return 0;
    }
    BIO_free(privkeybio);

    ADD_TEST(test_encrypt_decrypt_aes_cbc);
    ADD_TEST(test_encrypt_decrypt_aes_128_gcm);
    ADD_TEST(test_encrypt_decrypt_aes_192_gcm);
    ADD_TEST(test_encrypt_decrypt_aes_256_gcm);
    ADD_TEST(test_non_aead_on_auth_envelope_enc);
    ADD_TEST(test_non_aead_on_auth_envelope_dec);
    ADD_TEST(test_short_mac_on_auth_envelope_data);
    ADD_TEST(test_CMS_add1_cert);
    ADD_TEST(test_d2i_CMS_bio_NULL);
    ADD_TEST(test_CMS_set1_key_mem_leak);
    ADD_TEST(test_encrypted_data);
    ADD_TEST(test_encrypted_data_aead);
    ADD_ALL_TESTS(test_d2i_CMS_decode, 2);
    ADD_TEST(test_cms_aesgcm_iv_too_long);
    ADD_TEST(test_pwri_kek_unwrap_short_encrypted_key);
    return 1;
}

void cleanup_tests(void)
{
    X509_free(cert);
    EVP_PKEY_free(privkey);
}