[email protected] | 38409aec | 2014-07-19 00:54:51 | [diff] [blame] | 1 | // Copyright 2014 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 | |
eroman | 7bcd6a7 | 2015-09-16 02:41:21 | [diff] [blame] | 5 | #include "components/webcrypto/algorithms/aes.h" |
[email protected] | 38409aec | 2014-07-19 00:54:51 | [diff] [blame] | 6 | |
avi | 5dd91f8 | 2015-12-25 22:30:46 | [diff] [blame] | 7 | #include <stddef.h> |
| 8 | |
eroman | d72073d | 2015-09-19 01:37:02 | [diff] [blame] | 9 | #include "components/webcrypto/algorithms/secret_key_util.h" |
eroman | 0bb976e | 2016-08-03 06:36:38 | [diff] [blame] | 10 | #include "components/webcrypto/algorithms/util.h" |
eroman | ab1308a | 2015-10-01 19:38:04 | [diff] [blame] | 11 | #include "components/webcrypto/blink_key_handle.h" |
erg | 56f1232 | 2015-04-17 00:51:48 | [diff] [blame] | 12 | #include "components/webcrypto/crypto_data.h" |
| 13 | #include "components/webcrypto/jwk.h" |
erg | 56f1232 | 2015-04-17 00:51:48 | [diff] [blame] | 14 | #include "components/webcrypto/status.h" |
Blink Reformat | a30d423 | 2018-04-07 15:31:06 | [diff] [blame] | 15 | #include "third_party/blink/public/platform/web_crypto_algorithm_params.h" |
| 16 | #include "third_party/blink/public/platform/web_crypto_key_algorithm.h" |
[email protected] | 38409aec | 2014-07-19 00:54:51 | [diff] [blame] | 17 | |
[email protected] | 38409aec | 2014-07-19 00:54:51 | [diff] [blame] | 18 | namespace webcrypto { |
| 19 | |
eroman | dceea88 | 2015-09-16 00:31:52 | [diff] [blame] | 20 | namespace { |
| 21 | |
| 22 | // Creates an AES algorithm name for the given key size (in bytes). For |
| 23 | // instance "A128CBC" is the result of suffix="CBC", keylen_bytes=16. |
| 24 | std::string MakeJwkAesAlgorithmName(const std::string& suffix, |
| 25 | size_t keylen_bytes) { |
| 26 | if (keylen_bytes == 16) |
| 27 | return std::string("A128") + suffix; |
| 28 | if (keylen_bytes == 24) |
| 29 | return std::string("A192") + suffix; |
| 30 | if (keylen_bytes == 32) |
| 31 | return std::string("A256") + suffix; |
| 32 | return std::string(); |
| 33 | } |
| 34 | |
eroman | ab1308a | 2015-10-01 19:38:04 | [diff] [blame] | 35 | // Synthesizes an import algorithm given a key algorithm, so that |
| 36 | // deserialization can re-use the ImportKey*() methods. |
| 37 | blink::WebCryptoAlgorithm SynthesizeImportAlgorithmForClone( |
| 38 | const blink::WebCryptoKeyAlgorithm& algorithm) { |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 39 | return blink::WebCryptoAlgorithm::AdoptParamsAndCreate(algorithm.Id(), |
eroman | ab1308a | 2015-10-01 19:38:04 | [diff] [blame] | 40 | nullptr); |
| 41 | } |
| 42 | |
eroman | dceea88 | 2015-09-16 00:31:52 | [diff] [blame] | 43 | } // namespace |
| 44 | |
[email protected] | 38409aec | 2014-07-19 00:54:51 | [diff] [blame] | 45 | AesAlgorithm::AesAlgorithm(blink::WebCryptoKeyUsageMask all_key_usages, |
| 46 | const std::string& jwk_suffix) |
| 47 | : all_key_usages_(all_key_usages), jwk_suffix_(jwk_suffix) { |
| 48 | } |
| 49 | |
| 50 | AesAlgorithm::AesAlgorithm(const std::string& jwk_suffix) |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 51 | : all_key_usages_(blink::kWebCryptoKeyUsageEncrypt | |
| 52 | blink::kWebCryptoKeyUsageDecrypt | |
| 53 | blink::kWebCryptoKeyUsageWrapKey | |
| 54 | blink::kWebCryptoKeyUsageUnwrapKey), |
| 55 | jwk_suffix_(jwk_suffix) {} |
[email protected] | 38409aec | 2014-07-19 00:54:51 | [diff] [blame] | 56 | |
eroman | 9b747eaf | 2014-10-18 22:03:28 | [diff] [blame] | 57 | Status AesAlgorithm::GenerateKey(const blink::WebCryptoAlgorithm& algorithm, |
| 58 | bool extractable, |
eroman | 0e1d34e | 2014-10-21 19:13:31 | [diff] [blame] | 59 | blink::WebCryptoKeyUsageMask usages, |
eroman | 9b747eaf | 2014-10-18 22:03:28 | [diff] [blame] | 60 | GenerateKeyResult* result) const { |
eroman | 0bb976e | 2016-08-03 06:36:38 | [diff] [blame] | 61 | Status status = CheckKeyCreationUsages(all_key_usages_, usages); |
eroman | 9b747eaf | 2014-10-18 22:03:28 | [diff] [blame] | 62 | if (status.IsError()) |
| 63 | return status; |
[email protected] | 38409aec | 2014-07-19 00:54:51 | [diff] [blame] | 64 | |
Miyoung Shin | 7561ca0b | 2019-03-03 14:14:15 | [diff] [blame] | 65 | uint16_t keylen_bits = algorithm.AesKeyGenParams()->LengthBits(); |
eroman | d72073d | 2015-09-19 01:37:02 | [diff] [blame] | 66 | |
eroman | f5d6b2a | 2015-09-23 01:05:43 | [diff] [blame] | 67 | // 192-bit AES is intentionally unsupported (http://crbug.com/533699). |
eroman | d72073d | 2015-09-19 01:37:02 | [diff] [blame] | 68 | if (keylen_bits == 192) |
| 69 | return Status::ErrorAes192BitUnsupported(); |
| 70 | |
| 71 | if (keylen_bits != 128 && keylen_bits != 256) |
| 72 | return Status::ErrorGenerateAesKeyLength(); |
[email protected] | 38409aec | 2014-07-19 00:54:51 | [diff] [blame] | 73 | |
eroman | ba702e2 | 2015-01-01 02:48:13 | [diff] [blame] | 74 | return GenerateWebCryptoSecretKey( |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 75 | blink::WebCryptoKeyAlgorithm::CreateAes(algorithm.Id(), keylen_bits), |
eroman | 401e4afc | 2014-12-10 17:41:04 | [diff] [blame] | 76 | extractable, usages, keylen_bits, result); |
[email protected] | 38409aec | 2014-07-19 00:54:51 | [diff] [blame] | 77 | } |
| 78 | |
eroman | 0bb976e | 2016-08-03 06:36:38 | [diff] [blame] | 79 | Status AesAlgorithm::ImportKey(blink::WebCryptoKeyFormat format, |
| 80 | const CryptoData& key_data, |
| 81 | const blink::WebCryptoAlgorithm& algorithm, |
| 82 | bool extractable, |
| 83 | blink::WebCryptoKeyUsageMask usages, |
| 84 | blink::WebCryptoKey* key) const { |
[email protected] | 38409aec | 2014-07-19 00:54:51 | [diff] [blame] | 85 | switch (format) { |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 86 | case blink::kWebCryptoKeyFormatRaw: |
eroman | 0bb976e | 2016-08-03 06:36:38 | [diff] [blame] | 87 | return ImportKeyRaw(key_data, algorithm, extractable, usages, key); |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 88 | case blink::kWebCryptoKeyFormatJwk: |
eroman | 0bb976e | 2016-08-03 06:36:38 | [diff] [blame] | 89 | return ImportKeyJwk(key_data, algorithm, extractable, usages, key); |
[email protected] | 38409aec | 2014-07-19 00:54:51 | [diff] [blame] | 90 | default: |
| 91 | return Status::ErrorUnsupportedImportKeyFormat(); |
| 92 | } |
| 93 | } |
| 94 | |
eroman | 0bb976e | 2016-08-03 06:36:38 | [diff] [blame] | 95 | Status AesAlgorithm::ExportKey(blink::WebCryptoKeyFormat format, |
| 96 | const blink::WebCryptoKey& key, |
| 97 | std::vector<uint8_t>* buffer) const { |
| 98 | switch (format) { |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 99 | case blink::kWebCryptoKeyFormatRaw: |
eroman | 0bb976e | 2016-08-03 06:36:38 | [diff] [blame] | 100 | return ExportKeyRaw(key, buffer); |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 101 | case blink::kWebCryptoKeyFormatJwk: |
eroman | 0bb976e | 2016-08-03 06:36:38 | [diff] [blame] | 102 | return ExportKeyJwk(key, buffer); |
| 103 | default: |
| 104 | return Status::ErrorUnsupportedExportKeyFormat(); |
| 105 | } |
| 106 | } |
| 107 | |
[email protected] | 38409aec | 2014-07-19 00:54:51 | [diff] [blame] | 108 | Status AesAlgorithm::ImportKeyRaw(const CryptoData& key_data, |
| 109 | const blink::WebCryptoAlgorithm& algorithm, |
| 110 | bool extractable, |
eroman | 0e1d34e | 2014-10-21 19:13:31 | [diff] [blame] | 111 | blink::WebCryptoKeyUsageMask usages, |
[email protected] | 38409aec | 2014-07-19 00:54:51 | [diff] [blame] | 112 | blink::WebCryptoKey* key) const { |
eroman | 0bb976e | 2016-08-03 06:36:38 | [diff] [blame] | 113 | Status status = CheckKeyCreationUsages(all_key_usages_, usages); |
| 114 | if (status.IsError()) |
| 115 | return status; |
| 116 | |
[email protected] | 38409aec | 2014-07-19 00:54:51 | [diff] [blame] | 117 | const unsigned int keylen_bytes = key_data.byte_length(); |
eroman | d72073d | 2015-09-19 01:37:02 | [diff] [blame] | 118 | |
eroman | f5d6b2a | 2015-09-23 01:05:43 | [diff] [blame] | 119 | // 192-bit AES is intentionally unsupported (http://crbug.com/533699). |
eroman | d72073d | 2015-09-19 01:37:02 | [diff] [blame] | 120 | if (keylen_bytes == 24) |
| 121 | return Status::ErrorAes192BitUnsupported(); |
| 122 | |
| 123 | if (keylen_bytes != 16 && keylen_bytes != 32) |
| 124 | return Status::ErrorImportAesKeyLength(); |
[email protected] | 38409aec | 2014-07-19 00:54:51 | [diff] [blame] | 125 | |
| 126 | // No possibility of overflow. |
| 127 | unsigned int keylen_bits = keylen_bytes * 8; |
| 128 | |
eroman | ba702e2 | 2015-01-01 02:48:13 | [diff] [blame] | 129 | return CreateWebCryptoSecretKey( |
| 130 | key_data, |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 131 | blink::WebCryptoKeyAlgorithm::CreateAes(algorithm.Id(), keylen_bits), |
eroman | ba702e2 | 2015-01-01 02:48:13 | [diff] [blame] | 132 | extractable, usages, key); |
[email protected] | 38409aec | 2014-07-19 00:54:51 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | Status AesAlgorithm::ImportKeyJwk(const CryptoData& key_data, |
| 136 | const blink::WebCryptoAlgorithm& algorithm, |
| 137 | bool extractable, |
eroman | 0e1d34e | 2014-10-21 19:13:31 | [diff] [blame] | 138 | blink::WebCryptoKeyUsageMask usages, |
[email protected] | 38409aec | 2014-07-19 00:54:51 | [diff] [blame] | 139 | blink::WebCryptoKey* key) const { |
eroman | 0bb976e | 2016-08-03 06:36:38 | [diff] [blame] | 140 | Status status = CheckKeyCreationUsages(all_key_usages_, usages); |
| 141 | if (status.IsError()) |
| 142 | return status; |
| 143 | |
[email protected] | 53b6c9d2 | 2014-07-19 05:08:38 | [diff] [blame] | 144 | std::vector<uint8_t> raw_data; |
eroman | dceea88 | 2015-09-16 00:31:52 | [diff] [blame] | 145 | JwkReader jwk; |
eroman | 0bb976e | 2016-08-03 06:36:38 | [diff] [blame] | 146 | status = ReadSecretKeyNoExpectedAlgJwk(key_data, extractable, usages, |
| 147 | &raw_data, &jwk); |
[email protected] | 38409aec | 2014-07-19 00:54:51 | [diff] [blame] | 148 | if (status.IsError()) |
| 149 | return status; |
| 150 | |
eroman | dceea88 | 2015-09-16 00:31:52 | [diff] [blame] | 151 | bool has_jwk_alg; |
| 152 | std::string jwk_alg; |
| 153 | status = jwk.GetAlg(&jwk_alg, &has_jwk_alg); |
| 154 | if (status.IsError()) |
| 155 | return status; |
| 156 | |
| 157 | if (has_jwk_alg) { |
| 158 | std::string expected_algorithm_name = |
| 159 | MakeJwkAesAlgorithmName(jwk_suffix_, raw_data.size()); |
| 160 | |
| 161 | if (jwk_alg != expected_algorithm_name) { |
| 162 | // Give a different error message if the key length was wrong. |
| 163 | if (jwk_alg == MakeJwkAesAlgorithmName(jwk_suffix_, 16) || |
| 164 | jwk_alg == MakeJwkAesAlgorithmName(jwk_suffix_, 24) || |
| 165 | jwk_alg == MakeJwkAesAlgorithmName(jwk_suffix_, 32)) { |
| 166 | return Status::ErrorJwkIncorrectKeyLength(); |
| 167 | } |
| 168 | return Status::ErrorJwkAlgorithmInconsistent(); |
| 169 | } |
| 170 | } |
| 171 | |
eroman | 38bb4bd | 2014-11-24 23:47:06 | [diff] [blame] | 172 | return ImportKeyRaw(CryptoData(raw_data), algorithm, extractable, usages, |
| 173 | key); |
[email protected] | 38409aec | 2014-07-19 00:54:51 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | Status AesAlgorithm::ExportKeyRaw(const blink::WebCryptoKey& key, |
[email protected] | 53b6c9d2 | 2014-07-19 05:08:38 | [diff] [blame] | 177 | std::vector<uint8_t>* buffer) const { |
eroman | d62cb47 | 2015-09-18 18:24:23 | [diff] [blame] | 178 | *buffer = GetSymmetricKeyData(key); |
[email protected] | 38409aec | 2014-07-19 00:54:51 | [diff] [blame] | 179 | return Status::Success(); |
| 180 | } |
| 181 | |
| 182 | Status AesAlgorithm::ExportKeyJwk(const blink::WebCryptoKey& key, |
[email protected] | 53b6c9d2 | 2014-07-19 05:08:38 | [diff] [blame] | 183 | std::vector<uint8_t>* buffer) const { |
eroman | d62cb47 | 2015-09-18 18:24:23 | [diff] [blame] | 184 | const std::vector<uint8_t>& raw_data = GetSymmetricKeyData(key); |
[email protected] | 38409aec | 2014-07-19 00:54:51 | [diff] [blame] | 185 | |
| 186 | WriteSecretKeyJwk(CryptoData(raw_data), |
| 187 | MakeJwkAesAlgorithmName(jwk_suffix_, raw_data.size()), |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 188 | key.Extractable(), key.Usages(), buffer); |
[email protected] | 38409aec | 2014-07-19 00:54:51 | [diff] [blame] | 189 | |
| 190 | return Status::Success(); |
| 191 | } |
| 192 | |
eroman | a895fed | 2014-11-08 03:10:25 | [diff] [blame] | 193 | Status AesAlgorithm::DeserializeKeyForClone( |
| 194 | const blink::WebCryptoKeyAlgorithm& algorithm, |
| 195 | blink::WebCryptoKeyType type, |
| 196 | bool extractable, |
| 197 | blink::WebCryptoKeyUsageMask usages, |
| 198 | const CryptoData& key_data, |
| 199 | blink::WebCryptoKey* key) const { |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 200 | if (algorithm.ParamsType() != blink::kWebCryptoKeyAlgorithmParamsTypeAes || |
| 201 | type != blink::kWebCryptoKeyTypeSecret) |
jbroman | 2e33aac | 2016-12-01 02:10:35 | [diff] [blame] | 202 | return Status::ErrorUnexpected(); |
| 203 | |
eroman | ab1308a | 2015-10-01 19:38:04 | [diff] [blame] | 204 | return ImportKeyRaw(key_data, SynthesizeImportAlgorithmForClone(algorithm), |
| 205 | extractable, usages, key); |
eroman | a895fed | 2014-11-08 03:10:25 | [diff] [blame] | 206 | } |
| 207 | |
eroman | f93fd5b | 2014-12-11 00:21:06 | [diff] [blame] | 208 | Status AesAlgorithm::GetKeyLength( |
| 209 | const blink::WebCryptoAlgorithm& key_length_algorithm, |
| 210 | bool* has_length_bits, |
| 211 | unsigned int* length_bits) const { |
eroman | d72073d | 2015-09-19 01:37:02 | [diff] [blame] | 212 | *has_length_bits = true; |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 213 | *length_bits = key_length_algorithm.AesDerivedKeyParams()->LengthBits(); |
eroman | d72073d | 2015-09-19 01:37:02 | [diff] [blame] | 214 | |
| 215 | if (*length_bits == 128 || *length_bits == 256) |
| 216 | return Status::Success(); |
| 217 | |
eroman | f5d6b2a | 2015-09-23 01:05:43 | [diff] [blame] | 218 | // 192-bit AES is intentionally unsupported (http://crbug.com/533699). |
eroman | d72073d | 2015-09-19 01:37:02 | [diff] [blame] | 219 | if (*length_bits == 192) |
| 220 | return Status::ErrorAes192BitUnsupported(); |
| 221 | |
| 222 | return Status::ErrorGetAesKeyLength(); |
eroman | f93fd5b | 2014-12-11 00:21:06 | [diff] [blame] | 223 | } |
| 224 | |
[email protected] | 38409aec | 2014-07-19 00:54:51 | [diff] [blame] | 225 | } // namespace webcrypto |