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

Side by Side Diff: net/base/cert_verify_proc_openssl.cc

Issue 11549033: Separate CertVerifyProcAndroid from CertVerifyProcOpenSSL (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Created 8 years 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
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 "net/base/cert_verify_proc_openssl.h" 5 #include "net/base/cert_verify_proc_openssl.h"
6 6
7 #include <openssl/x509v3.h> 7 #include <openssl/x509v3.h>
8 8
9 #include <string> 9 #include <string>
10 #include <vector> 10 #include <vector>
11 11
12 #include "base/logging.h" 12 #include "base/logging.h"
13 #include "base/sha1.h" 13 #include "base/sha1.h"
14 #include "crypto/openssl_util.h" 14 #include "crypto/openssl_util.h"
15 #include "crypto/sha2.h" 15 #include "crypto/sha2.h"
16 #include "net/base/asn1_util.h" 16 #include "net/base/asn1_util.h"
17 #include "net/base/cert_status_flags.h" 17 #include "net/base/cert_status_flags.h"
18 #include "net/base/cert_verifier.h" 18 #include "net/base/cert_verifier.h"
19 #include "net/base/cert_verify_result.h" 19 #include "net/base/cert_verify_result.h"
20 #include "net/base/net_errors.h" 20 #include "net/base/net_errors.h"
21 #include "net/base/x509_certificate.h" 21 #include "net/base/x509_certificate.h"
22 22
23 #if defined(OS_ANDROID)
24 #include "net/android/network_library.h"
25 #endif
26
27 namespace net { 23 namespace net {
28 24
29 namespace { 25 namespace {
30 26
31 // Maps X509_STORE_CTX_get_error() return values to our cert status flags. 27 // Maps X509_STORE_CTX_get_error() return values to our cert status flags.
32 CertStatus MapCertErrorToCertStatus(int err) { 28 CertStatus MapCertErrorToCertStatus(int err) {
33 switch (err) { 29 switch (err) {
34 case X509_V_ERR_SUBJECT_ISSUER_MISMATCH: 30 case X509_V_ERR_SUBJECT_ISSUER_MISMATCH:
35 return CERT_STATUS_COMMON_NAME_INVALID; 31 return CERT_STATUS_COMMON_NAME_INVALID;
36 case X509_V_ERR_CERT_NOT_YET_VALID: 32 case X509_V_ERR_CERT_NOT_YET_VALID:
(...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after
154 base::SHA1HashBytes(reinterpret_cast<const uint8*>(spki_bytes.data()), 150 base::SHA1HashBytes(reinterpret_cast<const uint8*>(spki_bytes.data()),
155 spki_bytes.size(), sha1.data()); 151 spki_bytes.size(), sha1.data());
156 hashes->push_back(sha1); 152 hashes->push_back(sha1);
157 153
158 HashValue sha256(HASH_VALUE_SHA256); 154 HashValue sha256(HASH_VALUE_SHA256);
159 crypto::SHA256HashString(spki_bytes, sha1.data(), crypto::kSHA256Length); 155 crypto::SHA256HashString(spki_bytes, sha1.data(), crypto::kSHA256Length);
160 hashes->push_back(sha256); 156 hashes->push_back(sha256);
161 } 157 }
162 } 158 }
163 159
164 #if defined(OS_ANDROID)
165 // Returns true if we have verification result in |verify_result| from Android
166 // Trust Manager. Otherwise returns false.
167 bool VerifyFromAndroidTrustManager(const std::vector<std::string>& cert_bytes,
168 CertVerifyResult* verify_result) {
169 // TODO(joth): Fetch the authentication type from SSL rather than hardcode.
170 bool verified = true;
171 android::VerifyResult result =
172 android::VerifyX509CertChain(cert_bytes, "RSA");
173 switch (result) {
174 case android::VERIFY_OK:
175 break;
176 case android::VERIFY_NO_TRUSTED_ROOT:
177 verify_result->cert_status |= CERT_STATUS_AUTHORITY_INVALID;
178 break;
179 case android::VERIFY_INVOCATION_ERROR:
180 verified = false;
181 break;
182 default:
183 verify_result->cert_status |= CERT_STATUS_INVALID;
184 break;
185 }
186 return verified;
187 }
188
189 void GetChainDEREncodedBytes(X509Certificate* cert,
190 std::vector<std::string>* chain_bytes) {
191 X509Certificate::OSCertHandle cert_handle = cert->os_cert_handle();
192 X509Certificate::OSCertHandles cert_handles =
193 cert->GetIntermediateCertificates();
194
195 // Make sure the peer's own cert is the first in the chain, if it's not
196 // already there.
197 if (cert_handles.empty() || cert_handles[0] != cert_handle)
198 cert_handles.insert(cert_handles.begin(), cert_handle);
199
200 chain_bytes->reserve(cert_handles.size());
201 for (X509Certificate::OSCertHandles::const_iterator it =
202 cert_handles.begin(); it != cert_handles.end(); ++it) {
203 std::string cert_bytes;
204 X509Certificate::X509Certificate::GetDEREncoded(*it, &cert_bytes);
205 chain_bytes->push_back(cert_bytes);
206 }
207 }
208 #endif // defined(OS_ANDROID)
209
210 } // namespace 160 } // namespace
211 161
212 CertVerifyProcOpenSSL::CertVerifyProcOpenSSL() {} 162 CertVerifyProcOpenSSL::CertVerifyProcOpenSSL() {}
213 163
214 CertVerifyProcOpenSSL::~CertVerifyProcOpenSSL() {} 164 CertVerifyProcOpenSSL::~CertVerifyProcOpenSSL() {}
215 165
216 int CertVerifyProcOpenSSL::VerifyInternal(X509Certificate* cert, 166 int CertVerifyProcOpenSSL::VerifyInternal(X509Certificate* cert,
217 const std::string& hostname, 167 const std::string& hostname,
218 int flags, 168 int flags,
219 CRLSet* crl_set, 169 CRLSet* crl_set,
220 CertVerifyResult* verify_result) { 170 CertVerifyResult* verify_result) {
221 crypto::EnsureOpenSSLInit(); 171 crypto::EnsureOpenSSLInit();
222 172
223 if (!cert->VerifyNameMatch(hostname)) 173 if (!cert->VerifyNameMatch(hostname))
224 verify_result->cert_status |= CERT_STATUS_COMMON_NAME_INVALID; 174 verify_result->cert_status |= CERT_STATUS_COMMON_NAME_INVALID;
225 175
226 bool verify_attempted = false; 176 crypto::ScopedOpenSSL<X509_STORE_CTX, X509_STORE_CTX_free> ctx(
177 X509_STORE_CTX_new());
227 178
228 #if defined(OS_ANDROID) 179 crypto::ScopedOpenSSL<STACK_OF(X509), sk_X509_free_fn> intermediates(
229 std::vector<std::string> cert_bytes; 180 sk_X509_new_null());
230 GetChainDEREncodedBytes(cert, &cert_bytes); 181 if (!intermediates.get())
182 return ERR_OUT_OF_MEMORY;
231 183
232 verify_attempted = VerifyFromAndroidTrustManager(cert_bytes, verify_result); 184 const X509Certificate::OSCertHandles& os_intermediates =
233 #endif 185 cert->GetIntermediateCertificates();
186 for (X509Certificate::OSCertHandles::const_iterator it =
187 os_intermediates.begin(); it != os_intermediates.end(); ++it) {
188 if (!sk_X509_push(intermediates.get(), *it))
189 return ERR_OUT_OF_MEMORY;
190 }
191 int rv = X509_STORE_CTX_init(ctx.get(), X509Certificate::cert_store(),
192 cert->os_cert_handle(), intermediates.get());
193 CHECK_EQ(1, rv);
Ryan Sleevi 2012/12/13 02:16:36 [existing?] BUG: I don't think this should be a CH
ppi 2012/12/13 04:49:09 Nice catch! I will address this separately: crbug.
234 194
235 if (verify_attempted) { 195 if (X509_verify_cert(ctx.get()) != 1) {
236 if (IsCertStatusError(verify_result->cert_status)) 196 int x509_error = X509_STORE_CTX_get_error(ctx.get());
237 return MapCertStatusToNetError(verify_result->cert_status); 197 CertStatus cert_status = MapCertErrorToCertStatus(x509_error);
238 } else { 198 LOG(ERROR) << "X509 Verification error "
239 crypto::ScopedOpenSSL<X509_STORE_CTX, X509_STORE_CTX_free> ctx( 199 << X509_verify_cert_error_string(x509_error)
240 X509_STORE_CTX_new()); 200 << " : " << x509_error
201 << " : " << X509_STORE_CTX_get_error_depth(ctx.get())
202 << " : " << cert_status;
203 verify_result->cert_status |= cert_status;
204 }
241 205
242 crypto::ScopedOpenSSL<STACK_OF(X509), sk_X509_free_fn> intermediates( 206 GetCertChainInfo(ctx.get(), verify_result);
243 sk_X509_new_null()); 207 AppendPublicKeyHashes(ctx.get(), &verify_result->public_key_hashes);
244 if (!intermediates.get()) 208 if (IsCertStatusError(verify_result->cert_status))
245 return ERR_OUT_OF_MEMORY; 209 return MapCertStatusToNetError(verify_result->cert_status);
246
247 const X509Certificate::OSCertHandles& os_intermediates =
248 cert->GetIntermediateCertificates();
249 for (X509Certificate::OSCertHandles::const_iterator it =
250 os_intermediates.begin(); it != os_intermediates.end(); ++it) {
251 if (!sk_X509_push(intermediates.get(), *it))
252 return ERR_OUT_OF_MEMORY;
253 }
254 int rv = X509_STORE_CTX_init(ctx.get(), X509Certificate::cert_store(),
255 cert->os_cert_handle(), intermediates.get());
256 CHECK_EQ(1, rv);
257
258 if (X509_verify_cert(ctx.get()) != 1) {
259 int x509_error = X509_STORE_CTX_get_error(ctx.get());
260 CertStatus cert_status = MapCertErrorToCertStatus(x509_error);
261 LOG(ERROR) << "X509 Verification error "
262 << X509_verify_cert_error_string(x509_error)
263 << " : " << x509_error
264 << " : " << X509_STORE_CTX_get_error_depth(ctx.get())
265 << " : " << cert_status;
266 verify_result->cert_status |= cert_status;
267 }
268
269 GetCertChainInfo(ctx.get(), verify_result);
270 AppendPublicKeyHashes(ctx.get(), &verify_result->public_key_hashes);
271 if (IsCertStatusError(verify_result->cert_status))
272 return MapCertStatusToNetError(verify_result->cert_status);
273 }
274 210
275 // Currently we only ues OpenSSL's default root CA paths, so treat all 211 // Currently we only ues OpenSSL's default root CA paths, so treat all
276 // correctly verified certs as being from a known root. 212 // correctly verified certs as being from a known root.
277 // TODO(joth): if the motivations described in 213 // TODO(joth): if the motivations described in
278 // http://src.chromium.org/viewvc/chrome?view=rev&revision=80778 become an 214 // http://src.chromium.org/viewvc/chrome?view=rev&revision=80778 become an
279 // issue on OpenSSL builds, we will need to embed a hardcoded list of well 215 // issue on OpenSSL builds, we will need to embed a hardcoded list of well
280 // known root CAs, as per the _mac and _win versions. 216 // known root CAs, as per the _mac and _win versions.
281 verify_result->is_issued_by_known_root = true; 217 verify_result->is_issued_by_known_root = true;
282 218
283 return OK; 219 return OK;
284 } 220 }
285 221
286 } // namespace net 222 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698