Skip to content

Commit

Permalink
Update unittests
Browse files Browse the repository at this point in the history
IB-7357

Signed-off-by: Raul Metsma <raul@metsma.ee>
  • Loading branch information
metsma committed Apr 13, 2022
1 parent 96e131d commit 18e8376
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 57 deletions.
11 changes: 3 additions & 8 deletions src/SignatureXAdES_B.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -710,10 +710,7 @@ void SignatureXAdES_B::checkKeyInfo() const
}();

// lets check digest with x509 that was in keyInfo
Digest certDigestCalc(certDigest.digestMethod().algorithm());
certDigestCalc.update(x509);
vector<unsigned char> calcDigest = certDigestCalc.result();

vector<unsigned char> calcDigest = Digest(certDigest.digestMethod().algorithm()).result(x509);
if(certDigest.digestValue().size() != calcDigest.size() ||
memcmp(calcDigest.data(), certDigest.digestValue().data(), certDigest.digestValue().size()) != 0)
{
Expand Down Expand Up @@ -832,10 +829,9 @@ void SignatureXAdES_B::setSigningCertificate(const X509Cert& x509)
{
// Calculate digest of the X.509 certificate.
Digest digest;
digest.update(x509);
CertIDListType signingCertificate;
signingCertificate.cert().push_back(CertIDType(
DigestAlgAndValueType(DigestMethodType(digest.uri()), toBase64(digest.result())),
DigestAlgAndValueType(DigestMethodType(digest.uri()), toBase64(digest.result(x509))),
X509IssuerSerialType(x509.issuerName(), x509.serial())));
getSignedSignatureProperties().signingCertificate(signingCertificate);
}
Expand All @@ -850,10 +846,9 @@ void SignatureXAdES_B::setSigningCertificateV2(const X509Cert& x509)
{
// Calculate digest of the X.509 certificate.
Digest digest;
digest.update(x509);
CertIDListV2Type signingCertificate;
signingCertificate.cert().push_back(CertIDTypeV2(
DigestAlgAndValueType(DigestMethodType(digest.uri()), toBase64(digest.result()))));
DigestAlgAndValueType(DigestMethodType(digest.uri()), toBase64(digest.result(x509)))));
getSignedSignatureProperties().signingCertificateV2(signingCertificate);
}

Expand Down
7 changes: 2 additions & 5 deletions src/SignatureXAdES_LT.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -155,9 +155,7 @@ void SignatureXAdES_LT::validate(const std::string &policy) const
string method = Digest::digestInfoUri(ocsp.nonce());
if(method.empty())
THROW("Nonce digest method is missing");
Digest calc(method);
calc.update(getSignatureValue());
vector<unsigned char> digest = calc.result();
vector<unsigned char> digest = Digest(method).result(getSignatureValue());
vector<unsigned char> respDigest = Digest::digestInfoDigest(ocsp.nonce());
if(digest != respDigest)
{
Expand Down Expand Up @@ -216,8 +214,7 @@ void SignatureXAdES_LT::extendSignatureProfile(const std::string &profile)

// Calculate NONCE value.
Digest calc;
calc.update(getSignatureValue());
vector<unsigned char> nonce = Digest::addDigestInfo(calc.result(), calc.uri());
vector<unsigned char> nonce = Digest::addDigestInfo(calc.result(getSignatureValue()), calc.uri());
DEBUGMEM("OID + Calculated signature HASH (nonce):", nonce.data(), nonce.size());

// Get issuer certificate from certificate store.
Expand Down
11 changes: 11 additions & 0 deletions src/crypto/Digest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -290,3 +290,14 @@ vector<unsigned char> Digest::result() const
THROW_OPENSSLEXCEPTION("Failed to create %s digest", uri().c_str());
return *d;
}

vector<unsigned char> Digest::result(const vector<unsigned char> &data)
{
return result(data.data(), data.size());
}

vector<unsigned char> Digest::result(const unsigned char *data, size_t length)
{
update(data, length);
return result();
}
2 changes: 2 additions & 0 deletions src/crypto/Digest.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ namespace digidoc
void reset(const std::string &uri = {});
void update(const std::vector<unsigned char> &data);
void update(const unsigned char *data, size_t length);
std::vector<unsigned char> result(const std::vector<unsigned char> &data);
std::vector<unsigned char> result(const unsigned char *data, size_t length);
std::vector<unsigned char> result() const;
std::string uri() const;

