blob: b01d1e47a15a1a88655fd8a5e53b8c7d82b46b61 [file] [log] [blame]
Adam Langleye9ada862015-05-11 17:20:37 -07001/*
2 * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3 * project.
4 */
5/* ====================================================================
6 * Copyright (c) 2015 The OpenSSL Project. All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 *
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
18 * distribution.
19 *
20 * 3. All advertising materials mentioning features or use of this
21 * software must display the following acknowledgment:
22 * "This product includes software developed by the OpenSSL Project
23 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
24 *
25 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26 * endorse or promote products derived from this software without
27 * prior written permission. For written permission, please contact
28 * licensing@OpenSSL.org.
29 *
30 * 5. Products derived from this software may not be called "OpenSSL"
31 * nor may "OpenSSL" appear in their names without prior written
32 * permission of the OpenSSL Project.
33 *
34 * 6. Redistributions of any form whatsoever must retain the following
35 * acknowledgment:
36 * "This product includes software developed by the OpenSSL Project
37 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
38 *
39 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
43 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50 * OF THE POSSIBILITY OF SUCH DAMAGE.
51 * ====================================================================
52 */
53
David Benjamin6e899c72016-06-09 18:02:18 -040054#include <openssl/evp.h>
55
Adam Langleye9ada862015-05-11 17:20:37 -070056#include <stdio.h>
57#include <stdint.h>
58#include <stdlib.h>
59#include <string.h>
60
David Benjamin6e899c72016-06-09 18:02:18 -040061OPENSSL_MSVC_PRAGMA(warning(push))
62OPENSSL_MSVC_PRAGMA(warning(disable: 4702))
Kenny Rootb8494592015-09-25 02:29:14 +000063
Adam Langleye9ada862015-05-11 17:20:37 -070064#include <map>
65#include <string>
David Benjamin4969cc92016-04-22 15:02:23 -040066#include <utility>
Adam Langleye9ada862015-05-11 17:20:37 -070067#include <vector>
68
David Benjamin6e899c72016-06-09 18:02:18 -040069OPENSSL_MSVC_PRAGMA(warning(pop))
Kenny Rootb8494592015-09-25 02:29:14 +000070
David Benjamin4969cc92016-04-22 15:02:23 -040071#include <openssl/bytestring.h>
Adam Langleye9ada862015-05-11 17:20:37 -070072#include <openssl/crypto.h>
73#include <openssl/digest.h>
74#include <openssl/err.h>
Adam Langleye9ada862015-05-11 17:20:37 -070075
76#include "../test/file_test.h"
77#include "../test/scoped_types.h"
Adam Langleye9ada862015-05-11 17:20:37 -070078
79
Kenny Rootb8494592015-09-25 02:29:14 +000080// evp_test dispatches between multiple test types. PrivateKey tests take a key
81// name parameter and single block, decode it as a PEM private key, and save it
82// under that key name. Decrypt, Sign, and Verify tests take a previously
83// imported key name as parameter and test their respective operations.
Adam Langleye9ada862015-05-11 17:20:37 -070084
85static const EVP_MD *GetDigest(FileTest *t, const std::string &name) {
86 if (name == "MD5") {
87 return EVP_md5();
88 } else if (name == "SHA1") {
89 return EVP_sha1();
90 } else if (name == "SHA224") {
91 return EVP_sha224();
92 } else if (name == "SHA256") {
93 return EVP_sha256();
94 } else if (name == "SHA384") {
95 return EVP_sha384();
96 } else if (name == "SHA512") {
97 return EVP_sha512();
98 }
99 t->PrintLine("Unknown digest: '%s'", name.c_str());
100 return nullptr;
101}
102
David Benjamin4969cc92016-04-22 15:02:23 -0400103static int GetKeyType(FileTest *t, const std::string &name) {
104 if (name == "RSA") {
105 return EVP_PKEY_RSA;
106 }
107 if (name == "EC") {
108 return EVP_PKEY_EC;
109 }
110 if (name == "DSA") {
111 return EVP_PKEY_DSA;
112 }
113 t->PrintLine("Unknown key type: '%s'", name.c_str());
114 return EVP_PKEY_NONE;
115}
Adam Langleye9ada862015-05-11 17:20:37 -0700116
David Benjamin4969cc92016-04-22 15:02:23 -0400117using KeyMap = std::map<std::string, ScopedEVP_PKEY>;
118
119static bool ImportKey(FileTest *t, KeyMap *key_map,
120 EVP_PKEY *(*parse_func)(CBS *cbs),
121 int (*marshal_func)(CBB *cbb, const EVP_PKEY *key)) {
122 std::vector<uint8_t> input;
123 if (!t->GetBytes(&input, "Input")) {
124 return false;
125 }
126
127 CBS cbs;
128 CBS_init(&cbs, input.data(), input.size());
129 ScopedEVP_PKEY pkey(parse_func(&cbs));
130 if (!pkey) {
131 return false;
132 }
133
134 std::string key_type;
135 if (!t->GetAttribute(&key_type, "Type")) {
136 return false;
137 }
138 if (EVP_PKEY_id(pkey.get()) != GetKeyType(t, key_type)) {
139 t->PrintLine("Bad key type.");
140 return false;
141 }
142
143 // The key must re-encode correctly.
144 ScopedCBB cbb;
145 uint8_t *der;
146 size_t der_len;
147 if (!CBB_init(cbb.get(), 0) ||
148 !marshal_func(cbb.get(), pkey.get()) ||
149 !CBB_finish(cbb.get(), &der, &der_len)) {
150 return false;
151 }
152 ScopedOpenSSLBytes free_der(der);
153
154 std::vector<uint8_t> output = input;
155 if (t->HasAttribute("Output") &&
156 !t->GetBytes(&output, "Output")) {
157 return false;
158 }
159 if (!t->ExpectBytesEqual(output.data(), output.size(), der, der_len)) {
160 t->PrintLine("Re-encoding the key did not match.");
161 return false;
162 }
163
164 // Save the key for future tests.
Adam Langleye9ada862015-05-11 17:20:37 -0700165 const std::string &key_name = t->GetParameter();
166 if (key_map->count(key_name) > 0) {
167 t->PrintLine("Duplicate key '%s'.", key_name.c_str());
168 return false;
169 }
David Benjamin4969cc92016-04-22 15:02:23 -0400170 (*key_map)[key_name] = std::move(pkey);
Adam Langleye9ada862015-05-11 17:20:37 -0700171 return true;
172}
173
Adam Langleye9ada862015-05-11 17:20:37 -0700174static bool TestEVP(FileTest *t, void *arg) {
175 KeyMap *key_map = reinterpret_cast<KeyMap*>(arg);
176 if (t->GetType() == "PrivateKey") {
David Benjamin4969cc92016-04-22 15:02:23 -0400177 return ImportKey(t, key_map, EVP_parse_private_key,
178 EVP_marshal_private_key);
179 }
180
181 if (t->GetType() == "PublicKey") {
182 return ImportKey(t, key_map, EVP_parse_public_key, EVP_marshal_public_key);
Adam Langleye9ada862015-05-11 17:20:37 -0700183 }
184
185 int (*key_op_init)(EVP_PKEY_CTX *ctx);
186 int (*key_op)(EVP_PKEY_CTX *ctx, uint8_t *out, size_t *out_len,
187 const uint8_t *in, size_t in_len);
188 if (t->GetType() == "Decrypt") {
189 key_op_init = EVP_PKEY_decrypt_init;
190 key_op = EVP_PKEY_decrypt;
191 } else if (t->GetType() == "Sign") {
192 key_op_init = EVP_PKEY_sign_init;
193 key_op = EVP_PKEY_sign;
194 } else if (t->GetType() == "Verify") {
195 key_op_init = EVP_PKEY_verify_init;
196 key_op = nullptr; // EVP_PKEY_verify is handled differently.
197 } else {
198 t->PrintLine("Unknown test '%s'", t->GetType().c_str());
199 return false;
200 }
201
202 // Load the key.
203 const std::string &key_name = t->GetParameter();
204 if (key_map->count(key_name) == 0) {
205 t->PrintLine("Could not find key '%s'.", key_name.c_str());
206 return false;
207 }
David Benjamin4969cc92016-04-22 15:02:23 -0400208 EVP_PKEY *key = (*key_map)[key_name].get();
Adam Langleye9ada862015-05-11 17:20:37 -0700209
210 std::vector<uint8_t> input, output;
211 if (!t->GetBytes(&input, "Input") ||
212 !t->GetBytes(&output, "Output")) {
213 return false;
214 }
215
216 // Set up the EVP_PKEY_CTX.
217 ScopedEVP_PKEY_CTX ctx(EVP_PKEY_CTX_new(key, nullptr));
218 if (!ctx || !key_op_init(ctx.get())) {
219 return false;
220 }
221 if (t->HasAttribute("Digest")) {
222 const EVP_MD *digest = GetDigest(t, t->GetAttributeOrDie("Digest"));
223 if (digest == nullptr ||
224 !EVP_PKEY_CTX_set_signature_md(ctx.get(), digest)) {
225 return false;
226 }
227 }
228
229 if (t->GetType() == "Verify") {
Adam Langley4139edb2016-01-13 15:00:54 -0800230 if (!EVP_PKEY_verify(ctx.get(), output.data(), output.size(), input.data(),
231 input.size())) {
Adam Langleye9ada862015-05-11 17:20:37 -0700232 // ECDSA sometimes doesn't push an error code. Push one on the error queue
233 // so it's distinguishable from other errors.
Kenny Rootb8494592015-09-25 02:29:14 +0000234 OPENSSL_PUT_ERROR(USER, ERR_R_EVP_LIB);
Adam Langleye9ada862015-05-11 17:20:37 -0700235 return false;
236 }
237 return true;
238 }
239
240 size_t len;
241 std::vector<uint8_t> actual;
Adam Langley4139edb2016-01-13 15:00:54 -0800242 if (!key_op(ctx.get(), nullptr, &len, input.data(), input.size())) {
Adam Langleye9ada862015-05-11 17:20:37 -0700243 return false;
244 }
245 actual.resize(len);
Adam Langley4139edb2016-01-13 15:00:54 -0800246 if (!key_op(ctx.get(), actual.data(), &len, input.data(), input.size())) {
Adam Langleye9ada862015-05-11 17:20:37 -0700247 return false;
248 }
249 actual.resize(len);
Adam Langley4139edb2016-01-13 15:00:54 -0800250 if (!t->ExpectBytesEqual(output.data(), output.size(), actual.data(), len)) {
Adam Langleye9ada862015-05-11 17:20:37 -0700251 return false;
252 }
253 return true;
254}
255
256int main(int argc, char **argv) {
257 CRYPTO_library_init();
258 if (argc != 2) {
259 fprintf(stderr, "%s <test file.txt>\n", argv[0]);
260 return 1;
261 }
262
263 KeyMap map;
David Benjamin4969cc92016-04-22 15:02:23 -0400264 return FileTestMain(TestEVP, &map, argv[1]);
Adam Langleye9ada862015-05-11 17:20:37 -0700265}