]>
Commit | Line | Data |
---|---|---|
f484cdf5 | 1 | |
2 | /* | |
f484cdf5 | 3 | * AUTHOR: Benno Rice |
27d8545c | 4 | * DEBUG: section 83 SSL accelerator support |
f484cdf5 | 5 | * |
6 | * SQUID Internet Object Cache http://squid.nlanr.net/Squid/ | |
7 | * ---------------------------------------------------------- | |
8 | * | |
9 | * Squid is the result of efforts by numerous individuals from the | |
10 | * Internet community. Development is led by Duane Wessels of the | |
11 | * National Laboratory for Applied Network Research and funded by the | |
12 | * National Science Foundation. Squid is Copyrighted (C) 1998 by | |
13 | * Duane Wessels and the University of California San Diego. Please | |
14 | * see the COPYRIGHT file for full details. Squid incorporates | |
15 | * software developed and/or copyrighted by other sources. Please see | |
16 | * the CREDITS file for full details. | |
17 | * | |
18 | * This program is free software; you can redistribute it and/or modify | |
19 | * it under the terms of the GNU General Public License as published by | |
20 | * the Free Software Foundation; either version 2 of the License, or | |
21 | * (at your option) any later version. | |
26ac0430 | 22 | * |
f484cdf5 | 23 | * This program is distributed in the hope that it will be useful, |
24 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
25 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
26 | * GNU General Public License for more details. | |
26ac0430 | 27 | * |
f484cdf5 | 28 | * You should have received a copy of the GNU General Public License |
29 | * along with this program; if not, write to the Free Software | |
30 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA. | |
31 | * | |
32 | */ | |
33 | ||
582c2af2 | 34 | #include "squid.h" |
454e8283 | 35 | |
36 | /* MS Visual Studio Projects are monolithic, so we need the following | |
37 | * #if to exclude the SSL code from compile process when not needed. | |
38 | */ | |
cb4f4424 | 39 | #if USE_OPENSSL |
454e8283 | 40 | |
c0941a6a | 41 | #include "acl/FilledChecklist.h" |
86660d64 | 42 | #include "anyp/PortCfg.h" |
582c2af2 FC |
43 | #include "fde.h" |
44 | #include "globals.h" | |
86c63190 | 45 | #include "ipc/MemMap.h" |
4d5904f7 | 46 | #include "SquidConfig.h" |
10a69fc0 | 47 | #include "SquidTime.h" |
2cef0ca6 | 48 | #include "ssl/Config.h" |
4d16918e | 49 | #include "ssl/ErrorDetail.h" |
95d2589c | 50 | #include "ssl/gadgets.h" |
602d9612 | 51 | #include "ssl/support.h" |
fc54b8d2 | 52 | #include "URL.h" |
f484cdf5 | 53 | |
21d845b1 FC |
54 | #if HAVE_ERRNO_H |
55 | #include <errno.h> | |
56 | #endif | |
57 | ||
10a69fc0 CT |
58 | static void setSessionCallbacks(SSL_CTX *ctx); |
59 | Ipc::MemMap *SslSessionCache = NULL; | |
60 | const char *SslSessionCacheName = "ssl_session_cache"; | |
61 | ||
caf3666d AR |
62 | const char *Ssl::BumpModeStr[] = { |
63 | "none", | |
64 | "client-first", | |
65 | "server-first", | |
66 | NULL | |
67 | }; | |
68 | ||
63be0a78 | 69 | /** |
70 | \defgroup ServerProtocolSSLInternal Server-Side SSL Internals | |
71 | \ingroup ServerProtocolSSLAPI | |
72 | */ | |
73 | ||
74 | /// \ingroup ServerProtocolSSLInternal | |
307b83b7 | 75 | static int |
76 | ssl_ask_password_cb(char *buf, int size, int rwflag, void *userdata) | |
77 | { | |
78 | FILE *in; | |
79 | int len = 0; | |
80 | char cmdline[1024]; | |
81 | ||
82 | snprintf(cmdline, sizeof(cmdline), "\"%s\" \"%s\"", Config.Program.ssl_password, (const char *)userdata); | |
83 | in = popen(cmdline, "r"); | |
84 | ||
85 | if (fgets(buf, size, in)) | |
86 | ||
87 | len = strlen(buf); | |
88 | ||
89 | while (len > 0 && (buf[len - 1] == '\n' || buf[len - 1] == '\r')) | |
5e263176 | 90 | --len; |
307b83b7 | 91 | |
92 | buf[len] = '\0'; | |
93 | ||
94 | pclose(in); | |
95 | ||
96 | return len; | |
97 | } | |
98 | ||
63be0a78 | 99 | /// \ingroup ServerProtocolSSLInternal |
307b83b7 | 100 | static void |
101 | ssl_ask_password(SSL_CTX * context, const char * prompt) | |
102 | { | |
103 | if (Config.Program.ssl_password) { | |
104 | SSL_CTX_set_default_passwd_cb(context, ssl_ask_password_cb); | |
105 | SSL_CTX_set_default_passwd_cb_userdata(context, (void *)prompt); | |
106 | } | |
107 | } | |
108 | ||
63be0a78 | 109 | /// \ingroup ServerProtocolSSLInternal |
f484cdf5 | 110 | static RSA * |
e6ccf245 | 111 | ssl_temp_rsa_cb(SSL * ssl, int anInt, int keylen) |
f484cdf5 | 112 | { |
e01f02ed | 113 | static RSA *rsa_512 = NULL; |
114 | static RSA *rsa_1024 = NULL; | |
115 | RSA *rsa = NULL; | |
116 | int newkey = 0; | |
f484cdf5 | 117 | |
e01f02ed | 118 | switch (keylen) { |
119 | ||
120 | case 512: | |
121 | ||
122 | if (!rsa_512) { | |
123 | rsa_512 = RSA_generate_key(512, RSA_F4, NULL, NULL); | |
124 | newkey = 1; | |
125 | } | |
126 | ||
127 | rsa = rsa_512; | |
128 | break; | |
129 | ||
130 | case 1024: | |
131 | ||
132 | if (!rsa_1024) { | |
133 | rsa_1024 = RSA_generate_key(1024, RSA_F4, NULL, NULL); | |
134 | newkey = 1; | |
135 | } | |
136 | ||
137 | rsa = rsa_1024; | |
138 | break; | |
139 | ||
140 | default: | |
e0236918 | 141 | debugs(83, DBG_IMPORTANT, "ssl_temp_rsa_cb: Unexpected key length " << keylen); |
e01f02ed | 142 | return NULL; |
143 | } | |
144 | ||
145 | if (rsa == NULL) { | |
e0236918 | 146 | debugs(83, DBG_IMPORTANT, "ssl_temp_rsa_cb: Failed to generate key " << keylen); |
e01f02ed | 147 | return NULL; |
148 | } | |
149 | ||
150 | if (newkey) { | |
151 | if (do_debug(83, 5)) | |
152 | PEM_write_RSAPrivateKey(debug_log, rsa, NULL, NULL, 0, NULL, NULL); | |
153 | ||
e0236918 | 154 | debugs(83, DBG_IMPORTANT, "Generated ephemeral RSA key of length " << keylen); |
e01f02ed | 155 | } |
62e76326 | 156 | |
f484cdf5 | 157 | return rsa; |
158 | } | |
159 | ||
4d16918e CT |
160 | int Ssl::asn1timeToString(ASN1_TIME *tm, char *buf, int len) |
161 | { | |
162 | BIO *bio; | |
163 | int write = 0; | |
164 | bio = BIO_new(BIO_s_mem()); | |
165 | if (bio) { | |
e34763f4 A |
166 | if (ASN1_TIME_print(bio, tm)) |
167 | write = BIO_read(bio, buf, len-1); | |
168 | BIO_free(bio); | |
4d16918e CT |
169 | } |
170 | buf[write]='\0'; | |
171 | return write; | |
172 | } | |
173 | ||
174 | int Ssl::matchX509CommonNames(X509 *peer_cert, void *check_data, int (*check_func)(void *check_data, ASN1_STRING *cn_data)) | |
175 | { | |
176 | assert(peer_cert); | |
177 | ||
178 | X509_NAME *name = X509_get_subject_name(peer_cert); | |
179 | ||
180 | for (int i = X509_NAME_get_index_by_NID(name, NID_commonName, -1); i >= 0; i = X509_NAME_get_index_by_NID(name, NID_commonName, i)) { | |
e34763f4 | 181 | |
4d16918e CT |
182 | ASN1_STRING *cn_data = X509_NAME_ENTRY_get_data(X509_NAME_get_entry(name, i)); |
183 | ||
184 | if ( (*check_func)(check_data, cn_data) == 0) | |
185 | return 1; | |
186 | } | |
187 | ||
188 | STACK_OF(GENERAL_NAME) * altnames; | |
189 | altnames = (STACK_OF(GENERAL_NAME)*)X509_get_ext_d2i(peer_cert, NID_subject_alt_name, NULL, NULL); | |
190 | ||
191 | if (altnames) { | |
192 | int numalts = sk_GENERAL_NAME_num(altnames); | |
d7ae3534 | 193 | for (int i = 0; i < numalts; ++i) { |
4d16918e CT |
194 | const GENERAL_NAME *check = sk_GENERAL_NAME_value(altnames, i); |
195 | if (check->type != GEN_DNS) { | |
196 | continue; | |
197 | } | |
198 | ASN1_STRING *cn_data = check->d.dNSName; | |
e34763f4 | 199 | |
40dd2b59 CT |
200 | if ( (*check_func)(check_data, cn_data) == 0) { |
201 | sk_GENERAL_NAME_pop_free(altnames, GENERAL_NAME_free); | |
4d16918e | 202 | return 1; |
40dd2b59 | 203 | } |
4d16918e CT |
204 | } |
205 | sk_GENERAL_NAME_pop_free(altnames, GENERAL_NAME_free); | |
206 | } | |
207 | return 0; | |
208 | } | |
209 | ||
210 | static int check_domain( void *check_data, ASN1_STRING *cn_data) | |
211 | { | |
212 | char cn[1024]; | |
213 | const char *server = (const char *)check_data; | |
214 | ||
e34763f4 | 215 | if (cn_data->length > (int)sizeof(cn) - 1) { |
4d16918e CT |
216 | return 1; //if does not fit our buffer just ignore |
217 | } | |
218 | memcpy(cn, cn_data->data, cn_data->length); | |
219 | cn[cn_data->length] = '\0'; | |
220 | debugs(83, 4, "Verifying server domain " << server << " to certificate name/subjectAltName " << cn); | |
221 | return matchDomainName(server, cn[0] == '*' ? cn + 1 : cn); | |
222 | } | |
223 | ||
8eb0a7ee CT |
224 | bool Ssl::checkX509ServerValidity(X509 *cert, const char *server) |
225 | { | |
226 | return matchX509CommonNames(cert, (void *)server, check_domain); | |
227 | } | |
228 | ||
63be0a78 | 229 | /// \ingroup ServerProtocolSSLInternal |
f484cdf5 | 230 | static int |
231 | ssl_verify_cb(int ok, X509_STORE_CTX * ctx) | |
232 | { | |
7698a79c CT |
233 | // preserve original ctx->error before SSL_ calls can overwrite it |
234 | Ssl::ssl_error_t error_no = ok ? SSL_ERROR_NONE : ctx->error; | |
235 | ||
236 | char buffer[256] = ""; | |
a7ad6e4e | 237 | SSL *ssl = (SSL *)X509_STORE_CTX_get_ex_data(ctx, SSL_get_ex_data_X509_STORE_CTX_idx()); |
238 | SSL_CTX *sslctx = SSL_get_SSL_CTX(ssl); | |
239 | const char *server = (const char *)SSL_get_ex_data(ssl, ssl_ex_index_server); | |
240 | void *dont_verify_domain = SSL_CTX_get_ex_data(sslctx, ssl_ctx_ex_index_dont_verify_domain); | |
815eaa44 | 241 | ACLChecklist *check = (ACLChecklist*)SSL_get_ex_data(ssl, ssl_ex_index_cert_error_check); |
e7bcc25f | 242 | X509 *peeked_cert = (X509 *)SSL_get_ex_data(ssl, ssl_ex_index_ssl_peeked_cert); |
a7ad6e4e | 243 | X509 *peer_cert = ctx->cert; |
f484cdf5 | 244 | |
a7ad6e4e | 245 | X509_NAME_oneline(X509_get_subject_name(peer_cert), buffer, |
62e76326 | 246 | sizeof(buffer)); |
a7ad6e4e | 247 | |
0ad3ff51 CT |
248 | // detect infinite loops |
249 | uint32_t *validationCounter = static_cast<uint32_t *>(SSL_get_ex_data(ssl, ssl_ex_index_ssl_validation_counter)); | |
250 | if (!validationCounter) { | |
251 | validationCounter = new uint32_t(1); | |
252 | SSL_set_ex_data(ssl, ssl_ex_index_ssl_validation_counter, validationCounter); | |
253 | } else { | |
254 | // overflows allowed if SQUID_CERT_VALIDATION_ITERATION_MAX >= UINT32_MAX | |
255 | (*validationCounter)++; | |
256 | } | |
257 | ||
258 | if ((*validationCounter) >= SQUID_CERT_VALIDATION_ITERATION_MAX) { | |
259 | ok = 0; // or the validation loop will never stop | |
260 | error_no = SQUID_X509_V_ERR_INFINITE_VALIDATION; | |
261 | debugs(83, 2, "SQUID_X509_V_ERR_INFINITE_VALIDATION: " << | |
262 | *validationCounter << " iterations while checking " << buffer); | |
263 | } | |
264 | ||
a7ad6e4e | 265 | if (ok) { |
bf8fe701 | 266 | debugs(83, 5, "SSL Certificate signature OK: " << buffer); |
62e76326 | 267 | |
4747ea4c CT |
268 | // Check for domain mismatch only if the current certificate is the peer certificate. |
269 | if (server && peer_cert == X509_STORE_CTX_get_current_cert(ctx)) { | |
8eb0a7ee | 270 | if (!Ssl::checkX509ServerValidity(peer_cert, server)) { |
815eaa44 | 271 | debugs(83, 2, "SQUID_X509_V_ERR_DOMAIN_MISMATCH: Certificate " << buffer << " does not match domainname " << server); |
62e76326 | 272 | ok = 0; |
4d16918e | 273 | error_no = SQUID_X509_V_ERR_DOMAIN_MISMATCH; |
62e76326 | 274 | } |
275 | } | |
7698a79c | 276 | } |
0db46e4f | 277 | |
e7bcc25f | 278 | if (ok && peeked_cert) { |
7a957a93 | 279 | // Check whether the already peeked certificate matches the new one. |
e7bcc25f CT |
280 | if (X509_cmp(peer_cert, peeked_cert) != 0) { |
281 | debugs(83, 2, "SQUID_X509_V_ERR_CERT_CHANGE: Certificate " << buffer << " does not match peeked certificate"); | |
282 | ok = 0; | |
283 | error_no = SQUID_X509_V_ERR_CERT_CHANGE; | |
284 | } | |
285 | } | |
286 | ||
7698a79c | 287 | if (!ok) { |
62a7607e CT |
288 | X509 *broken_cert = X509_STORE_CTX_get_current_cert(ctx); |
289 | if (!broken_cert) | |
290 | broken_cert = peer_cert; | |
291 | ||
292 | Ssl::CertErrors *errs = static_cast<Ssl::CertErrors *>(SSL_get_ex_data(ssl, ssl_ex_index_ssl_errors)); | |
7a957a93 | 293 | if (!errs) { |
62a7607e | 294 | errs = new Ssl::CertErrors(Ssl::CertError(error_no, broken_cert)); |
7a957a93 | 295 | if (!SSL_set_ex_data(ssl, ssl_ex_index_ssl_errors, (void *)errs)) { |
fb2178bb | 296 | debugs(83, 2, "Failed to set ssl error_no in ssl_verify_cb: Certificate " << buffer); |
7a957a93 AR |
297 | delete errs; |
298 | errs = NULL; | |
fb2178bb | 299 | } |
87f237a9 | 300 | } else // remember another error number |
62a7607e | 301 | errs->push_back_unique(Ssl::CertError(error_no, broken_cert)); |
fb2178bb | 302 | |
7698a79c | 303 | if (const char *err_descr = Ssl::GetErrorDescr(error_no)) |
cf09bec7 CT |
304 | debugs(83, 5, err_descr << ": " << buffer); |
305 | else | |
7698a79c CT |
306 | debugs(83, DBG_IMPORTANT, "SSL unknown certificate error " << error_no << " in " << buffer); |
307 | ||
0ad3ff51 CT |
308 | // Check if the certificate error can be bypassed. |
309 | // Infinity validation loop errors can not bypassed. | |
310 | if (error_no != SQUID_X509_V_ERR_INFINITE_VALIDATION) { | |
311 | if (check) { | |
312 | ACLFilledChecklist *filledCheck = Filled(check); | |
313 | assert(!filledCheck->sslErrors); | |
314 | filledCheck->sslErrors = new Ssl::CertErrors(Ssl::CertError(error_no, broken_cert)); | |
315 | filledCheck->serverCert.resetAndLock(peer_cert); | |
316 | if (check->fastCheck() == ACCESS_ALLOWED) { | |
317 | debugs(83, 3, "bypassing SSL error " << error_no << " in " << buffer); | |
318 | ok = 1; | |
319 | } else { | |
320 | debugs(83, 5, "confirming SSL error " << error_no); | |
321 | } | |
322 | delete filledCheck->sslErrors; | |
323 | filledCheck->sslErrors = NULL; | |
324 | filledCheck->serverCert.reset(NULL); | |
7698a79c | 325 | } |
0ad3ff51 CT |
326 | // If the certificate validator is used then we need to allow all errors and |
327 | // pass them to certficate validator for more processing | |
328 | else if (Ssl::TheConfig.ssl_crt_validator) { | |
329 | ok = 1; | |
330 | // Check if we have stored certificates chain. Store if not. | |
331 | if (!SSL_get_ex_data(ssl, ssl_ex_index_ssl_cert_chain)) { | |
332 | STACK_OF(X509) *certStack = X509_STORE_CTX_get1_chain(ctx); | |
333 | if (certStack && !SSL_set_ex_data(ssl, ssl_ex_index_ssl_cert_chain, certStack)) | |
334 | sk_X509_pop_free(certStack, X509_free); | |
335 | } | |
4747ea4c CT |
336 | } |
337 | } | |
a7ad6e4e | 338 | } |
62e76326 | 339 | |
26ac0430 | 340 | if (!dont_verify_domain && server) {} |
62e76326 | 341 | |
7698a79c | 342 | if (!ok && !SSL_get_ex_data(ssl, ssl_ex_index_ssl_error_detail) ) { |
f622c461 MF |
343 | |
344 | // Find the broken certificate. It may be intermediate. | |
345 | X509 *broken_cert = peer_cert; // reasonable default if search fails | |
346 | // Our SQUID_X509_V_ERR_DOMAIN_MISMATCH implies peer_cert is at fault. | |
347 | if (error_no != SQUID_X509_V_ERR_DOMAIN_MISMATCH) { | |
348 | if (X509 *last_used_cert = X509_STORE_CTX_get_current_cert(ctx)) | |
349 | broken_cert = last_used_cert; | |
350 | } | |
351 | ||
352 | Ssl::ErrorDetail *errDetail = | |
de878a55 | 353 | new Ssl::ErrorDetail(error_no, peer_cert, broken_cert); |
f622c461 | 354 | |
e34763f4 | 355 | if (!SSL_set_ex_data(ssl, ssl_ex_index_ssl_error_detail, errDetail)) { |
4d16918e CT |
356 | debugs(83, 2, "Failed to set Ssl::ErrorDetail in ssl_verify_cb: Certificate " << buffer); |
357 | delete errDetail; | |
358 | } | |
359 | } | |
360 | ||
f484cdf5 | 361 | return ok; |
362 | } | |
363 | ||
63be0a78 | 364 | /// \ingroup ServerProtocolSSLInternal |
26ac0430 | 365 | static struct ssl_option { |
79d4ccdf | 366 | const char *name; |
367 | long value; | |
62e76326 | 368 | } |
369 | ||
370 | ssl_options[] = { | |
79d4ccdf | 371 | |
32d002cb | 372 | #if SSL_OP_MICROSOFT_SESS_ID_BUG |
26ac0430 AJ |
373 | { |
374 | "MICROSOFT_SESS_ID_BUG", SSL_OP_MICROSOFT_SESS_ID_BUG | |
375 | }, | |
673cd7db | 376 | #endif |
32d002cb | 377 | #if SSL_OP_NETSCAPE_CHALLENGE_BUG |
26ac0430 AJ |
378 | { |
379 | "NETSCAPE_CHALLENGE_BUG", SSL_OP_NETSCAPE_CHALLENGE_BUG | |
380 | }, | |
673cd7db | 381 | #endif |
32d002cb | 382 | #if SSL_OP_NETSCAPE_REUSE_CIPHER_CHANGE_BUG |
26ac0430 AJ |
383 | { |
384 | "NETSCAPE_REUSE_CIPHER_CHANGE_BUG", SSL_OP_NETSCAPE_REUSE_CIPHER_CHANGE_BUG | |
385 | }, | |
673cd7db | 386 | #endif |
32d002cb | 387 | #if SSL_OP_SSLREF2_REUSE_CERT_TYPE_BUG |
26ac0430 AJ |
388 | { |
389 | "SSLREF2_REUSE_CERT_TYPE_BUG", SSL_OP_SSLREF2_REUSE_CERT_TYPE_BUG | |
390 | }, | |
673cd7db | 391 | #endif |
32d002cb | 392 | #if SSL_OP_MICROSOFT_BIG_SSLV3_BUFFER |
26ac0430 AJ |
393 | { |
394 | "MICROSOFT_BIG_SSLV3_BUFFER", SSL_OP_MICROSOFT_BIG_SSLV3_BUFFER | |
395 | }, | |
673cd7db | 396 | #endif |
32d002cb | 397 | #if SSL_OP_MSIE_SSLV2_RSA_PADDING |
26ac0430 AJ |
398 | { |
399 | "MSIE_SSLV2_RSA_PADDING", SSL_OP_MSIE_SSLV2_RSA_PADDING | |
400 | }, | |
673cd7db | 401 | #endif |
32d002cb | 402 | #if SSL_OP_SSLEAY_080_CLIENT_DH_BUG |
26ac0430 AJ |
403 | { |
404 | "SSLEAY_080_CLIENT_DH_BUG", SSL_OP_SSLEAY_080_CLIENT_DH_BUG | |
405 | }, | |
673cd7db | 406 | #endif |
32d002cb | 407 | #if SSL_OP_TLS_D5_BUG |
26ac0430 AJ |
408 | { |
409 | "TLS_D5_BUG", SSL_OP_TLS_D5_BUG | |
410 | }, | |
673cd7db | 411 | #endif |
32d002cb | 412 | #if SSL_OP_TLS_BLOCK_PADDING_BUG |
26ac0430 AJ |
413 | { |
414 | "TLS_BLOCK_PADDING_BUG", SSL_OP_TLS_BLOCK_PADDING_BUG | |
415 | }, | |
673cd7db | 416 | #endif |
32d002cb | 417 | #if SSL_OP_TLS_ROLLBACK_BUG |
26ac0430 AJ |
418 | { |
419 | "TLS_ROLLBACK_BUG", SSL_OP_TLS_ROLLBACK_BUG | |
420 | }, | |
673cd7db | 421 | #endif |
32d002cb | 422 | #if SSL_OP_ALL |
26ac0430 | 423 | { |
866eafc6 | 424 | "ALL", (long)SSL_OP_ALL |
26ac0430 | 425 | }, |
673cd7db | 426 | #endif |
32d002cb | 427 | #if SSL_OP_SINGLE_DH_USE |
26ac0430 AJ |
428 | { |
429 | "SINGLE_DH_USE", SSL_OP_SINGLE_DH_USE | |
430 | }, | |
673cd7db | 431 | #endif |
32d002cb | 432 | #if SSL_OP_EPHEMERAL_RSA |
26ac0430 AJ |
433 | { |
434 | "EPHEMERAL_RSA", SSL_OP_EPHEMERAL_RSA | |
435 | }, | |
673cd7db | 436 | #endif |
32d002cb | 437 | #if SSL_OP_PKCS1_CHECK_1 |
26ac0430 AJ |
438 | { |
439 | "PKCS1_CHECK_1", SSL_OP_PKCS1_CHECK_1 | |
440 | }, | |
673cd7db | 441 | #endif |
32d002cb | 442 | #if SSL_OP_PKCS1_CHECK_2 |
26ac0430 AJ |
443 | { |
444 | "PKCS1_CHECK_2", SSL_OP_PKCS1_CHECK_2 | |
445 | }, | |
673cd7db | 446 | #endif |
32d002cb | 447 | #if SSL_OP_NETSCAPE_CA_DN_BUG |
26ac0430 AJ |
448 | { |
449 | "NETSCAPE_CA_DN_BUG", SSL_OP_NETSCAPE_CA_DN_BUG | |
450 | }, | |
673cd7db | 451 | #endif |
32d002cb | 452 | #if SSL_OP_NON_EXPORT_FIRST |
26ac0430 AJ |
453 | { |
454 | "NON_EXPORT_FIRST", SSL_OP_NON_EXPORT_FIRST | |
455 | }, | |
673cd7db | 456 | #endif |
32d002cb | 457 | #if SSL_OP_CIPHER_SERVER_PREFERENCE |
26ac0430 AJ |
458 | { |
459 | "CIPHER_SERVER_PREFERENCE", SSL_OP_CIPHER_SERVER_PREFERENCE | |
460 | }, | |
673cd7db | 461 | #endif |
32d002cb | 462 | #if SSL_OP_NETSCAPE_DEMO_CIPHER_CHANGE_BUG |
26ac0430 AJ |
463 | { |
464 | "NETSCAPE_DEMO_CIPHER_CHANGE_BUG", SSL_OP_NETSCAPE_DEMO_CIPHER_CHANGE_BUG | |
465 | }, | |
673cd7db | 466 | #endif |
32d002cb | 467 | #if SSL_OP_NO_SSLv2 |
26ac0430 AJ |
468 | { |
469 | "NO_SSLv2", SSL_OP_NO_SSLv2 | |
470 | }, | |
673cd7db | 471 | #endif |
32d002cb | 472 | #if SSL_OP_NO_SSLv3 |
26ac0430 AJ |
473 | { |
474 | "NO_SSLv3", SSL_OP_NO_SSLv3 | |
475 | }, | |
673cd7db | 476 | #endif |
32d002cb | 477 | #if SSL_OP_NO_TLSv1 |
26ac0430 AJ |
478 | { |
479 | "NO_TLSv1", SSL_OP_NO_TLSv1 | |
480 | }, | |
3d96b0e8 AJ |
481 | #endif |
482 | #if SSL_OP_NO_TLSv1_1 | |
483 | { | |
484 | "NO_TLSv1_1", SSL_OP_NO_TLSv1_1 | |
485 | }, | |
486 | #endif | |
487 | #if SSL_OP_NO_TLSv1_2 | |
488 | { | |
489 | "NO_TLSv1_2", SSL_OP_NO_TLSv1_2 | |
490 | }, | |
71606c88 SW |
491 | #endif |
492 | #if SSL_OP_NO_COMPRESSION | |
493 | { | |
494 | "No_Compression", SSL_OP_NO_COMPRESSION | |
495 | }, | |
673cd7db | 496 | #endif |
26ac0430 AJ |
497 | { |
498 | "", 0 | |
499 | }, | |
500 | { | |
501 | NULL, 0 | |
502 | } | |
503 | }; | |
79d4ccdf | 504 | |
63be0a78 | 505 | /// \ingroup ServerProtocolSSLInternal |
86660d64 CT |
506 | long |
507 | Ssl::parse_options(const char *options) | |
79d4ccdf | 508 | { |
943c5f16 | 509 | long op = 0; |
79d4ccdf | 510 | char *tmp; |
511 | char *option; | |
512 | ||
513 | if (!options) | |
62e76326 | 514 | goto no_options; |
79d4ccdf | 515 | |
516 | tmp = xstrdup(options); | |
62e76326 | 517 | |
79d4ccdf | 518 | option = strtok(tmp, ":,"); |
62e76326 | 519 | |
79d4ccdf | 520 | while (option) { |
62e76326 | 521 | |
522 | struct ssl_option *opt = NULL, *opttmp; | |
523 | long value = 0; | |
524 | enum { | |
525 | MODE_ADD, MODE_REMOVE | |
526 | } mode; | |
527 | ||
528 | switch (*option) { | |
529 | ||
530 | case '!': | |
531 | ||
532 | case '-': | |
533 | mode = MODE_REMOVE; | |
d7ae3534 | 534 | ++option; |
62e76326 | 535 | break; |
536 | ||
537 | case '+': | |
538 | mode = MODE_ADD; | |
d7ae3534 | 539 | ++option; |
62e76326 | 540 | break; |
541 | ||
542 | default: | |
543 | mode = MODE_ADD; | |
544 | break; | |
545 | } | |
546 | ||
d7ae3534 | 547 | for (opttmp = ssl_options; opttmp->name; ++opttmp) { |
62e76326 | 548 | if (strcmp(opttmp->name, option) == 0) { |
549 | opt = opttmp; | |
550 | break; | |
551 | } | |
552 | } | |
553 | ||
554 | if (opt) | |
555 | value = opt->value; | |
556 | else if (strncmp(option, "0x", 2) == 0) { | |
557 | /* Special case.. hex specification */ | |
558 | value = strtol(option + 2, NULL, 16); | |
559 | } else { | |
560 | fatalf("Unknown SSL option '%s'", option); | |
561 | value = 0; /* Keep GCC happy */ | |
562 | } | |
563 | ||
564 | switch (mode) { | |
565 | ||
566 | case MODE_ADD: | |
567 | op |= value; | |
568 | break; | |
569 | ||
570 | case MODE_REMOVE: | |
571 | op &= ~value; | |
572 | break; | |
573 | } | |
574 | ||
575 | option = strtok(NULL, ":,"); | |
79d4ccdf | 576 | } |
577 | ||
578 | safe_free(tmp); | |
62e76326 | 579 | |
580 | no_options: | |
79d4ccdf | 581 | return op; |
582 | } | |
583 | ||
63be0a78 | 584 | /// \ingroup ServerProtocolSSLInternal |
a7ad6e4e | 585 | #define SSL_FLAG_NO_DEFAULT_CA (1<<0) |
63be0a78 | 586 | /// \ingroup ServerProtocolSSLInternal |
a7ad6e4e | 587 | #define SSL_FLAG_DELAYED_AUTH (1<<1) |
63be0a78 | 588 | /// \ingroup ServerProtocolSSLInternal |
a7ad6e4e | 589 | #define SSL_FLAG_DONT_VERIFY_PEER (1<<2) |
63be0a78 | 590 | /// \ingroup ServerProtocolSSLInternal |
a7ad6e4e | 591 | #define SSL_FLAG_DONT_VERIFY_DOMAIN (1<<3) |
63be0a78 | 592 | /// \ingroup ServerProtocolSSLInternal |
b13877cc | 593 | #define SSL_FLAG_NO_SESSION_REUSE (1<<4) |
63be0a78 | 594 | /// \ingroup ServerProtocolSSLInternal |
a82a4fe4 | 595 | #define SSL_FLAG_VERIFY_CRL (1<<5) |
63be0a78 | 596 | /// \ingroup ServerProtocolSSLInternal |
a82a4fe4 | 597 | #define SSL_FLAG_VERIFY_CRL_ALL (1<<6) |
a7ad6e4e | 598 | |
63be0a78 | 599 | /// \ingroup ServerProtocolSSLInternal |
86660d64 CT |
600 | long |
601 | Ssl::parse_flags(const char *flags) | |
a7ad6e4e | 602 | { |
603 | long fl = 0; | |
604 | char *tmp; | |
605 | char *flag; | |
606 | ||
607 | if (!flags) | |
62e76326 | 608 | return 0; |
a7ad6e4e | 609 | |
610 | tmp = xstrdup(flags); | |
62e76326 | 611 | |
a7ad6e4e | 612 | flag = strtok(tmp, ":,"); |
62e76326 | 613 | |
a7ad6e4e | 614 | while (flag) { |
62e76326 | 615 | if (strcmp(flag, "NO_DEFAULT_CA") == 0) |
616 | fl |= SSL_FLAG_NO_DEFAULT_CA; | |
617 | else if (strcmp(flag, "DELAYED_AUTH") == 0) | |
618 | fl |= SSL_FLAG_DELAYED_AUTH; | |
619 | else if (strcmp(flag, "DONT_VERIFY_PEER") == 0) | |
620 | fl |= SSL_FLAG_DONT_VERIFY_PEER; | |
621 | else if (strcmp(flag, "DONT_VERIFY_DOMAIN") == 0) | |
622 | fl |= SSL_FLAG_DONT_VERIFY_DOMAIN; | |
b13877cc | 623 | else if (strcmp(flag, "NO_SESSION_REUSE") == 0) |
624 | fl |= SSL_FLAG_NO_SESSION_REUSE; | |
a82a4fe4 | 625 | |
32d002cb | 626 | #if X509_V_FLAG_CRL_CHECK |
a82a4fe4 | 627 | |
628 | else if (strcmp(flag, "VERIFY_CRL") == 0) | |
629 | fl |= SSL_FLAG_VERIFY_CRL; | |
630 | else if (strcmp(flag, "VERIFY_CRL_ALL") == 0) | |
631 | fl |= SSL_FLAG_VERIFY_CRL_ALL; | |
632 | ||
633 | #endif | |
634 | ||
62e76326 | 635 | else |
636 | fatalf("Unknown ssl flag '%s'", flag); | |
637 | ||
638 | flag = strtok(NULL, ":,"); | |
a7ad6e4e | 639 | } |
62e76326 | 640 | |
a7ad6e4e | 641 | safe_free(tmp); |
642 | return fl; | |
643 | } | |
644 | ||
815eaa44 | 645 | // "dup" function for SSL_get_ex_new_index("cert_err_check") |
646 | static int | |
647 | ssl_dupAclChecklist(CRYPTO_EX_DATA *, CRYPTO_EX_DATA *, void *, | |
26ac0430 AJ |
648 | int, long, void *) |
649 | { | |
815eaa44 | 650 | // We do not support duplication of ACLCheckLists. |
651 | // If duplication is needed, we can count copies with cbdata. | |
652 | assert(false); | |
653 | return 0; | |
654 | } | |
655 | ||
656 | // "free" function for SSL_get_ex_new_index("cert_err_check") | |
657 | static void | |
658 | ssl_freeAclChecklist(void *, void *ptr, CRYPTO_EX_DATA *, | |
26ac0430 AJ |
659 | int, long, void *) |
660 | { | |
815eaa44 | 661 | delete static_cast<ACLChecklist *>(ptr); // may be NULL |
662 | } | |
a7ad6e4e | 663 | |
4d16918e CT |
664 | // "free" function for SSL_get_ex_new_index("ssl_error_detail") |
665 | static void | |
666 | ssl_free_ErrorDetail(void *, void *ptr, CRYPTO_EX_DATA *, | |
667 | int, long, void *) | |
668 | { | |
669 | Ssl::ErrorDetail *errDetail = static_cast <Ssl::ErrorDetail *>(ptr); | |
670 | delete errDetail; | |
671 | } | |
672 | ||
fb2178bb | 673 | static void |
7a957a93 | 674 | ssl_free_SslErrors(void *, void *ptr, CRYPTO_EX_DATA *, |
87f237a9 | 675 | int, long, void *) |
fb2178bb | 676 | { |
62a7607e | 677 | Ssl::CertErrors *errs = static_cast <Ssl::CertErrors*>(ptr); |
7a957a93 | 678 | delete errs; |
fb2178bb CT |
679 | } |
680 | ||
0ad3ff51 CT |
681 | // "free" function for SSL_get_ex_new_index("ssl_ex_index_ssl_validation_counter") |
682 | static void | |
683 | ssl_free_int(void *, void *ptr, CRYPTO_EX_DATA *, | |
3cc296c4 | 684 | int, long, void *) |
0ad3ff51 CT |
685 | { |
686 | uint32_t *counter = static_cast <uint32_t *>(ptr); | |
687 | delete counter; | |
688 | } | |
689 | ||
4747ea4c CT |
690 | /// \ingroup ServerProtocolSSLInternal |
691 | /// Callback handler function to release STACK_OF(X509) "ex" data stored | |
692 | /// in an SSL object. | |
693 | static void | |
694 | ssl_free_CertChain(void *, void *ptr, CRYPTO_EX_DATA *, | |
695 | int, long, void *) | |
696 | { | |
697 | STACK_OF(X509) *certsChain = static_cast <STACK_OF(X509) *>(ptr); | |
698 | sk_X509_pop_free(certsChain,X509_free); | |
699 | } | |
700 | ||
e7bcc25f CT |
701 | // "free" function for X509 certificates |
702 | static void | |
703 | ssl_free_X509(void *, void *ptr, CRYPTO_EX_DATA *, | |
87f237a9 | 704 | int, long, void *) |
e7bcc25f CT |
705 | { |
706 | X509 *cert = static_cast <X509 *>(ptr); | |
707 | X509_free(cert); | |
708 | } | |
709 | ||
63be0a78 | 710 | /// \ingroup ServerProtocolSSLInternal |
a7ad6e4e | 711 | static void |
712 | ssl_initialize(void) | |
f484cdf5 | 713 | { |
56a35ad1 AR |
714 | static bool initialized = false; |
715 | if (initialized) | |
716 | return; | |
717 | initialized = true; | |
62e76326 | 718 | |
56a35ad1 AR |
719 | SSL_load_error_strings(); |
720 | SSLeay_add_ssl_algorithms(); | |
62e76326 | 721 | |
56a35ad1 AR |
722 | #if HAVE_OPENSSL_ENGINE_H |
723 | if (Config.SSL.ssl_engine) { | |
724 | ENGINE *e; | |
725 | if (!(e = ENGINE_by_id(Config.SSL.ssl_engine))) | |
726 | fatalf("Unable to find SSL engine '%s'\n", Config.SSL.ssl_engine); | |
727 | ||
728 | if (!ENGINE_set_default(e, ENGINE_METHOD_ALL)) { | |
729 | int ssl_error = ERR_get_error(); | |
730 | fatalf("Failed to initialise SSL engine: %s\n", ERR_error_string(ssl_error, NULL)); | |
62e76326 | 731 | } |
56a35ad1 | 732 | } |
a7ad6e4e | 733 | #else |
56a35ad1 AR |
734 | if (Config.SSL.ssl_engine) |
735 | fatalf("Your OpenSSL has no SSL engine support\n"); | |
a7ad6e4e | 736 | #endif |
62e76326 | 737 | |
a7ad6e4e | 738 | ssl_ex_index_server = SSL_get_ex_new_index(0, (void *) "server", NULL, NULL, NULL); |
739 | ssl_ctx_ex_index_dont_verify_domain = SSL_CTX_get_ex_new_index(0, (void *) "dont_verify_domain", NULL, NULL, NULL); | |
815eaa44 | 740 | ssl_ex_index_cert_error_check = SSL_get_ex_new_index(0, (void *) "cert_error_check", NULL, &ssl_dupAclChecklist, &ssl_freeAclChecklist); |
4d16918e | 741 | ssl_ex_index_ssl_error_detail = SSL_get_ex_new_index(0, (void *) "ssl_error_detail", NULL, NULL, &ssl_free_ErrorDetail); |
e7bcc25f | 742 | ssl_ex_index_ssl_peeked_cert = SSL_get_ex_new_index(0, (void *) "ssl_peeked_cert", NULL, NULL, &ssl_free_X509); |
7a957a93 | 743 | ssl_ex_index_ssl_errors = SSL_get_ex_new_index(0, (void *) "ssl_errors", NULL, NULL, &ssl_free_SslErrors); |
4747ea4c | 744 | ssl_ex_index_ssl_cert_chain = SSL_get_ex_new_index(0, (void *) "ssl_cert_chain", NULL, NULL, &ssl_free_CertChain); |
0ad3ff51 | 745 | ssl_ex_index_ssl_validation_counter = SSL_get_ex_new_index(0, (void *) "ssl_validation_counter", NULL, NULL, &ssl_free_int); |
a7ad6e4e | 746 | } |
747 | ||
63be0a78 | 748 | /// \ingroup ServerProtocolSSLInternal |
a82a4fe4 | 749 | static int |
750 | ssl_load_crl(SSL_CTX *sslContext, const char *CRLfile) | |
751 | { | |
752 | X509_STORE *st = SSL_CTX_get_cert_store(sslContext); | |
753 | X509_CRL *crl; | |
754 | BIO *in = BIO_new_file(CRLfile, "r"); | |
755 | int count = 0; | |
756 | ||
757 | if (!in) { | |
bf8fe701 | 758 | debugs(83, 2, "WARNING: Failed to open CRL file '" << CRLfile << "'"); |
a82a4fe4 | 759 | return 0; |
760 | } | |
761 | ||
762 | while ((crl = PEM_read_bio_X509_CRL(in,NULL,NULL,NULL))) { | |
763 | if (!X509_STORE_add_crl(st, crl)) | |
bf8fe701 | 764 | debugs(83, 2, "WARNING: Failed to add CRL from file '" << CRLfile << "'"); |
a82a4fe4 | 765 | else |
d7ae3534 | 766 | ++count; |
a82a4fe4 | 767 | |
768 | X509_CRL_free(crl); | |
769 | } | |
770 | ||
771 | BIO_free(in); | |
772 | return count; | |
773 | } | |
774 | ||
86660d64 CT |
775 | STACK_OF(X509_CRL) * |
776 | Ssl::loadCrl(const char *CRLFile, long &flags) | |
a7ad6e4e | 777 | { |
86660d64 CT |
778 | X509_CRL *crl; |
779 | BIO *in = BIO_new_file(CRLFile, "r"); | |
780 | if (!in) { | |
781 | debugs(83, 2, "WARNING: Failed to open CRL file '" << CRLFile << "'"); | |
f4e4d4d6 CT |
782 | return NULL; |
783 | } | |
784 | ||
86660d64 CT |
785 | STACK_OF(X509_CRL) *CRLs = sk_X509_CRL_new_null(); |
786 | if (!CRLs) { | |
787 | debugs(83, 2, "WARNING: Failed to allocate X509_CRL stack to load file '" << CRLFile << "'"); | |
50b4c080 | 788 | return NULL; |
86660d64 | 789 | } |
62e76326 | 790 | |
86660d64 CT |
791 | int count = 0; |
792 | while ((crl = PEM_read_bio_X509_CRL(in,NULL,NULL,NULL))) { | |
793 | if (!sk_X509_CRL_push(CRLs, crl)) | |
794 | debugs(83, 2, "WARNING: Failed to add CRL from file '" << CRLFile << "'"); | |
795 | else | |
796 | ++count; | |
797 | } | |
798 | BIO_free(in); | |
62e76326 | 799 | |
86660d64 CT |
800 | if (count) |
801 | flags |= SSL_FLAG_VERIFY_CRL; | |
3d96b0e8 | 802 | |
86660d64 CT |
803 | return CRLs; |
804 | } | |
3d96b0e8 | 805 | |
86660d64 CT |
806 | DH * |
807 | Ssl::readDHParams(const char *dhfile) | |
808 | { | |
809 | FILE *in = fopen(dhfile, "r"); | |
810 | DH *dh = NULL; | |
811 | int codes; | |
62e76326 | 812 | |
86660d64 CT |
813 | if (in) { |
814 | dh = PEM_read_DHparams(in, NULL, NULL, NULL); | |
815 | fclose(in); | |
f484cdf5 | 816 | } |
817 | ||
86660d64 CT |
818 | if (!dh) |
819 | debugs(83, DBG_IMPORTANT, "WARNING: Failed to read DH parameters '" << dhfile << "'"); | |
820 | else if (dh && DH_check(dh, &codes) == 0) { | |
821 | if (codes) { | |
822 | debugs(83, DBG_IMPORTANT, "WARNING: Failed to verify DH parameters '" << dhfile << "' (" << std::hex << codes << ")"); | |
823 | DH_free(dh); | |
824 | dh = NULL; | |
825 | } | |
f484cdf5 | 826 | } |
86660d64 CT |
827 | return dh; |
828 | } | |
62e76326 | 829 | |
86660d64 CT |
830 | static bool |
831 | configureSslContext(SSL_CTX *sslContext, AnyP::PortCfg &port) | |
832 | { | |
833 | int ssl_error; | |
834 | SSL_CTX_set_options(sslContext, port.sslOptions); | |
f484cdf5 | 835 | |
86660d64 CT |
836 | if (port.sslContextSessionId) |
837 | SSL_CTX_set_session_id_context(sslContext, (const unsigned char *)port.sslContextSessionId, strlen(port.sslContextSessionId)); | |
6b2936d5 | 838 | |
86660d64 | 839 | if (port.sslContextFlags & SSL_FLAG_NO_SESSION_REUSE) { |
b13877cc | 840 | SSL_CTX_set_session_cache_mode(sslContext, SSL_SESS_CACHE_OFF); |
841 | } | |
842 | ||
a9d79803 | 843 | if (Config.SSL.unclean_shutdown) { |
bf8fe701 | 844 | debugs(83, 5, "Enabling quiet SSL shutdowns (RFC violation)."); |
a9d79803 | 845 | |
846 | SSL_CTX_set_quiet_shutdown(sslContext, 1); | |
847 | } | |
848 | ||
86660d64 CT |
849 | if (port.cipher) { |
850 | debugs(83, 5, "Using chiper suite " << port.cipher << "."); | |
62e76326 | 851 | |
86660d64 | 852 | if (!SSL_CTX_set_cipher_list(sslContext, port.cipher)) { |
62e76326 | 853 | ssl_error = ERR_get_error(); |
86660d64 CT |
854 | debugs(83, DBG_CRITICAL, "ERROR: Failed to set SSL cipher suite '" << port.cipher << "': " << ERR_error_string(ssl_error, NULL)); |
855 | return false; | |
62e76326 | 856 | } |
79d4ccdf | 857 | } |
62e76326 | 858 | |
bf8fe701 | 859 | debugs(83, 9, "Setting RSA key generation callback."); |
a7ad6e4e | 860 | SSL_CTX_set_tmp_rsa_callback(sslContext, ssl_temp_rsa_cb); |
861 | ||
bf8fe701 | 862 | debugs(83, 9, "Setting CA certificate locations."); |
62e76326 | 863 | |
86660d64 CT |
864 | const char *cafile = port.cafile ? port.cafile : port.clientca; |
865 | if ((cafile || port.capath) && !SSL_CTX_load_verify_locations(sslContext, cafile, port.capath)) { | |
62e76326 | 866 | ssl_error = ERR_get_error(); |
48e7baac | 867 | debugs(83, DBG_IMPORTANT, "WARNING: Ignoring error setting CA certificate locations: " << ERR_error_string(ssl_error, NULL)); |
a7ad6e4e | 868 | } |
62e76326 | 869 | |
86660d64 | 870 | if (!(port.sslContextFlags & SSL_FLAG_NO_DEFAULT_CA) && |
62e76326 | 871 | !SSL_CTX_set_default_verify_paths(sslContext)) { |
872 | ssl_error = ERR_get_error(); | |
48e7baac | 873 | debugs(83, DBG_IMPORTANT, "WARNING: Ignoring error setting default CA certificate location: " << ERR_error_string(ssl_error, NULL)); |
a7ad6e4e | 874 | } |
62e76326 | 875 | |
86660d64 | 876 | if (port.clientCA.get()) { |
8c1ff4ef | 877 | ERR_clear_error(); |
86660d64 | 878 | SSL_CTX_set_client_CA_list(sslContext, port.clientCA.get()); |
62e76326 | 879 | |
86660d64 | 880 | if (port.sslContextFlags & SSL_FLAG_DELAYED_AUTH) { |
bf8fe701 | 881 | debugs(83, 9, "Not requesting client certificates until acl processing requires one"); |
62e76326 | 882 | SSL_CTX_set_verify(sslContext, SSL_VERIFY_NONE, NULL); |
883 | } else { | |
bf8fe701 | 884 | debugs(83, 9, "Requiring client certificates."); |
62e76326 | 885 | SSL_CTX_set_verify(sslContext, SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT, ssl_verify_cb); |
886 | } | |
a82a4fe4 | 887 | |
86660d64 CT |
888 | if (port.clientVerifyCrls.get()) { |
889 | X509_STORE *st = SSL_CTX_get_cert_store(sslContext); | |
890 | for (int i = 0; i < sk_X509_CRL_num(port.clientVerifyCrls.get()); ++i) { | |
891 | X509_CRL *crl = sk_X509_CRL_value(port.clientVerifyCrls.get(), i); | |
892 | if (!X509_STORE_add_crl(st, crl)) | |
893 | debugs(83, 2, "WARNING: Failed to add CRL"); | |
894 | } | |
a82a4fe4 | 895 | } |
896 | ||
32d002cb | 897 | #if X509_V_FLAG_CRL_CHECK |
86660d64 | 898 | if (port.sslContextFlags & SSL_FLAG_VERIFY_CRL_ALL) |
a82a4fe4 | 899 | X509_STORE_set_flags(SSL_CTX_get_cert_store(sslContext), X509_V_FLAG_CRL_CHECK|X509_V_FLAG_CRL_CHECK_ALL); |
86660d64 | 900 | else if (port.sslContextFlags & SSL_FLAG_VERIFY_CRL) |
a82a4fe4 | 901 | X509_STORE_set_flags(SSL_CTX_get_cert_store(sslContext), X509_V_FLAG_CRL_CHECK); |
a82a4fe4 | 902 | #endif |
903 | ||
a7ad6e4e | 904 | } else { |
bf8fe701 | 905 | debugs(83, 9, "Not requiring any client certificates"); |
62e76326 | 906 | SSL_CTX_set_verify(sslContext, SSL_VERIFY_NONE, NULL); |
a7ad6e4e | 907 | } |
f484cdf5 | 908 | |
86660d64 CT |
909 | if (port.dhParams.get()) { |
910 | SSL_CTX_set_tmp_dh(sslContext, port.dhParams.get()); | |
911 | } | |
35105e4b | 912 | |
86660d64 CT |
913 | if (port.sslContextFlags & SSL_FLAG_DONT_VERIFY_DOMAIN) |
914 | SSL_CTX_set_ex_data(sslContext, ssl_ctx_ex_index_dont_verify_domain, (void *) -1); | |
915 | ||
10a69fc0 CT |
916 | setSessionCallbacks(sslContext); |
917 | ||
86660d64 CT |
918 | return true; |
919 | } | |
920 | ||
921 | SSL_CTX * | |
922 | sslCreateServerContext(AnyP::PortCfg &port) | |
923 | { | |
924 | int ssl_error; | |
925 | SSL_CTX *sslContext; | |
926 | const char *keyfile, *certfile; | |
927 | certfile = port.cert; | |
928 | keyfile = port.key; | |
929 | ||
930 | ssl_initialize(); | |
931 | ||
932 | if (!keyfile) | |
933 | keyfile = certfile; | |
934 | ||
935 | if (!certfile) | |
936 | certfile = keyfile; | |
937 | ||
938 | sslContext = SSL_CTX_new(port.contextMethod); | |
939 | ||
940 | if (sslContext == NULL) { | |
941 | ssl_error = ERR_get_error(); | |
942 | debugs(83, DBG_CRITICAL, "ERROR: Failed to allocate SSL context: " << ERR_error_string(ssl_error, NULL)); | |
943 | return NULL; | |
944 | } | |
945 | ||
946 | if (!SSL_CTX_use_certificate(sslContext, port.signingCert.get())) { | |
947 | ssl_error = ERR_get_error(); | |
948 | debugs(83, DBG_CRITICAL, "ERROR: Failed to acquire SSL certificate '" << certfile << "': " << ERR_error_string(ssl_error, NULL)); | |
949 | SSL_CTX_free(sslContext); | |
950 | return NULL; | |
951 | } | |
952 | ||
953 | if (!SSL_CTX_use_PrivateKey(sslContext, port.signPkey.get())) { | |
954 | ssl_error = ERR_get_error(); | |
955 | debugs(83, DBG_CRITICAL, "ERROR: Failed to acquire SSL private key '" << keyfile << "': " << ERR_error_string(ssl_error, NULL)); | |
956 | SSL_CTX_free(sslContext); | |
957 | return NULL; | |
958 | } | |
959 | ||
960 | Ssl::addChainToSslContext(sslContext, port.certsToChain.get()); | |
961 | ||
962 | /* Alternate code; | |
963 | debugs(83, DBG_IMPORTANT, "Using certificate in " << certfile); | |
964 | ||
965 | if (!SSL_CTX_use_certificate_chain_file(sslContext, certfile)) { | |
966 | ssl_error = ERR_get_error(); | |
967 | debugs(83, DBG_CRITICAL, "ERROR: Failed to acquire SSL certificate '" << certfile << "': " << ERR_error_string(ssl_error, NULL)); | |
968 | SSL_CTX_free(sslContext); | |
969 | return NULL; | |
35105e4b | 970 | } |
971 | ||
86660d64 CT |
972 | debugs(83, DBG_IMPORTANT, "Using private key in " << keyfile); |
973 | ssl_ask_password(sslContext, keyfile); | |
974 | ||
975 | if (!SSL_CTX_use_PrivateKey_file(sslContext, keyfile, SSL_FILETYPE_PEM)) { | |
976 | ssl_error = ERR_get_error(); | |
977 | debugs(83, DBG_CRITICAL, "ERROR: Failed to acquire SSL private key '" << keyfile << "': " << ERR_error_string(ssl_error, NULL)); | |
978 | SSL_CTX_free(sslContext); | |
979 | return NULL; | |
35105e4b | 980 | } |
981 | ||
86660d64 | 982 | debugs(83, 5, "Comparing private and public SSL keys."); |
35105e4b | 983 | |
86660d64 CT |
984 | if (!SSL_CTX_check_private_key(sslContext)) { |
985 | ssl_error = ERR_get_error(); | |
986 | debugs(83, DBG_CRITICAL, "ERROR: SSL private key '" << certfile << "' does not match public key '" << | |
987 | keyfile << "': " << ERR_error_string(ssl_error, NULL)); | |
988 | SSL_CTX_free(sslContext); | |
989 | return NULL; | |
990 | } | |
991 | */ | |
992 | ||
993 | if (!configureSslContext(sslContext, port)) { | |
994 | debugs(83, DBG_CRITICAL, "ERROR: Configuring static SSL context"); | |
995 | SSL_CTX_free(sslContext); | |
996 | return NULL; | |
997 | } | |
62e76326 | 998 | |
a7ad6e4e | 999 | return sslContext; |
a7ad6e4e | 1000 | } |
1001 | ||
1002 | SSL_CTX * | |
a82a4fe4 | 1003 | sslCreateClientContext(const char *certfile, const char *keyfile, int version, const char *cipher, const char *options, const char *flags, const char *CAfile, const char *CApath, const char *CRLfile) |
a7ad6e4e | 1004 | { |
1005 | int ssl_error; | |
19179f7c CT |
1006 | Ssl::ContextMethod method; |
1007 | SSL_CTX * sslContext; | |
86660d64 | 1008 | long fl = Ssl::parse_flags(flags); |
a7ad6e4e | 1009 | |
1010 | ssl_initialize(); | |
1011 | ||
1012 | if (!keyfile) | |
62e76326 | 1013 | keyfile = certfile; |
1014 | ||
a7ad6e4e | 1015 | if (!certfile) |
62e76326 | 1016 | certfile = keyfile; |
a7ad6e4e | 1017 | |
a7ad6e4e | 1018 | switch (version) { |
62e76326 | 1019 | |
a7ad6e4e | 1020 | case 2: |
50b4c080 | 1021 | #ifndef OPENSSL_NO_SSL2 |
bf8fe701 | 1022 | debugs(83, 5, "Using SSLv2."); |
62e76326 | 1023 | method = SSLv2_client_method(); |
50b4c080 | 1024 | #else |
48e7baac | 1025 | debugs(83, DBG_IMPORTANT, "SSLv2 is not available in this Proxy."); |
50b4c080 AJ |
1026 | return NULL; |
1027 | #endif | |
62e76326 | 1028 | break; |
1029 | ||
a7ad6e4e | 1030 | case 3: |
bf8fe701 | 1031 | debugs(83, 5, "Using SSLv3."); |
62e76326 | 1032 | method = SSLv3_client_method(); |
1033 | break; | |
1034 | ||
a7ad6e4e | 1035 | case 4: |
bf8fe701 | 1036 | debugs(83, 5, "Using TLSv1."); |
62e76326 | 1037 | method = TLSv1_client_method(); |
1038 | break; | |
1039 | ||
3d96b0e8 AJ |
1040 | case 5: |
1041 | #if OPENSSL_VERSION_NUMBER >= 0x10001000L // NP: not sure exactly which sub-version yet. | |
1042 | debugs(83, 5, "Using TLSv1.1."); | |
1043 | method = TLSv1_1_client_method(); | |
1044 | #else | |
1045 | debugs(83, DBG_IMPORTANT, "TLSv1.1 is not available in this Proxy."); | |
1046 | return NULL; | |
1047 | #endif | |
1048 | break; | |
1049 | ||
1050 | case 6: | |
1051 | #if OPENSSL_VERSION_NUMBER >= 0x10001000L // NP: not sure exactly which sub-version yet. | |
1052 | debugs(83, 5, "Using TLSv1.2"); | |
1053 | method = TLSv1_2_client_method(); | |
1054 | #else | |
1055 | debugs(83, DBG_IMPORTANT, "TLSv1.2 is not available in this Proxy."); | |
1056 | return NULL; | |
1057 | #endif | |
1058 | break; | |
1059 | ||
a7ad6e4e | 1060 | case 1: |
62e76326 | 1061 | |
a7ad6e4e | 1062 | default: |
bf8fe701 | 1063 | debugs(83, 5, "Using SSLv2/SSLv3."); |
62e76326 | 1064 | method = SSLv23_client_method(); |
1065 | break; | |
a7ad6e4e | 1066 | } |
1067 | ||
1068 | sslContext = SSL_CTX_new(method); | |
62e76326 | 1069 | |
a7ad6e4e | 1070 | if (sslContext == NULL) { |
62e76326 | 1071 | ssl_error = ERR_get_error(); |
1072 | fatalf("Failed to allocate SSL context: %s\n", | |
1073 | ERR_error_string(ssl_error, NULL)); | |
a7ad6e4e | 1074 | } |
62e76326 | 1075 | |
86660d64 | 1076 | SSL_CTX_set_options(sslContext, Ssl::parse_options(options)); |
a7ad6e4e | 1077 | |
1078 | if (cipher) { | |
bf8fe701 | 1079 | debugs(83, 5, "Using chiper suite " << cipher << "."); |
62e76326 | 1080 | |
1081 | if (!SSL_CTX_set_cipher_list(sslContext, cipher)) { | |
1082 | ssl_error = ERR_get_error(); | |
1083 | fatalf("Failed to set SSL cipher suite '%s': %s\n", | |
1084 | cipher, ERR_error_string(ssl_error, NULL)); | |
1085 | } | |
a7ad6e4e | 1086 | } |
62e76326 | 1087 | |
a7ad6e4e | 1088 | if (certfile) { |
e0236918 | 1089 | debugs(83, DBG_IMPORTANT, "Using certificate in " << certfile); |
62e76326 | 1090 | |
1091 | if (!SSL_CTX_use_certificate_chain_file(sslContext, certfile)) { | |
1092 | ssl_error = ERR_get_error(); | |
1093 | fatalf("Failed to acquire SSL certificate '%s': %s\n", | |
1094 | certfile, ERR_error_string(ssl_error, NULL)); | |
1095 | } | |
1096 | ||
e0236918 | 1097 | debugs(83, DBG_IMPORTANT, "Using private key in " << keyfile); |
307b83b7 | 1098 | ssl_ask_password(sslContext, keyfile); |
62e76326 | 1099 | |
1100 | if (!SSL_CTX_use_PrivateKey_file(sslContext, keyfile, SSL_FILETYPE_PEM)) { | |
1101 | ssl_error = ERR_get_error(); | |
1102 | fatalf("Failed to acquire SSL private key '%s': %s\n", | |
1103 | keyfile, ERR_error_string(ssl_error, NULL)); | |
1104 | } | |
1105 | ||
bf8fe701 | 1106 | debugs(83, 5, "Comparing private and public SSL keys."); |
62e76326 | 1107 | |
1108 | if (!SSL_CTX_check_private_key(sslContext)) { | |
1109 | ssl_error = ERR_get_error(); | |
1110 | fatalf("SSL private key '%s' does not match public key '%s': %s\n", | |
1111 | certfile, keyfile, ERR_error_string(ssl_error, NULL)); | |
1112 | } | |
a7ad6e4e | 1113 | } |
62e76326 | 1114 | |
bf8fe701 | 1115 | debugs(83, 9, "Setting RSA key generation callback."); |
f484cdf5 | 1116 | SSL_CTX_set_tmp_rsa_callback(sslContext, ssl_temp_rsa_cb); |
1117 | ||
a7ad6e4e | 1118 | if (fl & SSL_FLAG_DONT_VERIFY_PEER) { |
48e7baac | 1119 | debugs(83, 2, "NOTICE: Peer certificates are not verified for validity!"); |
62e76326 | 1120 | SSL_CTX_set_verify(sslContext, SSL_VERIFY_NONE, NULL); |
a7ad6e4e | 1121 | } else { |
bf8fe701 | 1122 | debugs(83, 9, "Setting certificate verification callback."); |
62e76326 | 1123 | SSL_CTX_set_verify(sslContext, SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT, ssl_verify_cb); |
a7ad6e4e | 1124 | } |
f484cdf5 | 1125 | |
bf8fe701 | 1126 | debugs(83, 9, "Setting CA certificate locations."); |
62e76326 | 1127 | |
a82a4fe4 | 1128 | if ((CAfile || CApath) && !SSL_CTX_load_verify_locations(sslContext, CAfile, CApath)) { |
1129 | ssl_error = ERR_get_error(); | |
48e7baac | 1130 | debugs(83, DBG_IMPORTANT, "WARNING: Ignoring error setting CA certificate locations: " << ERR_error_string(ssl_error, NULL)); |
a82a4fe4 | 1131 | } |
1132 | ||
1133 | if (CRLfile) { | |
1134 | ssl_load_crl(sslContext, CRLfile); | |
1135 | fl |= SSL_FLAG_VERIFY_CRL; | |
1136 | } | |
1137 | ||
32d002cb | 1138 | #if X509_V_FLAG_CRL_CHECK |
a82a4fe4 | 1139 | if (fl & SSL_FLAG_VERIFY_CRL_ALL) |
1140 | X509_STORE_set_flags(SSL_CTX_get_cert_store(sslContext), X509_V_FLAG_CRL_CHECK|X509_V_FLAG_CRL_CHECK_ALL); | |
1141 | else if (fl & SSL_FLAG_VERIFY_CRL) | |
1142 | X509_STORE_set_flags(SSL_CTX_get_cert_store(sslContext), X509_V_FLAG_CRL_CHECK); | |
1143 | ||
1144 | #endif | |
62e76326 | 1145 | |
a7ad6e4e | 1146 | if (!(fl & SSL_FLAG_NO_DEFAULT_CA) && |
62e76326 | 1147 | !SSL_CTX_set_default_verify_paths(sslContext)) { |
1148 | ssl_error = ERR_get_error(); | |
48e7baac | 1149 | debugs(83, DBG_IMPORTANT, "WARNING: Ignoring error setting default CA certificate location: " << ERR_error_string(ssl_error, NULL)); |
f484cdf5 | 1150 | } |
62e76326 | 1151 | |
d193a436 | 1152 | return sslContext; |
f484cdf5 | 1153 | } |
1154 | ||
63be0a78 | 1155 | /// \ingroup ServerProtocolSSLInternal |
d193a436 | 1156 | int |
e6ccf245 | 1157 | ssl_read_method(int fd, char *buf, int len) |
f484cdf5 | 1158 | { |
6de9e64b | 1159 | SSL *ssl = fd_table[fd].ssl; |
79d4ccdf | 1160 | int i; |
1161 | ||
6de9e64b | 1162 | #if DONT_DO_THIS |
1163 | ||
1164 | if (!SSL_is_init_finished(ssl)) { | |
1165 | errno = ENOTCONN; | |
1166 | return -1; | |
1167 | } | |
79d4ccdf | 1168 | |
6de9e64b | 1169 | #endif |
1170 | ||
1171 | i = SSL_read(ssl, buf, len); | |
1172 | ||
1173 | if (i > 0 && SSL_pending(ssl) > 0) { | |
bf8fe701 | 1174 | debugs(83, 2, "SSL FD " << fd << " is pending"); |
be4d35dc | 1175 | fd_table[fd].flags.read_pending = true; |
79d4ccdf | 1176 | } else |
be4d35dc | 1177 | fd_table[fd].flags.read_pending = false; |
79d4ccdf | 1178 | |
1179 | return i; | |
f484cdf5 | 1180 | } |
1181 | ||
63be0a78 | 1182 | /// \ingroup ServerProtocolSSLInternal |
d193a436 | 1183 | int |
e6ccf245 | 1184 | ssl_write_method(int fd, const char *buf, int len) |
f484cdf5 | 1185 | { |
6de9e64b | 1186 | SSL *ssl = fd_table[fd].ssl; |
1187 | int i; | |
1188 | ||
1189 | if (!SSL_is_init_finished(ssl)) { | |
1190 | errno = ENOTCONN; | |
1191 | return -1; | |
1192 | } | |
1193 | ||
1194 | i = SSL_write(ssl, buf, len); | |
1195 | ||
1196 | return i; | |
f484cdf5 | 1197 | } |
79d4ccdf | 1198 | |
1199 | void | |
575d05c4 | 1200 | ssl_shutdown_method(SSL *ssl) |
79d4ccdf | 1201 | { |
79d4ccdf | 1202 | SSL_shutdown(ssl); |
1203 | } | |
a7ad6e4e | 1204 | |
63be0a78 | 1205 | /// \ingroup ServerProtocolSSLInternal |
a7ad6e4e | 1206 | static const char * |
1207 | ssl_get_attribute(X509_NAME * name, const char *attribute_name) | |
1208 | { | |
1209 | static char buffer[1024]; | |
1210 | int nid; | |
1211 | ||
1212 | buffer[0] = '\0'; | |
1213 | ||
1214 | if (strcmp(attribute_name, "DN") == 0) { | |
62e76326 | 1215 | X509_NAME_oneline(name, buffer, sizeof(buffer)); |
1216 | goto done; | |
a7ad6e4e | 1217 | } |
62e76326 | 1218 | |
a7ad6e4e | 1219 | nid = OBJ_txt2nid((char *) attribute_name); |
62e76326 | 1220 | |
a7ad6e4e | 1221 | if (nid == 0) { |
e0236918 | 1222 | debugs(83, DBG_IMPORTANT, "WARNING: Unknown SSL attribute name '" << attribute_name << "'"); |
62e76326 | 1223 | return NULL; |
a7ad6e4e | 1224 | } |
62e76326 | 1225 | |
a7ad6e4e | 1226 | X509_NAME_get_text_by_NID(name, nid, buffer, sizeof(buffer)); |
62e76326 | 1227 | |
1228 | done: | |
a7ad6e4e | 1229 | return *buffer ? buffer : NULL; |
1230 | } | |
1231 | ||
63be0a78 | 1232 | /// \ingroup ServerProtocolSSLInternal |
a7ad6e4e | 1233 | const char * |
00352183 | 1234 | Ssl::GetX509UserAttribute(X509 * cert, const char *attribute_name) |
a7ad6e4e | 1235 | { |
a7ad6e4e | 1236 | X509_NAME *name; |
23e6c4ae | 1237 | const char *ret; |
a7ad6e4e | 1238 | |
a7ad6e4e | 1239 | if (!cert) |
62e76326 | 1240 | return NULL; |
a7ad6e4e | 1241 | |
5a4684b6 | 1242 | name = X509_get_subject_name(cert); |
a7ad6e4e | 1243 | |
23e6c4ae | 1244 | ret = ssl_get_attribute(name, attribute_name); |
1245 | ||
23e6c4ae | 1246 | return ret; |
a7ad6e4e | 1247 | } |
1248 | ||
00352183 AR |
1249 | const char * |
1250 | Ssl::GetX509Fingerprint(X509 * cert, const char *) | |
1251 | { | |
1252 | static char buf[1024]; | |
1253 | if (!cert) | |
1254 | return NULL; | |
960e100b | 1255 | |
00352183 AR |
1256 | unsigned int n; |
1257 | unsigned char md[EVP_MAX_MD_SIZE]; | |
1258 | if (!X509_digest(cert, EVP_sha1(), md, &n)) | |
1259 | return NULL; | |
1260 | ||
1261 | assert(3 * n + 1 < sizeof(buf)); | |
1262 | ||
1263 | char *s = buf; | |
1264 | for (unsigned int i=0; i < n; ++i, s += 3) { | |
1265 | const char term = (i + 1 < n) ? ':' : '\0'; | |
1266 | snprintf(s, 4, "%02X%c", md[i], term); | |
1267 | } | |
1268 | ||
1269 | return buf; | |
1270 | } | |
1271 | ||
63be0a78 | 1272 | /// \ingroup ServerProtocolSSLInternal |
a7ad6e4e | 1273 | const char * |
00352183 | 1274 | Ssl::GetX509CAAttribute(X509 * cert, const char *attribute_name) |
a7ad6e4e | 1275 | { |
00352183 | 1276 | |
a7ad6e4e | 1277 | X509_NAME *name; |
23e6c4ae | 1278 | const char *ret; |
a7ad6e4e | 1279 | |
a7ad6e4e | 1280 | if (!cert) |
62e76326 | 1281 | return NULL; |
a7ad6e4e | 1282 | |
5a4684b6 | 1283 | name = X509_get_issuer_name(cert); |
a7ad6e4e | 1284 | |
23e6c4ae | 1285 | ret = ssl_get_attribute(name, attribute_name); |
1286 | ||
00352183 AR |
1287 | return ret; |
1288 | } | |
1289 | ||
1290 | const char *sslGetUserAttribute(SSL *ssl, const char *attribute_name) | |
1291 | { | |
1292 | if (!ssl) | |
1293 | return NULL; | |
1294 | ||
1295 | X509 *cert = SSL_get_peer_certificate(ssl); | |
1296 | ||
1297 | const char *attr = Ssl::GetX509UserAttribute(cert, attribute_name); | |
1298 | ||
23e6c4ae | 1299 | X509_free(cert); |
00352183 AR |
1300 | return attr; |
1301 | } | |
23e6c4ae | 1302 | |
00352183 AR |
1303 | const char *sslGetCAAttribute(SSL *ssl, const char *attribute_name) |
1304 | { | |
1305 | if (!ssl) | |
1306 | return NULL; | |
1307 | ||
1308 | X509 *cert = SSL_get_peer_certificate(ssl); | |
1309 | ||
1310 | const char *attr = Ssl::GetX509CAAttribute(cert, attribute_name); | |
1311 | ||
1312 | X509_free(cert); | |
1313 | return attr; | |
a7ad6e4e | 1314 | } |
1315 | ||
a7ad6e4e | 1316 | const char * |
1317 | sslGetUserEmail(SSL * ssl) | |
1318 | { | |
e6ceef10 | 1319 | return sslGetUserAttribute(ssl, "emailAddress"); |
a7ad6e4e | 1320 | } |
4ac9968f | 1321 | |
1322 | const char * | |
1323 | sslGetUserCertificatePEM(SSL *ssl) | |
1324 | { | |
1325 | X509 *cert; | |
1326 | BIO *mem; | |
1327 | static char *str = NULL; | |
1328 | char *ptr; | |
1329 | long len; | |
1330 | ||
1331 | safe_free(str); | |
1332 | ||
1333 | if (!ssl) | |
1334 | return NULL; | |
1335 | ||
1336 | cert = SSL_get_peer_certificate(ssl); | |
1337 | ||
1338 | if (!cert) | |
1339 | return NULL; | |
1340 | ||
1341 | mem = BIO_new(BIO_s_mem()); | |
1342 | ||
1343 | PEM_write_bio_X509(mem, cert); | |
1344 | ||
4ac9968f | 1345 | len = BIO_get_mem_data(mem, &ptr); |
1346 | ||
1347 | str = (char *)xmalloc(len + 1); | |
1348 | ||
1349 | memcpy(str, ptr, len); | |
1350 | ||
1351 | str[len] = '\0'; | |
1352 | ||
1353 | X509_free(cert); | |
1354 | ||
1355 | BIO_free(mem); | |
1356 | ||
1357 | return str; | |
1358 | } | |
3d61c476 | 1359 | |
1360 | const char * | |
1361 | sslGetUserCertificateChainPEM(SSL *ssl) | |
1362 | { | |
1363 | STACK_OF(X509) *chain; | |
1364 | BIO *mem; | |
1365 | static char *str = NULL; | |
1366 | char *ptr; | |
1367 | long len; | |
1368 | int i; | |
1369 | ||
1370 | safe_free(str); | |
1371 | ||
1372 | if (!ssl) | |
1373 | return NULL; | |
1374 | ||
1375 | chain = SSL_get_peer_cert_chain(ssl); | |
1376 | ||
1377 | if (!chain) | |
1378 | return sslGetUserCertificatePEM(ssl); | |
1379 | ||
1380 | mem = BIO_new(BIO_s_mem()); | |
1381 | ||
d7ae3534 | 1382 | for (i = 0; i < sk_X509_num(chain); ++i) { |
3d61c476 | 1383 | X509 *cert = sk_X509_value(chain, i); |
1384 | PEM_write_bio_X509(mem, cert); | |
1385 | } | |
1386 | ||
1387 | len = BIO_get_mem_data(mem, &ptr); | |
1388 | ||
1389 | str = (char *)xmalloc(len + 1); | |
1390 | memcpy(str, ptr, len); | |
1391 | str[len] = '\0'; | |
1392 | ||
1393 | BIO_free(mem); | |
1394 | ||
1395 | return str; | |
1396 | } | |
454e8283 | 1397 | |
86660d64 CT |
1398 | Ssl::ContextMethod |
1399 | Ssl::contextMethod(int version) | |
1400 | { | |
1401 | Ssl::ContextMethod method; | |
1402 | ||
1403 | switch (version) { | |
1404 | ||
1405 | case 2: | |
1406 | #ifndef OPENSSL_NO_SSL2 | |
1407 | debugs(83, 5, "Using SSLv2."); | |
1408 | method = SSLv2_server_method(); | |
1409 | #else | |
1410 | debugs(83, DBG_IMPORTANT, "SSLv2 is not available in this Proxy."); | |
1411 | return NULL; | |
1412 | #endif | |
1413 | break; | |
1414 | ||
1415 | case 3: | |
1416 | debugs(83, 5, "Using SSLv3."); | |
1417 | method = SSLv3_server_method(); | |
1418 | break; | |
1419 | ||
1420 | case 4: | |
1421 | debugs(83, 5, "Using TLSv1."); | |
1422 | method = TLSv1_server_method(); | |
1423 | break; | |
1424 | ||
1425 | case 5: | |
1426 | #if OPENSSL_VERSION_NUMBER >= 0x10001000L // NP: not sure exactly which sub-version yet. | |
1427 | debugs(83, 5, "Using TLSv1.1."); | |
1428 | method = TLSv1_1_server_method(); | |
1429 | #else | |
1430 | debugs(83, DBG_IMPORTANT, "TLSv1.1 is not available in this Proxy."); | |
1431 | return NULL; | |
1432 | #endif | |
1433 | break; | |
1434 | ||
1435 | case 6: | |
1436 | #if OPENSSL_VERSION_NUMBER >= 0x10001000L // NP: not sure exactly which sub-version yet. | |
1437 | debugs(83, 5, "Using TLSv1.2"); | |
1438 | method = TLSv1_2_server_method(); | |
1439 | #else | |
1440 | debugs(83, DBG_IMPORTANT, "TLSv1.2 is not available in this Proxy."); | |
1441 | return NULL; | |
1442 | #endif | |
1443 | break; | |
1444 | ||
1445 | case 1: | |
1446 | ||
1447 | default: | |
1448 | debugs(83, 5, "Using SSLv2/SSLv3."); | |
1449 | method = SSLv23_server_method(); | |
1450 | break; | |
1451 | } | |
1452 | return method; | |
1453 | } | |
1454 | ||
95d2589c CT |
1455 | /// \ingroup ServerProtocolSSLInternal |
1456 | /// Create SSL context and apply ssl certificate and private key to it. | |
86660d64 CT |
1457 | static SSL_CTX * |
1458 | createSSLContext(Ssl::X509_Pointer & x509, Ssl::EVP_PKEY_Pointer & pkey, AnyP::PortCfg &port) | |
95d2589c | 1459 | { |
86660d64 | 1460 | Ssl::SSL_CTX_Pointer sslContext(SSL_CTX_new(port.contextMethod)); |
95d2589c CT |
1461 | |
1462 | if (!SSL_CTX_use_certificate(sslContext.get(), x509.get())) | |
1463 | return NULL; | |
1464 | ||
1465 | if (!SSL_CTX_use_PrivateKey(sslContext.get(), pkey.get())) | |
1466 | return NULL; | |
86660d64 CT |
1467 | |
1468 | if (!configureSslContext(sslContext.get(), port)) | |
1469 | return NULL; | |
1470 | ||
95d2589c CT |
1471 | return sslContext.release(); |
1472 | } | |
1473 | ||
86660d64 CT |
1474 | SSL_CTX * |
1475 | Ssl::generateSslContextUsingPkeyAndCertFromMemory(const char * data, AnyP::PortCfg &port) | |
95d2589c CT |
1476 | { |
1477 | Ssl::X509_Pointer cert; | |
1478 | Ssl::EVP_PKEY_Pointer pkey; | |
1479 | if (!readCertAndPrivateKeyFromMemory(cert, pkey, data)) | |
1480 | return NULL; | |
1481 | ||
1482 | if (!cert || !pkey) | |
1483 | return NULL; | |
1484 | ||
86660d64 | 1485 | return createSSLContext(cert, pkey, port); |
95d2589c CT |
1486 | } |
1487 | ||
86660d64 CT |
1488 | SSL_CTX * |
1489 | Ssl::generateSslContext(CertificateProperties const &properties, AnyP::PortCfg &port) | |
95d2589c CT |
1490 | { |
1491 | Ssl::X509_Pointer cert; | |
1492 | Ssl::EVP_PKEY_Pointer pkey; | |
aebe6888 | 1493 | if (!generateSslCertificate(cert, pkey, properties)) |
95d2589c | 1494 | return NULL; |
9a90aace | 1495 | |
95d2589c CT |
1496 | if (!cert) |
1497 | return NULL; | |
1498 | ||
1499 | if (!pkey) | |
1500 | return NULL; | |
1501 | ||
86660d64 | 1502 | return createSSLContext(cert, pkey, port); |
95d2589c CT |
1503 | } |
1504 | ||
4ece76b2 | 1505 | bool Ssl::verifySslCertificate(SSL_CTX * sslContext, CertificateProperties const &properties) |
95d2589c | 1506 | { |
b8658552 CT |
1507 | // SSL_get_certificate is buggy in openssl versions 1.0.1d and 1.0.1e |
1508 | // Try to retrieve certificate directly from SSL_CTX object | |
fc321c30 | 1509 | #if SQUID_USE_SSLGETCERTIFICATE_HACK |
b8658552 CT |
1510 | X509 ***pCert = (X509 ***)sslContext->cert; |
1511 | X509 * cert = pCert && *pCert ? **pCert : NULL; | |
fc321c30 CT |
1512 | #elif SQUID_SSLGETCERTIFICATE_BUGGY |
1513 | X509 * cert = NULL; | |
1514 | assert(0); | |
b8658552 | 1515 | #else |
95d2589c CT |
1516 | // Temporary ssl for getting X509 certificate from SSL_CTX. |
1517 | Ssl::SSL_Pointer ssl(SSL_new(sslContext)); | |
1518 | X509 * cert = SSL_get_certificate(ssl.get()); | |
b8658552 CT |
1519 | #endif |
1520 | if (!cert) | |
1521 | return false; | |
95d2589c CT |
1522 | ASN1_TIME * time_notBefore = X509_get_notBefore(cert); |
1523 | ASN1_TIME * time_notAfter = X509_get_notAfter(cert); | |
1524 | bool ret = (X509_cmp_current_time(time_notBefore) < 0 && X509_cmp_current_time(time_notAfter) > 0); | |
e7bcc25f CT |
1525 | if (!ret) |
1526 | return false; | |
1527 | ||
4ece76b2 | 1528 | return certificateMatchesProperties(cert, properties); |
95d2589c CT |
1529 | } |
1530 | ||
253749a8 CT |
1531 | bool |
1532 | Ssl::setClientSNI(SSL *ssl, const char *fqdn) | |
1533 | { | |
1534 | //The SSL_CTRL_SET_TLSEXT_HOSTNAME is a openssl macro which indicates | |
1535 | // if the TLS servername extension (SNI) is enabled in openssl library. | |
1536 | #if defined(SSL_CTRL_SET_TLSEXT_HOSTNAME) | |
1537 | if (!SSL_set_tlsext_host_name(ssl, fqdn)) { | |
1538 | const int ssl_error = ERR_get_error(); | |
1539 | debugs(83, 3, "WARNING: unable to set TLS servername extension (SNI): " << | |
1540 | ERR_error_string(ssl_error, NULL) << "\n"); | |
1541 | return false; | |
1542 | } | |
1543 | return true; | |
1544 | #else | |
1545 | debugs(83, 7, "no support for TLS servername extension (SNI)\n"); | |
1546 | return false; | |
1547 | #endif | |
1548 | } | |
1549 | ||
a594dbfa CT |
1550 | void Ssl::addChainToSslContext(SSL_CTX *sslContext, STACK_OF(X509) *chain) |
1551 | { | |
1552 | if (!chain) | |
1553 | return; | |
1554 | ||
d7ae3534 | 1555 | for (int i = 0; i < sk_X509_num(chain); ++i) { |
a594dbfa CT |
1556 | X509 *cert = sk_X509_value(chain, i); |
1557 | if (SSL_CTX_add_extra_chain_cert(sslContext, cert)) { | |
1558 | // increase the certificate lock | |
1559 | CRYPTO_add(&(cert->references),1,CRYPTO_LOCK_X509); | |
1560 | } else { | |
1561 | const int ssl_error = ERR_get_error(); | |
1562 | debugs(83, DBG_IMPORTANT, "WARNING: can not add certificate to SSL context chain: " << ERR_error_string(ssl_error, NULL)); | |
1563 | } | |
1564 | } | |
1565 | } | |
1566 | ||
1567 | /** | |
1568 | \ingroup ServerProtocolSSLInternal | |
1569 | * Read certificate from file. | |
1570 | * See also: static readSslX509Certificate function, gadgets.cc file | |
1571 | */ | |
1572 | static X509 * readSslX509CertificatesChain(char const * certFilename, STACK_OF(X509)* chain) | |
1573 | { | |
1574 | if (!certFilename) | |
1575 | return NULL; | |
1576 | Ssl::BIO_Pointer bio(BIO_new(BIO_s_file_internal())); | |
1577 | if (!bio) | |
1578 | return NULL; | |
1579 | if (!BIO_read_filename(bio.get(), certFilename)) | |
1580 | return NULL; | |
1581 | X509 *certificate = PEM_read_bio_X509(bio.get(), NULL, NULL, NULL); | |
1582 | ||
1583 | if (certificate && chain) { | |
1584 | ||
c11211d9 | 1585 | if (X509_check_issued(certificate, certificate) == X509_V_OK) |
a594dbfa CT |
1586 | debugs(83, 5, "Certificate is self-signed, will not be chained"); |
1587 | else { | |
a411d213 | 1588 | // and add to the chain any other certificate exist in the file |
a594dbfa CT |
1589 | while (X509 *ca = PEM_read_bio_X509(bio.get(), NULL, NULL, NULL)) { |
1590 | if (!sk_X509_push(chain, ca)) | |
1591 | debugs(83, DBG_IMPORTANT, "WARNING: unable to add CA certificate to cert chain"); | |
1592 | } | |
1593 | } | |
1594 | } | |
c11211d9 | 1595 | |
a594dbfa CT |
1596 | return certificate; |
1597 | } | |
1598 | ||
1599 | void Ssl::readCertChainAndPrivateKeyFromFiles(X509_Pointer & cert, EVP_PKEY_Pointer & pkey, X509_STACK_Pointer & chain, char const * certFilename, char const * keyFilename) | |
1600 | { | |
1601 | if (keyFilename == NULL) | |
1602 | keyFilename = certFilename; | |
86660d64 CT |
1603 | |
1604 | if (certFilename == NULL) | |
1605 | certFilename = keyFilename; | |
1606 | ||
1607 | debugs(83, DBG_IMPORTANT, "Using certificate in " << certFilename); | |
1608 | ||
a594dbfa CT |
1609 | if (!chain) |
1610 | chain.reset(sk_X509_new_null()); | |
1611 | if (!chain) | |
1612 | debugs(83, DBG_IMPORTANT, "WARNING: unable to allocate memory for cert chain"); | |
37144aca AR |
1613 | // XXX: ssl_ask_password_cb needs SSL_CTX_set_default_passwd_cb_userdata() |
1614 | // so this may not fully work iff Config.Program.ssl_password is set. | |
1615 | pem_password_cb *cb = ::Config.Program.ssl_password ? &ssl_ask_password_cb : NULL; | |
1616 | pkey.reset(readSslPrivateKey(keyFilename, cb)); | |
a594dbfa CT |
1617 | cert.reset(readSslX509CertificatesChain(certFilename, chain.get())); |
1618 | if (!pkey || !cert || !X509_check_private_key(cert.get(), pkey.get())) { | |
1619 | pkey.reset(NULL); | |
1620 | cert.reset(NULL); | |
1621 | } | |
1622 | } | |
1623 | ||
95588170 CT |
1624 | bool Ssl::generateUntrustedCert(X509_Pointer &untrustedCert, EVP_PKEY_Pointer &untrustedPkey, X509_Pointer const &cert, EVP_PKEY_Pointer const & pkey) |
1625 | { | |
1626 | // Generate the self-signed certificate, using a hard-coded subject prefix | |
1627 | Ssl::CertificateProperties certProperties; | |
1628 | if (const char *cn = CommonHostName(cert.get())) { | |
1629 | certProperties.commonName = "Not trusted by \""; | |
1630 | certProperties.commonName += cn; | |
1631 | certProperties.commonName += "\""; | |
87f237a9 | 1632 | } else if (const char *org = getOrganization(cert.get())) { |
95588170 CT |
1633 | certProperties.commonName = "Not trusted by \""; |
1634 | certProperties.commonName += org; | |
1635 | certProperties.commonName += "\""; | |
87f237a9 | 1636 | } else |
95588170 CT |
1637 | certProperties.commonName = "Not trusted"; |
1638 | certProperties.setCommonName = true; | |
1639 | // O, OU, and other CA subject fields will be mimicked | |
1640 | // Expiration date and other common properties will be mimicked | |
1641 | certProperties.signAlgorithm = Ssl::algSignSelf; | |
1642 | certProperties.signWithPkey.resetAndLock(pkey.get()); | |
1643 | certProperties.mimicCert.resetAndLock(cert.get()); | |
1644 | return Ssl::generateSslCertificate(untrustedCert, untrustedPkey, certProperties); | |
1645 | } | |
1646 | ||
62a7607e | 1647 | Ssl::CertError::CertError(ssl_error_t anErr, X509 *aCert): code(anErr) |
170cb017 | 1648 | { |
62a7607e CT |
1649 | cert.resetAndLock(aCert); |
1650 | } | |
1651 | ||
1652 | Ssl::CertError::CertError(CertError const &err): code(err.code) | |
1653 | { | |
1654 | cert.resetAndLock(err.cert.get()); | |
1655 | } | |
1656 | ||
1657 | Ssl::CertError & | |
170cb017 | 1658 | Ssl::CertError::operator = (const CertError &old) |
62a7607e | 1659 | { |
170cb017 | 1660 | code = old.code; |
62a7607e CT |
1661 | cert.resetAndLock(old.cert.get()); |
1662 | return *this; | |
1663 | } | |
1664 | ||
1665 | bool | |
1666 | Ssl::CertError::operator == (const CertError &ce) const | |
1667 | { | |
1668 | return code == ce.code && cert.get() == ce.cert.get(); | |
1669 | } | |
1670 | ||
1671 | bool | |
1672 | Ssl::CertError::operator != (const CertError &ce) const | |
1673 | { | |
1674 | return code != ce.code || cert.get() != ce.cert.get(); | |
1675 | } | |
1676 | ||
10a69fc0 CT |
1677 | static int |
1678 | store_session_cb(SSL *ssl, SSL_SESSION *session) | |
1679 | { | |
1680 | if (!SslSessionCache) | |
1681 | return 0; | |
1682 | ||
1683 | debugs(83, 5, "Request to store SSL Session "); | |
1684 | ||
1685 | SSL_SESSION_set_timeout(session, Config.SSL.session_ttl); | |
1686 | ||
1687 | unsigned char *id = session->session_id; | |
1688 | unsigned int idlen = session->session_id_length; | |
1689 | unsigned char key[MEMMAP_SLOT_KEY_SIZE]; | |
1690 | // Session ids are of size 32bytes. They should always fit to a | |
86c63190 | 1691 | // MemMap::Slot::key |
10a69fc0 CT |
1692 | assert(idlen <= MEMMAP_SLOT_KEY_SIZE); |
1693 | memset(key, 0, sizeof(key)); | |
1694 | memcpy(key, id, idlen); | |
1695 | int pos; | |
1696 | Ipc::MemMap::Slot *slotW = SslSessionCache->openForWriting((const cache_key*)key, pos); | |
1697 | if (slotW) { | |
1698 | int lenRequired = i2d_SSL_SESSION(session, NULL); | |
1699 | if (lenRequired < MEMMAP_SLOT_DATA_SIZE) { | |
1700 | unsigned char *p = (unsigned char *)slotW->p; | |
1701 | lenRequired = i2d_SSL_SESSION(session, &p); | |
1702 | slotW->set(key, NULL, lenRequired, squid_curtime + Config.SSL.session_ttl); | |
1703 | } | |
1704 | SslSessionCache->closeForWriting(pos); | |
1705 | debugs(83, 5, "wrote an ssl session entry of size " << lenRequired << " at pos " << pos); | |
1706 | } | |
1707 | return 0; | |
1708 | } | |
1709 | ||
1710 | static void | |
1711 | remove_session_cb(SSL_CTX *, SSL_SESSION *sessionID) | |
1712 | { | |
1713 | if (!SslSessionCache) | |
1714 | return ; | |
1715 | ||
1716 | debugs(83, 5, "Request to remove corrupted or not valid SSL Session "); | |
1717 | int pos; | |
1718 | Ipc::MemMap::Slot const *slot = SslSessionCache->openForReading((const cache_key*)sessionID, pos); | |
1719 | if (slot == NULL) | |
1720 | return; | |
1721 | SslSessionCache->closeForReading(pos); | |
1722 | // TODO: | |
1723 | // What if we are not able to remove the session? | |
1724 | // Maybe schedule a job to remove it later? | |
1725 | // For now we just have an invalid entry in cache until will be expired | |
1726 | // The openSSL will reject it when we try to use it | |
1727 | SslSessionCache->free(pos); | |
1728 | } | |
1729 | ||
1730 | static SSL_SESSION * | |
1731 | get_session_cb(SSL *, unsigned char *sessionID, int len, int *copy) | |
1732 | { | |
1733 | if (!SslSessionCache) | |
1734 | return NULL; | |
1735 | ||
1736 | SSL_SESSION *session = NULL; | |
1737 | const unsigned int *p; | |
1738 | p = (unsigned int *)sessionID; | |
1739 | debugs(83, 5, "Request to search for SSL Session of len:" << | |
1740 | len << p[0] << ":" << p[1]); | |
1741 | ||
1742 | int pos; | |
1743 | Ipc::MemMap::Slot const *slot = SslSessionCache->openForReading((const cache_key*)sessionID, pos); | |
1744 | if (slot != NULL) { | |
1745 | if (slot->expire > squid_curtime) { | |
1746 | const unsigned char *ptr = slot->p; | |
1747 | session = d2i_SSL_SESSION(NULL, &ptr, slot->pSize); | |
1748 | debugs(83, 5, "Session retrieved from cache at pos " << pos); | |
1749 | } else | |
1750 | debugs(83, 5, "Session in cache expired"); | |
1751 | SslSessionCache->closeForReading(pos); | |
1752 | } | |
1753 | ||
1754 | if (!session) | |
1755 | debugs(83, 5, "Failed to retrieved from cache\n"); | |
1756 | ||
1757 | // With the parameter copy the callback can require the SSL engine | |
1758 | // to increment the reference count of the SSL_SESSION object, Normally | |
1759 | // the reference count is not incremented and therefore the session must | |
1760 | // not be explicitly freed with SSL_SESSION_free(3). | |
1761 | *copy = 0; | |
1762 | return session; | |
1763 | } | |
1764 | ||
1765 | static void | |
1766 | setSessionCallbacks(SSL_CTX *ctx) | |
1767 | { | |
1768 | if (SslSessionCache) { | |
1769 | SSL_CTX_set_session_cache_mode(ctx, SSL_SESS_CACHE_SERVER|SSL_SESS_CACHE_NO_INTERNAL); | |
1770 | SSL_CTX_sess_set_new_cb(ctx, store_session_cb); | |
1771 | SSL_CTX_sess_set_remove_cb(ctx, remove_session_cb); | |
1772 | SSL_CTX_sess_set_get_cb(ctx, get_session_cb); | |
1773 | } | |
1774 | } | |
1775 | ||
1776 | static bool | |
1777 | isSslServer() | |
1778 | { | |
1779 | if (Config.Sockaddr.https) | |
1780 | return true; | |
1781 | ||
1782 | for (AnyP::PortCfg *s = Config.Sockaddr.http; s; s = s->next) { | |
1783 | if (s->flags.tunnelSslBumping) | |
1784 | return true; | |
1785 | } | |
1786 | ||
1787 | return false; | |
1788 | } | |
1789 | ||
1790 | #define SSL_SESSION_ID_SIZE 32 | |
1791 | #define SSL_SESSION_MAX_SIZE 10*1024 | |
1792 | ||
1793 | void | |
1794 | Ssl::initialize_session_cache() | |
1795 | { | |
1796 | ||
1797 | if (!isSslServer()) //no need to configure ssl session cache. | |
1798 | return; | |
1799 | ||
86c63190 | 1800 | // Check if the MemMap keys and data are enough big to hold |
10a69fc0 CT |
1801 | // session ids and session data |
1802 | assert(SSL_SESSION_ID_SIZE >= MEMMAP_SLOT_KEY_SIZE); | |
1803 | assert(SSL_SESSION_MAX_SIZE >= MEMMAP_SLOT_DATA_SIZE); | |
1804 | ||
1805 | int configuredItems = ::Config.SSL.sessionCacheSize / sizeof(Ipc::MemMap::Slot); | |
1806 | if (IamWorkerProcess() && configuredItems) | |
1807 | SslSessionCache = new Ipc::MemMap(SslSessionCacheName); | |
1808 | else { | |
1809 | SslSessionCache = NULL; | |
1810 | return; | |
1811 | } | |
1812 | ||
1813 | for (AnyP::PortCfg *s = ::Config.Sockaddr.https; s; s = s->next) { | |
1814 | if (s->staticSslContext.get() != NULL) | |
1815 | setSessionCallbacks(s->staticSslContext.get()); | |
1816 | } | |
1817 | ||
1818 | for (AnyP::PortCfg *s = ::Config.Sockaddr.http; s; s = s->next) { | |
1819 | if (s->staticSslContext.get() != NULL) | |
1820 | setSessionCallbacks(s->staticSslContext.get()); | |
1821 | } | |
1822 | } | |
1823 | ||
1824 | void | |
1825 | destruct_session_cache() | |
1826 | { | |
1827 | delete SslSessionCache; | |
1828 | } | |
1829 | ||
1830 | /// initializes shared memory segments used by MemStore | |
1831 | class SharedSessionCacheRr: public Ipc::Mem::RegisteredRunner | |
1832 | { | |
1833 | public: | |
1834 | /* RegisteredRunner API */ | |
1835 | SharedSessionCacheRr(): owner(NULL) {} | |
21b7990f | 1836 | virtual void useConfig(); |
10a69fc0 CT |
1837 | virtual ~SharedSessionCacheRr(); |
1838 | ||
1839 | protected: | |
21b7990f | 1840 | virtual void create(); |
10a69fc0 CT |
1841 | |
1842 | private: | |
1843 | Ipc::MemMap::Owner *owner; | |
1844 | }; | |
1845 | ||
21b7990f | 1846 | RunnerRegistrationEntry(SharedSessionCacheRr); |
10a69fc0 CT |
1847 | |
1848 | void | |
21b7990f | 1849 | SharedSessionCacheRr::useConfig() |
10a69fc0 | 1850 | { |
21b7990f | 1851 | Ipc::Mem::RegisteredRunner::useConfig(); |
10a69fc0 CT |
1852 | } |
1853 | ||
1854 | void | |
21b7990f | 1855 | SharedSessionCacheRr::create() |
10a69fc0 CT |
1856 | { |
1857 | if (!isSslServer()) //no need to configure ssl session cache. | |
1858 | return; | |
1859 | ||
1860 | int items; | |
1861 | items = Config.SSL.sessionCacheSize / sizeof(Ipc::MemMap::Slot); | |
1862 | if (items) | |
1863 | owner = Ipc::MemMap::Init(SslSessionCacheName, items); | |
1864 | } | |
1865 | ||
1866 | SharedSessionCacheRr::~SharedSessionCacheRr() | |
1867 | { | |
1868 | delete owner; | |
1869 | } | |
1870 | ||
cb4f4424 | 1871 | #endif /* USE_OPENSSL */ |