Expand Down
91 changes: 47 additions & 44 deletions test/libdigidocpp_boost.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include <DataFile.h>
#include <Signature.h>
#include <XmlConf.h>
#include <crypto/Digest.h>
#include <crypto/PKCS12Signer.h>
#include <crypto/X509Crypto.h>
#include <util/DateTime.h>
Expand Down Expand Up @@ -87,33 +88,34 @@ BOOST_AUTO_TEST_CASE(signerParameters)
BOOST_CHECK_EQUAL(signer->postalCode(), "12345");
BOOST_CHECK_EQUAL(signer->countryName(), "Estonia");

const vector<unsigned char> digest = {'S', 'i', 'g', 'n', 'a', 't', 'u', 'r', 'e', '\0' };
const vector<unsigned char> data {'S', 'i', 'g', 'n', 'a', 't', 'u', 'r', 'e', '\0' };
const vector<unsigned char> digest = Digest(URI_SHA256).result(data);
vector<unsigned char> signature;
BOOST_CHECK_NO_THROW(signature = signer->sign("http://www.w3.org/2001/04/xmlenc#sha256", digest));

const vector<unsigned char> sig = {
0x17, 0x2F, 0x5B, 0x7D, 0x16, 0x27, 0x32, 0x86, 0x4E, 0xF1, 0x97, 0x62,
0x2B, 0x6E, 0x5E, 0x03, 0x52, 0x48, 0x6C, 0x14, 0xE0, 0x0C, 0xE8, 0xBB,
0xA7, 0x88, 0x9D, 0x06, 0x08, 0x9A, 0x1A, 0x33, 0xA8, 0xD7, 0x96, 0x10,
0x32, 0xE0, 0xF1, 0x57, 0x85, 0xB7, 0x04, 0x59, 0x27, 0x34, 0xCB, 0x74,
0xD1, 0xCB, 0xE5, 0x2C, 0x28, 0xD7, 0xF8, 0x9E, 0x2C, 0x14, 0x5B, 0xCA,
0xBC, 0x14, 0x29, 0xD2, 0xC7, 0xC7, 0x6A, 0xC3, 0x02, 0x82, 0x21, 0x83,
0x7D, 0x64, 0xD3, 0xB6, 0x7E, 0x46, 0xDE, 0x46, 0xCB, 0x60, 0xE7, 0x0C,
0xB3, 0x13, 0xBD, 0x85, 0x55, 0xF2, 0x19, 0xAB, 0xD7, 0x60, 0x8F, 0xF3,
0x11, 0xAB, 0xD8, 0x36, 0x3A, 0x54, 0x5C, 0xA8, 0x16, 0x0F, 0x09, 0xFC,
0x6F, 0xCA, 0x8C, 0x60, 0xDF, 0xCF, 0x9F, 0x1D, 0xA4, 0xF4, 0xF8, 0x61,
0x0F, 0x68, 0xC3, 0x80, 0xB0, 0xD9, 0x07, 0xB9, 0x85, 0x72, 0xE7, 0x2E,
0x10, 0xED, 0xB2, 0xB4, 0xD7, 0xFA, 0x8D, 0xA5, 0xB0, 0x87, 0x21, 0x9A,
0xAD, 0x26, 0xB6, 0x89, 0x60, 0x13, 0x7B, 0x72, 0x59, 0x2B, 0xCF, 0x0E,
0x6B, 0xBB, 0x04, 0x87, 0xCB, 0x1D, 0x1A, 0x15, 0x37, 0x2D, 0x9C, 0x91,
0xFA, 0xA5, 0x7E, 0x45, 0xA7, 0x85, 0x7A, 0x7E, 0x77, 0x9B, 0x66, 0x44,
0xC4, 0xCE, 0x63, 0xA6, 0xFC, 0x8F, 0xD0, 0x8F, 0xD4, 0xA0, 0x8A, 0x68,
0x82, 0xBA, 0x71, 0x94, 0x65, 0xF7, 0x01, 0x79, 0x7D, 0xDB, 0xCB, 0x06,
0x27, 0xF6, 0xC7, 0x05, 0xA6, 0x7F, 0xAE, 0x0B, 0x85, 0x94, 0x3A, 0x8A,
0xB0, 0x7F, 0xA6, 0x42, 0xA2, 0x6D, 0x3E, 0x22, 0x0B, 0xBF, 0xE9, 0xDA,
0x70, 0xAF, 0xDF, 0x78, 0xBA, 0x73, 0xB4, 0xCC, 0xD3, 0x14, 0x76, 0x78,
0xB3, 0x64, 0x5B, 0x78, 0x33, 0xE1, 0x73, 0x9F, 0xD4, 0x5F, 0x43, 0xCD,
0xDB, 0x63, 0x96, 0x52
BOOST_CHECK_NO_THROW(signature = signer->sign(URI_SHA256, digest));

const vector<unsigned char> sig {
0x19, 0x48, 0x15, 0x11, 0x27, 0xA0, 0x1D, 0xB5, 0x4F, 0x0B, 0x91, 0x6F,
0x54, 0x2B, 0x6F, 0x69, 0xAD, 0xAB, 0x9A, 0x23, 0x7C, 0x3F, 0x35, 0xEF,
0x24, 0xDE, 0xE1, 0x77, 0xB9, 0xED, 0xC8, 0xDF, 0x34, 0x4F, 0x14, 0x7E,
0xD5, 0xE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0x8D, 0xE3, 0x78, 0x89, 0xF9, 0x70, 0xD2, 0x8A, 0xD9,
0x31, 0x97, 0x8A, 0x7B, 0x2E, 0xD9, 0x99, 0xBE, 0xE2, 0x3E, 0xA9, 0xBA,
0xE2, 0x3A, 0xE0, 0xD4, 0x38, 0x43, 0x8B, 0x80, 0xA5, 0x7A, 0xAA, 0x59,
0xEE, 0xD9, 0xED, 0x5A
};
BOOST_CHECK_EQUAL(signature, sig);
}
Expand Down Expand Up @@ -155,11 +157,12 @@ BOOST_AUTO_TEST_CASE(parameters)
BOOST_CHECK_EQUAL(test.compareIssuerToString("CN=\\\"test\\\""), 0);

