2020#include " X509Cert.h"
2121
2222#include " crypto/OpenSSLHelpers.h"
23- #include " crypto/X509Crypto.h"
2423#include " util/log.h"
2524
2625#include < openssl/asn1t.h>
2726#include < openssl/pem.h>
2827#include < openssl/x509v3.h>
2928
30- #include < functional>
31-
3229using namespace digidoc ;
3330using namespace std ;
3431
@@ -229,7 +226,7 @@ X509Cert::X509Cert(const unsigned char *bytes, size_t size, Format format)
229226 }
230227 else
231228 {
232- SCOPE (BIO, bio, BIO_new_mem_buf ((void *)bytes, int (size)));
229+ auto bio = make_unique_ptr<BIO_free>( BIO_new_mem_buf ((void *)bytes, int (size)));
233230 cert.reset (PEM_read_bio_X509 (bio.get (), nullptr , nullptr , nullptr ), X509_free);
234231 }
235232 if (!cert)
@@ -247,7 +244,7 @@ X509Cert::X509Cert(const string &path, Format format)
247244{
248245 if (path.empty ())
249246 THROW (" No path given to parse X509." );
250- SCOPE (BIO, bio, BIO_new_file (path.c_str (), " rb" ));
247+ auto bio = make_unique_ptr<BIO_free>( BIO_new_file (path.c_str (), " rb" ));
251248 if (!bio)
252249 THROW_OPENSSLEXCEPTION (" Failed to open X.509 certificate file '%s'" , path.c_str ());
253250 if (format == Der)
@@ -298,7 +295,7 @@ string X509Cert::serial() const
298295{
299296 if (!cert)
300297 return {};
301- if (auto bn = make_unique_ptr (ASN1_INTEGER_to_BN (X509_get_serialNumber (cert.get ()), nullptr ), BN_free ))
298+ if (auto bn = make_unique_ptr<BN_free> (ASN1_INTEGER_to_BN (X509_get_serialNumber (cert.get ()), nullptr )))
302299 {
303300 auto openssl_free = [](char *data) { OPENSSL_free (data); };
304301 if (auto str = unique_ptr<char ,decltype (openssl_free)>(BN_bn2dec (bn.get ()), openssl_free))
@@ -327,7 +324,7 @@ vector<X509Cert::KeyUsage> X509Cert::keyUsage() const
327324 vector<KeyUsage> usage;
328325 if (!cert)
329326 return usage;
330- SCOPE (ASN1_BIT_STRING, keyusage, X509_get_ext_d2i (cert.get (), NID_key_usage, nullptr , nullptr ));
327+ auto keyusage = make_unique_cast<ASN1_BIT_STRING_free>( X509_get_ext_d2i (cert.get (), NID_key_usage, nullptr , nullptr ));
331328 if (!keyusage)
332329 return usage;
333330
@@ -347,7 +344,7 @@ vector<string> X509Cert::certificatePolicies() const
347344 vector<string> pol;
348345 if (!cert)
349346 return pol;
350- SCOPE (CERTIFICATEPOLICIES, cp, X509_get_ext_d2i (cert.get (), NID_certificate_policies, nullptr , nullptr ));
347+ auto cp = make_unique_cast<CERTIFICATEPOLICIES_free>( X509_get_ext_d2i (cert.get (), NID_certificate_policies, nullptr , nullptr ));
351348 if (!cp)
352349 return pol;
353350 for (int i = 0 ; i < sk_POLICYINFO_num (cp.get ()); ++i)
@@ -367,7 +364,7 @@ vector<string> X509Cert::qcStatements() const
367364 if (pos == -1 )
368365 return result;
369366 X509_EXTENSION *ext = X509_get_ext (cert.get (), pos);
370- SCOPE (QCStatements, qc, ASN1_item_unpack (X509_EXTENSION_get_data (ext), ASN1_ITEM_rptr (QCStatements)));
367+ auto qc = make_unique_cast<QCStatements_free>( ASN1_item_unpack (X509_EXTENSION_get_data (ext), ASN1_ITEM_rptr (QCStatements)));
371368 if (!qc)
372369 return result;
373370
@@ -380,33 +377,31 @@ vector<string> X509Cert::qcStatements() const
380377#ifndef TEMPLATE
381378 if (!s->statementInfo )
382379 continue ;
383- SCOPE (SemanticsInformation, si, ASN1_item_unpack (s->statementInfo ->value .sequence , ASN1_ITEM_rptr (SemanticsInformation)));
380+ auto si = make_unique_cast<SemanticsInformation_free>( ASN1_item_unpack (s->statementInfo ->value .sequence , ASN1_ITEM_rptr (SemanticsInformation)));
384381 if (!si)
385382 continue ;
386- oid = toOID (si->semanticsIdentifier );
383+ result. push_back ( toOID (si->semanticsIdentifier ) );
387384#else
388- oid = toOID (s->statementInfo .semanticsInformation ->semanticsIdentifier );
385+ result. push_back ( toOID (s->statementInfo .semanticsInformation ->semanticsIdentifier ) );
389386#endif
390- result.push_back (oid);
391387 }
392388 else if (oid == QC_QCT)
393389 {
394390#ifndef TEMPLATE
395391 if (!s->statementInfo )
396392 continue ;
397- SCOPE (QcType, qct, ASN1_item_unpack (s->statementInfo ->value .sequence , ASN1_ITEM_rptr (QcType)));
393+ auto qct = make_unique_cast<QcType_free>( ASN1_item_unpack (s->statementInfo ->value .sequence , ASN1_ITEM_rptr (QcType)));
398394 if (!qct)
399395 continue ;
400396 for (int j = 0 ; j < sk_ASN1_OBJECT_num (qct.get ()); ++j)
401397 {
402- oid = toOID (sk_ASN1_OBJECT_value (qct.get (), j));
398+ result. push_back ( toOID (sk_ASN1_OBJECT_value (qct.get (), j) ));
403399#else
404400#endif
405- result.push_back (oid);
406401 }
407402 }
408403 else
409- result.push_back (oid);
404+ result.push_back (std::move ( oid) );
410405 }
411406 return result;
412407}
@@ -463,7 +458,7 @@ string X509Cert::toString(const string &obj) const
463458 }
464459 else
465460 {
466- SCOPE (BIO, mem, BIO_new (BIO_s_mem ()));
461+ auto mem = make_unique_ptr<BIO_free>( BIO_new (BIO_s_mem ()));
467462 if (!mem)
468463 THROW_OPENSSLEXCEPTION (" Failed to allocate memory for X509_NAME conversion" );
469464
@@ -494,7 +489,7 @@ bool X509Cert::isCA() const
494489{
495490 if (!cert)
496491 return false ;
497- SCOPE (BASIC_CONSTRAINTS, cons, X509_get_ext_d2i (cert.get (), NID_basic_constraints, nullptr , nullptr ));
492+ auto cons = make_unique_cast<BASIC_CONSTRAINTS_free>( X509_get_ext_d2i (cert.get (), NID_basic_constraints, nullptr , nullptr ));
498493 return cons && cons->ca > 0 ;
499494}
500495
0 commit comments