| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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 COMPONENTS_WEBCRYPTO_ALGORITHM_IMPLEMENTATION_H_ | 5 #ifndef COMPONENTS_WEBCRYPTO_ALGORITHM_IMPLEMENTATION_H_ |
| 6 #define COMPONENTS_WEBCRYPTO_ALGORITHM_IMPLEMENTATION_H_ | 6 #define COMPONENTS_WEBCRYPTO_ALGORITHM_IMPLEMENTATION_H_ |
| 7 | 7 |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 | 9 |
| 10 #include <memory> | 10 #include <memory> |
| (...skipping 20 matching lines...) Expand all Loading... |
| 31 // Unless stated otherwise methods of AlgorithmImplementation are responsible | 31 // Unless stated otherwise methods of AlgorithmImplementation are responsible |
| 32 // for sanitizing their inputs. The following can be assumed: | 32 // for sanitizing their inputs. The following can be assumed: |
| 33 // | 33 // |
| 34 // * |algorithm.id()| and |key.algorithm.id()| matches the algorithm under | 34 // * |algorithm.id()| and |key.algorithm.id()| matches the algorithm under |
| 35 // which the implementation was registerd. | 35 // which the implementation was registerd. |
| 36 // * |algorithm| has the correct parameters type for the operation. | 36 // * |algorithm| has the correct parameters type for the operation. |
| 37 // * The key usages have already been verified. In fact in the case of calls | 37 // * The key usages have already been verified. In fact in the case of calls |
| 38 // to Encrypt()/Decrypt() the corresponding key usages may not be present | 38 // to Encrypt()/Decrypt() the corresponding key usages may not be present |
| 39 // (when wrapping/unwrapping). | 39 // (when wrapping/unwrapping). |
| 40 // | 40 // |
| 41 // An AlgorithmImplementation can also assume that | 41 // An AlgorithmImplementation can also assume that crypto::EnsureOpenSSLInit() |
| 42 // crypto::EnsureOpenSSLInit() will be called before any of its | 42 // will be called before any of its methods are invoked (except the |
| 43 // methods are invoked (except the constructor). | 43 // constructor). |
| 44 class AlgorithmImplementation { | 44 class AlgorithmImplementation { |
| 45 public: | 45 public: |
| 46 virtual ~AlgorithmImplementation(); | 46 virtual ~AlgorithmImplementation(); |
| 47 | 47 |
| 48 // This method corresponds to Web Crypto's crypto.subtle.encrypt(). | 48 // This is what is run whenever the spec says: |
| 49 // "Let result be the result of performing the encrypt operation" |
| 50 // |
| 51 // (crypto.subtle.encrypt() dispatches to this) |
| 49 virtual Status Encrypt(const blink::WebCryptoAlgorithm& algorithm, | 52 virtual Status Encrypt(const blink::WebCryptoAlgorithm& algorithm, |
| 50 const blink::WebCryptoKey& key, | 53 const blink::WebCryptoKey& key, |
| 51 const CryptoData& data, | 54 const CryptoData& data, |
| 52 std::vector<uint8_t>* buffer) const; | 55 std::vector<uint8_t>* buffer) const; |
| 53 | 56 |
| 54 // This method corresponds to Web Crypto's crypto.subtle.decrypt(). | 57 // This is what is run whenever the spec says: |
| 58 // "Let result be the result of performing the decrypt operation" |
| 59 // |
| 60 // (crypto.subtle.decrypt() dispatches to this) |
| 55 virtual Status Decrypt(const blink::WebCryptoAlgorithm& algorithm, | 61 virtual Status Decrypt(const blink::WebCryptoAlgorithm& algorithm, |
| 56 const blink::WebCryptoKey& key, | 62 const blink::WebCryptoKey& key, |
| 57 const CryptoData& data, | 63 const CryptoData& data, |
| 58 std::vector<uint8_t>* buffer) const; | 64 std::vector<uint8_t>* buffer) const; |
| 59 | 65 |
| 60 // This method corresponds to Web Crypto's crypto.subtle.sign(). | 66 // This is what is run whenever the spec says: |
| 67 // "Let result be the result of performing the sign operation" |
| 68 // |
| 69 // (crypto.subtle.sign() dispatches to this) |
| 61 virtual Status Sign(const blink::WebCryptoAlgorithm& algorithm, | 70 virtual Status Sign(const blink::WebCryptoAlgorithm& algorithm, |
| 62 const blink::WebCryptoKey& key, | 71 const blink::WebCryptoKey& key, |
| 63 const CryptoData& data, | 72 const CryptoData& data, |
| 64 std::vector<uint8_t>* buffer) const; | 73 std::vector<uint8_t>* buffer) const; |
| 65 | 74 |
| 66 // This method corresponds to Web Crypto's crypto.subtle.verify(). | 75 // This is what is run whenever the spec says: |
| 76 // "Let result be the result of performing the verify operation" |
| 77 // |
| 78 // (crypto.subtle.verify() dispatches to this) |
| 67 virtual Status Verify(const blink::WebCryptoAlgorithm& algorithm, | 79 virtual Status Verify(const blink::WebCryptoAlgorithm& algorithm, |
| 68 const blink::WebCryptoKey& key, | 80 const blink::WebCryptoKey& key, |
| 69 const CryptoData& signature, | 81 const CryptoData& signature, |
| 70 const CryptoData& data, | 82 const CryptoData& data, |
| 71 bool* signature_match) const; | 83 bool* signature_match) const; |
| 72 | 84 |
| 73 // This method corresponds to Web Crypto's crypto.subtle.digest(). | 85 // This is what is run whenever the spec says: |
| 86 // "Let result be the result of performing the digest operation" |
| 87 // |
| 88 // (crypto.subtle.digest() dispatches to this) |
| 74 virtual Status Digest(const blink::WebCryptoAlgorithm& algorithm, | 89 virtual Status Digest(const blink::WebCryptoAlgorithm& algorithm, |
| 75 const CryptoData& data, | 90 const CryptoData& data, |
| 76 std::vector<uint8_t>* buffer) const; | 91 std::vector<uint8_t>* buffer) const; |
| 77 | 92 |
| 78 // This method corresponds to Web Crypto's crypto.subtle.generateKey(). | 93 // This is what is run whenever the spec says: |
| 94 // "Let result be the result of executing the generate key operation" |
| 79 // | 95 // |
| 80 // Implementations MUST verify |usages| and return an error if it is not | 96 // (crypto.subtle.generateKey() dispatches to this) |
| 81 // appropriate. | |
| 82 virtual Status GenerateKey(const blink::WebCryptoAlgorithm& algorithm, | 97 virtual Status GenerateKey(const blink::WebCryptoAlgorithm& algorithm, |
| 83 bool extractable, | 98 bool extractable, |
| 84 blink::WebCryptoKeyUsageMask usages, | 99 blink::WebCryptoKeyUsageMask usages, |
| 85 GenerateKeyResult* result) const; | 100 GenerateKeyResult* result) const; |
| 86 | 101 |
| 87 // This method corresponds to Web Crypto's "derive bits" operation. It is | 102 // This is what is run whenever the spec says: |
| 88 // essentially crypto.subtle.deriveBits() with the exception that the length | 103 // "Let result be a new ArrayBuffer containing the result of executing the |
| 89 // can be "null" (|has_length_bits = true|). | 104 // derive bits operation" |
| 90 // | 105 // |
| 91 // In cases where the length was not specified, an appropriate default for the | 106 // (crypto.subtle.deriveBits() dispatches to this) |
| 92 // algorithm should be used (as described by the spec). | |
| 93 virtual Status DeriveBits(const blink::WebCryptoAlgorithm& algorithm, | 107 virtual Status DeriveBits(const blink::WebCryptoAlgorithm& algorithm, |
| 94 const blink::WebCryptoKey& base_key, | 108 const blink::WebCryptoKey& base_key, |
| 95 bool has_optional_length_bits, | 109 bool has_optional_length_bits, |
| 96 unsigned int optional_length_bits, | 110 unsigned int optional_length_bits, |
| 97 std::vector<uint8_t>* derived_bytes) const; | 111 std::vector<uint8_t>* derived_bytes) const; |
| 98 | 112 |
| 99 // This method corresponds with Web Crypto's "Get key length" operation. | 113 // This is what is run whenever the spec says: |
| 114 // "Let length be the result of executing the get key length algorithm" |
| 100 // | 115 // |
| 101 // In the Web Crypto spec the operation returns either "null" or an | 116 // In the Web Crypto spec the operation returns either "null" or an |
| 102 // "Integer". In this code "null" is represented by setting | 117 // "Integer". In this code "null" is represented by setting |
| 103 // |*has_length_bits = false|. | 118 // |*has_length_bits = false|. |
| 104 virtual Status GetKeyLength( | 119 virtual Status GetKeyLength( |
| 105 const blink::WebCryptoAlgorithm& key_length_algorithm, | 120 const blink::WebCryptoAlgorithm& key_length_algorithm, |
| 106 bool* has_length_bits, | 121 bool* has_length_bits, |
| 107 unsigned int* length_bits) const; | 122 unsigned int* length_bits) const; |
| 108 | 123 |
| 109 // ----------------------------------------------- | 124 // This is what is run whenever the spec says: |
| 110 // Key import | 125 // "Let result be the result of performing the import key operation" |
| 111 // ----------------------------------------------- | 126 // |
| 127 // (crypto.subtle.importKey() dispatches to this). |
| 128 virtual Status ImportKey(blink::WebCryptoKeyFormat format, |
| 129 const CryptoData& key_data, |
| 130 const blink::WebCryptoAlgorithm& algorithm, |
| 131 bool extractable, |
| 132 blink::WebCryptoKeyUsageMask usages, |
| 133 blink::WebCryptoKey* key) const; |
| 112 | 134 |
| 113 // VerifyKeyUsagesBeforeImportKey() must be called before either | 135 // This is what is run whenever the spec says: |
| 114 // importing a key, or unwrapping a key. | 136 // "Let result be the result of performing the export key operation" |
| 115 // | 137 // |
| 116 // Implementations should return an error if the requested usages are invalid | 138 // (crypto.subtle.exportKey() dispatches to this). |
| 117 // when importing for the specified format. | 139 virtual Status ExportKey(blink::WebCryptoKeyFormat format, |
| 118 // | 140 const blink::WebCryptoKey& key, |
| 119 // For instance, importing an RSA-SSA key with 'spki' format and Sign usage | 141 std::vector<uint8_t>* buffer) const; |
| 120 // is invalid. The 'spki' format implies it will be a public key, and public | |
| 121 // keys do not support signing. | |
| 122 // | |
| 123 // When called with format=JWK the key type may be unknown. The | |
| 124 // ImportKeyJwk() must do the final usage check. | |
| 125 virtual Status VerifyKeyUsagesBeforeImportKey( | |
| 126 blink::WebCryptoKeyFormat format, | |
| 127 blink::WebCryptoKeyUsageMask usages) const; | |
| 128 | |
| 129 // Dispatches to the format-specific ImportKey* method. | |
| 130 Status ImportKey(blink::WebCryptoKeyFormat format, | |
| 131 const CryptoData& key_data, | |
| 132 const blink::WebCryptoAlgorithm& algorithm, | |
| 133 bool extractable, | |
| 134 blink::WebCryptoKeyUsageMask usages, | |
| 135 blink::WebCryptoKey* key) const; | |
| 136 | |
| 137 // This method corresponds to Web Crypto's | |
| 138 // crypto.subtle.importKey(format='raw'). | |
| 139 virtual Status ImportKeyRaw(const CryptoData& key_data, | |
| 140 const blink::WebCryptoAlgorithm& algorithm, | |
| 141 bool extractable, | |
| 142 blink::WebCryptoKeyUsageMask usages, | |
| 143 blink::WebCryptoKey* key) const; | |
| 144 | |
| 145 // This method corresponds to Web Crypto's | |
| 146 // crypto.subtle.importKey(format='pkcs8'). | |
| 147 virtual Status ImportKeyPkcs8(const CryptoData& key_data, | |
| 148 const blink::WebCryptoAlgorithm& algorithm, | |
| 149 bool extractable, | |
| 150 blink::WebCryptoKeyUsageMask usages, | |
| 151 blink::WebCryptoKey* key) const; | |
| 152 | |
| 153 // This method corresponds to Web Crypto's | |
| 154 // crypto.subtle.importKey(format='spki'). | |
| 155 virtual Status ImportKeySpki(const CryptoData& key_data, | |
| 156 const blink::WebCryptoAlgorithm& algorithm, | |
| 157 bool extractable, | |
| 158 blink::WebCryptoKeyUsageMask usages, | |
| 159 blink::WebCryptoKey* key) const; | |
| 160 | |
| 161 // This method corresponds to Web Crypto's | |
| 162 // crypto.subtle.importKey(format='jwk'). | |
| 163 virtual Status ImportKeyJwk(const CryptoData& key_data, | |
| 164 const blink::WebCryptoAlgorithm& algorithm, | |
| 165 bool extractable, | |
| 166 blink::WebCryptoKeyUsageMask usages, | |
| 167 blink::WebCryptoKey* key) const; | |
| 168 | |
| 169 // ----------------------------------------------- | |
| 170 // Key export | |
| 171 // ----------------------------------------------- | |
| 172 | |
| 173 // Dispatches to the format-specific ExportKey* method. | |
| 174 Status ExportKey(blink::WebCryptoKeyFormat format, | |
| 175 const blink::WebCryptoKey& key, | |
| 176 std::vector<uint8_t>* buffer) const; | |
| 177 | |
| 178 virtual Status ExportKeyRaw(const blink::WebCryptoKey& key, | |
| 179 std::vector<uint8_t>* buffer) const; | |
| 180 | |
| 181 virtual Status ExportKeyPkcs8(const blink::WebCryptoKey& key, | |
| 182 std::vector<uint8_t>* buffer) const; | |
| 183 | |
| 184 virtual Status ExportKeySpki(const blink::WebCryptoKey& key, | |
| 185 std::vector<uint8_t>* buffer) const; | |
| 186 | |
| 187 virtual Status ExportKeyJwk(const blink::WebCryptoKey& key, | |
| 188 std::vector<uint8_t>* buffer) const; | |
| 189 | 142 |
| 190 // ----------------------------------------------- | 143 // ----------------------------------------------- |
| 191 // Structured clone | 144 // Structured clone |
| 192 // ----------------------------------------------- | 145 // ----------------------------------------------- |
| 193 | 146 |
| 194 // The Structured clone methods are used for synchronous serialization / | 147 // The Structured clone methods are used for synchronous serialization / |
| 195 // deserialization of a WebCryptoKey. | 148 // deserialization of a WebCryptoKey. |
| 196 // | 149 // |
| 197 // This serialized format is used by Blink to: | 150 // This serialized format is used by Blink to: |
| 198 // * Copy WebCryptoKeys between threads (postMessage to WebWorkers) | 151 // * Copy WebCryptoKeys between threads (postMessage to WebWorkers) |
| (...skipping 21 matching lines...) Expand all Loading... |
| 220 blink::WebCryptoKeyType type, | 173 blink::WebCryptoKeyType type, |
| 221 bool extractable, | 174 bool extractable, |
| 222 blink::WebCryptoKeyUsageMask usages, | 175 blink::WebCryptoKeyUsageMask usages, |
| 223 const CryptoData& key_data, | 176 const CryptoData& key_data, |
| 224 blink::WebCryptoKey* key) const; | 177 blink::WebCryptoKey* key) const; |
| 225 }; | 178 }; |
| 226 | 179 |
| 227 } // namespace webcrypto | 180 } // namespace webcrypto |
| 228 | 181 |
| 229 #endif // COMPONENTS_WEBCRYPTO_ALGORITHM_IMPLEMENTATION_H_ | 182 #endif // COMPONENTS_WEBCRYPTO_ALGORITHM_IMPLEMENTATION_H_ |
| OLD | NEW |