xref: /freebsd/crypto/openssl/test/evp_extra_test2.c (revision f25b8c9fb4f58cf61adb47d7570abe7caa6d385d)
1 /*
2  * Copyright 2015-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 /* We need to use some deprecated APIs */
11 #define OPENSSL_SUPPRESS_DEPRECATED
12 
13 /*
14  * Really these tests should be in evp_extra_test - but that doesn't
15  * yet support testing with a non-default libctx. Once it does we should move
16  * everything into one file. Consequently some things are duplicated between
17  * the two files.
18  */
19 
20 #include <openssl/evp.h>
21 #include <openssl/pem.h>
22 #include <openssl/provider.h>
23 #include <openssl/rsa.h>
24 #include <openssl/dh.h>
25 #include <openssl/core_names.h>
26 #include <openssl/ui.h>
27 
28 #include "testutil.h"
29 #include "internal/nelem.h"
30 #include "crypto/evp.h"
31 #include "../crypto/evp/evp_local.h"
32 
33 /* Defined in tls-provider.c */
34 int tls_provider_init(const OSSL_CORE_HANDLE *handle,
35     const OSSL_DISPATCH *in,
36     const OSSL_DISPATCH **out,
37     void **provctx);
38 
39 static OSSL_LIB_CTX *mainctx = NULL;
40 static OSSL_PROVIDER *nullprov = NULL;
41 
42 /*
43  * kExampleRSAKeyDER is an RSA private key in ASN.1, DER format. Of course, you
44  * should never use this key anywhere but in an example.
45  */
46 static const unsigned char kExampleRSAKeyDER[] = {
47     0x30,
48     0x82,
49     0x02,
50     0x5c,
51     0x02,
52     0x01,
53     0x00,
54     0x02,
55     0x81,
56     0x81,
57     0x00,
58     0xf8,
59     0xb8,
60     0x6c,
61     0x83,
62     0xb4,
63     0xbc,
64     0xd9,
65     0xa8,
66     0x57,
67     0xc0,
68     0xa5,
69     0xb4,
70     0x59,
71     0x76,
72     0x8c,
73     0x54,
74     0x1d,
75     0x79,
76     0xeb,
77     0x22,
78     0x52,
79     0x04,
80     0x7e,
81     0xd3,
82     0x37,
83     0xeb,
84     0x41,
85     0xfd,
86     0x83,
87     0xf9,
88     0xf0,
89     0xa6,
90     0x85,
91     0x15,
92     0x34,
93     0x75,
94     0x71,
95     0x5a,
96     0x84,
97     0xa8,
98     0x3c,
99     0xd2,
100     0xef,
101     0x5a,
102     0x4e,
103     0xd3,
104     0xde,
105     0x97,
106     0x8a,
107     0xdd,
108     0xff,
109     0xbb,
110     0xcf,
111     0x0a,
112     0xaa,
113     0x86,
114     0x92,
115     0xbe,
116     0xb8,
117     0x50,
118     0xe4,
119     0xcd,
120     0x6f,
121     0x80,
122     0x33,
123     0x30,
124     0x76,
125     0x13,
126     0x8f,
127     0xca,
128     0x7b,
129     0xdc,
130     0xec,
131     0x5a,
132     0xca,
133     0x63,
134     0xc7,
135     0x03,
136     0x25,
137     0xef,
138     0xa8,
139     0x8a,
140     0x83,
141     0x58,
142     0x76,
143     0x20,
144     0xfa,
145     0x16,
146     0x77,
147     0xd7,
148     0x79,
149     0x92,
150     0x63,
151     0x01,
152     0x48,
153     0x1a,
154     0xd8,
155     0x7b,
156     0x67,
157     0xf1,
158     0x52,
159     0x55,
160     0x49,
161     0x4e,
162     0xd6,
163     0x6e,
164     0x4a,
165     0x5c,
166     0xd7,
167     0x7a,
168     0x37,
169     0x36,
170     0x0c,
171     0xde,
172     0xdd,
173     0x8f,
174     0x44,
175     0xe8,
176     0xc2,
177     0xa7,
178     0x2c,
179     0x2b,
180     0xb5,
181     0xaf,
182     0x64,
183     0x4b,
184     0x61,
185     0x07,
186     0x02,
187     0x03,
188     0x01,
189     0x00,
190     0x01,
191     0x02,
192     0x81,
193     0x80,
194     0x74,
195     0x88,
196     0x64,
197     0x3f,
198     0x69,
199     0x45,
200     0x3a,
201     0x6d,
202     0xc7,
203     0x7f,
204     0xb9,
205     0xa3,
206     0xc0,
207     0x6e,
208     0xec,
209     0xdc,
210     0xd4,
211     0x5a,
212     0xb5,
213     0x32,
214     0x85,
215     0x5f,
216     0x19,
217     0xd4,
218     0xf8,
219     0xd4,
220     0x3f,
221     0x3c,
222     0xfa,
223     0xc2,
224     0xf6,
225     0x5f,
226     0xee,
227     0xe6,
228     0xba,
229     0x87,
230     0x74,
231     0x2e,
232     0xc7,
233     0x0c,
234     0xd4,
235     0x42,
236     0xb8,
237     0x66,
238     0x85,
239     0x9c,
240     0x7b,
241     0x24,
242     0x61,
243     0xaa,
244     0x16,
245     0x11,
246     0xf6,
247     0xb5,
248     0xb6,
249     0xa4,
250     0x0a,
251     0xc9,
252     0x55,
253     0x2e,
254     0x81,
255     0xa5,
256     0x47,
257     0x61,
258     0xcb,
259     0x25,
260     0x8f,
261     0xc2,
262     0x15,
263     0x7b,
264     0x0e,
265     0x7c,
266     0x36,
267     0x9f,
268     0x3a,
269     0xda,
270     0x58,
271     0x86,
272     0x1c,
273     0x5b,
274     0x83,
275     0x79,
276     0xe6,
277     0x2b,
278     0xcc,
279     0xe6,
280     0xfa,
281     0x2c,
282     0x61,
283     0xf2,
284     0x78,
285     0x80,
286     0x1b,
287     0xe2,
288     0xf3,
289     0x9d,
290     0x39,
291     0x2b,
292     0x65,
293     0x57,
294     0x91,
295     0x3d,
296     0x71,
297     0x99,
298     0x73,
299     0xa5,
300     0xc2,
301     0x79,
302     0x20,
303     0x8c,
304     0x07,
305     0x4f,
306     0xe5,
307     0xb4,
308     0x60,
309     0x1f,
310     0x99,
311     0xa2,
312     0xb1,
313     0x4f,
314     0x0c,
315     0xef,
316     0xbc,
317     0x59,
318     0x53,
319     0x00,
320     0x7d,
321     0xb1,
322     0x02,
323     0x41,
324     0x00,
325     0xfc,
326     0x7e,
327     0x23,
328     0x65,
329     0x70,
330     0xf8,
331     0xce,
332     0xd3,
333     0x40,
334     0x41,
335     0x80,
336     0x6a,
337     0x1d,
338     0x01,
339     0xd6,
340     0x01,
341     0xff,
342     0xb6,
343     0x1b,
344     0x3d,
345     0x3d,
346     0x59,
347     0x09,
348     0x33,
349     0x79,
350     0xc0,
351     0x4f,
352     0xde,
353     0x96,
354     0x27,
355     0x4b,
356     0x18,
357     0xc6,
358     0xd9,
359     0x78,
360     0xf1,
361     0xf4,
362     0x35,
363     0x46,
364     0xe9,
365     0x7c,
366     0x42,
367     0x7a,
368     0x5d,
369     0x9f,
370     0xef,
371     0x54,
372     0xb8,
373     0xf7,
374     0x9f,
375     0xc4,
376     0x33,
377     0x6c,
378     0xf3,
379     0x8c,
380     0x32,
381     0x46,
382     0x87,
383     0x67,
384     0x30,
385     0x7b,
386     0xa7,
387     0xac,
388     0xe3,
389     0x02,
390     0x41,
391     0x00,
392     0xfc,
393     0x2c,
394     0xdf,
395     0x0c,
396     0x0d,
397     0x88,
398     0xf5,
399     0xb1,
400     0x92,
401     0xa8,
402     0x93,
403     0x47,
404     0x63,
405     0x55,
406     0xf5,
407     0xca,
408     0x58,
409     0x43,
410     0xba,
411     0x1c,
412     0xe5,
413     0x9e,
414     0xb6,
415     0x95,
416     0x05,
417     0xcd,
418     0xb5,
419     0x82,
420     0xdf,
421     0xeb,
422     0x04,
423     0x53,
424     0x9d,
425     0xbd,
426     0xc2,
427     0x38,
428     0x16,
429     0xb3,
430     0x62,
431     0xdd,
432     0xa1,
433     0x46,
434     0xdb,
435     0x6d,
436     0x97,
437     0x93,
438     0x9f,
439     0x8a,
440     0xc3,
441     0x9b,
442     0x64,
443     0x7e,
444     0x42,
445     0xe3,
446     0x32,
447     0x57,
448     0x19,
449     0x1b,
450     0xd5,
451     0x6e,
452     0x85,
453     0xfa,
454     0xb8,
455     0x8d,
456     0x02,
457     0x41,
458     0x00,
459     0xbc,
460     0x3d,
461     0xde,
462     0x6d,
463     0xd6,
464     0x97,
465     0xe8,
466     0xba,
467     0x9e,
468     0x81,
469     0x37,
470     0x17,
471     0xe5,
472     0xa0,
473     0x64,
474     0xc9,
475     0x00,
476     0xb7,
477     0xe7,
478     0xfe,
479     0xf4,
480     0x29,
481     0xd9,
482     0x2e,
483     0x43,
484     0x6b,
485     0x19,
486     0x20,
487     0xbd,
488     0x99,
489     0x75,
490     0xe7,
491     0x76,
492     0xf8,
493     0xd3,
494     0xae,
495     0xaf,
496     0x7e,
497     0xb8,
498     0xeb,
499     0x81,
500     0xf4,
501     0x9d,
502     0xfe,
503     0x07,
504     0x2b,
505     0x0b,
506     0x63,
507     0x0b,
508     0x5a,
509     0x55,
510     0x90,
511     0x71,
512     0x7d,
513     0xf1,
514     0xdb,
515     0xd9,
516     0xb1,
517     0x41,
518     0x41,
519     0x68,
520     0x2f,
521     0x4e,
522     0x39,
523     0x02,
524     0x40,
525     0x5a,
526     0x34,
527     0x66,
528     0xd8,
529     0xf5,
530     0xe2,
531     0x7f,
532     0x18,
533     0xb5,
534     0x00,
535     0x6e,
536     0x26,
537     0x84,
538     0x27,
539     0x14,
540     0x93,
541     0xfb,
542     0xfc,
543     0xc6,
544     0x0f,
545     0x5e,
546     0x27,
547     0xe6,
548     0xe1,
549     0xe9,
550     0xc0,
551     0x8a,
552     0xe4,
553     0x34,
554     0xda,
555     0xe9,
556     0xa2,
557     0x4b,
558     0x73,
559     0xbc,
560     0x8c,
561     0xb9,
562     0xba,
563     0x13,
564     0x6c,
565     0x7a,
566     0x2b,
567     0x51,
568     0x84,
569     0xa3,
570     0x4a,
571     0xe0,
572     0x30,
573     0x10,
574     0x06,
575     0x7e,
576     0xed,
577     0x17,
578     0x5a,
579     0x14,
580     0x00,
581     0xc9,
582     0xef,
583     0x85,
584     0xea,
585     0x52,
586     0x2c,
587     0xbc,
588     0x65,
589     0x02,
590     0x40,
591     0x51,
592     0xe3,
593     0xf2,
594     0x83,
595     0x19,
596     0x9b,
597     0xc4,
598     0x1e,
599     0x2f,
600     0x50,
601     0x3d,
602     0xdf,
603     0x5a,
604     0xa2,
605     0x18,
606     0xca,
607     0x5f,
608     0x2e,
609     0x49,
610     0xaf,
611     0x6f,
612     0xcc,
613     0xfa,
614     0x65,
615     0x77,
616     0x94,
617     0xb5,
618     0xa1,
619     0x0a,
620     0xa9,
621     0xd1,
622     0x8a,
623     0x39,
624     0x37,
625     0xf4,
626     0x0b,
627     0xa0,
628     0xd7,
629     0x82,
630     0x27,
631     0x5e,
632     0xae,
633     0x17,
634     0x17,
635     0xa1,
636     0x1e,
637     0x54,
638     0x34,
639     0xbf,
640     0x6e,
641     0xc4,
642     0x8e,
643     0x99,
644     0x5d,
645     0x08,
646     0xf1,
647     0x2d,
648     0x86,
649     0x9d,
650     0xa5,
651     0x20,
652     0x1b,
653     0xe5,
654     0xdf,
655 };
656 
657 /*
658  * kExampleRSAKeyPKCS8 is kExampleRSAKeyDER encoded in a PKCS #8
659  * PrivateKeyInfo.
660  */
661 static const unsigned char kExampleRSAKeyPKCS8[] = {
662     0x30,
663     0x82,
664     0x02,
665     0x76,
666     0x02,
667     0x01,
668     0x00,
669     0x30,
670     0x0d,
671     0x06,
672     0x09,
673     0x2a,
674     0x86,
675     0x48,
676     0x86,
677     0xf7,
678     0x0d,
679     0x01,
680     0x01,
681     0x01,
682     0x05,
683     0x00,
684     0x04,
685     0x82,
686     0x02,
687     0x60,
688     0x30,
689     0x82,
690     0x02,
691     0x5c,
692     0x02,
693     0x01,
694     0x00,
695     0x02,
696     0x81,
697     0x81,
698     0x00,
699     0xf8,
700     0xb8,
701     0x6c,
702     0x83,
703     0xb4,
704     0xbc,
705     0xd9,
706     0xa8,
707     0x57,
708     0xc0,
709     0xa5,
710     0xb4,
711     0x59,
712     0x76,
713     0x8c,
714     0x54,
715     0x1d,
716     0x79,
717     0xeb,
718     0x22,
719     0x52,
720     0x04,
721     0x7e,
722     0xd3,
723     0x37,
724     0xeb,
725     0x41,
726     0xfd,
727     0x83,
728     0xf9,
729     0xf0,
730     0xa6,
731     0x85,
732     0x15,
733     0x34,
734     0x75,
735     0x71,
736     0x5a,
737     0x84,
738     0xa8,
739     0x3c,
740     0xd2,
741     0xef,
742     0x5a,
743     0x4e,
744     0xd3,
745     0xde,
746     0x97,
747     0x8a,
748     0xdd,
749     0xff,
750     0xbb,
751     0xcf,
752     0x0a,
753     0xaa,
754     0x86,
755     0x92,
756     0xbe,
757     0xb8,
758     0x50,
759     0xe4,
760     0xcd,
761     0x6f,
762     0x80,
763     0x33,
764     0x30,
765     0x76,
766     0x13,
767     0x8f,
768     0xca,
769     0x7b,
770     0xdc,
771     0xec,
772     0x5a,
773     0xca,
774     0x63,
775     0xc7,
776     0x03,
777     0x25,
778     0xef,
779     0xa8,
780     0x8a,
781     0x83,
782     0x58,
783     0x76,
784     0x20,
785     0xfa,
786     0x16,
787     0x77,
788     0xd7,
789     0x79,
790     0x92,
791     0x63,
792     0x01,
793     0x48,
794     0x1a,
795     0xd8,
796     0x7b,
797     0x67,
798     0xf1,
799     0x52,
800     0x55,
801     0x49,
802     0x4e,
803     0xd6,
804     0x6e,
805     0x4a,
806     0x5c,
807     0xd7,
808     0x7a,
809     0x37,
810     0x36,
811     0x0c,
812     0xde,
813     0xdd,
814     0x8f,
815     0x44,
816     0xe8,
817     0xc2,
818     0xa7,
819     0x2c,
820     0x2b,
821     0xb5,
822     0xaf,
823     0x64,
824     0x4b,
825     0x61,
826     0x07,
827     0x02,
828     0x03,
829     0x01,
830     0x00,
831     0x01,
832     0x02,
833     0x81,
834     0x80,
835     0x74,
836     0x88,
837     0x64,
838     0x3f,
839     0x69,
840     0x45,
841     0x3a,
842     0x6d,
843     0xc7,
844     0x7f,
845     0xb9,
846     0xa3,
847     0xc0,
848     0x6e,
849     0xec,
850     0xdc,
851     0xd4,
852     0x5a,
853     0xb5,
854     0x32,
855     0x85,
856     0x5f,
857     0x19,
858     0xd4,
859     0xf8,
860     0xd4,
861     0x3f,
862     0x3c,
863     0xfa,
864     0xc2,
865     0xf6,
866     0x5f,
867     0xee,
868     0xe6,
869     0xba,
870     0x87,
871     0x74,
872     0x2e,
873     0xc7,
874     0x0c,
875     0xd4,
876     0x42,
877     0xb8,
878     0x66,
879     0x85,
880     0x9c,
881     0x7b,
882     0x24,
883     0x61,
884     0xaa,
885     0x16,
886     0x11,
887     0xf6,
888     0xb5,
889     0xb6,
890     0xa4,
891     0x0a,
892     0xc9,
893     0x55,
894     0x2e,
895     0x81,
896     0xa5,
897     0x47,
898     0x61,
899     0xcb,
900     0x25,
901     0x8f,
902     0xc2,
903     0x15,
904     0x7b,
905     0x0e,
906     0x7c,
907     0x36,
908     0x9f,
909     0x3a,
910     0xda,
911     0x58,
912     0x86,
913     0x1c,
914     0x5b,
915     0x83,
916     0x79,
917     0xe6,
918     0x2b,
919     0xcc,
920     0xe6,
921     0xfa,
922     0x2c,
923     0x61,
924     0xf2,
925     0x78,
926     0x80,
927     0x1b,
928     0xe2,
929     0xf3,
930     0x9d,
931     0x39,
932     0x2b,
933     0x65,
934     0x57,
935     0x91,
936     0x3d,
937     0x71,
938     0x99,
939     0x73,
940     0xa5,
941     0xc2,
942     0x79,
943     0x20,
944     0x8c,
945     0x07,
946     0x4f,
947     0xe5,
948     0xb4,
949     0x60,
950     0x1f,
951     0x99,
952     0xa2,
953     0xb1,
954     0x4f,
955     0x0c,
956     0xef,
957     0xbc,
958     0x59,
959     0x53,
960     0x00,
961     0x7d,
962     0xb1,
963     0x02,
964     0x41,
965     0x00,
966     0xfc,
967     0x7e,
968     0x23,
969     0x65,
970     0x70,
971     0xf8,
972     0xce,
973     0xd3,
974     0x40,
975     0x41,
976     0x80,
977     0x6a,
978     0x1d,
979     0x01,
980     0xd6,
981     0x01,
982     0xff,
983     0xb6,
984     0x1b,
985     0x3d,
986     0x3d,
987     0x59,
988     0x09,
989     0x33,
990     0x79,
991     0xc0,
992     0x4f,
993     0xde,
994     0x96,
995     0x27,
996     0x4b,
997     0x18,
998     0xc6,
999     0xd9,
1000     0x78,
1001     0xf1,
1002     0xf4,
1003     0x35,
1004     0x46,
1005     0xe9,
1006     0x7c,
1007     0x42,
1008     0x7a,
1009     0x5d,
1010     0x9f,
1011     0xef,
1012     0x54,
1013     0xb8,
1014     0xf7,
1015     0x9f,
1016     0xc4,
1017     0x33,
1018     0x6c,
1019     0xf3,
1020     0x8c,
1021     0x32,
1022     0x46,
1023     0x87,
1024     0x67,
1025     0x30,
1026     0x7b,
1027     0xa7,
1028     0xac,
1029     0xe3,
1030     0x02,
1031     0x41,
1032     0x00,
1033     0xfc,
1034     0x2c,
1035     0xdf,
1036     0x0c,
1037     0x0d,
1038     0x88,
1039     0xf5,
1040     0xb1,
1041     0x92,
1042     0xa8,
1043     0x93,
1044     0x47,
1045     0x63,
1046     0x55,
1047     0xf5,
1048     0xca,
1049     0x58,
1050     0x43,
1051     0xba,
1052     0x1c,
1053     0xe5,
1054     0x9e,
1055     0xb6,
1056     0x95,
1057     0x05,
1058     0xcd,
1059     0xb5,
1060     0x82,
1061     0xdf,
1062     0xeb,
1063     0x04,
1064     0x53,
1065     0x9d,
1066     0xbd,
1067     0xc2,
1068     0x38,
1069     0x16,
1070     0xb3,
1071     0x62,
1072     0xdd,
1073     0xa1,
1074     0x46,
1075     0xdb,
1076     0x6d,
1077     0x97,
1078     0x93,
1079     0x9f,
1080     0x8a,
1081     0xc3,
1082     0x9b,
1083     0x64,
1084     0x7e,
1085     0x42,
1086     0xe3,
1087     0x32,
1088     0x57,
1089     0x19,
1090     0x1b,
1091     0xd5,
1092     0x6e,
1093     0x85,
1094     0xfa,
1095     0xb8,
1096     0x8d,
1097     0x02,
1098     0x41,
1099     0x00,
1100     0xbc,
1101     0x3d,
1102     0xde,
1103     0x6d,
1104     0xd6,
1105     0x97,
1106     0xe8,
1107     0xba,
1108     0x9e,
1109     0x81,
1110     0x37,
1111     0x17,
1112     0xe5,
1113     0xa0,
1114     0x64,
1115     0xc9,
1116     0x00,
1117     0xb7,
1118     0xe7,
1119     0xfe,
1120     0xf4,
1121     0x29,
1122     0xd9,
1123     0x2e,
1124     0x43,
1125     0x6b,
1126     0x19,
1127     0x20,
1128     0xbd,
1129     0x99,
1130     0x75,
1131     0xe7,
1132     0x76,
1133     0xf8,
1134     0xd3,
1135     0xae,
1136     0xaf,
1137     0x7e,
1138     0xb8,
1139     0xeb,
1140     0x81,
1141     0xf4,
1142     0x9d,
1143     0xfe,
1144     0x07,
1145     0x2b,
1146     0x0b,
1147     0x63,
1148     0x0b,
1149     0x5a,
1150     0x55,
1151     0x90,
1152     0x71,
1153     0x7d,
1154     0xf1,
1155     0xdb,
1156     0xd9,
1157     0xb1,
1158     0x41,
1159     0x41,
1160     0x68,
1161     0x2f,
1162     0x4e,
1163     0x39,
1164     0x02,
1165     0x40,
1166     0x5a,
1167     0x34,
1168     0x66,
1169     0xd8,
1170     0xf5,
1171     0xe2,
1172     0x7f,
1173     0x18,
1174     0xb5,
1175     0x00,
1176     0x6e,
1177     0x26,
1178     0x84,
1179     0x27,
1180     0x14,
1181     0x93,
1182     0xfb,
1183     0xfc,
1184     0xc6,
1185     0x0f,
1186     0x5e,
1187     0x27,
1188     0xe6,
1189     0xe1,
1190     0xe9,
1191     0xc0,
1192     0x8a,
1193     0xe4,
1194     0x34,
1195     0xda,
1196     0xe9,
1197     0xa2,
1198     0x4b,
1199     0x73,
1200     0xbc,
1201     0x8c,
1202     0xb9,
1203     0xba,
1204     0x13,
1205     0x6c,
1206     0x7a,
1207     0x2b,
1208     0x51,
1209     0x84,
1210     0xa3,
1211     0x4a,
1212     0xe0,
1213     0x30,
1214     0x10,
1215     0x06,
1216     0x7e,
1217     0xed,
1218     0x17,
1219     0x5a,
1220     0x14,
1221     0x00,
1222     0xc9,
1223     0xef,
1224     0x85,
1225     0xea,
1226     0x52,
1227     0x2c,
1228     0xbc,
1229     0x65,
1230     0x02,
1231     0x40,
1232     0x51,
1233     0xe3,
1234     0xf2,
1235     0x83,
1236     0x19,
1237     0x9b,
1238     0xc4,
1239     0x1e,
1240     0x2f,
1241     0x50,
1242     0x3d,
1243     0xdf,
1244     0x5a,
1245     0xa2,
1246     0x18,
1247     0xca,
1248     0x5f,
1249     0x2e,
1250     0x49,
1251     0xaf,
1252     0x6f,
1253     0xcc,
1254     0xfa,
1255     0x65,
1256     0x77,
1257     0x94,
1258     0xb5,
1259     0xa1,
1260     0x0a,
1261     0xa9,
1262     0xd1,
1263     0x8a,
1264     0x39,
1265     0x37,
1266     0xf4,
1267     0x0b,
1268     0xa0,
1269     0xd7,
1270     0x82,
1271     0x27,
1272     0x5e,
1273     0xae,
1274     0x17,
1275     0x17,
1276     0xa1,
1277     0x1e,
1278     0x54,
1279     0x34,
1280     0xbf,
1281     0x6e,
1282     0xc4,
1283     0x8e,
1284     0x99,
1285     0x5d,
1286     0x08,
1287     0xf1,
1288     0x2d,
1289     0x86,
1290     0x9d,
1291     0xa5,
1292     0x20,
1293     0x1b,
1294     0xe5,
1295     0xdf,
1296 };
1297 
1298 #ifndef OPENSSL_NO_DH
1299 static const unsigned char kExampleDHPrivateKeyDER[] = {
1300     0x30, 0x82, 0x02, 0x26, 0x02, 0x01, 0x00, 0x30, 0x82, 0x01, 0x17, 0x06,
1301     0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x03, 0x01, 0x30, 0x82,
1302     0x01, 0x08, 0x02, 0x82, 0x01, 0x01, 0x00, 0xD8, 0x4B, 0x0F, 0x0E, 0x6B,
1303     0x79, 0xE9, 0x23, 0x4E, 0xE4, 0xBE, 0x9A, 0x8F, 0x7A, 0x5C, 0xA3, 0x20,
1304     0xD0, 0x86, 0x6B, 0x95, 0x78, 0x39, 0x59, 0x7A, 0x11, 0x2A, 0x5B, 0x87,
1305     0xA4, 0xFB, 0x2F, 0x99, 0xD0, 0x57, 0xF5, 0xE1, 0xA3, 0xAF, 0x41, 0xD1,
1306     0xCD, 0xA3, 0x94, 0xBB, 0xE5, 0x5A, 0x68, 0xE2, 0xEE, 0x69, 0x56, 0x51,
1307     0xB2, 0xEE, 0xF2, 0xFE, 0x10, 0xC9, 0x55, 0xE3, 0x82, 0x3C, 0x50, 0x0D,
1308     0xF5, 0x82, 0x73, 0xE4, 0xD6, 0x3E, 0x45, 0xB4, 0x89, 0x80, 0xE4, 0xF0,
1309     0x99, 0x85, 0x2B, 0x4B, 0xF9, 0xB8, 0xFD, 0x2C, 0x3C, 0x49, 0x2E, 0xB3,
1310     0x56, 0x7E, 0x99, 0x07, 0xD3, 0xF7, 0xD9, 0xE4, 0x0C, 0x64, 0xC5, 0x7D,
1311     0x03, 0x8E, 0x05, 0x3C, 0x0A, 0x40, 0x17, 0xAD, 0xA8, 0x0F, 0x9B, 0xF4,
1312     0x8B, 0xA7, 0xDB, 0x16, 0x4F, 0x4A, 0x57, 0x0B, 0x89, 0x80, 0x0B, 0x9F,
1313     0x26, 0x56, 0x3F, 0x1D, 0xFA, 0x52, 0x2D, 0x1A, 0x9E, 0xDC, 0x42, 0xA3,
1314     0x2E, 0xA9, 0x87, 0xE3, 0x8B, 0x45, 0x5E, 0xEE, 0x99, 0xB8, 0x30, 0x15,
1315     0x58, 0xA3, 0x5F, 0xB5, 0x69, 0xD8, 0x0C, 0xE8, 0x6B, 0x36, 0xD8, 0xAB,
1316     0xD8, 0xE4, 0x77, 0x46, 0x13, 0xA2, 0x15, 0xB3, 0x9C, 0xAD, 0x99, 0x91,
1317     0xE5, 0xA3, 0x30, 0x7D, 0x40, 0x70, 0xB3, 0x32, 0x5E, 0xAF, 0x96, 0x8D,
1318     0xE6, 0x3F, 0x47, 0xA3, 0x18, 0xDA, 0xE1, 0x9A, 0x20, 0x11, 0xE1, 0x49,
1319     0x51, 0x45, 0xE3, 0x8C, 0xA5, 0x56, 0x39, 0x67, 0xCB, 0x9D, 0xCF, 0xBA,
1320     0xF4, 0x46, 0x4E, 0x0A, 0xB6, 0x0B, 0xA9, 0xB4, 0xF6, 0xF1, 0x6A, 0xC8,
1321     0x63, 0xE2, 0xB4, 0xB2, 0x9F, 0x44, 0xAA, 0x0A, 0xDA, 0x53, 0xF7, 0x52,
1322     0x14, 0x57, 0xEE, 0x2C, 0x5D, 0x31, 0x9C, 0x27, 0x03, 0x64, 0x9E, 0xC0,
1323     0x1E, 0x4B, 0x1B, 0x4F, 0xEE, 0xA6, 0x3F, 0xC1, 0x3E, 0x61, 0x93, 0x02,
1324     0x01, 0x02, 0x04, 0x82, 0x01, 0x04, 0x02, 0x82, 0x01, 0x00, 0x7E, 0xC2,
1325     0x04, 0xF9, 0x95, 0xC7, 0xEF, 0x96, 0xBE, 0xA0, 0x9D, 0x2D, 0xC3, 0x0C,
1326     0x3A, 0x67, 0x02, 0x7C, 0x7D, 0x3B, 0xC9, 0xB1, 0xDE, 0x13, 0x97, 0x64,
1327     0xEF, 0x87, 0x80, 0x4F, 0xBF, 0xA2, 0xAC, 0x18, 0x6B, 0xD5, 0xB2, 0x42,
1328     0x0F, 0xDA, 0x28, 0x40, 0x93, 0x40, 0xB2, 0x1E, 0x80, 0xB0, 0x6C, 0xDE,
1329     0x9C, 0x54, 0xA4, 0xB4, 0x68, 0x29, 0xE0, 0x13, 0x57, 0x1D, 0xC9, 0x87,
1330     0xC0, 0xDE, 0x2F, 0x1D, 0x72, 0xF0, 0xC0, 0xE4, 0x4E, 0x04, 0x48, 0xF5,
1331     0x2D, 0x8D, 0x9A, 0x1B, 0xE5, 0xEB, 0x06, 0xAB, 0x7C, 0x74, 0x10, 0x3C,
1332     0xA8, 0x2D, 0x39, 0xBC, 0xE3, 0x15, 0x3E, 0x63, 0x37, 0x8C, 0x1B, 0xF1,
1333     0xB3, 0x99, 0xB6, 0xAE, 0x5A, 0xEB, 0xB3, 0x3D, 0x30, 0x39, 0x69, 0xDB,
1334     0xF2, 0x4F, 0x94, 0xB7, 0x71, 0xAF, 0xBA, 0x5C, 0x1F, 0xF8, 0x6B, 0xE5,
1335     0xD1, 0xB1, 0x00, 0x81, 0xE2, 0x6D, 0xEC, 0x65, 0xF7, 0x7E, 0xCE, 0x03,
1336     0x84, 0x68, 0x42, 0x6A, 0x8B, 0x47, 0x8E, 0x4A, 0x88, 0xDE, 0x82, 0xDD,
1337     0xAF, 0xA9, 0x6F, 0x18, 0xF7, 0xC6, 0xE2, 0xB9, 0x97, 0xCE, 0x47, 0x8F,
1338     0x85, 0x19, 0x61, 0x42, 0x67, 0x21, 0x7D, 0x13, 0x6E, 0xB5, 0x5A, 0x62,
1339     0xF3, 0x08, 0xE2, 0x70, 0x3B, 0x0E, 0x85, 0x3C, 0xA1, 0xD3, 0xED, 0x7A,
1340     0x43, 0xD6, 0xDE, 0x30, 0x5C, 0x48, 0xB2, 0x99, 0xAB, 0x3E, 0x65, 0xA6,
1341     0x66, 0x80, 0x22, 0xFF, 0x92, 0xC1, 0x42, 0x1C, 0x30, 0x87, 0x74, 0x1E,
1342     0x53, 0x57, 0x7C, 0xF8, 0x77, 0x51, 0xF1, 0x74, 0x16, 0xF4, 0x45, 0x26,
1343     0x77, 0x0A, 0x05, 0x96, 0x13, 0x12, 0x06, 0x86, 0x2B, 0xB8, 0x49, 0x82,
1344     0x69, 0x43, 0x0A, 0x57, 0xA7, 0x30, 0x19, 0x4C, 0xB8, 0x47, 0x82, 0x6E,
1345     0x64, 0x7A, 0x06, 0x13, 0x5A, 0x82, 0x98, 0xD6, 0x7A, 0x09, 0xEC, 0x03,
1346     0x8D, 0x03
1347 };
1348 #endif /* OPENSSL_NO_DH */
1349 
1350 #ifndef OPENSSL_NO_EC
1351 /*
1352  * kExampleECKeyDER is a sample EC private key encoded as an ECPrivateKey
1353  * structure.
1354  */
1355 static const unsigned char kExampleECKeyDER[] = {
1356     0x30,
1357     0x77,
1358     0x02,
1359     0x01,
1360     0x01,
1361     0x04,
1362     0x20,
1363     0x07,
1364     0x0f,
1365     0x08,
1366     0x72,
1367     0x7a,
1368     0xd4,
1369     0xa0,
1370     0x4a,
1371     0x9c,
1372     0xdd,
1373     0x59,
1374     0xc9,
1375     0x4d,
1376     0x89,
1377     0x68,
1378     0x77,
1379     0x08,
1380     0xb5,
1381     0x6f,
1382     0xc9,
1383     0x5d,
1384     0x30,
1385     0x77,
1386     0x0e,
1387     0xe8,
1388     0xd1,
1389     0xc9,
1390     0xce,
1391     0x0a,
1392     0x8b,
1393     0xb4,
1394     0x6a,
1395     0xa0,
1396     0x0a,
1397     0x06,
1398     0x08,
1399     0x2a,
1400     0x86,
1401     0x48,
1402     0xce,
1403     0x3d,
1404     0x03,
1405     0x01,
1406     0x07,
1407     0xa1,
1408     0x44,
1409     0x03,
1410     0x42,
1411     0x00,
1412     0x04,
1413     0xe6,
1414     0x2b,
1415     0x69,
1416     0xe2,
1417     0xbf,
1418     0x65,
1419     0x9f,
1420     0x97,
1421     0xbe,
1422     0x2f,
1423     0x1e,
1424     0x0d,
1425     0x94,
1426     0x8a,
1427     0x4c,
1428     0xd5,
1429     0x97,
1430     0x6b,
1431     0xb7,
1432     0xa9,
1433     0x1e,
1434     0x0d,
1435     0x46,
1436     0xfb,
1437     0xdd,
1438     0xa9,
1439     0xa9,
1440     0x1e,
1441     0x9d,
1442     0xdc,
1443     0xba,
1444     0x5a,
1445     0x01,
1446     0xe7,
1447     0xd6,
1448     0x97,
1449     0xa8,
1450     0x0a,
1451     0x18,
1452     0xf9,
1453     0xc3,
1454     0xc4,
1455     0xa3,
1456     0x1e,
1457     0x56,
1458     0xe2,
1459     0x7c,
1460     0x83,
1461     0x48,
1462     0xdb,
1463     0x16,
1464     0x1a,
1465     0x1c,
1466     0xf5,
1467     0x1d,
1468     0x7e,
1469     0xf1,
1470     0x94,
1471     0x2d,
1472     0x4b,
1473     0xcf,
1474     0x72,
1475     0x22,
1476     0xc1,
1477 };
1478 
1479 /* P-384 sample EC private key in PKCS8 format (no public key) */
1480 static const unsigned char kExampleECKey2DER[] = {
1481     0x30, 0x4E, 0x02, 0x01, 0x00, 0x30, 0x10, 0x06, 0x07, 0x2A, 0x86, 0x48,
1482     0xCE, 0x3D, 0x02, 0x01, 0x06, 0x05, 0x2B, 0x81, 0x04, 0x00, 0x22, 0x04,
1483     0x37, 0x30, 0x35, 0x02, 0x01, 0x01, 0x04, 0x30, 0x73, 0xE3, 0x3A, 0x05,
1484     0xF2, 0xB6, 0x99, 0x6D, 0x0C, 0x33, 0x7F, 0x15, 0x9E, 0x10, 0xA9, 0x17,
1485     0x4C, 0x0A, 0x82, 0x57, 0x71, 0x13, 0x7A, 0xAC, 0x46, 0xA2, 0x5E, 0x1C,
1486     0xE0, 0xC7, 0xB2, 0xF8, 0x20, 0x40, 0xC2, 0x27, 0xC8, 0xBE, 0x02, 0x7E,
1487     0x96, 0x69, 0xE0, 0x04, 0xCB, 0x89, 0x0B, 0x42
1488 };
1489 
1490 #ifndef OPENSSL_NO_ECX
1491 static const unsigned char kExampleECXKey2DER[] = {
1492     0x30, 0x2E, 0x02, 0x01, 0x00, 0x30, 0x05, 0x06, 0x03, 0x2b, 0x65, 0x6e,
1493     0x04, 0x22, 0x04, 0x20, 0xc8, 0xa9, 0xd5, 0xa9, 0x10, 0x91, 0xad, 0x85,
1494     0x1c, 0x66, 0x8b, 0x07, 0x36, 0xc1, 0xc9, 0xa0, 0x29, 0x36, 0xc0, 0xd3,
1495     0xad, 0x62, 0x67, 0x08, 0x58, 0x08, 0x80, 0x47, 0xba, 0x05, 0x74, 0x75
1496 };
1497 #endif
1498 #endif
1499 
1500 typedef struct APK_DATA_st {
1501     const unsigned char *kder;
1502     size_t size;
1503     int evptype;
1504 } APK_DATA;
1505 
1506 static APK_DATA keydata[] = {
1507     { kExampleRSAKeyDER, sizeof(kExampleRSAKeyDER), EVP_PKEY_RSA },
1508     { kExampleRSAKeyPKCS8, sizeof(kExampleRSAKeyPKCS8), EVP_PKEY_RSA },
1509 #ifndef OPENSSL_NO_EC
1510 #ifndef OPENSSL_NO_ECX
1511     { kExampleECXKey2DER, sizeof(kExampleECXKey2DER), EVP_PKEY_X25519 },
1512 #endif
1513     { kExampleECKeyDER, sizeof(kExampleECKeyDER), EVP_PKEY_EC },
1514     { kExampleECKey2DER, sizeof(kExampleECKey2DER), EVP_PKEY_EC },
1515 #endif
1516 #ifndef OPENSSL_NO_DH
1517     { kExampleDHPrivateKeyDER, sizeof(kExampleDHPrivateKeyDER), EVP_PKEY_DH },
1518 #endif
1519 };
1520 
pkey_has_private(EVP_PKEY * key,const char * privtag,int use_octstring)1521 static int pkey_has_private(EVP_PKEY *key, const char *privtag,
1522     int use_octstring)
1523 {
1524     int ret = 0;
1525 
1526     if (use_octstring) {
1527         unsigned char buf[64];
1528 
1529         ret = EVP_PKEY_get_octet_string_param(key, privtag, buf, sizeof(buf),
1530             NULL);
1531     } else {
1532         BIGNUM *bn = NULL;
1533 
1534         ret = EVP_PKEY_get_bn_param(key, privtag, &bn);
1535         BN_free(bn);
1536     }
1537     return ret;
1538 }
1539 
do_pkey_tofrom_data_select(EVP_PKEY * key,const char * keytype)1540 static int do_pkey_tofrom_data_select(EVP_PKEY *key, const char *keytype)
1541 {
1542     int ret = 0;
1543     OSSL_PARAM *pub_params = NULL, *keypair_params = NULL;
1544     EVP_PKEY *fromkey = NULL, *fromkeypair = NULL;
1545     EVP_PKEY_CTX *fromctx = NULL;
1546     const char *privtag = strcmp(keytype, "RSA") == 0 ? "d" : "priv";
1547     const int use_octstring = strcmp(keytype, "X25519") == 0;
1548 
1549     /*
1550      * Select only the public key component when using EVP_PKEY_todata() and
1551      * check that the resulting param array does not contain a private key.
1552      */
1553     if (!TEST_int_eq(EVP_PKEY_todata(key, EVP_PKEY_PUBLIC_KEY, &pub_params), 1)
1554         || !TEST_ptr_null(OSSL_PARAM_locate(pub_params, privtag)))
1555         goto end;
1556     /*
1557      * Select the keypair when using EVP_PKEY_todata() and check that
1558      * the param array contains a private key.
1559      */
1560     if (!TEST_int_eq(EVP_PKEY_todata(key, EVP_PKEY_KEYPAIR, &keypair_params), 1)
1561         || !TEST_ptr(OSSL_PARAM_locate(keypair_params, privtag)))
1562         goto end;
1563 
1564     /*
1565      * Select only the public key when using EVP_PKEY_fromdata() and check that
1566      * the resulting key does not contain a private key.
1567      */
1568     if (!TEST_ptr(fromctx = EVP_PKEY_CTX_new_from_name(mainctx, keytype, NULL))
1569         || !TEST_int_eq(EVP_PKEY_fromdata_init(fromctx), 1)
1570         || !TEST_int_eq(EVP_PKEY_fromdata(fromctx, &fromkey, EVP_PKEY_PUBLIC_KEY,
1571                             keypair_params),
1572             1)
1573         || !TEST_false(pkey_has_private(fromkey, privtag, use_octstring)))
1574         goto end;
1575     /*
1576      * Select the keypair when using EVP_PKEY_fromdata() and check that
1577      * the resulting key contains a private key.
1578      */
1579     if (!TEST_int_eq(EVP_PKEY_fromdata(fromctx, &fromkeypair,
1580                          EVP_PKEY_KEYPAIR, keypair_params),
1581             1)
1582         || !TEST_true(pkey_has_private(fromkeypair, privtag, use_octstring)))
1583         goto end;
1584     ret = 1;
1585 end:
1586     EVP_PKEY_free(fromkeypair);
1587     EVP_PKEY_free(fromkey);
1588     EVP_PKEY_CTX_free(fromctx);
1589     OSSL_PARAM_free(keypair_params);
1590     OSSL_PARAM_free(pub_params);
1591     return ret;
1592 }
1593 
1594 #ifndef OPENSSL_NO_DH
test_dh_tofrom_data_select(void)1595 static int test_dh_tofrom_data_select(void)
1596 {
1597     int ret;
1598     OSSL_PARAM params[2];
1599     EVP_PKEY *key = NULL;
1600     EVP_PKEY_CTX *gctx = NULL;
1601 #ifndef OPENSSL_NO_DEPRECATED_3_0
1602     const DH *dhkey;
1603     const BIGNUM *privkey;
1604 #endif
1605 
1606     params[0] = OSSL_PARAM_construct_utf8_string("group", "ffdhe2048", 0);
1607     params[1] = OSSL_PARAM_construct_end();
1608     ret = TEST_ptr(gctx = EVP_PKEY_CTX_new_from_name(mainctx, "DHX", NULL))
1609         && TEST_int_gt(EVP_PKEY_keygen_init(gctx), 0)
1610         && TEST_true(EVP_PKEY_CTX_set_params(gctx, params))
1611         && TEST_int_gt(EVP_PKEY_generate(gctx, &key), 0)
1612         && TEST_true(do_pkey_tofrom_data_select(key, "DHX"));
1613 #ifndef OPENSSL_NO_DEPRECATED_3_0
1614     ret = ret && TEST_ptr(dhkey = EVP_PKEY_get0_DH(key))
1615         && TEST_ptr(privkey = DH_get0_priv_key(dhkey))
1616         && TEST_int_le(BN_num_bits(privkey), 225);
1617 #endif
1618     EVP_PKEY_free(key);
1619     EVP_PKEY_CTX_free(gctx);
1620     return ret;
1621 }
1622 
test_dh_paramgen(void)1623 static int test_dh_paramgen(void)
1624 {
1625     int ret;
1626     OSSL_PARAM params[3];
1627     EVP_PKEY *pkey = NULL;
1628     EVP_PKEY_CTX *gctx = NULL;
1629     unsigned int pbits = 512; /* minimum allowed for speed */
1630 
1631     params[0] = OSSL_PARAM_construct_uint(OSSL_PKEY_PARAM_FFC_PBITS, &pbits);
1632     params[1] = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_FFC_TYPE,
1633         "generator", 0);
1634     params[2] = OSSL_PARAM_construct_end();
1635 
1636     ret = TEST_ptr(gctx = EVP_PKEY_CTX_new_from_name(mainctx, "DH", NULL))
1637         && TEST_int_gt(EVP_PKEY_paramgen_init(gctx), 0)
1638         && TEST_true(EVP_PKEY_CTX_set_params(gctx, params))
1639         && TEST_true(EVP_PKEY_paramgen(gctx, &pkey))
1640         && TEST_ptr(pkey);
1641 
1642     EVP_PKEY_CTX_free(gctx);
1643     gctx = NULL;
1644 
1645     ret = ret && TEST_ptr(gctx = EVP_PKEY_CTX_new_from_pkey(mainctx, pkey, NULL))
1646         && TEST_int_eq(EVP_PKEY_param_check(gctx), 1)
1647         && TEST_int_eq(EVP_PKEY_param_check_quick(gctx), 1);
1648 
1649     EVP_PKEY_CTX_free(gctx);
1650     EVP_PKEY_free(pkey);
1651     return ret;
1652 }
1653 
set_fromdata_string(EVP_PKEY_CTX * ctx,const char * name,char * value)1654 static int set_fromdata_string(EVP_PKEY_CTX *ctx, const char *name, char *value)
1655 {
1656     int ret;
1657     OSSL_PARAM params[2];
1658     EVP_PKEY *pkey = NULL;
1659 
1660     if (EVP_PKEY_fromdata_init(ctx) != 1)
1661         return -1;
1662     params[0] = OSSL_PARAM_construct_utf8_string(name, value, 0);
1663     params[1] = OSSL_PARAM_construct_end();
1664     ret = EVP_PKEY_fromdata(ctx, &pkey, EVP_PKEY_KEY_PARAMETERS, params);
1665     EVP_PKEY_free(pkey);
1666     return ret;
1667 }
1668 
set_fromdata_uint(EVP_PKEY_CTX * ctx,const char * name)1669 static int set_fromdata_uint(EVP_PKEY_CTX *ctx, const char *name)
1670 {
1671     int ret;
1672     unsigned int tmp = 0;
1673     OSSL_PARAM params[2];
1674     EVP_PKEY *pkey = NULL;
1675 
1676     if (EVP_PKEY_fromdata_init(ctx) != 1)
1677         return -1;
1678     params[0] = OSSL_PARAM_construct_uint(name, &tmp);
1679     params[1] = OSSL_PARAM_construct_end();
1680     ret = EVP_PKEY_fromdata(ctx, &pkey, EVP_PKEY_KEY_PARAMETERS, params);
1681     EVP_PKEY_free(pkey);
1682     return ret;
1683 }
1684 
test_dh_paramfromdata(void)1685 static int test_dh_paramfromdata(void)
1686 {
1687     EVP_PKEY_CTX *ctx = NULL;
1688     int ret = 0;
1689 
1690     /* Test failure paths for FFC - mainly due to setting the wrong param type */
1691     ret = TEST_ptr(ctx = EVP_PKEY_CTX_new_from_name(mainctx, "DH", NULL))
1692         && TEST_int_eq(set_fromdata_uint(ctx, OSSL_PKEY_PARAM_GROUP_NAME), 0)
1693         && TEST_int_eq(set_fromdata_string(ctx, OSSL_PKEY_PARAM_GROUP_NAME, "bad"), 0)
1694         && TEST_int_eq(set_fromdata_string(ctx, OSSL_PKEY_PARAM_FFC_P, "bad"), 0)
1695         && TEST_int_eq(set_fromdata_string(ctx, OSSL_PKEY_PARAM_FFC_GINDEX, "bad"), 0)
1696         && TEST_int_eq(set_fromdata_string(ctx, OSSL_PKEY_PARAM_FFC_PCOUNTER, "bad"), 0)
1697         && TEST_int_eq(set_fromdata_string(ctx, OSSL_PKEY_PARAM_FFC_COFACTOR, "bad"), 0)
1698         && TEST_int_eq(set_fromdata_string(ctx, OSSL_PKEY_PARAM_FFC_H, "bad"), 0)
1699         && TEST_int_eq(set_fromdata_uint(ctx, OSSL_PKEY_PARAM_FFC_SEED), 0)
1700         && TEST_int_eq(set_fromdata_string(ctx, OSSL_PKEY_PARAM_FFC_VALIDATE_PQ, "bad"), 0)
1701         && TEST_int_eq(set_fromdata_string(ctx, OSSL_PKEY_PARAM_FFC_VALIDATE_G, "bad"), 0)
1702         && TEST_int_eq(set_fromdata_string(ctx, OSSL_PKEY_PARAM_FFC_VALIDATE_LEGACY, "bad"), 0)
1703         && TEST_int_eq(set_fromdata_uint(ctx, OSSL_PKEY_PARAM_FFC_DIGEST), 0);
1704 
1705     EVP_PKEY_CTX_free(ctx);
1706     return ret;
1707 }
1708 
1709 #endif
1710 
1711 /* Test that calling EVP_PKEY_Q_keygen() for a non-standard keytype works as expected */
test_new_keytype(void)1712 static int test_new_keytype(void)
1713 {
1714     int ret = 0;
1715     EVP_PKEY *key = NULL;
1716     OSSL_PROVIDER *tlsprov = NULL;
1717     EVP_PKEY_CTX *ctx = NULL;
1718     size_t outlen, secretlen, secretlen2;
1719     unsigned char *out = NULL, *secret = NULL, *secret2 = NULL;
1720 
1721     /* without tls-provider key should not be create-able */
1722     if (TEST_ptr(key = EVP_PKEY_Q_keygen(mainctx, NULL, "XOR")))
1723         goto err;
1724     /* prepare & load tls-provider */
1725     if (!TEST_true(OSSL_PROVIDER_add_builtin(mainctx, "tls-provider",
1726             tls_provider_init))
1727         || !TEST_ptr(tlsprov = OSSL_PROVIDER_load(mainctx, "tls-provider")))
1728         goto err;
1729     /* now try creating key again, should work this time */
1730     if (!TEST_ptr(key = EVP_PKEY_Q_keygen(mainctx, NULL, "XOR")))
1731         goto err;
1732     /* now do encaps/decaps to validate all is good */
1733     if (!TEST_ptr(ctx = EVP_PKEY_CTX_new(key, NULL))
1734         || !TEST_int_eq(EVP_PKEY_encapsulate_init(ctx, NULL), 1)
1735         || !TEST_int_eq(EVP_PKEY_encapsulate(ctx, NULL, &outlen, NULL, &secretlen), 1))
1736         goto err;
1737     out = OPENSSL_malloc(outlen);
1738     secret = OPENSSL_malloc(secretlen);
1739     secret2 = OPENSSL_malloc(secretlen);
1740     if (out == NULL || secret == NULL || secret2 == NULL
1741         || !TEST_int_eq(EVP_PKEY_encapsulate(ctx, out, &outlen, secret, &secretlen), 1)
1742         || !TEST_int_eq(EVP_PKEY_decapsulate_init(ctx, NULL), 1)
1743         || !TEST_int_eq(EVP_PKEY_decapsulate(ctx, secret2, &secretlen2, out, outlen), 1)
1744         || !TEST_mem_eq(secret, secretlen, secret2, secretlen2))
1745         goto err;
1746     ret = OSSL_PROVIDER_unload(tlsprov);
1747 
1748 err:
1749     OPENSSL_free(out);
1750     OPENSSL_free(secret);
1751     OPENSSL_free(secret2);
1752     EVP_PKEY_CTX_free(ctx);
1753     EVP_PKEY_free(key);
1754     return ret;
1755 }
1756 
1757 #ifndef OPENSSL_NO_EC
1758 
test_ec_d2i_i2d_pubkey(void)1759 static int test_ec_d2i_i2d_pubkey(void)
1760 {
1761     int ret = 0;
1762     FILE *fp = NULL;
1763     EVP_PKEY *key = NULL, *outkey = NULL;
1764     static const char *filename = "pubkey.der";
1765 
1766     if (!TEST_ptr(fp = fopen(filename, "wb"))
1767         || !TEST_ptr(key = EVP_PKEY_Q_keygen(mainctx, NULL, "EC", "P-256"))
1768         || !TEST_true(i2d_PUBKEY_fp(fp, key))
1769         || !TEST_int_eq(fclose(fp), 0))
1770         goto err;
1771     fp = NULL;
1772 
1773     if (!TEST_ptr(fp = fopen(filename, "rb"))
1774         || !TEST_ptr(outkey = d2i_PUBKEY_ex_fp(fp, NULL, mainctx, NULL))
1775         || !TEST_int_eq(EVP_PKEY_eq(key, outkey), 1))
1776         goto err;
1777 
1778     ret = 1;
1779 
1780 err:
1781     EVP_PKEY_free(outkey);
1782     EVP_PKEY_free(key);
1783     fclose(fp);
1784     return ret;
1785 }
1786 
test_ec_tofrom_data_select(void)1787 static int test_ec_tofrom_data_select(void)
1788 {
1789     int ret;
1790     EVP_PKEY *key = NULL;
1791 
1792     ret = TEST_ptr(key = EVP_PKEY_Q_keygen(mainctx, NULL, "EC", "P-256"))
1793         && TEST_true(do_pkey_tofrom_data_select(key, "EC"));
1794     EVP_PKEY_free(key);
1795     return ret;
1796 }
1797 
1798 #ifndef OPENSSL_NO_ECX
test_ecx_tofrom_data_select(void)1799 static int test_ecx_tofrom_data_select(void)
1800 {
1801     int ret;
1802     EVP_PKEY *key = NULL;
1803 
1804     ret = TEST_ptr(key = EVP_PKEY_Q_keygen(mainctx, NULL, "X25519"))
1805         && TEST_true(do_pkey_tofrom_data_select(key, "X25519"));
1806     EVP_PKEY_free(key);
1807     return ret;
1808 }
1809 #endif
1810 #endif
1811 
1812 #ifndef OPENSSL_NO_SM2
test_sm2_tofrom_data_select(void)1813 static int test_sm2_tofrom_data_select(void)
1814 {
1815     int ret;
1816     EVP_PKEY *key = NULL;
1817 
1818     ret = TEST_ptr(key = EVP_PKEY_Q_keygen(mainctx, NULL, "SM2"))
1819         && TEST_true(do_pkey_tofrom_data_select(key, "SM2"));
1820     EVP_PKEY_free(key);
1821     return ret;
1822 }
1823 #endif
1824 
test_rsa_tofrom_data_select(void)1825 static int test_rsa_tofrom_data_select(void)
1826 {
1827     int ret;
1828     EVP_PKEY *key = NULL;
1829     const unsigned char *pdata = kExampleRSAKeyDER;
1830     int pdata_len = sizeof(kExampleRSAKeyDER);
1831 
1832     ret = TEST_ptr(key = d2i_AutoPrivateKey_ex(NULL, &pdata, pdata_len,
1833                        mainctx, NULL))
1834         && TEST_true(do_pkey_tofrom_data_select(key, "RSA"));
1835     EVP_PKEY_free(key);
1836     return ret;
1837 }
1838 
1839 /* This is the equivalent of test_d2i_AutoPrivateKey in evp_extra_test */
test_d2i_AutoPrivateKey_ex(int i)1840 static int test_d2i_AutoPrivateKey_ex(int i)
1841 {
1842     int ret = 0;
1843     const unsigned char *p;
1844     EVP_PKEY *pkey = NULL;
1845     const APK_DATA *ak = &keydata[i];
1846     const unsigned char *input = ak->kder;
1847     size_t input_len = ak->size;
1848     int expected_id = ak->evptype;
1849     BIGNUM *p_bn = NULL;
1850     BIGNUM *g_bn = NULL;
1851     BIGNUM *priv_bn = NULL;
1852 
1853     p = input;
1854     if (!TEST_ptr(pkey = d2i_AutoPrivateKey_ex(NULL, &p, input_len, mainctx,
1855                       NULL))
1856         || !TEST_ptr_eq(p, input + input_len)
1857         || !TEST_int_eq(EVP_PKEY_get_id(pkey), expected_id))
1858         goto done;
1859 
1860     if (ak->evptype == EVP_PKEY_RSA) {
1861         if (!TEST_true(EVP_PKEY_get_bn_param(pkey, OSSL_PKEY_PARAM_RSA_D,
1862                 &priv_bn)))
1863             goto done;
1864     } else if (ak->evptype == EVP_PKEY_X25519) {
1865         unsigned char buffer[32];
1866         size_t len;
1867 
1868         if (!TEST_true(EVP_PKEY_get_octet_string_param(pkey,
1869                 OSSL_PKEY_PARAM_PRIV_KEY,
1870                 buffer, sizeof(buffer),
1871                 &len)))
1872             goto done;
1873     } else {
1874         if (!TEST_true(EVP_PKEY_get_bn_param(pkey, OSSL_PKEY_PARAM_PRIV_KEY,
1875                 &priv_bn)))
1876             goto done;
1877     }
1878 
1879     if (ak->evptype == EVP_PKEY_DH) {
1880         if (!TEST_true(EVP_PKEY_get_bn_param(pkey, OSSL_PKEY_PARAM_FFC_P, &p_bn))
1881             || !TEST_true(EVP_PKEY_get_bn_param(pkey, OSSL_PKEY_PARAM_FFC_G,
1882                 &g_bn)))
1883             goto done;
1884     }
1885 
1886     ret = 1;
1887 done:
1888     BN_free(p_bn);
1889     BN_free(g_bn);
1890     BN_free(priv_bn);
1891     EVP_PKEY_free(pkey);
1892     return ret;
1893 }
1894 
1895 #ifndef OPENSSL_NO_DES
test_pkcs8key_nid_bio(void)1896 static int test_pkcs8key_nid_bio(void)
1897 {
1898     int ret;
1899     const int nid = NID_pbe_WithSHA1And3_Key_TripleDES_CBC;
1900     static const char pwd[] = "PASSWORD";
1901     EVP_PKEY *pkey = NULL, *pkey_dec = NULL;
1902     BIO *in = NULL, *enc_bio = NULL;
1903     char *enc_data = NULL;
1904     long enc_datalen = 0;
1905     OSSL_PROVIDER *provider = NULL;
1906 
1907     ret = TEST_ptr(provider = OSSL_PROVIDER_load(NULL, "default"))
1908         && TEST_ptr(enc_bio = BIO_new(BIO_s_mem()))
1909         && TEST_ptr(in = BIO_new_mem_buf(kExampleRSAKeyPKCS8,
1910                         sizeof(kExampleRSAKeyPKCS8)))
1911         && TEST_ptr(pkey = d2i_PrivateKey_ex_bio(in, NULL, NULL, NULL))
1912         && TEST_int_eq(i2d_PKCS8PrivateKey_nid_bio(enc_bio, pkey, nid,
1913                            pwd, sizeof(pwd) - 1,
1914                            NULL, NULL),
1915             1)
1916         && TEST_int_gt(enc_datalen = BIO_get_mem_data(enc_bio, &enc_data), 0)
1917         && TEST_ptr(pkey_dec = d2i_PKCS8PrivateKey_bio(enc_bio, NULL, NULL,
1918                         (void *)pwd))
1919         && TEST_true(EVP_PKEY_eq(pkey, pkey_dec));
1920 
1921     EVP_PKEY_free(pkey_dec);
1922     EVP_PKEY_free(pkey);
1923     BIO_free(in);
1924     BIO_free(enc_bio);
1925     OSSL_PROVIDER_unload(provider);
1926     return ret;
1927 }
1928 #endif /* OPENSSL_NO_DES */
1929 
test_alternative_default(void)1930 static int test_alternative_default(void)
1931 {
1932     OSSL_LIB_CTX *oldctx;
1933     EVP_MD *sha256;
1934     int ok = 0;
1935 
1936     /*
1937      * setup_tests() loaded the "null" provider in the current default, so
1938      * we know this fetch should fail.
1939      */
1940     if (!TEST_ptr_null(sha256 = EVP_MD_fetch(NULL, "SHA2-256", NULL)))
1941         goto err;
1942 
1943     /*
1944      * Now we switch to our main library context, and try again.  Since no
1945      * providers are loaded in this one, it should fall back to the default.
1946      */
1947     if (!TEST_ptr(oldctx = OSSL_LIB_CTX_set0_default(mainctx))
1948         || !TEST_ptr(sha256 = EVP_MD_fetch(NULL, "SHA2-256", NULL)))
1949         goto err;
1950     EVP_MD_free(sha256);
1951     sha256 = NULL;
1952 
1953     /*
1954      * Switching back should give us our main library context back, and
1955      * fetching SHA2-256 should fail again.
1956      */
1957     if (!TEST_ptr_eq(OSSL_LIB_CTX_set0_default(oldctx), mainctx)
1958         || !TEST_ptr_null(sha256 = EVP_MD_fetch(NULL, "SHA2-256", NULL)))
1959         goto err;
1960 
1961     ok = 1;
1962 err:
1963     EVP_MD_free(sha256);
1964     return ok;
1965 }
1966 
test_provider_unload_effective(int testid)1967 static int test_provider_unload_effective(int testid)
1968 {
1969     EVP_MD *sha256 = NULL;
1970     OSSL_PROVIDER *provider = NULL;
1971     int ok = 0;
1972 
1973     if (!TEST_ptr(provider = OSSL_PROVIDER_load(NULL, "default"))
1974         || !TEST_ptr(sha256 = EVP_MD_fetch(NULL, "SHA2-256", NULL)))
1975         goto err;
1976 
1977     if (testid > 0) {
1978         OSSL_PROVIDER_unload(provider);
1979         provider = NULL;
1980         EVP_MD_free(sha256);
1981         sha256 = NULL;
1982     } else {
1983         EVP_MD_free(sha256);
1984         sha256 = NULL;
1985         OSSL_PROVIDER_unload(provider);
1986         provider = NULL;
1987     }
1988 
1989     /*
1990      * setup_tests() loaded the "null" provider in the current default, and
1991      * we unloaded it above after the load so we know this fetch should fail.
1992      */
1993     if (!TEST_ptr_null(sha256 = EVP_MD_fetch(NULL, "SHA2-256", NULL)))
1994         goto err;
1995 
1996     ok = 1;
1997 err:
1998     EVP_MD_free(sha256);
1999     OSSL_PROVIDER_unload(provider);
2000     return ok;
2001 }
2002 
test_d2i_PrivateKey_ex(int testid)2003 static int test_d2i_PrivateKey_ex(int testid)
2004 {
2005     int ok = 0;
2006     OSSL_PROVIDER *provider = NULL;
2007     BIO *key_bio = NULL;
2008     EVP_PKEY *pkey = NULL;
2009     int id = (testid == 0) ? 0 : 2;
2010 
2011     if (!TEST_ptr(provider = OSSL_PROVIDER_load(NULL, "default")))
2012         goto err;
2013     if (!TEST_ptr(key_bio = BIO_new_mem_buf(keydata[id].kder, keydata[id].size)))
2014         goto err;
2015     if (!TEST_ptr_null(pkey = PEM_read_bio_PrivateKey(key_bio, NULL, NULL, NULL)))
2016         goto err;
2017 
2018     ERR_clear_error();
2019     if (!TEST_int_ge(BIO_seek(key_bio, 0), 0))
2020         goto err;
2021     ok = TEST_ptr(pkey = d2i_PrivateKey_bio(key_bio, NULL));
2022     TEST_int_eq(ERR_peek_error(), 0);
2023     test_openssl_errors();
2024 
2025 err:
2026     EVP_PKEY_free(pkey);
2027     BIO_free(key_bio);
2028     OSSL_PROVIDER_unload(provider);
2029 
2030     return ok;
2031 }
2032 
test_PEM_read_bio_negative(int testid)2033 static int test_PEM_read_bio_negative(int testid)
2034 {
2035     int ok = 0;
2036     OSSL_PROVIDER *provider = NULL;
2037     BIO *key_bio = NULL;
2038     EVP_PKEY *pkey = NULL;
2039 
2040     if (!TEST_ptr(key_bio = BIO_new_mem_buf(keydata[testid].kder, keydata[testid].size)))
2041         goto err;
2042     ERR_clear_error();
2043     if (!TEST_ptr_null(pkey = PEM_read_bio_PrivateKey(key_bio, NULL, NULL, NULL)))
2044         goto err;
2045     if (!TEST_int_ne(ERR_peek_error(), 0))
2046         goto err;
2047     if (!TEST_ptr(provider = OSSL_PROVIDER_load(NULL, "default")))
2048         goto err;
2049     if (!TEST_int_ge(BIO_seek(key_bio, 0), 0))
2050         goto err;
2051     ERR_clear_error();
2052     if (!TEST_ptr_null(pkey = PEM_read_bio_PrivateKey(key_bio, NULL, NULL, NULL)))
2053         goto err;
2054     if (!TEST_int_ne(ERR_peek_error(), 0))
2055         goto err;
2056 
2057     ok = 1;
2058 
2059 err:
2060     test_openssl_errors();
2061     EVP_PKEY_free(pkey);
2062     BIO_free(key_bio);
2063     OSSL_PROVIDER_unload(provider);
2064 
2065     return ok;
2066 }
2067 
test_PEM_read_bio_negative_wrong_password(int testid)2068 static int test_PEM_read_bio_negative_wrong_password(int testid)
2069 {
2070     int ok = 0;
2071     OSSL_PROVIDER *provider = OSSL_PROVIDER_load(NULL, "default");
2072     EVP_PKEY *read_pkey = NULL;
2073     EVP_PKEY *write_pkey = EVP_RSA_gen(1024);
2074     BIO *key_bio = BIO_new(BIO_s_mem());
2075     const UI_METHOD *undo_ui_method = NULL;
2076     const UI_METHOD *ui_method = NULL;
2077     if (testid > 0)
2078         ui_method = UI_null();
2079 
2080     if (!TEST_ptr(provider))
2081         goto err;
2082     if (!TEST_ptr(key_bio))
2083         goto err;
2084     if (!TEST_ptr(write_pkey))
2085         goto err;
2086     undo_ui_method = UI_get_default_method();
2087     UI_set_default_method(ui_method);
2088 
2089     if (/* Output Encrypted private key in PEM form */
2090         !TEST_true(PEM_write_bio_PrivateKey(key_bio, write_pkey, EVP_aes_256_cbc(),
2091             NULL, 0, NULL, "pass")))
2092         goto err;
2093 
2094     ERR_clear_error();
2095     read_pkey = PEM_read_bio_PrivateKey(key_bio, NULL, NULL, NULL);
2096     if (!TEST_ptr_null(read_pkey))
2097         goto err;
2098 
2099     if (!TEST_int_eq(ERR_GET_REASON(ERR_get_error()), PEM_R_PROBLEMS_GETTING_PASSWORD))
2100         goto err;
2101     ok = 1;
2102 
2103 err:
2104     test_openssl_errors();
2105     EVP_PKEY_free(read_pkey);
2106     EVP_PKEY_free(write_pkey);
2107     BIO_free(key_bio);
2108     OSSL_PROVIDER_unload(provider);
2109     UI_set_default_method(undo_ui_method);
2110 
2111     return ok;
2112 }
2113 
do_fromdata_key_is_equal(const OSSL_PARAM params[],const EVP_PKEY * expected,const char * type)2114 static int do_fromdata_key_is_equal(const OSSL_PARAM params[],
2115     const EVP_PKEY *expected, const char *type)
2116 {
2117     EVP_PKEY_CTX *ctx = NULL;
2118     EVP_PKEY *pkey = NULL;
2119     int ret;
2120 
2121     ret = TEST_ptr(ctx = EVP_PKEY_CTX_new_from_name(mainctx, type, NULL))
2122         && TEST_int_eq(EVP_PKEY_fromdata_init(ctx), 1)
2123         && TEST_int_eq(EVP_PKEY_fromdata(ctx, &pkey,
2124                            EVP_PKEY_KEYPAIR,
2125                            (OSSL_PARAM *)params),
2126             1)
2127         && TEST_true(EVP_PKEY_eq(pkey, expected));
2128     EVP_PKEY_CTX_free(ctx);
2129     EVP_PKEY_free(pkey);
2130     return ret;
2131 }
2132 
2133 #ifndef OPENSSL_NO_DSA
2134 /*
2135  * This data was generated using:
2136  * > openssl genpkey \
2137  *   -genparam -algorithm DSA -pkeyopt type:fips186_4 -text \
2138  *   -pkeyopt gindex:5 -out dsa_param.pem
2139  * > openssl genpkey \
2140  *   -paramfile dsa_param.pem -pkeyopt type:fips186_4 -out dsa_priv.pem
2141  */
2142 static const unsigned char dsa_key[] = {
2143     0x30,
2144     0x82,
2145     0x03,
2146     0x4e,
2147     0x02,
2148     0x01,
2149     0x00,
2150     0x02,
2151     0x82,
2152     0x01,
2153     0x01,
2154     0x00,
2155     0xda,
2156     0xb3,
2157     0x46,
2158     0x4d,
2159     0x54,
2160     0x57,
2161     0xc7,
2162     0xb4,
2163     0x61,
2164     0xa0,
2165     0x6f,
2166     0x66,
2167     0x17,
2168     0xda,
2169     0xeb,
2170     0x90,
2171     0xf0,
2172     0xa3,
2173     0xd1,
2174     0x29,
2175     0xc9,
2176     0x5f,
2177     0xf2,
2178     0x21,
2179     0x3d,
2180     0x85,
2181     0xa3,
2182     0x4a,
2183     0xf0,
2184     0xf8,
2185     0x36,
2186     0x39,
2187     0x1b,
2188     0xe3,
2189     0xee,
2190     0x37,
2191     0x70,
2192     0x06,
2193     0x9b,
2194     0xe8,
2195     0xe3,
2196     0x0a,
2197     0xd2,
2198     0xf1,
2199     0xf6,
2200     0xc4,
2201     0x42,
2202     0x23,
2203     0x1f,
2204     0x74,
2205     0x78,
2206     0xc2,
2207     0x16,
2208     0xf5,
2209     0xce,
2210     0xd6,
2211     0xab,
2212     0xa0,
2213     0xc6,
2214     0xe8,
2215     0x99,
2216     0x3d,
2217     0xf8,
2218     0x8b,
2219     0xfb,
2220     0x47,
2221     0xf8,
2222     0x5e,
2223     0x05,
2224     0x68,
2225     0x6d,
2226     0x8b,
2227     0xa8,
2228     0xad,
2229     0xa1,
2230     0xc2,
2231     0x3a,
2232     0x4e,
2233     0xe0,
2234     0xad,
2235     0xec,
2236     0x38,
2237     0x75,
2238     0x21,
2239     0x55,
2240     0x22,
2241     0xce,
2242     0xa2,
2243     0xe9,
2244     0xe5,
2245     0x3b,
2246     0xd7,
2247     0x44,
2248     0xeb,
2249     0x5a,
2250     0x03,
2251     0x59,
2252     0xa0,
2253     0xc5,
2254     0x7a,
2255     0x92,
2256     0x59,
2257     0x7d,
2258     0x7a,
2259     0x07,
2260     0x80,
2261     0xfc,
2262     0x4e,
2263     0xf8,
2264     0x56,
2265     0x7e,
2266     0xf1,
2267     0x06,
2268     0xe0,
2269     0xba,
2270     0xb2,
2271     0xe7,
2272     0x5b,
2273     0x22,
2274     0x55,
2275     0xee,
2276     0x4b,
2277     0x42,
2278     0x61,
2279     0x67,
2280     0x2c,
2281     0x43,
2282     0x9a,
2283     0x38,
2284     0x2b,
2285     0x17,
2286     0xc2,
2287     0x62,
2288     0x12,
2289     0x8b,
2290     0x0b,
2291     0x22,
2292     0x8c,
2293     0x0c,
2294     0x1c,
2295     0x1c,
2296     0x92,
2297     0xb1,
2298     0xec,
2299     0x70,
2300     0xce,
2301     0x0f,
2302     0x8c,
2303     0xff,
2304     0x8d,
2305     0x21,
2306     0xf9,
2307     0x19,
2308     0x68,
2309     0x4d,
2310     0x32,
2311     0x59,
2312     0x78,
2313     0x42,
2314     0x1d,
2315     0x0c,
2316     0xc5,
2317     0x1a,
2318     0xcb,
2319     0x28,
2320     0xe2,
2321     0xc1,
2322     0x1a,
2323     0x35,
2324     0xf1,
2325     0x42,
2326     0x0a,
2327     0x19,
2328     0x39,
2329     0xfa,
2330     0x83,
2331     0xd1,
2332     0xb4,
2333     0xaa,
2334     0x69,
2335     0x0f,
2336     0xc2,
2337     0x8e,
2338     0xf9,
2339     0x59,
2340     0x2c,
2341     0xee,
2342     0x11,
2343     0xfc,
2344     0x3e,
2345     0x4b,
2346     0x44,
2347     0xfb,
2348     0x9a,
2349     0x32,
2350     0xc8,
2351     0x78,
2352     0x23,
2353     0x56,
2354     0x85,
2355     0x49,
2356     0x21,
2357     0x43,
2358     0x12,
2359     0x79,
2360     0xbd,
2361     0xa0,
2362     0x70,
2363     0x47,
2364     0x2f,
2365     0xae,
2366     0xb6,
2367     0xd7,
2368     0x6c,
2369     0xc6,
2370     0x07,
2371     0x76,
2372     0xa9,
2373     0x8a,
2374     0xa2,
2375     0x16,
2376     0x02,
2377     0x89,
2378     0x1f,
2379     0x1a,
2380     0xd1,
2381     0xa2,
2382     0x96,
2383     0x56,
2384     0xd1,
2385     0x1f,
2386     0x10,
2387     0xe1,
2388     0xe5,
2389     0x9f,
2390     0x3f,
2391     0xdd,
2392     0x09,
2393     0x0c,
2394     0x40,
2395     0x90,
2396     0x71,
2397     0xef,
2398     0x14,
2399     0x41,
2400     0x02,
2401     0x82,
2402     0x3a,
2403     0x6b,
2404     0xe1,
2405     0xf8,
2406     0x2c,
2407     0x5d,
2408     0xbe,
2409     0xfd,
2410     0x1b,
2411     0x02,
2412     0x1d,
2413     0x00,
2414     0xe0,
2415     0x20,
2416     0xe0,
2417     0x7c,
2418     0x02,
2419     0x16,
2420     0xa7,
2421     0x6c,
2422     0x6a,
2423     0x19,
2424     0xba,
2425     0xd5,
2426     0x83,
2427     0x73,
2428     0xf3,
2429     0x7d,
2430     0x31,
2431     0xef,
2432     0xa7,
2433     0xe1,
2434     0x5d,
2435     0x5b,
2436     0x7f,
2437     0xf3,
2438     0xfc,
2439     0xda,
2440     0x84,
2441     0x31,
2442     0x02,
2443     0x82,
2444     0x01,
2445     0x01,
2446     0x00,
2447     0x83,
2448     0xdb,
2449     0xa1,
2450     0xbc,
2451     0x3e,
2452     0xc7,
2453     0x29,
2454     0xa5,
2455     0x6a,
2456     0x5c,
2457     0x2c,
2458     0xe8,
2459     0x7a,
2460     0x8c,
2461     0x7e,
2462     0xe8,
2463     0xb8,
2464     0x3e,
2465     0x13,
2466     0x47,
2467     0xcd,
2468     0x36,
2469     0x7e,
2470     0x79,
2471     0x30,
2472     0x7a,
2473     0x28,
2474     0x03,
2475     0xd3,
2476     0xd4,
2477     0xd2,
2478     0xe3,
2479     0xee,
2480     0x3b,
2481     0x46,
2482     0xda,
2483     0xe0,
2484     0x71,
2485     0xe6,
2486     0xcf,
2487     0x46,
2488     0x86,
2489     0x0a,
2490     0x37,
2491     0x57,
2492     0xb6,
2493     0xe9,
2494     0xcf,
2495     0xa1,
2496     0x78,
2497     0x19,
2498     0xb8,
2499     0x72,
2500     0x9f,
2501     0x30,
2502     0x8c,
2503     0x2a,
2504     0x04,
2505     0x7c,
2506     0x2f,
2507     0x0c,
2508     0x27,
2509     0xa7,
2510     0xb3,
2511     0x23,
2512     0xe0,
2513     0x46,
2514     0xf2,
2515     0x75,
2516     0x0c,
2517     0x03,
2518     0x4c,
2519     0xad,
2520     0xfb,
2521     0xc1,
2522     0xcb,
2523     0x28,
2524     0xcd,
2525     0xa0,
2526     0x63,
2527     0xdb,
2528     0x44,
2529     0x88,
2530     0xe0,
2531     0xda,
2532     0x6c,
2533     0x5b,
2534     0x89,
2535     0xb2,
2536     0x5b,
2537     0x40,
2538     0x6d,
2539     0xeb,
2540     0x78,
2541     0x7a,
2542     0xd5,
2543     0xaf,
2544     0x40,
2545     0x52,
2546     0x46,
2547     0x63,
2548     0x92,
2549     0x13,
2550     0x0d,
2551     0xee,
2552     0xee,
2553     0xf9,
2554     0x53,
2555     0xca,
2556     0x2d,
2557     0x4e,
2558     0x3b,
2559     0x13,
2560     0xd8,
2561     0x0f,
2562     0x50,
2563     0xd0,
2564     0x44,
2565     0x57,
2566     0x67,
2567     0x0f,
2568     0x45,
2569     0x8f,
2570     0x21,
2571     0x30,
2572     0x97,
2573     0x9e,
2574     0x80,
2575     0xd9,
2576     0xd0,
2577     0x91,
2578     0xb7,
2579     0xc9,
2580     0x5a,
2581     0x69,
2582     0xda,
2583     0xeb,
2584     0xd5,
2585     0xea,
2586     0x37,
2587     0xf6,
2588     0xb3,
2589     0xbe,
2590     0x1f,
2591     0x24,
2592     0xf1,
2593     0x55,
2594     0x14,
2595     0x28,
2596     0x05,
2597     0xb5,
2598     0xd8,
2599     0x84,
2600     0x0f,
2601     0x62,
2602     0x85,
2603     0xaa,
2604     0xec,
2605     0x77,
2606     0x64,
2607     0xfd,
2608     0x80,
2609     0x7c,
2610     0x41,
2611     0x00,
2612     0x88,
2613     0xa3,
2614     0x79,
2615     0x7d,
2616     0x4f,
2617     0x6f,
2618     0xe3,
2619     0x76,
2620     0xf4,
2621     0xb5,
2622     0x97,
2623     0xb7,
2624     0xeb,
2625     0x67,
2626     0x28,
2627     0xba,
2628     0x07,
2629     0x1a,
2630     0x59,
2631     0x32,
2632     0xc1,
2633     0x53,
2634     0xd9,
2635     0x05,
2636     0x6b,
2637     0x63,
2638     0x93,
2639     0xce,
2640     0xa1,
2641     0xd9,
2642     0x7a,
2643     0xb2,
2644     0xff,
2645     0x1c,
2646     0x12,
2647     0x0a,
2648     0x9a,
2649     0xe5,
2650     0x51,
2651     0x1e,
2652     0xba,
2653     0xfc,
2654     0x95,
2655     0x2e,
2656     0x28,
2657     0xa9,
2658     0xfc,
2659     0x4c,
2660     0xed,
2661     0x7b,
2662     0x05,
2663     0xca,
2664     0x67,
2665     0xe0,
2666     0x2d,
2667     0xd7,
2668     0x54,
2669     0xb3,
2670     0x05,
2671     0x1c,
2672     0x23,
2673     0x2b,
2674     0x35,
2675     0x2e,
2676     0x19,
2677     0x48,
2678     0x59,
2679     0x0e,
2680     0x58,
2681     0xa8,
2682     0x01,
2683     0x56,
2684     0xfb,
2685     0x78,
2686     0x90,
2687     0xba,
2688     0x08,
2689     0x77,
2690     0x94,
2691     0x45,
2692     0x05,
2693     0x13,
2694     0xc7,
2695     0x6b,
2696     0x96,
2697     0xd2,
2698     0xa3,
2699     0xa6,
2700     0x01,
2701     0x9f,
2702     0x34,
2703     0x02,
2704     0x82,
2705     0x01,
2706     0x00,
2707     0x16,
2708     0x1a,
2709     0xb4,
2710     0x6d,
2711     0x9f,
2712     0x16,
2713     0x6c,
2714     0xcc,
2715     0x91,
2716     0x66,
2717     0xfe,
2718     0x30,
2719     0xeb,
2720     0x8e,
2721     0x44,
2722     0xba,
2723     0x2b,
2724     0x7a,
2725     0xc9,
2726     0xa8,
2727     0x95,
2728     0xf2,
2729     0xa6,
2730     0x38,
2731     0xd8,
2732     0xaf,
2733     0x3e,
2734     0x91,
2735     0x68,
2736     0xe8,
2737     0x52,
2738     0xf3,
2739     0x97,
2740     0x37,
2741     0x70,
2742     0xf2,
2743     0x47,
2744     0xa3,
2745     0xf4,
2746     0x62,
2747     0x26,
2748     0xf5,
2749     0x3b,
2750     0x71,
2751     0x52,
2752     0x50,
2753     0x15,
2754     0x9c,
2755     0x6d,
2756     0xa6,
2757     0x6d,
2758     0x92,
2759     0x4c,
2760     0x48,
2761     0x76,
2762     0x31,
2763     0x54,
2764     0x48,
2765     0xa5,
2766     0x99,
2767     0x7a,
2768     0xd4,
2769     0x61,
2770     0xf7,
2771     0x21,
2772     0x44,
2773     0xe7,
2774     0xd8,
2775     0x82,
2776     0xc3,
2777     0x50,
2778     0xd3,
2779     0xd9,
2780     0xd4,
2781     0x66,
2782     0x20,
2783     0xab,
2784     0x70,
2785     0x4c,
2786     0x97,
2787     0x9b,
2788     0x8d,
2789     0xac,
2790     0x1f,
2791     0x78,
2792     0x27,
2793     0x1e,
2794     0x47,
2795     0xf8,
2796     0x3b,
2797     0xd1,
2798     0x55,
2799     0x73,
2800     0xf3,
2801     0xb4,
2802     0x8e,
2803     0x6d,
2804     0x45,
2805     0x40,
2806     0x54,
2807     0xc6,
2808     0xd8,
2809     0x95,
2810     0x15,
2811     0x27,
2812     0xb7,
2813     0x5f,
2814     0x65,
2815     0xaa,
2816     0xcb,
2817     0x24,
2818     0xc9,
2819     0x49,
2820     0x87,
2821     0x32,
2822     0xad,
2823     0xcb,
2824     0xf8,
2825     0x35,
2826     0x63,
2827     0x56,
2828     0x72,
2829     0x7c,
2830     0x4e,
2831     0x6c,
2832     0xad,
2833     0x5f,
2834     0x26,
2835     0x8c,
2836     0xd2,
2837     0x80,
2838     0x41,
2839     0xaf,
2840     0x88,
2841     0x23,
2842     0x20,
2843     0x03,
2844     0xa4,
2845     0xd5,
2846     0x3c,
2847     0x53,
2848     0x54,
2849     0xb0,
2850     0x3d,
2851     0xed,
2852     0x0e,
2853     0x9e,
2854     0x53,
2855     0x0a,
2856     0x63,
2857     0x5f,
2858     0xfd,
2859     0x28,
2860     0x57,
2861     0x09,
2862     0x07,
2863     0x73,
2864     0xf4,
2865     0x0c,
2866     0xd4,
2867     0x71,
2868     0x5d,
2869     0x6b,
2870     0xa0,
2871     0xd7,
2872     0x86,
2873     0x99,
2874     0x29,
2875     0x9b,
2876     0xca,
2877     0xfb,
2878     0xcc,
2879     0xd6,
2880     0x2f,
2881     0xfe,
2882     0xbe,
2883     0x94,
2884     0xef,
2885     0x1a,
2886     0x0e,
2887     0x55,
2888     0x84,
2889     0xa7,
2890     0xaf,
2891     0x7b,
2892     0xfa,
2893     0xed,
2894     0x77,
2895     0x61,
2896     0x28,
2897     0x22,
2898     0xee,
2899     0x6b,
2900     0x11,
2901     0xdd,
2902     0xb0,
2903     0x17,
2904     0x1e,
2905     0x06,
2906     0xe4,
2907     0x29,
2908     0x4c,
2909     0xc2,
2910     0x3f,
2911     0xd6,
2912     0x75,
2913     0xb6,
2914     0x08,
2915     0x04,
2916     0x55,
2917     0x13,
2918     0x48,
2919     0x4f,
2920     0x44,
2921     0xea,
2922     0x8d,
2923     0xaf,
2924     0xcb,
2925     0xac,
2926     0x22,
2927     0xc4,
2928     0x6a,
2929     0xb3,
2930     0x86,
2931     0xe5,
2932     0x47,
2933     0xa9,
2934     0xb5,
2935     0x72,
2936     0x17,
2937     0x23,
2938     0x11,
2939     0x81,
2940     0x7f,
2941     0x00,
2942     0x00,
2943     0x67,
2944     0x5c,
2945     0xf4,
2946     0x58,
2947     0xcc,
2948     0xe2,
2949     0x46,
2950     0xce,
2951     0xf5,
2952     0x6d,
2953     0xd8,
2954     0x18,
2955     0x91,
2956     0xc4,
2957     0x20,
2958     0xbf,
2959     0x07,
2960     0x48,
2961     0x45,
2962     0xfd,
2963     0x02,
2964     0x1c,
2965     0x2f,
2966     0x68,
2967     0x44,
2968     0xcb,
2969     0xfb,
2970     0x6b,
2971     0xcb,
2972     0x8d,
2973     0x02,
2974     0x49,
2975     0x7c,
2976     0xee,
2977     0xd2,
2978     0xa6,
2979     0xd3,
2980     0x43,
2981     0xb8,
2982     0xa4,
2983     0x09,
2984     0xb7,
2985     0xc1,
2986     0xd4,
2987     0x4b,
2988     0xc3,
2989     0x66,
2990     0xa7,
2991     0xe0,
2992     0x21,
2993 };
2994 static const unsigned char dsa_p[] = {
2995     0x00, 0xda, 0xb3, 0x46, 0x4d, 0x54, 0x57, 0xc7, 0xb4, 0x61, 0xa0, 0x6f, 0x66, 0x17, 0xda,
2996     0xeb, 0x90, 0xf0, 0xa3, 0xd1, 0x29, 0xc9, 0x5f, 0xf2, 0x21, 0x3d, 0x85, 0xa3, 0x4a, 0xf0,
2997     0xf8, 0x36, 0x39, 0x1b, 0xe3, 0xee, 0x37, 0x70, 0x06, 0x9b, 0xe8, 0xe3, 0x0a, 0xd2, 0xf1,
2998     0xf6, 0xc4, 0x42, 0x23, 0x1f, 0x74, 0x78, 0xc2, 0x16, 0xf5, 0xce, 0xd6, 0xab, 0xa0, 0xc6,
2999     0xe8, 0x99, 0x3d, 0xf8, 0x8b, 0xfb, 0x47, 0xf8, 0x5e, 0x05, 0x68, 0x6d, 0x8b, 0xa8, 0xad,
3000     0xa1, 0xc2, 0x3a, 0x4e, 0xe0, 0xad, 0xec, 0x38, 0x75, 0x21, 0x55, 0x22, 0xce, 0xa2, 0xe9,
3001     0xe5, 0x3b, 0xd7, 0x44, 0xeb, 0x5a, 0x03, 0x59, 0xa0, 0xc5, 0x7a, 0x92, 0x59, 0x7d, 0x7a,
3002     0x07, 0x80, 0xfc, 0x4e, 0xf8, 0x56, 0x7e, 0xf1, 0x06, 0xe0, 0xba, 0xb2, 0xe7, 0x5b, 0x22,
3003     0x55, 0xee, 0x4b, 0x42, 0x61, 0x67, 0x2c, 0x43, 0x9a, 0x38, 0x2b, 0x17, 0xc2, 0x62, 0x12,
3004     0x8b, 0x0b, 0x22, 0x8c, 0x0c, 0x1c, 0x1c, 0x92, 0xb1, 0xec, 0x70, 0xce, 0x0f, 0x8c, 0xff,
3005     0x8d, 0x21, 0xf9, 0x19, 0x68, 0x4d, 0x32, 0x59, 0x78, 0x42, 0x1d, 0x0c, 0xc5, 0x1a, 0xcb,
3006     0x28, 0xe2, 0xc1, 0x1a, 0x35, 0xf1, 0x42, 0x0a, 0x19, 0x39, 0xfa, 0x83, 0xd1, 0xb4, 0xaa,
3007     0x69, 0x0f, 0xc2, 0x8e, 0xf9, 0x59, 0x2c, 0xee, 0x11, 0xfc, 0x3e, 0x4b, 0x44, 0xfb, 0x9a,
3008     0x32, 0xc8, 0x78, 0x23, 0x56, 0x85, 0x49, 0x21, 0x43, 0x12, 0x79, 0xbd, 0xa0, 0x70, 0x47,
3009     0x2f, 0xae, 0xb6, 0xd7, 0x6c, 0xc6, 0x07, 0x76, 0xa9, 0x8a, 0xa2, 0x16, 0x02, 0x89, 0x1f,
3010     0x1a, 0xd1, 0xa2, 0x96, 0x56, 0xd1, 0x1f, 0x10, 0xe1, 0xe5, 0x9f, 0x3f, 0xdd, 0x09, 0x0c,
3011     0x40, 0x90, 0x71, 0xef, 0x14, 0x41, 0x02, 0x82, 0x3a, 0x6b, 0xe1, 0xf8, 0x2c, 0x5d, 0xbe,
3012     0xfd, 0x1b
3013 };
3014 static const unsigned char dsa_q[] = {
3015     0x00, 0xe0, 0x20, 0xe0, 0x7c, 0x02, 0x16, 0xa7, 0x6c, 0x6a, 0x19, 0xba, 0xd5, 0x83, 0x73,
3016     0xf3, 0x7d, 0x31, 0xef, 0xa7, 0xe1, 0x5d, 0x5b, 0x7f, 0xf3, 0xfc, 0xda, 0x84, 0x31
3017 };
3018 static const unsigned char dsa_g[] = {
3019     0x00, 0x83, 0xdb, 0xa1, 0xbc, 0x3e, 0xc7, 0x29, 0xa5, 0x6a, 0x5c, 0x2c, 0xe8, 0x7a, 0x8c,
3020     0x7e, 0xe8, 0xb8, 0x3e, 0x13, 0x47, 0xcd, 0x36, 0x7e, 0x79, 0x30, 0x7a, 0x28, 0x03, 0xd3,
3021     0xd4, 0xd2, 0xe3, 0xee, 0x3b, 0x46, 0xda, 0xe0, 0x71, 0xe6, 0xcf, 0x46, 0x86, 0x0a, 0x37,
3022     0x57, 0xb6, 0xe9, 0xcf, 0xa1, 0x78, 0x19, 0xb8, 0x72, 0x9f, 0x30, 0x8c, 0x2a, 0x04, 0x7c,
3023     0x2f, 0x0c, 0x27, 0xa7, 0xb3, 0x23, 0xe0, 0x46, 0xf2, 0x75, 0x0c, 0x03, 0x4c, 0xad, 0xfb,
3024     0xc1, 0xcb, 0x28, 0xcd, 0xa0, 0x63, 0xdb, 0x44, 0x88, 0xe0, 0xda, 0x6c, 0x5b, 0x89, 0xb2,
3025     0x5b, 0x40, 0x6d, 0xeb, 0x78, 0x7a, 0xd5, 0xaf, 0x40, 0x52, 0x46, 0x63, 0x92, 0x13, 0x0d,
3026     0xee, 0xee, 0xf9, 0x53, 0xca, 0x2d, 0x4e, 0x3b, 0x13, 0xd8, 0x0f, 0x50, 0xd0, 0x44, 0x57,
3027     0x67, 0x0f, 0x45, 0x8f, 0x21, 0x30, 0x97, 0x9e, 0x80, 0xd9, 0xd0, 0x91, 0xb7, 0xc9, 0x5a,
3028     0x69, 0xda, 0xeb, 0xd5, 0xea, 0x37, 0xf6, 0xb3, 0xbe, 0x1f, 0x24, 0xf1, 0x55, 0x14, 0x28,
3029     0x05, 0xb5, 0xd8, 0x84, 0x0f, 0x62, 0x85, 0xaa, 0xec, 0x77, 0x64, 0xfd, 0x80, 0x7c, 0x41,
3030     0x00, 0x88, 0xa3, 0x79, 0x7d, 0x4f, 0x6f, 0xe3, 0x76, 0xf4, 0xb5, 0x97, 0xb7, 0xeb, 0x67,
3031     0x28, 0xba, 0x07, 0x1a, 0x59, 0x32, 0xc1, 0x53, 0xd9, 0x05, 0x6b, 0x63, 0x93, 0xce, 0xa1,
3032     0xd9, 0x7a, 0xb2, 0xff, 0x1c, 0x12, 0x0a, 0x9a, 0xe5, 0x51, 0x1e, 0xba, 0xfc, 0x95, 0x2e,
3033     0x28, 0xa9, 0xfc, 0x4c, 0xed, 0x7b, 0x05, 0xca, 0x67, 0xe0, 0x2d, 0xd7, 0x54, 0xb3, 0x05,
3034     0x1c, 0x23, 0x2b, 0x35, 0x2e, 0x19, 0x48, 0x59, 0x0e, 0x58, 0xa8, 0x01, 0x56, 0xfb, 0x78,
3035     0x90, 0xba, 0x08, 0x77, 0x94, 0x45, 0x05, 0x13, 0xc7, 0x6b, 0x96, 0xd2, 0xa3, 0xa6, 0x01,
3036     0x9f, 0x34
3037 };
3038 static const unsigned char dsa_priv[] = {
3039     0x2f, 0x68, 0x44, 0xcb, 0xfb, 0x6b, 0xcb, 0x8d, 0x02, 0x49, 0x7c, 0xee, 0xd2, 0xa6, 0xd3,
3040     0x43, 0xb8, 0xa4, 0x09, 0xb7, 0xc1, 0xd4, 0x4b, 0xc3, 0x66, 0xa7, 0xe0, 0x21
3041 };
3042 static const unsigned char dsa_pub[] = {
3043     0x16, 0x1a, 0xb4, 0x6d, 0x9f, 0x16, 0x6c, 0xcc, 0x91, 0x66, 0xfe, 0x30, 0xeb, 0x8e, 0x44,
3044     0xba, 0x2b, 0x7a, 0xc9, 0xa8, 0x95, 0xf2, 0xa6, 0x38, 0xd8, 0xaf, 0x3e, 0x91, 0x68, 0xe8,
3045     0x52, 0xf3, 0x97, 0x37, 0x70, 0xf2, 0x47, 0xa3, 0xf4, 0x62, 0x26, 0xf5, 0x3b, 0x71, 0x52,
3046     0x50, 0x15, 0x9c, 0x6d, 0xa6, 0x6d, 0x92, 0x4c, 0x48, 0x76, 0x31, 0x54, 0x48, 0xa5, 0x99,
3047     0x7a, 0xd4, 0x61, 0xf7, 0x21, 0x44, 0xe7, 0xd8, 0x82, 0xc3, 0x50, 0xd3, 0xd9, 0xd4, 0x66,
3048     0x20, 0xab, 0x70, 0x4c, 0x97, 0x9b, 0x8d, 0xac, 0x1f, 0x78, 0x27, 0x1e, 0x47, 0xf8, 0x3b,
3049     0xd1, 0x55, 0x73, 0xf3, 0xb4, 0x8e, 0x6d, 0x45, 0x40, 0x54, 0xc6, 0xd8, 0x95, 0x15, 0x27,
3050     0xb7, 0x5f, 0x65, 0xaa, 0xcb, 0x24, 0xc9, 0x49, 0x87, 0x32, 0xad, 0xcb, 0xf8, 0x35, 0x63,
3051     0x56, 0x72, 0x7c, 0x4e, 0x6c, 0xad, 0x5f, 0x26, 0x8c, 0xd2, 0x80, 0x41, 0xaf, 0x88, 0x23,
3052     0x20, 0x03, 0xa4, 0xd5, 0x3c, 0x53, 0x54, 0xb0, 0x3d, 0xed, 0x0e, 0x9e, 0x53, 0x0a, 0x63,
3053     0x5f, 0xfd, 0x28, 0x57, 0x09, 0x07, 0x73, 0xf4, 0x0c, 0xd4, 0x71, 0x5d, 0x6b, 0xa0, 0xd7,
3054     0x86, 0x99, 0x29, 0x9b, 0xca, 0xfb, 0xcc, 0xd6, 0x2f, 0xfe, 0xbe, 0x94, 0xef, 0x1a, 0x0e,
3055     0x55, 0x84, 0xa7, 0xaf, 0x7b, 0xfa, 0xed, 0x77, 0x61, 0x28, 0x22, 0xee, 0x6b, 0x11, 0xdd,
3056     0xb0, 0x17, 0x1e, 0x06, 0xe4, 0x29, 0x4c, 0xc2, 0x3f, 0xd6, 0x75, 0xb6, 0x08, 0x04, 0x55,
3057     0x13, 0x48, 0x4f, 0x44, 0xea, 0x8d, 0xaf, 0xcb, 0xac, 0x22, 0xc4, 0x6a, 0xb3, 0x86, 0xe5,
3058     0x47, 0xa9, 0xb5, 0x72, 0x17, 0x23, 0x11, 0x81, 0x7f, 0x00, 0x00, 0x67, 0x5c, 0xf4, 0x58,
3059     0xcc, 0xe2, 0x46, 0xce, 0xf5, 0x6d, 0xd8, 0x18, 0x91, 0xc4, 0x20, 0xbf, 0x07, 0x48, 0x45,
3060     0xfd
3061 };
3062 
do_check_params(OSSL_PARAM key_params[],int expected)3063 static int do_check_params(OSSL_PARAM key_params[], int expected)
3064 {
3065     EVP_PKEY_CTX *gen_ctx = NULL, *check_ctx = NULL;
3066     EVP_PKEY *pkey = NULL;
3067     int ret;
3068 
3069     ret = TEST_ptr(gen_ctx = EVP_PKEY_CTX_new_from_name(mainctx, "DSA", NULL))
3070         && TEST_int_eq(EVP_PKEY_fromdata_init(gen_ctx), 1)
3071         && TEST_int_eq(EVP_PKEY_fromdata(gen_ctx, &pkey,
3072                            EVP_PKEY_KEYPAIR, key_params),
3073             1)
3074         && TEST_ptr(check_ctx = EVP_PKEY_CTX_new_from_pkey(mainctx, pkey,
3075                         NULL))
3076         && TEST_int_eq(EVP_PKEY_param_check(check_ctx), expected);
3077     EVP_PKEY_CTX_free(check_ctx);
3078     EVP_PKEY_CTX_free(gen_ctx);
3079     EVP_PKEY_free(pkey);
3080     return ret;
3081 }
3082 
do_check_bn(OSSL_PARAM params[],const char * key,const unsigned char * expected,size_t expected_len)3083 static int do_check_bn(OSSL_PARAM params[], const char *key,
3084     const unsigned char *expected, size_t expected_len)
3085 {
3086     OSSL_PARAM *p;
3087     BIGNUM *bn = NULL;
3088     unsigned char buffer[256 + 1];
3089     int ret, len;
3090 
3091     ret = TEST_ptr(p = OSSL_PARAM_locate(params, key))
3092         && TEST_true(OSSL_PARAM_get_BN(p, &bn))
3093         && TEST_int_gt(len = BN_bn2binpad(bn, buffer, expected_len), 0)
3094         && TEST_mem_eq(expected, expected_len, buffer, len);
3095     BN_free(bn);
3096     return ret;
3097 }
3098 
do_check_int(OSSL_PARAM params[],const char * key,int expected)3099 static int do_check_int(OSSL_PARAM params[], const char *key, int expected)
3100 {
3101     OSSL_PARAM *p;
3102     int val = 0;
3103 
3104     return TEST_ptr(p = OSSL_PARAM_locate(params, key))
3105         && TEST_true(OSSL_PARAM_get_int(p, &val))
3106         && TEST_int_eq(val, expected);
3107 }
3108 
test_dsa_tofrom_data_select(void)3109 static int test_dsa_tofrom_data_select(void)
3110 {
3111     int ret;
3112     EVP_PKEY *key = NULL;
3113     const unsigned char *pkeydata = dsa_key;
3114 
3115     ret = TEST_ptr(key = d2i_AutoPrivateKey_ex(NULL, &pkeydata, sizeof(dsa_key),
3116                        mainctx, NULL))
3117         && TEST_true(do_pkey_tofrom_data_select(key, "DSA"));
3118 
3119     EVP_PKEY_free(key);
3120     return ret;
3121 }
3122 
test_dsa_todata(void)3123 static int test_dsa_todata(void)
3124 {
3125     EVP_PKEY *pkey = NULL;
3126     OSSL_PARAM *to_params = NULL, *all_params = NULL;
3127     OSSL_PARAM gen_params[4];
3128     int ret = 0;
3129     const unsigned char *pkeydata = dsa_key;
3130 
3131     unsigned char dsa_seed[] = {
3132         0xbc, 0x8a, 0x81, 0x64, 0x9e, 0x9d, 0x63, 0xa7, 0xa3, 0x5d, 0x87, 0xdd,
3133         0x32, 0xf3, 0xc1, 0x9f, 0x18, 0x22, 0xeb, 0x73, 0x63, 0xad, 0x5e, 0x7b,
3134         0x90, 0xc1, 0xe3, 0xe0
3135     };
3136     int dsa_pcounter = 319;
3137     int dsa_gindex = 5;
3138 
3139     gen_params[0] = OSSL_PARAM_construct_octet_string(OSSL_PKEY_PARAM_FFC_SEED,
3140         (void *)dsa_seed,
3141         sizeof(dsa_seed));
3142     gen_params[1] = OSSL_PARAM_construct_int(OSSL_PKEY_PARAM_FFC_GINDEX,
3143         &dsa_gindex);
3144     gen_params[2] = OSSL_PARAM_construct_int(OSSL_PKEY_PARAM_FFC_PCOUNTER,
3145         &dsa_pcounter);
3146     gen_params[3] = OSSL_PARAM_construct_end();
3147 
3148     if (!TEST_ptr(pkey = d2i_AutoPrivateKey_ex(NULL, &pkeydata, sizeof(dsa_key),
3149                       mainctx, NULL))
3150         || !TEST_int_eq(EVP_PKEY_todata(pkey, EVP_PKEY_KEYPAIR, &to_params), 1)
3151         || !do_check_bn(to_params, OSSL_PKEY_PARAM_FFC_P, dsa_p, sizeof(dsa_p))
3152         || !do_check_bn(to_params, OSSL_PKEY_PARAM_FFC_Q, dsa_q, sizeof(dsa_q))
3153         || !do_check_bn(to_params, OSSL_PKEY_PARAM_FFC_G, dsa_g, sizeof(dsa_g))
3154         || !do_check_bn(to_params, OSSL_PKEY_PARAM_PUB_KEY, dsa_pub,
3155             sizeof(dsa_pub))
3156         || !do_check_bn(to_params, OSSL_PKEY_PARAM_PRIV_KEY, dsa_priv,
3157             sizeof(dsa_priv))
3158         || !do_check_int(to_params, OSSL_PKEY_PARAM_FFC_GINDEX, -1)
3159         || !do_check_int(to_params, OSSL_PKEY_PARAM_FFC_PCOUNTER, -1)
3160         || !do_check_int(to_params, OSSL_PKEY_PARAM_FFC_H, 0)
3161         || !do_check_int(to_params, OSSL_PKEY_PARAM_FFC_VALIDATE_PQ, 1)
3162         || !do_check_int(to_params, OSSL_PKEY_PARAM_FFC_VALIDATE_G, 1)
3163         || !do_check_int(to_params, OSSL_PKEY_PARAM_FFC_VALIDATE_LEGACY, 0)
3164         || !TEST_ptr_null(OSSL_PARAM_locate(to_params, OSSL_PKEY_PARAM_FFC_SEED)))
3165         goto err;
3166 
3167     if (!do_fromdata_key_is_equal(to_params, pkey, "DSA"))
3168         goto err;
3169 
3170     if (!TEST_ptr(all_params = OSSL_PARAM_merge(to_params, gen_params))
3171         || !do_check_params(all_params, 1))
3172         goto err;
3173     gen_params[1] = OSSL_PARAM_construct_int(OSSL_PKEY_PARAM_FFC_GINDEX,
3174         &dsa_gindex);
3175     gen_params[2] = OSSL_PARAM_construct_int(OSSL_PKEY_PARAM_FFC_PCOUNTER,
3176         &dsa_pcounter);
3177     /*
3178      * Check that modifying the shallow copy values used in OSSL_PARAM_merge()
3179      * results in an invalid key. This also verifies that the fips186-4
3180      * validation code is running.
3181      */
3182     dsa_gindex++;
3183     if (!do_check_params(all_params, 0))
3184         goto err;
3185     dsa_gindex--;
3186     dsa_pcounter++;
3187     if (!do_check_params(all_params, 0))
3188         goto err;
3189     dsa_pcounter--;
3190     dsa_seed[0] = 0xb0;
3191     if (!do_check_params(all_params, 0))
3192         goto err;
3193 
3194     ret = 1;
3195 err:
3196     EVP_PKEY_free(pkey);
3197     OSSL_PARAM_free(all_params);
3198     OSSL_PARAM_free(to_params);
3199     return ret;
3200 }
3201 
3202 /*
3203  * Test that OSSL_PKEY_PARAM_FFC_DIGEST_PROPS is set properly when using fromdata
3204  * This test:
3205  *   checks for failure when the property query is bad (tstid == 0)
3206  *   checks for success when the property query is valid (tstid == 1)
3207  */
test_dsa_fromdata_digest_prop(int tstid)3208 static int test_dsa_fromdata_digest_prop(int tstid)
3209 {
3210     EVP_PKEY_CTX *ctx = NULL, *gctx = NULL;
3211     EVP_PKEY *pkey = NULL, *pkey2 = NULL;
3212     OSSL_PARAM params[4], *p = params;
3213     int ret = 0;
3214     int expected = (tstid == 0 ? 0 : 1);
3215     unsigned int pbits = 512; /* minimum allowed for speed */
3216 
3217     *p++ = OSSL_PARAM_construct_uint(OSSL_PKEY_PARAM_FFC_PBITS, &pbits);
3218     *p++ = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_FFC_DIGEST, "SHA512", 0);
3219     /* Setting a bad prop query here should fail during paramgen - when it tries to do a fetch */
3220     *p++ = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_FFC_DIGEST_PROPS,
3221         tstid == 0 ? "provider=unknown" : "provider=default", 0);
3222     *p++ = OSSL_PARAM_construct_end();
3223 
3224     if (!TEST_ptr(ctx = EVP_PKEY_CTX_new_from_name(mainctx, "DSA", NULL))
3225         || !TEST_int_eq(EVP_PKEY_fromdata_init(ctx), 1)
3226         || !TEST_int_eq(EVP_PKEY_fromdata(ctx, &pkey, EVP_PKEY_KEY_PARAMETERS, params), 1))
3227         goto err;
3228 
3229     if (!TEST_ptr(gctx = EVP_PKEY_CTX_new_from_pkey(mainctx, pkey, NULL))
3230         || !TEST_int_eq(EVP_PKEY_paramgen_init(gctx), 1)
3231         || !TEST_int_eq(EVP_PKEY_paramgen(gctx, &pkey2), expected))
3232         goto err;
3233 
3234     ret = 1;
3235 err:
3236     EVP_PKEY_free(pkey2);
3237     EVP_PKEY_free(pkey);
3238     EVP_PKEY_CTX_free(ctx);
3239     EVP_PKEY_CTX_free(gctx);
3240     return ret;
3241 }
3242 #endif /* OPENSSL_NO_DSA */
3243 
test_pkey_todata_null(void)3244 static int test_pkey_todata_null(void)
3245 {
3246     OSSL_PARAM *params = NULL;
3247     EVP_PKEY *pkey = NULL;
3248     int ret = 0;
3249     const unsigned char *pdata = keydata[0].kder;
3250 
3251     ret = TEST_ptr(pkey = d2i_AutoPrivateKey_ex(NULL, &pdata, keydata[0].size,
3252                        mainctx, NULL))
3253         && TEST_int_eq(EVP_PKEY_todata(NULL, EVP_PKEY_KEYPAIR, &params), 0)
3254         && TEST_int_eq(EVP_PKEY_todata(pkey, EVP_PKEY_KEYPAIR, NULL), 0);
3255     EVP_PKEY_free(pkey);
3256     return ret;
3257 }
3258 
3259 static OSSL_CALLBACK test_pkey_export_cb;
3260 
test_pkey_export_cb(const OSSL_PARAM params[],void * arg)3261 static int test_pkey_export_cb(const OSSL_PARAM params[], void *arg)
3262 {
3263     if (arg == NULL)
3264         return 0;
3265     return do_fromdata_key_is_equal(params, (EVP_PKEY *)arg, "RSA");
3266 }
3267 
test_pkey_export_null(void)3268 static int test_pkey_export_null(void)
3269 {
3270     EVP_PKEY *pkey = NULL;
3271     int ret = 0;
3272     const unsigned char *pdata = keydata[0].kder;
3273 
3274     ret = TEST_ptr(pkey = d2i_AutoPrivateKey_ex(NULL, &pdata, keydata[0].size,
3275                        mainctx, NULL))
3276         && TEST_int_eq(EVP_PKEY_export(NULL, EVP_PKEY_KEYPAIR,
3277                            test_pkey_export_cb, NULL),
3278             0)
3279         && TEST_int_eq(EVP_PKEY_export(pkey, EVP_PKEY_KEYPAIR, NULL, NULL), 0);
3280     EVP_PKEY_free(pkey);
3281     return ret;
3282 }
3283 
test_pkey_export(void)3284 static int test_pkey_export(void)
3285 {
3286     EVP_PKEY *pkey = NULL;
3287 #ifndef OPENSSL_NO_DEPRECATED_3_0
3288     RSA *rsa = NULL;
3289 #endif
3290     int ret = 1;
3291     const unsigned char *pdata = keydata[0].kder;
3292     int pdata_len = keydata[0].size;
3293 
3294     if (!TEST_ptr(pkey = d2i_AutoPrivateKey_ex(NULL, &pdata, pdata_len,
3295                       mainctx, NULL))
3296         || !TEST_true(EVP_PKEY_export(pkey, EVP_PKEY_KEYPAIR,
3297             test_pkey_export_cb, pkey))
3298         || !TEST_false(EVP_PKEY_export(pkey, EVP_PKEY_KEYPAIR,
3299             test_pkey_export_cb, NULL)))
3300         ret = 0;
3301     EVP_PKEY_free(pkey);
3302 
3303 #ifndef OPENSSL_NO_DEPRECATED_3_0
3304     /* Now, try with a legacy key */
3305     pdata = keydata[0].kder;
3306     pdata_len = keydata[0].size;
3307     if (!TEST_ptr(rsa = d2i_RSAPrivateKey(NULL, &pdata, pdata_len))
3308         || !TEST_ptr(pkey = EVP_PKEY_new())
3309         || !TEST_true(EVP_PKEY_assign_RSA(pkey, rsa))
3310         || !TEST_true(EVP_PKEY_export(pkey, EVP_PKEY_KEYPAIR,
3311             test_pkey_export_cb, pkey))
3312         || !TEST_false(EVP_PKEY_export(pkey, EVP_PKEY_KEYPAIR,
3313             test_pkey_export_cb, NULL)))
3314         ret = 0;
3315     EVP_PKEY_free(pkey);
3316 #endif
3317     return ret;
3318 }
3319 
test_rsa_pss_sign(void)3320 static int test_rsa_pss_sign(void)
3321 {
3322     EVP_PKEY *pkey = NULL;
3323     EVP_PKEY_CTX *pctx = NULL;
3324     int ret = 0;
3325     const unsigned char *pdata = keydata[0].kder;
3326     const char *mdname = "SHA2-256";
3327     OSSL_PARAM sig_params[3];
3328     unsigned char mdbuf[256 / 8] = { 0 };
3329     int padding = RSA_PKCS1_PSS_PADDING;
3330     unsigned char *sig = NULL;
3331     size_t sig_len = 0;
3332 
3333     sig_params[0] = OSSL_PARAM_construct_int(OSSL_PKEY_PARAM_PAD_MODE,
3334         &padding);
3335     sig_params[1] = OSSL_PARAM_construct_utf8_string(OSSL_SIGNATURE_PARAM_DIGEST,
3336         (char *)mdname, 0);
3337     sig_params[2] = OSSL_PARAM_construct_end();
3338 
3339     ret = TEST_ptr(pkey = d2i_AutoPrivateKey_ex(NULL, &pdata, keydata[0].size,
3340                        mainctx, NULL))
3341         && TEST_ptr(pctx = EVP_PKEY_CTX_new_from_pkey(mainctx, pkey, NULL))
3342         && TEST_int_gt(EVP_PKEY_sign_init_ex(pctx, sig_params), 0)
3343         && TEST_int_gt(EVP_PKEY_sign(pctx, NULL, &sig_len, mdbuf,
3344                            sizeof(mdbuf)),
3345             0)
3346         && TEST_int_gt(sig_len, 0)
3347         && TEST_ptr(sig = OPENSSL_malloc(sig_len))
3348         && TEST_int_gt(EVP_PKEY_sign(pctx, sig, &sig_len, mdbuf,
3349                            sizeof(mdbuf)),
3350             0);
3351 
3352     EVP_PKEY_CTX_free(pctx);
3353     OPENSSL_free(sig);
3354     EVP_PKEY_free(pkey);
3355 
3356     return ret;
3357 }
3358 
test_evp_md_ctx_dup(void)3359 static int test_evp_md_ctx_dup(void)
3360 {
3361     EVP_MD_CTX *mdctx;
3362     EVP_MD_CTX *copyctx = NULL;
3363     int ret;
3364 
3365     /* test copying freshly initialized context */
3366     ret = TEST_ptr(mdctx = EVP_MD_CTX_new())
3367         && TEST_ptr(copyctx = EVP_MD_CTX_dup(mdctx));
3368 
3369     EVP_MD_CTX_free(mdctx);
3370     EVP_MD_CTX_free(copyctx);
3371     return ret;
3372 }
3373 
test_evp_md_ctx_copy(void)3374 static int test_evp_md_ctx_copy(void)
3375 {
3376     EVP_MD_CTX *mdctx = NULL;
3377     EVP_MD_CTX *copyctx = NULL;
3378     int ret;
3379 
3380     /* test copying freshly initialized context */
3381     ret = TEST_ptr(mdctx = EVP_MD_CTX_new())
3382         && TEST_ptr(copyctx = EVP_MD_CTX_new())
3383         && TEST_true(EVP_MD_CTX_copy_ex(copyctx, mdctx));
3384 
3385     EVP_MD_CTX_free(mdctx);
3386     EVP_MD_CTX_free(copyctx);
3387     return ret;
3388 }
3389 
test_evp_md_ctx_copy2(void)3390 static int test_evp_md_ctx_copy2(void)
3391 {
3392     int ret = 0;
3393     EVP_MD *md = NULL;
3394     OSSL_LIB_CTX *ctx = NULL;
3395     EVP_MD_CTX *inctx = NULL, *outctx = NULL;
3396     void *origin_algctx = NULL;
3397 
3398     if (!TEST_ptr(ctx = OSSL_LIB_CTX_new())
3399         || !TEST_ptr(md = EVP_MD_fetch(ctx, "sha256", NULL)))
3400         goto end;
3401 
3402     inctx = EVP_MD_CTX_new();
3403     outctx = EVP_MD_CTX_new();
3404 
3405     if (!TEST_ptr(inctx) || !TEST_ptr(outctx))
3406         goto end;
3407 
3408     /* init inctx and outctx, now the contexts are from same providers */
3409     if (!TEST_true(EVP_DigestInit_ex2(inctx, md, NULL)))
3410         goto end;
3411     if (!TEST_true(EVP_DigestInit_ex2(outctx, md, NULL)))
3412         goto end;
3413 
3414     /*
3415      * Test the EVP_MD_CTX_copy_ex function. After copying,
3416      * outctx->algctx should be the same as the original.
3417      */
3418     origin_algctx = outctx->algctx;
3419     ret = TEST_true(EVP_MD_CTX_copy_ex(outctx, inctx))
3420         && TEST_true(outctx->algctx == origin_algctx);
3421 
3422 end:
3423     EVP_MD_free(md);
3424     EVP_MD_CTX_free(inctx);
3425     EVP_MD_CTX_free(outctx);
3426     OSSL_LIB_CTX_free(ctx);
3427     return ret;
3428 }
3429 
3430 #if !defined OPENSSL_NO_DES && !defined OPENSSL_NO_MD5
test_evp_pbe_alg_add(void)3431 static int test_evp_pbe_alg_add(void)
3432 {
3433     int ret = 0;
3434     int cipher_nid = 0, md_nid = 0;
3435     EVP_PBE_KEYGEN_EX *keygen_ex = NULL;
3436     EVP_PBE_KEYGEN *keygen = NULL;
3437 
3438     if (!TEST_true(EVP_PBE_alg_add(NID_pbeWithMD5AndDES_CBC, EVP_des_cbc(), EVP_md5(),
3439             PKCS5_PBE_keyivgen)))
3440         goto err;
3441 
3442     if (!TEST_true(EVP_PBE_find_ex(EVP_PBE_TYPE_OUTER, NID_pbeWithMD5AndDES_CBC,
3443             &cipher_nid, &md_nid, &keygen, &keygen_ex)))
3444         goto err;
3445 
3446     if (!TEST_true(keygen != NULL))
3447         goto err;
3448     if (!TEST_true(keygen_ex == NULL))
3449         goto err;
3450 
3451     ret = 1;
3452 
3453 err:
3454     return ret;
3455 }
3456 #endif
3457 
3458 /*
3459  * Currently, EVP_<OBJ>_fetch doesn't support
3460  * colon separated alternative names for lookup
3461  * so add a test here to ensure that when one is provided
3462  * libcrypto returns an error
3463  */
evp_test_name_parsing(void)3464 static int evp_test_name_parsing(void)
3465 {
3466     EVP_MD *md;
3467 
3468     if (!TEST_ptr_null(md = EVP_MD_fetch(mainctx, "SHA256:BogusName", NULL))) {
3469         EVP_MD_free(md);
3470         return 0;
3471     }
3472 
3473     return 1;
3474 }
3475 
setup_tests(void)3476 int setup_tests(void)
3477 {
3478     if (!test_get_libctx(&mainctx, &nullprov, NULL, NULL, NULL)) {
3479         OSSL_LIB_CTX_free(mainctx);
3480         mainctx = NULL;
3481         return 0;
3482     }
3483 
3484     ADD_TEST(evp_test_name_parsing);
3485     ADD_TEST(test_alternative_default);
3486     ADD_ALL_TESTS(test_d2i_AutoPrivateKey_ex, OSSL_NELEM(keydata));
3487     ADD_TEST(test_new_keytype);
3488 #ifndef OPENSSL_NO_EC
3489     ADD_ALL_TESTS(test_d2i_PrivateKey_ex, 2);
3490     ADD_TEST(test_ec_tofrom_data_select);
3491 #ifndef OPENSSL_NO_ECX
3492     ADD_TEST(test_ecx_tofrom_data_select);
3493 #endif
3494     ADD_TEST(test_ec_d2i_i2d_pubkey);
3495 #else
3496     ADD_ALL_TESTS(test_d2i_PrivateKey_ex, 1);
3497 #endif
3498 #ifndef OPENSSL_NO_SM2
3499     ADD_TEST(test_sm2_tofrom_data_select);
3500 #endif
3501 #ifndef OPENSSL_NO_DSA
3502     ADD_TEST(test_dsa_todata);
3503     ADD_TEST(test_dsa_tofrom_data_select);
3504     ADD_ALL_TESTS(test_dsa_fromdata_digest_prop, 2);
3505 #endif
3506 #ifndef OPENSSL_NO_DH
3507     ADD_TEST(test_dh_tofrom_data_select);
3508     ADD_TEST(test_dh_paramgen);
3509     ADD_TEST(test_dh_paramfromdata);
3510 #endif
3511     ADD_TEST(test_rsa_tofrom_data_select);
3512 
3513     ADD_TEST(test_pkey_todata_null);
3514     ADD_TEST(test_pkey_export_null);
3515     ADD_TEST(test_pkey_export);
3516 #ifndef OPENSSL_NO_DES
3517     ADD_TEST(test_pkcs8key_nid_bio);
3518 #endif
3519     ADD_ALL_TESTS(test_PEM_read_bio_negative, OSSL_NELEM(keydata));
3520     ADD_ALL_TESTS(test_PEM_read_bio_negative_wrong_password, 2);
3521     ADD_TEST(test_rsa_pss_sign);
3522     ADD_TEST(test_evp_md_ctx_dup);
3523     ADD_TEST(test_evp_md_ctx_copy);
3524     ADD_TEST(test_evp_md_ctx_copy2);
3525     ADD_ALL_TESTS(test_provider_unload_effective, 2);
3526 #if !defined OPENSSL_NO_DES && !defined OPENSSL_NO_MD5
3527     ADD_TEST(test_evp_pbe_alg_add);
3528 #endif
3529     return 1;
3530 }
3531 
cleanup_tests(void)3532 void cleanup_tests(void)
3533 {
3534     OSSL_LIB_CTX_free(mainctx);
3535     OSSL_PROVIDER_unload(nullprov);
3536 }
3537