| OLD | NEW |
| 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/cert/x509_certificate.h" | 5 #include "net/cert/x509_certificate.h" |
| 6 | 6 |
| 7 #include <stdlib.h> | 7 #include <stdlib.h> |
| 8 | 8 |
| 9 #include <algorithm> | 9 #include <algorithm> |
| 10 #include <map> | 10 #include <map> |
| (...skipping 294 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 305 OSCertHandle cert_handle = CreateOSCertHandleFromBytes(data, length); | 305 OSCertHandle cert_handle = CreateOSCertHandleFromBytes(data, length); |
| 306 if (!cert_handle) | 306 if (!cert_handle) |
| 307 return NULL; | 307 return NULL; |
| 308 | 308 |
| 309 X509Certificate* cert = CreateFromHandle(cert_handle, OSCertHandles()); | 309 X509Certificate* cert = CreateFromHandle(cert_handle, OSCertHandles()); |
| 310 FreeOSCertHandle(cert_handle); | 310 FreeOSCertHandle(cert_handle); |
| 311 return cert; | 311 return cert; |
| 312 } | 312 } |
| 313 | 313 |
| 314 // static | 314 // static |
| 315 X509Certificate* X509Certificate::CreateFromPickle(PickleIterator* pickle_iter, | 315 X509Certificate* X509Certificate::CreateFromPickle( |
| 316 PickleType type) { | 316 base::PickleIterator* pickle_iter, |
| 317 PickleType type) { |
| 317 if (type == PICKLETYPE_CERTIFICATE_CHAIN_V3) { | 318 if (type == PICKLETYPE_CERTIFICATE_CHAIN_V3) { |
| 318 int chain_length = 0; | 319 int chain_length = 0; |
| 319 if (!pickle_iter->ReadLength(&chain_length)) | 320 if (!pickle_iter->ReadLength(&chain_length)) |
| 320 return NULL; | 321 return NULL; |
| 321 | 322 |
| 322 std::vector<base::StringPiece> cert_chain; | 323 std::vector<base::StringPiece> cert_chain; |
| 323 const char* data = NULL; | 324 const char* data = NULL; |
| 324 int data_length = 0; | 325 int data_length = 0; |
| 325 for (int i = 0; i < chain_length; ++i) { | 326 for (int i = 0; i < chain_length; ++i) { |
| 326 if (!pickle_iter->ReadData(&data, &data_length)) | 327 if (!pickle_iter->ReadData(&data, &data_length)) |
| (...skipping 25 matching lines...) Expand all Loading... |
| 352 // x86-64 is little-endian, if that happens, the next 32 bits will be all | 353 // x86-64 is little-endian, if that happens, the next 32 bits will be all |
| 353 // zeroes (the high bits) and the 32 bits we already read above are the | 354 // zeroes (the high bits) and the 32 bits we already read above are the |
| 354 // correct value (we assume there are never more than 2^32 - 1 intermediate | 355 // correct value (we assume there are never more than 2^32 - 1 intermediate |
| 355 // certificates in a chain; in practice, more than a dozen or so is | 356 // certificates in a chain; in practice, more than a dozen or so is |
| 356 // basically unheard of). Since it's invalid for a certificate to start with | 357 // basically unheard of). Since it's invalid for a certificate to start with |
| 357 // 32 bits of zeroes, we check for that here and skip it if we find it. We | 358 // 32 bits of zeroes, we check for that here and skip it if we find it. We |
| 358 // save a copy of the pickle iterator to restore in case we don't get 32 | 359 // save a copy of the pickle iterator to restore in case we don't get 32 |
| 359 // bits of zeroes. Now we always write 32 bits, so after a while, these old | 360 // bits of zeroes. Now we always write 32 bits, so after a while, these old |
| 360 // cached pickles will all get replaced. | 361 // cached pickles will all get replaced. |
| 361 // TODO(mdm): remove this compatibility code in April 2013 or so. | 362 // TODO(mdm): remove this compatibility code in April 2013 or so. |
| 362 PickleIterator saved_iter = *pickle_iter; | 363 base::PickleIterator saved_iter = *pickle_iter; |
| 363 uint32 zero_check = 0; | 364 uint32 zero_check = 0; |
| 364 if (!pickle_iter->ReadUInt32(&zero_check)) { | 365 if (!pickle_iter->ReadUInt32(&zero_check)) { |
| 365 // This may not be an error. If there are no intermediates, and we're | 366 // This may not be an error. If there are no intermediates, and we're |
| 366 // reading an old 32-bit pickle, and there's nothing else after this in | 367 // reading an old 32-bit pickle, and there's nothing else after this in |
| 367 // the pickle, we should report success. Note that it is technically | 368 // the pickle, we should report success. Note that it is technically |
| 368 // possible for us to skip over zeroes that should have occurred after | 369 // possible for us to skip over zeroes that should have occurred after |
| 369 // an empty certificate list; to avoid this going forward, only do this | 370 // an empty certificate list; to avoid this going forward, only do this |
| 370 // backward-compatibility stuff for PICKLETYPE_CERTIFICATE_CHAIN_V1 | 371 // backward-compatibility stuff for PICKLETYPE_CERTIFICATE_CHAIN_V1 |
| 371 // which comes from the pickle version number in http_response_info.cc. | 372 // which comes from the pickle version number in http_response_info.cc. |
| 372 if (num_intermediates) { | 373 if (num_intermediates) { |
| (...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 466 for (OSCertHandles::iterator it = certificates.begin(); | 467 for (OSCertHandles::iterator it = certificates.begin(); |
| 467 it != certificates.end(); ++it) { | 468 it != certificates.end(); ++it) { |
| 468 X509Certificate* result = CreateFromHandle(*it, OSCertHandles()); | 469 X509Certificate* result = CreateFromHandle(*it, OSCertHandles()); |
| 469 results.push_back(scoped_refptr<X509Certificate>(result)); | 470 results.push_back(scoped_refptr<X509Certificate>(result)); |
| 470 FreeOSCertHandle(*it); | 471 FreeOSCertHandle(*it); |
| 471 } | 472 } |
| 472 | 473 |
| 473 return results; | 474 return results; |
| 474 } | 475 } |
| 475 | 476 |
| 476 void X509Certificate::Persist(Pickle* pickle) { | 477 void X509Certificate::Persist(base::Pickle* pickle) { |
| 477 DCHECK(cert_handle_); | 478 DCHECK(cert_handle_); |
| 478 // This would be an absolutely insane number of intermediates. | 479 // This would be an absolutely insane number of intermediates. |
| 479 if (intermediate_ca_certs_.size() > static_cast<size_t>(INT_MAX) - 1) { | 480 if (intermediate_ca_certs_.size() > static_cast<size_t>(INT_MAX) - 1) { |
| 480 NOTREACHED(); | 481 NOTREACHED(); |
| 481 return; | 482 return; |
| 482 } | 483 } |
| 483 if (!pickle->WriteInt( | 484 if (!pickle->WriteInt( |
| 484 static_cast<int>(intermediate_ca_certs_.size() + 1)) || | 485 static_cast<int>(intermediate_ca_certs_.size() + 1)) || |
| 485 !WriteOSCertHandleToPickle(cert_handle_, pickle)) { | 486 !WriteOSCertHandleToPickle(cert_handle_, pickle)) { |
| 486 NOTREACHED(); | 487 NOTREACHED(); |
| (...skipping 269 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 756 RemoveFromCache(cert_handle_); | 757 RemoveFromCache(cert_handle_); |
| 757 FreeOSCertHandle(cert_handle_); | 758 FreeOSCertHandle(cert_handle_); |
| 758 } | 759 } |
| 759 for (size_t i = 0; i < intermediate_ca_certs_.size(); ++i) { | 760 for (size_t i = 0; i < intermediate_ca_certs_.size(); ++i) { |
| 760 RemoveFromCache(intermediate_ca_certs_[i]); | 761 RemoveFromCache(intermediate_ca_certs_[i]); |
| 761 FreeOSCertHandle(intermediate_ca_certs_[i]); | 762 FreeOSCertHandle(intermediate_ca_certs_[i]); |
| 762 } | 763 } |
| 763 } | 764 } |
| 764 | 765 |
| 765 } // namespace net | 766 } // namespace net |
| OLD | NEW |