]>
git.ipfire.org Git - thirdparty/squid.git/blob - src/ssl/crtd_message.cc
2 #include "ssl/crtd_message.h"
3 #include "ssl/gadgets.h"
14 Ssl::CrtdMessage::CrtdMessage(MessageKind kind
)
15 : body_size(0), state(kind
== REPLY
? BEFORE_LENGTH
: BEFORE_CODE
)
18 Ssl::CrtdMessage::ParseResult
Ssl::CrtdMessage::parse(const char * buffer
, size_t len
)
20 char const *current_pos
= buffer
;
21 while (current_pos
!= buffer
+ len
&& state
!= END
) {
24 if (xisspace(*current_pos
)) {
28 if (xisalpha(*current_pos
)) {
36 if (xisalnum(*current_pos
) || *current_pos
== '_') {
37 current_block
+= *current_pos
;
41 if (xisspace(*current_pos
)) {
43 current_block
.clear();
44 state
= BEFORE_LENGTH
;
51 if (xisspace(*current_pos
)) {
55 if (xisdigit(*current_pos
)) {
63 if (xisdigit(*current_pos
)) {
64 current_block
+= *current_pos
;
68 if (xisspace(*current_pos
)) {
69 body_size
= atoi(current_block
.c_str());
70 current_block
.clear();
82 if (xisspace(*current_pos
)) {
91 size_t body_len
= (static_cast<size_t>(buffer
+ len
- current_pos
) >= body_size
- current_block
.length())
92 ? body_size
- current_block
.length()
93 : static_cast<size_t>(buffer
+ len
- current_pos
);
94 current_block
+= std::string(current_pos
, body_len
);
95 current_pos
+= body_len
;
96 if (current_block
.length() == body_size
) {
100 if (current_block
.length() > body_size
) {
111 if (state
!= END
) return INCOMPLETE
;
115 std::string
const & Ssl::CrtdMessage::getBody() const { return body
; }
117 std::string
const & Ssl::CrtdMessage::getCode() const { return code
; }
119 void Ssl::CrtdMessage::setBody(std::string
const & aBody
) { body
= aBody
; }
121 void Ssl::CrtdMessage::setCode(std::string
const & aCode
) { code
= aCode
; }
123 std::string
Ssl::CrtdMessage::compose() const
125 if (code
.empty()) return std::string();
127 snprintf(buffer
, sizeof(buffer
), "%zd", body
.length());
128 return code
+ ' ' + buffer
+ ' ' + body
;
131 void Ssl::CrtdMessage::clear()
137 current_block
.clear();
140 void Ssl::CrtdMessage::parseBody(CrtdMessage::BodyParams
& map
, std::string
& other_part
) const
143 // Copy string for using it as temp buffer.
144 std::string
temp_body(body
.c_str(), body
.length());
145 char * buffer
= const_cast<char *>(temp_body
.c_str());
146 char * token
= strtok(buffer
, "\r\n");
147 while (token
!= NULL
) {
148 std::string
current_string(token
);
149 size_t equal_pos
= current_string
.find('=');
150 if (equal_pos
== std::string::npos
) {
151 size_t offset_body_part
= token
- temp_body
.c_str();
152 other_part
= std::string(body
.c_str() + offset_body_part
, body
.length() - offset_body_part
);
155 std::string
param(current_string
.c_str(), current_string
.c_str() + equal_pos
);
156 std::string
value(current_string
.c_str() + equal_pos
+ 1);
157 map
.insert(std::make_pair(param
, value
));
159 token
= strtok(NULL
, "\r\n");
163 void Ssl::CrtdMessage::composeBody(CrtdMessage::BodyParams
const & map
, std::string
const & other_part
)
166 for (BodyParams::const_iterator i
= map
.begin(); i
!= map
.end(); ++i
) {
167 if (i
!= map
.begin())
169 body
+= i
->first
+ "=" + i
->second
;
171 if (!other_part
.empty())
172 body
+= '\n' + other_part
;
175 bool Ssl::CrtdMessage::parseRequest(Ssl::CertificateProperties
&certProperties
, std::string
&error
)
177 Ssl::CrtdMessage::BodyParams map
;
178 std::string certs_part
;
179 parseBody(map
, certs_part
);
180 Ssl::CrtdMessage::BodyParams::iterator i
= map
.find(Ssl::CrtdMessage::param_host
);
181 if (i
== map
.end()) {
182 error
= "Cannot find \"host\" parameter in request message";
185 certProperties
.commonName
= i
->second
;
187 i
= map
.find(Ssl::CrtdMessage::param_SetValidAfter
);
188 if (i
!= map
.end() && strcasecmp(i
->second
.c_str(), "on") == 0)
189 certProperties
.setValidAfter
= true;
191 i
= map
.find(Ssl::CrtdMessage::param_SetValidBefore
);
192 if (i
!= map
.end() && strcasecmp(i
->second
.c_str(), "on") == 0)
193 certProperties
.setValidBefore
= true;
195 i
= map
.find(Ssl::CrtdMessage::param_SetCommonName
);
196 if (i
!= map
.end()) {
197 // use this as Common Name instead of the hostname
198 // defined with host or Common Name from mimic cert
199 certProperties
.commonName
= i
->second
;
200 certProperties
.setCommonName
= true;
203 i
= map
.find(Ssl::CrtdMessage::param_Sign
);
204 if (i
!= map
.end()) {
205 if ((certProperties
.signAlgorithm
= Ssl::certSignAlgorithmId(i
->second
.c_str())) == Ssl::algSignEnd
) {
206 error
= "Wrong signing algoritm: " + i
->second
;
210 certProperties
.signAlgorithm
= Ssl::algSignTrusted
;
212 if (!Ssl::readCertAndPrivateKeyFromMemory(certProperties
.signWithX509
, certProperties
.signWithPkey
, certs_part
.c_str())) {
213 error
= "Broken signing certificate!";
217 static const std::string
CERT_BEGIN_STR("-----BEGIN CERTIFICATE");
219 if ((pos
= certs_part
.find(CERT_BEGIN_STR
)) != std::string::npos
) {
220 pos
+= CERT_BEGIN_STR
.length();
221 if ((pos
= certs_part
.find(CERT_BEGIN_STR
, pos
)) != std::string::npos
)
222 Ssl::readCertFromMemory(certProperties
.mimicCert
, certs_part
.c_str() + pos
);
227 void Ssl::CrtdMessage::composeRequest(Ssl::CertificateProperties
const &certProperties
)
230 body
= Ssl::CrtdMessage::param_host
+ "=" + certProperties
.commonName
;
231 if (certProperties
.setCommonName
)
232 body
+= "\n" + Ssl::CrtdMessage::param_SetCommonName
+ "=" + certProperties
.commonName
;
233 if (certProperties
.setValidAfter
)
234 body
+= "\n" + Ssl::CrtdMessage::param_SetValidAfter
+ "=on";
235 if (certProperties
.setValidBefore
)
236 body
+= "\n" + Ssl::CrtdMessage::param_SetValidBefore
+ "=on";
237 if (certProperties
.signAlgorithm
!= Ssl::algSignEnd
)
238 body
+= "\n" + Ssl::CrtdMessage::param_Sign
+ "=" + certSignAlgorithm(certProperties
.signAlgorithm
);
240 std::string certsPart
;
241 if (!Ssl::writeCertAndPrivateKeyToMemory(certProperties
.signWithX509
, certProperties
.signWithPkey
, certsPart
))
242 throw std::runtime_error("Ssl::writeCertAndPrivateKeyToMemory()");
243 if (certProperties
.mimicCert
.get()) {
244 if (!Ssl::appendCertToMemory(certProperties
.mimicCert
, certsPart
))
245 throw std::runtime_error("Ssl::appendCertToMemory()");
247 body
+= "\n" + certsPart
;
250 const std::string
Ssl::CrtdMessage::code_new_certificate("new_certificate");
251 const std::string
Ssl::CrtdMessage::param_host("host");
252 const std::string
Ssl::CrtdMessage::param_SetValidAfter(Ssl::CertAdaptAlgorithmStr
[algSetValidAfter
]);
253 const std::string
Ssl::CrtdMessage::param_SetValidBefore(Ssl::CertAdaptAlgorithmStr
[algSetValidBefore
]);
254 const std::string
Ssl::CrtdMessage::param_SetCommonName(Ssl::CertAdaptAlgorithmStr
[algSetCommonName
]);
255 const std::string
Ssl::CrtdMessage::param_Sign("Sign");