2 * Copyright (C) 1996-2018 The Squid Software Foundation and contributors
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.
9 /* DEBUG: section 83 SSL accelerator support */
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.
18 #include "acl/FilledChecklist.h"
19 #include "anyp/PortCfg.h"
24 #include "ipc/MemMap.h"
25 #include "security/CertError.h"
26 #include "security/Session.h"
27 #include "SquidConfig.h"
28 #include "SquidTime.h"
30 #include "ssl/Config.h"
31 #include "ssl/ErrorDetail.h"
32 #include "ssl/gadgets.h"
33 #include "ssl/support.h"
38 // TODO: Move ssl_ex_index_* global variables from global.cc here.
39 int ssl_ex_index_ssl_untrusted_chain
= -1;
41 static Ssl::CertsIndexedList SquidUntrustedCerts
;
43 const EVP_MD
*Ssl::DefaultSignHash
= NULL
;
45 std::vector
<const char *> Ssl::BumpModeStr
= {
58 \defgroup ServerProtocolSSLInternal Server-Side SSL Internals
59 \ingroup ServerProtocolSSLAPI
63 Ssl::AskPasswordCb(char *buf
, int size
, int rwflag
, void *userdata
)
69 snprintf(cmdline
, sizeof(cmdline
), "\"%s\" \"%s\"", ::Config
.Program
.ssl_password
, (const char *)userdata
);
70 in
= popen(cmdline
, "r");
72 if (fgets(buf
, size
, in
))
76 while (len
> 0 && (buf
[len
- 1] == '\n' || buf
[len
- 1] == '\r'))
86 /// \ingroup ServerProtocolSSLInternal
88 ssl_ask_password(SSL_CTX
* context
, const char * prompt
)
90 if (Config
.Program
.ssl_password
) {
91 SSL_CTX_set_default_passwd_cb(context
, Ssl::AskPasswordCb
);
92 SSL_CTX_set_default_passwd_cb_userdata(context
, (void *)prompt
);
96 #if HAVE_LIBSSL_SSL_CTX_SET_TMP_RSA_CALLBACK
98 ssl_temp_rsa_cb(SSL
* ssl
, int anInt
, int keylen
)
100 static RSA
*rsa_512
= NULL
;
101 static RSA
*rsa_1024
= NULL
;
110 rsa_512
= RSA_generate_key(512, RSA_F4
, NULL
, NULL
);
120 rsa_1024
= RSA_generate_key(1024, RSA_F4
, NULL
, NULL
);
128 debugs(83, DBG_IMPORTANT
, "ssl_temp_rsa_cb: Unexpected key length " << keylen
);
133 debugs(83, DBG_IMPORTANT
, "ssl_temp_rsa_cb: Failed to generate key " << keylen
);
138 if (Debug::Enabled(83, 5))
139 PEM_write_RSAPrivateKey(debug_log
, rsa
, NULL
, NULL
, 0, NULL
, NULL
);
141 debugs(83, DBG_IMPORTANT
, "Generated ephemeral RSA key of length " << keylen
);
149 Ssl::MaybeSetupRsaCallback(Security::ContextPointer
&ctx
)
151 #if HAVE_LIBSSL_SSL_CTX_SET_TMP_RSA_CALLBACK
152 debugs(83, 9, "Setting RSA key generation callback.");
153 SSL_CTX_set_tmp_rsa_callback(ctx
.get(), ssl_temp_rsa_cb
);
157 int Ssl::asn1timeToString(ASN1_TIME
*tm
, char *buf
, int len
)
161 bio
= BIO_new(BIO_s_mem());
163 if (ASN1_TIME_print(bio
, tm
))
164 write
= BIO_read(bio
, buf
, len
-1);
171 int Ssl::matchX509CommonNames(X509
*peer_cert
, void *check_data
, int (*check_func
)(void *check_data
, ASN1_STRING
*cn_data
))
175 X509_NAME
*name
= X509_get_subject_name(peer_cert
);
177 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
)) {
179 ASN1_STRING
*cn_data
= X509_NAME_ENTRY_get_data(X509_NAME_get_entry(name
, i
));
181 if ( (*check_func
)(check_data
, cn_data
) == 0)
185 STACK_OF(GENERAL_NAME
) * altnames
;
186 altnames
= (STACK_OF(GENERAL_NAME
)*)X509_get_ext_d2i(peer_cert
, NID_subject_alt_name
, NULL
, NULL
);
189 int numalts
= sk_GENERAL_NAME_num(altnames
);
190 for (int i
= 0; i
< numalts
; ++i
) {
191 const GENERAL_NAME
*check
= sk_GENERAL_NAME_value(altnames
, i
);
192 if (check
->type
!= GEN_DNS
) {
195 ASN1_STRING
*cn_data
= check
->d
.dNSName
;
197 if ( (*check_func
)(check_data
, cn_data
) == 0) {
198 sk_GENERAL_NAME_pop_free(altnames
, GENERAL_NAME_free
);
202 sk_GENERAL_NAME_pop_free(altnames
, GENERAL_NAME_free
);
207 static int check_domain( void *check_data
, ASN1_STRING
*cn_data
)
210 const char *server
= (const char *)check_data
;
212 if (cn_data
->length
== 0)
213 return 1; // zero length cn, ignore
215 if (cn_data
->length
> (int)sizeof(cn
) - 1)
216 return 1; //if does not fit our buffer just ignore
218 char *s
= reinterpret_cast<char*>(cn_data
->data
);
220 for (int i
= 0; i
< cn_data
->length
; ++i
, ++d
, ++s
) {
222 return 1; // always a domain mismatch. contains 0x00
225 cn
[cn_data
->length
] = '\0';
226 debugs(83, 4, "Verifying server domain " << server
<< " to certificate name/subjectAltName " << cn
);
227 return matchDomainName(server
, (cn
[0] == '*' ? cn
+ 1 : cn
), mdnRejectSubsubDomains
);
230 bool Ssl::checkX509ServerValidity(X509
*cert
, const char *server
)
232 return matchX509CommonNames(cert
, (void *)server
, check_domain
);
235 #if !HAVE_LIBCRYPTO_X509_STORE_CTX_GET0_CERT
236 static inline X509
*X509_STORE_CTX_get0_cert(X509_STORE_CTX
*ctx
)
242 /// \ingroup ServerProtocolSSLInternal
244 ssl_verify_cb(int ok
, X509_STORE_CTX
* ctx
)
246 // preserve original ctx->error before SSL_ calls can overwrite it
247 Security::ErrorCode error_no
= ok
? SSL_ERROR_NONE
: X509_STORE_CTX_get_error(ctx
);
249 char buffer
[256] = "";
250 SSL
*ssl
= (SSL
*)X509_STORE_CTX_get_ex_data(ctx
, SSL_get_ex_data_X509_STORE_CTX_idx());
251 SSL_CTX
*sslctx
= SSL_get_SSL_CTX(ssl
);
252 SBuf
*server
= (SBuf
*)SSL_get_ex_data(ssl
, ssl_ex_index_server
);
253 void *dont_verify_domain
= SSL_CTX_get_ex_data(sslctx
, ssl_ctx_ex_index_dont_verify_domain
);
254 ACLChecklist
*check
= (ACLChecklist
*)SSL_get_ex_data(ssl
, ssl_ex_index_cert_error_check
);
255 X509
*peeked_cert
= (X509
*)SSL_get_ex_data(ssl
, ssl_ex_index_ssl_peeked_cert
);
256 Security::CertPointer peer_cert
;
257 peer_cert
.resetAndLock(X509_STORE_CTX_get0_cert(ctx
));
259 X509_NAME_oneline(X509_get_subject_name(peer_cert
.get()), buffer
, sizeof(buffer
));
261 // detect infinite loops
262 uint32_t *validationCounter
= static_cast<uint32_t *>(SSL_get_ex_data(ssl
, ssl_ex_index_ssl_validation_counter
));
263 if (!validationCounter
) {
264 validationCounter
= new uint32_t(1);
265 SSL_set_ex_data(ssl
, ssl_ex_index_ssl_validation_counter
, validationCounter
);
267 // overflows allowed if SQUID_CERT_VALIDATION_ITERATION_MAX >= UINT32_MAX
268 (*validationCounter
)++;
271 if ((*validationCounter
) >= SQUID_CERT_VALIDATION_ITERATION_MAX
) {
272 ok
= 0; // or the validation loop will never stop
273 error_no
= SQUID_X509_V_ERR_INFINITE_VALIDATION
;
274 debugs(83, 2, "SQUID_X509_V_ERR_INFINITE_VALIDATION: " <<
275 *validationCounter
<< " iterations while checking " << buffer
);
279 debugs(83, 5, "SSL Certificate signature OK: " << buffer
);
281 // Check for domain mismatch only if the current certificate is the peer certificate.
282 if (!dont_verify_domain
&& server
&& peer_cert
.get() == X509_STORE_CTX_get_current_cert(ctx
)) {
283 if (!Ssl::checkX509ServerValidity(peer_cert
.get(), server
->c_str())) {
284 debugs(83, 2, "SQUID_X509_V_ERR_DOMAIN_MISMATCH: Certificate " << buffer
<< " does not match domainname " << server
);
286 error_no
= SQUID_X509_V_ERR_DOMAIN_MISMATCH
;
291 if (ok
&& peeked_cert
) {
292 // Check whether the already peeked certificate matches the new one.
293 if (X509_cmp(peer_cert
.get(), peeked_cert
) != 0) {
294 debugs(83, 2, "SQUID_X509_V_ERR_CERT_CHANGE: Certificate " << buffer
<< " does not match peeked certificate");
296 error_no
= SQUID_X509_V_ERR_CERT_CHANGE
;
301 Security::CertPointer broken_cert
;
302 broken_cert
.resetAndLock(X509_STORE_CTX_get_current_cert(ctx
));
304 broken_cert
= peer_cert
;
306 Security::CertErrors
*errs
= static_cast<Security::CertErrors
*>(SSL_get_ex_data(ssl
, ssl_ex_index_ssl_errors
));
307 const int depth
= X509_STORE_CTX_get_error_depth(ctx
);
309 errs
= new Security::CertErrors(Security::CertError(error_no
, broken_cert
, depth
));
310 if (!SSL_set_ex_data(ssl
, ssl_ex_index_ssl_errors
, (void *)errs
)) {
311 debugs(83, 2, "Failed to set ssl error_no in ssl_verify_cb: Certificate " << buffer
);
315 } else // remember another error number
316 errs
->push_back_unique(Security::CertError(error_no
, broken_cert
, depth
));
318 if (const char *err_descr
= Ssl::GetErrorDescr(error_no
))
319 debugs(83, 5, err_descr
<< ": " << buffer
);
321 debugs(83, DBG_IMPORTANT
, "SSL unknown certificate error " << error_no
<< " in " << buffer
);
323 // Check if the certificate error can be bypassed.
324 // Infinity validation loop errors can not bypassed.
325 if (error_no
!= SQUID_X509_V_ERR_INFINITE_VALIDATION
) {
327 ACLFilledChecklist
*filledCheck
= Filled(check
);
328 assert(!filledCheck
->sslErrors
);
329 filledCheck
->sslErrors
= new Security::CertErrors(Security::CertError(error_no
, broken_cert
));
330 filledCheck
->serverCert
= peer_cert
;
331 if (check
->fastCheck().allowed()) {
332 debugs(83, 3, "bypassing SSL error " << error_no
<< " in " << buffer
);
335 debugs(83, 5, "confirming SSL error " << error_no
);
337 delete filledCheck
->sslErrors
;
338 filledCheck
->sslErrors
= NULL
;
339 filledCheck
->serverCert
.reset();
341 // If the certificate validator is used then we need to allow all errors and
342 // pass them to certficate validator for more processing
343 else if (Ssl::TheConfig
.ssl_crt_validator
) {
349 if (Ssl::TheConfig
.ssl_crt_validator
) {
350 // Check if we have stored certificates chain. Store if not.
351 if (!SSL_get_ex_data(ssl
, ssl_ex_index_ssl_cert_chain
)) {
352 STACK_OF(X509
) *certStack
= X509_STORE_CTX_get1_chain(ctx
);
353 if (certStack
&& !SSL_set_ex_data(ssl
, ssl_ex_index_ssl_cert_chain
, certStack
))
354 sk_X509_pop_free(certStack
, X509_free
);
358 if (!ok
&& !SSL_get_ex_data(ssl
, ssl_ex_index_ssl_error_detail
) ) {
360 // Find the broken certificate. It may be intermediate.
361 Security::CertPointer
broken_cert(peer_cert
); // reasonable default if search fails
362 // Our SQUID_X509_V_ERR_DOMAIN_MISMATCH implies peer_cert is at fault.
363 if (error_no
!= SQUID_X509_V_ERR_DOMAIN_MISMATCH
) {
364 if (auto *last_used_cert
= X509_STORE_CTX_get_current_cert(ctx
))
365 broken_cert
.resetAndLock(last_used_cert
);
368 auto *errDetail
= new Ssl::ErrorDetail(error_no
, peer_cert
.get(), broken_cert
.get());
369 if (!SSL_set_ex_data(ssl
, ssl_ex_index_ssl_error_detail
, errDetail
)) {
370 debugs(83, 2, "Failed to set Ssl::ErrorDetail in ssl_verify_cb: Certificate " << buffer
);
379 Ssl::SetupVerifyCallback(Security::ContextPointer
&ctx
)
381 SSL_CTX_set_verify(ctx
.get(), SSL_VERIFY_PEER
| SSL_VERIFY_FAIL_IF_NO_PEER_CERT
, ssl_verify_cb
);
384 // "dup" function for SSL_get_ex_new_index("cert_err_check")
385 #if SQUID_USE_CONST_CRYPTO_EX_DATA_DUP
387 ssl_dupAclChecklist(CRYPTO_EX_DATA
*, const CRYPTO_EX_DATA
*, void *,
391 ssl_dupAclChecklist(CRYPTO_EX_DATA
*, CRYPTO_EX_DATA
*, void *,
395 // We do not support duplication of ACLCheckLists.
396 // If duplication is needed, we can count copies with cbdata.
401 // "free" function for SSL_get_ex_new_index("cert_err_check")
403 ssl_freeAclChecklist(void *, void *ptr
, CRYPTO_EX_DATA
*,
406 delete static_cast<ACLChecklist
*>(ptr
); // may be NULL
409 // "free" function for SSL_get_ex_new_index("ssl_error_detail")
411 ssl_free_ErrorDetail(void *, void *ptr
, CRYPTO_EX_DATA
*,
414 Ssl::ErrorDetail
*errDetail
= static_cast <Ssl::ErrorDetail
*>(ptr
);
419 ssl_free_SslErrors(void *, void *ptr
, CRYPTO_EX_DATA
*,
422 Security::CertErrors
*errs
= static_cast <Security::CertErrors
*>(ptr
);
426 // "free" function for SSL_get_ex_new_index("ssl_ex_index_ssl_validation_counter")
428 ssl_free_int(void *, void *ptr
, CRYPTO_EX_DATA
*,
431 uint32_t *counter
= static_cast <uint32_t *>(ptr
);
435 /// \ingroup ServerProtocolSSLInternal
436 /// Callback handler function to release STACK_OF(X509) "ex" data stored
437 /// in an SSL object.
439 ssl_free_CertChain(void *, void *ptr
, CRYPTO_EX_DATA
*,
442 STACK_OF(X509
) *certsChain
= static_cast <STACK_OF(X509
) *>(ptr
);
443 sk_X509_pop_free(certsChain
,X509_free
);
446 // "free" function for X509 certificates
448 ssl_free_X509(void *, void *ptr
, CRYPTO_EX_DATA
*,
451 X509
*cert
= static_cast <X509
*>(ptr
);
455 // "free" function for SBuf
457 ssl_free_SBuf(void *, void *ptr
, CRYPTO_EX_DATA
*,
460 SBuf
*buf
= static_cast <SBuf
*>(ptr
);
465 Ssl::Initialize(void)
467 static bool initialized
= false;
472 SSL_load_error_strings();
473 SSLeay_add_ssl_algorithms();
475 #if HAVE_OPENSSL_ENGINE_H
476 if (::Config
.SSL
.ssl_engine
) {
477 ENGINE_load_builtin_engines();
479 if (!(e
= ENGINE_by_id(::Config
.SSL
.ssl_engine
)))
480 fatalf("Unable to find SSL engine '%s'\n", ::Config
.SSL
.ssl_engine
);
482 if (!ENGINE_set_default(e
, ENGINE_METHOD_ALL
)) {
483 const int ssl_error
= ERR_get_error();
484 fatalf("Failed to initialise SSL engine: %s\n", Security::ErrorString(ssl_error
));
488 if (::Config
.SSL
.ssl_engine
)
489 fatalf("Your OpenSSL has no SSL engine support\n");
492 const char *defName
= ::Config
.SSL
.certSignHash
? ::Config
.SSL
.certSignHash
: SQUID_SSL_SIGN_HASH_IF_NONE
;
493 Ssl::DefaultSignHash
= EVP_get_digestbyname(defName
);
494 if (!Ssl::DefaultSignHash
)
495 fatalf("Sign hash '%s' is not supported\n", defName
);
497 ssl_ex_index_server
= SSL_get_ex_new_index(0, (void *) "server", NULL
, NULL
, ssl_free_SBuf
);
498 ssl_ctx_ex_index_dont_verify_domain
= SSL_CTX_get_ex_new_index(0, (void *) "dont_verify_domain", NULL
, NULL
, NULL
);
499 ssl_ex_index_cert_error_check
= SSL_get_ex_new_index(0, (void *) "cert_error_check", NULL
, &ssl_dupAclChecklist
, &ssl_freeAclChecklist
);
500 ssl_ex_index_ssl_error_detail
= SSL_get_ex_new_index(0, (void *) "ssl_error_detail", NULL
, NULL
, &ssl_free_ErrorDetail
);
501 ssl_ex_index_ssl_peeked_cert
= SSL_get_ex_new_index(0, (void *) "ssl_peeked_cert", NULL
, NULL
, &ssl_free_X509
);
502 ssl_ex_index_ssl_errors
= SSL_get_ex_new_index(0, (void *) "ssl_errors", NULL
, NULL
, &ssl_free_SslErrors
);
503 ssl_ex_index_ssl_cert_chain
= SSL_get_ex_new_index(0, (void *) "ssl_cert_chain", NULL
, NULL
, &ssl_free_CertChain
);
504 ssl_ex_index_ssl_validation_counter
= SSL_get_ex_new_index(0, (void *) "ssl_validation_counter", NULL
, NULL
, &ssl_free_int
);
505 ssl_ex_index_ssl_untrusted_chain
= SSL_get_ex_new_index(0, (void *) "ssl_untrusted_chain", NULL
, NULL
, &ssl_free_CertChain
);
509 Ssl::InitServerContext(Security::ContextPointer
&ctx
, AnyP::PortCfg
&port
)
518 Ssl::InitClientContext(Security::ContextPointer
&ctx
, Security::PeerOptions
&peer
, long fl
)
523 if (!peer
.sslCipher
.isEmpty()) {
524 debugs(83, 5, "Using chiper suite " << peer
.sslCipher
<< ".");
526 const char *cipher
= peer
.sslCipher
.c_str();
527 if (!SSL_CTX_set_cipher_list(ctx
.get(), cipher
)) {
528 const int ssl_error
= ERR_get_error();
529 fatalf("Failed to set SSL cipher suite '%s': %s\n",
530 cipher
, Security::ErrorString(ssl_error
));
534 if (!peer
.certs
.empty()) {
535 // TODO: support loading multiple cert/key pairs
536 auto &keys
= peer
.certs
.front();
537 if (!keys
.certFile
.isEmpty()) {
538 debugs(83, DBG_IMPORTANT
, "Using certificate in " << keys
.certFile
);
540 const char *certfile
= keys
.certFile
.c_str();
541 if (!SSL_CTX_use_certificate_chain_file(ctx
.get(), certfile
)) {
542 const int ssl_error
= ERR_get_error();
543 fatalf("Failed to acquire SSL certificate '%s': %s\n",
544 certfile
, Security::ErrorString(ssl_error
));
547 debugs(83, DBG_IMPORTANT
, "Using private key in " << keys
.privateKeyFile
);
548 const char *keyfile
= keys
.privateKeyFile
.c_str();
549 ssl_ask_password(ctx
.get(), keyfile
);
551 if (!SSL_CTX_use_PrivateKey_file(ctx
.get(), keyfile
, SSL_FILETYPE_PEM
)) {
552 const int ssl_error
= ERR_get_error();
553 fatalf("Failed to acquire SSL private key '%s': %s\n",
554 keyfile
, Security::ErrorString(ssl_error
));
557 debugs(83, 5, "Comparing private and public SSL keys.");
559 if (!SSL_CTX_check_private_key(ctx
.get())) {
560 const int ssl_error
= ERR_get_error();
561 fatalf("SSL private key '%s' does not match public key '%s': %s\n",
562 certfile
, keyfile
, Security::ErrorString(ssl_error
));
567 MaybeSetupRsaCallback(ctx
);
569 if (fl
& SSL_FLAG_DONT_VERIFY_PEER
) {
570 debugs(83, 2, "SECURITY WARNING: Peer certificates are not verified for validity!");
571 SSL_CTX_set_verify(ctx
.get(), SSL_VERIFY_NONE
, NULL
);
573 debugs(83, 9, "Setting certificate verification callback.");
574 Ssl::SetupVerifyCallback(ctx
);
580 /// \ingroup ServerProtocolSSLInternal
582 ssl_get_attribute(X509_NAME
* name
, const char *attribute_name
)
584 static char buffer
[1024];
587 if (strcmp(attribute_name
, "DN") == 0) {
588 X509_NAME_oneline(name
, buffer
, sizeof(buffer
));
590 int nid
= OBJ_txt2nid(const_cast<char *>(attribute_name
));
592 debugs(83, DBG_IMPORTANT
, "WARNING: Unknown SSL attribute name '" << attribute_name
<< "'");
595 X509_NAME_get_text_by_NID(name
, nid
, buffer
, sizeof(buffer
));
598 return *buffer
? buffer
: nullptr;
601 /// \ingroup ServerProtocolSSLInternal
603 Ssl::GetX509UserAttribute(X509
* cert
, const char *attribute_name
)
611 name
= X509_get_subject_name(cert
);
613 ret
= ssl_get_attribute(name
, attribute_name
);
619 Ssl::GetX509Fingerprint(X509
* cert
, const char *)
621 static char buf
[1024];
626 unsigned char md
[EVP_MAX_MD_SIZE
];
627 if (!X509_digest(cert
, EVP_sha1(), md
, &n
))
630 assert(3 * n
+ 1 < sizeof(buf
));
633 for (unsigned int i
=0; i
< n
; ++i
, s
+= 3) {
634 const char term
= (i
+ 1 < n
) ? ':' : '\0';
635 snprintf(s
, 4, "%02X%c", md
[i
], term
);
641 /// \ingroup ServerProtocolSSLInternal
643 Ssl::GetX509CAAttribute(X509
* cert
, const char *attribute_name
)
652 name
= X509_get_issuer_name(cert
);
654 ret
= ssl_get_attribute(name
, attribute_name
);
659 const char *sslGetUserAttribute(SSL
*ssl
, const char *attribute_name
)
664 X509
*cert
= SSL_get_peer_certificate(ssl
);
666 const char *attr
= Ssl::GetX509UserAttribute(cert
, attribute_name
);
672 const char *sslGetCAAttribute(SSL
*ssl
, const char *attribute_name
)
677 X509
*cert
= SSL_get_peer_certificate(ssl
);
679 const char *attr
= Ssl::GetX509CAAttribute(cert
, attribute_name
);
686 sslGetUserEmail(SSL
* ssl
)
688 return sslGetUserAttribute(ssl
, "emailAddress");
692 sslGetUserCertificatePEM(SSL
*ssl
)
696 static char *str
= NULL
;
705 cert
= SSL_get_peer_certificate(ssl
);
710 mem
= BIO_new(BIO_s_mem());
712 PEM_write_bio_X509(mem
, cert
);
714 len
= BIO_get_mem_data(mem
, &ptr
);
716 str
= (char *)xmalloc(len
+ 1);
718 memcpy(str
, ptr
, len
);
730 sslGetUserCertificateChainPEM(SSL
*ssl
)
732 STACK_OF(X509
) *chain
;
734 static char *str
= NULL
;
744 chain
= SSL_get_peer_cert_chain(ssl
);
747 return sslGetUserCertificatePEM(ssl
);
749 mem
= BIO_new(BIO_s_mem());
751 for (i
= 0; i
< sk_X509_num(chain
); ++i
) {
752 X509
*cert
= sk_X509_value(chain
, i
);
753 PEM_write_bio_X509(mem
, cert
);
756 len
= BIO_get_mem_data(mem
, &ptr
);
758 str
= (char *)xmalloc(len
+ 1);
759 memcpy(str
, ptr
, len
);
767 /// Create SSL context and apply ssl certificate and private key to it.
768 Security::ContextPointer
769 Ssl::createSSLContext(Security::CertPointer
& x509
, Security::PrivateKeyPointer
& pkey
, Security::ServerOptions
&options
)
771 Security::ContextPointer
ctx(options
.createBlankContext());
773 if (!SSL_CTX_use_certificate(ctx
.get(), x509
.get()))
774 return Security::ContextPointer();
776 if (!SSL_CTX_use_PrivateKey(ctx
.get(), pkey
.get()))
777 return Security::ContextPointer();
779 if (!options
.updateContextConfig(ctx
))
780 return Security::ContextPointer();
785 Security::ContextPointer
786 Ssl::GenerateSslContextUsingPkeyAndCertFromMemory(const char * data
, Security::ServerOptions
&options
, bool trusted
)
788 Security::CertPointer cert
;
789 Security::PrivateKeyPointer pkey
;
790 if (!readCertAndPrivateKeyFromMemory(cert
, pkey
, data
) || !cert
|| !pkey
)
791 return Security::ContextPointer();
793 Security::ContextPointer
ctx(createSSLContext(cert
, pkey
, options
));
795 Ssl::chainCertificatesToSSLContext(ctx
, options
);
799 Security::ContextPointer
800 Ssl::GenerateSslContext(CertificateProperties
const &properties
, Security::ServerOptions
&options
, bool trusted
)
802 Security::CertPointer cert
;
803 Security::PrivateKeyPointer pkey
;
804 if (!generateSslCertificate(cert
, pkey
, properties
) || !cert
|| !pkey
)
805 return Security::ContextPointer();
807 Security::ContextPointer
ctx(createSSLContext(cert
, pkey
, options
));
809 Ssl::chainCertificatesToSSLContext(ctx
, options
);
814 Ssl::chainCertificatesToSSLContext(Security::ContextPointer
&ctx
, Security::ServerOptions
&options
)
817 // Add signing certificate to the certificates chain
818 X509
*signingCert
= options
.signingCa
.cert
.get();
819 if (SSL_CTX_add_extra_chain_cert(ctx
.get(), signingCert
)) {
820 // increase the certificate lock
821 X509_up_ref(signingCert
);
823 const int ssl_error
= ERR_get_error();
824 debugs(33, DBG_IMPORTANT
, "WARNING: can not add signing certificate to SSL context chain: " << Security::ErrorString(ssl_error
));
827 for (auto cert
: options
.signingCa
.chain
) {
828 if (SSL_CTX_add_extra_chain_cert(ctx
.get(), cert
.get())) {
829 // increase the certificate lock
830 X509_up_ref(cert
.get());
832 const auto error
= ERR_get_error();
833 debugs(83, DBG_IMPORTANT
, "WARNING: can not add certificate to SSL dynamic context chain: " << Security::ErrorString(error
));
839 Ssl::configureUnconfiguredSslContext(Security::ContextPointer
&ctx
, Ssl::CertSignAlgorithm signAlgorithm
,AnyP::PortCfg
&port
)
841 if (ctx
&& signAlgorithm
== Ssl::algSignTrusted
)
842 Ssl::chainCertificatesToSSLContext(ctx
, port
.secure
);
846 Ssl::configureSSL(SSL
*ssl
, CertificateProperties
const &properties
, AnyP::PortCfg
&port
)
848 Security::CertPointer cert
;
849 Security::PrivateKeyPointer pkey
;
850 if (!generateSslCertificate(cert
, pkey
, properties
))
859 if (!SSL_use_certificate(ssl
, cert
.get()))
862 if (!SSL_use_PrivateKey(ssl
, pkey
.get()))
869 Ssl::configureSSLUsingPkeyAndCertFromMemory(SSL
*ssl
, const char *data
, AnyP::PortCfg
&port
)
871 Security::CertPointer cert
;
872 Security::PrivateKeyPointer pkey
;
873 if (!readCertAndPrivateKeyFromMemory(cert
, pkey
, data
))
879 if (!SSL_use_certificate(ssl
, cert
.get()))
882 if (!SSL_use_PrivateKey(ssl
, pkey
.get()))
889 Ssl::verifySslCertificate(Security::ContextPointer
&ctx
, CertificateProperties
const &properties
)
891 #if HAVE_SSL_CTX_GET0_CERTIFICATE
892 X509
* cert
= SSL_CTX_get0_certificate(ctx
.get());
893 #elif SQUID_USE_SSLGETCERTIFICATE_HACK
894 // SSL_get_certificate is buggy in openssl versions 1.0.1d and 1.0.1e
895 // Try to retrieve certificate directly from Security::ContextPointer object
896 X509
***pCert
= (X509
***)ctx
->cert
;
897 X509
* cert
= pCert
&& *pCert
? **pCert
: NULL
;
898 #elif SQUID_SSLGETCERTIFICATE_BUGGY
902 // Temporary ssl for getting X509 certificate from SSL_CTX.
903 Security::SessionPointer
ssl(Security::NewSessionObject(ctx
));
904 X509
* cert
= SSL_get_certificate(ssl
.get());
908 ASN1_TIME
* time_notBefore
= X509_get_notBefore(cert
);
909 ASN1_TIME
* time_notAfter
= X509_get_notAfter(cert
);
910 return (X509_cmp_current_time(time_notBefore
) < 0 && X509_cmp_current_time(time_notAfter
) > 0);
914 Ssl::setClientSNI(SSL
*ssl
, const char *fqdn
)
916 const Ip::Address
test(fqdn
);
917 if (!test
.isAnyAddr())
918 return; // raw IP is inappropriate for SNI
920 //The SSL_CTRL_SET_TLSEXT_HOSTNAME is a openssl macro which indicates
921 // if the TLS servername extension (SNI) is enabled in openssl library.
922 #if defined(SSL_CTRL_SET_TLSEXT_HOSTNAME)
923 if (!SSL_set_tlsext_host_name(ssl
, fqdn
)) {
924 const int ssl_error
= ERR_get_error();
925 debugs(83, 3, "WARNING: unable to set TLS servername extension (SNI): " <<
926 Security::ErrorString(ssl_error
) << "\n");
929 debugs(83, 7, "no support for TLS servername extension (SNI)");
934 hasAuthorityInfoAccessCaIssuers(X509
*cert
)
936 AUTHORITY_INFO_ACCESS
*info
;
939 info
= static_cast<AUTHORITY_INFO_ACCESS
*>(X509_get_ext_d2i(cert
, NID_info_access
, NULL
, NULL
));
943 static char uri
[MAX_URL
];
946 for (int i
= 0; i
< sk_ACCESS_DESCRIPTION_num(info
); i
++) {
947 ACCESS_DESCRIPTION
*ad
= sk_ACCESS_DESCRIPTION_value(info
, i
);
948 if (OBJ_obj2nid(ad
->method
) == NID_ad_ca_issuers
) {
949 if (ad
->location
->type
== GEN_URI
) {
951 reinterpret_cast<const char *>(
952 #if HAVE_LIBCRYPTO_ASN1_STRING_GET0_DATA
953 ASN1_STRING_get0_data(ad
->location
->d
.uniformResourceIdentifier
)
955 ASN1_STRING_data(ad
->location
->d
.uniformResourceIdentifier
)
963 AUTHORITY_INFO_ACCESS_free(info
);
964 return uri
[0] != '\0' ? uri
: nullptr;
968 Ssl::loadCerts(const char *certsFile
, Ssl::CertsIndexedList
&list
)
970 BIO
*in
= BIO_new_file(certsFile
, "r");
972 debugs(83, DBG_IMPORTANT
, "Failed to open '" << certsFile
<< "' to load certificates");
977 while((aCert
= PEM_read_bio_X509(in
, NULL
, NULL
, NULL
))) {
978 static char buffer
[2048];
979 X509_NAME_oneline(X509_get_subject_name(aCert
), buffer
, sizeof(buffer
));
980 list
.insert(std::pair
<SBuf
, X509
*>(SBuf(buffer
), aCert
));
982 debugs(83, 4, "Loaded " << list
.size() << " certificates from file: '" << certsFile
<< "'");
987 /// quickly find the issuer certificate of a certificate cert in the
988 /// Ssl::CertsIndexedList list
990 findCertIssuerFast(Ssl::CertsIndexedList
&list
, X509
*cert
)
992 static char buffer
[2048];
994 if (X509_NAME
*issuerName
= X509_get_issuer_name(cert
))
995 X509_NAME_oneline(issuerName
, buffer
, sizeof(buffer
));
999 const auto ret
= list
.equal_range(SBuf(buffer
));
1000 for (Ssl::CertsIndexedList::iterator it
= ret
.first
; it
!= ret
.second
; ++it
) {
1001 X509
*issuer
= it
->second
;
1002 if (X509_check_issued(issuer
, cert
) == X509_V_OK
) {
1009 /// slowly find the issuer certificate of a given cert using linear search
1011 findCertIssuer(Security::CertList
const &list
, X509
*cert
)
1013 for (Security::CertList::const_iterator it
= list
.begin(); it
!= list
.end(); ++it
) {
1014 if (X509_check_issued(it
->get(), cert
) == X509_V_OK
)
1020 /// \return true if the cert issuer exist in the certificates stored in connContext
1022 issuerExistInCaDb(X509
*cert
, const Security::ContextPointer
&connContext
)
1027 X509_STORE_CTX
*storeCtx
= X509_STORE_CTX_new();
1029 debugs(83, DBG_IMPORTANT
, "Failed to allocate STORE_CTX object");
1033 bool gotIssuer
= false;
1034 X509_STORE
*store
= SSL_CTX_get_cert_store(connContext
.get());
1035 if (X509_STORE_CTX_init(storeCtx
, store
, nullptr, nullptr)) {
1036 X509
*issuer
= nullptr;
1037 gotIssuer
= (X509_STORE_CTX_get1_issuer(&issuer
, storeCtx
, cert
) > 0);
1041 const int ssl_error
= ERR_get_error();
1042 debugs(83, DBG_IMPORTANT
, "Failed to initialize STORE_CTX object: " << Security::ErrorString(ssl_error
));
1044 X509_STORE_CTX_free(storeCtx
);
1050 Ssl::uriOfIssuerIfMissing(X509
*cert
, Security::CertList
const &serverCertificates
, const Security::ContextPointer
&context
)
1052 if (!cert
|| !serverCertificates
.size())
1055 if (!findCertIssuer(serverCertificates
, cert
)) {
1056 //if issuer is missing
1057 if (const char *issuerUri
= hasAuthorityInfoAccessCaIssuers(cert
)) {
1058 // There is a URI where we can download a certificate.
1059 if (!findCertIssuerFast(SquidUntrustedCerts
, cert
) &&
1060 !issuerExistInCaDb(cert
, context
)) {
1061 // and issuer not found in local databases containing
1062 // untrusted certificates and trusted CA certificates
1071 Ssl::missingChainCertificatesUrls(std::queue
<SBuf
> &URIs
, Security::CertList
const &serverCertificates
, const Security::ContextPointer
&context
)
1073 if (!serverCertificates
.size())
1076 for (const auto &i
: serverCertificates
) {
1077 if (const char *issuerUri
= uriOfIssuerIfMissing(i
.get(), serverCertificates
, context
))
1078 URIs
.push(SBuf(issuerUri
));
1083 Ssl::SSL_add_untrusted_cert(SSL
*ssl
, X509
*cert
)
1085 STACK_OF(X509
) *untrustedStack
= static_cast <STACK_OF(X509
) *>(SSL_get_ex_data(ssl
, ssl_ex_index_ssl_untrusted_chain
));
1086 if (!untrustedStack
) {
1087 untrustedStack
= sk_X509_new_null();
1088 if (!SSL_set_ex_data(ssl
, ssl_ex_index_ssl_untrusted_chain
, untrustedStack
)) {
1089 sk_X509_pop_free(untrustedStack
, X509_free
);
1090 throw TextException("Failed to attach untrusted certificates chain", Here());
1093 sk_X509_push(untrustedStack
, cert
);
1096 /// Search for the issuer certificate of cert in sk list.
1098 sk_x509_findIssuer(STACK_OF(X509
) *sk
, X509
*cert
)
1103 const int skItemsNum
= sk_X509_num(sk
);
1104 for (int i
= 0; i
< skItemsNum
; ++i
) {
1105 X509
*issuer
= sk_X509_value(sk
, i
);
1106 if (X509_check_issued(issuer
, cert
) == X509_V_OK
)
1112 /// add missing issuer certificates to untrustedCerts
1114 completeIssuers(X509_STORE_CTX
*ctx
, STACK_OF(X509
) *untrustedCerts
)
1116 debugs(83, 2, "completing " << sk_X509_num(untrustedCerts
) << " OpenSSL untrusted certs using " << SquidUntrustedCerts
.size() << " configured untrusted certificates");
1118 #if HAVE_LIBCRYPTO_X509_VERIFY_PARAM_GET_DEPTH
1119 const X509_VERIFY_PARAM
*param
= X509_STORE_CTX_get0_param(ctx
);
1120 int depth
= X509_VERIFY_PARAM_get_depth(param
);
1122 int depth
= ctx
->param
->depth
;
1124 X509
*current
= X509_STORE_CTX_get0_cert(ctx
);
1126 for (i
= 0; current
&& (i
< depth
); ++i
) {
1127 if (X509_check_issued(current
, current
) == X509_V_OK
) {
1128 // either ctx->cert is itself self-signed or untrustedCerts
1129 // aready contain the self-signed current certificate
1133 // untrustedCerts is short, not worth indexing
1134 X509
*issuer
= sk_x509_findIssuer(untrustedCerts
, current
);
1136 if ((issuer
= findCertIssuerFast(SquidUntrustedCerts
, current
)))
1137 sk_X509_push(untrustedCerts
, issuer
);
1143 debugs(83, 2, "exceeded the maximum certificate chain length: " << depth
);
1146 /// OpenSSL certificate validation callback.
1148 untrustedToStoreCtx_cb(X509_STORE_CTX
*ctx
,void *data
)
1150 debugs(83, 4, "Try to use pre-downloaded intermediate certificates");
1152 SSL
*ssl
= static_cast<SSL
*>(X509_STORE_CTX_get_ex_data(ctx
, SSL_get_ex_data_X509_STORE_CTX_idx()));
1153 STACK_OF(X509
) *sslUntrustedStack
= static_cast <STACK_OF(X509
) *>(SSL_get_ex_data(ssl
, ssl_ex_index_ssl_untrusted_chain
));
1155 // OpenSSL already maintains ctx->untrusted but we cannot modify
1156 // internal OpenSSL list directly. We have to give OpenSSL our own
1157 // list, but it must include certificates on the OpenSSL ctx->untrusted
1158 #if HAVE_LIBCRYPTO_X509_STORE_CTX_GET0_UNTRUSTED
1159 STACK_OF(X509
) *oldUntrusted
= X509_STORE_CTX_get0_untrusted(ctx
);
1161 STACK_OF(X509
) *oldUntrusted
= ctx
->untrusted
;
1163 STACK_OF(X509
) *sk
= sk_X509_dup(oldUntrusted
); // oldUntrusted is always not NULL
1165 for (int i
= 0; i
< sk_X509_num(sslUntrustedStack
); ++i
) {
1166 X509
*cert
= sk_X509_value(sslUntrustedStack
, i
);
1167 sk_X509_push(sk
, cert
);
1170 // If the local untrusted certificates internal database is used
1171 // run completeIssuers to add missing certificates if possible.
1172 if (SquidUntrustedCerts
.size() > 0)
1173 completeIssuers(ctx
, sk
);
1175 X509_STORE_CTX_set_chain(ctx
, sk
); // No locking/unlocking, just sets ctx->untrusted
1176 int ret
= X509_verify_cert(ctx
);
1177 #if HAVE_LIBCRYPTO_X509_STORE_CTX_SET0_UNTRUSTED
1178 X509_STORE_CTX_set0_untrusted(ctx
, oldUntrusted
);
1180 X509_STORE_CTX_set_chain(ctx
, oldUntrusted
); // Set back the old untrusted list
1182 sk_X509_free(sk
); // Release sk list
1187 Ssl::useSquidUntrusted(SSL_CTX
*sslContext
)
1189 SSL_CTX_set_cert_verify_callback(sslContext
, untrustedToStoreCtx_cb
, NULL
);
1193 Ssl::loadSquidUntrusted(const char *path
)
1195 return Ssl::loadCerts(path
, SquidUntrustedCerts
);
1199 Ssl::unloadSquidUntrusted()
1201 if (SquidUntrustedCerts
.size()) {
1202 for (Ssl::CertsIndexedList::iterator it
= SquidUntrustedCerts
.begin(); it
!= SquidUntrustedCerts
.end(); ++it
) {
1203 X509_free(it
->second
);
1205 SquidUntrustedCerts
.clear();
1209 bool Ssl::generateUntrustedCert(Security::CertPointer
&untrustedCert
, Security::PrivateKeyPointer
&untrustedPkey
, Security::CertPointer
const &cert
, Security::PrivateKeyPointer
const & pkey
)
1211 // Generate the self-signed certificate, using a hard-coded subject prefix
1212 Ssl::CertificateProperties certProperties
;
1213 if (const char *cn
= CommonHostName(cert
.get())) {
1214 certProperties
.commonName
= "Not trusted by \"";
1215 certProperties
.commonName
+= cn
;
1216 certProperties
.commonName
+= "\"";
1217 } else if (const char *org
= getOrganization(cert
.get())) {
1218 certProperties
.commonName
= "Not trusted by \"";
1219 certProperties
.commonName
+= org
;
1220 certProperties
.commonName
+= "\"";
1222 certProperties
.commonName
= "Not trusted";
1223 certProperties
.setCommonName
= true;
1224 // O, OU, and other CA subject fields will be mimicked
1225 // Expiration date and other common properties will be mimicked
1226 certProperties
.signAlgorithm
= Ssl::algSignSelf
;
1227 certProperties
.signWithPkey
.resetAndLock(pkey
.get());
1228 certProperties
.mimicCert
.resetAndLock(cert
.get());
1229 return Ssl::generateSslCertificate(untrustedCert
, untrustedPkey
, certProperties
);
1232 void Ssl::InRamCertificateDbKey(const Ssl::CertificateProperties
&certProperties
, SBuf
&key
)
1234 bool origSignatureAsKey
= false;
1235 if (certProperties
.mimicCert
) {
1236 if (auto *sig
= Ssl::X509_get_signature(certProperties
.mimicCert
)) {
1237 origSignatureAsKey
= true;
1238 key
.append((const char *)sig
->data
, sig
->length
);
1242 if (!origSignatureAsKey
|| certProperties
.setCommonName
) {
1243 // Use common name instead
1244 key
.append(certProperties
.commonName
.c_str());
1246 key
.append(certProperties
.setCommonName
? '1' : '0');
1247 key
.append(certProperties
.setValidAfter
? '1' : '0');
1248 key
.append(certProperties
.setValidBefore
? '1' : '0');
1249 key
.append(certProperties
.signAlgorithm
!= Ssl:: algSignEnd
? certSignAlgorithm(certProperties
.signAlgorithm
) : "-");
1250 key
.append(certProperties
.signHash
? EVP_MD_name(certProperties
.signHash
) : "-");
1252 if (certProperties
.mimicCert
) {
1253 Ssl::BIO_Pointer
bio(BIO_new_SBuf(&key
));
1254 ASN1_item_i2d_bio(ASN1_ITEM_rptr(X509
), bio
.get(), (ASN1_VALUE
*)certProperties
.mimicCert
.get());
1259 bio_sbuf_create(BIO
* bio
)
1261 BIO_set_init(bio
, 0);
1262 BIO_set_data(bio
, NULL
);
1267 bio_sbuf_destroy(BIO
* bio
)
1275 bio_sbuf_write(BIO
* bio
, const char* data
, int len
)
1277 SBuf
*buf
= static_cast<SBuf
*>(BIO_get_data(bio
));
1278 // TODO: Convert exceptions into BIO errors
1279 buf
->append(data
, len
);
1284 bio_sbuf_puts(BIO
* bio
, const char* data
)
1286 // TODO: use bio_sbuf_write() instead
1287 SBuf
*buf
= static_cast<SBuf
*>(BIO_get_data(bio
));
1288 size_t oldLen
= buf
->length();
1290 return buf
->length() - oldLen
;
1294 bio_sbuf_ctrl(BIO
* bio
, int cmd
, long num
, void* ptr
) {
1295 SBuf
*buf
= static_cast<SBuf
*>(BIO_get_data(bio
));
1297 case BIO_CTRL_RESET
:
1298 // TODO: Convert exceptions into BIO errors
1301 case BIO_CTRL_FLUSH
:
1308 BIO
*Ssl::BIO_new_SBuf(SBuf
*buf
)
1310 #if HAVE_LIBCRYPTO_BIO_METH_NEW
1311 static BIO_METHOD
*BioSBufMethods
= nullptr;
1312 if (!BioSBufMethods
) {
1313 BioSBufMethods
= BIO_meth_new(BIO_TYPE_MEM
, "Squid-SBuf");
1314 BIO_meth_set_write(BioSBufMethods
, bio_sbuf_write
);
1315 BIO_meth_set_read(BioSBufMethods
, nullptr);
1316 BIO_meth_set_puts(BioSBufMethods
, bio_sbuf_puts
);
1317 BIO_meth_set_gets(BioSBufMethods
, nullptr);
1318 BIO_meth_set_ctrl(BioSBufMethods
, bio_sbuf_ctrl
);
1319 BIO_meth_set_create(BioSBufMethods
, bio_sbuf_create
);
1320 BIO_meth_set_destroy(BioSBufMethods
, bio_sbuf_destroy
);
1323 static BIO_METHOD
*BioSBufMethods
= new BIO_METHOD({
1336 BIO
*bio
= BIO_new(BioSBufMethods
);
1338 BIO_set_data(bio
, buf
);
1339 BIO_set_init(bio
, 1);
1343 #endif /* USE_OPENSSL */