2 * Copyright (C) 1996-2015 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.
10 #include "acl/FilledChecklist.h"
13 #include "ssl/cert_validate_message.h"
14 #include "ssl/ErrorDetail.h"
15 #include "ssl/support.h"
19 Ssl::CertValidationMsg::composeRequest(CertValidationRequest
const &vcert
)
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
));
25 if (const char *sslVersion
= SSL_get_version(vcert
.ssl
))
26 body
+= "\n" + Ssl::CertValidationMsg::param_proto_version
+ "=" + sslVersion
;
28 if (const char *cipherName
= SSL_CIPHER_get_name(SSL_get_current_cipher(vcert
.ssl
)))
29 body
+= "\n" + Ssl::CertValidationMsg::param_cipher
+ "=" + cipherName
;
32 peerCerts
= SSL_get_peer_cert_chain(vcert
.ssl
);
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
) + "=";
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())) {
51 for (const Ssl::CertErrors
*err
= vcert
.errors
; err
; err
= err
->next
, ++i
) {
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");
61 body
+= param_error_cert
+ xitoa(i
) + "=";
62 body
+= param_cert
+ xitoa((errorCertPos
>= 0 ? errorCertPos
: 0));
68 get_error_id(const char *label
, size_t len
)
70 const char *e
= label
+ len
-1;
71 while (e
!= label
&& xisdigit(*e
)) --e
;
73 return strtol(e
, 0 , 10);
77 Ssl::CertValidationMsg::parseResponse(CertValidationResponse
&resp
, STACK_OF(X509
) *peerCerts
, std::string
&error
)
79 std::vector
<CertItem
> certs
;
81 const char *param
= body
.c_str();
83 while (xisspace(*param
)) param
++;
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
);
92 const char *value
=param
+param_len
+1;
94 if (param_len
> param_cert
.length() &&
95 strncmp(param
, param_cert
.c_str(), param_cert
.length()) == 0) {
97 ci
.name
.assign(param
, param_len
);
99 readCertFromMemory(x509
, value
);
100 ci
.setCert(x509
.get());
103 const char *b
= strstr(value
, "-----END CERTIFICATE-----");
105 debugs(83, DBG_IMPORTANT
, "WARNING: cert Validator response parse error: Failed to find certificate boundary " << value
);
108 b
+= strlen("-----END CERTIFICATE-----");
113 size_t value_len
= strcspn(value
, "\r\n");
114 std::string
v(value
, value_len
);
116 debugs(83, 5, "Returned value: " << std::string(param
, param_len
).c_str() << ": " <<
119 int errorId
= get_error_id(param
, param_len
);
120 Ssl::CertValidationResponse::RecvdError
¤tItem
= resp
.getError(errorId
);
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
);
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) {
135 if (X509
*cert
= getCertByName(certs
, v
)) {
136 debugs(83, 6, "The certificate with id \"" << v
<< "\" found.");
137 currentItem
.setCert(cert
);
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
));
149 debugs(83, DBG_IMPORTANT
, "WARNING: cert validator response parse error: Unknown parameter name " << std::string(param
, param_len
).c_str());
153 param
= value
+ value_len
+1;
156 /*Run through parsed errors to check for errors*/
157 typedef Ssl::CertValidationResponse::RecvdErrors::const_iterator SVCRECI
;
158 for (SVCRECI i
= resp
.errors
.begin(); i
!= resp
.errors
.end(); ++i
) {
159 if (i
->error_no
== SSL_ERROR_NONE
) {
160 debugs(83, DBG_IMPORTANT
, "WARNING: cert validator incomplete response: Missing error name from error_id: " << i
->id
);
169 Ssl::CertValidationMsg::getCertByName(std::vector
<CertItem
> const &certs
, std::string
const & name
)
171 typedef std::vector
<CertItem
>::const_iterator SVCI
;
172 for (SVCI ci
= certs
.begin(); ci
!= certs
.end(); ++ci
) {
173 if (ci
->name
.compare(name
) == 0)
174 return ci
->cert
.get();
179 Ssl::CertValidationResponse::RecvdError
&
180 Ssl::CertValidationResponse::getError(int errorId
)
182 typedef Ssl::CertValidationResponse::RecvdErrors::iterator SVCREI
;
183 for (SVCREI i
= errors
.begin(); i
!= errors
.end(); ++i
) {
184 if (i
->id
== errorId
)
187 Ssl::CertValidationResponse::RecvdError errItem
;
188 errItem
.id
= errorId
;
189 errors
.push_back(errItem
);
190 return errors
.back();
193 Ssl::CertValidationResponse::RecvdError::RecvdError(const RecvdError
&old
)
196 error_no
= old
.error_no
;
197 error_reason
= old
.error_reason
;
198 setCert(old
.cert
.get());
201 Ssl::CertValidationResponse::RecvdError
& Ssl::CertValidationResponse::RecvdError::operator = (const RecvdError
&old
)
204 error_no
= old
.error_no
;
205 error_reason
= old
.error_reason
;
206 setCert(old
.cert
.get());
211 Ssl::CertValidationResponse::RecvdError::setCert(X509
*aCert
)
213 cert
.resetAndLock(aCert
);
216 Ssl::CertValidationMsg::CertItem::CertItem(const CertItem
&old
)
219 setCert(old
.cert
.get());
222 Ssl::CertValidationMsg::CertItem
& Ssl::CertValidationMsg::CertItem::operator = (const CertItem
&old
)
225 setCert(old
.cert
.get());
230 Ssl::CertValidationMsg::CertItem::setCert(X509
*aCert
)
232 cert
.resetAndLock(aCert
);
235 const std::string
Ssl::CertValidationMsg::code_cert_validate("cert_validate");
236 const std::string
Ssl::CertValidationMsg::param_domain("domain");
237 const std::string
Ssl::CertValidationMsg::param_cert("cert_");
238 const std::string
Ssl::CertValidationMsg::param_error_name("error_name_");
239 const std::string
Ssl::CertValidationMsg::param_error_reason("error_reason_");
240 const std::string
Ssl::CertValidationMsg::param_error_cert("error_cert_");
241 const std::string
Ssl::CertValidationMsg::param_proto_version("proto_version");
242 const std::string
Ssl::CertValidationMsg::param_cipher("cipher");