]> granicus.if.org Git - apache/blob - modules/ssl/ssl_util.c
ssl_util_getmodconfig() and ssl_util_getmodconfig_ssl() show up high
[apache] / modules / ssl / ssl_util.c
1 /*                      _             _
2 **  _ __ ___   ___   __| |    ___ ___| |  mod_ssl
3 ** | '_ ` _ \ / _ \ / _` |   / __/ __| |  Apache Interface to OpenSSL
4 ** | | | | | | (_) | (_| |   \__ \__ \ |  www.modssl.org
5 ** |_| |_| |_|\___/ \__,_|___|___/___/_|  ftp.modssl.org
6 **                      |_____|
7 **  ssl_util.c
8 **  Utility Functions
9 */
10
11 /* ====================================================================
12  * The Apache Software License, Version 1.1
13  *
14  * Copyright (c) 2000-2001 The Apache Software Foundation.  All rights
15  * reserved.
16  *
17  * Redistribution and use in source and binary forms, with or without
18  * modification, are permitted provided that the following conditions
19  * are met:
20  *
21  * 1. Redistributions of source code must retain the above copyright
22  *    notice, this list of conditions and the following disclaimer.
23  *
24  * 2. Redistributions in binary form must reproduce the above copyright
25  *    notice, this list of conditions and the following disclaimer in
26  *    the documentation and/or other materials provided with the
27  *    distribution.
28  *
29  * 3. The end-user documentation included with the redistribution,
30  *    if any, must include the following acknowledgment:
31  *       "This product includes software developed by the
32  *        Apache Software Foundation (http://www.apache.org/)."
33  *    Alternately, this acknowledgment may appear in the software itself,
34  *    if and wherever such third-party acknowledgments normally appear.
35  *
36  * 4. The names "Apache" and "Apache Software Foundation" must
37  *    not be used to endorse or promote products derived from this
38  *    software without prior written permission. For written
39  *    permission, please contact apache@apache.org.
40  *
41  * 5. Products derived from this software may not be called "Apache",
42  *    nor may "Apache" appear in their name, without prior written
43  *    permission of the Apache Software Foundation.
44  *
45  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
46  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
47  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
48  * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
49  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
50  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
51  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
52  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
53  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
54  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
55  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
56  * SUCH DAMAGE.
57  * ====================================================================
58  */
59                              /* ``Every day of my life
60                                   I am forced to add another
61                                   name to the list of people
62                                   who piss me off!''
63                                             -- Calvin          */
64
65 #include "mod_ssl.h"
66 #include "ap_mpm.h"
67 #include "apr_thread_mutex.h"
68
69 /*  _________________________________________________________________
70 **
71 **  Utility Functions
72 **  _________________________________________________________________
73 */
74
75 char *ssl_util_vhostid(apr_pool_t *p, server_rec *s)
76 {
77     char *id;
78     SSLSrvConfigRec *sc;
79     char *host;
80     apr_port_t port;
81
82     host = s->server_hostname;
83     if (s->port != 0)
84         port = s->port;
85     else {
86         sc = mySrvConfig(s);
87         if (sc->bEnabled)
88             port = DEFAULT_HTTPS_PORT;
89         else
90             port = DEFAULT_HTTP_PORT;
91     }
92     id = apr_psprintf(p, "%s:%lu", host, (unsigned long)port);
93     return id;
94 }
95
96 void ssl_util_strupper(char *s)
97 {
98     for (; *s; ++s)
99         *s = apr_toupper(*s);
100     return;
101 }
102
103 static const char ssl_util_uuencode_six2pr[64+1] =
104     "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
105
106 void ssl_util_uuencode(char *szTo, const char *szFrom, BOOL bPad)
107 {
108     ssl_util_uuencode_binary((unsigned char *)szTo,
109                              (const unsigned char *)szFrom,
110                              strlen(szFrom), bPad);
111 }
112
113 void ssl_util_uuencode_binary(unsigned char *szTo,
114                               const unsigned char *szFrom,
115                               int nLength, BOOL bPad)
116 {
117     const unsigned char *s;
118     int nPad = 0;
119
120     for (s = szFrom; nLength > 0; s += 3) {
121         *szTo++ = ssl_util_uuencode_six2pr[s[0] >> 2];
122         *szTo++ = ssl_util_uuencode_six2pr[(s[0] << 4 | s[1] >> 4) & 0x3f];
123         if (--nLength == 0) {
124             nPad = 2;
125             break;
126         }
127         *szTo++ = ssl_util_uuencode_six2pr[(s[1] << 2 | s[2] >> 6) & 0x3f];
128         if (--nLength == 0) {
129             nPad = 1;
130             break;
131         }
132         *szTo++ = ssl_util_uuencode_six2pr[s[2] & 0x3f];
133         --nLength;
134     }
135     while(bPad && nPad--) {
136         *szTo++ = NUL;
137     }
138     *szTo = NUL;
139     return;
140 }
141
142 apr_file_t *ssl_util_ppopen(server_rec *s, apr_pool_t *p, char *cmd)
143 {
144     apr_procattr_t *procattr;
145     apr_proc_t *proc;
146
147     if (apr_procattr_create(&procattr, p) != APR_SUCCESS) 
148         return NULL;
149     if (apr_procattr_io_set(procattr, APR_FULL_BLOCK, APR_FULL_BLOCK, 
150                             APR_FULL_BLOCK) != APR_SUCCESS)
151         return NULL;
152     if (apr_procattr_dir_set(procattr, 
153                              ap_make_dirstr_parent(p, cmd)) != APR_SUCCESS)
154         return NULL;
155     if (apr_procattr_cmdtype_set(procattr, APR_PROGRAM) != APR_SUCCESS)
156         return NULL;
157     if ((proc = (apr_proc_t *)apr_pcalloc(p, sizeof(apr_proc_t))) == NULL)
158         return NULL;
159     if (apr_proc_create(proc, cmd, NULL, NULL, procattr, p) != APR_SUCCESS)
160         return NULL;
161     return proc->out;
162 }
163
164 void ssl_util_ppclose(server_rec *s, apr_pool_t *p, apr_file_t *fp)
165 {
166     apr_file_close(fp);
167     return;
168 }
169
170 /*
171  * Run a filter program and read the first line of its stdout output
172  */
173 char *ssl_util_readfilter(server_rec *s, apr_pool_t *p, char *cmd)
174 {
175     static char buf[MAX_STRING_LEN];
176     apr_file_t *fp;
177     apr_size_t nbytes;
178     char c;
179     int k;
180
181     if ((fp = ssl_util_ppopen(s, p, cmd)) == NULL)
182         return NULL;
183     for (k = 0; apr_file_read(fp, &c, &nbytes) == APR_SUCCESS
184                 && nbytes == 1 && (k < MAX_STRING_LEN-1)     ; ) {
185         if (c == '\n' || c == '\r')
186             break;
187         buf[k++] = c;
188     }
189     buf[k] = NUL;
190     ssl_util_ppclose(s, p, fp);
191
192     return buf;
193 }
194
195 BOOL ssl_util_path_check(ssl_pathcheck_t pcm, const char *path, apr_pool_t *p)
196 {
197     apr_finfo_t finfo;
198
199     if (path == NULL)
200         return FALSE;
201     if (pcm & SSL_PCM_EXISTS && apr_stat(&finfo, path, 
202                                 APR_FINFO_TYPE|APR_FINFO_SIZE, p) != 0)
203         return FALSE;
204     if (pcm & SSL_PCM_ISREG && finfo.filetype != APR_REG)
205         return FALSE;
206     if (pcm & SSL_PCM_ISDIR && finfo.filetype != APR_DIR)
207         return FALSE;
208     if (pcm & SSL_PCM_ISNONZERO && finfo.size <= 0)
209         return FALSE;
210     return TRUE;
211 }
212
213 ssl_algo_t ssl_util_algotypeof(X509 *pCert, EVP_PKEY *pKey) 
214 {
215     ssl_algo_t t;
216             
217     t = SSL_ALGO_UNKNOWN;
218     if (pCert != NULL)
219         pKey = X509_get_pubkey(pCert);
220     if (pKey != NULL) {
221         switch (EVP_PKEY_type(pKey->type)) {
222             case EVP_PKEY_RSA: 
223                 t = SSL_ALGO_RSA;
224                 break;
225             case EVP_PKEY_DSA: 
226                 t = SSL_ALGO_DSA;
227                 break;
228             default:
229                 break;
230         }
231     }
232     return t;
233 }
234
235 char *ssl_util_algotypestr(ssl_algo_t t) 
236 {
237     char *cp;
238
239     cp = "UNKNOWN";
240     switch (t) {
241         case SSL_ALGO_RSA: 
242             cp = "RSA";
243             break;
244         case SSL_ALGO_DSA: 
245             cp = "DSA";
246             break;
247         default:
248             break;
249     }
250     return cp;
251 }
252
253 char *ssl_util_ptxtsub(apr_pool_t *p, const char *cpLine,
254                        const char *cpMatch, char *cpSubst)
255 {
256 #define MAX_PTXTSUB 100
257     char *cppMatch[MAX_PTXTSUB];
258     char *cpResult;
259     int nResult;
260     int nLine;
261     int nSubst;
262     int nMatch;
263     char *cpI;
264     char *cpO;
265     char *cp;
266     int i;
267
268     /*
269      * Pass 1: find substitution locations and calculate sizes
270      */
271     nLine  = strlen(cpLine);
272     nMatch = strlen(cpMatch);
273     nSubst = strlen(cpSubst);
274     for (cpI = (char *)cpLine, i = 0, nResult = 0;
275          cpI < cpLine+nLine && i < MAX_PTXTSUB;    ) {
276         if ((cp = strstr(cpI, cpMatch)) != NULL) {
277             cppMatch[i++] = cp;
278             nResult += ((cp-cpI)+nSubst);
279             cpI = (cp+nMatch);
280         }
281         else {
282             nResult += strlen(cpI);
283             break;
284         }
285     }
286     cppMatch[i] = NULL;
287     if (i == 0)
288         return NULL;
289
290     /*
291      * Pass 2: allocate memory and assemble result
292      */
293     cpResult = apr_pcalloc(p, nResult+1);
294     for (cpI = (char *)cpLine, cpO = cpResult, i = 0;
295          cppMatch[i] != NULL;
296          i++) {
297         apr_cpystrn(cpO, cpI, cppMatch[i]-cpI+1);
298         cpO += (cppMatch[i]-cpI);
299         apr_cpystrn(cpO, cpSubst, nSubst+1);
300         cpO += nSubst;
301         cpI = (cppMatch[i]+nMatch);
302     }
303     apr_cpystrn(cpO, cpI, cpResult+nResult-cpO+1);
304
305     return cpResult;
306 }
307
308 #if APR_HAS_THREADS
309 /*
310  * To ensure thread-safetyness in OpenSSL - work in progress
311  */
312
313 static apr_thread_mutex_t **lock_cs;
314 static long                 lock_count[CRYPTO_NUM_LOCKS];
315
316 static void ssl_util_thr_lock(int mode, int type, const char *file, int line)
317 {
318     if (mode & CRYPTO_LOCK) {
319         apr_thread_mutex_lock(lock_cs[type]);
320         lock_count[type]++;
321     }
322     else {
323         apr_thread_mutex_unlock(lock_cs[type]);
324     }
325 }
326
327 static unsigned long ssl_util_thr_id(void)
328 {
329     return (unsigned long) apr_os_thread_current();
330 }
331
332 static apr_status_t ssl_util_thread_cleanup(void *data)
333 {
334     int i;
335
336     CRYPTO_set_locking_callback(NULL);
337
338     for (i = 0; i < CRYPTO_NUM_LOCKS; i++) {
339         apr_thread_mutex_destroy(lock_cs[i]);
340     }
341
342     return APR_SUCCESS;
343 }
344
345 void ssl_util_thread_setup(server_rec *s, apr_pool_t *p)
346 {
347     int i, threaded_mpm;
348     /* This variable is not used? -aaron
349     SSLModConfigRec *mc = myModConfig(s);
350     */
351
352     ap_mpm_query(AP_MPMQ_IS_THREADED, &threaded_mpm);
353
354     if (!threaded_mpm) {
355         return;
356     }
357
358     lock_cs = apr_palloc(p, CRYPTO_NUM_LOCKS * sizeof(apr_thread_mutex_t *));
359
360     /*
361      * XXX: CRYPTO_NUM_LOCKS == 28
362      * should determine if there are lock types we do not need
363      * for example: debug_malloc, debug_malloc2 (see crypto/cryptlib.c)
364      */
365     for (i = 0; i < CRYPTO_NUM_LOCKS; i++) {
366         lock_count[i] = 0;
367         /* XXX: Can we remove the lock_count now that apr_thread_mutex_t
368          * can support nested (aka recursive) locks? -aaron */
369         apr_thread_mutex_create(&(lock_cs[i]), APR_THREAD_MUTEX_DEFAULT, p);
370     }
371
372     CRYPTO_set_id_callback(ssl_util_thr_id);
373
374     CRYPTO_set_locking_callback(ssl_util_thr_lock);
375
376     apr_pool_cleanup_register(p, NULL,
377                               ssl_util_thread_cleanup,
378                               apr_pool_cleanup_null);
379
380 }
381 #endif