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

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

Powered by Google App Engine
This is Rietveld 408576698