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 16 matching lines...) Expand all Loading... |
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<CryptoOperationImpl> opImpl = CryptoOperationImpl::create(); |
132 op->initializationSucceded(new DummyOperation(op.get())); | 66 WebKit::WebCryptoOperationResult result(opImpl.get()); |
133 return op.release(); | 67 platformCrypto->digest(algorithm, result); |
| 68 if (opImpl->throwInitializationError(es)) |
| 69 return 0; |
| 70 return CryptoOperation::create(algorithm, opImpl.get()); |
134 } | 71 } |
135 //------------------------------------------------------------------------------ | |
136 | 72 |
137 } // namespace | 73 } // namespace |
138 | 74 |
139 SubtleCrypto::SubtleCrypto() | 75 SubtleCrypto::SubtleCrypto() |
140 { | 76 { |
141 ScriptWrappable::init(this); | 77 ScriptWrappable::init(this); |
142 } | 78 } |
143 | 79 |
144 PassRefPtr<CryptoOperation> SubtleCrypto::encrypt(const Dictionary& rawAlgorithm
, ExceptionState& es) | 80 PassRefPtr<CryptoOperation> SubtleCrypto::encrypt(const Dictionary& rawAlgorithm
, ExceptionState& es) |
145 { | 81 { |
146 return doDummyOperation(rawAlgorithm, Encrypt, es); | 82 return dummyOperation(rawAlgorithm, Encrypt, es); |
147 } | 83 } |
148 | 84 |
149 PassRefPtr<CryptoOperation> SubtleCrypto::decrypt(const Dictionary& rawAlgorithm
, ExceptionState& es) | 85 PassRefPtr<CryptoOperation> SubtleCrypto::decrypt(const Dictionary& rawAlgorithm
, ExceptionState& es) |
150 { | 86 { |
151 return doDummyOperation(rawAlgorithm, Decrypt, es); | 87 return dummyOperation(rawAlgorithm, Decrypt, es); |
152 } | 88 } |
153 | 89 |
154 PassRefPtr<CryptoOperation> SubtleCrypto::sign(const Dictionary& rawAlgorithm, E
xceptionState& es) | 90 PassRefPtr<CryptoOperation> SubtleCrypto::sign(const Dictionary& rawAlgorithm, E
xceptionState& es) |
155 { | 91 { |
156 return doDummyOperation(rawAlgorithm, Sign, es); | 92 return dummyOperation(rawAlgorithm, Sign, es); |
157 } | 93 } |
158 | 94 |
159 PassRefPtr<CryptoOperation> SubtleCrypto::verifySignature(const Dictionary& rawA
lgorithm, ExceptionState& es) | 95 PassRefPtr<CryptoOperation> SubtleCrypto::verifySignature(const Dictionary& rawA
lgorithm, ExceptionState& es) |
160 { | 96 { |
161 return doDummyOperation(rawAlgorithm, Verify, es); | 97 return dummyOperation(rawAlgorithm, Verify, es); |
162 } | 98 } |
163 | 99 |
164 PassRefPtr<CryptoOperation> SubtleCrypto::digest(const Dictionary& rawAlgorithm,
ExceptionState& es) | 100 PassRefPtr<CryptoOperation> SubtleCrypto::digest(const Dictionary& rawAlgorithm,
ExceptionState& es) |
165 { | 101 { |
166 WebKit::WebCrypto* platformCrypto = mockPlatformCrypto(); | 102 WebKit::WebCrypto* platformCrypto = WebKit::Platform::current()->crypto(); |
167 if (!platformCrypto) { | 103 if (!platformCrypto) { |
168 es.throwDOMException(NotSupportedError); | 104 es.throwDOMException(NotSupportedError); |
169 return 0; | 105 return 0; |
170 } | 106 } |
171 | 107 |
172 WebKit::WebCryptoAlgorithm algorithm; | 108 WebKit::WebCryptoAlgorithm algorithm; |
173 if (!normalizeAlgorithm(rawAlgorithm, Digest, algorithm, es)) | 109 if (!normalizeAlgorithm(rawAlgorithm, Digest, algorithm, es)) |
174 return 0; | 110 return 0; |
175 | 111 |
176 RefPtr<CryptoOperation> op = CryptoOperation::create(algorithm, &es); | 112 RefPtr<CryptoOperationImpl> opImpl = CryptoOperationImpl::create(); |
177 platformCrypto->digest(algorithm, op.get()); | 113 WebKit::WebCryptoOperationResult result(opImpl.get()); |
178 return op.release(); | 114 platformCrypto->digest(algorithm, result); |
| 115 if (opImpl->throwInitializationError(es)) |
| 116 return 0; |
| 117 return CryptoOperation::create(algorithm, opImpl.get()); |
| 118 } |
| 119 |
| 120 ScriptObject SubtleCrypto::importKey(const String& rawFormat, ArrayBufferView* k
eyData, const Dictionary& rawAlgorithm, bool extractable, const Vector<String>&
rawKeyUsages, ExceptionState& es) |
| 121 { |
| 122 WebKit::WebCrypto* platformCrypto = WebKit::Platform::current()->crypto(); |
| 123 if (!platformCrypto) { |
| 124 es.throwDOMException(NotSupportedError); |
| 125 return ScriptObject(); |
| 126 } |
| 127 |
| 128 WebKit::WebCryptoKeyUsageMask keyUsages; |
| 129 if (!Key::parseUsageMask(rawKeyUsages, keyUsages)) { |
| 130 es.throwDOMException(TypeError); |
| 131 return ScriptObject(); |
| 132 } |
| 133 |
| 134 WebKit::WebCryptoKeyFormat format; |
| 135 if (!Key::parseFormat(rawFormat, format)) { |
| 136 es.throwDOMException(TypeError); |
| 137 return ScriptObject(); |
| 138 } |
| 139 |
| 140 WebKit::WebCryptoAlgorithm algorithm; |
| 141 if (!normalizeAlgorithmForImportKey(rawAlgorithm, algorithm, es)) |
| 142 return ScriptObject(); |
| 143 |
| 144 const unsigned char* keyDataBytes = static_cast<unsigned char*>(keyData->bas
eAddress()); |
| 145 |
| 146 RefPtr<KeyOperation> keyOp = KeyOperation::create(); |
| 147 WebKit::WebCryptoKeyOperationResult result(keyOp.get()); |
| 148 platformCrypto->importKey(format, keyDataBytes, keyData->byteLength(), algor
ithm, extractable, keyUsages, result); |
| 149 return keyOp->returnValue(es); |
179 } | 150 } |
180 | 151 |
181 } // namespace WebCore | 152 } // namespace WebCore |
OLD | NEW |