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 14 matching lines...) Expand all Loading... |
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
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" |
| 36 #include "WebPrivatePtr.h" |
35 | 37 |
36 namespace WebKit { | 38 namespace WebKit { |
37 | 39 |
38 class WebArrayBuffer; | 40 class WebArrayBuffer; |
39 class WebCryptoAlgorithm; | 41 class WebCryptoKeyOperation; |
40 class WebCryptoKey; | 42 class WebCryptoKeyOperationResult; |
41 class WebCryptoOperation; | 43 class WebCryptoOperation; |
42 class WebCryptoOperationResult; | 44 class WebCryptoOperationResult; |
43 | 45 |
44 class WebCrypto { | 46 class WebCrypto { |
45 public: | 47 public: |
46 // FIXME: Deprecated, delete once chromium side is updated. | 48 // FIXME: Deprecated, delete once chromium side is updated. |
47 virtual WebCryptoOperation* digest(const WebCryptoAlgorithm&) { return 0; } | 49 virtual WebCryptoOperation* digest(const WebCryptoAlgorithm&) { return 0; } |
48 | 50 |
49 // The following methods begin an asynchronous multi-part cryptographic | 51 // The following methods begin an asynchronous multi-part cryptographic |
50 // operation. | 52 // operation. |
51 // | 53 // |
52 // Let the WebCryptoOperationResult* be called "result". | 54 // Let the WebCryptoOperationResult& be called "result". |
53 // | 55 // |
54 // Implementations can either: | 56 // Before returning, implementations can either: |
55 // | 57 // |
56 // * Synchronously fail initialization by calling: | 58 // * Synchronously fail initialization by calling: |
57 // result->initializationFailed(errorDetails) | 59 // result.initializationFailed(errorDetails) |
58 // | 60 // |
59 // OR | 61 // OR |
60 // | 62 // |
61 // * Create a new WebCryptoOperation* and return it by calling: | 63 // * Create a new WebCryptoOperation* and return it by calling: |
62 // result->initializationSucceeded(cryptoOperation) | 64 // result.initializationSucceeded(cryptoOperation) |
63 // | 65 // |
64 // If initialization succeeds, then Blink will subsequently call | 66 // If initialization succeeds, then Blink will subsequently call |
65 // methods on cryptoOperation: | 67 // methods on cryptoOperation: |
66 // | 68 // |
67 // - cryptoOperation->process() to feed it data | 69 // - cryptoOperation->process() to feed it data |
68 // - cryptoOperation->finish() to indicate there is no more data | 70 // - cryptoOperation->finish() to indicate there is no more data |
69 // - cryptoOperation->abort() to cancel. | 71 // - cryptoOperation->abort() to cancel. |
70 // | 72 // |
71 // The embedder may call result->completeWithXXX() at any time while the | 73 // The embedder may call result.completeWithXXX() at any time while the |
72 // cryptoOperation is "in progress". Typically completion is set once | 74 // cryptoOperation is "in progress" (can copy the initial "result" object). |
73 // cryptoOperation->finish() is called, however it can also be called | 75 // Typically completion is set once cryptoOperation->finish() is called, |
74 // during cryptoOperation->process() (for instance to set an error). | 76 // however it can also be called during cryptoOperation->process() (for |
| 77 // instance to set an error). |
75 // | 78 // |
76 // The cryptoOperation pointer MUST remain valid while it is "in progress". | 79 // The cryptoOperation pointer MUST remain valid while it is "in progress". |
77 // The cryptoOperation is said to be "in progress" from the time after | 80 // The cryptoOperation is said to be "in progress" from the time after |
78 // result->initializationSucceded() has been called, up until either: | 81 // result->initializationSucceded() has been called, up until either: |
79 // | 82 // |
80 // - Blink calls cryptoOperation->abort() | 83 // - Blink calls cryptoOperation->abort() |
81 // OR | 84 // OR |
82 // - Embedder calls result->completeWithXXX() | 85 // - Embedder calls result.completeWithXXX() |
83 // | 86 // |
84 // Once the cryptoOperation is no longer "in progress" the "result" pointer | 87 // Once the cryptoOperation is no longer "in progress" the embedder is |
85 // is no longer valid. At this time the embedder is responsible for freeing | 88 // responsible for freeing the cryptoOperation. |
86 // the cryptoOperation. | 89 virtual void digest(const WebCryptoAlgorithm&, WebCryptoOperationResult&) {
} |
87 virtual void digest(const WebCryptoAlgorithm&, WebCryptoOperationResult*) {
} | 90 |
| 91 // The following methods begin an asynchronous single-part key operation. |
| 92 // |
| 93 // Let the WebCryptoKeyOperationResult& be called "result". |
| 94 // |
| 95 // Before returning, implementations can either: |
| 96 // |
| 97 // (a) Synchronously fail initialization by calling: |
| 98 // result.initializationFailed(errorDetails) |
| 99 // (this results in throwing a Javascript exception) |
| 100 // |
| 101 // (b) Synchronously complete the request (success or error) by calling: |
| 102 // result.completeWithXXX() |
| 103 // |
| 104 // (c) Defer completion by calling |
| 105 // result.initializationSucceeded(keyOperation) |
| 106 // |
| 107 // If asynchronous completion (c) was chosen, then the embedder can notify |
| 108 // completion after returning by calling result.completeWithXXX() (can make |
| 109 // a copy of "result"). |
| 110 // |
| 111 // The keyOperation pointer MUST remain valid while it is "in progress". |
| 112 // The keyOperation is said to be "in progress" from the time after |
| 113 // result->initializationSucceded() has been called, up until either: |
| 114 // |
| 115 // - Blink calls keyOperation->abort() |
| 116 // OR |
| 117 // - Embedder calls result.completeWithXXX() |
| 118 // |
| 119 // Once the keyOperation is no longer "in progress" the embedder is |
| 120 // responsible for freeing the cryptoOperation. |
| 121 virtual void importKey(WebCryptoKeyFormat, const unsigned char* keyData, siz
e_t keyDataSize, const WebCryptoAlgorithm&, bool extractable, WebCryptoKeyUsageM
ask, WebCryptoKeyOperationResult&) { } |
88 | 122 |
89 protected: | 123 protected: |
90 virtual ~WebCrypto() { } | 124 virtual ~WebCrypto() { } |
91 }; | 125 }; |
92 | 126 |
93 class WebCryptoOperation { | 127 class WebCryptoOperation { |
94 public: | 128 public: |
95 // Feeds data (bytes, size) to the operation. | 129 // Feeds data (bytes, size) to the operation. |
96 // - |bytes| may be 0 if |size| is 0 | 130 // - |bytes| may be 0 if |size| is 0 |
97 // - |bytes| is valid only until process() returns | 131 // - |bytes| is valid only until process() returns |
98 // - process() will not be called after abort() or finish() | 132 // - process() will not be called after abort() or finish() |
99 virtual void process(const unsigned char*, size_t) = 0; | 133 virtual void process(const unsigned char*, size_t) = 0; |
100 | 134 |
101 // Cancels the in-progress operation. | 135 // Cancels the in-progress operation. |
102 // * Implementations should delete |this| after aborting. | 136 // * Implementations should delete |this| after aborting. |
103 virtual void abort() = 0; | 137 virtual void abort() = 0; |
104 | 138 |
105 // Indicates that there is no more data to receive. | 139 // Indicates that there is no more data to receive. |
106 virtual void finish() = 0; | 140 virtual void finish() = 0; |
107 | 141 |
108 protected: | 142 protected: |
109 virtual ~WebCryptoOperation() { } | 143 virtual ~WebCryptoOperation() { } |
110 }; | 144 }; |
111 | 145 |
| 146 // Can be implemented by embedder for unit-testing. |
| 147 class WebCryptoOperationResultPrivate { |
| 148 public: |
| 149 virtual ~WebCryptoOperationResultPrivate() { } |
| 150 |
| 151 virtual void initializationFailed() = 0; |
| 152 virtual void initializationSucceeded(WebCryptoOperation*) = 0; |
| 153 virtual void completeWithError() = 0; |
| 154 virtual void completeWithArrayBuffer(const WebArrayBuffer&) = 0; |
| 155 |
| 156 virtual void ref() = 0; |
| 157 virtual void deref() = 0; |
| 158 }; |
| 159 |
112 // FIXME: Add error types. | 160 // FIXME: Add error types. |
113 class WebCryptoOperationResult { | 161 class WebCryptoOperationResult { |
114 public: | 162 public: |
115 // Only one of these should be called. | 163 explicit WebCryptoOperationResult(WebCryptoOperationResultPrivate* impl) |
116 virtual void initializationFailed() = 0; | 164 { |
117 virtual void initializationSucceded(WebCryptoOperation*) = 0; | 165 assign(impl); |
| 166 } |
118 | 167 |
119 // Only one of these should be called to set the result. | 168 ~WebCryptoOperationResult() { reset(); } |
120 virtual void completeWithError() = 0; | 169 |
121 virtual void completeWithArrayBuffer(const WebArrayBuffer&) = 0; | 170 WebCryptoOperationResult(const WebCryptoOperationResult& o) |
| 171 { |
| 172 assign(o); |
| 173 } |
| 174 |
| 175 WebCryptoOperationResult& operator=(const WebCryptoOperationResult& o) |
| 176 { |
| 177 assign(o); |
| 178 return *this; |
| 179 } |
| 180 |
| 181 WEBKIT_EXPORT void initializationFailed(); |
| 182 WEBKIT_EXPORT void initializationSucceeded(WebCryptoOperation*); |
| 183 WEBKIT_EXPORT void completeWithError(); |
| 184 WEBKIT_EXPORT void completeWithArrayBuffer(const WebArrayBuffer&); |
| 185 |
| 186 private: |
| 187 WEBKIT_EXPORT void reset(); |
| 188 WEBKIT_EXPORT void assign(const WebCryptoOperationResult&); |
| 189 WEBKIT_EXPORT void assign(WebCryptoOperationResultPrivate*); |
| 190 |
| 191 WebPrivatePtr<WebCryptoOperationResultPrivate> m_impl; |
| 192 }; |
| 193 |
| 194 class WebCryptoKeyOperation { |
| 195 public: |
| 196 // Cancels the in-progress operation. |
| 197 // * Implementations should delete |this| after aborting. |
| 198 virtual void abort() = 0; |
122 | 199 |
123 protected: | 200 protected: |
124 virtual ~WebCryptoOperationResult() { } | 201 virtual ~WebCryptoKeyOperation() { } |
| 202 }; |
| 203 |
| 204 // Can be implemented by embedder for unit-testing. |
| 205 class WebCryptoKeyOperationResultPrivate { |
| 206 public: |
| 207 virtual ~WebCryptoKeyOperationResultPrivate() { } |
| 208 |
| 209 virtual void initializationFailed() = 0; |
| 210 virtual void initializationSucceeded(WebCryptoKeyOperation*) = 0; |
| 211 virtual void completeWithError() = 0; |
| 212 virtual void completeWithKey(const WebCryptoKey&) = 0; |
| 213 |
| 214 virtual void ref() = 0; |
| 215 virtual void deref() = 0; |
| 216 }; |
| 217 |
| 218 class WebCryptoKeyOperationResult { |
| 219 public: |
| 220 explicit WebCryptoKeyOperationResult(WebCryptoKeyOperationResultPrivate* imp
l) |
| 221 { |
| 222 assign(impl); |
| 223 } |
| 224 |
| 225 ~WebCryptoKeyOperationResult() { reset(); } |
| 226 |
| 227 WebCryptoKeyOperationResult(const WebCryptoKeyOperationResult& o) |
| 228 { |
| 229 assign(o); |
| 230 } |
| 231 |
| 232 WebCryptoKeyOperationResult& operator=(const WebCryptoKeyOperationResult& o) |
| 233 { |
| 234 assign(o); |
| 235 return *this; |
| 236 } |
| 237 |
| 238 WEBKIT_EXPORT void initializationFailed(); |
| 239 WEBKIT_EXPORT void initializationSucceeded(WebCryptoKeyOperation*); |
| 240 |
| 241 WEBKIT_EXPORT void completeWithError(); |
| 242 WEBKIT_EXPORT void completeWithKey(const WebCryptoKey&); |
| 243 |
| 244 private: |
| 245 WEBKIT_EXPORT void reset(); |
| 246 WEBKIT_EXPORT void assign(const WebCryptoKeyOperationResult&); |
| 247 WEBKIT_EXPORT void assign(WebCryptoKeyOperationResultPrivate*); |
| 248 |
| 249 WebPrivatePtr<WebCryptoKeyOperationResultPrivate> m_impl; |
125 }; | 250 }; |
126 | 251 |
127 } // namespace WebKit | 252 } // namespace WebKit |
128 | 253 |
129 #endif | 254 #endif |
OLD | NEW |