2 * Copyright (c) 2002 Apple Computer, Inc. All Rights Reserved.
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
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.
18 #include <Security/SecKey.h>
19 #include <Security/KeyItem.h>
21 #include "SecBridge.h"
23 #include <Security/Access.h>
24 #include <Security/Keychains.h>
25 #include <Security/KeyItem.h>
32 return gTypes().KeyItem
.typeID
;
34 END_SECAPI1(_kCFRuntimeNotATypeID
)
39 SecKeychainRef keychainRef
,
40 CSSM_ALGORITHMS algorithm
,
42 CSSM_CC_HANDLE contextHandle
,
43 CSSM_KEYUSE publicKeyUsage
,
45 CSSM_KEYUSE privateKeyUsage
,
46 uint32 privateKeyAttr
,
47 SecAccessRef initialAccess
,
48 SecKeyRef
* publicKeyRef
,
49 SecKeyRef
* privateKeyRef
)
53 Keychain keychain
= Keychain::optional(keychainRef
);
54 SecPointer
<Access
> theAccess(initialAccess
? Access::required(initialAccess
) : new Access("<key>"));
55 SecPointer
<KeyItem
> pubItem
, privItem
;
57 KeyItem::createPair(keychain
,
69 // Return the generated keys.
71 *publicKeyRef
= pubItem
->handle();
73 *privateKeyRef
= privItem
->handle();
79 SecKeyGetCSSMKey(SecKeyRef key
, const CSSM_KEY
**cssmKey
)
83 Required(cssmKey
) = KeyItem::required(key
)->key();
94 SecKeyGetCSPHandle(SecKeyRef keyRef
, CSSM_CSP_HANDLE
*cspHandle
)
98 SecPointer
<KeyItem
> keyItem(KeyItem::required(keyRef
));
99 Required(cspHandle
) = keyItem
->csp()->handle();
105 SecKeyGetAlgorithmID(SecKeyRef keyRef
, const CSSM_X509_ALGORITHM_IDENTIFIER
**algid
)
109 SecPointer
<KeyItem
> keyItem(KeyItem::required(keyRef
));
110 Required(algid
) = &keyItem
->algorithmIdentifier();
116 SecKeyGetStrengthInBits(SecKeyRef keyRef
, const CSSM_X509_ALGORITHM_IDENTIFIER
*algid
, unsigned int *strength
)
120 SecPointer
<KeyItem
> keyItem(KeyItem::required(keyRef
));
121 Required(strength
) = keyItem
->strengthInBits(algid
);
127 SecKeyGetCredentials(
129 CSSM_ACL_AUTHORIZATION_TAG operation
,
130 SecCredentialType credentialType
,
131 const CSSM_ACCESS_CREDENTIALS
**outCredentials
)
135 SecPointer
<KeyItem
> keyItem(KeyItem::required(keyRef
));
136 Required(outCredentials
) = keyItem
->getCredentials(operation
, credentialType
);
143 SecKeychainRef keychainRef
,
144 const CSSM_KEY
*publicCssmKey
,
145 const CSSM_KEY
*privateCssmKey
,
146 SecAccessRef initialAccess
,
147 SecKeyRef
* publicKey
,
148 SecKeyRef
* privateKey
)
152 Keychain keychain
= Keychain::optional(keychainRef
);
153 SecPointer
<Access
> theAccess(initialAccess
? Access::required(initialAccess
) : new Access("<key>"));
154 SecPointer
<KeyItem
> pubItem
, privItem
;
156 KeyItem::importPair(keychain
,
157 Required(publicCssmKey
),
158 Required(privateCssmKey
),
163 // Return the generated keys.
165 *publicKey
= pubItem
->handle();
167 *privateKey
= privItem
->handle();
174 SecKeychainRef keychainRef
,
175 CSSM_ALGORITHMS algorithm
,
176 uint32 keySizeInBits
,
177 CSSM_CC_HANDLE contextHandle
,
178 CSSM_KEYUSE keyUsage
,
180 SecAccessRef initialAccess
,
186 SecPointer
<Access
> theAccess
;
189 keychain
= KeychainImpl::required(keychainRef
);
191 theAccess
= Access::required(initialAccess
);
193 KeyItem
*item
= KeyItem::generate(keychain
,
201 // Return the generated key.
203 *keyRef
= item
->handle();
209 OSStatus
SecKeyCreate(const CSSM_KEY
*cssmKey
,
215 CssmClient::CSP
csp(cssmKey
->KeyHeader
.CspId
);
216 CssmClient::Key
key(csp
, *cssmKey
);
217 KeyItem
*item
= new KeyItem(key
);
219 // Return the generated key.
221 *keyRef
= item
->handle();