2 * Copyright (c) 2001,2003-2011 Apple Inc. All Rights Reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
23 * srCdsaUtils.h -- common CDSA access utilities
26 #ifndef _COMMON_CDSA_UTILS_H_
27 #define _COMMON_CDSA_UTILS_H_
29 #include <Security/cssm.h>
30 #include <Security/SecKeychain.h>
31 #include <CoreFoundation/CFString.h>
37 /* common memory allocators shared by app and CSSM */
38 extern void * srAppMalloc (CSSM_SIZE size
, void *allocRef
);
39 extern void srAppFree (void *mem_ptr
, void *allocRef
);
40 extern void * srAppRealloc (void *ptr
, CSSM_SIZE size
, void *allocRef
);
41 extern void * srAppCalloc (uint32 num
, CSSM_SIZE size
, void *allocRef
);
43 #define APP_MALLOC(s) srAppMalloc(s, NULL)
44 #define APP_FREE(p) srAppFree(p, NULL)
45 #define APP_REALLOC(p, s) srAppRealloc(p, s, NULL)
46 #define APP_CALLOC(n, s) srAppRealloc(n, s, NULL)
48 extern CSSM_BOOL
srCompareCssmData(
52 /* OID flavor of same, which will break when an OID is not a CSSM_DATA */
53 #define srCompareOid(o1, o2) srCompareCssmData(o1, o2)
55 void srPrintError(const char *op
, CSSM_RETURN err
);
57 /* Init CSSM; returns CSSM_FALSE on error. Reusable. */
58 extern CSSM_BOOL
srCssmStartup();
60 /* Attach to CSP. Returns zero on error. */
61 extern CSSM_CSP_HANDLE
srCspStartup(
62 CSSM_BOOL bareCsp
); // true ==> CSP, false ==> CSP/DL
64 /* Attach to DL side of CSPDL. */
65 extern CSSM_DL_HANDLE
srDlStartup();
67 /* Attach to CL, TP */
68 extern CSSM_CL_HANDLE
srClStartup();
69 extern CSSM_TP_HANDLE
srTpStartup();
72 * Derive symmetric key using PBE.
74 extern CSSM_RETURN
srCspDeriveKey(CSSM_CSP_HANDLE cspHand
,
75 uint32 keyAlg
, // CSSM_ALGID_RC5, etc.
78 uint32 keyUsage
, // CSSM_KEYUSE_ENCRYPT, etc.
80 CSSM_DATA_PTR password
, // in PKCS-5 lingo
81 CSSM_DATA_PTR salt
, // ditto
82 uint32 iterationCnt
, // ditto
86 * Generate key pair of arbitrary algorithm.
88 extern CSSM_RETURN
srCspGenKeyPair(CSSM_CSP_HANDLE cspHand
,
89 CSSM_DL_DB_HANDLE
*dlDbHand
, // optional
93 uint32 keySize
, // in bits
94 CSSM_KEY_PTR pubKey
, // mallocd by caller
95 CSSM_KEYUSE pubKeyUsage
, // CSSM_KEYUSE_ENCRYPT, etc.
96 CSSM_KEYATTR_FLAGS pubAttrs
, // CSSM_KEYATTR_EXTRACTABLE, etc.
97 CSSM_KEY_PTR privKey
, // mallocd by caller
98 CSSM_KEYUSE privKeyUsage
, // CSSM_KEYUSE_DECRYPT, etc.
99 CSSM_KEYATTR_FLAGS privAttrs
); // CSSM_KEYATTR_EXTRACTABLE, etc.
101 /* Convert a reference key to a raw key. */
102 CSSM_RETURN
srRefKeyToRaw(CSSM_CSP_HANDLE cspHand
,
103 const CSSM_KEY
*refKey
,
104 CSSM_KEY_PTR rawKey
); // RETURNED
107 * Add a certificate to a keychain.
109 CSSM_RETURN
srAddCertToKC(
110 SecKeychainRef keychain
,
111 const CSSM_DATA
*cert
,
112 CSSM_CERT_TYPE certType
,
113 CSSM_CERT_ENCODING certEncoding
,
114 const char *printName
, // C string
115 const CSSM_DATA
*keyLabel
); // ??
118 * Convert a CSSM_DATA_PTR, referring to a DER-encoded int, to an
121 unsigned srDER_ToInt(
122 const CSSM_DATA
*DER_Data
);
124 char *srCfStrToCString(
131 #endif /* _COMMON_CDSA_UTILS_H_ */