unique_ptr<Signer> signer1(new PKCS12Signer("signer1.p12", "signer1"));
vector<unsigned char> data({'H','e','l','l','o',' ','w','o','r','l','d'});
vector<unsigned char> signature = signer1->sign("http://www.w3.org/2001/04/xmlenc#sha256", data);
BOOST_CHECK_EQUAL(digidoc::X509Crypto(signer1->cert()).verify("http://www.w3.org/2001/04/xmlenc#sha256", data, signature), true);
data.resize(data.size() - 1);
BOOST_CHECK_EQUAL(digidoc::X509Crypto(signer1->cert()).verify("http://www.w3.org/2001/04/xmlenc#sha256", data, signature), false);
const vector<unsigned char> data{'H','e','l','l','o',' ','w','o','r','l','d'};
vector<unsigned char> digest = Digest(URI_SHA256).result(data);
vector<unsigned char> signature = signer1->sign(URI_SHA256, digest);
BOOST_CHECK_EQUAL(digidoc::X509Crypto(signer1->cert()).verify(URI_SHA256, digest, signature), true);
digest[0] += 1;
BOOST_CHECK_EQUAL(digidoc::X509Crypto(signer1->cert()).verify(URI_SHA256, digest, signature), false);
}
BOOST_AUTO_TEST_SUITE_END()

Expand Down Expand Up @@ -230,48 +233,48 @@ BOOST_AUTO_TEST_CASE_TEMPLATE(document, Doc, DocTypes)
const DataFile *data = d->dataFiles().front();
BOOST_CHECK_NO_THROW(data->saveAs("test1.tmp"));

