Chromium Code Reviews| 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 "components/webcrypto/algorithms/secret_key_util.h" | |
| 6 | |
| 7 #include <openssl/rand.h> | |
| 8 | |
| 9 #include "components/webcrypto/crypto_data.h" | |
| 10 #include "components/webcrypto/generate_key_result.h" | |
| 11 #include "components/webcrypto/jwk.h" | |
| 12 #include "components/webcrypto/key.h" | |
| 13 #include "components/webcrypto/status.h" | |
| 14 #include "components/webcrypto/webcrypto_util.h" | |
| 15 #include "crypto/openssl_util.h" | |
| 16 | |
| 17 namespace webcrypto { | |
| 18 | |
| 19 Status GenerateWebCryptoSecretKey(const blink::WebCryptoKeyAlgorithm& algorithm, | |
| 20 bool extractable, | |
| 21 blink::WebCryptoKeyUsageMask usages, | |
| 22 unsigned int keylen_bits, | |
| 23 GenerateKeyResult* result) { | |
| 24 crypto::OpenSSLErrStackTracer err_tracer(FROM_HERE); | |
| 25 | |
| 26 unsigned int keylen_bytes = NumBitsToBytes(keylen_bits); | |
| 27 std::vector<unsigned char> random_bytes(keylen_bytes, 0); | |
| 28 | |
| 29 if (keylen_bytes > 0) { | |
|
davidben
2015/09/18 22:16:00
Optional: RAND_bytes does reasonable things if you
eroman
2015/09/18 23:13:23
This check predated the use of vector_as_array(),
| |
| 30 if (!(RAND_bytes(&random_bytes[0], keylen_bytes))) | |
|
davidben
2015/09/18 22:15:59
&random_bytes[0] -> vector_as_array?
davidben
2015/09/18 22:15:59
Style: unnecessary parens around RAND_byte?
eroman
2015/09/18 23:13:23
Done.
eroman
2015/09/18 23:13:23
Done.
| |
| 31 return Status::OperationError(); | |
| 32 TruncateToBitLength(keylen_bits, &random_bytes); | |
| 33 } | |
| 34 | |
| 35 result->AssignSecretKey(blink::WebCryptoKey::create( | |
| 36 CreateSymmetricKeyHandle(CryptoData(random_bytes)), | |
| 37 blink::WebCryptoKeyTypeSecret, extractable, algorithm, usages)); | |
| 38 | |
| 39 return Status::Success(); | |
| 40 } | |
| 41 | |
| 42 Status CreateWebCryptoSecretKey(const CryptoData& key_data, | |
| 43 const blink::WebCryptoKeyAlgorithm& algorithm, | |
| 44 bool extractable, | |
| 45 blink::WebCryptoKeyUsageMask usages, | |
| 46 blink::WebCryptoKey* key) { | |
| 47 *key = blink::WebCryptoKey::create(CreateSymmetricKeyHandle(key_data), | |
| 48 blink::WebCryptoKeyTypeSecret, extractable, | |
| 49 algorithm, usages); | |
| 50 return Status::Success(); | |
| 51 } | |
| 52 | |
| 53 void WriteSecretKeyJwk(const CryptoData& raw_key_data, | |
| 54 const std::string& algorithm, | |
| 55 bool extractable, | |
| 56 blink::WebCryptoKeyUsageMask usages, | |
| 57 std::vector<uint8_t>* jwk_key_data) { | |
| 58 JwkWriter writer(algorithm, extractable, usages, "oct"); | |
| 59 writer.SetBytes("k", raw_key_data); | |
| 60 writer.ToJson(jwk_key_data); | |
| 61 } | |
| 62 | |
| 63 Status ReadSecretKeyNoExpectedAlgJwk( | |
| 64 const CryptoData& key_data, | |
| 65 bool expected_extractable, | |
| 66 blink::WebCryptoKeyUsageMask expected_usages, | |
| 67 std::vector<uint8_t>* raw_key_data, | |
| 68 JwkReader* jwk) { | |
| 69 Status status = jwk->Init(key_data, expected_extractable, expected_usages, | |
| 70 "oct", std::string()); | |
| 71 if (status.IsError()) | |
| 72 return status; | |
| 73 | |
| 74 std::string jwk_k_value; | |
| 75 status = jwk->GetBytes("k", &jwk_k_value); | |
| 76 if (status.IsError()) | |
| 77 return status; | |
| 78 raw_key_data->assign(jwk_k_value.begin(), jwk_k_value.end()); | |
| 79 | |
| 80 return Status::Success(); | |
| 81 } | |
| 82 | |
| 83 } // namespace webcrypto | |
| OLD | NEW |