Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(3346)

Unified Diff: chrome/common/extensions/api/networking_private/networking_private_crypto.cc

Issue 102993002: Implement Networking Private API VerifyAndEncryptCredentials method (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Sync to r255173 Created 6 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: chrome/common/extensions/api/networking_private/networking_private_crypto.cc
diff --git a/chrome/browser/extensions/api/networking_private/networking_private_crypto.cc b/chrome/common/extensions/api/networking_private/networking_private_crypto.cc
similarity index 63%
rename from chrome/browser/extensions/api/networking_private/networking_private_crypto.cc
rename to chrome/common/extensions/api/networking_private/networking_private_crypto.cc
index ddaf11cdd04f2e6f3db350ec8b60e6cf9da357e0..d2c1d75af3b5f582e27bfd688d2bb43c368bd02f 100644
--- a/chrome/browser/extensions/api/networking_private/networking_private_crypto.cc
+++ b/chrome/common/extensions/api/networking_private/networking_private_crypto.cc
@@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
-#include "chrome/browser/extensions/api/networking_private/networking_private_crypto.h"
+#include "chrome/common/extensions/api/networking_private/networking_private_crypto.h"
mef 2014/03/13 20:22:29 NetworkingPrivateCrypto was originally reviewed by
#include <cert.h>
#include <cryptohi.h>
@@ -24,27 +24,29 @@
#include "net/cert/x509_certificate.h"
const unsigned char kTrustedCAPublicKeyDER[] = {
- 0x30, 0x82, 0x01, 0x0a, 0x02, 0x82, 0x01, 0x01, 0x00, 0xbc, 0x22, 0x80, 0xbd,
- 0x80, 0xf6, 0x3a, 0x21, 0x00, 0x3b, 0xae, 0x76, 0x5e, 0x35, 0x7f, 0x3d, 0xc3,
- 0x64, 0x5c, 0x55, 0x94, 0x86, 0x34, 0x2f, 0x05, 0x87, 0x28, 0x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, 0x50, 0x06, 0x0b, 0xe0, 0x8a, 0xd4, 0xce,
+ 0xe1, 0x6a, 0x55, 0x1f, 0x8b, 0x17, 0x00, 0xe6, 0x69, 0xa3, 0x27, 0xe6,
+ 0x08, 0x25, 0x69, 0x3c, 0x12, 0x9d, 0x8d, 0x05, 0x2c, 0xd6, 0x2e, 0xa2,
+ 0x31, 0xde, 0xb4, 0x52, 0x50, 0xd6, 0x20, 0x49, 0xde, 0x71, 0xa0, 0xf9,
+ 0xad, 0x20, 0x40, 0x12, 0xf1, 0xdd, 0x25, 0xeb, 0xd5, 0xe6, 0xb8, 0x36,
+ 0xf4, 0xd6, 0x8f, 0x7f, 0xca, 0x43, 0xdc, 0xd7, 0x10, 0x5b, 0xe6, 0x3f,
+ 0x51, 0x8a, 0x85, 0xb3, 0xf3, 0xff, 0xf6, 0x03, 0x2d, 0xcb, 0x23, 0x4f,
+ 0x9c, 0xad, 0x18, 0xe7, 0x93, 0x05, 0x8c, 0xac, 0x52, 0x9a, 0xf7, 0x4c,
+ 0xe9, 0x99, 0x7a, 0xbe, 0x6e, 0x7e, 0x4d, 0x0a, 0xe3, 0xc6, 0x1c, 0xa9,
+ 0x93, 0xfa, 0x3a, 0xa5, 0x91, 0x5d, 0x1c, 0xbd, 0x66, 0xeb, 0xcc, 0x60,
+ 0xdc, 0x86, 0x74, 0xca, 0xcf, 0xf8, 0x92, 0x1c, 0x98, 0x7d, 0x57, 0xfa,
+ 0x61, 0x47, 0x9e, 0xab, 0x80, 0xb7, 0xe4, 0x48, 0x80, 0x2a, 0x92, 0xc5,
+ 0x1b, 0x02, 0x03, 0x01, 0x00, 0x01};
namespace {
@@ -53,7 +55,7 @@ namespace {
// |der_output|.
bool GetDERFromPEM(const std::string& pem_data,
const std::string& pem_type,
- std::string* der_output) {
+ std::vector<uint8>* der_output) {
std::vector<std::string> headers;
headers.push_back(pem_type);
net::PEMTokenizer pem_tok(pem_data, headers);
@@ -61,7 +63,7 @@ bool GetDERFromPEM(const std::string& pem_data,
return false;
}
- *der_output = pem_tok.data();
+ der_output->assign(pem_tok.data().begin(), pem_tok.data().end());
return true;
}
@@ -78,22 +80,21 @@ bool NetworkingPrivateCrypto::VerifyCredentials(
const std::string& connected_mac) {
crypto::EnsureNSSInit();
- std::string cert_data;
+ std::vector<uint8> cert_data;
if (!GetDERFromPEM(certificate, "CERTIFICATE", &cert_data)) {
LOG(ERROR) << "Failed to parse certificate.";
return false;
}
SECItem der_cert;
der_cert.type = siDERCertBuffer;
- der_cert.data = reinterpret_cast<unsigned char*>(
- const_cast<char*>(cert_data.c_str()));
- der_cert.len = cert_data.length();
+ der_cert.data = cert_data.data();
+ der_cert.len = cert_data.size();
// Parse into a certificate structure.
typedef scoped_ptr<
CERTCertificate,
crypto::NSSDestroyer<CERTCertificate, CERT_DestroyCertificate> >
- ScopedCERTCertificate;
+ ScopedCERTCertificate;
ScopedCERTCertificate cert(CERT_NewTempCertificate(
CERT_GetDefaultCertDB(), &der_cert, NULL, PR_FALSE, PR_TRUE));
if (!cert.get()) {
@@ -104,8 +105,8 @@ bool NetworkingPrivateCrypto::VerifyCredentials(
// Check that the certificate is signed by trusted CA.
SECItem trusted_ca_key_der_item;
trusted_ca_key_der_item.type = siDERCertBuffer;
- trusted_ca_key_der_item.data = const_cast<unsigned char*>(
- kTrustedCAPublicKeyDER),
+ trusted_ca_key_der_item.data =
+ const_cast<unsigned char*>(kTrustedCAPublicKeyDER),
trusted_ca_key_der_item.len = sizeof(kTrustedCAPublicKeyDER);
crypto::ScopedSECKEYPublicKey ca_public_key(
SECKEY_ImportDERPublicKey(&trusted_ca_key_der_item, CKK_RSA));
@@ -142,13 +143,18 @@ bool NetworkingPrivateCrypto::VerifyCredentials(
}
SECItem signature_item;
signature_item.type = siBuffer;
- signature_item.data = reinterpret_cast<unsigned char*>(
- const_cast<char*>(signature.c_str()));
+ signature_item.data =
+ reinterpret_cast<unsigned char*>(const_cast<char*>(signature.c_str()));
signature_item.len = static_cast<unsigned int>(signature.size());
- verified = VFY_VerifyDataDirect(reinterpret_cast<unsigned char*>(
- const_cast<char*>(data.c_str())), data.size(),
- public_key.get(), &signature_item, SEC_OID_PKCS1_RSA_ENCRYPTION,
- SEC_OID_SHA1, NULL, NULL);
+ verified = VFY_VerifyDataDirect(
+ reinterpret_cast<unsigned char*>(const_cast<char*>(data.c_str())),
+ data.size(),
+ public_key.get(),
+ &signature_item,
+ SEC_OID_PKCS1_RSA_ENCRYPTION,
+ SEC_OID_SHA1,
+ NULL,
+ NULL);
if (verified != SECSuccess) {
LOG(ERROR) << "Signed blobs did not match.";
return false;
@@ -156,19 +162,19 @@ bool NetworkingPrivateCrypto::VerifyCredentials(
return true;
}
-bool NetworkingPrivateCrypto::EncryptByteString(const std::string& pub_key_der,
- const std::string& data,
- std::string* encrypted_output) {
+bool NetworkingPrivateCrypto::EncryptByteString(
+ const std::vector<uint8>& pub_key_der,
+ const std::string& data,
+ std::vector<uint8>* encrypted_output) {
crypto::EnsureNSSInit();
SECItem pub_key_der_item;
pub_key_der_item.type = siDERCertBuffer;
- pub_key_der_item.data = reinterpret_cast<unsigned char*>(
- const_cast<char*>(pub_key_der.c_str()));
+ pub_key_der_item.data = const_cast<unsigned char*>(pub_key_der.data());
pub_key_der_item.len = pub_key_der.size();
- crypto::ScopedSECKEYPublicKey public_key(SECKEY_ImportDERPublicKey(
- &pub_key_der_item, CKK_RSA));
+ crypto::ScopedSECKEYPublicKey public_key(
+ SECKEY_ImportDERPublicKey(&pub_key_der_item, CKK_RSA));
if (!public_key.get()) {
LOG(ERROR) << "Failed to parse public key.";
return false;
@@ -193,24 +199,22 @@ bool NetworkingPrivateCrypto::EncryptByteString(const std::string& pub_key_der,
LOG(ERROR) << "Error during encryption.";
return false;
}
- encrypted_output->assign(reinterpret_cast<char*>(rsa_output.get()),
- encrypted_length);
+ encrypted_output->assign(rsa_output.get(),
+ rsa_output.get() + encrypted_length);
return true;
}
bool NetworkingPrivateCrypto::DecryptByteString(
const std::string& private_key_pem,
- const std::string& encrypted_data,
+ const std::vector<uint8>& encrypted_data,
std::string* decrypted_output) {
crypto::EnsureNSSInit();
- std::string private_key_der;
- if (!GetDERFromPEM(private_key_pem, "PRIVATE KEY", &private_key_der)) {
+ std::vector<uint8> private_key_data;
+ if (!GetDERFromPEM(private_key_pem, "PRIVATE KEY", &private_key_data)) {
LOG(ERROR) << "Failed to parse private key PEM.";
return false;
}
- std::vector<uint8> private_key_data(private_key_der.begin(),
- private_key_der.end());
scoped_ptr<crypto::RSAPrivateKey> private_key(
crypto::RSAPrivateKey::CreateFromPrivateKeyInfo(private_key_data));
if (!private_key || !private_key->public_key()) {
@@ -226,9 +230,8 @@ bool NetworkingPrivateCrypto::DecryptByteString(
rsa_output.get(),
&output_length,
encrypted_length,
- reinterpret_cast<unsigned char*>(
- const_cast<char*>(encrypted_data.data())),
- encrypted_data.length());
+ const_cast<unsigned char*>(encrypted_data.data()),
+ encrypted_data.size());
if (decrypted != SECSuccess) {
LOG(ERROR) << "Error during decryption.";
return false;
@@ -237,4 +240,3 @@ bool NetworkingPrivateCrypto::DecryptByteString(
output_length);
return true;
}
-

Powered by Google App Engine
This is Rietveld 408576698