ssl_util.c revision 6b441532f6ac4ebd1c4867ab5f8a0165247b178e
1e9517ea2156b990be21f44676d3370318eacf17Tinderbox User** _ __ ___ ___ __| | ___ ___| | mod_ssl
f0aad5341752aefe5059832f6cf3abc3283c6e16Tinderbox User** | '_ ` _ \ / _ \ / _` | / __/ __| | Apache Interface to OpenSSL
1e9517ea2156b990be21f44676d3370318eacf17Tinderbox User** | | | | | | (_) | (_| | \__ \__ \ | www.modssl.org
1e9517ea2156b990be21f44676d3370318eacf17Tinderbox User** |_| |_| |_|\___/ \__,_|___|___/___/_| ftp.modssl.org
1e9517ea2156b990be21f44676d3370318eacf17Tinderbox User** Utility Functions
1e9517ea2156b990be21f44676d3370318eacf17Tinderbox User/* ====================================================================
1e9517ea2156b990be21f44676d3370318eacf17Tinderbox User * The Apache Software License, Version 1.1
1e9517ea2156b990be21f44676d3370318eacf17Tinderbox User * Copyright (c) 2000-2003 The Apache Software Foundation. All rights
1e9517ea2156b990be21f44676d3370318eacf17Tinderbox User * Redistribution and use in source and binary forms, with or without
1e9517ea2156b990be21f44676d3370318eacf17Tinderbox User * modification, are permitted provided that the following conditions
1e9517ea2156b990be21f44676d3370318eacf17Tinderbox User * 1. Redistributions of source code must retain the above copyright
1e9517ea2156b990be21f44676d3370318eacf17Tinderbox User * notice, this list of conditions and the following disclaimer.
1e9517ea2156b990be21f44676d3370318eacf17Tinderbox User * 2. Redistributions in binary form must reproduce the above copyright
1e9517ea2156b990be21f44676d3370318eacf17Tinderbox User * notice, this list of conditions and the following disclaimer in
1e9517ea2156b990be21f44676d3370318eacf17Tinderbox User * the documentation and/or other materials provided with the
1e9517ea2156b990be21f44676d3370318eacf17Tinderbox User * distribution.
1e9517ea2156b990be21f44676d3370318eacf17Tinderbox User * 3. The end-user documentation included with the redistribution,
1e9517ea2156b990be21f44676d3370318eacf17Tinderbox User * if any, must include the following acknowledgment:
1e9517ea2156b990be21f44676d3370318eacf17Tinderbox User * "This product includes software developed by the
1e9517ea2156b990be21f44676d3370318eacf17Tinderbox User * Apache Software Foundation (http://www.apache.org/)."
1e9517ea2156b990be21f44676d3370318eacf17Tinderbox User * Alternately, this acknowledgment may appear in the software itself,
1e9517ea2156b990be21f44676d3370318eacf17Tinderbox User * if and wherever such third-party acknowledgments normally appear.
7911e6f9de303bca5a3d8b34f4330c8f7cecffaeTinderbox User * 4. The names "Apache" and "Apache Software Foundation" must
7911e6f9de303bca5a3d8b34f4330c8f7cecffaeTinderbox User * not be used to endorse or promote products derived from this
7911e6f9de303bca5a3d8b34f4330c8f7cecffaeTinderbox User * software without prior written permission. For written
1e9517ea2156b990be21f44676d3370318eacf17Tinderbox User * permission, please contact apache@apache.org.
7911e6f9de303bca5a3d8b34f4330c8f7cecffaeTinderbox User * 5. Products derived from this software may not be called "Apache",
7911e6f9de303bca5a3d8b34f4330c8f7cecffaeTinderbox User * nor may "Apache" appear in their name, without prior written
7911e6f9de303bca5a3d8b34f4330c8f7cecffaeTinderbox User * permission of the Apache Software Foundation.
7911e6f9de303bca5a3d8b34f4330c8f7cecffaeTinderbox User * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
7911e6f9de303bca5a3d8b34f4330c8f7cecffaeTinderbox User * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
7911e6f9de303bca5a3d8b34f4330c8f7cecffaeTinderbox User * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
7911e6f9de303bca5a3d8b34f4330c8f7cecffaeTinderbox User * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
1e9517ea2156b990be21f44676d3370318eacf17Tinderbox User * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
7911e6f9de303bca5a3d8b34f4330c8f7cecffaeTinderbox User * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
7911e6f9de303bca5a3d8b34f4330c8f7cecffaeTinderbox User * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
7911e6f9de303bca5a3d8b34f4330c8f7cecffaeTinderbox User * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
7911e6f9de303bca5a3d8b34f4330c8f7cecffaeTinderbox User * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
7911e6f9de303bca5a3d8b34f4330c8f7cecffaeTinderbox User * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
7911e6f9de303bca5a3d8b34f4330c8f7cecffaeTinderbox User * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
7911e6f9de303bca5a3d8b34f4330c8f7cecffaeTinderbox User * SUCH DAMAGE.
7911e6f9de303bca5a3d8b34f4330c8f7cecffaeTinderbox User * ====================================================================
7911e6f9de303bca5a3d8b34f4330c8f7cecffaeTinderbox User /* ``Every day of my life
7911e6f9de303bca5a3d8b34f4330c8f7cecffaeTinderbox User I am forced to add another
7911e6f9de303bca5a3d8b34f4330c8f7cecffaeTinderbox User name to the list of people
7911e6f9de303bca5a3d8b34f4330c8f7cecffaeTinderbox User who piss me off!''
7911e6f9de303bca5a3d8b34f4330c8f7cecffaeTinderbox User/* _________________________________________________________________
7911e6f9de303bca5a3d8b34f4330c8f7cecffaeTinderbox User** Utility Functions
7911e6f9de303bca5a3d8b34f4330c8f7cecffaeTinderbox User** _________________________________________________________________
7911e6f9de303bca5a3d8b34f4330c8f7cecffaeTinderbox Userchar *ssl_util_vhostid(apr_pool_t *p, server_rec *s)
1e9517ea2156b990be21f44676d3370318eacf17Tinderbox User id = apr_psprintf(p, "%s:%lu", host, (unsigned long)port);
1e9517ea2156b990be21f44676d3370318eacf17Tinderbox User for (; *s; ++s)
7911e6f9de303bca5a3d8b34f4330c8f7cecffaeTinderbox Userstatic const char ssl_util_uuencode_six2pr[64+1] =
1e9517ea2156b990be21f44676d3370318eacf17Tinderbox User "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
1e9517ea2156b990be21f44676d3370318eacf17Tinderbox Uservoid ssl_util_uuencode(char *szTo, const char *szFrom, BOOL bPad)
1e9517ea2156b990be21f44676d3370318eacf17Tinderbox User ssl_util_uuencode_binary((unsigned char *)szTo,
1e9517ea2156b990be21f44676d3370318eacf17Tinderbox User (const unsigned char *)szFrom,
1e9517ea2156b990be21f44676d3370318eacf17Tinderbox Uservoid ssl_util_uuencode_binary(unsigned char *szTo,
7911e6f9de303bca5a3d8b34f4330c8f7cecffaeTinderbox User const unsigned char *szFrom,
1e9517ea2156b990be21f44676d3370318eacf17Tinderbox User const unsigned char *s;
1e9517ea2156b990be21f44676d3370318eacf17Tinderbox User *szTo++ = ssl_util_uuencode_six2pr[s[0] >> 2];
7911e6f9de303bca5a3d8b34f4330c8f7cecffaeTinderbox User *szTo++ = ssl_util_uuencode_six2pr[(s[0] << 4 | s[1] >> 4) & 0x3f];
1e9517ea2156b990be21f44676d3370318eacf17Tinderbox User *szTo++ = ssl_util_uuencode_six2pr[(s[1] << 2 | s[2] >> 6) & 0x3f];
7911e6f9de303bca5a3d8b34f4330c8f7cecffaeTinderbox User *szTo++ = ssl_util_uuencode_six2pr[s[2] & 0x3f];
1e9517ea2156b990be21f44676d3370318eacf17Tinderbox Userapr_file_t *ssl_util_ppopen(server_rec *s, apr_pool_t *p, const char *cmd,
1e9517ea2156b990be21f44676d3370318eacf17Tinderbox User const char * const *argv)
1e9517ea2156b990be21f44676d3370318eacf17Tinderbox User if (apr_procattr_create(&procattr, p) != APR_SUCCESS)
7911e6f9de303bca5a3d8b34f4330c8f7cecffaeTinderbox User if (apr_procattr_io_set(procattr, APR_FULL_BLOCK, APR_FULL_BLOCK,
1e9517ea2156b990be21f44676d3370318eacf17Tinderbox User ap_make_dirstr_parent(p, cmd)) != APR_SUCCESS)
7911e6f9de303bca5a3d8b34f4330c8f7cecffaeTinderbox User if (apr_procattr_cmdtype_set(procattr, APR_PROGRAM) != APR_SUCCESS)
1e9517ea2156b990be21f44676d3370318eacf17Tinderbox User if ((proc = (apr_proc_t *)apr_pcalloc(p, sizeof(apr_proc_t))) == NULL)
7911e6f9de303bca5a3d8b34f4330c8f7cecffaeTinderbox User if (apr_proc_create(proc, cmd, argv, NULL, procattr, p) != APR_SUCCESS)
7911e6f9de303bca5a3d8b34f4330c8f7cecffaeTinderbox Uservoid ssl_util_ppclose(server_rec *s, apr_pool_t *p, apr_file_t *fp)
1e9517ea2156b990be21f44676d3370318eacf17Tinderbox User * Run a filter program and read the first line of its stdout output
1e9517ea2156b990be21f44676d3370318eacf17Tinderbox Userchar *ssl_util_readfilter(server_rec *s, apr_pool_t *p, const char *cmd,
1e9517ea2156b990be21f44676d3370318eacf17Tinderbox User const char * const *argv)
1e9517ea2156b990be21f44676d3370318eacf17Tinderbox User if ((fp = ssl_util_ppopen(s, p, cmd, argv)) == NULL)
1e9517ea2156b990be21f44676d3370318eacf17Tinderbox User /* XXX: we are reading 1 byte at a time here */
1e9517ea2156b990be21f44676d3370318eacf17Tinderbox User for (k = 0; apr_file_read(fp, &c, &nbytes) == APR_SUCCESS
1e9517ea2156b990be21f44676d3370318eacf17Tinderbox User && nbytes == 1 && (k < MAX_STRING_LEN-1) ; ) {
7911e6f9de303bca5a3d8b34f4330c8f7cecffaeTinderbox UserBOOL ssl_util_path_check(ssl_pathcheck_t pcm, const char *path, apr_pool_t *p)
7911e6f9de303bca5a3d8b34f4330c8f7cecffaeTinderbox User if (pcm & SSL_PCM_EXISTS && apr_stat(&finfo, path,
7911e6f9de303bca5a3d8b34f4330c8f7cecffaeTinderbox User if (pcm & SSL_PCM_ISREG && finfo.filetype != APR_REG)
1e9517ea2156b990be21f44676d3370318eacf17Tinderbox User if (pcm & SSL_PCM_ISDIR && finfo.filetype != APR_DIR)
7911e6f9de303bca5a3d8b34f4330c8f7cecffaeTinderbox User if (pcm & SSL_PCM_ISNONZERO && finfo.size <= 0)
1e9517ea2156b990be21f44676d3370318eacf17Tinderbox Userssl_algo_t ssl_util_algotypeof(X509 *pCert, EVP_PKEY *pKey)
1e9517ea2156b990be21f44676d3370318eacf17Tinderbox Userchar *ssl_util_ptxtsub(apr_pool_t *p, const char *cpLine,
1e9517ea2156b990be21f44676d3370318eacf17Tinderbox User * Pass 1: find substitution locations and calculate sizes
7911e6f9de303bca5a3d8b34f4330c8f7cecffaeTinderbox User for (cpI = (char *)cpLine, i = 0, nResult = 0;
7911e6f9de303bca5a3d8b34f4330c8f7cecffaeTinderbox User * Pass 2: allocate memory and assemble result
7911e6f9de303bca5a3d8b34f4330c8f7cecffaeTinderbox User for (cpI = (char *)cpLine, cpO = cpResult, i = 0;
1e9517ea2156b990be21f44676d3370318eacf17Tinderbox User apr_cpystrn(cpO, cpI, cpResult+nResult-cpO+1);
1e9517ea2156b990be21f44676d3370318eacf17Tinderbox User * certain key and cert data needs to survive restarts,
1e9517ea2156b990be21f44676d3370318eacf17Tinderbox User * which are stored in the user data table of s->process->pool.
7911e6f9de303bca5a3d8b34f4330c8f7cecffaeTinderbox User * to prevent "leaking" of this data, we use malloc/free
7911e6f9de303bca5a3d8b34f4330c8f7cecffaeTinderbox User * rather than apr_palloc and these wrappers to help make sure
1e9517ea2156b990be21f44676d3370318eacf17Tinderbox User * we do not leak the malloc-ed data.
7911e6f9de303bca5a3d8b34f4330c8f7cecffaeTinderbox Userunsigned char *ssl_asn1_table_set(apr_hash_t *table,
1e9517ea2156b990be21f44676d3370318eacf17Tinderbox User ssl_asn1_t *asn1 = apr_hash_get(table, key, klen);
1e9517ea2156b990be21f44676d3370318eacf17Tinderbox User * if a value for this key already exists,
1e9517ea2156b990be21f44676d3370318eacf17Tinderbox User * reuse as much of the already malloc-ed data
7911e6f9de303bca5a3d8b34f4330c8f7cecffaeTinderbox User * as possible.
1e9517ea2156b990be21f44676d3370318eacf17Tinderbox User asn1->source_mtime = 0; /* used as a note for encrypted private keys */
1e9517ea2156b990be21f44676d3370318eacf17Tinderbox User return asn1->cpData; /* caller will assign a value to this */
7911e6f9de303bca5a3d8b34f4330c8f7cecffaeTinderbox Userssl_asn1_t *ssl_asn1_table_get(apr_hash_t *table,
7911e6f9de303bca5a3d8b34f4330c8f7cecffaeTinderbox User return (ssl_asn1_t *)apr_hash_get(table, key, APR_HASH_KEY_STRING);
7911e6f9de303bca5a3d8b34f4330c8f7cecffaeTinderbox User ssl_asn1_t *asn1 = apr_hash_get(table, key, klen);
7911e6f9de303bca5a3d8b34f4330c8f7cecffaeTinderbox Userstatic const char *ssl_asn1_key_types[] = {"RSA", "DSA"};
1e9517ea2156b990be21f44676d3370318eacf17Tinderbox Userconst char *ssl_asn1_table_keyfmt(apr_pool_t *p,
7911e6f9de303bca5a3d8b34f4330c8f7cecffaeTinderbox User const char *id,
1e9517ea2156b990be21f44676d3370318eacf17Tinderbox User const char *keystr = ssl_asn1_keystr(keytype);
1e9517ea2156b990be21f44676d3370318eacf17Tinderbox User return apr_pstrcat(p, id, ":", keystr, NULL);
1e9517ea2156b990be21f44676d3370318eacf17Tinderbox User * To ensure thread-safetyness in OpenSSL - work in progress
7911e6f9de303bca5a3d8b34f4330c8f7cecffaeTinderbox Userstatic int ssl_util_thr_lock(int mode, int type,
1e9517ea2156b990be21f44676d3370318eacf17Tinderbox Userstatic void ssl_util_thr_lock(int mode, int type,
1e9517ea2156b990be21f44676d3370318eacf17Tinderbox Userstatic void ssl_util_thr_lock(int mode, int type,
7911e6f9de303bca5a3d8b34f4330c8f7cecffaeTinderbox Userstatic unsigned long ssl_util_thr_id(void)
1e9517ea2156b990be21f44676d3370318eacf17Tinderbox User /* OpenSSL needs this to return an unsigned long. On OS/390, the pthread
7911e6f9de303bca5a3d8b34f4330c8f7cecffaeTinderbox User * id is a structure twice that big. Use the TCB pointer instead as a
7911e6f9de303bca5a3d8b34f4330c8f7cecffaeTinderbox User * unique unsigned long.
7911e6f9de303bca5a3d8b34f4330c8f7cecffaeTinderbox User unsigned long PSATOLD;
1e9517ea2156b990be21f44676d3370318eacf17Tinderbox User return (unsigned long) apr_os_thread_current();
7911e6f9de303bca5a3d8b34f4330c8f7cecffaeTinderbox Userstatic apr_status_t ssl_util_thread_cleanup(void *data)
1e9517ea2156b990be21f44676d3370318eacf17Tinderbox User /* Let the registered mutex cleanups do their own thing
1e9517ea2156b990be21f44676d3370318eacf17Tinderbox User lock_cs = apr_palloc(p, lock_num_locks * sizeof(*lock_cs));
1e9517ea2156b990be21f44676d3370318eacf17Tinderbox User for (i = 0; i < lock_num_locks; i++) {
7911e6f9de303bca5a3d8b34f4330c8f7cecffaeTinderbox User apr_thread_mutex_create(&(lock_cs[i]), APR_THREAD_MUTEX_DEFAULT, p);
1e9517ea2156b990be21f44676d3370318eacf17Tinderbox User CRYPTO_set_locking_callback(ssl_util_thr_lock);