/ SecurityTool / macOS / srCdsaUtils.h
srCdsaUtils.h
  1  /*
  2   * Copyright (c) 2001,2003-2011 Apple Inc. All Rights Reserved.
  3   *
  4   * @APPLE_LICENSE_HEADER_START@
  5   * 
  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
 11   * file.
 12   * 
 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.
 20   * 
 21   * @APPLE_LICENSE_HEADER_END@
 22   *
 23   * srCdsaUtils.h -- common CDSA access utilities
 24   */
 25  
 26  #ifndef	_COMMON_CDSA_UTILS_H_
 27  #define _COMMON_CDSA_UTILS_H_
 28  
 29  #include <Security/cssm.h>
 30  #include <Security/SecKeychain.h>
 31  #include <CoreFoundation/CFString.h>
 32  
 33  #ifdef	__cplusplus
 34  extern "C" {
 35  #endif
 36  
 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);
 42  
 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)
 47  
 48  extern CSSM_BOOL srCompareCssmData(
 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 srCompareOid(o1, o2)	srCompareCssmData(o1, o2)
 54  
 55  void srPrintError(const char *op, CSSM_RETURN err);
 56  
 57  /* Init CSSM; returns CSSM_FALSE on error. Reusable. */
 58  extern CSSM_BOOL srCssmStartup(void);
 59  
 60  /* Attach to CSP. Returns zero on error. */
 61  extern CSSM_CSP_HANDLE srCspStartup(
 62  	CSSM_BOOL bareCsp);					// true ==> CSP, false ==> CSP/DL
 63  
 64  /* Attach to DL side of CSPDL. */
 65  extern CSSM_DL_HANDLE srDlStartup(void);
 66  
 67  /* Attach to CL, TP */
 68  extern CSSM_CL_HANDLE srClStartup(void);
 69  extern CSSM_TP_HANDLE srTpStartup(void);
 70  
 71  /*
 72   * Derive symmetric key using PBE.
 73   */
 74  extern CSSM_RETURN srCspDeriveKey(CSSM_CSP_HANDLE cspHand,
 75  		uint32				keyAlg,			// CSSM_ALGID_RC5, etc.
 76  		const char 			*keyLabel,
 77  		unsigned 			keyLabelLen,
 78  		uint32 				keyUsage,		// CSSM_KEYUSE_ENCRYPT, etc.
 79  		uint32 				keySizeInBits,
 80  		CSSM_DATA_PTR		password,		// in PKCS-5 lingo
 81  		CSSM_DATA_PTR		salt,			// ditto
 82  		uint32				iterationCnt,	// ditto
 83  		CSSM_KEY_PTR		key);
 84  
 85  /*
 86   * Generate key pair of arbitrary algorithm. 
 87   */
 88  extern CSSM_RETURN srCspGenKeyPair(CSSM_CSP_HANDLE cspHand,
 89  	CSSM_DL_DB_HANDLE *dlDbHand,	// optional
 90  	uint32 algorithm,
 91  	const char *keyLabel,
 92  	unsigned keyLabelLen,
 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. 
100  
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
105  
106  /*
107   * Add a certificate to a keychain.
108   */
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);		// ??
116  
117  /*
118   * Convert a CSSM_DATA_PTR, referring to a DER-encoded int, to an
119   * unsigned.
120   */
121  unsigned srDER_ToInt(
122  	const CSSM_DATA 	*DER_Data);
123  	
124  char *srCfStrToCString(
125  	CFStringRef cfStr);
126  
127  #ifdef	__cplusplus
128  }
129  #endif
130  
131  #endif	/* _COMMON_CDSA_UTILS_H_ */