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_cdsa_client/wrapkey.h>
23
24namespace Security {
25namespace CssmClient {
26
27
28Key
29WrapKey::operator () (Key &keyToBeWrapped, const CssmData *descriptiveData)
30{
31	Key wrappedKey;
32
33	check(CSSM_WrapKey(handle(), neededCred(), keyToBeWrapped, descriptiveData,
34					   wrappedKey.makeNewKey(attachment())));
35	wrappedKey->activate();
36
37	return wrappedKey;
38}
39
40void
41WrapKey::operator () (const CssmKey &keyToBeWrapped, CssmKey &wrappedKey,
42					  const CssmData *descriptiveData)
43{
44	check(CSSM_WrapKey(handle(), neededCred(), &keyToBeWrapped,
45		descriptiveData, &wrappedKey));
46}
47
48void
49WrapKey::activate()
50{
51	if (!mActive)
52	{
53		Crypt::activate();
54		if (mWrappedKeyFormat != CSSM_KEYBLOB_WRAPPED_FORMAT_NONE)
55			set(CSSM_ATTRIBUTE_WRAPPED_KEY_FORMAT, mWrappedKeyFormat);
56	}
57}
58
59Key
60UnwrapKey::operator () (const CssmKey &keyToBeUnwrapped, const KeySpec &spec)
61{
62	CssmData data(reinterpret_cast<uint8 *>(1), 0);
63	Key unwrappedKey;
64	check(CSSM_UnwrapKey(handle(), NULL,
65						 &keyToBeUnwrapped, spec.usage, spec.attributes,
66						 spec.label, &compositeRcc(),
67						 unwrappedKey.makeNewKey(attachment()), &data));
68	unwrappedKey->activate();
69
70	return unwrappedKey;
71}
72
73void
74UnwrapKey::operator () (const CssmKey &keyToBeUnwrapped, const KeySpec &spec,
75						CssmKey &unwrappedKey)
76{
77	CssmData data(reinterpret_cast<uint8 *>(1), 0);
78	check(CSSM_UnwrapKey(handle(), NULL, &keyToBeUnwrapped, spec.usage,
79						 spec.attributes, spec.label, &compositeRcc(),
80						 &unwrappedKey, &data));
81}
82
83Key
84UnwrapKey::operator () (const CssmKey &keyToBeUnwrapped, const KeySpec &spec,
85						Key &optionalPublicKey)
86{
87	CssmData data(reinterpret_cast<uint8 *>(1), 0);
88	Key unwrappedKey;
89	check(CSSM_UnwrapKey(handle(), optionalPublicKey,
90						 &keyToBeUnwrapped, spec.usage, spec.attributes,
91						 spec.label, &compositeRcc(),
92						 unwrappedKey.makeNewKey(attachment()), &data));
93
94	unwrappedKey->activate();
95
96	return unwrappedKey;
97}
98
99void
100UnwrapKey::operator () (const CssmKey &keyToBeUnwrapped, const KeySpec &spec,
101						CssmKey &unwrappedKey,
102						const CssmKey *optionalPublicKey)
103{
104	CssmData data(reinterpret_cast<uint8 *>(1), 0);
105	check(CSSM_UnwrapKey(handle(), optionalPublicKey, &keyToBeUnwrapped,
106						 spec.usage, spec.attributes, spec.label,
107						 &compositeRcc(), &unwrappedKey, &data));
108}
109
110
111Key
112UnwrapKey::operator () (const CssmKey &keyToBeUnwrapped, const KeySpec &spec,
113						CssmData *descriptiveData)
114{
115	Key unwrappedKey;
116	check(CSSM_UnwrapKey(handle(), NULL, &keyToBeUnwrapped, spec.usage,
117						 spec.attributes, spec.label, &compositeRcc(),
118						 unwrappedKey.makeNewKey(attachment()),
119						 descriptiveData));
120	unwrappedKey->activate();
121
122	return unwrappedKey;
123}
124
125void
126UnwrapKey::operator () (const CssmKey &keyToBeUnwrapped, const KeySpec &spec,
127						CssmKey &unwrappedKey, CssmData *descriptiveData)
128{
129	check(CSSM_UnwrapKey(handle(), NULL, &keyToBeUnwrapped, spec.usage,
130						 spec.attributes, spec.label, &compositeRcc(),
131						 &unwrappedKey, descriptiveData));
132}
133
134Key
135UnwrapKey::operator () (const CssmKey &keyToBeUnwrapped, const KeySpec &spec,
136						const Key &optionalPublicKey, CssmData *descriptiveData)
137{
138	Key unwrappedKey;
139	check(CSSM_UnwrapKey(handle(), optionalPublicKey, &keyToBeUnwrapped,
140						 spec.usage, spec.attributes, spec.label,
141						 &compositeRcc(),
142						 unwrappedKey.makeNewKey(attachment()),
143						 descriptiveData));
144	unwrappedKey->activate();
145
146	return unwrappedKey;
147}
148
149void
150UnwrapKey::operator () (const CssmKey &keyToBeUnwrapped, const KeySpec &spec,
151						CssmKey &unwrappedKey, CssmData *descriptiveData,
152						const CssmKey *optionalPublicKey)
153{
154	check(CSSM_UnwrapKey(handle(), optionalPublicKey, &keyToBeUnwrapped,
155						 spec.usage, spec.attributes, spec.label,
156						 &compositeRcc(), &unwrappedKey,
157						 descriptiveData));
158}
159
160
161void DeriveKey::activate()
162{
163	if (!mActive)
164	{
165        check(CSSM_CSP_CreateDeriveKeyContext(attachment()->handle(), mAlgorithm,
166            mTargetType, mKeySize, mCred, mKey, mIterationCount, mSalt, mSeed, &mHandle));
167		mActive = true;
168    }
169}
170
171
172Key
173DeriveKey::operator () (CssmData *param, const KeySpec &spec)
174{
175	Key derivedKey;
176	check(CSSM_DeriveKey(handle(), param, spec.usage, spec.attributes,
177						 spec.label, &compositeRcc(),
178						 derivedKey.makeNewKey(attachment())));
179	derivedKey->activate();
180
181	return derivedKey;
182}
183
184void
185DeriveKey::operator () (CssmData *param, const KeySpec &spec,
186						CssmKey &derivedKey)
187{
188	check(CSSM_DeriveKey(handle(), param, spec.usage, spec.attributes,
189						 spec.label, &compositeRcc(), &derivedKey));
190}
191
192} // end namespace CssmClient
193} // end namespace Security
194