]> git.saurik.com Git - apple/security.git/blame - Keychain/SecKey.cpp
Security-54.1.3.tar.gz
[apple/security.git] / Keychain / SecKey.cpp
CommitLineData
bac41a7b 1/*
29654253 2 * Copyright (c) 2002 Apple Computer, Inc. All Rights Reserved.
bac41a7b
A
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
29654253 18#include <Security/SecKey.h>
bac41a7b 19
29654253 20#include "SecBridge.h"
bac41a7b 21
5a719ac8
A
22#include <Security/Access.h>
23#include <Security/Keychains.h>
24#include <Security/KeyItem.h>
25
29654253
A
26CFTypeID
27SecKeyGetTypeID(void)
28{
29 BEGIN_SECAPI
bac41a7b 30
29654253 31 return gTypes().keyItem.typeId;
bac41a7b 32
29654253
A
33 END_SECAPI1(_kCFRuntimeNotATypeID)
34}
bac41a7b 35
29654253
A
36OSStatus
37SecKeyCreatePair(
5a719ac8 38 SecKeychainRef keychainRef,
29654253 39 CSSM_ALGORITHMS algorithm,
5a719ac8
A
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)
29654253
A
49{
50 BEGIN_SECAPI
51
5a719ac8
A
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);
29654253
A
73
74 END_SECAPI
75}
76
77OSStatus
78SecKeyGetCSSMKey(SecKeyRef key, const CSSM_KEY **cssmKey)
79{
80 BEGIN_SECAPI
81
82 Required(cssmKey) = &gTypes().keyItem.required(key)->cssmKey();
83
84 END_SECAPI
85}
5a719ac8
A
86
87
88//
89// Private APIs
90//
91
92OSStatus
93SecKeyGetCredentials(
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
107OSStatus
108SecKeyImportPair(
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}