support.cc
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1996-2019 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 "anyp/Uri.h"
21 #include "fatal.h"
22 #include "fd.h"
23 #include "fde.h"
24 #include "globals.h"
25 #include "ipc/MemMap.h"
26 #include "security/CertError.h"
27 #include "security/Session.h"
28 #include "SquidConfig.h"
29 #include "SquidTime.h"
30 #include "ssl/bio.h"
31 #include "ssl/Config.h"
32 #include "ssl/ErrorDetail.h"
33 #include "ssl/gadgets.h"
34 #include "ssl/support.h"
35 
36 #include <cerrno>
37 
38 // TODO: Move ssl_ex_index_* global variables from global.cc here.
40 
42 
43 const EVP_MD *Ssl::DefaultSignHash = NULL;
44 
45 std::vector<const char *> Ssl::BumpModeStr = {
46  "none",
47  "client-first",
48  "server-first",
49  "peek",
50  "stare",
51  "bump",
52  "splice",
53  "terminate"
54  /*,"err"*/
55 };
56 
62 int
63 Ssl::AskPasswordCb(char *buf, int size, int rwflag, void *userdata)
64 {
65  FILE *in;
66  int len = 0;
67  char cmdline[1024];
68 
69  snprintf(cmdline, sizeof(cmdline), "\"%s\" \"%s\"", ::Config.Program.ssl_password, (const char *)userdata);
70  in = popen(cmdline, "r");
71 
72  if (fgets(buf, size, in))
73 
74  len = strlen(buf);
75 
76  while (len > 0 && (buf[len - 1] == '\n' || buf[len - 1] == '\r'))
77  --len;
78 
79  buf[len] = '\0';
80 
81  pclose(in);
82 
83  return len;
84 }
85 
87 static void
88 ssl_ask_password(SSL_CTX * context, const char * prompt)
89 {
91  SSL_CTX_set_default_passwd_cb(context, Ssl::AskPasswordCb);
92  SSL_CTX_set_default_passwd_cb_userdata(context, (void *)prompt);
93  }
94 }
95 
96 #if HAVE_LIBSSL_SSL_CTX_SET_TMP_RSA_CALLBACK
97 static RSA *
98 ssl_temp_rsa_cb(SSL * ssl, int anInt, int keylen)
99 {
100  static RSA *rsa_512 = nullptr;
101  static RSA *rsa_1024 = nullptr;
102  static BIGNUM *e = nullptr;
103  RSA *rsa = nullptr;
104  int newkey = 0;
105 
106  if (!e) {
107  e = BN_new();
108  if (!e || !BN_set_word(e, RSA_F4)) {
109  debugs(83, DBG_IMPORTANT, "ssl_temp_rsa_cb: Failed to set exponent for key " << keylen);
110  BN_free(e);
111  e = nullptr;
112  return nullptr;
113  }
114  }
115 
116  switch (keylen) {
117 
118  case 512:
119 
120  if (!rsa_512) {
121  rsa_512 = RSA_new();
122  if (rsa_512 && RSA_generate_key_ex(rsa_512, 512, e, nullptr)) {
123  newkey = 1;
124  } else {
125  RSA_free(rsa_512);
126  rsa_512 = nullptr;
127  }
128  }
129 
130  rsa = rsa_512;
131  break;
132 
133  case 1024:
134 
135  if (!rsa_1024) {
136  rsa_1024 = RSA_new();
137  if (rsa_1024 && RSA_generate_key_ex(rsa_1024, 1024, e, nullptr)) {
138  newkey = 1;
139  } else {
140  RSA_free(rsa_1024);
141  rsa_1024 = nullptr;
142  }
143  }
144 
145  rsa = rsa_1024;
146  break;
147 
148  default:
149  debugs(83, DBG_IMPORTANT, "ssl_temp_rsa_cb: Unexpected key length " << keylen);
150  return NULL;
151  }
152 
153  if (rsa == NULL) {
154  debugs(83, DBG_IMPORTANT, "ssl_temp_rsa_cb: Failed to generate key " << keylen);
155  return NULL;
156  }
157 
158  if (newkey) {
159  if (Debug::Enabled(83, 5))
160  PEM_write_RSAPrivateKey(debug_log, rsa, NULL, NULL, 0, NULL, NULL);
161 
162  debugs(83, DBG_IMPORTANT, "Generated ephemeral RSA key of length " << keylen);
163  }
164 
165  return rsa;
166 }
167 #endif
168 
169 void
171 {
172 #if HAVE_LIBSSL_SSL_CTX_SET_TMP_RSA_CALLBACK
173  debugs(83, 9, "Setting RSA key generation callback.");
174  SSL_CTX_set_tmp_rsa_callback(ctx.get(), ssl_temp_rsa_cb);
175 #endif
176 }
177 
178 int Ssl::asn1timeToString(ASN1_TIME *tm, char *buf, int len)
179 {
180  BIO *bio;
181  int write = 0;
182  bio = BIO_new(BIO_s_mem());
183  if (bio) {
184  if (ASN1_TIME_print(bio, tm))
185  write = BIO_read(bio, buf, len-1);
186  BIO_free(bio);
187  }
188  buf[write]='\0';
189  return write;
190 }
191 
192 int Ssl::matchX509CommonNames(X509 *peer_cert, void *check_data, int (*check_func)(void *check_data, ASN1_STRING *cn_data))
193 {
194  assert(peer_cert);
195 
196  X509_NAME *name = X509_get_subject_name(peer_cert);
197 
198  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)) {
199 
200  ASN1_STRING *cn_data = X509_NAME_ENTRY_get_data(X509_NAME_get_entry(name, i));
201 
202  if ( (*check_func)(check_data, cn_data) == 0)
203  return 1;
204  }
205 
206  STACK_OF(GENERAL_NAME) * altnames;
207  altnames = (STACK_OF(GENERAL_NAME)*)X509_get_ext_d2i(peer_cert, NID_subject_alt_name, NULL, NULL);
208 
209  if (altnames) {
210  int numalts = sk_GENERAL_NAME_num(altnames);
211  for (int i = 0; i < numalts; ++i) {
212  const GENERAL_NAME *check = sk_GENERAL_NAME_value(altnames, i);
213  if (check->type != GEN_DNS) {
214  continue;
215  }
216  ASN1_STRING *cn_data = check->d.dNSName;
217 
218  if ( (*check_func)(check_data, cn_data) == 0) {
219  sk_GENERAL_NAME_pop_free(altnames, GENERAL_NAME_free);
220  return 1;
221  }
222  }
223  sk_GENERAL_NAME_pop_free(altnames, GENERAL_NAME_free);
224  }
225  return 0;
226 }
227 
228 static int check_domain( void *check_data, ASN1_STRING *cn_data)
229 {
230  char cn[1024];
231  const char *server = (const char *)check_data;
232 
233  if (cn_data->length == 0)
234  return 1; // zero length cn, ignore
235 
236  if (cn_data->length > (int)sizeof(cn) - 1)
237  return 1; //if does not fit our buffer just ignore
238 
239  char *s = reinterpret_cast<char*>(cn_data->data);
240  char *d = cn;
241  for (int i = 0; i < cn_data->length; ++i, ++d, ++s) {
242  if (*s == '\0')
243  return 1; // always a domain mismatch. contains 0x00
244  *d = *s;
245  }
246  cn[cn_data->length] = '\0';
247  debugs(83, 4, "Verifying server domain " << server << " to certificate name/subjectAltName " << cn);
248  return matchDomainName(server, (cn[0] == '*' ? cn + 1 : cn), mdnRejectSubsubDomains);
249 }
250 
251 bool Ssl::checkX509ServerValidity(X509 *cert, const char *server)
252 {
253  return matchX509CommonNames(cert, (void *)server, check_domain);
254 }
255 
257 static int
258 ssl_verify_cb(int ok, X509_STORE_CTX * ctx)
259 {
260  // preserve original ctx->error before SSL_ calls can overwrite it
261  Security::ErrorCode error_no = ok ? SSL_ERROR_NONE : X509_STORE_CTX_get_error(ctx);
262 
263  char buffer[256] = "";
264  SSL *ssl = (SSL *)X509_STORE_CTX_get_ex_data(ctx, SSL_get_ex_data_X509_STORE_CTX_idx());
265  SSL_CTX *sslctx = SSL_get_SSL_CTX(ssl);
266  SBuf *server = (SBuf *)SSL_get_ex_data(ssl, ssl_ex_index_server);
267  void *dont_verify_domain = SSL_CTX_get_ex_data(sslctx, ssl_ctx_ex_index_dont_verify_domain);
268  ACLChecklist *check = (ACLChecklist*)SSL_get_ex_data(ssl, ssl_ex_index_cert_error_check);
269  X509 *peeked_cert = (X509 *)SSL_get_ex_data(ssl, ssl_ex_index_ssl_peeked_cert);
270  Security::CertPointer peer_cert;
271  peer_cert.resetAndLock(X509_STORE_CTX_get0_cert(ctx));
272 
273  X509_NAME_oneline(X509_get_subject_name(peer_cert.get()), buffer, sizeof(buffer));
274 
275  // detect infinite loops
276  uint32_t *validationCounter = static_cast<uint32_t *>(SSL_get_ex_data(ssl, ssl_ex_index_ssl_validation_counter));
277  if (!validationCounter) {
278  validationCounter = new uint32_t(1);
279  SSL_set_ex_data(ssl, ssl_ex_index_ssl_validation_counter, validationCounter);
280  } else {
281  // overflows allowed if SQUID_CERT_VALIDATION_ITERATION_MAX >= UINT32_MAX
282  (*validationCounter)++;
283  }
284 
285  if ((*validationCounter) >= SQUID_CERT_VALIDATION_ITERATION_MAX) {
286  ok = 0; // or the validation loop will never stop
288  debugs(83, 2, "SQUID_X509_V_ERR_INFINITE_VALIDATION: " <<
289  *validationCounter << " iterations while checking " << buffer);
290  }
291 
292  if (ok) {
293  debugs(83, 5, "SSL Certificate signature OK: " << buffer);
294 
295  // Check for domain mismatch only if the current certificate is the peer certificate.
296  if (!dont_verify_domain && server && peer_cert.get() == X509_STORE_CTX_get_current_cert(ctx)) {
297  if (!Ssl::checkX509ServerValidity(peer_cert.get(), server->c_str())) {
298  debugs(83, 2, "SQUID_X509_V_ERR_DOMAIN_MISMATCH: Certificate " << buffer << " does not match domainname " << server);
299  ok = 0;
301  }
302  }
303  }
304 
305  if (ok && peeked_cert) {
306  // Check whether the already peeked certificate matches the new one.
307  if (X509_cmp(peer_cert.get(), peeked_cert) != 0) {
308  debugs(83, 2, "SQUID_X509_V_ERR_CERT_CHANGE: Certificate " << buffer << " does not match peeked certificate");
309  ok = 0;
310  error_no = SQUID_X509_V_ERR_CERT_CHANGE;
311  }
312  }
313 
314  if (!ok) {
315  Security::CertPointer broken_cert;
316  broken_cert.resetAndLock(X509_STORE_CTX_get_current_cert(ctx));
317  if (!broken_cert)
318  broken_cert = peer_cert;
319 
320  Security::CertErrors *errs = static_cast<Security::CertErrors *>(SSL_get_ex_data(ssl, ssl_ex_index_ssl_errors));
321  const int depth = X509_STORE_CTX_get_error_depth(ctx);
322  if (!errs) {
323  errs = new Security::CertErrors(Security::CertError(error_no, broken_cert, depth));
324  if (!SSL_set_ex_data(ssl, ssl_ex_index_ssl_errors, (void *)errs)) {
325  debugs(83, 2, "Failed to set ssl error_no in ssl_verify_cb: Certificate " << buffer);
326  delete errs;
327  errs = NULL;
328  }
329  } else // remember another error number
330  errs->push_back_unique(Security::CertError(error_no, broken_cert, depth));
331 
332  if (const char *err_descr = Ssl::GetErrorDescr(error_no))
333  debugs(83, 5, err_descr << ": " << buffer);
334  else
335  debugs(83, DBG_IMPORTANT, "SSL unknown certificate error " << error_no << " in " << buffer);
336 
337  // Check if the certificate error can be bypassed.
338  // Infinity validation loop errors can not bypassed.
339  if (error_no != SQUID_X509_V_ERR_INFINITE_VALIDATION) {
340  if (check) {
341  ACLFilledChecklist *filledCheck = Filled(check);
342  assert(!filledCheck->sslErrors);
343  filledCheck->sslErrors = new Security::CertErrors(Security::CertError(error_no, broken_cert));
344  filledCheck->serverCert = peer_cert;
345  if (check->fastCheck().allowed()) {
346  debugs(83, 3, "bypassing SSL error " << error_no << " in " << buffer);
347  ok = 1;
348  } else {
349  debugs(83, 5, "confirming SSL error " << error_no);
350  }
351  delete filledCheck->sslErrors;
352  filledCheck->sslErrors = NULL;
353  filledCheck->serverCert.reset();
354  }
355  // If the certificate validator is used then we need to allow all errors and
356  // pass them to certficate validator for more processing
357  else if (Ssl::TheConfig.ssl_crt_validator) {
358  ok = 1;
359  }
360  }
361  }
362 
363  if (Ssl::TheConfig.ssl_crt_validator) {
364  // Check if we have stored certificates chain. Store if not.
365  if (!SSL_get_ex_data(ssl, ssl_ex_index_ssl_cert_chain)) {
366  STACK_OF(X509) *certStack = X509_STORE_CTX_get1_chain(ctx);
367  if (certStack && !SSL_set_ex_data(ssl, ssl_ex_index_ssl_cert_chain, certStack))
368  sk_X509_pop_free(certStack, X509_free);
369  }
370  }
371 
372  if (!ok && !SSL_get_ex_data(ssl, ssl_ex_index_ssl_error_detail) ) {
373 
374  // Find the broken certificate. It may be intermediate.
375  Security::CertPointer broken_cert(peer_cert); // reasonable default if search fails
376  // Our SQUID_X509_V_ERR_DOMAIN_MISMATCH implies peer_cert is at fault.
377  if (error_no != SQUID_X509_V_ERR_DOMAIN_MISMATCH) {
378  if (auto *last_used_cert = X509_STORE_CTX_get_current_cert(ctx))
379  broken_cert.resetAndLock(last_used_cert);
380  }
381 
382  auto *errDetail = new Ssl::ErrorDetail(error_no, peer_cert.get(), broken_cert.get());
383  if (!SSL_set_ex_data(ssl, ssl_ex_index_ssl_error_detail, errDetail)) {
384  debugs(83, 2, "Failed to set Ssl::ErrorDetail in ssl_verify_cb: Certificate " << buffer);
385  delete errDetail;
386  }
387  }
388 
389  return ok;
390 }
391 
392 void
394 {
395  SSL_CTX_set_verify(ctx.get(), SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT, ssl_verify_cb);
396 }
397 
398 // "dup" function for SSL_get_ex_new_index("cert_err_check")
399 #if SQUID_USE_CONST_CRYPTO_EX_DATA_DUP
400 static int
401 ssl_dupAclChecklist(CRYPTO_EX_DATA *, const CRYPTO_EX_DATA *, void *,
402  int, long, void *)
403 #else
404 static int
405 ssl_dupAclChecklist(CRYPTO_EX_DATA *, CRYPTO_EX_DATA *, void *,
406  int, long, void *)
407 #endif
408 {
409  // We do not support duplication of ACLCheckLists.
410  // If duplication is needed, we can count copies with cbdata.
411  assert(false);
412  return 0;
413 }
414 
415 // "free" function for SSL_get_ex_new_index("cert_err_check")
416 static void
417 ssl_freeAclChecklist(void *, void *ptr, CRYPTO_EX_DATA *,
418  int, long, void *)
419 {
420  delete static_cast<ACLChecklist *>(ptr); // may be NULL
421 }
422 
423 // "free" function for SSL_get_ex_new_index("ssl_error_detail")
424 static void
425 ssl_free_ErrorDetail(void *, void *ptr, CRYPTO_EX_DATA *,
426  int, long, void *)
427 {
428  Ssl::ErrorDetail *errDetail = static_cast <Ssl::ErrorDetail *>(ptr);
429  delete errDetail;
430 }
431 
432 static void
433 ssl_free_SslErrors(void *, void *ptr, CRYPTO_EX_DATA *,
434  int, long, void *)
435 {
436  Security::CertErrors *errs = static_cast <Security::CertErrors*>(ptr);
437  delete errs;
438 }
439 
440 // "free" function for SSL_get_ex_new_index("ssl_ex_index_ssl_validation_counter")
441 static void
442 ssl_free_int(void *, void *ptr, CRYPTO_EX_DATA *,
443  int, long, void *)
444 {
445  uint32_t *counter = static_cast <uint32_t *>(ptr);
446  delete counter;
447 }
448 
452 static void
453 ssl_free_CertChain(void *, void *ptr, CRYPTO_EX_DATA *,
454  int, long, void *)
455 {
456  STACK_OF(X509) *certsChain = static_cast <STACK_OF(X509) *>(ptr);
457  sk_X509_pop_free(certsChain,X509_free);
458 }
459 
460 // "free" function for X509 certificates
461 static void
462 ssl_free_X509(void *, void *ptr, CRYPTO_EX_DATA *,
463  int, long, void *)
464 {
465  X509 *cert = static_cast <X509 *>(ptr);
466  X509_free(cert);
467 }
468 
469 // "free" function for SBuf
470 static void
471 ssl_free_SBuf(void *, void *ptr, CRYPTO_EX_DATA *,
472  int, long, void *)
473 {
474  SBuf *buf = static_cast <SBuf *>(ptr);
475  delete buf;
476 }
477 
478 void
480 {
481  static bool initialized = false;
482  if (initialized)
483  return;
484  initialized = true;
485 
487 
488 #if !defined(OPENSSL_NO_ENGINE)
489  if (::Config.SSL.ssl_engine) {
490  ENGINE_load_builtin_engines();
491  ENGINE *e;
492  if (!(e = ENGINE_by_id(::Config.SSL.ssl_engine)))
493  fatalf("Unable to find SSL engine '%s'\n", ::Config.SSL.ssl_engine);
494 
495  if (!ENGINE_set_default(e, ENGINE_METHOD_ALL)) {
496  const int ssl_error = ERR_get_error();
497  fatalf("Failed to initialise SSL engine: %s\n", Security::ErrorString(ssl_error));
498  }
499  }
500 #else
501  if (::Config.SSL.ssl_engine)
502  fatalf("Your OpenSSL has no SSL engine support\n");
503 #endif
504 
505  const char *defName = ::Config.SSL.certSignHash ? ::Config.SSL.certSignHash : SQUID_SSL_SIGN_HASH_IF_NONE;
506  Ssl::DefaultSignHash = EVP_get_digestbyname(defName);
507  if (!Ssl::DefaultSignHash)
508  fatalf("Sign hash '%s' is not supported\n", defName);
509 
510  ssl_ex_index_server = SSL_get_ex_new_index(0, (void *) "server", NULL, NULL, ssl_free_SBuf);
511  ssl_ctx_ex_index_dont_verify_domain = SSL_CTX_get_ex_new_index(0, (void *) "dont_verify_domain", NULL, NULL, NULL);
512  ssl_ex_index_cert_error_check = SSL_get_ex_new_index(0, (void *) "cert_error_check", NULL, &ssl_dupAclChecklist, &ssl_freeAclChecklist);
513  ssl_ex_index_ssl_error_detail = SSL_get_ex_new_index(0, (void *) "ssl_error_detail", NULL, NULL, &ssl_free_ErrorDetail);
514  ssl_ex_index_ssl_peeked_cert = SSL_get_ex_new_index(0, (void *) "ssl_peeked_cert", NULL, NULL, &ssl_free_X509);
515  ssl_ex_index_ssl_errors = SSL_get_ex_new_index(0, (void *) "ssl_errors", NULL, NULL, &ssl_free_SslErrors);
516  ssl_ex_index_ssl_cert_chain = SSL_get_ex_new_index(0, (void *) "ssl_cert_chain", NULL, NULL, &ssl_free_CertChain);
517  ssl_ex_index_ssl_validation_counter = SSL_get_ex_new_index(0, (void *) "ssl_validation_counter", NULL, NULL, &ssl_free_int);
518  ssl_ex_index_ssl_untrusted_chain = SSL_get_ex_new_index(0, (void *) "ssl_untrusted_chain", NULL, NULL, &ssl_free_CertChain);
519 }
520 
521 bool
523 {
524  if (!ctx)
525  return false;
526 
527  return true;
528 }
529 
530 bool
532 {
533  if (!ctx)
534  return false;
535 
536  if (!peer.sslCipher.isEmpty()) {
537  debugs(83, 5, "Using chiper suite " << peer.sslCipher << ".");
538 
539  const char *cipher = peer.sslCipher.c_str();
540  if (!SSL_CTX_set_cipher_list(ctx.get(), cipher)) {
541  const int ssl_error = ERR_get_error();
542  fatalf("Failed to set SSL cipher suite '%s': %s\n",
543  cipher, Security::ErrorString(ssl_error));
544  }
545  }
546 
547  if (!peer.certs.empty()) {
548  // TODO: support loading multiple cert/key pairs
549  auto &keys = peer.certs.front();
550  if (!keys.certFile.isEmpty()) {
551  debugs(83, DBG_IMPORTANT, "Using certificate in " << keys.certFile);
552 
553  const char *certfile = keys.certFile.c_str();
554  if (!SSL_CTX_use_certificate_chain_file(ctx.get(), certfile)) {
555  const int ssl_error = ERR_get_error();
556  fatalf("Failed to acquire SSL certificate '%s': %s\n",
557  certfile, Security::ErrorString(ssl_error));
558  }
559 
560  debugs(83, DBG_IMPORTANT, "Using private key in " << keys.privateKeyFile);
561  const char *keyfile = keys.privateKeyFile.c_str();
562  ssl_ask_password(ctx.get(), keyfile);
563 
564  if (!SSL_CTX_use_PrivateKey_file(ctx.get(), keyfile, SSL_FILETYPE_PEM)) {
565  const int ssl_error = ERR_get_error();
566  fatalf("Failed to acquire SSL private key '%s': %s\n",
567  keyfile, Security::ErrorString(ssl_error));
568  }
569 
570  debugs(83, 5, "Comparing private and public SSL keys.");
571 
572  if (!SSL_CTX_check_private_key(ctx.get())) {
573  const int ssl_error = ERR_get_error();
574  fatalf("SSL private key '%s' does not match public key '%s': %s\n",
575  certfile, keyfile, Security::ErrorString(ssl_error));
576  }
577  }
578  }
579 
581 
582  if (fl & SSL_FLAG_DONT_VERIFY_PEER) {
583  debugs(83, 2, "SECURITY WARNING: Peer certificates are not verified for validity!");
584  SSL_CTX_set_verify(ctx.get(), SSL_VERIFY_NONE, NULL);
585  } else {
586  debugs(83, 9, "Setting certificate verification callback.");
588  }
589 
590  return true;
591 }
592 
594 static const char *
595 ssl_get_attribute(X509_NAME * name, const char *attribute_name)
596 {
597  static char buffer[1024];
598  buffer[0] = '\0';
599 
600  if (strcmp(attribute_name, "DN") == 0) {
601  X509_NAME_oneline(name, buffer, sizeof(buffer));
602  } else {
603  int nid = OBJ_txt2nid(const_cast<char *>(attribute_name));
604  if (nid == 0) {
605  debugs(83, DBG_IMPORTANT, "WARNING: Unknown SSL attribute name '" << attribute_name << "'");
606  return nullptr;
607  }
608  X509_NAME_get_text_by_NID(name, nid, buffer, sizeof(buffer));
609  }
610 
611  return *buffer ? buffer : nullptr;
612 }
613 
615 const char *
616 Ssl::GetX509UserAttribute(X509 * cert, const char *attribute_name)
617 {
618  X509_NAME *name;
619  const char *ret;
620 
621  if (!cert)
622  return NULL;
623 
624  name = X509_get_subject_name(cert);
625 
626  ret = ssl_get_attribute(name, attribute_name);
627 
628  return ret;
629 }
630 
631 const char *
632 Ssl::GetX509Fingerprint(X509 * cert, const char *)
633 {
634  static char buf[1024];
635  if (!cert)
636  return NULL;
637 
638  unsigned int n;
639  unsigned char md[EVP_MAX_MD_SIZE];
640  if (!X509_digest(cert, EVP_sha1(), md, &n))
641  return NULL;
642 
643  assert(3 * n + 1 < sizeof(buf));
644 
645  char *s = buf;
646  for (unsigned int i=0; i < n; ++i, s += 3) {
647  const char term = (i + 1 < n) ? ':' : '\0';
648  snprintf(s, 4, "%02X%c", md[i], term);
649  }
650 
651  return buf;
652 }
653 
654 SBuf
655 Ssl::GetX509PEM(X509 * cert)
656 {
657  assert(cert);
658 
659  Ssl::BIO_Pointer bio(BIO_new(BIO_s_mem()));
660  PEM_write_bio_X509(bio.get(), cert);
661 
662  char *ptr;
663  const auto len = BIO_get_mem_data(bio.get(), &ptr);
664  return SBuf(ptr, len);
665 }
666 
668 const char *
669 Ssl::GetX509CAAttribute(X509 * cert, const char *attribute_name)
670 {
671 
672  X509_NAME *name;
673  const char *ret;
674 
675  if (!cert)
676  return NULL;
677 
678  name = X509_get_issuer_name(cert);
679 
680  ret = ssl_get_attribute(name, attribute_name);
681 
682  return ret;
683 }
684 
685 const char *sslGetUserAttribute(SSL *ssl, const char *attribute_name)
686 {
687  if (!ssl)
688  return NULL;
689 
690  X509 *cert = SSL_get_peer_certificate(ssl);
691 
692  const char *attr = Ssl::GetX509UserAttribute(cert, attribute_name);
693 
694  X509_free(cert);
695  return attr;
696 }
697 
698 const char *sslGetCAAttribute(SSL *ssl, const char *attribute_name)
699 {
700  if (!ssl)
701  return NULL;
702 
703  X509 *cert = SSL_get_peer_certificate(ssl);
704 
705  const char *attr = Ssl::GetX509CAAttribute(cert, attribute_name);
706 
707  X509_free(cert);
708  return attr;
709 }
710 
711 const char *
712 sslGetUserEmail(SSL * ssl)
713 {
714  return sslGetUserAttribute(ssl, "emailAddress");
715 }
716 
717 SBuf
719 {
720  assert(ssl);
721 
722  if (const auto cert = SSL_get_peer_certificate(ssl))
723  return Ssl::GetX509PEM(cert);
724 
725  return SBuf();
726 }
727 
728 SBuf
730 {
731  assert(ssl);
732 
733  auto chain = SSL_get_peer_cert_chain(ssl);
734 
735  if (!chain)
736  return sslGetUserCertificatePEM(ssl);
737 
738  Ssl::BIO_Pointer bio(BIO_new(BIO_s_mem()));
739 
740  for (int i = 0; i < sk_X509_num(chain); ++i) {
741  X509 *cert = sk_X509_value(chain, i);
742  PEM_write_bio_X509(bio.get(), cert);
743  }
744 
745  char *ptr;
746  const auto len = BIO_get_mem_data(bio.get(), &ptr);
747  return SBuf(ptr, len);
748 }
749 
752 Ssl::createSSLContext(Security::CertPointer & x509, Security::PrivateKeyPointer & pkey, Security::ServerOptions &options)
753 {
755 
756  if (!SSL_CTX_use_certificate(ctx.get(), x509.get()))
757  return Security::ContextPointer();
758 
759  if (!SSL_CTX_use_PrivateKey(ctx.get(), pkey.get()))
760  return Security::ContextPointer();
761 
762  if (!options.updateContextConfig(ctx))
763  return Security::ContextPointer();
764 
765  return ctx;
766 }
767 
770 {
772  Security::PrivateKeyPointer pkey;
773  if (!readCertAndPrivateKeyFromMemory(cert, pkey, data) || !cert || !pkey)
774  return Security::ContextPointer();
775 
776  Security::ContextPointer ctx(createSSLContext(cert, pkey, options));
777  if (ctx && trusted)
779  return ctx;
780 }
781 
784 {
786  Security::PrivateKeyPointer pkey;
787  if (!generateSslCertificate(cert, pkey, properties) || !cert || !pkey)
788  return Security::ContextPointer();
789 
790  Security::ContextPointer ctx(createSSLContext(cert, pkey, options));
791  if (ctx && trusted)
793  return ctx;
794 }
795 
796 void
798 {
799  assert(ctx);
800  // Add signing certificate to the certificates chain
801  X509 *signingCert = options.signingCa.cert.get();
802  if (SSL_CTX_add_extra_chain_cert(ctx.get(), signingCert)) {
803  // increase the certificate lock
804  X509_up_ref(signingCert);
805  } else {
806  const int ssl_error = ERR_get_error();
807  debugs(33, DBG_IMPORTANT, "WARNING: can not add signing certificate to SSL context chain: " << Security::ErrorString(ssl_error));
808  }
809 
810  for (auto cert : options.signingCa.chain) {
811  if (SSL_CTX_add_extra_chain_cert(ctx.get(), cert.get())) {
812  // increase the certificate lock
813  X509_up_ref(cert.get());
814  } else {
815  const auto error = ERR_get_error();
816  debugs(83, DBG_IMPORTANT, "WARNING: can not add certificate to SSL dynamic context chain: " << Security::ErrorString(error));
817  }
818  }
819 }
820 
821 void
823 {
824  if (ctx && signAlgorithm == Ssl::algSignTrusted)
826 }
827 
828 bool
830 {
832  Security::PrivateKeyPointer pkey;
833  if (!generateSslCertificate(cert, pkey, properties))
834  return false;
835 
836  if (!cert)
837  return false;
838 
839  if (!pkey)
840  return false;
841 
842  if (!SSL_use_certificate(ssl, cert.get()))
843  return false;
844 
845  if (!SSL_use_PrivateKey(ssl, pkey.get()))
846  return false;
847 
848  return true;
849 }
850 
851 bool
853 {
855  Security::PrivateKeyPointer pkey;
856  if (!readCertAndPrivateKeyFromMemory(cert, pkey, data))
857  return false;
858 
859  if (!cert || !pkey)
860  return false;
861 
862  if (!SSL_use_certificate(ssl, cert.get()))
863  return false;
864 
865  if (!SSL_use_PrivateKey(ssl, pkey.get()))
866  return false;
867 
868  return true;
869 }
870 
871 bool
873 {
874 #if HAVE_SSL_CTX_GET0_CERTIFICATE
875  X509 * cert = SSL_CTX_get0_certificate(ctx.get());
876 #elif SQUID_USE_SSLGETCERTIFICATE_HACK
877  // SSL_get_certificate is buggy in openssl versions 1.0.1d and 1.0.1e
878  // Try to retrieve certificate directly from Security::ContextPointer object
879  X509 ***pCert = (X509 ***)ctx->cert;
880  X509 * cert = pCert && *pCert ? **pCert : NULL;
881 #elif SQUID_SSLGETCERTIFICATE_BUGGY
882  X509 * cert = NULL;
883  assert(0);
884 #else
885  // Temporary ssl for getting X509 certificate from SSL_CTX.
887  X509 * cert = SSL_get_certificate(ssl.get());
888 #endif
889  if (!cert)
890  return false;
891  const auto time_notBefore = X509_getm_notBefore(cert);
892  const auto time_notAfter = X509_getm_notAfter(cert);
893  return (X509_cmp_current_time(time_notBefore) < 0 && X509_cmp_current_time(time_notAfter) > 0);
894 }
895 
896 void
897 Ssl::setClientSNI(SSL *ssl, const char *fqdn)
898 {
899  const Ip::Address test(fqdn);
900  if (!test.isAnyAddr())
901  return; // raw IP is inappropriate for SNI
902 
903  //The SSL_CTRL_SET_TLSEXT_HOSTNAME is a openssl macro which indicates
904  // if the TLS servername extension (SNI) is enabled in openssl library.
905 #if defined(SSL_CTRL_SET_TLSEXT_HOSTNAME)
906  if (!SSL_set_tlsext_host_name(ssl, fqdn)) {
907  const int ssl_error = ERR_get_error();
908  debugs(83, 3, "WARNING: unable to set TLS servername extension (SNI): " <<
909  Security::ErrorString(ssl_error) << "\n");
910  }
911 #else
912  debugs(83, 7, "no support for TLS servername extension (SNI)");
913 #endif
914 }
915 
916 static const char *
918 {
919  AUTHORITY_INFO_ACCESS *info;
920  if (!cert)
921  return nullptr;
922  info = static_cast<AUTHORITY_INFO_ACCESS *>(X509_get_ext_d2i(cert, NID_info_access, NULL, NULL));
923  if (!info)
924  return nullptr;
925 
926  static char uri[MAX_URL];
927  uri[0] = '\0';
928 
929  for (int i = 0; i < sk_ACCESS_DESCRIPTION_num(info); i++) {
930  ACCESS_DESCRIPTION *ad = sk_ACCESS_DESCRIPTION_value(info, i);
931  if (OBJ_obj2nid(ad->method) == NID_ad_ca_issuers) {
932  if (ad->location->type == GEN_URI) {
933  xstrncpy(uri,
934  reinterpret_cast<const char *>(
935  ASN1_STRING_get0_data(ad->location->d.uniformResourceIdentifier)
936  ),
937  sizeof(uri));
938  }
939  break;
940  }
941  }
942  AUTHORITY_INFO_ACCESS_free(info);
943  return uri[0] != '\0' ? uri : nullptr;
944 }
945 
946 bool
947 Ssl::loadCerts(const char *certsFile, Ssl::CertsIndexedList &list)
948 {
949  BIO *in = BIO_new_file(certsFile, "r");
950  if (!in) {
951  debugs(83, DBG_IMPORTANT, "Failed to open '" << certsFile << "' to load certificates");
952  return false;
953  }
954 
955  X509 *aCert;
956  while((aCert = PEM_read_bio_X509(in, NULL, NULL, NULL))) {
957  static char buffer[2048];
958  X509_NAME_oneline(X509_get_subject_name(aCert), buffer, sizeof(buffer));
959  list.insert(std::pair<SBuf, X509 *>(SBuf(buffer), aCert));
960  }
961  debugs(83, 4, "Loaded " << list.size() << " certificates from file: '" << certsFile << "'");
962  BIO_free(in);
963  return true;
964 }
965 
968 static X509 *
970 {
971  static char buffer[2048];
972 
973  if (X509_NAME *issuerName = X509_get_issuer_name(cert))
974  X509_NAME_oneline(issuerName, buffer, sizeof(buffer));
975  else
976  return NULL;
977 
978  const auto ret = list.equal_range(SBuf(buffer));
979  for (Ssl::CertsIndexedList::iterator it = ret.first; it != ret.second; ++it) {
980  X509 *issuer = it->second;
981  if (X509_check_issued(issuer, cert) == X509_V_OK) {
982  return issuer;
983  }
984  }
985  return NULL;
986 }
987 
989 static bool
990 findCertIssuer(Security::CertList const &list, X509 *cert)
991 {
992  for (Security::CertList::const_iterator it = list.begin(); it != list.end(); ++it) {
993  if (X509_check_issued(it->get(), cert) == X509_V_OK)
994  return true;
995  }
996  return false;
997 }
998 
1000 static bool
1001 issuerExistInCaDb(X509 *cert, const Security::ContextPointer &connContext)
1002 {
1003  if (!connContext)
1004  return false;
1005 
1006  X509_STORE_CTX *storeCtx = X509_STORE_CTX_new();
1007  if (!storeCtx) {
1008  debugs(83, DBG_IMPORTANT, "Failed to allocate STORE_CTX object");
1009  return false;
1010  }
1011 
1012  bool gotIssuer = false;
1013  X509_STORE *store = SSL_CTX_get_cert_store(connContext.get());
1014  if (X509_STORE_CTX_init(storeCtx, store, nullptr, nullptr)) {
1015  X509 *issuer = nullptr;
1016  gotIssuer = (X509_STORE_CTX_get1_issuer(&issuer, storeCtx, cert) > 0);
1017  if (issuer)
1018  X509_free(issuer);
1019  } else {
1020  const int ssl_error = ERR_get_error();
1021  debugs(83, DBG_IMPORTANT, "Failed to initialize STORE_CTX object: " << Security::ErrorString(ssl_error));
1022  }
1023  X509_STORE_CTX_free(storeCtx);
1024 
1025  return gotIssuer;
1026 }
1027 
1028 const char *
1029 Ssl::uriOfIssuerIfMissing(X509 *cert, Security::CertList const &serverCertificates, const Security::ContextPointer &context)
1030 {
1031  if (!cert || !serverCertificates.size())
1032  return nullptr;
1033 
1034  if (!findCertIssuer(serverCertificates, cert)) {
1035  //if issuer is missing
1036  if (const char *issuerUri = hasAuthorityInfoAccessCaIssuers(cert)) {
1037  // There is a URI where we can download a certificate.
1038  if (!findCertIssuerFast(SquidUntrustedCerts, cert) &&
1039  !issuerExistInCaDb(cert, context)) {
1040  // and issuer not found in local databases containing
1041  // untrusted certificates and trusted CA certificates
1042  return issuerUri;
1043  }
1044  }
1045  }
1046  return nullptr;
1047 }
1048 
1049 void
1050 Ssl::missingChainCertificatesUrls(std::queue<SBuf> &URIs, Security::CertList const &serverCertificates, const Security::ContextPointer &context)
1051 {
1052  if (!serverCertificates.size())
1053  return;
1054 
1055  for (const auto &i : serverCertificates) {
1056  if (const char *issuerUri = uriOfIssuerIfMissing(i.get(), serverCertificates, context))
1057  URIs.push(SBuf(issuerUri));
1058  }
1059 }
1060 
1061 void
1062 Ssl::SSL_add_untrusted_cert(SSL *ssl, X509 *cert)
1063 {
1064  STACK_OF(X509) *untrustedStack = static_cast <STACK_OF(X509) *>(SSL_get_ex_data(ssl, ssl_ex_index_ssl_untrusted_chain));
1065  if (!untrustedStack) {
1066  untrustedStack = sk_X509_new_null();
1067  if (!SSL_set_ex_data(ssl, ssl_ex_index_ssl_untrusted_chain, untrustedStack)) {
1068  sk_X509_pop_free(untrustedStack, X509_free);
1069  throw TextException("Failed to attach untrusted certificates chain", Here());
1070  }
1071  }
1072  sk_X509_push(untrustedStack, cert);
1073 }
1074 
1076 static X509 *
1077 sk_x509_findIssuer(STACK_OF(X509) *sk, X509 *cert)
1078 {
1079  if (!sk)
1080  return NULL;
1081 
1082  const int skItemsNum = sk_X509_num(sk);
1083  for (int i = 0; i < skItemsNum; ++i) {
1084  X509 *issuer = sk_X509_value(sk, i);
1085  if (X509_check_issued(issuer, cert) == X509_V_OK)
1086  return issuer;
1087  }
1088  return NULL;
1089 }
1090 
1092 static void
1093 completeIssuers(X509_STORE_CTX *ctx, STACK_OF(X509) *untrustedCerts)
1094 {
1095  debugs(83, 2, "completing " << sk_X509_num(untrustedCerts) << " OpenSSL untrusted certs using " << SquidUntrustedCerts.size() << " configured untrusted certificates");
1096 
1097  const X509_VERIFY_PARAM *param = X509_STORE_CTX_get0_param(ctx);
1098  int depth = X509_VERIFY_PARAM_get_depth(param);
1099  X509 *current = X509_STORE_CTX_get0_cert(ctx);
1100  int i = 0;
1101  for (i = 0; current && (i < depth); ++i) {
1102  if (X509_check_issued(current, current) == X509_V_OK) {
1103  // either ctx->cert is itself self-signed or untrustedCerts
1104  // aready contain the self-signed current certificate
1105  break;
1106  }
1107 
1108  // untrustedCerts is short, not worth indexing
1109  X509 *issuer = sk_x509_findIssuer(untrustedCerts, current);
1110  if (!issuer) {
1111  if ((issuer = findCertIssuerFast(SquidUntrustedCerts, current)))
1112  sk_X509_push(untrustedCerts, issuer);
1113  }
1114  current = issuer;
1115  }
1116 
1117  if (i >= depth)
1118  debugs(83, 2, "exceeded the maximum certificate chain length: " << depth);
1119 }
1120 
1122 static int
1123 untrustedToStoreCtx_cb(X509_STORE_CTX *ctx,void *data)
1124 {
1125  debugs(83, 4, "Try to use pre-downloaded intermediate certificates");
1126 
1127  SSL *ssl = static_cast<SSL *>(X509_STORE_CTX_get_ex_data(ctx, SSL_get_ex_data_X509_STORE_CTX_idx()));
1128  STACK_OF(X509) *sslUntrustedStack = static_cast <STACK_OF(X509) *>(SSL_get_ex_data(ssl, ssl_ex_index_ssl_untrusted_chain));
1129 
1130  // OpenSSL already maintains ctx->untrusted but we cannot modify
1131  // internal OpenSSL list directly. We have to give OpenSSL our own
1132  // list, but it must include certificates on the OpenSSL ctx->untrusted
1133  STACK_OF(X509) *oldUntrusted = X509_STORE_CTX_get0_untrusted(ctx);
1134  STACK_OF(X509) *sk = sk_X509_dup(oldUntrusted); // oldUntrusted is always not NULL
1135 
1136  for (int i = 0; i < sk_X509_num(sslUntrustedStack); ++i) {
1137  X509 *cert = sk_X509_value(sslUntrustedStack, i);
1138  sk_X509_push(sk, cert);
1139  }
1140 
1141  // If the local untrusted certificates internal database is used
1142  // run completeIssuers to add missing certificates if possible.
1143  if (SquidUntrustedCerts.size() > 0)
1144  completeIssuers(ctx, sk);
1145 
1146  X509_STORE_CTX_set0_untrusted(ctx, sk); // No locking/unlocking, just sets ctx->untrusted
1147  int ret = X509_verify_cert(ctx);
1148  X509_STORE_CTX_set0_untrusted(ctx, oldUntrusted); // Set back the old untrusted list
1149  sk_X509_free(sk); // Release sk list
1150  return ret;
1151 }
1152 
1153 void
1154 Ssl::useSquidUntrusted(SSL_CTX *sslContext)
1155 {
1156  SSL_CTX_set_cert_verify_callback(sslContext, untrustedToStoreCtx_cb, NULL);
1157 }
1158 
1159 bool
1160 Ssl::loadSquidUntrusted(const char *path)
1161 {
1162  return Ssl::loadCerts(path, SquidUntrustedCerts);
1163 }
1164 
1165 void
1167 {
1168  if (SquidUntrustedCerts.size()) {
1169  for (Ssl::CertsIndexedList::iterator it = SquidUntrustedCerts.begin(); it != SquidUntrustedCerts.end(); ++it) {
1170  X509_free(it->second);
1171  }
1172  SquidUntrustedCerts.clear();
1173  }
1174 }
1175 
1176 bool Ssl::generateUntrustedCert(Security::CertPointer &untrustedCert, Security::PrivateKeyPointer &untrustedPkey, Security::CertPointer const &cert, Security::PrivateKeyPointer const & pkey)
1177 {
1178  // Generate the self-signed certificate, using a hard-coded subject prefix
1179  Ssl::CertificateProperties certProperties;
1180  if (const char *cn = CommonHostName(cert.get())) {
1181  certProperties.commonName = "Not trusted by \"";
1182  certProperties.commonName += cn;
1183  certProperties.commonName += "\"";
1184  } else if (const char *org = getOrganization(cert.get())) {
1185  certProperties.commonName = "Not trusted by \"";
1186  certProperties.commonName += org;
1187  certProperties.commonName += "\"";
1188  } else
1189  certProperties.commonName = "Not trusted";
1190  certProperties.setCommonName = true;
1191  // O, OU, and other CA subject fields will be mimicked
1192  // Expiration date and other common properties will be mimicked
1193  certProperties.signAlgorithm = Ssl::algSignSelf;
1194  certProperties.signWithPkey.resetAndLock(pkey.get());
1195  certProperties.mimicCert.resetAndLock(cert.get());
1196  return Ssl::generateSslCertificate(untrustedCert, untrustedPkey, certProperties);
1197 }
1198 
1200 {
1201  bool origSignatureAsKey = false;
1202  if (certProperties.mimicCert) {
1203  if (auto *sig = Ssl::X509_get_signature(certProperties.mimicCert)) {
1204  origSignatureAsKey = true;
1205  key.append((const char *)sig->data, sig->length);
1206  }
1207  }
1208 
1209  if (!origSignatureAsKey || certProperties.setCommonName) {
1210  // Use common name instead
1211  key.append(certProperties.commonName.c_str());
1212  }
1213  key.append(certProperties.setCommonName ? '1' : '0');
1214  key.append(certProperties.setValidAfter ? '1' : '0');
1215  key.append(certProperties.setValidBefore ? '1' : '0');
1216  key.append(certProperties.signAlgorithm != Ssl:: algSignEnd ? certSignAlgorithm(certProperties.signAlgorithm) : "-");
1217  key.append(certProperties.signHash ? EVP_MD_name(certProperties.signHash) : "-");
1218 
1219  if (certProperties.mimicCert) {
1220  Ssl::BIO_Pointer bio(BIO_new_SBuf(&key));
1221  ASN1_item_i2d_bio(ASN1_ITEM_rptr(X509), bio.get(), (ASN1_VALUE *)certProperties.mimicCert.get());
1222  }
1223 }
1224 
1225 static int
1227 {
1228  BIO_set_init(bio, 0);
1229  BIO_set_data(bio, NULL);
1230  return 1;
1231 }
1232 
1233 static int
1235 {
1236  if (!bio)
1237  return 0;
1238  return 1;
1239 }
1240 
1241 int
1242 bio_sbuf_write(BIO* bio, const char* data, int len)
1243 {
1244  SBuf *buf = static_cast<SBuf *>(BIO_get_data(bio));
1245  // TODO: Convert exceptions into BIO errors
1246  buf->append(data, len);
1247  return len;
1248 }
1249 
1250 int
1251 bio_sbuf_puts(BIO* bio, const char* data)
1252 {
1253  // TODO: use bio_sbuf_write() instead
1254  SBuf *buf = static_cast<SBuf *>(BIO_get_data(bio));
1255  size_t oldLen = buf->length();
1256  buf->append(data);
1257  return buf->length() - oldLen;
1258 }
1259 
1260 long
1261 bio_sbuf_ctrl(BIO* bio, int cmd, long num, void* ptr) {
1262  SBuf *buf = static_cast<SBuf *>(BIO_get_data(bio));
1263  switch (cmd) {
1264  case BIO_CTRL_RESET:
1265  // TODO: Convert exceptions into BIO errors
1266  buf->clear();
1267  return 1;
1268  case BIO_CTRL_FLUSH:
1269  return 1;
1270  default:
1271  return 0;
1272  }
1273 }
1274 
1276 {
1277 #if HAVE_LIBCRYPTO_BIO_METH_NEW
1278  static BIO_METHOD *BioSBufMethods = nullptr;
1279  if (!BioSBufMethods) {
1280  BioSBufMethods = BIO_meth_new(BIO_TYPE_MEM, "Squid-SBuf");
1281  BIO_meth_set_write(BioSBufMethods, bio_sbuf_write);
1282  BIO_meth_set_read(BioSBufMethods, nullptr);
1283  BIO_meth_set_puts(BioSBufMethods, bio_sbuf_puts);
1284  BIO_meth_set_gets(BioSBufMethods, nullptr);
1285  BIO_meth_set_ctrl(BioSBufMethods, bio_sbuf_ctrl);
1286  BIO_meth_set_create(BioSBufMethods, bio_sbuf_create);
1287  BIO_meth_set_destroy(BioSBufMethods, bio_sbuf_destroy);
1288  }
1289 #else
1290  static BIO_METHOD *BioSBufMethods = new BIO_METHOD({
1291  BIO_TYPE_MEM,
1292  "Squid SBuf",
1294  nullptr,
1295  bio_sbuf_puts,
1296  nullptr,
1297  bio_sbuf_ctrl,
1300  NULL
1301  });
1302 #endif
1303  BIO *bio = BIO_new(BioSBufMethods);
1304  Must(bio);
1305  BIO_set_data(bio, buf);
1306  BIO_set_init(bio, 1);
1307  return bio;
1308 }
1309 
1310 #endif /* USE_OPENSSL */
1311 
int X509_VERIFY_PARAM_get_depth(const X509_VERIFY_PARAM *param)
Definition: openssl.h:227
int ssl_ex_index_ssl_peeked_cert
size_type length() const
Returns the number of bytes stored in SBuf.
Definition: SBuf.h:404
bool setCommonName
Replace the CN field of the mimicing subject with the given.
Definition: gadgets.h:221
std::multimap< SBuf, X509 * > CertsIndexedList
certificates indexed by issuer name
Definition: support.h:155
#define debug_log
change-avoidance macro; new code should call DebugStream() instead
Definition: Debug.h:110
GETX509ATTRIBUTE GetX509UserAttribute
Definition: support.h:118
static bool findCertIssuer(Security::CertList const &list, X509 *cert)
slowly find the issuer certificate of a given cert using linear search
Definition: support.cc:990
long bio_sbuf_ctrl(BIO *bio, int cmd, long num, void *ptr)
Definition: support.cc:1261
bool setValidAfter
Do not mimic "Not Valid After" field.
Definition: gadgets.h:219
static bool issuerExistInCaDb(X509 *cert, const Security::ContextPointer &connContext)
Definition: support.cc:1001
#define assert(EX)
Definition: assert.h:17
static void ssl_free_SBuf(void *, void *ptr, CRYPTO_EX_DATA *, int, long, void *)
Definition: support.cc:471
bool setValidBefore
Do not mimic "Not Valid Before" field.
Definition: gadgets.h:220
bool verifySslCertificate(Security::ContextPointer &, CertificateProperties const &)
Definition: support.cc:872
int ssl_ex_index_server
Security::CertPointer cert
public X.509 certificate from certFile
Definition: KeyData.h:31
X509 * X509_STORE_CTX_get0_cert(X509_STORE_CTX *ctx)
Definition: openssl.h:188
Definition: SBuf.h:86
static const char * hasAuthorityInfoAccessCaIssuers(X509 *cert)
Definition: support.cc:917
void configureUnconfiguredSslContext(Security::ContextPointer &, Ssl::CertSignAlgorithm signAlgorithm, AnyP::PortCfg &)
Definition: support.cc:822
bool updateContextConfig(Security::ContextPointer &)
update the given TLS security context using squid.conf settings
const char * GetErrorDescr(Security::ErrorCode value)
A short description of the TLS error "value".
Definition: ErrorDetail.cc:416
ACLFilledChecklist * Filled(ACLChecklist *checklist)
convenience and safety wrapper for dynamic_cast<ACLFilledChecklist*>
int AskPasswordCb(char *buf, int size, int rwflag, void *userdata)
Definition: support.cc:63
void error(char *format,...)
int i
Definition: membanger.c:49
SBuf & append(const SBuf &S)
Definition: SBuf.cc:195
static X509 * findCertIssuerFast(Ssl::CertsIndexedList &list, X509 *cert)
Definition: support.cc:969
#define SQUID_X509_V_ERR_DOMAIN_MISMATCH
Definition: support.h:44
T * get() const
Returns raw and possibly nullptr pointer.
std::list< Security::CertPointer > CertList
Definition: forward.h:80
#define SQUID_X509_V_ERR_CERT_CHANGE
Definition: support.h:42
bool loadSquidUntrusted(const char *path)
Definition: support.cc:1160
bool loadCerts(const char *certsFile, Ssl::CertsIndexedList &list)
Definition: support.cc:947
int ssl_ex_index_cert_error_check
std::string commonName
A CN to use for the generated certificate.
Definition: gadgets.h:222
void clear()
Definition: SBuf.cc:178
#define Must(condition)
Like assert() but throws an exception instead of aborting the process.
Definition: TextException.h:69
bool isEmpty() const
Definition: SBuf.h:420
int ssl_ex_index_ssl_validation_counter
void BIO_set_init(BIO *table, int init)
Definition: openssl.h:72
const char * sslGetUserAttribute(SSL *ssl, const char *attribute_name)
Definition: support.cc:685
static void ssl_free_CertChain(void *, void *ptr, CRYPTO_EX_DATA *, int, long, void *)
Definition: support.cc:453
static void completeIssuers(X509_STORE_CTX *ctx, STACK_OF(X509) *untrustedCerts)
add missing issuer certificates to untrustedCerts
Definition: support.cc:1093
#define SSL_FLAG_DONT_VERIFY_PEER
Definition: forward.h:47
Security::CertList chain
any certificates which must be chained from cert
Definition: KeyData.h:35
Security::ServerOptions secure
TLS configuration options for this listening port.
Definition: PortCfg.h:65
TLS squid.conf settings for a remote server peer.
Definition: PeerOptions.h:22
Acl::Answer const & fastCheck()
Definition: Checklist.cc:336
Security::ContextPointer GenerateSslContextUsingPkeyAndCertFromMemory(const char *data, Security::ServerOptions &, bool trusted)
Definition: support.cc:769
#define X509_getm_notBefore
Definition: openssl.h:206
static int untrustedToStoreCtx_cb(X509_STORE_CTX *ctx, void *data)
OpenSSL certificate validation callback.
Definition: support.cc:1123
char * ssl_password
Definition: SquidConfig.h:204
#define X509_STORE_CTX_set0_untrusted
Definition: openssl.h:204
void chainCertificatesToSSLContext(Security::ContextPointer &, Security::ServerOptions &)
Definition: support.cc:797
void * BIO_get_data(BIO *table)
Definition: openssl.h:60
bool generateSslCertificate(Security::CertPointer &cert, Security::PrivateKeyPointer &pkey, CertificateProperties const &properties)
Definition: gadgets.cc:675
void fatalf(const char *fmt,...)
Definition: fatal.cc:68
static void ssl_ask_password(SSL_CTX *context, const char *prompt)
Definition: support.cc:88
const char * sslGetCAAttribute(SSL *ssl, const char *attribute_name)
Definition: support.cc:698
Security::ContextPointer GenerateSslContext(CertificateProperties const &, Security::ServerOptions &, bool trusted)
Definition: support.cc:783
void useSquidUntrusted(SSL_CTX *sslContext)
Definition: support.cc:1154
Security::PrivateKeyPointer signWithPkey
The key of the signing certificate.
Definition: gadgets.h:218
bool InitServerContext(Security::ContextPointer &, AnyP::PortCfg &)
initialize a TLS server context with OpenSSL specific settings
Definition: support.cc:522
void const char HLPCB void * data
Definition: stub_helper.cc:16
const unsigned char * ASN1_STRING_get0_data(const ASN1_STRING *x)
Definition: openssl.h:52
bool configureSSLUsingPkeyAndCertFromMemory(SSL *ssl, const char *data, AnyP::PortCfg &port)
Definition: support.cc:852
CbDataList< Security::CertError > CertErrors
Holds a list of X.509 certificate errors.
Definition: forward.h:57
int ssl_ctx_ex_index_dont_verify_domain
static void ssl_free_ErrorDetail(void *, void *ptr, CRYPTO_EX_DATA *, int, long, void *)
Definition: support.cc:425
#define debugs(SECTION, LEVEL, CONTENT)
Definition: Debug.h:124
#define DBG_IMPORTANT
Definition: Debug.h:46
static void ssl_freeAclChecklist(void *, void *ptr, CRYPTO_EX_DATA *, int, long, void *)
Definition: support.cc:417
int ssl_ex_index_ssl_error_detail
bool push_back_unique(C const &element)
Definition: CbDataList.h:87
Security::CertPointer mimicCert
Certificate to mimic.
Definition: gadgets.h:216
const char * certSignAlgorithm(int sg)
Definition: gadgets.h:163
bool allowed() const
Definition: Acl.h:143
const char * CommonHostName(X509 *x509)
Definition: gadgets.cc:903
GETX509PEM GetX509PEM
Definition: support.h:124
void missingChainCertificatesUrls(std::queue< SBuf > &URIs, Security::CertList const &serverCertificates, const Security::ContextPointer &context)
Definition: support.cc:1050
CertSignAlgorithm
Definition: gadgets.h:150
Security::KeyData signingCa
x509 certificate and key for signing generated certificates
Definition: ServerOptions.h:87
Security::CertPointer serverCert
The peer certificate.
static int check_domain(void *check_data, ASN1_STRING *cn_data)
Definition: support.cc:228
int ErrorCode
Squid defined error code (<0), an error code returned by X.509 API, or SSL_ERROR_NONE.
Definition: forward.h:91
static int ssl_verify_cb(int ok, X509_STORE_CTX *ctx)
Definition: support.cc:258
void reset()
Forget the raw pointer - unlock if any value was set. Become a nil pointer.
GETX509ATTRIBUTE GetX509CAAttribute
Definition: support.h:121
bool InitClientContext(Security::ContextPointer &, Security::PeerOptions &, long flags)
initialize a TLS client context with OpenSSL specific settings
Definition: support.cc:531
char * xstrncpy(char *dst, const char *src, size_t n)
Definition: xstring.cc:37
std::shared_ptr< SSL_CTX > ContextPointer
Definition: Context.h:29
Security::ContextPointer createSSLContext(Security::CertPointer &x509, Security::PrivateKeyPointer &pkey, Security::ServerOptions &)
Create SSL context and apply ssl certificate and private key to it.
Definition: support.cc:752
void MaybeSetupRsaCallback(Security::ContextPointer &)
if required, setup callback for generating ephemeral RSA keys
Definition: support.cc:170
STACK_OF(X509) *X509_STORE_CTX_get0_untrusted(X509_STORE_CTX *ctx)
Definition: openssl.h:195
void BIO_set_data(BIO *table, void *data)
Definition: openssl.h:66
static const char * ssl_get_attribute(X509_NAME *name, const char *attribute_name)
Definition: support.cc:595
static int ssl_dupAclChecklist(CRYPTO_EX_DATA *, CRYPTO_EX_DATA *, void *, int, long, void *)
Definition: support.cc:405
const char * c_str()
Definition: SBuf.cc:526
std::unique_ptr< BIO, HardFun< void, BIO *, &BIO_vfree > > BIO_Pointer
Definition: gadgets.h:50
void SSL_add_untrusted_cert(SSL *ssl, X509 *cert)
Definition: support.cc:1062
int ssl_ex_index_ssl_errors
static int port
Definition: ldap_backend.cc:69
bool isAnyAddr() const
Definition: Address.cc:170
static int bio_sbuf_create(BIO *bio)
Definition: support.cc:1226
int unsigned int const char *desc STUB void int len
Definition: stub_fd.cc:20
SBuf sslGetUserCertificatePEM(SSL *ssl)
Definition: support.cc:718
int bio_sbuf_write(BIO *bio, const char *data, int len)
Definition: support.cc:1242
void const char * buf
Definition: stub_helper.cc:16
const char * uriOfIssuerIfMissing(X509 *cert, Security::CertList const &serverCertificates, const Security::ContextPointer &context)
Definition: support.cc:1029
an std::runtime_error with thrower location info
Definition: TextException.h:19
static char * keys[]
Definition: WinSvc.cc:84
CertSignAlgorithm signAlgorithm
The signing algorithm to use.
Definition: gadgets.h:223
void Initialize()
Definition: support.cc:479
struct SquidConfig::@104 Program
bool configureSSL(SSL *ssl, CertificateProperties const &properties, AnyP::PortCfg &port)
Definition: support.cc:829
static Ssl::CertsIndexedList SquidUntrustedCerts
Definition: support.cc:41
bool checkX509ServerValidity(X509 *cert, const char *server)
Definition: support.cc:251
#define X509_getm_notAfter
Definition: openssl.h:205
#define SQUID_X509_V_ERR_INFINITE_VALIDATION
Definition: support.h:41
void SQUID_OPENSSL_init_ssl(void)
Definition: openssl.h:236
void InRamCertificateDbKey(const Ssl::CertificateProperties &certProperties, SBuf &key)
Definition: support.cc:1199
int ssl_ex_index_ssl_untrusted_chain
Definition: support.cc:39
static char server[MAXLINE]
void setClientSNI(SSL *ssl, const char *fqdn)
Definition: support.cc:897
#define SQUID_SSL_SIGN_HASH_IF_NONE
Definition: gadgets.h:39
int bio_sbuf_puts(BIO *bio, const char *data)
Definition: support.cc:1251
bool generateUntrustedCert(Security::CertPointer &untrustedCert, Security::PrivateKeyPointer &untrustedPkey, Security::CertPointer const &cert, Security::PrivateKeyPointer const &pkey)
Definition: support.cc:1176
static int bio_sbuf_destroy(BIO *bio)
Definition: support.cc:1234
static void ssl_free_int(void *, void *ptr, CRYPTO_EX_DATA *, int, long, void *)
Definition: support.cc:442
int asn1timeToString(ASN1_TIME *tm, char *buf, int len)
Definition: support.cc:178
const char * getOrganization(X509 *x509)
Definition: gadgets.cc:908
const Security::CertErrors * sslErrors
SSL [certificate validation] errors, in undefined order.
TLS squid.conf settings for a listening port.
Definition: ServerOptions.h:25
static X509 * sk_x509_findIssuer(STACK_OF(X509) *sk, X509 *cert)
Search for the issuer certificate of cert in sk list.
Definition: support.cc:1077
bool readCertAndPrivateKeyFromMemory(Security::CertPointer &cert, Security::PrivateKeyPointer &pkey, char const *bufferToRead)
Definition: gadgets.cc:116
static void ssl_free_SslErrors(void *, void *ptr, CRYPTO_EX_DATA *, int, long, void *)
Definition: support.cc:433
static void ssl_free_X509(void *, void *ptr, CRYPTO_EX_DATA *, int, long, void *)
Definition: support.cc:462
std::list< Security::KeyData > certs
details from the cert= and file= config parameters
Definition: PeerOptions.h:102
const EVP_MD * signHash
The signing hash to use.
Definition: gadgets.h:224
int matchX509CommonNames(X509 *peer_cert, void *check_data, int(*check_func)(void *check_data, ASN1_STRING *cn_data))
Definition: support.cc:192
#define MAX_URL
Definition: defines.h:118
const EVP_MD * DefaultSignHash
Definition: support.cc:43
int ssl_ex_index_ssl_cert_chain
Security::SessionPointer NewSessionObject(const Security::ContextPointer &)
Definition: Session.cc:97
GETX509ATTRIBUTE GetX509Fingerprint
Definition: support.h:127
static bool Enabled(const int section, const int level)
whether debugging the given section and the given level produces output
Definition: Debug.h:75
void SetupVerifyCallback(Security::ContextPointer &)
set the certificate verify callback for a context
Definition: support.cc:393
void unloadSquidUntrusted()
Definition: support.cc:1166
int matchDomainName(const char *h, const char *d, uint8_t flags)
Definition: Uri.cc:693
#define SQUID_CERT_VALIDATION_ITERATION_MAX
Definition: support.h:54
const char * sslGetUserEmail(SSL *ssl)
Definition: support.cc:712
BIO * BIO_new_SBuf(SBuf *buf)
Definition: support.cc:1275
Config TheConfig
Definition: Config.cc:12
#define Here()
source code location of the caller
Definition: Here.h:15
const char * ErrorString(const ErrorCode code)
Definition: forward.h:96
const ASN1_BIT_STRING * X509_get_signature(const Security::CertPointer &)
Definition: gadgets.cc:939
virtual Security::ContextPointer createBlankContext() const
generate an unset security context object
class SquidConfig Config
Definition: SquidConfig.cc:12
#define NULL
Definition: types.h:166
int size
Definition: ModDevPoll.cc:77
SBuf sslGetUserCertificateChainPEM(SSL *ssl)
Definition: support.cc:729
std::shared_ptr< SSL > SessionPointer
Definition: Session.h:44

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors