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