]>
git.ipfire.org Git - thirdparty/squid.git/blob - src/ssl/crtd_message.cc
6 #include "ssl/gadgets.h"
7 #include "ssl/crtd_message.h"
18 Ssl::CrtdMessage::CrtdMessage()
19 : body_size(0), state(BEFORE_CODE
)
22 Ssl::CrtdMessage::ParseResult
Ssl::CrtdMessage::parse(const char * buffer
, size_t len
)
24 char const *current_pos
= buffer
;
25 while (current_pos
!= buffer
+ len
&& state
!= END
) {
28 if (xisspace(*current_pos
)) {
32 if (xisalpha(*current_pos
)) {
40 if (xisalnum(*current_pos
) || *current_pos
== '_') {
41 current_block
+= *current_pos
;
45 if (xisspace(*current_pos
)) {
47 current_block
.clear();
48 state
= BEFORE_LENGTH
;
55 if (xisspace(*current_pos
)) {
59 if (xisdigit(*current_pos
)) {
67 if (xisdigit(*current_pos
)) {
68 current_block
+= *current_pos
;
72 if (xisspace(*current_pos
)) {
73 body_size
= atoi(current_block
.c_str());
74 current_block
.clear();
86 if (xisspace(*current_pos
)) {
95 size_t body_len
= (static_cast<size_t>(buffer
+ len
- current_pos
) >= body_size
- current_block
.length())
96 ? body_size
- current_block
.length()
97 : static_cast<size_t>(buffer
+ len
- current_pos
);
98 current_block
+= std::string(current_pos
, body_len
);
99 current_pos
+= body_len
;
100 if (current_block
.length() == body_size
) {
101 body
= current_block
;
104 if (current_block
.length() > body_size
) {
115 if (state
!= END
) return INCOMPLETE
;
119 std::string
const & Ssl::CrtdMessage::getBody() const { return body
; }
121 std::string
const & Ssl::CrtdMessage::getCode() const { return code
; }
123 void Ssl::CrtdMessage::setBody(std::string
const & aBody
) { body
= aBody
; }
125 void Ssl::CrtdMessage::setCode(std::string
const & aCode
) { code
= aCode
; }
127 std::string
Ssl::CrtdMessage::compose() const
129 if (code
.empty()) return std::string();
131 snprintf(buffer
, sizeof(buffer
), "%zd", body
.length());
132 return code
+ ' ' + buffer
+ ' ' + body
;
135 void Ssl::CrtdMessage::clear()
141 current_block
.clear();
144 void Ssl::CrtdMessage::parseBody(CrtdMessage::BodyParams
& map
, std::string
& other_part
) const
147 // Copy string for using it as temp buffer.
148 std::string
temp_body(body
.c_str(), body
.length());
149 char * buffer
= const_cast<char *>(temp_body
.c_str());
150 char * token
= strtok(buffer
, "\r\n");
151 while (token
!= NULL
) {
152 std::string
current_string(token
);
153 size_t equal_pos
= current_string
.find('=');
154 if (equal_pos
== std::string::npos
) {
155 size_t offset_body_part
= token
- temp_body
.c_str();
156 other_part
= std::string(body
.c_str() + offset_body_part
, body
.length() - offset_body_part
);
159 std::string
param(current_string
.c_str(), current_string
.c_str() + equal_pos
);
160 std::string
value(current_string
.c_str() + equal_pos
+ 1);
161 map
.insert(std::make_pair(param
, value
));
163 token
= strtok(NULL
, "\r\n");
167 void Ssl::CrtdMessage::composeBody(CrtdMessage::BodyParams
const & map
, std::string
const & other_part
)
170 for (BodyParams::const_iterator i
= map
.begin(); i
!= map
.end(); ++i
) {
171 if (i
!= map
.begin())
173 body
+= i
->first
+ "=" + i
->second
;
175 if (!other_part
.empty())
176 body
+= '\n' + other_part
;
179 bool Ssl::CrtdMessage::parseRequest(Ssl::CertificateProperties
&certProperties
, std::string
&error
)
181 Ssl::CrtdMessage::BodyParams map
;
182 std::string certs_part
;
183 parseBody(map
, certs_part
);
184 Ssl::CrtdMessage::BodyParams::iterator i
= map
.find(Ssl::CrtdMessage::param_host
);
185 if (i
== map
.end()) {
186 error
= "Cannot find \"host\" parameter in request message";
189 certProperties
.commonName
= i
->second
;
191 i
= map
.find(Ssl::CrtdMessage::param_SetValidAfter
);
192 if (i
!= map
.end() && strcasecmp(i
->second
.c_str(), "on") == 0)
193 certProperties
.setValidAfter
= true;
195 i
= map
.find(Ssl::CrtdMessage::param_SetValidBefore
);
196 if (i
!= map
.end() && strcasecmp(i
->second
.c_str(), "on") == 0)
197 certProperties
.setValidBefore
= true;
199 i
= map
.find(Ssl::CrtdMessage::param_SetCommonName
);
200 if (i
!= map
.end()) {
201 // use this as Common Name instead of the hostname
202 // defined with host or Common Name from mimic cert
203 certProperties
.commonName
= i
->second
;
204 certProperties
.setCommonName
= true;
207 i
= map
.find(Ssl::CrtdMessage::param_Sign
);
208 if (i
!= map
.end()) {
209 if ((certProperties
.signAlgorithm
= Ssl::certSignAlgorithmId(i
->second
.c_str())) == Ssl::algSignEnd
) {
210 error
= "Wrong signing algoritm: " + i
->second
;
214 certProperties
.signAlgorithm
= Ssl::algSignTrusted
;
216 if (!Ssl::readCertAndPrivateKeyFromMemory(certProperties
.signWithX509
, certProperties
.signWithPkey
, certs_part
.c_str())) {
217 error
= "Broken signing certificate!";
221 static const std::string
CERT_BEGIN_STR("-----BEGIN CERTIFICATE");
223 if ((pos
= certs_part
.find(CERT_BEGIN_STR
)) != std::string::npos
) {
224 pos
+= CERT_BEGIN_STR
.length();
225 if ((pos
= certs_part
.find(CERT_BEGIN_STR
, pos
)) != std::string::npos
)
226 Ssl::readCertFromMemory(certProperties
.mimicCert
, certs_part
.c_str() + pos
);
231 void Ssl::CrtdMessage::composeRequest(Ssl::CertificateProperties
const &certProperties
)
234 body
= Ssl::CrtdMessage::param_host
+ "=" + certProperties
.commonName
;
235 if (certProperties
.setCommonName
)
236 body
+= "\n" + Ssl::CrtdMessage::param_SetCommonName
+ "=" + certProperties
.commonName
;
237 if (certProperties
.setValidAfter
)
238 body
+= "\n" + Ssl::CrtdMessage::param_SetValidAfter
+ "=on";
239 if (certProperties
.setValidBefore
)
240 body
+= "\n" + Ssl::CrtdMessage::param_SetValidBefore
+ "=on";
241 if (certProperties
.signAlgorithm
!= Ssl::algSignEnd
)
242 body
+= "\n" + Ssl::CrtdMessage::param_Sign
+ "=" + certSignAlgorithm(certProperties
.signAlgorithm
);
244 std::string certsPart
;
245 if (!Ssl::writeCertAndPrivateKeyToMemory(certProperties
.signWithX509
, certProperties
.signWithPkey
, certsPart
))
246 throw std::runtime_error("Ssl::writeCertAndPrivateKeyToMemory()");
247 if (certProperties
.mimicCert
.get()) {
248 if (!Ssl::appendCertToMemory(certProperties
.mimicCert
, certsPart
))
249 throw std::runtime_error("Ssl::appendCertToMemory()");
251 body
+= "\n" + certsPart
;
254 const std::string
Ssl::CrtdMessage::code_new_certificate("new_certificate");
255 const std::string
Ssl::CrtdMessage::param_host("host");
256 const std::string
Ssl::CrtdMessage::param_SetValidAfter(Ssl::CertAdaptAlgorithmStr
[algSetValidAfter
]);
257 const std::string
Ssl::CrtdMessage::param_SetValidBefore(Ssl::CertAdaptAlgorithmStr
[algSetValidBefore
]);
258 const std::string
Ssl::CrtdMessage::param_SetCommonName(Ssl::CertAdaptAlgorithmStr
[algSetCommonName
]);
259 const std::string
Ssl::CrtdMessage::param_Sign("Sign");