Fix a crash in ssl_security_cert_chain
[openssl.git] / test / evp_extra_test2.c
1 /*
2  * Copyright 2015-2022 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/core_names.h>
25 #include "testutil.h"
26 #include "internal/nelem.h"
27
28 static OSSL_LIB_CTX *mainctx = NULL;
29 static OSSL_PROVIDER *nullprov = NULL;
30
31 /*
32  * kExampleRSAKeyDER is an RSA private key in ASN.1, DER format. Of course, you
33  * should never use this key anywhere but in an example.
34  */
35 static const unsigned char kExampleRSAKeyDER[] = {
36     0x30, 0x82, 0x02, 0x5c, 0x02, 0x01, 0x00, 0x02, 0x81, 0x81, 0x00, 0xf8,
37     0xb8, 0x6c, 0x83, 0xb4, 0xbc, 0xd9, 0xa8, 0x57, 0xc0, 0xa5, 0xb4, 0x59,
38     0x76, 0x8c, 0x54, 0x1d, 0x79, 0xeb, 0x22, 0x52, 0x04, 0x7e, 0xd3, 0x37,
39     0xeb, 0x41, 0xfd, 0x83, 0xf9, 0xf0, 0xa6, 0x85, 0x15, 0x34, 0x75, 0x71,
40     0x5a, 0x84, 0xa8, 0x3c, 0xd2, 0xef, 0x5a, 0x4e, 0xd3, 0xde, 0x97, 0x8a,
41     0xdd, 0xff, 0xbb, 0xcf, 0x0a, 0xaa, 0x86, 0x92, 0xbe, 0xb8, 0x50, 0xe4,
42     0xcd, 0x6f, 0x80, 0x33, 0x30, 0x76, 0x13, 0x8f, 0xca, 0x7b, 0xdc, 0xec,
43     0x5a, 0xca, 0x63, 0xc7, 0x03, 0x25, 0xef, 0xa8, 0x8a, 0x83, 0x58, 0x76,
44     0x20, 0xfa, 0x16, 0x77, 0xd7, 0x79, 0x92, 0x63, 0x01, 0x48, 0x1a, 0xd8,
45     0x7b, 0x67, 0xf1, 0x52, 0x55, 0x49, 0x4e, 0xd6, 0x6e, 0x4a, 0x5c, 0xd7,
46     0x7a, 0x37, 0x36, 0x0c, 0xde, 0xdd, 0x8f, 0x44, 0xe8, 0xc2, 0xa7, 0x2c,
47     0x2b, 0xb5, 0xaf, 0x64, 0x4b, 0x61, 0x07, 0x02, 0x03, 0x01, 0x00, 0x01,
48     0x02, 0x81, 0x80, 0x74, 0x88, 0x64, 0x3f, 0x69, 0x45, 0x3a, 0x6d, 0xc7,
49     0x7f, 0xb9, 0xa3, 0xc0, 0x6e, 0xec, 0xdc, 0xd4, 0x5a, 0xb5, 0x32, 0x85,
50     0x5f, 0x19, 0xd4, 0xf8, 0xd4, 0x3f, 0x3c, 0xfa, 0xc2, 0xf6, 0x5f, 0xee,
51     0xe6, 0xba, 0x87, 0x74, 0x2e, 0xc7, 0x0c, 0xd4, 0x42, 0xb8, 0x66, 0x85,
52     0x9c, 0x7b, 0x24, 0x61, 0xaa, 0x16, 0x11, 0xf6, 0xb5, 0xb6, 0xa4, 0x0a,
53     0xc9, 0x55, 0x2e, 0x81, 0xa5, 0x47, 0x61, 0xcb, 0x25, 0x8f, 0xc2, 0x15,
54     0x7b, 0x0e, 0x7c, 0x36, 0x9f, 0x3a, 0xda, 0x58, 0x86, 0x1c, 0x5b, 0x83,
55     0x79, 0xe6, 0x2b, 0xcc, 0xe6, 0xfa, 0x2c, 0x61, 0xf2, 0x78, 0x80, 0x1b,
56     0xe2, 0xf3, 0x9d, 0x39, 0x2b, 0x65, 0x57, 0x91, 0x3d, 0x71, 0x99, 0x73,
57     0xa5, 0xc2, 0x79, 0x20, 0x8c, 0x07, 0x4f, 0xe5, 0xb4, 0x60, 0x1f, 0x99,
58     0xa2, 0xb1, 0x4f, 0x0c, 0xef, 0xbc, 0x59, 0x53, 0x00, 0x7d, 0xb1, 0x02,
59     0x41, 0x00, 0xfc, 0x7e, 0x23, 0x65, 0x70, 0xf8, 0xce, 0xd3, 0x40, 0x41,
60     0x80, 0x6a, 0x1d, 0x01, 0xd6, 0x01, 0xff, 0xb6, 0x1b, 0x3d, 0x3d, 0x59,
61     0x09, 0x33, 0x79, 0xc0, 0x4f, 0xde, 0x96, 0x27, 0x4b, 0x18, 0xc6, 0xd9,
62     0x78, 0xf1, 0xf4, 0x35, 0x46, 0xe9, 0x7c, 0x42, 0x7a, 0x5d, 0x9f, 0xef,
63     0x54, 0xb8, 0xf7, 0x9f, 0xc4, 0x33, 0x6c, 0xf3, 0x8c, 0x32, 0x46, 0x87,
64     0x67, 0x30, 0x7b, 0xa7, 0xac, 0xe3, 0x02, 0x41, 0x00, 0xfc, 0x2c, 0xdf,
65     0x0c, 0x0d, 0x88, 0xf5, 0xb1, 0x92, 0xa8, 0x93, 0x47, 0x63, 0x55, 0xf5,
66     0xca, 0x58, 0x43, 0xba, 0x1c, 0xe5, 0x9e, 0xb6, 0x95, 0x05, 0xcd, 0xb5,
67     0x82, 0xdf, 0xeb, 0x04, 0x53, 0x9d, 0xbd, 0xc2, 0x38, 0x16, 0xb3, 0x62,
68     0xdd, 0xa1, 0x46, 0xdb, 0x6d, 0x97, 0x93, 0x9f, 0x8a, 0xc3, 0x9b, 0x64,
69     0x7e, 0x42, 0xe3, 0x32, 0x57, 0x19, 0x1b, 0xd5, 0x6e, 0x85, 0xfa, 0xb8,
70     0x8d, 0x02, 0x41, 0x00, 0xbc, 0x3d, 0xde, 0x6d, 0xd6, 0x97, 0xe8, 0xba,
71     0x9e, 0x81, 0x37, 0x17, 0xe5, 0xa0, 0x64, 0xc9, 0x00, 0xb7, 0xe7, 0xfe,
72     0xf4, 0x29, 0xd9, 0x2e, 0x43, 0x6b, 0x19, 0x20, 0xbd, 0x99, 0x75, 0xe7,
73     0x76, 0xf8, 0xd3, 0xae, 0xaf, 0x7e, 0xb8, 0xeb, 0x81, 0xf4, 0x9d, 0xfe,
74     0x07, 0x2b, 0x0b, 0x63, 0x0b, 0x5a, 0x55, 0x90, 0x71, 0x7d, 0xf1, 0xdb,
75     0xd9, 0xb1, 0x41, 0x41, 0x68, 0x2f, 0x4e, 0x39, 0x02, 0x40, 0x5a, 0x34,
76     0x66, 0xd8, 0xf5, 0xe2, 0x7f, 0x18, 0xb5, 0x00, 0x6e, 0x26, 0x84, 0x27,
77     0x14, 0x93, 0xfb, 0xfc, 0xc6, 0x0f, 0x5e, 0x27, 0xe6, 0xe1, 0xe9, 0xc0,
78     0x8a, 0xe4, 0x34, 0xda, 0xe9, 0xa2, 0x4b, 0x73, 0xbc, 0x8c, 0xb9, 0xba,
79     0x13, 0x6c, 0x7a, 0x2b, 0x51, 0x84, 0xa3, 0x4a, 0xe0, 0x30, 0x10, 0x06,
80     0x7e, 0xed, 0x17, 0x5a, 0x14, 0x00, 0xc9, 0xef, 0x85, 0xea, 0x52, 0x2c,
81     0xbc, 0x65, 0x02, 0x40, 0x51, 0xe3, 0xf2, 0x83, 0x19, 0x9b, 0xc4, 0x1e,
82     0x2f, 0x50, 0x3d, 0xdf, 0x5a, 0xa2, 0x18, 0xca, 0x5f, 0x2e, 0x49, 0xaf,
83     0x6f, 0xcc, 0xfa, 0x65, 0x77, 0x94, 0xb5, 0xa1, 0x0a, 0xa9, 0xd1, 0x8a,
84     0x39, 0x37, 0xf4, 0x0b, 0xa0, 0xd7, 0x82, 0x27, 0x5e, 0xae, 0x17, 0x17,
85     0xa1, 0x1e, 0x54, 0x34, 0xbf, 0x6e, 0xc4, 0x8e, 0x99, 0x5d, 0x08, 0xf1,
86     0x2d, 0x86, 0x9d, 0xa5, 0x20, 0x1b, 0xe5, 0xdf,
87 };
88
89 /*
90  * kExampleRSAKeyPKCS8 is kExampleRSAKeyDER encoded in a PKCS #8
91  * PrivateKeyInfo.
92  */
93 static const unsigned char kExampleRSAKeyPKCS8[] = {
94     0x30, 0x82, 0x02, 0x76, 0x02, 0x01, 0x00, 0x30, 0x0d, 0x06, 0x09, 0x2a,
95     0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01, 0x05, 0x00, 0x04, 0x82,
96     0x02, 0x60, 0x30, 0x82, 0x02, 0x5c, 0x02, 0x01, 0x00, 0x02, 0x81, 0x81,
97     0x00, 0xf8, 0xb8, 0x6c, 0x83, 0xb4, 0xbc, 0xd9, 0xa8, 0x57, 0xc0, 0xa5,
98     0xb4, 0x59, 0x76, 0x8c, 0x54, 0x1d, 0x79, 0xeb, 0x22, 0x52, 0x04, 0x7e,
99     0xd3, 0x37, 0xeb, 0x41, 0xfd, 0x83, 0xf9, 0xf0, 0xa6, 0x85, 0x15, 0x34,
100     0x75, 0x71, 0x5a, 0x84, 0xa8, 0x3c, 0xd2, 0xef, 0x5a, 0x4e, 0xd3, 0xde,
101     0x97, 0x8a, 0xdd, 0xff, 0xbb, 0xcf, 0x0a, 0xaa, 0x86, 0x92, 0xbe, 0xb8,
102     0x50, 0xe4, 0xcd, 0x6f, 0x80, 0x33, 0x30, 0x76, 0x13, 0x8f, 0xca, 0x7b,
103     0xdc, 0xec, 0x5a, 0xca, 0x63, 0xc7, 0x03, 0x25, 0xef, 0xa8, 0x8a, 0x83,
104     0x58, 0x76, 0x20, 0xfa, 0x16, 0x77, 0xd7, 0x79, 0x92, 0x63, 0x01, 0x48,
105     0x1a, 0xd8, 0x7b, 0x67, 0xf1, 0x52, 0x55, 0x49, 0x4e, 0xd6, 0x6e, 0x4a,
106     0x5c, 0xd7, 0x7a, 0x37, 0x36, 0x0c, 0xde, 0xdd, 0x8f, 0x44, 0xe8, 0xc2,
107     0xa7, 0x2c, 0x2b, 0xb5, 0xaf, 0x64, 0x4b, 0x61, 0x07, 0x02, 0x03, 0x01,
108     0x00, 0x01, 0x02, 0x81, 0x80, 0x74, 0x88, 0x64, 0x3f, 0x69, 0x45, 0x3a,
109     0x6d, 0xc7, 0x7f, 0xb9, 0xa3, 0xc0, 0x6e, 0xec, 0xdc, 0xd4, 0x5a, 0xb5,
110     0x32, 0x85, 0x5f, 0x19, 0xd4, 0xf8, 0xd4, 0x3f, 0x3c, 0xfa, 0xc2, 0xf6,
111     0x5f, 0xee, 0xe6, 0xba, 0x87, 0x74, 0x2e, 0xc7, 0x0c, 0xd4, 0x42, 0xb8,
112     0x66, 0x85, 0x9c, 0x7b, 0x24, 0x61, 0xaa, 0x16, 0x11, 0xf6, 0xb5, 0xb6,
113     0xa4, 0x0a, 0xc9, 0x55, 0x2e, 0x81, 0xa5, 0x47, 0x61, 0xcb, 0x25, 0x8f,
114     0xc2, 0x15, 0x7b, 0x0e, 0x7c, 0x36, 0x9f, 0x3a, 0xda, 0x58, 0x86, 0x1c,
115     0x5b, 0x83, 0x79, 0xe6, 0x2b, 0xcc, 0xe6, 0xfa, 0x2c, 0x61, 0xf2, 0x78,
116     0x80, 0x1b, 0xe2, 0xf3, 0x9d, 0x39, 0x2b, 0x65, 0x57, 0x91, 0x3d, 0x71,
117     0x99, 0x73, 0xa5, 0xc2, 0x79, 0x20, 0x8c, 0x07, 0x4f, 0xe5, 0xb4, 0x60,
118     0x1f, 0x99, 0xa2, 0xb1, 0x4f, 0x0c, 0xef, 0xbc, 0x59, 0x53, 0x00, 0x7d,
119     0xb1, 0x02, 0x41, 0x00, 0xfc, 0x7e, 0x23, 0x65, 0x70, 0xf8, 0xce, 0xd3,
120     0x40, 0x41, 0x80, 0x6a, 0x1d, 0x01, 0xd6, 0x01, 0xff, 0xb6, 0x1b, 0x3d,
121     0x3d, 0x59, 0x09, 0x33, 0x79, 0xc0, 0x4f, 0xde, 0x96, 0x27, 0x4b, 0x18,
122     0xc6, 0xd9, 0x78, 0xf1, 0xf4, 0x35, 0x46, 0xe9, 0x7c, 0x42, 0x7a, 0x5d,
123     0x9f, 0xef, 0x54, 0xb8, 0xf7, 0x9f, 0xc4, 0x33, 0x6c, 0xf3, 0x8c, 0x32,
124     0x46, 0x87, 0x67, 0x30, 0x7b, 0xa7, 0xac, 0xe3, 0x02, 0x41, 0x00, 0xfc,
125     0x2c, 0xdf, 0x0c, 0x0d, 0x88, 0xf5, 0xb1, 0x92, 0xa8, 0x93, 0x47, 0x63,
126     0x55, 0xf5, 0xca, 0x58, 0x43, 0xba, 0x1c, 0xe5, 0x9e, 0xb6, 0x95, 0x05,
127     0xcd, 0xb5, 0x82, 0xdf, 0xeb, 0x04, 0x53, 0x9d, 0xbd, 0xc2, 0x38, 0x16,
128     0xb3, 0x62, 0xdd, 0xa1, 0x46, 0xdb, 0x6d, 0x97, 0x93, 0x9f, 0x8a, 0xc3,
129     0x9b, 0x64, 0x7e, 0x42, 0xe3, 0x32, 0x57, 0x19, 0x1b, 0xd5, 0x6e, 0x85,
130     0xfa, 0xb8, 0x8d, 0x02, 0x41, 0x00, 0xbc, 0x3d, 0xde, 0x6d, 0xd6, 0x97,
131     0xe8, 0xba, 0x9e, 0x81, 0x37, 0x17, 0xe5, 0xa0, 0x64, 0xc9, 0x00, 0xb7,
132     0xe7, 0xfe, 0xf4, 0x29, 0xd9, 0x2e, 0x43, 0x6b, 0x19, 0x20, 0xbd, 0x99,
133     0x75, 0xe7, 0x76, 0xf8, 0xd3, 0xae, 0xaf, 0x7e, 0xb8, 0xeb, 0x81, 0xf4,
134     0x9d, 0xfe, 0x07, 0x2b, 0x0b, 0x63, 0x0b, 0x5a, 0x55, 0x90, 0x71, 0x7d,
135     0xf1, 0xdb, 0xd9, 0xb1, 0x41, 0x41, 0x68, 0x2f, 0x4e, 0x39, 0x02, 0x40,
136     0x5a, 0x34, 0x66, 0xd8, 0xf5, 0xe2, 0x7f, 0x18, 0xb5, 0x00, 0x6e, 0x26,
137     0x84, 0x27, 0x14, 0x93, 0xfb, 0xfc, 0xc6, 0x0f, 0x5e, 0x27, 0xe6, 0xe1,
138     0xe9, 0xc0, 0x8a, 0xe4, 0x34, 0xda, 0xe9, 0xa2, 0x4b, 0x73, 0xbc, 0x8c,
139     0xb9, 0xba, 0x13, 0x6c, 0x7a, 0x2b, 0x51, 0x84, 0xa3, 0x4a, 0xe0, 0x30,
140     0x10, 0x06, 0x7e, 0xed, 0x17, 0x5a, 0x14, 0x00, 0xc9, 0xef, 0x85, 0xea,
141     0x52, 0x2c, 0xbc, 0x65, 0x02, 0x40, 0x51, 0xe3, 0xf2, 0x83, 0x19, 0x9b,
142     0xc4, 0x1e, 0x2f, 0x50, 0x3d, 0xdf, 0x5a, 0xa2, 0x18, 0xca, 0x5f, 0x2e,
143     0x49, 0xaf, 0x6f, 0xcc, 0xfa, 0x65, 0x77, 0x94, 0xb5, 0xa1, 0x0a, 0xa9,
144     0xd1, 0x8a, 0x39, 0x37, 0xf4, 0x0b, 0xa0, 0xd7, 0x82, 0x27, 0x5e, 0xae,
145     0x17, 0x17, 0xa1, 0x1e, 0x54, 0x34, 0xbf, 0x6e, 0xc4, 0x8e, 0x99, 0x5d,
146     0x08, 0xf1, 0x2d, 0x86, 0x9d, 0xa5, 0x20, 0x1b, 0xe5, 0xdf,
147 };
148
149 #ifndef OPENSSL_NO_DH
150 static const unsigned char kExampleDHPrivateKeyDER[] = {
151     0x30, 0x82, 0x02, 0x26, 0x02, 0x01, 0x00, 0x30, 0x82, 0x01, 0x17, 0x06,
152     0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x03, 0x01, 0x30, 0x82,
153     0x01, 0x08, 0x02, 0x82, 0x01, 0x01, 0x00, 0xD8, 0x4B, 0x0F, 0x0E, 0x6B,
154     0x79, 0xE9, 0x23, 0x4E, 0xE4, 0xBE, 0x9A, 0x8F, 0x7A, 0x5C, 0xA3, 0x20,
155     0xD0, 0x86, 0x6B, 0x95, 0x78, 0x39, 0x59, 0x7A, 0x11, 0x2A, 0x5B, 0x87,
156     0xA4, 0xFB, 0x2F, 0x99, 0xD0, 0x57, 0xF5, 0xE1, 0xA3, 0xAF, 0x41, 0xD1,
157     0xCD, 0xA3, 0x94, 0xBB, 0xE5, 0x5A, 0x68, 0xE2, 0xEE, 0x69, 0x56, 0x51,
158     0xB2, 0xEE, 0xF2, 0xFE, 0x10, 0xC9, 0x55, 0xE3, 0x82, 0x3C, 0x50, 0x0D,
159     0xF5, 0x82, 0x73, 0xE4, 0xD6, 0x3E, 0x45, 0xB4, 0x89, 0x80, 0xE4, 0xF0,
160     0x99, 0x85, 0x2B, 0x4B, 0xF9, 0xB8, 0xFD, 0x2C, 0x3C, 0x49, 0x2E, 0xB3,
161     0x56, 0x7E, 0x99, 0x07, 0xD3, 0xF7, 0xD9, 0xE4, 0x0C, 0x64, 0xC5, 0x7D,
162     0x03, 0x8E, 0x05, 0x3C, 0x0A, 0x40, 0x17, 0xAD, 0xA8, 0x0F, 0x9B, 0xF4,
163     0x8B, 0xA7, 0xDB, 0x16, 0x4F, 0x4A, 0x57, 0x0B, 0x89, 0x80, 0x0B, 0x9F,
164     0x26, 0x56, 0x3F, 0x1D, 0xFA, 0x52, 0x2D, 0x1A, 0x9E, 0xDC, 0x42, 0xA3,
165     0x2E, 0xA9, 0x87, 0xE3, 0x8B, 0x45, 0x5E, 0xEE, 0x99, 0xB8, 0x30, 0x15,
166     0x58, 0xA3, 0x5F, 0xB5, 0x69, 0xD8, 0x0C, 0xE8, 0x6B, 0x36, 0xD8, 0xAB,
167     0xD8, 0xE4, 0x77, 0x46, 0x13, 0xA2, 0x15, 0xB3, 0x9C, 0xAD, 0x99, 0x91,
168     0xE5, 0xA3, 0x30, 0x7D, 0x40, 0x70, 0xB3, 0x32, 0x5E, 0xAF, 0x96, 0x8D,
169     0xE6, 0x3F, 0x47, 0xA3, 0x18, 0xDA, 0xE1, 0x9A, 0x20, 0x11, 0xE1, 0x49,
170     0x51, 0x45, 0xE3, 0x8C, 0xA5, 0x56, 0x39, 0x67, 0xCB, 0x9D, 0xCF, 0xBA,
171     0xF4, 0x46, 0x4E, 0x0A, 0xB6, 0x0B, 0xA9, 0xB4, 0xF6, 0xF1, 0x6A, 0xC8,
172     0x63, 0xE2, 0xB4, 0xB2, 0x9F, 0x44, 0xAA, 0x0A, 0xDA, 0x53, 0xF7, 0x52,
173     0x14, 0x57, 0xEE, 0x2C, 0x5D, 0x31, 0x9C, 0x27, 0x03, 0x64, 0x9E, 0xC0,
174     0x1E, 0x4B, 0x1B, 0x4F, 0xEE, 0xA6, 0x3F, 0xC1, 0x3E, 0x61, 0x93, 0x02,
175     0x01, 0x02, 0x04, 0x82, 0x01, 0x04, 0x02, 0x82, 0x01, 0x00, 0x7E, 0xC2,
176     0x04, 0xF9, 0x95, 0xC7, 0xEF, 0x96, 0xBE, 0xA0, 0x9D, 0x2D, 0xC3, 0x0C,
177     0x3A, 0x67, 0x02, 0x7C, 0x7D, 0x3B, 0xC9, 0xB1, 0xDE, 0x13, 0x97, 0x64,
178     0xEF, 0x87, 0x80, 0x4F, 0xBF, 0xA2, 0xAC, 0x18, 0x6B, 0xD5, 0xB2, 0x42,
179     0x0F, 0xDA, 0x28, 0x40, 0x93, 0x40, 0xB2, 0x1E, 0x80, 0xB0, 0x6C, 0xDE,
180     0x9C, 0x54, 0xA4, 0xB4, 0x68, 0x29, 0xE0, 0x13, 0x57, 0x1D, 0xC9, 0x87,
181     0xC0, 0xDE, 0x2F, 0x1D, 0x72, 0xF0, 0xC0, 0xE4, 0x4E, 0x04, 0x48, 0xF5,
182     0x2D, 0x8D, 0x9A, 0x1B, 0xE5, 0xEB, 0x06, 0xAB, 0x7C, 0x74, 0x10, 0x3C,
183     0xA8, 0x2D, 0x39, 0xBC, 0xE3, 0x15, 0x3E, 0x63, 0x37, 0x8C, 0x1B, 0xF1,
184     0xB3, 0x99, 0xB6, 0xAE, 0x5A, 0xEB, 0xB3, 0x3D, 0x30, 0x39, 0x69, 0xDB,
185     0xF2, 0x4F, 0x94, 0xB7, 0x71, 0xAF, 0xBA, 0x5C, 0x1F, 0xF8, 0x6B, 0xE5,
186     0xD1, 0xB1, 0x00, 0x81, 0xE2, 0x6D, 0xEC, 0x65, 0xF7, 0x7E, 0xCE, 0x03,
187     0x84, 0x68, 0x42, 0x6A, 0x8B, 0x47, 0x8E, 0x4A, 0x88, 0xDE, 0x82, 0xDD,
188     0xAF, 0xA9, 0x6F, 0x18, 0xF7, 0xC6, 0xE2, 0xB9, 0x97, 0xCE, 0x47, 0x8F,
189     0x85, 0x19, 0x61, 0x42, 0x67, 0x21, 0x7D, 0x13, 0x6E, 0xB5, 0x5A, 0x62,
190     0xF3, 0x08, 0xE2, 0x70, 0x3B, 0x0E, 0x85, 0x3C, 0xA1, 0xD3, 0xED, 0x7A,
191     0x43, 0xD6, 0xDE, 0x30, 0x5C, 0x48, 0xB2, 0x99, 0xAB, 0x3E, 0x65, 0xA6,
192     0x66, 0x80, 0x22, 0xFF, 0x92, 0xC1, 0x42, 0x1C, 0x30, 0x87, 0x74, 0x1E,
193     0x53, 0x57, 0x7C, 0xF8, 0x77, 0x51, 0xF1, 0x74, 0x16, 0xF4, 0x45, 0x26,
194     0x77, 0x0A, 0x05, 0x96, 0x13, 0x12, 0x06, 0x86, 0x2B, 0xB8, 0x49, 0x82,
195     0x69, 0x43, 0x0A, 0x57, 0xA7, 0x30, 0x19, 0x4C, 0xB8, 0x47, 0x82, 0x6E,
196     0x64, 0x7A, 0x06, 0x13, 0x5A, 0x82, 0x98, 0xD6, 0x7A, 0x09, 0xEC, 0x03,
197     0x8D, 0x03
198 };
199 #endif /* OPENSSL_NO_DH */
200
201 #ifndef OPENSSL_NO_EC
202 /*
203  * kExampleECKeyDER is a sample EC private key encoded as an ECPrivateKey
204  * structure.
205  */
206 static const unsigned char kExampleECKeyDER[] = {
207     0x30, 0x77, 0x02, 0x01, 0x01, 0x04, 0x20, 0x07, 0x0f, 0x08, 0x72, 0x7a,
208     0xd4, 0xa0, 0x4a, 0x9c, 0xdd, 0x59, 0xc9, 0x4d, 0x89, 0x68, 0x77, 0x08,
209     0xb5, 0x6f, 0xc9, 0x5d, 0x30, 0x77, 0x0e, 0xe8, 0xd1, 0xc9, 0xce, 0x0a,
210     0x8b, 0xb4, 0x6a, 0xa0, 0x0a, 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d,
211     0x03, 0x01, 0x07, 0xa1, 0x44, 0x03, 0x42, 0x00, 0x04, 0xe6, 0x2b, 0x69,
212     0xe2, 0xbf, 0x65, 0x9f, 0x97, 0xbe, 0x2f, 0x1e, 0x0d, 0x94, 0x8a, 0x4c,
213     0xd5, 0x97, 0x6b, 0xb7, 0xa9, 0x1e, 0x0d, 0x46, 0xfb, 0xdd, 0xa9, 0xa9,
214     0x1e, 0x9d, 0xdc, 0xba, 0x5a, 0x01, 0xe7, 0xd6, 0x97, 0xa8, 0x0a, 0x18,
215     0xf9, 0xc3, 0xc4, 0xa3, 0x1e, 0x56, 0xe2, 0x7c, 0x83, 0x48, 0xdb, 0x16,
216     0x1a, 0x1c, 0xf5, 0x1d, 0x7e, 0xf1, 0x94, 0x2d, 0x4b, 0xcf, 0x72, 0x22,
217     0xc1,
218 };
219
220 /* P-384 sample EC private key in PKCS8 format (no public key) */
221 static const unsigned char kExampleECKey2DER[] = {
222     0x30, 0x4E, 0x02, 0x01, 0x00, 0x30, 0x10, 0x06, 0x07, 0x2A, 0x86, 0x48,
223     0xCE, 0x3D, 0x02, 0x01, 0x06, 0x05, 0x2B, 0x81, 0x04, 0x00, 0x22, 0x04,
224     0x37, 0x30, 0x35, 0x02, 0x01, 0x01, 0x04, 0x30, 0x73, 0xE3, 0x3A, 0x05,
225     0xF2, 0xB6, 0x99, 0x6D, 0x0C, 0x33, 0x7F, 0x15, 0x9E, 0x10, 0xA9, 0x17,
226     0x4C, 0x0A, 0x82, 0x57, 0x71, 0x13, 0x7A, 0xAC, 0x46, 0xA2, 0x5E, 0x1C,
227     0xE0, 0xC7, 0xB2, 0xF8, 0x20, 0x40, 0xC2, 0x27, 0xC8, 0xBE, 0x02, 0x7E,
228     0x96, 0x69, 0xE0, 0x04, 0xCB, 0x89, 0x0B, 0x42
229 };
230
231 static const unsigned char kExampleECXKey2DER[] = {
232     0x30, 0x2E, 0x02, 0x01, 0x00, 0x30, 0x05, 0x06, 0x03, 0x2b, 0x65, 0x6e,
233     0x04, 0x22, 0x04, 0x20, 0xc8, 0xa9, 0xd5, 0xa9, 0x10, 0x91, 0xad, 0x85,
234     0x1c, 0x66, 0x8b, 0x07, 0x36, 0xc1, 0xc9, 0xa0, 0x29, 0x36, 0xc0, 0xd3,
235     0xad, 0x62, 0x67, 0x08, 0x58, 0x08, 0x80, 0x47, 0xba, 0x05, 0x74, 0x75
236 };
237 #endif
238
239 typedef struct APK_DATA_st {
240     const unsigned char *kder;
241     size_t size;
242     int evptype;
243 } APK_DATA;
244
245 static APK_DATA keydata[] = {
246     {kExampleRSAKeyDER, sizeof(kExampleRSAKeyDER), EVP_PKEY_RSA},
247     {kExampleRSAKeyPKCS8, sizeof(kExampleRSAKeyPKCS8), EVP_PKEY_RSA},
248 #ifndef OPENSSL_NO_EC
249     {kExampleECXKey2DER, sizeof(kExampleECXKey2DER), EVP_PKEY_X25519},
250     {kExampleECKeyDER, sizeof(kExampleECKeyDER), EVP_PKEY_EC},
251     {kExampleECKey2DER, sizeof(kExampleECKey2DER), EVP_PKEY_EC},
252 #endif
253 #ifndef OPENSSL_NO_DH
254     {kExampleDHPrivateKeyDER, sizeof(kExampleDHPrivateKeyDER), EVP_PKEY_DH},
255 #endif
256 };
257
258 static int pkey_has_private(EVP_PKEY *key, const char *privtag,
259                             int use_octstring)
260 {
261     int ret = 0;
262
263     if (use_octstring) {
264         unsigned char buf[64];
265
266         ret = EVP_PKEY_get_octet_string_param(key, privtag, buf, sizeof(buf),
267                                               NULL);
268     } else {
269         BIGNUM *bn = NULL;
270
271         ret = EVP_PKEY_get_bn_param(key, privtag, &bn);
272         BN_free(bn);
273     }
274     return ret;
275 }
276
277 static int do_pkey_tofrom_data_select(EVP_PKEY *key, const char *keytype)
278 {
279     int ret = 0;
280     OSSL_PARAM *pub_params = NULL, *keypair_params = NULL;
281     EVP_PKEY *fromkey = NULL, *fromkeypair = NULL;
282     EVP_PKEY_CTX *fromctx = NULL;
283     const char *privtag = strcmp(keytype, "RSA") == 0 ? "d" : "priv";
284     const int use_octstring = strcmp(keytype, "X25519") == 0;
285
286     /*
287      * Select only the public key component when using EVP_PKEY_todata() and
288      * check that the resulting param array does not contain a private key.
289      */
290     if (!TEST_int_eq(EVP_PKEY_todata(key, EVP_PKEY_PUBLIC_KEY, &pub_params), 1)
291         || !TEST_ptr_null(OSSL_PARAM_locate(pub_params, privtag)))
292         goto end;
293     /*
294      * Select the keypair when using EVP_PKEY_todata() and check that
295      * the param array contains a private key.
296      */
297     if (!TEST_int_eq(EVP_PKEY_todata(key, EVP_PKEY_KEYPAIR, &keypair_params), 1)
298         || !TEST_ptr(OSSL_PARAM_locate(keypair_params, privtag)))
299         goto end;
300
301     /*
302      * Select only the public key when using EVP_PKEY_fromdata() and check that
303      * the resulting key does not contain a private key.
304      */
305     if (!TEST_ptr(fromctx = EVP_PKEY_CTX_new_from_name(mainctx, keytype, NULL))
306         || !TEST_int_eq(EVP_PKEY_fromdata_init(fromctx), 1)
307         || !TEST_int_eq(EVP_PKEY_fromdata(fromctx, &fromkey, EVP_PKEY_PUBLIC_KEY,
308                                           keypair_params), 1)
309         || !TEST_false(pkey_has_private(fromkey, privtag, use_octstring)))
310         goto end;
311     /*
312      * Select the keypair when using EVP_PKEY_fromdata() and check that
313      * the resulting key contains a private key.
314      */
315     if (!TEST_int_eq(EVP_PKEY_fromdata(fromctx, &fromkeypair,
316                                        EVP_PKEY_KEYPAIR, keypair_params), 1)
317         || !TEST_true(pkey_has_private(fromkeypair, privtag, use_octstring)))
318         goto end;
319     ret = 1;
320 end:
321     EVP_PKEY_free(fromkeypair);
322     EVP_PKEY_free(fromkey);
323     EVP_PKEY_CTX_free(fromctx);
324     OSSL_PARAM_free(keypair_params);
325     OSSL_PARAM_free(pub_params);
326     return ret;
327 }
328
329 #ifndef OPENSSL_NO_DH
330 static int test_dh_tofrom_data_select(void)
331 {
332     int ret;
333     OSSL_PARAM params[2];
334     EVP_PKEY *key = NULL;
335     EVP_PKEY_CTX *gctx = NULL;
336
337     params[0] = OSSL_PARAM_construct_utf8_string("group", "ffdhe2048", 0);
338     params[1] = OSSL_PARAM_construct_end();
339     ret = TEST_ptr(gctx = EVP_PKEY_CTX_new_from_name(mainctx, "DHX", NULL))
340           && TEST_int_gt(EVP_PKEY_keygen_init(gctx), 0)
341           && TEST_true(EVP_PKEY_CTX_set_params(gctx, params))
342           && TEST_int_gt(EVP_PKEY_generate(gctx, &key), 0)
343           && TEST_true(do_pkey_tofrom_data_select(key, "DHX"));
344     EVP_PKEY_free(key);
345     EVP_PKEY_CTX_free(gctx);
346     return ret;
347 }
348 #endif
349
350 #ifndef OPENSSL_NO_EC
351 static int test_ec_tofrom_data_select(void)
352 {
353     int ret;
354     EVP_PKEY *key = NULL;
355
356     ret = TEST_ptr(key = EVP_PKEY_Q_keygen(mainctx, NULL, "EC", "P-256"))
357           && TEST_true(do_pkey_tofrom_data_select(key, "EC"));
358     EVP_PKEY_free(key);
359     return ret;
360 }
361
362 static int test_ecx_tofrom_data_select(void)
363 {
364     int ret;
365     EVP_PKEY *key = NULL;
366
367     ret = TEST_ptr(key = EVP_PKEY_Q_keygen(mainctx, NULL, "X25519"))
368           && TEST_true(do_pkey_tofrom_data_select(key, "X25519"));
369     EVP_PKEY_free(key);
370     return ret;
371 }
372 #endif
373
374 static int test_rsa_tofrom_data_select(void)
375 {
376     int ret;
377     EVP_PKEY *key = NULL;
378     const unsigned char *pdata = kExampleRSAKeyDER;
379     int pdata_len = sizeof(kExampleRSAKeyDER);
380
381     ret = TEST_ptr(key = d2i_AutoPrivateKey_ex(NULL, &pdata, pdata_len,
382                                                mainctx, NULL))
383           && TEST_true(do_pkey_tofrom_data_select(key, "RSA"));
384     EVP_PKEY_free(key);
385     return ret;
386 }
387
388 /* This is the equivalent of test_d2i_AutoPrivateKey in evp_extra_test */
389 static int test_d2i_AutoPrivateKey_ex(int i)
390 {
391     int ret = 0;
392     const unsigned char *p;
393     EVP_PKEY *pkey = NULL;
394     const APK_DATA *ak = &keydata[i];
395     const unsigned char *input = ak->kder;
396     size_t input_len = ak->size;
397     int expected_id = ak->evptype;
398     BIGNUM *p_bn = NULL;
399     BIGNUM *g_bn = NULL;
400     BIGNUM *priv_bn = NULL;
401
402     p = input;
403     if (!TEST_ptr(pkey = d2i_AutoPrivateKey_ex(NULL, &p, input_len, mainctx,
404                                                NULL))
405             || !TEST_ptr_eq(p, input + input_len)
406             || !TEST_int_eq(EVP_PKEY_get_id(pkey), expected_id))
407         goto done;
408
409     if (ak->evptype == EVP_PKEY_RSA) {
410         if (!TEST_true(EVP_PKEY_get_bn_param(pkey, OSSL_PKEY_PARAM_RSA_D,
411                                              &priv_bn)))
412             goto done;
413     } else if (ak->evptype == EVP_PKEY_X25519) {
414         unsigned char buffer[32];
415         size_t len;
416
417         if (!TEST_true(EVP_PKEY_get_octet_string_param(pkey,
418                                                        OSSL_PKEY_PARAM_PRIV_KEY,
419                                                        buffer, sizeof(buffer),
420                                                        &len)))
421             goto done;
422     } else {
423         if (!TEST_true(EVP_PKEY_get_bn_param(pkey, OSSL_PKEY_PARAM_PRIV_KEY,
424                                              &priv_bn)))
425             goto done;
426     }
427
428     if (ak->evptype == EVP_PKEY_DH) {
429         if (!TEST_true(EVP_PKEY_get_bn_param(pkey, OSSL_PKEY_PARAM_FFC_P, &p_bn))
430             || !TEST_true(EVP_PKEY_get_bn_param(pkey, OSSL_PKEY_PARAM_FFC_G,
431                                                 &g_bn)))
432             goto done;
433     }
434
435     ret = 1;
436 done:
437     BN_free(p_bn);
438     BN_free(g_bn);
439     BN_free(priv_bn);
440     EVP_PKEY_free(pkey);
441     return ret;
442 }
443
444 #ifndef OPENSSL_NO_DES
445 static int test_pkcs8key_nid_bio(void)
446 {
447     int ret;
448     const int nid = NID_pbe_WithSHA1And3_Key_TripleDES_CBC;
449     static const char pwd[] = "PASSWORD";
450     EVP_PKEY *pkey = NULL, *pkey_dec = NULL;
451     BIO *in = NULL, *enc_bio = NULL;
452     char *enc_data = NULL;
453     long enc_datalen = 0;
454     OSSL_PROVIDER *provider = NULL;
455
456     ret = TEST_ptr(provider = OSSL_PROVIDER_load(NULL, "default"))
457           && TEST_ptr(enc_bio = BIO_new(BIO_s_mem()))
458           && TEST_ptr(in = BIO_new_mem_buf(kExampleRSAKeyPKCS8,
459                                            sizeof(kExampleRSAKeyPKCS8)))
460           && TEST_ptr(pkey = d2i_PrivateKey_ex_bio(in, NULL, NULL, NULL))
461           && TEST_int_eq(i2d_PKCS8PrivateKey_nid_bio(enc_bio, pkey, nid,
462                                                      pwd, sizeof(pwd) - 1,
463                                                      NULL, NULL), 1)
464           && TEST_int_gt(enc_datalen = BIO_get_mem_data(enc_bio, &enc_data), 0)
465           && TEST_ptr(pkey_dec = d2i_PKCS8PrivateKey_bio(enc_bio, NULL, NULL,
466                                                          (void *)pwd))
467           && TEST_true(EVP_PKEY_eq(pkey, pkey_dec));
468
469     EVP_PKEY_free(pkey_dec);
470     EVP_PKEY_free(pkey);
471     BIO_free(in);
472     BIO_free(enc_bio);
473     OSSL_PROVIDER_unload(provider);
474     return ret;
475 }
476 #endif /* OPENSSL_NO_DES */
477
478 static int test_alternative_default(void)
479 {
480     OSSL_LIB_CTX *oldctx;
481     EVP_MD *sha256;
482     int ok = 0;
483
484     /*
485      * setup_tests() loaded the "null" provider in the current default, so
486      * we know this fetch should fail.
487      */
488     if (!TEST_ptr_null(sha256 = EVP_MD_fetch(NULL, "SHA2-256", NULL)))
489         goto err;
490
491     /*
492      * Now we switch to our main library context, and try again.  Since no
493      * providers are loaded in this one, it should fall back to the default.
494      */
495     if (!TEST_ptr(oldctx = OSSL_LIB_CTX_set0_default(mainctx))
496         || !TEST_ptr(sha256 = EVP_MD_fetch(NULL, "SHA2-256", NULL)))
497         goto err;
498     EVP_MD_free(sha256);
499     sha256 = NULL;
500
501     /*
502      * Switching back should give us our main library context back, and
503      * fetching SHA2-256 should fail again.
504      */
505     if (!TEST_ptr_eq(OSSL_LIB_CTX_set0_default(oldctx), mainctx)
506         || !TEST_ptr_null(sha256 = EVP_MD_fetch(NULL, "SHA2-256", NULL)))
507         goto err;
508
509     ok = 1;
510  err:
511     EVP_MD_free(sha256);
512     return ok;
513 }
514
515 static int test_provider_unload_effective(int testid)
516 {
517     EVP_MD *sha256 = NULL;
518     OSSL_PROVIDER *provider = NULL;
519     int ok = 0;
520
521     if (!TEST_ptr(provider = OSSL_PROVIDER_load(NULL, "default"))
522         || !TEST_ptr(sha256 = EVP_MD_fetch(NULL, "SHA2-256", NULL)))
523         goto err;
524
525     if (testid > 0) {
526         OSSL_PROVIDER_unload(provider);
527         provider = NULL;
528         EVP_MD_free(sha256);
529         sha256 = NULL;
530     } else {
531         EVP_MD_free(sha256);
532         sha256 = NULL;
533         OSSL_PROVIDER_unload(provider);
534         provider = NULL;
535     }
536
537     /*
538      * setup_tests() loaded the "null" provider in the current default, and
539      * we unloaded it above after the load so we know this fetch should fail.
540      */
541     if (!TEST_ptr_null(sha256 = EVP_MD_fetch(NULL, "SHA2-256", NULL)))
542         goto err;
543
544     ok = 1;
545  err:
546     EVP_MD_free(sha256);
547     OSSL_PROVIDER_unload(provider);
548     return ok;
549 }
550
551 static int test_d2i_PrivateKey_ex(int testid)
552 {
553     int ok = 0;
554     OSSL_PROVIDER *provider = NULL;
555     BIO *key_bio = NULL;
556     EVP_PKEY *pkey = NULL;
557     int id = (testid == 0) ? 0 : 2;
558
559     if (!TEST_ptr(provider = OSSL_PROVIDER_load(NULL, "default")))
560         goto err;
561     if (!TEST_ptr(key_bio = BIO_new_mem_buf(keydata[id].kder, keydata[id].size)))
562         goto err;
563     if (!TEST_ptr_null(pkey = PEM_read_bio_PrivateKey(key_bio, NULL, NULL, NULL)))
564         goto err;
565
566     ERR_clear_error();
567     if (!TEST_int_ge(BIO_seek(key_bio, 0), 0))
568         goto err;
569     ok = TEST_ptr(pkey = d2i_PrivateKey_bio(key_bio, NULL));
570     TEST_int_eq(ERR_peek_error(), 0);
571     test_openssl_errors();
572
573  err:
574     EVP_PKEY_free(pkey);
575     BIO_free(key_bio);
576     OSSL_PROVIDER_unload(provider);
577
578     return ok;
579 }
580
581 static int test_PEM_read_bio_negative(int testid)
582 {
583     int ok = 0;
584     OSSL_PROVIDER *provider = NULL;
585     BIO *key_bio = NULL;
586     EVP_PKEY *pkey = NULL;
587
588     if (!TEST_ptr(key_bio = BIO_new_mem_buf(keydata[testid].kder, keydata[testid].size)))
589         goto err;
590     ERR_clear_error();
591     if (!TEST_ptr_null(pkey = PEM_read_bio_PrivateKey(key_bio, NULL, NULL, NULL)))
592         goto err;
593     if (!TEST_int_ne(ERR_peek_error(), 0))
594         goto err;
595     if (!TEST_ptr(provider = OSSL_PROVIDER_load(NULL, "default")))
596         goto err;
597     if (!TEST_int_ge(BIO_seek(key_bio, 0), 0))
598         goto err;
599     ERR_clear_error();
600     if (!TEST_ptr_null(pkey = PEM_read_bio_PrivateKey(key_bio, NULL, NULL, NULL)))
601         goto err;
602     if (!TEST_int_ne(ERR_peek_error(), 0))
603         goto err;
604
605     ok = 1;
606
607  err:
608     test_openssl_errors();
609     EVP_PKEY_free(pkey);
610     BIO_free(key_bio);
611     OSSL_PROVIDER_unload(provider);
612
613     return ok;
614 }
615
616 static int do_fromdata_key_is_equal(const OSSL_PARAM params[],
617                                     const EVP_PKEY *expected, const char *type)
618 {
619     EVP_PKEY_CTX *ctx = NULL;
620     EVP_PKEY *pkey = NULL;
621     int ret;
622
623     ret = TEST_ptr(ctx = EVP_PKEY_CTX_new_from_name(mainctx, type, NULL))
624           && TEST_int_eq(EVP_PKEY_fromdata_init(ctx), 1)
625           && TEST_int_eq(EVP_PKEY_fromdata(ctx, &pkey,
626                                            EVP_PKEY_KEYPAIR,
627                                            (OSSL_PARAM *)params), 1)
628           && TEST_true(EVP_PKEY_eq(pkey, expected));
629     EVP_PKEY_CTX_free(ctx);
630     EVP_PKEY_free(pkey);
631     return ret;
632 }
633
634 #ifndef OPENSSL_NO_DSA
635 /*
636  * This data was generated using:
637  * > openssl genpkey \
638  *   -genparam -algorithm DSA -pkeyopt type:fips186_4 -text \
639  *   -pkeyopt gindex:5 -out dsa_param.pem
640  * > openssl genpkey \
641  *   -paramfile dsa_param.pem -pkeyopt type:fips186_4 -out dsa_priv.pem
642  */
643 static const unsigned char dsa_key[] = {
644     0x30, 0x82, 0x03, 0x4e, 0x02, 0x01, 0x00, 0x02, 0x82, 0x01, 0x01, 0x00,
645     0xda, 0xb3, 0x46, 0x4d, 0x54, 0x57, 0xc7, 0xb4, 0x61, 0xa0, 0x6f, 0x66,
646     0x17, 0xda, 0xeb, 0x90, 0xf0, 0xa3, 0xd1, 0x29, 0xc9, 0x5f, 0xf2, 0x21,
647     0x3d, 0x85, 0xa3, 0x4a, 0xf0, 0xf8, 0x36, 0x39, 0x1b, 0xe3, 0xee, 0x37,
648     0x70, 0x06, 0x9b, 0xe8, 0xe3, 0x0a, 0xd2, 0xf1, 0xf6, 0xc4, 0x42, 0x23,
649     0x1f, 0x74, 0x78, 0xc2, 0x16, 0xf5, 0xce, 0xd6, 0xab, 0xa0, 0xc6, 0xe8,
650     0x99, 0x3d, 0xf8, 0x8b, 0xfb, 0x47, 0xf8, 0x5e, 0x05, 0x68, 0x6d, 0x8b,
651     0xa8, 0xad, 0xa1, 0xc2, 0x3a, 0x4e, 0xe0, 0xad, 0xec, 0x38, 0x75, 0x21,
652     0x55, 0x22, 0xce, 0xa2, 0xe9, 0xe5, 0x3b, 0xd7, 0x44, 0xeb, 0x5a, 0x03,
653     0x59, 0xa0, 0xc5, 0x7a, 0x92, 0x59, 0x7d, 0x7a, 0x07, 0x80, 0xfc, 0x4e,
654     0xf8, 0x56, 0x7e, 0xf1, 0x06, 0xe0, 0xba, 0xb2, 0xe7, 0x5b, 0x22, 0x55,
655     0xee, 0x4b, 0x42, 0x61, 0x67, 0x2c, 0x43, 0x9a, 0x38, 0x2b, 0x17, 0xc2,
656     0x62, 0x12, 0x8b, 0x0b, 0x22, 0x8c, 0x0c, 0x1c, 0x1c, 0x92, 0xb1, 0xec,
657     0x70, 0xce, 0x0f, 0x8c, 0xff, 0x8d, 0x21, 0xf9, 0x19, 0x68, 0x4d, 0x32,
658     0x59, 0x78, 0x42, 0x1d, 0x0c, 0xc5, 0x1a, 0xcb, 0x28, 0xe2, 0xc1, 0x1a,
659     0x35, 0xf1, 0x42, 0x0a, 0x19, 0x39, 0xfa, 0x83, 0xd1, 0xb4, 0xaa, 0x69,
660     0x0f, 0xc2, 0x8e, 0xf9, 0x59, 0x2c, 0xee, 0x11, 0xfc, 0x3e, 0x4b, 0x44,
661     0xfb, 0x9a, 0x32, 0xc8, 0x78, 0x23, 0x56, 0x85, 0x49, 0x21, 0x43, 0x12,
662     0x79, 0xbd, 0xa0, 0x70, 0x47, 0x2f, 0xae, 0xb6, 0xd7, 0x6c, 0xc6, 0x07,
663     0x76, 0xa9, 0x8a, 0xa2, 0x16, 0x02, 0x89, 0x1f, 0x1a, 0xd1, 0xa2, 0x96,
664     0x56, 0xd1, 0x1f, 0x10, 0xe1, 0xe5, 0x9f, 0x3f, 0xdd, 0x09, 0x0c, 0x40,
665     0x90, 0x71, 0xef, 0x14, 0x41, 0x02, 0x82, 0x3a, 0x6b, 0xe1, 0xf8, 0x2c,
666     0x5d, 0xbe, 0xfd, 0x1b, 0x02, 0x1d, 0x00, 0xe0, 0x20, 0xe0, 0x7c, 0x02,
667     0x16, 0xa7, 0x6c, 0x6a, 0x19, 0xba, 0xd5, 0x83, 0x73, 0xf3, 0x7d, 0x31,
668     0xef, 0xa7, 0xe1, 0x5d, 0x5b, 0x7f, 0xf3, 0xfc, 0xda, 0x84, 0x31, 0x02,
669     0x82, 0x01, 0x01, 0x00, 0x83, 0xdb, 0xa1, 0xbc, 0x3e, 0xc7, 0x29, 0xa5,
670     0x6a, 0x5c, 0x2c, 0xe8, 0x7a, 0x8c, 0x7e, 0xe8, 0xb8, 0x3e, 0x13, 0x47,
671     0xcd, 0x36, 0x7e, 0x79, 0x30, 0x7a, 0x28, 0x03, 0xd3, 0xd4, 0xd2, 0xe3,
672     0xee, 0x3b, 0x46, 0xda, 0xe0, 0x71, 0xe6, 0xcf, 0x46, 0x86, 0x0a, 0x37,
673     0x57, 0xb6, 0xe9, 0xcf, 0xa1, 0x78, 0x19, 0xb8, 0x72, 0x9f, 0x30, 0x8c,
674     0x2a, 0x04, 0x7c, 0x2f, 0x0c, 0x27, 0xa7, 0xb3, 0x23, 0xe0, 0x46, 0xf2,
675     0x75, 0x0c, 0x03, 0x4c, 0xad, 0xfb, 0xc1, 0xcb, 0x28, 0xcd, 0xa0, 0x63,
676     0xdb, 0x44, 0x88, 0xe0, 0xda, 0x6c, 0x5b, 0x89, 0xb2, 0x5b, 0x40, 0x6d,
677     0xeb, 0x78, 0x7a, 0xd5, 0xaf, 0x40, 0x52, 0x46, 0x63, 0x92, 0x13, 0x0d,
678     0xee, 0xee, 0xf9, 0x53, 0xca, 0x2d, 0x4e, 0x3b, 0x13, 0xd8, 0x0f, 0x50,
679     0xd0, 0x44, 0x57, 0x67, 0x0f, 0x45, 0x8f, 0x21, 0x30, 0x97, 0x9e, 0x80,
680     0xd9, 0xd0, 0x91, 0xb7, 0xc9, 0x5a, 0x69, 0xda, 0xeb, 0xd5, 0xea, 0x37,
681     0xf6, 0xb3, 0xbe, 0x1f, 0x24, 0xf1, 0x55, 0x14, 0x28, 0x05, 0xb5, 0xd8,
682     0x84, 0x0f, 0x62, 0x85, 0xaa, 0xec, 0x77, 0x64, 0xfd, 0x80, 0x7c, 0x41,
683     0x00, 0x88, 0xa3, 0x79, 0x7d, 0x4f, 0x6f, 0xe3, 0x76, 0xf4, 0xb5, 0x97,
684     0xb7, 0xeb, 0x67, 0x28, 0xba, 0x07, 0x1a, 0x59, 0x32, 0xc1, 0x53, 0xd9,
685     0x05, 0x6b, 0x63, 0x93, 0xce, 0xa1, 0xd9, 0x7a, 0xb2, 0xff, 0x1c, 0x12,
686     0x0a, 0x9a, 0xe5, 0x51, 0x1e, 0xba, 0xfc, 0x95, 0x2e, 0x28, 0xa9, 0xfc,
687     0x4c, 0xed, 0x7b, 0x05, 0xca, 0x67, 0xe0, 0x2d, 0xd7, 0x54, 0xb3, 0x05,
688     0x1c, 0x23, 0x2b, 0x35, 0x2e, 0x19, 0x48, 0x59, 0x0e, 0x58, 0xa8, 0x01,
689     0x56, 0xfb, 0x78, 0x90, 0xba, 0x08, 0x77, 0x94, 0x45, 0x05, 0x13, 0xc7,
690     0x6b, 0x96, 0xd2, 0xa3, 0xa6, 0x01, 0x9f, 0x34, 0x02, 0x82, 0x01, 0x00,
691     0x16, 0x1a, 0xb4, 0x6d, 0x9f, 0x16, 0x6c, 0xcc, 0x91, 0x66, 0xfe, 0x30,
692     0xeb, 0x8e, 0x44, 0xba, 0x2b, 0x7a, 0xc9, 0xa8, 0x95, 0xf2, 0xa6, 0x38,
693     0xd8, 0xaf, 0x3e, 0x91, 0x68, 0xe8, 0x52, 0xf3, 0x97, 0x37, 0x70, 0xf2,
694     0x47, 0xa3, 0xf4, 0x62, 0x26, 0xf5, 0x3b, 0x71, 0x52, 0x50, 0x15, 0x9c,
695     0x6d, 0xa6, 0x6d, 0x92, 0x4c, 0x48, 0x76, 0x31, 0x54, 0x48, 0xa5, 0x99,
696     0x7a, 0xd4, 0x61, 0xf7, 0x21, 0x44, 0xe7, 0xd8, 0x82, 0xc3, 0x50, 0xd3,
697     0xd9, 0xd4, 0x66, 0x20, 0xab, 0x70, 0x4c, 0x97, 0x9b, 0x8d, 0xac, 0x1f,
698     0x78, 0x27, 0x1e, 0x47, 0xf8, 0x3b, 0xd1, 0x55, 0x73, 0xf3, 0xb4, 0x8e,
699     0x6d, 0x45, 0x40, 0x54, 0xc6, 0xd8, 0x95, 0x15, 0x27, 0xb7, 0x5f, 0x65,
700     0xaa, 0xcb, 0x24, 0xc9, 0x49, 0x87, 0x32, 0xad, 0xcb, 0xf8, 0x35, 0x63,
701     0x56, 0x72, 0x7c, 0x4e, 0x6c, 0xad, 0x5f, 0x26, 0x8c, 0xd2, 0x80, 0x41,
702     0xaf, 0x88, 0x23, 0x20, 0x03, 0xa4, 0xd5, 0x3c, 0x53, 0x54, 0xb0, 0x3d,
703     0xed, 0x0e, 0x9e, 0x53, 0x0a, 0x63, 0x5f, 0xfd, 0x28, 0x57, 0x09, 0x07,
704     0x73, 0xf4, 0x0c, 0xd4, 0x71, 0x5d, 0x6b, 0xa0, 0xd7, 0x86, 0x99, 0x29,
705     0x9b, 0xca, 0xfb, 0xcc, 0xd6, 0x2f, 0xfe, 0xbe, 0x94, 0xef, 0x1a, 0x0e,
706     0x55, 0x84, 0xa7, 0xaf, 0x7b, 0xfa, 0xed, 0x77, 0x61, 0x28, 0x22, 0xee,
707     0x6b, 0x11, 0xdd, 0xb0, 0x17, 0x1e, 0x06, 0xe4, 0x29, 0x4c, 0xc2, 0x3f,
708     0xd6, 0x75, 0xb6, 0x08, 0x04, 0x55, 0x13, 0x48, 0x4f, 0x44, 0xea, 0x8d,
709     0xaf, 0xcb, 0xac, 0x22, 0xc4, 0x6a, 0xb3, 0x86, 0xe5, 0x47, 0xa9, 0xb5,
710     0x72, 0x17, 0x23, 0x11, 0x81, 0x7f, 0x00, 0x00, 0x67, 0x5c, 0xf4, 0x58,
711     0xcc, 0xe2, 0x46, 0xce, 0xf5, 0x6d, 0xd8, 0x18, 0x91, 0xc4, 0x20, 0xbf,
712     0x07, 0x48, 0x45, 0xfd, 0x02, 0x1c, 0x2f, 0x68, 0x44, 0xcb, 0xfb, 0x6b,
713     0xcb, 0x8d, 0x02, 0x49, 0x7c, 0xee, 0xd2, 0xa6, 0xd3, 0x43, 0xb8, 0xa4,
714     0x09, 0xb7, 0xc1, 0xd4, 0x4b, 0xc3, 0x66, 0xa7, 0xe0, 0x21,
715 };
716 static const unsigned char dsa_p[] = {
717     0x00, 0xda, 0xb3, 0x46, 0x4d, 0x54, 0x57, 0xc7, 0xb4, 0x61, 0xa0, 0x6f, 0x66, 0x17, 0xda,
718     0xeb, 0x90, 0xf0, 0xa3, 0xd1, 0x29, 0xc9, 0x5f, 0xf2, 0x21, 0x3d, 0x85, 0xa3, 0x4a, 0xf0,
719     0xf8, 0x36, 0x39, 0x1b, 0xe3, 0xee, 0x37, 0x70, 0x06, 0x9b, 0xe8, 0xe3, 0x0a, 0xd2, 0xf1,
720     0xf6, 0xc4, 0x42, 0x23, 0x1f, 0x74, 0x78, 0xc2, 0x16, 0xf5, 0xce, 0xd6, 0xab, 0xa0, 0xc6,
721     0xe8, 0x99, 0x3d, 0xf8, 0x8b, 0xfb, 0x47, 0xf8, 0x5e, 0x05, 0x68, 0x6d, 0x8b, 0xa8, 0xad,
722     0xa1, 0xc2, 0x3a, 0x4e, 0xe0, 0xad, 0xec, 0x38, 0x75, 0x21, 0x55, 0x22, 0xce, 0xa2, 0xe9,
723     0xe5, 0x3b, 0xd7, 0x44, 0xeb, 0x5a, 0x03, 0x59, 0xa0, 0xc5, 0x7a, 0x92, 0x59, 0x7d, 0x7a,
724     0x07, 0x80, 0xfc, 0x4e, 0xf8, 0x56, 0x7e, 0xf1, 0x06, 0xe0, 0xba, 0xb2, 0xe7, 0x5b, 0x22,
725     0x55, 0xee, 0x4b, 0x42, 0x61, 0x67, 0x2c, 0x43, 0x9a, 0x38, 0x2b, 0x17, 0xc2, 0x62, 0x12,
726     0x8b, 0x0b, 0x22, 0x8c, 0x0c, 0x1c, 0x1c, 0x92, 0xb1, 0xec, 0x70, 0xce, 0x0f, 0x8c, 0xff,
727     0x8d, 0x21, 0xf9, 0x19, 0x68, 0x4d, 0x32, 0x59, 0x78, 0x42, 0x1d, 0x0c, 0xc5, 0x1a, 0xcb,
728     0x28, 0xe2, 0xc1, 0x1a, 0x35, 0xf1, 0x42, 0x0a, 0x19, 0x39, 0xfa, 0x83, 0xd1, 0xb4, 0xaa,
729     0x69, 0x0f, 0xc2, 0x8e, 0xf9, 0x59, 0x2c, 0xee, 0x11, 0xfc, 0x3e, 0x4b, 0x44, 0xfb, 0x9a,
730     0x32, 0xc8, 0x78, 0x23, 0x56, 0x85, 0x49, 0x21, 0x43, 0x12, 0x79, 0xbd, 0xa0, 0x70, 0x47,
731     0x2f, 0xae, 0xb6, 0xd7, 0x6c, 0xc6, 0x07, 0x76, 0xa9, 0x8a, 0xa2, 0x16, 0x02, 0x89, 0x1f,
732     0x1a, 0xd1, 0xa2, 0x96, 0x56, 0xd1, 0x1f, 0x10, 0xe1, 0xe5, 0x9f, 0x3f, 0xdd, 0x09, 0x0c,
733     0x40, 0x90, 0x71, 0xef, 0x14, 0x41, 0x02, 0x82, 0x3a, 0x6b, 0xe1, 0xf8, 0x2c, 0x5d, 0xbe,
734     0xfd, 0x1b
735 };
736 static const unsigned char dsa_q[] = {
737     0x00, 0xe0, 0x20, 0xe0, 0x7c, 0x02, 0x16, 0xa7, 0x6c, 0x6a, 0x19, 0xba, 0xd5, 0x83, 0x73,
738     0xf3, 0x7d, 0x31, 0xef, 0xa7, 0xe1, 0x5d, 0x5b, 0x7f, 0xf3, 0xfc, 0xda, 0x84, 0x31
739 };
740 static const unsigned char dsa_g[] = {
741     0x00, 0x83, 0xdb, 0xa1, 0xbc, 0x3e, 0xc7, 0x29, 0xa5, 0x6a, 0x5c, 0x2c, 0xe8, 0x7a, 0x8c,
742     0x7e, 0xe8, 0xb8, 0x3e, 0x13, 0x47, 0xcd, 0x36, 0x7e, 0x79, 0x30, 0x7a, 0x28, 0x03, 0xd3,
743     0xd4, 0xd2, 0xe3, 0xee, 0x3b, 0x46, 0xda, 0xe0, 0x71, 0xe6, 0xcf, 0x46, 0x86, 0x0a, 0x37,
744     0x57, 0xb6, 0xe9, 0xcf, 0xa1, 0x78, 0x19, 0xb8, 0x72, 0x9f, 0x30, 0x8c, 0x2a, 0x04, 0x7c,
745     0x2f, 0x0c, 0x27, 0xa7, 0xb3, 0x23, 0xe0, 0x46, 0xf2, 0x75, 0x0c, 0x03, 0x4c, 0xad, 0xfb,
746     0xc1, 0xcb, 0x28, 0xcd, 0xa0, 0x63, 0xdb, 0x44, 0x88, 0xe0, 0xda, 0x6c, 0x5b, 0x89, 0xb2,
747     0x5b, 0x40, 0x6d, 0xeb, 0x78, 0x7a, 0xd5, 0xaf, 0x40, 0x52, 0x46, 0x63, 0x92, 0x13, 0x0d,
748     0xee, 0xee, 0xf9, 0x53, 0xca, 0x2d, 0x4e, 0x3b, 0x13, 0xd8, 0x0f, 0x50, 0xd0, 0x44, 0x57,
749     0x67, 0x0f, 0x45, 0x8f, 0x21, 0x30, 0x97, 0x9e, 0x80, 0xd9, 0xd0, 0x91, 0xb7, 0xc9, 0x5a,
750     0x69, 0xda, 0xeb, 0xd5, 0xea, 0x37, 0xf6, 0xb3, 0xbe, 0x1f, 0x24, 0xf1, 0x55, 0x14, 0x28,
751     0x05, 0xb5, 0xd8, 0x84, 0x0f, 0x62, 0x85, 0xaa, 0xec, 0x77, 0x64, 0xfd, 0x80, 0x7c, 0x41,
752     0x00, 0x88, 0xa3, 0x79, 0x7d, 0x4f, 0x6f, 0xe3, 0x76, 0xf4, 0xb5, 0x97, 0xb7, 0xeb, 0x67,
753     0x28, 0xba, 0x07, 0x1a, 0x59, 0x32, 0xc1, 0x53, 0xd9, 0x05, 0x6b, 0x63, 0x93, 0xce, 0xa1,
754     0xd9, 0x7a, 0xb2, 0xff, 0x1c, 0x12, 0x0a, 0x9a, 0xe5, 0x51, 0x1e, 0xba, 0xfc, 0x95, 0x2e,
755     0x28, 0xa9, 0xfc, 0x4c, 0xed, 0x7b, 0x05, 0xca, 0x67, 0xe0, 0x2d, 0xd7, 0x54, 0xb3, 0x05,
756     0x1c, 0x23, 0x2b, 0x35, 0x2e, 0x19, 0x48, 0x59, 0x0e, 0x58, 0xa8, 0x01, 0x56, 0xfb, 0x78,
757     0x90, 0xba, 0x08, 0x77, 0x94, 0x45, 0x05, 0x13, 0xc7, 0x6b, 0x96, 0xd2, 0xa3, 0xa6, 0x01,
758     0x9f, 0x34
759 };
760 static const unsigned char dsa_priv[] = {
761     0x2f, 0x68, 0x44, 0xcb, 0xfb, 0x6b, 0xcb, 0x8d, 0x02, 0x49, 0x7c, 0xee, 0xd2, 0xa6, 0xd3,
762     0x43, 0xb8, 0xa4, 0x09, 0xb7, 0xc1, 0xd4, 0x4b, 0xc3, 0x66, 0xa7, 0xe0, 0x21
763 };
764 static const unsigned char dsa_pub[] = {
765     0x16, 0x1a, 0xb4, 0x6d, 0x9f, 0x16, 0x6c, 0xcc, 0x91, 0x66, 0xfe, 0x30, 0xeb, 0x8e, 0x44,
766     0xba, 0x2b, 0x7a, 0xc9, 0xa8, 0x95, 0xf2, 0xa6, 0x38, 0xd8, 0xaf, 0x3e, 0x91, 0x68, 0xe8,
767     0x52, 0xf3, 0x97, 0x37, 0x70, 0xf2, 0x47, 0xa3, 0xf4, 0x62, 0x26, 0xf5, 0x3b, 0x71, 0x52,
768     0x50, 0x15, 0x9c, 0x6d, 0xa6, 0x6d, 0x92, 0x4c, 0x48, 0x76, 0x31, 0x54, 0x48, 0xa5, 0x99,
769     0x7a, 0xd4, 0x61, 0xf7, 0x21, 0x44, 0xe7, 0xd8, 0x82, 0xc3, 0x50, 0xd3, 0xd9, 0xd4, 0x66,
770     0x20, 0xab, 0x70, 0x4c, 0x97, 0x9b, 0x8d, 0xac, 0x1f, 0x78, 0x27, 0x1e, 0x47, 0xf8, 0x3b,
771     0xd1, 0x55, 0x73, 0xf3, 0xb4, 0x8e, 0x6d, 0x45, 0x40, 0x54, 0xc6, 0xd8, 0x95, 0x15, 0x27,
772     0xb7, 0x5f, 0x65, 0xaa, 0xcb, 0x24, 0xc9, 0x49, 0x87, 0x32, 0xad, 0xcb, 0xf8, 0x35, 0x63,
773     0x56, 0x72, 0x7c, 0x4e, 0x6c, 0xad, 0x5f, 0x26, 0x8c, 0xd2, 0x80, 0x41, 0xaf, 0x88, 0x23,
774     0x20, 0x03, 0xa4, 0xd5, 0x3c, 0x53, 0x54, 0xb0, 0x3d, 0xed, 0x0e, 0x9e, 0x53, 0x0a, 0x63,
775     0x5f, 0xfd, 0x28, 0x57, 0x09, 0x07, 0x73, 0xf4, 0x0c, 0xd4, 0x71, 0x5d, 0x6b, 0xa0, 0xd7,
776     0x86, 0x99, 0x29, 0x9b, 0xca, 0xfb, 0xcc, 0xd6, 0x2f, 0xfe, 0xbe, 0x94, 0xef, 0x1a, 0x0e,
777     0x55, 0x84, 0xa7, 0xaf, 0x7b, 0xfa, 0xed, 0x77, 0x61, 0x28, 0x22, 0xee, 0x6b, 0x11, 0xdd,
778     0xb0, 0x17, 0x1e, 0x06, 0xe4, 0x29, 0x4c, 0xc2, 0x3f, 0xd6, 0x75, 0xb6, 0x08, 0x04, 0x55,
779     0x13, 0x48, 0x4f, 0x44, 0xea, 0x8d, 0xaf, 0xcb, 0xac, 0x22, 0xc4, 0x6a, 0xb3, 0x86, 0xe5,
780     0x47, 0xa9, 0xb5, 0x72, 0x17, 0x23, 0x11, 0x81, 0x7f, 0x00, 0x00, 0x67, 0x5c, 0xf4, 0x58,
781     0xcc, 0xe2, 0x46, 0xce, 0xf5, 0x6d, 0xd8, 0x18, 0x91, 0xc4, 0x20, 0xbf, 0x07, 0x48, 0x45,
782     0xfd
783 };
784
785 static int do_check_params(OSSL_PARAM key_params[], int expected)
786 {
787     EVP_PKEY_CTX *gen_ctx = NULL, *check_ctx = NULL;
788     EVP_PKEY *pkey = NULL;
789     int ret;
790
791     ret = TEST_ptr(gen_ctx = EVP_PKEY_CTX_new_from_name(mainctx, "DSA", NULL))
792           && TEST_int_eq(EVP_PKEY_fromdata_init(gen_ctx), 1)
793           && TEST_int_eq(EVP_PKEY_fromdata(gen_ctx, &pkey,
794                                            EVP_PKEY_KEYPAIR, key_params), 1)
795           && TEST_ptr(check_ctx = EVP_PKEY_CTX_new_from_pkey(mainctx, pkey,
796                                                         NULL))
797           && TEST_int_eq(EVP_PKEY_param_check(check_ctx), expected);
798     EVP_PKEY_CTX_free(check_ctx);
799     EVP_PKEY_CTX_free(gen_ctx);
800     EVP_PKEY_free(pkey);
801     return ret;
802 }
803
804 static int do_check_bn(OSSL_PARAM params[], const char *key,
805                        const unsigned char *expected, size_t expected_len)
806 {
807     OSSL_PARAM *p;
808     BIGNUM *bn = NULL;
809     unsigned char buffer[256 + 1];
810     int ret, len;
811
812     ret = TEST_ptr(p = OSSL_PARAM_locate(params, key))
813           && TEST_true(OSSL_PARAM_get_BN(p, &bn))
814           && TEST_int_gt(len = BN_bn2binpad(bn, buffer, expected_len), 0)
815           && TEST_mem_eq(expected, expected_len, buffer, len);
816     BN_free(bn);
817     return ret;
818 }
819
820 static int do_check_int(OSSL_PARAM params[], const char *key, int expected)
821 {
822     OSSL_PARAM *p;
823     int val = 0;
824
825     return TEST_ptr(p = OSSL_PARAM_locate(params, key))
826            && TEST_true(OSSL_PARAM_get_int(p, &val))
827            && TEST_int_eq(val, expected);
828 }
829
830 static int test_dsa_tofrom_data_select(void)
831 {
832     int ret;
833     EVP_PKEY *key = NULL;
834     const unsigned char *pkeydata = dsa_key;
835
836     ret = TEST_ptr(key = d2i_AutoPrivateKey_ex(NULL, &pkeydata, sizeof(dsa_key),
837                                                mainctx, NULL))
838           && TEST_true(do_pkey_tofrom_data_select(key, "DSA"));
839
840     EVP_PKEY_free(key);
841     return ret;
842 }
843
844 static int test_dsa_todata(void)
845 {
846     EVP_PKEY *pkey = NULL;
847     OSSL_PARAM *to_params = NULL, *all_params = NULL;
848     OSSL_PARAM gen_params[4];
849     int ret = 0;
850     const unsigned char *pkeydata = dsa_key;
851
852     unsigned char dsa_seed[] = {
853         0xbc, 0x8a, 0x81, 0x64, 0x9e, 0x9d, 0x63, 0xa7, 0xa3, 0x5d, 0x87, 0xdd,
854         0x32, 0xf3, 0xc1, 0x9f, 0x18, 0x22, 0xeb, 0x73, 0x63, 0xad, 0x5e, 0x7b,
855         0x90, 0xc1, 0xe3, 0xe0
856     };
857     int dsa_pcounter = 319;
858     int dsa_gindex = 5;
859
860     gen_params[0] = OSSL_PARAM_construct_octet_string(OSSL_PKEY_PARAM_FFC_SEED,
861                                                       (void*)dsa_seed,
862                                                       sizeof(dsa_seed));
863     gen_params[1] = OSSL_PARAM_construct_int(OSSL_PKEY_PARAM_FFC_GINDEX,
864                                              &dsa_gindex);
865     gen_params[2] = OSSL_PARAM_construct_int(OSSL_PKEY_PARAM_FFC_PCOUNTER,
866                                              &dsa_pcounter);
867     gen_params[3] = OSSL_PARAM_construct_end();
868
869     if (!TEST_ptr(pkey = d2i_AutoPrivateKey_ex(NULL, &pkeydata, sizeof(dsa_key),
870                                                mainctx, NULL))
871         || !TEST_int_eq(EVP_PKEY_todata(pkey, EVP_PKEY_KEYPAIR, &to_params), 1)
872         || !do_check_bn(to_params, OSSL_PKEY_PARAM_FFC_P, dsa_p, sizeof(dsa_p))
873         || !do_check_bn(to_params, OSSL_PKEY_PARAM_FFC_Q, dsa_q, sizeof(dsa_q))
874         || !do_check_bn(to_params, OSSL_PKEY_PARAM_FFC_G, dsa_g, sizeof(dsa_g))
875         || !do_check_bn(to_params, OSSL_PKEY_PARAM_PUB_KEY, dsa_pub,
876                         sizeof(dsa_pub))
877         || !do_check_bn(to_params, OSSL_PKEY_PARAM_PRIV_KEY, dsa_priv,
878                         sizeof(dsa_priv))
879         || !do_check_int(to_params, OSSL_PKEY_PARAM_FFC_GINDEX, -1)
880         || !do_check_int(to_params, OSSL_PKEY_PARAM_FFC_PCOUNTER, -1)
881         || !do_check_int(to_params, OSSL_PKEY_PARAM_FFC_H, 0)
882         || !do_check_int(to_params, OSSL_PKEY_PARAM_FFC_VALIDATE_PQ, 1)
883         || !do_check_int(to_params, OSSL_PKEY_PARAM_FFC_VALIDATE_G, 1)
884         || !do_check_int(to_params, OSSL_PKEY_PARAM_FFC_VALIDATE_LEGACY, 0)
885         || !TEST_ptr_null(OSSL_PARAM_locate(to_params, OSSL_PKEY_PARAM_FFC_SEED)))
886         goto err;
887
888     if (!do_fromdata_key_is_equal(to_params, pkey, "DSA"))
889         goto err;
890
891     if (!TEST_ptr(all_params = OSSL_PARAM_merge(to_params, gen_params))
892         || !do_check_params(all_params, 1))
893         goto err;
894     gen_params[1] = OSSL_PARAM_construct_int(OSSL_PKEY_PARAM_FFC_GINDEX,
895                                              &dsa_gindex);
896     gen_params[2] = OSSL_PARAM_construct_int(OSSL_PKEY_PARAM_FFC_PCOUNTER,
897                                              &dsa_pcounter);
898     /*
899      * Check that modifying the shallow copy values used in OSSL_PARAM_merge()
900      * results in an invalid key. This also verifies that the fips186-4
901      * validation code is running.
902      */
903     dsa_gindex++;
904     if (!do_check_params(all_params, 0))
905         goto err;
906     dsa_gindex--;
907     dsa_pcounter++;
908     if (!do_check_params(all_params, 0))
909         goto err;
910     dsa_pcounter--;
911     dsa_seed[0] = 0xb0;
912     if (!do_check_params(all_params, 0))
913         goto err;
914
915     ret = 1;
916 err:
917     EVP_PKEY_free(pkey);
918     OSSL_PARAM_free(all_params);
919     OSSL_PARAM_free(to_params);
920     return ret;
921 }
922 #endif /* OPENSSL_NO_DSA */
923
924 static int test_pkey_todata_null(void)
925 {
926     OSSL_PARAM *params = NULL;
927     EVP_PKEY *pkey = NULL;
928     int ret = 0;
929     const unsigned char *pdata = keydata[0].kder;
930
931     ret = TEST_ptr(pkey = d2i_AutoPrivateKey_ex(NULL, &pdata, keydata[0].size,
932                                                 mainctx, NULL))
933           && TEST_int_eq(EVP_PKEY_todata(NULL, EVP_PKEY_KEYPAIR, &params), 0)
934           && TEST_int_eq(EVP_PKEY_todata(pkey, EVP_PKEY_KEYPAIR, NULL), 0);
935     EVP_PKEY_free(pkey);
936     return ret;
937 }
938
939 static OSSL_CALLBACK test_pkey_export_cb;
940
941 static int test_pkey_export_cb(const OSSL_PARAM params[], void *arg)
942 {
943     if (arg == NULL)
944         return 0;
945     return do_fromdata_key_is_equal(params, (EVP_PKEY *)arg, "RSA");
946 }
947
948 static int test_pkey_export_null(void)
949 {
950     EVP_PKEY *pkey = NULL;
951     int ret = 0;
952     const unsigned char *pdata = keydata[0].kder;
953
954     ret = TEST_ptr(pkey = d2i_AutoPrivateKey_ex(NULL, &pdata, keydata[0].size,
955                                                 mainctx, NULL))
956           && TEST_int_eq(EVP_PKEY_export(NULL, EVP_PKEY_KEYPAIR,
957                                          test_pkey_export_cb, NULL), 0)
958           && TEST_int_eq(EVP_PKEY_export(pkey, EVP_PKEY_KEYPAIR, NULL, NULL), 0);
959     EVP_PKEY_free(pkey);
960     return ret;
961 }
962
963 static int test_pkey_export(void)
964 {
965     EVP_PKEY *pkey = NULL;
966 #ifndef OPENSSL_NO_DEPRECATED_3_0
967     RSA *rsa = NULL;
968 #endif
969     int ret = 1;
970     const unsigned char *pdata = keydata[0].kder;
971     int pdata_len = keydata[0].size;
972
973     if (!TEST_ptr(pkey = d2i_AutoPrivateKey_ex(NULL, &pdata, pdata_len,
974                                                mainctx, NULL))
975         || !TEST_true(EVP_PKEY_export(pkey, EVP_PKEY_KEYPAIR,
976                                        test_pkey_export_cb, pkey))
977         || !TEST_false(EVP_PKEY_export(pkey, EVP_PKEY_KEYPAIR,
978                                        test_pkey_export_cb, NULL)))
979         ret = 0;
980     EVP_PKEY_free(pkey);
981
982 #ifndef OPENSSL_NO_DEPRECATED_3_0
983     /* Now, try with a legacy key */
984     pdata = keydata[0].kder;
985     pdata_len = keydata[0].size;
986     if (!TEST_ptr(rsa = d2i_RSAPrivateKey(NULL, &pdata, pdata_len))
987         || !TEST_ptr(pkey = EVP_PKEY_new())
988         || !TEST_true(EVP_PKEY_assign_RSA(pkey, rsa))
989         || !TEST_true(EVP_PKEY_export(pkey, EVP_PKEY_KEYPAIR,
990                                       test_pkey_export_cb, pkey))
991         || !TEST_false(EVP_PKEY_export(pkey, EVP_PKEY_KEYPAIR,
992                                        test_pkey_export_cb, NULL)))
993         ret = 0;
994     EVP_PKEY_free(pkey);
995 #endif
996     return ret;
997 }
998
999 static int test_rsa_pss_sign(void)
1000 {
1001     EVP_PKEY *pkey = NULL;
1002     EVP_PKEY_CTX *pctx = NULL;
1003     int ret = 0;
1004     const unsigned char *pdata = keydata[0].kder;
1005     const char *mdname = "SHA2-256";
1006     OSSL_PARAM sig_params[3];
1007     unsigned char mdbuf[256 / 8] = { 0 };
1008     int padding = RSA_PKCS1_PSS_PADDING;
1009     unsigned char *sig = NULL;
1010     size_t sig_len = 0;
1011
1012     sig_params[0] = OSSL_PARAM_construct_int(OSSL_PKEY_PARAM_PAD_MODE,
1013                                              &padding);
1014     sig_params[1] = OSSL_PARAM_construct_utf8_string(OSSL_SIGNATURE_PARAM_DIGEST,
1015                                                      (char *)mdname, 0);
1016     sig_params[2] = OSSL_PARAM_construct_end();
1017
1018     ret = TEST_ptr(pkey = d2i_AutoPrivateKey_ex(NULL, &pdata, keydata[0].size,
1019                                                 mainctx, NULL))
1020           && TEST_ptr(pctx = EVP_PKEY_CTX_new_from_pkey(mainctx, pkey, NULL))
1021           && TEST_int_gt(EVP_PKEY_sign_init_ex(pctx, sig_params), 0)
1022           && TEST_int_gt(EVP_PKEY_sign(pctx, NULL, &sig_len, mdbuf,
1023                                        sizeof(mdbuf)), 0)
1024           && TEST_int_gt(sig_len, 0)
1025           && TEST_ptr(sig = OPENSSL_malloc(sig_len))
1026           && TEST_int_gt(EVP_PKEY_sign(pctx, sig, &sig_len, mdbuf,
1027                                        sizeof(mdbuf)), 0);
1028
1029     EVP_PKEY_CTX_free(pctx);
1030     OPENSSL_free(sig);
1031     EVP_PKEY_free(pkey);
1032
1033     return ret;
1034 }
1035
1036 static int test_evp_md_ctx_dup(void)
1037 {
1038     EVP_MD_CTX *mdctx;
1039     EVP_MD_CTX *copyctx = NULL;
1040     int ret;
1041
1042     /* test copying freshly initialized context */
1043     ret = TEST_ptr(mdctx = EVP_MD_CTX_new())
1044           && TEST_ptr(copyctx = EVP_MD_CTX_dup(mdctx));
1045
1046     EVP_MD_CTX_free(mdctx);
1047     EVP_MD_CTX_free(copyctx);
1048     return ret;
1049 }
1050
1051 static int test_evp_md_ctx_copy(void)
1052 {
1053     EVP_MD_CTX *mdctx = NULL;
1054     EVP_MD_CTX *copyctx = NULL;
1055     int ret;
1056
1057     /* test copying freshly initialized context */
1058     ret = TEST_ptr(mdctx = EVP_MD_CTX_new())
1059           && TEST_ptr(copyctx = EVP_MD_CTX_new())
1060           && TEST_true(EVP_MD_CTX_copy_ex(copyctx, mdctx));
1061
1062     EVP_MD_CTX_free(mdctx);
1063     EVP_MD_CTX_free(copyctx);
1064     return ret;
1065 }
1066
1067 int setup_tests(void)
1068 {
1069     if (!test_get_libctx(&mainctx, &nullprov, NULL, NULL, NULL)) {
1070         OSSL_LIB_CTX_free(mainctx);
1071         mainctx = NULL;
1072         return 0;
1073     }
1074
1075     ADD_TEST(test_alternative_default);
1076     ADD_ALL_TESTS(test_d2i_AutoPrivateKey_ex, OSSL_NELEM(keydata));
1077 #ifndef OPENSSL_NO_EC
1078     ADD_ALL_TESTS(test_d2i_PrivateKey_ex, 2);
1079     ADD_TEST(test_ec_tofrom_data_select);
1080     ADD_TEST(test_ecx_tofrom_data_select);
1081 #else
1082     ADD_ALL_TESTS(test_d2i_PrivateKey_ex, 1);
1083 #endif
1084 #ifndef OPENSSL_NO_DSA
1085     ADD_TEST(test_dsa_todata);
1086     ADD_TEST(test_dsa_tofrom_data_select);
1087 #endif
1088 #ifndef OPENSSL_NO_DH
1089     ADD_TEST(test_dh_tofrom_data_select);
1090 #endif
1091     ADD_TEST(test_rsa_tofrom_data_select);
1092
1093     ADD_TEST(test_pkey_todata_null);
1094     ADD_TEST(test_pkey_export_null);
1095     ADD_TEST(test_pkey_export);
1096 #ifndef OPENSSL_NO_DES
1097     ADD_TEST(test_pkcs8key_nid_bio);
1098 #endif
1099     ADD_ALL_TESTS(test_PEM_read_bio_negative, OSSL_NELEM(keydata));
1100     ADD_TEST(test_rsa_pss_sign);
1101     ADD_TEST(test_evp_md_ctx_dup);
1102     ADD_TEST(test_evp_md_ctx_copy);
1103     ADD_ALL_TESTS(test_provider_unload_effective, 2);
1104     return 1;
1105 }
1106
1107 void cleanup_tests(void)
1108 {
1109     OSSL_LIB_CTX_free(mainctx);
1110     OSSL_PROVIDER_unload(nullprov);
1111 }