Skip to content

Commit dfee761

Browse files
committed
Use SHA-256 for CertID
IB-6977 Signed-off-by: Raul Metsma <raul@metsma.ee>
1 parent 29450e5 commit dfee761

File tree

5 files changed

+20
-19
lines changed

5 files changed

+20
-19
lines changed

src/crypto/Digest.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,7 @@ Digest::Digest(string_view uri)
4949

5050
vector<unsigned char> Digest::digestInfoDigest(const std::vector<unsigned char> &digest)
5151
{
52-
const unsigned char *p = digest.data();
53-
auto sig = make_unique_ptr<X509_SIG_free>(d2i_X509_SIG(nullptr, &p, long(digest.size())));
52+
auto sig = d2i<d2i_X509_SIG, X509_SIG_free>(digest);
5453
if(!sig)
5554
return {};
5655
const ASN1_OCTET_STRING *value {};
@@ -60,8 +59,7 @@ vector<unsigned char> Digest::digestInfoDigest(const std::vector<unsigned char>
6059

6160
string Digest::digestInfoUri(const std::vector<unsigned char> &digest)
6261
{
63-
const unsigned char *p = digest.data();
64-
auto sig = make_unique_ptr<X509_SIG_free>(d2i_X509_SIG(nullptr, &p, long(digest.size())));
62+
auto sig = d2i<d2i_X509_SIG, X509_SIG_free>(digest);
6563
if(!sig)
6664
return {};
6765
const X509_ALGOR *algor {};

src/crypto/OCSP.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,6 @@
3030
#include <algorithm>
3131
#include <array>
3232

33-
#ifdef WIN32 //hack for win32 build
34-
#undef OCSP_REQUEST
35-
#undef OCSP_RESPONSE
36-
#endif
3733
#include <openssl/ocsp.h>
3834
#include <openssl/pem.h>
3935
#include <openssl/rand.h>
@@ -73,7 +69,10 @@ OCSP::OCSP(const X509Cert &cert, const X509Cert &issuer, const std::string &user
7369
if(!req)
7470
THROW_OPENSSLEXCEPTION("Failed to create new OCSP request, out of memory?");
7571

76-
OCSP_CERTID *certId = OCSP_cert_to_id(nullptr, cert.handle(), issuer.handle());
72+
const EVP_MD *evp_md {};
73+
if(url.find("eidpki.ee") != std::string::npos)
74+
evp_md = EVP_get_digestbynid(NID_sha256);
75+
OCSP_CERTID *certId = OCSP_cert_to_id(evp_md, cert.handle(), issuer.handle());
7776
if(!OCSP_request_add0_id(req.get(), certId))
7877
THROW_OPENSSLEXCEPTION("Failed to add certificate ID to OCSP request.");
7978

@@ -236,6 +235,8 @@ void OCSP::verifyResponse(const X509Cert &cert) const
236235
if(OCSP_id_get0_info(nullptr, &md, nullptr, nullptr, const_cast<OCSP_CERTID*>(certID)) == 1)
237236
evp_md = EVP_get_digestbyobj(md);
238237
auto certId = make_unique_ptr<OCSP_CERTID_free>(OCSP_cert_to_id(evp_md, cert.handle(), issuer.handle()));
238+
if(OCSP_id_cmp(certID, certId.get()) != 0)
239+
continue;
239240
if(OCSP_resp_find_status(basic.get(), certId.get(), &status, nullptr, nullptr, nullptr, nullptr) == 1)
240241
break;
241242
}

src/crypto/OpenSSLHelpers.h

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,6 @@
2828
namespace digidoc
2929
{
3030

31-
#define SCOPE_PTR(TYPE, DATA) make_unique_ptr<TYPE##_free>(DATA)
32-
#define SCOPE(TYPE, VAR, DATA) auto VAR = make_unique_ptr<TYPE>(DATA, TYPE##_free)
33-
3431
template<auto F, class T>
3532
[[nodiscard]]
3633
inline std::vector<unsigned char> i2d(T *obj)
@@ -54,6 +51,13 @@ inline std::vector<unsigned char> i2d(const T &obj)
5451
return i2d<F>(obj.get());
5552
}
5653

54+
template<auto F, auto D, class C>
55+
constexpr auto d2i(const C &c)
56+
{
57+
const unsigned char *p = c.data();
58+
return make_unique_ptr<D>(F(nullptr, &p, long(c.size())));
59+
}
60+
5761
/**
5862
* OpenSSL exception implementation. Thrown if the openssl returns error
5963
*/

src/crypto/PKCS12Signer.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,10 @@ class PKCS12Signer::Private
5454
PKCS12Signer::PKCS12Signer(const string &path, const string &pass)
5555
: d(make_unique<Private>())
5656
{
57-
auto bio = SCOPE_PTR(BIO, BIO_new_file(path.c_str(), "rb"));
57+
auto bio = make_unique_ptr<BIO_free>(BIO_new_file(path.c_str(), "rb"));
5858
if(!bio)
5959
THROW_OPENSSLEXCEPTION("Failed to open PKCS12 certificate: %s.", path.c_str());
60-
auto p12 = SCOPE_PTR(PKCS12, d2i_PKCS12_bio(bio.get(), nullptr));
60+
auto p12 = make_unique_ptr<PKCS12_free>(d2i_PKCS12_bio(bio.get(), nullptr));
6161
if(!p12)
6262
THROW_OPENSSLEXCEPTION("Failed to read PKCS12 certificate: %s.", path.c_str());
6363
if(!PKCS12_parse(p12.get(), pass.c_str(), &d->key, &d->cert, nullptr))
@@ -82,7 +82,7 @@ vector<unsigned char> PKCS12Signer::sign(const string &method, const vector<unsi
8282
int result = 0;
8383
vector<unsigned char> signature;
8484
size_t size = 0;
85-
SCOPE(EVP_PKEY_CTX, ctx, EVP_PKEY_CTX_new(d->key, nullptr));
85+
auto ctx = make_unique_ptr<EVP_PKEY_CTX_free>(EVP_PKEY_CTX_new(d->key, nullptr));
8686
if(!ctx || EVP_PKEY_sign_init(ctx.get()) <= 0)
8787
THROW_OPENSSLEXCEPTION("Failed to sign the digest");
8888
switch(EVP_PKEY_base_id(d->key))
@@ -111,8 +111,7 @@ vector<unsigned char> PKCS12Signer::sign(const string &method, const vector<unsi
111111
result = EVP_PKEY_sign(ctx.get(), asn1.data(), &size, digest.data(), digest.size());
112112
if(result <= 0)
113113
break;
114-
const unsigned char *p = asn1.data();
115-
SCOPE(ECDSA_SIG, sig, d2i_ECDSA_SIG(nullptr, &p, long(asn1.size())));
114+
auto sig = d2i<d2i_ECDSA_SIG, ECDSA_SIG_free>(asn1);
116115
const BIGNUM *r = nullptr, *s = nullptr;
117116
ECDSA_SIG_get0(sig.get(), &r, &s);
118117
auto r_len = size_t(BN_num_bytes(r));

src/crypto/X509Crypto.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,7 @@ X509Crypto::X509Crypto(X509Cert cert)
5454
bool X509Crypto::compareIssuerToDer(const vector<unsigned char> &data) const
5555
{
5656
// DER-encoded instance of type IssuerSerial type defined in IETF RFC 5035 [17].
57-
const unsigned char *p = data.data();
58-
SCOPE(ESS_ISSUER_SERIAL, is, d2i_ESS_ISSUER_SERIAL(nullptr, &p, long(data.size())));
57+
auto is = d2i<d2i_ESS_ISSUER_SERIAL, ESS_ISSUER_SERIAL_free>(data);
5958
if(!is || sk_GENERAL_NAME_num(is->issuer) != 1)
6059
return false;
6160

0 commit comments

Comments
 (0)