OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "base/numerics/safe_math.h" | |
6 #include "content/child/webcrypto/algorithm_implementation.h" | |
7 #include "content/child/webcrypto/crypto_data.h" | |
8 #include "content/child/webcrypto/openssl/key_openssl.h" | |
9 #include "content/child/webcrypto/openssl/util_openssl.h" | |
10 #include "content/child/webcrypto/status.h" | |
11 #include "content/child/webcrypto/webcrypto_util.h" | |
12 #include "third_party/WebKit/public/platform/WebCryptoAlgorithmParams.h" | |
13 #include "third_party/WebKit/public/platform/WebCryptoKeyAlgorithm.h" | |
14 | |
15 namespace content { | |
16 | |
17 namespace webcrypto { | |
18 | |
19 namespace { | |
20 | |
21 const blink::WebCryptoKeyUsageMask kAllKeyUsages = | |
22 blink::WebCryptoKeyUsageDeriveKey | blink::WebCryptoKeyUsageDeriveBits; | |
23 | |
24 class Pbkdf2Implementation : public AlgorithmImplementation { | |
25 public: | |
26 Pbkdf2Implementation() {} | |
27 | |
28 Status GenerateKey(const blink::WebCryptoAlgorithm& algorithm, | |
eroman
2015/01/13 00:19:20
You can remove this alltogether since the default
xun.sun
2015/01/13 19:10:41
Done.
| |
29 bool extractable, | |
30 blink::WebCryptoKeyUsageMask usages, | |
31 GenerateKeyResult* result) const override { | |
32 return Status::ErrorUnsupported(); | |
33 } | |
34 | |
35 Status VerifyKeyUsagesBeforeImportKey( | |
36 blink::WebCryptoKeyFormat format, | |
37 blink::WebCryptoKeyUsageMask usages) const override { | |
38 switch (format) { | |
39 case blink::WebCryptoKeyFormatRaw: | |
40 return CheckKeyCreationUsages(kAllKeyUsages, usages, false); | |
41 default: | |
42 return Status::ErrorUnsupportedImportKeyFormat(); | |
43 } | |
44 } | |
45 | |
46 Status ImportKeyRaw(const CryptoData& key_data, | |
47 const blink::WebCryptoAlgorithm& algorithm, | |
48 bool extractable, | |
49 blink::WebCryptoKeyUsageMask usages, | |
50 blink::WebCryptoKey* key) const override { | |
51 const blink::WebCryptoKeyAlgorithm key_algorithm = | |
52 blink::WebCryptoKeyAlgorithm::createPbkdf2(algorithm.id()); | |
53 | |
54 return CreateWebCryptoSecretKey(key_data, key_algorithm, extractable, | |
55 usages, key); | |
56 } | |
57 | |
58 Status DeriveBits(const blink::WebCryptoAlgorithm& algorithm, | |
59 const blink::WebCryptoKey& base_key, | |
60 bool has_optional_length_bits, | |
61 unsigned int optional_length_bits, | |
62 std::vector<uint8_t>* derived_bytes) const override { | |
63 if (!has_optional_length_bits || optional_length_bits % 8) | |
64 return Status::ErrorPbkdf2InvalidLength(); | |
65 | |
eroman
2015/01/13 00:30:17
You also need a call to
crypto::OpenSSLErrStackTr
xun.sun
2015/01/13 19:10:41
Done.
| |
66 const blink::WebCryptoPbkdf2Params* params = algorithm.pbkdf2Params(); | |
67 | |
68 const blink::WebCryptoAlgorithm& hash = params->hash(); | |
69 const EVP_MD* digest_algorithm = GetDigest(hash.id()); | |
70 if (!digest_algorithm) | |
71 return Status::ErrorUnsupported(); | |
72 | |
73 unsigned int keylen_bytes = optional_length_bits / 8; | |
74 derived_bytes->resize(keylen_bytes); | |
75 | |
76 const std::vector<uint8_t>& password = | |
77 SymKeyOpenSsl::Cast(base_key)->raw_key_data(); | |
78 | |
79 base::CheckedNumeric<int> password_size = password.size(); | |
80 if (!password_size.IsValid()) | |
81 return Status::ErrorDataTooLarge(); | |
82 | |
83 int result = PKCS5_PBKDF2_HMAC( | |
84 reinterpret_cast<const char*>(password.data()), | |
eroman
2015/01/13 00:19:20
I am not sure that all chromium platforms support
xun.sun
2015/01/13 19:10:41
Yeah let's see what happens. Is making a copy the
| |
85 password_size.ValueOrDie(), params->salt().data(), | |
86 params->salt().size(), params->iterations(), digest_algorithm, | |
87 keylen_bytes, derived_bytes->data()); | |
88 | |
89 if (result == 1) | |
90 return Status::Success(); | |
91 return Status::OperationError(); | |
92 } | |
93 | |
94 Status SerializeKeyForClone( | |
95 const blink::WebCryptoKey& key, | |
96 blink::WebVector<uint8_t>* key_data) const override { | |
97 key_data->assign(SymKeyOpenSsl::Cast(key)->serialized_key_data()); | |
98 return Status::Success(); | |
99 } | |
100 | |
101 Status DeserializeKeyForClone(const blink::WebCryptoKeyAlgorithm& algorithm, | |
102 blink::WebCryptoKeyType type, | |
103 bool extractable, | |
104 blink::WebCryptoKeyUsageMask usages, | |
105 const CryptoData& key_data, | |
106 blink::WebCryptoKey* key) const override { | |
107 return CreateWebCryptoSecretKey(key_data, algorithm, extractable, usages, | |
108 key); | |
109 } | |
eroman
2015/01/13 00:19:20
This algorithm also needs an implementation for Ge
xun.sun
2015/01/13 19:10:41
Implemented GetKeyLength(). How show I test it?
| |
110 }; | |
111 | |
112 } // namespace | |
113 | |
114 AlgorithmImplementation* CreatePlatformPbkdf2Implementation() { | |
115 return new Pbkdf2Implementation; | |
116 } | |
117 | |
118 } // namespace webcrypto | |
119 | |
120 } // namespace content | |
OLD | NEW |