x509_util_mac.cc 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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
  3. // found in the LICENSE file.
  4. #include "net/cert/x509_util_mac.h"
  5. #include "base/check_op.h"
  6. namespace net {
  7. // CSSM functions are deprecated as of OSX 10.7, but have no replacement.
  8. // https://bugs.chromium.org/p/chromium/issues/detail?id=590914#c1
  9. #pragma clang diagnostic push
  10. #pragma clang diagnostic ignored "-Wdeprecated-declarations"
  11. namespace x509_util {
  12. CSSMFieldValue::CSSMFieldValue()
  13. : cl_handle_(CSSM_INVALID_HANDLE), oid_(nullptr), field_(nullptr) {}
  14. CSSMFieldValue::CSSMFieldValue(CSSM_CL_HANDLE cl_handle,
  15. const CSSM_OID* oid,
  16. CSSM_DATA_PTR field)
  17. : cl_handle_(cl_handle),
  18. oid_(const_cast<CSSM_OID_PTR>(oid)),
  19. field_(field) {
  20. }
  21. CSSMFieldValue::~CSSMFieldValue() {
  22. Reset(CSSM_INVALID_HANDLE, nullptr, nullptr);
  23. }
  24. void CSSMFieldValue::Reset(CSSM_CL_HANDLE cl_handle,
  25. CSSM_OID_PTR oid,
  26. CSSM_DATA_PTR field) {
  27. if (cl_handle_ && oid_ && field_)
  28. CSSM_CL_FreeFieldValue(cl_handle_, oid_, field_);
  29. cl_handle_ = cl_handle;
  30. oid_ = oid;
  31. field_ = field;
  32. }
  33. CSSMCachedCertificate::CSSMCachedCertificate()
  34. : cl_handle_(CSSM_INVALID_HANDLE),
  35. cached_cert_handle_(CSSM_INVALID_HANDLE) {
  36. }
  37. CSSMCachedCertificate::~CSSMCachedCertificate() {
  38. if (cl_handle_ && cached_cert_handle_)
  39. CSSM_CL_CertAbortCache(cl_handle_, cached_cert_handle_);
  40. }
  41. OSStatus CSSMCachedCertificate::Init(SecCertificateRef os_cert_handle) {
  42. DCHECK(!cl_handle_ && !cached_cert_handle_);
  43. DCHECK(os_cert_handle);
  44. CSSM_DATA cert_data;
  45. OSStatus status = SecCertificateGetData(os_cert_handle, &cert_data);
  46. if (status)
  47. return status;
  48. status = SecCertificateGetCLHandle(os_cert_handle, &cl_handle_);
  49. if (status) {
  50. DCHECK(!cl_handle_);
  51. return status;
  52. }
  53. status = CSSM_CL_CertCache(cl_handle_, &cert_data, &cached_cert_handle_);
  54. if (status)
  55. DCHECK(!cached_cert_handle_);
  56. return status;
  57. }
  58. OSStatus CSSMCachedCertificate::GetField(const CSSM_OID* field_oid,
  59. CSSMFieldValue* field) const {
  60. DCHECK(cl_handle_);
  61. DCHECK(cached_cert_handle_);
  62. CSSM_OID_PTR oid = const_cast<CSSM_OID_PTR>(field_oid);
  63. CSSM_DATA_PTR field_ptr = nullptr;
  64. CSSM_HANDLE results_handle = CSSM_INVALID_HANDLE;
  65. uint32_t field_value_count = 0;
  66. CSSM_RETURN status = CSSM_CL_CertGetFirstCachedFieldValue(
  67. cl_handle_, cached_cert_handle_, oid, &results_handle,
  68. &field_value_count, &field_ptr);
  69. if (status)
  70. return status;
  71. // Note: |field_value_count| may be > 1, indicating that more than one
  72. // value is present. This may happen with extensions, but for current
  73. // usages, only the first value is returned.
  74. CSSM_CL_CertAbortQuery(cl_handle_, results_handle);
  75. field->Reset(cl_handle_, oid, field_ptr);
  76. return CSSM_OK;
  77. }
  78. } // namespace x509_util
  79. #pragma clang diagnostic pop // "-Wdeprecated-declarations"
  80. } // namespace net