]> git.ipfire.org Git - thirdparty/squid.git/blob - src/ssl/support.cc
SourceFormat Enforcement
[thirdparty/squid.git] / src / ssl / support.cc
1 /*
2 * Copyright (C) 1996-2016 The Squid Software Foundation and contributors
3 *
4 * Squid software is distributed under GPLv2+ license and includes
5 * contributions from numerous individuals and organizations.
6 * Please see the COPYING and CONTRIBUTORS files for details.
7 */
8
9 /* DEBUG: section 83 SSL accelerator support */
10
11 #include "squid.h"
12
13 /* MS Visual Studio Projects are monolithic, so we need the following
14 * #if to exclude the SSL code from compile process when not needed.
15 */
16 #if USE_OPENSSL
17
18 #include "acl/FilledChecklist.h"
19 #include "anyp/PortCfg.h"
20 #include "fatal.h"
21 #include "fd.h"
22 #include "fde.h"
23 #include "globals.h"
24 #include "ipc/MemMap.h"
25 #include "SquidConfig.h"
26 #include "SquidTime.h"
27 #include "ssl/bio.h"
28 #include "ssl/Config.h"
29 #include "ssl/ErrorDetail.h"
30 #include "ssl/gadgets.h"
31 #include "ssl/support.h"
32 #include "URL.h"
33
34 #include <cerrno>
35
36 static void setSessionCallbacks(Security::ContextPtr ctx);
37 Ipc::MemMap *SslSessionCache = NULL;
38 const char *SslSessionCacheName = "ssl_session_cache";
39
40 static Ssl::CertsIndexedList SquidUntrustedCerts;
41
42 const EVP_MD *Ssl::DefaultSignHash = NULL;
43
44 const char *Ssl::BumpModeStr[] = {
45 "none",
46 "client-first",
47 "server-first",
48 "peek",
49 "stare",
50 "bump",
51 "splice",
52 "terminate",
53 /*"err",*/
54 NULL
55 };
56
57 /**
58 \defgroup ServerProtocolSSLInternal Server-Side SSL Internals
59 \ingroup ServerProtocolSSLAPI
60 */
61
62 /// \ingroup ServerProtocolSSLInternal
63 static int
64 ssl_ask_password_cb(char *buf, int size, int rwflag, void *userdata)
65 {
66 FILE *in;
67 int len = 0;
68 char cmdline[1024];
69
70 snprintf(cmdline, sizeof(cmdline), "\"%s\" \"%s\"", Config.Program.ssl_password, (const char *)userdata);
71 in = popen(cmdline, "r");
72
73 if (fgets(buf, size, in))
74
75 len = strlen(buf);
76
77 while (len > 0 && (buf[len - 1] == '\n' || buf[len - 1] == '\r'))
78 --len;
79
80 buf[len] = '\0';
81
82 pclose(in);
83
84 return len;
85 }
86
87 /// \ingroup ServerProtocolSSLInternal
88 static void
89 ssl_ask_password(SSL_CTX * context, const char * prompt)
90 {
91 if (Config.Program.ssl_password) {
92 SSL_CTX_set_default_passwd_cb(context, ssl_ask_password_cb);
93 SSL_CTX_set_default_passwd_cb_userdata(context, (void *)prompt);
94 }
95 }
96
97 /// \ingroup ServerProtocolSSLInternal
98 static RSA *
99 ssl_temp_rsa_cb(SSL * ssl, int anInt, int keylen)
100 {
101 static RSA *rsa_512 = NULL;
102 static RSA *rsa_1024 = NULL;
103 RSA *rsa = NULL;
104 int newkey = 0;
105
106 switch (keylen) {
107
108 case 512:
109
110 if (!rsa_512) {
111 rsa_512 = RSA_generate_key(512, RSA_F4, NULL, NULL);
112 newkey = 1;
113 }
114
115 rsa = rsa_512;
116 break;
117
118 case 1024:
119
120 if (!rsa_1024) {
121 rsa_1024 = RSA_generate_key(1024, RSA_F4, NULL, NULL);
122 newkey = 1;
123 }
124
125 rsa = rsa_1024;
126 break;
127
128 default:
129 debugs(83, DBG_IMPORTANT, "ssl_temp_rsa_cb: Unexpected key length " << keylen);
130 return NULL;
131 }
132
133 if (rsa == NULL) {
134 debugs(83, DBG_IMPORTANT, "ssl_temp_rsa_cb: Failed to generate key " << keylen);
135 return NULL;
136 }
137
138 if (newkey) {
139 if (do_debug(83, 5))
140 PEM_write_RSAPrivateKey(debug_log, rsa, NULL, NULL, 0, NULL, NULL);
141
142 debugs(83, DBG_IMPORTANT, "Generated ephemeral RSA key of length " << keylen);
143 }
144
145 return rsa;
146 }
147
148 int Ssl::asn1timeToString(ASN1_TIME *tm, char *buf, int len)
149 {
150 BIO *bio;
151 int write = 0;
152 bio = BIO_new(BIO_s_mem());
153 if (bio) {
154 if (ASN1_TIME_print(bio, tm))
155 write = BIO_read(bio, buf, len-1);
156 BIO_free(bio);
157 }
158 buf[write]='\0';
159 return write;
160 }
161
162 int Ssl::matchX509CommonNames(X509 *peer_cert, void *check_data, int (*check_func)(void *check_data, ASN1_STRING *cn_data))
163 {
164 assert(peer_cert);
165
166 X509_NAME *name = X509_get_subject_name(peer_cert);
167
168 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)) {
169
170 ASN1_STRING *cn_data = X509_NAME_ENTRY_get_data(X509_NAME_get_entry(name, i));
171
172 if ( (*check_func)(check_data, cn_data) == 0)
173 return 1;
174 }
175
176 STACK_OF(GENERAL_NAME) * altnames;
177 altnames = (STACK_OF(GENERAL_NAME)*)X509_get_ext_d2i(peer_cert, NID_subject_alt_name, NULL, NULL);
178
179 if (altnames) {
180 int numalts = sk_GENERAL_NAME_num(altnames);
181 for (int i = 0; i < numalts; ++i) {
182 const GENERAL_NAME *check = sk_GENERAL_NAME_value(altnames, i);
183 if (check->type != GEN_DNS) {
184 continue;
185 }
186 ASN1_STRING *cn_data = check->d.dNSName;
187
188 if ( (*check_func)(check_data, cn_data) == 0) {
189 sk_GENERAL_NAME_pop_free(altnames, GENERAL_NAME_free);
190 return 1;
191 }
192 }
193 sk_GENERAL_NAME_pop_free(altnames, GENERAL_NAME_free);
194 }
195 return 0;
196 }
197
198 static int check_domain( void *check_data, ASN1_STRING *cn_data)
199 {
200 char cn[1024];
201 const char *server = (const char *)check_data;
202
203 if (cn_data->length > (int)sizeof(cn) - 1) {
204 return 1; //if does not fit our buffer just ignore
205 }
206 char *s = reinterpret_cast<char*>(cn_data->data);
207 char *d = cn;
208 for (int i = 0; i < cn_data->length; ++i, ++d, ++s) {
209 if (*s == '\0')
210 return 1; // always a domain mismatch. contains 0x00
211 *d = *s;
212 }
213 cn[cn_data->length] = '\0';
214 debugs(83, 4, "Verifying server domain " << server << " to certificate name/subjectAltName " << cn);
215 return matchDomainName(server, cn[0] == '*' ? cn + 1 : cn);
216 }
217
218 bool Ssl::checkX509ServerValidity(X509 *cert, const char *server)
219 {
220 return matchX509CommonNames(cert, (void *)server, check_domain);
221 }
222
223 /// \ingroup ServerProtocolSSLInternal
224 static int
225 ssl_verify_cb(int ok, X509_STORE_CTX * ctx)
226 {
227 // preserve original ctx->error before SSL_ calls can overwrite it
228 Ssl::ssl_error_t error_no = ok ? SSL_ERROR_NONE : ctx->error;
229
230 char buffer[256] = "";
231 SSL *ssl = (SSL *)X509_STORE_CTX_get_ex_data(ctx, SSL_get_ex_data_X509_STORE_CTX_idx());
232 Security::ContextPtr sslctx = SSL_get_SSL_CTX(ssl);
233 SBuf *server = (SBuf *)SSL_get_ex_data(ssl, ssl_ex_index_server);
234 void *dont_verify_domain = SSL_CTX_get_ex_data(sslctx, ssl_ctx_ex_index_dont_verify_domain);
235 ACLChecklist *check = (ACLChecklist*)SSL_get_ex_data(ssl, ssl_ex_index_cert_error_check);
236 X509 *peeked_cert = (X509 *)SSL_get_ex_data(ssl, ssl_ex_index_ssl_peeked_cert);
237 X509 *peer_cert = ctx->cert;
238
239 X509_NAME_oneline(X509_get_subject_name(peer_cert), buffer,
240 sizeof(buffer));
241
242 // detect infinite loops
243 uint32_t *validationCounter = static_cast<uint32_t *>(SSL_get_ex_data(ssl, ssl_ex_index_ssl_validation_counter));
244 if (!validationCounter) {
245 validationCounter = new uint32_t(1);
246 SSL_set_ex_data(ssl, ssl_ex_index_ssl_validation_counter, validationCounter);
247 } else {
248 // overflows allowed if SQUID_CERT_VALIDATION_ITERATION_MAX >= UINT32_MAX
249 (*validationCounter)++;
250 }
251
252 if ((*validationCounter) >= SQUID_CERT_VALIDATION_ITERATION_MAX) {
253 ok = 0; // or the validation loop will never stop
254 error_no = SQUID_X509_V_ERR_INFINITE_VALIDATION;
255 debugs(83, 2, "SQUID_X509_V_ERR_INFINITE_VALIDATION: " <<
256 *validationCounter << " iterations while checking " << buffer);
257 }
258
259 if (ok) {
260 debugs(83, 5, "SSL Certificate signature OK: " << buffer);
261
262 // Check for domain mismatch only if the current certificate is the peer certificate.
263 if (!dont_verify_domain && server && peer_cert == X509_STORE_CTX_get_current_cert(ctx)) {
264 if (!Ssl::checkX509ServerValidity(peer_cert, server->c_str())) {
265 debugs(83, 2, "SQUID_X509_V_ERR_DOMAIN_MISMATCH: Certificate " << buffer << " does not match domainname " << server);
266 ok = 0;
267 error_no = SQUID_X509_V_ERR_DOMAIN_MISMATCH;
268 }
269 }
270 }
271
272 if (ok && peeked_cert) {
273 // Check whether the already peeked certificate matches the new one.
274 if (X509_cmp(peer_cert, peeked_cert) != 0) {
275 debugs(83, 2, "SQUID_X509_V_ERR_CERT_CHANGE: Certificate " << buffer << " does not match peeked certificate");
276 ok = 0;
277 error_no = SQUID_X509_V_ERR_CERT_CHANGE;
278 }
279 }
280
281 if (!ok) {
282 X509 *broken_cert = X509_STORE_CTX_get_current_cert(ctx);
283 if (!broken_cert)
284 broken_cert = peer_cert;
285
286 Ssl::CertErrors *errs = static_cast<Ssl::CertErrors *>(SSL_get_ex_data(ssl, ssl_ex_index_ssl_errors));
287 if (!errs) {
288 const int depth = X509_STORE_CTX_get_error_depth(ctx);
289 errs = new Ssl::CertErrors(Ssl::CertError(error_no, broken_cert, depth));
290 if (!SSL_set_ex_data(ssl, ssl_ex_index_ssl_errors, (void *)errs)) {
291 debugs(83, 2, "Failed to set ssl error_no in ssl_verify_cb: Certificate " << buffer);
292 delete errs;
293 errs = NULL;
294 }
295 } else // remember another error number
296 errs->push_back_unique(Ssl::CertError(error_no, broken_cert));
297
298 if (const char *err_descr = Ssl::GetErrorDescr(error_no))
299 debugs(83, 5, err_descr << ": " << buffer);
300 else
301 debugs(83, DBG_IMPORTANT, "SSL unknown certificate error " << error_no << " in " << buffer);
302
303 // Check if the certificate error can be bypassed.
304 // Infinity validation loop errors can not bypassed.
305 if (error_no != SQUID_X509_V_ERR_INFINITE_VALIDATION) {
306 if (check) {
307 ACLFilledChecklist *filledCheck = Filled(check);
308 assert(!filledCheck->sslErrors);
309 filledCheck->sslErrors = new Ssl::CertErrors(Ssl::CertError(error_no, broken_cert));
310 filledCheck->serverCert.resetAndLock(peer_cert);
311 if (check->fastCheck() == ACCESS_ALLOWED) {
312 debugs(83, 3, "bypassing SSL error " << error_no << " in " << buffer);
313 ok = 1;
314 } else {
315 debugs(83, 5, "confirming SSL error " << error_no);
316 }
317 delete filledCheck->sslErrors;
318 filledCheck->sslErrors = NULL;
319 filledCheck->serverCert.reset(NULL);
320 }
321 // If the certificate validator is used then we need to allow all errors and
322 // pass them to certficate validator for more processing
323 else if (Ssl::TheConfig.ssl_crt_validator) {
324 ok = 1;
325 }
326 }
327 }
328
329 if (Ssl::TheConfig.ssl_crt_validator) {
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 }
336 }
337
338 if (!ok && !SSL_get_ex_data(ssl, ssl_ex_index_ssl_error_detail) ) {
339
340 // Find the broken certificate. It may be intermediate.
341 X509 *broken_cert = peer_cert; // reasonable default if search fails
342 // Our SQUID_X509_V_ERR_DOMAIN_MISMATCH implies peer_cert is at fault.
343 if (error_no != SQUID_X509_V_ERR_DOMAIN_MISMATCH) {
344 if (X509 *last_used_cert = X509_STORE_CTX_get_current_cert(ctx))
345 broken_cert = last_used_cert;
346 }
347
348 Ssl::ErrorDetail *errDetail =
349 new Ssl::ErrorDetail(error_no, peer_cert, broken_cert);
350
351 if (!SSL_set_ex_data(ssl, ssl_ex_index_ssl_error_detail, errDetail)) {
352 debugs(83, 2, "Failed to set Ssl::ErrorDetail in ssl_verify_cb: Certificate " << buffer);
353 delete errDetail;
354 }
355 }
356
357 return ok;
358 }
359
360 // "dup" function for SSL_get_ex_new_index("cert_err_check")
361 static int
362 ssl_dupAclChecklist(CRYPTO_EX_DATA *, CRYPTO_EX_DATA *, void *,
363 int, long, void *)
364 {
365 // We do not support duplication of ACLCheckLists.
366 // If duplication is needed, we can count copies with cbdata.
367 assert(false);
368 return 0;
369 }
370
371 // "free" function for SSL_get_ex_new_index("cert_err_check")
372 static void
373 ssl_freeAclChecklist(void *, void *ptr, CRYPTO_EX_DATA *,
374 int, long, void *)
375 {
376 delete static_cast<ACLChecklist *>(ptr); // may be NULL
377 }
378
379 // "free" function for SSL_get_ex_new_index("ssl_error_detail")
380 static void
381 ssl_free_ErrorDetail(void *, void *ptr, CRYPTO_EX_DATA *,
382 int, long, void *)
383 {
384 Ssl::ErrorDetail *errDetail = static_cast <Ssl::ErrorDetail *>(ptr);
385 delete errDetail;
386 }
387
388 static void
389 ssl_free_SslErrors(void *, void *ptr, CRYPTO_EX_DATA *,
390 int, long, void *)
391 {
392 Ssl::CertErrors *errs = static_cast <Ssl::CertErrors*>(ptr);
393 delete errs;
394 }
395
396 // "free" function for SSL_get_ex_new_index("ssl_ex_index_ssl_validation_counter")
397 static void
398 ssl_free_int(void *, void *ptr, CRYPTO_EX_DATA *,
399 int, long, void *)
400 {
401 uint32_t *counter = static_cast <uint32_t *>(ptr);
402 delete counter;
403 }
404
405 /// \ingroup ServerProtocolSSLInternal
406 /// Callback handler function to release STACK_OF(X509) "ex" data stored
407 /// in an SSL object.
408 static void
409 ssl_free_CertChain(void *, void *ptr, CRYPTO_EX_DATA *,
410 int, long, void *)
411 {
412 STACK_OF(X509) *certsChain = static_cast <STACK_OF(X509) *>(ptr);
413 sk_X509_pop_free(certsChain,X509_free);
414 }
415
416 // "free" function for X509 certificates
417 static void
418 ssl_free_X509(void *, void *ptr, CRYPTO_EX_DATA *,
419 int, long, void *)
420 {
421 X509 *cert = static_cast <X509 *>(ptr);
422 X509_free(cert);
423 }
424
425 // "free" function for SBuf
426 static void
427 ssl_free_SBuf(void *, void *ptr, CRYPTO_EX_DATA *,
428 int, long, void *)
429 {
430 SBuf *buf = static_cast <SBuf *>(ptr);
431 delete buf;
432 }
433
434 void
435 Ssl::Initialize(void)
436 {
437 static bool initialized = false;
438 if (initialized)
439 return;
440 initialized = true;
441
442 SSL_load_error_strings();
443 SSLeay_add_ssl_algorithms();
444
445 #if HAVE_OPENSSL_ENGINE_H
446 if (::Config.SSL.ssl_engine) {
447 ENGINE *e;
448 if (!(e = ENGINE_by_id(::Config.SSL.ssl_engine)))
449 fatalf("Unable to find SSL engine '%s'\n", ::Config.SSL.ssl_engine);
450
451 if (!ENGINE_set_default(e, ENGINE_METHOD_ALL)) {
452 const int ssl_error = ERR_get_error();
453 fatalf("Failed to initialise SSL engine: %s\n", ERR_error_string(ssl_error, NULL));
454 }
455 }
456 #else
457 if (::Config.SSL.ssl_engine)
458 fatalf("Your OpenSSL has no SSL engine support\n");
459 #endif
460
461 const char *defName = ::Config.SSL.certSignHash ? ::Config.SSL.certSignHash : SQUID_SSL_SIGN_HASH_IF_NONE;
462 Ssl::DefaultSignHash = EVP_get_digestbyname(defName);
463 if (!Ssl::DefaultSignHash)
464 fatalf("Sign hash '%s' is not supported\n", defName);
465
466 ssl_ex_index_server = SSL_get_ex_new_index(0, (void *) "server", NULL, NULL, ssl_free_SBuf);
467 ssl_ctx_ex_index_dont_verify_domain = SSL_CTX_get_ex_new_index(0, (void *) "dont_verify_domain", NULL, NULL, NULL);
468 ssl_ex_index_cert_error_check = SSL_get_ex_new_index(0, (void *) "cert_error_check", NULL, &ssl_dupAclChecklist, &ssl_freeAclChecklist);
469 ssl_ex_index_ssl_error_detail = SSL_get_ex_new_index(0, (void *) "ssl_error_detail", NULL, NULL, &ssl_free_ErrorDetail);
470 ssl_ex_index_ssl_peeked_cert = SSL_get_ex_new_index(0, (void *) "ssl_peeked_cert", NULL, NULL, &ssl_free_X509);
471 ssl_ex_index_ssl_errors = SSL_get_ex_new_index(0, (void *) "ssl_errors", NULL, NULL, &ssl_free_SslErrors);
472 ssl_ex_index_ssl_cert_chain = SSL_get_ex_new_index(0, (void *) "ssl_cert_chain", NULL, NULL, &ssl_free_CertChain);
473 ssl_ex_index_ssl_validation_counter = SSL_get_ex_new_index(0, (void *) "ssl_validation_counter", NULL, NULL, &ssl_free_int);
474 }
475
476 #if defined(SSL3_FLAGS_NO_RENEGOTIATE_CIPHERS)
477 static void
478 ssl_info_cb(const SSL *ssl, int where, int ret)
479 {
480 (void)ret;
481 if ((where & SSL_CB_HANDSHAKE_DONE) != 0) {
482 // disable renegotiation (CVE-2009-3555)
483 ssl->s3->flags |= SSL3_FLAGS_NO_RENEGOTIATE_CIPHERS;
484 }
485 }
486 #endif
487
488 static bool
489 configureSslContext(Security::ContextPtr sslContext, AnyP::PortCfg &port)
490 {
491 int ssl_error;
492 SSL_CTX_set_options(sslContext, port.secure.parsedOptions);
493
494 #if defined(SSL3_FLAGS_NO_RENEGOTIATE_CIPHERS)
495 SSL_CTX_set_info_callback(sslContext, ssl_info_cb);
496 #endif
497
498 if (port.sslContextSessionId)
499 SSL_CTX_set_session_id_context(sslContext, (const unsigned char *)port.sslContextSessionId, strlen(port.sslContextSessionId));
500
501 if (port.secure.parsedFlags & SSL_FLAG_NO_SESSION_REUSE) {
502 SSL_CTX_set_session_cache_mode(sslContext, SSL_SESS_CACHE_OFF);
503 }
504
505 if (Config.SSL.unclean_shutdown) {
506 debugs(83, 5, "Enabling quiet SSL shutdowns (RFC violation).");
507
508 SSL_CTX_set_quiet_shutdown(sslContext, 1);
509 }
510
511 if (!port.secure.sslCipher.isEmpty()) {
512 debugs(83, 5, "Using chiper suite " << port.secure.sslCipher << ".");
513
514 if (!SSL_CTX_set_cipher_list(sslContext, port.secure.sslCipher.c_str())) {
515 ssl_error = ERR_get_error();
516 debugs(83, DBG_CRITICAL, "ERROR: Failed to set SSL cipher suite '" << port.secure.sslCipher << "': " << ERR_error_string(ssl_error, NULL));
517 return false;
518 }
519 }
520
521 debugs(83, 9, "Setting RSA key generation callback.");
522 SSL_CTX_set_tmp_rsa_callback(sslContext, ssl_temp_rsa_cb);
523
524 port.secure.updateContextEecdh(sslContext);
525 port.secure.updateContextCa(sslContext);
526
527 if (port.clientCA.get()) {
528 ERR_clear_error();
529 if (STACK_OF(X509_NAME) *clientca = SSL_dup_CA_list(port.clientCA.get())) {
530 SSL_CTX_set_client_CA_list(sslContext, clientca);
531 } else {
532 ssl_error = ERR_get_error();
533 debugs(83, DBG_CRITICAL, "ERROR: Failed to dupe the client CA list: " << ERR_error_string(ssl_error, NULL));
534 return false;
535 }
536
537 if (port.secure.parsedFlags & SSL_FLAG_DELAYED_AUTH) {
538 debugs(83, 9, "Not requesting client certificates until acl processing requires one");
539 SSL_CTX_set_verify(sslContext, SSL_VERIFY_NONE, NULL);
540 } else {
541 debugs(83, 9, "Requiring client certificates.");
542 SSL_CTX_set_verify(sslContext, SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT, ssl_verify_cb);
543 }
544
545 port.secure.updateContextCrl(sslContext);
546
547 } else {
548 debugs(83, 9, "Not requiring any client certificates");
549 SSL_CTX_set_verify(sslContext, SSL_VERIFY_NONE, NULL);
550 }
551
552 if (port.secure.parsedFlags & SSL_FLAG_DONT_VERIFY_DOMAIN)
553 SSL_CTX_set_ex_data(sslContext, ssl_ctx_ex_index_dont_verify_domain, (void *) -1);
554
555 setSessionCallbacks(sslContext);
556
557 return true;
558 }
559
560 Security::ContextPtr
561 sslCreateServerContext(AnyP::PortCfg &port)
562 {
563 Security::ContextPtr sslContext(port.secure.createBlankContext());
564 if (!sslContext)
565 return nullptr;
566
567 if (!SSL_CTX_use_certificate(sslContext, port.signingCert.get())) {
568 const int ssl_error = ERR_get_error();
569 const auto &keys = port.secure.certs.front();
570 debugs(83, DBG_CRITICAL, "ERROR: Failed to acquire TLS certificate '" << keys.certFile << "': " << ERR_error_string(ssl_error, NULL));
571 SSL_CTX_free(sslContext);
572 return NULL;
573 }
574
575 if (!SSL_CTX_use_PrivateKey(sslContext, port.signPkey.get())) {
576 const int ssl_error = ERR_get_error();
577 const auto &keys = port.secure.certs.front();
578 debugs(83, DBG_CRITICAL, "ERROR: Failed to acquire TLS private key '" << keys.privateKeyFile << "': " << ERR_error_string(ssl_error, NULL));
579 SSL_CTX_free(sslContext);
580 return NULL;
581 }
582
583 Ssl::addChainToSslContext(sslContext, port.certsToChain.get());
584
585 /* Alternate code;
586 debugs(83, DBG_IMPORTANT, "Using certificate in " << certfile);
587
588 if (!SSL_CTX_use_certificate_chain_file(sslContext, certfile)) {
589 ssl_error = ERR_get_error();
590 debugs(83, DBG_CRITICAL, "ERROR: Failed to acquire SSL certificate '" << certfile << "': " << ERR_error_string(ssl_error, NULL));
591 SSL_CTX_free(sslContext);
592 return NULL;
593 }
594
595 debugs(83, DBG_IMPORTANT, "Using private key in " << keyfile);
596 ssl_ask_password(sslContext, keyfile);
597
598 if (!SSL_CTX_use_PrivateKey_file(sslContext, keyfile, SSL_FILETYPE_PEM)) {
599 ssl_error = ERR_get_error();
600 debugs(83, DBG_CRITICAL, "ERROR: Failed to acquire SSL private key '" << keyfile << "': " << ERR_error_string(ssl_error, NULL));
601 SSL_CTX_free(sslContext);
602 return NULL;
603 }
604
605 debugs(83, 5, "Comparing private and public SSL keys.");
606
607 if (!SSL_CTX_check_private_key(sslContext)) {
608 ssl_error = ERR_get_error();
609 debugs(83, DBG_CRITICAL, "ERROR: SSL private key '" << certfile << "' does not match public key '" <<
610 keyfile << "': " << ERR_error_string(ssl_error, NULL));
611 SSL_CTX_free(sslContext);
612 return NULL;
613 }
614 */
615
616 if (!configureSslContext(sslContext, port)) {
617 debugs(83, DBG_CRITICAL, "ERROR: Configuring static SSL context");
618 SSL_CTX_free(sslContext);
619 return NULL;
620 }
621
622 return sslContext;
623 }
624
625 Security::ContextPtr
626 sslCreateClientContext(Security::PeerOptions &peer, long options, long fl)
627 {
628 Security::ContextPtr sslContext(peer.createBlankContext());
629 if (!sslContext)
630 return nullptr;
631
632 SSL_CTX_set_options(sslContext, options);
633
634 #if defined(SSL3_FLAGS_NO_RENEGOTIATE_CIPHERS)
635 SSL_CTX_set_info_callback(sslContext, ssl_info_cb);
636 #endif
637
638 if (!peer.sslCipher.isEmpty()) {
639 debugs(83, 5, "Using chiper suite " << peer.sslCipher << ".");
640
641 const char *cipher = peer.sslCipher.c_str();
642 if (!SSL_CTX_set_cipher_list(sslContext, cipher)) {
643 const int ssl_error = ERR_get_error();
644 fatalf("Failed to set SSL cipher suite '%s': %s\n",
645 cipher, ERR_error_string(ssl_error, NULL));
646 }
647 }
648
649 if (!peer.certs.empty()) {
650 // TODO: support loading multiple cert/key pairs
651 auto &keys = peer.certs.front();
652 if (!keys.certFile.isEmpty()) {
653 debugs(83, DBG_IMPORTANT, "Using certificate in " << keys.certFile);
654
655 const char *certfile = keys.certFile.c_str();
656 if (!SSL_CTX_use_certificate_chain_file(sslContext, certfile)) {
657 const int ssl_error = ERR_get_error();
658 fatalf("Failed to acquire SSL certificate '%s': %s\n",
659 certfile, ERR_error_string(ssl_error, NULL));
660 }
661
662 debugs(83, DBG_IMPORTANT, "Using private key in " << keys.privateKeyFile);
663 const char *keyfile = keys.privateKeyFile.c_str();
664 ssl_ask_password(sslContext, keyfile);
665
666 if (!SSL_CTX_use_PrivateKey_file(sslContext, keyfile, SSL_FILETYPE_PEM)) {
667 const int ssl_error = ERR_get_error();
668 fatalf("Failed to acquire SSL private key '%s': %s\n",
669 keyfile, ERR_error_string(ssl_error, NULL));
670 }
671
672 debugs(83, 5, "Comparing private and public SSL keys.");
673
674 if (!SSL_CTX_check_private_key(sslContext)) {
675 const int ssl_error = ERR_get_error();
676 fatalf("SSL private key '%s' does not match public key '%s': %s\n",
677 certfile, keyfile, ERR_error_string(ssl_error, NULL));
678 }
679 }
680 }
681
682 debugs(83, 9, "Setting RSA key generation callback.");
683 SSL_CTX_set_tmp_rsa_callback(sslContext, ssl_temp_rsa_cb);
684
685 if (fl & SSL_FLAG_DONT_VERIFY_PEER) {
686 debugs(83, 2, "NOTICE: Peer certificates are not verified for validity!");
687 SSL_CTX_set_verify(sslContext, SSL_VERIFY_NONE, NULL);
688 } else {
689 debugs(83, 9, "Setting certificate verification callback.");
690 SSL_CTX_set_verify(sslContext, SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT, ssl_verify_cb);
691 }
692
693 return sslContext;
694 }
695
696 /// \ingroup ServerProtocolSSLInternal
697 int
698 ssl_read_method(int fd, char *buf, int len)
699 {
700 SSL *ssl = fd_table[fd].ssl;
701 int i;
702
703 #if DONT_DO_THIS
704
705 if (!SSL_is_init_finished(ssl)) {
706 errno = ENOTCONN;
707 return -1;
708 }
709
710 #endif
711
712 i = SSL_read(ssl, buf, len);
713
714 if (i > 0 && SSL_pending(ssl) > 0) {
715 debugs(83, 2, "SSL FD " << fd << " is pending");
716 fd_table[fd].flags.read_pending = true;
717 } else
718 fd_table[fd].flags.read_pending = false;
719
720 return i;
721 }
722
723 /// \ingroup ServerProtocolSSLInternal
724 int
725 ssl_write_method(int fd, const char *buf, int len)
726 {
727 SSL *ssl = fd_table[fd].ssl;
728 int i;
729
730 if (!SSL_is_init_finished(ssl)) {
731 errno = ENOTCONN;
732 return -1;
733 }
734
735 i = SSL_write(ssl, buf, len);
736
737 return i;
738 }
739
740 void
741 ssl_shutdown_method(SSL *ssl)
742 {
743 SSL_shutdown(ssl);
744 }
745
746 /// \ingroup ServerProtocolSSLInternal
747 static const char *
748 ssl_get_attribute(X509_NAME * name, const char *attribute_name)
749 {
750 static char buffer[1024];
751 int nid;
752
753 buffer[0] = '\0';
754
755 if (strcmp(attribute_name, "DN") == 0) {
756 X509_NAME_oneline(name, buffer, sizeof(buffer));
757 goto done;
758 }
759
760 nid = OBJ_txt2nid((char *) attribute_name);
761
762 if (nid == 0) {
763 debugs(83, DBG_IMPORTANT, "WARNING: Unknown SSL attribute name '" << attribute_name << "'");
764 return NULL;
765 }
766
767 X509_NAME_get_text_by_NID(name, nid, buffer, sizeof(buffer));
768
769 done:
770 return *buffer ? buffer : NULL;
771 }
772
773 /// \ingroup ServerProtocolSSLInternal
774 const char *
775 Ssl::GetX509UserAttribute(X509 * cert, const char *attribute_name)
776 {
777 X509_NAME *name;
778 const char *ret;
779
780 if (!cert)
781 return NULL;
782
783 name = X509_get_subject_name(cert);
784
785 ret = ssl_get_attribute(name, attribute_name);
786
787 return ret;
788 }
789
790 const char *
791 Ssl::GetX509Fingerprint(X509 * cert, const char *)
792 {
793 static char buf[1024];
794 if (!cert)
795 return NULL;
796
797 unsigned int n;
798 unsigned char md[EVP_MAX_MD_SIZE];
799 if (!X509_digest(cert, EVP_sha1(), md, &n))
800 return NULL;
801
802 assert(3 * n + 1 < sizeof(buf));
803
804 char *s = buf;
805 for (unsigned int i=0; i < n; ++i, s += 3) {
806 const char term = (i + 1 < n) ? ':' : '\0';
807 snprintf(s, 4, "%02X%c", md[i], term);
808 }
809
810 return buf;
811 }
812
813 /// \ingroup ServerProtocolSSLInternal
814 const char *
815 Ssl::GetX509CAAttribute(X509 * cert, const char *attribute_name)
816 {
817
818 X509_NAME *name;
819 const char *ret;
820
821 if (!cert)
822 return NULL;
823
824 name = X509_get_issuer_name(cert);
825
826 ret = ssl_get_attribute(name, attribute_name);
827
828 return ret;
829 }
830
831 const char *sslGetUserAttribute(SSL *ssl, const char *attribute_name)
832 {
833 if (!ssl)
834 return NULL;
835
836 X509 *cert = SSL_get_peer_certificate(ssl);
837
838 const char *attr = Ssl::GetX509UserAttribute(cert, attribute_name);
839
840 X509_free(cert);
841 return attr;
842 }
843
844 const char *sslGetCAAttribute(SSL *ssl, const char *attribute_name)
845 {
846 if (!ssl)
847 return NULL;
848
849 X509 *cert = SSL_get_peer_certificate(ssl);
850
851 const char *attr = Ssl::GetX509CAAttribute(cert, attribute_name);
852
853 X509_free(cert);
854 return attr;
855 }
856
857 const char *
858 sslGetUserEmail(SSL * ssl)
859 {
860 return sslGetUserAttribute(ssl, "emailAddress");
861 }
862
863 const char *
864 sslGetUserCertificatePEM(SSL *ssl)
865 {
866 X509 *cert;
867 BIO *mem;
868 static char *str = NULL;
869 char *ptr;
870 long len;
871
872 safe_free(str);
873
874 if (!ssl)
875 return NULL;
876
877 cert = SSL_get_peer_certificate(ssl);
878
879 if (!cert)
880 return NULL;
881
882 mem = BIO_new(BIO_s_mem());
883
884 PEM_write_bio_X509(mem, cert);
885
886 len = BIO_get_mem_data(mem, &ptr);
887
888 str = (char *)xmalloc(len + 1);
889
890 memcpy(str, ptr, len);
891
892 str[len] = '\0';
893
894 X509_free(cert);
895
896 BIO_free(mem);
897
898 return str;
899 }
900
901 const char *
902 sslGetUserCertificateChainPEM(SSL *ssl)
903 {
904 STACK_OF(X509) *chain;
905 BIO *mem;
906 static char *str = NULL;
907 char *ptr;
908 long len;
909 int i;
910
911 safe_free(str);
912
913 if (!ssl)
914 return NULL;
915
916 chain = SSL_get_peer_cert_chain(ssl);
917
918 if (!chain)
919 return sslGetUserCertificatePEM(ssl);
920
921 mem = BIO_new(BIO_s_mem());
922
923 for (i = 0; i < sk_X509_num(chain); ++i) {
924 X509 *cert = sk_X509_value(chain, i);
925 PEM_write_bio_X509(mem, cert);
926 }
927
928 len = BIO_get_mem_data(mem, &ptr);
929
930 str = (char *)xmalloc(len + 1);
931 memcpy(str, ptr, len);
932 str[len] = '\0';
933
934 BIO_free(mem);
935
936 return str;
937 }
938
939 /// Create SSL context and apply ssl certificate and private key to it.
940 Security::ContextPtr
941 Ssl::createSSLContext(Security::CertPointer & x509, Ssl::EVP_PKEY_Pointer & pkey, AnyP::PortCfg &port)
942 {
943 #if (OPENSSL_VERSION_NUMBER >= 0x10100000L)
944 Ssl::SSL_CTX_Pointer sslContext(SSL_CTX_new(TLS_server_method()));
945 #else
946 Ssl::SSL_CTX_Pointer sslContext(SSL_CTX_new(SSLv23_server_method()));
947 #endif
948
949 if (!SSL_CTX_use_certificate(sslContext.get(), x509.get()))
950 return NULL;
951
952 if (!SSL_CTX_use_PrivateKey(sslContext.get(), pkey.get()))
953 return NULL;
954
955 if (!configureSslContext(sslContext.get(), port))
956 return NULL;
957
958 return sslContext.release();
959 }
960
961 Security::ContextPtr
962 Ssl::generateSslContextUsingPkeyAndCertFromMemory(const char * data, AnyP::PortCfg &port)
963 {
964 Security::CertPointer cert;
965 Ssl::EVP_PKEY_Pointer pkey;
966 if (!readCertAndPrivateKeyFromMemory(cert, pkey, data) || !cert || !pkey)
967 return nullptr;
968
969 return createSSLContext(cert, pkey, port);
970 }
971
972 Security::ContextPtr
973 Ssl::generateSslContext(CertificateProperties const &properties, AnyP::PortCfg &port)
974 {
975 Security::CertPointer cert;
976 Ssl::EVP_PKEY_Pointer pkey;
977 if (!generateSslCertificate(cert, pkey, properties) || !cert || !pkey)
978 return nullptr;
979
980 return createSSLContext(cert, pkey, port);
981 }
982
983 bool
984 Ssl::configureSSL(SSL *ssl, CertificateProperties const &properties, AnyP::PortCfg &port)
985 {
986 Security::CertPointer cert;
987 Ssl::EVP_PKEY_Pointer pkey;
988 if (!generateSslCertificate(cert, pkey, properties))
989 return false;
990
991 if (!cert)
992 return false;
993
994 if (!pkey)
995 return false;
996
997 if (!SSL_use_certificate(ssl, cert.get()))
998 return false;
999
1000 if (!SSL_use_PrivateKey(ssl, pkey.get()))
1001 return false;
1002
1003 return true;
1004 }
1005
1006 bool
1007 Ssl::configureSSLUsingPkeyAndCertFromMemory(SSL *ssl, const char *data, AnyP::PortCfg &port)
1008 {
1009 Security::CertPointer cert;
1010 Ssl::EVP_PKEY_Pointer pkey;
1011 if (!readCertAndPrivateKeyFromMemory(cert, pkey, data))
1012 return false;
1013
1014 if (!cert || !pkey)
1015 return false;
1016
1017 if (!SSL_use_certificate(ssl, cert.get()))
1018 return false;
1019
1020 if (!SSL_use_PrivateKey(ssl, pkey.get()))
1021 return false;
1022
1023 return true;
1024 }
1025
1026 bool Ssl::verifySslCertificate(Security::ContextPtr sslContext, CertificateProperties const &properties)
1027 {
1028 // SSL_get_certificate is buggy in openssl versions 1.0.1d and 1.0.1e
1029 // Try to retrieve certificate directly from Security::ContextPtr object
1030 #if SQUID_USE_SSLGETCERTIFICATE_HACK
1031 X509 ***pCert = (X509 ***)sslContext->cert;
1032 X509 * cert = pCert && *pCert ? **pCert : NULL;
1033 #elif SQUID_SSLGETCERTIFICATE_BUGGY
1034 X509 * cert = NULL;
1035 assert(0);
1036 #else
1037 // Temporary ssl for getting X509 certificate from SSL_CTX.
1038 Ssl::SSL_Pointer ssl(SSL_new(sslContext));
1039 X509 * cert = SSL_get_certificate(ssl.get());
1040 #endif
1041 if (!cert)
1042 return false;
1043 ASN1_TIME * time_notBefore = X509_get_notBefore(cert);
1044 ASN1_TIME * time_notAfter = X509_get_notAfter(cert);
1045 bool ret = (X509_cmp_current_time(time_notBefore) < 0 && X509_cmp_current_time(time_notAfter) > 0);
1046 if (!ret)
1047 return false;
1048
1049 return certificateMatchesProperties(cert, properties);
1050 }
1051
1052 bool
1053 Ssl::setClientSNI(SSL *ssl, const char *fqdn)
1054 {
1055 //The SSL_CTRL_SET_TLSEXT_HOSTNAME is a openssl macro which indicates
1056 // if the TLS servername extension (SNI) is enabled in openssl library.
1057 #if defined(SSL_CTRL_SET_TLSEXT_HOSTNAME)
1058 if (!SSL_set_tlsext_host_name(ssl, fqdn)) {
1059 const int ssl_error = ERR_get_error();
1060 debugs(83, 3, "WARNING: unable to set TLS servername extension (SNI): " <<
1061 ERR_error_string(ssl_error, NULL) << "\n");
1062 return false;
1063 }
1064 return true;
1065 #else
1066 debugs(83, 7, "no support for TLS servername extension (SNI)\n");
1067 return false;
1068 #endif
1069 }
1070
1071 void Ssl::addChainToSslContext(Security::ContextPtr sslContext, STACK_OF(X509) *chain)
1072 {
1073 if (!chain)
1074 return;
1075
1076 for (int i = 0; i < sk_X509_num(chain); ++i) {
1077 X509 *cert = sk_X509_value(chain, i);
1078 if (SSL_CTX_add_extra_chain_cert(sslContext, cert)) {
1079 // increase the certificate lock
1080 CRYPTO_add(&(cert->references),1,CRYPTO_LOCK_X509);
1081 } else {
1082 const int ssl_error = ERR_get_error();
1083 debugs(83, DBG_IMPORTANT, "WARNING: can not add certificate to SSL context chain: " << ERR_error_string(ssl_error, NULL));
1084 }
1085 }
1086 }
1087
1088 bool
1089 Ssl::loadCerts(const char *certsFile, Ssl::CertsIndexedList &list)
1090 {
1091 BIO *in = BIO_new_file(certsFile, "r");
1092 if (!in) {
1093 debugs(83, DBG_IMPORTANT, "Failed to open '" << certsFile << "' to load certificates");
1094 return false;
1095 }
1096
1097 X509 *aCert;
1098 while((aCert = PEM_read_bio_X509(in, NULL, NULL, NULL))) {
1099 static char buffer[2048];
1100 X509_NAME_oneline(X509_get_subject_name(aCert), buffer, sizeof(buffer));
1101 list.insert(std::pair<SBuf, X509 *>(SBuf(buffer), aCert));
1102 }
1103 debugs(83, 4, "Loaded " << list.size() << " certificates from file: '" << certsFile << "'");
1104 BIO_free(in);
1105 return true;
1106 }
1107
1108 /// quickly find a certificate with a given issuer in Ssl::CertsIndexedList.
1109 static X509 *
1110 findCertByIssuerFast(X509_STORE_CTX *ctx, Ssl::CertsIndexedList &list, X509 *cert)
1111 {
1112 static char buffer[2048];
1113
1114 if (X509_NAME *issuerName = X509_get_issuer_name(cert))
1115 X509_NAME_oneline(issuerName, buffer, sizeof(buffer));
1116 else
1117 return NULL;
1118
1119 const auto ret = list.equal_range(SBuf(buffer));
1120 for (Ssl::CertsIndexedList::iterator it = ret.first; it != ret.second; ++it) {
1121 X509 *issuer = it->second;
1122 if (ctx->check_issued(ctx, cert, issuer)) {
1123 return issuer;
1124 }
1125 }
1126 return NULL;
1127 }
1128
1129 /// slowly find a certificate with a given issuer using linear search
1130 static X509 *
1131 findCertByIssuerSlowly(X509_STORE_CTX *ctx, STACK_OF(X509) *sk, X509 *cert)
1132 {
1133 const int skItemsNum = sk_X509_num(sk);
1134 for (int i = 0; i < skItemsNum; ++i) {
1135 X509 *issuer = sk_X509_value(sk, i);
1136 if (ctx->check_issued(ctx, cert, issuer))
1137 return issuer;
1138 }
1139 return NULL;
1140 }
1141
1142 /// add missing issuer certificates to untrustedCerts
1143 static void
1144 completeIssuers(X509_STORE_CTX *ctx, STACK_OF(X509) *untrustedCerts)
1145 {
1146 debugs(83, 2, "completing " << sk_X509_num(untrustedCerts) << " OpenSSL untrusted certs using " << SquidUntrustedCerts.size() << " configured untrusted certificates");
1147
1148 int depth = ctx->param->depth;
1149 X509 *current = ctx->cert;
1150 int i = 0;
1151 for (i = 0; current && (i < depth); ++i) {
1152 if (ctx->check_issued(ctx, current, current)) {
1153 // either ctx->cert is itself self-signed or untrustedCerts
1154 // aready contain the self-signed current certificate
1155 break;
1156 }
1157
1158 // untrustedCerts is short, not worth indexing
1159 X509 *issuer = findCertByIssuerSlowly(ctx, untrustedCerts, current);
1160 if (!issuer) {
1161 if ((issuer = findCertByIssuerFast(ctx, SquidUntrustedCerts, current)))
1162 sk_X509_push(untrustedCerts, issuer);
1163 }
1164 current = issuer;
1165 }
1166
1167 if (i >= depth)
1168 debugs(83, 2, "exceeded the maximum certificate chain length: " << depth);
1169 }
1170
1171 /// OpenSSL certificate validation callback.
1172 static int
1173 untrustedToStoreCtx_cb(X509_STORE_CTX *ctx,void *data)
1174 {
1175 debugs(83, 4, "Try to use pre-downloaded intermediate certificates\n");
1176
1177 // OpenSSL already maintains ctx->untrusted but we cannot modify
1178 // internal OpenSSL list directly. We have to give OpenSSL our own
1179 // list, but it must include certificates on the OpenSSL ctx->untrusted
1180 STACK_OF(X509) *oldUntrusted = ctx->untrusted;
1181 STACK_OF(X509) *sk = sk_X509_dup(oldUntrusted); // oldUntrusted is always not NULL
1182 completeIssuers(ctx, sk);
1183 X509_STORE_CTX_set_chain(ctx, sk); // No locking/unlocking, just sets ctx->untrusted
1184 int ret = X509_verify_cert(ctx);
1185 X509_STORE_CTX_set_chain(ctx, oldUntrusted); // Set back the old untrusted list
1186 sk_X509_free(sk); // Release sk list
1187 return ret;
1188 }
1189
1190 void
1191 Ssl::useSquidUntrusted(SSL_CTX *sslContext)
1192 {
1193 if (SquidUntrustedCerts.size() > 0)
1194 SSL_CTX_set_cert_verify_callback(sslContext, untrustedToStoreCtx_cb, NULL);
1195 else
1196 SSL_CTX_set_cert_verify_callback(sslContext, NULL, NULL);
1197 }
1198
1199 bool
1200 Ssl::loadSquidUntrusted(const char *path)
1201 {
1202 return Ssl::loadCerts(path, SquidUntrustedCerts);
1203 }
1204
1205 void
1206 Ssl::unloadSquidUntrusted()
1207 {
1208 if (SquidUntrustedCerts.size()) {
1209 for (Ssl::CertsIndexedList::iterator it = SquidUntrustedCerts.begin(); it != SquidUntrustedCerts.end(); ++it) {
1210 X509_free(it->second);
1211 }
1212 SquidUntrustedCerts.clear();
1213 }
1214 }
1215
1216 /**
1217 \ingroup ServerProtocolSSLInternal
1218 * Read certificate from file.
1219 * See also: static readSslX509Certificate function, gadgets.cc file
1220 */
1221 static X509 * readSslX509CertificatesChain(char const * certFilename, STACK_OF(X509)* chain)
1222 {
1223 if (!certFilename)
1224 return NULL;
1225 Ssl::BIO_Pointer bio(BIO_new(BIO_s_file_internal()));
1226 if (!bio)
1227 return NULL;
1228 if (!BIO_read_filename(bio.get(), certFilename))
1229 return NULL;
1230 X509 *certificate = PEM_read_bio_X509(bio.get(), NULL, NULL, NULL);
1231
1232 if (certificate && chain) {
1233
1234 if (X509_check_issued(certificate, certificate) == X509_V_OK)
1235 debugs(83, 5, "Certificate is self-signed, will not be chained");
1236 else {
1237 // and add to the chain any other certificate exist in the file
1238 while (X509 *ca = PEM_read_bio_X509(bio.get(), NULL, NULL, NULL)) {
1239 if (!sk_X509_push(chain, ca))
1240 debugs(83, DBG_IMPORTANT, "WARNING: unable to add CA certificate to cert chain");
1241 }
1242 }
1243 }
1244
1245 return certificate;
1246 }
1247
1248 void Ssl::readCertChainAndPrivateKeyFromFiles(Security::CertPointer & cert, EVP_PKEY_Pointer & pkey, X509_STACK_Pointer & chain, char const * certFilename, char const * keyFilename)
1249 {
1250 if (keyFilename == NULL)
1251 keyFilename = certFilename;
1252
1253 if (certFilename == NULL)
1254 certFilename = keyFilename;
1255
1256 debugs(83, DBG_IMPORTANT, "Using certificate in " << certFilename);
1257
1258 if (!chain)
1259 chain.reset(sk_X509_new_null());
1260 if (!chain)
1261 debugs(83, DBG_IMPORTANT, "WARNING: unable to allocate memory for cert chain");
1262 // XXX: ssl_ask_password_cb needs SSL_CTX_set_default_passwd_cb_userdata()
1263 // so this may not fully work iff Config.Program.ssl_password is set.
1264 pem_password_cb *cb = ::Config.Program.ssl_password ? &ssl_ask_password_cb : NULL;
1265 pkey.reset(readSslPrivateKey(keyFilename, cb));
1266 cert.reset(readSslX509CertificatesChain(certFilename, chain.get()));
1267 if (!pkey || !cert || !X509_check_private_key(cert.get(), pkey.get())) {
1268 pkey.reset(NULL);
1269 cert.reset(NULL);
1270 }
1271 }
1272
1273 bool Ssl::generateUntrustedCert(Security::CertPointer &untrustedCert, EVP_PKEY_Pointer &untrustedPkey, Security::CertPointer const &cert, EVP_PKEY_Pointer const & pkey)
1274 {
1275 // Generate the self-signed certificate, using a hard-coded subject prefix
1276 Ssl::CertificateProperties certProperties;
1277 if (const char *cn = CommonHostName(cert.get())) {
1278 certProperties.commonName = "Not trusted by \"";
1279 certProperties.commonName += cn;
1280 certProperties.commonName += "\"";
1281 } else if (const char *org = getOrganization(cert.get())) {
1282 certProperties.commonName = "Not trusted by \"";
1283 certProperties.commonName += org;
1284 certProperties.commonName += "\"";
1285 } else
1286 certProperties.commonName = "Not trusted";
1287 certProperties.setCommonName = true;
1288 // O, OU, and other CA subject fields will be mimicked
1289 // Expiration date and other common properties will be mimicked
1290 certProperties.signAlgorithm = Ssl::algSignSelf;
1291 certProperties.signWithPkey.resetAndLock(pkey.get());
1292 certProperties.mimicCert.resetAndLock(cert.get());
1293 return Ssl::generateSslCertificate(untrustedCert, untrustedPkey, certProperties);
1294 }
1295
1296 SSL *
1297 SslCreate(Security::ContextPtr sslContext, const int fd, Ssl::Bio::Type type, const char *squidCtx)
1298 {
1299 if (fd < 0) {
1300 debugs(83, DBG_IMPORTANT, "Gone connection");
1301 return NULL;
1302 }
1303
1304 const char *errAction = NULL;
1305 int errCode = 0;
1306 if (SSL *ssl = SSL_new(sslContext)) {
1307 // without BIO, we would call SSL_set_fd(ssl, fd) instead
1308 if (BIO *bio = Ssl::Bio::Create(fd, type)) {
1309 Ssl::Bio::Link(ssl, bio); // cannot fail
1310
1311 fd_table[fd].ssl = ssl;
1312 fd_table[fd].read_method = &ssl_read_method;
1313 fd_table[fd].write_method = &ssl_write_method;
1314 fd_note(fd, squidCtx);
1315
1316 return ssl;
1317 }
1318 errCode = ERR_get_error();
1319 errAction = "failed to initialize I/O";
1320 SSL_free(ssl);
1321 } else {
1322 errCode = ERR_get_error();
1323 errAction = "failed to allocate handle";
1324 }
1325
1326 debugs(83, DBG_IMPORTANT, "ERROR: " << squidCtx << ' ' << errAction <<
1327 ": " << ERR_error_string(errCode, NULL));
1328 return NULL;
1329 }
1330
1331 SSL *
1332 Ssl::CreateClient(Security::ContextPtr sslContext, const int fd, const char *squidCtx)
1333 {
1334 return SslCreate(sslContext, fd, Ssl::Bio::BIO_TO_SERVER, squidCtx);
1335 }
1336
1337 SSL *
1338 Ssl::CreateServer(Security::ContextPtr sslContext, const int fd, const char *squidCtx)
1339 {
1340 return SslCreate(sslContext, fd, Ssl::Bio::BIO_TO_CLIENT, squidCtx);
1341 }
1342
1343 Ssl::CertError::CertError(ssl_error_t anErr, X509 *aCert, int aDepth): code(anErr), depth(aDepth)
1344 {
1345 cert.resetAndLock(aCert);
1346 }
1347
1348 Ssl::CertError::CertError(CertError const &err): code(err.code), depth(err.depth)
1349 {
1350 cert.resetAndLock(err.cert.get());
1351 }
1352
1353 Ssl::CertError &
1354 Ssl::CertError::operator = (const CertError &old)
1355 {
1356 code = old.code;
1357 cert.resetAndLock(old.cert.get());
1358 return *this;
1359 }
1360
1361 bool
1362 Ssl::CertError::operator == (const CertError &ce) const
1363 {
1364 return code == ce.code && cert.get() == ce.cert.get();
1365 }
1366
1367 bool
1368 Ssl::CertError::operator != (const CertError &ce) const
1369 {
1370 return code != ce.code || cert.get() != ce.cert.get();
1371 }
1372
1373 static int
1374 store_session_cb(SSL *ssl, SSL_SESSION *session)
1375 {
1376 if (!SslSessionCache)
1377 return 0;
1378
1379 debugs(83, 5, "Request to store SSL Session ");
1380
1381 SSL_SESSION_set_timeout(session, Config.SSL.session_ttl);
1382
1383 unsigned char *id = session->session_id;
1384 unsigned int idlen = session->session_id_length;
1385 unsigned char key[MEMMAP_SLOT_KEY_SIZE];
1386 // Session ids are of size 32bytes. They should always fit to a
1387 // MemMap::Slot::key
1388 assert(idlen <= MEMMAP_SLOT_KEY_SIZE);
1389 memset(key, 0, sizeof(key));
1390 memcpy(key, id, idlen);
1391 int pos;
1392 Ipc::MemMap::Slot *slotW = SslSessionCache->openForWriting((const cache_key*)key, pos);
1393 if (slotW) {
1394 int lenRequired = i2d_SSL_SESSION(session, NULL);
1395 if (lenRequired < MEMMAP_SLOT_DATA_SIZE) {
1396 unsigned char *p = (unsigned char *)slotW->p;
1397 lenRequired = i2d_SSL_SESSION(session, &p);
1398 slotW->set(key, NULL, lenRequired, squid_curtime + Config.SSL.session_ttl);
1399 }
1400 SslSessionCache->closeForWriting(pos);
1401 debugs(83, 5, "wrote an ssl session entry of size " << lenRequired << " at pos " << pos);
1402 }
1403 return 0;
1404 }
1405
1406 static void
1407 remove_session_cb(SSL_CTX *, SSL_SESSION *sessionID)
1408 {
1409 if (!SslSessionCache)
1410 return ;
1411
1412 debugs(83, 5, "Request to remove corrupted or not valid SSL Session ");
1413 int pos;
1414 Ipc::MemMap::Slot const *slot = SslSessionCache->openForReading((const cache_key*)sessionID, pos);
1415 if (slot == NULL)
1416 return;
1417 SslSessionCache->closeForReading(pos);
1418 // TODO:
1419 // What if we are not able to remove the session?
1420 // Maybe schedule a job to remove it later?
1421 // For now we just have an invalid entry in cache until will be expired
1422 // The openSSL will reject it when we try to use it
1423 SslSessionCache->free(pos);
1424 }
1425
1426 static SSL_SESSION *
1427 get_session_cb(SSL *, unsigned char *sessionID, int len, int *copy)
1428 {
1429 if (!SslSessionCache)
1430 return NULL;
1431
1432 SSL_SESSION *session = NULL;
1433 const unsigned int *p;
1434 p = (unsigned int *)sessionID;
1435 debugs(83, 5, "Request to search for SSL Session of len:" <<
1436 len << p[0] << ":" << p[1]);
1437
1438 int pos;
1439 Ipc::MemMap::Slot const *slot = SslSessionCache->openForReading((const cache_key*)sessionID, pos);
1440 if (slot != NULL) {
1441 if (slot->expire > squid_curtime) {
1442 const unsigned char *ptr = slot->p;
1443 session = d2i_SSL_SESSION(NULL, &ptr, slot->pSize);
1444 debugs(83, 5, "Session retrieved from cache at pos " << pos);
1445 } else
1446 debugs(83, 5, "Session in cache expired");
1447 SslSessionCache->closeForReading(pos);
1448 }
1449
1450 if (!session)
1451 debugs(83, 5, "Failed to retrieved from cache\n");
1452
1453 // With the parameter copy the callback can require the SSL engine
1454 // to increment the reference count of the SSL_SESSION object, Normally
1455 // the reference count is not incremented and therefore the session must
1456 // not be explicitly freed with SSL_SESSION_free(3).
1457 *copy = 0;
1458 return session;
1459 }
1460
1461 static void
1462 setSessionCallbacks(Security::ContextPtr ctx)
1463 {
1464 if (SslSessionCache) {
1465 SSL_CTX_set_session_cache_mode(ctx, SSL_SESS_CACHE_SERVER|SSL_SESS_CACHE_NO_INTERNAL);
1466 SSL_CTX_sess_set_new_cb(ctx, store_session_cb);
1467 SSL_CTX_sess_set_remove_cb(ctx, remove_session_cb);
1468 SSL_CTX_sess_set_get_cb(ctx, get_session_cb);
1469 }
1470 }
1471
1472 static bool
1473 isSslServer()
1474 {
1475 for (AnyP::PortCfgPointer s = HttpPortList; s != NULL; s = s->next) {
1476 if (s->secure.encryptTransport)
1477 return true;
1478 if (s->flags.tunnelSslBumping)
1479 return true;
1480 }
1481
1482 return false;
1483 }
1484
1485 #define SSL_SESSION_ID_SIZE 32
1486 #define SSL_SESSION_MAX_SIZE 10*1024
1487
1488 void
1489 Ssl::initialize_session_cache()
1490 {
1491
1492 if (!isSslServer()) //no need to configure ssl session cache.
1493 return;
1494
1495 // Check if the MemMap keys and data are enough big to hold
1496 // session ids and session data
1497 assert(SSL_SESSION_ID_SIZE >= MEMMAP_SLOT_KEY_SIZE);
1498 assert(SSL_SESSION_MAX_SIZE >= MEMMAP_SLOT_DATA_SIZE);
1499
1500 int configuredItems = ::Config.SSL.sessionCacheSize / sizeof(Ipc::MemMap::Slot);
1501 if (IamWorkerProcess() && configuredItems)
1502 SslSessionCache = new Ipc::MemMap(SslSessionCacheName);
1503 else {
1504 SslSessionCache = NULL;
1505 return;
1506 }
1507
1508 for (AnyP::PortCfgPointer s = HttpPortList; s != NULL; s = s->next) {
1509 if (s->secure.staticContext.get())
1510 setSessionCallbacks(s->secure.staticContext.get());
1511 }
1512 }
1513
1514 void
1515 destruct_session_cache()
1516 {
1517 delete SslSessionCache;
1518 }
1519
1520 /// initializes shared memory segments used by MemStore
1521 class SharedSessionCacheRr: public Ipc::Mem::RegisteredRunner
1522 {
1523 public:
1524 /* RegisteredRunner API */
1525 SharedSessionCacheRr(): owner(NULL) {}
1526 virtual void useConfig();
1527 virtual ~SharedSessionCacheRr();
1528
1529 protected:
1530 virtual void create();
1531
1532 private:
1533 Ipc::MemMap::Owner *owner;
1534 };
1535
1536 RunnerRegistrationEntry(SharedSessionCacheRr);
1537
1538 void
1539 SharedSessionCacheRr::useConfig()
1540 {
1541 Ipc::Mem::RegisteredRunner::useConfig();
1542 }
1543
1544 void
1545 SharedSessionCacheRr::create()
1546 {
1547 if (!isSslServer()) //no need to configure ssl session cache.
1548 return;
1549
1550 int items;
1551 items = Config.SSL.sessionCacheSize / sizeof(Ipc::MemMap::Slot);
1552 if (items)
1553 owner = Ipc::MemMap::Init(SslSessionCacheName, items);
1554 }
1555
1556 SharedSessionCacheRr::~SharedSessionCacheRr()
1557 {
1558 delete owner;
1559 }
1560
1561 #endif /* USE_OPENSSL */
1562