]> git.saurik.com Git - apple/security.git/blob - cdsa/cdsa_client/keyclient.cpp
Security-164.1.tar.gz
[apple/security.git] / cdsa / cdsa_client / keyclient.cpp
1 /*
2 * Copyright (c) 2000-2001 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 // keyclient
21 //
22 #include <Security/keyclient.h>
23 #include <Security/cssmdata.h>
24
25
26 using namespace CssmClient;
27
28
29 KeyImpl::KeyImpl(const CSP &csp) : ObjectImpl(csp), CssmKey()
30 {
31 mActive=false;
32 }
33
34 KeyImpl::KeyImpl(const CSP &csp, const CSSM_KEY &key, bool copy) : ObjectImpl(csp), CssmKey(key)
35 {
36 if (copy)
37 keyData() = CssmAutoData(csp.allocator(), keyData()).release();
38 mActive=true;
39 }
40
41 KeyImpl::KeyImpl(const CSP &csp, const CSSM_DATA &keyData) : ObjectImpl(csp),
42 CssmKey(keyData.Length, csp->allocator().alloc<uint8>(keyData.Length))
43 {
44 memcpy(KeyData.Data, keyData.Data, keyData.Length);
45 mActive=true;
46 }
47
48 KeyImpl::~KeyImpl()
49 {
50 try
51 {
52 deactivate();
53 }
54 catch(...) {}
55 }
56
57 void
58 KeyImpl::deleteKey(const CSSM_ACCESS_CREDENTIALS *cred)
59 {
60 if (mActive)
61 {
62 mActive=false;
63 check(CSSM_FreeKey(csp()->handle(), cred, this, CSSM_TRUE));
64 }
65 }
66
67 CssmKeySize
68 KeyImpl::sizeInBits() const
69 {
70 CssmKeySize size;
71 check(CSSM_QueryKeySizeInBits(csp()->handle(), NULL, this, &size));
72 return size;
73 }
74
75 void
76 KeyImpl::getAcl(AutoAclEntryInfoList &aclInfos, const char *selectionTag) const
77 {
78 aclInfos.allocator(allocator());
79 check(CSSM_GetKeyAcl(csp()->handle(), this, reinterpret_cast<const CSSM_STRING *>(selectionTag), aclInfos, aclInfos));
80 }
81
82 void
83 KeyImpl::changeAcl(const CSSM_ACL_EDIT &aclEdit,
84 const CSSM_ACCESS_CREDENTIALS *accessCred)
85 {
86 check(CSSM_ChangeKeyAcl(csp()->handle(),
87 AccessCredentials::needed(accessCred), &aclEdit, this));
88 }
89
90 void
91 KeyImpl::getOwner(AutoAclOwnerPrototype &owner) const
92 {
93 owner.allocator(allocator());
94 check(CSSM_GetKeyOwner(csp()->handle(), this, owner));
95 }
96
97 void
98 KeyImpl::changeOwner(const CSSM_ACL_OWNER_PROTOTYPE &newOwner,
99 const CSSM_ACCESS_CREDENTIALS *accessCred)
100 {
101 check(CSSM_ChangeKeyOwner(csp()->handle(),
102 AccessCredentials::needed(accessCred), this, &newOwner));
103 }
104
105 void KeyImpl::activate()
106 {
107 mActive=true;
108 }
109
110 void KeyImpl::deactivate()
111 {
112 if (mActive)
113 {
114 mActive=false;
115 check(CSSM_FreeKey(csp()->handle(), NULL, this, CSSM_FALSE));
116 }
117 }