]> git.saurik.com Git - apple/security.git/blob - Keychain/SecKey.cpp
1bba809a57098a460b708fd88936324525b0d132
[apple/security.git] / Keychain / SecKey.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 #include <Security/SecKey.h>
19 #include <Security/KeyItem.h>
20
21 #include "SecBridge.h"
22
23 #include <Security/Access.h>
24 #include <Security/Keychains.h>
25 #include <Security/KeyItem.h>
26
27 CFTypeID
28 SecKeyGetTypeID(void)
29 {
30 BEGIN_SECAPI
31
32 return gTypes().KeyItem.typeID;
33
34 END_SECAPI1(_kCFRuntimeNotATypeID)
35 }
36
37 OSStatus
38 SecKeyCreatePair(
39 SecKeychainRef keychainRef,
40 CSSM_ALGORITHMS algorithm,
41 uint32 keySizeInBits,
42 CSSM_CC_HANDLE contextHandle,
43 CSSM_KEYUSE publicKeyUsage,
44 uint32 publicKeyAttr,
45 CSSM_KEYUSE privateKeyUsage,
46 uint32 privateKeyAttr,
47 SecAccessRef initialAccess,
48 SecKeyRef* publicKeyRef,
49 SecKeyRef* privateKeyRef)
50 {
51 BEGIN_SECAPI
52
53 Keychain keychain = Keychain::optional(keychainRef);
54 SecPointer<Access> theAccess(initialAccess ? Access::required(initialAccess) : new Access("<key>"));
55 SecPointer<KeyItem> pubItem, privItem;
56
57 KeyItem::createPair(keychain,
58 algorithm,
59 keySizeInBits,
60 contextHandle,
61 publicKeyUsage,
62 publicKeyAttr,
63 privateKeyUsage,
64 privateKeyAttr,
65 theAccess,
66 pubItem,
67 privItem);
68
69 // Return the generated keys.
70 if (publicKeyRef)
71 *publicKeyRef = pubItem->handle();
72 if (privateKeyRef)
73 *privateKeyRef = privItem->handle();
74
75 END_SECAPI
76 }
77
78 OSStatus
79 SecKeyGetCSSMKey(SecKeyRef key, const CSSM_KEY **cssmKey)
80 {
81 BEGIN_SECAPI
82
83 Required(cssmKey) = KeyItem::required(key)->key();
84
85 END_SECAPI
86 }
87
88
89 //
90 // Private APIs
91 //
92
93 OSStatus
94 SecKeyGetCSPHandle(SecKeyRef keyRef, CSSM_CSP_HANDLE *cspHandle)
95 {
96 BEGIN_SECAPI
97
98 SecPointer<KeyItem> keyItem(KeyItem::required(keyRef));
99 Required(cspHandle) = keyItem->csp()->handle();
100
101 END_SECAPI
102 }
103
104 OSStatus
105 SecKeyGetAlgorithmID(SecKeyRef keyRef, const CSSM_X509_ALGORITHM_IDENTIFIER **algid)
106 {
107 BEGIN_SECAPI
108
109 SecPointer<KeyItem> keyItem(KeyItem::required(keyRef));
110 Required(algid) = &keyItem->algorithmIdentifier();
111
112 END_SECAPI
113 }
114
115 OSStatus
116 SecKeyGetStrengthInBits(SecKeyRef keyRef, const CSSM_X509_ALGORITHM_IDENTIFIER *algid, unsigned int *strength)
117 {
118 BEGIN_SECAPI
119
120 SecPointer<KeyItem> keyItem(KeyItem::required(keyRef));
121 Required(strength) = keyItem->strengthInBits(algid);
122
123 END_SECAPI
124 }
125
126 OSStatus
127 SecKeyGetCredentials(
128 SecKeyRef keyRef,
129 CSSM_ACL_AUTHORIZATION_TAG operation,
130 SecCredentialType credentialType,
131 const CSSM_ACCESS_CREDENTIALS **outCredentials)
132 {
133 BEGIN_SECAPI
134
135 SecPointer<KeyItem> keyItem(KeyItem::required(keyRef));
136 Required(outCredentials) = keyItem->getCredentials(operation, credentialType);
137
138 END_SECAPI
139 }
140
141 OSStatus
142 SecKeyImportPair(
143 SecKeychainRef keychainRef,
144 const CSSM_KEY *publicCssmKey,
145 const CSSM_KEY *privateCssmKey,
146 SecAccessRef initialAccess,
147 SecKeyRef* publicKey,
148 SecKeyRef* privateKey)
149 {
150 BEGIN_SECAPI
151
152 Keychain keychain = Keychain::optional(keychainRef);
153 SecPointer<Access> theAccess(initialAccess ? Access::required(initialAccess) : new Access("<key>"));
154 SecPointer<KeyItem> pubItem, privItem;
155
156 KeyItem::importPair(keychain,
157 Required(publicCssmKey),
158 Required(privateCssmKey),
159 theAccess,
160 pubItem,
161 privItem);
162
163 // Return the generated keys.
164 if (publicKey)
165 *publicKey = pubItem->handle();
166 if (privateKey)
167 *privateKey = privItem->handle();
168
169 END_SECAPI
170 }
171
172 OSStatus
173 SecKeyGenerate(
174 SecKeychainRef keychainRef,
175 CSSM_ALGORITHMS algorithm,
176 uint32 keySizeInBits,
177 CSSM_CC_HANDLE contextHandle,
178 CSSM_KEYUSE keyUsage,
179 uint32 keyAttr,
180 SecAccessRef initialAccess,
181 SecKeyRef* keyRef)
182 {
183 BEGIN_SECAPI
184
185 Keychain keychain;
186 SecPointer<Access> theAccess;
187
188 if (keychainRef)
189 keychain = KeychainImpl::required(keychainRef);
190 if (initialAccess)
191 theAccess = Access::required(initialAccess);
192
193 KeyItem *item = KeyItem::generate(keychain,
194 algorithm,
195 keySizeInBits,
196 contextHandle,
197 keyUsage,
198 keyAttr,
199 theAccess);
200
201 // Return the generated key.
202 if (keyRef)
203 *keyRef = item->handle();
204
205 END_SECAPI
206 }
207
208
209 OSStatus SecKeyCreate(const CSSM_KEY *cssmKey,
210 SecKeyRef* keyRef)
211 {
212 BEGIN_SECAPI
213
214 Required(cssmKey);
215 CssmClient::CSP csp(cssmKey->KeyHeader.CspId);
216 CssmClient::Key key(csp, *cssmKey);
217 KeyItem *item = new KeyItem(key);
218
219 // Return the generated key.
220 if (keyRef)
221 *keyRef = item->handle();
222
223 END_SECAPI
224 }