2 ** _ __ ___ ___ __| | ___ ___| | mod_ssl
3 ** | '_ ` _ \ / _ \ / _` | / __/ __| | Apache Interface to OpenSSL
4 ** | | | | | | (_) | (_| | \__ \__ \ | www.modssl.org
5 ** |_| |_| |_|\___/ \__,_|___|___/___/_| ftp.modssl.org
8 ** Additional Utility Functions for OpenSSL
11 /* ====================================================================
12 * The Apache Software License, Version 1.1
14 * Copyright (c) 2000-2002 The Apache Software Foundation. All rights
17 * Redistribution and use in source and binary forms, with or without
18 * modification, are permitted provided that the following conditions
21 * 1. Redistributions of source code must retain the above copyright
22 * notice, this list of conditions and the following disclaimer.
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
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.
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.
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.
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
57 * ====================================================================
62 /* _________________________________________________________________
64 ** Additional High-Level Functions for OpenSSL
65 ** _________________________________________________________________
68 /* we initialize this index at startup time
69 * and never write to it at request time,
70 * so this static is thread safe.
71 * also note that OpenSSL increments at static variable when
72 * SSL_get_ex_new_index() is called, so we _must_ do this at startup.
74 static int SSL_app_data2_idx = -1;
76 void SSL_init_app_data2_idx(void)
80 if (SSL_app_data2_idx > -1) {
84 /* we _do_ need to call this twice */
85 for (i=0; i<=1; i++) {
87 SSL_get_ex_new_index(0,
88 "Second Application Data for SSL",
93 void *SSL_get_app_data2(SSL *ssl)
95 return (void *)SSL_get_ex_data(ssl, SSL_app_data2_idx);
98 void SSL_set_app_data2(SSL *ssl, void *arg)
100 SSL_set_ex_data(ssl, SSL_app_data2_idx, (char *)arg);
104 /* _________________________________________________________________
106 ** High-Level Certificate / Private Key Loading
107 ** _________________________________________________________________
110 X509 *SSL_read_X509(char* filename, X509 **x509, int (*cb)(char*,int,int,void*))
116 /* 1. try PEM (= DER+Base64+headers) */
117 if ((bioS=BIO_new_file(filename, "r")) == NULL)
119 rc=PEM_read_bio_X509 (bioS, x509, cb, NULL);
123 /* 2. try DER+Base64 */
124 if ((bioS=BIO_new_file(filename, "r")) == NULL)
127 if ((bioF = BIO_new(BIO_f_base64())) == NULL) {
131 bioS = BIO_push(bioF, bioS);
132 rc = d2i_X509_bio(bioS, NULL);
135 /* 3. try plain DER */
136 if ((bioS=BIO_new_file(filename, "r")) == NULL)
138 rc = d2i_X509_bio(bioS, NULL);
142 if (rc != NULL && x509 != NULL) {
150 #if SSL_LIBRARY_VERSION <= 0x00904100
151 static EVP_PKEY *d2i_PrivateKey_bio(BIO *bio, EVP_PKEY **key)
153 return ((EVP_PKEY *)ASN1_d2i_bio(
154 (char *(*)())EVP_PKEY_new,
155 (char *(*)())d2i_PrivateKey,
156 (bio), (unsigned char **)(key)));
160 EVP_PKEY *SSL_read_PrivateKey(char* filename, EVP_PKEY **key, int (*cb)(char*,int,int,void*), void *s)
166 /* 1. try PEM (= DER+Base64+headers) */
167 if ((bioS=BIO_new_file(filename, "r")) == NULL)
169 rc = PEM_read_bio_PrivateKey(bioS, key, cb, s);
173 /* 2. try DER+Base64 */
174 if ( (bioS = BIO_new_file(filename, "r")) == NULL )
177 if ((bioF = BIO_new(BIO_f_base64())) == NULL) {
181 bioS = BIO_push(bioF, bioS);
182 rc = d2i_PrivateKey_bio(bioS, NULL);
185 /* 3. try plain DER */
186 if ( (bioS = BIO_new_file(filename, "r")) == NULL )
188 rc = d2i_PrivateKey_bio(bioS, NULL);
192 if (rc != NULL && key != NULL) {
200 /* _________________________________________________________________
203 ** _________________________________________________________________
206 int SSL_smart_shutdown(SSL *ssl)
212 * Repeat the calls, because SSL_shutdown internally dispatches through a
213 * little state machine. Usually only one or two interation should be
214 * needed, so we restrict the total number of restrictions in order to
215 * avoid process hangs in case the client played bad with the socket
216 * connection and OpenSSL cannot recognize it.
219 for (i = 0; i < 4 /* max 2x pending + 2x data = 4 */; i++) {
220 if ((rc = SSL_shutdown(ssl)))
226 /* _________________________________________________________________
228 ** Certificate Revocation List (CRL) Storage
229 ** _________________________________________________________________
232 X509_STORE *SSL_X509_STORE_create(char *cpFile, char *cpPath)
235 X509_LOOKUP *pLookup;
237 if (cpFile == NULL && cpPath == NULL)
239 if ((pStore = X509_STORE_new()) == NULL)
241 if (cpFile != NULL) {
242 if ((pLookup = X509_STORE_add_lookup(pStore, X509_LOOKUP_file())) == NULL) {
243 X509_STORE_free(pStore);
246 X509_LOOKUP_load_file(pLookup, cpFile, X509_FILETYPE_PEM);
248 if (cpPath != NULL) {
249 if ((pLookup = X509_STORE_add_lookup(pStore, X509_LOOKUP_hash_dir())) == NULL) {
250 X509_STORE_free(pStore);
253 X509_LOOKUP_add_dir(pLookup, cpPath, X509_FILETYPE_PEM);
258 int SSL_X509_STORE_lookup(X509_STORE *pStore, int nType,
259 X509_NAME *pName, X509_OBJECT *pObj)
261 X509_STORE_CTX pStoreCtx;
264 X509_STORE_CTX_init(&pStoreCtx, pStore, NULL, NULL);
265 rc = X509_STORE_get_by_subject(&pStoreCtx, nType, pName, pObj);
266 X509_STORE_CTX_cleanup(&pStoreCtx);
270 /* _________________________________________________________________
272 ** Cipher Suite Spec String Creation
273 ** _________________________________________________________________
276 char *SSL_make_ciphersuite(apr_pool_t *p, SSL *ssl)
278 STACK_OF(SSL_CIPHER) *sk;
287 if ((sk = (STACK_OF(SSL_CIPHER) *)SSL_get_ciphers(ssl)) == NULL)
290 for (i = 0; i < sk_SSL_CIPHER_num(sk); i++) {
291 c = sk_SSL_CIPHER_value(sk, i);
292 l += strlen(SSL_CIPHER_get_name(c))+2+1;
296 cpCipherSuite = (char *)apr_palloc(p, l+1);
298 for (i = 0; i < sk_SSL_CIPHER_num(sk); i++) {
299 c = sk_SSL_CIPHER_value(sk, i);
300 l = strlen(SSL_CIPHER_get_name(c));
301 memcpy(cp, SSL_CIPHER_get_name(c), l);
304 *cp++ = (SSL_CIPHER_get_valid(c) == 1 ? '1' : '0');
308 return cpCipherSuite;
311 /* _________________________________________________________________
313 ** Certificate Checks
314 ** _________________________________________________________________
317 /* check whether cert contains extended key usage with a SGC tag */
318 BOOL SSL_X509_isSGC(X509 *cert)
328 idx = X509_get_ext_by_NID(cert, NID_ext_key_usage, -1);
330 ext = X509_get_ext(cert, idx);
331 if ((sk = (STACK *)X509V3_EXT_d2i(ext)) != NULL) {
332 for (i = 0; i < sk_num(sk); i++) {
333 ext_nid = OBJ_obj2nid((ASN1_OBJECT *)sk_value(sk, i));
334 if (ext_nid == NID_ms_sgc || ext_nid == NID_ns_sgc) {
344 /* retrieve basic constraints ingredients */
345 BOOL SSL_X509_getBC(X509 *cert, int *ca, int *pathlen)
348 BASIC_CONSTRAINTS *bc;
353 if ((idx = X509_get_ext_by_NID(cert, NID_basic_constraints, -1)) < 0)
355 ext = X509_get_ext(cert, idx);
358 if ((bc = (BASIC_CONSTRAINTS *)X509V3_EXT_d2i(ext)) == NULL)
361 *pathlen = -1 /* unlimited */;
362 if (bc->pathlen != NULL) {
363 if ((bn = ASN1_INTEGER_to_BN(bc->pathlen, NULL)) == NULL)
365 if ((cp = BN_bn2dec(bn)) == NULL)
371 BASIC_CONSTRAINTS_free(bc);
375 /* retrieve subject CommonName of certificate */
376 BOOL SSL_X509_getCN(apr_pool_t *p, X509 *xs, char **cppCN)
379 X509_NAME_ENTRY *xsne;
384 xsn = X509_get_subject_name(xs);
385 for (i = 0; i < sk_X509_NAME_ENTRY_num((STACK_OF(X509_NAME_ENTRY) *)
386 X509_NAME_get_entries(xsn)); i++) {
387 xsne = sk_X509_NAME_ENTRY_value((STACK_OF(X509_NAME_ENTRY) *)
388 X509_NAME_get_entries(xsn), i);
389 nid = OBJ_obj2nid((ASN1_OBJECT *)X509_NAME_ENTRY_get_object(xsne));
390 if (nid == NID_commonName) {
391 data_ptr = X509_NAME_ENTRY_get_data_ptr(xsne);
392 data_len = X509_NAME_ENTRY_get_data_len(xsne);
393 *cppCN = apr_palloc(p, data_len+1);
394 apr_cpystrn(*cppCN, (char *)data_ptr, data_len+1);
395 (*cppCN)[data_len] = NUL;
396 #ifdef CHARSET_EBCDIC
397 ascii2ebcdic(*cppCN, *cppCN, strlen(*cppCN));
405 /* _________________________________________________________________
407 ** Low-Level CA Certificate Loading
408 ** _________________________________________________________________
411 #ifdef SSL_EXPERIMENTAL_PROXY
413 BOOL SSL_load_CrtAndKeyInfo_file(apr_pool_t *p, STACK_OF(X509_INFO) *sk, char *filename)
417 if ((in = BIO_new(BIO_s_file())) == NULL)
419 if (BIO_read_filename(in, filename) <= 0) {
424 #if SSL_LIBRARY_VERSION < 0x00904000
425 PEM_X509_INFO_read_bio(in, sk, NULL);
427 PEM_X509_INFO_read_bio(in, sk, NULL, NULL);
433 BOOL SSL_load_CrtAndKeyInfo_path(apr_pool_t *p, STACK_OF(X509_INFO) *sk, char *pathname)
441 apr_pool_sub_make(&sp, p, NULL);
442 if (apr_dir_open(&dir, pathname, sp)) != APR_SUCCESS) {
443 apr_pool_destroy(sp);
447 while ((apr_dir_read(&dirent, APR_FINFO_DIRENT, dir)) == APR_SUCCESS) {
448 fullname = apr_pstrcat(sp, pathname, "/", dirent.name, NULL);
449 if (dirent.filetype != APR_REG)
451 if (SSL_load_CrtAndKeyInfo_file(sp, sk, fullname))
455 apr_pool_destroy(sp);
459 #endif /* SSL_EXPERIMENTAL_PROXY */
461 /* _________________________________________________________________
463 ** Extra Server Certificate Chain Support
464 ** _________________________________________________________________
468 * Read a file that optionally contains the server certificate in PEM
469 * format, possibly followed by a sequence of CA certificates that
470 * should be sent to the peer in the SSL Certificate message.
472 int SSL_CTX_use_certificate_chain(
473 SSL_CTX *ctx, char *file, int skipfirst, int (*cb)(char*,int,int,void*))
481 if ((bio = BIO_new(BIO_s_file_internal())) == NULL)
483 if (BIO_read_filename(bio, file) <= 0) {
487 /* optionally skip a leading server certificate */
489 #if SSL_LIBRARY_VERSION < 0x00904000
490 if ((x509 = PEM_read_bio_X509(bio, NULL, cb)) == NULL) {
492 if ((x509 = PEM_read_bio_X509(bio, NULL, cb, NULL)) == NULL) {
499 /* free a perhaps already configured extra chain */
500 extra_certs=SSL_CTX_get_extra_certs(ctx);
501 if (extra_certs != NULL) {
502 sk_X509_pop_free((STACK_OF(X509) *)extra_certs, X509_free);
503 SSL_CTX_set_extra_certs(ctx,NULL);
505 /* create new extra chain by loading the certs */
507 #if SSL_LIBRARY_VERSION < 0x00904000
508 while ((x509 = PEM_read_bio_X509(bio, NULL, cb)) != NULL) {
510 while ((x509 = PEM_read_bio_X509(bio, NULL, cb, NULL)) != NULL) {
512 if (!SSL_CTX_add_extra_chain_cert(ctx, x509)) {
519 /* Make sure that only the error is just an EOF */
520 if ((err = ERR_peek_error()) > 0) {
521 if (!( ERR_GET_LIB(err) == ERR_LIB_PEM
522 && ERR_GET_REASON(err) == PEM_R_NO_START_LINE)) {
526 while (ERR_get_error() > 0) ;
532 /* _________________________________________________________________
535 ** _________________________________________________________________
538 char *SSL_SESSION_id2sz(unsigned char *id, int idlen,
539 char *str, int strsize)
545 for (n = 0; n < idlen && n < SSL_MAX_SSL_SESSION_ID_LENGTH; n++) {
546 apr_snprintf(cp, strsize - (cp-str), "%02X", id[n]);