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 28 matching lines...) Expand all Loading... |
39 #include "public/platform/WebCrypto.h" | 39 #include "public/platform/WebCrypto.h" |
40 #include "public/platform/WebCryptoAlgorithm.h" | 40 #include "public/platform/WebCryptoAlgorithm.h" |
41 #include "wtf/ArrayBufferView.h" | 41 #include "wtf/ArrayBufferView.h" |
42 | 42 |
43 namespace WebCore { | 43 namespace WebCore { |
44 | 44 |
45 namespace { | 45 namespace { |
46 | 46 |
47 // Seems like the generated bindings should take care of these however it | 47 // Seems like the generated bindings should take care of these however it |
48 // currently doesn't. See also http://crbug.com/264520 | 48 // currently doesn't. See also http://crbug.com/264520 |
49 template <typename T> | 49 bool ensureNotNull(const ArrayPiece& x, const char* paramName, CryptoResult* res
ult) |
50 bool ensureNotNull(T* x, const char* paramName, CryptoResult* result) | |
51 { | 50 { |
52 if (!x) { | 51 if (x.isNull()) { |
53 String message = String("Invalid ") + paramName + String(" argument"); | 52 String message = String("Invalid ") + paramName + String(" argument"); |
54 result->completeWithError(blink::WebCryptoErrorTypeType, blink::WebStrin
g(message)); | 53 result->completeWithError(blink::WebCryptoErrorTypeType, blink::WebStrin
g(message)); |
55 return false; | 54 return false; |
56 } | 55 } |
57 return true; | 56 return true; |
58 } | 57 } |
59 | 58 |
60 ScriptPromise startCryptoOperation(const Dictionary& rawAlgorithm, Key* key, Alg
orithmOperation operationType, ArrayBufferView* signature, ArrayBufferView* data
Buffer) | 59 bool ensureNotNull(Key* key, const char* paramName, CryptoResult* result) |
| 60 { |
| 61 if (!key) { |
| 62 String message = String("Invalid ") + paramName + String(" argument"); |
| 63 result->completeWithError(blink::WebCryptoErrorTypeType, blink::WebStrin
g(message)); |
| 64 return false; |
| 65 } |
| 66 return true; |
| 67 } |
| 68 |
| 69 ScriptPromise startCryptoOperation(const Dictionary& rawAlgorithm, Key* key, Alg
orithmOperation operationType, const ArrayPiece& signature, const ArrayPiece& da
taBuffer) |
61 { | 70 { |
62 RefPtr<CryptoResultImpl> result = CryptoResultImpl::create(); | 71 RefPtr<CryptoResultImpl> result = CryptoResultImpl::create(); |
63 ScriptPromise promise = result->promise(); | 72 ScriptPromise promise = result->promise(); |
64 | 73 |
65 bool requiresKey = operationType != Digest; | 74 bool requiresKey = operationType != Digest; |
66 | 75 |
67 if (requiresKey && !ensureNotNull(key, "key", result.get())) | 76 if (requiresKey && !ensureNotNull(key, "key", result.get())) |
68 return promise; | 77 return promise; |
69 if (operationType == Verify && !ensureNotNull(signature, "signature", result
.get())) | 78 if (operationType == Verify && !ensureNotNull(signature, "signature", result
.get())) |
70 return promise; | 79 return promise; |
71 if (!ensureNotNull(dataBuffer, "dataBuffer", result.get())) | 80 if (!ensureNotNull(dataBuffer, "dataBuffer", result.get())) |
72 return promise; | 81 return promise; |
73 | 82 |
74 blink::WebCryptoAlgorithm algorithm; | 83 blink::WebCryptoAlgorithm algorithm; |
75 if (!parseAlgorithm(rawAlgorithm, operationType, algorithm, result.get())) | 84 if (!parseAlgorithm(rawAlgorithm, operationType, algorithm, result.get())) |
76 return promise; | 85 return promise; |
77 | 86 |
78 if (requiresKey && !key->canBeUsedForAlgorithm(algorithm, operationType, res
ult.get())) | 87 if (requiresKey && !key->canBeUsedForAlgorithm(algorithm, operationType, res
ult.get())) |
79 return promise; | 88 return promise; |
80 | 89 |
81 const unsigned char* data = static_cast<const unsigned char*>(dataBuffer->ba
seAddress()); | 90 const unsigned char* data = dataBuffer.bytes(); |
82 unsigned dataSize = dataBuffer->byteLength(); | 91 unsigned dataSize = dataBuffer.byteLength(); |
83 | 92 |
84 switch (operationType) { | 93 switch (operationType) { |
85 case Encrypt: | 94 case Encrypt: |
86 blink::Platform::current()->crypto()->encrypt(algorithm, key->key(), dat
a, dataSize, result->result()); | 95 blink::Platform::current()->crypto()->encrypt(algorithm, key->key(), dat
a, dataSize, result->result()); |
87 break; | 96 break; |
88 case Decrypt: | 97 case Decrypt: |
89 blink::Platform::current()->crypto()->decrypt(algorithm, key->key(), dat
a, dataSize, result->result()); | 98 blink::Platform::current()->crypto()->decrypt(algorithm, key->key(), dat
a, dataSize, result->result()); |
90 break; | 99 break; |
91 case Sign: | 100 case Sign: |
92 blink::Platform::current()->crypto()->sign(algorithm, key->key(), data,
dataSize, result->result()); | 101 blink::Platform::current()->crypto()->sign(algorithm, key->key(), data,
dataSize, result->result()); |
93 break; | 102 break; |
94 case Verify: | 103 case Verify: |
95 blink::Platform::current()->crypto()->verifySignature(algorithm, key->ke
y(), reinterpret_cast<const unsigned char*>(signature->baseAddress()), signature
->byteLength(), data, dataSize, result->result()); | 104 blink::Platform::current()->crypto()->verifySignature(algorithm, key->ke
y(), signature.bytes(), signature.byteLength(), data, dataSize, result->result()
); |
96 break; | 105 break; |
97 case Digest: | 106 case Digest: |
98 blink::Platform::current()->crypto()->digest(algorithm, data, dataSize,
result->result()); | 107 blink::Platform::current()->crypto()->digest(algorithm, data, dataSize,
result->result()); |
99 break; | 108 break; |
100 default: | 109 default: |
101 ASSERT_NOT_REACHED(); | 110 ASSERT_NOT_REACHED(); |
102 return ScriptPromise(); | 111 return ScriptPromise(); |
103 } | 112 } |
104 | 113 |
105 return promise; | 114 return promise; |
106 } | 115 } |
107 | 116 |
108 } // namespace | 117 } // namespace |
109 | 118 |
110 SubtleCrypto::SubtleCrypto() | 119 SubtleCrypto::SubtleCrypto() |
111 { | 120 { |
112 ScriptWrappable::init(this); | 121 ScriptWrappable::init(this); |
113 } | 122 } |
114 | 123 |
115 ScriptPromise SubtleCrypto::encrypt(const Dictionary& rawAlgorithm, Key* key, Ar
rayBufferView* data) | 124 ScriptPromise SubtleCrypto::encrypt(const Dictionary& rawAlgorithm, Key* key, co
nst ArrayPiece& data) |
116 { | 125 { |
117 return startCryptoOperation(rawAlgorithm, key, Encrypt, 0, data); | 126 return startCryptoOperation(rawAlgorithm, key, Encrypt, ArrayPiece(), data); |
118 } | 127 } |
119 | 128 |
120 ScriptPromise SubtleCrypto::decrypt(const Dictionary& rawAlgorithm, Key* key, Ar
rayBufferView* data) | 129 ScriptPromise SubtleCrypto::decrypt(const Dictionary& rawAlgorithm, Key* key, co
nst ArrayPiece& data) |
121 { | 130 { |
122 return startCryptoOperation(rawAlgorithm, key, Decrypt, 0, data); | 131 return startCryptoOperation(rawAlgorithm, key, Decrypt, ArrayPiece(), data); |
123 } | 132 } |
124 | 133 |
125 ScriptPromise SubtleCrypto::sign(const Dictionary& rawAlgorithm, Key* key, Array
BufferView* data) | 134 ScriptPromise SubtleCrypto::sign(const Dictionary& rawAlgorithm, Key* key, const
ArrayPiece& data) |
126 { | 135 { |
127 return startCryptoOperation(rawAlgorithm, key, Sign, 0, data); | 136 return startCryptoOperation(rawAlgorithm, key, Sign, ArrayPiece(), data); |
128 } | 137 } |
129 | 138 |
130 ScriptPromise SubtleCrypto::verifySignature(const Dictionary& rawAlgorithm, Key*
key, ArrayBufferView* signature, ArrayBufferView* data) | 139 ScriptPromise SubtleCrypto::verifySignature(const Dictionary& rawAlgorithm, Key*
key, const ArrayPiece& signature, const ArrayPiece& data) |
131 { | 140 { |
132 return startCryptoOperation(rawAlgorithm, key, Verify, signature, data); | 141 return startCryptoOperation(rawAlgorithm, key, Verify, signature, data); |
133 } | 142 } |
134 | 143 |
135 ScriptPromise SubtleCrypto::digest(const Dictionary& rawAlgorithm, ArrayBufferVi
ew* data) | 144 ScriptPromise SubtleCrypto::digest(const Dictionary& rawAlgorithm, const ArrayPi
ece& data) |
136 { | 145 { |
137 return startCryptoOperation(rawAlgorithm, 0, Digest, 0, data); | 146 return startCryptoOperation(rawAlgorithm, 0, Digest, ArrayPiece(), data); |
138 } | 147 } |
139 | 148 |
140 ScriptPromise SubtleCrypto::generateKey(const Dictionary& rawAlgorithm, bool ext
ractable, const Vector<String>& rawKeyUsages) | 149 ScriptPromise SubtleCrypto::generateKey(const Dictionary& rawAlgorithm, bool ext
ractable, const Vector<String>& rawKeyUsages) |
141 { | 150 { |
142 RefPtr<CryptoResultImpl> result = CryptoResultImpl::create(); | 151 RefPtr<CryptoResultImpl> result = CryptoResultImpl::create(); |
143 ScriptPromise promise = result->promise(); | 152 ScriptPromise promise = result->promise(); |
144 | 153 |
145 blink::WebCryptoKeyUsageMask keyUsages; | 154 blink::WebCryptoKeyUsageMask keyUsages; |
146 if (!Key::parseUsageMask(rawKeyUsages, keyUsages, result.get())) | 155 if (!Key::parseUsageMask(rawKeyUsages, keyUsages, result.get())) |
147 return promise; | 156 return promise; |
148 | 157 |
149 blink::WebCryptoAlgorithm algorithm; | 158 blink::WebCryptoAlgorithm algorithm; |
150 if (!parseAlgorithm(rawAlgorithm, GenerateKey, algorithm, result.get())) | 159 if (!parseAlgorithm(rawAlgorithm, GenerateKey, algorithm, result.get())) |
151 return promise; | 160 return promise; |
152 | 161 |
153 blink::Platform::current()->crypto()->generateKey(algorithm, extractable, ke
yUsages, result->result()); | 162 blink::Platform::current()->crypto()->generateKey(algorithm, extractable, ke
yUsages, result->result()); |
154 return promise; | 163 return promise; |
155 } | 164 } |
156 | 165 |
157 ScriptPromise SubtleCrypto::importKey(const String& rawFormat, ArrayBufferView*
keyData, const Dictionary& rawAlgorithm, bool extractable, const Vector<String>&
rawKeyUsages) | 166 ScriptPromise SubtleCrypto::importKey(const String& rawFormat, const ArrayPiece&
keyData, const Dictionary& rawAlgorithm, bool extractable, const Vector<String>
& rawKeyUsages) |
158 { | 167 { |
159 RefPtr<CryptoResultImpl> result = CryptoResultImpl::create(); | 168 RefPtr<CryptoResultImpl> result = CryptoResultImpl::create(); |
160 ScriptPromise promise = result->promise(); | 169 ScriptPromise promise = result->promise(); |
161 | 170 |
162 if (!ensureNotNull(keyData, "keyData", result.get())) | 171 if (!ensureNotNull(keyData, "keyData", result.get())) |
163 return promise; | 172 return promise; |
164 | 173 |
165 blink::WebCryptoKeyFormat format; | 174 blink::WebCryptoKeyFormat format; |
166 if (!Key::parseFormat(rawFormat, format, result.get())) | 175 if (!Key::parseFormat(rawFormat, format, result.get())) |
167 return promise; | 176 return promise; |
168 | 177 |
169 blink::WebCryptoKeyUsageMask keyUsages; | 178 blink::WebCryptoKeyUsageMask keyUsages; |
170 if (!Key::parseUsageMask(rawKeyUsages, keyUsages, result.get())) | 179 if (!Key::parseUsageMask(rawKeyUsages, keyUsages, result.get())) |
171 return promise; | 180 return promise; |
172 | 181 |
173 blink::WebCryptoAlgorithm algorithm; | 182 blink::WebCryptoAlgorithm algorithm; |
174 if (!parseAlgorithm(rawAlgorithm, ImportKey, algorithm, result.get())) | 183 if (!parseAlgorithm(rawAlgorithm, ImportKey, algorithm, result.get())) |
175 return promise; | 184 return promise; |
176 | 185 |
177 const unsigned char* keyDataBytes = static_cast<unsigned char*>(keyData->bas
eAddress()); | 186 blink::Platform::current()->crypto()->importKey(format, keyData.bytes(), key
Data.byteLength(), algorithm, extractable, keyUsages, result->result()); |
178 | |
179 blink::Platform::current()->crypto()->importKey(format, keyDataBytes, keyDat
a->byteLength(), algorithm, extractable, keyUsages, result->result()); | |
180 return promise; | 187 return promise; |
181 } | 188 } |
182 | 189 |
183 ScriptPromise SubtleCrypto::exportKey(const String& rawFormat, Key* key) | 190 ScriptPromise SubtleCrypto::exportKey(const String& rawFormat, Key* key) |
184 { | 191 { |
185 RefPtr<CryptoResultImpl> result = CryptoResultImpl::create(); | 192 RefPtr<CryptoResultImpl> result = CryptoResultImpl::create(); |
186 ScriptPromise promise = result->promise(); | 193 ScriptPromise promise = result->promise(); |
187 | 194 |
188 if (!ensureNotNull(key, "key", result.get())) | 195 if (!ensureNotNull(key, "key", result.get())) |
189 return promise; | 196 return promise; |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
225 return promise; | 232 return promise; |
226 } | 233 } |
227 | 234 |
228 if (!wrappingKey->canBeUsedForAlgorithm(wrapAlgorithm, WrapKey, result.get()
)) | 235 if (!wrappingKey->canBeUsedForAlgorithm(wrapAlgorithm, WrapKey, result.get()
)) |
229 return promise; | 236 return promise; |
230 | 237 |
231 blink::Platform::current()->crypto()->wrapKey(format, key->key(), wrappingKe
y->key(), wrapAlgorithm, result->result()); | 238 blink::Platform::current()->crypto()->wrapKey(format, key->key(), wrappingKe
y->key(), wrapAlgorithm, result->result()); |
232 return promise; | 239 return promise; |
233 } | 240 } |
234 | 241 |
235 ScriptPromise SubtleCrypto::unwrapKey(const String& rawFormat, ArrayBufferView*
wrappedKey, Key* unwrappingKey, const Dictionary& rawUnwrapAlgorithm, const Dict
ionary& rawUnwrappedKeyAlgorithm, bool extractable, const Vector<String>& rawKey
Usages) | 242 ScriptPromise SubtleCrypto::unwrapKey(const String& rawFormat, const ArrayPiece&
wrappedKey, Key* unwrappingKey, const Dictionary& rawUnwrapAlgorithm, const Dic
tionary& rawUnwrappedKeyAlgorithm, bool extractable, const Vector<String>& rawKe
yUsages) |
236 { | 243 { |
237 RefPtr<CryptoResultImpl> result = CryptoResultImpl::create(); | 244 RefPtr<CryptoResultImpl> result = CryptoResultImpl::create(); |
238 ScriptPromise promise = result->promise(); | 245 ScriptPromise promise = result->promise(); |
239 | 246 |
240 if (!ensureNotNull(wrappedKey, "wrappedKey", result.get())) | 247 if (!ensureNotNull(wrappedKey, "wrappedKey", result.get())) |
241 return promise; | 248 return promise; |
242 if (!ensureNotNull(unwrappingKey, "unwrappingKey", result.get())) | 249 if (!ensureNotNull(unwrappingKey, "unwrappingKey", result.get())) |
243 return promise; | 250 return promise; |
244 | 251 |
245 blink::WebCryptoKeyFormat format; | 252 blink::WebCryptoKeyFormat format; |
246 if (!Key::parseFormat(rawFormat, format, result.get())) | 253 if (!Key::parseFormat(rawFormat, format, result.get())) |
247 return promise; | 254 return promise; |
248 | 255 |
249 blink::WebCryptoKeyUsageMask keyUsages; | 256 blink::WebCryptoKeyUsageMask keyUsages; |
250 if (!Key::parseUsageMask(rawKeyUsages, keyUsages, result.get())) | 257 if (!Key::parseUsageMask(rawKeyUsages, keyUsages, result.get())) |
251 return promise; | 258 return promise; |
252 | 259 |
253 blink::WebCryptoAlgorithm unwrapAlgorithm; | 260 blink::WebCryptoAlgorithm unwrapAlgorithm; |
254 if (!parseAlgorithm(rawUnwrapAlgorithm, UnwrapKey, unwrapAlgorithm, result.g
et())) | 261 if (!parseAlgorithm(rawUnwrapAlgorithm, UnwrapKey, unwrapAlgorithm, result.g
et())) |
255 return promise; | 262 return promise; |
256 | 263 |
257 blink::WebCryptoAlgorithm unwrappedKeyAlgorithm; | 264 blink::WebCryptoAlgorithm unwrappedKeyAlgorithm; |
258 if (!parseAlgorithm(rawUnwrappedKeyAlgorithm, ImportKey, unwrappedKeyAlgorit
hm, result.get())) | 265 if (!parseAlgorithm(rawUnwrappedKeyAlgorithm, ImportKey, unwrappedKeyAlgorit
hm, result.get())) |
259 return promise; | 266 return promise; |
260 | 267 |
261 if (!unwrappingKey->canBeUsedForAlgorithm(unwrapAlgorithm, UnwrapKey, result
.get())) | 268 if (!unwrappingKey->canBeUsedForAlgorithm(unwrapAlgorithm, UnwrapKey, result
.get())) |
262 return promise; | 269 return promise; |
263 | 270 |
264 const unsigned char* wrappedKeyData = static_cast<const unsigned char*>(wrap
pedKey->baseAddress()); | 271 blink::Platform::current()->crypto()->unwrapKey(format, wrappedKey.bytes(),
wrappedKey.byteLength(), unwrappingKey->key(), unwrapAlgorithm, unwrappedKeyAlgo
rithm, extractable, keyUsages, result->result()); |
265 unsigned wrappedKeyDataSize = wrappedKey->byteLength(); | |
266 | |
267 blink::Platform::current()->crypto()->unwrapKey(format, wrappedKeyData, wrap
pedKeyDataSize, unwrappingKey->key(), unwrapAlgorithm, unwrappedKeyAlgorithm, ex
tractable, keyUsages, result->result()); | |
268 return promise; | 272 return promise; |
269 } | 273 } |
270 | 274 |
271 } // namespace WebCore | 275 } // namespace WebCore |
OLD | NEW |