Home | History | Annotate | Download | only in webcrypto
      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 
      5 #include "content/child/webcrypto/status.h"
      6 
      7 namespace content {
      8 
      9 namespace webcrypto {
     10 
     11 bool Status::IsError() const {
     12   return type_ == TYPE_ERROR;
     13 }
     14 
     15 bool Status::IsSuccess() const {
     16   return type_ == TYPE_SUCCESS;
     17 }
     18 
     19 Status Status::Success() {
     20   return Status(TYPE_SUCCESS);
     21 }
     22 
     23 Status Status::OperationError() {
     24   return Status(blink::WebCryptoErrorTypeOperation, "");
     25 }
     26 
     27 Status Status::DataError() {
     28   return Status(blink::WebCryptoErrorTypeData, "");
     29 }
     30 
     31 Status Status::ErrorJwkNotDictionary() {
     32   return Status(blink::WebCryptoErrorTypeData,
     33                 "JWK input could not be parsed to a JSON dictionary");
     34 }
     35 
     36 Status Status::ErrorJwkPropertyMissing(const std::string& property) {
     37   return Status(blink::WebCryptoErrorTypeData,
     38                 "The required JWK property \"" + property + "\" was missing");
     39 }
     40 
     41 Status Status::ErrorJwkPropertyWrongType(const std::string& property,
     42                                          const std::string& expected_type) {
     43   return Status(
     44       blink::WebCryptoErrorTypeData,
     45       "The JWK property \"" + property + "\" must be a " + expected_type);
     46 }
     47 
     48 Status Status::ErrorJwkBase64Decode(const std::string& property) {
     49   return Status(
     50       blink::WebCryptoErrorTypeData,
     51       "The JWK property \"" + property + "\" could not be base64 decoded");
     52 }
     53 
     54 Status Status::ErrorJwkExtInconsistent() {
     55   return Status(
     56       blink::WebCryptoErrorTypeData,
     57       "The \"ext\" property of the JWK dictionary is inconsistent what that "
     58       "specified by the Web Crypto call");
     59 }
     60 
     61 Status Status::ErrorJwkAlgorithmInconsistent() {
     62   return Status(blink::WebCryptoErrorTypeData,
     63                 "The JWK \"alg\" property was inconsistent with that specified "
     64                 "by the Web Crypto call");
     65 }
     66 
     67 Status Status::ErrorJwkUnrecognizedUse() {
     68   return Status(blink::WebCryptoErrorTypeData,
     69                 "The JWK \"use\" property could not be parsed");
     70 }
     71 
     72 Status Status::ErrorJwkUnrecognizedKeyop() {
     73   return Status(blink::WebCryptoErrorTypeData,
     74                 "The JWK \"key_ops\" property could not be parsed");
     75 }
     76 
     77 Status Status::ErrorJwkUseInconsistent() {
     78   return Status(blink::WebCryptoErrorTypeData,
     79                 "The JWK \"use\" property was inconsistent with that specified "
     80                 "by the Web Crypto call. The JWK usage must be a superset of "
     81                 "those requested");
     82 }
     83 
     84 Status Status::ErrorJwkKeyopsInconsistent() {
     85   return Status(blink::WebCryptoErrorTypeData,
     86                 "The JWK \"key_ops\" property was inconsistent with that "
     87                 "specified by the Web Crypto call. The JWK usage must be a "
     88                 "superset of those requested");
     89 }
     90 
     91 Status Status::ErrorJwkUseAndKeyopsInconsistent() {
     92   return Status(blink::WebCryptoErrorTypeData,
     93                 "The JWK \"use\" and \"key_ops\" properties were both found "
     94                 "but are inconsistent with each other.");
     95 }
     96 
     97 Status Status::ErrorJwkUnexpectedKty(const std::string& expected) {
     98   return Status(blink::WebCryptoErrorTypeData,
     99                 "The JWK \"kty\" property was not \"" + expected + "\"");
    100 }
    101 
    102 Status Status::ErrorJwkIncorrectKeyLength() {
    103   return Status(blink::WebCryptoErrorTypeData,
    104                 "The JWK \"k\" property did not include the right length "
    105                 "of key data for the given algorithm.");
    106 }
    107 
    108 Status Status::ErrorJwkEmptyBigInteger(const std::string& property) {
    109   return Status(blink::WebCryptoErrorTypeData,
    110                 "The JWK \"" + property + "\" property was empty.");
    111 }
    112 
    113 Status Status::ErrorJwkBigIntegerHasLeadingZero(const std::string& property) {
    114   return Status(
    115       blink::WebCryptoErrorTypeData,
    116       "The JWK \"" + property + "\" property contained a leading zero.");
    117 }
    118 
    119 Status Status::ErrorImportEmptyKeyData() {
    120   return Status(blink::WebCryptoErrorTypeData, "No key data was provided");
    121 }
    122 
    123 Status Status::ErrorUnsupportedImportKeyFormat() {
    124   return Status(blink::WebCryptoErrorTypeNotSupported,
    125                 "Unsupported import key format for algorithm");
    126 }
    127 
    128 Status Status::ErrorUnsupportedExportKeyFormat() {
    129   return Status(blink::WebCryptoErrorTypeNotSupported,
    130                 "Unsupported export key format for algorithm");
    131 }
    132 
    133 Status Status::ErrorImportAesKeyLength() {
    134   return Status(blink::WebCryptoErrorTypeData,
    135                 "AES key data must be 128, 192 or 256 bits");
    136 }
    137 
    138 Status Status::ErrorAes192BitUnsupported() {
    139   return Status(blink::WebCryptoErrorTypeNotSupported,
    140                 "192-bit AES keys are not supported");
    141 }
    142 
    143 Status Status::ErrorUnexpectedKeyType() {
    144   return Status(blink::WebCryptoErrorTypeInvalidAccess,
    145                 "The key is not of the expected type");
    146 }
    147 
    148 Status Status::ErrorIncorrectSizeAesCbcIv() {
    149   return Status(blink::WebCryptoErrorTypeData,
    150                 "The \"iv\" has an unexpected length -- must be 16 bytes");
    151 }
    152 
    153 Status Status::ErrorIncorrectSizeAesCtrCounter() {
    154   return Status(blink::WebCryptoErrorTypeData,
    155                 "The \"counter\" has an unexpected length -- must be 16 bytes");
    156 }
    157 
    158 Status Status::ErrorInvalidAesCtrCounterLength() {
    159   return Status(blink::WebCryptoErrorTypeData,
    160                 "The \"length\" property must be >= 1 and <= 128");
    161 }
    162 
    163 Status Status::ErrorAesCtrInputTooLongCounterRepeated() {
    164   return Status(blink::WebCryptoErrorTypeData,
    165                 "The input is too large for the counter length.");
    166 }
    167 
    168 Status Status::ErrorDataTooLarge() {
    169   return Status(blink::WebCryptoErrorTypeData,
    170                 "The provided data is too large");
    171 }
    172 
    173 Status Status::ErrorDataTooSmall() {
    174   return Status(blink::WebCryptoErrorTypeData,
    175                 "The provided data is too small");
    176 }
    177 
    178 Status Status::ErrorUnsupported() {
    179   return ErrorUnsupported("The requested operation is unsupported");
    180 }
    181 
    182 Status Status::ErrorUnsupported(const std::string& message) {
    183   return Status(blink::WebCryptoErrorTypeNotSupported, message);
    184 }
    185 
    186 Status Status::ErrorUnexpected() {
    187   return Status(blink::WebCryptoErrorTypeUnknown,
    188                 "Something unexpected happened...");
    189 }
    190 
    191 Status Status::ErrorInvalidAesGcmTagLength() {
    192   return Status(
    193       blink::WebCryptoErrorTypeData,
    194       "The tag length is invalid: Must be 32, 64, 96, 104, 112, 120, or 128 "
    195       "bits");
    196 }
    197 
    198 Status Status::ErrorInvalidAesKwDataLength() {
    199   return Status(blink::WebCryptoErrorTypeData,
    200                 "The AES-KW input data length is invalid: not a multiple of 8 "
    201                 "bytes");
    202 }
    203 
    204 Status Status::ErrorGenerateKeyPublicExponent() {
    205   return Status(blink::WebCryptoErrorTypeData,
    206                 "The \"publicExponent\" must be either 3 or 65537");
    207 }
    208 
    209 Status Status::ErrorImportRsaEmptyModulus() {
    210   return Status(blink::WebCryptoErrorTypeData, "The modulus is empty");
    211 }
    212 
    213 Status Status::ErrorGenerateRsaUnsupportedModulus() {
    214   return Status(blink::WebCryptoErrorTypeNotSupported,
    215                 "The modulus length must be a multiple of 8 bits and >= 256 "
    216                 "and <= 16384");
    217 }
    218 
    219 Status Status::ErrorImportRsaEmptyExponent() {
    220   return Status(blink::WebCryptoErrorTypeData,
    221                 "No bytes for the exponent were provided");
    222 }
    223 
    224 Status Status::ErrorKeyNotExtractable() {
    225   return Status(blink::WebCryptoErrorTypeInvalidAccess,
    226                 "They key is not extractable");
    227 }
    228 
    229 Status Status::ErrorGenerateKeyLength() {
    230   return Status(blink::WebCryptoErrorTypeData,
    231                 "Invalid key length: it is either zero or not a multiple of 8 "
    232                 "bits");
    233 }
    234 
    235 Status Status::ErrorCreateKeyBadUsages() {
    236   return Status(blink::WebCryptoErrorTypeData,
    237                 "Cannot create a key using the specified key usages.");
    238 }
    239 
    240 Status::Status(blink::WebCryptoErrorType error_type,
    241                const std::string& error_details_utf8)
    242     : type_(TYPE_ERROR),
    243       error_type_(error_type),
    244       error_details_(error_details_utf8) {
    245 }
    246 
    247 Status::Status(Type type) : type_(type) {
    248 }
    249 
    250 }  // namespace webcrypto
    251 
    252 }  // namespace content
    253