| 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 20 matching lines...) Expand all Loading... |
| 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" | 36 #include "WebPrivatePtr.h" |
| 37 | 37 |
| 38 namespace WebKit { | 38 namespace WebKit { |
| 39 | 39 |
| 40 class WebArrayBuffer; | 40 class WebArrayBuffer; |
| 41 class WebCryptoKeyOperation; | |
| 42 class WebCryptoKeyOperationResult; | |
| 43 class WebCryptoOperation; | 41 class WebCryptoOperation; |
| 44 class WebCryptoOperationResult; | 42 |
| 43 class WebCryptoResultPrivate { |
| 44 public: |
| 45 virtual void ref() = 0; |
| 46 virtual void deref() = 0; |
| 47 |
| 48 virtual void completeWithError() = 0; |
| 49 virtual void completeWithBuffer(const WebArrayBuffer&) = 0; |
| 50 virtual void completeWithBoolean(bool) = 0; |
| 51 virtual void completeWithKey(const WebCryptoKey&) = 0; |
| 52 |
| 53 protected: |
| 54 virtual ~WebCryptoResultPrivate() { } |
| 55 }; |
| 56 |
| 57 class WebCryptoResult { |
| 58 public: |
| 59 WebCryptoResult(WebCryptoResultPrivate* impl) |
| 60 { |
| 61 assign(impl); |
| 62 } |
| 63 |
| 64 WebCryptoResult(const WebCryptoResult& o) |
| 65 { |
| 66 assign(o); |
| 67 } |
| 68 |
| 69 ~WebCryptoResult() |
| 70 { |
| 71 reset(); |
| 72 } |
| 73 |
| 74 WebCryptoResult& operator=(const WebCryptoResult& o) |
| 75 { |
| 76 assign(o); |
| 77 return *this; |
| 78 } |
| 79 |
| 80 WEBKIT_EXPORT void completeWithError(); |
| 81 WEBKIT_EXPORT void completeWithBuffer(const WebArrayBuffer&); |
| 82 WEBKIT_EXPORT void completeWithBuffer(const void*, size_t); |
| 83 WEBKIT_EXPORT void completeWithBoolean(bool); |
| 84 WEBKIT_EXPORT void completeWithKey(const WebCryptoKey&); |
| 85 |
| 86 private: |
| 87 WEBKIT_EXPORT void reset(); |
| 88 WEBKIT_EXPORT void assign(const WebCryptoResult&); |
| 89 WEBKIT_EXPORT void assign(WebCryptoResultPrivate*); |
| 90 |
| 91 WebPrivatePtr<WebCryptoResultPrivate> m_impl; |
| 92 }; |
| 45 | 93 |
| 46 class WebCrypto { | 94 class WebCrypto { |
| 47 public: | 95 public: |
| 48 // FIXME: Deprecated, delete once chromium side is updated. | 96 // FIXME: Deprecated, delete once chromium side is updated. |
| 49 virtual WebCryptoOperation* digest(const WebCryptoAlgorithm&) { WEBKIT_ASSER
T_NOT_REACHED(); return 0; } | 97 virtual WebCryptoOperation* digest(const WebCryptoAlgorithm&) { WEBKIT_ASSER
T_NOT_REACHED(); return 0; } |
| 50 | 98 |
| 51 // The following methods begin an asynchronous multi-part cryptographic | 99 // Starts a one-shot cryptographic operation which can complete either |
| 52 // operation. | 100 // synchronously, or asynchronously. |
| 53 // | 101 // |
| 54 // Let the WebCryptoOperationResult& be called "result". | 102 // Let the WebCryptoResult be called "result". |
| 55 // | 103 // |
| 56 // Before returning, implementations can either: | 104 // The result should be set exactly once, from the same thread which |
| 57 // | 105 // initiated the operation. |
| 58 // * Synchronously fail initialization by calling: | 106 virtual void encrypt(const WebCryptoAlgorithm&, const WebCryptoKey&, const u
nsigned char* data, size_t data_size, WebCryptoResult) { WEBKIT_ASSERT_NOT_REACH
ED(); } |
| 59 // result.initializationFailed(errorDetails) | 107 virtual void decrypt(const WebCryptoAlgorithm&, const WebCryptoKey&, const u
nsigned char* data, size_t data_size, WebCryptoResult) { WEBKIT_ASSERT_NOT_REACH
ED(); } |
| 60 // | 108 virtual void sign(const WebCryptoAlgorithm&, const WebCryptoKey&, const unsi
gned char* data, size_t data_size, WebCryptoResult) { WEBKIT_ASSERT_NOT_REACHED(
); } |
| 61 // OR | 109 virtual void verifySignature(const WebCryptoAlgorithm&, const WebCryptoKey&,
const unsigned char* signature, size_t signature_size, const unsigned char* dat
a, size_t data_size, WebCryptoResult) { WEBKIT_ASSERT_NOT_REACHED(); } |
| 62 // | 110 virtual void digest(const WebCryptoAlgorithm&, const unsigned char* data, si
ze_t data_size, WebCryptoResult) { WEBKIT_ASSERT_NOT_REACHED(); } |
| 63 // * Create a new WebCryptoOperation* and return it by calling: | 111 virtual void generateKey(const WebCryptoAlgorithm&, bool extractable, WebCry
ptoKeyUsageMask, WebCryptoResult) { WEBKIT_ASSERT_NOT_REACHED(); } |
| 64 // result.initializationSucceeded(cryptoOperation) | 112 virtual void importKey(WebCryptoKeyFormat, const unsigned char* keyData, siz
e_t keyDataSize, const WebCryptoAlgorithm&, bool extractable, WebCryptoKeyUsageM
ask, WebCryptoResult) { WEBKIT_ASSERT_NOT_REACHED(); } |
| 65 // | |
| 66 // If initialization succeeds, then Blink will subsequently call | |
| 67 // methods on cryptoOperation: | |
| 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 | |
| 95 // The following methods begin an asynchronous single-part key operation. | |
| 96 // | |
| 97 // Let the WebCryptoKeyOperationResult& be called "result". | |
| 98 // | |
| 99 // Before returning, implementations can either: | |
| 100 // | |
| 101 // (a) Synchronously fail initialization by calling: | |
| 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 | 113 |
| 128 protected: | 114 protected: |
| 129 virtual ~WebCrypto() { } | 115 virtual ~WebCrypto() { } |
| 130 }; | 116 }; |
| 131 | 117 |
| 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 | 118 } // namespace WebKit |
| 260 | 119 |
| 261 #endif | 120 #endif |
| OLD | NEW |