From 956e14c8c610af488cdb94ae57ae1b4aa819aa83 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Felix=20D=C3=B6rre?= Date: Sun, 16 Nov 2014 19:39:29 +0100 Subject: [PATCH] upd: add a shared_ptr, rename parse to parseCSR... --- src/X509.cpp | 2 +- src/X509.h | 2 +- src/simpleOpensslSigner.cpp | 11 +++++++---- src/util.cpp | 2 +- src/util.h | 2 +- test/src/X509Req.cpp | 6 +++--- 6 files changed, 14 insertions(+), 11 deletions(-) diff --git a/src/X509.cpp b/src/X509.cpp index 8bd8137..d5f8bc5 100644 --- a/src/X509.cpp +++ b/src/X509.cpp @@ -52,7 +52,7 @@ std::shared_ptr X509Req::getPkey() { return pk; } -std::shared_ptr X509Req::parse( std::string content ) { +std::shared_ptr X509Req::parseCSR( std::string content ) { std::shared_ptr in = std::shared_ptr( BIO_new_mem_buf( const_cast( content.c_str() ), -1 ), BIO_free ); X509_REQ* req = PEM_read_bio_X509_REQ( in.get(), NULL, NULL, NULL ); diff --git a/src/X509.h b/src/X509.h index dbc6d0a..5f1b76f 100644 --- a/src/X509.h +++ b/src/X509.h @@ -15,7 +15,7 @@ private: X509Req( X509_REQ* csr ); X509Req( std::string spkac ); public: - static std::shared_ptr parse( std::string content ); + static std::shared_ptr parseCSR( std::string content ); static std::shared_ptr parseSPKAC( std::string content ); int verify(); std::shared_ptr getPkey(); diff --git a/src/simpleOpensslSigner.cpp b/src/simpleOpensslSigner.cpp index 1cc0814..0096d5e 100644 --- a/src/simpleOpensslSigner.cpp +++ b/src/simpleOpensslSigner.cpp @@ -105,9 +105,12 @@ std::shared_ptr SimpleOpensslSigner::nextSerial( uint16_t profile ) { throw "Big number math failed while calcing serials."; } - char* serStr = BN_bn2hex( serial.get() ); - writeFile( serStr, "serial" ); - OPENSSL_free( serStr ); + std::shared_ptr serStr = std::shared_ptr( + BN_bn2hex( serial.get() ), + []( char* ref ) { + OPENSSL_free( ref ); + } ); + writeFile( "serial", serStr.get() ); return std::shared_ptr( BN_bin2bn( data.get(), len + 4 + 16 , 0 ), BN_free ); } @@ -122,7 +125,7 @@ std::shared_ptr SimpleOpensslSigner::sign( std::shared_ptrcsr_type == "SPKAC" ) { req = X509Req::parseSPKAC( cert->csr_content ); } else if( cert->csr_type == "CSR" ) { - req = X509Req::parse( cert->csr_content ); + req = X509Req::parseCSR( cert->csr_content ); } else { throw "Error, unknown REQ rype " + ( cert->csr_type ); } diff --git a/src/util.cpp b/src/util.cpp index b174bb0..13443af 100644 --- a/src/util.cpp +++ b/src/util.cpp @@ -2,7 +2,7 @@ #include -void writeFile( const std::string& content, const std::string& name ) { +void writeFile( const std::string& name, const std::string& content ) { std::ofstream file; file.open( name ); diff --git a/src/util.h b/src/util.h index b0d7fce..94ca64f 100644 --- a/src/util.h +++ b/src/util.h @@ -2,5 +2,5 @@ #include -void writeFile( const std::string& content, const std::string& name ); +void writeFile( const std::string& name, const std::string& content ); std::string readFile( const std::string& name ); diff --git a/test/src/X509Req.cpp b/test/src/X509Req.cpp index 1d23062..c6c95ac 100644 --- a/test/src/X509Req.cpp +++ b/test/src/X509Req.cpp @@ -9,17 +9,17 @@ BOOST_AUTO_TEST_SUITE( TestX509Req ) BOOST_AUTO_TEST_CASE( CSR ) { // Testing a valid CSR - std::shared_ptr req( X509Req::parse( readFile( "testdata/test.csr" ) ) ); + std::shared_ptr req( X509Req::parseCSR( readFile( "testdata/test.csr" ) ) ); BOOST_REQUIRE( req ); BOOST_CHECK( req->verify() == 1 ); // Testing a CSR, where the signature content has been tampered with - req = std::shared_ptr( X509Req::parse( readFile( "testdata/test_false_sig.csr" ) ) ); + req = std::shared_ptr( X509Req::parseCSR( readFile( "testdata/test_false_sig.csr" ) ) ); BOOST_REQUIRE( req ); BOOST_CHECK( req->verify() == 0 ); // Testing a CSR, where the signature OID is something strange - req = std::shared_ptr( X509Req::parse( readFile( "testdata/test_invalid_sig.csr" ) ) ); + req = std::shared_ptr( X509Req::parseCSR( readFile( "testdata/test_invalid_sig.csr" ) ) ); BOOST_REQUIRE( req ); BOOST_CHECK( req->verify() < 0 ); } -- 2.39.5