BOOST_CHECK_EQUAL(data->calcDigest("http://www.w3.org/2000/09/xmldsig#sha1"), vector<unsigned char>({
BOOST_CHECK_EQUAL(data->calcDigest(URI_SHA1), vector<unsigned char>({
0x1B, 0xE1, 0x68, 0xFF, 0x83, 0x7F, 0x04, 0x3B, 0xDE, 0x17,
0xC0, 0x31, 0x43, 0x41, 0xC8, 0x42, 0x71, 0x04, 0x7B, 0x31 }));
BOOST_CHECK_EQUAL(data->calcDigest("http://www.w3.org/2001/04/xmldsig-more#sha224"), vector<unsigned char>({
BOOST_CHECK_EQUAL(data->calcDigest(URI_SHA224), vector<unsigned char>({
0xD7, 0x60, 0x41, 0x11, 0x2E, 0x34, 0x3B, 0x2B, 0xDC, 0x14,
0xD4, 0x39, 0x34, 0xE5, 0xAE, 0xD7, 0xAB, 0xF9, 0x01, 0x92,
0xC6, 0x54, 0x3B, 0xDF, 0x2A, 0xE4, 0xF8, 0x1B }));
BOOST_CHECK_EQUAL(data->calcDigest("http://www.w3.org/2001/04/xmlenc#sha256"), vector<unsigned char>({
BOOST_CHECK_EQUAL(data->calcDigest(URI_SHA256), vector<unsigned char>({
0xA8, 0x83, 0xDA, 0xFC, 0x48, 0x0D, 0x46, 0x6E, 0xE0, 0x4E,
0x0D, 0x6D, 0xA9, 0x86, 0xBD, 0x78, 0xEB, 0x1F, 0xDD, 0x21,
0x78, 0xD0, 0x46, 0x93, 0x72, 0x3D, 0xA3, 0xA8, 0xF9, 0x5D,
0x42, 0xF4 }));
BOOST_CHECK_EQUAL(data->calcDigest("http://www.w3.org/2001/04/xmldsig-more#sha384"), vector<unsigned char>({
BOOST_CHECK_EQUAL(data->calcDigest(URI_SHA384), vector<unsigned char>({
0x63, 0x7E, 0x2E, 0xDD, 0x55, 0x55, 0x70, 0xED, 0xA9, 0x66,
0xD9, 0x9D, 0x4E, 0x77, 0xD9, 0xFB, 0xB3, 0xAA, 0xB8, 0x4A,
0x49, 0x8F, 0xF5, 0x5A, 0xC2, 0x1B, 0x96, 0x3C, 0x1E, 0x05,
0xC2, 0xAD, 0xDF, 0xB5, 0xC1, 0x5C, 0xD2, 0x07, 0x1E, 0x7E,
0xDD, 0x47, 0x35, 0x9D, 0x78, 0x79, 0x41, 0xD3 }));
BOOST_CHECK_EQUAL(data->calcDigest("http://www.w3.org/2001/04/xmlenc#sha512"), vector<unsigned char>({
BOOST_CHECK_EQUAL(data->calcDigest(URI_SHA512), vector<unsigned char>({
0x79, 0x85, 0x55, 0x83, 0x70, 0xF0, 0xDE, 0x86, 0xA8, 0x64,
0xE0, 0x05, 0x0A, 0xFD, 0xF4, 0x5D, 0x70, 0x29, 0xB8, 0x79,
0x8B, 0xCD, 0x72, 0xCD, 0xDB, 0xF7, 0x81, 0x32, 0x9F, 0x99,
0x38, 0x0E, 0x3F, 0x3B, 0x1A, 0xFD, 0xCA, 0x67, 0x65, 0xD8,
0x9F, 0xC3, 0x88, 0xB2, 0x13, 0xDF, 0x8F, 0x6A, 0x19, 0x3C,
0xFC, 0x56, 0xD4, 0xFF, 0x2E, 0xF6, 0xE0, 0xA9, 0x9B, 0xD8,
0x83, 0xA6, 0xD9, 0x8C }));
BOOST_CHECK_EQUAL(data->calcDigest("http://www.w3.org/2007/05/xmldsig-more#sha3-224"), vector<unsigned char>({
BOOST_CHECK_EQUAL(data->calcDigest(URI_SHA3_224), vector<unsigned char>({
0x8A, 0x02, 0x0A, 0x4C, 0x68, 0x12, 0x07, 0x36, 0x2D, 0xEA,
0x91, 0xD5, 0x9F, 0x66, 0x0A, 0x47, 0xA1, 0x83, 0xE1, 0xE3,
0xD4, 0x88, 0x32, 0x8D, 0xDD, 0x64, 0xA7, 0xE4}));
BOOST_CHECK_EQUAL(data->calcDigest("http://www.w3.org/2007/05/xmldsig-more#sha3-256"), vector<unsigned char>({
BOOST_CHECK_EQUAL(data->calcDigest(URI_SHA3_256), vector<unsigned char>({
0x85, 0x9A, 0x7A, 0x76, 0x03, 0x02, 0x8D, 0xEE, 0xB3, 0xB6,
0x62, 0x34, 0xCF, 0xFA, 0x51, 0x91, 0x46, 0x6D, 0x1A, 0x05,
0x38, 0xE4, 0x49, 0xA1, 0x98, 0x12, 0x27, 0x3B, 0x0D, 0x98,
0xDC, 0x1C }));
BOOST_CHECK_EQUAL(data->calcDigest("http://www.w3.org/2007/05/xmldsig-more#sha3-384"), vector<unsigned char>({
BOOST_CHECK_EQUAL(data->calcDigest(URI_SHA3_384), vector<unsigned char>({
0x81, 0xEC, 0x6A, 0x88, 0x5D, 0x9F, 0x59, 0x16, 0x0B, 0x93,
0x18, 0x55, 0xF3, 0x42, 0x2E, 0x37, 0x07, 0x2C, 0xDA, 0x80,
0x59, 0xDA, 0xE0, 0x63, 0xEB, 0x64, 0x71, 0x6F, 0xE4, 0xC4,
0xE9, 0xC0, 0xE7, 0x01, 0xE3, 0xE6, 0x47, 0x13, 0x15, 0xBA,
0x44, 0x6B, 0xAD, 0x40, 0x31, 0x81, 0x1E, 0x5D }));
BOOST_CHECK_EQUAL(data->calcDigest("http://www.w3.org/2007/05/xmldsig-more#sha3-512"), vector<unsigned char>({
BOOST_CHECK_EQUAL(data->calcDigest(URI_SHA3_512), vector<unsigned char>({
0x05, 0x05, 0xDA, 0x4C, 0x58, 0x3D, 0x0B, 0xFA, 0x95, 0x69,
0x2B, 0xF9, 0x84, 0x5A, 0xBF, 0x2A, 0x40, 0xF3, 0x2E, 0x34,
0xCA, 0xEF, 0x5A, 0xCC, 0x57, 0xBD, 0x30, 0x15, 0xAE, 0xFA,
Expand Down Expand Up @@ -329,7 +332,7 @@ BOOST_AUTO_TEST_CASE_TEMPLATE(signature, Doc, DocTypes)
Signature *s3 = nullptr;
BOOST_CHECK_NO_THROW(s3 = d->sign(signer3.get()));
BOOST_CHECK_EQUAL(d->signatures().size(), 2U);
BOOST_CHECK_EQUAL(s3->signatureMethod(), "http://www.w3.org/2001/04/xmldsig-more#ecdsa-sha256");
BOOST_CHECK_EQUAL(s3->signatureMethod(), URI_ECDSA_SHA256);
if(s3)
{
BOOST_CHECK_EQUAL(s3->signingCertificate(), signer3->cert());
Expand Down Expand Up @@ -392,7 +395,7 @@ BOOST_AUTO_TEST_CASE_TEMPLATE(signature, Doc, DocTypes)
BOOST_CHECK_NO_THROW(s->validate());

d = Container::createPtr(Doc::EXT + ".tmp");
signer1->setMethod("http://www.w3.org/2007/05/xmldsig-more#sha256-rsa-MGF1");
signer1->setMethod(URI_RSA_PSS_SHA256);
BOOST_CHECK_NO_THROW(d->addDataFile("test1.txt", "text/plain"));
BOOST_CHECK_NO_THROW(d->sign(signer1.get()));
s = d->signatures().back();
Expand All @@ -404,7 +407,7 @@ BOOST_AUTO_TEST_CASE_TEMPLATE(signature, Doc, DocTypes)
BOOST_CHECK_NO_THROW(d->addDataFile("test1.txt", "text/plain"));
Signature *s4 = nullptr;
BOOST_CHECK_NO_THROW(s4 = d->sign(signer4.get()));
BOOST_CHECK_EQUAL(s4->signatureMethod(), "http://www.w3.org/2001/04/xmldsig-more#ecdsa-sha384");
BOOST_CHECK_EQUAL(s4->signatureMethod(), URI_ECDSA_SHA384);
}

// Remove second Signature
Expand Down Expand Up @@ -479,7 +482,7 @@ BOOST_AUTO_TEST_CASE(XmlConfCase) {
XmlConf c("digidocpp.conf", util::File::path(DIGIDOCPPCONF, "/conf.xsd"));
BOOST_CHECK_EQUAL(c.logLevel(), 2);
BOOST_CHECK_EQUAL(c.logFile(), "digidocpp.log");
BOOST_CHECK_EQUAL(c.digestUri(), "http://www.w3.org/2001/04/xmlenc#sha256");
BOOST_CHECK_EQUAL(c.digestUri(), URI_SHA256);
//BOOST_CHECK_EQUAL(c.PKCS11Driver(), PKCS11_MODULE);
BOOST_CHECK_EQUAL(c.xsdPath().substr(c.xsdPath().size() - 6, 6), "schema");
BOOST_CHECK_EQUAL(c.proxyHost(), "host");
Expand Down

0 comments on commit 18e8376

Please sign in to comment.