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 | 5 * modification, are permitted provided that the following conditions |
6 * are met: | 6 * are met: |
7 * | 7 * |
8 * 1. Redistributions of source code must retain the above copyright | 8 * 1. 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 * 2. Redistributions in binary form must reproduce the above copyright | 10 * 2. Redistributions in binary form must reproduce the above copyright |
11 * notice, this list of conditions and the following disclaimer in the | 11 * notice, this list of conditions and the following disclaimer in the |
12 * documentation and/or other materials provided with the distribution. | 12 * documentation and/or other materials provided with the distribution. |
13 * 3. Neither the name of Google, Inc. ("Google") nor the names of | 13 * 3. Neither the name of Google, Inc. ("Google") nor the names of |
14 * its contributors may be used to endorse or promote products derived | 14 * its contributors may be used to endorse or promote products derived |
15 * from this software without specific prior written permission. | 15 * from this software without specific prior written permission. |
16 * | 16 * |
17 * THIS SOFTWARE IS PROVIDED BY GOOGLE AND ITS CONTRIBUTORS "AS IS" AND ANY | 17 * THIS SOFTWARE IS PROVIDED BY GOOGLE AND ITS CONTRIBUTORS "AS IS" AND ANY |
18 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | 18 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | 19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
20 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY | 20 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY |
21 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | 21 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
22 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | 22 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
23 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND | 23 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
24 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 24 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF | 25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
27 */ | 27 */ |
28 | 28 |
29 | |
30 #include "config.h" | 29 #include "config.h" |
31 #include "modules/crypto/SubtleCrypto.h" | 30 #include "modules/crypto/SubtleCrypto.h" |
32 | 31 |
33 #include "core/dom/ExceptionCode.h" | 32 #include "core/dom/ExceptionCode.h" |
34 #include "modules/crypto/CryptoOperation.h" | 33 #include "modules/crypto/CryptoOperation.h" |
35 #include "modules/crypto/NormalizeAlgorithm.h" | 34 #include "modules/crypto/NormalizeAlgorithm.h" |
| 35 #include "public/platform/WebArrayBuffer.h" // FIXME: temporary |
| 36 #include "public/platform/WebCrypto.h" |
36 #include "wtf/ArrayBuffer.h" | 37 #include "wtf/ArrayBuffer.h" |
37 #include "wtf/ArrayBufferView.h" | 38 #include "wtf/ArrayBufferView.h" |
| 39 #include "wtf/SHA1.h" // FIXME: temporary |
| 40 |
38 | 41 |
39 namespace WebCore { | 42 namespace WebCore { |
40 | 43 |
| 44 namespace { |
| 45 |
| 46 // FIXME: The following are temporary implementations of what *should* go on the |
| 47 // embedder's side. Since SHA1 is easily implemented, this serves as |
| 48 // a useful proof of concept to get layout tests up and running and |
| 49 // returning correct results, until the embedder's side is implemented. |
| 50 //------------------------------------------------------------------------------ |
| 51 class DummyOperation : public WebKit::WebCryptoOperation { |
| 52 public: |
| 53 virtual void process(const unsigned char* bytes, size_t size) OVERRIDE |
| 54 { |
| 55 ASSERT_NOT_REACHED(); |
| 56 } |
| 57 virtual void abort() OVERRIDE |
| 58 { |
| 59 delete this; |
| 60 } |
| 61 virtual void finish(WebKit::WebCryptoOperationResult* result) OVERRIDE |
| 62 { |
| 63 ASSERT_NOT_REACHED(); |
| 64 } |
| 65 }; |
| 66 |
| 67 class MockSha1Operation : public DummyOperation { |
| 68 public: |
| 69 virtual void process(const unsigned char* bytes, size_t size) OVERRIDE |
| 70 { |
| 71 m_sha1.addBytes(bytes, size); |
| 72 } |
| 73 |
| 74 virtual void finish(WebKit::WebCryptoOperationResult* result) OVERRIDE |
| 75 { |
| 76 Vector<uint8_t, 20> hash; |
| 77 m_sha1.computeHash(hash); |
| 78 |
| 79 WebKit::WebArrayBuffer buffer = WebKit::WebArrayBuffer::create(hash.size
(), 1); |
| 80 memcpy(buffer.data(), hash.data(), hash.size()); |
| 81 |
| 82 result->setArrayBuffer(buffer); |
| 83 delete this; |
| 84 } |
| 85 |
| 86 private: |
| 87 SHA1 m_sha1; |
| 88 }; |
| 89 //------------------------------------------------------------------------------ |
| 90 |
| 91 } // namespace |
| 92 |
41 SubtleCrypto::SubtleCrypto() | 93 SubtleCrypto::SubtleCrypto() |
42 { | 94 { |
43 ScriptWrappable::init(this); | 95 ScriptWrappable::init(this); |
44 } | 96 } |
45 | 97 |
46 PassRefPtr<CryptoOperation> SubtleCrypto::encrypt(const Dictionary& rawAlgorithm
, ExceptionCode& ec) | 98 PassRefPtr<CryptoOperation> SubtleCrypto::encrypt(const Dictionary& rawAlgorithm
, ExceptionCode& ec) |
47 { | 99 { |
48 WebKit::WebCryptoAlgorithm algorithm; | 100 WebKit::WebCryptoAlgorithm algorithm; |
49 if (!normalizeAlgorithm(rawAlgorithm, Encrypt, algorithm, ec)) | 101 if (!normalizeAlgorithm(rawAlgorithm, Encrypt, algorithm, ec)) |
50 return 0; | 102 return 0; |
51 return CryptoOperation::create(algorithm); | 103 return CryptoOperation::create(algorithm, new DummyOperation); |
52 } | 104 } |
53 | 105 |
54 PassRefPtr<CryptoOperation> SubtleCrypto::decrypt(const Dictionary& rawAlgorithm
, ExceptionCode& ec) | 106 PassRefPtr<CryptoOperation> SubtleCrypto::decrypt(const Dictionary& rawAlgorithm
, ExceptionCode& ec) |
55 { | 107 { |
56 WebKit::WebCryptoAlgorithm algorithm; | 108 WebKit::WebCryptoAlgorithm algorithm; |
57 if (!normalizeAlgorithm(rawAlgorithm, Decrypt, algorithm, ec)) | 109 if (!normalizeAlgorithm(rawAlgorithm, Decrypt, algorithm, ec)) |
58 return 0; | 110 return 0; |
59 return CryptoOperation::create(algorithm); | 111 return CryptoOperation::create(algorithm, new DummyOperation); |
60 } | 112 } |
61 | 113 |
62 PassRefPtr<CryptoOperation> SubtleCrypto::sign(const Dictionary& rawAlgorithm, E
xceptionCode& ec) | 114 PassRefPtr<CryptoOperation> SubtleCrypto::sign(const Dictionary& rawAlgorithm, E
xceptionCode& ec) |
63 { | 115 { |
64 WebKit::WebCryptoAlgorithm algorithm; | 116 WebKit::WebCryptoAlgorithm algorithm; |
65 if (!normalizeAlgorithm(rawAlgorithm, Sign, algorithm, ec)) | 117 if (!normalizeAlgorithm(rawAlgorithm, Sign, algorithm, ec)) |
66 return 0; | 118 return 0; |
67 return CryptoOperation::create(algorithm); | 119 return CryptoOperation::create(algorithm, new DummyOperation); |
68 } | 120 } |
69 | 121 |
70 PassRefPtr<CryptoOperation> SubtleCrypto::verifySignature(const Dictionary& rawA
lgorithm, ExceptionCode& ec) | 122 PassRefPtr<CryptoOperation> SubtleCrypto::verifySignature(const Dictionary& rawA
lgorithm, ExceptionCode& ec) |
71 { | 123 { |
72 WebKit::WebCryptoAlgorithm algorithm; | 124 WebKit::WebCryptoAlgorithm algorithm; |
73 if (!normalizeAlgorithm(rawAlgorithm, Verify, algorithm, ec)) | 125 if (!normalizeAlgorithm(rawAlgorithm, Verify, algorithm, ec)) |
74 return 0; | 126 return 0; |
75 return CryptoOperation::create(algorithm); | 127 return CryptoOperation::create(algorithm, new DummyOperation); |
76 } | 128 } |
77 | 129 |
78 PassRefPtr<CryptoOperation> SubtleCrypto::digest(const Dictionary& rawAlgorithm,
ExceptionCode& ec) | 130 PassRefPtr<CryptoOperation> SubtleCrypto::digest(const Dictionary& rawAlgorithm,
ExceptionCode& ec) |
79 { | 131 { |
80 WebKit::WebCryptoAlgorithm algorithm; | 132 WebKit::WebCryptoAlgorithm algorithm; |
81 if (!normalizeAlgorithm(rawAlgorithm, Digest, algorithm, ec)) | 133 if (!normalizeAlgorithm(rawAlgorithm, Digest, algorithm, ec)) |
82 return 0; | 134 return 0; |
83 return CryptoOperation::create(algorithm); | 135 |
| 136 // FIXME: Create the WebCryptoImplementation by calling out to |
| 137 // Platform::crypto() instead. |
| 138 WebKit::WebCryptoOperation* operationImpl = algorithm.id() == WebKit::WebCry
ptoAlgorithmIdSha1 ? new MockSha1Operation : new DummyOperation; |
| 139 |
| 140 return CryptoOperation::create(algorithm, operationImpl); |
84 } | 141 } |
85 | 142 |
86 } // namespace WebCore | 143 } // namespace WebCore |
OLD | NEW |