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 ( ( 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::ofstream* logP = new std::ofstream( std::string( "logs/" ) + job->id + std::string( "_" ) + job->warning + std::string( ".log" ) );
111 std::shared_ptr<std::ofstream> logPtr(
113 []( std::ofstream * ptr ) {
117 std::ofstream& log = *logP;
119 sign->setLog( logPtr );
120 log << "TASK ID: " << job->id << std::endl;
121 log << "TRY: " << job->warning << std::endl;
122 log << "TARGET: " << job->target << std::endl;
123 log << "TASK: " << job->task << std::endl << std::endl;
125 if( job->task == "sign" ) {
127 std::shared_ptr<TBSCertificate> cert = jp->fetchTBSCert( job );
128 cert->wishFrom = job->from;
129 cert->wishTo = job->to;
130 log << "INFO: message digest: " << cert->md << std::endl;
131 log << "INFO: profile id: " << cert->profile << std::endl;
133 for( auto& SAN : cert->SANs ) {
134 log << "INFO: SAN " << SAN->type << ": " << SAN->content;
137 for( auto& AVA : cert->AVAs ) {
138 log << "INFO: AVA " << AVA->name << ": " << AVA->value;
142 std::cout << "wasn't able to load CSR" << std::endl;
147 log << "FINE: Found the CSR at '" << cert->csr << "'" << std::endl;
148 cert->csr_content = readFile( keyDir + "/../" + cert->csr );
149 log << "FINE: CSR is " << std::endl << cert->csr_content << std::endl;
151 std::shared_ptr<SignedCertificate> res = sign->sign( cert );
154 log << "ERROR: The signer failed. There was no certificate." << std::endl;
159 log << "FINE: CERTIFICATE LOG: " << res->log << std::endl;
160 log << "FINE: CERTIFICATE:" << std::endl << res->certificate << std::endl;
161 std::string fn = writeBackFile( job->target.c_str(), res->certificate, keyDir );
163 jp->writeBack( job, res );
164 log << "FINE: signing done." << std::endl;
167 jp->finishJob( job );
171 } catch( const char* c ) {
172 log << "ERROR: " << c << std::endl;
173 } catch( std::string c ) {
174 log << "ERROR: " << c << std::endl;
179 } catch( const char* c ) {
180 log << "ERROR: " << c << std::endl;
181 } catch( std::string c ) {
182 log << "ERROR: " << c << std::endl;
184 } else if( job->task == "revoke" ) {
186 auto data = jp->getRevocationInfo( job );
187 std::vector<std::string> serials;
188 serials.push_back( data.first );
189 std::pair<std::shared_ptr<CRL>, std::string> rev = sign->revoke( CAs.at( data.second ), serials );
190 std::string date = rev.second;
191 const unsigned char* pos = ( const unsigned char* ) date.data();
192 std::shared_ptr<ASN1_TIME> time( d2i_ASN1_TIME( NULL, &pos, date.size() ), ASN1_TIME_free );
194 jp->writeBackRevocation( job, timeToString( time ) );
195 jp->finishJob( job );
196 } catch( const char* c ) {
197 std::cout << "Exception: " << c << std::endl;
200 log << "Unknown job type" << job->task << std::endl;
204 if( !DAEMON || once ) {