]> WPIA git - cassiopeia.git/blob - src/apps/client.cpp
add: Implement automatic re-signing of the CRL
[cassiopeia.git] / src / apps / client.cpp
1 #include <sys/stat.h>
2 #include <unistd.h>
3
4 #include <iostream>
5 #include <fstream>
6 #include <streambuf>
7 #include <unordered_map>
8
9 #include "db/database.h"
10 #include "db/mysql.h"
11 #include "crypto/simpleOpensslSigner.h"
12 #include "crypto/remoteSigner.h"
13 #include "crypto/sslUtil.h"
14 #include "util.h"
15 #include "io/bios.h"
16 #include "io/slipBio.h"
17 #include "config.h"
18
19 #ifdef NO_DAEMON
20 #define DAEMON false
21 #else
22 #define DAEMON true
23 #endif
24
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;
29
30 void checkCRLs( std::shared_ptr<Signer> sign ) {
31     std::cout << "Signing CRLs" << std::endl;
32
33     for( auto x : CAs ) {
34         std::cout << "Checking: " << x.first << std::endl;
35
36         if( !x.second->crlNeedsResign() ) {
37             std::cout << "Skipping Resigning CRL: " + x.second->name << std::endl;
38             continue;
39         }
40
41         std::cout << "Resigning CRL: " + x.second->name << std::endl;
42
43         try {
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;
48         }
49     }
50 }
51
52 int main( int argc, const char* argv[] ) {
53     ( void ) argc;
54     ( void ) argv;
55     bool once = false;
56
57     if( argc == 2 && std::string( "--once" ) == std::string( argv[1] ) ) {
58         once = true;
59     }
60
61     std::string path;
62
63 #ifdef NDEBUG
64     path = "/etc/cacert/cassiopeia/cassiopeia.conf";
65 #else
66     path = "config.txt";
67 #endif
68
69     if( parseConfig( path ) != 0 ) {
70         return -1;
71     }
72
73     if( serialPath == "" ) {
74         std::cout << "Error: no serial device is given" << std::endl;
75         return -1;
76     }
77
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() );
84
85     time_t lastCRLCheck = 0;
86
87     while( true ) {
88         time_t current;
89         time( &current );
90
91         if( lastCRLCheck + 30 * 60 < current ) {
92             // todo set good log TODO FIXME
93             sign->setLog( std::shared_ptr<std::ostream>(
94                 &std::cout,
95                 []( std::ostream * o ) {
96                     ( void ) o;
97                 } ) );
98             checkCRLs( sign );
99             lastCRLCheck = current;
100         }
101
102         std::shared_ptr<Job> job = jp->fetchJob();
103
104         if( !job ) {
105             std::cout << "Nothing to work on" << std::endl;
106             sleep( 5 );
107             continue;
108         }
109
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(
112             logP,
113             []( std::ofstream * ptr ) {
114                 ( *ptr ).close();
115                 delete ptr;
116             } );
117         std::ofstream& log = *logP;
118
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;
124
125         if( job->task == "sign" ) {
126             try {
127                 std::shared_ptr<TBSCertificate> cert = jp->fetchTBSCert( job );
128                 log << "INFO: message digest: " << cert->md << std::endl;
129                 log << "INFO: profile id: " << cert->profile << std::endl;
130
131                 for( auto& SAN : cert->SANs ) {
132                     log << "INFO: SAN " << SAN->type << ": " << SAN->content;
133                 }
134
135                 for( auto& AVA : cert->AVAs ) {
136                     log << "INFO: AVA " << AVA->name << ": " << AVA->value;
137                 }
138
139                 if( !cert ) {
140                     std::cout << "wasn't able to load CSR" << std::endl;
141                     jp->failJob( job );
142                     continue;
143                 }
144
145                 log << "FINE: Found the CSR at '" << cert->csr << "'" << std::endl;
146                 cert->csr_content = readFile( keyDir + "/../" + cert->csr );
147                 log << "FINE: CSR is " << std::endl << cert->csr_content << std::endl;
148
149                 std::shared_ptr<SignedCertificate> res = sign->sign( cert );
150
151                 if( !res ) {
152                     log << "ERROR: The signer failed. There was no certificate." << std::endl;
153                     jp->failJob( job );
154                     continue;
155                 }
156
157                 log << "FINE: CERTIFICATE LOG: " << res->log << std::endl;
158                 log << "FINE: CERTIFICATE:" << std::endl << res->certificate << std::endl;
159                 std::string fn = writeBackFile( job->target.c_str(), res->certificate, keyDir );
160                 res->crt_name = fn;
161                 jp->writeBack( job, res );
162                 log << "FINE: signing done." << std::endl;
163
164                 if( DAEMON ) {
165                     jp->finishJob( job );
166                 }
167
168                 continue;
169             } catch( const char* c ) {
170                 log << "ERROR: " << c << std::endl;
171             } catch( std::string c ) {
172                 log << "ERROR: " << c << std::endl;
173             }
174
175             try {
176                 jp->failJob( job );
177             } catch( const char* c ) {
178                 log << "ERROR: " << c << std::endl;
179             } catch( std::string c ) {
180                 log << "ERROR: " << c << std::endl;
181             }
182         } else if( job->task == "revoke" ) {
183             try {
184                 auto data = jp->getRevocationInfo( job );
185                 std::vector<std::string> serials;
186                 serials.push_back( data.first );
187                 std::pair<std::shared_ptr<CRL>, std::string> rev = sign->revoke( CAs.at( data.second ), serials );
188                 std::string date = rev.second;
189                 const unsigned char* pos = ( const unsigned char* ) date.data();
190                 std::shared_ptr<ASN1_TIME> time( d2i_ASN1_TIME( NULL, &pos, date.size() ), ASN1_TIME_free );
191
192                 jp->writeBackRevocation( job, timeToString( time ) );
193                 jp->finishJob( job );
194             } catch( const char* c ) {
195                 std::cout << "Exception: " << c << std::endl;
196             }
197         } else {
198             log << "Unknown job type" << job->task << std::endl;
199             jp->failJob( job );
200         }
201
202         if( !DAEMON || once ) {
203             return 0;
204         }
205     }
206 }