| 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 "chrome/common/extensions/api/networking_private/networking_private_cry
pto.h" | 5 #include "chrome/common/extensions/api/networking_private/networking_private_cry
pto.h" |
| 6 | 6 |
| 7 #include <openssl/digest.h> | 7 #include <openssl/digest.h> |
| 8 #include <openssl/evp.h> | 8 #include <openssl/evp.h> |
| 9 #include <openssl/rsa.h> | 9 #include <openssl/rsa.h> |
| 10 #include <openssl/x509.h> | 10 #include <openssl/x509.h> |
| 11 | 11 |
| 12 #include "base/logging.h" | 12 #include "base/logging.h" |
| 13 #include "base/memory/scoped_ptr.h" | 13 #include "base/memory/scoped_ptr.h" |
| 14 #include "base/stl_util.h" | |
| 15 #include "base/strings/string_util.h" | 14 #include "base/strings/string_util.h" |
| 16 #include "crypto/openssl_util.h" | 15 #include "crypto/openssl_util.h" |
| 17 #include "crypto/rsa_private_key.h" | 16 #include "crypto/rsa_private_key.h" |
| 18 #include "crypto/scoped_openssl_types.h" | 17 #include "crypto/scoped_openssl_types.h" |
| 19 #include "extensions/common/cast/cast_cert_validator.h" | 18 #include "extensions/common/cast/cast_cert_validator.h" |
| 20 #include "net/cert/pem_tokenizer.h" | 19 #include "net/cert/pem_tokenizer.h" |
| 21 | 20 |
| 22 namespace { | 21 namespace { |
| 23 | 22 |
| 24 namespace cast_crypto = ::extensions::api::cast_crypto; | 23 namespace cast_crypto = ::extensions::api::cast_crypto; |
| (...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 109 } | 108 } |
| 110 return true; | 109 return true; |
| 111 } | 110 } |
| 112 | 111 |
| 113 bool EncryptByteString(const std::vector<uint8_t>& pub_key_der, | 112 bool EncryptByteString(const std::vector<uint8_t>& pub_key_der, |
| 114 const std::string& data, | 113 const std::string& data, |
| 115 std::vector<uint8_t>* encrypted_output) { | 114 std::vector<uint8_t>* encrypted_output) { |
| 116 crypto::EnsureOpenSSLInit(); | 115 crypto::EnsureOpenSSLInit(); |
| 117 crypto::OpenSSLErrStackTracer err_tracer(FROM_HERE); | 116 crypto::OpenSSLErrStackTracer err_tracer(FROM_HERE); |
| 118 | 117 |
| 119 crypto::ScopedRSA rsa(RSA_public_key_from_bytes(vector_as_array(&pub_key_der), | 118 crypto::ScopedRSA rsa( |
| 120 pub_key_der.size())); | 119 RSA_public_key_from_bytes(pub_key_der.data(), pub_key_der.size())); |
| 121 if (!rsa || RSA_size(rsa.get()) == 0) { | 120 if (!rsa || RSA_size(rsa.get()) == 0) { |
| 122 LOG(ERROR) << "Failed to parse public key"; | 121 LOG(ERROR) << "Failed to parse public key"; |
| 123 return false; | 122 return false; |
| 124 } | 123 } |
| 125 | 124 |
| 126 encrypted_output->resize(RSA_size(rsa.get())); | 125 encrypted_output->resize(RSA_size(rsa.get())); |
| 127 int encrypted_length = RSA_public_encrypt( | 126 int encrypted_length = RSA_public_encrypt( |
| 128 data.size(), reinterpret_cast<const uint8_t*>(data.data()), | 127 data.size(), reinterpret_cast<const uint8_t*>(data.data()), |
| 129 vector_as_array(encrypted_output), rsa.get(), RSA_PKCS1_PADDING); | 128 encrypted_output->data(), rsa.get(), RSA_PKCS1_PADDING); |
| 130 if (encrypted_length < 0) { | 129 if (encrypted_length < 0) { |
| 131 LOG(ERROR) << "Error during decryption"; | 130 LOG(ERROR) << "Error during decryption"; |
| 132 return false; | 131 return false; |
| 133 } | 132 } |
| 134 encrypted_output->resize(encrypted_length); | 133 encrypted_output->resize(encrypted_length); |
| 135 return true; | 134 return true; |
| 136 } | 135 } |
| 137 | 136 |
| 138 bool DecryptByteString(const std::string& private_key_pem, | 137 bool DecryptByteString(const std::string& private_key_pem, |
| 139 const std::vector<uint8_t>& encrypted_data, | 138 const std::vector<uint8_t>& encrypted_data, |
| (...skipping 26 matching lines...) Expand all Loading... |
| 166 rsa.get(), RSA_PKCS1_PADDING); | 165 rsa.get(), RSA_PKCS1_PADDING); |
| 167 if (output_length < 0) { | 166 if (output_length < 0) { |
| 168 LOG(ERROR) << "Error during decryption."; | 167 LOG(ERROR) << "Error during decryption."; |
| 169 return false; | 168 return false; |
| 170 } | 169 } |
| 171 decrypted_output->resize(output_length); | 170 decrypted_output->resize(output_length); |
| 172 return true; | 171 return true; |
| 173 } | 172 } |
| 174 | 173 |
| 175 } // namespace networking_private_crypto | 174 } // namespace networking_private_crypto |
| OLD | NEW |