]> git.saurik.com Git - apple/security.git/blob - Keychain/srCdsaUtils.h
Security-179.tar.gz
[apple/security.git] / Keychain / srCdsaUtils.h
1 /*
2 File: srCdsaUtils.h
3
4 Description: common CDSA access utilities
5
6 Author: dmitch
7
8 Copyright: © Copyright 2001 Apple Computer, Inc. All rights reserved.
9
10 Disclaimer: IMPORTANT: This Apple software is supplied to you by Apple
11 Computer, Inc. ("Apple") in consideration of your agreement to
12 the following terms, and your use, installation, modification
13 or redistribution of this Apple software constitutes acceptance
14 of these terms. If you do not agree with these terms, please
15 do not use, install, modify or redistribute this Apple software.
16
17 In consideration of your agreement to abide by the following
18 terms, and subject to these terms, Apple grants you a personal,
19 non-exclusive license, under Apple's copyrights in this
20 original Apple software (the "Apple Software"), to use,
21 reproduce, modify and redistribute the Apple Software, with
22 or without modifications, in source and/or binary forms;
23 provided that if you redistribute the Apple Software in
24 its entirety and without modifications, you must retain
25 this notice and the following text and disclaimers in all
26 such redistributions of the Apple Software. Neither the
27 name, trademarks, service marks or logos of Apple Computer,
28 Inc. may be used to endorse or promote products derived from the
29 Apple Software without specific prior written permission from
30 Apple. Except as expressly stated in this notice, no other
31 rights or licenses, express or implied, are granted by Apple
32 herein, including but not limited to any patent rights that
33 may be infringed by your derivative works or by other works
34 in which the Apple Software may be incorporated.
35
36 The Apple Software is provided by Apple on an "AS IS" basis.
37 APPLE MAKES NO WARRANTIES, EXPRESS OR IMPLIED, INCLUDING
38 WITHOUT LIMITATION THE IMPLIED WARRANTIES OF NON-INFRINGEMENT,
39 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE,
40 REGARDING THE APPLE SOFTWARE OR ITS USE AND OPERATION ALONE
41 OR IN COMBINATION WITH YOUR PRODUCTS.
42
43 IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT,
44 INCIDENTAL OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
45 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, MODIFICATION
48 AND/OR DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED
49 AND WHETHER UNDER THEORY OF CONTRACT, TORT (INCLUDING
50 NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE
51 HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
52 */
53
54 #ifndef _COMMON_CDSA_UTILS_H_
55 #define _COMMON_CDSA_UTILS_H_
56
57 #include <Security/cssm.h>
58 #include <Security/SecKeychain.h>
59 #include <CoreFoundation/CFString.h>
60
61 #ifdef __cplusplus
62 extern "C" {
63 #endif
64
65 /* common memory allocators shared by app and CSSM */
66 extern void * srAppMalloc (uint32 size, void *allocRef);
67 extern void srAppFree (void *mem_ptr, void *allocRef);
68 extern void * srAppRealloc (void *ptr, uint32 size, void *allocRef);
69 extern void * srAppCalloc (uint32 num, uint32 size, void *allocRef);
70
71 #define APP_MALLOC(s) srAppMalloc(s, NULL)
72 #define APP_FREE(p) srAppFree(p, NULL)
73 #define APP_REALLOC(p, s) srAppRealloc(p, s, NULL)
74 #define APP_CALLOC(n, s) srAppRealloc(n, s, NULL)
75
76 extern CSSM_BOOL srCompareCssmData(
77 const CSSM_DATA *d1,
78 const CSSM_DATA *d2);
79
80 /* OID flavor of same, which will break when an OID is not a CSSM_DATA */
81 #define srCompareOid(o1, o2) srCompareCssmData(o1, o2)
82
83 void srPrintError(char *op, CSSM_RETURN err);
84
85 /* Init CSSM; returns CSSM_FALSE on error. Reusable. */
86 extern CSSM_BOOL srCssmStartup();
87
88 /* Attach to CSP. Returns zero on error. */
89 extern CSSM_CSP_HANDLE srCspStartup(
90 CSSM_BOOL bareCsp); // true ==> CSP, false ==> CSP/DL
91
92 /* Attach to DL side of CSPDL. */
93 extern CSSM_DL_HANDLE srDlStartup();
94
95 /* Attach to CL, TP */
96 extern CSSM_CL_HANDLE srClStartup();
97 extern CSSM_TP_HANDLE srTpStartup();
98
99 /*
100 * Derive symmetric key using PBE.
101 */
102 extern CSSM_RETURN srCspDeriveKey(CSSM_CSP_HANDLE cspHand,
103 uint32 keyAlg, // CSSM_ALGID_RC5, etc.
104 const char *keyLabel,
105 unsigned keyLabelLen,
106 uint32 keyUsage, // CSSM_KEYUSE_ENCRYPT, etc.
107 uint32 keySizeInBits,
108 CSSM_DATA_PTR password, // in PKCS-5 lingo
109 CSSM_DATA_PTR salt, // ditto
110 uint32 iterationCnt, // ditto
111 CSSM_KEY_PTR key);
112
113 /*
114 * Generate key pair of arbitrary algorithm.
115 */
116 extern CSSM_RETURN srCspGenKeyPair(CSSM_CSP_HANDLE cspHand,
117 CSSM_DL_DB_HANDLE *dlDbHand, // optional
118 uint32 algorithm,
119 const char *keyLabel,
120 unsigned keyLabelLen,
121 uint32 keySize, // in bits
122 CSSM_KEY_PTR pubKey, // mallocd by caller
123 CSSM_KEYUSE pubKeyUsage, // CSSM_KEYUSE_ENCRYPT, etc.
124 CSSM_KEYATTR_FLAGS pubAttrs, // CSSM_KEYATTR_EXTRACTABLE, etc.
125 CSSM_KEY_PTR privKey, // mallocd by caller
126 CSSM_KEYUSE privKeyUsage, // CSSM_KEYUSE_DECRYPT, etc.
127 CSSM_KEYATTR_FLAGS privAttrs); // CSSM_KEYATTR_EXTRACTABLE, etc.
128
129 /* Convert a reference key to a raw key. */
130 CSSM_RETURN srRefKeyToRaw(CSSM_CSP_HANDLE cspHand,
131 const CSSM_KEY *refKey,
132 CSSM_KEY_PTR rawKey); // RETURNED
133
134 /*
135 * Add a certificate to a keychain.
136 */
137 CSSM_RETURN srAddCertToKC(
138 SecKeychainRef keychain,
139 const CSSM_DATA *cert,
140 CSSM_CERT_TYPE certType,
141 CSSM_CERT_ENCODING certEncoding,
142 const char *printName, // C string
143 const CSSM_DATA *keyLabel); // ??
144
145 /*
146 * Convert a CSSM_DATA_PTR, referring to a DER-encoded int, to an
147 * unsigned.
148 */
149 unsigned srDER_ToInt(
150 const CSSM_DATA *DER_Data);
151
152 char *srCfStrToCString(
153 CFStringRef cfStr);
154
155 #ifdef __cplusplus
156 }
157 #endif
158
159 #endif /* _COMMON_CDSA_UTILS_H_ */