]>
git.ipfire.org Git - thirdparty/squid.git/blob - src/ssl/cert_validate_message.cc
2 #include "acl/FilledChecklist.h"
3 #include "ssl/support.h"
4 #include "ssl/cert_validate_message.h"
5 #include "ssl/ErrorDetail.h"
8 Ssl::CertValidationMsg::composeRequest(CertValidationRequest
const &vcert
)
11 body
+= Ssl::CertValidationMsg::param_host
+ "=" + vcert
.domainName
;
13 body
+= "\n" + Ssl::CertValidationMsg::param_error
+ "=";
15 for (const Ssl::Errors
*err
= vcert
.errors
; err
; err
= err
->next
) {
18 body
+= GetErrorName(err
->element
);
23 STACK_OF(X509
) *peerCerts
= SSL_get_peer_cert_chain(vcert
.ssl
);
26 Ssl::BIO_Pointer
bio(BIO_new(BIO_s_mem()));
27 for (int i
= 0; i
< sk_X509_num(peerCerts
); ++i
) {
28 X509
*cert
= sk_X509_value(peerCerts
, i
);
29 PEM_write_bio_X509(bio
.get(), cert
);
30 body
= body
+ "cert_" + xitoa(i
) + "=";
32 long len
= BIO_get_mem_data(bio
.get(), &ptr
);
33 body
.append(ptr
, len
);
34 // Normally openssl toolkit terminates Certificate with a '\n'.
35 if (ptr
[len
-1] != '\n')
37 if (!BIO_reset(bio
.get())) {
45 get_error_id(const char *label
, size_t len
)
47 const char *e
= label
+ len
-1;
48 while (e
!= label
&& xisdigit(*e
)) --e
;
50 return strtol(e
, 0 , 10);
54 Ssl::CertValidationMsg::parseResponse(CertValidationResponse
&resp
, STACK_OF(X509
) *peerCerts
, std::string
&error
)
56 std::vector
<CertItem
> certs
;
58 const char *param
= body
.c_str();
60 while (xisspace(*param
)) param
++;
64 size_t param_len
= strcspn(param
, "=\r\n");
65 if (param
[param_len
] != '=') {
66 debugs(83, DBG_IMPORTANT
, "WARNING: cert validator response parse error: " << param
);
69 const char *value
=param
+param_len
+1;
71 if (param_len
> param_cert
.length() &&
72 strncmp(param
, param_cert
.c_str(), param_cert
.length()) == 0) {
74 ci
.name
.assign(param
, param_len
);
76 readCertFromMemory(x509
, value
);
77 ci
.setCert(x509
.get());
80 const char *b
= strstr(value
, "-----END CERTIFICATE-----");
82 debugs(83, DBG_IMPORTANT
, "WARNING: cert Validator response parse error: Failed to find certificate boundary " << value
);
85 b
+= strlen("-----END CERTIFICATE-----");
90 size_t value_len
= strcspn(value
, "\r\n");
91 std::string
v(value
, value_len
);
93 debugs(83, 5, "Returned value: " << std::string(param
, param_len
).c_str() << ": " <<
96 int errorId
= get_error_id(param
, param_len
);
97 Ssl::CertValidationResponse::RecvdError
¤tItem
= resp
.getError(errorId
);
99 if (param_len
> param_error_name
.length() &&
100 strncmp(param
, param_error_name
.c_str(), param_error_name
.length()) == 0) {
101 currentItem
.error_no
= Ssl::GetErrorCode(v
.c_str());
102 if (currentItem
.error_no
== SSL_ERROR_NONE
) {
103 debugs(83, DBG_IMPORTANT
, "WARNING: cert validator response parse error: Unknown SSL Error: " << v
);
106 } else if (param_len
> param_error_reason
.length() &&
107 strncmp(param
, param_error_reason
.c_str(), param_error_reason
.length()) == 0) {
108 currentItem
.error_reason
= v
;
109 } else if (param_len
> param_error_cert
.length() &&
110 strncmp(param
, param_error_cert
.c_str(), param_error_cert
.length()) == 0) {
112 if (X509
*cert
= getCertByName(certs
, v
)) {
113 debugs(83, 6, "The certificate with id \"" << v
<< "\" found.");
114 currentItem
.setCert(cert
);
116 //In this case we assume that the certID is one of the certificates sent
117 // to cert validator. The certificates sent to cert validator have names in
118 // form "cert_xx" where the "xx" is an integer represents the position of
119 // the certificate inside peer certificates list.
120 const int certId
= get_error_id(v
.c_str(), v
.length());
121 debugs(83, 6, "Cert index in peer certificates list:" << certId
);
122 //if certId is not correct sk_X509_value returns NULL
123 currentItem
.setCert(sk_X509_value(peerCerts
, certId
));
126 debugs(83, DBG_IMPORTANT
, "WARNING: cert validator response parse error: Unknown parameter name " << std::string(param
, param_len
).c_str());
130 param
= value
+ value_len
+1;
133 /*Run through parsed errors to check for errors*/
134 typedef Ssl::CertValidationResponse::RecvdErrors::const_iterator SVCRECI
;
135 for (SVCRECI i
= resp
.errors
.begin(); i
!= resp
.errors
.end(); ++i
) {
136 if (i
->error_no
== SSL_ERROR_NONE
) {
137 debugs(83, DBG_IMPORTANT
, "WARNING: cert validator incomplete response: Missing error name from error_id: " << i
->id
);
146 Ssl::CertValidationMsg::getCertByName(std::vector
<CertItem
> const &certs
, std::string
const & name
)
148 typedef std::vector
<CertItem
>::const_iterator SVCI
;
149 for (SVCI ci
= certs
.begin(); ci
!= certs
.end(); ++ci
) {
150 if (ci
->name
.compare(name
) == 0)
151 return ci
->cert
.get();
156 Ssl::CertValidationResponse::RecvdError
&
157 Ssl::CertValidationResponse::getError(int errorId
)
159 typedef Ssl::CertValidationResponse::RecvdErrors::iterator SVCREI
;
160 for (SVCREI i
= errors
.begin(); i
!= errors
.end(); ++i
) {
161 if (i
->id
== errorId
)
164 Ssl::CertValidationResponse::RecvdError errItem
;
165 errItem
.id
= errorId
;
166 errors
.push_back(errItem
);
167 return errors
.back();
170 Ssl::CertValidationResponse::RecvdError::RecvdError(const RecvdError
&old
)
173 error_no
= old
.error_no
;
174 error_reason
= old
.error_reason
;
175 setCert(old
.cert
.get());
178 Ssl::CertValidationResponse::RecvdError
& Ssl::CertValidationResponse::RecvdError::operator = (const RecvdError
&old
)
181 error_no
= old
.error_no
;
182 error_reason
= old
.error_reason
;
183 setCert(old
.cert
.get());
188 Ssl::CertValidationResponse::RecvdError::setCert(X509
*aCert
)
190 cert
.resetAndLock(aCert
);
193 Ssl::CertValidationMsg::CertItem::CertItem(const CertItem
&old
)
196 setCert(old
.cert
.get());
199 Ssl::CertValidationMsg::CertItem
& Ssl::CertValidationMsg::CertItem::operator = (const CertItem
&old
)
202 setCert(old
.cert
.get());
207 Ssl::CertValidationMsg::CertItem::setCert(X509
*aCert
)
209 cert
.resetAndLock(aCert
);
212 const std::string
Ssl::CertValidationMsg::code_cert_validate("cert_validate");
213 const std::string
Ssl::CertValidationMsg::param_domain("domain");
214 const std::string
Ssl::CertValidationMsg::param_error("errors");
215 const std::string
Ssl::CertValidationMsg::param_cert("cert_");
216 const std::string
Ssl::CertValidationMsg::param_error_name("error_name_");
217 const std::string
Ssl::CertValidationMsg::param_error_reason("error_reason_");
218 const std::string
Ssl::CertValidationMsg::param_error_cert("error_cert_");