]> git.saurik.com Git - apple/security.git/blob - cdsa/cdsa_client/wrapkey.cpp
Security-164.1.tar.gz
[apple/security.git] / cdsa / cdsa_client / wrapkey.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 // wrapkey - client interface for wrapping and unwrapping keys
21 //
22 #include <Security/wrapkey.h>
23
24 using namespace CssmClient;
25
26
27 Key
28 WrapKey::operator () (Key &keyToBeWrapped, const CssmData *descriptiveData)
29 {
30 Key wrappedKey;
31
32 check(CSSM_WrapKey(handle(), mCred, keyToBeWrapped, descriptiveData,
33 wrappedKey.makeNewKey(attachment())));
34 wrappedKey->activate();
35
36 return wrappedKey;
37 }
38
39 void
40 WrapKey::operator () (const CssmKey &keyToBeWrapped, CssmKey &wrappedKey,
41 const CssmData *descriptiveData)
42 {
43 check(CSSM_WrapKey(handle(), mCred, &keyToBeWrapped, descriptiveData, &wrappedKey));
44 }
45
46 void
47 WrapKey::activate()
48 {
49 if (!mActive)
50 {
51 Crypt::activate();
52 if (mWrappedKeyFormat != CSSM_KEYBLOB_WRAPPED_FORMAT_NONE);
53 set(CSSM_ATTRIBUTE_WRAPPED_KEY_FORMAT, mWrappedKeyFormat);
54 }
55 }
56
57 Key
58 UnwrapKey::operator () (const CssmKey &keyToBeUnwrapped, const KeySpec &spec)
59 {
60 Key unwrappedKey;
61
62 const ResourceControlContext resourceControlContext
63 (mAclEntry, const_cast<AccessCredentials *>(mCred));
64 CssmData data(reinterpret_cast<uint8 *>(1), 0);
65
66 check(CSSM_UnwrapKey(handle(), NULL,
67 &keyToBeUnwrapped, spec.usage, spec.attributes,
68 spec.label, &resourceControlContext,
69 unwrappedKey.makeNewKey(attachment()), &data));
70 unwrappedKey->activate();
71
72 return unwrappedKey;
73 }
74
75 void
76 UnwrapKey::operator () (const CssmKey &keyToBeUnwrapped, const KeySpec &spec,
77 CssmKey &unwrappedKey)
78 {
79 const ResourceControlContext resourceControlContext
80 (mAclEntry, const_cast<AccessCredentials *>(mCred));
81 CssmData data(reinterpret_cast<uint8 *>(1), 0);
82
83 check(CSSM_UnwrapKey(handle(), NULL, &keyToBeUnwrapped, spec.usage,
84 spec.attributes, spec.label, &resourceControlContext,
85 &unwrappedKey, &data));
86 }
87
88 Key
89 UnwrapKey::operator () (const CssmKey &keyToBeUnwrapped, const KeySpec &spec,
90 Key &optionalPublicKey)
91 {
92 Key unwrappedKey;
93
94 const ResourceControlContext resourceControlContext
95 (mAclEntry, const_cast<AccessCredentials *>(mCred));
96 CssmData data(reinterpret_cast<uint8 *>(1), 0);
97
98 check(CSSM_UnwrapKey(handle(), optionalPublicKey,
99 &keyToBeUnwrapped, spec.usage, spec.attributes,
100 spec.label, &resourceControlContext,
101 unwrappedKey.makeNewKey(attachment()), &data));
102
103 unwrappedKey->activate();
104
105 return unwrappedKey;
106 }
107
108 void
109 UnwrapKey::operator () (const CssmKey &keyToBeUnwrapped, const KeySpec &spec,
110 CssmKey &unwrappedKey,
111 const CssmKey *optionalPublicKey)
112 {
113 const ResourceControlContext resourceControlContext
114 (mAclEntry, const_cast<AccessCredentials *>(mCred));
115 CssmData data(reinterpret_cast<uint8 *>(1), 0);
116
117 check(CSSM_UnwrapKey(handle(), optionalPublicKey, &keyToBeUnwrapped,
118 spec.usage, spec.attributes, spec.label,
119 &resourceControlContext, &unwrappedKey, &data));
120 }
121
122
123 Key
124 UnwrapKey::operator () (const CssmKey &keyToBeUnwrapped, const KeySpec &spec,
125 CssmData *descriptiveData)
126 {
127 Key unwrappedKey;
128
129 const ResourceControlContext resourceControlContext
130 (mAclEntry, const_cast<AccessCredentials *>(mCred));
131
132 check(CSSM_UnwrapKey(handle(), NULL, &keyToBeUnwrapped, spec.usage,
133 spec.attributes, spec.label, &resourceControlContext,
134 unwrappedKey.makeNewKey(attachment()),
135 descriptiveData));
136 unwrappedKey->activate();
137
138 return unwrappedKey;
139 }
140
141 void
142 UnwrapKey::operator () (const CssmKey &keyToBeUnwrapped, const KeySpec &spec,
143 CssmKey &unwrappedKey, CssmData *descriptiveData)
144 {
145 const ResourceControlContext resourceControlContext
146 (mAclEntry, const_cast<AccessCredentials *>(mCred));
147
148 check(CSSM_UnwrapKey(handle(), NULL, &keyToBeUnwrapped, spec.usage,
149 spec.attributes, spec.label, &resourceControlContext,
150 &unwrappedKey, descriptiveData));
151 }
152
153 Key
154 UnwrapKey::operator () (const CssmKey &keyToBeUnwrapped, const KeySpec &spec,
155 Key &optionalPublicKey, CssmData *descriptiveData)
156 {
157 Key unwrappedKey;
158
159 const ResourceControlContext resourceControlContext
160 (mAclEntry, const_cast<AccessCredentials *>(mCred));
161
162 check(CSSM_UnwrapKey(handle(), optionalPublicKey, &keyToBeUnwrapped,
163 spec.usage, spec.attributes, spec.label,
164 &resourceControlContext,
165 unwrappedKey.makeNewKey(attachment()),
166 descriptiveData));
167 unwrappedKey->activate();
168
169 return unwrappedKey;
170 }
171
172 void
173 UnwrapKey::operator () (const CssmKey &keyToBeUnwrapped, const KeySpec &spec,
174 CssmKey &unwrappedKey, CssmData *descriptiveData,
175 const CssmKey *optionalPublicKey)
176 {
177 const ResourceControlContext resourceControlContext
178 (mAclEntry, const_cast<AccessCredentials *>(mCred));
179
180 check(CSSM_UnwrapKey(handle(), optionalPublicKey, &keyToBeUnwrapped,
181 spec.usage, spec.attributes, spec.label,
182 &resourceControlContext, &unwrappedKey,
183 descriptiveData));
184 }
185
186
187 void DeriveKey::activate()
188 {
189 if (!mActive)
190 {
191 check(CSSM_CSP_CreateDeriveKeyContext(attachment()->handle(), mAlgorithm,
192 mTargetType, mKeySize, mCred, mKey, mIterationCount, mSalt, mSeed, &mHandle));
193 mActive = true;
194 }
195 }
196
197
198 Key
199 DeriveKey::operator () (CssmData *param, const KeySpec &spec)
200 {
201 Key derivedKey;
202
203 const ResourceControlContext resourceControlContext
204 (mAclEntry, const_cast<AccessCredentials *>(mCred));
205
206 check(CSSM_DeriveKey(handle(), param, spec.usage, spec.attributes,
207 spec.label, &resourceControlContext,
208 derivedKey.makeNewKey(attachment())));
209 derivedKey->activate();
210
211 return derivedKey;
212 }
213
214 void
215 DeriveKey::operator () (CssmData *param, const KeySpec &spec,
216 CssmKey &derivedKey)
217 {
218 const ResourceControlContext resourceControlContext
219 (mAclEntry, const_cast<AccessCredentials *>(mCred));
220
221 check(CSSM_DeriveKey(handle(), param, spec.usage, spec.attributes,
222 spec.label, &resourceControlContext, &derivedKey));
223 }