7 #include <unordered_map>
9 #include "db/database.h"
11 #include "crypto/simpleOpensslSigner.h"
12 #include "crypto/remoteSigner.h"
13 #include "crypto/sslUtil.h"
16 #include "io/slipBio.h"
25 extern std::string keyDir;
26 extern std::string sqlHost, sqlUser, sqlPass, sqlDB;
27 extern std::string serialPath;
28 extern std::unordered_map<std::string, std::shared_ptr<CAConfig>> CAs;
30 int main( int argc, const char* argv[] ) {
35 if( argc == 2 && std::string( "--once" ) == std::string( argv[1] ) ) {
42 path = "/etc/cacert/cassiopeia/cassiopeia.conf";
47 if( parseConfig( path ) != 0 ) {
51 if( serialPath == "" ) {
52 std::cout << "Error: no serial device is given" << std::endl;
56 std::shared_ptr<JobProvider> jp( new MySQLJobProvider( sqlHost, sqlUser, sqlPass, sqlDB ) );
57 std::shared_ptr<BIO> b = openSerial( serialPath );
58 std::shared_ptr<BIO> slip1( BIO_new( toBio<SlipBIO>() ), BIO_free );
59 ( ( SlipBIO* )slip1->ptr )->setTarget( std::shared_ptr<OpensslBIO>( new OpensslBIOWrapper( b ) ) );
60 std::shared_ptr<RemoteSigner> sign( new RemoteSigner( slip1, generateSSLContext( false ) ) );
61 // std::shared_ptr<Signer> sign( new SimpleOpensslSigner() );
64 std::shared_ptr<Job> job = jp->fetchJob();
67 std::cout << "Nothing to work on" << std::endl;
72 std::ofstream* logP = new std::ofstream( std::string( "logs/" ) + job->id + std::string( "_" ) + job->warning + std::string( ".log" ) );
73 std::shared_ptr<std::ofstream> logPtr(
75 []( std::ofstream * ptr ) {
79 std::ofstream& log = *logP;
81 sign->setLog( logPtr );
82 log << "TASK ID: " << job->id << std::endl;
83 log << "TRY: " << job->warning << std::endl;
84 log << "TARGET: " << job->target << std::endl;
85 log << "TASK: " << job->task << std::endl << std::endl;
87 if( job->task == "sign" ) {
89 std::shared_ptr<TBSCertificate> cert = jp->fetchTBSCert( job );
90 log << "INFO: message digest: " << cert->md << std::endl;
91 log << "INFO: profile id: " << cert->profile << std::endl;
93 for( auto& SAN : cert->SANs ) {
94 log << "INFO: SAN " << SAN->type << ": " << SAN->content;
97 for( auto& AVA : cert->AVAs ) {
98 log << "INFO: AVA " << AVA->name << ": " << AVA->value;
102 std::cout << "wasn't able to load CSR" << std::endl;
107 log << "FINE: Found the CSR at '" << cert->csr << "'" << std::endl;
108 cert->csr_content = readFile( keyDir + "/../" + cert->csr );
109 log << "FINE: CSR is " << std::endl << cert->csr_content << std::endl;
111 std::shared_ptr<SignedCertificate> res = sign->sign( cert );
114 log << "ERROR: The signer failed. There was no certificate." << std::endl;
119 log << "FINE: CERTIFICATE LOG: " << res->log << std::endl;
120 log << "FINE: CERTIFICATE:" << std::endl << res->certificate << std::endl;
121 std::string fn = writeBackFile( job->target.c_str(), res->certificate, keyDir );
123 jp->writeBack( job, res );
124 log << "FINE: signing done." << std::endl;
127 jp->finishJob( job );
131 } catch( const char* c ) {
132 log << "ERROR: " << c << std::endl;
133 } catch( std::string c ) {
134 log << "ERROR: " << c << std::endl;
139 } catch( const char* c ) {
140 log << "ERROR: " << c << std::endl;
141 } catch( std::string c ) {
142 log << "ERROR: " << c << std::endl;
144 } else if( job->task == "revoke" ) {
146 auto data = jp->getRevocationInfo( job );
147 std::vector<std::string> serials;
148 serials.push_back( data.first );
149 std::pair<std::shared_ptr<CRL>, std::string> rev = sign->revoke( CAs.at( data.second ), serials );
150 std::string date = rev.second;
151 const unsigned char* pos = ( const unsigned char* ) date.data();
152 std::shared_ptr<ASN1_TIME> time( d2i_ASN1_TIME( NULL, &pos, date.size() ), ASN1_TIME_free );
154 jp->writeBackRevocation( job, timeToString( time ) );
155 jp->finishJob( job );
156 } catch( const char* c ) {
157 std::cout << "Exception: " << c << std::endl;
160 log << "Unknown job type" << job->task << std::endl;
164 if( !DAEMON || once ) {