Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 /* | 1 /* |
| 2 * Copyright (C) 2013 Google Inc. All rights reserved. | 2 * Copyright (C) 2013 Google Inc. All rights reserved. |
| 3 * | 3 * |
| 4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
| 5 * modification, are permitted provided that the following conditions are | 5 * modification, are permitted provided that the following conditions are |
| 6 * met: | 6 * met: |
| 7 * | 7 * |
| 8 * * Redistributions of source code must retain the above copyright | 8 * * Redistributions of source code must retain the above copyright |
| 9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
| 10 * * Redistributions in binary form must reproduce the above | 10 * * Redistributions in binary form must reproduce the above |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 */ | 29 */ |
| 30 | 30 |
| 31 #ifndef WebCrypto_h | 31 #ifndef WebCrypto_h |
| 32 #define WebCrypto_h | 32 #define WebCrypto_h |
| 33 | 33 |
| 34 #include "WebCommon.h" | 34 #include "WebCommon.h" |
| 35 #include "WebCryptoKey.h" | 35 #include "WebCryptoKey.h" |
| 36 #include "WebPrivatePtr.h" | |
| 37 | 36 |
| 38 namespace WebKit { | 37 namespace WebKit { |
| 39 | 38 |
| 40 class WebArrayBuffer; | 39 class WebArrayBuffer; |
| 41 class WebCryptoKeyOperation; | |
| 42 class WebCryptoKeyOperationResult; | |
| 43 class WebCryptoOperation; | 40 class WebCryptoOperation; |
| 44 class WebCryptoOperationResult; | 41 |
| 42 class WebCryptoOperationResult { | |
| 43 public: | |
| 44 virtual void completeWithError() = 0; | |
| 45 virtual void completeWithBuffer(const WebArrayBuffer&) = 0; | |
| 46 virtual void completeWithBoolean(bool) = 0; | |
| 47 virtual void completeWithKey(const WebCryptoKey&) = 0; | |
| 48 | |
| 49 // Helper to create a WebArrayBuffer. | |
| 50 WEBKIT_EXPORT void completeWithBuffer(const void*, size_t); | |
| 51 | |
| 52 virtual void willFinishLater() = 0; | |
| 53 | |
| 54 protected: | |
| 55 virtual ~WebCryptoOperationResult() { } | |
|
abarth-chromium
2013/08/16 04:53:07
We can make this destructor public so that the Chr
| |
| 56 }; | |
| 45 | 57 |
| 46 class WebCrypto { | 58 class WebCrypto { |
| 47 public: | 59 public: |
| 48 // FIXME: Deprecated, delete once chromium side is updated. | 60 // FIXME: Deprecated, delete once chromium side is updated. |
| 49 virtual WebCryptoOperation* digest(const WebCryptoAlgorithm&) { WEBKIT_ASSER T_NOT_REACHED(); return 0; } | 61 virtual WebCryptoOperation* digest(const WebCryptoAlgorithm&) { WEBKIT_ASSER T_NOT_REACHED(); return 0; } |
| 50 | 62 |
| 51 // The following methods begin an asynchronous multi-part cryptographic | 63 // Starts a one-shot cryptographic operation which can complete either |
| 52 // operation. | 64 // synchronously, or asynchronously. |
| 53 // | 65 // |
| 54 // Let the WebCryptoOperationResult& be called "result". | 66 // Let the WebCryptoOperationResult* be called "result". |
| 55 // | 67 // |
| 56 // Before returning, implementations can either: | 68 // (A) To complete synchronously, call one of the |
| 69 // "result->completeWithXXX()" methods before returning. | |
| 57 // | 70 // |
| 58 // * Synchronously fail initialization by calling: | 71 // OR |
| 59 // result.initializationFailed(errorDetails) | |
| 60 // | 72 // |
| 61 // OR | 73 // (B) To complete asynchronously call result->willFinishLater() before |
| 74 // returning. Later call one of the "result->completeWithXXX()" methods. | |
| 62 // | 75 // |
| 63 // * Create a new WebCryptoOperation* and return it by calling: | 76 // Note that: |
| 64 // result.initializationSucceeded(cryptoOperation) | 77 // * The "result" pointer is owned by the caller and should not be deleted. |
| 65 // | 78 // * Exactly one of the "result->completeXXX()" methods should be called. |
| 66 // If initialization succeeds, then Blink will subsequently call | 79 // * "result->completeXXX()" must be called from the same thread which |
| 67 // methods on cryptoOperation: | 80 // started the operation. |
| 68 // | |
| 69 // - cryptoOperation->process() to feed it data | |
| 70 // - cryptoOperation->finish() to indicate there is no more data | |
| 71 // - cryptoOperation->abort() to cancel. | |
| 72 // | |
| 73 // The embedder may call result.completeWithXXX() at any time while the | |
| 74 // cryptoOperation is "in progress" (can copy the initial "result" object). | |
| 75 // Typically completion is set once cryptoOperation->finish() is called, | |
| 76 // however it can also be called during cryptoOperation->process() (for | |
| 77 // instance to set an error). | |
| 78 // | |
| 79 // The cryptoOperation pointer MUST remain valid while it is "in progress". | |
| 80 // The cryptoOperation is said to be "in progress" from the time after | |
| 81 // result->initializationSucceded() has been called, up until either: | |
| 82 // | |
| 83 // - Blink calls cryptoOperation->abort() | |
| 84 // OR | |
| 85 // - Embedder calls result.completeWithXXX() | |
| 86 // | |
| 87 // Once the cryptoOperation is no longer "in progress" the embedder is | |
| 88 // responsible for freeing the cryptoOperation. | |
| 89 virtual void encrypt(const WebCryptoAlgorithm&, const WebCryptoKey&, WebCryp toOperationResult&) { WEBKIT_ASSERT_NOT_REACHED(); } | |
| 90 virtual void decrypt(const WebCryptoAlgorithm&, const WebCryptoKey&, WebCryp toOperationResult&) { WEBKIT_ASSERT_NOT_REACHED(); } | |
| 91 virtual void sign(const WebCryptoAlgorithm&, const WebCryptoKey&, WebCryptoO perationResult&) { WEBKIT_ASSERT_NOT_REACHED(); } | |
| 92 virtual void verifySignature(const WebCryptoAlgorithm&, const WebCryptoKey&, const unsigned char* signature, size_t, WebCryptoOperationResult&) { WEBKIT_ASS ERT_NOT_REACHED(); } | |
| 93 virtual void digest(const WebCryptoAlgorithm&, WebCryptoOperationResult&) { WEBKIT_ASSERT_NOT_REACHED(); } | |
| 94 | 81 |
| 95 // The following methods begin an asynchronous single-part key operation. | 82 virtual void encrypt(const WebCryptoAlgorithm&, const WebCryptoKey&, const u nsigned char* data, size_t data_size, WebCryptoOperationResult*) { WEBKIT_ASSERT _NOT_REACHED(); } |
| 96 // | 83 virtual void decrypt(const WebCryptoAlgorithm&, const WebCryptoKey&, const u nsigned char* data, size_t data_size, WebCryptoOperationResult*) { WEBKIT_ASSERT _NOT_REACHED(); } |
| 97 // Let the WebCryptoKeyOperationResult& be called "result". | 84 virtual void sign(const WebCryptoAlgorithm&, const WebCryptoKey&, const unsi gned char* data, size_t data_size, WebCryptoOperationResult*) { WEBKIT_ASSERT_NO T_REACHED(); } |
| 98 // | 85 virtual void verifySignature(const WebCryptoAlgorithm&, const WebCryptoKey&, const unsigned char* signature, size_t signature_size, const unsigned char* dat a, size_t data_size, WebCryptoOperationResult*) { WEBKIT_ASSERT_NOT_REACHED(); } |
| 99 // Before returning, implementations can either: | 86 virtual void digest(const WebCryptoAlgorithm&, const unsigned char* data, si ze_t data_size, WebCryptoOperationResult*) { WEBKIT_ASSERT_NOT_REACHED(); } |
| 100 // | 87 virtual void generateKey(const WebCryptoAlgorithm&, bool extractable, WebCry ptoKeyUsageMask, WebCryptoOperationResult*) { WEBKIT_ASSERT_NOT_REACHED(); } |
| 101 // (a) Synchronously fail initialization by calling: | 88 virtual void importKey(WebCryptoKeyFormat, const unsigned char* keyData, siz e_t keyDataSize, const WebCryptoAlgorithm&, bool extractable, WebCryptoKeyUsageM ask, WebCryptoOperationResult*) { WEBKIT_ASSERT_NOT_REACHED(); } |
| 102 // result.initializationFailed(errorDetails) | |
| 103 // (this results in throwing a Javascript exception) | |
| 104 // | |
| 105 // (b) Synchronously complete the request (success or error) by calling: | |
| 106 // result.completeWithXXX() | |
| 107 // | |
| 108 // (c) Defer completion by calling | |
| 109 // result.initializationSucceeded(keyOperation) | |
| 110 // | |
| 111 // If asynchronous completion (c) was chosen, then the embedder can notify | |
| 112 // completion after returning by calling result.completeWithXXX() (can make | |
| 113 // a copy of "result"). | |
| 114 // | |
| 115 // The keyOperation pointer MUST remain valid while it is "in progress". | |
| 116 // The keyOperation is said to be "in progress" from the time after | |
| 117 // result->initializationSucceded() has been called, up until either: | |
| 118 // | |
| 119 // - Blink calls keyOperation->abort() | |
| 120 // OR | |
| 121 // - Embedder calls result.completeWithXXX() | |
| 122 // | |
| 123 // Once the keyOperation is no longer "in progress" the embedder is | |
| 124 // responsible for freeing the cryptoOperation. | |
| 125 virtual void generateKey(const WebCryptoAlgorithm&, bool extractable, WebCry ptoKeyUsageMask, WebCryptoKeyOperationResult&) { WEBKIT_ASSERT_NOT_REACHED(); } | |
| 126 virtual void importKey(WebCryptoKeyFormat, const unsigned char* keyData, siz e_t keyDataSize, const WebCryptoAlgorithm&, bool extractable, WebCryptoKeyUsageM ask, WebCryptoKeyOperationResult&) { WEBKIT_ASSERT_NOT_REACHED(); } | |
| 127 | 89 |
| 128 protected: | 90 protected: |
| 129 virtual ~WebCrypto() { } | 91 virtual ~WebCrypto() { } |
| 130 }; | 92 }; |
| 131 | 93 |
| 132 class WebCryptoOperation { | |
| 133 public: | |
| 134 // Feeds data (bytes, size) to the operation. | |
| 135 // - |bytes| may be 0 if |size| is 0 | |
| 136 // - |bytes| is valid only until process() returns | |
| 137 // - process() will not be called after abort() or finish() | |
| 138 virtual void process(const unsigned char*, size_t) = 0; | |
| 139 | |
| 140 // Cancels the in-progress operation. | |
| 141 // * Implementations should delete |this| after aborting. | |
| 142 virtual void abort() = 0; | |
| 143 | |
| 144 // Indicates that there is no more data to receive. | |
| 145 virtual void finish() = 0; | |
| 146 | |
| 147 protected: | |
| 148 virtual ~WebCryptoOperation() { } | |
| 149 }; | |
| 150 | |
| 151 // Can be implemented by embedder for unit-testing. | |
| 152 class WebCryptoOperationResultPrivate { | |
| 153 public: | |
| 154 virtual ~WebCryptoOperationResultPrivate() { } | |
| 155 | |
| 156 virtual void initializationFailed() = 0; | |
| 157 virtual void initializationSucceeded(WebCryptoOperation*) = 0; | |
| 158 virtual void completeWithError() = 0; | |
| 159 virtual void completeWithArrayBuffer(const WebArrayBuffer&) = 0; | |
| 160 virtual void completeWithBoolean(bool) = 0; | |
| 161 | |
| 162 virtual void ref() = 0; | |
| 163 virtual void deref() = 0; | |
| 164 }; | |
| 165 | |
| 166 // FIXME: Add error types. | |
| 167 class WebCryptoOperationResult { | |
| 168 public: | |
| 169 explicit WebCryptoOperationResult(WebCryptoOperationResultPrivate* impl) | |
| 170 { | |
| 171 assign(impl); | |
| 172 } | |
| 173 | |
| 174 ~WebCryptoOperationResult() { reset(); } | |
| 175 | |
| 176 WebCryptoOperationResult(const WebCryptoOperationResult& o) | |
| 177 { | |
| 178 assign(o); | |
| 179 } | |
| 180 | |
| 181 WebCryptoOperationResult& operator=(const WebCryptoOperationResult& o) | |
| 182 { | |
| 183 assign(o); | |
| 184 return *this; | |
| 185 } | |
| 186 | |
| 187 WEBKIT_EXPORT void initializationFailed(); | |
| 188 WEBKIT_EXPORT void initializationSucceeded(WebCryptoOperation*); | |
| 189 WEBKIT_EXPORT void completeWithError(); | |
| 190 WEBKIT_EXPORT void completeWithArrayBuffer(const WebArrayBuffer&); | |
| 191 WEBKIT_EXPORT void completeWithBoolean(bool); | |
| 192 | |
| 193 private: | |
| 194 WEBKIT_EXPORT void reset(); | |
| 195 WEBKIT_EXPORT void assign(const WebCryptoOperationResult&); | |
| 196 WEBKIT_EXPORT void assign(WebCryptoOperationResultPrivate*); | |
| 197 | |
| 198 WebPrivatePtr<WebCryptoOperationResultPrivate> m_impl; | |
| 199 }; | |
| 200 | |
| 201 class WebCryptoKeyOperation { | |
| 202 public: | |
| 203 // Cancels the in-progress operation. | |
| 204 // * Implementations should delete |this| after aborting. | |
| 205 virtual void abort() = 0; | |
| 206 | |
| 207 protected: | |
| 208 virtual ~WebCryptoKeyOperation() { } | |
| 209 }; | |
| 210 | |
| 211 // Can be implemented by embedder for unit-testing. | |
| 212 class WebCryptoKeyOperationResultPrivate { | |
| 213 public: | |
| 214 virtual ~WebCryptoKeyOperationResultPrivate() { } | |
| 215 | |
| 216 virtual void initializationFailed() = 0; | |
| 217 virtual void initializationSucceeded(WebCryptoKeyOperation*) = 0; | |
| 218 virtual void completeWithError() = 0; | |
| 219 virtual void completeWithKey(const WebCryptoKey&) = 0; | |
| 220 | |
| 221 virtual void ref() = 0; | |
| 222 virtual void deref() = 0; | |
| 223 }; | |
| 224 | |
| 225 class WebCryptoKeyOperationResult { | |
| 226 public: | |
| 227 explicit WebCryptoKeyOperationResult(WebCryptoKeyOperationResultPrivate* imp l) | |
| 228 { | |
| 229 assign(impl); | |
| 230 } | |
| 231 | |
| 232 ~WebCryptoKeyOperationResult() { reset(); } | |
| 233 | |
| 234 WebCryptoKeyOperationResult(const WebCryptoKeyOperationResult& o) | |
| 235 { | |
| 236 assign(o); | |
| 237 } | |
| 238 | |
| 239 WebCryptoKeyOperationResult& operator=(const WebCryptoKeyOperationResult& o) | |
| 240 { | |
| 241 assign(o); | |
| 242 return *this; | |
| 243 } | |
| 244 | |
| 245 WEBKIT_EXPORT void initializationFailed(); | |
| 246 WEBKIT_EXPORT void initializationSucceeded(WebCryptoKeyOperation*); | |
| 247 | |
| 248 WEBKIT_EXPORT void completeWithError(); | |
| 249 WEBKIT_EXPORT void completeWithKey(const WebCryptoKey&); | |
| 250 | |
| 251 private: | |
| 252 WEBKIT_EXPORT void reset(); | |
| 253 WEBKIT_EXPORT void assign(const WebCryptoKeyOperationResult&); | |
| 254 WEBKIT_EXPORT void assign(WebCryptoKeyOperationResultPrivate*); | |
| 255 | |
| 256 WebPrivatePtr<WebCryptoKeyOperationResultPrivate> m_impl; | |
| 257 }; | |
| 258 | |
| 259 } // namespace WebKit | 94 } // namespace WebKit |
| 260 | 95 |
| 261 #endif | 96 #endif |
| OLD | NEW |