forked from open-eid/libdigidocpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOCSP.cpp
More file actions
292 lines (263 loc) · 9.78 KB
/
Copy pathOCSP.cpp
File metadata and controls
292 lines (263 loc) · 9.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
/*
* libdigidocpp
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#include "OCSP.h"
#include "Conf.h"
#include "Container.h"
#include "crypto/Connect.h"
#include "crypto/OpenSSLHelpers.h"
#include "crypto/X509CertStore.h"
#include "util/DateTime.h"
#include "util/log.h"
#include <algorithm>
#include <array>
#ifdef WIN32 //hack for win32 build
#undef OCSP_REQUEST
#undef OCSP_RESPONSE
#endif
#include <openssl/ocsp.h>
#include <openssl/pem.h>
#include <openssl/rand.h>
#include <openssl/sha.h>
using namespace digidoc;
using namespace std;
/**
* Initialize OCSP certificate validator.
*/
OCSP::OCSP(const X509Cert &cert, const X509Cert &issuer, const std::string &userAgent)
: resp(nullptr, OCSP_RESPONSE_free)
, basic(nullptr, OCSP_BASICRESP_free)
{
if(!cert)
THROW("Can not check X.509 certificate, certificate is NULL pointer.");
if(!issuer)
THROW("Can not check X.509 certificate, issuer certificate is NULL pointer.");
string url = Conf::instance()->ocsp(cert.issuerName("CN"));
if(url.empty())
{
STACK_OF(OPENSSL_STRING) *urls = X509_get1_ocsp(cert.handle());
if(urls && sk_OPENSSL_STRING_num(urls) > 0)
url = sk_OPENSSL_STRING_value(urls, 0);
X509_email_free(urls);
}
DEBUG("OCSP url %s", url.c_str());
if(url.empty())
{
Exception e(EXCEPTION_PARAMS("Failed to find ocsp responder url."));
e.setCode(Exception::OCSPResponderMissing);
throw e;
}
OCSP_CERTID *certId = OCSP_cert_to_id(nullptr, cert.handle(), issuer.handle());
SCOPE(OCSP_REQUEST, req, OCSP_REQUEST_new());
if(!req)
THROW_OPENSSLEXCEPTION("Failed to create new OCSP request, out of memory?");
if(!OCSP_request_add0_id(req.get(), certId))
THROW_OPENSSLEXCEPTION("Failed to add certificate ID to OCSP request.");
if(!OCSP_request_add1_nonce(req.get(), nullptr, 32)) // rfc8954: SIZE(1..32)
THROW_OPENSSLEXCEPTION("Failed to add NONCE to OCSP request.");
Connect::Result result = Connect(url, "POST", 0, {}, userAgent).exec({
{"Content-Type", "application/ocsp-request"},
{"Accept", "application/ocsp-response"},
{"Connection", "Close"},
{"Cache-Control", "no-cache"}
}, i2d<i2d_OCSP_REQUEST>(req));
if(result.isForbidden())
THROW("OCSP service responded - Forbidden");
if(!result)
THROW("Failed to send OCSP request");
const auto *p2 = (const unsigned char*)result.content.c_str();
resp.reset(d2i_OCSP_RESPONSE(nullptr, &p2, long(result.content.size())));
switch(int respStatus = OCSP_response_status(resp.get()))
{
case OCSP_RESPONSE_STATUS_SUCCESSFUL: break;
case OCSP_RESPONSE_STATUS_UNAUTHORIZED:
{
Exception e(EXCEPTION_PARAMS("OCSP request failed"));
e.setCode(Exception::OCSPRequestUnauthorized);
throw e;
}
default:
THROW("OCSP request failed, response status: %s", OCSP_response_status_str(respStatus));
}
basic.reset(OCSP_response_get1_basic(resp.get()));
if(!basic)
THROW("Incorrect OCSP response.");
if(OCSP_check_nonce(req.get(), basic.get()) <= 0)
THROW("Incorrect NONCE field value.");
ASN1_GENERALIZEDTIME *thisUpdate {}, *nextUpdate {};
if(OCSP_resp_find_status(basic.get(), certId, nullptr, nullptr, nullptr, &thisUpdate, &nextUpdate) != 1)
THROW("Failed to find CERT_ID from OCSP response.");
DEBUG("OCSP producedAt: %s", util::date::to_string(producedAt()).c_str());
if(!OCSP_check_validity(thisUpdate, nextUpdate, 15*60, 2*60))
{
Exception e(EXCEPTION_PARAMS("OCSP response not in valid time slot."));
e.setCode(Exception::OCSPTimeSlot);
throw e;
}
}
OCSP::OCSP(const unsigned char *data, size_t size)
: resp(nullptr, OCSP_RESPONSE_free)
, basic(nullptr, OCSP_BASICRESP_free)
{
if(size == 0)
return;
resp.reset(d2i_OCSP_RESPONSE(nullptr, &data, long(size)));
if(resp)
basic.reset(OCSP_response_get1_basic(resp.get()));
}
bool OCSP::compareResponderCert(const X509Cert &cert) const
{
if(!basic || !cert)
return false;
const ASN1_OCTET_STRING *hash {};
const X509_NAME *name {};
OCSP_resp_get0_id(basic.get(), &hash, &name);
if(name)
return X509_NAME_cmp(X509_get_subject_name(cert.handle()), name) == 0;
if(hash)
{
std::array<unsigned char,SHA_DIGEST_LENGTH> sha1{};
ASN1_BIT_STRING *key = X509_get0_pubkey_bitstr(cert.handle());
SHA1(key->data, size_t(key->length), sha1.data());
return equal(sha1.cbegin(), sha1.cend(), hash->data, std::next(hash->data, hash->length));
}
return false;
}
X509Cert OCSP::responderCert() const
{
if(!basic)
return X509Cert();
const STACK_OF(X509) *certs = OCSP_resp_get0_certs(basic.get());
for(int i = 0; i < sk_X509_num(certs); ++i)
{
X509Cert cert(sk_X509_value(certs, i));
if(compareResponderCert(cert))
return cert;
}
for(const X509Cert &cert: X509CertStore::instance()->certs(X509CertStore::OCSP))
{
if(compareResponderCert(cert))
return cert;
}
return X509Cert();
}
OCSP::operator vector<unsigned char>() const
{
return i2d<i2d_OCSP_RESPONSE>(resp);
}
/**
* Check that response was signed with trusted OCSP certificate
*/
void OCSP::verifyResponse(const X509Cert &cert) const
{
if(!basic)
THROW("Failed to verify OCSP response.");
tm tm = producedAt();
// Some OCSP-s do not have certificates in response and stack is used for finding certificate for this
auto stack = make_unique_ptr(sk_X509_new_null(), [](auto *sk) { sk_X509_free(sk); });
for(const X509Cert &i: X509CertStore::instance()->certs(X509CertStore::OCSP))
{
if(compareResponderCert(i))
sk_X509_push(stack.get(), i.handle());
}
auto store = X509CertStore::createStore(X509CertStore::OCSP, tm);
if(OCSP_basic_verify(basic.get(), stack.get(), store.get(), OCSP_NOCHECKS | OCSP_PARTIAL_CHAIN) != 1)
{
unsigned long err = ERR_get_error();
if(ERR_GET_LIB(err) == ERR_LIB_OCSP &&
(ERR_GET_REASON(err) == OCSP_R_CERTIFICATE_VERIFY_ERROR ||
ERR_GET_REASON(err) == OCSP_R_SIGNER_CERTIFICATE_NOT_FOUND))
{
OpenSSLException e(EXCEPTION_PARAMS("Failed to verify OCSP Responder certificate"), err);
e.setCode(Exception::CertificateUnknown);
throw e;
}
throw OpenSSLException(EXCEPTION_PARAMS("Failed to verify OCSP response."), err);
}
// Find issuer before OCSP validation to activate region TSL
X509Cert issuer = X509CertStore::instance()->findIssuer(cert, X509CertStore::CA);
if(!issuer)
{
Exception e(EXCEPTION_PARAMS("Certificate status: unknown"));
e.setCode(Exception::CertificateUnknown);
throw e;
}
int status = V_OCSP_CERTSTATUS_UNKNOWN;
for(int i = 0, count = OCSP_resp_count(basic.get()); i < count; ++i)
{
const EVP_MD *evp_md {};
const OCSP_CERTID *certID = OCSP_SINGLERESP_get0_id(OCSP_resp_get0(basic.get(), i));
ASN1_OBJECT *md {};
if(OCSP_id_get0_info(nullptr, &md, nullptr, nullptr, const_cast<OCSP_CERTID*>(certID)) == 1)
evp_md = EVP_get_digestbyobj(md);
SCOPE(OCSP_CERTID, certId, OCSP_cert_to_id(evp_md, cert.handle(), issuer.handle()));
if(OCSP_resp_find_status(basic.get(), certId.get(), &status, nullptr, nullptr, nullptr, nullptr) == 1)
break;
}
switch(status)
{
case V_OCSP_CERTSTATUS_GOOD: break;
case V_OCSP_CERTSTATUS_REVOKED:
{
DEBUG("OCSP status: REVOKED");
Exception e(EXCEPTION_PARAMS("Certificate status: revoked"));
e.setCode(Exception::CertificateRevoked);
throw e;
}
case V_OCSP_CERTSTATUS_UNKNOWN:
default:
{
DEBUG("OCSP status: UNKNOWN");
Exception e(EXCEPTION_PARAMS("Certificate status: unknown"));
e.setCode( Exception::CertificateUnknown );
throw e;
}
}
}
/**
* Return OCSP nonce
*/
vector<unsigned char> OCSP::nonce() const
{
vector<unsigned char> nonce;
if(!basic)
return nonce;
int resp_idx = OCSP_BASICRESP_get_ext_by_NID(basic.get(), NID_id_pkix_OCSP_Nonce, -1);
if(resp_idx < 0)
return nonce;
X509_EXTENSION *ext = OCSP_BASICRESP_get_ext(basic.get(), resp_idx);
if(!ext)
return nonce;
ASN1_OCTET_STRING *value = X509_EXTENSION_get_data(ext);
nonce.assign(value->data, std::next(value->data, value->length));
//OpenSSL OCSP created messages NID_id_pkix_OCSP_Nonce field is DER encoded twice, not a problem with java impl
//XXX: UglyHackTM check if nonceAsn1 contains ASN1_OCTET_STRING
//XXX: if first 2 bytes seem to be beginning of DER ASN1_OCTET_STRING then remove them
// We assume that bdoc nonce has always octet string header
if(nonce.size() > 2 && nonce[0] == V_ASN1_OCTET_STRING && nonce[1] == nonce.size()-2)
nonce.erase(nonce.begin(), nonce.begin() + 2);
return nonce;
}
tm OCSP::producedAt() const
{
tm tm {};
if(!basic)
return tm;
ASN1_TIME_to_tm(OCSP_resp_get0_produced_at(basic.get()), &tm);
return tm;
}