/ src / security_cdsa_utils.c
security_cdsa_utils.c
 1  /*
 2   * Copyright (c) 2005 Apple Computer, 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  /*
24   *  libresolv.c
25   *  CFNetwork
26   *
27   *  Created by Jeremy Wyld on 9/29/04.
28   *  Copyright 2004 Apple Computer, Inc. All rights reserved.
29   *
30   */
31  
32  #include <security_cdsa_utils/cuEnc64.h>
33  #include <security_cdsa_utils/cuCdsaUtils.h>
34  #include <CFNetwork/CFNetworkInternal.h>
35  #include <sys/errno.h>
36  #include <mach-o/dyld.h>
37  
38  //#ifndef DYNAMICALLY_LOAD_CDSA_UTILS
39  //#define DYNAMICALLY_LOAD_CDSA_UTILS 1
40  //#endif
41  
42  #if DYNAMICALLY_LOAD_CDSA_UTILS
43  
44  static const void* cdsa_utils = NULL;
45  static const char* const kCDSAUtilsPath = "/usr/local/SecurityPieces/Frameworks/security_cdsa_utils.framework/Versions/A/security_cdsa_utils";
46  
47  static CSSM_HANDLE returns_CSSM_HANDLE(void) { return CSSM_INVALID_HANDLE; }
48  static CSSM_RETURN returns_CSSM_RETURN(void) { return -1; }
49  static unsigned char* returns_uchar(void) { return NULL; }
50  
51  #define GET_DYNAMIC_SYMBOL(sym, rettype, arglist, alt)	\
52  static rettype (* sym##_proc)arglist = NULL;	\
53  if (sym##_proc == NULL) {	\
54  	if (cdsa_utils || (cdsa_utils = __CFNetworkLoadFramework(kCDSAUtilsPath)))	\
55  		sym##_proc = (rettype(*)arglist)NSAddressOfSymbol(NSLookupSymbolInImage(cdsa_utils, "_"#sym, NSLOOKUPSYMBOLINIMAGE_OPTION_BIND));	\
56  			if (! sym##_proc) sym##_proc = (rettype(*)arglist)alt;	\
57  }
58  
59  CSSM_CSP_HANDLE
60  cuCspStartup(CSSM_BOOL bareCsp) {
61  	
62      GET_DYNAMIC_SYMBOL(cuCspStartup, CSSM_CSP_HANDLE, (CSSM_BOOL), returns_CSSM_HANDLE);
63      
64      return (*cuCspStartup_proc)(bareCsp);
65  }
66  
67  CSSM_RETURN
68  cuCspDetachUnload(CSSM_CSP_HANDLE cspHand, CSSM_BOOL bareCsp) {
69  	
70      GET_DYNAMIC_SYMBOL(cuCspDetachUnload, CSSM_RETURN, (CSSM_CSP_HANDLE, CSSM_BOOL), returns_CSSM_RETURN);
71      
72      return (*cuCspDetachUnload_proc)(cspHand, bareCsp);
73  }
74  
75  unsigned char*
76  cuEnc64(const unsigned char *inbuf, unsigned inlen, unsigned *outlen) {
77  	
78      GET_DYNAMIC_SYMBOL(cuEnc64, unsigned char*, (const unsigned char*, unsigned, unsigned*), returns_uchar);
79      
80      return (*cuEnc64_proc)(inbuf, inlen, outlen);
81  }
82  
83  unsigned char*
84  cuDec64(const unsigned char *inbuf, unsigned inlen, unsigned *outlen) {
85  	
86      GET_DYNAMIC_SYMBOL(cuDec64, unsigned char*, (const unsigned char*, unsigned, unsigned*), returns_uchar);
87      
88      return (*cuDec64_proc)(inbuf, inlen, outlen);
89  }
90  
91  #undef GET_DYNAMIC_SYMBOL
92  #endif	/* DYNAMICALLY_LOAD_CDSA_UTILS */