OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CONTENT_RENDERER_WEBCRYPTO_WEBCRYPTO_UTIL_H_ | 5 #ifndef CONTENT_RENDERER_WEBCRYPTO_WEBCRYPTO_UTIL_H_ |
6 #define CONTENT_RENDERER_WEBCRYPTO_WEBCRYPTO_UTIL_H_ | 6 #define CONTENT_RENDERER_WEBCRYPTO_WEBCRYPTO_UTIL_H_ |
7 | 7 |
8 #include <map> | 8 #include <map> |
9 #include <string> | 9 #include <string> |
10 #include <vector> | 10 #include <vector> |
11 #include "base/basictypes.h" | 11 #include "base/basictypes.h" |
12 #include "content/common/content_export.h" | 12 #include "content/common/content_export.h" |
13 #include "third_party/WebKit/public/platform/WebArrayBuffer.h" | 13 #include "third_party/WebKit/public/platform/WebArrayBuffer.h" |
14 #include "third_party/WebKit/public/platform/WebCryptoAlgorithm.h" | 14 #include "third_party/WebKit/public/platform/WebCryptoAlgorithm.h" |
15 | 15 |
16 namespace content { | 16 namespace content { |
17 | 17 |
18 namespace webcrypto { | 18 namespace webcrypto { |
19 | 19 |
| 20 // Status indicates whether an operation completed successfully, or with an |
| 21 // error. The error is used for verification in unit-tests, as well as for |
| 22 // display to the user. |
| 23 // |
| 24 // As such, it is important that errors DO NOT reveal any sensitive material |
| 25 // (like key bytes). |
| 26 // |
| 27 // Care must be taken with what errors are reported back to blink when doing |
| 28 // compound operations like unwrapping a JWK key. In this case, errors |
| 29 // generated by the JWK import are not appropriate to report since the wrapped |
| 30 // JWK is not visible to the caller. |
| 31 class CONTENT_EXPORT Status { |
| 32 public: |
| 33 // Returns true if the Status represents an error (any one of them). |
| 34 bool IsError() const; |
| 35 |
| 36 // Returns true if the Status represent success. |
| 37 bool IsSuccess() const; |
| 38 |
| 39 // Returns a UTF-8 error message (non-localized) describing the error. This |
| 40 // message is intended to be displayed in the dev tools console. |
| 41 std::string ToString() const; |
| 42 |
| 43 // Constructs a status representing success. |
| 44 static Status Success(); |
| 45 |
| 46 // Constructs a status representing a generic error. It contains no extra |
| 47 // details. |
| 48 static Status Error(); |
| 49 |
| 50 // ------------------------------------ |
| 51 // Errors when importing a JWK formatted key |
| 52 // ------------------------------------ |
| 53 |
| 54 // The key bytes could not parsed as JSON dictionary. This either |
| 55 // means there was a parsing error, or the JSON object was not |
| 56 // convertable to a dictionary. |
| 57 static Status ErrorJwkNotDictionary(); |
| 58 |
| 59 // The required "kty" parameter was missing, or is not a string. |
| 60 static Status ErrorJwkMissingKty(); |
| 61 |
| 62 // The "extractable" parameter was specified and was a boolean, but was |
| 63 // incompatible with the value requested by the Web Crypto call. |
| 64 static Status ErrorJwkExtractableInconsistent(); |
| 65 |
| 66 // The "alg" parameter could not be converted to an equivalent |
| 67 // WebCryptoAlgorithm. Either it was malformed or unrecognized. |
| 68 static Status ErrorJwkUnrecognizedAlgorithm(); |
| 69 |
| 70 // The "alg" parameter is incompatible with the (optional) Algorithm |
| 71 // specified by the Web Crypto import operation. |
| 72 static Status ErrorJwkAlgorithmInconsistent(); |
| 73 |
| 74 // The "alg" parameter was not provided, however neither was an algorithm |
| 75 // provided by the Web Crypto import operation. |
| 76 static Status ErrorJwkAlgorithmMissing(); |
| 77 |
| 78 // The "use" parameter was specified, however it couldn't be converted to an |
| 79 // equivalent Web Crypto usage. |
| 80 static Status ErrorJwkUnrecognizedUsage(); |
| 81 |
| 82 // The "use" parameter was specified, however it is incompatible with that |
| 83 // specified by the Web Crypto import operation. |
| 84 static Status ErrorJwkUsageInconsistent(); |
| 85 |
| 86 // The "k" parameter was either missing, could not be parsed as a base-64 |
| 87 // encoded string, or the decoded bytes were empty. |
| 88 static Status ErrorJwkDecodeK(); |
| 89 |
| 90 // The "n" parameter was either missing, could not be parsed as a base-64 |
| 91 // encoded string, or the decoded bytes were empty. |
| 92 static Status ErrorJwkDecodeN(); |
| 93 |
| 94 // The "e" parameter was either missing, could not be parsed as a base-64 |
| 95 // encoded string, or the decoded bytes were empty. |
| 96 static Status ErrorJwkDecodeE(); |
| 97 |
| 98 // TODO(eroman): Private key import through JWK is not yet supported. |
| 99 static Status ErrorJwkRsaPrivateKeyUnsupported(); |
| 100 |
| 101 // The "kty" parameter was given and was a string, however it was |
| 102 // unrecognized. |
| 103 static Status ErrorJwkUnrecognizedKty(); |
| 104 |
| 105 // ------------------------------------ |
| 106 // Other errors |
| 107 // ------------------------------------ |
| 108 |
| 109 // No key data was provided when importing an spki, pkcs8, or jwk formatted |
| 110 // key. This does not apply to raw format, since it is possible to have empty |
| 111 // key data there. |
| 112 static Status ErrorImportEmptyKeyData(); |
| 113 |
| 114 // The wrong key was used for the operation. For instance, a public key was |
| 115 // used to verify a RsaSsaPkcs1v1_5 signature, or tried exporting a private |
| 116 // key using spki format. |
| 117 static Status ErrorUnexpectedKeyType(); |
| 118 |
| 119 // When doing an AES-CBC encryption/decryption, the "iv" parameter was not 16 |
| 120 // bytes. |
| 121 static Status ErrorIncorrectSizeAesCbcIv(); |
| 122 |
| 123 // The data provided to an encrypt/decrypt/sign/verify operation was too |
| 124 // large. This can either represent an internal limitation (for instance |
| 125 // representing buffer lengths as uints), or an algorithm restriction (for |
| 126 // instance RSAES can operation on messages relative to the length of the |
| 127 // key's modulus). |
| 128 static Status ErrorDataTooLarge(); |
| 129 |
| 130 // Something was unsupported or unimplemented. This can mean the algorithm in |
| 131 // question was unsupported, some parameter combination was unsupported, or |
| 132 // something has not yet been implemented. |
| 133 static Status ErrorUnsupported(); |
| 134 |
| 135 // Something unexpected happened in the code, which implies there is a |
| 136 // source-level bug. These should not happen, but safer to fail than simply |
| 137 // DCHECK. |
| 138 static Status ErrorUnexpected(); |
| 139 |
| 140 // The authentication tag length specified for AES-GCM encrypt/decrypt was |
| 141 // either greater than 128 bits, or it was not a multiple of 8 bits. |
| 142 // (zero length is allowed). |
| 143 static Status ErrorInvalidAesGcmTagLength(); |
| 144 |
| 145 // The "publicExponent" used to generate a key was invalid: either no bytes |
| 146 // were specified, or the number was too large to fit into an "unsigned long" |
| 147 // (implemention limitation), or the exponent was zero. |
| 148 static Status ErrorGenerateKeyPublicExponent(); |
| 149 |
| 150 // The algorithm was null when importing a raw-formatted key. In this case it |
| 151 // is required. |
| 152 static Status ErrorMissingAlgorithmImportRawKey(); |
| 153 |
| 154 // The modulus bytes were empty when importing an RSA public key. |
| 155 static Status ErrorImportRsaEmptyModulus(); |
| 156 |
| 157 // The the modulus length was zero bits when generating an RSA public key. |
| 158 static Status ErrorGenerateRsaZeroModulus(); |
| 159 |
| 160 // The exponent bytes were empty when importing an RSA public key. |
| 161 static Status ErrorImportRsaEmptyExponent(); |
| 162 |
| 163 // An unextractable key was used by an operation which exports the key data. |
| 164 static Status ErrorKeyNotExtractable(); |
| 165 |
| 166 // The key length specified when generating a key was invalid. Either it was |
| 167 // zero, or it was not a multiple of 8 bits. |
| 168 static Status ErrorGenerateKeyLength(); |
| 169 |
| 170 private: |
| 171 // |error_details_utf8| can be NULL to indicate there was no error. |
| 172 // Otherwise it is a UTF-8 string literal (the pointer must remain valid). |
| 173 explicit Status(const char* error_details_utf8); |
| 174 |
| 175 const char* error_details_; |
| 176 }; |
| 177 |
20 // Returns a pointer to the start of |data|, or NULL if it is empty. This is a | 178 // Returns a pointer to the start of |data|, or NULL if it is empty. This is a |
21 // convenience function for getting the pointer, and should not be used beyond | 179 // convenience function for getting the pointer, and should not be used beyond |
22 // the expected lifetime of |data|. | 180 // the expected lifetime of |data|. |
23 CONTENT_EXPORT const uint8* Uint8VectorStart(const std::vector<uint8>& data); | 181 CONTENT_EXPORT const uint8* Uint8VectorStart(const std::vector<uint8>& data); |
24 | 182 |
25 // Shrinks a WebArrayBuffer to a new size. | 183 // Shrinks a WebArrayBuffer to a new size. |
26 // TODO(eroman): This works by re-allocating a new buffer. It would be better if | 184 // TODO(eroman): This works by re-allocating a new buffer. It would be better if |
27 // the WebArrayBuffer could just be truncated instead. | 185 // the WebArrayBuffer could just be truncated instead. |
28 void ShrinkBuffer(blink::WebArrayBuffer* buffer, unsigned new_size); | 186 void ShrinkBuffer(blink::WebArrayBuffer* buffer, unsigned new_size); |
29 | 187 |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
87 uint8 tag_length_bytes); | 245 uint8 tag_length_bytes); |
88 | 246 |
89 // Returns the internal block size for SHA-* | 247 // Returns the internal block size for SHA-* |
90 unsigned int ShaBlockSizeBytes(blink::WebCryptoAlgorithmId hash_id); | 248 unsigned int ShaBlockSizeBytes(blink::WebCryptoAlgorithmId hash_id); |
91 | 249 |
92 } // namespace webcrypto | 250 } // namespace webcrypto |
93 | 251 |
94 } // namespace content | 252 } // namespace content |
95 | 253 |
96 #endif // CONTENT_RENDERER_WEBCRYPTO_WEBCRYPTO_UTIL_H_ | 254 #endif // CONTENT_RENDERER_WEBCRYPTO_WEBCRYPTO_UTIL_H_ |
OLD | NEW |