]>
git.ipfire.org Git - thirdparty/squid.git/blob - src/ssl/crtd_message.cc
2 #include "ssl/crtd_message.h"
3 #include "ssl/gadgets.h"
9 Ssl::CrtdMessage::CrtdMessage(MessageKind kind
)
10 : body_size(0), state(kind
== REPLY
? BEFORE_LENGTH
: BEFORE_CODE
)
13 Ssl::CrtdMessage::ParseResult
Ssl::CrtdMessage::parse(const char * buffer
, size_t len
)
15 char const *current_pos
= buffer
;
16 while (current_pos
!= buffer
+ len
&& state
!= END
) {
19 if (xisspace(*current_pos
)) {
23 if (xisalpha(*current_pos
)) {
31 if (xisalnum(*current_pos
) || *current_pos
== '_') {
32 current_block
+= *current_pos
;
36 if (xisspace(*current_pos
)) {
38 current_block
.clear();
39 state
= BEFORE_LENGTH
;
46 if (xisspace(*current_pos
)) {
50 if (xisdigit(*current_pos
)) {
58 if (xisdigit(*current_pos
)) {
59 current_block
+= *current_pos
;
63 if (xisspace(*current_pos
)) {
64 body_size
= atoi(current_block
.c_str());
65 current_block
.clear();
77 if (xisspace(*current_pos
)) {
86 size_t body_len
= (static_cast<size_t>(buffer
+ len
- current_pos
) >= body_size
- current_block
.length())
87 ? body_size
- current_block
.length()
88 : static_cast<size_t>(buffer
+ len
- current_pos
);
89 current_block
+= std::string(current_pos
, body_len
);
90 current_pos
+= body_len
;
91 if (current_block
.length() == body_size
) {
95 if (current_block
.length() > body_size
) {
106 if (state
!= END
) return INCOMPLETE
;
110 std::string
const & Ssl::CrtdMessage::getBody() const { return body
; }
112 std::string
const & Ssl::CrtdMessage::getCode() const { return code
; }
114 void Ssl::CrtdMessage::setBody(std::string
const & aBody
) { body
= aBody
; }
116 void Ssl::CrtdMessage::setCode(std::string
const & aCode
) { code
= aCode
; }
118 std::string
Ssl::CrtdMessage::compose() const
120 if (code
.empty()) return std::string();
122 snprintf(buffer
, sizeof(buffer
), "%zd", body
.length());
123 return code
+ ' ' + buffer
+ ' ' + body
;
126 void Ssl::CrtdMessage::clear()
132 current_block
.clear();
135 void Ssl::CrtdMessage::parseBody(CrtdMessage::BodyParams
& map
, std::string
& other_part
) const
138 // Copy string for using it as temp buffer.
139 std::string
temp_body(body
.c_str(), body
.length());
140 char * buffer
= const_cast<char *>(temp_body
.c_str());
141 char * token
= strtok(buffer
, "\r\n");
142 while (token
!= NULL
) {
143 std::string
current_string(token
);
144 size_t equal_pos
= current_string
.find('=');
145 if (equal_pos
== std::string::npos
) {
146 size_t offset_body_part
= token
- temp_body
.c_str();
147 other_part
= std::string(body
.c_str() + offset_body_part
, body
.length() - offset_body_part
);
150 std::string
param(current_string
.c_str(), current_string
.c_str() + equal_pos
);
151 std::string
value(current_string
.c_str() + equal_pos
+ 1);
152 map
.insert(std::make_pair(param
, value
));
154 token
= strtok(NULL
, "\r\n");
158 void Ssl::CrtdMessage::composeBody(CrtdMessage::BodyParams
const & map
, std::string
const & other_part
)
161 for (BodyParams::const_iterator i
= map
.begin(); i
!= map
.end(); ++i
) {
162 if (i
!= map
.begin())
164 body
+= i
->first
+ "=" + i
->second
;
166 if (!other_part
.empty())
167 body
+= '\n' + other_part
;
170 bool Ssl::CrtdMessage::parseRequest(Ssl::CertificateProperties
&certProperties
, std::string
&error
)
172 Ssl::CrtdMessage::BodyParams map
;
173 std::string certs_part
;
174 parseBody(map
, certs_part
);
175 Ssl::CrtdMessage::BodyParams::iterator i
= map
.find(Ssl::CrtdMessage::param_host
);
176 if (i
== map
.end()) {
177 error
= "Cannot find \"host\" parameter in request message";
180 certProperties
.commonName
= i
->second
;
182 i
= map
.find(Ssl::CrtdMessage::param_SetValidAfter
);
183 if (i
!= map
.end() && strcasecmp(i
->second
.c_str(), "on") == 0)
184 certProperties
.setValidAfter
= true;
186 i
= map
.find(Ssl::CrtdMessage::param_SetValidBefore
);
187 if (i
!= map
.end() && strcasecmp(i
->second
.c_str(), "on") == 0)
188 certProperties
.setValidBefore
= true;
190 i
= map
.find(Ssl::CrtdMessage::param_SetCommonName
);
191 if (i
!= map
.end()) {
192 // use this as Common Name instead of the hostname
193 // defined with host or Common Name from mimic cert
194 certProperties
.commonName
= i
->second
;
195 certProperties
.setCommonName
= true;
198 i
= map
.find(Ssl::CrtdMessage::param_Sign
);
199 if (i
!= map
.end()) {
200 if ((certProperties
.signAlgorithm
= Ssl::certSignAlgorithmId(i
->second
.c_str())) == Ssl::algSignEnd
) {
201 error
= "Wrong signing algoritm: " + i
->second
;
205 certProperties
.signAlgorithm
= Ssl::algSignTrusted
;
207 if (!Ssl::readCertAndPrivateKeyFromMemory(certProperties
.signWithX509
, certProperties
.signWithPkey
, certs_part
.c_str())) {
208 error
= "Broken signing certificate!";
212 static const std::string
CERT_BEGIN_STR("-----BEGIN CERTIFICATE");
214 if ((pos
= certs_part
.find(CERT_BEGIN_STR
)) != std::string::npos
) {
215 pos
+= CERT_BEGIN_STR
.length();
216 if ((pos
= certs_part
.find(CERT_BEGIN_STR
, pos
)) != std::string::npos
)
217 Ssl::readCertFromMemory(certProperties
.mimicCert
, certs_part
.c_str() + pos
);
222 void Ssl::CrtdMessage::composeRequest(Ssl::CertificateProperties
const &certProperties
)
225 body
= Ssl::CrtdMessage::param_host
+ "=" + certProperties
.commonName
;
226 if (certProperties
.setCommonName
)
227 body
+= "\n" + Ssl::CrtdMessage::param_SetCommonName
+ "=" + certProperties
.commonName
;
228 if (certProperties
.setValidAfter
)
229 body
+= "\n" + Ssl::CrtdMessage::param_SetValidAfter
+ "=on";
230 if (certProperties
.setValidBefore
)
231 body
+= "\n" + Ssl::CrtdMessage::param_SetValidBefore
+ "=on";
232 if (certProperties
.signAlgorithm
!= Ssl::algSignEnd
)
233 body
+= "\n" + Ssl::CrtdMessage::param_Sign
+ "=" + certSignAlgorithm(certProperties
.signAlgorithm
);
235 std::string certsPart
;
236 if (!Ssl::writeCertAndPrivateKeyToMemory(certProperties
.signWithX509
, certProperties
.signWithPkey
, certsPart
))
237 throw std::runtime_error("Ssl::writeCertAndPrivateKeyToMemory()");
238 if (certProperties
.mimicCert
.get()) {
239 if (!Ssl::appendCertToMemory(certProperties
.mimicCert
, certsPart
))
240 throw std::runtime_error("Ssl::appendCertToMemory()");
242 body
+= "\n" + certsPart
;
245 const std::string
Ssl::CrtdMessage::code_new_certificate("new_certificate");
246 const std::string
Ssl::CrtdMessage::param_host("host");
247 const std::string
Ssl::CrtdMessage::param_SetValidAfter(Ssl::CertAdaptAlgorithmStr
[algSetValidAfter
]);
248 const std::string
Ssl::CrtdMessage::param_SetValidBefore(Ssl::CertAdaptAlgorithmStr
[algSetValidBefore
]);
249 const std::string
Ssl::CrtdMessage::param_SetCommonName(Ssl::CertAdaptAlgorithmStr
[algSetCommonName
]);
250 const std::string
Ssl::CrtdMessage::param_Sign("Sign");