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