1 /*
2 * Copyright 2015-2026 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, 0x82, 0x02, 0x5c, 0x02, 0x01, 0x00, 0x02, 0x81, 0x81,
48 0x00, 0xf8, 0xb8, 0x6c, 0x83, 0xb4, 0xbc, 0xd9, 0xa8, 0x57,
49 0xc0, 0xa5, 0xb4, 0x59, 0x76, 0x8c, 0x54, 0x1d, 0x79, 0xeb,
50 0x22, 0x52, 0x04, 0x7e, 0xd3, 0x37, 0xeb, 0x41, 0xfd, 0x83,
51 0xf9, 0xf0, 0xa6, 0x85, 0x15, 0x34, 0x75, 0x71, 0x5a, 0x84,
52 0xa8, 0x3c, 0xd2, 0xef, 0x5a, 0x4e, 0xd3, 0xde, 0x97, 0x8a,
53 0xdd, 0xff, 0xbb, 0xcf, 0x0a, 0xaa, 0x86, 0x92, 0xbe, 0xb8,
54 0x50, 0xe4, 0xcd, 0x6f, 0x80, 0x33, 0x30, 0x76, 0x13, 0x8f,
55 0xca, 0x7b, 0xdc, 0xec, 0x5a, 0xca, 0x63, 0xc7, 0x03, 0x25,
56 0xef, 0xa8, 0x8a, 0x83, 0x58, 0x76, 0x20, 0xfa, 0x16, 0x77,
57 0xd7, 0x79, 0x92, 0x63, 0x01, 0x48, 0x1a, 0xd8, 0x7b, 0x67,
58 0xf1, 0x52, 0x55, 0x49, 0x4e, 0xd6, 0x6e, 0x4a, 0x5c, 0xd7,
59 0x7a, 0x37, 0x36, 0x0c, 0xde, 0xdd, 0x8f, 0x44, 0xe8, 0xc2,
60 0xa7, 0x2c, 0x2b, 0xb5, 0xaf, 0x64, 0x4b, 0x61, 0x07, 0x02,
61 0x03, 0x01, 0x00, 0x01, 0x02, 0x81, 0x80, 0x74, 0x88, 0x64,
62 0x3f, 0x69, 0x45, 0x3a, 0x6d, 0xc7, 0x7f, 0xb9, 0xa3, 0xc0,
63 0x6e, 0xec, 0xdc, 0xd4, 0x5a, 0xb5, 0x32, 0x85, 0x5f, 0x19,
64 0xd4, 0xf8, 0xd4, 0x3f, 0x3c, 0xfa, 0xc2, 0xf6, 0x5f, 0xee,
65 0xe6, 0xba, 0x87, 0x74, 0x2e, 0xc7, 0x0c, 0xd4, 0x42, 0xb8,
66 0x66, 0x85, 0x9c, 0x7b, 0x24, 0x61, 0xaa, 0x16, 0x11, 0xf6,
67 0xb5, 0xb6, 0xa4, 0x0a, 0xc9, 0x55, 0x2e, 0x81, 0xa5, 0x47,
68 0x61, 0xcb, 0x25, 0x8f, 0xc2, 0x15, 0x7b, 0x0e, 0x7c, 0x36,
69 0x9f, 0x3a, 0xda, 0x58, 0x86, 0x1c, 0x5b, 0x83, 0x79, 0xe6,
70 0x2b, 0xcc, 0xe6, 0xfa, 0x2c, 0x61, 0xf2, 0x78, 0x80, 0x1b,
71 0xe2, 0xf3, 0x9d, 0x39, 0x2b, 0x65, 0x57, 0x91, 0x3d, 0x71,
72 0x99, 0x73, 0xa5, 0xc2, 0x79, 0x20, 0x8c, 0x07, 0x4f, 0xe5,
73 0xb4, 0x60, 0x1f, 0x99, 0xa2, 0xb1, 0x4f, 0x0c, 0xef, 0xbc,
74 0x59, 0x53, 0x00, 0x7d, 0xb1, 0x02, 0x41, 0x00, 0xfc, 0x7e,
75 0x23, 0x65, 0x70, 0xf8, 0xce, 0xd3, 0x40, 0x41, 0x80, 0x6a,
76 0x1d, 0x01, 0xd6, 0x01, 0xff, 0xb6, 0x1b, 0x3d, 0x3d, 0x59,
77 0x09, 0x33, 0x79, 0xc0, 0x4f, 0xde, 0x96, 0x27, 0x4b, 0x18,
78 0xc6, 0xd9, 0x78, 0xf1, 0xf4, 0x35, 0x46, 0xe9, 0x7c, 0x42,
79 0x7a, 0x5d, 0x9f, 0xef, 0x54, 0xb8, 0xf7, 0x9f, 0xc4, 0x33,
80 0x6c, 0xf3, 0x8c, 0x32, 0x46, 0x87, 0x67, 0x30, 0x7b, 0xa7,
81 0xac, 0xe3, 0x02, 0x41, 0x00, 0xfc, 0x2c, 0xdf, 0x0c, 0x0d,
82 0x88, 0xf5, 0xb1, 0x92, 0xa8, 0x93, 0x47, 0x63, 0x55, 0xf5,
83 0xca, 0x58, 0x43, 0xba, 0x1c, 0xe5, 0x9e, 0xb6, 0x95, 0x05,
84 0xcd, 0xb5, 0x82, 0xdf, 0xeb, 0x04, 0x53, 0x9d, 0xbd, 0xc2,
85 0x38, 0x16, 0xb3, 0x62, 0xdd, 0xa1, 0x46, 0xdb, 0x6d, 0x97,
86 0x93, 0x9f, 0x8a, 0xc3, 0x9b, 0x64, 0x7e, 0x42, 0xe3, 0x32,
87 0x57, 0x19, 0x1b, 0xd5, 0x6e, 0x85, 0xfa, 0xb8, 0x8d, 0x02,
88 0x41, 0x00, 0xbc, 0x3d, 0xde, 0x6d, 0xd6, 0x97, 0xe8, 0xba,
89 0x9e, 0x81, 0x37, 0x17, 0xe5, 0xa0, 0x64, 0xc9, 0x00, 0xb7,
90 0xe7, 0xfe, 0xf4, 0x29, 0xd9, 0x2e, 0x43, 0x6b, 0x19, 0x20,
91 0xbd, 0x99, 0x75, 0xe7, 0x76, 0xf8, 0xd3, 0xae, 0xaf, 0x7e,
92 0xb8, 0xeb, 0x81, 0xf4, 0x9d, 0xfe, 0x07, 0x2b, 0x0b, 0x63,
93 0x0b, 0x5a, 0x55, 0x90, 0x71, 0x7d, 0xf1, 0xdb, 0xd9, 0xb1,
94 0x41, 0x41, 0x68, 0x2f, 0x4e, 0x39, 0x02, 0x40, 0x5a, 0x34,
95 0x66, 0xd8, 0xf5, 0xe2, 0x7f, 0x18, 0xb5, 0x00, 0x6e, 0x26,
96 0x84, 0x27, 0x14, 0x93, 0xfb, 0xfc, 0xc6, 0x0f, 0x5e, 0x27,
97 0xe6, 0xe1, 0xe9, 0xc0, 0x8a, 0xe4, 0x34, 0xda, 0xe9, 0xa2,
98 0x4b, 0x73, 0xbc, 0x8c, 0xb9, 0xba, 0x13, 0x6c, 0x7a, 0x2b,
99 0x51, 0x84, 0xa3, 0x4a, 0xe0, 0x30, 0x10, 0x06, 0x7e, 0xed,
100 0x17, 0x5a, 0x14, 0x00, 0xc9, 0xef, 0x85, 0xea, 0x52, 0x2c,
101 0xbc, 0x65, 0x02, 0x40, 0x51, 0xe3, 0xf2, 0x83, 0x19, 0x9b,
102 0xc4, 0x1e, 0x2f, 0x50, 0x3d, 0xdf, 0x5a, 0xa2, 0x18, 0xca,
103 0x5f, 0x2e, 0x49, 0xaf, 0x6f, 0xcc, 0xfa, 0x65, 0x77, 0x94,
104 0xb5, 0xa1, 0x0a, 0xa9, 0xd1, 0x8a, 0x39, 0x37, 0xf4, 0x0b,
105 0xa0, 0xd7, 0x82, 0x27, 0x5e, 0xae, 0x17, 0x17, 0xa1, 0x1e,
106 0x54, 0x34, 0xbf, 0x6e, 0xc4, 0x8e, 0x99, 0x5d, 0x08, 0xf1,
107 0x2d, 0x86, 0x9d, 0xa5, 0x20, 0x1b, 0xe5, 0xdf
108 };
109
110 /*
111 * kExampleRSAKeyPKCS8 is kExampleRSAKeyDER encoded in a PKCS #8
112 * PrivateKeyInfo.
113 */
114 static const unsigned char kExampleRSAKeyPKCS8[] = {
115 0x30, 0x82, 0x02, 0x76, 0x02, 0x01, 0x00, 0x30, 0x0d, 0x06,
116 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01,
117 0x05, 0x00, 0x04, 0x82, 0x02, 0x60, 0x30, 0x82, 0x02, 0x5c,
118 0x02, 0x01, 0x00, 0x02, 0x81, 0x81, 0x00, 0xf8, 0xb8, 0x6c,
119 0x83, 0xb4, 0xbc, 0xd9, 0xa8, 0x57, 0xc0, 0xa5, 0xb4, 0x59,
120 0x76, 0x8c, 0x54, 0x1d, 0x79, 0xeb, 0x22, 0x52, 0x04, 0x7e,
121 0xd3, 0x37, 0xeb, 0x41, 0xfd, 0x83, 0xf9, 0xf0, 0xa6, 0x85,
122 0x15, 0x34, 0x75, 0x71, 0x5a, 0x84, 0xa8, 0x3c, 0xd2, 0xef,
123 0x5a, 0x4e, 0xd3, 0xde, 0x97, 0x8a, 0xdd, 0xff, 0xbb, 0xcf,
124 0x0a, 0xaa, 0x86, 0x92, 0xbe, 0xb8, 0x50, 0xe4, 0xcd, 0x6f,
125 0x80, 0x33, 0x30, 0x76, 0x13, 0x8f, 0xca, 0x7b, 0xdc, 0xec,
126 0x5a, 0xca, 0x63, 0xc7, 0x03, 0x25, 0xef, 0xa8, 0x8a, 0x83,
127 0x58, 0x76, 0x20, 0xfa, 0x16, 0x77, 0xd7, 0x79, 0x92, 0x63,
128 0x01, 0x48, 0x1a, 0xd8, 0x7b, 0x67, 0xf1, 0x52, 0x55, 0x49,
129 0x4e, 0xd6, 0x6e, 0x4a, 0x5c, 0xd7, 0x7a, 0x37, 0x36, 0x0c,
130 0xde, 0xdd, 0x8f, 0x44, 0xe8, 0xc2, 0xa7, 0x2c, 0x2b, 0xb5,
131 0xaf, 0x64, 0x4b, 0x61, 0x07, 0x02, 0x03, 0x01, 0x00, 0x01,
132 0x02, 0x81, 0x80, 0x74, 0x88, 0x64, 0x3f, 0x69, 0x45, 0x3a,
133 0x6d, 0xc7, 0x7f, 0xb9, 0xa3, 0xc0, 0x6e, 0xec, 0xdc, 0xd4,
134 0x5a, 0xb5, 0x32, 0x85, 0x5f, 0x19, 0xd4, 0xf8, 0xd4, 0x3f,
135 0x3c, 0xfa, 0xc2, 0xf6, 0x5f, 0xee, 0xe6, 0xba, 0x87, 0x74,
136 0x2e, 0xc7, 0x0c, 0xd4, 0x42, 0xb8, 0x66, 0x85, 0x9c, 0x7b,
137 0x24, 0x61, 0xaa, 0x16, 0x11, 0xf6, 0xb5, 0xb6, 0xa4, 0x0a,
138 0xc9, 0x55, 0x2e, 0x81, 0xa5, 0x47, 0x61, 0xcb, 0x25, 0x8f,
139 0xc2, 0x15, 0x7b, 0x0e, 0x7c, 0x36, 0x9f, 0x3a, 0xda, 0x58,
140 0x86, 0x1c, 0x5b, 0x83, 0x79, 0xe6, 0x2b, 0xcc, 0xe6, 0xfa,
141 0x2c, 0x61, 0xf2, 0x78, 0x80, 0x1b, 0xe2, 0xf3, 0x9d, 0x39,
142 0x2b, 0x65, 0x57, 0x91, 0x3d, 0x71, 0x99, 0x73, 0xa5, 0xc2,
143 0x79, 0x20, 0x8c, 0x07, 0x4f, 0xe5, 0xb4, 0x60, 0x1f, 0x99,
144 0xa2, 0xb1, 0x4f, 0x0c, 0xef, 0xbc, 0x59, 0x53, 0x00, 0x7d,
145 0xb1, 0x02, 0x41, 0x00, 0xfc, 0x7e, 0x23, 0x65, 0x70, 0xf8,
146 0xce, 0xd3, 0x40, 0x41, 0x80, 0x6a, 0x1d, 0x01, 0xd6, 0x01,
147 0xff, 0xb6, 0x1b, 0x3d, 0x3d, 0x59, 0x09, 0x33, 0x79, 0xc0,
148 0x4f, 0xde, 0x96, 0x27, 0x4b, 0x18, 0xc6, 0xd9, 0x78, 0xf1,
149 0xf4, 0x35, 0x46, 0xe9, 0x7c, 0x42, 0x7a, 0x5d, 0x9f, 0xef,
150 0x54, 0xb8, 0xf7, 0x9f, 0xc4, 0x33, 0x6c, 0xf3, 0x8c, 0x32,
151 0x46, 0x87, 0x67, 0x30, 0x7b, 0xa7, 0xac, 0xe3, 0x02, 0x41,
152 0x00, 0xfc, 0x2c, 0xdf, 0x0c, 0x0d, 0x88, 0xf5, 0xb1, 0x92,
153 0xa8, 0x93, 0x47, 0x63, 0x55, 0xf5, 0xca, 0x58, 0x43, 0xba,
154 0x1c, 0xe5, 0x9e, 0xb6, 0x95, 0x05, 0xcd, 0xb5, 0x82, 0xdf,
155 0xeb, 0x04, 0x53, 0x9d, 0xbd, 0xc2, 0x38, 0x16, 0xb3, 0x62,
156 0xdd, 0xa1, 0x46, 0xdb, 0x6d, 0x97, 0x93, 0x9f, 0x8a, 0xc3,
157 0x9b, 0x64, 0x7e, 0x42, 0xe3, 0x32, 0x57, 0x19, 0x1b, 0xd5,
158 0x6e, 0x85, 0xfa, 0xb8, 0x8d, 0x02, 0x41, 0x00, 0xbc, 0x3d,
159 0xde, 0x6d, 0xd6, 0x97, 0xe8, 0xba, 0x9e, 0x81, 0x37, 0x17,
160 0xe5, 0xa0, 0x64, 0xc9, 0x00, 0xb7, 0xe7, 0xfe, 0xf4, 0x29,
161 0xd9, 0x2e, 0x43, 0x6b, 0x19, 0x20, 0xbd, 0x99, 0x75, 0xe7,
162 0x76, 0xf8, 0xd3, 0xae, 0xaf, 0x7e, 0xb8, 0xeb, 0x81, 0xf4,
163 0x9d, 0xfe, 0x07, 0x2b, 0x0b, 0x63, 0x0b, 0x5a, 0x55, 0x90,
164 0x71, 0x7d, 0xf1, 0xdb, 0xd9, 0xb1, 0x41, 0x41, 0x68, 0x2f,
165 0x4e, 0x39, 0x02, 0x40, 0x5a, 0x34, 0x66, 0xd8, 0xf5, 0xe2,
166 0x7f, 0x18, 0xb5, 0x00, 0x6e, 0x26, 0x84, 0x27, 0x14, 0x93,
167 0xfb, 0xfc, 0xc6, 0x0f, 0x5e, 0x27, 0xe6, 0xe1, 0xe9, 0xc0,
168 0x8a, 0xe4, 0x34, 0xda, 0xe9, 0xa2, 0x4b, 0x73, 0xbc, 0x8c,
169 0xb9, 0xba, 0x13, 0x6c, 0x7a, 0x2b, 0x51, 0x84, 0xa3, 0x4a,
170 0xe0, 0x30, 0x10, 0x06, 0x7e, 0xed, 0x17, 0x5a, 0x14, 0x00,
171 0xc9, 0xef, 0x85, 0xea, 0x52, 0x2c, 0xbc, 0x65, 0x02, 0x40,
172 0x51, 0xe3, 0xf2, 0x83, 0x19, 0x9b, 0xc4, 0x1e, 0x2f, 0x50,
173 0x3d, 0xdf, 0x5a, 0xa2, 0x18, 0xca, 0x5f, 0x2e, 0x49, 0xaf,
174 0x6f, 0xcc, 0xfa, 0x65, 0x77, 0x94, 0xb5, 0xa1, 0x0a, 0xa9,
175 0xd1, 0x8a, 0x39, 0x37, 0xf4, 0x0b, 0xa0, 0xd7, 0x82, 0x27,
176 0x5e, 0xae, 0x17, 0x17, 0xa1, 0x1e, 0x54, 0x34, 0xbf, 0x6e,
177 0xc4, 0x8e, 0x99, 0x5d, 0x08, 0xf1, 0x2d, 0x86, 0x9d, 0xa5,
178 0x20, 0x1b, 0xe5, 0xdf
179 };
180
181 /*
182 * An RSA-PSS 2048-bit private key in ASN.1 DER form. Used by the
183 * table-driven checks instead of keygen to save CI cycles (RSA keygen
184 * at this size is slow). Generated with:
185 * > openssl genpkey -quiet -algorithm rsa-pss -outform DER > rsa-pss.der
186 * > xxd -i < rsa-pss.der
187 * Of course, never use this key for anything but tests.
188 */
189 static const unsigned char kExampleRSAPSSKeyDER[] = {
190 0x30, 0x82, 0x04, 0xba, 0x02, 0x01, 0x00, 0x30, 0x0b, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86,
191 0xf7, 0x0d, 0x01, 0x01, 0x0a, 0x04, 0x82, 0x04, 0xa6, 0x30, 0x82, 0x04, 0xa2, 0x02, 0x01,
192 0x00, 0x02, 0x82, 0x01, 0x01, 0x00, 0xa3, 0x32, 0x24, 0xdb, 0x9b, 0xec, 0x8f, 0x18, 0xbe,
193 0xeb, 0xef, 0x9b, 0x0e, 0x06, 0x11, 0x78, 0x26, 0x66, 0x2b, 0x75, 0xff, 0x79, 0x50, 0xa9,
194 0x96, 0xb2, 0xdf, 0xe1, 0x71, 0x2f, 0x8f, 0x3f, 0x08, 0x16, 0x3f, 0x53, 0x5a, 0x2c, 0x7c,
195 0xbb, 0xb9, 0xa2, 0x54, 0x2c, 0x81, 0xaf, 0x58, 0xd1, 0xb4, 0xfe, 0xd6, 0x2a, 0x5c, 0x47,
196 0xfe, 0x20, 0xf0, 0xce, 0xdc, 0xfe, 0x08, 0x7f, 0x3a, 0x24, 0xcd, 0xa0, 0x87, 0xcb, 0x0e,
197 0xd7, 0xcf, 0xee, 0x26, 0x12, 0x1f, 0xdb, 0x8b, 0x0f, 0x12, 0x32, 0x05, 0x73, 0x8f, 0x60,
198 0xfa, 0x75, 0x49, 0xb5, 0x50, 0x2d, 0x45, 0x72, 0x8b, 0x1d, 0x7f, 0xd3, 0x47, 0x2c, 0x5b,
199 0x0e, 0x24, 0x28, 0xe2, 0xc6, 0x01, 0x74, 0x10, 0xbe, 0xe4, 0xc7, 0xd2, 0xe5, 0xf4, 0x90,
200 0x10, 0xc0, 0x28, 0x66, 0xed, 0xc4, 0x84, 0xac, 0xb2, 0x01, 0x8e, 0x8a, 0xd1, 0x86, 0x82,
201 0x77, 0x89, 0xd8, 0x64, 0xaa, 0xca, 0x1d, 0x27, 0x3e, 0xfd, 0x78, 0x4e, 0xd7, 0x8d, 0xc5,
202 0x25, 0xab, 0xb1, 0xfb, 0x94, 0x86, 0xa1, 0x3d, 0xc4, 0x8d, 0x62, 0x5e, 0x59, 0x13, 0x25,
203 0xb7, 0x77, 0x20, 0x7e, 0x0a, 0xf8, 0xd0, 0x4f, 0xf4, 0x9c, 0x6f, 0x19, 0x67, 0xe7, 0x6f,
204 0x62, 0xd9, 0xc9, 0xe5, 0x97, 0x90, 0xaa, 0xd5, 0x94, 0x98, 0x61, 0xb7, 0xf9, 0xfc, 0x86,
205 0x45, 0xd8, 0x6f, 0x37, 0x35, 0x51, 0x0d, 0x9f, 0xb0, 0x9f, 0xd5, 0x1b, 0x10, 0x87, 0xa2,
206 0xb9, 0xe0, 0x5e, 0xc9, 0x6b, 0x1b, 0x5d, 0xf3, 0x3a, 0x16, 0x4a, 0x79, 0x25, 0xcf, 0x95,
207 0xfc, 0xca, 0x8a, 0x7a, 0xac, 0x49, 0xd9, 0x36, 0xa5, 0x45, 0xc4, 0x9a, 0x01, 0x60, 0xed,
208 0x8b, 0xcf, 0x66, 0xf2, 0xfb, 0x98, 0x93, 0x69, 0x84, 0xf2, 0xa8, 0x02, 0x12, 0x4d, 0x7b,
209 0x48, 0xa1, 0xfb, 0x4d, 0x59, 0x96, 0x57, 0x02, 0x03, 0x01, 0x00, 0x01, 0x02, 0x82, 0x01,
210 0x00, 0x0f, 0x43, 0x65, 0xda, 0x1a, 0x53, 0xbe, 0x30, 0x78, 0xce, 0xd5, 0x4a, 0xa1, 0xbd,
211 0xa4, 0x8d, 0xa1, 0xbd, 0xc5, 0x8b, 0xd4, 0x38, 0x92, 0xf8, 0x41, 0x8d, 0x7a, 0x4d, 0xfa,
212 0x09, 0x92, 0x32, 0xb9, 0x7d, 0x67, 0x2f, 0xd3, 0x0b, 0xbd, 0x20, 0xcb, 0xa4, 0x68, 0x8d,
213 0xff, 0x15, 0x18, 0xd0, 0x08, 0x47, 0x1b, 0x5d, 0xbb, 0xe2, 0xc9, 0xdc, 0x67, 0xd9, 0x59,
214 0x26, 0xf3, 0x41, 0x7e, 0xfd, 0x29, 0x34, 0x3d, 0xef, 0x1a, 0x74, 0xc9, 0xd0, 0x42, 0xc4,
215 0x48, 0x31, 0x7e, 0xe8, 0x8a, 0x71, 0x9e, 0x3b, 0xe8, 0x29, 0x90, 0x53, 0x3e, 0xf1, 0x13,
216 0xce, 0x20, 0x24, 0x2d, 0xe9, 0xb4, 0xe6, 0x5c, 0x4e, 0x44, 0x37, 0xe6, 0xde, 0xe2, 0x12,
217 0xd5, 0xd8, 0xf6, 0xa5, 0xfb, 0xfc, 0xca, 0x77, 0x52, 0x6f, 0xd4, 0xf9, 0x3b, 0xee, 0x20,
218 0x87, 0xd4, 0x46, 0xd8, 0xa1, 0x37, 0xa5, 0x39, 0x22, 0x5e, 0xb6, 0x2c, 0xe8, 0x49, 0xf4,
219 0x78, 0x07, 0x23, 0xba, 0x19, 0x6e, 0x25, 0xb6, 0x7b, 0x2b, 0x9d, 0x7e, 0x19, 0x26, 0x95,
220 0xe7, 0x20, 0xb8, 0x21, 0xd1, 0xf5, 0xb9, 0x6d, 0xa6, 0x2a, 0xcb, 0x46, 0xe4, 0x62, 0x71,
221 0x63, 0xe1, 0xa0, 0x77, 0x1d, 0xf7, 0x10, 0xf6, 0x1e, 0x70, 0x27, 0x72, 0xf8, 0x68, 0x19,
222 0x6d, 0x84, 0xa1, 0x46, 0xcd, 0x52, 0x0f, 0x86, 0x77, 0x7a, 0x92, 0x5c, 0xc6, 0x39, 0xc2,
223 0x67, 0x12, 0xb0, 0x68, 0x6c, 0xe0, 0x19, 0xde, 0x61, 0xeb, 0x39, 0x1f, 0x14, 0xd0, 0xc7,
224 0xd6, 0x77, 0xc2, 0x83, 0x9e, 0xe2, 0xa3, 0x3a, 0x22, 0x02, 0x45, 0xf1, 0x10, 0x86, 0x77,
225 0x4a, 0x95, 0xf3, 0x75, 0x90, 0x22, 0x9b, 0xb2, 0xcd, 0xcd, 0x2f, 0x27, 0xff, 0xe7, 0xac,
226 0x9c, 0xdb, 0x85, 0x31, 0xeb, 0x2b, 0xfa, 0x8f, 0x9e, 0xbe, 0x92, 0x1a, 0xcd, 0xd3, 0x98,
227 0x1a, 0x75, 0x02, 0x81, 0x81, 0x00, 0xd5, 0x06, 0xa1, 0x08, 0x41, 0x02, 0xab, 0x3a, 0x00,
228 0xf5, 0x74, 0x5f, 0xb7, 0xf0, 0xa7, 0x9d, 0x0a, 0x26, 0x32, 0x7e, 0x48, 0xa4, 0xf7, 0xbe,
229 0xcf, 0xba, 0x27, 0x2a, 0x7d, 0x60, 0xd2, 0x16, 0x59, 0xc3, 0xf0, 0x78, 0x33, 0x3a, 0x36,
230 0x86, 0xb5, 0xd4, 0x29, 0x25, 0x23, 0x5a, 0xeb, 0x5a, 0x96, 0x82, 0xfc, 0x08, 0x08, 0x1c,
231 0xcc, 0xed, 0xb3, 0xf1, 0x27, 0xca, 0x1c, 0xa8, 0xe8, 0x36, 0x77, 0xa4, 0x57, 0x9a, 0x1d,
232 0xde, 0x2b, 0x3b, 0xc3, 0x41, 0x7c, 0x58, 0x33, 0x11, 0x3b, 0xee, 0x7a, 0xc9, 0xe9, 0x4b,
233 0xa2, 0x3b, 0xc3, 0x68, 0x19, 0x6a, 0x00, 0x7c, 0x59, 0x90, 0x77, 0x11, 0xed, 0x47, 0x4a,
234 0x5a, 0xfe, 0x3a, 0x29, 0x3a, 0xad, 0x80, 0xe9, 0xff, 0xce, 0x07, 0x3f, 0x80, 0x5f, 0x98,
235 0x14, 0x1a, 0xf0, 0x9c, 0xc7, 0x8d, 0x42, 0x06, 0xc4, 0xf8, 0x89, 0xe3, 0xe0, 0x23, 0x02,
236 0x81, 0x81, 0x00, 0xc4, 0x1e, 0x20, 0x31, 0xfa, 0xe7, 0x6a, 0xa5, 0x18, 0xbe, 0xce, 0x45,
237 0x15, 0x97, 0x67, 0x5f, 0xd0, 0xc6, 0x0f, 0xa2, 0x6c, 0x68, 0x5e, 0xe9, 0xca, 0x48, 0x8c,
238 0x46, 0x08, 0x06, 0xc1, 0xe9, 0xac, 0x8c, 0x01, 0x55, 0xc7, 0xf2, 0x02, 0x98, 0x2b, 0x8d,
239 0x4e, 0x24, 0x2d, 0x56, 0x7b, 0x40, 0x24, 0x28, 0xaa, 0x4a, 0xf7, 0x83, 0x88, 0x38, 0xa5,
240 0x03, 0x2b, 0xb3, 0x1c, 0x4a, 0x95, 0x70, 0xf6, 0x80, 0xcc, 0x21, 0xd0, 0xab, 0x8a, 0x10,
241 0xb2, 0xa6, 0x1a, 0x27, 0xb6, 0xae, 0x24, 0xed, 0xa9, 0xfe, 0xfc, 0x74, 0x7f, 0x97, 0x9b,
242 0xe1, 0xc2, 0x46, 0x48, 0xed, 0x5f, 0x2f, 0x9e, 0x54, 0x77, 0x1f, 0x61, 0x46, 0x24, 0x08,
243 0x7e, 0xb5, 0x63, 0xa1, 0xe2, 0x6f, 0x58, 0xc8, 0x76, 0x98, 0x8a, 0x9a, 0xa5, 0xb9, 0x15,
244 0xfb, 0xf4, 0xb8, 0xfd, 0x84, 0xb6, 0xbc, 0x5f, 0x59, 0xfa, 0x3d, 0x02, 0x81, 0x80, 0x2a,
245 0x97, 0x43, 0xb4, 0xf1, 0xc2, 0x85, 0xd7, 0x77, 0xff, 0x9b, 0x3f, 0xd3, 0xf7, 0xe2, 0x9b,
246 0x2c, 0x3f, 0x4c, 0xd3, 0x20, 0xd5, 0x12, 0xcf, 0x6c, 0x9a, 0xcc, 0x5f, 0xdb, 0x67, 0x73,
247 0x39, 0x92, 0xc2, 0xf1, 0x1e, 0x27, 0x27, 0xd9, 0x64, 0xff, 0x8f, 0xa6, 0x96, 0x4d, 0x46,
248 0x4d, 0x4d, 0xf4, 0x4f, 0xc5, 0xf2, 0x19, 0x25, 0x20, 0xf7, 0xd9, 0x85, 0x3a, 0xae, 0x57,
249 0x22, 0x92, 0x22, 0xe9, 0xb2, 0xca, 0xec, 0xfe, 0x51, 0xc0, 0x15, 0x17, 0xc9, 0xcd, 0x01,
250 0x99, 0x5c, 0x35, 0xbe, 0x4d, 0x80, 0x77, 0x0b, 0x6f, 0x29, 0x41, 0xbd, 0x5b, 0x6f, 0x6d,
251 0x1f, 0x6f, 0x8c, 0xc9, 0xb1, 0xa4, 0xde, 0x1f, 0x08, 0xa8, 0x51, 0x9a, 0x92, 0xa7, 0xc3,
252 0xe1, 0x85, 0x0a, 0x7d, 0x3a, 0x7e, 0x01, 0x1e, 0xd7, 0xce, 0x37, 0x13, 0x31, 0x76, 0x95,
253 0x9f, 0xe2, 0xdb, 0xca, 0x3d, 0xec, 0x3f, 0x02, 0x81, 0x80, 0x73, 0x99, 0x66, 0x54, 0xcf,
254 0x96, 0x34, 0xd0, 0x58, 0x03, 0xb0, 0x46, 0x1c, 0x72, 0x00, 0x27, 0x04, 0x42, 0x9a, 0xd8,
255 0x9c, 0x99, 0xf0, 0xc8, 0x51, 0xc9, 0xed, 0x95, 0x22, 0x0a, 0x09, 0xa7, 0x19, 0x63, 0xf5,
256 0x2d, 0x81, 0x0b, 0xef, 0xe7, 0x8e, 0x54, 0x5e, 0x69, 0x08, 0xa8, 0x5f, 0x41, 0xf1, 0x8e,
257 0x5e, 0xc2, 0x8d, 0x9e, 0xe4, 0x5e, 0xaf, 0x35, 0x6d, 0x3e, 0xc8, 0x40, 0x56, 0x52, 0x1d,
258 0x9a, 0xd1, 0xb8, 0x64, 0xed, 0x98, 0x16, 0x3b, 0x97, 0x98, 0xf7, 0x2b, 0xc0, 0xfe, 0x57,
259 0x70, 0xca, 0xb2, 0x38, 0x61, 0x35, 0x16, 0x57, 0x3e, 0x52, 0x7b, 0x8e, 0x1f, 0x7b, 0x4c,
260 0x12, 0x71, 0x9c, 0xf7, 0x93, 0x86, 0x8d, 0xd3, 0x52, 0x4c, 0x06, 0x12, 0x18, 0x9b, 0xf6,
261 0x9d, 0xa1, 0x50, 0xa1, 0xd3, 0x69, 0x83, 0xc1, 0xde, 0x00, 0x64, 0xbf, 0x23, 0x8f, 0x69,
262 0x8c, 0xf9, 0xe9, 0x02, 0x81, 0x80, 0x02, 0xce, 0x66, 0x6b, 0x32, 0x73, 0x62, 0x60, 0x27,
263 0x3e, 0x38, 0x1d, 0xd8, 0x70, 0xdb, 0x4e, 0x32, 0xf7, 0x7a, 0x7b, 0x22, 0x7b, 0x9b, 0xd7,
264 0x49, 0x4f, 0x5b, 0x9b, 0xaa, 0x2c, 0x5b, 0x99, 0x22, 0x1e, 0x6e, 0x7e, 0x19, 0x8a, 0xf1,
265 0x97, 0xfe, 0x3f, 0xcd, 0x9e, 0xa4, 0x46, 0xf0, 0x9a, 0x62, 0x0f, 0x1a, 0xcd, 0x77, 0xe1,
266 0x88, 0xe1, 0x9a, 0x22, 0x84, 0x1b, 0xbf, 0xf0, 0x71, 0xf4, 0x64, 0xfd, 0xd5, 0xaa, 0xb4,
267 0x28, 0xdc, 0xef, 0xd9, 0xec, 0x29, 0x11, 0xc4, 0x58, 0x0f, 0xeb, 0x3c, 0x41, 0x66, 0x4a,
268 0x98, 0x18, 0x26, 0xfe, 0x3e, 0x7e, 0xc3, 0x36, 0xfa, 0xbc, 0x64, 0x69, 0x1f, 0xce, 0x1c,
269 0xc1, 0xb5, 0xe9, 0xa8, 0x78, 0xf7, 0x6d, 0xb6, 0x0c, 0x58, 0x29, 0xe2, 0xbf, 0xd0, 0xbe,
270 0x2e, 0x10, 0xfa, 0x74, 0x02, 0x9b, 0x48, 0xa5, 0xc9, 0x74, 0xed, 0xed, 0x7d, 0xa8
271 };
272
273 #ifndef OPENSSL_NO_DH
274 static char group_ffdhe2048[] = "ffdhe2048";
275 static const OSSL_PARAM dhx_keygen_params[] = {
276 OSSL_PARAM_utf8_string("group", group_ffdhe2048,
277 sizeof(group_ffdhe2048) - 1),
278 OSSL_PARAM_END
279 };
280 #endif
281
282 #ifndef OPENSSL_NO_EC
283 static char group_p256[] = "P-256";
284 static const OSSL_PARAM ec_keygen_params[] = {
285 OSSL_PARAM_utf8_string("group", group_p256, sizeof(group_p256) - 1),
286 OSSL_PARAM_END
287 };
288 #endif
289
290 #ifndef OPENSSL_NO_DH
291 static const unsigned char kExampleDHPrivateKeyDER[] = {
292 0x30, 0x82, 0x02, 0x26, 0x02, 0x01, 0x00, 0x30, 0x82, 0x01, 0x17, 0x06,
293 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x03, 0x01, 0x30, 0x82,
294 0x01, 0x08, 0x02, 0x82, 0x01, 0x01, 0x00, 0xD8, 0x4B, 0x0F, 0x0E, 0x6B,
295 0x79, 0xE9, 0x23, 0x4E, 0xE4, 0xBE, 0x9A, 0x8F, 0x7A, 0x5C, 0xA3, 0x20,
296 0xD0, 0x86, 0x6B, 0x95, 0x78, 0x39, 0x59, 0x7A, 0x11, 0x2A, 0x5B, 0x87,
297 0xA4, 0xFB, 0x2F, 0x99, 0xD0, 0x57, 0xF5, 0xE1, 0xA3, 0xAF, 0x41, 0xD1,
298 0xCD, 0xA3, 0x94, 0xBB, 0xE5, 0x5A, 0x68, 0xE2, 0xEE, 0x69, 0x56, 0x51,
299 0xB2, 0xEE, 0xF2, 0xFE, 0x10, 0xC9, 0x55, 0xE3, 0x82, 0x3C, 0x50, 0x0D,
300 0xF5, 0x82, 0x73, 0xE4, 0xD6, 0x3E, 0x45, 0xB4, 0x89, 0x80, 0xE4, 0xF0,
301 0x99, 0x85, 0x2B, 0x4B, 0xF9, 0xB8, 0xFD, 0x2C, 0x3C, 0x49, 0x2E, 0xB3,
302 0x56, 0x7E, 0x99, 0x07, 0xD3, 0xF7, 0xD9, 0xE4, 0x0C, 0x64, 0xC5, 0x7D,
303 0x03, 0x8E, 0x05, 0x3C, 0x0A, 0x40, 0x17, 0xAD, 0xA8, 0x0F, 0x9B, 0xF4,
304 0x8B, 0xA7, 0xDB, 0x16, 0x4F, 0x4A, 0x57, 0x0B, 0x89, 0x80, 0x0B, 0x9F,
305 0x26, 0x56, 0x3F, 0x1D, 0xFA, 0x52, 0x2D, 0x1A, 0x9E, 0xDC, 0x42, 0xA3,
306 0x2E, 0xA9, 0x87, 0xE3, 0x8B, 0x45, 0x5E, 0xEE, 0x99, 0xB8, 0x30, 0x15,
307 0x58, 0xA3, 0x5F, 0xB5, 0x69, 0xD8, 0x0C, 0xE8, 0x6B, 0x36, 0xD8, 0xAB,
308 0xD8, 0xE4, 0x77, 0x46, 0x13, 0xA2, 0x15, 0xB3, 0x9C, 0xAD, 0x99, 0x91,
309 0xE5, 0xA3, 0x30, 0x7D, 0x40, 0x70, 0xB3, 0x32, 0x5E, 0xAF, 0x96, 0x8D,
310 0xE6, 0x3F, 0x47, 0xA3, 0x18, 0xDA, 0xE1, 0x9A, 0x20, 0x11, 0xE1, 0x49,
311 0x51, 0x45, 0xE3, 0x8C, 0xA5, 0x56, 0x39, 0x67, 0xCB, 0x9D, 0xCF, 0xBA,
312 0xF4, 0x46, 0x4E, 0x0A, 0xB6, 0x0B, 0xA9, 0xB4, 0xF6, 0xF1, 0x6A, 0xC8,
313 0x63, 0xE2, 0xB4, 0xB2, 0x9F, 0x44, 0xAA, 0x0A, 0xDA, 0x53, 0xF7, 0x52,
314 0x14, 0x57, 0xEE, 0x2C, 0x5D, 0x31, 0x9C, 0x27, 0x03, 0x64, 0x9E, 0xC0,
315 0x1E, 0x4B, 0x1B, 0x4F, 0xEE, 0xA6, 0x3F, 0xC1, 0x3E, 0x61, 0x93, 0x02,
316 0x01, 0x02, 0x04, 0x82, 0x01, 0x04, 0x02, 0x82, 0x01, 0x00, 0x7E, 0xC2,
317 0x04, 0xF9, 0x95, 0xC7, 0xEF, 0x96, 0xBE, 0xA0, 0x9D, 0x2D, 0xC3, 0x0C,
318 0x3A, 0x67, 0x02, 0x7C, 0x7D, 0x3B, 0xC9, 0xB1, 0xDE, 0x13, 0x97, 0x64,
319 0xEF, 0x87, 0x80, 0x4F, 0xBF, 0xA2, 0xAC, 0x18, 0x6B, 0xD5, 0xB2, 0x42,
320 0x0F, 0xDA, 0x28, 0x40, 0x93, 0x40, 0xB2, 0x1E, 0x80, 0xB0, 0x6C, 0xDE,
321 0x9C, 0x54, 0xA4, 0xB4, 0x68, 0x29, 0xE0, 0x13, 0x57, 0x1D, 0xC9, 0x87,
322 0xC0, 0xDE, 0x2F, 0x1D, 0x72, 0xF0, 0xC0, 0xE4, 0x4E, 0x04, 0x48, 0xF5,
323 0x2D, 0x8D, 0x9A, 0x1B, 0xE5, 0xEB, 0x06, 0xAB, 0x7C, 0x74, 0x10, 0x3C,
324 0xA8, 0x2D, 0x39, 0xBC, 0xE3, 0x15, 0x3E, 0x63, 0x37, 0x8C, 0x1B, 0xF1,
325 0xB3, 0x99, 0xB6, 0xAE, 0x5A, 0xEB, 0xB3, 0x3D, 0x30, 0x39, 0x69, 0xDB,
326 0xF2, 0x4F, 0x94, 0xB7, 0x71, 0xAF, 0xBA, 0x5C, 0x1F, 0xF8, 0x6B, 0xE5,
327 0xD1, 0xB1, 0x00, 0x81, 0xE2, 0x6D, 0xEC, 0x65, 0xF7, 0x7E, 0xCE, 0x03,
328 0x84, 0x68, 0x42, 0x6A, 0x8B, 0x47, 0x8E, 0x4A, 0x88, 0xDE, 0x82, 0xDD,
329 0xAF, 0xA9, 0x6F, 0x18, 0xF7, 0xC6, 0xE2, 0xB9, 0x97, 0xCE, 0x47, 0x8F,
330 0x85, 0x19, 0x61, 0x42, 0x67, 0x21, 0x7D, 0x13, 0x6E, 0xB5, 0x5A, 0x62,
331 0xF3, 0x08, 0xE2, 0x70, 0x3B, 0x0E, 0x85, 0x3C, 0xA1, 0xD3, 0xED, 0x7A,
332 0x43, 0xD6, 0xDE, 0x30, 0x5C, 0x48, 0xB2, 0x99, 0xAB, 0x3E, 0x65, 0xA6,
333 0x66, 0x80, 0x22, 0xFF, 0x92, 0xC1, 0x42, 0x1C, 0x30, 0x87, 0x74, 0x1E,
334 0x53, 0x57, 0x7C, 0xF8, 0x77, 0x51, 0xF1, 0x74, 0x16, 0xF4, 0x45, 0x26,
335 0x77, 0x0A, 0x05, 0x96, 0x13, 0x12, 0x06, 0x86, 0x2B, 0xB8, 0x49, 0x82,
336 0x69, 0x43, 0x0A, 0x57, 0xA7, 0x30, 0x19, 0x4C, 0xB8, 0x47, 0x82, 0x6E,
337 0x64, 0x7A, 0x06, 0x13, 0x5A, 0x82, 0x98, 0xD6, 0x7A, 0x09, 0xEC, 0x03,
338 0x8D, 0x03
339 };
340 #endif /* OPENSSL_NO_DH */
341
342 #ifndef OPENSSL_NO_EC
343 /*
344 * kExampleECKeyDER is a sample EC private key encoded as an ECPrivateKey
345 * structure.
346 */
347 static const unsigned char kExampleECKeyDER[] = {
348 0x30, 0x77, 0x02, 0x01, 0x01, 0x04, 0x20, 0x07, 0x0f, 0x08,
349 0x72, 0x7a, 0xd4, 0xa0, 0x4a, 0x9c, 0xdd, 0x59, 0xc9, 0x4d,
350 0x89, 0x68, 0x77, 0x08, 0xb5, 0x6f, 0xc9, 0x5d, 0x30, 0x77,
351 0x0e, 0xe8, 0xd1, 0xc9, 0xce, 0x0a, 0x8b, 0xb4, 0x6a, 0xa0,
352 0x0a, 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01,
353 0x07, 0xa1, 0x44, 0x03, 0x42, 0x00, 0x04, 0xe6, 0x2b, 0x69,
354 0xe2, 0xbf, 0x65, 0x9f, 0x97, 0xbe, 0x2f, 0x1e, 0x0d, 0x94,
355 0x8a, 0x4c, 0xd5, 0x97, 0x6b, 0xb7, 0xa9, 0x1e, 0x0d, 0x46,
356 0xfb, 0xdd, 0xa9, 0xa9, 0x1e, 0x9d, 0xdc, 0xba, 0x5a, 0x01,
357 0xe7, 0xd6, 0x97, 0xa8, 0x0a, 0x18, 0xf9, 0xc3, 0xc4, 0xa3,
358 0x1e, 0x56, 0xe2, 0x7c, 0x83, 0x48, 0xdb, 0x16, 0x1a, 0x1c,
359 0xf5, 0x1d, 0x7e, 0xf1, 0x94, 0x2d, 0x4b, 0xcf, 0x72, 0x22,
360 0xc1
361 };
362
363 /* P-384 sample EC private key in PKCS8 format (no public key) */
364 static const unsigned char kExampleECKey2DER[] = {
365 0x30, 0x4E, 0x02, 0x01, 0x00, 0x30, 0x10, 0x06, 0x07, 0x2A, 0x86, 0x48,
366 0xCE, 0x3D, 0x02, 0x01, 0x06, 0x05, 0x2B, 0x81, 0x04, 0x00, 0x22, 0x04,
367 0x37, 0x30, 0x35, 0x02, 0x01, 0x01, 0x04, 0x30, 0x73, 0xE3, 0x3A, 0x05,
368 0xF2, 0xB6, 0x99, 0x6D, 0x0C, 0x33, 0x7F, 0x15, 0x9E, 0x10, 0xA9, 0x17,
369 0x4C, 0x0A, 0x82, 0x57, 0x71, 0x13, 0x7A, 0xAC, 0x46, 0xA2, 0x5E, 0x1C,
370 0xE0, 0xC7, 0xB2, 0xF8, 0x20, 0x40, 0xC2, 0x27, 0xC8, 0xBE, 0x02, 0x7E,
371 0x96, 0x69, 0xE0, 0x04, 0xCB, 0x89, 0x0B, 0x42
372 };
373
374 #ifndef OPENSSL_NO_ECX
375 static const unsigned char kExampleECXKey2DER[] = {
376 0x30, 0x2E, 0x02, 0x01, 0x00, 0x30, 0x05, 0x06, 0x03, 0x2b, 0x65, 0x6e,
377 0x04, 0x22, 0x04, 0x20, 0xc8, 0xa9, 0xd5, 0xa9, 0x10, 0x91, 0xad, 0x85,
378 0x1c, 0x66, 0x8b, 0x07, 0x36, 0xc1, 0xc9, 0xa0, 0x29, 0x36, 0xc0, 0xd3,
379 0xad, 0x62, 0x67, 0x08, 0x58, 0x08, 0x80, 0x47, 0xba, 0x05, 0x74, 0x75
380 };
381 #endif
382 #endif
383
384 typedef struct APK_DATA_st {
385 const unsigned char *kder;
386 size_t size;
387 int evptype;
388 } APK_DATA;
389
390 static APK_DATA keydata[] = {
391 { kExampleRSAKeyDER, sizeof(kExampleRSAKeyDER), EVP_PKEY_RSA },
392 { kExampleRSAKeyPKCS8, sizeof(kExampleRSAKeyPKCS8), EVP_PKEY_RSA },
393 #ifndef OPENSSL_NO_EC
394 #ifndef OPENSSL_NO_ECX
395 { kExampleECXKey2DER, sizeof(kExampleECXKey2DER), EVP_PKEY_X25519 },
396 #endif
397 { kExampleECKeyDER, sizeof(kExampleECKeyDER), EVP_PKEY_EC },
398 { kExampleECKey2DER, sizeof(kExampleECKey2DER), EVP_PKEY_EC },
399 #endif
400 #ifndef OPENSSL_NO_DH
401 { kExampleDHPrivateKeyDER, sizeof(kExampleDHPrivateKeyDER), EVP_PKEY_DH },
402 #endif
403 };
404
pkey_has_private(EVP_PKEY * key,const char * privtag,int use_octstring)405 static int pkey_has_private(EVP_PKEY *key, const char *privtag,
406 int use_octstring)
407 {
408 if (use_octstring) {
409 /*
410 * Existence probe only: pass NULL buffer, ignore the returned size.
411 * Avoids hard-coded fixed buffers that would cap at 64 bytes and
412 * fail for PQC private keys (ML-DSA-44 is ~2.5 KB).
413 */
414 return EVP_PKEY_get_octet_string_param(key, privtag, NULL, 0, NULL);
415 } else {
416 BIGNUM *bn = NULL;
417 int ret = EVP_PKEY_get_bn_param(key, privtag, &bn);
418
419 BN_free(bn);
420 return ret;
421 }
422 }
423
424 /*
425 * Private-component tags we know about, in lookup order. Extend this
426 * list if a new algorithm adopts a different convention; do_pkey_tofrom
427 * _data_select() scans it on each key and only needs one match.
428 */
429 static const char *const pkey_priv_tags[] = {
430 OSSL_PKEY_PARAM_PRIV_KEY, /* DH, DSA, EC, ECX, SM2, ML-*, SLH-DSA */
431 OSSL_PKEY_PARAM_RSA_D, /* RSA, RSA-PSS */
432 };
433
do_pkey_tofrom_data_select(EVP_PKEY * key,const char * keytype)434 static int do_pkey_tofrom_data_select(EVP_PKEY *key, const char *keytype)
435 {
436 int ret = 0;
437 OSSL_PARAM *pub_params = NULL, *keypair_params = NULL;
438 const OSSL_PARAM *priv = NULL;
439 EVP_PKEY *fromkey = NULL, *fromkeypair = NULL;
440 EVP_PKEY_CTX *fromctx = NULL;
441 const char *privtag;
442 int use_octstring;
443 size_t t;
444
445 /*
446 * Export the full keypair first so we can discover the algorithm's
447 * private-component tag from the emitted params rather than hand-coding
448 * it per keytype.
449 */
450 if (!TEST_int_eq(EVP_PKEY_todata(key, EVP_PKEY_KEYPAIR, &keypair_params), 1))
451 goto end;
452
453 for (t = 0; t < OSSL_NELEM(pkey_priv_tags); t++)
454 if ((priv = OSSL_PARAM_locate(keypair_params, pkey_priv_tags[t])) != NULL)
455 break;
456 if (priv == NULL) {
457 TEST_error("%s: KEYPAIR todata() emitted no known private component",
458 keytype);
459 goto end;
460 }
461 privtag = priv->key;
462 use_octstring = priv->data_type == OSSL_PARAM_OCTET_STRING;
463
464 /*
465 * Select only the public key via EVP_PKEY_todata(): the private tag we
466 * just learned must be absent.
467 */
468 if (!TEST_int_eq(EVP_PKEY_todata(key, EVP_PKEY_PUBLIC_KEY, &pub_params), 1)
469 || !TEST_ptr_null(OSSL_PARAM_locate(pub_params, privtag)))
470 goto end;
471
472 /*
473 * Round-trip: importing only the public selection from the full keypair
474 * params must yield a key that has no private component.
475 */
476 if (!TEST_ptr(fromctx = EVP_PKEY_CTX_new_from_name(mainctx, keytype, NULL))
477 || !TEST_int_eq(EVP_PKEY_fromdata_init(fromctx), 1)
478 || !TEST_int_eq(EVP_PKEY_fromdata(fromctx, &fromkey, EVP_PKEY_PUBLIC_KEY,
479 keypair_params),
480 1)
481 || !TEST_false(pkey_has_private(fromkey, privtag, use_octstring)))
482 goto end;
483
484 /* Round-trip: a full keypair import must carry the private component. */
485 if (!TEST_int_eq(EVP_PKEY_fromdata(fromctx, &fromkeypair,
486 EVP_PKEY_KEYPAIR, keypair_params),
487 1)
488 || !TEST_true(pkey_has_private(fromkeypair, privtag, use_octstring)))
489 goto end;
490 ret = 1;
491 end:
492 EVP_PKEY_free(fromkeypair);
493 EVP_PKEY_free(fromkey);
494 EVP_PKEY_CTX_free(fromctx);
495 OSSL_PARAM_free(keypair_params);
496 OSSL_PARAM_free(pub_params);
497 return ret;
498 }
499
500 /*
501 * Pass every advertised gettable parameter through EVP_PKEY_get_params()
502 * as a probe, twice: once with NULL data (size negotiation), then again
503 * with storage allocated for the advertised type. Either fetch must
504 * succeed as a whole. Pass 2 is what exercises the type-matching
505 * branches in the per-type OSSL_PARAM setters: a NULL-data probe
506 * succeeds before those branches are reached, which is why this check
507 * would otherwise miss gettable-table / getter mismatches (an entry
508 * advertised as OCTET_STRING whose getter populates it as a BIGNUM).
509 *
510 * The provider is free to leave individual entries unmodified, e.g.
511 * private-key data on a public-only key; only whole-fetch failure
512 * is treated as a regression.
513 */
do_pkey_all_gettables_probe(EVP_PKEY * pkey,const char * keytype)514 static int do_pkey_all_gettables_probe(EVP_PKEY *pkey, const char *keytype)
515 {
516 int ret = 0;
517 const OSSL_PARAM *gettable;
518 OSSL_PARAM *probe = NULL;
519 void **storage = NULL;
520 size_t n = 0, i;
521 int need_refetch = 0;
522
523 if (!TEST_ptr(gettable = EVP_PKEY_gettable_params(pkey)))
524 goto err;
525
526 while (gettable[n].key != NULL)
527 n++;
528 if (!TEST_ptr(probe = OPENSSL_zalloc((n + 1) * sizeof(*probe)))
529 || (n > 0
530 && !TEST_ptr(storage = OPENSSL_zalloc(n * sizeof(*storage)))))
531 goto err;
532
533 for (i = 0; i < n; i++) {
534 /*
535 * Copy the advertised entry as a template, then reset the
536 * data fields to the "probe" state. For OCTET_PTR / UTF8_PTR
537 * the setter writes *data directly (no size negotiation),
538 * so hand it a pointer slot up front.
539 */
540 probe[i] = gettable[i];
541 probe[i].data = NULL;
542 probe[i].data_size = 0;
543 probe[i].return_size = OSSL_PARAM_UNMODIFIED;
544 if (gettable[i].data_type == OSSL_PARAM_OCTET_PTR
545 || gettable[i].data_type == OSSL_PARAM_UTF8_PTR) {
546 if (!TEST_ptr(storage[i] = OPENSSL_zalloc(sizeof(void *))))
547 goto err;
548 probe[i].data = storage[i];
549 }
550 }
551 probe[n] = OSSL_PARAM_construct_end();
552
553 /* Pass 1: NULL-data probe populates return_size for sized entries. */
554 if (!TEST_true(EVP_PKEY_get_params(pkey, probe))) {
555 TEST_info("%s: Pass 1 (size probe) failed", keytype);
556 goto err;
557 }
558
559 /* Allocate storage for each populated sized entry. */
560 for (i = 0; i < n; i++) {
561 size_t need;
562
563 if (!OSSL_PARAM_modified(&probe[i]))
564 continue;
565 switch (probe[i].data_type) {
566 case OSSL_PARAM_INTEGER:
567 case OSSL_PARAM_UNSIGNED_INTEGER:
568 case OSSL_PARAM_OCTET_STRING:
569 case OSSL_PARAM_REAL:
570 need = probe[i].return_size;
571 if (!TEST_ptr(storage[i] = OPENSSL_malloc(need == 0 ? 1 : need)))
572 goto err;
573 probe[i].data = storage[i];
574 probe[i].data_size = need;
575 probe[i].return_size = OSSL_PARAM_UNMODIFIED;
576 need_refetch = 1;
577 break;
578 case OSSL_PARAM_UTF8_STRING:
579 /* Leave room for a trailing NUL beyond return_size. */
580 need = probe[i].return_size + 1;
581 if (!TEST_ptr(storage[i] = OPENSSL_malloc(need)))
582 goto err;
583 probe[i].data = storage[i];
584 probe[i].data_size = need;
585 probe[i].return_size = OSSL_PARAM_UNMODIFIED;
586 need_refetch = 1;
587 break;
588 default:
589 /* PTR types were populated in Pass 1; unknown types left as-is. */
590 break;
591 }
592 }
593
594 /*
595 * Pass 2: real fetch, with typed storage. This is what actually
596 * catches advertised-vs-populated type mismatches: the per-type
597 * OSSL_PARAM_set_*() setters only enforce data_type when data is
598 * non-NULL.
599 */
600 if (need_refetch && !TEST_true(EVP_PKEY_get_params(pkey, probe))) {
601 TEST_info("%s: Pass 2 (real fetch) failed", keytype);
602 goto err;
603 }
604 ret = 1;
605 err:
606 if (storage != NULL)
607 for (i = 0; i < n; i++)
608 OPENSSL_free(storage[i]);
609 OPENSSL_free(storage);
610 OPENSSL_free(probe);
611 return ret;
612 }
613
614 /*
615 * Composite check: exercise both the selection-mask semantics of
616 * todata/fromdata and the advertised-vs-populated type consistency of
617 * every gettable parameter on the given key. The individual helpers are
618 * algorithm-agnostic, so this driver works for any asymmetric EVP_PKEY.
619 * Each keytype-specific test function reduces to (a) key generation and
620 * (b) invoking this driver.
621 */
do_pkey_checks(EVP_PKEY * key,const char * keytype)622 static int do_pkey_checks(EVP_PKEY *key, const char *keytype)
623 {
624 return do_pkey_tofrom_data_select(key, keytype)
625 && do_pkey_all_gettables_probe(key, keytype);
626 }
627
628 #ifndef OPENSSL_NO_DH
test_dh_paramgen(void)629 static int test_dh_paramgen(void)
630 {
631 int ret;
632 OSSL_PARAM params[3];
633 EVP_PKEY *pkey = NULL;
634 EVP_PKEY_CTX *gctx = NULL;
635 unsigned int pbits = 512; /* minimum allowed for speed */
636
637 params[0] = OSSL_PARAM_construct_uint(OSSL_PKEY_PARAM_FFC_PBITS, &pbits);
638 params[1] = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_FFC_TYPE,
639 "generator", 0);
640 params[2] = OSSL_PARAM_construct_end();
641
642 ret = TEST_ptr(gctx = EVP_PKEY_CTX_new_from_name(mainctx, "DH", NULL))
643 && TEST_int_gt(EVP_PKEY_paramgen_init(gctx), 0)
644 && TEST_true(EVP_PKEY_CTX_set_params(gctx, params))
645 && TEST_true(EVP_PKEY_paramgen(gctx, &pkey))
646 && TEST_ptr(pkey);
647
648 EVP_PKEY_CTX_free(gctx);
649 gctx = NULL;
650
651 ret = ret && TEST_ptr(gctx = EVP_PKEY_CTX_new_from_pkey(mainctx, pkey, NULL))
652 && TEST_int_eq(EVP_PKEY_param_check(gctx), 1)
653 && TEST_int_eq(EVP_PKEY_param_check_quick(gctx), 1);
654
655 EVP_PKEY_CTX_free(gctx);
656 EVP_PKEY_free(pkey);
657 return ret;
658 }
659
set_fromdata_string(EVP_PKEY_CTX * ctx,const char * name,char * value)660 static int set_fromdata_string(EVP_PKEY_CTX *ctx, const char *name, char *value)
661 {
662 int ret;
663 OSSL_PARAM params[2];
664 EVP_PKEY *pkey = NULL;
665
666 if (EVP_PKEY_fromdata_init(ctx) != 1)
667 return -1;
668 params[0] = OSSL_PARAM_construct_utf8_string(name, value, 0);
669 params[1] = OSSL_PARAM_construct_end();
670 ret = EVP_PKEY_fromdata(ctx, &pkey, EVP_PKEY_KEY_PARAMETERS, params);
671 EVP_PKEY_free(pkey);
672 return ret;
673 }
674
set_fromdata_uint(EVP_PKEY_CTX * ctx,const char * name)675 static int set_fromdata_uint(EVP_PKEY_CTX *ctx, const char *name)
676 {
677 int ret;
678 unsigned int tmp = 0;
679 OSSL_PARAM params[2];
680 EVP_PKEY *pkey = NULL;
681
682 if (EVP_PKEY_fromdata_init(ctx) != 1)
683 return -1;
684 params[0] = OSSL_PARAM_construct_uint(name, &tmp);
685 params[1] = OSSL_PARAM_construct_end();
686 ret = EVP_PKEY_fromdata(ctx, &pkey, EVP_PKEY_KEY_PARAMETERS, params);
687 EVP_PKEY_free(pkey);
688 return ret;
689 }
690
test_dh_paramfromdata(void)691 static int test_dh_paramfromdata(void)
692 {
693 EVP_PKEY_CTX *ctx = NULL;
694 int ret = 0;
695
696 /* Test failure paths for FFC - mainly due to setting the wrong param type */
697 ret = TEST_ptr(ctx = EVP_PKEY_CTX_new_from_name(mainctx, "DH", NULL))
698 && TEST_int_eq(set_fromdata_uint(ctx, OSSL_PKEY_PARAM_GROUP_NAME), 0)
699 && TEST_int_eq(set_fromdata_string(ctx, OSSL_PKEY_PARAM_GROUP_NAME, "bad"), 0)
700 && TEST_int_eq(set_fromdata_string(ctx, OSSL_PKEY_PARAM_FFC_P, "bad"), 0)
701 && TEST_int_eq(set_fromdata_string(ctx, OSSL_PKEY_PARAM_FFC_GINDEX, "bad"), 0)
702 && TEST_int_eq(set_fromdata_string(ctx, OSSL_PKEY_PARAM_FFC_PCOUNTER, "bad"), 0)
703 && TEST_int_eq(set_fromdata_string(ctx, OSSL_PKEY_PARAM_FFC_COFACTOR, "bad"), 0)
704 && TEST_int_eq(set_fromdata_string(ctx, OSSL_PKEY_PARAM_FFC_H, "bad"), 0)
705 && TEST_int_eq(set_fromdata_uint(ctx, OSSL_PKEY_PARAM_FFC_SEED), 0)
706 && TEST_int_eq(set_fromdata_string(ctx, OSSL_PKEY_PARAM_FFC_VALIDATE_PQ, "bad"), 0)
707 && TEST_int_eq(set_fromdata_string(ctx, OSSL_PKEY_PARAM_FFC_VALIDATE_G, "bad"), 0)
708 && TEST_int_eq(set_fromdata_string(ctx, OSSL_PKEY_PARAM_FFC_VALIDATE_LEGACY, "bad"), 0)
709 && TEST_int_eq(set_fromdata_uint(ctx, OSSL_PKEY_PARAM_FFC_DIGEST), 0);
710
711 EVP_PKEY_CTX_free(ctx);
712 return ret;
713 }
714
715 #endif
716
717 /* Test that calling EVP_PKEY_Q_keygen() for a non-standard keytype works as expected */
test_new_keytype(void)718 static int test_new_keytype(void)
719 {
720 int ret = 0;
721 EVP_PKEY *key = NULL;
722 OSSL_PROVIDER *tlsprov = NULL;
723 EVP_PKEY_CTX *ctx = NULL;
724 size_t outlen, secretlen, secretlen2;
725 unsigned char *out = NULL, *secret = NULL, *secret2 = NULL;
726
727 /* without tls-provider key should not be create-able */
728 if (!TEST_ptr_null(key = EVP_PKEY_Q_keygen(mainctx, NULL, "XOR")))
729 goto err;
730 /* prepare & load tls-provider */
731 if (!TEST_true(OSSL_PROVIDER_add_builtin(mainctx, "tls-provider",
732 tls_provider_init))
733 || !TEST_ptr(tlsprov = OSSL_PROVIDER_load(mainctx, "tls-provider")))
734 goto err;
735 /* now try creating key again, should work this time */
736 if (!TEST_ptr(key = EVP_PKEY_Q_keygen(mainctx, NULL, "XOR")))
737 goto err;
738 /* now do encaps/decaps to validate all is good */
739 if (!TEST_ptr(ctx = EVP_PKEY_CTX_new(key, NULL))
740 || !TEST_int_eq(EVP_PKEY_encapsulate_init(ctx, NULL), 1)
741 || !TEST_int_eq(EVP_PKEY_encapsulate(ctx, NULL, &outlen, NULL, &secretlen), 1))
742 goto err;
743 out = OPENSSL_malloc(outlen);
744 secret = OPENSSL_malloc(secretlen);
745 secret2 = OPENSSL_malloc(secretlen);
746 if (out == NULL || secret == NULL || secret2 == NULL
747 || !TEST_int_eq(EVP_PKEY_encapsulate(ctx, out, &outlen, secret, &secretlen), 1)
748 || !TEST_int_eq(EVP_PKEY_decapsulate_init(ctx, NULL), 1)
749 || !TEST_int_eq(EVP_PKEY_decapsulate(ctx, secret2, &secretlen2, out, outlen), 1)
750 || !TEST_mem_eq(secret, secretlen, secret2, secretlen2))
751 goto err;
752 ret = OSSL_PROVIDER_unload(tlsprov);
753
754 err:
755 OPENSSL_free(out);
756 OPENSSL_free(secret);
757 OPENSSL_free(secret2);
758 EVP_PKEY_CTX_free(ctx);
759 EVP_PKEY_free(key);
760 return ret;
761 }
762
763 #ifndef OPENSSL_NO_EC
764
test_ec_d2i_i2d_pubkey(void)765 static int test_ec_d2i_i2d_pubkey(void)
766 {
767 int ret = 0;
768 FILE *fp = NULL;
769 EVP_PKEY *key = NULL, *outkey = NULL;
770 static const char *filename = "pubkey.der";
771
772 if (!TEST_ptr(fp = fopen(filename, "wb"))
773 || !TEST_ptr(key = EVP_PKEY_Q_keygen(mainctx, NULL, "EC", "P-256"))
774 || !TEST_true(i2d_PUBKEY_fp(fp, key))
775 || !TEST_int_eq(fclose(fp), 0))
776 goto err;
777 fp = NULL;
778
779 if (!TEST_ptr(fp = fopen(filename, "rb"))
780 || !TEST_ptr(outkey = d2i_PUBKEY_ex_fp(fp, NULL, mainctx, NULL))
781 || !TEST_int_eq(EVP_PKEY_eq(key, outkey), 1))
782 goto err;
783
784 ret = 1;
785
786 err:
787 EVP_PKEY_free(outkey);
788 EVP_PKEY_free(key);
789 fclose(fp);
790 return ret;
791 }
792
793 #endif /* OPENSSL_NO_EC */
794
795 /* This is the equivalent of test_d2i_AutoPrivateKey in evp_extra_test */
test_d2i_AutoPrivateKey_ex(int i)796 static int test_d2i_AutoPrivateKey_ex(int i)
797 {
798 int ret = 0;
799 const unsigned char *p;
800 EVP_PKEY *pkey = NULL;
801 const APK_DATA *ak = &keydata[i];
802 const unsigned char *input = ak->kder;
803 size_t input_len = ak->size;
804 int expected_id = ak->evptype;
805 BIGNUM *p_bn = NULL;
806 BIGNUM *g_bn = NULL;
807 BIGNUM *priv_bn = NULL;
808
809 p = input;
810 if (!TEST_ptr(pkey = d2i_AutoPrivateKey_ex(NULL, &p, input_len, mainctx,
811 NULL))
812 || !TEST_ptr_eq(p, input + input_len)
813 || !TEST_int_eq(EVP_PKEY_get_id(pkey), expected_id))
814 goto done;
815
816 if (ak->evptype == EVP_PKEY_RSA) {
817 if (!TEST_true(EVP_PKEY_get_bn_param(pkey, OSSL_PKEY_PARAM_RSA_D,
818 &priv_bn)))
819 goto done;
820 } else if (ak->evptype == EVP_PKEY_X25519) {
821 unsigned char buffer[32];
822 size_t len;
823
824 if (!TEST_true(EVP_PKEY_get_octet_string_param(pkey,
825 OSSL_PKEY_PARAM_PRIV_KEY,
826 buffer, sizeof(buffer),
827 &len)))
828 goto done;
829 } else {
830 if (!TEST_true(EVP_PKEY_get_bn_param(pkey, OSSL_PKEY_PARAM_PRIV_KEY,
831 &priv_bn)))
832 goto done;
833 }
834
835 if (ak->evptype == EVP_PKEY_DH) {
836 if (!TEST_true(EVP_PKEY_get_bn_param(pkey, OSSL_PKEY_PARAM_FFC_P, &p_bn))
837 || !TEST_true(EVP_PKEY_get_bn_param(pkey, OSSL_PKEY_PARAM_FFC_G,
838 &g_bn)))
839 goto done;
840 }
841
842 ret = 1;
843 done:
844 BN_free(p_bn);
845 BN_free(g_bn);
846 BN_free(priv_bn);
847 EVP_PKEY_free(pkey);
848 return ret;
849 }
850
851 #ifndef OPENSSL_NO_DES
test_pkcs8key_nid_bio(void)852 static int test_pkcs8key_nid_bio(void)
853 {
854 int ret;
855 const int nid = NID_pbe_WithSHA1And3_Key_TripleDES_CBC;
856 static const char pwd[] = "PASSWORD";
857 EVP_PKEY *pkey = NULL, *pkey_dec = NULL;
858 BIO *in = NULL, *enc_bio = NULL;
859 char *enc_data = NULL;
860 long enc_datalen = 0;
861 OSSL_PROVIDER *provider = NULL;
862
863 ret = TEST_ptr(provider = OSSL_PROVIDER_load(NULL, "default"))
864 && TEST_ptr(enc_bio = BIO_new(BIO_s_mem()))
865 && TEST_ptr(in = BIO_new_mem_buf(kExampleRSAKeyPKCS8,
866 sizeof(kExampleRSAKeyPKCS8)))
867 && TEST_ptr(pkey = d2i_PrivateKey_ex_bio(in, NULL, NULL, NULL))
868 && TEST_int_eq(i2d_PKCS8PrivateKey_nid_bio(enc_bio, pkey, nid,
869 pwd, sizeof(pwd) - 1,
870 NULL, NULL),
871 1)
872 && TEST_int_gt(enc_datalen = BIO_get_mem_data(enc_bio, &enc_data), 0)
873 && TEST_ptr(pkey_dec = d2i_PKCS8PrivateKey_bio(enc_bio, NULL, NULL,
874 (void *)pwd))
875 && TEST_true(EVP_PKEY_eq(pkey, pkey_dec));
876
877 EVP_PKEY_free(pkey_dec);
878 EVP_PKEY_free(pkey);
879 BIO_free(in);
880 BIO_free(enc_bio);
881 OSSL_PROVIDER_unload(provider);
882 return ret;
883 }
884 #endif /* OPENSSL_NO_DES */
885
test_alternative_default(void)886 static int test_alternative_default(void)
887 {
888 OSSL_LIB_CTX *oldctx;
889 EVP_MD *sha256;
890 int ok = 0;
891
892 /*
893 * setup_tests() loaded the "null" provider in the current default, so
894 * we know this fetch should fail.
895 */
896 if (!TEST_ptr_null(sha256 = EVP_MD_fetch(NULL, "SHA2-256", NULL)))
897 goto err;
898
899 /*
900 * Now we switch to our main library context, and try again. Since no
901 * providers are loaded in this one, it should fall back to the default.
902 */
903 if (!TEST_ptr(oldctx = OSSL_LIB_CTX_set0_default(mainctx))
904 || !TEST_ptr(sha256 = EVP_MD_fetch(NULL, "SHA2-256", NULL)))
905 goto err;
906 EVP_MD_free(sha256);
907 sha256 = NULL;
908
909 /*
910 * Switching back should give us our main library context back, and
911 * fetching SHA2-256 should fail again.
912 */
913 if (!TEST_ptr_eq(OSSL_LIB_CTX_set0_default(oldctx), mainctx)
914 || !TEST_ptr_null(sha256 = EVP_MD_fetch(NULL, "SHA2-256", NULL)))
915 goto err;
916
917 ok = 1;
918 err:
919 EVP_MD_free(sha256);
920 return ok;
921 }
922
test_provider_unload_effective(int testid)923 static int test_provider_unload_effective(int testid)
924 {
925 EVP_MD *sha256 = NULL;
926 OSSL_PROVIDER *provider = NULL;
927 int ok = 0;
928
929 if (!TEST_ptr(provider = OSSL_PROVIDER_load(NULL, "default"))
930 || !TEST_ptr(sha256 = EVP_MD_fetch(NULL, "SHA2-256", NULL)))
931 goto err;
932
933 if (testid > 0) {
934 OSSL_PROVIDER_unload(provider);
935 provider = NULL;
936 EVP_MD_free(sha256);
937 sha256 = NULL;
938 } else {
939 EVP_MD_free(sha256);
940 sha256 = NULL;
941 OSSL_PROVIDER_unload(provider);
942 provider = NULL;
943 }
944
945 /*
946 * setup_tests() loaded the "null" provider in the current default, and
947 * we unloaded it above after the load so we know this fetch should fail.
948 */
949 if (!TEST_ptr_null(sha256 = EVP_MD_fetch(NULL, "SHA2-256", NULL)))
950 goto err;
951
952 ok = 1;
953 err:
954 EVP_MD_free(sha256);
955 OSSL_PROVIDER_unload(provider);
956 return ok;
957 }
958
test_d2i_PrivateKey_ex(int testid)959 static int test_d2i_PrivateKey_ex(int testid)
960 {
961 int ok = 0;
962 OSSL_PROVIDER *provider = NULL;
963 BIO *key_bio = NULL;
964 EVP_PKEY *pkey = NULL;
965 int id = (testid == 0) ? 0 : 2;
966
967 if (!TEST_ptr(provider = OSSL_PROVIDER_load(NULL, "default")))
968 goto err;
969 if (!TEST_ptr(key_bio = BIO_new_mem_buf(keydata[id].kder, keydata[id].size)))
970 goto err;
971 if (!TEST_ptr_null(pkey = PEM_read_bio_PrivateKey(key_bio, NULL, NULL, NULL)))
972 goto err;
973
974 ERR_clear_error();
975 if (!TEST_int_ge(BIO_seek(key_bio, 0), 0))
976 goto err;
977 ok = TEST_ptr(pkey = d2i_PrivateKey_bio(key_bio, NULL));
978 TEST_int_eq(ERR_peek_error(), 0);
979 test_openssl_errors();
980
981 err:
982 EVP_PKEY_free(pkey);
983 BIO_free(key_bio);
984 OSSL_PROVIDER_unload(provider);
985
986 return ok;
987 }
988
test_PEM_read_bio_negative(int testid)989 static int test_PEM_read_bio_negative(int testid)
990 {
991 int ok = 0;
992 OSSL_PROVIDER *provider = NULL;
993 BIO *key_bio = NULL;
994 EVP_PKEY *pkey = NULL;
995
996 if (!TEST_ptr(key_bio = BIO_new_mem_buf(keydata[testid].kder, keydata[testid].size)))
997 goto err;
998 ERR_clear_error();
999 if (!TEST_ptr_null(pkey = PEM_read_bio_PrivateKey(key_bio, NULL, NULL, NULL)))
1000 goto err;
1001 if (!TEST_int_ne(ERR_peek_error(), 0))
1002 goto err;
1003 if (!TEST_ptr(provider = OSSL_PROVIDER_load(NULL, "default")))
1004 goto err;
1005 if (!TEST_int_ge(BIO_seek(key_bio, 0), 0))
1006 goto err;
1007 ERR_clear_error();
1008 if (!TEST_ptr_null(pkey = PEM_read_bio_PrivateKey(key_bio, NULL, NULL, NULL)))
1009 goto err;
1010 if (!TEST_int_ne(ERR_peek_error(), 0))
1011 goto err;
1012
1013 ok = 1;
1014
1015 err:
1016 test_openssl_errors();
1017 EVP_PKEY_free(pkey);
1018 BIO_free(key_bio);
1019 OSSL_PROVIDER_unload(provider);
1020
1021 return ok;
1022 }
1023
test_PEM_read_bio_negative_wrong_password(int testid)1024 static int test_PEM_read_bio_negative_wrong_password(int testid)
1025 {
1026 int ok = 0;
1027 OSSL_PROVIDER *provider = OSSL_PROVIDER_load(NULL, "default");
1028 EVP_PKEY *read_pkey = NULL;
1029 EVP_PKEY *write_pkey = EVP_RSA_gen(1024);
1030 BIO *key_bio = BIO_new(BIO_s_mem());
1031 const UI_METHOD *undo_ui_method = NULL;
1032 const UI_METHOD *ui_method = NULL;
1033 if (testid > 0)
1034 ui_method = UI_null();
1035
1036 if (!TEST_ptr(provider))
1037 goto err;
1038 if (!TEST_ptr(key_bio))
1039 goto err;
1040 if (!TEST_ptr(write_pkey))
1041 goto err;
1042 undo_ui_method = UI_get_default_method();
1043 UI_set_default_method(ui_method);
1044
1045 if (/* Output Encrypted private key in PEM form */
1046 !TEST_true(PEM_write_bio_PrivateKey(key_bio, write_pkey, EVP_aes_256_cbc(),
1047 NULL, 0, NULL, "pass")))
1048 goto err;
1049
1050 ERR_clear_error();
1051 read_pkey = PEM_read_bio_PrivateKey(key_bio, NULL, NULL, NULL);
1052 if (!TEST_ptr_null(read_pkey))
1053 goto err;
1054
1055 if (!TEST_int_eq(ERR_GET_REASON(ERR_get_error()), PEM_R_PROBLEMS_GETTING_PASSWORD))
1056 goto err;
1057 ok = 1;
1058
1059 err:
1060 test_openssl_errors();
1061 EVP_PKEY_free(read_pkey);
1062 EVP_PKEY_free(write_pkey);
1063 BIO_free(key_bio);
1064 OSSL_PROVIDER_unload(provider);
1065 UI_set_default_method(undo_ui_method);
1066
1067 return ok;
1068 }
1069
do_fromdata_key_is_equal(const OSSL_PARAM params[],const EVP_PKEY * expected,const char * type)1070 static int do_fromdata_key_is_equal(const OSSL_PARAM params[],
1071 const EVP_PKEY *expected, const char *type)
1072 {
1073 EVP_PKEY_CTX *ctx = NULL;
1074 EVP_PKEY *pkey = NULL;
1075 int ret;
1076
1077 ret = TEST_ptr(ctx = EVP_PKEY_CTX_new_from_name(mainctx, type, NULL))
1078 && TEST_int_eq(EVP_PKEY_fromdata_init(ctx), 1)
1079 && TEST_int_eq(EVP_PKEY_fromdata(ctx, &pkey,
1080 EVP_PKEY_KEYPAIR,
1081 (OSSL_PARAM *)params),
1082 1)
1083 && TEST_true(EVP_PKEY_eq(pkey, expected));
1084 EVP_PKEY_CTX_free(ctx);
1085 EVP_PKEY_free(pkey);
1086 return ret;
1087 }
1088
1089 #ifndef OPENSSL_NO_DSA
1090 /*
1091 * This data was generated using:
1092 * > openssl genpkey \
1093 * -genparam -algorithm DSA -pkeyopt type:fips186_4 -text \
1094 * -pkeyopt gindex:5 -out dsa_param.pem
1095 * > openssl genpkey \
1096 * -paramfile dsa_param.pem -pkeyopt type:fips186_4 -out dsa_priv.pem
1097 */
1098 static const unsigned char dsa_key[] = {
1099 0x30, 0x82, 0x03, 0x4e, 0x02, 0x01, 0x00, 0x02, 0x82, 0x01,
1100 0x01, 0x00, 0xda, 0xb3, 0x46, 0x4d, 0x54, 0x57, 0xc7, 0xb4,
1101 0x61, 0xa0, 0x6f, 0x66, 0x17, 0xda, 0xeb, 0x90, 0xf0, 0xa3,
1102 0xd1, 0x29, 0xc9, 0x5f, 0xf2, 0x21, 0x3d, 0x85, 0xa3, 0x4a,
1103 0xf0, 0xf8, 0x36, 0x39, 0x1b, 0xe3, 0xee, 0x37, 0x70, 0x06,
1104 0x9b, 0xe8, 0xe3, 0x0a, 0xd2, 0xf1, 0xf6, 0xc4, 0x42, 0x23,
1105 0x1f, 0x74, 0x78, 0xc2, 0x16, 0xf5, 0xce, 0xd6, 0xab, 0xa0,
1106 0xc6, 0xe8, 0x99, 0x3d, 0xf8, 0x8b, 0xfb, 0x47, 0xf8, 0x5e,
1107 0x05, 0x68, 0x6d, 0x8b, 0xa8, 0xad, 0xa1, 0xc2, 0x3a, 0x4e,
1108 0xe0, 0xad, 0xec, 0x38, 0x75, 0x21, 0x55, 0x22, 0xce, 0xa2,
1109 0xe9, 0xe5, 0x3b, 0xd7, 0x44, 0xeb, 0x5a, 0x03, 0x59, 0xa0,
1110 0xc5, 0x7a, 0x92, 0x59, 0x7d, 0x7a, 0x07, 0x80, 0xfc, 0x4e,
1111 0xf8, 0x56, 0x7e, 0xf1, 0x06, 0xe0, 0xba, 0xb2, 0xe7, 0x5b,
1112 0x22, 0x55, 0xee, 0x4b, 0x42, 0x61, 0x67, 0x2c, 0x43, 0x9a,
1113 0x38, 0x2b, 0x17, 0xc2, 0x62, 0x12, 0x8b, 0x0b, 0x22, 0x8c,
1114 0x0c, 0x1c, 0x1c, 0x92, 0xb1, 0xec, 0x70, 0xce, 0x0f, 0x8c,
1115 0xff, 0x8d, 0x21, 0xf9, 0x19, 0x68, 0x4d, 0x32, 0x59, 0x78,
1116 0x42, 0x1d, 0x0c, 0xc5, 0x1a, 0xcb, 0x28, 0xe2, 0xc1, 0x1a,
1117 0x35, 0xf1, 0x42, 0x0a, 0x19, 0x39, 0xfa, 0x83, 0xd1, 0xb4,
1118 0xaa, 0x69, 0x0f, 0xc2, 0x8e, 0xf9, 0x59, 0x2c, 0xee, 0x11,
1119 0xfc, 0x3e, 0x4b, 0x44, 0xfb, 0x9a, 0x32, 0xc8, 0x78, 0x23,
1120 0x56, 0x85, 0x49, 0x21, 0x43, 0x12, 0x79, 0xbd, 0xa0, 0x70,
1121 0x47, 0x2f, 0xae, 0xb6, 0xd7, 0x6c, 0xc6, 0x07, 0x76, 0xa9,
1122 0x8a, 0xa2, 0x16, 0x02, 0x89, 0x1f, 0x1a, 0xd1, 0xa2, 0x96,
1123 0x56, 0xd1, 0x1f, 0x10, 0xe1, 0xe5, 0x9f, 0x3f, 0xdd, 0x09,
1124 0x0c, 0x40, 0x90, 0x71, 0xef, 0x14, 0x41, 0x02, 0x82, 0x3a,
1125 0x6b, 0xe1, 0xf8, 0x2c, 0x5d, 0xbe, 0xfd, 0x1b, 0x02, 0x1d,
1126 0x00, 0xe0, 0x20, 0xe0, 0x7c, 0x02, 0x16, 0xa7, 0x6c, 0x6a,
1127 0x19, 0xba, 0xd5, 0x83, 0x73, 0xf3, 0x7d, 0x31, 0xef, 0xa7,
1128 0xe1, 0x5d, 0x5b, 0x7f, 0xf3, 0xfc, 0xda, 0x84, 0x31, 0x02,
1129 0x82, 0x01, 0x01, 0x00, 0x83, 0xdb, 0xa1, 0xbc, 0x3e, 0xc7,
1130 0x29, 0xa5, 0x6a, 0x5c, 0x2c, 0xe8, 0x7a, 0x8c, 0x7e, 0xe8,
1131 0xb8, 0x3e, 0x13, 0x47, 0xcd, 0x36, 0x7e, 0x79, 0x30, 0x7a,
1132 0x28, 0x03, 0xd3, 0xd4, 0xd2, 0xe3, 0xee, 0x3b, 0x46, 0xda,
1133 0xe0, 0x71, 0xe6, 0xcf, 0x46, 0x86, 0x0a, 0x37, 0x57, 0xb6,
1134 0xe9, 0xcf, 0xa1, 0x78, 0x19, 0xb8, 0x72, 0x9f, 0x30, 0x8c,
1135 0x2a, 0x04, 0x7c, 0x2f, 0x0c, 0x27, 0xa7, 0xb3, 0x23, 0xe0,
1136 0x46, 0xf2, 0x75, 0x0c, 0x03, 0x4c, 0xad, 0xfb, 0xc1, 0xcb,
1137 0x28, 0xcd, 0xa0, 0x63, 0xdb, 0x44, 0x88, 0xe0, 0xda, 0x6c,
1138 0x5b, 0x89, 0xb2, 0x5b, 0x40, 0x6d, 0xeb, 0x78, 0x7a, 0xd5,
1139 0xaf, 0x40, 0x52, 0x46, 0x63, 0x92, 0x13, 0x0d, 0xee, 0xee,
1140 0xf9, 0x53, 0xca, 0x2d, 0x4e, 0x3b, 0x13, 0xd8, 0x0f, 0x50,
1141 0xd0, 0x44, 0x57, 0x67, 0x0f, 0x45, 0x8f, 0x21, 0x30, 0x97,
1142 0x9e, 0x80, 0xd9, 0xd0, 0x91, 0xb7, 0xc9, 0x5a, 0x69, 0xda,
1143 0xeb, 0xd5, 0xea, 0x37, 0xf6, 0xb3, 0xbe, 0x1f, 0x24, 0xf1,
1144 0x55, 0x14, 0x28, 0x05, 0xb5, 0xd8, 0x84, 0x0f, 0x62, 0x85,
1145 0xaa, 0xec, 0x77, 0x64, 0xfd, 0x80, 0x7c, 0x41, 0x00, 0x88,
1146 0xa3, 0x79, 0x7d, 0x4f, 0x6f, 0xe3, 0x76, 0xf4, 0xb5, 0x97,
1147 0xb7, 0xeb, 0x67, 0x28, 0xba, 0x07, 0x1a, 0x59, 0x32, 0xc1,
1148 0x53, 0xd9, 0x05, 0x6b, 0x63, 0x93, 0xce, 0xa1, 0xd9, 0x7a,
1149 0xb2, 0xff, 0x1c, 0x12, 0x0a, 0x9a, 0xe5, 0x51, 0x1e, 0xba,
1150 0xfc, 0x95, 0x2e, 0x28, 0xa9, 0xfc, 0x4c, 0xed, 0x7b, 0x05,
1151 0xca, 0x67, 0xe0, 0x2d, 0xd7, 0x54, 0xb3, 0x05, 0x1c, 0x23,
1152 0x2b, 0x35, 0x2e, 0x19, 0x48, 0x59, 0x0e, 0x58, 0xa8, 0x01,
1153 0x56, 0xfb, 0x78, 0x90, 0xba, 0x08, 0x77, 0x94, 0x45, 0x05,
1154 0x13, 0xc7, 0x6b, 0x96, 0xd2, 0xa3, 0xa6, 0x01, 0x9f, 0x34,
1155 0x02, 0x82, 0x01, 0x00, 0x16, 0x1a, 0xb4, 0x6d, 0x9f, 0x16,
1156 0x6c, 0xcc, 0x91, 0x66, 0xfe, 0x30, 0xeb, 0x8e, 0x44, 0xba,
1157 0x2b, 0x7a, 0xc9, 0xa8, 0x95, 0xf2, 0xa6, 0x38, 0xd8, 0xaf,
1158 0x3e, 0x91, 0x68, 0xe8, 0x52, 0xf3, 0x97, 0x37, 0x70, 0xf2,
1159 0x47, 0xa3, 0xf4, 0x62, 0x26, 0xf5, 0x3b, 0x71, 0x52, 0x50,
1160 0x15, 0x9c, 0x6d, 0xa6, 0x6d, 0x92, 0x4c, 0x48, 0x76, 0x31,
1161 0x54, 0x48, 0xa5, 0x99, 0x7a, 0xd4, 0x61, 0xf7, 0x21, 0x44,
1162 0xe7, 0xd8, 0x82, 0xc3, 0x50, 0xd3, 0xd9, 0xd4, 0x66, 0x20,
1163 0xab, 0x70, 0x4c, 0x97, 0x9b, 0x8d, 0xac, 0x1f, 0x78, 0x27,
1164 0x1e, 0x47, 0xf8, 0x3b, 0xd1, 0x55, 0x73, 0xf3, 0xb4, 0x8e,
1165 0x6d, 0x45, 0x40, 0x54, 0xc6, 0xd8, 0x95, 0x15, 0x27, 0xb7,
1166 0x5f, 0x65, 0xaa, 0xcb, 0x24, 0xc9, 0x49, 0x87, 0x32, 0xad,
1167 0xcb, 0xf8, 0x35, 0x63, 0x56, 0x72, 0x7c, 0x4e, 0x6c, 0xad,
1168 0x5f, 0x26, 0x8c, 0xd2, 0x80, 0x41, 0xaf, 0x88, 0x23, 0x20,
1169 0x03, 0xa4, 0xd5, 0x3c, 0x53, 0x54, 0xb0, 0x3d, 0xed, 0x0e,
1170 0x9e, 0x53, 0x0a, 0x63, 0x5f, 0xfd, 0x28, 0x57, 0x09, 0x07,
1171 0x73, 0xf4, 0x0c, 0xd4, 0x71, 0x5d, 0x6b, 0xa0, 0xd7, 0x86,
1172 0x99, 0x29, 0x9b, 0xca, 0xfb, 0xcc, 0xd6, 0x2f, 0xfe, 0xbe,
1173 0x94, 0xef, 0x1a, 0x0e, 0x55, 0x84, 0xa7, 0xaf, 0x7b, 0xfa,
1174 0xed, 0x77, 0x61, 0x28, 0x22, 0xee, 0x6b, 0x11, 0xdd, 0xb0,
1175 0x17, 0x1e, 0x06, 0xe4, 0x29, 0x4c, 0xc2, 0x3f, 0xd6, 0x75,
1176 0xb6, 0x08, 0x04, 0x55, 0x13, 0x48, 0x4f, 0x44, 0xea, 0x8d,
1177 0xaf, 0xcb, 0xac, 0x22, 0xc4, 0x6a, 0xb3, 0x86, 0xe5, 0x47,
1178 0xa9, 0xb5, 0x72, 0x17, 0x23, 0x11, 0x81, 0x7f, 0x00, 0x00,
1179 0x67, 0x5c, 0xf4, 0x58, 0xcc, 0xe2, 0x46, 0xce, 0xf5, 0x6d,
1180 0xd8, 0x18, 0x91, 0xc4, 0x20, 0xbf, 0x07, 0x48, 0x45, 0xfd,
1181 0x02, 0x1c, 0x2f, 0x68, 0x44, 0xcb, 0xfb, 0x6b, 0xcb, 0x8d,
1182 0x02, 0x49, 0x7c, 0xee, 0xd2, 0xa6, 0xd3, 0x43, 0xb8, 0xa4,
1183 0x09, 0xb7, 0xc1, 0xd4, 0x4b, 0xc3, 0x66, 0xa7, 0xe0, 0x21
1184 };
1185 static const unsigned char dsa_p[] = {
1186 0x00, 0xda, 0xb3, 0x46, 0x4d, 0x54, 0x57, 0xc7, 0xb4, 0x61, 0xa0, 0x6f, 0x66, 0x17, 0xda,
1187 0xeb, 0x90, 0xf0, 0xa3, 0xd1, 0x29, 0xc9, 0x5f, 0xf2, 0x21, 0x3d, 0x85, 0xa3, 0x4a, 0xf0,
1188 0xf8, 0x36, 0x39, 0x1b, 0xe3, 0xee, 0x37, 0x70, 0x06, 0x9b, 0xe8, 0xe3, 0x0a, 0xd2, 0xf1,
1189 0xf6, 0xc4, 0x42, 0x23, 0x1f, 0x74, 0x78, 0xc2, 0x16, 0xf5, 0xce, 0xd6, 0xab, 0xa0, 0xc6,
1190 0xe8, 0x99, 0x3d, 0xf8, 0x8b, 0xfb, 0x47, 0xf8, 0x5e, 0x05, 0x68, 0x6d, 0x8b, 0xa8, 0xad,
1191 0xa1, 0xc2, 0x3a, 0x4e, 0xe0, 0xad, 0xec, 0x38, 0x75, 0x21, 0x55, 0x22, 0xce, 0xa2, 0xe9,
1192 0xe5, 0x3b, 0xd7, 0x44, 0xeb, 0x5a, 0x03, 0x59, 0xa0, 0xc5, 0x7a, 0x92, 0x59, 0x7d, 0x7a,
1193 0x07, 0x80, 0xfc, 0x4e, 0xf8, 0x56, 0x7e, 0xf1, 0x06, 0xe0, 0xba, 0xb2, 0xe7, 0x5b, 0x22,
1194 0x55, 0xee, 0x4b, 0x42, 0x61, 0x67, 0x2c, 0x43, 0x9a, 0x38, 0x2b, 0x17, 0xc2, 0x62, 0x12,
1195 0x8b, 0x0b, 0x22, 0x8c, 0x0c, 0x1c, 0x1c, 0x92, 0xb1, 0xec, 0x70, 0xce, 0x0f, 0x8c, 0xff,
1196 0x8d, 0x21, 0xf9, 0x19, 0x68, 0x4d, 0x32, 0x59, 0x78, 0x42, 0x1d, 0x0c, 0xc5, 0x1a, 0xcb,
1197 0x28, 0xe2, 0xc1, 0x1a, 0x35, 0xf1, 0x42, 0x0a, 0x19, 0x39, 0xfa, 0x83, 0xd1, 0xb4, 0xaa,
1198 0x69, 0x0f, 0xc2, 0x8e, 0xf9, 0x59, 0x2c, 0xee, 0x11, 0xfc, 0x3e, 0x4b, 0x44, 0xfb, 0x9a,
1199 0x32, 0xc8, 0x78, 0x23, 0x56, 0x85, 0x49, 0x21, 0x43, 0x12, 0x79, 0xbd, 0xa0, 0x70, 0x47,
1200 0x2f, 0xae, 0xb6, 0xd7, 0x6c, 0xc6, 0x07, 0x76, 0xa9, 0x8a, 0xa2, 0x16, 0x02, 0x89, 0x1f,
1201 0x1a, 0xd1, 0xa2, 0x96, 0x56, 0xd1, 0x1f, 0x10, 0xe1, 0xe5, 0x9f, 0x3f, 0xdd, 0x09, 0x0c,
1202 0x40, 0x90, 0x71, 0xef, 0x14, 0x41, 0x02, 0x82, 0x3a, 0x6b, 0xe1, 0xf8, 0x2c, 0x5d, 0xbe,
1203 0xfd, 0x1b
1204 };
1205 static const unsigned char dsa_q[] = {
1206 0x00, 0xe0, 0x20, 0xe0, 0x7c, 0x02, 0x16, 0xa7, 0x6c, 0x6a, 0x19, 0xba, 0xd5, 0x83, 0x73,
1207 0xf3, 0x7d, 0x31, 0xef, 0xa7, 0xe1, 0x5d, 0x5b, 0x7f, 0xf3, 0xfc, 0xda, 0x84, 0x31
1208 };
1209 static const unsigned char dsa_g[] = {
1210 0x00, 0x83, 0xdb, 0xa1, 0xbc, 0x3e, 0xc7, 0x29, 0xa5, 0x6a, 0x5c, 0x2c, 0xe8, 0x7a, 0x8c,
1211 0x7e, 0xe8, 0xb8, 0x3e, 0x13, 0x47, 0xcd, 0x36, 0x7e, 0x79, 0x30, 0x7a, 0x28, 0x03, 0xd3,
1212 0xd4, 0xd2, 0xe3, 0xee, 0x3b, 0x46, 0xda, 0xe0, 0x71, 0xe6, 0xcf, 0x46, 0x86, 0x0a, 0x37,
1213 0x57, 0xb6, 0xe9, 0xcf, 0xa1, 0x78, 0x19, 0xb8, 0x72, 0x9f, 0x30, 0x8c, 0x2a, 0x04, 0x7c,
1214 0x2f, 0x0c, 0x27, 0xa7, 0xb3, 0x23, 0xe0, 0x46, 0xf2, 0x75, 0x0c, 0x03, 0x4c, 0xad, 0xfb,
1215 0xc1, 0xcb, 0x28, 0xcd, 0xa0, 0x63, 0xdb, 0x44, 0x88, 0xe0, 0xda, 0x6c, 0x5b, 0x89, 0xb2,
1216 0x5b, 0x40, 0x6d, 0xeb, 0x78, 0x7a, 0xd5, 0xaf, 0x40, 0x52, 0x46, 0x63, 0x92, 0x13, 0x0d,
1217 0xee, 0xee, 0xf9, 0x53, 0xca, 0x2d, 0x4e, 0x3b, 0x13, 0xd8, 0x0f, 0x50, 0xd0, 0x44, 0x57,
1218 0x67, 0x0f, 0x45, 0x8f, 0x21, 0x30, 0x97, 0x9e, 0x80, 0xd9, 0xd0, 0x91, 0xb7, 0xc9, 0x5a,
1219 0x69, 0xda, 0xeb, 0xd5, 0xea, 0x37, 0xf6, 0xb3, 0xbe, 0x1f, 0x24, 0xf1, 0x55, 0x14, 0x28,
1220 0x05, 0xb5, 0xd8, 0x84, 0x0f, 0x62, 0x85, 0xaa, 0xec, 0x77, 0x64, 0xfd, 0x80, 0x7c, 0x41,
1221 0x00, 0x88, 0xa3, 0x79, 0x7d, 0x4f, 0x6f, 0xe3, 0x76, 0xf4, 0xb5, 0x97, 0xb7, 0xeb, 0x67,
1222 0x28, 0xba, 0x07, 0x1a, 0x59, 0x32, 0xc1, 0x53, 0xd9, 0x05, 0x6b, 0x63, 0x93, 0xce, 0xa1,
1223 0xd9, 0x7a, 0xb2, 0xff, 0x1c, 0x12, 0x0a, 0x9a, 0xe5, 0x51, 0x1e, 0xba, 0xfc, 0x95, 0x2e,
1224 0x28, 0xa9, 0xfc, 0x4c, 0xed, 0x7b, 0x05, 0xca, 0x67, 0xe0, 0x2d, 0xd7, 0x54, 0xb3, 0x05,
1225 0x1c, 0x23, 0x2b, 0x35, 0x2e, 0x19, 0x48, 0x59, 0x0e, 0x58, 0xa8, 0x01, 0x56, 0xfb, 0x78,
1226 0x90, 0xba, 0x08, 0x77, 0x94, 0x45, 0x05, 0x13, 0xc7, 0x6b, 0x96, 0xd2, 0xa3, 0xa6, 0x01,
1227 0x9f, 0x34
1228 };
1229 static const unsigned char dsa_priv[] = {
1230 0x2f, 0x68, 0x44, 0xcb, 0xfb, 0x6b, 0xcb, 0x8d, 0x02, 0x49, 0x7c, 0xee, 0xd2, 0xa6, 0xd3,
1231 0x43, 0xb8, 0xa4, 0x09, 0xb7, 0xc1, 0xd4, 0x4b, 0xc3, 0x66, 0xa7, 0xe0, 0x21
1232 };
1233 static const unsigned char dsa_pub[] = {
1234 0x16, 0x1a, 0xb4, 0x6d, 0x9f, 0x16, 0x6c, 0xcc, 0x91, 0x66, 0xfe, 0x30, 0xeb, 0x8e, 0x44,
1235 0xba, 0x2b, 0x7a, 0xc9, 0xa8, 0x95, 0xf2, 0xa6, 0x38, 0xd8, 0xaf, 0x3e, 0x91, 0x68, 0xe8,
1236 0x52, 0xf3, 0x97, 0x37, 0x70, 0xf2, 0x47, 0xa3, 0xf4, 0x62, 0x26, 0xf5, 0x3b, 0x71, 0x52,
1237 0x50, 0x15, 0x9c, 0x6d, 0xa6, 0x6d, 0x92, 0x4c, 0x48, 0x76, 0x31, 0x54, 0x48, 0xa5, 0x99,
1238 0x7a, 0xd4, 0x61, 0xf7, 0x21, 0x44, 0xe7, 0xd8, 0x82, 0xc3, 0x50, 0xd3, 0xd9, 0xd4, 0x66,
1239 0x20, 0xab, 0x70, 0x4c, 0x97, 0x9b, 0x8d, 0xac, 0x1f, 0x78, 0x27, 0x1e, 0x47, 0xf8, 0x3b,
1240 0xd1, 0x55, 0x73, 0xf3, 0xb4, 0x8e, 0x6d, 0x45, 0x40, 0x54, 0xc6, 0xd8, 0x95, 0x15, 0x27,
1241 0xb7, 0x5f, 0x65, 0xaa, 0xcb, 0x24, 0xc9, 0x49, 0x87, 0x32, 0xad, 0xcb, 0xf8, 0x35, 0x63,
1242 0x56, 0x72, 0x7c, 0x4e, 0x6c, 0xad, 0x5f, 0x26, 0x8c, 0xd2, 0x80, 0x41, 0xaf, 0x88, 0x23,
1243 0x20, 0x03, 0xa4, 0xd5, 0x3c, 0x53, 0x54, 0xb0, 0x3d, 0xed, 0x0e, 0x9e, 0x53, 0x0a, 0x63,
1244 0x5f, 0xfd, 0x28, 0x57, 0x09, 0x07, 0x73, 0xf4, 0x0c, 0xd4, 0x71, 0x5d, 0x6b, 0xa0, 0xd7,
1245 0x86, 0x99, 0x29, 0x9b, 0xca, 0xfb, 0xcc, 0xd6, 0x2f, 0xfe, 0xbe, 0x94, 0xef, 0x1a, 0x0e,
1246 0x55, 0x84, 0xa7, 0xaf, 0x7b, 0xfa, 0xed, 0x77, 0x61, 0x28, 0x22, 0xee, 0x6b, 0x11, 0xdd,
1247 0xb0, 0x17, 0x1e, 0x06, 0xe4, 0x29, 0x4c, 0xc2, 0x3f, 0xd6, 0x75, 0xb6, 0x08, 0x04, 0x55,
1248 0x13, 0x48, 0x4f, 0x44, 0xea, 0x8d, 0xaf, 0xcb, 0xac, 0x22, 0xc4, 0x6a, 0xb3, 0x86, 0xe5,
1249 0x47, 0xa9, 0xb5, 0x72, 0x17, 0x23, 0x11, 0x81, 0x7f, 0x00, 0x00, 0x67, 0x5c, 0xf4, 0x58,
1250 0xcc, 0xe2, 0x46, 0xce, 0xf5, 0x6d, 0xd8, 0x18, 0x91, 0xc4, 0x20, 0xbf, 0x07, 0x48, 0x45,
1251 0xfd
1252 };
1253 #endif
1254
1255 #ifndef OPENSSL_NO_DSA
do_check_params(OSSL_PARAM key_params[],int expected)1256 static int do_check_params(OSSL_PARAM key_params[], int expected)
1257 {
1258 EVP_PKEY_CTX *gen_ctx = NULL, *check_ctx = NULL;
1259 EVP_PKEY *pkey = NULL;
1260 int ret;
1261
1262 ret = TEST_ptr(gen_ctx = EVP_PKEY_CTX_new_from_name(mainctx, "DSA", NULL))
1263 && TEST_int_eq(EVP_PKEY_fromdata_init(gen_ctx), 1)
1264 && TEST_int_eq(EVP_PKEY_fromdata(gen_ctx, &pkey,
1265 EVP_PKEY_KEYPAIR, key_params),
1266 1)
1267 && TEST_ptr(check_ctx = EVP_PKEY_CTX_new_from_pkey(mainctx, pkey,
1268 NULL))
1269 && TEST_int_eq(EVP_PKEY_param_check(check_ctx), expected);
1270 EVP_PKEY_CTX_free(check_ctx);
1271 EVP_PKEY_CTX_free(gen_ctx);
1272 EVP_PKEY_free(pkey);
1273 return ret;
1274 }
1275
do_check_bn(OSSL_PARAM params[],const char * key,const unsigned char * expected,size_t expected_len)1276 static int do_check_bn(OSSL_PARAM params[], const char *key,
1277 const unsigned char *expected, size_t expected_len)
1278 {
1279 OSSL_PARAM *p;
1280 BIGNUM *bn = NULL;
1281 unsigned char buffer[256 + 1];
1282 int ret, len;
1283
1284 ret = TEST_ptr(p = OSSL_PARAM_locate(params, key))
1285 && TEST_true(OSSL_PARAM_get_BN(p, &bn))
1286 && TEST_int_gt(len = BN_bn2binpad(bn, buffer, expected_len), 0)
1287 && TEST_mem_eq(expected, expected_len, buffer, len);
1288 BN_free(bn);
1289 return ret;
1290 }
1291
do_check_int(OSSL_PARAM params[],const char * key,int expected)1292 static int do_check_int(OSSL_PARAM params[], const char *key, int expected)
1293 {
1294 OSSL_PARAM *p;
1295 int val = 0;
1296
1297 return TEST_ptr(p = OSSL_PARAM_locate(params, key))
1298 && TEST_true(OSSL_PARAM_get_int(p, &val))
1299 && TEST_int_eq(val, expected);
1300 }
1301
test_dsa_todata(void)1302 static int test_dsa_todata(void)
1303 {
1304 EVP_PKEY *pkey = NULL;
1305 OSSL_PARAM *to_params = NULL, *all_params = NULL;
1306 OSSL_PARAM gen_params[4];
1307 int ret = 0;
1308 const unsigned char *pkeydata = dsa_key;
1309
1310 unsigned char dsa_seed[] = {
1311 0xbc, 0x8a, 0x81, 0x64, 0x9e, 0x9d, 0x63, 0xa7, 0xa3, 0x5d, 0x87, 0xdd,
1312 0x32, 0xf3, 0xc1, 0x9f, 0x18, 0x22, 0xeb, 0x73, 0x63, 0xad, 0x5e, 0x7b,
1313 0x90, 0xc1, 0xe3, 0xe0
1314 };
1315 int dsa_pcounter = 319;
1316 int dsa_gindex = 5;
1317
1318 gen_params[0] = OSSL_PARAM_construct_octet_string(OSSL_PKEY_PARAM_FFC_SEED,
1319 (void *)dsa_seed,
1320 sizeof(dsa_seed));
1321 gen_params[1] = OSSL_PARAM_construct_int(OSSL_PKEY_PARAM_FFC_GINDEX,
1322 &dsa_gindex);
1323 gen_params[2] = OSSL_PARAM_construct_int(OSSL_PKEY_PARAM_FFC_PCOUNTER,
1324 &dsa_pcounter);
1325 gen_params[3] = OSSL_PARAM_construct_end();
1326
1327 if (!TEST_ptr(pkey = d2i_AutoPrivateKey_ex(NULL, &pkeydata, sizeof(dsa_key),
1328 mainctx, NULL))
1329 || !TEST_int_eq(EVP_PKEY_todata(pkey, EVP_PKEY_KEYPAIR, &to_params), 1)
1330 || !do_check_bn(to_params, OSSL_PKEY_PARAM_FFC_P, dsa_p, sizeof(dsa_p))
1331 || !do_check_bn(to_params, OSSL_PKEY_PARAM_FFC_Q, dsa_q, sizeof(dsa_q))
1332 || !do_check_bn(to_params, OSSL_PKEY_PARAM_FFC_G, dsa_g, sizeof(dsa_g))
1333 || !do_check_bn(to_params, OSSL_PKEY_PARAM_PUB_KEY, dsa_pub,
1334 sizeof(dsa_pub))
1335 || !do_check_bn(to_params, OSSL_PKEY_PARAM_PRIV_KEY, dsa_priv,
1336 sizeof(dsa_priv))
1337 || !do_check_int(to_params, OSSL_PKEY_PARAM_FFC_GINDEX, -1)
1338 || !do_check_int(to_params, OSSL_PKEY_PARAM_FFC_PCOUNTER, -1)
1339 || !do_check_int(to_params, OSSL_PKEY_PARAM_FFC_H, 0)
1340 || !do_check_int(to_params, OSSL_PKEY_PARAM_FFC_VALIDATE_PQ, 1)
1341 || !do_check_int(to_params, OSSL_PKEY_PARAM_FFC_VALIDATE_G, 1)
1342 || !do_check_int(to_params, OSSL_PKEY_PARAM_FFC_VALIDATE_LEGACY, 0)
1343 || !TEST_ptr_null(OSSL_PARAM_locate(to_params, OSSL_PKEY_PARAM_FFC_SEED)))
1344 goto err;
1345
1346 if (!do_fromdata_key_is_equal(to_params, pkey, "DSA"))
1347 goto err;
1348
1349 if (!TEST_ptr(all_params = OSSL_PARAM_merge(to_params, gen_params))
1350 || !do_check_params(all_params, 1))
1351 goto err;
1352 gen_params[1] = OSSL_PARAM_construct_int(OSSL_PKEY_PARAM_FFC_GINDEX,
1353 &dsa_gindex);
1354 gen_params[2] = OSSL_PARAM_construct_int(OSSL_PKEY_PARAM_FFC_PCOUNTER,
1355 &dsa_pcounter);
1356 /*
1357 * Check that modifying the shallow copy values used in OSSL_PARAM_merge()
1358 * results in an invalid key. This also verifies that the fips186-4
1359 * validation code is running.
1360 */
1361 dsa_gindex++;
1362 if (!do_check_params(all_params, 0))
1363 goto err;
1364 dsa_gindex--;
1365 dsa_pcounter++;
1366 if (!do_check_params(all_params, 0))
1367 goto err;
1368 dsa_pcounter--;
1369 dsa_seed[0] = 0xb0;
1370 if (!do_check_params(all_params, 0))
1371 goto err;
1372
1373 ret = 1;
1374 err:
1375 EVP_PKEY_free(pkey);
1376 OSSL_PARAM_free(all_params);
1377 OSSL_PARAM_free(to_params);
1378 return ret;
1379 }
1380
1381 /*
1382 * Test that OSSL_PKEY_PARAM_FFC_DIGEST_PROPS is set properly when using fromdata
1383 * This test:
1384 * checks for failure when the property query is bad (tstid == 0)
1385 * checks for success when the property query is valid (tstid == 1)
1386 */
test_dsa_fromdata_digest_prop(int tstid)1387 static int test_dsa_fromdata_digest_prop(int tstid)
1388 {
1389 EVP_PKEY_CTX *ctx = NULL, *gctx = NULL;
1390 EVP_PKEY *pkey = NULL, *pkey2 = NULL;
1391 OSSL_PARAM params[4], *p = params;
1392 int ret = 0;
1393 int expected = (tstid == 0 ? 0 : 1);
1394 unsigned int pbits = 512; /* minimum allowed for speed */
1395
1396 *p++ = OSSL_PARAM_construct_uint(OSSL_PKEY_PARAM_FFC_PBITS, &pbits);
1397 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_FFC_DIGEST, "SHA512", 0);
1398 /* Setting a bad prop query here should fail during paramgen - when it tries to do a fetch */
1399 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_FFC_DIGEST_PROPS,
1400 tstid == 0 ? "provider=unknown" : "provider=default", 0);
1401 *p++ = OSSL_PARAM_construct_end();
1402
1403 if (!TEST_ptr(ctx = EVP_PKEY_CTX_new_from_name(mainctx, "DSA", NULL))
1404 || !TEST_int_eq(EVP_PKEY_fromdata_init(ctx), 1)
1405 || !TEST_int_eq(EVP_PKEY_fromdata(ctx, &pkey, EVP_PKEY_KEY_PARAMETERS, params), 1))
1406 goto err;
1407
1408 if (!TEST_ptr(gctx = EVP_PKEY_CTX_new_from_pkey(mainctx, pkey, NULL))
1409 || !TEST_int_eq(EVP_PKEY_paramgen_init(gctx), 1)
1410 || !TEST_int_eq(EVP_PKEY_paramgen(gctx, &pkey2), expected))
1411 goto err;
1412
1413 ret = 1;
1414 err:
1415 EVP_PKEY_free(pkey2);
1416 EVP_PKEY_free(pkey);
1417 EVP_PKEY_CTX_free(ctx);
1418 EVP_PKEY_CTX_free(gctx);
1419 return ret;
1420 }
1421 #endif /* OPENSSL_NO_DSA */
1422
test_pkey_todata_null(void)1423 static int test_pkey_todata_null(void)
1424 {
1425 OSSL_PARAM *params = NULL;
1426 EVP_PKEY *pkey = NULL;
1427 int ret = 0;
1428 const unsigned char *pdata = keydata[0].kder;
1429
1430 ret = TEST_ptr(pkey = d2i_AutoPrivateKey_ex(NULL, &pdata, keydata[0].size,
1431 mainctx, NULL))
1432 && TEST_int_eq(EVP_PKEY_todata(NULL, EVP_PKEY_KEYPAIR, ¶ms), 0)
1433 && TEST_int_eq(EVP_PKEY_todata(pkey, EVP_PKEY_KEYPAIR, NULL), 0);
1434 EVP_PKEY_free(pkey);
1435 return ret;
1436 }
1437
1438 static OSSL_CALLBACK test_pkey_export_cb;
1439
test_pkey_export_cb(const OSSL_PARAM params[],void * arg)1440 static int test_pkey_export_cb(const OSSL_PARAM params[], void *arg)
1441 {
1442 if (arg == NULL)
1443 return 0;
1444 return do_fromdata_key_is_equal(params, (EVP_PKEY *)arg, "RSA");
1445 }
1446
test_pkey_export_null(void)1447 static int test_pkey_export_null(void)
1448 {
1449 EVP_PKEY *pkey = NULL;
1450 int ret = 0;
1451 const unsigned char *pdata = keydata[0].kder;
1452
1453 ret = TEST_ptr(pkey = d2i_AutoPrivateKey_ex(NULL, &pdata, keydata[0].size,
1454 mainctx, NULL))
1455 && TEST_int_eq(EVP_PKEY_export(NULL, EVP_PKEY_KEYPAIR,
1456 test_pkey_export_cb, NULL),
1457 0)
1458 && TEST_int_eq(EVP_PKEY_export(pkey, EVP_PKEY_KEYPAIR, NULL, NULL), 0);
1459 EVP_PKEY_free(pkey);
1460 return ret;
1461 }
1462
test_pkey_export(void)1463 static int test_pkey_export(void)
1464 {
1465 EVP_PKEY *pkey = NULL;
1466 #ifndef OPENSSL_NO_DEPRECATED_3_0
1467 RSA *rsa = NULL;
1468 #endif
1469 int ret = 1;
1470 const unsigned char *pdata = keydata[0].kder;
1471 int pdata_len = keydata[0].size;
1472
1473 if (!TEST_ptr(pkey = d2i_AutoPrivateKey_ex(NULL, &pdata, pdata_len,
1474 mainctx, NULL))
1475 || !TEST_true(EVP_PKEY_export(pkey, EVP_PKEY_KEYPAIR,
1476 test_pkey_export_cb, pkey))
1477 || !TEST_false(EVP_PKEY_export(pkey, EVP_PKEY_KEYPAIR,
1478 test_pkey_export_cb, NULL)))
1479 ret = 0;
1480 EVP_PKEY_free(pkey);
1481
1482 #ifndef OPENSSL_NO_DEPRECATED_3_0
1483 /* Now, try with a legacy key */
1484 pdata = keydata[0].kder;
1485 pdata_len = keydata[0].size;
1486 if (!TEST_ptr(rsa = d2i_RSAPrivateKey(NULL, &pdata, pdata_len))
1487 || !TEST_ptr(pkey = EVP_PKEY_new())
1488 || !TEST_true(EVP_PKEY_assign_RSA(pkey, rsa))
1489 || !TEST_true(EVP_PKEY_export(pkey, EVP_PKEY_KEYPAIR,
1490 test_pkey_export_cb, pkey))
1491 || !TEST_false(EVP_PKEY_export(pkey, EVP_PKEY_KEYPAIR,
1492 test_pkey_export_cb, NULL)))
1493 ret = 0;
1494 EVP_PKEY_free(pkey);
1495 #endif
1496 return ret;
1497 }
1498
test_rsa_pss_sign(void)1499 static int test_rsa_pss_sign(void)
1500 {
1501 EVP_PKEY *pkey = NULL;
1502 EVP_PKEY_CTX *pctx = NULL;
1503 int ret = 0;
1504 const unsigned char *pdata = keydata[0].kder;
1505 const char *mdname = "SHA2-256";
1506 OSSL_PARAM sig_params[3];
1507 unsigned char mdbuf[256 / 8] = { 0 };
1508 int padding = RSA_PKCS1_PSS_PADDING;
1509 unsigned char *sig = NULL;
1510 size_t sig_len = 0;
1511
1512 sig_params[0] = OSSL_PARAM_construct_int(OSSL_PKEY_PARAM_PAD_MODE,
1513 &padding);
1514 sig_params[1] = OSSL_PARAM_construct_utf8_string(OSSL_SIGNATURE_PARAM_DIGEST,
1515 (char *)mdname, 0);
1516 sig_params[2] = OSSL_PARAM_construct_end();
1517
1518 ret = TEST_ptr(pkey = d2i_AutoPrivateKey_ex(NULL, &pdata, keydata[0].size,
1519 mainctx, NULL))
1520 && TEST_ptr(pctx = EVP_PKEY_CTX_new_from_pkey(mainctx, pkey, NULL))
1521 && TEST_int_gt(EVP_PKEY_sign_init_ex(pctx, sig_params), 0)
1522 && TEST_int_gt(EVP_PKEY_sign(pctx, NULL, &sig_len, mdbuf,
1523 sizeof(mdbuf)),
1524 0)
1525 && TEST_int_gt(sig_len, 0)
1526 && TEST_ptr(sig = OPENSSL_malloc(sig_len))
1527 && TEST_int_gt(EVP_PKEY_sign(pctx, sig, &sig_len, mdbuf,
1528 sizeof(mdbuf)),
1529 0);
1530
1531 EVP_PKEY_CTX_free(pctx);
1532 OPENSSL_free(sig);
1533 EVP_PKEY_free(pkey);
1534
1535 return ret;
1536 }
1537
test_evp_md_ctx_dup(void)1538 static int test_evp_md_ctx_dup(void)
1539 {
1540 EVP_MD_CTX *mdctx;
1541 EVP_MD_CTX *copyctx = NULL;
1542 int ret;
1543
1544 /* test copying freshly initialized context */
1545 ret = TEST_ptr(mdctx = EVP_MD_CTX_new())
1546 && TEST_ptr(copyctx = EVP_MD_CTX_dup(mdctx));
1547
1548 EVP_MD_CTX_free(mdctx);
1549 EVP_MD_CTX_free(copyctx);
1550 return ret;
1551 }
1552
test_evp_md_ctx_copy(void)1553 static int test_evp_md_ctx_copy(void)
1554 {
1555 EVP_MD_CTX *mdctx = NULL;
1556 EVP_MD_CTX *copyctx = NULL;
1557 int ret;
1558
1559 /* test copying freshly initialized context */
1560 ret = TEST_ptr(mdctx = EVP_MD_CTX_new())
1561 && TEST_ptr(copyctx = EVP_MD_CTX_new())
1562 && TEST_true(EVP_MD_CTX_copy_ex(copyctx, mdctx));
1563
1564 EVP_MD_CTX_free(mdctx);
1565 EVP_MD_CTX_free(copyctx);
1566 return ret;
1567 }
1568
test_evp_md_ctx_copy2(void)1569 static int test_evp_md_ctx_copy2(void)
1570 {
1571 int ret = 0;
1572 EVP_MD *md = NULL;
1573 OSSL_LIB_CTX *ctx = NULL;
1574 EVP_MD_CTX *inctx = NULL, *outctx = NULL;
1575 void *origin_algctx = NULL;
1576
1577 if (!TEST_ptr(ctx = OSSL_LIB_CTX_new())
1578 || !TEST_ptr(md = EVP_MD_fetch(ctx, "sha256", NULL)))
1579 goto end;
1580
1581 inctx = EVP_MD_CTX_new();
1582 outctx = EVP_MD_CTX_new();
1583
1584 if (!TEST_ptr(inctx) || !TEST_ptr(outctx))
1585 goto end;
1586
1587 /* init inctx and outctx, now the contexts are from same providers */
1588 if (!TEST_true(EVP_DigestInit_ex2(inctx, md, NULL)))
1589 goto end;
1590 if (!TEST_true(EVP_DigestInit_ex2(outctx, md, NULL)))
1591 goto end;
1592
1593 /*
1594 * Test the EVP_MD_CTX_copy_ex function. After copying,
1595 * outctx->algctx should be the same as the original.
1596 */
1597 origin_algctx = outctx->algctx;
1598 ret = TEST_true(EVP_MD_CTX_copy_ex(outctx, inctx))
1599 && TEST_true(outctx->algctx == origin_algctx);
1600
1601 end:
1602 EVP_MD_free(md);
1603 EVP_MD_CTX_free(inctx);
1604 EVP_MD_CTX_free(outctx);
1605 OSSL_LIB_CTX_free(ctx);
1606 return ret;
1607 }
1608
1609 #if !defined OPENSSL_NO_DES && !defined OPENSSL_NO_MD5
test_evp_pbe_alg_add(void)1610 static int test_evp_pbe_alg_add(void)
1611 {
1612 int ret = 0;
1613 int cipher_nid = 0, md_nid = 0;
1614 EVP_PBE_KEYGEN_EX *keygen_ex = NULL;
1615 EVP_PBE_KEYGEN *keygen = NULL;
1616
1617 if (!TEST_true(EVP_PBE_alg_add(NID_pbeWithMD5AndDES_CBC, EVP_des_cbc(), EVP_md5(),
1618 PKCS5_PBE_keyivgen)))
1619 goto err;
1620
1621 if (!TEST_true(EVP_PBE_find_ex(EVP_PBE_TYPE_OUTER, NID_pbeWithMD5AndDES_CBC,
1622 &cipher_nid, &md_nid, &keygen, &keygen_ex)))
1623 goto err;
1624
1625 if (!TEST_true(keygen != NULL))
1626 goto err;
1627 if (!TEST_true(keygen_ex == NULL))
1628 goto err;
1629
1630 ret = 1;
1631
1632 err:
1633 return ret;
1634 }
1635 #endif
1636
1637 /*
1638 * Currently, EVP_<OBJ>_fetch doesn't support
1639 * colon separated alternative names for lookup
1640 * so add a test here to ensure that when one is provided
1641 * libcrypto returns an error
1642 */
evp_test_name_parsing(void)1643 static int evp_test_name_parsing(void)
1644 {
1645 EVP_MD *md;
1646
1647 if (!TEST_ptr_null(md = EVP_MD_fetch(mainctx, "SHA256:BogusName", NULL))) {
1648 EVP_MD_free(md);
1649 return 0;
1650 }
1651
1652 return 1;
1653 }
1654
1655 /*
1656 * Table-driven driver for do_pkey_checks(). Each entry names the
1657 * algorithm and supplies either (a) a prefabricated DER blob, loaded
1658 * via d2i_AutoPrivateKey_ex(), or (b) an optional OSSL_PARAM array
1659 * passed to EVP_PKEY_CTX_set_params() before EVP_PKEY_generate().
1660 * The downstream checks are algorithm-agnostic; only the per-algorithm
1661 * inputs live in this table.
1662 */
1663 typedef struct pkey_test_spec_st {
1664 const char *name; /* algorithm name for keygen or logging */
1665 const unsigned char *der; /* prefab private key DER, or NULL */
1666 uint32_t der_len; /* size of the DER blob when set */
1667 const OSSL_PARAM *params; /* keygen params, or NULL for defaults */
1668 } PKEY_TEST_SPEC;
1669
1670 static const PKEY_TEST_SPEC pkey_test_specs[] = {
1671 #ifndef OPENSSL_NO_DH
1672 { "DHX", NULL, 0, dhx_keygen_params },
1673 #endif
1674 #ifndef OPENSSL_NO_DSA
1675 { "DSA", dsa_key, sizeof(dsa_key), NULL },
1676 #endif
1677 { "RSA", kExampleRSAKeyDER, sizeof(kExampleRSAKeyDER), NULL },
1678 { "RSA-PSS", kExampleRSAPSSKeyDER, sizeof(kExampleRSAPSSKeyDER), NULL },
1679 #ifndef OPENSSL_NO_EC
1680 { "EC", NULL, 0, ec_keygen_params },
1681 #ifndef OPENSSL_NO_ECX
1682 { "X25519", NULL, 0, NULL },
1683 { "ED25519", NULL, 0, NULL },
1684 #endif
1685 #ifndef OPENSSL_NO_SM2
1686 { "SM2", NULL, 0, NULL },
1687 #endif
1688 #endif
1689 #ifndef OPENSSL_NO_ML_KEM
1690 { "ML-KEM-512", NULL, 0, NULL },
1691 #endif
1692 #ifndef OPENSSL_NO_ML_DSA
1693 { "ML-DSA-44", NULL, 0, NULL },
1694 #endif
1695 #ifndef OPENSSL_NO_SLH_DSA
1696 { "SLH-DSA-SHA2-128f", NULL, 0, NULL },
1697 #endif
1698 };
1699
pkey_from_spec(OSSL_LIB_CTX * libctx,const PKEY_TEST_SPEC * spec)1700 static EVP_PKEY *pkey_from_spec(OSSL_LIB_CTX *libctx,
1701 const PKEY_TEST_SPEC *spec)
1702 {
1703 EVP_PKEY *key = NULL;
1704 EVP_PKEY_CTX *gctx = NULL;
1705
1706 if (spec->der != NULL) {
1707 const unsigned char *p = spec->der;
1708
1709 return d2i_AutoPrivateKey_ex(NULL, &p, spec->der_len, libctx, NULL);
1710 }
1711
1712 if ((gctx = EVP_PKEY_CTX_new_from_name(libctx, spec->name, NULL)) == NULL
1713 || EVP_PKEY_keygen_init(gctx) <= 0
1714 || (spec->params != NULL
1715 && EVP_PKEY_CTX_set_params(gctx, spec->params) <= 0)
1716 || EVP_PKEY_generate(gctx, &key) <= 0) {
1717 EVP_PKEY_free(key);
1718 key = NULL;
1719 }
1720 EVP_PKEY_CTX_free(gctx);
1721 return key;
1722 }
1723
test_pkey_by_spec(int i)1724 static int test_pkey_by_spec(int i)
1725 {
1726 EVP_PKEY *key = pkey_from_spec(mainctx, &pkey_test_specs[i]);
1727 int ret = TEST_ptr(key)
1728 && do_pkey_checks(key, pkey_test_specs[i].name);
1729
1730 EVP_PKEY_free(key);
1731 return ret;
1732 }
1733
1734 #if !defined(OPENSSL_NO_DH) && !defined(OPENSSL_NO_DEPRECATED_3_0)
1735 /*
1736 * DHX keygen with group "ffdhe2048" reduces the private exponent modulo
1737 * the sub-group order, so the exposed BN_num_bits() should not exceed
1738 * the sub-group size. This tail check is DHX-specific and therefore
1739 * does not fit the generic spec-table driver.
1740 */
test_dh_priv_bits(void)1741 static int test_dh_priv_bits(void)
1742 {
1743 static const PKEY_TEST_SPEC dhx_spec = {
1744 "DHX", NULL, 0, dhx_keygen_params
1745 };
1746 int ret;
1747 EVP_PKEY *key;
1748 const DH *dhkey;
1749 const BIGNUM *privkey;
1750
1751 if (!TEST_ptr(key = pkey_from_spec(mainctx, &dhx_spec)))
1752 return 0;
1753 ret = TEST_ptr(dhkey = EVP_PKEY_get0_DH(key))
1754 && TEST_ptr(privkey = DH_get0_priv_key(dhkey))
1755 && TEST_int_le(BN_num_bits(privkey), 225);
1756 EVP_PKEY_free(key);
1757 return ret;
1758 }
1759 #endif
1760
setup_tests(void)1761 int setup_tests(void)
1762 {
1763 if (!test_get_libctx(&mainctx, &nullprov, NULL, NULL, NULL)) {
1764 OSSL_LIB_CTX_free(mainctx);
1765 mainctx = NULL;
1766 return 0;
1767 }
1768
1769 ADD_TEST(evp_test_name_parsing);
1770 ADD_TEST(test_alternative_default);
1771 ADD_ALL_TESTS(test_d2i_AutoPrivateKey_ex, OSSL_NELEM(keydata));
1772 ADD_TEST(test_new_keytype);
1773 #ifndef OPENSSL_NO_EC
1774 ADD_ALL_TESTS(test_d2i_PrivateKey_ex, 2);
1775 ADD_TEST(test_ec_d2i_i2d_pubkey);
1776 #else
1777 ADD_ALL_TESTS(test_d2i_PrivateKey_ex, 1);
1778 #endif
1779 #ifndef OPENSSL_NO_DSA
1780 ADD_TEST(test_dsa_todata);
1781 ADD_ALL_TESTS(test_dsa_fromdata_digest_prop, 2);
1782 #endif
1783 #ifndef OPENSSL_NO_DH
1784 ADD_TEST(test_dh_paramgen);
1785 ADD_TEST(test_dh_paramfromdata);
1786 #ifndef OPENSSL_NO_DEPRECATED_3_0
1787 ADD_TEST(test_dh_priv_bits);
1788 #endif
1789 #endif
1790 ADD_ALL_TESTS(test_pkey_by_spec, OSSL_NELEM(pkey_test_specs));
1791
1792 ADD_TEST(test_pkey_todata_null);
1793 ADD_TEST(test_pkey_export_null);
1794 ADD_TEST(test_pkey_export);
1795 #ifndef OPENSSL_NO_DES
1796 ADD_TEST(test_pkcs8key_nid_bio);
1797 #endif
1798 ADD_ALL_TESTS(test_PEM_read_bio_negative, OSSL_NELEM(keydata));
1799 ADD_ALL_TESTS(test_PEM_read_bio_negative_wrong_password, 2);
1800 ADD_TEST(test_rsa_pss_sign);
1801 ADD_TEST(test_evp_md_ctx_dup);
1802 ADD_TEST(test_evp_md_ctx_copy);
1803 ADD_TEST(test_evp_md_ctx_copy2);
1804 ADD_ALL_TESTS(test_provider_unload_effective, 2);
1805 #if !defined OPENSSL_NO_DES && !defined OPENSSL_NO_MD5
1806 ADD_TEST(test_evp_pbe_alg_add);
1807 #endif
1808 return 1;
1809 }
1810
cleanup_tests(void)1811 void cleanup_tests(void)
1812 {
1813 OSSL_LIB_CTX_free(mainctx);
1814 OSSL_PROVIDER_unload(nullprov);
1815 }
1816