[email protected] | 37be4cfa | 2014-03-20 05:39:37 | [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 | |
erg | 56f1232 | 2015-04-17 00:51:48 | [diff] [blame] | 5 | #include "components/webcrypto/status.h" |
[email protected] | 37be4cfa | 2014-03-20 05:39:37 | [diff] [blame] | 6 | |
eroman | b2ead6d | 2014-11-14 02:26:14 | [diff] [blame] | 7 | #include "base/format_macros.h" |
| 8 | #include "base/strings/stringprintf.h" |
| 9 | |
[email protected] | 37be4cfa | 2014-03-20 05:39:37 | [diff] [blame] | 10 | namespace webcrypto { |
| 11 | |
[email protected] | c503936 | 2014-04-28 19:10:34 | [diff] [blame] | 12 | bool Status::IsError() const { |
| 13 | return type_ == TYPE_ERROR; |
[email protected] | 37be4cfa | 2014-03-20 05:39:37 | [diff] [blame] | 14 | } |
| 15 | |
[email protected] | c503936 | 2014-04-28 19:10:34 | [diff] [blame] | 16 | bool Status::IsSuccess() const { |
| 17 | return type_ == TYPE_SUCCESS; |
| 18 | } |
[email protected] | 37be4cfa | 2014-03-20 05:39:37 | [diff] [blame] | 19 | |
[email protected] | c503936 | 2014-04-28 19:10:34 | [diff] [blame] | 20 | Status Status::Success() { |
| 21 | return Status(TYPE_SUCCESS); |
| 22 | } |
| 23 | |
| 24 | Status Status::OperationError() { |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 25 | return Status(blink::kWebCryptoErrorTypeOperation, ""); |
[email protected] | c503936 | 2014-04-28 19:10:34 | [diff] [blame] | 26 | } |
| 27 | |
| 28 | Status Status::DataError() { |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 29 | return Status(blink::kWebCryptoErrorTypeData, ""); |
[email protected] | c503936 | 2014-04-28 19:10:34 | [diff] [blame] | 30 | } |
[email protected] | 37be4cfa | 2014-03-20 05:39:37 | [diff] [blame] | 31 | |
| 32 | Status Status::ErrorJwkNotDictionary() { |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 33 | return Status(blink::kWebCryptoErrorTypeData, |
[email protected] | c503936 | 2014-04-28 19:10:34 | [diff] [blame] | 34 | "JWK input could not be parsed to a JSON dictionary"); |
[email protected] | 37be4cfa | 2014-03-20 05:39:37 | [diff] [blame] | 35 | } |
| 36 | |
eroman | 398e7e1 | 2014-11-08 03:05:10 | [diff] [blame] | 37 | Status Status::ErrorJwkMemberMissing(const std::string& member_name) { |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 38 | return Status(blink::kWebCryptoErrorTypeData, |
eroman | 8c565ba | 2014-12-10 20:59:06 | [diff] [blame] | 39 | "The required JWK member \"" + member_name + "\" was missing"); |
[email protected] | 37be4cfa | 2014-03-20 05:39:37 | [diff] [blame] | 40 | } |
| 41 | |
eroman | 398e7e1 | 2014-11-08 03:05:10 | [diff] [blame] | 42 | Status Status::ErrorJwkMemberWrongType(const std::string& member_name, |
| 43 | const std::string& expected_type) { |
[email protected] | c503936 | 2014-04-28 19:10:34 | [diff] [blame] | 44 | return Status( |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 45 | blink::kWebCryptoErrorTypeData, |
eroman | 8c565ba | 2014-12-10 20:59:06 | [diff] [blame] | 46 | "The JWK member \"" + member_name + "\" must be a " + expected_type); |
eroman | 398e7e1 | 2014-11-08 03:05:10 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | Status Status::ErrorJwkBase64Decode(const std::string& member_name) { |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 50 | return Status(blink::kWebCryptoErrorTypeData, |
eroman | 8c565ba | 2014-12-10 20:59:06 | [diff] [blame] | 51 | "The JWK member \"" + member_name + |
| 52 | "\" could not be base64url decoded or contained padding"); |
[email protected] | 37be4cfa | 2014-03-20 05:39:37 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | Status Status::ErrorJwkExtInconsistent() { |
| 56 | return Status( |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 57 | blink::kWebCryptoErrorTypeData, |
eroman | 8c565ba | 2014-12-10 20:59:06 | [diff] [blame] | 58 | "The \"ext\" member of the JWK dictionary is inconsistent what that " |
[email protected] | 37be4cfa | 2014-03-20 05:39:37 | [diff] [blame] | 59 | "specified by the Web Crypto call"); |
| 60 | } |
| 61 | |
[email protected] | 37be4cfa | 2014-03-20 05:39:37 | [diff] [blame] | 62 | Status Status::ErrorJwkAlgorithmInconsistent() { |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 63 | return Status(blink::kWebCryptoErrorTypeData, |
eroman | 8c565ba | 2014-12-10 20:59:06 | [diff] [blame] | 64 | "The JWK \"alg\" member was inconsistent with that specified " |
[email protected] | c503936 | 2014-04-28 19:10:34 | [diff] [blame] | 65 | "by the Web Crypto call"); |
[email protected] | 37be4cfa | 2014-03-20 05:39:37 | [diff] [blame] | 66 | } |
| 67 | |
[email protected] | 37be4cfa | 2014-03-20 05:39:37 | [diff] [blame] | 68 | Status Status::ErrorJwkUnrecognizedUse() { |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 69 | return Status(blink::kWebCryptoErrorTypeData, |
eroman | 8c565ba | 2014-12-10 20:59:06 | [diff] [blame] | 70 | "The JWK \"use\" member could not be parsed"); |
[email protected] | 37be4cfa | 2014-03-20 05:39:37 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | Status Status::ErrorJwkUnrecognizedKeyop() { |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 74 | return Status(blink::kWebCryptoErrorTypeData, |
eroman | 8c565ba | 2014-12-10 20:59:06 | [diff] [blame] | 75 | "The JWK \"key_ops\" member could not be parsed"); |
[email protected] | 37be4cfa | 2014-03-20 05:39:37 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | Status Status::ErrorJwkUseInconsistent() { |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 79 | return Status(blink::kWebCryptoErrorTypeData, |
eroman | 8c565ba | 2014-12-10 20:59:06 | [diff] [blame] | 80 | "The JWK \"use\" member was inconsistent with that specified " |
[email protected] | c503936 | 2014-04-28 19:10:34 | [diff] [blame] | 81 | "by the Web Crypto call. The JWK usage must be a superset of " |
| 82 | "those requested"); |
[email protected] | 37be4cfa | 2014-03-20 05:39:37 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | Status Status::ErrorJwkKeyopsInconsistent() { |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 86 | return Status(blink::kWebCryptoErrorTypeData, |
eroman | 8c565ba | 2014-12-10 20:59:06 | [diff] [blame] | 87 | "The JWK \"key_ops\" member was inconsistent with that " |
[email protected] | c503936 | 2014-04-28 19:10:34 | [diff] [blame] | 88 | "specified by the Web Crypto call. The JWK usage must be a " |
| 89 | "superset of those requested"); |
[email protected] | 37be4cfa | 2014-03-20 05:39:37 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | Status Status::ErrorJwkUseAndKeyopsInconsistent() { |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 93 | return Status(blink::kWebCryptoErrorTypeData, |
[email protected] | c503936 | 2014-04-28 19:10:34 | [diff] [blame] | 94 | "The JWK \"use\" and \"key_ops\" properties were both found " |
| 95 | "but are inconsistent with each other."); |
[email protected] | 37be4cfa | 2014-03-20 05:39:37 | [diff] [blame] | 96 | } |
| 97 | |
[email protected] | 38409aec | 2014-07-19 00:54:51 | [diff] [blame] | 98 | Status Status::ErrorJwkUnexpectedKty(const std::string& expected) { |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 99 | return Status(blink::kWebCryptoErrorTypeData, |
eroman | 8c565ba | 2014-12-10 20:59:06 | [diff] [blame] | 100 | "The JWK \"kty\" member was not \"" + expected + "\""); |
[email protected] | 37be4cfa | 2014-03-20 05:39:37 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | Status Status::ErrorJwkIncorrectKeyLength() { |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 104 | return Status(blink::kWebCryptoErrorTypeData, |
eroman | 8c565ba | 2014-12-10 20:59:06 | [diff] [blame] | 105 | "The JWK \"k\" member did not include the right length " |
[email protected] | c503936 | 2014-04-28 19:10:34 | [diff] [blame] | 106 | "of key data for the given algorithm."); |
[email protected] | 37be4cfa | 2014-03-20 05:39:37 | [diff] [blame] | 107 | } |
| 108 | |
eroman | 398e7e1 | 2014-11-08 03:05:10 | [diff] [blame] | 109 | Status Status::ErrorJwkEmptyBigInteger(const std::string& member_name) { |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 110 | return Status(blink::kWebCryptoErrorTypeData, |
eroman | 8c565ba | 2014-12-10 20:59:06 | [diff] [blame] | 111 | "The JWK \"" + member_name + "\" member was empty."); |
[email protected] | 31130d6c | 2014-07-26 19:59:14 | [diff] [blame] | 112 | } |
| 113 | |
eroman | 398e7e1 | 2014-11-08 03:05:10 | [diff] [blame] | 114 | Status Status::ErrorJwkBigIntegerHasLeadingZero( |
| 115 | const std::string& member_name) { |
[email protected] | 31130d6c | 2014-07-26 19:59:14 | [diff] [blame] | 116 | return Status( |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 117 | blink::kWebCryptoErrorTypeData, |
eroman | 8c565ba | 2014-12-10 20:59:06 | [diff] [blame] | 118 | "The JWK \"" + member_name + "\" member contained a leading zero."); |
[email protected] | 6f82af2 | 2014-05-21 15:26:14 | [diff] [blame] | 119 | } |
| 120 | |
eroman | ca92d1e | 2014-10-28 01:15:18 | [diff] [blame] | 121 | Status Status::ErrorJwkDuplicateKeyOps() { |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 122 | return Status(blink::kWebCryptoErrorTypeData, |
eroman | 8c565ba | 2014-12-10 20:59:06 | [diff] [blame] | 123 | "The \"key_ops\" member of the JWK dictionary contains " |
eroman | ca92d1e | 2014-10-28 01:15:18 | [diff] [blame] | 124 | "duplicate usages."); |
| 125 | } |
| 126 | |
[email protected] | 38409aec | 2014-07-19 00:54:51 | [diff] [blame] | 127 | Status Status::ErrorUnsupportedImportKeyFormat() { |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 128 | return Status(blink::kWebCryptoErrorTypeNotSupported, |
[email protected] | 38409aec | 2014-07-19 00:54:51 | [diff] [blame] | 129 | "Unsupported import key format for algorithm"); |
| 130 | } |
| 131 | |
| 132 | Status Status::ErrorUnsupportedExportKeyFormat() { |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 133 | return Status(blink::kWebCryptoErrorTypeNotSupported, |
[email protected] | 38409aec | 2014-07-19 00:54:51 | [diff] [blame] | 134 | "Unsupported export key format for algorithm"); |
| 135 | } |
| 136 | |
[email protected] | c503936 | 2014-04-28 19:10:34 | [diff] [blame] | 137 | Status Status::ErrorImportAesKeyLength() { |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 138 | return Status(blink::kWebCryptoErrorTypeData, |
eroman | be884dce | 2014-11-26 21:23:29 | [diff] [blame] | 139 | "AES key data must be 128 or 256 bits"); |
[email protected] | 37be4cfa | 2014-03-20 05:39:37 | [diff] [blame] | 140 | } |
| 141 | |
eroman | f93fd5b | 2014-12-11 00:21:06 | [diff] [blame] | 142 | Status Status::ErrorGetAesKeyLength() { |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 143 | return Status(blink::kWebCryptoErrorTypeOperation, |
eroman | f93fd5b | 2014-12-11 00:21:06 | [diff] [blame] | 144 | "AES key length must be 128 or 256 bits"); |
| 145 | } |
| 146 | |
eroman | 9e9ed05 | 2014-11-26 03:00:40 | [diff] [blame] | 147 | Status Status::ErrorGenerateAesKeyLength() { |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 148 | return Status(blink::kWebCryptoErrorTypeOperation, |
eroman | 9e9ed05 | 2014-11-26 03:00:40 | [diff] [blame] | 149 | "AES key length must be 128 or 256 bits"); |
| 150 | } |
| 151 | |
[email protected] | b05ba93 | 2014-06-13 08:49:15 | [diff] [blame] | 152 | Status Status::ErrorAes192BitUnsupported() { |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 153 | return Status(blink::kWebCryptoErrorTypeOperation, |
[email protected] | b05ba93 | 2014-06-13 08:49:15 | [diff] [blame] | 154 | "192-bit AES keys are not supported"); |
| 155 | } |
| 156 | |
[email protected] | 37be4cfa | 2014-03-20 05:39:37 | [diff] [blame] | 157 | Status Status::ErrorUnexpectedKeyType() { |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 158 | return Status(blink::kWebCryptoErrorTypeInvalidAccess, |
[email protected] | c503936 | 2014-04-28 19:10:34 | [diff] [blame] | 159 | "The key is not of the expected type"); |
[email protected] | 37be4cfa | 2014-03-20 05:39:37 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | Status Status::ErrorIncorrectSizeAesCbcIv() { |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 163 | return Status(blink::kWebCryptoErrorTypeOperation, |
[email protected] | c503936 | 2014-04-28 19:10:34 | [diff] [blame] | 164 | "The \"iv\" has an unexpected length -- must be 16 bytes"); |
[email protected] | 37be4cfa | 2014-03-20 05:39:37 | [diff] [blame] | 165 | } |
| 166 | |
eroman | 4d7a0e0 | 2014-08-27 00:30:33 | [diff] [blame] | 167 | Status Status::ErrorIncorrectSizeAesCtrCounter() { |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 168 | return Status(blink::kWebCryptoErrorTypeOperation, |
eroman | 4d7a0e0 | 2014-08-27 00:30:33 | [diff] [blame] | 169 | "The \"counter\" has an unexpected length -- must be 16 bytes"); |
| 170 | } |
| 171 | |
| 172 | Status Status::ErrorInvalidAesCtrCounterLength() { |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 173 | return Status(blink::kWebCryptoErrorTypeOperation, |
eroman | 8c565ba | 2014-12-10 20:59:06 | [diff] [blame] | 174 | "The \"length\" member must be >= 1 and <= 128"); |
eroman | 4d7a0e0 | 2014-08-27 00:30:33 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | Status Status::ErrorAesCtrInputTooLongCounterRepeated() { |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 178 | return Status(blink::kWebCryptoErrorTypeData, |
eroman | 4d7a0e0 | 2014-08-27 00:30:33 | [diff] [blame] | 179 | "The input is too large for the counter length."); |
| 180 | } |
| 181 | |
[email protected] | 37be4cfa | 2014-03-20 05:39:37 | [diff] [blame] | 182 | Status Status::ErrorDataTooLarge() { |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 183 | return Status(blink::kWebCryptoErrorTypeOperation, |
[email protected] | c503936 | 2014-04-28 19:10:34 | [diff] [blame] | 184 | "The provided data is too large"); |
[email protected] | 37be4cfa | 2014-03-20 05:39:37 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | Status Status::ErrorDataTooSmall() { |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 188 | return Status(blink::kWebCryptoErrorTypeOperation, |
[email protected] | c503936 | 2014-04-28 19:10:34 | [diff] [blame] | 189 | "The provided data is too small"); |
[email protected] | 37be4cfa | 2014-03-20 05:39:37 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | Status Status::ErrorUnsupported() { |
[email protected] | cb7f3e3 | 2014-06-19 14:41:41 | [diff] [blame] | 193 | return ErrorUnsupported("The requested operation is unsupported"); |
| 194 | } |
| 195 | |
| 196 | Status Status::ErrorUnsupported(const std::string& message) { |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 197 | return Status(blink::kWebCryptoErrorTypeNotSupported, message); |
[email protected] | 37be4cfa | 2014-03-20 05:39:37 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | Status Status::ErrorUnexpected() { |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 201 | return Status(blink::kWebCryptoErrorTypeOperation, |
[email protected] | c503936 | 2014-04-28 19:10:34 | [diff] [blame] | 202 | "Something unexpected happened..."); |
[email protected] | 37be4cfa | 2014-03-20 05:39:37 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | Status Status::ErrorInvalidAesGcmTagLength() { |
| 206 | return Status( |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 207 | blink::kWebCryptoErrorTypeOperation, |
[email protected] | 37be4cfa | 2014-03-20 05:39:37 | [diff] [blame] | 208 | "The tag length is invalid: Must be 32, 64, 96, 104, 112, 120, or 128 " |
| 209 | "bits"); |
| 210 | } |
| 211 | |
| 212 | Status Status::ErrorInvalidAesKwDataLength() { |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 213 | return Status(blink::kWebCryptoErrorTypeData, |
[email protected] | c503936 | 2014-04-28 19:10:34 | [diff] [blame] | 214 | "The AES-KW input data length is invalid: not a multiple of 8 " |
| 215 | "bytes"); |
[email protected] | 37be4cfa | 2014-03-20 05:39:37 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | Status Status::ErrorGenerateKeyPublicExponent() { |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 219 | return Status(blink::kWebCryptoErrorTypeOperation, |
[email protected] | c360f83 | 2014-06-12 13:21:50 | [diff] [blame] | 220 | "The \"publicExponent\" must be either 3 or 65537"); |
[email protected] | 37be4cfa | 2014-03-20 05:39:37 | [diff] [blame] | 221 | } |
| 222 | |
[email protected] | 37be4cfa | 2014-03-20 05:39:37 | [diff] [blame] | 223 | Status Status::ErrorImportRsaEmptyModulus() { |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 224 | return Status(blink::kWebCryptoErrorTypeData, "The modulus is empty"); |
[email protected] | 37be4cfa | 2014-03-20 05:39:37 | [diff] [blame] | 225 | } |
| 226 | |
[email protected] | e9aa71a | 2014-07-25 08:38:02 | [diff] [blame] | 227 | Status Status::ErrorGenerateRsaUnsupportedModulus() { |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 228 | return Status(blink::kWebCryptoErrorTypeOperation, |
[email protected] | e9aa71a | 2014-07-25 08:38:02 | [diff] [blame] | 229 | "The modulus length must be a multiple of 8 bits and >= 256 " |
| 230 | "and <= 16384"); |
[email protected] | 37be4cfa | 2014-03-20 05:39:37 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | Status Status::ErrorImportRsaEmptyExponent() { |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 234 | return Status(blink::kWebCryptoErrorTypeData, |
[email protected] | c503936 | 2014-04-28 19:10:34 | [diff] [blame] | 235 | "No bytes for the exponent were provided"); |
[email protected] | 37be4cfa | 2014-03-20 05:39:37 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | Status Status::ErrorKeyNotExtractable() { |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 239 | return Status(blink::kWebCryptoErrorTypeInvalidAccess, |
[email protected] | c503936 | 2014-04-28 19:10:34 | [diff] [blame] | 240 | "They key is not extractable"); |
[email protected] | 37be4cfa | 2014-03-20 05:39:37 | [diff] [blame] | 241 | } |
| 242 | |
eroman | 9e9ed05 | 2014-11-26 03:00:40 | [diff] [blame] | 243 | Status Status::ErrorGenerateHmacKeyLengthZero() { |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 244 | return Status(blink::kWebCryptoErrorTypeOperation, |
xun.sun | e573755 | 2014-12-02 23:52:24 | [diff] [blame] | 245 | "HMAC key length must not be zero"); |
eroman | 9e9ed05 | 2014-11-26 03:00:40 | [diff] [blame] | 246 | } |
| 247 | |
eroman | 825de71 | 2014-12-29 22:34:10 | [diff] [blame] | 248 | Status Status::ErrorHmacImportEmptyKey() { |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 249 | return Status(blink::kWebCryptoErrorTypeData, |
eroman | 825de71 | 2014-12-29 22:34:10 | [diff] [blame] | 250 | "HMAC key data must not be empty"); |
| 251 | } |
| 252 | |
eroman | f93fd5b | 2014-12-11 00:21:06 | [diff] [blame] | 253 | Status Status::ErrorGetHmacKeyLengthZero() { |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 254 | return Status(blink::kWebCryptoErrorTypeType, |
eroman | f93fd5b | 2014-12-11 00:21:06 | [diff] [blame] | 255 | "HMAC key length must not be zero"); |
| 256 | } |
| 257 | |
eroman | 5d519974 | 2014-12-10 22:18:07 | [diff] [blame] | 258 | Status Status::ErrorHmacImportBadLength() { |
| 259 | return Status( |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 260 | blink::kWebCryptoErrorTypeData, |
eroman | 5d519974 | 2014-12-10 22:18:07 | [diff] [blame] | 261 | "The optional HMAC key length must be shorter than the key data, and by " |
| 262 | "no more than 7 bits."); |
| 263 | } |
| 264 | |
[email protected] | 9c2e9cf | 2014-05-23 23:13:47 | [diff] [blame] | 265 | Status Status::ErrorCreateKeyBadUsages() { |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 266 | return Status(blink::kWebCryptoErrorTypeSyntax, |
[email protected] | 9c2e9cf | 2014-05-23 23:13:47 | [diff] [blame] | 267 | "Cannot create a key using the specified key usages."); |
| 268 | } |
| 269 | |
nharper | 70f820a | 2014-12-03 20:36:07 | [diff] [blame] | 270 | Status Status::ErrorCreateKeyEmptyUsages() { |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 271 | return Status(blink::kWebCryptoErrorTypeSyntax, |
nharper | 70f820a | 2014-12-03 20:36:07 | [diff] [blame] | 272 | "Usages cannot be empty when creating a key."); |
| 273 | } |
| 274 | |
eroman | b2ead6d | 2014-11-14 02:26:14 | [diff] [blame] | 275 | Status Status::ErrorImportedEcKeyIncorrectCurve() { |
| 276 | return Status( |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 277 | blink::kWebCryptoErrorTypeData, |
eroman | b2ead6d | 2014-11-14 02:26:14 | [diff] [blame] | 278 | "The imported EC key specifies a different curve than requested"); |
| 279 | } |
| 280 | |
| 281 | Status Status::ErrorJwkIncorrectCrv() { |
| 282 | return Status( |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 283 | blink::kWebCryptoErrorTypeData, |
eroman | b2ead6d | 2014-11-14 02:26:14 | [diff] [blame] | 284 | "The JWK's \"crv\" member specifies a different curve than requested"); |
| 285 | } |
| 286 | |
| 287 | Status Status::ErrorEcKeyInvalid() { |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 288 | return Status(blink::kWebCryptoErrorTypeData, |
eroman | b2ead6d | 2014-11-14 02:26:14 | [diff] [blame] | 289 | "The imported EC key is invalid"); |
| 290 | } |
| 291 | |
| 292 | Status Status::JwkOctetStringWrongLength(const std::string& member_name, |
| 293 | size_t expected_length, |
| 294 | size_t actual_length) { |
| 295 | return Status( |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 296 | blink::kWebCryptoErrorTypeData, |
eroman | b2ead6d | 2014-11-14 02:26:14 | [diff] [blame] | 297 | base::StringPrintf( |
| 298 | "The JWK's \"%s\" member defines an octet string of length %" PRIuS |
| 299 | " bytes but should be %" PRIuS, |
| 300 | member_name.c_str(), actual_length, expected_length)); |
| 301 | } |
| 302 | |
eroman | ed48e81 | 2014-11-28 19:59:13 | [diff] [blame] | 303 | Status Status::ErrorEcdhPublicKeyWrongType() { |
| 304 | return Status( |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 305 | blink::kWebCryptoErrorTypeInvalidAccess, |
eroman | ed48e81 | 2014-11-28 19:59:13 | [diff] [blame] | 306 | "The public parameter for ECDH key derivation is not a public EC key"); |
| 307 | } |
| 308 | |
| 309 | Status Status::ErrorEcdhPublicKeyWrongAlgorithm() { |
| 310 | return Status( |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 311 | blink::kWebCryptoErrorTypeInvalidAccess, |
eroman | ed48e81 | 2014-11-28 19:59:13 | [diff] [blame] | 312 | "The public parameter for ECDH key derivation must be for ECDH"); |
| 313 | } |
| 314 | |
| 315 | Status Status::ErrorEcdhCurveMismatch() { |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 316 | return Status(blink::kWebCryptoErrorTypeInvalidAccess, |
eroman | ed48e81 | 2014-11-28 19:59:13 | [diff] [blame] | 317 | "The public parameter for ECDH key derivation is for a " |
| 318 | "different named curve"); |
| 319 | } |
| 320 | |
| 321 | Status Status::ErrorEcdhLengthTooBig(unsigned int max_length_bits) { |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 322 | return Status(blink::kWebCryptoErrorTypeOperation, |
eroman | ed48e81 | 2014-11-28 19:59:13 | [diff] [blame] | 323 | base::StringPrintf( |
| 324 | "Length specified for ECDH key derivation is too large. " |
| 325 | "Maximum allowed is %u bits", |
| 326 | max_length_bits)); |
| 327 | } |
| 328 | |
nharper | 65103179 | 2015-01-13 18:10:39 | [diff] [blame] | 329 | Status Status::ErrorHkdfLengthTooLong() { |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 330 | return Status(blink::kWebCryptoErrorTypeOperation, |
nharper | 65103179 | 2015-01-13 18:10:39 | [diff] [blame] | 331 | "The length provided for HKDF is too large."); |
| 332 | } |
| 333 | |
eroman | 4451821b | 2017-04-26 19:30:24 | [diff] [blame] | 334 | Status Status::ErrorHkdfLengthNotWholeByte() { |
| 335 | return Status(blink::kWebCryptoErrorTypeOperation, |
| 336 | "The length provided for HKDF is not a multiple of 8 bits."); |
| 337 | } |
| 338 | |
nharper | 65103179 | 2015-01-13 18:10:39 | [diff] [blame] | 339 | Status Status::ErrorHkdfDeriveBitsLengthNotSpecified() { |
| 340 | // TODO(nharper): The spec might change so that an OperationError should be |
| 341 | // thrown here instead of a TypeError. |
| 342 | // (https://www.w3.org/Bugs/Public/show_bug.cgi?id=27771) |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 343 | return Status(blink::kWebCryptoErrorTypeType, |
nharper | 65103179 | 2015-01-13 18:10:39 | [diff] [blame] | 344 | "No length was specified for the HKDF Derive Bits operation."); |
| 345 | } |
| 346 | |
xun.sun | 22a80e7 | 2015-01-21 13:57:19 | [diff] [blame] | 347 | Status Status::ErrorPbkdf2InvalidLength() { |
| 348 | return Status( |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 349 | blink::kWebCryptoErrorTypeOperation, |
xun.sun | 22a80e7 | 2015-01-21 13:57:19 | [diff] [blame] | 350 | "Length for PBKDF2 key derivation must be a multiple of 8 bits."); |
| 351 | } |
| 352 | |
| 353 | Status Status::ErrorPbkdf2DeriveBitsLengthNotSpecified() { |
| 354 | return Status( |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 355 | blink::kWebCryptoErrorTypeOperation, |
xun.sun | 22a80e7 | 2015-01-21 13:57:19 | [diff] [blame] | 356 | "No length was specified for the PBKDF2 Derive Bits operation."); |
| 357 | } |
| 358 | |
eroman | 2d840b6 | 2016-07-19 22:04:21 | [diff] [blame] | 359 | Status Status::ErrorPbkdf2DeriveBitsLengthZero() { |
| 360 | return Status( |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 361 | blink::kWebCryptoErrorTypeOperation, |
eroman | 2d840b6 | 2016-07-19 22:04:21 | [diff] [blame] | 362 | "A length of 0 was specified for PBKDF2's Derive Bits operation."); |
| 363 | } |
| 364 | |
eroman | 7961664 | 2015-10-05 21:18:14 | [diff] [blame] | 365 | Status Status::ErrorPbkdf2Iterations0() { |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 366 | return Status(blink::kWebCryptoErrorTypeOperation, |
eroman | 7961664 | 2015-10-05 21:18:14 | [diff] [blame] | 367 | "PBKDF2 requires iterations > 0"); |
| 368 | } |
| 369 | |
eroman | c9529a8 | 2016-09-07 18:59:26 | [diff] [blame] | 370 | Status Status::ErrorImportExtractableKdfKey() { |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 371 | return Status(blink::kWebCryptoErrorTypeSyntax, |
eroman | c9529a8 | 2016-09-07 18:59:26 | [diff] [blame] | 372 | "KDF keys must set extractable=false"); |
| 373 | } |
| 374 | |
[email protected] | c503936 | 2014-04-28 19:10:34 | [diff] [blame] | 375 | Status::Status(blink::WebCryptoErrorType error_type, |
| 376 | const std::string& error_details_utf8) |
| 377 | : type_(TYPE_ERROR), |
| 378 | error_type_(error_type), |
| 379 | error_details_(error_details_utf8) { |
| 380 | } |
[email protected] | 37be4cfa | 2014-03-20 05:39:37 | [diff] [blame] | 381 | |
[email protected] | c503936 | 2014-04-28 19:10:34 | [diff] [blame] | 382 | Status::Status(Type type) : type_(type) { |
| 383 | } |
[email protected] | 37be4cfa | 2014-03-20 05:39:37 | [diff] [blame] | 384 | |
| 385 | } // namespace webcrypto |