1 /* Licensed to the Apache Software Foundation (ASF) under one or more
2 * contributor license agreements. See the NOTICE file distributed with
3 * this work for additional information regarding copyright ownership.
4 * The ASF licenses this file to You under the Apache License, Version 2.0
5 * (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 /* _ _
18 * _ __ ___ ___ __| | ___ ___| | mod_ssl
19 * | '_ ` _ \ / _ \ / _` | / __/ __| | Apache Interface to OpenSSL
20 * | | | | | | (_) | (_| | \__ \__ \ |
21 * |_| |_| |_|\___/ \__,_|___|___/___/_|
22 * |_____|
23 * ssl_engine_log.c
24 * Logging Facility
25 */
26 /* ``The difference between a computer
27 industry job and open-source software
28 hacking is about 30 hours a week.''
29 -- Ralf S. Engelschall */
30 #include "ssl_private.h"
31
32 /* _________________________________________________________________
33 **
34 ** Logfile Support
35 ** _________________________________________________________________
36 */
37
38 static const struct {
39 const char *cpPattern;
40 const char *cpAnnotation;
41 } ssl_log_annotate[] = {
42 { "*envelope*bad*decrypt*", "wrong pass phrase!?" },
43 { "*CLIENT_HELLO*unknown*protocol*", "speaking not SSL to HTTPS port!?" },
44 { "*CLIENT_HELLO*http*request*", "speaking HTTP to HTTPS port!?" },
45 { "*SSL3_READ_BYTES:sslv3*alert*bad*certificate*", "Subject CN in certificate not server name or identical to CA!?" },
46 { "*self signed certificate in certificate chain*", "Client certificate signed by CA not known to server?" },
47 { "*peer did not return a certificate*", "No CAs known to server for verification?" },
48 { "*no shared cipher*", "Too restrictive SSLCipherSuite or using DSA server certificate?" },
49 { "*no start line*", "Bad file contents or format - or even just a forgotten SSLCertificateKeyFile?" },
50 { "*bad password read*", "You entered an incorrect pass phrase!?" },
51 { "*bad mac decode*", "Browser still remembered details of a re-created server certificate?" },
52 { NULL, NULL }
53 };
54
ssl_log_annotation(const char * error)55 static const char *ssl_log_annotation(const char *error)
56 {
57 int i = 0;
58
59 while (ssl_log_annotate[i].cpPattern != NULL
60 && ap_strcmp_match(error, ssl_log_annotate[i].cpPattern) != 0)
61 i++;
62
63 return ssl_log_annotate[i].cpAnnotation;
64 }
65
ssl_die(server_rec * s)66 void ssl_die(server_rec *s)
67 {
68 if (s != NULL && s->is_virtual && s->error_fname != NULL)
69 ap_log_error(APLOG_MARK, APLOG_EMERG, 0, NULL, APLOGNO(02311)
70 "Fatal error initialising mod_ssl, exiting. "
71 "See %s for more information",
72 ap_server_root_relative(s->process->pool,
73 s->error_fname));
74 else
75 ap_log_error(APLOG_MARK, APLOG_EMERG, 0, NULL, APLOGNO(02312)
76 "Fatal error initialising mod_ssl, exiting.");
77
78 /*
79 * This is used for fatal errors and here
80 * it is common module practice to really
81 * exit from the complete program.
82 * XXX: The config hooks should return errors instead of calling exit().
83 */
84 exit(1);
85 }
86
87 /*
88 * Prints the SSL library error information.
89 */
ssl_log_ssl_error(const char * file,int line,int level,server_rec * s)90 void ssl_log_ssl_error(const char *file, int line, int level, server_rec *s)
91 {
92 unsigned long e;
93 const char *data;
94 int flags;
95
96 while ((e = ERR_peek_error_line_data(NULL, NULL, &data, &flags))) {
97 const char *annotation;
98 char err[256];
99
100 if (!(flags & ERR_TXT_STRING)) {
101 data = NULL;
102 }
103
104 ERR_error_string_n(e, err, sizeof err);
105 annotation = ssl_log_annotation(err);
106
107 ap_log_error(file, line, APLOG_MODULE_INDEX, level, 0, s,
108 "SSL Library Error: %s%s%s%s%s%s",
109 /* %s */
110 err,
111 /* %s%s%s */
112 data ? " (" : "", data ? data : "", data ? ")" : "",
113 /* %s%s */
114 annotation ? " -- " : "",
115 annotation ? annotation : "");
116
117 /* Pop the error off the stack: */
118 ERR_get_error();
119 }
120 }
121
ssl_log_cert_error(const char * file,int line,int level,apr_status_t rv,const server_rec * s,const conn_rec * c,const request_rec * r,apr_pool_t * p,X509 * cert,const char * format,va_list ap)122 static void ssl_log_cert_error(const char *file, int line, int level,
123 apr_status_t rv, const server_rec *s,
124 const conn_rec *c, const request_rec *r,
125 apr_pool_t *p, X509 *cert, const char *format,
126 va_list ap)
127 {
128 char buf[HUGE_STRING_LEN];
129 int msglen, n;
130 char *name;
131
132 apr_vsnprintf(buf, sizeof buf, format, ap);
133
134 msglen = strlen(buf);
135
136 if (cert) {
137 BIO *bio = BIO_new(BIO_s_mem());
138
139 if (bio) {
140 /*
141 * Limit the maximum length of the subject and issuer DN strings
142 * in the log message. 300 characters should always be sufficient
143 * for holding both the timestamp, module name, pid etc. stuff
144 * at the beginning of the line and the trailing information about
145 * serial, notbefore and notafter.
146 */
147 int maxdnlen = (HUGE_STRING_LEN - msglen - 300) / 2;
148
149 BIO_puts(bio, " [subject: ");
150 name = SSL_X509_NAME_to_string(p, X509_get_subject_name(cert),
151 maxdnlen);
152 if (!strIsEmpty(name)) {
153 BIO_puts(bio, name);
154 } else {
155 BIO_puts(bio, "-empty-");
156 }
157
158 BIO_puts(bio, " / issuer: ");
159 name = SSL_X509_NAME_to_string(p, X509_get_issuer_name(cert),
160 maxdnlen);
161 if (!strIsEmpty(name)) {
162 BIO_puts(bio, name);
163 } else {
164 BIO_puts(bio, "-empty-");
165 }
166
167 BIO_puts(bio, " / serial: ");
168 if (i2a_ASN1_INTEGER(bio, X509_get_serialNumber(cert)) == -1)
169 BIO_puts(bio, "(ERROR)");
170
171 BIO_puts(bio, " / notbefore: ");
172 ASN1_TIME_print(bio, X509_get_notBefore(cert));
173
174 BIO_puts(bio, " / notafter: ");
175 ASN1_TIME_print(bio, X509_get_notAfter(cert));
176
177 BIO_puts(bio, "]");
178
179 n = BIO_read(bio, buf + msglen, sizeof buf - msglen - 1);
180 if (n > 0)
181 buf[msglen + n] = '\0';
182
183 BIO_free(bio);
184 }
185 }
186 else {
187 apr_snprintf(buf + msglen, sizeof buf - msglen,
188 " [certificate: -not available-]");
189 }
190
191 if (r) {
192 ap_log_rerror(file, line, APLOG_MODULE_INDEX, level, rv, r, "%s", buf);
193 }
194 else if (c) {
195 ap_log_cerror(file, line, APLOG_MODULE_INDEX, level, rv, c, "%s", buf);
196 }
197 else if (s) {
198 ap_log_error(file, line, APLOG_MODULE_INDEX, level, rv, s, "%s", buf);
199 }
200
201 }
202
203 /*
204 * Wrappers for ap_log_error/ap_log_cerror/ap_log_rerror which log additional
205 * details of the X509 cert. For ssl_log_xerror, a pool needs to be passed in
206 * as well (for temporary allocation of the cert's subject/issuer name strings,
207 * in the other cases we use the connection and request pool, respectively).
208 */
ssl_log_xerror(const char * file,int line,int level,apr_status_t rv,apr_pool_t * ptemp,server_rec * s,X509 * cert,const char * fmt,...)209 void ssl_log_xerror(const char *file, int line, int level, apr_status_t rv,
210 apr_pool_t *ptemp, server_rec *s, X509 *cert,
211 const char *fmt, ...)
212 {
213 if (APLOG_IS_LEVEL(s,level)) {
214 va_list ap;
215 va_start(ap, fmt);
216 ssl_log_cert_error(file, line, level, rv, s, NULL, NULL, ptemp,
217 cert, fmt, ap);
218 va_end(ap);
219 }
220 }
221
ssl_log_cxerror(const char * file,int line,int level,apr_status_t rv,conn_rec * c,X509 * cert,const char * fmt,...)222 void ssl_log_cxerror(const char *file, int line, int level, apr_status_t rv,
223 conn_rec *c, X509 *cert, const char *fmt, ...)
224 {
225 if (APLOG_IS_LEVEL(mySrvFromConn(c),level)) {
226 va_list ap;
227 va_start(ap, fmt);
228 ssl_log_cert_error(file, line, level, rv, NULL, c, NULL, c->pool,
229 cert, fmt, ap);
230 va_end(ap);
231 }
232 }
233
ssl_log_rxerror(const char * file,int line,int level,apr_status_t rv,request_rec * r,X509 * cert,const char * fmt,...)234 void ssl_log_rxerror(const char *file, int line, int level, apr_status_t rv,
235 request_rec *r, X509 *cert, const char *fmt, ...)
236 {
237 if (APLOG_R_IS_LEVEL(r,level)) {
238 va_list ap;
239 va_start(ap, fmt);
240 ssl_log_cert_error(file, line, level, rv, NULL, NULL, r, r->pool,
241 cert, fmt, ap);
242 va_end(ap);
243 }
244 }
245