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/child/webcrypto/openssl/util_openssl.h" | 5 #include "content/child/webcrypto/openssl/util_openssl.h" |
6 | 6 |
7 #include <openssl/evp.h> | 7 #include <openssl/evp.h> |
8 #include <openssl/pkcs12.h> | 8 #include <openssl/pkcs12.h> |
9 #include <openssl/rand.h> | 9 #include <openssl/rand.h> |
10 | 10 |
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
94 EVP_AEAD_CTX ctx; | 94 EVP_AEAD_CTX ctx; |
95 | 95 |
96 if (!aead_alg) | 96 if (!aead_alg) |
97 return Status::ErrorUnexpected(); | 97 return Status::ErrorUnexpected(); |
98 | 98 |
99 if (!EVP_AEAD_CTX_init(&ctx, aead_alg, vector_as_array(&raw_key), | 99 if (!EVP_AEAD_CTX_init(&ctx, aead_alg, vector_as_array(&raw_key), |
100 raw_key.size(), tag_length_bytes, NULL)) { | 100 raw_key.size(), tag_length_bytes, NULL)) { |
101 return Status::OperationError(); | 101 return Status::OperationError(); |
102 } | 102 } |
103 | 103 |
104 crypto::ScopedOpenSSL<EVP_AEAD_CTX, EVP_AEAD_CTX_cleanup>::Type ctx_cleanup( | 104 crypto::ScopedOpenSSL<EVP_AEAD_CTX, EVP_AEAD_CTX_cleanup> ctx_cleanup(&ctx); |
105 &ctx); | |
106 | 105 |
107 size_t len; | 106 size_t len; |
108 int ok; | 107 int ok; |
109 | 108 |
110 if (mode == DECRYPT) { | 109 if (mode == DECRYPT) { |
111 if (data.byte_length() < tag_length_bytes) | 110 if (data.byte_length() < tag_length_bytes) |
112 return Status::ErrorDataTooSmall(); | 111 return Status::ErrorDataTooSmall(); |
113 | 112 |
114 buffer->resize(data.byte_length() - tag_length_bytes); | 113 buffer->resize(data.byte_length() - tag_length_bytes); |
115 | 114 |
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
219 | 218 |
220 return Status::Success(); | 219 return Status::Success(); |
221 } | 220 } |
222 | 221 |
223 Status ImportUnverifiedPkeyFromPkcs8(const CryptoData& key_data, | 222 Status ImportUnverifiedPkeyFromPkcs8(const CryptoData& key_data, |
224 int expected_pkey_id, | 223 int expected_pkey_id, |
225 crypto::ScopedEVP_PKEY* pkey) { | 224 crypto::ScopedEVP_PKEY* pkey) { |
226 crypto::OpenSSLErrStackTracer err_tracer(FROM_HERE); | 225 crypto::OpenSSLErrStackTracer err_tracer(FROM_HERE); |
227 | 226 |
228 const uint8_t* ptr = key_data.bytes(); | 227 const uint8_t* ptr = key_data.bytes(); |
229 crypto::ScopedOpenSSL<PKCS8_PRIV_KEY_INFO, PKCS8_PRIV_KEY_INFO_free>::Type | 228 crypto::ScopedOpenSSL<PKCS8_PRIV_KEY_INFO, PKCS8_PRIV_KEY_INFO_free> p8inf( |
230 p8inf(d2i_PKCS8_PRIV_KEY_INFO(nullptr, &ptr, key_data.byte_length())); | 229 d2i_PKCS8_PRIV_KEY_INFO(nullptr, &ptr, key_data.byte_length())); |
231 if (!p8inf.get() || ptr != key_data.bytes() + key_data.byte_length()) | 230 if (!p8inf.get() || ptr != key_data.bytes() + key_data.byte_length()) |
232 return Status::DataError(); | 231 return Status::DataError(); |
233 | 232 |
234 pkey->reset(EVP_PKCS82PKEY(p8inf.get())); | 233 pkey->reset(EVP_PKCS82PKEY(p8inf.get())); |
235 if (!pkey->get()) | 234 if (!pkey->get()) |
236 return Status::DataError(); | 235 return Status::DataError(); |
237 | 236 |
238 if (EVP_PKEY_id(pkey->get()) != expected_pkey_id) | 237 if (EVP_PKEY_id(pkey->get()) != expected_pkey_id) |
239 return Status::DataError(); // Data did not define expected key type. | 238 return Status::DataError(); // Data did not define expected key type. |
240 | 239 |
241 return Status::Success(); | 240 return Status::Success(); |
242 } | 241 } |
243 | 242 |
244 BIGNUM* CreateBIGNUM(const std::string& n) { | 243 BIGNUM* CreateBIGNUM(const std::string& n) { |
245 return BN_bin2bn(reinterpret_cast<const uint8_t*>(n.data()), n.size(), NULL); | 244 return BN_bin2bn(reinterpret_cast<const uint8_t*>(n.data()), n.size(), NULL); |
246 } | 245 } |
247 | 246 |
248 std::vector<uint8_t> BIGNUMToVector(const BIGNUM* n) { | 247 std::vector<uint8_t> BIGNUMToVector(const BIGNUM* n) { |
249 std::vector<uint8_t> v(BN_num_bytes(n)); | 248 std::vector<uint8_t> v(BN_num_bytes(n)); |
250 BN_bn2bin(n, vector_as_array(&v)); | 249 BN_bn2bin(n, vector_as_array(&v)); |
251 return v; | 250 return v; |
252 } | 251 } |
253 | 252 |
254 } // namespace webcrypto | 253 } // namespace webcrypto |
255 | 254 |
256 } // namespace content | 255 } // namespace content |
OLD | NEW |