/* Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License.
*/
/* we initialize this index at startup time * and never write to it at request time, * so this static is thread safe. * also note that OpenSSL increments at static variable when * SSL_get_ex_new_index() is called, so we _must_ do this at startup.
*/ staticint app_data2_idx = -1;
void modssl_init_app_data2_idx(void)
{ int i;
if (app_data2_idx > -1) { return;
}
/* we _do_ need to call this twice */ for (i = 0; i <= 1; i++) {
app_data2_idx =
SSL_get_ex_new_index(0, "Second Application Data for SSL",
NULL, NULL, NULL);
}
}
int modssl_smart_shutdown(SSL *ssl)
{ int i; int rc; int flush;
/* * Repeat the calls, because SSL_shutdown internally dispatches through a * little state machine. Usually only one or two iterations should be * needed, so we restrict the total number of restrictions in order to * avoid process hangs in case the client played bad with the socket * connection and OpenSSL cannot recognize it.
*/
rc = 0;
flush = !(SSL_get_shutdown(ssl) & SSL_SENT_SHUTDOWN); for (i = 0; i < 4 /* max 2x pending + 2x data = 4 */; i++) {
rc = SSL_shutdown(ssl); if (rc >= 0 && flush && (SSL_get_shutdown(ssl) & SSL_SENT_SHUTDOWN)) { /* Once the close notify is sent through the output filters, * ensure it is flushed through the socket.
*/ if (BIO_flush(SSL_get_wbio(ssl)) <= 0) {
rc = -1; break;
}
flush = 0;
} if (rc != 0) break;
} return rc;
}
char *modssl_bio_free_read(apr_pool_t *p, BIO *bio)
{ int len = BIO_pending(bio); char *result = NULL;
if (len > 0) {
result = apr_palloc(p, len+1);
len = BIO_read(bio, result, len);
result[len] = NUL;
}
BIO_free(bio); return result;
}
/* Convert ASN.1 string to a pool-allocated char * string, escaping * control characters. If raw is zero, convert to UTF-8, otherwise
* unchanged from the character set. */ staticchar *asn1_string_convert(apr_pool_t *p, ASN1_STRING *asn1str, int raw)
{
BIO *bio; int flags = ASN1_STRFLGS_ESC_CTRL;
if ((bio = BIO_new(BIO_s_mem())) == NULL) return NULL;
if (!raw) flags |= ASN1_STRFLGS_UTF8_CONVERT;
ASN1_STRING_print_ex(bio, asn1str, flags);
return modssl_bio_free_read(p, bio);
}
#define asn1_string_to_utf8(p, a) asn1_string_convert(p, a, 0)
/* convert a NAME_ENTRY to UTF8 string */ char *modssl_X509_NAME_ENTRY_to_string(apr_pool_t *p, X509_NAME_ENTRY *xsne, int raw)
{ char *result = asn1_string_convert(p, X509_NAME_ENTRY_get_data(xsne), raw);
ap_xlate_proto_from_ascii(result, len); return result;
}
/* * convert an X509_NAME to an RFC 2253 formatted string, optionally truncated * to maxlen characters (specify a maxlen of 0 for no length limit)
*/ char *modssl_X509_NAME_to_string(apr_pool_t *p, X509_NAME *dn, int maxlen)
{ char *result = NULL;
BIO *bio; int len;
if ((bio = BIO_new(BIO_s_mem())) == NULL) return NULL;
X509_NAME_print_ex(bio, dn, 0, XN_FLAG_RFC2253);
len = BIO_pending(bio); if (len > 0) {
result = apr_palloc(p, (maxlen > 0) ? maxlen+1 : len+1); if (maxlen > 0 && maxlen < len) {
len = BIO_read(bio, result, maxlen); if (maxlen > 2) { /* insert trailing ellipsis if there's enough space */
apr_snprintf(result + maxlen - 3, 4, "...");
}
} else {
len = BIO_read(bio, result, len);
}
result[len] = NUL;
}
BIO_free(bio);
return result;
}
staticvoid parse_otherName_value(apr_pool_t *p, ASN1_TYPE *value, constchar *onf, apr_array_header_t **entries)
{ constchar *str; int nid = onf ? OBJ_txt2nid(onf) : NID_undef;
if (!value || (nid == NID_undef) || !*entries) return;
/* * Currently supported otherName forms (values for "onf"): * "msUPN" (1.3.6.1.4.1.311.20.2.3): Microsoft User Principal Name * "id-on-dnsSRV" (1.3.6.1.5.5.7.8.7): SRVName, as specified in RFC 4985
*/ if ((nid == NID_ms_upn) && (value->type == V_ASN1_UTF8STRING) &&
(str = asn1_string_to_utf8(p, value->value.utf8string))) {
APR_ARRAY_PUSH(*entries, constchar *) = str;
} elseif (strEQ(onf, "id-on-dnsSRV") &&
(value->type == V_ASN1_IA5STRING) &&
(str = asn1_string_to_utf8(p, value->value.ia5string))) {
APR_ARRAY_PUSH(*entries, constchar *) = str;
}
}
/* * Return an array of subjectAltName entries of type "type". If idx is -1, * return all entries of the given type, otherwise return an array consisting * of the n-th occurrence of that type only. Currently supported types: * GEN_EMAIL (rfc822Name) * GEN_DNS (dNSName) * GEN_OTHERNAME (requires the otherName form ["onf"] argument to be supplied, * see parse_otherName_value for the currently supported forms)
*/ BOOL modssl_X509_getSAN(apr_pool_t *p, X509 *x509, int type, constchar *onf, int idx, apr_array_header_t **entries)
{
STACK_OF(GENERAL_NAME) *names; int nid = onf ? OBJ_txt2nid(onf) : NID_undef;
/* return an array of (RFC 6125 coined) DNS-IDs and CN-IDs in a certificate */ staticBOOL getIDs(apr_pool_t *p, X509 *x509, apr_array_header_t **ids)
{
X509_NAME *subj; int i = -1;
/* First, the DNS-IDs (dNSName entries in the subjectAltName extension) */ if (!x509 ||
(modssl_X509_getSAN(p, x509, GEN_DNS, NULL, -1, ids) == FALSE && !*ids)) {
*ids = NULL; returnFALSE;
}
/* Second, the CN-IDs (commonName attributes in the subject DN) */
subj = X509_get_subject_name(x509); while ((i = X509_NAME_get_index_by_NID(subj, NID_commonName, i)) != -1) {
APR_ARRAY_PUSH(*ids, constchar *) =
modssl_X509_NAME_ENTRY_to_string(p, X509_NAME_get_entry(subj, i), 0);
}
return apr_is_empty_array(*ids) ? FALSE : TRUE;
}
/* * Check if a certificate matches for a particular name, by iterating over its * DNS-IDs and CN-IDs (RFC 6125), optionally with basic wildcard matching. * If server_rec is non-NULL, some (debug/trace) logging is enabled.
*/ BOOL modssl_X509_match_name(apr_pool_t *p, X509 *x509, constchar *name, BOOL allow_wildcard, server_rec *s)
{ BOOL matched = FALSE;
apr_array_header_t *ids;
/* * At some day in the future, this might be replaced with X509_check_host() * (available in OpenSSL 1.0.2 and later), but two points should be noted: * 1) wildcard matching in X509_check_host() might yield different * results (by default, it supports a broader set of patterns, e.g. * wildcards in non-initial positions); * 2) we lose the option of logging each DNS- and CN-ID (until a match * is found).
*/
if (getIDs(p, x509, &ids)) { constchar *cp; int i; char **id = (char **)ids->elts; BOOL is_wildcard;
for (i = 0; i < ids->nelts; i++) { if (!id[i]) continue;
/* * Determine if it is a wildcard ID - we're restrictive * in the sense that we require the wildcard character to be * THE left-most label (i.e., the ID must start with "*.")
*/
is_wildcard = (*id[i] == '*' && *(id[i]+1) == '.') ? TRUE : FALSE;
/* * If the ID includes a wildcard character (and the caller is * allowing wildcards), check if it matches for the left-most * DNS label - i.e., the wildcard character is not allowed * to match a dot. Otherwise, try a simple string compare.
*/ if ((allow_wildcard == TRUE && is_wildcard == TRUE &&
(cp = ap_strchr_c(name, '.')) && !strcasecmp(id[i]+1, cp)) ||
!strcasecmp(id[i], name)) {
matched = TRUE;
}
if (s) {
ap_log_error(APLOG_MARK, APLOG_TRACE3, 0, s, "[%s] modssl_X509_match_name: expecting name '%s', " "%smatched by ID '%s'",
(mySrvConfig(s))->vhost_id, name,
matched == TRUE ? "" : "NOT ", id[i]);
}
if (matched == TRUE) { break;
}
}
}
if (s) {
ssl_log_xerror(SSLLOG_MARK, APLOG_DEBUG, 0, p, s, x509,
APLOGNO(02412) "[%s] Cert %s for name '%s'",
(mySrvConfig(s))->vhost_id,
matched == TRUE ? "matches" : "does not match",
name);
}
char *modssl_SSL_SESSION_id2sz(IDCONST unsignedchar *id, int idlen, char *str, int strsize)
{ if (idlen > SSL_MAX_SSL_SESSION_ID_LENGTH)
idlen = SSL_MAX_SSL_SESSION_ID_LENGTH;
/* We must ensure not to process more than what would fit in the
* destination buffer, including terminating NULL */ if (idlen > (strsize-1) / 2)
idlen = (strsize-1) / 2;
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung ist noch experimentell.