]> git.saurik.com Git - apple/security.git/blob - Keychain/SecKey.cpp
f7a4de545e7658e9eb2d81a65e9d37fb56e1353d
[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
20 #include "SecBridge.h"
21
22 #include <Security/Access.h>
23 #include <Security/Keychains.h>
24 #include <Security/KeyItem.h>
25
26 CFTypeID
27 SecKeyGetTypeID(void)
28 {
29 BEGIN_SECAPI
30
31 return gTypes().keyItem.typeId;
32
33 END_SECAPI1(_kCFRuntimeNotATypeID)
34 }
35
36 OSStatus
37 SecKeyCreatePair(
38 SecKeychainRef keychainRef,
39 CSSM_ALGORITHMS algorithm,
40 uint32 keySizeInBits,
41 CSSM_CC_HANDLE contextHandle,
42 CSSM_KEYUSE publicKeyUsage,
43 uint32 publicKeyAttr,
44 CSSM_KEYUSE privateKeyUsage,
45 uint32 privateKeyAttr,
46 SecAccessRef initialAccess,
47 SecKeyRef* publicKeyRef,
48 SecKeyRef* privateKeyRef)
49 {
50 BEGIN_SECAPI
51
52 Keychain keychain = Keychain::optional(keychainRef);
53 RefPointer<Access> theAccess(initialAccess ? gTypes().access.required(initialAccess) : new Access("<key>"));
54 RefPointer<KeyItem> pubItem, privItem;
55
56 KeyItem::createPair(keychain,
57 algorithm,
58 keySizeInBits,
59 contextHandle,
60 publicKeyUsage,
61 publicKeyAttr,
62 privateKeyUsage,
63 privateKeyAttr,
64 theAccess,
65 pubItem,
66 privItem);
67
68 // Return the generated keys.
69 if (publicKeyRef)
70 *publicKeyRef = gTypes().keyItem.handle(*pubItem);
71 if (privateKeyRef)
72 *privateKeyRef = gTypes().keyItem.handle(*privItem);
73
74 END_SECAPI
75 }
76
77 OSStatus
78 SecKeyGetCSSMKey(SecKeyRef key, const CSSM_KEY **cssmKey)
79 {
80 BEGIN_SECAPI
81
82 Required(cssmKey) = &gTypes().keyItem.required(key)->cssmKey();
83
84 END_SECAPI
85 }
86
87
88 //
89 // Private APIs
90 //
91
92 OSStatus
93 SecKeyGetCredentials(
94 SecKeyRef keyRef,
95 CSSM_ACL_AUTHORIZATION_TAG operation,
96 SecCredentialType credentialType,
97 const CSSM_ACCESS_CREDENTIALS **outCredentials)
98 {
99 BEGIN_SECAPI
100
101 RefPointer<KeyItem> keyItem(gTypes().keyItem.required(keyRef));
102 Required(outCredentials) = keyItem->getCredentials(operation, credentialType);
103
104 END_SECAPI
105 }
106
107 OSStatus
108 SecKeyImportPair(
109 SecKeychainRef keychainRef,
110 const CssmKey *publicCssmKey,
111 const CssmKey *privateCssmKey,
112 SecAccessRef initialAccess,
113 SecKeyRef* publicKeyRef,
114 SecKeyRef* privateKeyRef)
115 {
116 BEGIN_SECAPI
117
118 Keychain keychain = Keychain::optional(keychainRef);
119 RefPointer<Access> theAccess(initialAccess ? gTypes().access.required(initialAccess) : new Access("<key>"));
120 RefPointer<KeyItem> pubItem, privItem;
121
122 KeyItem::importPair(keychain,
123 Required(publicCssmKey),
124 Required(privateCssmKey),
125 theAccess,
126 pubItem,
127 privItem);
128
129 // Return the generated keys.
130 if (publicKeyRef)
131 *publicKeyRef = gTypes().keyItem.handle(*pubItem);
132 if (privateKeyRef)
133 *privateKeyRef = gTypes().keyItem.handle(*privItem);
134
135 END_SECAPI
136 }