Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include <openssl/err.h> | |
| 6 #include <openssl/hkdf.h> | |
| 7 | |
| 8 // TODO: figure out which headers belong here and remove ones that were | |
|
eroman
2014/12/23 23:13:36
(1) TODO(nharper)
(2) Address the TODO now :)
nharper
2015/01/06 22:53:42
Done.
| |
| 9 // just copied over from hmac | |
| 10 #include "base/logging.h" | |
| 11 #include "base/stl_util.h" | |
| 12 #include "content/child/webcrypto/algorithm_implementation.h" | |
| 13 #include "content/child/webcrypto/crypto_data.h" | |
| 14 #include "content/child/webcrypto/jwk.h" | |
| 15 #include "content/child/webcrypto/openssl/key_openssl.h" | |
| 16 #include "content/child/webcrypto/openssl/sym_key_openssl.h" | |
| 17 #include "content/child/webcrypto/openssl/util_openssl.h" | |
| 18 #include "content/child/webcrypto/status.h" | |
| 19 #include "content/child/webcrypto/webcrypto_util.h" | |
| 20 #include "crypto/openssl_util.h" | |
| 21 #include "crypto/secure_util.h" | |
| 22 #include "third_party/WebKit/public/platform/WebCryptoAlgorithmParams.h" | |
| 23 #include "third_party/WebKit/public/platform/WebCryptoKeyAlgorithm.h" | |
| 24 | |
| 25 namespace content { | |
| 26 | |
| 27 namespace webcrypto { | |
| 28 | |
| 29 namespace { | |
| 30 | |
| 31 const blink::WebCryptoKeyUsageMask kValidUsages = | |
| 32 blink::WebCryptoKeyUsageDeriveKey | blink::WebCryptoKeyUsageDeriveBits; | |
| 33 | |
| 34 class HkdfImplementation : public AlgorithmImplementation { | |
| 35 public: | |
| 36 HkdfImplementation() {} | |
| 37 | |
| 38 // This is doing a check that is only valid for raw format, but since | |
| 39 // ImportKeyRaw is the the only ImportKey implemented here, there's no | |
| 40 // need to check the format here. | |
|
eroman
2014/12/23 23:13:36
You should do the check, otherwise this is inconsi
nharper
2015/01/06 22:53:42
I added a check here for this. It turns out in bot
| |
| 41 Status VerifyKeyUsagesBeforeImportKey( | |
| 42 blink::WebCryptoKeyFormat format, | |
| 43 blink::WebCryptoKeyUsageMask usages) const override { | |
| 44 return CheckKeyCreationUsages(kValidUsages, usages, false); | |
| 45 } | |
| 46 | |
|
eroman
2014/12/23 23:13:36
This also needs support for cloning/de-cloning.
nharper
2015/01/06 22:53:42
Done.
| |
| 47 Status ImportKeyRaw(const CryptoData& key_data, | |
| 48 const blink::WebCryptoAlgorithm& algorithm, | |
| 49 bool extractable, | |
| 50 blink::WebCryptoKeyUsageMask usages, | |
| 51 blink::WebCryptoKey* key) const override { | |
| 52 *key = blink::WebCryptoKey::create( | |
| 53 new SymKeyOpenSsl(key_data), blink::WebCryptoKeyTypeSecret, extractable, | |
| 54 blink::WebCryptoKeyAlgorithm::createHkdf(), usages); | |
| 55 return Status::Success(); | |
| 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 // When called by crypto.subtle.deriveBits(), length will already be present | |
| 64 // and converted to a number. It won't be called as part of deriveKey, | |
|
eroman
2014/12/23 23:13:36
I don't understand this comment. HKDF does support
nharper
2015/01/06 22:53:42
Done.
| |
| 65 // because that's not supported for this algorithm. Therefore, it's | |
| 66 // unexpected for this method to ever be called without a length. | |
| 67 if (!has_optional_length_bits) | |
| 68 return Status::ErrorUnexpected(); | |
| 69 | |
| 70 // "If the hash member of normalizedAlgorithm does not describe a recognized | |
| 71 // algorithm that supports the digest operation, then throw a | |
| 72 // NotSupportedError" | |
| 73 const EVP_MD* digest_algorithm = | |
| 74 GetDigest(algorithm.hkdfParams()->hash().id()); | |
|
eroman
2014/12/23 23:13:36
It is guaranteed to already be a digest, since the
nharper
2015/01/06 22:53:42
This same check is done in the HMAC code - any obj
eroman
2015/01/07 00:23:42
Nope. Although we should be consistent about when
nharper
2015/01/08 01:31:23
It looks like sha_openssl.cc is the only one (code
| |
| 75 if (!digest_algorithm) | |
| 76 return Status::ErrorUnsupported(); | |
| 77 | |
| 78 // Size output to fit length | |
| 79 unsigned int derived_bytes_len = (optional_length_bits + 7) / 8; | |
|
eroman
2014/12/23 23:13:36
Use NumBitsToBytes() instead.
It is more readable
nharper
2015/01/06 22:53:42
Done.
| |
| 80 derived_bytes->resize(derived_bytes_len); | |
| 81 | |
| 82 // Algorithm dispatch checks that the algorithm in |base_key| matches | |
| 83 // |algorithm|. | |
| 84 std::vector<uint8_t> raw_key = | |
|
eroman
2014/12/23 23:13:36
Don't make a copy of the key material.
nharper
2015/01/06 22:53:42
Done.
| |
| 85 SymKeyOpenSsl::Cast(base_key)->raw_key_data(); | |
| 86 blink::WebVector<unsigned char> salt = algorithm.hkdfParams()->salt(); | |
|
eroman
2014/12/23 23:13:36
Don't make copies here.
nharper
2015/01/06 22:53:42
Done.
| |
| 87 blink::WebVector<unsigned char> info = algorithm.hkdfParams()->info(); | |
| 88 if (!HKDF(derived_bytes->data(), derived_bytes_len, digest_algorithm, | |
| 89 raw_key.data(), raw_key.size(), salt.data(), salt.size(), | |
| 90 info.data(), info.size())) { | |
| 91 uint32_t error = ERR_get_error(); | |
| 92 if (ERR_GET_LIB(error) == ERR_LIB_HKDF && | |
| 93 ERR_GET_REASON(error) == HKDF_R_OUTPUT_TOO_LARGE) { | |
| 94 return Status::ErrorHkdfLengthTooLong(); | |
| 95 } else { | |
|
eroman
2014/12/23 23:13:36
nit: omit "else" when short-circuiting.
nharper
2015/01/06 22:53:42
Done. Also removed the curly braces from the one-l
| |
| 96 return Status::OperationError(); | |
| 97 } | |
| 98 } | |
| 99 return Status::Success(); | |
| 100 } | |
| 101 }; | |
| 102 | |
| 103 } // namespace | |
| 104 | |
| 105 AlgorithmImplementation* CreatePlatformHkdfImplementation() { | |
| 106 return new HkdfImplementation; | |
| 107 } | |
| 108 | |
| 109 } // namespace webcrypto | |
| 110 | |
| 111 } // namespace content | |
| OLD | NEW |