]>
Commit | Line | Data |
---|---|---|
bbc27441 | 1 | /* |
f70aedc4 | 2 | * Copyright (C) 1996-2021 The Squid Software Foundation and contributors |
bbc27441 AJ |
3 | * |
4 | * Squid software is distributed under GPLv2+ license and includes | |
5 | * contributions from numerous individuals and organizations. | |
6 | * Please see the COPYING and CONTRIBUTORS files for details. | |
7 | */ | |
8 | ||
f7f3304a | 9 | #include "squid.h" |
24438ec5 | 10 | #include "../helper.h" |
502bcc7b | 11 | #include "anyp/PortCfg.h" |
72247610 | 12 | #include "cache_cf.h" |
b3f7fd88 | 13 | #include "fs_io.h" |
24438ec5 | 14 | #include "helper/Reply.h" |
72247610 | 15 | #include "Parsing.h" |
70ac5b29 | 16 | #include "sbuf/Stream.h" |
602d9612 | 17 | #include "SquidConfig.h" |
f9b6ff6e | 18 | #include "SquidString.h" |
95d2589c | 19 | #include "SquidTime.h" |
14798e73 | 20 | #include "ssl/cert_validate_message.h" |
602d9612 A |
21 | #include "ssl/Config.h" |
22 | #include "ssl/helper.h" | |
fc54b8d2 | 23 | #include "wordlist.h" |
95d2589c | 24 | |
1f8b5f0e | 25 | #include <limits> |
26 | ||
72247610 | 27 | Ssl::CertValidationHelper::CacheType *Ssl::CertValidationHelper::HelperCache = nullptr; |
968b000d | 28 | |
58fa3f51 CT |
29 | #if USE_SSL_CRTD |
30 | ||
31 | namespace Ssl { | |
32 | ||
33 | /// Initiator of an Ssl::Helper query. | |
34 | class GeneratorRequestor { | |
35 | public: | |
36 | GeneratorRequestor(HLPCB *aCallback, void *aData): callback(aCallback), data(aData) {} | |
37 | HLPCB *callback; | |
38 | CallbackData data; | |
39 | }; | |
40 | ||
41 | /// A pending Ssl::Helper request, combining the original and collapsed queries. | |
42 | class GeneratorRequest { | |
43 | CBDATA_CLASS(GeneratorRequest); | |
44 | ||
45 | public: | |
46 | /// adds a GeneratorRequestor | |
47 | void emplace(HLPCB *callback, void *data) { requestors.emplace_back(callback, data); } | |
48 | ||
49 | SBuf query; ///< Ssl::Helper request message (GeneratorRequests key) | |
50 | ||
51 | /// Ssl::Helper request initiators waiting for the same answer (FIFO). | |
52 | typedef std::vector<GeneratorRequestor> GeneratorRequestors; | |
53 | GeneratorRequestors requestors; | |
54 | }; | |
55 | ||
56 | /// Ssl::Helper query:GeneratorRequest map | |
57 | typedef std::unordered_map<SBuf, GeneratorRequest*> GeneratorRequests; | |
58 | ||
59 | static void HandleGeneratorReply(void *data, const ::Helper::Reply &reply); | |
60 | ||
61 | } // namespace Ssl | |
62 | ||
63 | CBDATA_NAMESPACED_CLASS_INIT(Ssl, GeneratorRequest); | |
64 | ||
65 | /// prints Ssl::GeneratorRequest for debugging | |
66 | static std::ostream & | |
67 | operator <<(std::ostream &os, const Ssl::GeneratorRequest &gr) | |
68 | { | |
69 | return os << "crtGenRq" << gr.query.id.value << "/" << gr.requestors.size(); | |
70 | } | |
71 | ||
72 | /// pending Ssl::Helper requests (to all certificate generator helpers combined) | |
73 | static Ssl::GeneratorRequests TheGeneratorRequests; | |
74 | ||
23da195f | 75 | helper *Ssl::Helper::ssl_crtd = nullptr; |
95d2589c CT |
76 | |
77 | void Ssl::Helper::Init() | |
78 | { | |
586089cd CT |
79 | assert(ssl_crtd == NULL); |
80 | ||
f0763147 AJ |
81 | // we need to start ssl_crtd only if some port(s) need to bump SSL *and* generate certificates |
82 | // TODO: generate host certificates for SNI enabled accel ports | |
24e6c8f1 | 83 | bool found = false; |
fa720bfb | 84 | for (AnyP::PortCfgPointer s = HttpPortList; !found && s != NULL; s = s->next) |
cf487124 | 85 | found = s->flags.tunnelSslBumping && s->secure.generateHostCertificates; |
24e6c8f1 | 86 | if (!found) |
586089cd CT |
87 | return; |
88 | ||
91ce75c9 | 89 | ssl_crtd = new helper("sslcrtd_program"); |
1af735c7 | 90 | ssl_crtd->childs.updateLimits(Ssl::TheConfig.ssl_crtdChildren); |
95d2589c | 91 | ssl_crtd->ipc_type = IPC_STREAM; |
0af9303a CT |
92 | // The crtd messages may contain the eol ('\n') character. We are |
93 | // going to use the '\1' char as the end-of-message mark. | |
94 | ssl_crtd->eom = '\1'; | |
95d2589c CT |
95 | assert(ssl_crtd->cmdline == NULL); |
96 | { | |
97 | char *tmp = xstrdup(Ssl::TheConfig.ssl_crtd); | |
98 | char *tmp_begin = tmp; | |
ba5d55c1 | 99 | char *token = NULL; |
95d2589c CT |
100 | while ((token = strwordtok(NULL, &tmp))) { |
101 | wordlistAdd(&ssl_crtd->cmdline, token); | |
95d2589c CT |
102 | } |
103 | safe_free(tmp_begin); | |
104 | } | |
95d2589c CT |
105 | helperOpenServers(ssl_crtd); |
106 | } | |
107 | ||
108 | void Ssl::Helper::Shutdown() | |
109 | { | |
110 | if (!ssl_crtd) | |
111 | return; | |
112 | helperShutdown(ssl_crtd); | |
113 | wordlistDestroy(&ssl_crtd->cmdline); | |
95d2589c CT |
114 | delete ssl_crtd; |
115 | ssl_crtd = NULL; | |
116 | } | |
117 | ||
23da195f CT |
118 | void |
119 | Ssl::Helper::Reconfigure() | |
95d2589c | 120 | { |
23da195f CT |
121 | Shutdown(); |
122 | Init(); | |
123 | } | |
95d2589c | 124 | |
23da195f CT |
125 | void Ssl::Helper::Submit(CrtdMessage const & message, HLPCB * callback, void * data) |
126 | { | |
58fa3f51 CT |
127 | SBuf rawMessage(message.compose().c_str()); // XXX: helpers cannot use SBuf |
128 | rawMessage.append("\n", 1); | |
129 | ||
130 | const auto pending = TheGeneratorRequests.find(rawMessage); | |
131 | if (pending != TheGeneratorRequests.end()) { | |
132 | pending->second->emplace(callback, data); | |
133 | debugs(83, 5, "collapsed request from " << data << " onto " << *pending->second); | |
134 | return; | |
135 | } | |
136 | ||
137 | GeneratorRequest *request = new GeneratorRequest; | |
138 | request->query = rawMessage; | |
139 | request->emplace(callback, data); | |
140 | TheGeneratorRequests.emplace(request->query, request); | |
141 | debugs(83, 5, "request from " << data << " as " << *request); | |
23da195f CT |
142 | // ssl_crtd becomes nil if Squid is reconfigured without SslBump or |
143 | // certificate generation disabled in the new configuration | |
144 | if (ssl_crtd && ssl_crtd->trySubmit(request->query.c_str(), HandleGeneratorReply, request)) | |
95d2589c | 145 | return; |
58fa3f51 CT |
146 | |
147 | ::Helper::Reply failReply(::Helper::BrokenHelper); | |
148 | failReply.notes.add("message", "error 45 Temporary network problem, please retry later"); | |
149 | HandleGeneratorReply(request, failReply); | |
150 | } | |
151 | ||
152 | /// receives helper response | |
153 | static void | |
154 | Ssl::HandleGeneratorReply(void *data, const ::Helper::Reply &reply) | |
155 | { | |
156 | const std::unique_ptr<Ssl::GeneratorRequest> request(static_cast<Ssl::GeneratorRequest*>(data)); | |
157 | assert(request); | |
158 | const auto erased = TheGeneratorRequests.erase(request->query); | |
159 | assert(erased); | |
160 | ||
161 | for (auto &requestor: request->requestors) { | |
162 | if (void *cbdata = requestor.data.validDone()) { | |
163 | debugs(83, 5, "to " << cbdata << " in " << *request); | |
164 | requestor.callback(cbdata, reply); | |
165 | } | |
95d2589c | 166 | } |
95d2589c | 167 | } |
4a77bb4e | 168 | #endif //USE_SSL_CRTD |
2cef0ca6 | 169 | |
23da195f | 170 | helper *Ssl::CertValidationHelper::ssl_crt_validator = nullptr; |
2cef0ca6 AR |
171 | |
172 | void Ssl::CertValidationHelper::Init() | |
173 | { | |
23da195f CT |
174 | if (!Ssl::TheConfig.ssl_crt_validator) |
175 | return; | |
176 | ||
2cef0ca6 AR |
177 | assert(ssl_crt_validator == NULL); |
178 | ||
179 | // we need to start ssl_crtd only if some port(s) need to bump SSL | |
180 | bool found = false; | |
fa720bfb | 181 | for (AnyP::PortCfgPointer s = HttpPortList; !found && s != NULL; s = s->next) |
6a25a046 | 182 | found = s->flags.tunnelSslBumping; |
2cef0ca6 AR |
183 | if (!found) |
184 | return; | |
185 | ||
186 | ssl_crt_validator = new helper("ssl_crt_validator"); | |
187 | ssl_crt_validator->childs.updateLimits(Ssl::TheConfig.ssl_crt_validator_Children); | |
188 | ssl_crt_validator->ipc_type = IPC_STREAM; | |
189 | // The crtd messages may contain the eol ('\n') character. We are | |
190 | // going to use the '\1' char as the end-of-message mark. | |
191 | ssl_crt_validator->eom = '\1'; | |
192 | assert(ssl_crt_validator->cmdline == NULL); | |
14798e73 | 193 | |
72247610 AJ |
194 | /* defaults */ |
195 | int ttl = 3600; // 1 hour | |
196 | size_t cache = 64*1024*1024; // 64 MB | |
2cef0ca6 | 197 | { |
72247610 | 198 | // TODO: Do this during parseConfigFile() for proper parsing, error handling |
2cef0ca6 AR |
199 | char *tmp = xstrdup(Ssl::TheConfig.ssl_crt_validator); |
200 | char *tmp_begin = tmp; | |
201 | char * token = NULL; | |
14798e73 | 202 | bool parseParams = true; |
2cef0ca6 | 203 | while ((token = strwordtok(NULL, &tmp))) { |
14798e73 | 204 | if (parseParams) { |
72247610 AJ |
205 | if (strcmp(token, "ttl=infinity") == 0) { |
206 | ttl = std::numeric_limits<CacheType::Ttl>::max(); | |
207 | continue; | |
208 | } else if (strncmp(token, "ttl=", 4) == 0) { | |
209 | ttl = xatoi(token + 4); | |
210 | if (ttl < 0) { | |
211 | throw TextException(ToSBuf("Negative TTL in sslcrtvalidator_program ", Ssl::TheConfig.ssl_crt_validator, | |
70ac5b29 | 212 | Debug::Extra, "For unlimited TTL, use ttl=infinity"), |
213 | Here()); | |
72247610 | 214 | } |
14798e73 CT |
215 | continue; |
216 | } else if (strncmp(token, "cache=", 6) == 0) { | |
72247610 | 217 | cache = xatoi(token + 6); |
14798e73 CT |
218 | continue; |
219 | } else | |
220 | parseParams = false; | |
221 | } | |
2cef0ca6 AR |
222 | wordlistAdd(&ssl_crt_validator->cmdline, token); |
223 | } | |
4a77bb4e | 224 | xfree(tmp_begin); |
2cef0ca6 AR |
225 | } |
226 | helperOpenServers(ssl_crt_validator); | |
14798e73 CT |
227 | |
228 | //WARNING: initializing static member in an object initialization method | |
229 | assert(HelperCache == NULL); | |
72247610 | 230 | HelperCache = new CacheType(cache, ttl); |
2cef0ca6 AR |
231 | } |
232 | ||
233 | void Ssl::CertValidationHelper::Shutdown() | |
234 | { | |
235 | if (!ssl_crt_validator) | |
236 | return; | |
237 | helperShutdown(ssl_crt_validator); | |
238 | wordlistDestroy(&ssl_crt_validator->cmdline); | |
239 | delete ssl_crt_validator; | |
240 | ssl_crt_validator = NULL; | |
4c304fb9 | 241 | |
14798e73 | 242 | // CertValidationHelper::HelperCache is a static member, it is not good policy to |
4c304fb9 | 243 | // reset it here. Will work because the current Ssl::CertValidationHelper is |
14798e73 CT |
244 | // always the same static object. |
245 | delete HelperCache; | |
246 | HelperCache = NULL; | |
247 | } | |
248 | ||
23da195f CT |
249 | void |
250 | Ssl::CertValidationHelper::Reconfigure() | |
251 | { | |
252 | Shutdown(); | |
253 | Init(); | |
254 | } | |
255 | ||
5c2f68b7 AJ |
256 | class submitData |
257 | { | |
258 | CBDATA_CLASS(submitData); | |
259 | ||
260 | public: | |
5107d2c4 | 261 | SBuf query; |
0e208dad | 262 | AsyncCall::Pointer callback; |
e601ca5d | 263 | Security::SessionPointer ssl; |
14798e73 CT |
264 | }; |
265 | CBDATA_CLASS_INIT(submitData); | |
266 | ||
267 | static void | |
24438ec5 | 268 | sslCrtvdHandleReplyWrapper(void *data, const ::Helper::Reply &reply) |
14798e73 CT |
269 | { |
270 | Ssl::CertValidationMsg replyMsg(Ssl::CrtdMessage::REPLY); | |
14798e73 CT |
271 | |
272 | submitData *crtdvdData = static_cast<submitData *>(data); | |
8fe1a85a CT |
273 | assert(crtdvdData->ssl.get()); |
274 | Ssl::CertValidationResponse::Pointer validationResponse = new Ssl::CertValidationResponse(crtdvdData->ssl); | |
2428ce02 | 275 | if (reply.result == ::Helper::BrokenHelper) { |
14798e73 | 276 | debugs(83, DBG_IMPORTANT, "\"ssl_crtvd\" helper error response: " << reply.other().content()); |
2428ce02 | 277 | validationResponse->resultCode = ::Helper::BrokenHelper; |
ddc77a2e CT |
278 | } else if (!reply.other().hasContent()) { |
279 | debugs(83, DBG_IMPORTANT, "\"ssl_crtvd\" helper returned NULL response"); | |
280 | validationResponse->resultCode = ::Helper::BrokenHelper; | |
14798e73 | 281 | } else if (replyMsg.parse(reply.other().content(), reply.other().contentSize()) != Ssl::CrtdMessage::OK || |
8b082ed9 | 282 | !replyMsg.parseResponse(*validationResponse) ) { |
14798e73 CT |
283 | debugs(83, DBG_IMPORTANT, "WARNING: Reply from ssl_crtvd for " << " is incorrect"); |
284 | debugs(83, DBG_IMPORTANT, "Certificate cannot be validated. ssl_crtvd response: " << replyMsg.getBody()); | |
2428ce02 | 285 | validationResponse->resultCode = ::Helper::BrokenHelper; |
4c304fb9 | 286 | } else |
14798e73 CT |
287 | validationResponse->resultCode = reply.result; |
288 | ||
0e208dad CT |
289 | Ssl::CertValidationHelper::CbDialer *dialer = dynamic_cast<Ssl::CertValidationHelper::CbDialer*>(crtdvdData->callback->getDialer()); |
290 | Must(dialer); | |
291 | dialer->arg1 = validationResponse; | |
292 | ScheduleCallHere(crtdvdData->callback); | |
14798e73 CT |
293 | |
294 | if (Ssl::CertValidationHelper::HelperCache && | |
2428ce02 | 295 | (validationResponse->resultCode == ::Helper::Okay || validationResponse->resultCode == ::Helper::Error)) { |
72247610 | 296 | (void)Ssl::CertValidationHelper::HelperCache->add(crtdvdData->query, validationResponse); |
0e208dad | 297 | } |
14798e73 | 298 | |
14798e73 | 299 | delete crtdvdData; |
2cef0ca6 AR |
300 | } |
301 | ||
23da195f | 302 | void Ssl::CertValidationHelper::Submit(Ssl::CertValidationRequest const &request, AsyncCall::Pointer &callback) |
2cef0ca6 | 303 | { |
14798e73 CT |
304 | Ssl::CertValidationMsg message(Ssl::CrtdMessage::REQUEST); |
305 | message.setCode(Ssl::CertValidationMsg::code_cert_validate); | |
306 | message.composeRequest(request); | |
307 | debugs(83, 5, "SSL crtvd request: " << message.compose().c_str()); | |
308 | ||
309 | submitData *crtdvdData = new submitData; | |
5107d2c4 CT |
310 | crtdvdData->query.assign(message.compose().c_str()); |
311 | crtdvdData->query.append('\n'); | |
14798e73 | 312 | crtdvdData->callback = callback; |
0b168d25 | 313 | crtdvdData->ssl = request.ssl; |
0e208dad | 314 | Ssl::CertValidationResponse::Pointer const*validationResponse; |
14798e73 CT |
315 | |
316 | if (CertValidationHelper::HelperCache && | |
5107d2c4 | 317 | (validationResponse = CertValidationHelper::HelperCache->get(crtdvdData->query))) { |
0e208dad CT |
318 | |
319 | CertValidationHelper::CbDialer *dialer = dynamic_cast<CertValidationHelper::CbDialer*>(callback->getDialer()); | |
bac324a9 | 320 | Must(dialer); |
0e208dad CT |
321 | dialer->arg1 = *validationResponse; |
322 | ScheduleCallHere(callback); | |
14798e73 CT |
323 | delete crtdvdData; |
324 | return; | |
325 | } | |
6825b101 | 326 | |
23da195f CT |
327 | // ssl_crt_validator becomes nil if Squid is reconfigured with cert |
328 | // validator disabled in the new configuration | |
329 | if (ssl_crt_validator && ssl_crt_validator->trySubmit(crtdvdData->query.c_str(), sslCrtvdHandleReplyWrapper, crtdvdData)) | |
6825b101 | 330 | return; |
23da195f CT |
331 | |
332 | Ssl::CertValidationResponse::Pointer resp = new Ssl::CertValidationResponse(crtdvdData->ssl); | |
333 | resp->resultCode = ::Helper::BrokenHelper; | |
334 | Ssl::CertValidationHelper::CbDialer *dialer = dynamic_cast<Ssl::CertValidationHelper::CbDialer*>(callback->getDialer()); | |
335 | Must(dialer); | |
336 | dialer->arg1 = resp; | |
337 | ScheduleCallHere(callback); | |
338 | delete crtdvdData; | |
339 | return; | |
2cef0ca6 | 340 | } |
f53969cc | 341 |