ssl_util_ssl.c revision 76fd02ce6aa984189050fa979bb5142dc409fc66
4b22b9337f359bfd063322244f5336cc7c6ffcfars/* Licensed to the Apache Software Foundation (ASF) under one or more
4b22b9337f359bfd063322244f5336cc7c6ffcfars * contributor license agreements. See the NOTICE file distributed with
cda73f64f20b8a0afc4909f5ea1f055ec7913856Toomas Soome * this work for additional information regarding copyright ownership.
4b22b9337f359bfd063322244f5336cc7c6ffcfars * The ASF licenses this file to You under the Apache License, Version 2.0
4b22b9337f359bfd063322244f5336cc7c6ffcfars * (the "License"); you may not use this file except in compliance with
4b22b9337f359bfd063322244f5336cc7c6ffcfars * the License. You may obtain a copy of the License at
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome * Unless required by applicable law or agreed to in writing, software
4b22b9337f359bfd063322244f5336cc7c6ffcfars * distributed under the License is distributed on an "AS IS" BASIS,
4b22b9337f359bfd063322244f5336cc7c6ffcfars * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
4b22b9337f359bfd063322244f5336cc7c6ffcfars * See the License for the specific language governing permissions and
4b22b9337f359bfd063322244f5336cc7c6ffcfars * limitations under the License.
4b22b9337f359bfd063322244f5336cc7c6ffcfars * _ __ ___ ___ __| | ___ ___| | mod_ssl
4b22b9337f359bfd063322244f5336cc7c6ffcfars * | '_ ` _ \ / _ \ / _` | / __/ __| | Apache Interface to OpenSSL
4b22b9337f359bfd063322244f5336cc7c6ffcfars * | | | | | | (_) | (_| | \__ \__ \ |
4b22b9337f359bfd063322244f5336cc7c6ffcfars * |_| |_| |_|\___/ \__,_|___|___/___/_|
4b22b9337f359bfd063322244f5336cc7c6ffcfars * Additional Utility Functions for OpenSSL
4b22b9337f359bfd063322244f5336cc7c6ffcfars/* _________________________________________________________________
4b22b9337f359bfd063322244f5336cc7c6ffcfars** Additional High-Level Functions for OpenSSL
4b22b9337f359bfd063322244f5336cc7c6ffcfars** _________________________________________________________________
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome/* we initialize this index at startup time
4b22b9337f359bfd063322244f5336cc7c6ffcfars * and never write to it at request time,
4b22b9337f359bfd063322244f5336cc7c6ffcfars * so this static is thread safe.
4b22b9337f359bfd063322244f5336cc7c6ffcfars * also note that OpenSSL increments at static variable when
4b22b9337f359bfd063322244f5336cc7c6ffcfars * SSL_get_ex_new_index() is called, so we _must_ do this at startup.
4b22b9337f359bfd063322244f5336cc7c6ffcfars /* we _do_ need to call this twice */
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome for (i=0; i<=1; i++) {
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome "Second Application Data for SSL",
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome return (void *)SSL_get_ex_data(ssl, SSL_app_data2_idx);
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome SSL_set_ex_data(ssl, SSL_app_data2_idx, (char *)arg);
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome/* _________________________________________________________________
cda73f64f20b8a0afc4909f5ea1f055ec7913856Toomas Soome** High-Level Certificate / Private Key Loading
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome** _________________________________________________________________
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas SoomeX509 *SSL_read_X509(char* filename, X509 **x509, pem_password_cb *cb)
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome /* 1. try PEM (= DER+Base64+headers) */
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome if ((bioS=BIO_new_file(filename, "r")) == NULL)
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome /* 2. try DER+Base64 */
cda73f64f20b8a0afc4909f5ea1f055ec7913856Toomas Soome if ((bioF = BIO_new(BIO_f_base64())) == NULL) {
4b22b9337f359bfd063322244f5336cc7c6ffcfars /* 3. try plain DER */
4b22b9337f359bfd063322244f5336cc7c6ffcfarsEVP_PKEY *SSL_read_PrivateKey(char* filename, EVP_PKEY **key, pem_password_cb *cb, void *s)
4b22b9337f359bfd063322244f5336cc7c6ffcfars /* 1. try PEM (= DER+Base64+headers) */
4b22b9337f359bfd063322244f5336cc7c6ffcfars /* 2. try DER+Base64 */
4b22b9337f359bfd063322244f5336cc7c6ffcfars /* 3. try plain DER */
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome/* _________________________________________________________________
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome** Smart shutdown
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome** _________________________________________________________________
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome * Repeat the calls, because SSL_shutdown internally dispatches through a
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome * little state machine. Usually only one or two interation should be
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome * needed, so we restrict the total number of restrictions in order to
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome * avoid process hangs in case the client played bad with the socket
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome * connection and OpenSSL cannot recognize it.
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome for (i = 0; i < 4 /* max 2x pending + 2x data = 4 */; i++) {
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome/* _________________________________________________________________
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome** Certificate Checks
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome** _________________________________________________________________
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome/* check whether cert contains extended key usage with a SGC tag */
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome sk = X509_get_ext_d2i(cert, NID_ext_key_usage, NULL, NULL);
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome ext_nid = OBJ_obj2nid(sk_ASN1_OBJECT_value(sk, i));
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome if (ext_nid == NID_ms_sgc || ext_nid == NID_ns_sgc) {
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome/* retrieve basic constraints ingredients */
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas SoomeBOOL SSL_X509_getBC(X509 *cert, int *ca, int *pathlen)
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome bc = X509_get_ext_d2i(cert, NID_basic_constraints, NULL, NULL);
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome if ((bn = ASN1_INTEGER_to_BN(bc->pathlen, NULL)) == NULL)
4b22b9337f359bfd063322244f5336cc7c6ffcfars/* convert a NAME_ENTRY to UTF8 string */
4b22b9337f359bfd063322244f5336cc7c6ffcfarschar *SSL_X509_NAME_ENTRY_to_string(apr_pool_t *p, X509_NAME_ENTRY *xsne)
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome ASN1_STRING_print_ex(bio, X509_NAME_ENTRY_get_data(xsne),
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome ASN1_STRFLGS_ESC_CTRL|ASN1_STRFLGS_UTF8_CONVERT);
4b22b9337f359bfd063322244f5336cc7c6ffcfars * convert an X509_NAME to an RFC 2253 formatted string, optionally truncated
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome * to maxlen characters (specify a maxlen of 0 for no length limit)
4b22b9337f359bfd063322244f5336cc7c6ffcfarschar *SSL_X509_NAME_to_string(apr_pool_t *p, X509_NAME *dn, int maxlen)
4b22b9337f359bfd063322244f5336cc7c6ffcfars if (len > 0) {
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome /* insert trailing ellipsis if there's enough space */
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome/* return an array of (RFC 6125 coined) DNS-IDs and CN-IDs in a certificate */
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas SoomeBOOL SSL_X509_getIDs(apr_pool_t *p, X509 *x509, apr_array_header_t **ids)
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome if (!x509 || !(*ids = apr_array_make(p, 0, sizeof(char *)))) {
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome /* First, the DNS-IDs (dNSName entries in the subjectAltName extension) */
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome if ((names = X509_get_ext_d2i(x509, NID_subject_alt_name, NULL, NULL)) &&
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome for (i = 0; i < sk_GENERAL_NAME_num(names); i++) {
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome ASN1_STRING_print_ex(bio, name->d.ia5, ASN1_STRFLGS_ESC_CTRL|
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome if (n > 0) {
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome /* Second, the CN-IDs (commonName attributes in the subject DN) */
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome while ((i = X509_NAME_get_index_by_NID(subj, NID_commonName, i)) != -1) {
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome *cpp = SSL_X509_NAME_ENTRY_to_string(p, X509_NAME_get_entry(subj, i));
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome return apr_is_empty_array(*ids) ? FALSE : TRUE;
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome * Check if a certificate matches for a particular name, by iterating over its
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome * DNS-IDs and CN-IDs (RFC 6125), optionally with basic wildcard matching.
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome * If server_rec is non-NULL, some (debug/trace) logging is enabled.
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas SoomeBOOL SSL_X509_match_name(apr_pool_t *p, X509 *x509, const char *name,
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome * At some day in the future, this might be replaced with X509_check_host()
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome * (available in OpenSSL 1.0.2 and later), but two points should be noted:
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome * 1) wildcard matching in X509_check_host() might yield different
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome * results (by default, it supports a broader set of patterns, e.g.
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome * wildcards in non-initial positions);
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome * 2) we lose the option of logging each DNS- and CN-ID (until a match
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome * is found).
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome const char *cp;
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome * Determine if it is a wildcard ID - we're restrictive
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome * in the sense that we require the wildcard character to be
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome * THE left-most label (i.e., the ID must start with "*.")
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome is_wildcard = (*id[i] == '*' && *(id[i]+1) == '.') ? TRUE : FALSE;
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome * If the ID includes a wildcard character (and the caller is
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome * allowing wildcards), check if it matches for the left-most
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome * DNS label - i.e., the wildcard character is not allowed
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome * to match a dot. Otherwise, try a simple string compare.
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome (cp = ap_strchr_c(name, '.')) && !strcasecmp(id[i]+1, cp)) ||
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome "[%s] SSL_X509_match_name: expecting name '%s', "
4b22b9337f359bfd063322244f5336cc7c6ffcfars "%smatched by ID '%s'",
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome ssl_log_xerror(SSLLOG_MARK, APLOG_DEBUG, 0, p, s, x509,
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome/* _________________________________________________________________
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome** Low-Level CA Certificate Loading
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome** _________________________________________________________________
4b22b9337f359bfd063322244f5336cc7c6ffcfars const char *filename)
4b22b9337f359bfd063322244f5336cc7c6ffcfars /* XXX: this dir read code is exactly the same as that in
4b22b9337f359bfd063322244f5336cc7c6ffcfars * ssl_engine_init.c, only the call to handle the fullname is different,
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome * should fold the duplication.
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome apr_int32_t finfo_flags = APR_FINFO_TYPE|APR_FINFO_NAME;
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome if (apr_dir_open(&dir, pathname, ptemp) != APR_SUCCESS) {
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome while ((apr_dir_read(&dirent, finfo_flags, dir)) == APR_SUCCESS) {
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome continue; /* don't try to load directories */
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome if (SSL_X509_INFO_load_file(ptemp, sk, fullname)) {
4b22b9337f359bfd063322244f5336cc7c6ffcfars/* _________________________________________________________________
4b22b9337f359bfd063322244f5336cc7c6ffcfars** Extra Server Certificate Chain Support
4b22b9337f359bfd063322244f5336cc7c6ffcfars** _________________________________________________________________
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome * Read a file that optionally contains the server certificate in PEM
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome * format, possibly followed by a sequence of CA certificates that
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome * should be sent to the peer in the SSL Certificate message.
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome SSL_CTX *ctx, char *file, int skipfirst, pem_password_cb *cb)
4b22b9337f359bfd063322244f5336cc7c6ffcfars unsigned long err;
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome if ((bio = BIO_new(BIO_s_file_internal())) == NULL)
4b22b9337f359bfd063322244f5336cc7c6ffcfars return -1;
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome /* optionally skip a leading server certificate */
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome if ((x509 = PEM_read_bio_X509(bio, NULL, cb, NULL)) == NULL) {
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome /* free a perhaps already configured extra chain */
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome sk_X509_pop_free((STACK_OF(X509) *)ctx->extra_certs, X509_free);
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome /* create new extra chain by loading the certs */
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome while ((x509 = PEM_read_bio_X509(bio, NULL, cb, NULL)) != NULL) {
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome if (!SSL_CTX_add_extra_chain_cert(ctx, x509)) {
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome /* Make sure that only the error is just an EOF */
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome && ERR_GET_REASON(err) == PEM_R_NO_START_LINE)) {
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome while (ERR_get_error() > 0) ;
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome/* _________________________________________________________________
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome** Session Stuff
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome** _________________________________________________________________
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soomechar *SSL_SESSION_id2sz(unsigned char *id, int idlen,
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome /* We must ensure not to process more than what would fit in the
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome * destination buffer, including terminating NULL */
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome /* XXX: is this ap_str_toupper() necessary ? */