File: loadman.c

package info (click to toggle)
tcltrf 2.1.4-dfsg3-8
  • links: PTS
  • area: main
  • in suites: sid, trixie
  • size: 9,656 kB
  • sloc: ansic: 73,139; sh: 3,155; tcl: 1,343; makefile: 182; exp: 22
file content (281 lines) | stat: -rw-r--r-- 6,392 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
/*
 * loadman.c --
 *
 *	Loader for various crypto libraries.
 *
 * Copyright (c) 1997 Andreas Kupries (andreas_kupries@users.sourceforge.net)
 * All rights reserved.
 *
 * CVS: $Id: loadman.c,v 1.12 2008/12/11 19:04:25 andreas_kupries Exp $
 */

#include "loadman.h"

/*
 * Allow the Makefile to define this value
 */

#ifndef SSL_LIB_NAME
#    ifdef __WIN32__
#    define SSL_LIB_NAME "crypto32.dll"
#    endif /* __WIN32__ */
#    ifdef __APPLE__
#    define SSL_LIB_NAME "libcrypto.dylib"
#    endif /* __APPLE__ */
#    ifndef SSL_LIB_NAME
#    define SSL_LIB_NAME "libcrypto.so"
#    endif /* SSL_LIB_NAME */
#endif /* SSL_LIB_NAME */


#ifndef CRYPT_LIB_NAME
#    ifdef __WIN32__
#    define CRYPT_LIB_NAME "crypt.dll"
#    endif /* __WIN32__ */
#    ifdef __APPLE__
#    define CRYPT_LIB_NAME "libcrypto.dylib"
#    endif /* __APPLE__ */
#    ifndef CRYPT_LIB_NAME
#    define CRYPT_LIB_NAME "libcrypt.so"
#    endif /* SSL_LIB_NAME */
#endif /* SSL_LIB_NAME */


typedef struct SslLibFunctions {
  void* handle;
  /* MD2 */
  void (* md2_init)        _ANSI_ARGS_ ((MD2_CTX* c));
  void (* md2_update)      _ANSI_ARGS_ ((MD2_CTX* c, unsigned char* data, unsigned long length));
  void (* md2_final)       _ANSI_ARGS_ ((unsigned char* digest, MD2_CTX* c));
  /* SHA1 */
  void (* sha1_init)        _ANSI_ARGS_ ((SHA_CTX* c));
  void (* sha1_update)      _ANSI_ARGS_ ((SHA_CTX* c, unsigned char* data, unsigned long length));
  void (* sha1_final)       _ANSI_ARGS_ ((unsigned char* digest, SHA_CTX* c));
} sslLibFunctions;


static char* ssl_symbols [] = {
  /* md2 */
  "MD2_Init",
  "MD2_Update",
  "MD2_Final",
  /* sha1 */
  "SHA1_Init",
  "SHA1_Update",
  "SHA1_Final",
  /* -- */
  (char *) NULL,
};

#ifndef MD5_STATIC_BUILD
static char* crypt_symbols [] = {
  /* md5 */
  "MD5_Init",
  "MD5_Update",
  "MD5_Final",
  "crypt",
  /* -- */
  (char *) NULL,
};
#endif



/*
 * Global variables containing the vectors to DES, MD2, ...
 */

md2Functions  md2f  = {0}; /* THREADING: serialize initialization */
sha1Functions sha1f = {0}; /* THREADING: serialize initialization */
md5Functions  md5f  = {0}; /* THREADING: serialize initialization */

#ifdef MD5_STATIC_BUILD
#include "../md5-crypt/md5.h" /* THREADING: import of one constant var, read-only => safe */
extern char *md5_crypt(const char *key, const char *salt);
static void md5_update(MD5_CTX *c, unsigned char* data, unsigned long length)
{
    md5_process_bytes(data, length, c);
}
static void md5_final(unsigned char* digest, MD5_CTX* c) {
     md5_finish_ctx(c, digest);
}
#endif

/*
 * Internal global var's, contains all vectors loaded from SSL's 'cryptlib'.
 *                        contains all vectors loaded from 'libdes' library.
 */

static sslLibFunctions ssl; /* THREADING: serialize initialization */

