OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "content/renderer/webcrypto/platform_crypto.h" | 5 #include "content/renderer/webcrypto/platform_crypto.h" |
6 | 6 |
7 #include <vector> | 7 #include <vector> |
8 #include <openssl/aes.h> | 8 #include <openssl/aes.h> |
9 #include <openssl/evp.h> | 9 #include <openssl/evp.h> |
10 #include <openssl/hmac.h> | 10 #include <openssl/hmac.h> |
11 #include <openssl/rand.h> | 11 #include <openssl/rand.h> |
12 #include <openssl/sha.h> | 12 #include <openssl/sha.h> |
13 | 13 |
14 #include "base/logging.h" | 14 #include "base/logging.h" |
15 #include "content/renderer/webcrypto/crypto_data.h" | 15 #include "content/renderer/webcrypto/crypto_data.h" |
16 #include "content/renderer/webcrypto/webcrypto_util.h" | 16 #include "content/renderer/webcrypto/webcrypto_util.h" |
17 #include "crypto/openssl_util.h" | 17 #include "crypto/openssl_util.h" |
18 #include "third_party/WebKit/public/platform/WebArrayBuffer.h" | 18 #include "third_party/WebKit/public/platform/WebArrayBuffer.h" |
19 #include "third_party/WebKit/public/platform/WebCryptoAlgorithm.h" | 19 #include "third_party/WebKit/public/platform/WebCryptoAlgorithm.h" |
20 #include "third_party/WebKit/public/platform/WebCryptoAlgorithmParams.h" | 20 #include "third_party/WebKit/public/platform/WebCryptoAlgorithmParams.h" |
| 21 #ifdef WEBCRYPTO_HAS_KEY_ALGORITHM |
| 22 #include "third_party/WebKit/public/platform/WebCryptoKeyAlgorithm.h" |
| 23 #endif |
21 | 24 |
22 namespace content { | 25 namespace content { |
23 | 26 |
24 namespace webcrypto { | 27 namespace webcrypto { |
25 | 28 |
26 namespace platform { | 29 namespace platform { |
27 | 30 |
28 class SymKey : public Key { | 31 class SymKey : public Key { |
29 public: | 32 public: |
30 explicit SymKey(const CryptoData& key_data) | 33 explicit SymKey(const CryptoData& key_data) |
(...skipping 184 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
215 // TODO(eroman): Is this right? | 218 // TODO(eroman): Is this right? |
216 if (keylen_bytes == 0) | 219 if (keylen_bytes == 0) |
217 return Status::ErrorGenerateKeyLength(); | 220 return Status::ErrorGenerateKeyLength(); |
218 | 221 |
219 crypto::OpenSSLErrStackTracer(FROM_HERE); | 222 crypto::OpenSSLErrStackTracer(FROM_HERE); |
220 | 223 |
221 std::vector<unsigned char> random_bytes(keylen_bytes, 0); | 224 std::vector<unsigned char> random_bytes(keylen_bytes, 0); |
222 if (!(RAND_bytes(&random_bytes[0], keylen_bytes))) | 225 if (!(RAND_bytes(&random_bytes[0], keylen_bytes))) |
223 return Status::Error(); | 226 return Status::Error(); |
224 | 227 |
| 228 #ifdef WEBCRYPTO_HAS_KEY_ALGORITHM |
| 229 blink::WebCryptoKeyAlgorithm key_algorithm; |
| 230 if (!CreateSecretKeyAlgorithm(algorithm, keylen_bytes, &key_algorithm)) |
| 231 return Status::ErrorUnexpected(); |
| 232 #else |
| 233 const blink::WebCryptoAlgorithm key_algorithm = algorithm; |
| 234 #endif |
| 235 |
225 *key = blink::WebCryptoKey::create(new SymKey(CryptoData(random_bytes)), | 236 *key = blink::WebCryptoKey::create(new SymKey(CryptoData(random_bytes)), |
226 blink::WebCryptoKeyTypeSecret, | 237 blink::WebCryptoKeyTypeSecret, |
227 extractable, | 238 extractable, |
228 algorithm, | 239 key_algorithm, |
229 usage_mask); | 240 usage_mask); |
230 | 241 |
231 return Status::Success(); | 242 return Status::Success(); |
232 } | 243 } |
233 | 244 |
234 Status GenerateRsaKeyPair(const blink::WebCryptoAlgorithm& algorithm, | 245 Status GenerateRsaKeyPair(const blink::WebCryptoAlgorithm& algorithm, |
235 bool extractable, | 246 bool extractable, |
236 blink::WebCryptoKeyUsageMask usage_mask, | 247 blink::WebCryptoKeyUsageMask usage_mask, |
| 248 unsigned int modulus_length_bits, |
| 249 const CryptoData& public_exponent, |
| 250 const blink::WebCryptoAlgorithm& hash, |
237 blink::WebCryptoKey* public_key, | 251 blink::WebCryptoKey* public_key, |
238 blink::WebCryptoKey* private_key) { | 252 blink::WebCryptoKey* private_key) { |
239 // TODO(padolph): Placeholder for OpenSSL implementation. | 253 // TODO(padolph): Placeholder for OpenSSL implementation. |
240 // Issue http://crbug.com/267888. | 254 // Issue http://crbug.com/267888. |
241 return Status::ErrorUnsupported(); | 255 return Status::ErrorUnsupported(); |
242 } | 256 } |
243 | 257 |
244 Status ImportKeyRaw(const blink::WebCryptoAlgorithm& algorithm, | 258 Status ImportKeyRaw(const blink::WebCryptoAlgorithm& algorithm, |
245 const CryptoData& key_data, | 259 const CryptoData& key_data, |
246 bool extractable, | 260 bool extractable, |
247 blink::WebCryptoKeyUsageMask usage_mask, | 261 blink::WebCryptoKeyUsageMask usage_mask, |
248 blink::WebCryptoKey* key) { | 262 blink::WebCryptoKey* key) { |
| 263 |
| 264 #ifdef WEBCRYPTO_HAS_KEY_ALGORITHM |
| 265 blink::WebCryptoKeyAlgorithm key_algorithm; |
| 266 if (!CreateSecretKeyAlgorithm( |
| 267 algorithm, key_data.byte_length(), &key_algorithm)) |
| 268 return Status::ErrorUnexpected(); |
| 269 #else |
| 270 const blink::WebCryptoAlgorithm key_algorithm = algorithm; |
| 271 #endif |
| 272 |
249 *key = blink::WebCryptoKey::create(new SymKey(key_data), | 273 *key = blink::WebCryptoKey::create(new SymKey(key_data), |
250 blink::WebCryptoKeyTypeSecret, | 274 blink::WebCryptoKeyTypeSecret, |
251 extractable, | 275 extractable, |
252 algorithm, | 276 key_algorithm, |
253 usage_mask); | 277 usage_mask); |
254 | 278 |
255 return Status::Success(); | 279 return Status::Success(); |
256 } | 280 } |
257 | 281 |
258 Status SignHmac(SymKey* key, | 282 Status SignHmac(SymKey* key, |
259 const blink::WebCryptoAlgorithm& hash, | 283 const blink::WebCryptoAlgorithm& hash, |
260 const CryptoData& data, | 284 const CryptoData& data, |
261 blink::WebArrayBuffer* buffer) { | 285 blink::WebArrayBuffer* buffer) { |
262 blink::WebArrayBuffer result; | 286 blink::WebArrayBuffer result; |
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
371 const blink::WebCryptoAlgorithm& hash, | 395 const blink::WebCryptoAlgorithm& hash, |
372 const CryptoData& signature, | 396 const CryptoData& signature, |
373 const CryptoData& data, | 397 const CryptoData& data, |
374 bool* signature_match) { | 398 bool* signature_match) { |
375 // TODO(eroman): http://crbug.com/267888 | 399 // TODO(eroman): http://crbug.com/267888 |
376 return Status::ErrorUnsupported(); | 400 return Status::ErrorUnsupported(); |
377 } | 401 } |
378 | 402 |
379 Status ImportKeySpki(const blink::WebCryptoAlgorithm& algorithm_or_null, | 403 Status ImportKeySpki(const blink::WebCryptoAlgorithm& algorithm_or_null, |
380 const CryptoData& key_data, | 404 const CryptoData& key_data, |
381 bool extractable, | |
382 blink::WebCryptoKeyUsageMask usage_mask, | 405 blink::WebCryptoKeyUsageMask usage_mask, |
383 blink::WebCryptoKey* key) { | 406 blink::WebCryptoKey* key) { |
384 // TODO(eroman): http://crbug.com/267888 | 407 // TODO(eroman): http://crbug.com/267888 |
385 return Status::ErrorUnsupported(); | 408 return Status::ErrorUnsupported(); |
386 } | 409 } |
387 | 410 |
388 Status ImportKeyPkcs8(const blink::WebCryptoAlgorithm& algorithm_or_null, | 411 Status ImportKeyPkcs8(const blink::WebCryptoAlgorithm& algorithm_or_null, |
389 const CryptoData& key_data, | 412 const CryptoData& key_data, |
390 bool extractable, | 413 bool extractable, |
391 blink::WebCryptoKeyUsageMask usage_mask, | 414 blink::WebCryptoKeyUsageMask usage_mask, |
392 blink::WebCryptoKey* key) { | 415 blink::WebCryptoKey* key) { |
393 // TODO(eroman): http://crbug.com/267888 | 416 // TODO(eroman): http://crbug.com/267888 |
394 return Status::ErrorUnsupported(); | 417 return Status::ErrorUnsupported(); |
395 } | 418 } |
396 | 419 |
397 Status ExportKeySpki(PublicKey* key, blink::WebArrayBuffer* buffer) { | 420 Status ExportKeySpki(PublicKey* key, blink::WebArrayBuffer* buffer) { |
398 // TODO(eroman): http://crbug.com/267888 | 421 // TODO(eroman): http://crbug.com/267888 |
399 return Status::ErrorUnsupported(); | 422 return Status::ErrorUnsupported(); |
400 } | 423 } |
401 | 424 |
402 } // namespace platform | 425 } // namespace platform |
403 | 426 |
404 } // namespace webcrypto | 427 } // namespace webcrypto |
405 | 428 |
406 } // namespace content | 429 } // namespace content |
OLD | NEW |