status.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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. #ifndef COMPONENTS_WEBCRYPTO_STATUS_H_
  5. #define COMPONENTS_WEBCRYPTO_STATUS_H_
  6. #include <stddef.h>
  7. #include <string>
  8. #include "third_party/blink/public/platform/web_crypto.h"
  9. namespace webcrypto {
  10. // Status indicates whether an operation completed successfully, or with an
  11. // error. The error is used for verification in unit-tests, as well as for
  12. // display to the user.
  13. //
  14. // As such, it is important that errors DO NOT reveal any sensitive material
  15. // (like key bytes).
  16. class Status {
  17. public:
  18. Status() : type_(TYPE_ERROR) {}
  19. // Returns true if the Status represents an error (any one of them).
  20. bool IsError() const;
  21. // Returns true if the Status represent success.
  22. bool IsSuccess() const;
  23. // Returns a UTF-8 error message (non-localized) describing the error.
  24. const std::string& error_details() const { return error_details_; }
  25. blink::WebCryptoErrorType error_type() const { return error_type_; }
  26. // Constructs a status representing success.
  27. static Status Success();
  28. // Constructs a status representing a generic operation error. It contains no
  29. // extra details.
  30. static Status OperationError();
  31. // Constructs a status representing a generic data error. It contains no
  32. // extra details.
  33. static Status DataError();
  34. // ------------------------------------
  35. // Errors when importing a JWK formatted key
  36. // ------------------------------------
  37. // The key bytes could not parsed as JSON dictionary. This either
  38. // means there was a parsing error, or the JSON object was not
  39. // convertable to a dictionary.
  40. static Status ErrorJwkNotDictionary();
  41. // The required JWK member |member_name| was missing.
  42. static Status ErrorJwkMemberMissing(const std::string& member_name);
  43. // The JWK member |member_name| was not of type |expected_type|.
  44. static Status ErrorJwkMemberWrongType(const std::string& member_name,
  45. const std::string& expected_type);
  46. // The JWK member |member_name| was a string, however could not be
  47. // successfully base64 decoded.
  48. static Status ErrorJwkBase64Decode(const std::string& member_name);
  49. // The "ext" parameter was specified but was
  50. // incompatible with the value requested by the Web Crypto call.
  51. static Status ErrorJwkExtInconsistent();
  52. // The "alg" parameter is incompatible with the (optional) Algorithm
  53. // specified by the Web Crypto import operation.
  54. static Status ErrorJwkAlgorithmInconsistent();
  55. // The "use" parameter was specified, however it couldn't be converted to an
  56. // equivalent Web Crypto usage.
  57. static Status ErrorJwkUnrecognizedUse();
  58. // The "key_ops" parameter was specified, however one of the values in the
  59. // array couldn't be converted to an equivalent Web Crypto usage.
  60. static Status ErrorJwkUnrecognizedKeyop();
  61. // The "use" parameter was specified, however it is incompatible with that
  62. // specified by the Web Crypto import operation.
  63. static Status ErrorJwkUseInconsistent();
  64. // The "key_ops" parameter was specified, however it is incompatible with that
  65. // specified by the Web Crypto import operation.
  66. static Status ErrorJwkKeyopsInconsistent();
  67. // Both the "key_ops" and the "use" parameters were specified, however they
  68. // are incompatible with each other.
  69. static Status ErrorJwkUseAndKeyopsInconsistent();
  70. // The "kty" parameter was given and was a string, however it was not the
  71. // expected value.
  72. static Status ErrorJwkUnexpectedKty(const std::string& expected);
  73. // The amount of key data provided was incompatible with the selected
  74. // algorithm. For instance if the algorith name was A128CBC then EXACTLY
  75. // 128-bits of key data must have been provided. If 192-bits of key data were
  76. // given that is an error.
  77. static Status ErrorJwkIncorrectKeyLength();
  78. // The JWK member |member_name| is supposed to represent a big-endian unsigned
  79. // integer, however was the empty string.
  80. static Status ErrorJwkEmptyBigInteger(const std::string& member_name);
  81. // The big-endian unsigned integer |member_name| contained leading zeros. This
  82. // violates the JWA requirement that such octet strings be minimal.
  83. static Status ErrorJwkBigIntegerHasLeadingZero(
  84. const std::string& member_name);
  85. // The key_ops lists a usage more than once.
  86. static Status ErrorJwkDuplicateKeyOps();
  87. // ------------------------------------
  88. // Other errors
  89. // ------------------------------------
  90. // Tried importing a key using an unsupported format for the key type (for
  91. // instance importing an HMAC key using format=spki).
  92. static Status ErrorUnsupportedImportKeyFormat();
  93. // Tried exporting a key using an unsupported format for the key type (for
  94. // instance exporting an HMAC key using format=spki).
  95. static Status ErrorUnsupportedExportKeyFormat();
  96. // The key data buffer provided for importKey() is an incorrect length for
  97. // AES.
  98. static Status ErrorImportAesKeyLength();
  99. // The length specified when deriving an AES key was not 128 or 256 bits.
  100. static Status ErrorGetAesKeyLength();
  101. // Attempted to generate an AES key with an invalid length.
  102. static Status ErrorGenerateAesKeyLength();
  103. // 192-bit AES keys are valid, however unsupported (http://crbug.com/533699)
  104. static Status ErrorAes192BitUnsupported();
  105. // The wrong key was used for the operation. For instance, a public key was
  106. // used to verify a RsaSsaPkcs1v1_5 signature, or tried exporting a private
  107. // key using spki format.
  108. static Status ErrorUnexpectedKeyType();
  109. // When doing an AES-CBC encryption/decryption, the "iv" parameter was not 16
  110. // bytes.
  111. static Status ErrorIncorrectSizeAesCbcIv();
  112. // When doing AES-CTR encryption/decryption, the "counter" parameter was not
  113. // 16 bytes.
  114. static Status ErrorIncorrectSizeAesCtrCounter();
  115. // When doing AES-CTR encryption/decryption, the "length" parameter for the
  116. // counter was out of range.
  117. static Status ErrorInvalidAesCtrCounterLength();
  118. // The input to encrypt/decrypt was too large. Based on the counter size, it
  119. // would cause the counter to wraparound and repeat earlier values.
  120. static Status ErrorAesCtrInputTooLongCounterRepeated();
  121. // The data provided to an encrypt/decrypt/sign/verify operation was too
  122. // large. This can either represent an internal limitation (for instance
  123. // representing buffer lengths as uints).
  124. static Status ErrorDataTooLarge();
  125. // The data provided to an encrypt/decrypt/sign/verify operation was too
  126. // small. This usually represents an algorithm restriction (for instance
  127. // AES-KW requires a minimum of 24 bytes input data).
  128. static Status ErrorDataTooSmall();
  129. // Something was unsupported or unimplemented. This can mean the algorithm in
  130. // question was unsupported, some parameter combination was unsupported, or
  131. // something has not yet been implemented.
  132. static Status ErrorUnsupported();
  133. static Status ErrorUnsupported(const std::string& message);
  134. // Something unexpected happened in the code, which implies there is a
  135. // source-level bug. These should not happen, but safer to fail than simply
  136. // DCHECK.
  137. static Status ErrorUnexpected();
  138. // The authentication tag length specified for AES-GCM encrypt/decrypt was
  139. // not 32, 64, 96, 104, 112, 120, or 128.
  140. static Status ErrorInvalidAesGcmTagLength();
  141. // The input data given to an AES-KW encrypt/decrypt operation was not a
  142. // multiple of 8 bytes, as required by RFC 3394.
  143. static Status ErrorInvalidAesKwDataLength();
  144. // The "publicExponent" used to generate a key was invalid or unsupported.
  145. // Only values of 3 and 65537 are allowed.
  146. static Status ErrorGenerateKeyPublicExponent();
  147. // The modulus bytes were empty when importing an RSA public key.
  148. static Status ErrorImportRsaEmptyModulus();
  149. // The modulus length was unsupported when generating an RSA key pair.
  150. static Status ErrorGenerateRsaUnsupportedModulus();
  151. // The exponent bytes were empty when importing an RSA public key.
  152. static Status ErrorImportRsaEmptyExponent();
  153. // An unextractable key was used by an operation which exports the key data.
  154. static Status ErrorKeyNotExtractable();
  155. // Attempted to generate an HMAC key using a key length of 0.
  156. static Status ErrorGenerateHmacKeyLengthZero();
  157. // Attempted to import an HMAC key containing no data.
  158. static Status ErrorHmacImportEmptyKey();
  159. // Attempted to derive an HMAC key with zero length.
  160. static Status ErrorGetHmacKeyLengthZero();
  161. // Attempted to import an HMAC key using a bad optional length.
  162. static Status ErrorHmacImportBadLength();
  163. // Attempted to create a key (either by importKey(), generateKey(), or
  164. // unwrapKey()) however the key usages were not applicable for the key type
  165. // and algorithm.
  166. static Status ErrorCreateKeyBadUsages();
  167. // No usages were specified when generating/importing a secret or private key.
  168. static Status ErrorCreateKeyEmptyUsages();
  169. // An EC key imported using SPKI/PKCS8 format had the wrong curve specified in
  170. // the key.
  171. static Status ErrorImportedEcKeyIncorrectCurve();
  172. // The "crv" member for a JWK did not match the expectations from importKey()
  173. static Status ErrorJwkIncorrectCrv();
  174. // The EC key failed validation (coordinates don't lie on curve, out of range,
  175. // etc.)
  176. static Status ErrorEcKeyInvalid();
  177. // The octet string |member_name| was expected to be |expected_length| bytes
  178. // long, but was instead |actual_length| bytes long.
  179. static Status JwkOctetStringWrongLength(const std::string& member_name,
  180. size_t expected_length,
  181. size_t actual_length);
  182. // The public key given for ECDH key derivation was not an EC public key.
  183. static Status ErrorEcdhPublicKeyWrongType();
  184. // The public key's algorithm was not ECDH.
  185. static Status ErrorEcdhPublicKeyWrongAlgorithm();
  186. // The public and private keys given to ECDH key derivation were not for the
  187. // same named curve.
  188. static Status ErrorEcdhCurveMismatch();
  189. // The requested bit length for ECDH key derivation was too large.
  190. static Status ErrorEcdhLengthTooBig(unsigned int max_length_bits);
  191. // The requested length for HKDF was too large.
  192. static Status ErrorHkdfLengthTooLong();
  193. // The length to HKDF's deriveBits() was not a multiple of 8.
  194. static Status ErrorHkdfLengthNotWholeByte();
  195. // No length parameter was provided for HKDF's Derive Bits operation.
  196. static Status ErrorHkdfDeriveBitsLengthNotSpecified();
  197. // The requested bit length for PBKDF2 key derivation was invalid.
  198. static Status ErrorPbkdf2InvalidLength();
  199. // No length parameter was provided for PBKDF2's Derive Bits operation.
  200. static Status ErrorPbkdf2DeriveBitsLengthNotSpecified();
  201. // PBKDF2's deriveBits() was called with an unsupported length of 0.
  202. static Status ErrorPbkdf2DeriveBitsLengthZero();
  203. // PBKDF2 was called with iterations == 0.
  204. static Status ErrorPbkdf2Iterations0();
  205. // Tried importing a key with extractable=true for one of the *KDF
  206. // algorithms.
  207. static Status ErrorImportExtractableKdfKey();
  208. private:
  209. enum Type { TYPE_ERROR, TYPE_SUCCESS };
  210. // Constructs an error with the specified error type and message.
  211. Status(blink::WebCryptoErrorType error_type,
  212. const std::string& error_details_utf8);
  213. // Constructs a success or error without any details.
  214. explicit Status(Type type);
  215. Type type_;
  216. blink::WebCryptoErrorType error_type_;
  217. std::string error_details_;
  218. };
  219. } // namespace webcrypto
  220. #endif // COMPONENTS_WEBCRYPTO_STATUS_H_