10 #include "simpleOpensslSigner.h"
14 #include "remoteSigner.h"
24 std::vector<Profile> profiles;
25 std::string sqlHost, sqlUser, sqlPass, sqlDB;
27 std::string writeBackFile( uint32_t serial, std::string cert ) {
28 std::string filename = "keys";
29 mkdir( filename.c_str(), 0755 );
31 mkdir( filename.c_str(), 0755 );
32 filename += "/" + std::to_string( serial / 1000 );
33 mkdir( filename.c_str(), 0755 );
34 filename += "/" + std::to_string( serial ) + ".crt";
35 writeFile( filename, cert );
36 std::cout << "wrote to " << filename << std::endl;
44 config.open( "/etc/cacert/cassiopeia/cassiopeia.conf" );
46 config.open( "config.txt" );
49 if( !config.is_open() ) {
50 std::cerr << "config missing" << std::endl;
56 while( config >> line1 ) {
57 if( line1[0] == '#' ) {
61 int splitter = line1.find( "=" );
63 if( splitter == -1 ) {
64 std::cerr << "Ignoring malformed config line: " << line1 << std::endl;
68 std::string key = line1.substr( 0, splitter );
69 std::string value = line1.substr( splitter + 1 );
71 if( key == "key.directory" ) {
74 } else if( key == "sql.host" ) {
76 } else if( key == "sql.user" ) {
78 } else if( key == "sql.password" ) {
80 } else if( key == "sql.database" ) {
84 if( key.compare( 0, 8, "profile." ) == 0 ) {
85 int numE = key.find( ".", 9 );
88 std::cout << "invalid line: " << line1 << std::endl;
92 unsigned int i = atoi( key.substr( 8, numE - 8 ).c_str() );
93 std::string rest = key.substr( numE + 1 );
95 if( i + 1 > profiles.size() ) {
96 profiles.resize( i + 1 );
100 profiles[i].key = value;
101 } else if( rest == "cert" ) {
102 profiles[i].cert = value;
104 std::cout << "invalid line: " << line1 << std::endl;
110 std::cout << profiles.size() << " profiles loaded." << std::endl;
113 std::cerr << "Missing config property key.directory" << std::endl;
122 int handlermain( int argc, const char* argv[] );
124 int main( int argc, const char* argv[] ) {
129 if( argc == 2 && std::string( "--once" ) == std::string( argv[1] ) ) {
133 if( parseConfig() != 0 ) {
138 return handlermain( argc, argv );
141 std::shared_ptr<JobProvider> jp( new MySQLJobProvider( sqlHost, sqlUser, sqlPass, sqlDB ) );
142 std::shared_ptr<BIO> b = openSerial( "/dev/ttyUSB0" );
143 std::shared_ptr<BIO> slip1( BIO_new( toBio<SlipBIO>() ), BIO_free );
144 ( ( SlipBIO* )slip1->ptr )->setTarget( std::shared_ptr<OpensslBIO>( new OpensslBIOWrapper( b ) ) );
145 std::shared_ptr<RemoteSigner> sign( new RemoteSigner( slip1, generateSSLContext( false ) ) );
146 // std::shared_ptr<Signer> sign( new SimpleOpensslSigner() );
149 std::shared_ptr<Job> job = jp->fetchJob();
152 std::cout << "Nothing to work on" << std::endl;
157 if( job->task == "sign" ) {
159 std::shared_ptr<TBSCertificate> cert = jp->fetchTBSCert( job );
162 std::cout << "wasn't able to load CSR" << std::endl;
166 std::cout << "Found a CSR at '" << cert->csr << "' signing" << std::endl;
167 cert->csr_content = readFile( cert->csr );
168 std::cout << cert->csr_content << " content " << std::endl;
170 std::shared_ptr<SignedCertificate> res = sign->sign( cert );
171 std::cout << "did it!" << res->certificate << std::endl;
172 std::string fn = writeBackFile( atoi( job->target.c_str() ), res->certificate );
174 jp->writeBack( job, res );
175 std::cout << "wrote back" << std::endl;
176 } catch( const char* c ) {
177 std::cerr << "ERROR: " << c << std::endl;
179 } catch( std::string c ) {
180 std::cerr << "ERROR: " << c << std::endl;
184 std::cout << "Unknown job type" << job->task << std::endl;
187 if( DAEMON && !jp->finishJob( job ) ) {
191 if( !DAEMON || once ) {