]> git.saurik.com Git - apple/security.git/blob - Keychain/Certificates.cpp
Security-176.tar.gz
[apple/security.git] / Keychain / Certificates.cpp
1 /*
2 * Copyright (c) 2002 Apple Computer, Inc. All Rights Reserved.
3 *
4 * The contents of this file constitute Original Code as defined in and are
5 * subject to the Apple Public Source License Version 1.2 (the 'License').
6 * You may not use this file except in compliance with the License. Please obtain
7 * a copy of the License at http://www.apple.com/publicsource and read it before
8 * using this file.
9 *
10 * This Original Code and all software distributed under the License are
11 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS
12 * OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, INCLUDING WITHOUT
13 * LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
14 * PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. Please see the License for the
15 * specific language governing rights and limitations under the License.
16 */
17
18
19 /*
20 File: Certificates.cpp
21
22 Contains: Working with Certificates
23
24 Copyright: 2002 by Apple Computer, Inc., all rights reserved.
25
26 To Do:
27 */
28
29 #include <Security/Certificates.h>
30 #include <Security/CertLibRef.h>//%%%should be included in Certificates.h
31
32 using namespace KeychainCore;
33
34 void CertificateImpl::CertificateImplCommonInit(CSSM_CERT_TYPE type)
35 {
36 mType = type;
37 mCLReference = NULL;
38 //
39 // Create a CL reference for this certificate type.
40 // %%%find us the 1st CL reference we can find for this cert type (this can change)
41 //
42 CertLibCursorImpl* cursor = NULL;
43 cursor = new CertLibCursorImpl(type);
44 if (!cursor)
45 MacOSError::throwMe(errSecItemNotFound/*%%%*/);
46
47 CertLib certLib;//%%%allocated on the stack?!
48 if (!cursor->next(certLib))
49 {
50 delete cursor;
51 MacOSError::throwMe(errSecItemNotFound/*%%%*/);
52 }
53 delete cursor;
54
55 mCLReference = CertLibRef::handle(certLib); // 'tis a SecCertificateLibraryRef
56 }
57
58 CertificateImpl::CertificateImpl(const CSSM_DATA* data, CSSM_CERT_TYPE type):
59 mItem(NULL)
60 {
61 CertificateImplCommonInit(type);
62 (void*)mData.Data = malloc(data->Length);
63 memcpy(mData.Data, data->Data, data->Length);
64 mData.Length = data->Length;
65 }
66
67 CertificateImpl::CertificateImpl(SecKeychainItemRef item, CSSM_CERT_TYPE type)
68 {
69 CertificateImplCommonInit(type);
70 mItem = item;
71 SecRetain(item);
72 mData.Data = NULL;
73 mData.Length = 0;
74 }
75
76 CertificateImpl::~CertificateImpl()
77 {
78 if (mData.Data)
79 {
80 if (mItem)
81 SecKeychainItemFreeContent(NULL, mData.Data); // free if copied via SecKeychainItemCopyContent.
82 else
83 free(mData.Data); // free if copied from the caller when cert ref was created.
84 }
85 if (mItem)
86 SecRelease(mItem);
87
88 if (mCLReference)
89 SecRelease(mCLReference);
90 }
91
92 CSSM_DATA* CertificateImpl::getData()
93 {
94 if (mItem)
95 {
96 if (mData.Data)
97 SecKeychainItemFreeContent(NULL, mData.Data);
98
99 OSStatus result = SecKeychainItemCopyContent(mItem, NULL, NULL, &mData.Length, (void**)&(mData.Data));
100 if (result)
101 MacOSError::throwMe(result);
102 } // otherwise, return the data originally specified when the cert ref was created.
103 return &mData;
104 }
105
106 CSSM_X509_NAME* CertificateImpl::getSubject()
107 {
108 return NULL;//%%%use mCLReference to get subject
109 }
110
111 CSSM_X509_NAME* CertificateImpl::getIssuer()
112 {
113 return NULL;//%%%use mCLReference to get issuer
114 }