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