]> git.ipfire.org Git - thirdparty/squid.git/blob - src/ssl/cert_validate_message.cc
SourceFormat Enforcement
[thirdparty/squid.git] / src / ssl / cert_validate_message.cc
1 /*
2 * Copyright (C) 1996-2016 The Squid Software Foundation and contributors
3 *
4 * Squid software is distributed under GPLv2+ license and includes
5 * contributions from numerous individuals and organizations.
6 * Please see the COPYING and CONTRIBUTORS files for details.
7 */
8
9 #include "squid.h"
10 #include "acl/FilledChecklist.h"
11 #include "globals.h"
12 #include "helper.h"
13 #include "ssl/cert_validate_message.h"
14 #include "ssl/ErrorDetail.h"
15 #include "ssl/support.h"
16 #include "util.h"
17
18 void
19 Ssl::CertValidationMsg::composeRequest(CertValidationRequest const &vcert)
20 {
21 body.clear();
22 body += Ssl::CertValidationMsg::param_host + "=" + vcert.domainName;
23 STACK_OF(X509) *peerCerts = static_cast<STACK_OF(X509) *>(SSL_get_ex_data(vcert.ssl, ssl_ex_index_ssl_cert_chain));
24
25 if (const char *sslVersion = SSL_get_version(vcert.ssl))
26 body += "\n" + Ssl::CertValidationMsg::param_proto_version + "=" + sslVersion;
27
28 if (const char *cipherName = SSL_CIPHER_get_name(SSL_get_current_cipher(vcert.ssl)))
29 body += "\n" + Ssl::CertValidationMsg::param_cipher + "=" + cipherName;
30
31 if (!peerCerts)
32 peerCerts = SSL_get_peer_cert_chain(vcert.ssl);
33
34 if (peerCerts) {
35 Ssl::BIO_Pointer bio(BIO_new(BIO_s_mem()));
36 for (int i = 0; i < sk_X509_num(peerCerts); ++i) {
37 X509 *cert = sk_X509_value(peerCerts, i);
38 PEM_write_bio_X509(bio.get(), cert);
39 body = body + "\n" + param_cert + xitoa(i) + "=";
40 char *ptr;
41 long len = BIO_get_mem_data(bio.get(), &ptr);
42 body.append(ptr, (ptr[len-1] == '\n' ? len - 1 : len));
43 if (!BIO_reset(bio.get())) {
44 // print an error?
45 }
46 }
47 }
48
49 if (vcert.errors) {
50 int i = 0;
51 for (const Ssl::CertErrors *err = vcert.errors; err; err = err->next, ++i) {
52 body +="\n";
53 body = body + param_error_name + xitoa(i) + "=" + GetErrorName(err->element.code) + "\n";
54 int errorCertPos = -1;
55 if (err->element.cert.get())
56 errorCertPos = sk_X509_find(peerCerts, err->element.cert.get());
57 if (errorCertPos < 0) {
58 // assert this error ?
59 debugs(83, 4, "WARNING: wrong cert in cert validator request");
60 }
61 body += param_error_cert + xitoa(i) + "=";
62 body += param_cert + xitoa((errorCertPos >= 0 ? errorCertPos : 0));
63 }
64 }
65 }
66
67 static int
68 get_error_id(const char *label, size_t len)
69 {
70 const char *e = label + len -1;
71 while (e != label && xisdigit(*e)) --e;
72 if (e != label) ++e;
73 return strtol(e, 0 , 10);
74 }
75
76 bool
77 Ssl::CertValidationMsg::parseResponse(CertValidationResponse &resp, STACK_OF(X509) *peerCerts, std::string &error)
78 {
79 std::vector<CertItem> certs;
80
81 const char *param = body.c_str();
82 while (*param) {
83 while (xisspace(*param)) param++;
84 if (! *param)
85 break;
86
87 size_t param_len = strcspn(param, "=\r\n");
88 if (param[param_len] != '=') {
89 debugs(83, DBG_IMPORTANT, "WARNING: cert validator response parse error: " << param);
90 return false;
91 }
92 const char *value=param+param_len+1;
93
94 if (param_len > param_cert.length() &&
95 strncmp(param, param_cert.c_str(), param_cert.length()) == 0) {
96 CertItem ci;
97 ci.name.assign(param, param_len);
98 Security::CertPointer x509;
99 readCertFromMemory(x509, value);
100 ci.setCert(x509.get());
101 certs.push_back(ci);
102
103 const char *b = strstr(value, "-----END CERTIFICATE-----");
104 if (b == NULL) {
105 debugs(83, DBG_IMPORTANT, "WARNING: cert Validator response parse error: Failed to find certificate boundary " << value);
106 return false;
107 }
108 b += strlen("-----END CERTIFICATE-----");
109 param = b + 1;
110 continue;
111 }
112
113 size_t value_len = strcspn(value, "\r\n");
114 std::string v(value, value_len);
115
116 debugs(83, 5, "Returned value: " << std::string(param, param_len).c_str() << ": " <<
117 v.c_str());
118
119 int errorId = get_error_id(param, param_len);
120 Ssl::CertValidationResponse::RecvdError &currentItem = resp.getError(errorId);
121
122 if (param_len > param_error_name.length() &&
123 strncmp(param, param_error_name.c_str(), param_error_name.length()) == 0) {
124 currentItem.error_no = Ssl::GetErrorCode(v.c_str());
125 if (currentItem.error_no == SSL_ERROR_NONE) {
126 debugs(83, DBG_IMPORTANT, "WARNING: cert validator response parse error: Unknown SSL Error: " << v);
127 return false;
128 }
129 } else if (param_len > param_error_reason.length() &&
130 strncmp(param, param_error_reason.c_str(), param_error_reason.length()) == 0) {
131 currentItem.error_reason = v;
132 } else if (param_len > param_error_cert.length() &&
133 strncmp(param, param_error_cert.c_str(), param_error_cert.length()) == 0) {
134
135 if (X509 *cert = getCertByName(certs, v)) {
136 debugs(83, 6, "The certificate with id \"" << v << "\" found.");
137 currentItem.setCert(cert);
138 } else {
139 //In this case we assume that the certID is one of the certificates sent
140 // to cert validator. The certificates sent to cert validator have names in
141 // form "cert_xx" where the "xx" is an integer represents the position of
142 // the certificate inside peer certificates list.
143 const int certId = get_error_id(v.c_str(), v.length());
144 debugs(83, 6, "Cert index in peer certificates list:" << certId);
145 //if certId is not correct sk_X509_value returns NULL
146 currentItem.setCert(sk_X509_value(peerCerts, certId));
147 }
148 } else if (param_len > param_error_depth.length() &&
149 strncmp(param, param_error_depth.c_str(), param_error_depth.length()) == 0 &&
150 std::all_of(v.begin(), v.end(), isdigit)) {
151 currentItem.error_depth = atoi(v.c_str());
152 } else {
153 debugs(83, DBG_IMPORTANT, "WARNING: cert validator response parse error: Unknown parameter name " << std::string(param, param_len).c_str());
154 return false;
155 }
156
157 param = value + value_len +1;
158 }
159
160 /*Run through parsed errors to check for errors*/
161 typedef Ssl::CertValidationResponse::RecvdErrors::const_iterator SVCRECI;
162 for (SVCRECI i = resp.errors.begin(); i != resp.errors.end(); ++i) {
163 if (i->error_no == SSL_ERROR_NONE) {
164 debugs(83, DBG_IMPORTANT, "WARNING: cert validator incomplete response: Missing error name from error_id: " << i->id);
165 return false;
166 }
167 }
168
169 return true;
170 }
171
172 X509 *
173 Ssl::CertValidationMsg::getCertByName(std::vector<CertItem> const &certs, std::string const & name)
174 {
175 typedef std::vector<CertItem>::const_iterator SVCI;
176 for (SVCI ci = certs.begin(); ci != certs.end(); ++ci) {
177 if (ci->name.compare(name) == 0)
178 return ci->cert.get();
179 }
180 return NULL;
181 }
182
183 Ssl::CertValidationResponse::RecvdError &
184 Ssl::CertValidationResponse::getError(int errorId)
185 {
186 typedef Ssl::CertValidationResponse::RecvdErrors::iterator SVCREI;
187 for (SVCREI i = errors.begin(); i != errors.end(); ++i) {
188 if (i->id == errorId)
189 return *i;
190 }
191 Ssl::CertValidationResponse::RecvdError errItem;
192 errItem.id = errorId;
193 errors.push_back(errItem);
194 return errors.back();
195 }
196
197 Ssl::CertValidationResponse::RecvdError::RecvdError(const RecvdError &old)
198 {
199 id = old.id;
200 error_no = old.error_no;
201 error_reason = old.error_reason;
202 setCert(old.cert.get());
203 }
204
205 Ssl::CertValidationResponse::RecvdError & Ssl::CertValidationResponse::RecvdError::operator = (const RecvdError &old)
206 {
207 id = old.id;
208 error_no = old.error_no;
209 error_reason = old.error_reason;
210 setCert(old.cert.get());
211 return *this;
212 }
213
214 void
215 Ssl::CertValidationResponse::RecvdError::setCert(X509 *aCert)
216 {
217 cert.resetAndLock(aCert);
218 }
219
220 Ssl::CertValidationMsg::CertItem::CertItem(const CertItem &old)
221 {
222 name = old.name;
223 setCert(old.cert.get());
224 }
225
226 Ssl::CertValidationMsg::CertItem & Ssl::CertValidationMsg::CertItem::operator = (const CertItem &old)
227 {
228 name = old.name;
229 setCert(old.cert.get());
230 return *this;
231 }
232
233 void
234 Ssl::CertValidationMsg::CertItem::setCert(X509 *aCert)
235 {
236 cert.resetAndLock(aCert);
237 }
238
239 const std::string Ssl::CertValidationMsg::code_cert_validate("cert_validate");
240 const std::string Ssl::CertValidationMsg::param_domain("domain");
241 const std::string Ssl::CertValidationMsg::param_cert("cert_");
242 const std::string Ssl::CertValidationMsg::param_error_name("error_name_");
243 const std::string Ssl::CertValidationMsg::param_error_reason("error_reason_");
244 const std::string Ssl::CertValidationMsg::param_error_cert("error_cert_");
245 const std::string Ssl::CertValidationMsg::param_error_depth("error_depth_");
246 const std::string Ssl::CertValidationMsg::param_proto_version("proto_version");
247 const std::string Ssl::CertValidationMsg::param_cipher("cipher");
248