]> WPIA git - cassiopeia.git/blob - src/apps/client.cpp
add: write signed from and to back to db
[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 int main( int argc, const char* argv[] ) {
31     ( void ) argc;
32     ( void ) argv;
33     bool once = false;
34
35     if( argc == 2 && std::string( "--once" ) == std::string( argv[1] ) ) {
36         once = true;
37     }
38
39     std::string path;
40
41 #ifdef NDEBUG
42     path = "/etc/cacert/cassiopeia/cassiopeia.conf";
43 #else
44     path = "config.txt";
45 #endif
46
47     if( parseConfig( path ) != 0 ) {
48         return -1;
49     }
50
51     if( serialPath == "" ) {
52         std::cout << "Error: no serial device is given" << std::endl;
53         return -1;
54     }
55
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() );
62
63     while( true ) {
64         std::shared_ptr<Job> job = jp->fetchJob();
65
66         if( !job ) {
67             std::cout << "Nothing to work on" << std::endl;
68             sleep( 5 );
69             continue;
70         }
71
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(
74             logP,
75             []( std::ofstream * ptr ) {
76                 ( *ptr ).close();
77                 delete ptr;
78             } );
79         std::ofstream& log = *logP;
80
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;
86
87         if( job->task == "sign" ) {
88             try {
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;
92
93                 for( auto& SAN : cert->SANs ) {
94                     log << "INFO: SAN " << SAN->type << ": " << SAN->content;
95                 }
96
97                 for( auto& AVA : cert->AVAs ) {
98                     log << "INFO: AVA " << AVA->name << ": " << AVA->value;
99                 }
100
101                 if( !cert ) {
102                     std::cout << "wasn't able to load CSR" << std::endl;
103                     jp->failJob( job );
104                     continue;
105                 }
106
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;
110
111                 std::shared_ptr<SignedCertificate> res = sign->sign( cert );
112
113                 if( !res ) {
114                     log << "ERROR: The signer failed. There was no certificate." << std::endl;
115                     jp->failJob( job );
116                     continue;
117                 }
118
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 );
122                 res->crt_name = fn;
123                 jp->writeBack( job, res );
124                 log << "FINE: signing done." << std::endl;
125
126                 if( DAEMON ) {
127                     jp->finishJob( job );
128                 }
129
130                 continue;
131             } catch( const char* c ) {
132                 log << "ERROR: " << c << std::endl;
133             } catch( std::string c ) {
134                 log << "ERROR: " << c << std::endl;
135             }
136
137             try {
138                 jp->failJob( job );
139             } catch( const char* c ) {
140                 log << "ERROR: " << c << std::endl;
141             } catch( std::string c ) {
142                 log << "ERROR: " << c << std::endl;
143             }
144         } else if( job->task == "revoke" ) {
145             try {
146                 auto data = jp->getRevocationInfo( job );
147                 std::pair<std::shared_ptr<CRL>, std::string> rev = sign->revoke( CAs.at( data.second ), data.first );
148                 std::string date = rev.second;
149                 const unsigned char* pos = ( const unsigned char* ) date.data();
150                 std::shared_ptr<ASN1_TIME> time( d2i_ASN1_TIME( NULL, &pos, date.size() ), ASN1_TIME_free );
151
152                 jp->writeBackRevocation( job, timeToString( time ) );
153                 jp->finishJob( job );
154             } catch( const char* c ) {
155                 std::cout << "Exception: " << c << std::endl;
156             }
157         } else {
158             log << "Unknown job type" << job->task << std::endl;
159             jp->failJob( job );
160         }
161
162         if( !DAEMON || once ) {
163             return 0;
164         }
165     }
166 }