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 15 matching lines...) Expand all Loading... |
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 "bindings/v8/ExceptionState.h" | 34 #include "bindings/v8/ExceptionState.h" |
35 #include "core/dom/ExceptionCode.h" | 35 #include "core/dom/ExceptionCode.h" |
| 36 #include "core/platform/NotImplemented.h" |
36 #include "modules/crypto/CryptoOperation.h" | 37 #include "modules/crypto/CryptoOperation.h" |
37 #include "modules/crypto/Key.h" | 38 #include "modules/crypto/Key.h" |
38 #include "modules/crypto/KeyOperation.h" | 39 #include "modules/crypto/KeyOperation.h" |
39 #include "modules/crypto/NormalizeAlgorithm.h" | 40 #include "modules/crypto/NormalizeAlgorithm.h" |
40 #include "public/platform/Platform.h" | 41 #include "public/platform/Platform.h" |
41 #include "public/platform/WebCrypto.h" | 42 #include "public/platform/WebCrypto.h" |
42 #include "wtf/ArrayBufferView.h" | 43 #include "wtf/ArrayBufferView.h" |
43 | 44 |
44 namespace WebCore { | 45 namespace WebCore { |
45 | 46 |
46 // FIXME: Outstanding KeyOperations and CryptoOperations should be aborted when | 47 // FIXME: Outstanding KeyOperations and CryptoOperations should be aborted when |
47 // tearing down SubtleCrypto (to avoid problems completing a | 48 // tearing down SubtleCrypto (to avoid problems completing a |
48 // ScriptPromiseResolver which is no longer valid). | 49 // ScriptPromiseResolver which is no longer valid). |
49 | 50 |
50 namespace { | 51 namespace { |
51 | 52 |
52 // FIXME: Temporary | 53 bool keyCanBeUsedForAlgorithm(const WebKit::WebCryptoKey& key, const WebKit::Web
CryptoAlgorithm& algorithm, ExceptionState& es) |
53 PassRefPtr<CryptoOperation> dummyOperation(const Dictionary& rawAlgorithm, Algor
ithmOperation operationType, ExceptionState& es) | 54 { |
| 55 // FIXME: Need to enforce that the key's algorithm matches the operation, |
| 56 // and that the key's usages allow it to be used with this operation. |
| 57 notImplemented(); |
| 58 return true; |
| 59 } |
| 60 |
| 61 PassRefPtr<CryptoOperation> createCryptoOperation(const Dictionary& rawAlgorithm
, Key* key, AlgorithmOperation operationType, ExceptionState& es) |
54 { | 62 { |
55 WebKit::WebCrypto* platformCrypto = WebKit::Platform::current()->crypto(); | 63 WebKit::WebCrypto* platformCrypto = WebKit::Platform::current()->crypto(); |
56 if (!platformCrypto) { | 64 if (!platformCrypto) { |
57 es.throwDOMException(NotSupportedError); | 65 es.throwDOMException(NotSupportedError); |
58 return 0; | 66 return 0; |
59 } | 67 } |
60 | 68 |
61 WebKit::WebCryptoAlgorithm algorithm; | 69 WebKit::WebCryptoAlgorithm algorithm; |
62 if (!normalizeAlgorithm(rawAlgorithm, operationType, algorithm, es)) | 70 if (!normalizeAlgorithm(rawAlgorithm, operationType, algorithm, es)) |
63 return 0; | 71 return 0; |
64 | 72 |
| 73 // All operations other than Digest require a valid Key. |
| 74 if (operationType != Digest) { |
| 75 if (!key) { |
| 76 es.throwDOMException(TypeError); |
| 77 return 0; |
| 78 } |
| 79 |
| 80 if (!keyCanBeUsedForAlgorithm(key->key(), algorithm, es)) { |
| 81 return 0; |
| 82 } |
| 83 } |
| 84 |
65 RefPtr<CryptoOperationImpl> opImpl = CryptoOperationImpl::create(); | 85 RefPtr<CryptoOperationImpl> opImpl = CryptoOperationImpl::create(); |
66 WebKit::WebCryptoOperationResult result(opImpl.get()); | 86 WebKit::WebCryptoOperationResult result(opImpl.get()); |
67 platformCrypto->digest(algorithm, result); | 87 |
| 88 switch (operationType) { |
| 89 case Encrypt: |
| 90 platformCrypto->encrypt(algorithm, key->key(), result); |
| 91 break; |
| 92 case Decrypt: |
| 93 platformCrypto->decrypt(algorithm, key->key(), result); |
| 94 break; |
| 95 case Sign: |
| 96 platformCrypto->sign(algorithm, key->key(), result); |
| 97 break; |
| 98 case Digest: |
| 99 platformCrypto->digest(algorithm, result); |
| 100 break; |
| 101 default: |
| 102 ASSERT_NOT_REACHED(); |
| 103 return 0; |
| 104 } |
| 105 |
68 if (opImpl->throwInitializationError(es)) | 106 if (opImpl->throwInitializationError(es)) |
69 return 0; | 107 return 0; |
70 return CryptoOperation::create(algorithm, opImpl.get()); | 108 return CryptoOperation::create(algorithm, opImpl.get()); |
71 } | 109 } |
72 | 110 |
73 } // namespace | 111 } // namespace |
74 | 112 |
75 SubtleCrypto::SubtleCrypto() | 113 SubtleCrypto::SubtleCrypto() |
76 { | 114 { |
77 ScriptWrappable::init(this); | 115 ScriptWrappable::init(this); |
78 } | 116 } |
79 | 117 |
80 PassRefPtr<CryptoOperation> SubtleCrypto::encrypt(const Dictionary& rawAlgorithm
, Key*, ExceptionState& es) | 118 PassRefPtr<CryptoOperation> SubtleCrypto::encrypt(const Dictionary& rawAlgorithm
, Key* key, ExceptionState& es) |
81 { | 119 { |
82 return dummyOperation(rawAlgorithm, Encrypt, es); | 120 return createCryptoOperation(rawAlgorithm, key, Encrypt, es); |
83 } | 121 } |
84 | 122 |
85 PassRefPtr<CryptoOperation> SubtleCrypto::decrypt(const Dictionary& rawAlgorithm
, Key*, ExceptionState& es) | 123 PassRefPtr<CryptoOperation> SubtleCrypto::decrypt(const Dictionary& rawAlgorithm
, Key* key, ExceptionState& es) |
86 { | 124 { |
87 return dummyOperation(rawAlgorithm, Decrypt, es); | 125 return createCryptoOperation(rawAlgorithm, key, Decrypt, es); |
88 } | 126 } |
89 | 127 |
90 PassRefPtr<CryptoOperation> SubtleCrypto::sign(const Dictionary& rawAlgorithm, K
ey*, ExceptionState& es) | 128 PassRefPtr<CryptoOperation> SubtleCrypto::sign(const Dictionary& rawAlgorithm, K
ey* key, ExceptionState& es) |
91 { | 129 { |
92 return dummyOperation(rawAlgorithm, Sign, es); | 130 return createCryptoOperation(rawAlgorithm, key, Sign, es); |
93 } | 131 } |
94 | 132 |
95 PassRefPtr<CryptoOperation> SubtleCrypto::verifySignature(const Dictionary& rawA
lgorithm, Key*, ExceptionState& es) | 133 PassRefPtr<CryptoOperation> SubtleCrypto::verifySignature(const Dictionary& rawA
lgorithm, Key* key, ExceptionState& es) |
96 { | 134 { |
97 return dummyOperation(rawAlgorithm, Verify, es); | 135 // FIXME |
| 136 return 0; |
98 } | 137 } |
99 | 138 |
100 PassRefPtr<CryptoOperation> SubtleCrypto::digest(const Dictionary& rawAlgorithm,
ExceptionState& es) | 139 PassRefPtr<CryptoOperation> SubtleCrypto::digest(const Dictionary& rawAlgorithm,
ExceptionState& es) |
101 { | 140 { |
102 WebKit::WebCrypto* platformCrypto = WebKit::Platform::current()->crypto(); | 141 return createCryptoOperation(rawAlgorithm, 0, Digest, es); |
103 if (!platformCrypto) { | |
104 es.throwDOMException(NotSupportedError); | |
105 return 0; | |
106 } | |
107 | |
108 WebKit::WebCryptoAlgorithm algorithm; | |
109 if (!normalizeAlgorithm(rawAlgorithm, Digest, algorithm, es)) | |
110 return 0; | |
111 | |
112 RefPtr<CryptoOperationImpl> opImpl = CryptoOperationImpl::create(); | |
113 WebKit::WebCryptoOperationResult result(opImpl.get()); | |
114 platformCrypto->digest(algorithm, result); | |
115 if (opImpl->throwInitializationError(es)) | |
116 return 0; | |
117 return CryptoOperation::create(algorithm, opImpl.get()); | |
118 } | 142 } |
119 | 143 |
120 ScriptObject SubtleCrypto::importKey(const String& rawFormat, ArrayBufferView* k
eyData, const Dictionary& rawAlgorithm, bool extractable, const Vector<String>&
rawKeyUsages, ExceptionState& es) | 144 ScriptObject SubtleCrypto::importKey(const String& rawFormat, ArrayBufferView* k
eyData, const Dictionary& rawAlgorithm, bool extractable, const Vector<String>&
rawKeyUsages, ExceptionState& es) |
121 { | 145 { |
122 WebKit::WebCrypto* platformCrypto = WebKit::Platform::current()->crypto(); | 146 WebKit::WebCrypto* platformCrypto = WebKit::Platform::current()->crypto(); |
123 if (!platformCrypto) { | 147 if (!platformCrypto) { |
124 es.throwDOMException(NotSupportedError); | 148 es.throwDOMException(NotSupportedError); |
125 return ScriptObject(); | 149 return ScriptObject(); |
126 } | 150 } |
127 | 151 |
(...skipping 20 matching lines...) Expand all Loading... |
148 | 172 |
149 const unsigned char* keyDataBytes = static_cast<unsigned char*>(keyData->bas
eAddress()); | 173 const unsigned char* keyDataBytes = static_cast<unsigned char*>(keyData->bas
eAddress()); |
150 | 174 |
151 RefPtr<KeyOperation> keyOp = KeyOperation::create(); | 175 RefPtr<KeyOperation> keyOp = KeyOperation::create(); |
152 WebKit::WebCryptoKeyOperationResult result(keyOp.get()); | 176 WebKit::WebCryptoKeyOperationResult result(keyOp.get()); |
153 platformCrypto->importKey(format, keyDataBytes, keyData->byteLength(), algor
ithm, extractable, keyUsages, result); | 177 platformCrypto->importKey(format, keyDataBytes, keyData->byteLength(), algor
ithm, extractable, keyUsages, result); |
154 return keyOp->returnValue(es); | 178 return keyOp->returnValue(es); |
155 } | 179 } |
156 | 180 |
157 } // namespace WebCore | 181 } // namespace WebCore |
OLD | NEW |