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

Side by Side 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: EnsureInitialized in WiFiService SetEventObservers. Created 6 years, 9 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 unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 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/browser/extensions/api/networking_private/networking_private_cr ypto.h" 5 #include "chrome/common/extensions/api/networking_private/networking_private_cry pto.h"
6 6
7 #include <cert.h> 7 #include <cert.h>
8 #include <cryptohi.h> 8 #include <cryptohi.h>
9 #include <keyhi.h> 9 #include <keyhi.h>
10 #include <keythi.h> 10 #include <keythi.h>
11 #include <pk11pub.h> 11 #include <pk11pub.h>
12 #include <sechash.h> 12 #include <sechash.h>
13 #include <secport.h> 13 #include <secport.h>
14 14
15 #include "base/base64.h" 15 #include "base/base64.h"
16 #include "base/memory/scoped_ptr.h" 16 #include "base/memory/scoped_ptr.h"
17 #include "base/strings/string_number_conversions.h" 17 #include "base/strings/string_number_conversions.h"
18 #include "base/strings/string_util.h" 18 #include "base/strings/string_util.h"
19 #include "base/strings/stringprintf.h" 19 #include "base/strings/stringprintf.h"
20 #include "crypto/nss_util.h" 20 #include "crypto/nss_util.h"
21 #include "crypto/rsa_private_key.h" 21 #include "crypto/rsa_private_key.h"
22 #include "crypto/scoped_nss_types.h" 22 #include "crypto/scoped_nss_types.h"
23 #include "net/cert/pem_tokenizer.h" 23 #include "net/cert/pem_tokenizer.h"
24 #include "net/cert/x509_certificate.h" 24 #include "net/cert/x509_certificate.h"
25 25
26 const unsigned char kTrustedCAPublicKeyDER[] = { 26 const unsigned char kTrustedCAPublicKeyDER[] = {
27 0x30, 0x82, 0x01, 0x0a, 0x02, 0x82, 0x01, 0x01, 0x00, 0xbc, 0x22, 0x80, 0xbd, 27 0x30, 0x82, 0x01, 0x0a, 0x02, 0x82, 0x01, 0x01, 0x00, 0xbc, 0x22, 0x80,
28 0x80, 0xf6, 0x3a, 0x21, 0x00, 0x3b, 0xae, 0x76, 0x5e, 0x35, 0x7f, 0x3d, 0xc3, 28 0xbd, 0x80, 0xf6, 0x3a, 0x21, 0x00, 0x3b, 0xae, 0x76, 0x5e, 0x35, 0x7f,
29 0x64, 0x5c, 0x55, 0x94, 0x86, 0x34, 0x2f, 0x05, 0x87, 0x28, 0xcd, 0xf7, 0x69, 29 0x3d, 0xc3, 0x64, 0x5c, 0x55, 0x94, 0x86, 0x34, 0x2f, 0x05, 0x87, 0x28,
30 0x8c, 0x17, 0xb3, 0x50, 0xa7, 0xb8, 0x82, 0xfa, 0xdf, 0xc7, 0x43, 0x2d, 0xd6, 30 0xcd, 0xf7, 0x69, 0x8c, 0x17, 0xb3, 0x50, 0xa7, 0xb8, 0x82, 0xfa, 0xdf,
31 0x7e, 0xab, 0xa0, 0x6f, 0xb7, 0x13, 0x72, 0x80, 0xa4, 0x47, 0x15, 0xc1, 0x20, 31 0xc7, 0x43, 0x2d, 0xd6, 0x7e, 0xab, 0xa0, 0x6f, 0xb7, 0x13, 0x72, 0x80,
32 0x99, 0x50, 0xcd, 0xec, 0x14, 0x62, 0x09, 0x5b, 0xa4, 0x98, 0xcd, 0xd2, 0x41, 32 0xa4, 0x47, 0x15, 0xc1, 0x20, 0x99, 0x50, 0xcd, 0xec, 0x14, 0x62, 0x09,
33 0xb6, 0x36, 0x4e, 0xff, 0xe8, 0x2e, 0x32, 0x30, 0x4a, 0x81, 0xa8, 0x42, 0xa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, 0x57, 0xfa,
48 0x61, 0x47, 0x9e, 0xab, 0x80, 0xb7, 0xe4, 0x48, 0x80, 0x2a, 0x92, 0xc5,
49 0x1b, 0x02, 0x03, 0x01, 0x00, 0x01};
48 50
49 namespace { 51 namespace {
50 52
51 // Parses |pem_data| for a PEM block of |pem_type|. 53 // Parses |pem_data| for a PEM block of |pem_type|.
52 // Returns true if a |pem_type| block is found, storing the decoded result in 54 // Returns true if a |pem_type| block is found, storing the decoded result in
53 // |der_output|. 55 // |der_output|.
54 bool GetDERFromPEM(const std::string& pem_data, 56 bool GetDERFromPEM(const std::string& pem_data,
55 const std::string& pem_type, 57 const std::string& pem_type,
56 std::string* der_output) { 58 std::vector<uint8>* der_output) {
57 std::vector<std::string> headers; 59 std::vector<std::string> headers;
58 headers.push_back(pem_type); 60 headers.push_back(pem_type);
59 net::PEMTokenizer pem_tok(pem_data, headers); 61 net::PEMTokenizer pem_tok(pem_data, headers);
60 if (!pem_tok.GetNext()) { 62 if (!pem_tok.GetNext()) {
61 return false; 63 return false;
62 } 64 }
63 65
64 *der_output = pem_tok.data(); 66 der_output->assign(pem_tok.data().begin(), pem_tok.data().end());
65 return true; 67 return true;
66 } 68 }
67 69
68 } // namespace 70 } // namespace
69 71
70 NetworkingPrivateCrypto::NetworkingPrivateCrypto() {} 72 NetworkingPrivateCrypto::NetworkingPrivateCrypto() {}
71 73
72 NetworkingPrivateCrypto::~NetworkingPrivateCrypto() {} 74 NetworkingPrivateCrypto::~NetworkingPrivateCrypto() {}
73 75
74 bool NetworkingPrivateCrypto::VerifyCredentials( 76 bool NetworkingPrivateCrypto::VerifyCredentials(
75 const std::string& certificate, 77 const std::string& certificate,
76 const std::string& signature, 78 const std::string& signature,
77 const std::string& data, 79 const std::string& data,
78 const std::string& connected_mac) { 80 const std::string& connected_mac) {
79 crypto::EnsureNSSInit(); 81 crypto::EnsureNSSInit();
80 82
81 std::string cert_data; 83 std::vector<uint8> cert_data;
82 if (!GetDERFromPEM(certificate, "CERTIFICATE", &cert_data)) { 84 if (!GetDERFromPEM(certificate, "CERTIFICATE", &cert_data)) {
83 LOG(ERROR) << "Failed to parse certificate."; 85 LOG(ERROR) << "Failed to parse certificate.";
84 return false; 86 return false;
85 } 87 }
86 SECItem der_cert; 88 SECItem der_cert;
87 der_cert.type = siDERCertBuffer; 89 der_cert.type = siDERCertBuffer;
88 der_cert.data = reinterpret_cast<unsigned char*>( 90 der_cert.data = cert_data.data();
89 const_cast<char*>(cert_data.c_str())); 91 der_cert.len = cert_data.size();
90 der_cert.len = cert_data.length();
91 92
92 // Parse into a certificate structure. 93 // Parse into a certificate structure.
93 typedef scoped_ptr< 94 typedef scoped_ptr<
94 CERTCertificate, 95 CERTCertificate,
95 crypto::NSSDestroyer<CERTCertificate, CERT_DestroyCertificate> > 96 crypto::NSSDestroyer<CERTCertificate, CERT_DestroyCertificate> >
96 ScopedCERTCertificate; 97 ScopedCERTCertificate;
97 ScopedCERTCertificate cert(CERT_NewTempCertificate( 98 ScopedCERTCertificate cert(CERT_NewTempCertificate(
98 CERT_GetDefaultCertDB(), &der_cert, NULL, PR_FALSE, PR_TRUE)); 99 CERT_GetDefaultCertDB(), &der_cert, NULL, PR_FALSE, PR_TRUE));
99 if (!cert.get()) { 100 if (!cert.get()) {
100 LOG(ERROR) << "Failed to parse certificate."; 101 LOG(ERROR) << "Failed to parse certificate.";
101 return false; 102 return false;
102 } 103 }
103 104
104 // Check that the certificate is signed by trusted CA. 105 // Check that the certificate is signed by trusted CA.
105 SECItem trusted_ca_key_der_item; 106 SECItem trusted_ca_key_der_item;
106 trusted_ca_key_der_item.type = siDERCertBuffer; 107 trusted_ca_key_der_item.type = siDERCertBuffer;
107 trusted_ca_key_der_item.data = const_cast<unsigned char*>( 108 trusted_ca_key_der_item.data =
108 kTrustedCAPublicKeyDER), 109 const_cast<unsigned char*>(kTrustedCAPublicKeyDER),
109 trusted_ca_key_der_item.len = sizeof(kTrustedCAPublicKeyDER); 110 trusted_ca_key_der_item.len = sizeof(kTrustedCAPublicKeyDER);
110 crypto::ScopedSECKEYPublicKey ca_public_key( 111 crypto::ScopedSECKEYPublicKey ca_public_key(
111 SECKEY_ImportDERPublicKey(&trusted_ca_key_der_item, CKK_RSA)); 112 SECKEY_ImportDERPublicKey(&trusted_ca_key_der_item, CKK_RSA));
112 SECStatus verified = CERT_VerifySignedDataWithPublicKey( 113 SECStatus verified = CERT_VerifySignedDataWithPublicKey(
113 &cert->signatureWrap, ca_public_key.get(), NULL); 114 &cert->signatureWrap, ca_public_key.get(), NULL);
114 if (verified != SECSuccess) { 115 if (verified != SECSuccess) {
115 LOG(ERROR) << "Certificate is not issued by the trusted CA."; 116 LOG(ERROR) << "Certificate is not issued by the trusted CA.";
116 return false; 117 return false;
117 } 118 }
118 119
(...skipping 16 matching lines...) Expand all
135 136
136 // Make sure that the certificate matches the unsigned data presented. 137 // Make sure that the certificate matches the unsigned data presented.
137 // Verify that the |signature| matches |data|. 138 // Verify that the |signature| matches |data|.
138 crypto::ScopedSECKEYPublicKey public_key(CERT_ExtractPublicKey(cert.get())); 139 crypto::ScopedSECKEYPublicKey public_key(CERT_ExtractPublicKey(cert.get()));
139 if (!public_key.get()) { 140 if (!public_key.get()) {
140 LOG(ERROR) << "Unable to extract public key from certificate."; 141 LOG(ERROR) << "Unable to extract public key from certificate.";
141 return false; 142 return false;
142 } 143 }
143 SECItem signature_item; 144 SECItem signature_item;
144 signature_item.type = siBuffer; 145 signature_item.type = siBuffer;
145 signature_item.data = reinterpret_cast<unsigned char*>( 146 signature_item.data =
146 const_cast<char*>(signature.c_str())); 147 reinterpret_cast<unsigned char*>(const_cast<char*>(signature.c_str()));
147 signature_item.len = static_cast<unsigned int>(signature.size()); 148 signature_item.len = static_cast<unsigned int>(signature.size());
148 verified = VFY_VerifyDataDirect(reinterpret_cast<unsigned char*>( 149 verified = VFY_VerifyDataDirect(
149 const_cast<char*>(data.c_str())), data.size(), 150 reinterpret_cast<unsigned char*>(const_cast<char*>(data.c_str())),
150 public_key.get(), &signature_item, SEC_OID_PKCS1_RSA_ENCRYPTION, 151 data.size(),
151 SEC_OID_SHA1, NULL, NULL); 152 public_key.get(),
153 &signature_item,
154 SEC_OID_PKCS1_RSA_ENCRYPTION,
155 SEC_OID_SHA1,
156 NULL,
157 NULL);
152 if (verified != SECSuccess) { 158 if (verified != SECSuccess) {
153 LOG(ERROR) << "Signed blobs did not match."; 159 LOG(ERROR) << "Signed blobs did not match.";
154 return false; 160 return false;
155 } 161 }
156 return true; 162 return true;
157 } 163 }
158 164
159 bool NetworkingPrivateCrypto::EncryptByteString(const std::string& pub_key_der, 165 bool NetworkingPrivateCrypto::EncryptByteString(
160 const std::string& data, 166 const std::vector<uint8>& pub_key_der,
161 std::string* encrypted_output) { 167 const std::string& data,
168 std::vector<uint8>* encrypted_output) {
162 crypto::EnsureNSSInit(); 169 crypto::EnsureNSSInit();
163 170
164 SECItem pub_key_der_item; 171 SECItem pub_key_der_item;
165 pub_key_der_item.type = siDERCertBuffer; 172 pub_key_der_item.type = siDERCertBuffer;
166 pub_key_der_item.data = reinterpret_cast<unsigned char*>( 173 pub_key_der_item.data = const_cast<unsigned char*>(pub_key_der.data());
167 const_cast<char*>(pub_key_der.c_str()));
168 pub_key_der_item.len = pub_key_der.size(); 174 pub_key_der_item.len = pub_key_der.size();
169 175
170 crypto::ScopedSECKEYPublicKey public_key(SECKEY_ImportDERPublicKey( 176 crypto::ScopedSECKEYPublicKey public_key(
171 &pub_key_der_item, CKK_RSA)); 177 SECKEY_ImportDERPublicKey(&pub_key_der_item, CKK_RSA));
172 if (!public_key.get()) { 178 if (!public_key.get()) {
173 LOG(ERROR) << "Failed to parse public key."; 179 LOG(ERROR) << "Failed to parse public key.";
174 return false; 180 return false;
175 } 181 }
176 182
177 size_t encrypted_length = SECKEY_PublicKeyStrength(public_key.get()); 183 size_t encrypted_length = SECKEY_PublicKeyStrength(public_key.get());
178 // RSAES is defined as operating on messages up to a length of k - 11, where 184 // RSAES is defined as operating on messages up to a length of k - 11, where
179 // k is the octet length of the RSA modulus. 185 // k is the octet length of the RSA modulus.
180 if (encrypted_length < data.size() + 11) { 186 if (encrypted_length < data.size() + 11) {
181 LOG(ERROR) << "Too much data to encrypt."; 187 LOG(ERROR) << "Too much data to encrypt.";
182 return false; 188 return false;
183 } 189 }
184 190
185 scoped_ptr<unsigned char[]> rsa_output(new unsigned char[encrypted_length]); 191 scoped_ptr<unsigned char[]> rsa_output(new unsigned char[encrypted_length]);
186 SECStatus encrypted = PK11_PubEncryptPKCS1( 192 SECStatus encrypted = PK11_PubEncryptPKCS1(
187 public_key.get(), 193 public_key.get(),
188 rsa_output.get(), 194 rsa_output.get(),
189 reinterpret_cast<unsigned char*>(const_cast<char*>(data.data())), 195 reinterpret_cast<unsigned char*>(const_cast<char*>(data.data())),
190 data.length(), 196 data.length(),
191 NULL); 197 NULL);
192 if (encrypted != SECSuccess) { 198 if (encrypted != SECSuccess) {
193 LOG(ERROR) << "Error during encryption."; 199 LOG(ERROR) << "Error during encryption.";
194 return false; 200 return false;
195 } 201 }
196 encrypted_output->assign(reinterpret_cast<char*>(rsa_output.get()), 202 encrypted_output->assign(rsa_output.get(),
197 encrypted_length); 203 rsa_output.get() + encrypted_length);
198 return true; 204 return true;
199 } 205 }
200 206
201 bool NetworkingPrivateCrypto::DecryptByteString( 207 bool NetworkingPrivateCrypto::DecryptByteString(
202 const std::string& private_key_pem, 208 const std::string& private_key_pem,
203 const std::string& encrypted_data, 209 const std::vector<uint8>& encrypted_data,
204 std::string* decrypted_output) { 210 std::string* decrypted_output) {
205 crypto::EnsureNSSInit(); 211 crypto::EnsureNSSInit();
206 212
207 std::string private_key_der; 213 std::vector<uint8> private_key_data;
208 if (!GetDERFromPEM(private_key_pem, "PRIVATE KEY", &private_key_der)) { 214 if (!GetDERFromPEM(private_key_pem, "PRIVATE KEY", &private_key_data)) {
209 LOG(ERROR) << "Failed to parse private key PEM."; 215 LOG(ERROR) << "Failed to parse private key PEM.";
210 return false; 216 return false;
211 } 217 }
212 std::vector<uint8> private_key_data(private_key_der.begin(),
213 private_key_der.end());
214 scoped_ptr<crypto::RSAPrivateKey> private_key( 218 scoped_ptr<crypto::RSAPrivateKey> private_key(
215 crypto::RSAPrivateKey::CreateFromPrivateKeyInfo(private_key_data)); 219 crypto::RSAPrivateKey::CreateFromPrivateKeyInfo(private_key_data));
216 if (!private_key || !private_key->public_key()) { 220 if (!private_key || !private_key->public_key()) {
217 LOG(ERROR) << "Failed to parse private key DER."; 221 LOG(ERROR) << "Failed to parse private key DER.";
218 return false; 222 return false;
219 } 223 }
220 224
221 size_t encrypted_length = SECKEY_SignatureLen(private_key->public_key()); 225 size_t encrypted_length = SECKEY_SignatureLen(private_key->public_key());
222 scoped_ptr<unsigned char[]> rsa_output(new unsigned char[encrypted_length]); 226 scoped_ptr<unsigned char[]> rsa_output(new unsigned char[encrypted_length]);
223 unsigned int output_length = 0; 227 unsigned int output_length = 0;
224 SECStatus decrypted = 228 SECStatus decrypted =
225 PK11_PrivDecryptPKCS1(private_key->key(), 229 PK11_PrivDecryptPKCS1(private_key->key(),
226 rsa_output.get(), 230 rsa_output.get(),
227 &output_length, 231 &output_length,
228 encrypted_length, 232 encrypted_length,
229 reinterpret_cast<unsigned char*>( 233 const_cast<unsigned char*>(encrypted_data.data()),
230 const_cast<char*>(encrypted_data.data())), 234 encrypted_data.size());
231 encrypted_data.length());
232 if (decrypted != SECSuccess) { 235 if (decrypted != SECSuccess) {
233 LOG(ERROR) << "Error during decryption."; 236 LOG(ERROR) << "Error during decryption.";
234 return false; 237 return false;
235 } 238 }
236 decrypted_output->assign(reinterpret_cast<char*>(rsa_output.get()), 239 decrypted_output->assign(reinterpret_cast<char*>(rsa_output.get()),
237 output_length); 240 output_length);
238 return true; 241 return true;
239 } 242 }
240
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698