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 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
57 static bool ensureNotNull(Key* key, const char* paramName, CryptoResult* result) | 57 static bool ensureNotNull(Key* key, const char* paramName, CryptoResult* result) |
58 { | 58 { |
59 if (!key) { | 59 if (!key) { |
60 String message = String("Invalid ") + paramName + String(" argument"); | 60 String message = String("Invalid ") + paramName + String(" argument"); |
61 result->completeWithError(blink::WebCryptoErrorTypeType, blink::WebStrin
g(message)); | 61 result->completeWithError(blink::WebCryptoErrorTypeType, blink::WebStrin
g(message)); |
62 return false; | 62 return false; |
63 } | 63 } |
64 return true; | 64 return true; |
65 } | 65 } |
66 | 66 |
67 static ScriptPromise startCryptoOperation(ScriptState* scriptState, const Dictio
nary& rawAlgorithm, Key* key, AlgorithmOperation operationType, const ArrayPiece
& signature, const ArrayPiece& dataBuffer) | 67 bool parseAlgorithm(const Dictionary& raw, blink::WebCryptoOperation op, blink::
WebCryptoAlgorithm& algorithm, CryptoResult* result) |
| 68 { |
| 69 AlgorithmError error; |
| 70 bool success = normalizeAlgorithm(raw, op, algorithm, &error); |
| 71 if (!success) |
| 72 result->completeWithError(error.errorType, error.errorDetails); |
| 73 return success; |
| 74 } |
| 75 |
| 76 static ScriptPromise startCryptoOperation(ScriptState* scriptState, const Dictio
nary& rawAlgorithm, Key* key, blink::WebCryptoOperation operationType, const Arr
ayPiece& signature, const ArrayPiece& dataBuffer) |
68 { | 77 { |
69 RefPtr<CryptoResultImpl> result = CryptoResultImpl::create(scriptState); | 78 RefPtr<CryptoResultImpl> result = CryptoResultImpl::create(scriptState); |
70 ScriptPromise promise = result->promise(); | 79 ScriptPromise promise = result->promise(); |
71 | 80 |
72 bool requiresKey = operationType != Digest; | 81 bool requiresKey = operationType != blink::WebCryptoOperationDigest; |
73 | 82 |
74 if (requiresKey && !ensureNotNull(key, "key", result.get())) | 83 if (requiresKey && !ensureNotNull(key, "key", result.get())) |
75 return promise; | 84 return promise; |
76 if (operationType == Verify && !ensureNotNull(signature, "signature", result
.get())) | 85 if (operationType == blink::WebCryptoOperationVerify && !ensureNotNull(signa
ture, "signature", result.get())) |
77 return promise; | 86 return promise; |
78 if (!ensureNotNull(dataBuffer, "dataBuffer", result.get())) | 87 if (!ensureNotNull(dataBuffer, "dataBuffer", result.get())) |
79 return promise; | 88 return promise; |
80 | 89 |
81 blink::WebCryptoAlgorithm algorithm; | 90 blink::WebCryptoAlgorithm algorithm; |
82 if (!parseAlgorithm(rawAlgorithm, operationType, algorithm, result.get())) | 91 if (!parseAlgorithm(rawAlgorithm, operationType, algorithm, result.get())) |
83 return promise; | 92 return promise; |
84 | 93 |
85 if (requiresKey && !key->canBeUsedForAlgorithm(algorithm, operationType, res
ult.get())) | 94 if (requiresKey && !key->canBeUsedForAlgorithm(algorithm, operationType, res
ult.get())) |
86 return promise; | 95 return promise; |
87 | 96 |
88 const unsigned char* data = dataBuffer.bytes(); | 97 const unsigned char* data = dataBuffer.bytes(); |
89 unsigned dataSize = dataBuffer.byteLength(); | 98 unsigned dataSize = dataBuffer.byteLength(); |
90 | 99 |
91 switch (operationType) { | 100 switch (operationType) { |
92 case Encrypt: | 101 case blink::WebCryptoOperationEncrypt: |
93 blink::Platform::current()->crypto()->encrypt(algorithm, key->key(), dat
a, dataSize, result->result()); | 102 blink::Platform::current()->crypto()->encrypt(algorithm, key->key(), dat
a, dataSize, result->result()); |
94 break; | 103 break; |
95 case Decrypt: | 104 case blink::WebCryptoOperationDecrypt: |
96 blink::Platform::current()->crypto()->decrypt(algorithm, key->key(), dat
a, dataSize, result->result()); | 105 blink::Platform::current()->crypto()->decrypt(algorithm, key->key(), dat
a, dataSize, result->result()); |
97 break; | 106 break; |
98 case Sign: | 107 case blink::WebCryptoOperationSign: |
99 blink::Platform::current()->crypto()->sign(algorithm, key->key(), data,
dataSize, result->result()); | 108 blink::Platform::current()->crypto()->sign(algorithm, key->key(), data,
dataSize, result->result()); |
100 break; | 109 break; |
101 case Verify: | 110 case blink::WebCryptoOperationVerify: |
102 blink::Platform::current()->crypto()->verifySignature(algorithm, key->ke
y(), signature.bytes(), signature.byteLength(), data, dataSize, result->result()
); | 111 blink::Platform::current()->crypto()->verifySignature(algorithm, key->ke
y(), signature.bytes(), signature.byteLength(), data, dataSize, result->result()
); |
103 break; | 112 break; |
104 case Digest: | 113 case blink::WebCryptoOperationDigest: |
105 blink::Platform::current()->crypto()->digest(algorithm, data, dataSize,
result->result()); | 114 blink::Platform::current()->crypto()->digest(algorithm, data, dataSize,
result->result()); |
106 break; | 115 break; |
107 default: | 116 default: |
108 ASSERT_NOT_REACHED(); | 117 ASSERT_NOT_REACHED(); |
109 return ScriptPromise(); | 118 return ScriptPromise(); |
110 } | 119 } |
111 | 120 |
112 return promise; | 121 return promise; |
113 } | 122 } |
114 | 123 |
115 SubtleCrypto::SubtleCrypto() | 124 SubtleCrypto::SubtleCrypto() |
116 { | 125 { |
117 ScriptWrappable::init(this); | 126 ScriptWrappable::init(this); |
118 } | 127 } |
119 | 128 |
120 ScriptPromise SubtleCrypto::encrypt(ScriptState* scriptState, const Dictionary&
rawAlgorithm, Key* key, const ArrayPiece& data) | 129 ScriptPromise SubtleCrypto::encrypt(ScriptState* scriptState, const Dictionary&
rawAlgorithm, Key* key, const ArrayPiece& data) |
121 { | 130 { |
122 return startCryptoOperation(scriptState, rawAlgorithm, key, Encrypt, ArrayPi
ece(), data); | 131 return startCryptoOperation(scriptState, rawAlgorithm, key, blink::WebCrypto
OperationEncrypt, ArrayPiece(), data); |
123 } | 132 } |
124 | 133 |
125 ScriptPromise SubtleCrypto::decrypt(ScriptState* scriptState, const Dictionary&
rawAlgorithm, Key* key, const ArrayPiece& data) | 134 ScriptPromise SubtleCrypto::decrypt(ScriptState* scriptState, const Dictionary&
rawAlgorithm, Key* key, const ArrayPiece& data) |
126 { | 135 { |
127 return startCryptoOperation(scriptState, rawAlgorithm, key, Decrypt, ArrayPi
ece(), data); | 136 return startCryptoOperation(scriptState, rawAlgorithm, key, blink::WebCrypto
OperationDecrypt, ArrayPiece(), data); |
128 } | 137 } |
129 | 138 |
130 ScriptPromise SubtleCrypto::sign(ScriptState* scriptState, const Dictionary& raw
Algorithm, Key* key, const ArrayPiece& data) | 139 ScriptPromise SubtleCrypto::sign(ScriptState* scriptState, const Dictionary& raw
Algorithm, Key* key, const ArrayPiece& data) |
131 { | 140 { |
132 return startCryptoOperation(scriptState, rawAlgorithm, key, Sign, ArrayPiece
(), data); | 141 return startCryptoOperation(scriptState, rawAlgorithm, key, blink::WebCrypto
OperationSign, ArrayPiece(), data); |
133 } | 142 } |
134 | 143 |
135 ScriptPromise SubtleCrypto::verifySignature(ScriptState* scriptState, const Dict
ionary& rawAlgorithm, Key* key, const ArrayPiece& signature, const ArrayPiece& d
ata) | 144 ScriptPromise SubtleCrypto::verifySignature(ScriptState* scriptState, const Dict
ionary& rawAlgorithm, Key* key, const ArrayPiece& signature, const ArrayPiece& d
ata) |
136 { | 145 { |
137 return startCryptoOperation(scriptState, rawAlgorithm, key, Verify, signatur
e, data); | 146 return startCryptoOperation(scriptState, rawAlgorithm, key, blink::WebCrypto
OperationVerify, signature, data); |
138 } | 147 } |
139 | 148 |
140 ScriptPromise SubtleCrypto::digest(ScriptState* scriptState, const Dictionary& r
awAlgorithm, const ArrayPiece& data) | 149 ScriptPromise SubtleCrypto::digest(ScriptState* scriptState, const Dictionary& r
awAlgorithm, const ArrayPiece& data) |
141 { | 150 { |
142 return startCryptoOperation(scriptState, rawAlgorithm, 0, Digest, ArrayPiece
(), data); | 151 return startCryptoOperation(scriptState, rawAlgorithm, 0, blink::WebCryptoOp
erationDigest, ArrayPiece(), data); |
143 } | 152 } |
144 | 153 |
145 ScriptPromise SubtleCrypto::generateKey(ScriptState* scriptState, const Dictiona
ry& rawAlgorithm, bool extractable, const Vector<String>& rawKeyUsages) | 154 ScriptPromise SubtleCrypto::generateKey(ScriptState* scriptState, const Dictiona
ry& rawAlgorithm, bool extractable, const Vector<String>& rawKeyUsages) |
146 { | 155 { |
147 RefPtr<CryptoResultImpl> result = CryptoResultImpl::create(scriptState); | 156 RefPtr<CryptoResultImpl> result = CryptoResultImpl::create(scriptState); |
148 ScriptPromise promise = result->promise(); | 157 ScriptPromise promise = result->promise(); |
149 | 158 |
150 blink::WebCryptoKeyUsageMask keyUsages; | 159 blink::WebCryptoKeyUsageMask keyUsages; |
151 if (!Key::parseUsageMask(rawKeyUsages, keyUsages, result.get())) | 160 if (!Key::parseUsageMask(rawKeyUsages, keyUsages, result.get())) |
152 return promise; | 161 return promise; |
153 | 162 |
154 blink::WebCryptoAlgorithm algorithm; | 163 blink::WebCryptoAlgorithm algorithm; |
155 if (!parseAlgorithm(rawAlgorithm, GenerateKey, algorithm, result.get())) | 164 if (!parseAlgorithm(rawAlgorithm, blink::WebCryptoOperationGenerateKey, algo
rithm, result.get())) |
156 return promise; | 165 return promise; |
157 | 166 |
158 blink::Platform::current()->crypto()->generateKey(algorithm, extractable, ke
yUsages, result->result()); | 167 blink::Platform::current()->crypto()->generateKey(algorithm, extractable, ke
yUsages, result->result()); |
159 return promise; | 168 return promise; |
160 } | 169 } |
161 | 170 |
162 ScriptPromise SubtleCrypto::importKey(ScriptState* scriptState, const String& ra
wFormat, const ArrayPiece& keyData, const Dictionary& rawAlgorithm, bool extract
able, const Vector<String>& rawKeyUsages) | 171 ScriptPromise SubtleCrypto::importKey(ScriptState* scriptState, const String& ra
wFormat, const ArrayPiece& keyData, const Dictionary& rawAlgorithm, bool extract
able, const Vector<String>& rawKeyUsages) |
163 { | 172 { |
164 RefPtr<CryptoResultImpl> result = CryptoResultImpl::create(scriptState); | 173 RefPtr<CryptoResultImpl> result = CryptoResultImpl::create(scriptState); |
165 ScriptPromise promise = result->promise(); | 174 ScriptPromise promise = result->promise(); |
166 | 175 |
167 if (!ensureNotNull(keyData, "keyData", result.get())) | 176 if (!ensureNotNull(keyData, "keyData", result.get())) |
168 return promise; | 177 return promise; |
169 | 178 |
170 blink::WebCryptoKeyFormat format; | 179 blink::WebCryptoKeyFormat format; |
171 if (!Key::parseFormat(rawFormat, format, result.get())) | 180 if (!Key::parseFormat(rawFormat, format, result.get())) |
172 return promise; | 181 return promise; |
173 | 182 |
174 blink::WebCryptoKeyUsageMask keyUsages; | 183 blink::WebCryptoKeyUsageMask keyUsages; |
175 if (!Key::parseUsageMask(rawKeyUsages, keyUsages, result.get())) | 184 if (!Key::parseUsageMask(rawKeyUsages, keyUsages, result.get())) |
176 return promise; | 185 return promise; |
177 | 186 |
178 blink::WebCryptoAlgorithm algorithm; | 187 blink::WebCryptoAlgorithm algorithm; |
179 if (!parseAlgorithm(rawAlgorithm, ImportKey, algorithm, result.get())) | 188 if (!parseAlgorithm(rawAlgorithm, blink::WebCryptoOperationImportKey, algori
thm, result.get())) |
180 return promise; | 189 return promise; |
181 | 190 |
182 blink::Platform::current()->crypto()->importKey(format, keyData.bytes(), key
Data.byteLength(), algorithm, extractable, keyUsages, result->result()); | 191 blink::Platform::current()->crypto()->importKey(format, keyData.bytes(), key
Data.byteLength(), algorithm, extractable, keyUsages, result->result()); |
183 return promise; | 192 return promise; |
184 } | 193 } |
185 | 194 |
186 ScriptPromise SubtleCrypto::exportKey(ScriptState* scriptState, const String& ra
wFormat, Key* key) | 195 ScriptPromise SubtleCrypto::exportKey(ScriptState* scriptState, const String& ra
wFormat, Key* key) |
187 { | 196 { |
188 RefPtr<CryptoResultImpl> result = CryptoResultImpl::create(scriptState); | 197 RefPtr<CryptoResultImpl> result = CryptoResultImpl::create(scriptState); |
189 ScriptPromise promise = result->promise(); | 198 ScriptPromise promise = result->promise(); |
(...skipping 23 matching lines...) Expand all Loading... |
213 return promise; | 222 return promise; |
214 | 223 |
215 if (!ensureNotNull(wrappingKey, "wrappingKey", result.get())) | 224 if (!ensureNotNull(wrappingKey, "wrappingKey", result.get())) |
216 return promise; | 225 return promise; |
217 | 226 |
218 blink::WebCryptoKeyFormat format; | 227 blink::WebCryptoKeyFormat format; |
219 if (!Key::parseFormat(rawFormat, format, result.get())) | 228 if (!Key::parseFormat(rawFormat, format, result.get())) |
220 return promise; | 229 return promise; |
221 | 230 |
222 blink::WebCryptoAlgorithm wrapAlgorithm; | 231 blink::WebCryptoAlgorithm wrapAlgorithm; |
223 if (!parseAlgorithm(rawWrapAlgorithm, WrapKey, wrapAlgorithm, result.get())) | 232 if (!parseAlgorithm(rawWrapAlgorithm, blink::WebCryptoOperationWrapKey, wrap
Algorithm, result.get())) |
224 return promise; | 233 return promise; |
225 | 234 |
226 if (!key->extractable()) { | 235 if (!key->extractable()) { |
227 result->completeWithError(blink::WebCryptoErrorTypeInvalidAccess, "key i
s not extractable"); | 236 result->completeWithError(blink::WebCryptoErrorTypeInvalidAccess, "key i
s not extractable"); |
228 return promise; | 237 return promise; |
229 } | 238 } |
230 | 239 |
231 if (!wrappingKey->canBeUsedForAlgorithm(wrapAlgorithm, WrapKey, result.get()
)) | 240 if (!wrappingKey->canBeUsedForAlgorithm(wrapAlgorithm, blink::WebCryptoOpera
tionWrapKey, result.get())) |
232 return promise; | 241 return promise; |
233 | 242 |
234 blink::Platform::current()->crypto()->wrapKey(format, key->key(), wrappingKe
y->key(), wrapAlgorithm, result->result()); | 243 blink::Platform::current()->crypto()->wrapKey(format, key->key(), wrappingKe
y->key(), wrapAlgorithm, result->result()); |
235 return promise; | 244 return promise; |
236 } | 245 } |
237 | 246 |
238 ScriptPromise SubtleCrypto::unwrapKey(ScriptState* scriptState, const String& ra
wFormat, const ArrayPiece& wrappedKey, Key* unwrappingKey, const Dictionary& raw
UnwrapAlgorithm, const Dictionary& rawUnwrappedKeyAlgorithm, bool extractable, c
onst Vector<String>& rawKeyUsages) | 247 ScriptPromise SubtleCrypto::unwrapKey(ScriptState* scriptState, const String& ra
wFormat, const ArrayPiece& wrappedKey, Key* unwrappingKey, const Dictionary& raw
UnwrapAlgorithm, const Dictionary& rawUnwrappedKeyAlgorithm, bool extractable, c
onst Vector<String>& rawKeyUsages) |
239 { | 248 { |
240 RefPtr<CryptoResultImpl> result = CryptoResultImpl::create(scriptState); | 249 RefPtr<CryptoResultImpl> result = CryptoResultImpl::create(scriptState); |
241 ScriptPromise promise = result->promise(); | 250 ScriptPromise promise = result->promise(); |
242 | 251 |
243 if (!ensureNotNull(wrappedKey, "wrappedKey", result.get())) | 252 if (!ensureNotNull(wrappedKey, "wrappedKey", result.get())) |
244 return promise; | 253 return promise; |
245 if (!ensureNotNull(unwrappingKey, "unwrappingKey", result.get())) | 254 if (!ensureNotNull(unwrappingKey, "unwrappingKey", result.get())) |
246 return promise; | 255 return promise; |
247 | 256 |
248 blink::WebCryptoKeyFormat format; | 257 blink::WebCryptoKeyFormat format; |
249 if (!Key::parseFormat(rawFormat, format, result.get())) | 258 if (!Key::parseFormat(rawFormat, format, result.get())) |
250 return promise; | 259 return promise; |
251 | 260 |
252 blink::WebCryptoKeyUsageMask keyUsages; | 261 blink::WebCryptoKeyUsageMask keyUsages; |
253 if (!Key::parseUsageMask(rawKeyUsages, keyUsages, result.get())) | 262 if (!Key::parseUsageMask(rawKeyUsages, keyUsages, result.get())) |
254 return promise; | 263 return promise; |
255 | 264 |
256 blink::WebCryptoAlgorithm unwrapAlgorithm; | 265 blink::WebCryptoAlgorithm unwrapAlgorithm; |
257 if (!parseAlgorithm(rawUnwrapAlgorithm, UnwrapKey, unwrapAlgorithm, result.g
et())) | 266 if (!parseAlgorithm(rawUnwrapAlgorithm, blink::WebCryptoOperationUnwrapKey,
unwrapAlgorithm, result.get())) |
258 return promise; | 267 return promise; |
259 | 268 |
260 blink::WebCryptoAlgorithm unwrappedKeyAlgorithm; | 269 blink::WebCryptoAlgorithm unwrappedKeyAlgorithm; |
261 if (!parseAlgorithm(rawUnwrappedKeyAlgorithm, ImportKey, unwrappedKeyAlgorit
hm, result.get())) | 270 if (!parseAlgorithm(rawUnwrappedKeyAlgorithm, blink::WebCryptoOperationImpor
tKey, unwrappedKeyAlgorithm, result.get())) |
262 return promise; | 271 return promise; |
263 | 272 |
264 if (!unwrappingKey->canBeUsedForAlgorithm(unwrapAlgorithm, UnwrapKey, result
.get())) | 273 if (!unwrappingKey->canBeUsedForAlgorithm(unwrapAlgorithm, blink::WebCryptoO
perationUnwrapKey, result.get())) |
265 return promise; | 274 return promise; |
266 | 275 |
267 blink::Platform::current()->crypto()->unwrapKey(format, wrappedKey.bytes(),
wrappedKey.byteLength(), unwrappingKey->key(), unwrapAlgorithm, unwrappedKeyAlgo
rithm, extractable, keyUsages, result->result()); | 276 blink::Platform::current()->crypto()->unwrapKey(format, wrappedKey.bytes(),
wrappedKey.byteLength(), unwrappingKey->key(), unwrapAlgorithm, unwrappedKeyAlgo
rithm, extractable, keyUsages, result->result()); |
268 return promise; | 277 return promise; |
269 } | 278 } |
270 | 279 |
271 } // namespace WebCore | 280 } // namespace WebCore |
OLD | NEW |