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

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

Issue 19885002: WebCrypto: Add interfaces for importKey(). (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: Use a wrapper for the result rather than raw pointer 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
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 15 matching lines...) Expand all
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 #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/Key.h"
37 #include "modules/crypto/KeyOperation.h"
36 #include "modules/crypto/NormalizeAlgorithm.h" 38 #include "modules/crypto/NormalizeAlgorithm.h"
37 #include "public/platform/Platform.h" 39 #include "public/platform/Platform.h"
38 #include "public/platform/WebArrayBuffer.h" // FIXME: temporary
39 #include "public/platform/WebCrypto.h" 40 #include "public/platform/WebCrypto.h"
40 #include "wtf/ArrayBuffer.h"
41 #include "wtf/ArrayBufferView.h" 41 #include "wtf/ArrayBufferView.h"
42 #include "wtf/SHA1.h" // FIXME: temporary
43
44 42
45 namespace WebCore { 43 namespace WebCore {
46 44
47 namespace { 45 namespace {
48 46
49 // FIXME: The following are temporary implementations of what *should* go on the 47 // FIXME: Temporary
50 // embedder's side. Since SHA1 is easily implemented, this serves as 48 PassRefPtr<CryptoOperation> dummyOperation(const Dictionary& rawAlgorithm, Algor ithmOperation operationType, ExceptionCode& ec)
51 // a useful proof of concept to get layout tests up and running and 49 {
52 // returning correct results, until the embedder's side is implemented. 50 WebKit::WebCrypto* platformCrypto = WebKit::Platform::current()->crypto();
53 //------------------------------------------------------------------------------ 51 if (!platformCrypto) {
54 class DummyOperation : public WebKit::WebCryptoOperation { 52 ec = NotSupportedError;
55 public: 53 return 0;
56 explicit DummyOperation(WebKit::WebCryptoOperationResult* result) : m_result (result) { }
57
58 virtual void process(const unsigned char* bytes, size_t size) OVERRIDE
59 {
60 m_result->completeWithError();
61 delete this;
62 } 54 }
63 55
64 virtual void abort() OVERRIDE
65 {
66 delete this;
67 }
68
69 virtual void finish() OVERRIDE
70 {
71 m_result->completeWithError();
72 delete this;
73 }
74
75 protected:
76 WebKit::WebCryptoOperationResult* m_result;
77 };
78
79 class MockSha1Operation : public DummyOperation {
80 public:
81 explicit MockSha1Operation(WebKit::WebCryptoOperationResult* result) : Dummy Operation(result) { }
82
83 virtual void process(const unsigned char* bytes, size_t size) OVERRIDE
84 {
85 m_sha1.addBytes(bytes, size);
86 }
87
88 virtual void finish() OVERRIDE
89 {
90 Vector<uint8_t, 20> hash;
91 m_sha1.computeHash(hash);
92
93 WebKit::WebArrayBuffer buffer = WebKit::WebArrayBuffer::create(hash.size (), 1);
94 memcpy(buffer.data(), hash.data(), hash.size());
95
96 m_result->completeWithArrayBuffer(buffer);
97 delete this;
98 }
99
100 private:
101 SHA1 m_sha1;
102 };
103
104 class MockPlatformCrypto : public WebKit::WebCrypto {
105 public:
106 virtual void digest(const WebKit::WebCryptoAlgorithm& algorithm, WebKit::Web CryptoOperationResult* result) OVERRIDE
107 {
108 if (algorithm.id() == WebKit::WebCryptoAlgorithmIdSha1) {
109 result->initializationSucceded(new MockSha1Operation(result));
110 } else {
111 // Don't fail synchronously, since existing layout tests rely on
112 // digest for testing algorithm normalization.
113 result->initializationSucceded(new DummyOperation(result));
114 }
115 }
116 };
117
118 WebKit::WebCrypto* mockPlatformCrypto()
119 {
120 DEFINE_STATIC_LOCAL(MockPlatformCrypto, crypto, ());
121 return &crypto;
122 }
123
124 PassRefPtr<CryptoOperation> doDummyOperation(const Dictionary& rawAlgorithm, Alg orithmOperation operationType, ExceptionCode& ec)
125 {
126 WebKit::WebCryptoAlgorithm algorithm; 56 WebKit::WebCryptoAlgorithm algorithm;
127 if (!normalizeAlgorithm(rawAlgorithm, operationType, algorithm, ec)) 57 if (!normalizeAlgorithm(rawAlgorithm, operationType, algorithm, ec))
128 return 0; 58 return 0;
129 59
130 RefPtr<CryptoOperation> op = CryptoOperation::create(algorithm, &ec); 60 RefPtr<CryptoOperation> op = CryptoOperation::create(algorithm);
131 op->initializationSucceded(new DummyOperation(op.get())); 61 WebKit::WebCryptoOperationResult result(op.get());
132 return op.release(); 62 platformCrypto->digest(algorithm, result);
63 return op->returnValue(ec);
133 } 64 }
134 //------------------------------------------------------------------------------
135 65
136 } // namespace 66 } // namespace
137 67
138 SubtleCrypto::SubtleCrypto() 68 SubtleCrypto::SubtleCrypto()
139 { 69 {
140 ScriptWrappable::init(this); 70 ScriptWrappable::init(this);
141 } 71 }
142 72
143 PassRefPtr<CryptoOperation> SubtleCrypto::encrypt(const Dictionary& rawAlgorithm , ExceptionCode& ec) 73 PassRefPtr<CryptoOperation> SubtleCrypto::encrypt(const Dictionary& rawAlgorithm , ExceptionCode& ec)
144 { 74 {
145 return doDummyOperation(rawAlgorithm, Encrypt, ec); 75 return dummyOperation(rawAlgorithm, Encrypt, ec);
146 } 76 }
147 77
148 PassRefPtr<CryptoOperation> SubtleCrypto::decrypt(const Dictionary& rawAlgorithm , ExceptionCode& ec) 78 PassRefPtr<CryptoOperation> SubtleCrypto::decrypt(const Dictionary& rawAlgorithm , ExceptionCode& ec)
149 { 79 {
150 return doDummyOperation(rawAlgorithm, Decrypt, ec); 80 return dummyOperation(rawAlgorithm, Decrypt, ec);
151 } 81 }
152 82
153 PassRefPtr<CryptoOperation> SubtleCrypto::sign(const Dictionary& rawAlgorithm, E xceptionCode& ec) 83 PassRefPtr<CryptoOperation> SubtleCrypto::sign(const Dictionary& rawAlgorithm, E xceptionCode& ec)
154 { 84 {
155 return doDummyOperation(rawAlgorithm, Sign, ec); 85 return dummyOperation(rawAlgorithm, Sign, ec);
156 } 86 }
157 87
158 PassRefPtr<CryptoOperation> SubtleCrypto::verifySignature(const Dictionary& rawA lgorithm, ExceptionCode& ec) 88 PassRefPtr<CryptoOperation> SubtleCrypto::verifySignature(const Dictionary& rawA lgorithm, ExceptionCode& ec)
159 { 89 {
160 return doDummyOperation(rawAlgorithm, Verify, ec); 90 return dummyOperation(rawAlgorithm, Verify, ec);
161 } 91 }
162 92
163 PassRefPtr<CryptoOperation> SubtleCrypto::digest(const Dictionary& rawAlgorithm, ExceptionCode& ec) 93 PassRefPtr<CryptoOperation> SubtleCrypto::digest(const Dictionary& rawAlgorithm, ExceptionCode& ec)
164 { 94 {
165 WebKit::WebCrypto* platformCrypto = mockPlatformCrypto(); 95 WebKit::WebCrypto* platformCrypto = WebKit::Platform::current()->crypto();
166 if (!platformCrypto) { 96 if (!platformCrypto) {
167 ec = NotSupportedError; 97 ec = NotSupportedError;
168 return 0; 98 return 0;
169 } 99 }
170 100
171 WebKit::WebCryptoAlgorithm algorithm; 101 WebKit::WebCryptoAlgorithm algorithm;
172 if (!normalizeAlgorithm(rawAlgorithm, Digest, algorithm, ec)) 102 if (!normalizeAlgorithm(rawAlgorithm, Digest, algorithm, ec))
173 return 0; 103 return 0;
174 104
175 RefPtr<CryptoOperation> op = CryptoOperation::create(algorithm, &ec); 105 RefPtr<CryptoOperation> op = CryptoOperation::create(algorithm);
176 platformCrypto->digest(algorithm, op.get()); 106 WebKit::WebCryptoOperationResult result(op.get());
177 return op.release(); 107 platformCrypto->digest(algorithm, result);
108 return op->returnValue(ec);
abarth-chromium 2013/07/23 21:45:49 Thanks. This looks much better.
109 }
110
111 ScriptObject SubtleCrypto::importKey(const String& rawFormat, ArrayBufferView* k eyData, const Dictionary& rawAlgorithm, bool extractable, const Vector<String>& rawKeyUsages, ExceptionCode& ec)
112 {
113 WebKit::WebCrypto* platformCrypto = WebKit::Platform::current()->crypto();
114 if (!platformCrypto) {
115 ec = NotSupportedError;
116 return ScriptObject();
117 }
118
119 WebKit::WebCryptoKeyUsageMask keyUsages;
120 if (!Key::parseUsageMask(rawKeyUsages, keyUsages)) {
121 ec = TypeError;
122 return ScriptObject();
123 }
124
125 WebKit::WebCryptoKeyFormat format;
126 if (!Key::parseFormat(rawFormat, format)) {
127 ec = TypeError;
128 return ScriptObject();
129 }
130
131 WebKit::WebCryptoAlgorithm algorithm;
132 if (!normalizeAlgorithmForImportKey(rawAlgorithm, algorithm, ec))
133 return ScriptObject();
134
135 const unsigned char* keyDataBytes = static_cast<unsigned char*>(keyData->bas eAddress());
136
137 // FIXME: KeyOperation is never aborted. It should probably be aborted when
138 // the SubtleCrypto object that started it gets deleted. The concern being
139 // if the operation eventually does complete, the ScriptPromiseResolver
140 // might no longer be valid because the context it belonged to got torn
141 // down.
142 RefPtr<KeyOperation> keyOp = adoptRef(new KeyOperation);
abarth-chromium 2013/07/23 21:45:49 Usually classes have a static "create" function th
eroman 2013/07/23 23:29:02 Done.
143 WebKit::WebCryptoKeyOperationResult result(keyOp.get());
144 platformCrypto->importKey(format, keyDataBytes, keyData->byteLength(), algor ithm, extractable, keyUsages, result);
145 return keyOp->returnValue(ec);
178 } 146 }
179 147
180 } // namespace WebCore 148 } // namespace WebCore
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698