eroman | 5a6f502 | 2016-02-29 11:04:00 | [diff] [blame] | 1 | // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "components/webcrypto/fuzzer_support.h" |
| 6 | |
| 7 | #include "base/command_line.h" |
| 8 | #include "base/lazy_instance.h" |
kulshin | 77610984 | 2016-07-14 01:08:16 | [diff] [blame] | 9 | #include "base/numerics/safe_conversions.h" |
Alex Clarke | 636e705 | 2019-05-30 10:49:37 | [diff] [blame] | 10 | #include "base/task/single_thread_task_executor.h" |
eroman | 5a6f502 | 2016-02-29 11:04:00 | [diff] [blame] | 11 | #include "components/webcrypto/algorithm_dispatch.h" |
| 12 | #include "components/webcrypto/crypto_data.h" |
| 13 | #include "components/webcrypto/status.h" |
Ken Rockot | 9b26bc5 | 2018-07-04 19:57:49 | [diff] [blame] | 14 | #include "mojo/core/embedder/embedder.h" |
Blink Reformat | a30d423 | 2018-04-07 15:31:06 | [diff] [blame] | 15 | #include "third_party/blink/public/platform/platform.h" |
| 16 | #include "third_party/blink/public/platform/web_crypto_algorithm_params.h" |
| 17 | #include "third_party/blink/public/web/blink.h" |
eroman | 5a6f502 | 2016-02-29 11:04:00 | [diff] [blame] | 18 | |
| 19 | namespace webcrypto { |
| 20 | |
| 21 | namespace { |
| 22 | |
| 23 | // This mock is used to initialize blink. |
Nico Weber | 43ddd7a3 | 2017-08-15 19:19:27 | [diff] [blame] | 24 | class InitOnce : public blink::Platform { |
eroman | 5a6f502 | 2016-02-29 11:04:00 | [diff] [blame] | 25 | public: |
| 26 | InitOnce() { |
eroman | 5a6f502 | 2016-02-29 11:04:00 | [diff] [blame] | 27 | base::CommandLine::Init(0, nullptr); |
Ken Rockot | 9b26bc5 | 2018-07-04 19:57:49 | [diff] [blame] | 28 | mojo::core::Init(); |
Eric Roman | a73a168 | 2018-08-09 03:28:22 | [diff] [blame] | 29 | blink::Platform::CreateMainThreadAndInitialize(this); |
eroman | 5a6f502 | 2016-02-29 11:04:00 | [diff] [blame] | 30 | } |
jam | 7ffe5e2e | 2016-08-12 01:05:01 | [diff] [blame] | 31 | ~InitOnce() override {} |
Eric Roman | 9457c5e | 2017-09-12 21:53:16 | [diff] [blame] | 32 | |
| 33 | private: |
Alex Clarke | 636e705 | 2019-05-30 10:49:37 | [diff] [blame] | 34 | base::SingleThreadTaskExecutor main_thread_task_executor_; |
eroman | 5a6f502 | 2016-02-29 11:04:00 | [diff] [blame] | 35 | }; |
| 36 | |
| 37 | base::LazyInstance<InitOnce>::Leaky g_once = LAZY_INSTANCE_INITIALIZER; |
| 38 | |
| 39 | void EnsureInitialized() { |
| 40 | g_once.Get(); |
| 41 | } |
| 42 | |
| 43 | blink::WebCryptoAlgorithm CreateRsaHashedImportAlgorithm( |
| 44 | blink::WebCryptoAlgorithmId id, |
| 45 | blink::WebCryptoAlgorithmId hash_id) { |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 46 | DCHECK(blink::WebCryptoAlgorithm::IsHash(hash_id)); |
| 47 | return blink::WebCryptoAlgorithm::AdoptParamsAndCreate( |
eroman | 5a6f502 | 2016-02-29 11:04:00 | [diff] [blame] | 48 | id, |
| 49 | new blink::WebCryptoRsaHashedImportParams( |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 50 | blink::WebCryptoAlgorithm::AdoptParamsAndCreate(hash_id, nullptr))); |
eroman | 5a6f502 | 2016-02-29 11:04:00 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | blink::WebCryptoAlgorithm CreateEcImportAlgorithm( |
| 54 | blink::WebCryptoAlgorithmId id, |
| 55 | blink::WebCryptoNamedCurve named_curve) { |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 56 | return blink::WebCryptoAlgorithm::AdoptParamsAndCreate( |
eroman | 5a6f502 | 2016-02-29 11:04:00 | [diff] [blame] | 57 | id, new blink::WebCryptoEcKeyImportParams(named_curve)); |
| 58 | } |
| 59 | |
| 60 | } // namespace |
| 61 | |
| 62 | blink::WebCryptoKeyUsageMask GetCompatibleKeyUsages( |
| 63 | blink::WebCryptoKeyFormat format) { |
| 64 | // SPKI format implies import of a public key, whereas PKCS8 implies import |
| 65 | // of a private key. Pick usages that are compatible with a signature |
| 66 | // algorithm. |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 67 | return format == blink::kWebCryptoKeyFormatSpki |
| 68 | ? blink::kWebCryptoKeyUsageVerify |
| 69 | : blink::kWebCryptoKeyUsageSign; |
eroman | 5a6f502 | 2016-02-29 11:04:00 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | void ImportEcKeyFromDerFuzzData(const uint8_t* data, |
| 73 | size_t size, |
| 74 | blink::WebCryptoKeyFormat format) { |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 75 | DCHECK(format == blink::kWebCryptoKeyFormatSpki || |
| 76 | format == blink::kWebCryptoKeyFormatPkcs8); |
eroman | 5a6f502 | 2016-02-29 11:04:00 | [diff] [blame] | 77 | EnsureInitialized(); |
| 78 | |
| 79 | // There are 3 possible EC named curves. Fix this parameter. It shouldn't |
| 80 | // matter based on the current implementation for PKCS8 or SPKI. But it |
| 81 | // will have an impact when parsing JWK format. |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 82 | blink::WebCryptoNamedCurve curve = blink::kWebCryptoNamedCurveP384; |
eroman | 5a6f502 | 2016-02-29 11:04:00 | [diff] [blame] | 83 | |
| 84 | // Always use ECDSA as the algorithm. Shouldn't make much difference for |
| 85 | // non-JWK formats. |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 86 | blink::WebCryptoAlgorithmId algorithm_id = blink::kWebCryptoAlgorithmIdEcdsa; |
eroman | 5a6f502 | 2016-02-29 11:04:00 | [diff] [blame] | 87 | |
| 88 | // Use key usages that are compatible with the chosen algorithm and key type. |
| 89 | blink::WebCryptoKeyUsageMask usages = GetCompatibleKeyUsages(format); |
| 90 | |
| 91 | blink::WebCryptoKey key; |
| 92 | webcrypto::Status status = webcrypto::ImportKey( |
kulshin | 77610984 | 2016-07-14 01:08:16 | [diff] [blame] | 93 | format, webcrypto::CryptoData(data, base::checked_cast<uint32_t>(size)), |
eroman | 5a6f502 | 2016-02-29 11:04:00 | [diff] [blame] | 94 | CreateEcImportAlgorithm(algorithm_id, curve), true, usages, &key); |
| 95 | |
| 96 | // These errors imply a bad setup of parameters, and means ImportKey() may not |
| 97 | // be testing the actual parsing. |
| 98 | DCHECK_NE(status.error_details(), |
| 99 | Status::ErrorUnsupportedImportKeyFormat().error_details()); |
| 100 | DCHECK_NE(status.error_details(), |
| 101 | Status::ErrorCreateKeyBadUsages().error_details()); |
| 102 | } |
| 103 | |
eroman | cfcb6c9 | 2016-08-12 00:33:46 | [diff] [blame] | 104 | void ImportEcKeyFromRawFuzzData(const uint8_t* data, size_t size) { |
| 105 | EnsureInitialized(); |
| 106 | |
| 107 | // There are 3 possible EC named curves. Consume the first byte to decide on |
| 108 | // the curve. |
| 109 | uint8_t curve_index = 0; |
| 110 | if (size > 0) { |
| 111 | curve_index = data[0]; |
| 112 | data++; |
| 113 | size--; |
| 114 | } |
| 115 | |
| 116 | blink::WebCryptoNamedCurve curve; |
| 117 | |
| 118 | switch (curve_index % 3) { |
| 119 | case 0: |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 120 | curve = blink::kWebCryptoNamedCurveP256; |
eroman | cfcb6c9 | 2016-08-12 00:33:46 | [diff] [blame] | 121 | break; |
| 122 | case 1: |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 123 | curve = blink::kWebCryptoNamedCurveP384; |
eroman | cfcb6c9 | 2016-08-12 00:33:46 | [diff] [blame] | 124 | break; |
| 125 | default: |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 126 | curve = blink::kWebCryptoNamedCurveP521; |
eroman | cfcb6c9 | 2016-08-12 00:33:46 | [diff] [blame] | 127 | break; |
| 128 | } |
| 129 | |
| 130 | // Always use ECDSA as the algorithm. Shouldn't make an difference for import. |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 131 | blink::WebCryptoAlgorithmId algorithm_id = blink::kWebCryptoAlgorithmIdEcdsa; |
eroman | cfcb6c9 | 2016-08-12 00:33:46 | [diff] [blame] | 132 | |
| 133 | // Use key usages that are compatible with the chosen algorithm and key type. |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 134 | blink::WebCryptoKeyUsageMask usages = blink::kWebCryptoKeyUsageVerify; |
eroman | cfcb6c9 | 2016-08-12 00:33:46 | [diff] [blame] | 135 | |
| 136 | blink::WebCryptoKey key; |
| 137 | webcrypto::Status status = webcrypto::ImportKey( |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 138 | blink::kWebCryptoKeyFormatRaw, |
eroman | cfcb6c9 | 2016-08-12 00:33:46 | [diff] [blame] | 139 | webcrypto::CryptoData(data, base::checked_cast<uint32_t>(size)), |
| 140 | CreateEcImportAlgorithm(algorithm_id, curve), true, usages, &key); |
| 141 | |
| 142 | // These errors imply a bad setup of parameters, and means ImportKey() may not |
| 143 | // be testing the actual parsing. |
| 144 | DCHECK_NE(status.error_details(), |
| 145 | Status::ErrorUnsupportedImportKeyFormat().error_details()); |
| 146 | DCHECK_NE(status.error_details(), |
| 147 | Status::ErrorCreateKeyBadUsages().error_details()); |
| 148 | } |
| 149 | |
eroman | 5a6f502 | 2016-02-29 11:04:00 | [diff] [blame] | 150 | void ImportRsaKeyFromDerFuzzData(const uint8_t* data, |
| 151 | size_t size, |
| 152 | blink::WebCryptoKeyFormat format) { |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 153 | DCHECK(format == blink::kWebCryptoKeyFormatSpki || |
| 154 | format == blink::kWebCryptoKeyFormatPkcs8); |
eroman | 5a6f502 | 2016-02-29 11:04:00 | [diff] [blame] | 155 | EnsureInitialized(); |
| 156 | |
| 157 | // There are several possible hash functions. Fix this parameter. It shouldn't |
| 158 | // matter based on the current implementation for PKCS8 or SPKI. But it |
| 159 | // will have an impact when parsing JWK format. |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 160 | blink::WebCryptoAlgorithmId hash_id = blink::kWebCryptoAlgorithmIdSha256; |
eroman | 5a6f502 | 2016-02-29 11:04:00 | [diff] [blame] | 161 | |
| 162 | // Always use RSA-SSA PKCS#1 as the algorithm. Shouldn't make much difference |
| 163 | // for non-JWK formats. |
| 164 | blink::WebCryptoAlgorithmId algorithm_id = |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 165 | blink::kWebCryptoAlgorithmIdRsaSsaPkcs1v1_5; |
eroman | 5a6f502 | 2016-02-29 11:04:00 | [diff] [blame] | 166 | |
| 167 | // Use key usages that are compatible with the chosen algorithm and key type. |
| 168 | blink::WebCryptoKeyUsageMask usages = GetCompatibleKeyUsages(format); |
| 169 | |
| 170 | blink::WebCryptoKey key; |
| 171 | webcrypto::Status status = webcrypto::ImportKey( |
kulshin | 77610984 | 2016-07-14 01:08:16 | [diff] [blame] | 172 | format, webcrypto::CryptoData(data, base::checked_cast<uint32_t>(size)), |
eroman | 5a6f502 | 2016-02-29 11:04:00 | [diff] [blame] | 173 | CreateRsaHashedImportAlgorithm(algorithm_id, hash_id), true, usages, |
| 174 | &key); |
| 175 | |
| 176 | // These errors imply a bad setup of parameters, and means ImportKey() may not |
| 177 | // be testing the actual parsing. |
| 178 | DCHECK_NE(status.error_details(), |
| 179 | Status::ErrorUnsupportedImportKeyFormat().error_details()); |
| 180 | DCHECK_NE(status.error_details(), |
| 181 | Status::ErrorCreateKeyBadUsages().error_details()); |
| 182 | } |
| 183 | |
| 184 | } // namespace webcrypto |