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