]> WPIA git - cassiopeia.git/commitdiff
upd: add file I/O util funcs.
authorFelix Dörre <felix@dogcraft.de>
Thu, 13 Nov 2014 23:09:55 +0000 (00:09 +0100)
committerBenny Baumann <BenBE@geshi.org>
Sat, 24 Jan 2015 16:39:21 +0000 (17:39 +0100)
src/X509.cpp
src/X509.h
src/main.cpp
src/simpleOpensslSigner.cpp
src/util.cpp [new file with mode: 0644]
src/util.h [new file with mode: 0644]

index 92d7773f22f4cbff36b0b5cb4a179f030018087a..8bd8137f28295350fb275b1f9b74ea606e1f0e6f 100644 (file)
@@ -52,8 +52,8 @@ std::shared_ptr<EVP_PKEY> X509Req::getPkey() {
     return pk;
 }
 
-std::shared_ptr<X509Req> X509Req::parse( std::string filename ) {
-    std::shared_ptr<BIO> in = std::shared_ptr<BIO>( BIO_new_mem_buf( const_cast<char*>( filename.c_str() ), -1 ), BIO_free );
+std::shared_ptr<X509Req> X509Req::parse( std::string content ) {
+    std::shared_ptr<BIO> in = std::shared_ptr<BIO>( BIO_new_mem_buf( const_cast<char*>( content.c_str() ), -1 ), BIO_free );
     X509_REQ* req = PEM_read_bio_X509_REQ( in.get(), NULL, NULL, NULL );
 
     if( !req ) {
index ba565fe20ad27363b641d4277b78f5e28828af96..dbc6d0a0df470a76a4dceea1691ddce034f85d1f 100644 (file)
@@ -15,8 +15,8 @@ private:
     X509Req( X509_REQ* csr );
     X509Req( std::string spkac );
 public:
-    static std::shared_ptr<X509Req> parse( std::string filename );
-    static std::shared_ptr<X509Req> parseSPKAC( std::string filename );
+    static std::shared_ptr<X509Req> parse( std::string content );
+    static std::shared_ptr<X509Req> parseSPKAC( std::string content );
     int verify();
     std::shared_ptr<EVP_PKEY> getPkey();
 };
index cf4f383d843e396b1850e87e2a2aeccc05a9eea9..f4c98a7a16a8fbec632cc3de7f6960d543fa650d 100644 (file)
@@ -8,6 +8,7 @@
 #include "database.h"
 #include "mysql.h"
 #include "simpleOpensslSigner.h"
+#include "util.h"
 
 #ifdef NO_DAEMON
 #define DAEMON false
@@ -27,10 +28,7 @@ std::string writeBackFile( uint32_t serial, std::string cert ) {
     filename += "/" + std::to_string( serial / 1000 );
     mkdir( filename.c_str(), 0755 );
     filename += "/" + std::to_string( serial ) + ".crt";
-    std::ofstream file;
-    file.open( filename.c_str() );
-    file << cert.c_str();
-    file.close();
+    writeFile( filename, cert );
     std::cout << "wrote to " << filename << std::endl;
     return filename;
 }
@@ -45,10 +43,11 @@ int main( int argc, const char* argv[] ) {
     }
 
     std::ifstream config;
-    if(DAEMON){
-      config.open( "/etc/cacert/cassiopeia/cassiopeia.conf" );
-    }else{
-      config.open( "config.txt" );
+
+    if( DAEMON ) {
+        config.open( "/etc/cacert/cassiopeia/cassiopeia.conf" );
+    } else {
+        config.open( "config.txt" );
     }
 
     if( !config.is_open() ) {
@@ -143,8 +142,7 @@ int main( int argc, const char* argv[] ) {
                 }
 
                 std::cout << "Found a CSR at '" << cert->csr << "' signing" << std::endl;
-                std::ifstream t( cert->csr );
-                cert->csr_content = std::string( std::istreambuf_iterator<char>( t ), std::istreambuf_iterator<char>() );
+                cert->csr_content = readFile( cert->csr );
 
                 std::shared_ptr<SignedCertificate> res = sign->sign( cert );
                 std::string fn = writeBackFile( atoi( job->target.c_str() ), res->certificate );
index eb7d8b9e720232989b8a60c5b0bfc792a50d2fe0..1cc08149dba383ae3c6a3ea4fd7737780b7bc547 100644 (file)
@@ -1,7 +1,6 @@
 #include "simpleOpensslSigner.h"
 
 #include <iostream>
-#include <fstream>
 
 #include <openssl/ssl.h>
 #include <openssl/err.h>
@@ -11,6 +10,7 @@
 #include <openssl/x509v3.h>
 
 #include "X509.h"
+#include "util.h"
 
 extern std::vector<Profile> profiles;
 
@@ -74,10 +74,7 @@ SimpleOpensslSigner::~SimpleOpensslSigner() {
 }
 
 std::shared_ptr<BIGNUM> SimpleOpensslSigner::nextSerial( uint16_t profile ) {
-    std::ifstream serialif( "serial" );
-    std::string res;
-    serialif >> res;
-    serialif.close();
+    std::string res = readFile( "serial" );
 
     BIGNUM* bn = 0;
 
@@ -109,9 +106,7 @@ std::shared_ptr<BIGNUM> SimpleOpensslSigner::nextSerial( uint16_t profile ) {
     }
 
     char* serStr = BN_bn2hex( serial.get() );
-    std::ofstream serialf( "serial" );
-    serialf << serStr;
-    serialf.close();
+    writeFile( serStr, "serial" );
     OPENSSL_free( serStr );
 
     return std::shared_ptr<BIGNUM>( BN_bin2bn( data.get(), len + 4 + 16 , 0 ), BN_free );
diff --git a/src/util.cpp b/src/util.cpp
new file mode 100644 (file)
index 0000000..b174bb0
--- /dev/null
@@ -0,0 +1,19 @@
+#include "util.h"
+
+#include <fstream>
+
+void writeFile( const std::string& content, const std::string& name ) {
+    std::ofstream file;
+
+    file.open( name );
+    file << content;
+    file.close();
+}
+
+std::string readFile( const std::string& name ) {
+    std::ifstream t( name );
+    std::string res = std::string( std::istreambuf_iterator<char>( t ), std::istreambuf_iterator<char>() );
+    t.close();
+
+    return res;
+}
diff --git a/src/util.h b/src/util.h
new file mode 100644 (file)
index 0000000..b0d7fce
--- /dev/null
@@ -0,0 +1,6 @@
+#pragma once
+
+#include <string>
+
+void writeFile( const std::string& content, const std::string& name );
+std::string readFile( const std::string& name );