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 void checkCRLs( std::shared_ptr<Signer> sign ) {
31 std::cout << "Signing CRLs" << std::endl;
34 std::cout << "Checking: " << x.first << std::endl;
36 if( !x.second->crlNeedsResign() ) {
37 std::cout << "Skipping Resigning CRL: " + x.second->name << std::endl;
41 std::cout << "Resigning CRL: " + x.second->name << std::endl;
44 std::vector<std::string> serials;
45 std::pair<std::shared_ptr<CRL>, std::string> rev = sign->revoke( x.second, serials );
46 } catch( const char* c ) {
47 std::cout << "Exception: " << c << std::endl;
52 int main( int argc, const char* argv[] ) {
57 if( argc == 2 && std::string( "--once" ) == std::string( argv[1] ) ) {
64 path = "/etc/cacert/cassiopeia/cassiopeia.conf";
69 if( parseConfig( path ) != 0 ) {
73 if( serialPath == "" ) {
74 std::cout << "Error: no serial device is given" << std::endl;
78 std::shared_ptr<JobProvider> jp( new MySQLJobProvider( sqlHost, sqlUser, sqlPass, sqlDB ) );
79 std::shared_ptr<BIO> b = openSerial( serialPath );
80 std::shared_ptr<BIO> slip1( BIO_new( toBio<SlipBIO>() ), BIO_free );
81 static_cast<SlipBIO*>( slip1->ptr )->setTarget( std::shared_ptr<OpensslBIO>( new OpensslBIOWrapper( b ) ) );
82 std::shared_ptr<RemoteSigner> sign( new RemoteSigner( slip1, generateSSLContext( false ) ) );
83 // std::shared_ptr<Signer> sign( new SimpleOpensslSigner() );
85 time_t lastCRLCheck = 0;
91 if( lastCRLCheck + 30 * 60 < current ) {
92 // todo set good log TODO FIXME
93 sign->setLog( std::shared_ptr<std::ostream>(
95 []( std::ostream * o ) {
99 lastCRLCheck = current;
102 std::shared_ptr<Job> job = jp->fetchJob();
105 std::cout << "Nothing to work on" << std::endl;
110 std::shared_ptr<std::ofstream> logPtr = openLogfile( std::string( "logs/" ) + job->id + std::string( "_" ) + job->warning + std::string( ".log" ) );
112 std::ofstream& log = *( logPtr.get() );
114 sign->setLog( logPtr );
115 log << "TASK ID: " << job->id << std::endl;
116 log << "TRY: " << job->warning << std::endl;
117 log << "TARGET: " << job->target << std::endl;
118 log << "TASK: " << job->task << std::endl << std::endl;
120 if( job->task == "sign" ) {
122 std::shared_ptr<TBSCertificate> cert = jp->fetchTBSCert( job );
123 cert->wishFrom = job->from;
124 cert->wishTo = job->to;
125 log << "INFO: message digest: " << cert->md << std::endl;
126 log << "INFO: profile id: " << cert->profile << std::endl;
128 for( auto& SAN : cert->SANs ) {
129 log << "INFO: SAN " << SAN->type << ": " << SAN->content;
132 for( auto& AVA : cert->AVAs ) {
133 log << "INFO: AVA " << AVA->name << ": " << AVA->value;
137 std::cout << "wasn't able to load CSR" << std::endl;
142 log << "FINE: Found the CSR at '" << cert->csr << "'" << std::endl;
143 cert->csr_content = readFile( keyDir + "/../" + cert->csr );
144 log << "FINE: CSR is " << std::endl << cert->csr_content << std::endl;
146 std::shared_ptr<SignedCertificate> res = sign->sign( cert );
149 log << "ERROR: The signer failed. There was no certificate." << std::endl;
154 log << "FINE: CERTIFICATE LOG: " << res->log << std::endl;
155 log << "FINE: CERTIFICATE:" << std::endl << res->certificate << std::endl;
156 std::string fn = writeBackFile( job->target.c_str(), res->certificate, keyDir );
159 log << "ERROR: Writeback of the certificate failed." << std::endl;
165 jp->writeBack( job, res ); //! \FIXME: Check return value
166 log << "FINE: signing done." << std::endl;
169 jp->finishJob( job );
173 } catch( const char* c ) {
174 log << "ERROR: " << c << std::endl;
175 } catch( std::string& c ) {
176 log << "ERROR: " << c << std::endl;
181 } catch( const char* c ) {
182 log << "ERROR: " << c << std::endl;
183 } catch( std::string& c ) {
184 log << "ERROR: " << c << std::endl;
186 } else if( job->task == "revoke" ) {
188 auto data = jp->getRevocationInfo( job );
189 std::vector<std::string> serials;
190 serials.push_back( data.first );
191 std::pair<std::shared_ptr<CRL>, std::string> rev = sign->revoke( CAs.at( data.second ), serials );
192 std::string date = rev.second;
193 const unsigned char* pos = ( const unsigned char* ) date.data();
194 std::shared_ptr<ASN1_TIME> time( d2i_ASN1_TIME( NULL, &pos, date.size() ), ASN1_TIME_free );
196 jp->writeBackRevocation( job, timeToString( time ) );
197 jp->finishJob( job );
198 } catch( const char* c ) {
199 std::cout << "Exception: " << c << std::endl;
200 } catch( const std::string& c ) {
201 std::cout << "Exception: " << c << std::endl;
204 log << "Unknown job type" << job->task << std::endl;
208 if( !DAEMON || once ) {