4 Description: common CDSA access utilities
8 Copyright: © Copyright 2001 Apple Computer, Inc. All rights reserved.
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.
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.
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.
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.
54 #ifndef _COMMON_CDSA_UTILS_H_
55 #define _COMMON_CDSA_UTILS_H_
57 #include <Security/cssm.h>
58 #include <Security/SecKeychain.h>
59 #include <CoreFoundation/CFString.h>
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
);
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)
76 extern CSSM_BOOL
srCompareCssmData(
80 /* OID flavor of same, which will break when an OID is not a CSSM_DATA */
81 #define srCompareOid(o1, o2) srCompareCssmData(o1, o2)
83 void srPrintError(char *op
, CSSM_RETURN err
);
85 /* Init CSSM; returns CSSM_FALSE on error. Reusable. */
86 extern CSSM_BOOL
srCssmStartup();
88 /* Attach to CSP. Returns zero on error. */
89 extern CSSM_CSP_HANDLE
srCspStartup(
90 CSSM_BOOL bareCsp
); // true ==> CSP, false ==> CSP/DL
92 /* Attach to DL side of CSPDL. */
93 extern CSSM_DL_HANDLE
srDlStartup();
95 /* Attach to CL, TP */
96 extern CSSM_CL_HANDLE
srClStartup();
97 extern CSSM_TP_HANDLE
srTpStartup();
100 * Derive symmetric key using PBE.
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
114 * Generate key pair of arbitrary algorithm.
116 extern CSSM_RETURN
srCspGenKeyPair(CSSM_CSP_HANDLE cspHand
,
117 CSSM_DL_DB_HANDLE
*dlDbHand
, // optional
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.
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
135 * Add a certificate to a keychain.
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
); // ??
146 * Convert a CSSM_DATA_PTR, referring to a DER-encoded int, to an
149 unsigned srDER_ToInt(
150 const CSSM_DATA
*DER_Data
);
152 char *srCfStrToCString(
159 #endif /* _COMMON_CDSA_UTILS_H_ */