xref: /src/crypto/openssl/test/pkcs12_format_test.c (revision f25b8c9fb4f58cf61adb47d7570abe7caa6d385d)
1 /*
2  * Copyright 2020-2024 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the Apache License 2.0 (the "License").  You may not use
5  * this file except in compliance with the License.  You can obtain a copy
6  * in the file LICENSE in the source distribution or at
7  * https://www.openssl.org/source/license.html
8  */
9 
10 #include <stdio.h>
11 #include <string.h>
12 #include <stdlib.h>
13 
14 #include <openssl/pkcs12.h>
15 #include <openssl/x509.h>
16 #include <openssl/x509v3.h>
17 #include <openssl/pem.h>
18 
19 #include "testutil.h"
20 #include "helpers/pkcs12.h"
21 
22 static int default_libctx = 1;
23 
24 static OSSL_LIB_CTX *testctx = NULL;
25 static OSSL_PROVIDER *nullprov = NULL;
26 static OSSL_PROVIDER *deflprov = NULL;
27 static OSSL_PROVIDER *lgcyprov = NULL;
28 
29 /* --------------------------------------------------------------------------
30  * PKCS12 component test data
31  */
32 
33 static const unsigned char CERT1[] = {
34     0x30,
35     0x82,
36     0x01,
37     0xed,
38     0x30,
39     0x82,
40     0x01,
41     0x56,
42     0xa0,
43     0x03,
44     0x02,
45     0x01,
46     0x02,
47     0x02,
48     0x09,
49     0x00,
50     0x8b,
51     0x4b,
52     0x5e,
53     0x6c,
54     0x03,
55     0x28,
56     0x4e,
57     0xe6,
58     0x30,
59     0x0d,
60     0x06,
61     0x09,
62     0x2a,
63     0x86,
64     0x48,
65     0x86,
66     0xf7,
67     0x0d,
68     0x01,
69     0x01,
70     0x0b,
71     0x05,
72     0x00,
73     0x30,
74     0x19,
75     0x31,
76     0x17,
77     0x30,
78     0x15,
79     0x06,
80     0x03,
81     0x55,
82     0x04,
83     0x03,
84     0x0c,
85     0x0e,
86     0x50,
87     0x31,
88     0x32,
89     0x54,
90     0x65,
91     0x73,
92     0x74,
93     0x2d,
94     0x52,
95     0x6f,
96     0x6f,
97     0x74,
98     0x2d,
99     0x41,
100     0x30,
101     0x1e,
102     0x17,
103     0x0d,
104     0x31,
105     0x39,
106     0x30,
107     0x39,
108     0x33,
109     0x30,
110     0x30,
111     0x30,
112     0x34,
113     0x36,
114     0x35,
115     0x36,
116     0x5a,
117     0x17,
118     0x0d,
119     0x32,
120     0x39,
121     0x30,
122     0x39,
123     0x32,
124     0x37,
125     0x30,
126     0x30,
127     0x34,
128     0x36,
129     0x35,
130     0x36,
131     0x5a,
132     0x30,
133     0x1b,
134     0x31,
135     0x19,
136     0x30,
137     0x17,
138     0x06,
139     0x03,
140     0x55,
141     0x04,
142     0x03,
143     0x0c,
144     0x10,
145     0x50,
146     0x31,
147     0x32,
148     0x54,
149     0x65,
150     0x73,
151     0x74,
152     0x2d,
153     0x53,
154     0x65,
155     0x72,
156     0x76,
157     0x65,
158     0x72,
159     0x2d,
160     0x31,
161     0x30,
162     0x81,
163     0x9f,
164     0x30,
165     0x0d,
166     0x06,
167     0x09,
168     0x2a,
169     0x86,
170     0x48,
171     0x86,
172     0xf7,
173     0x0d,
174     0x01,
175     0x01,
176     0x01,
177     0x05,
178     0x00,
179     0x03,
180     0x81,
181     0x8d,
182     0x00,
183     0x30,
184     0x81,
185     0x89,
186     0x02,
187     0x81,
188     0x81,
189     0x00,
190     0xbc,
191     0xdc,
192     0x6f,
193     0x8c,
194     0x7a,
195     0x2a,
196     0x4b,
197     0xea,
198     0x66,
199     0x66,
200     0x04,
201     0xa9,
202     0x05,
203     0x92,
204     0x53,
205     0xd7,
206     0x13,
207     0x3c,
208     0x49,
209     0xe1,
210     0xc8,
211     0xbb,
212     0xdf,
213     0x3d,
214     0xcb,
215     0x88,
216     0x31,
217     0x07,
218     0x20,
219     0x59,
220     0x93,
221     0x24,
222     0x7f,
223     0x7d,
224     0xc6,
225     0x84,
226     0x81,
227     0x16,
228     0x64,
229     0x4a,
230     0x52,
231     0xa6,
232     0x30,
233     0x44,
234     0xdc,
235     0x1a,
236     0x30,
237     0xde,
238     0xae,
239     0x29,
240     0x18,
241     0xcf,
242     0xc7,
243     0xf3,
244     0xcf,
245     0x0c,
246     0xb7,
247     0x8e,
248     0x2b,
249     0x1e,
250     0x21,
251     0x01,
252     0x0b,
253     0xfb,
254     0xe5,
255     0xe6,
256     0xcf,
257     0x2b,
258     0x84,
259     0xe1,
260     0x33,
261     0xf8,
262     0xba,
263     0x02,
264     0xfc,
265     0x30,
266     0xfa,
267     0xc4,
268     0x33,
269     0xc7,
270     0x37,
271     0xc6,
272     0x7f,
273     0x72,
274     0x31,
275     0x92,
276     0x1d,
277     0x8f,
278     0xa0,
279     0xfb,
280     0xe5,
281     0x4a,
282     0x08,
283     0x31,
284     0x78,
285     0x80,
286     0x9c,
287     0x23,
288     0xb4,
289     0xe9,
290     0x19,
291     0x56,
292     0x04,
293     0xfa,
294     0x0d,
295     0x07,
296     0x04,
297     0xb7,
298     0x43,
299     0xac,
300     0x4c,
301     0x49,
302     0x7c,
303     0xc2,
304     0xa1,
305     0x44,
306     0xc1,
307     0x48,
308     0x7d,
309     0x28,
310     0xe5,
311     0x23,
312     0x66,
313     0x07,
314     0x22,
315     0xd5,
316     0xf0,
317     0xf1,
318     0x02,
319     0x03,
320     0x01,
321     0x00,
322     0x01,
323     0xa3,
324     0x3b,
325     0x30,
326     0x39,
327     0x30,
328     0x1f,
329     0x06,
330     0x03,
331     0x55,
332     0x1d,
333     0x23,
334     0x04,
335     0x18,
336     0x30,
337     0x16,
338     0x80,
339     0x14,
340     0xdb,
341     0xbb,
342     0xb8,
343     0x92,
344     0x4e,
345     0x24,
346     0x0b,
347     0x1b,
348     0xbb,
349     0x78,
350     0x33,
351     0xf9,
352     0x01,
353     0x02,
354     0x23,
355     0x0d,
356     0x96,
357     0x18,
358     0x30,
359     0x47,
360     0x30,
361     0x09,
362     0x06,
363     0x03,
364     0x55,
365     0x1d,
366     0x13,
367     0x04,
368     0x02,
369     0x30,
370     0x00,
371     0x30,
372     0x0b,
373     0x06,
374     0x03,
375     0x55,
376     0x1d,
377     0x0f,
378     0x04,
379     0x04,
380     0x03,
381     0x02,
382     0x04,
383     0xf0,
384     0x30,
385     0x0d,
386     0x06,
387     0x09,
388     0x2a,
389     0x86,
390     0x48,
391     0x86,
392     0xf7,
393     0x0d,
394     0x01,
395     0x01,
396     0x0b,
397     0x05,
398     0x00,
399     0x03,
400     0x81,
401     0x81,
402     0x00,
403     0x1c,
404     0x13,
405     0xdc,
406     0x02,
407     0xf1,
408     0x44,
409     0x36,
410     0x65,
411     0xa9,
412     0xbe,
413     0x30,
414     0x1c,
415     0x66,
416     0x14,
417     0x20,
418     0x86,
419     0x5a,
420     0xa8,
421     0x69,
422     0x25,
423     0xf8,
424     0x1a,
425     0xb6,
426     0x9e,
427     0x5e,
428     0xe9,
429     0x89,
430     0xb8,
431     0x67,
432     0x70,
433     0x19,
434     0x87,
435     0x60,
436     0xeb,
437     0x4b,
438     0x11,
439     0x71,
440     0x85,
441     0xf8,
442     0xe9,
443     0xa7,
444     0x3e,
445     0x20,
446     0x42,
447     0xec,
448     0x43,
449     0x25,
450     0x01,
451     0x03,
452     0xe5,
453     0x4d,
454     0x83,
455     0x22,
456     0xf5,
457     0x8e,
458     0x3a,
459     0x1a,
460     0x1b,
461     0xd4,
462     0x1c,
463     0xda,
464     0x6b,
465     0x9d,
466     0x10,
467     0x1b,
468     0xee,
469     0x67,
470     0x4e,
471     0x1f,
472     0x69,
473     0xab,
474     0xbc,
475     0xaa,
476     0x62,
477     0x8e,
478     0x9e,
479     0xc6,
480     0xee,
481     0xd6,
482     0x09,
483     0xc0,
484     0xca,
485     0xe0,
486     0xaa,
487     0x9f,
488     0x07,
489     0xb2,
490     0xc2,
491     0xbb,
492     0x31,
493     0x96,
494     0xa2,
495     0x04,
496     0x62,
497     0xd3,
498     0x13,
499     0x32,
500     0x29,
501     0x67,
502     0x6e,
503     0xad,
504     0x2e,
505     0x0b,
506     0xea,
507     0x04,
508     0x7c,
509     0x8c,
510     0x5a,
511     0x5d,
512     0xac,
513     0x14,
514     0xaa,
515     0x61,
516     0x7f,
517     0x28,
518     0x6c,
519     0x2d,
520     0x64,
521     0x2d,
522     0xc3,
523     0xaf,
524     0x77,
525     0x52,
526     0x90,
527     0xb4,
528     0x37,
529     0xc0,
530     0x30,
531 };
532 
533 static const unsigned char CERT2[] = {
534     0x30,
535     0x82,
536     0x01,
537     0xed,
538     0x30,
539     0x82,
540     0x01,
541     0x56,
542     0xa0,
543     0x03,
544     0x02,
545     0x01,
546     0x02,
547     0x02,
548     0x09,
549     0x00,
550     0x8b,
551     0x4b,
552     0x5e,
553     0x6c,
554     0x03,
555     0x28,
556     0x4e,
557     0xe7,
558     0x30,
559     0x0d,
560     0x06,
561     0x09,
562     0x2a,
563     0x86,
564     0x48,
565     0x86,
566     0xf7,
567     0x0d,
568     0x01,
569     0x01,
570     0x0b,
571     0x05,
572     0x00,
573     0x30,
574     0x19,
575     0x31,
576     0x17,
577     0x30,
578     0x15,
579     0x06,
580     0x03,
581     0x55,
582     0x04,
583     0x03,
584     0x0c,
585     0x0e,
586     0x50,
587     0x31,
588     0x32,
589     0x54,
590     0x65,
591     0x73,
592     0x74,
593     0x2d,
594     0x52,
595     0x6f,
596     0x6f,
597     0x74,
598     0x2d,
599     0x41,
600     0x30,
601     0x1e,
602     0x17,
603     0x0d,
604     0x31,
605     0x39,
606     0x30,
607     0x39,
608     0x33,
609     0x30,
610     0x30,
611     0x30,
612     0x34,
613     0x36,
614     0x35,
615     0x36,
616     0x5a,
617     0x17,
618     0x0d,
619     0x32,
620     0x39,
621     0x30,
622     0x39,
623     0x32,
624     0x37,
625     0x30,
626     0x30,
627     0x34,
628     0x36,
629     0x35,
630     0x36,
631     0x5a,
632     0x30,
633     0x1b,
634     0x31,
635     0x19,
636     0x30,
637     0x17,
638     0x06,
639     0x03,
640     0x55,
641     0x04,
642     0x03,
643     0x0c,
644     0x10,
645     0x50,
646     0x31,
647     0x32,
648     0x54,
649     0x65,
650     0x73,
651     0x74,
652     0x2d,
653     0x43,
654     0x6c,
655     0x69,
656     0x65,
657     0x6e,
658     0x74,
659     0x2d,
660     0x31,
661     0x30,
662     0x81,
663     0x9f,
664     0x30,
665     0x0d,
666     0x06,
667     0x09,
668     0x2a,
669     0x86,
670     0x48,
671     0x86,
672     0xf7,
673     0x0d,
674     0x01,
675     0x01,
676     0x01,
677     0x05,
678     0x00,
679     0x03,
680     0x81,
681     0x8d,
682     0x00,
683     0x30,
684     0x81,
685     0x89,
686     0x02,
687     0x81,
688     0x81,
689     0x00,
690     0xa8,
691     0x6e,
692     0x40,
693     0x86,
694     0x9f,
695     0x98,
696     0x59,
697     0xfb,
698     0x57,
699     0xbf,
700     0xc1,
701     0x55,
702     0x12,
703     0x38,
704     0xeb,
705     0xb3,
706     0x46,
707     0x34,
708     0xc9,
709     0x35,
710     0x4d,
711     0xfd,
712     0x03,
713     0xe9,
714     0x3a,
715     0x88,
716     0x9e,
717     0x97,
718     0x8f,
719     0xf4,
720     0xec,
721     0x36,
722     0x7b,
723     0x3f,
724     0xba,
725     0xb8,
726     0xa5,
727     0x96,
728     0x30,
729     0x03,
730     0xc5,
731     0xc6,
732     0xd9,
733     0xa8,
734     0x4e,
735     0xbc,
736     0x23,
737     0x51,
738     0xa1,
739     0x96,
740     0xd2,
741     0x03,
742     0x98,
743     0x73,
744     0xb6,
745     0x17,
746     0x9c,
747     0x77,
748     0xd4,
749     0x95,
750     0x1e,
751     0x1b,
752     0xb3,
753     0x1b,
754     0xc8,
755     0x71,
756     0xd1,
757     0x2e,
758     0x31,
759     0xc7,
760     0x6a,
761     0x75,
762     0x57,
763     0x08,
764     0x7f,
765     0xba,
766     0x70,
767     0x76,
768     0xf7,
769     0x67,
770     0xf4,
771     0x4e,
772     0xbe,
773     0xfc,
774     0x70,
775     0x61,
776     0x41,
777     0x07,
778     0x2b,
779     0x7c,
780     0x3c,
781     0x3b,
782     0xb3,
783     0xbc,
784     0xd5,
785     0xa8,
786     0xbd,
787     0x28,
788     0xd8,
789     0x49,
790     0xd3,
791     0xe1,
792     0x78,
793     0xc8,
794     0xc1,
795     0x42,
796     0x5e,
797     0x18,
798     0x36,
799     0xa8,
800     0x41,
801     0xf7,
802     0xc8,
803     0xaa,
804     0x35,
805     0xfe,
806     0x2d,
807     0xd1,
808     0xb4,
809     0xcc,
810     0x00,
811     0x67,
812     0xae,
813     0x79,
814     0xd3,
815     0x28,
816     0xd5,
817     0x5b,
818     0x02,
819     0x03,
820     0x01,
821     0x00,
822     0x01,
823     0xa3,
824     0x3b,
825     0x30,
826     0x39,
827     0x30,
828     0x1f,
829     0x06,
830     0x03,
831     0x55,
832     0x1d,
833     0x23,
834     0x04,
835     0x18,
836     0x30,
837     0x16,
838     0x80,
839     0x14,
840     0xdb,
841     0xbb,
842     0xb8,
843     0x92,
844     0x4e,
845     0x24,
846     0x0b,
847     0x1b,
848     0xbb,
849     0x78,
850     0x33,
851     0xf9,
852     0x01,
853     0x02,
854     0x23,
855     0x0d,
856     0x96,
857     0x18,
858     0x30,
859     0x47,
860     0x30,
861     0x09,
862     0x06,
863     0x03,
864     0x55,
865     0x1d,
866     0x13,
867     0x04,
868     0x02,
869     0x30,
870     0x00,
871     0x30,
872     0x0b,
873     0x06,
874     0x03,
875     0x55,
876     0x1d,
877     0x0f,
878     0x04,
879     0x04,
880     0x03,
881     0x02,
882     0x04,
883     0xf0,
884     0x30,
885     0x0d,
886     0x06,
887     0x09,
888     0x2a,
889     0x86,
890     0x48,
891     0x86,
892     0xf7,
893     0x0d,
894     0x01,
895     0x01,
896     0x0b,
897     0x05,
898     0x00,
899     0x03,
900     0x81,
901     0x81,
902     0x00,
903     0x3b,
904     0xa6,
905     0x73,
906     0xbe,
907     0xe0,
908     0x28,
909     0xed,
910     0x1f,
911     0x29,
912     0x78,
913     0x4c,
914     0xc0,
915     0x1f,
916     0xe9,
917     0x85,
918     0xc6,
919     0x8f,
920     0xe3,
921     0x87,
922     0x7c,
923     0xd9,
924     0xe7,
925     0x0a,
926     0x37,
927     0xe8,
928     0xaa,
929     0xb5,
930     0xd2,
931     0x7f,
932     0xf8,
933     0x90,
934     0x20,
935     0x80,
936     0x35,
937     0xa7,
938     0x79,
939     0x2b,
940     0x04,
941     0xa7,
942     0xbf,
943     0xe6,
944     0x7b,
945     0x58,
946     0xcb,
947     0xec,
948     0x0e,
949     0x58,
950     0xef,
951     0x2a,
952     0x70,
953     0x8a,
954     0x56,
955     0x8a,
956     0xcf,
957     0x6b,
958     0x7a,
959     0x74,
960     0x0c,
961     0xf4,
962     0x15,
963     0x37,
964     0x93,
965     0xcd,
966     0xe6,
967     0xb2,
968     0xa1,
969     0x83,
970     0x09,
971     0xdb,
972     0x9e,
973     0x4f,
974     0xff,
975     0x6a,
976     0x17,
977     0x4f,
978     0x33,
979     0xc9,
980     0xcc,
981     0x90,
982     0x2a,
983     0x67,
984     0xff,
985     0x16,
986     0x78,
987     0xa8,
988     0x2c,
989     0x10,
990     0xe0,
991     0x52,
992     0x8c,
993     0xe6,
994     0xe9,
995     0x90,
996     0x8d,
997     0xe0,
998     0x62,
999     0x04,
1000     0x9a,
1001     0x0f,
1002     0x44,
1003     0x01,
1004     0x82,
1005     0x14,
1006     0x92,
1007     0x44,
1008     0x25,
1009     0x69,
1010     0x22,
1011     0xb7,
1012     0xb8,
1013     0xc5,
1014     0x94,
1015     0x4c,
1016     0x4b,
1017     0x1c,
1018     0x9b,
1019     0x92,
1020     0x60,
1021     0x66,
1022     0x90,
1023     0x4e,
1024     0xb9,
1025     0xa8,
1026     0x4c,
1027     0x89,
1028     0xbb,
1029     0x0f,
1030     0x0b,
1031 };
1032 
1033 static const unsigned char KEY1[] = {
1034     0x30,
1035     0x82,
1036     0x02,
1037     0x5d,
1038     0x02,
1039     0x01,
1040     0x00,
1041     0x02,
1042     0x81,
1043     0x81,
1044     0x00,
1045     0xbc,
1046     0xdc,
1047     0x6f,
1048     0x8c,
1049     0x7a,
1050     0x2a,
1051     0x4b,
1052     0xea,
1053     0x66,
1054     0x66,
1055     0x04,
1056     0xa9,
1057     0x05,
1058     0x92,
1059     0x53,
1060     0xd7,
1061     0x13,
1062     0x3c,
1063     0x49,
1064     0xe1,
1065     0xc8,
1066     0xbb,
1067     0xdf,
1068     0x3d,
1069     0xcb,
1070     0x88,
1071     0x31,
1072     0x07,
1073     0x20,
1074     0x59,
1075     0x93,
1076     0x24,
1077     0x7f,
1078     0x7d,
1079     0xc6,
1080     0x84,
1081     0x81,
1082     0x16,
1083     0x64,
1084     0x4a,
1085     0x52,
1086     0xa6,
1087     0x30,
1088     0x44,
1089     0xdc,
1090     0x1a,
1091     0x30,
1092     0xde,
1093     0xae,
1094     0x29,
1095     0x18,
1096     0xcf,
1097     0xc7,
1098     0xf3,
1099     0xcf,
1100     0x0c,
1101     0xb7,
1102     0x8e,
1103     0x2b,
1104     0x1e,
1105     0x21,
1106     0x01,
1107     0x0b,
1108     0xfb,
1109     0xe5,
1110     0xe6,
1111     0xcf,
1112     0x2b,
1113     0x84,
1114     0xe1,
1115     0x33,
1116     0xf8,
1117     0xba,
1118     0x02,
1119     0xfc,
1120     0x30,
1121     0xfa,
1122     0xc4,
1123     0x33,
1124     0xc7,
1125     0x37,
1126     0xc6,
1127     0x7f,
1128     0x72,
1129     0x31,
1130     0x92,
1131     0x1d,
1132     0x8f,
1133     0xa0,
1134     0xfb,
1135     0xe5,
1136     0x4a,
1137     0x08,
1138     0x31,
1139     0x78,
1140     0x80,
1141     0x9c,
1142     0x23,
1143     0xb4,
1144     0xe9,
1145     0x19,
1146     0x56,
1147     0x04,
1148     0xfa,
1149     0x0d,
1150     0x07,
1151     0x04,
1152     0xb7,
1153     0x43,
1154     0xac,
1155     0x4c,
1156     0x49,
1157     0x7c,
1158     0xc2,
1159     0xa1,
1160     0x44,
1161     0xc1,
1162     0x48,
1163     0x7d,
1164     0x28,
1165     0xe5,
1166     0x23,
1167     0x66,
1168     0x07,
1169     0x22,
1170     0xd5,
1171     0xf0,
1172     0xf1,
1173     0x02,
1174     0x03,
1175     0x01,
1176     0x00,
1177     0x01,
1178     0x02,
1179     0x81,
1180     0x81,
1181     0x00,
1182     0xa5,
1183     0x6d,
1184     0xf9,
1185     0x8f,
1186     0xf5,
1187     0x5a,
1188     0xa3,
1189     0x50,
1190     0xd9,
1191     0x0d,
1192     0x37,
1193     0xbb,
1194     0xce,
1195     0x13,
1196     0x94,
1197     0xb8,
1198     0xea,
1199     0x32,
1200     0x7f,
1201     0x0c,
1202     0xf5,
1203     0x46,
1204     0x0b,
1205     0x90,
1206     0x17,
1207     0x7e,
1208     0x5e,
1209     0x63,
1210     0xbd,
1211     0xa4,
1212     0x78,
1213     0xcd,
1214     0x19,
1215     0x97,
1216     0xd4,
1217     0x92,
1218     0x30,
1219     0x78,
1220     0xaa,
1221     0xb4,
1222     0xa7,
1223     0x9c,
1224     0xc6,
1225     0xdf,
1226     0x2a,
1227     0x65,
1228     0x0e,
1229     0xb5,
1230     0x9f,
1231     0x9c,
1232     0x84,
1233     0x0d,
1234     0x4d,
1235     0x3a,
1236     0x74,
1237     0xfc,
1238     0xd0,
1239     0xb4,
1240     0x09,
1241     0x74,
1242     0xc4,
1243     0xb8,
1244     0x24,
1245     0x03,
1246     0xa8,
1247     0xf0,
1248     0xf8,
1249     0x0d,
1250     0x5c,
1251     0x8e,
1252     0xdf,
1253     0x4b,
1254     0xe1,
1255     0x0a,
1256     0x8f,
1257     0x4f,
1258     0xd5,
1259     0xc7,
1260     0x9b,
1261     0x54,
1262     0x55,
1263     0x8f,
1264     0x00,
1265     0x5c,
1266     0xea,
1267     0x4c,
1268     0x73,
1269     0xf9,
1270     0x1b,
1271     0xbf,
1272     0xb8,
1273     0x93,
1274     0x33,
1275     0x20,
1276     0xce,
1277     0x45,
1278     0xd9,
1279     0x03,
1280     0x02,
1281     0xb2,
1282     0x36,
1283     0xc5,
1284     0x0a,
1285     0x30,
1286     0x50,
1287     0x78,
1288     0x80,
1289     0x66,
1290     0x00,
1291     0x22,
1292     0x38,
1293     0x86,
1294     0xcf,
1295     0x63,
1296     0x4a,
1297     0x5c,
1298     0xbf,
1299     0x2b,
1300     0xd9,
1301     0x6e,
1302     0xe6,
1303     0xf0,
1304     0x39,
1305     0xad,
1306     0x12,
1307     0x25,
1308     0x41,
1309     0xb9,
1310     0x02,
1311     0x41,
1312     0x00,
1313     0xf3,
1314     0x7c,
1315     0x07,
1316     0x99,
1317     0x64,
1318     0x3a,
1319     0x28,
1320     0x8c,
1321     0x8d,
1322     0x05,
1323     0xfe,
1324     0x32,
1325     0xb5,
1326     0x4c,
1327     0x8c,
1328     0x6d,
1329     0xde,
1330     0x3d,
1331     0x16,
1332     0x08,
1333     0xa0,
1334     0x01,
1335     0x61,
1336     0x4f,
1337     0x8e,
1338     0xa0,
1339     0xf7,
1340     0x26,
1341     0x26,
1342     0xb5,
1343     0x8e,
1344     0xc0,
1345     0x7a,
1346     0xce,
1347     0x86,
1348     0x34,
1349     0xde,
1350     0xb8,
1351     0xef,
1352     0x86,
1353     0x01,
1354     0xbe,
1355     0x24,
1356     0xaa,
1357     0x9b,
1358     0x36,
1359     0x93,
1360     0x72,
1361     0x9b,
1362     0xf9,
1363     0xc6,
1364     0xcb,
1365     0x76,
1366     0x84,
1367     0x67,
1368     0x06,
1369     0x06,
1370     0x30,
1371     0x50,
1372     0xdf,
1373     0x42,
1374     0x17,
1375     0xe0,
1376     0xa7,
1377     0x02,
1378     0x41,
1379     0x00,
1380     0xc6,
1381     0x91,
1382     0xa0,
1383     0x41,
1384     0x34,
1385     0x11,
1386     0x67,
1387     0x4b,
1388     0x08,
1389     0x0f,
1390     0xda,
1391     0xa7,
1392     0x99,
1393     0xec,
1394     0x58,
1395     0x11,
1396     0xa5,
1397     0x82,
1398     0xdb,
1399     0x50,
1400     0xfe,
1401     0x77,
1402     0xe2,
1403     0xd1,
1404     0x53,
1405     0x9c,
1406     0x7d,
1407     0xe8,
1408     0xbf,
1409     0xe7,
1410     0x7c,
1411     0xa9,
1412     0x01,
1413     0xb1,
1414     0x87,
1415     0xc3,
1416     0x52,
1417     0x79,
1418     0x9e,
1419     0x2c,
1420     0xa7,
1421     0x6f,
1422     0x02,
1423     0x37,
1424     0x32,
1425     0xef,
1426     0x24,
1427     0x31,
1428     0x21,
1429     0x0b,
1430     0x86,
1431     0x05,
1432     0x32,
1433     0x4a,
1434     0x2e,
1435     0x0b,
1436     0x65,
1437     0x05,
1438     0xd3,
1439     0xd6,
1440     0x30,
1441     0xb2,
1442     0xfc,
1443     0xa7,
1444     0x02,
1445     0x41,
1446     0x00,
1447     0xc2,
1448     0xed,
1449     0x31,
1450     0xdc,
1451     0x40,
1452     0x9c,
1453     0x3a,
1454     0xe8,
1455     0x42,
1456     0xe2,
1457     0x60,
1458     0x5e,
1459     0x52,
1460     0x3c,
1461     0xc5,
1462     0x54,
1463     0x14,
1464     0x0e,
1465     0x8d,
1466     0x7c,
1467     0x3c,
1468     0x34,
1469     0xbe,
1470     0xa6,
1471     0x05,
1472     0x86,
1473     0xa2,
1474     0x36,
1475     0x5d,
1476     0xd9,
1477     0x0e,
1478     0x3e,
1479     0xd4,
1480     0x52,
1481     0x50,
1482     0xa9,
1483     0x35,
1484     0x01,
1485     0x93,
1486     0x68,
1487     0x92,
1488     0x2e,
1489     0x9a,
1490     0x86,
1491     0x27,
1492     0x1a,
1493     0xab,
1494     0x32,
1495     0x9e,
1496     0xe2,
1497     0x79,
1498     0x9f,
1499     0x5b,
1500     0xf3,
1501     0xa5,
1502     0xd2,
1503     0xf1,
1504     0xd3,
1505     0x6e,
1506     0x7b,
1507     0x3e,
1508     0x1b,
1509     0x85,
1510     0x93,
1511     0x02,
1512     0x40,
1513     0x68,
1514     0xb8,
1515     0xb6,
1516     0x7e,
1517     0x8c,
1518     0xba,
1519     0x3c,
1520     0xf2,
1521     0x8a,
1522     0x2e,
1523     0xea,
1524     0x4f,
1525     0x07,
1526     0xd3,
1527     0x68,
1528     0x62,
1529     0xee,
1530     0x1a,
1531     0x04,
1532     0x16,
1533     0x44,
1534     0x0d,
1535     0xef,
1536     0xf6,
1537     0x1b,
1538     0x95,
1539     0x65,
1540     0xa5,
1541     0xd1,
1542     0x47,
1543     0x81,
1544     0x2c,
1545     0x14,
1546     0xb3,
1547     0x8e,
1548     0xf9,
1549     0x08,
1550     0xcf,
1551     0x11,
1552     0x07,
1553     0x55,
1554     0xca,
1555     0x2a,
1556     0xad,
1557     0xf7,
1558     0xd3,
1559     0xbd,
1560     0x0f,
1561     0x97,
1562     0xf0,
1563     0xde,
1564     0xde,
1565     0x70,
1566     0xb6,
1567     0x44,
1568     0x70,
1569     0x47,
1570     0xf7,
1571     0xf9,
1572     0xcf,
1573     0x75,
1574     0x61,
1575     0x7f,
1576     0xf3,
1577     0x02,
1578     0x40,
1579     0x38,
1580     0x4a,
1581     0x67,
1582     0xaf,
1583     0xae,
1584     0xb6,
1585     0xb2,
1586     0x6a,
1587     0x00,
1588     0x25,
1589     0x5a,
1590     0xa4,
1591     0x65,
1592     0x20,
1593     0xb1,
1594     0x13,
1595     0xbd,
1596     0x83,
1597     0xff,
1598     0xb4,
1599     0xbc,
1600     0xf4,
1601     0xdd,
1602     0xa1,
1603     0xbb,
1604     0x1c,
1605     0x96,
1606     0x37,
1607     0x35,
1608     0xf4,
1609     0xbf,
1610     0xed,
1611     0x4c,
1612     0xed,
1613     0x92,
1614     0xe8,
1615     0xac,
1616     0xc9,
1617     0xc1,
1618     0xa5,
1619     0xa3,
1620     0x23,
1621     0x66,
1622     0x40,
1623     0x8a,
1624     0xa1,
1625     0xe6,
1626     0xe3,
1627     0x95,
1628     0xfe,
1629     0xc4,
1630     0x53,
1631     0xf5,
1632     0x7d,
1633     0x6e,
1634     0xca,
1635     0x45,
1636     0x42,
1637     0xe4,
1638     0xc2,
1639     0x9f,
1640     0xe5,
1641     0x1e,
1642     0xb5,
1643 };
1644 
1645 static const unsigned char KEY2[] = {
1646     0x30,
1647     0x82,
1648     0x02,
1649     0x5c,
1650     0x02,
1651     0x01,
1652     0x00,
1653     0x02,
1654     0x81,
1655     0x81,
1656     0x00,
1657     0xa8,
1658     0x6e,
1659     0x40,
1660     0x86,
1661     0x9f,
1662     0x98,
1663     0x59,
1664     0xfb,
1665     0x57,
1666     0xbf,
1667     0xc1,
1668     0x55,
1669     0x12,
1670     0x38,
1671     0xeb,
1672     0xb3,
1673     0x46,
1674     0x34,
1675     0xc9,
1676     0x35,
1677     0x4d,
1678     0xfd,
1679     0x03,
1680     0xe9,
1681     0x3a,
1682     0x88,
1683     0x9e,
1684     0x97,
1685     0x8f,
1686     0xf4,
1687     0xec,
1688     0x36,
1689     0x7b,
1690     0x3f,
1691     0xba,
1692     0xb8,
1693     0xa5,
1694     0x96,
1695     0x30,
1696     0x03,
1697     0xc5,
1698     0xc6,
1699     0xd9,
1700     0xa8,
1701     0x4e,
1702     0xbc,
1703     0x23,
1704     0x51,
1705     0xa1,
1706     0x96,
1707     0xd2,
1708     0x03,
1709     0x98,
1710     0x73,
1711     0xb6,
1712     0x17,
1713     0x9c,
1714     0x77,
1715     0xd4,
1716     0x95,
1717     0x1e,
1718     0x1b,
1719     0xb3,
1720     0x1b,
1721     0xc8,
1722     0x71,
1723     0xd1,
1724     0x2e,
1725     0x31,
1726     0xc7,
1727     0x6a,
1728     0x75,
1729     0x57,
1730     0x08,
1731     0x7f,
1732     0xba,
1733     0x70,
1734     0x76,
1735     0xf7,
1736     0x67,
1737     0xf4,
1738     0x4e,
1739     0xbe,
1740     0xfc,
1741     0x70,
1742     0x61,
1743     0x41,
1744     0x07,
1745     0x2b,
1746     0x7c,
1747     0x3c,
1748     0x3b,
1749     0xb3,
1750     0xbc,
1751     0xd5,
1752     0xa8,
1753     0xbd,
1754     0x28,
1755     0xd8,
1756     0x49,
1757     0xd3,
1758     0xe1,
1759     0x78,
1760     0xc8,
1761     0xc1,
1762     0x42,
1763     0x5e,
1764     0x18,
1765     0x36,
1766     0xa8,
1767     0x41,
1768     0xf7,
1769     0xc8,
1770     0xaa,
1771     0x35,
1772     0xfe,
1773     0x2d,
1774     0xd1,
1775     0xb4,
1776     0xcc,
1777     0x00,
1778     0x67,
1779     0xae,
1780     0x79,
1781     0xd3,
1782     0x28,
1783     0xd5,
1784     0x5b,
1785     0x02,
1786     0x03,
1787     0x01,
1788     0x00,
1789     0x01,
1790     0x02,
1791     0x81,
1792     0x81,
1793     0x00,
1794     0xa6,
1795     0x00,
1796     0x83,
1797     0xf8,
1798     0x2b,
1799     0x33,
1800     0xac,
1801     0xfb,
1802     0xdb,
1803     0xf0,
1804     0x52,
1805     0x4b,
1806     0xd6,
1807     0x39,
1808     0xe3,
1809     0x94,
1810     0x3d,
1811     0x8d,
1812     0xa9,
1813     0x01,
1814     0xb0,
1815     0x6b,
1816     0xbe,
1817     0x7f,
1818     0x10,
1819     0x01,
1820     0xb6,
1821     0xcd,
1822     0x0a,
1823     0x45,
1824     0x0a,
1825     0xca,
1826     0x67,
1827     0x8e,
1828     0xd8,
1829     0x29,
1830     0x44,
1831     0x8a,
1832     0x51,
1833     0xa8,
1834     0x66,
1835     0x35,
1836     0x26,
1837     0x30,
1838     0x8b,
1839     0xe9,
1840     0x41,
1841     0xa6,
1842     0x22,
1843     0xec,
1844     0xd2,
1845     0xf0,
1846     0x58,
1847     0x41,
1848     0x33,
1849     0x26,
1850     0xf2,
1851     0x3f,
1852     0xe8,
1853     0x75,
1854     0x4f,
1855     0xc7,
1856     0x5d,
1857     0x2e,
1858     0x5a,
1859     0xa8,
1860     0x7a,
1861     0xd2,
1862     0xbf,
1863     0x59,
1864     0xa0,
1865     0x86,
1866     0x79,
1867     0x0b,
1868     0x92,
1869     0x6c,
1870     0x95,
1871     0x5d,
1872     0x87,
1873     0x63,
1874     0x5c,
1875     0xd6,
1876     0x1a,
1877     0xc0,
1878     0xf6,
1879     0x7a,
1880     0x15,
1881     0x8d,
1882     0xc7,
1883     0x3c,
1884     0xb6,
1885     0x9e,
1886     0xa6,
1887     0x58,
1888     0x46,
1889     0x9b,
1890     0xbf,
1891     0x3e,
1892     0x28,
1893     0x8c,
1894     0xdf,
1895     0x1a,
1896     0x87,
1897     0xaa,
1898     0x7e,
1899     0xf5,
1900     0xf2,
1901     0xcb,
1902     0x5e,
1903     0x84,
1904     0x2d,
1905     0xf6,
1906     0x82,
1907     0x7e,
1908     0x89,
1909     0x4e,
1910     0xf5,
1911     0xe6,
1912     0x3c,
1913     0x92,
1914     0x80,
1915     0x1e,
1916     0x98,
1917     0x1c,
1918     0x6a,
1919     0x7b,
1920     0x57,
1921     0x01,
1922     0x02,
1923     0x41,
1924     0x00,
1925     0xdd,
1926     0x60,
1927     0x95,
1928     0xd7,
1929     0xa1,
1930     0x9d,
1931     0x0c,
1932     0xa1,
1933     0x84,
1934     0xc5,
1935     0x39,
1936     0xca,
1937     0x67,
1938     0x4c,
1939     0x1c,
1940     0x06,
1941     0x71,
1942     0x5b,
1943     0x5c,
1944     0x2d,
1945     0x8d,
1946     0xce,
1947     0xcd,
1948     0xe2,
1949     0x79,
1950     0xc8,
1951     0x33,
1952     0xbe,
1953     0x50,
1954     0x37,
1955     0x60,
1956     0x9f,
1957     0x3b,
1958     0xb9,
1959     0x59,
1960     0x55,
1961     0x22,
1962     0x1f,
1963     0xa5,
1964     0x4b,
1965     0x1d,
1966     0xca,
1967     0x38,
1968     0xa0,
1969     0xab,
1970     0x87,
1971     0x9c,
1972     0x86,
1973     0x0e,
1974     0xdb,
1975     0x1c,
1976     0x4f,
1977     0x4f,
1978     0x07,
1979     0xed,
1980     0x18,
1981     0x3f,
1982     0x05,
1983     0x3c,
1984     0xec,
1985     0x78,
1986     0x11,
1987     0xf6,
1988     0x99,
1989     0x02,
1990     0x41,
1991     0x00,
1992     0xc2,
1993     0xc5,
1994     0xcf,
1995     0xbe,
1996     0x95,
1997     0x91,
1998     0xeb,
1999     0xcf,
2000     0x47,
2001     0xf3,
2002     0x33,
2003     0x32,
2004     0xc7,
2005     0x7e,
2006     0x93,
2007     0x56,
2008     0xf7,
2009     0xd8,
2010     0xf9,
2011     0xd4,
2012     0xb6,
2013     0xd6,
2014     0x20,
2015     0xac,
2016     0xba,
2017     0x8a,
2018     0x20,
2019     0x19,
2020     0x14,
2021     0xab,
2022     0xc5,
2023     0x5d,
2024     0xb2,
2025     0x08,
2026     0xcc,
2027     0x77,
2028     0x7c,
2029     0x65,
2030     0xa8,
2031     0xdb,
2032     0x66,
2033     0x97,
2034     0x36,
2035     0x44,
2036     0x2c,
2037     0x63,
2038     0xc0,
2039     0x6a,
2040     0x7e,
2041     0xb0,
2042     0x0b,
2043     0x5c,
2044     0x90,
2045     0x12,
2046     0x50,
2047     0xb4,
2048     0x36,
2049     0x60,
2050     0xc3,
2051     0x1f,
2052     0x22,
2053     0x0c,
2054     0xc8,
2055     0x13,
2056     0x02,
2057     0x40,
2058     0x33,
2059     0xc8,
2060     0x7e,
2061     0x04,
2062     0x7c,
2063     0x97,
2064     0x61,
2065     0xf6,
2066     0xfe,
2067     0x39,
2068     0xac,
2069     0x34,
2070     0xfe,
2071     0x48,
2072     0xbd,
2073     0x5d,
2074     0x7c,
2075     0x72,
2076     0xa4,
2077     0x73,
2078     0x3b,
2079     0x72,
2080     0x9e,
2081     0x92,
2082     0x55,
2083     0x6e,
2084     0x51,
2085     0x3c,
2086     0x39,
2087     0x43,
2088     0x5a,
2089     0xe4,
2090     0xa4,
2091     0x71,
2092     0xcc,
2093     0xc5,
2094     0xaf,
2095     0x3f,
2096     0xbb,
2097     0xc8,
2098     0x80,
2099     0x65,
2100     0x67,
2101     0x2d,
2102     0x9e,
2103     0x32,
2104     0x10,
2105     0x99,
2106     0x03,
2107     0x2c,
2108     0x99,
2109     0xc8,
2110     0xab,
2111     0x71,
2112     0xed,
2113     0x31,
2114     0xf8,
2115     0xbb,
2116     0xde,
2117     0xee,
2118     0x69,
2119     0x7f,
2120     0xba,
2121     0x31,
2122     0x02,
2123     0x40,
2124     0x7e,
2125     0xbc,
2126     0x60,
2127     0x55,
2128     0x4e,
2129     0xd5,
2130     0xc8,
2131     0x6e,
2132     0xf4,
2133     0x0e,
2134     0x57,
2135     0xbe,
2136     0x2e,
2137     0xf9,
2138     0x39,
2139     0xbe,
2140     0x59,
2141     0x3f,
2142     0xa2,
2143     0x30,
2144     0xbb,
2145     0x57,
2146     0xd1,
2147     0xa3,
2148     0x13,
2149     0x2e,
2150     0x55,
2151     0x7c,
2152     0x7c,
2153     0x6a,
2154     0xd8,
2155     0xde,
2156     0x02,
2157     0xbe,
2158     0x9e,
2159     0xed,
2160     0x10,
2161     0xd0,
2162     0xc5,
2163     0x73,
2164     0x1d,
2165     0xea,
2166     0x3e,
2167     0xb1,
2168     0x55,
2169     0x81,
2170     0x02,
2171     0xef,
2172     0x48,
2173     0xc8,
2174     0x1c,
2175     0x5c,
2176     0x7a,
2177     0x92,
2178     0xb0,
2179     0x58,
2180     0xd3,
2181     0x19,
2182     0x5b,
2183     0x5d,
2184     0xa2,
2185     0xb6,
2186     0x56,
2187     0x69,
2188     0x02,
2189     0x40,
2190     0x1e,
2191     0x00,
2192     0x6a,
2193     0x9f,
2194     0xba,
2195     0xee,
2196     0x46,
2197     0x5a,
2198     0xc5,
2199     0xb5,
2200     0x9f,
2201     0x91,
2202     0x33,
2203     0xdd,
2204     0xc9,
2205     0x96,
2206     0x75,
2207     0xb7,
2208     0x87,
2209     0xcf,
2210     0x18,
2211     0x1c,
2212     0xb7,
2213     0xb9,
2214     0x3f,
2215     0x04,
2216     0x10,
2217     0xb8,
2218     0x75,
2219     0xa9,
2220     0xb8,
2221     0xa0,
2222     0x31,
2223     0x35,
2224     0x03,
2225     0x30,
2226     0x89,
2227     0xc8,
2228     0x37,
2229     0x68,
2230     0x20,
2231     0x30,
2232     0x99,
2233     0x39,
2234     0x96,
2235     0xd6,
2236     0x2b,
2237     0x3d,
2238     0x5e,
2239     0x45,
2240     0x84,
2241     0xf7,
2242     0xd2,
2243     0x61,
2244     0x50,
2245     0xc9,
2246     0x50,
2247     0xba,
2248     0x8d,
2249     0x08,
2250     0xaa,
2251     0xd0,
2252     0x08,
2253     0x1e,
2254 };
2255 
2256 static const PKCS12_ATTR ATTRS1[] = {
2257     { "friendlyName", "george" },
2258     { "localKeyID", "1234567890" },
2259     { "1.2.3.4.5", "MyCustomAttribute" },
2260     { NULL, NULL }
2261 };
2262 
2263 static const PKCS12_ATTR ATTRS2[] = {
2264     { "friendlyName", "janet" },
2265     { "localKeyID", "987654321" },
2266     { "1.2.3.5.8.13", "AnotherCustomAttribute" },
2267     { NULL, NULL }
2268 };
2269 
2270 static const PKCS12_ATTR ATTRS3[] = {
2271     { "friendlyName", "wildduk" },
2272     { "localKeyID", "1122334455" },
2273     { "oracle-jdk-trustedkeyusage", "anyExtendedKeyUsage" },
2274     { NULL, NULL }
2275 };
2276 
2277 static const PKCS12_ATTR ATTRS4[] = {
2278     { "friendlyName", "wildduk" },
2279     { "localKeyID", "1122334455" },
2280     { NULL, NULL }
2281 };
2282 
2283 static const PKCS12_ENC enc_default = {
2284 #ifndef OPENSSL_NO_DES
2285     NID_pbe_WithSHA1And3_Key_TripleDES_CBC,
2286 #else
2287     NID_aes_128_cbc,
2288 #endif
2289     "Password1",
2290     1000
2291 };
2292 
2293 static const PKCS12_ENC mac_default = {
2294     NID_sha1,
2295     "Password1",
2296     1000
2297 };
2298 
2299 static const int enc_nids_all[] = {
2300     /* NOTE: To use PBES2 we pass the desired cipher NID instead of NID_pbes2 */
2301     NID_aes_128_cbc,
2302     NID_aes_256_cbc,
2303 #ifndef OPENSSL_NO_DES
2304     NID_des_ede3_cbc,
2305     NID_des_cbc,
2306 #endif
2307 #ifndef OPENSSL_NO_RC5
2308     NID_rc5_cbc,
2309 #endif
2310 #ifndef OPENSSL_NO_RC4
2311     NID_rc4,
2312 #endif
2313 #ifndef OPENSSL_NO_RC2
2314     NID_rc2_cbc,
2315 #endif
2316 
2317 #ifndef OPENSSL_NO_MD2
2318 #ifndef OPENSSL_NO_DES
2319     NID_pbeWithMD2AndDES_CBC,
2320 #endif
2321 #ifndef OPENSSL_NO_RC2
2322     NID_pbeWithMD2AndRC2_CBC,
2323 #endif
2324 #endif
2325 
2326 #ifndef OPENSSL_NO_MD5
2327 #ifndef OPENSSL_NO_DES
2328     NID_pbeWithMD5AndDES_CBC,
2329 #endif
2330 #ifndef OPENSSL_NO_RC2
2331     NID_pbeWithMD5AndRC2_CBC,
2332 #endif
2333 #endif
2334 #ifndef OPENSSL_NO_DES
2335     NID_pbeWithSHA1AndDES_CBC,
2336 #endif
2337 #ifndef OPENSSL_NO_RC2
2338     NID_pbe_WithSHA1And128BitRC2_CBC,
2339     NID_pbe_WithSHA1And40BitRC2_CBC,
2340     NID_pbeWithSHA1AndRC2_CBC,
2341 #endif
2342 #ifndef OPENSSL_NO_RC4
2343     NID_pbe_WithSHA1And128BitRC4,
2344     NID_pbe_WithSHA1And40BitRC4,
2345 #endif
2346 #ifndef OPENSSL_NO_DES
2347     NID_pbe_WithSHA1And2_Key_TripleDES_CBC,
2348     NID_pbe_WithSHA1And3_Key_TripleDES_CBC,
2349 #endif
2350 };
2351 
2352 static const int enc_nids_no_legacy[] = {
2353     /* NOTE: To use PBES2 we pass the desired cipher NID instead of NID_pbes2 */
2354     NID_aes_128_cbc,
2355     NID_aes_256_cbc,
2356 #ifndef OPENSSL_NO_DES
2357     NID_des_ede3_cbc,
2358     NID_pbe_WithSHA1And2_Key_TripleDES_CBC,
2359     NID_pbe_WithSHA1And3_Key_TripleDES_CBC,
2360 #endif
2361 };
2362 
2363 static const int mac_nids[] = {
2364     NID_sha1,
2365     NID_md5,
2366     NID_sha256,
2367     NID_sha512,
2368     NID_sha3_256,
2369     NID_sha3_512
2370 };
2371 
2372 static const int iters[] = {
2373     1,
2374     1000
2375 };
2376 
2377 static const char *passwords[] = {
2378     "Password1",
2379     "",
2380 };
2381 
2382 /* --------------------------------------------------------------------------
2383  * Local functions
2384  */
2385 
get_custom_oid(void)2386 static int get_custom_oid(void)
2387 {
2388     static int sec_nid = -1;
2389 
2390     if (sec_nid != -1)
2391         return sec_nid;
2392     if (!TEST_true(OBJ_create("1.3.5.7.9", "CustomSecretOID", "My custom secret OID")))
2393         return -1;
2394     return sec_nid = OBJ_txt2nid("CustomSecretOID");
2395 }
2396 
2397 /* --------------------------------------------------------------------------
2398  * PKCS12 format tests
2399  */
2400 
test_single_cert_no_attrs(void)2401 static int test_single_cert_no_attrs(void)
2402 {
2403     PKCS12_BUILDER *pb = new_pkcs12_builder("1cert.p12");
2404 
2405     /* Generate/encode */
2406     start_pkcs12(pb);
2407 
2408     start_contentinfo(pb);
2409 
2410     add_certbag(pb, CERT1, sizeof(CERT1), NULL);
2411 
2412     end_contentinfo(pb);
2413 
2414     end_pkcs12(pb);
2415 
2416     /* Read/decode */
2417     start_check_pkcs12(pb);
2418 
2419     start_check_contentinfo(pb);
2420 
2421     check_certbag(pb, CERT1, sizeof(CERT1), NULL);
2422 
2423     end_check_contentinfo(pb);
2424 
2425     end_check_pkcs12(pb);
2426 
2427     return end_pkcs12_builder(pb);
2428 }
2429 
test_single_key(PKCS12_ENC * enc)2430 static int test_single_key(PKCS12_ENC *enc)
2431 {
2432     char fname[80];
2433     PKCS12_BUILDER *pb;
2434 
2435     BIO_snprintf(fname, sizeof(fname), "1key_ciph-%s_iter-%d.p12",
2436         OBJ_nid2sn(enc->nid), enc->iter);
2437 
2438     pb = new_pkcs12_builder(fname);
2439 
2440     /* Generate/encode */
2441     start_pkcs12(pb);
2442 
2443     start_contentinfo(pb);
2444 
2445     add_keybag(pb, KEY1, sizeof(KEY1), NULL, enc);
2446 
2447     end_contentinfo(pb);
2448 
2449     end_pkcs12(pb);
2450 
2451     /* Read/decode */
2452     start_check_pkcs12(pb);
2453 
2454     start_check_contentinfo(pb);
2455 
2456     check_keybag(pb, KEY1, sizeof(KEY1), NULL, enc);
2457 
2458     end_check_contentinfo(pb);
2459 
2460     end_check_pkcs12(pb);
2461 
2462     return end_pkcs12_builder(pb);
2463 }
2464 
test_single_key_enc_alg(int z)2465 static int test_single_key_enc_alg(int z)
2466 {
2467     PKCS12_ENC enc;
2468 
2469     if (lgcyprov == NULL)
2470         enc.nid = enc_nids_no_legacy[z];
2471     else
2472         enc.nid = enc_nids_all[z];
2473     enc.pass = enc_default.pass;
2474     enc.iter = enc_default.iter;
2475 
2476     return test_single_key(&enc);
2477 }
2478 
test_single_key_enc_pass(int z)2479 static int test_single_key_enc_pass(int z)
2480 {
2481     PKCS12_ENC enc;
2482 
2483     enc.nid = enc_default.nid;
2484     enc.pass = passwords[z];
2485     enc.iter = enc_default.iter;
2486 
2487     return test_single_key(&enc);
2488 }
2489 
test_single_key_enc_iter(int z)2490 static int test_single_key_enc_iter(int z)
2491 {
2492     PKCS12_ENC enc;
2493 
2494     enc.nid = enc_default.nid;
2495     enc.pass = enc_default.pass;
2496     enc.iter = iters[z];
2497 
2498     return test_single_key(&enc);
2499 }
2500 
test_single_key_with_attrs(void)2501 static int test_single_key_with_attrs(void)
2502 {
2503     PKCS12_BUILDER *pb = new_pkcs12_builder("1keyattrs.p12");
2504 
2505     /* Generate/encode */
2506     start_pkcs12(pb);
2507 
2508     start_contentinfo(pb);
2509 
2510     add_keybag(pb, KEY1, sizeof(KEY1), ATTRS1, &enc_default);
2511 
2512     end_contentinfo(pb);
2513 
2514     end_pkcs12(pb);
2515 
2516     /* Read/decode */
2517     start_check_pkcs12(pb);
2518 
2519     start_check_contentinfo(pb);
2520 
2521     check_keybag(pb, KEY1, sizeof(KEY1), ATTRS1, &enc_default);
2522 
2523     end_check_contentinfo(pb);
2524 
2525     end_check_pkcs12(pb);
2526 
2527     return end_pkcs12_builder(pb);
2528 }
2529 
test_single_cert_mac(PKCS12_ENC * mac)2530 static int test_single_cert_mac(PKCS12_ENC *mac)
2531 {
2532     char fname[80];
2533     PKCS12_BUILDER *pb;
2534 
2535     BIO_snprintf(fname, sizeof(fname), "1cert_mac-%s_iter-%d.p12",
2536         OBJ_nid2sn(mac->nid), mac->iter);
2537 
2538     pb = new_pkcs12_builder(fname);
2539 
2540     /* Generate/encode */
2541     start_pkcs12(pb);
2542 
2543     start_contentinfo(pb);
2544 
2545     add_certbag(pb, CERT1, sizeof(CERT1), NULL);
2546 
2547     end_contentinfo(pb);
2548 
2549     end_pkcs12_with_mac(pb, mac);
2550 
2551     /* Read/decode */
2552     start_check_pkcs12_with_mac(pb, mac);
2553 
2554     start_check_contentinfo(pb);
2555 
2556     check_certbag(pb, CERT1, sizeof(CERT1), NULL);
2557 
2558     end_check_contentinfo(pb);
2559 
2560     end_check_pkcs12(pb);
2561 
2562     return end_pkcs12_builder(pb);
2563 }
2564 
test_single_cert_mac_alg(int z)2565 static int test_single_cert_mac_alg(int z)
2566 {
2567     PKCS12_ENC mac;
2568 
2569     mac.nid = mac_nids[z];
2570     mac.pass = mac_default.pass;
2571     mac.iter = mac_default.iter;
2572 
2573     return test_single_cert_mac(&mac);
2574 }
2575 
test_single_cert_mac_pass(int z)2576 static int test_single_cert_mac_pass(int z)
2577 {
2578     PKCS12_ENC mac;
2579 
2580     mac.nid = mac_default.nid;
2581     mac.pass = passwords[z];
2582     mac.iter = mac_default.iter;
2583 
2584     return test_single_cert_mac(&mac);
2585 }
2586 
test_single_cert_mac_iter(int z)2587 static int test_single_cert_mac_iter(int z)
2588 {
2589     PKCS12_ENC mac;
2590 
2591     mac.nid = mac_default.nid;
2592     mac.pass = mac_default.pass;
2593     mac.iter = iters[z];
2594 
2595     return test_single_cert_mac(&mac);
2596 }
2597 
test_cert_key_with_attrs_and_mac(void)2598 static int test_cert_key_with_attrs_and_mac(void)
2599 {
2600     PKCS12_BUILDER *pb = new_pkcs12_builder("1cert1key.p12");
2601 
2602     /* Generate/encode */
2603     start_pkcs12(pb);
2604 
2605     start_contentinfo(pb);
2606 
2607     add_certbag(pb, CERT1, sizeof(CERT1), ATTRS1);
2608     add_keybag(pb, KEY1, sizeof(KEY1), ATTRS1, &enc_default);
2609 
2610     end_contentinfo(pb);
2611 
2612     end_pkcs12_with_mac(pb, &mac_default);
2613 
2614     /* Read/decode */
2615     start_check_pkcs12_with_mac(pb, &mac_default);
2616 
2617     start_check_contentinfo(pb);
2618 
2619     check_certbag(pb, CERT1, sizeof(CERT1), ATTRS1);
2620     check_keybag(pb, KEY1, sizeof(KEY1), ATTRS1, &enc_default);
2621 
2622     end_check_contentinfo(pb);
2623 
2624     end_check_pkcs12(pb);
2625 
2626     return end_pkcs12_builder(pb);
2627 }
2628 
test_cert_key_encrypted_content(void)2629 static int test_cert_key_encrypted_content(void)
2630 {
2631     PKCS12_BUILDER *pb = new_pkcs12_builder("1cert1key_enc.p12");
2632 
2633     /* Generate/encode */
2634     start_pkcs12(pb);
2635 
2636     start_contentinfo(pb);
2637 
2638     add_certbag(pb, CERT1, sizeof(CERT1), ATTRS1);
2639     add_keybag(pb, KEY1, sizeof(KEY1), ATTRS1, &enc_default);
2640 
2641     end_contentinfo_encrypted(pb, &enc_default);
2642 
2643     end_pkcs12_with_mac(pb, &mac_default);
2644 
2645     /* Read/decode */
2646     start_check_pkcs12_with_mac(pb, &mac_default);
2647 
2648     start_check_contentinfo_encrypted(pb, &enc_default);
2649 
2650     check_certbag(pb, CERT1, sizeof(CERT1), ATTRS1);
2651     check_keybag(pb, KEY1, sizeof(KEY1), ATTRS1, &enc_default);
2652 
2653     end_check_contentinfo(pb);
2654 
2655     end_check_pkcs12(pb);
2656 
2657     return end_pkcs12_builder(pb);
2658 }
2659 
test_single_secret_encrypted_content(void)2660 static int test_single_secret_encrypted_content(void)
2661 {
2662     PKCS12_BUILDER *pb = new_pkcs12_builder("1secret.p12");
2663     int custom_nid = get_custom_oid();
2664 
2665     /* Generate/encode */
2666     start_pkcs12(pb);
2667 
2668     start_contentinfo(pb);
2669 
2670     add_secretbag(pb, custom_nid, "VerySecretMessage", ATTRS1);
2671 
2672     end_contentinfo_encrypted(pb, &enc_default);
2673 
2674     end_pkcs12_with_mac(pb, &mac_default);
2675 
2676     /* Read/decode */
2677     start_check_pkcs12_with_mac(pb, &mac_default);
2678 
2679     start_check_contentinfo_encrypted(pb, &enc_default);
2680 
2681     check_secretbag(pb, custom_nid, "VerySecretMessage", ATTRS1);
2682 
2683     end_check_contentinfo(pb);
2684 
2685     end_check_pkcs12(pb);
2686 
2687     return end_pkcs12_builder(pb);
2688 }
2689 
test_single_secret(PKCS12_ENC * enc)2690 static int test_single_secret(PKCS12_ENC *enc)
2691 {
2692     int custom_nid;
2693     char fname[80];
2694     PKCS12_BUILDER *pb;
2695 
2696     BIO_snprintf(fname, sizeof(fname), "1secret_ciph-%s_iter-%d.p12",
2697         OBJ_nid2sn(enc->nid), enc->iter);
2698     pb = new_pkcs12_builder(fname);
2699     custom_nid = get_custom_oid();
2700 
2701     /* Generate/encode */
2702     start_pkcs12(pb);
2703 
2704     start_contentinfo(pb);
2705 
2706     add_secretbag(pb, custom_nid, "VerySecretMessage", ATTRS1);
2707 
2708     end_contentinfo_encrypted(pb, enc);
2709 
2710     end_pkcs12_with_mac(pb, &mac_default);
2711 
2712     /* Read/decode */
2713     start_check_pkcs12_with_mac(pb, &mac_default);
2714 
2715     start_check_contentinfo_encrypted(pb, enc);
2716 
2717     check_secretbag(pb, custom_nid, "VerySecretMessage", ATTRS1);
2718 
2719     end_check_contentinfo(pb);
2720 
2721     end_check_pkcs12(pb);
2722 
2723     return end_pkcs12_builder(pb);
2724 }
2725 
test_single_secret_enc_alg(int z)2726 static int test_single_secret_enc_alg(int z)
2727 {
2728     PKCS12_ENC enc;
2729 
2730     if (lgcyprov == NULL)
2731         enc.nid = enc_nids_no_legacy[z];
2732     else
2733         enc.nid = enc_nids_all[z];
2734     enc.pass = enc_default.pass;
2735     enc.iter = enc_default.iter;
2736 
2737     return test_single_secret(&enc);
2738 }
2739 
test_multiple_contents(void)2740 static int test_multiple_contents(void)
2741 {
2742     PKCS12_BUILDER *pb = new_pkcs12_builder("multi_contents.p12");
2743     int custom_nid = get_custom_oid();
2744 
2745     /* Generate/encode */
2746     start_pkcs12(pb);
2747 
2748     start_contentinfo(pb);
2749 
2750     add_certbag(pb, CERT1, sizeof(CERT1), ATTRS1);
2751     add_certbag(pb, CERT2, sizeof(CERT2), ATTRS2);
2752     add_keybag(pb, KEY1, sizeof(KEY1), ATTRS1, &enc_default);
2753     add_keybag(pb, KEY2, sizeof(KEY2), ATTRS2, &enc_default);
2754 
2755     end_contentinfo(pb);
2756 
2757     start_contentinfo(pb);
2758 
2759     add_secretbag(pb, custom_nid, "VeryVerySecretMessage", ATTRS1);
2760 
2761     end_contentinfo_encrypted(pb, &enc_default);
2762 
2763     end_pkcs12_with_mac(pb, &mac_default);
2764 
2765     /* Read/decode */
2766     start_check_pkcs12_with_mac(pb, &mac_default);
2767 
2768     start_check_contentinfo(pb);
2769 
2770     check_certbag(pb, CERT1, sizeof(CERT1), ATTRS1);
2771     check_certbag(pb, CERT2, sizeof(CERT2), ATTRS2);
2772     check_keybag(pb, KEY1, sizeof(KEY1), ATTRS1, &enc_default);
2773     check_keybag(pb, KEY2, sizeof(KEY2), ATTRS2, &enc_default);
2774 
2775     end_check_contentinfo(pb);
2776 
2777     start_check_contentinfo_encrypted(pb, &enc_default);
2778 
2779     check_secretbag(pb, custom_nid, "VeryVerySecretMessage", ATTRS1);
2780 
2781     end_check_contentinfo(pb);
2782 
2783     end_check_pkcs12(pb);
2784 
2785     return end_pkcs12_builder(pb);
2786 }
2787 
test_jdk_trusted_attr(void)2788 static int test_jdk_trusted_attr(void)
2789 {
2790     PKCS12_BUILDER *pb = new_pkcs12_builder("jdk_trusted.p12");
2791 
2792     /* Generate/encode */
2793     start_pkcs12(pb);
2794 
2795     start_contentinfo(pb);
2796 
2797     add_certbag(pb, CERT1, sizeof(CERT1), ATTRS3);
2798 
2799     end_contentinfo(pb);
2800 
2801     end_pkcs12_with_mac(pb, &mac_default);
2802 
2803     /* Read/decode */
2804     start_check_pkcs12_with_mac(pb, &mac_default);
2805 
2806     start_check_contentinfo(pb);
2807 
2808     check_certbag(pb, CERT1, sizeof(CERT1), ATTRS3);
2809 
2810     end_check_contentinfo(pb);
2811 
2812     end_check_pkcs12(pb);
2813 
2814     return end_pkcs12_builder(pb);
2815 }
2816 
test_set0_attrs(void)2817 static int test_set0_attrs(void)
2818 {
2819     PKCS12_BUILDER *pb = new_pkcs12_builder("attrs.p12");
2820     PKCS12_SAFEBAG *bag = NULL;
2821     STACK_OF(X509_ATTRIBUTE) *attrs = NULL;
2822     X509_ATTRIBUTE *attr = NULL;
2823 
2824     start_pkcs12(pb);
2825 
2826     start_contentinfo(pb);
2827 
2828     /* Add cert and attrs (name/locakkey only) */
2829     add_certbag(pb, CERT1, sizeof(CERT1), ATTRS4);
2830 
2831     bag = sk_PKCS12_SAFEBAG_value(pb->bags, 0);
2832     attrs = (STACK_OF(X509_ATTRIBUTE) *)PKCS12_SAFEBAG_get0_attrs(bag);
2833 
2834     /* Create new attr, add to list and confirm return attrs is not NULL */
2835     attr = X509_ATTRIBUTE_create(NID_oracle_jdk_trustedkeyusage, V_ASN1_OBJECT, OBJ_txt2obj("anyExtendedKeyUsage", 0));
2836     X509at_add1_attr(&attrs, attr);
2837     PKCS12_SAFEBAG_set0_attrs(bag, attrs);
2838     attrs = (STACK_OF(X509_ATTRIBUTE) *)PKCS12_SAFEBAG_get0_attrs(bag);
2839     X509_ATTRIBUTE_free(attr);
2840     if (!TEST_ptr(attrs)) {
2841         goto err;
2842     }
2843 
2844     end_contentinfo(pb);
2845 
2846     end_pkcs12(pb);
2847 
2848     /* Read/decode */
2849     start_check_pkcs12(pb);
2850 
2851     start_check_contentinfo(pb);
2852 
2853     /* Use existing check functionality to confirm cert bag attrs identical to ATTRS3 */
2854     check_certbag(pb, CERT1, sizeof(CERT1), ATTRS3);
2855 
2856     end_check_contentinfo(pb);
2857 
2858     end_check_pkcs12(pb);
2859 
2860     return end_pkcs12_builder(pb);
2861 
2862 err:
2863     (void)end_pkcs12_builder(pb);
2864     return 0;
2865 }
2866 
2867 #ifndef OPENSSL_NO_DES
pkcs12_create_test(void)2868 static int pkcs12_create_test(void)
2869 {
2870     int ret = 0;
2871     EVP_PKEY *pkey = NULL;
2872     PKCS12 *p12 = NULL;
2873     const unsigned char *p;
2874 
2875     static const unsigned char rsa_key[] = {
2876         0x30,
2877         0x82,
2878         0x02,
2879         0x5d,
2880         0x02,
2881         0x01,
2882         0x00,
2883         0x02,
2884         0x81,
2885         0x81,
2886         0x00,
2887         0xbb,
2888         0x24,
2889         0x7a,
2890         0x09,
2891         0x7e,
2892         0x0e,
2893         0xb2,
2894         0x37,
2895         0x32,
2896         0xcc,
2897         0x39,
2898         0x67,
2899         0xad,
2900         0xf1,
2901         0x9e,
2902         0x3d,
2903         0x6b,
2904         0x82,
2905         0x83,
2906         0xd1,
2907         0xd0,
2908         0xac,
2909         0xa4,
2910         0xc0,
2911         0x18,
2912         0xbe,
2913         0x8d,
2914         0x98,
2915         0x00,
2916         0xc0,
2917         0x7b,
2918         0xff,
2919         0x07,
2920         0x44,
2921         0xc9,
2922         0xca,
2923         0x1c,
2924         0xba,
2925         0x36,
2926         0xe1,
2927         0x27,
2928         0x69,
2929         0xff,
2930         0xb1,
2931         0xe3,
2932         0x8d,
2933         0x8b,
2934         0xee,
2935         0x57,
2936         0xa9,
2937         0x3a,
2938         0xaa,
2939         0x16,
2940         0x43,
2941         0x39,
2942         0x54,
2943         0x19,
2944         0x7c,
2945         0xae,
2946         0x69,
2947         0x24,
2948         0x14,
2949         0xf6,
2950         0x64,
2951         0xff,
2952         0xbc,
2953         0x74,
2954         0xc6,
2955         0x67,
2956         0x6c,
2957         0x4c,
2958         0xf1,
2959         0x02,
2960         0x49,
2961         0x69,
2962         0xc7,
2963         0x2b,
2964         0xe1,
2965         0xe1,
2966         0xa1,
2967         0xa3,
2968         0x43,
2969         0x14,
2970         0xf4,
2971         0x77,
2972         0x8f,
2973         0xc8,
2974         0xd0,
2975         0x85,
2976         0x5a,
2977         0x35,
2978         0x95,
2979         0xac,
2980         0x62,
2981         0xa9,
2982         0xc1,
2983         0x21,
2984         0x00,
2985         0x77,
2986         0xa0,
2987         0x8b,
2988         0x97,
2989         0x30,
2990         0xb4,
2991         0x5a,
2992         0x2c,
2993         0xb8,
2994         0x90,
2995         0x2f,
2996         0x48,
2997         0xa0,
2998         0x05,
2999         0x28,
3000         0x4b,
3001         0xf2,
3002         0x0f,
3003         0x8d,
3004         0xec,
3005         0x8b,
3006         0x4d,
3007         0x03,
3008         0x42,
3009         0x75,
3010         0xd6,
3011         0xad,
3012         0x81,
3013         0xc0,
3014         0x11,
3015         0x02,
3016         0x03,
3017         0x01,
3018         0x00,
3019         0x01,
3020         0x02,
3021         0x81,
3022         0x80,
3023         0x00,
3024         0xfc,
3025         0xb9,
3026         0x4a,
3027         0x26,
3028         0x07,
3029         0x89,
3030         0x51,
3031         0x2b,
3032         0x53,
3033         0x72,
3034         0x91,
3035         0xe0,
3036         0x18,
3037         0x3e,
3038         0xa6,
3039         0x5e,
3040         0x31,
3041         0xef,
3042         0x9c,
3043         0x0c,
3044         0x16,
3045         0x24,
3046         0x42,
3047         0xd0,
3048         0x28,
3049         0x33,
3050         0xf9,
3051         0xfa,
3052         0xd0,
3053         0x3c,
3054         0x54,
3055         0x04,
3056         0x06,
3057         0xc0,
3058         0x15,
3059         0xf5,
3060         0x1b,
3061         0x9a,
3062         0xb3,
3063         0x24,
3064         0x31,
3065         0xab,
3066         0x3c,
3067         0x6b,
3068         0x47,
3069         0x43,
3070         0xb0,
3071         0xd2,
3072         0xa9,
3073         0xdc,
3074         0x05,
3075         0xe1,
3076         0x81,
3077         0x59,
3078         0xb6,
3079         0x04,
3080         0xe9,
3081         0x66,
3082         0x61,
3083         0xaa,
3084         0xd7,
3085         0x0b,
3086         0x00,
3087         0x8f,
3088         0x3d,
3089         0xe5,
3090         0xbf,
3091         0xa2,
3092         0xf8,
3093         0x5e,
3094         0x25,
3095         0x6c,
3096         0x1e,
3097         0x22,
3098         0x0f,
3099         0xb4,
3100         0xfd,
3101         0x41,
3102         0xe2,
3103         0x03,
3104         0x31,
3105         0x5f,
3106         0xda,
3107         0x20,
3108         0xc5,
3109         0xc0,
3110         0xf3,
3111         0x55,
3112         0x0e,
3113         0xe1,
3114         0xc9,
3115         0xec,
3116         0xd7,
3117         0x3e,
3118         0x2a,
3119         0x0c,
3120         0x01,
3121         0xca,
3122         0x7b,
3123         0x22,
3124         0xcb,
3125         0xac,
3126         0xf4,
3127         0x2b,
3128         0x27,
3129         0xf0,
3130         0x78,
3131         0x5f,
3132         0xb5,
3133         0xc2,
3134         0xf9,
3135         0xe8,
3136         0x14,
3137         0x5a,
3138         0x6e,
3139         0x7e,
3140         0x86,
3141         0xbd,
3142         0x6a,
3143         0x9b,
3144         0x20,
3145         0x0c,
3146         0xba,
3147         0xcc,
3148         0x97,
3149         0x20,
3150         0x11,
3151         0x02,
3152         0x41,
3153         0x00,
3154         0xc9,
3155         0x59,
3156         0x9f,
3157         0x29,
3158         0x8a,
3159         0x5b,
3160         0x9f,
3161         0xe3,
3162         0x2a,
3163         0xd8,
3164         0x7e,
3165         0xc2,
3166         0x40,
3167         0x9f,
3168         0xa8,
3169         0x45,
3170         0xe5,
3171         0x3e,
3172         0x11,
3173         0x8d,
3174         0x3c,
3175         0xed,
3176         0x6e,
3177         0xab,
3178         0xce,
3179         0xd0,
3180         0x65,
3181         0x46,
3182         0xd8,
3183         0xc7,
3184         0x07,
3185         0x63,
3186         0xb5,
3187         0x23,
3188         0x34,
3189         0xf4,
3190         0x9f,
3191         0x7e,
3192         0x1c,
3193         0xc7,
3194         0xc7,
3195         0xf9,
3196         0x65,
3197         0xd1,
3198         0xf4,
3199         0x04,
3200         0x42,
3201         0x38,
3202         0xbe,
3203         0x3a,
3204         0x0c,
3205         0x9d,
3206         0x08,
3207         0x25,
3208         0xfc,
3209         0xa3,
3210         0x71,
3211         0xd9,
3212         0xae,
3213         0x0c,
3214         0x39,
3215         0x61,
3216         0xf4,
3217         0x89,
3218         0x02,
3219         0x41,
3220         0x00,
3221         0xed,
3222         0xef,
3223         0xab,
3224         0xa9,
3225         0xd5,
3226         0x39,
3227         0x9c,
3228         0xee,
3229         0x59,
3230         0x1b,
3231         0xff,
3232         0xcf,
3233         0x48,
3234         0x44,
3235         0x1b,
3236         0xb6,
3237         0x32,
3238         0xe7,
3239         0x46,
3240         0x24,
3241         0xf3,
3242         0x04,
3243         0x7f,
3244         0xde,
3245         0x95,
3246         0x08,
3247         0x6d,
3248         0x75,
3249         0x9e,
3250         0x67,
3251         0x17,
3252         0xba,
3253         0x5c,
3254         0xa4,
3255         0xd4,
3256         0xe2,
3257         0xe2,
3258         0x4d,
3259         0x77,
3260         0xce,
3261         0xeb,
3262         0x66,
3263         0x29,
3264         0xc5,
3265         0x96,
3266         0xe0,
3267         0x62,
3268         0xbb,
3269         0xe5,
3270         0xac,
3271         0xdc,
3272         0x44,
3273         0x62,
3274         0x54,
3275         0x86,
3276         0xed,
3277         0x64,
3278         0x0c,
3279         0xce,
3280         0xd0,
3281         0x60,
3282         0x03,
3283         0x9d,
3284         0x49,
3285         0x02,
3286         0x40,
3287         0x54,
3288         0xd9,
3289         0x18,
3290         0x72,
3291         0x27,
3292         0xe4,
3293         0xbe,
3294         0x76,
3295         0xbb,
3296         0x1a,
3297         0x6a,
3298         0x28,
3299         0x2f,
3300         0x95,
3301         0x58,
3302         0x12,
3303         0xc4,
3304         0x2c,
3305         0xa8,
3306         0xb6,
3307         0xcc,
3308         0xe2,
3309         0xfd,
3310         0x0d,
3311         0x17,
3312         0x64,
3313         0xc8,
3314         0x18,
3315         0xd7,
3316         0xc6,
3317         0xdf,
3318         0x3d,
3319         0x4c,
3320         0x1a,
3321         0x9e,
3322         0xf9,
3323         0x2a,
3324         0xb0,
3325         0xb9,
3326         0x2e,
3327         0x12,
3328         0xfd,
3329         0xec,
3330         0xc3,
3331         0x51,
3332         0xc1,
3333         0xed,
3334         0xa9,
3335         0xfd,
3336         0xb7,
3337         0x76,
3338         0x93,
3339         0x41,
3340         0xd8,
3341         0xc8,
3342         0x22,
3343         0x94,
3344         0x1a,
3345         0x77,
3346         0xf6,
3347         0x9c,
3348         0xc3,
3349         0xc3,
3350         0x89,
3351         0x02,
3352         0x41,
3353         0x00,
3354         0x8e,
3355         0xf9,
3356         0xa7,
3357         0x08,
3358         0xad,
3359         0xb5,
3360         0x2a,
3361         0x04,
3362         0xdb,
3363         0x8d,
3364         0x04,
3365         0xa1,
3366         0xb5,
3367         0x06,
3368         0x20,
3369         0x34,
3370         0xd2,
3371         0xcf,
3372         0xc0,
3373         0x89,
3374         0xb1,
3375         0x72,
3376         0x31,
3377         0xb8,
3378         0x39,
3379         0x8b,
3380         0xcf,
3381         0xe2,
3382         0x8e,
3383         0xa5,
3384         0xda,
3385         0x4f,
3386         0x45,
3387         0x1e,
3388         0x53,
3389         0x42,
3390         0x66,
3391         0xc4,
3392         0x30,
3393         0x4b,
3394         0x29,
3395         0x8e,
3396         0xc1,
3397         0x69,
3398         0x17,
3399         0x29,
3400         0x8c,
3401         0x8a,
3402         0xe6,
3403         0x0f,
3404         0x82,
3405         0x68,
3406         0xa1,
3407         0x41,
3408         0xb3,
3409         0xb6,
3410         0x70,
3411         0x99,
3412         0x75,
3413         0xa9,
3414         0x27,
3415         0x18,
3416         0xe4,
3417         0xe9,
3418         0x02,
3419         0x41,
3420         0x00,
3421         0x89,
3422         0xea,
3423         0x6e,
3424         0x6d,
3425         0x70,
3426         0xdf,
3427         0x25,
3428         0x5f,
3429         0x18,
3430         0x3f,
3431         0x48,
3432         0xda,
3433         0x63,
3434         0x10,
3435         0x8b,
3436         0xfe,
3437         0xa8,
3438         0x0c,
3439         0x94,
3440         0x0f,
3441         0xde,
3442         0x97,
3443         0x56,
3444         0x53,
3445         0x89,
3446         0x94,
3447         0xe2,
3448         0x1e,
3449         0x2c,
3450         0x74,
3451         0x3c,
3452         0x91,
3453         0x81,
3454         0x34,
3455         0x0b,
3456         0xa6,
3457         0x40,
3458         0xf8,
3459         0xcb,
3460         0x2a,
3461         0x60,
3462         0x8c,
3463         0xe0,
3464         0x02,
3465         0xb7,
3466         0x89,
3467         0x93,
3468         0xcf,
3469         0x18,
3470         0x9f,
3471         0x49,
3472         0x54,
3473         0xfd,
3474         0x7d,
3475         0x3f,
3476         0x9a,
3477         0xef,
3478         0xd4,
3479         0xa4,
3480         0x4f,
3481         0xc1,
3482         0x45,
3483         0x99,
3484         0x91,
3485     };
3486 
3487     p = rsa_key;
3488     if (!TEST_ptr(pkey = d2i_PrivateKey_ex(EVP_PKEY_RSA, NULL, &p,
3489                       sizeof(rsa_key), NULL, NULL)))
3490         goto err;
3491     if (!TEST_int_eq(ERR_peek_error(), 0))
3492         goto err;
3493     p12 = PKCS12_create(NULL, NULL, pkey, NULL, NULL,
3494         NID_pbe_WithSHA1And3_Key_TripleDES_CBC,
3495         NID_pbe_WithSHA1And3_Key_TripleDES_CBC, 2, 1, 0);
3496     if (!TEST_ptr(p12))
3497         goto err;
3498 
3499     if (!TEST_int_eq(ERR_peek_error(), 0))
3500         goto err;
3501     ret = 1;
3502 err:
3503     PKCS12_free(p12);
3504     EVP_PKEY_free(pkey);
3505     return ret;
3506 }
3507 #endif
3508 
pkcs12_recreate_test(void)3509 static int pkcs12_recreate_test(void)
3510 {
3511     int ret = 0;
3512     X509 *cert = NULL;
3513     X509 *cert_parsed = NULL;
3514     EVP_PKEY *pkey = NULL;
3515     EVP_PKEY *pkey_parsed = NULL;
3516     PKCS12 *p12 = NULL;
3517     PKCS12 *p12_parsed = NULL;
3518     PKCS12 *p12_recreated = NULL;
3519     const unsigned char *cert_bytes = CERT1;
3520     const unsigned char *key_bytes = KEY1;
3521     BIO *bio = NULL;
3522 
3523     cert = d2i_X509(NULL, &cert_bytes, sizeof(CERT1));
3524     if (!TEST_ptr(cert))
3525         goto err;
3526     pkey = d2i_AutoPrivateKey(NULL, &key_bytes, sizeof(KEY1));
3527     if (!TEST_ptr(pkey))
3528         goto err;
3529     p12 = PKCS12_create("pass", NULL, pkey, cert, NULL, NID_aes_256_cbc,
3530         NID_aes_256_cbc, 2, 1, 0);
3531     if (!TEST_ptr(p12))
3532         goto err;
3533     if (!TEST_int_eq(ERR_peek_error(), 0))
3534         goto err;
3535 
3536     bio = BIO_new(BIO_s_mem());
3537     if (!TEST_ptr(bio))
3538         goto err;
3539     if (!TEST_int_eq(i2d_PKCS12_bio(bio, p12), 1))
3540         goto err;
3541     p12_parsed = PKCS12_init_ex(NID_pkcs7_data, testctx, NULL);
3542     if (!TEST_ptr(p12_parsed))
3543         goto err;
3544     p12_parsed = d2i_PKCS12_bio(bio, &p12_parsed);
3545     if (!TEST_ptr(p12_parsed))
3546         goto err;
3547     if (!TEST_int_eq(PKCS12_parse(p12_parsed, "pass", &pkey_parsed,
3548                          &cert_parsed, NULL),
3549             1))
3550         goto err;
3551 
3552     /* cert_parsed also contains auxiliary data */
3553     p12_recreated = PKCS12_create("new_pass", NULL, pkey_parsed, cert_parsed,
3554         NULL, NID_aes_256_cbc, NID_aes_256_cbc,
3555         2, 1, 0);
3556     if (!TEST_ptr(p12_recreated))
3557         goto err;
3558     if (!TEST_int_eq(ERR_peek_error(), 0))
3559         goto err;
3560 
3561     ret = 1;
3562 err:
3563     BIO_free(bio);
3564     PKCS12_free(p12);
3565     PKCS12_free(p12_parsed);
3566     PKCS12_free(p12_recreated);
3567     EVP_PKEY_free(pkey);
3568     EVP_PKEY_free(pkey_parsed);
3569     X509_free(cert);
3570     X509_free(cert_parsed);
3571     return ret;
3572 }
3573 
3574 typedef enum OPTION_choice {
3575     OPT_ERR = -1,
3576     OPT_EOF = 0,
3577     OPT_WRITE,
3578     OPT_LEGACY,
3579     OPT_CONTEXT,
3580     OPT_TEST_ENUM
3581 } OPTION_CHOICE;
3582 
test_get_options(void)3583 const OPTIONS *test_get_options(void)
3584 {
3585     static const OPTIONS options[] = {
3586         OPT_TEST_OPTIONS_DEFAULT_USAGE,
3587         { "write", OPT_WRITE, '-', "Write PKCS12 objects to file" },
3588         { "legacy", OPT_LEGACY, '-', "Test the legacy APIs" },
3589         { "context", OPT_CONTEXT, '-', "Explicitly use a non-default library context" },
3590         { NULL }
3591     };
3592     return options;
3593 }
3594 
setup_tests(void)3595 int setup_tests(void)
3596 {
3597     OPTION_CHOICE o;
3598 
3599     while ((o = opt_next()) != OPT_EOF) {
3600         switch (o) {
3601         case OPT_WRITE:
3602             PKCS12_helper_set_write_files(1);
3603             break;
3604         case OPT_LEGACY:
3605             PKCS12_helper_set_legacy(1);
3606             break;
3607         case OPT_CONTEXT:
3608             default_libctx = 0;
3609             break;
3610         case OPT_TEST_CASES:
3611             break;
3612         default:
3613             return 0;
3614         }
3615     }
3616 
3617     if (!default_libctx) {
3618         testctx = OSSL_LIB_CTX_new();
3619         if (!TEST_ptr(testctx))
3620             return 0;
3621         nullprov = OSSL_PROVIDER_load(NULL, "null");
3622         if (!TEST_ptr(nullprov))
3623             return 0;
3624     }
3625 
3626     deflprov = OSSL_PROVIDER_load(testctx, "default");
3627     if (!TEST_ptr(deflprov))
3628         return 0;
3629     lgcyprov = OSSL_PROVIDER_load(testctx, "legacy");
3630 
3631     PKCS12_helper_set_libctx(testctx);
3632 
3633     /*
3634      * Verify that the default and fips providers in the default libctx are not
3635      * available if we are using a standalone context
3636      */
3637     if (!default_libctx) {
3638         if (!TEST_false(OSSL_PROVIDER_available(NULL, "default"))
3639             || !TEST_false(OSSL_PROVIDER_available(NULL, "fips")))
3640             return 0;
3641     }
3642 
3643     ADD_TEST(test_single_cert_no_attrs);
3644     if (lgcyprov == NULL) {
3645         ADD_ALL_TESTS(test_single_key_enc_alg, OSSL_NELEM(enc_nids_no_legacy));
3646         ADD_ALL_TESTS(test_single_secret_enc_alg, OSSL_NELEM(enc_nids_no_legacy));
3647     } else {
3648         ADD_ALL_TESTS(test_single_key_enc_alg, OSSL_NELEM(enc_nids_all));
3649         ADD_ALL_TESTS(test_single_secret_enc_alg, OSSL_NELEM(enc_nids_all));
3650     }
3651 #ifndef OPENSSL_NO_DES
3652     if (default_libctx)
3653         ADD_TEST(pkcs12_create_test);
3654 #endif
3655     if (default_libctx)
3656         ADD_TEST(pkcs12_recreate_test);
3657     ADD_ALL_TESTS(test_single_key_enc_pass, OSSL_NELEM(passwords));
3658     ADD_ALL_TESTS(test_single_key_enc_iter, OSSL_NELEM(iters));
3659     ADD_TEST(test_single_key_with_attrs);
3660     ADD_ALL_TESTS(test_single_cert_mac_alg, OSSL_NELEM(mac_nids));
3661     ADD_ALL_TESTS(test_single_cert_mac_pass, OSSL_NELEM(passwords));
3662     ADD_ALL_TESTS(test_single_cert_mac_iter, OSSL_NELEM(iters));
3663     ADD_TEST(test_cert_key_with_attrs_and_mac);
3664     ADD_TEST(test_cert_key_encrypted_content);
3665     ADD_TEST(test_single_secret_encrypted_content);
3666     ADD_TEST(test_multiple_contents);
3667     ADD_TEST(test_jdk_trusted_attr);
3668     ADD_TEST(test_set0_attrs);
3669     return 1;
3670 }
3671 
cleanup_tests(void)3672 void cleanup_tests(void)
3673 {
3674     OSSL_PROVIDER_unload(nullprov);
3675     OSSL_PROVIDER_unload(deflprov);
3676     OSSL_PROVIDER_unload(lgcyprov);
3677     OSSL_LIB_CTX_free(testctx);
3678 }
3679