/*
 *------------------------------------------------------*
 *
 *	TrfLoadMD2 --
 *
 *	------------------------------------------------*
 *	Makes MD2 functionality available.
 *	------------------------------------------------*
 *
 *	Sideeffects:
 *		Loads the required shared library and
 *		makes the addresses of MD2 functionality
 *		available. In case of failure an error
 *		message is left in the result area of
 *		the specified interpreter.
 *
 *	Result:
 *		A standard tcl error code.
 *
 *------------------------------------------------------*
 */

int
TrfLoadMD2 (interp)
    Tcl_Interp* interp;
{
#ifdef SSL_STATIC_BUILD
  md2f.loaded = 1;
  md2f.init   = MD2_Init;
  md2f.update = MD2_Update;
  md2f.final  = MD2_Final;
  return TCL_OK;
#else
  int res;

  TrfLock; /* THREADING: serialize initialization */

  if (md2f.loaded) {
    TrfUnlock;
    return TCL_OK;
  }

  res = Trf_LoadLibrary (interp, SSL_LIB_NAME, (VOID**) &ssl, ssl_symbols, 0);

  if ((res == TCL_OK) &&
      (ssl.md2_init   != NULL) &&
      (ssl.md2_update != NULL) &&
      (ssl.md2_final  != NULL)) {

    md2f.loaded = 1;
    md2f.init   = ssl.md2_init;
    md2f.update = ssl.md2_update;
    md2f.final  = ssl.md2_final;

    TrfUnlock;
    return TCL_OK;
  }

  TrfUnlock;
  return TCL_ERROR;
#endif
}

/*
 *------------------------------------------------------*
 *
 *	TrfLoadMD5 --
 *
 *	------------------------------------------------*
 *	Makes MD5 functionality available.
 *	------------------------------------------------*
 *
 *	Sideeffects:
 *		Loads the required shared library and
 *		makes the addresses of MD5 functionality
 *		available. In case of failure an error
 *		message is left in the result area of
 *		the specified interpreter.
 *
 *	Result:
 *		A standard tcl error code.
 *
 *------------------------------------------------------*
 */
int
TrfLoadMD5 (interp)
    Tcl_Interp* interp;
{
#ifdef MD5_STATIC_BUILD
  md5f.loaded = 1;
  md5f.init   = md5_init_ctx;
  md5f.update = md5_update;
  md5f.final  = md5_final;
  md5f.crypt  = md5_crypt;
  return TCL_OK;
#else
  int res;

  TrfLock; /* THREADING: serialize initialization */
  res = Trf_LoadLibrary (interp, CRYPT_LIB_NAME, (VOID**) &md5f,
			 crypt_symbols, 0);
  TrfUnlock;

  return res;
#endif
}

/*
 *------------------------------------------------------*
 *
 *	TrfLoadSHA1 --
 *
 *	------------------------------------------------*
 *	Makes SHA-1 functionality available.
 *	------------------------------------------------*
 *
 *	Sideeffects:
 *		Loads the required shared library and
 *		makes the addresses of SHA-1 functionality
 *		available. In case of failure an error
 *		message is left in the result area of
 *		the specified interpreter.
 *		
 *
 *	Result:
 *		A standard tcl error code.
 *
 *------------------------------------------------------*
 */

int
TrfLoadSHA1 (interp)
    Tcl_Interp* interp;
{
#ifdef SSL_STATIC_BUILD
  sha1f.loaded = 1;
  sha1f.init   = SHA1_Init;
  sha1f.update = SHA1_Update;
  sha1f.final  = SHA1_Final;
  return TCL_OK;
#else
  int res;

  TrfLock; /* THREADING: serialize initialization */

  if (sha1f.loaded) {
    TrfUnlock;
    return TCL_OK;
  }

  res = Trf_LoadLibrary (interp, SSL_LIB_NAME, (VOID**) &ssl, ssl_symbols, 0);

  if ((res == TCL_OK) &&
      (ssl.sha1_init   != NULL) &&
      (ssl.sha1_update != NULL) &&
      (ssl.sha1_final  != NULL)) {

    sha1f.loaded = 1;
    sha1f.init   = ssl.sha1_init;
    sha1f.update = ssl.sha1_update;
    sha1f.final  = ssl.sha1_final;

    TrfUnlock;
    return TCL_OK;
  }

  TrfUnlock;
  return TCL_ERROR;
#endif
}