2121 */
2222
2323#include " MsCryptoApiElectronicID.hpp"
24- #include " ../scope .hpp"
24+ #include " ../x509 .hpp"
2525
2626#include < openssl/x509v3.h>
2727#include < openssl/err.h>
@@ -35,12 +35,7 @@ using namespace electronic_id;
3535
3636JsonWebSignatureAlgorithm getESAlgorithmFromCert (const byte_vector& cert)
3737{
38- const unsigned char * certPtr = cert.data ();
39- auto x509 = SCOPE_GUARD (X509, d2i_X509 (nullptr , &certPtr, long (cert.size ())));
40- if (!x509) {
41- THROW (MsCryptoApiError, " Failed to create X509 object from certificate" );
42- }
43-
38+ auto x509 = toX509 (cert);
4439 EVP_PKEY* key = X509_get0_pubkey (x509.get ());
4540 if (EVP_PKEY_base_id (key) != EVP_PKEY_EC) {
4641 THROW (MsCryptoApiError, " EVP_PKEY_base_id() reports non-EC key where EC key expected" );
@@ -61,8 +56,9 @@ JsonWebSignatureAlgorithm getESAlgorithmFromCert(const byte_vector& cert)
6156 }
6257}
6358
64- ElectronicID::Signature sign (const byte_vector& hash, HashAlgorithm hashAlgo,
65- const HCRYPTPROV_OR_NCRYPT_KEY_HANDLE key, const bool isRSA)
59+ ElectronicID::Signature sign (const byte_vector& cert, const byte_vector& hash,
60+ HashAlgorithm hashAlgo, const HCRYPTPROV_OR_NCRYPT_KEY_HANDLE key,
61+ const bool isRSA)
6662{
6763 BCRYPT_PKCS1_PADDING_INFO padInfo {};
6864 switch (hashAlgo) {
@@ -114,6 +110,10 @@ ElectronicID::Signature sign(const byte_vector& hash, HashAlgorithm hashAlgo,
114110 THROW (MsCryptoApiError, " Signing failed with error: " + std::to_string (err));
115111 }
116112
113+ if (!verifyDigest (hashAlgo, cert, hash, signature)) {
114+ THROW (SmartCardError, " Failed to validate given signature!" );
115+ }
116+
117117 return {signature,
118118 SignatureAlgorithm {isRSA ? SignatureAlgorithm::RS : SignatureAlgorithm::ES, hashAlgo}};
119119}
@@ -129,7 +129,8 @@ JsonWebSignatureAlgorithm MsCryptoApiElectronicID::authSignatureAlgorithm() cons
129129 return isRSA () ? JsonWebSignatureAlgorithm::RS256 : getESAlgorithmFromCert (certData);
130130}
131131
132- byte_vector MsCryptoApiElectronicID::signWithAuthKey (const byte_vector& /* pin */ ,
132+ byte_vector MsCryptoApiElectronicID::signWithAuthKey (const byte_vector& cert,
133+ const byte_vector& /* pin */ ,
133134 const byte_vector& hash) const
134135{
135136 if (certType != CertificateType::AUTHENTICATION) {
@@ -141,12 +142,13 @@ byte_vector MsCryptoApiElectronicID::signWithAuthKey(const byte_vector& /* pin *
141142
142143 validateAuthHashLength (authSignatureAlgorithm (), name (), hash);
143144
144- const auto signature = sign (hash, authSignatureAlgorithm ().hashAlgorithm (), key, isRSA ());
145+ const auto signature = sign (cert, hash, authSignatureAlgorithm ().hashAlgorithm (), key, isRSA ());
145146 return signature.first ;
146147}
147148
148149ElectronicID::Signature
149- MsCryptoApiElectronicID::signWithSigningKey (const byte_vector& /* pin */ , const byte_vector& hash,
150+ MsCryptoApiElectronicID::signWithSigningKey (const byte_vector& cert, const byte_vector& /* pin */ ,
151+ const byte_vector& hash,
150152 const HashAlgorithm hashAlgo) const
151153{
152154 if (certType != CertificateType::SIGNING) {
@@ -158,7 +160,7 @@ MsCryptoApiElectronicID::signWithSigningKey(const byte_vector& /* pin */, const
158160
159161 validateSigningHash (*this , hashAlgo, hash);
160162
161- return sign (hash, hashAlgo, key, isRSA ());
163+ return sign (cert, hash, hashAlgo, key, isRSA ());
162164}
163165
164166} // namespace electronic_id
0 commit comments