2  * Copyright (c) 2001-2003 Apple Computer, Inc. All Rights Reserved. 
   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. 
  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. 
  22         Description: common CDSA access utilities 
  27 #ifndef _COMMON_CDSA_UTILS_H_ 
  28 #define _COMMON_CDSA_UTILS_H_ 
  30 #include <Security/cssm.h> 
  31 #include <Security/SecKeychain.h> 
  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
); 
  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) 
  48 extern CSSM_BOOL 
cuCompareCssmData( 
  52 /* OID flavor of same, which will break when an OID is not a CSSM_DATA */ 
  53 #define cuCompareOid(o1, o2)    cuCompareCssmData(o1, o2) 
  55 void cuPrintError(const char *op
, CSSM_RETURN err
); 
  57 /* Init CSSM; returns CSSM_FALSE on error. Reusable. */ 
  58 extern CSSM_BOOL 
cuCssmStartup(); 
  60 /* Attach to CSP. Returns zero on error. */ 
  61 extern CSSM_CSP_HANDLE 
cuCspStartup( 
  62         CSSM_BOOL bareCsp
);                                     // true ==> CSP, false ==> CSP/DL 
  64 /* Attach to DL side of CSPDL. */ 
  65 extern CSSM_DL_HANDLE 
cuDlStartup(); 
  67 /* Attach to CL, TP */ 
  68 extern CSSM_CL_HANDLE 
cuClStartup(); 
  69 extern CSSM_TP_HANDLE 
cuTpStartup(); 
  71 /* Open a DB, ensure it's empty. */ 
  72 CSSM_DB_HANDLE 
cuDbStartup( 
  73         CSSM_DL_HANDLE          dlHand
,                 // from dlStartup() 
  76 /* Attach to existing DB or create an empty new one. */ 
  77 CSSM_DB_HANDLE 
cuDbStartupByName(CSSM_DL_HANDLE dlHand
, 
  83  * Derive symmetric key using PBE. 
  85 extern CSSM_RETURN 
cuCspDeriveKey(CSSM_CSP_HANDLE cspHand
, 
  86                 uint32                          keyAlg
,                 // CSSM_ALGID_RC5, etc. 
  89                 uint32                          keyUsage
,               // CSSM_KEYUSE_ENCRYPT, etc. 
  91                 CSSM_DATA_PTR           password
,               // in PKCS-5 lingo 
  92                 CSSM_DATA_PTR           salt
,                   // ditto 
  93                 uint32                          iterationCnt
,   // ditto 
  97  * Generate key pair of arbitrary algorithm.  
  99 extern CSSM_RETURN 
cuCspGenKeyPair(CSSM_CSP_HANDLE cspHand
, 
 100         CSSM_DL_DB_HANDLE 
*dlDbHand
,    // optional 
 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.  
 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 
 118  * Add a certificate to a keychain. 
 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
);             // ?? 
 129  * Convert a CSSM_DATA_PTR, referring to a DER-encoded int, to an 
 132 unsigned cuDER_ToInt( 
 133         const CSSM_DATA         
*DER_Data
); 
 136  * Verify a CRL against system anchors and intermediate certs.  
 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
, 
 151 #endif  /* _COMMON_CDSA_UTILS_H_ */