blob: 9217f3ae516ddcf54017a933f8d691352cb7e36f [file] [log] [blame]
Rich Salz440e5d82016-05-17 14:20:24 -04001/*
2 * Copyright 2015-2016 The OpenSSL Project Authors. All Rights Reserved.
Matt Caswell71ea6b42015-02-09 09:45:35 +00003 *
Rich Salz440e5d82016-05-17 14:20:24 -04004 * Licensed under the OpenSSL license (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
Matt Caswell71ea6b42015-02-09 09:45:35 +00008 */
9
10#include <stdio.h>
11#include <stdlib.h>
12#include <openssl/bio.h>
13#include <openssl/crypto.h>
14#include <openssl/err.h>
15#include <openssl/evp.h>
16#include <openssl/rsa.h>
17#include <openssl/x509.h>
18
19/*
20 * kExampleRSAKeyDER is an RSA private key in ASN.1, DER format. Of course, you
21 * should never use this key anywhere but in an example.
22 */
23static const unsigned char kExampleRSAKeyDER[] = {
24 0x30, 0x82, 0x02, 0x5c, 0x02, 0x01, 0x00, 0x02, 0x81, 0x81, 0x00, 0xf8,
25 0xb8, 0x6c, 0x83, 0xb4, 0xbc, 0xd9, 0xa8, 0x57, 0xc0, 0xa5, 0xb4, 0x59,
26 0x76, 0x8c, 0x54, 0x1d, 0x79, 0xeb, 0x22, 0x52, 0x04, 0x7e, 0xd3, 0x37,
27 0xeb, 0x41, 0xfd, 0x83, 0xf9, 0xf0, 0xa6, 0x85, 0x15, 0x34, 0x75, 0x71,
28 0x5a, 0x84, 0xa8, 0x3c, 0xd2, 0xef, 0x5a, 0x4e, 0xd3, 0xde, 0x97, 0x8a,
29 0xdd, 0xff, 0xbb, 0xcf, 0x0a, 0xaa, 0x86, 0x92, 0xbe, 0xb8, 0x50, 0xe4,
30 0xcd, 0x6f, 0x80, 0x33, 0x30, 0x76, 0x13, 0x8f, 0xca, 0x7b, 0xdc, 0xec,
31 0x5a, 0xca, 0x63, 0xc7, 0x03, 0x25, 0xef, 0xa8, 0x8a, 0x83, 0x58, 0x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 0x9d, 0xfe,
62 0x07, 0x2b, 0x0b, 0x63, 0x0b, 0x5a, 0x55, 0x90, 0x71, 0x7d, 0xf1, 0xdb,
63 0xd9, 0xb1, 0x41, 0x41, 0x68, 0x2f, 0x4e, 0x39, 0x02, 0x40, 0x5a, 0x34,
64 0x66, 0xd8, 0xf5, 0xe2, 0x7f, 0x18, 0xb5, 0x00, 0x6e, 0x26, 0x84, 0x27,
65 0x14, 0x93, 0xfb, 0xfc, 0xc6, 0x0f, 0x5e, 0x27, 0xe6, 0xe1, 0xe9, 0xc0,
66 0x8a, 0xe4, 0x34, 0xda, 0xe9, 0xa2, 0x4b, 0x73, 0xbc, 0x8c, 0xb9, 0xba,
67 0x13, 0x6c, 0x7a, 0x2b, 0x51, 0x84, 0xa3, 0x4a, 0xe0, 0x30, 0x10, 0x06,
68 0x7e, 0xed, 0x17, 0x5a, 0x14, 0x00, 0xc9, 0xef, 0x85, 0xea, 0x52, 0x2c,
69 0xbc, 0x65, 0x02, 0x40, 0x51, 0xe3, 0xf2, 0x83, 0x19, 0x9b, 0xc4, 0x1e,
70 0x2f, 0x50, 0x3d, 0xdf, 0x5a, 0xa2, 0x18, 0xca, 0x5f, 0x2e, 0x49, 0xaf,
71 0x6f, 0xcc, 0xfa, 0x65, 0x77, 0x94, 0xb5, 0xa1, 0x0a, 0xa9, 0xd1, 0x8a,
72 0x39, 0x37, 0xf4, 0x0b, 0xa0, 0xd7, 0x82, 0x27, 0x5e, 0xae, 0x17, 0x17,
73 0xa1, 0x1e, 0x54, 0x34, 0xbf, 0x6e, 0xc4, 0x8e, 0x99, 0x5d, 0x08, 0xf1,
74 0x2d, 0x86, 0x9d, 0xa5, 0x20, 0x1b, 0xe5, 0xdf,
75};
76
77static const unsigned char kMsg[] = { 1, 2, 3, 4 };
78
79static const unsigned char kSignature[] = {
80 0xa5, 0xf0, 0x8a, 0x47, 0x5d, 0x3c, 0xb3, 0xcc, 0xa9, 0x79, 0xaf, 0x4d,
81 0x8c, 0xae, 0x4c, 0x14, 0xef, 0xc2, 0x0b, 0x34, 0x36, 0xde, 0xf4, 0x3e,
82 0x3d, 0xbb, 0x4a, 0x60, 0x5c, 0xc8, 0x91, 0x28, 0xda, 0xfb, 0x7e, 0x04,
83 0x96, 0x7e, 0x63, 0x13, 0x90, 0xce, 0xb9, 0xb4, 0x62, 0x7a, 0xfd, 0x09,
84 0x3d, 0xc7, 0x67, 0x78, 0x54, 0x04, 0xeb, 0x52, 0x62, 0x6e, 0x24, 0x67,
85 0xb4, 0x40, 0xfc, 0x57, 0x62, 0xc6, 0xf1, 0x67, 0xc1, 0x97, 0x8f, 0x6a,
86 0xa8, 0xae, 0x44, 0x46, 0x5e, 0xab, 0x67, 0x17, 0x53, 0x19, 0x3a, 0xda,
87 0x5a, 0xc8, 0x16, 0x3e, 0x86, 0xd5, 0xc5, 0x71, 0x2f, 0xfc, 0x23, 0x48,
88 0xd9, 0x0b, 0x13, 0xdd, 0x7b, 0x5a, 0x25, 0x79, 0xef, 0xa5, 0x7b, 0x04,
89 0xed, 0x44, 0xf6, 0x18, 0x55, 0xe4, 0x0a, 0xe9, 0x57, 0x79, 0x5d, 0xd7,
90 0x55, 0xa7, 0xab, 0x45, 0x02, 0x97, 0x60, 0x42,
91};
92
93/*
94 * kExampleRSAKeyPKCS8 is kExampleRSAKeyDER encoded in a PKCS #8
95 * PrivateKeyInfo.
96 */
97static const unsigned char kExampleRSAKeyPKCS8[] = {
98 0x30, 0x82, 0x02, 0x76, 0x02, 0x01, 0x00, 0x30, 0x0d, 0x06, 0x09, 0x2a,
99 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01, 0x05, 0x00, 0x04, 0x82,
100 0x02, 0x60, 0x30, 0x82, 0x02, 0x5c, 0x02, 0x01, 0x00, 0x02, 0x81, 0x81,
101 0x00, 0xf8, 0xb8, 0x6c, 0x83, 0xb4, 0xbc, 0xd9, 0xa8, 0x57, 0xc0, 0xa5,
102 0xb4, 0x59, 0x76, 0x8c, 0x54, 0x1d, 0x79, 0xeb, 0x22, 0x52, 0x04, 0x7e,
103 0xd3, 0x37, 0xeb, 0x41, 0xfd, 0x83, 0xf9, 0xf0, 0xa6, 0x85, 0x15, 0x34,
104 0x75, 0x71, 0x5a, 0x84, 0xa8, 0x3c, 0xd2, 0xef, 0x5a, 0x4e, 0xd3, 0xde,
105 0x97, 0x8a, 0xdd, 0xff, 0xbb, 0xcf, 0x0a, 0xaa, 0x86, 0x92, 0xbe, 0xb8,
106 0x50, 0xe4, 0xcd, 0x6f, 0x80, 0x33, 0x30, 0x76, 0x13, 0x8f, 0xca, 0x7b,
107 0xdc, 0xec, 0x5a, 0xca, 0x63, 0xc7, 0x03, 0x25, 0xef, 0xa8, 0x8a, 0x83,
108 0x58, 0x76, 0x20, 0xfa, 0x16, 0x77, 0xd7, 0x79, 0x92, 0x63, 0x01, 0x48,
109 0x1a, 0xd8, 0x7b, 0x67, 0xf1, 0x52, 0x55, 0x49, 0x4e, 0xd6, 0x6e, 0x4a,
110 0x5c, 0xd7, 0x7a, 0x37, 0x36, 0x0c, 0xde, 0xdd, 0x8f, 0x44, 0xe8, 0xc2,
111 0xa7, 0x2c, 0x2b, 0xb5, 0xaf, 0x64, 0x4b, 0x61, 0x07, 0x02, 0x03, 0x01,
112 0x00, 0x01, 0x02, 0x81, 0x80, 0x74, 0x88, 0x64, 0x3f, 0x69, 0x45, 0x3a,
113 0x6d, 0xc7, 0x7f, 0xb9, 0xa3, 0xc0, 0x6e, 0xec, 0xdc, 0xd4, 0x5a, 0xb5,
114 0x32, 0x85, 0x5f, 0x19, 0xd4, 0xf8, 0xd4, 0x3f, 0x3c, 0xfa, 0xc2, 0xf6,
115 0x5f, 0xee, 0xe6, 0xba, 0x87, 0x74, 0x2e, 0xc7, 0x0c, 0xd4, 0x42, 0xb8,
116 0x66, 0x85, 0x9c, 0x7b, 0x24, 0x61, 0xaa, 0x16, 0x11, 0xf6, 0xb5, 0xb6,
117 0xa4, 0x0a, 0xc9, 0x55, 0x2e, 0x81, 0xa5, 0x47, 0x61, 0xcb, 0x25, 0x8f,
118 0xc2, 0x15, 0x7b, 0x0e, 0x7c, 0x36, 0x9f, 0x3a, 0xda, 0x58, 0x86, 0x1c,
119 0x5b, 0x83, 0x79, 0xe6, 0x2b, 0xcc, 0xe6, 0xfa, 0x2c, 0x61, 0xf2, 0x78,
120 0x80, 0x1b, 0xe2, 0xf3, 0x9d, 0x39, 0x2b, 0x65, 0x57, 0x91, 0x3d, 0x71,
121 0x99, 0x73, 0xa5, 0xc2, 0x79, 0x20, 0x8c, 0x07, 0x4f, 0xe5, 0xb4, 0x60,
122 0x1f, 0x99, 0xa2, 0xb1, 0x4f, 0x0c, 0xef, 0xbc, 0x59, 0x53, 0x00, 0x7d,
123 0xb1, 0x02, 0x41, 0x00, 0xfc, 0x7e, 0x23, 0x65, 0x70, 0xf8, 0xce, 0xd3,
124 0x40, 0x41, 0x80, 0x6a, 0x1d, 0x01, 0xd6, 0x01, 0xff, 0xb6, 0x1b, 0x3d,
125 0x3d, 0x59, 0x09, 0x33, 0x79, 0xc0, 0x4f, 0xde, 0x96, 0x27, 0x4b, 0x18,
126 0xc6, 0xd9, 0x78, 0xf1, 0xf4, 0x35, 0x46, 0xe9, 0x7c, 0x42, 0x7a, 0x5d,
127 0x9f, 0xef, 0x54, 0xb8, 0xf7, 0x9f, 0xc4, 0x33, 0x6c, 0xf3, 0x8c, 0x32,
128 0x46, 0x87, 0x67, 0x30, 0x7b, 0xa7, 0xac, 0xe3, 0x02, 0x41, 0x00, 0xfc,
129 0x2c, 0xdf, 0x0c, 0x0d, 0x88, 0xf5, 0xb1, 0x92, 0xa8, 0x93, 0x47, 0x63,
130 0x55, 0xf5, 0xca, 0x58, 0x43, 0xba, 0x1c, 0xe5, 0x9e, 0xb6, 0x95, 0x05,
131 0xcd, 0xb5, 0x82, 0xdf, 0xeb, 0x04, 0x53, 0x9d, 0xbd, 0xc2, 0x38, 0x16,
132 0xb3, 0x62, 0xdd, 0xa1, 0x46, 0xdb, 0x6d, 0x97, 0x93, 0x9f, 0x8a, 0xc3,
133 0x9b, 0x64, 0x7e, 0x42, 0xe3, 0x32, 0x57, 0x19, 0x1b, 0xd5, 0x6e, 0x85,
134 0xfa, 0xb8, 0x8d, 0x02, 0x41, 0x00, 0xbc, 0x3d, 0xde, 0x6d, 0xd6, 0x97,
135 0xe8, 0xba, 0x9e, 0x81, 0x37, 0x17, 0xe5, 0xa0, 0x64, 0xc9, 0x00, 0xb7,
136 0xe7, 0xfe, 0xf4, 0x29, 0xd9, 0x2e, 0x43, 0x6b, 0x19, 0x20, 0xbd, 0x99,
137 0x75, 0xe7, 0x76, 0xf8, 0xd3, 0xae, 0xaf, 0x7e, 0xb8, 0xeb, 0x81, 0xf4,
138 0x9d, 0xfe, 0x07, 0x2b, 0x0b, 0x63, 0x0b, 0x5a, 0x55, 0x90, 0x71, 0x7d,
139 0xf1, 0xdb, 0xd9, 0xb1, 0x41, 0x41, 0x68, 0x2f, 0x4e, 0x39, 0x02, 0x40,
140 0x5a, 0x34, 0x66, 0xd8, 0xf5, 0xe2, 0x7f, 0x18, 0xb5, 0x00, 0x6e, 0x26,
141 0x84, 0x27, 0x14, 0x93, 0xfb, 0xfc, 0xc6, 0x0f, 0x5e, 0x27, 0xe6, 0xe1,
142 0xe9, 0xc0, 0x8a, 0xe4, 0x34, 0xda, 0xe9, 0xa2, 0x4b, 0x73, 0xbc, 0x8c,
143 0xb9, 0xba, 0x13, 0x6c, 0x7a, 0x2b, 0x51, 0x84, 0xa3, 0x4a, 0xe0, 0x30,
144 0x10, 0x06, 0x7e, 0xed, 0x17, 0x5a, 0x14, 0x00, 0xc9, 0xef, 0x85, 0xea,
145 0x52, 0x2c, 0xbc, 0x65, 0x02, 0x40, 0x51, 0xe3, 0xf2, 0x83, 0x19, 0x9b,
146 0xc4, 0x1e, 0x2f, 0x50, 0x3d, 0xdf, 0x5a, 0xa2, 0x18, 0xca, 0x5f, 0x2e,
147 0x49, 0xaf, 0x6f, 0xcc, 0xfa, 0x65, 0x77, 0x94, 0xb5, 0xa1, 0x0a, 0xa9,
148 0xd1, 0x8a, 0x39, 0x37, 0xf4, 0x0b, 0xa0, 0xd7, 0x82, 0x27, 0x5e, 0xae,
149 0x17, 0x17, 0xa1, 0x1e, 0x54, 0x34, 0xbf, 0x6e, 0xc4, 0x8e, 0x99, 0x5d,
150 0x08, 0xf1, 0x2d, 0x86, 0x9d, 0xa5, 0x20, 0x1b, 0xe5, 0xdf,
151};
152
Matt Caswella9880362015-02-26 10:35:50 +0000153#ifndef OPENSSL_NO_EC
Matt Caswell71ea6b42015-02-09 09:45:35 +0000154/*
155 * kExampleECKeyDER is a sample EC private key encoded as an ECPrivateKey
156 * structure.
157 */
158static const unsigned char kExampleECKeyDER[] = {
159 0x30, 0x77, 0x02, 0x01, 0x01, 0x04, 0x20, 0x07, 0x0f, 0x08, 0x72, 0x7a,
160 0xd4, 0xa0, 0x4a, 0x9c, 0xdd, 0x59, 0xc9, 0x4d, 0x89, 0x68, 0x77, 0x08,
161 0xb5, 0x6f, 0xc9, 0x5d, 0x30, 0x77, 0x0e, 0xe8, 0xd1, 0xc9, 0xce, 0x0a,
162 0x8b, 0xb4, 0x6a, 0xa0, 0x0a, 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d,
163 0x03, 0x01, 0x07, 0xa1, 0x44, 0x03, 0x42, 0x00, 0x04, 0xe6, 0x2b, 0x69,
164 0xe2, 0xbf, 0x65, 0x9f, 0x97, 0xbe, 0x2f, 0x1e, 0x0d, 0x94, 0x8a, 0x4c,
165 0xd5, 0x97, 0x6b, 0xb7, 0xa9, 0x1e, 0x0d, 0x46, 0xfb, 0xdd, 0xa9, 0xa9,
166 0x1e, 0x9d, 0xdc, 0xba, 0x5a, 0x01, 0xe7, 0xd6, 0x97, 0xa8, 0x0a, 0x18,
167 0xf9, 0xc3, 0xc4, 0xa3, 0x1e, 0x56, 0xe2, 0x7c, 0x83, 0x48, 0xdb, 0x16,
168 0x1a, 0x1c, 0xf5, 0x1d, 0x7e, 0xf1, 0x94, 0x2d, 0x4b, 0xcf, 0x72, 0x22,
169 0xc1,
170};
171
172/*
173 * kExampleBadECKeyDER is a sample EC private key encoded as an ECPrivateKey
174 * structure. The private key is equal to the order and will fail to import
175 */
176static const unsigned char kExampleBadECKeyDER[] = {
177 0x30, 0x66, 0x02, 0x01, 0x00, 0x30, 0x13, 0x06, 0x07, 0x2A, 0x86, 0x48,
178 0xCE, 0x3D, 0x02, 0x01, 0x06, 0x08, 0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x03,
179 0x01, 0x07, 0x04, 0x4C, 0x30, 0x4A, 0x02, 0x01, 0x01, 0x04, 0x20, 0xFF,
180 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
181 0xFF, 0xFF, 0xFF, 0xBC, 0xE6, 0xFA, 0xAD, 0xA7, 0x17, 0x9E, 0x84, 0xF3,
182 0xB9, 0xCA, 0xC2, 0xFC, 0x63, 0x25, 0x51, 0xA1, 0x23, 0x03, 0x21, 0x00,
183 0x00, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF,
184 0xFF, 0xFF, 0xFF, 0xFF, 0xBC, 0xE6, 0xFA, 0xAD, 0xA7, 0x17, 0x9E, 0x84,
185 0xF3, 0xB9, 0xCA, 0xC2, 0xFC, 0x63, 0x25, 0x51
186};
Matt Caswella9880362015-02-26 10:35:50 +0000187#endif
Matt Caswell71ea6b42015-02-09 09:45:35 +0000188
189static EVP_PKEY *load_example_rsa_key(void)
190{
191 EVP_PKEY *ret = NULL;
192 const unsigned char *derp = kExampleRSAKeyDER;
193 EVP_PKEY *pkey = NULL;
194 RSA *rsa = NULL;
195
196 if (!d2i_RSAPrivateKey(&rsa, &derp, sizeof(kExampleRSAKeyDER))) {
197 return NULL;
198 }
199
200 pkey = EVP_PKEY_new();
201 if (pkey == NULL || !EVP_PKEY_set1_RSA(pkey, rsa)) {
202 goto out;
203 }
204
205 ret = pkey;
206 pkey = NULL;
207
208 out:
Rich Salzc5ba2d92015-03-28 10:54:15 -0400209 EVP_PKEY_free(pkey);
Rich Salzd6407082015-03-24 10:17:37 -0400210 RSA_free(rsa);
Matt Caswell71ea6b42015-02-09 09:45:35 +0000211
212 return ret;
213}
214
215static int test_EVP_DigestSignInit(void)
216{
217 int ret = 0;
218 EVP_PKEY *pkey = NULL;
219 unsigned char *sig = NULL;
220 size_t sig_len = 0;
Richard Levitte6e59a892015-11-27 14:02:12 +0100221 EVP_MD_CTX *md_ctx, *md_ctx_verify;
Matt Caswell71ea6b42015-02-09 09:45:35 +0000222
Richard Levittebfb06412015-12-02 00:49:35 +0100223 md_ctx = EVP_MD_CTX_new();
224 md_ctx_verify = EVP_MD_CTX_new();
Richard Levitte6e59a892015-11-27 14:02:12 +0100225 if (md_ctx == NULL || md_ctx_verify == NULL)
226 goto out;
Matt Caswell71ea6b42015-02-09 09:45:35 +0000227
228 pkey = load_example_rsa_key();
229 if (pkey == NULL ||
Richard Levitte6e59a892015-11-27 14:02:12 +0100230 !EVP_DigestSignInit(md_ctx, NULL, EVP_sha256(), NULL, pkey) ||
231 !EVP_DigestSignUpdate(md_ctx, kMsg, sizeof(kMsg))) {
Matt Caswell71ea6b42015-02-09 09:45:35 +0000232 goto out;
233 }
234 /* Determine the size of the signature. */
Richard Levitte6e59a892015-11-27 14:02:12 +0100235 if (!EVP_DigestSignFinal(md_ctx, NULL, &sig_len)) {
Matt Caswell71ea6b42015-02-09 09:45:35 +0000236 goto out;
237 }
238 /* Sanity check for testing. */
239 if (sig_len != (size_t)EVP_PKEY_size(pkey)) {
240 fprintf(stderr, "sig_len mismatch\n");
241 goto out;
242 }
243
244 sig = OPENSSL_malloc(sig_len);
Richard Levitte6e59a892015-11-27 14:02:12 +0100245 if (sig == NULL || !EVP_DigestSignFinal(md_ctx, sig, &sig_len)) {
Matt Caswell71ea6b42015-02-09 09:45:35 +0000246 goto out;
247 }
248
249 /* Ensure that the signature round-trips. */
Richard Levitte6e59a892015-11-27 14:02:12 +0100250 if (!EVP_DigestVerifyInit(md_ctx_verify, NULL, EVP_sha256(), NULL, pkey)
251 || !EVP_DigestVerifyUpdate(md_ctx_verify, kMsg, sizeof(kMsg))
252 || !EVP_DigestVerifyFinal(md_ctx_verify, sig, sig_len)) {
Matt Caswell71ea6b42015-02-09 09:45:35 +0000253 goto out;
254 }
255
256 ret = 1;
257
258 out:
259 if (!ret) {
260 ERR_print_errors_fp(stderr);
261 }
262
Richard Levittebfb06412015-12-02 00:49:35 +0100263 EVP_MD_CTX_free(md_ctx);
264 EVP_MD_CTX_free(md_ctx_verify);
Rich Salzc5ba2d92015-03-28 10:54:15 -0400265 EVP_PKEY_free(pkey);
Rich Salzb548a1f2015-05-01 10:02:07 -0400266 OPENSSL_free(sig);
Matt Caswell71ea6b42015-02-09 09:45:35 +0000267
268 return ret;
269}
270
271static int test_EVP_DigestVerifyInit(void)
272{
273 int ret = 0;
274 EVP_PKEY *pkey = NULL;
Richard Levitte6e59a892015-11-27 14:02:12 +0100275 EVP_MD_CTX *md_ctx;
Matt Caswell71ea6b42015-02-09 09:45:35 +0000276
Richard Levittebfb06412015-12-02 00:49:35 +0100277 md_ctx = EVP_MD_CTX_new();
Matt Caswell71ea6b42015-02-09 09:45:35 +0000278
279 pkey = load_example_rsa_key();
280 if (pkey == NULL ||
Richard Levitte6e59a892015-11-27 14:02:12 +0100281 !EVP_DigestVerifyInit(md_ctx, NULL, EVP_sha256(), NULL, pkey) ||
282 !EVP_DigestVerifyUpdate(md_ctx, kMsg, sizeof(kMsg)) ||
283 !EVP_DigestVerifyFinal(md_ctx, kSignature, sizeof(kSignature))) {
Matt Caswell71ea6b42015-02-09 09:45:35 +0000284 goto out;
285 }
286 ret = 1;
287
288 out:
289 if (!ret) {
290 ERR_print_errors_fp(stderr);
291 }
292
Richard Levittebfb06412015-12-02 00:49:35 +0100293 EVP_MD_CTX_free(md_ctx);
Rich Salzc5ba2d92015-03-28 10:54:15 -0400294 EVP_PKEY_free(pkey);
Matt Caswell71ea6b42015-02-09 09:45:35 +0000295
296 return ret;
297}
298
299static int test_d2i_AutoPrivateKey(const unsigned char *input,
300 size_t input_len, int expected_id)
301{
302 int ret = 0;
303 const unsigned char *p;
304 EVP_PKEY *pkey = NULL;
305
306 p = input;
307 pkey = d2i_AutoPrivateKey(NULL, &p, input_len);
308 if (pkey == NULL || p != input + input_len) {
309 fprintf(stderr, "d2i_AutoPrivateKey failed\n");
310 goto done;
311 }
312
313 if (EVP_PKEY_id(pkey) != expected_id) {
314 fprintf(stderr, "Did not decode expected type\n");
315 goto done;
316 }
317
318 ret = 1;
319
320 done:
321 if (!ret) {
322 ERR_print_errors_fp(stderr);
323 }
324
Rich Salzc5ba2d92015-03-28 10:54:15 -0400325 EVP_PKEY_free(pkey);
Matt Caswell71ea6b42015-02-09 09:45:35 +0000326 return ret;
327}
328
Matt Caswella9880362015-02-26 10:35:50 +0000329#ifndef OPENSSL_NO_EC
Matt Caswell71ea6b42015-02-09 09:45:35 +0000330/* Tests loading a bad key in PKCS8 format */
331static int test_EVP_PKCS82PKEY(void)
332{
333 int ret = 0;
334 const unsigned char *derp = kExampleBadECKeyDER;
335 PKCS8_PRIV_KEY_INFO *p8inf = NULL;
336 EVP_PKEY *pkey = NULL;
337
338 p8inf = d2i_PKCS8_PRIV_KEY_INFO(NULL, &derp, sizeof(kExampleBadECKeyDER));
339
340 if (!p8inf || derp != kExampleBadECKeyDER + sizeof(kExampleBadECKeyDER)) {
341 fprintf(stderr, "Failed to parse key\n");
342 goto done;
343 }
344
345 pkey = EVP_PKCS82PKEY(p8inf);
346 if (pkey) {
347 fprintf(stderr, "Imported invalid EC key\n");
348 goto done;
349 }
350
351 ret = 1;
352
353 done:
Rich Salze0e920b2015-04-11 16:32:54 -0400354 PKCS8_PRIV_KEY_INFO_free(p8inf);
Rich Salzc5ba2d92015-03-28 10:54:15 -0400355 EVP_PKEY_free(pkey);
Matt Caswell71ea6b42015-02-09 09:45:35 +0000356
357 return ret;
358}
Matt Caswella9880362015-02-26 10:35:50 +0000359#endif
Matt Caswell71ea6b42015-02-09 09:45:35 +0000360
361int main(void)
362{
Rich Salzbbd86bf2016-01-07 15:06:38 -0500363 CRYPTO_set_mem_debug(1);
Matt Caswell71ea6b42015-02-09 09:45:35 +0000364 CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
365
Matt Caswell71ea6b42015-02-09 09:45:35 +0000366
367 if (!test_EVP_DigestSignInit()) {
368 fprintf(stderr, "EVP_DigestSignInit failed\n");
369 return 1;
370 }
371
372 if (!test_EVP_DigestVerifyInit()) {
373 fprintf(stderr, "EVP_DigestVerifyInit failed\n");
374 return 1;
375 }
376
377 if (!test_d2i_AutoPrivateKey(kExampleRSAKeyDER, sizeof(kExampleRSAKeyDER),
378 EVP_PKEY_RSA)) {
379 fprintf(stderr, "d2i_AutoPrivateKey(kExampleRSAKeyDER) failed\n");
380 return 1;
381 }
382
383 if (!test_d2i_AutoPrivateKey
384 (kExampleRSAKeyPKCS8, sizeof(kExampleRSAKeyPKCS8), EVP_PKEY_RSA)) {
385 fprintf(stderr, "d2i_AutoPrivateKey(kExampleRSAKeyPKCS8) failed\n");
386 return 1;
387 }
388
Matt Caswella9880362015-02-26 10:35:50 +0000389#ifndef OPENSSL_NO_EC
Matt Caswell71ea6b42015-02-09 09:45:35 +0000390 if (!test_d2i_AutoPrivateKey(kExampleECKeyDER, sizeof(kExampleECKeyDER),
391 EVP_PKEY_EC)) {
392 fprintf(stderr, "d2i_AutoPrivateKey(kExampleECKeyDER) failed\n");
393 return 1;
394 }
395
396 if (!test_EVP_PKCS82PKEY()) {
397 fprintf(stderr, "test_EVP_PKCS82PKEY failed\n");
398 return 1;
399 }
Matt Caswella9880362015-02-26 10:35:50 +0000400#endif
Matt Caswell71ea6b42015-02-09 09:45:35 +0000401
Viktor Dukhovnic2e27312016-01-10 14:42:10 -0500402#ifndef OPENSSL_NO_CRYPTO_MDEBUG
Dr. Stephen Henson541e9562016-01-14 22:00:03 +0000403 if (CRYPTO_mem_leaks_fp(stderr) <= 0)
404 return 1;
Rich Salzbbd86bf2016-01-07 15:06:38 -0500405#endif
Matt Caswell71ea6b42015-02-09 09:45:35 +0000406
407 printf("PASS\n");
408 return 0;
409}