Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 1 | /* |
| 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 Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 54 | #include <openssl/evp.h> |
| 55 | |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 56 | #include <stdio.h> |
| 57 | #include <stdint.h> |
| 58 | #include <stdlib.h> |
| 59 | #include <string.h> |
| 60 | |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 61 | OPENSSL_MSVC_PRAGMA(warning(push)) |
| 62 | OPENSSL_MSVC_PRAGMA(warning(disable: 4702)) |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 63 | |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 64 | #include <map> |
| 65 | #include <string> |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 66 | #include <utility> |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 67 | #include <vector> |
| 68 | |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 69 | OPENSSL_MSVC_PRAGMA(warning(pop)) |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 70 | |
David Benjamin | f0c4a6c | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 71 | #include <openssl/bytestring.h> |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 72 | #include <openssl/crypto.h> |
| 73 | #include <openssl/digest.h> |
| 74 | #include <openssl/err.h> |
Robert Sloan | 69939df | 2017-01-09 10:53:07 -0800 | [diff] [blame] | 75 | #include <openssl/rsa.h> |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 76 | |
| 77 | #include "../test/file_test.h" |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 78 | |
| 79 | |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 80 | // 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 Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 84 | |
| 85 | static 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 Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 103 | static 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 | } |
Robert Sloan | 572a4e2 | 2017-04-17 10:52:19 -0700 | [diff] [blame] | 113 | if (name == "Ed25519") { |
| 114 | return EVP_PKEY_ED25519; |
| 115 | } |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 116 | t->PrintLine("Unknown key type: '%s'", name.c_str()); |
| 117 | return EVP_PKEY_NONE; |
| 118 | } |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 119 | |
Robert Sloan | 69939df | 2017-01-09 10:53:07 -0800 | [diff] [blame] | 120 | static int GetRSAPadding(FileTest *t, int *out, const std::string &name) { |
| 121 | if (name == "PKCS1") { |
| 122 | *out = RSA_PKCS1_PADDING; |
| 123 | return true; |
| 124 | } |
| 125 | if (name == "PSS") { |
| 126 | *out = RSA_PKCS1_PSS_PADDING; |
| 127 | return true; |
| 128 | } |
| 129 | if (name == "OAEP") { |
| 130 | *out = RSA_PKCS1_OAEP_PADDING; |
| 131 | return true; |
| 132 | } |
| 133 | t->PrintLine("Unknown RSA padding mode: '%s'", name.c_str()); |
| 134 | return false; |
| 135 | } |
| 136 | |
David Benjamin | f0c4a6c | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 137 | using KeyMap = std::map<std::string, bssl::UniquePtr<EVP_PKEY>>; |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 138 | |
| 139 | static bool ImportKey(FileTest *t, KeyMap *key_map, |
| 140 | EVP_PKEY *(*parse_func)(CBS *cbs), |
| 141 | int (*marshal_func)(CBB *cbb, const EVP_PKEY *key)) { |
| 142 | std::vector<uint8_t> input; |
| 143 | if (!t->GetBytes(&input, "Input")) { |
| 144 | return false; |
| 145 | } |
| 146 | |
| 147 | CBS cbs; |
| 148 | CBS_init(&cbs, input.data(), input.size()); |
David Benjamin | f0c4a6c | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 149 | bssl::UniquePtr<EVP_PKEY> pkey(parse_func(&cbs)); |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 150 | if (!pkey) { |
| 151 | return false; |
| 152 | } |
| 153 | |
| 154 | std::string key_type; |
| 155 | if (!t->GetAttribute(&key_type, "Type")) { |
| 156 | return false; |
| 157 | } |
| 158 | if (EVP_PKEY_id(pkey.get()) != GetKeyType(t, key_type)) { |
| 159 | t->PrintLine("Bad key type."); |
| 160 | return false; |
| 161 | } |
| 162 | |
| 163 | // The key must re-encode correctly. |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 164 | bssl::ScopedCBB cbb; |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 165 | uint8_t *der; |
| 166 | size_t der_len; |
| 167 | if (!CBB_init(cbb.get(), 0) || |
| 168 | !marshal_func(cbb.get(), pkey.get()) || |
| 169 | !CBB_finish(cbb.get(), &der, &der_len)) { |
| 170 | return false; |
| 171 | } |
David Benjamin | f0c4a6c | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 172 | bssl::UniquePtr<uint8_t> free_der(der); |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 173 | |
| 174 | std::vector<uint8_t> output = input; |
| 175 | if (t->HasAttribute("Output") && |
| 176 | !t->GetBytes(&output, "Output")) { |
| 177 | return false; |
| 178 | } |
| 179 | if (!t->ExpectBytesEqual(output.data(), output.size(), der, der_len)) { |
| 180 | t->PrintLine("Re-encoding the key did not match."); |
| 181 | return false; |
| 182 | } |
| 183 | |
| 184 | // Save the key for future tests. |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 185 | const std::string &key_name = t->GetParameter(); |
| 186 | if (key_map->count(key_name) > 0) { |
| 187 | t->PrintLine("Duplicate key '%s'.", key_name.c_str()); |
| 188 | return false; |
| 189 | } |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 190 | (*key_map)[key_name] = std::move(pkey); |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 191 | return true; |
| 192 | } |
| 193 | |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 194 | static bool TestEVP(FileTest *t, void *arg) { |
| 195 | KeyMap *key_map = reinterpret_cast<KeyMap*>(arg); |
| 196 | if (t->GetType() == "PrivateKey") { |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 197 | return ImportKey(t, key_map, EVP_parse_private_key, |
| 198 | EVP_marshal_private_key); |
| 199 | } |
| 200 | |
| 201 | if (t->GetType() == "PublicKey") { |
| 202 | return ImportKey(t, key_map, EVP_parse_public_key, EVP_marshal_public_key); |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | int (*key_op_init)(EVP_PKEY_CTX *ctx); |
| 206 | int (*key_op)(EVP_PKEY_CTX *ctx, uint8_t *out, size_t *out_len, |
Robert Sloan | 572a4e2 | 2017-04-17 10:52:19 -0700 | [diff] [blame] | 207 | const uint8_t *in, size_t in_len) = nullptr; |
| 208 | int (*verify_op)(EVP_PKEY_CTX * ctx, const uint8_t *sig, size_t sig_len, |
| 209 | const uint8_t *in, size_t in_len) = nullptr; |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 210 | if (t->GetType() == "Decrypt") { |
| 211 | key_op_init = EVP_PKEY_decrypt_init; |
| 212 | key_op = EVP_PKEY_decrypt; |
| 213 | } else if (t->GetType() == "Sign") { |
| 214 | key_op_init = EVP_PKEY_sign_init; |
| 215 | key_op = EVP_PKEY_sign; |
Robert Sloan | 572a4e2 | 2017-04-17 10:52:19 -0700 | [diff] [blame] | 216 | } else if (t->GetType() == "SignMessage") { |
| 217 | key_op_init = EVP_PKEY_sign_init; |
| 218 | key_op = EVP_PKEY_sign_message; |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 219 | } else if (t->GetType() == "Verify") { |
| 220 | key_op_init = EVP_PKEY_verify_init; |
Robert Sloan | 572a4e2 | 2017-04-17 10:52:19 -0700 | [diff] [blame] | 221 | verify_op = EVP_PKEY_verify; |
| 222 | } else if (t->GetType() == "VerifyMessage") { |
| 223 | key_op_init = EVP_PKEY_verify_init; |
| 224 | verify_op = EVP_PKEY_verify_message; |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 225 | } else { |
| 226 | t->PrintLine("Unknown test '%s'", t->GetType().c_str()); |
| 227 | return false; |
| 228 | } |
| 229 | |
| 230 | // Load the key. |
| 231 | const std::string &key_name = t->GetParameter(); |
| 232 | if (key_map->count(key_name) == 0) { |
| 233 | t->PrintLine("Could not find key '%s'.", key_name.c_str()); |
| 234 | return false; |
| 235 | } |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 236 | EVP_PKEY *key = (*key_map)[key_name].get(); |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 237 | |
Robert Sloan | 8ecb7cd | 2017-03-21 09:39:01 -0700 | [diff] [blame] | 238 | std::vector<uint8_t> input; |
| 239 | if (!t->GetBytes(&input, "Input")) { |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 240 | return false; |
| 241 | } |
| 242 | |
| 243 | // Set up the EVP_PKEY_CTX. |
David Benjamin | f0c4a6c | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 244 | bssl::UniquePtr<EVP_PKEY_CTX> ctx(EVP_PKEY_CTX_new(key, nullptr)); |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 245 | if (!ctx || !key_op_init(ctx.get())) { |
| 246 | return false; |
| 247 | } |
| 248 | if (t->HasAttribute("Digest")) { |
| 249 | const EVP_MD *digest = GetDigest(t, t->GetAttributeOrDie("Digest")); |
| 250 | if (digest == nullptr || |
| 251 | !EVP_PKEY_CTX_set_signature_md(ctx.get(), digest)) { |
| 252 | return false; |
| 253 | } |
| 254 | } |
Robert Sloan | 69939df | 2017-01-09 10:53:07 -0800 | [diff] [blame] | 255 | if (t->HasAttribute("RSAPadding")) { |
| 256 | int padding; |
| 257 | if (!GetRSAPadding(t, &padding, t->GetAttributeOrDie("RSAPadding")) || |
| 258 | !EVP_PKEY_CTX_set_rsa_padding(ctx.get(), padding)) { |
| 259 | return false; |
| 260 | } |
| 261 | } |
| 262 | if (t->HasAttribute("PSSSaltLength") && |
| 263 | !EVP_PKEY_CTX_set_rsa_pss_saltlen( |
| 264 | ctx.get(), atoi(t->GetAttributeOrDie("PSSSaltLength").c_str()))) { |
| 265 | return false; |
| 266 | } |
| 267 | if (t->HasAttribute("MGF1Digest")) { |
| 268 | const EVP_MD *digest = GetDigest(t, t->GetAttributeOrDie("MGF1Digest")); |
| 269 | if (digest == nullptr || |
| 270 | !EVP_PKEY_CTX_set_rsa_mgf1_md(ctx.get(), digest)) { |
| 271 | return false; |
| 272 | } |
| 273 | } |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 274 | |
Robert Sloan | 572a4e2 | 2017-04-17 10:52:19 -0700 | [diff] [blame] | 275 | if (verify_op != nullptr) { |
Robert Sloan | 8ecb7cd | 2017-03-21 09:39:01 -0700 | [diff] [blame] | 276 | std::vector<uint8_t> output; |
| 277 | if (!t->GetBytes(&output, "Output") || |
Robert Sloan | 572a4e2 | 2017-04-17 10:52:19 -0700 | [diff] [blame] | 278 | !verify_op(ctx.get(), output.data(), output.size(), input.data(), |
| 279 | input.size())) { |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 280 | // ECDSA sometimes doesn't push an error code. Push one on the error queue |
| 281 | // so it's distinguishable from other errors. |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 282 | OPENSSL_PUT_ERROR(USER, ERR_R_EVP_LIB); |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 283 | return false; |
| 284 | } |
| 285 | return true; |
| 286 | } |
| 287 | |
| 288 | size_t len; |
Robert Sloan | 8ecb7cd | 2017-03-21 09:39:01 -0700 | [diff] [blame] | 289 | std::vector<uint8_t> actual, output; |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 290 | if (!key_op(ctx.get(), nullptr, &len, input.data(), input.size())) { |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 291 | return false; |
| 292 | } |
| 293 | actual.resize(len); |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 294 | if (!key_op(ctx.get(), actual.data(), &len, input.data(), input.size())) { |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 295 | return false; |
| 296 | } |
| 297 | actual.resize(len); |
Robert Sloan | 8ecb7cd | 2017-03-21 09:39:01 -0700 | [diff] [blame] | 298 | if (!t->GetBytes(&output, "Output") || |
| 299 | !t->ExpectBytesEqual(output.data(), output.size(), actual.data(), len)) { |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 300 | return false; |
| 301 | } |
| 302 | return true; |
| 303 | } |
| 304 | |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 305 | int main(int argc, char *argv[]) { |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 306 | CRYPTO_library_init(); |
| 307 | if (argc != 2) { |
| 308 | fprintf(stderr, "%s <test file.txt>\n", argv[0]); |
| 309 | return 1; |
| 310 | } |
| 311 | |
| 312 | KeyMap map; |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 313 | return FileTestMain(TestEVP, &map, argv[1]); |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 314 | } |