]> git.saurik.com Git - apple/security.git/blob - CdsaUtils/cuCdsaUtils.h
Security-179.tar.gz
[apple/security.git] / CdsaUtils / cuCdsaUtils.h
1 /*
2 * Copyright (c) 2001-2003 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
7 * obtain a copy of the License at http://www.apple.com/publicsource and
8 * read it before 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
12 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
13 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
14 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
15 * Please see the License for the specific language governing rights and
16 * limitations under the License.
17 */
18
19 /*
20 File: cuCdsaUtils.h
21
22 Description: common CDSA access utilities
23
24 Author: dmitch
25 */
26
27 #ifndef _COMMON_CDSA_UTILS_H_
28 #define _COMMON_CDSA_UTILS_H_
29
30 #include <Security/cssm.h>
31 #include <Security/SecKeychain.h>
32
33 #ifdef __cplusplus
34 extern "C" {
35 #endif
36
37 /* common memory allocators shared by app and CSSM */
38 extern void * cuAppMalloc (uint32 size, void *allocRef);
39 extern void cuAppFree (void *mem_ptr, void *allocRef);
40 extern void * cuAppRealloc (void *ptr, uint32 size, void *allocRef);
41 extern void * cuAppCalloc (uint32 num, uint32 size, void *allocRef);
42
43 #define APP_MALLOC(s) cuAppMalloc(s, NULL)
44 #define APP_FREE(p) cuAppFree(p, NULL)
45 #define APP_REALLOC(p, s) cuAppRealloc(p, s, NULL)
46 #define APP_CALLOC(n, s) cuAppRealloc(n, s, NULL)
47
48 extern CSSM_BOOL cuCompareCssmData(
49 const CSSM_DATA *d1,
50 const CSSM_DATA *d2);
51
52 /* OID flavor of same, which will break when an OID is not a CSSM_DATA */
53 #define cuCompareOid(o1, o2) cuCompareCssmData(o1, o2)
54
55 void cuPrintError(const char *op, CSSM_RETURN err);
56
57 /* Init CSSM; returns CSSM_FALSE on error. Reusable. */
58 extern CSSM_BOOL cuCssmStartup();
59
60 /* Attach to CSP. Returns zero on error. */
61 extern CSSM_CSP_HANDLE cuCspStartup(
62 CSSM_BOOL bareCsp); // true ==> CSP, false ==> CSP/DL
63
64 /* Attach to DL side of CSPDL. */
65 extern CSSM_DL_HANDLE cuDlStartup();
66
67 /* Attach to CL, TP */
68 extern CSSM_CL_HANDLE cuClStartup();
69 extern CSSM_TP_HANDLE cuTpStartup();
70
71 /* Open a DB, ensure it's empty. */
72 CSSM_DB_HANDLE cuDbStartup(
73 CSSM_DL_HANDLE dlHand, // from dlStartup()
74 const char *dbName);
75
76 /* Attach to existing DB or create an empty new one. */
77 CSSM_DB_HANDLE cuDbStartupByName(CSSM_DL_HANDLE dlHand,
78 char *dbName,
79 CSSM_BOOL doCreate,
80 CSSM_BOOL quiet);
81
82 /*
83 * Derive symmetric key using PBE.
84 */
85 extern CSSM_RETURN cuCspDeriveKey(CSSM_CSP_HANDLE cspHand,
86 uint32 keyAlg, // CSSM_ALGID_RC5, etc.
87 const char *keyLabel,
88 unsigned keyLabelLen,
89 uint32 keyUsage, // CSSM_KEYUSE_ENCRYPT, etc.
90 uint32 keySizeInBits,
91 CSSM_DATA_PTR password, // in PKCS-5 lingo
92 CSSM_DATA_PTR salt, // ditto
93 uint32 iterationCnt, // ditto
94 CSSM_KEY_PTR key);
95
96 /*
97 * Generate key pair of arbitrary algorithm.
98 */
99 extern CSSM_RETURN cuCspGenKeyPair(CSSM_CSP_HANDLE cspHand,
100 CSSM_DL_DB_HANDLE *dlDbHand, // optional
101 uint32 algorithm,
102 const char *keyLabel,
103 unsigned keyLabelLen,
104 uint32 keySize, // in bits
105 CSSM_KEY_PTR pubKey, // mallocd by caller
106 CSSM_KEYUSE pubKeyUsage, // CSSM_KEYUSE_ENCRYPT, etc.
107 CSSM_KEYATTR_FLAGS pubAttrs, // CSSM_KEYATTR_EXTRACTABLE, etc.
108 CSSM_KEY_PTR privKey, // mallocd by caller
109 CSSM_KEYUSE privKeyUsage, // CSSM_KEYUSE_DECRYPT, etc.
110 CSSM_KEYATTR_FLAGS privAttrs); // CSSM_KEYATTR_EXTRACTABLE, etc.
111
112 /* Convert a reference key to a raw key. */
113 CSSM_RETURN cuRefKeyToRaw(CSSM_CSP_HANDLE cspHand,
114 const CSSM_KEY *refKey,
115 CSSM_KEY_PTR rawKey); // RETURNED
116
117 /*
118 * Add a certificate to a keychain.
119 */
120 CSSM_RETURN cuAddCertToKC(
121 SecKeychainRef keychain,
122 const CSSM_DATA *cert,
123 CSSM_CERT_TYPE certType,
124 CSSM_CERT_ENCODING certEncoding,
125 const char *printName, // C string
126 const CSSM_DATA *keyLabel); // ??
127
128 /*
129 * Convert a CSSM_DATA_PTR, referring to a DER-encoded int, to an
130 * unsigned.
131 */
132 unsigned cuDER_ToInt(
133 const CSSM_DATA *DER_Data);
134
135 /*
136 * Verify a CRL against system anchors and intermediate certs.
137 */
138 CSSM_RETURN cuCrlVerify(
139 CSSM_TP_HANDLE tpHand,
140 CSSM_CL_HANDLE clHand,
141 CSSM_CSP_HANDLE cspHand,
142 const CSSM_DATA *crlData,
143 CSSM_DL_DB_HANDLE_PTR certKeychain, // intermediate certs
144 const CSSM_DATA *anchors,
145 uint32 anchorCount);
146
147 #ifdef __cplusplus
148 }
149 #endif
150
151 #endif /* _COMMON_CDSA_UTILS_H_ */