Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(42)

Side by Side Diff: Source/modules/crypto/SubtleCrypto.cpp

Issue 19776013: WebCrypto: Refactor the WebCryptoOperation interface to support errors. (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: Rebase onto master Created 7 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « Source/modules/crypto/CryptoOperation.cpp ('k') | public/platform/WebCrypto.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 16 matching lines...) Expand all
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 #include "config.h" 31 #include "config.h"
32 #include "modules/crypto/SubtleCrypto.h" 32 #include "modules/crypto/SubtleCrypto.h"
33 33
34 #include "core/dom/ExceptionCode.h" 34 #include "core/dom/ExceptionCode.h"
35 #include "modules/crypto/CryptoOperation.h" 35 #include "modules/crypto/CryptoOperation.h"
36 #include "modules/crypto/NormalizeAlgorithm.h" 36 #include "modules/crypto/NormalizeAlgorithm.h"
37 #include "public/platform/Platform.h"
37 #include "public/platform/WebArrayBuffer.h" // FIXME: temporary 38 #include "public/platform/WebArrayBuffer.h" // FIXME: temporary
38 #include "public/platform/WebCrypto.h" 39 #include "public/platform/WebCrypto.h"
39 #include "wtf/ArrayBuffer.h" 40 #include "wtf/ArrayBuffer.h"
40 #include "wtf/ArrayBufferView.h" 41 #include "wtf/ArrayBufferView.h"
41 #include "wtf/SHA1.h" // FIXME: temporary 42 #include "wtf/SHA1.h" // FIXME: temporary
42 43
43 44
44 namespace WebCore { 45 namespace WebCore {
45 46
46 namespace { 47 namespace {
47 48
48 // FIXME: The following are temporary implementations of what *should* go on the 49 // FIXME: The following are temporary implementations of what *should* go on the
49 // embedder's side. Since SHA1 is easily implemented, this serves as 50 // embedder's side. Since SHA1 is easily implemented, this serves as
50 // a useful proof of concept to get layout tests up and running and 51 // a useful proof of concept to get layout tests up and running and
51 // returning correct results, until the embedder's side is implemented. 52 // returning correct results, until the embedder's side is implemented.
52 //------------------------------------------------------------------------------ 53 //------------------------------------------------------------------------------
53 class DummyOperation : public WebKit::WebCryptoOperation { 54 class DummyOperation : public WebKit::WebCryptoOperation {
54 public: 55 public:
56 explicit DummyOperation(WebKit::WebCryptoOperationResult* result) : m_result (result) { }
57
55 virtual void process(const unsigned char* bytes, size_t size) OVERRIDE 58 virtual void process(const unsigned char* bytes, size_t size) OVERRIDE
56 { 59 {
57 ASSERT_NOT_REACHED(); 60 m_result->completeWithError();
61 delete this;
58 } 62 }
63
59 virtual void abort() OVERRIDE 64 virtual void abort() OVERRIDE
60 { 65 {
61 delete this; 66 delete this;
62 } 67 }
63 virtual void finish(WebKit::WebCryptoOperationResult* result) OVERRIDE 68
69 virtual void finish() OVERRIDE
64 { 70 {
65 ASSERT_NOT_REACHED(); 71 m_result->completeWithError();
72 delete this;
66 } 73 }
74
75 protected:
76 WebKit::WebCryptoOperationResult* m_result;
67 }; 77 };
68 78
69 class MockSha1Operation : public DummyOperation { 79 class MockSha1Operation : public DummyOperation {
70 public: 80 public:
81 explicit MockSha1Operation(WebKit::WebCryptoOperationResult* result) : Dummy Operation(result) { }
82
71 virtual void process(const unsigned char* bytes, size_t size) OVERRIDE 83 virtual void process(const unsigned char* bytes, size_t size) OVERRIDE
72 { 84 {
73 m_sha1.addBytes(bytes, size); 85 m_sha1.addBytes(bytes, size);
74 } 86 }
75 87
76 virtual void finish(WebKit::WebCryptoOperationResult* result) OVERRIDE 88 virtual void finish() OVERRIDE
77 { 89 {
78 Vector<uint8_t, 20> hash; 90 Vector<uint8_t, 20> hash;
79 m_sha1.computeHash(hash); 91 m_sha1.computeHash(hash);
80 92
81 WebKit::WebArrayBuffer buffer = WebKit::WebArrayBuffer::create(hash.size (), 1); 93 WebKit::WebArrayBuffer buffer = WebKit::WebArrayBuffer::create(hash.size (), 1);
82 memcpy(buffer.data(), hash.data(), hash.size()); 94 memcpy(buffer.data(), hash.data(), hash.size());
83 95
84 result->setArrayBuffer(buffer); 96 m_result->completeWithArrayBuffer(buffer);
85 delete this; 97 delete this;
86 } 98 }
87 99
88 private: 100 private:
89 SHA1 m_sha1; 101 SHA1 m_sha1;
90 }; 102 };
103
104 class MockPlatformCrypto : public WebKit::WebCrypto {
105 public:
106 // FIXME: delete
107 virtual WebKit::WebCryptoOperation* digest(const WebKit::WebCryptoAlgorithm& ) { return 0; }
108
109 virtual void digest2(const WebKit::WebCryptoAlgorithm& algorithm, WebKit::We bCryptoOperationResult* result) OVERRIDE
110 {
111 if (algorithm.id() == WebKit::WebCryptoAlgorithmIdSha1) {
112 result->initializationSucceded(new MockSha1Operation(result));
113 } else {
114 // Don't fail synchronously, since existing layout tests rely on
115 // digest for testing algorithm normalization.
116 result->initializationSucceded(new DummyOperation(result));
117 }
118 }
119 };
120
121 WebKit::WebCrypto* mockPlatformCrypto()
122 {
123 DEFINE_STATIC_LOCAL(MockPlatformCrypto, crypto, ());
124 return &crypto;
125 }
126
127 PassRefPtr<CryptoOperation> doDummyOperation(const Dictionary& rawAlgorithm, Alg orithmOperation operationType, ExceptionCode& ec)
128 {
129 WebKit::WebCryptoAlgorithm algorithm;
130 if (!normalizeAlgorithm(rawAlgorithm, operationType, algorithm, ec))
131 return 0;
132
133 RefPtr<CryptoOperation> op = CryptoOperation::create(algorithm, &ec);
134 op->initializationSucceded(new DummyOperation(op.get()));
135 return op.release();
136 }
91 //------------------------------------------------------------------------------ 137 //------------------------------------------------------------------------------
92 138
93 } // namespace 139 } // namespace
94 140
95 SubtleCrypto::SubtleCrypto() 141 SubtleCrypto::SubtleCrypto()
96 { 142 {
97 ScriptWrappable::init(this); 143 ScriptWrappable::init(this);
98 } 144 }
99 145
100 PassRefPtr<CryptoOperation> SubtleCrypto::encrypt(const Dictionary& rawAlgorithm , ExceptionCode& ec) 146 PassRefPtr<CryptoOperation> SubtleCrypto::encrypt(const Dictionary& rawAlgorithm , ExceptionCode& ec)
101 { 147 {
102 WebKit::WebCryptoAlgorithm algorithm; 148 return doDummyOperation(rawAlgorithm, Encrypt, ec);
103 if (!normalizeAlgorithm(rawAlgorithm, Encrypt, algorithm, ec))
104 return 0;
105 return CryptoOperation::create(algorithm, new DummyOperation);
106 } 149 }
107 150
108 PassRefPtr<CryptoOperation> SubtleCrypto::decrypt(const Dictionary& rawAlgorithm , ExceptionCode& ec) 151 PassRefPtr<CryptoOperation> SubtleCrypto::decrypt(const Dictionary& rawAlgorithm , ExceptionCode& ec)
109 { 152 {
110 WebKit::WebCryptoAlgorithm algorithm; 153 return doDummyOperation(rawAlgorithm, Decrypt, ec);
111 if (!normalizeAlgorithm(rawAlgorithm, Decrypt, algorithm, ec))
112 return 0;
113 return CryptoOperation::create(algorithm, new DummyOperation);
114 } 154 }
115 155
116 PassRefPtr<CryptoOperation> SubtleCrypto::sign(const Dictionary& rawAlgorithm, E xceptionCode& ec) 156 PassRefPtr<CryptoOperation> SubtleCrypto::sign(const Dictionary& rawAlgorithm, E xceptionCode& ec)
117 { 157 {
118 WebKit::WebCryptoAlgorithm algorithm; 158 return doDummyOperation(rawAlgorithm, Sign, ec);
119 if (!normalizeAlgorithm(rawAlgorithm, Sign, algorithm, ec))
120 return 0;
121 return CryptoOperation::create(algorithm, new DummyOperation);
122 } 159 }
123 160
124 PassRefPtr<CryptoOperation> SubtleCrypto::verifySignature(const Dictionary& rawA lgorithm, ExceptionCode& ec) 161 PassRefPtr<CryptoOperation> SubtleCrypto::verifySignature(const Dictionary& rawA lgorithm, ExceptionCode& ec)
125 { 162 {
126 WebKit::WebCryptoAlgorithm algorithm; 163 return doDummyOperation(rawAlgorithm, Verify, ec);
127 if (!normalizeAlgorithm(rawAlgorithm, Verify, algorithm, ec))
128 return 0;
129 return CryptoOperation::create(algorithm, new DummyOperation);
130 } 164 }
131 165
132 PassRefPtr<CryptoOperation> SubtleCrypto::digest(const Dictionary& rawAlgorithm, ExceptionCode& ec) 166 PassRefPtr<CryptoOperation> SubtleCrypto::digest(const Dictionary& rawAlgorithm, ExceptionCode& ec)
133 { 167 {
168 WebKit::WebCrypto* platformCrypto = mockPlatformCrypto();
169 if (!platformCrypto) {
170 ec = NotSupportedError;
171 return 0;
172 }
173
134 WebKit::WebCryptoAlgorithm algorithm; 174 WebKit::WebCryptoAlgorithm algorithm;
135 if (!normalizeAlgorithm(rawAlgorithm, Digest, algorithm, ec)) 175 if (!normalizeAlgorithm(rawAlgorithm, Digest, algorithm, ec))
136 return 0; 176 return 0;
137 177
138 // FIXME: Create the WebCryptoImplementation by calling out to 178 RefPtr<CryptoOperation> op = CryptoOperation::create(algorithm, &ec);
139 // Platform::crypto() instead. 179 platformCrypto->digest2(algorithm, op.get());
140 WebKit::WebCryptoOperation* operationImpl = algorithm.id() == WebKit::WebCry ptoAlgorithmIdSha1 ? new MockSha1Operation : new DummyOperation; 180 return op.release();
141
142 return CryptoOperation::create(algorithm, operationImpl);
143 } 181 }
144 182
145 } // namespace WebCore 183 } // namespace WebCore
OLDNEW
« no previous file with comments | « Source/modules/crypto/CryptoOperation.cpp ('k') | public/platform/WebCrypto.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698