2 Cassiopeia - CAcert signing module
3 Copyright (C) 2014 CAcert Inc.
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
28 #include "simpleOpensslSigner.h"
37 std::vector<Profile> profiles;
38 std::string sqlHost, sqlUser, sqlPass, sqlDB;
40 std::string writeBackFile( uint32_t serial, std::string cert ) {
41 std::string filename = "keys";
42 mkdir( filename.c_str(), 0755 );
44 mkdir( filename.c_str(), 0755 );
45 filename += "/" + std::to_string( serial / 1000 );
46 mkdir( filename.c_str(), 0755 );
47 filename += "/" + std::to_string( serial ) + ".crt";
49 file.open( filename.c_str() );
55 int main( int argc, const char* argv[] ) {
59 config.open( "config.txt" );
61 if( !config.is_open() ) {
62 std::cerr << "config missing" << std::endl;
68 while( config >> line1 ) {
69 if( line1[0] == '#' ) {
73 int splitter = line1.find( "=" );
75 if( splitter == -1 ) {
76 std::cerr << "Ignoring malformed config line: " << line1 << std::endl;
80 std::string key = line1.substr( 0, splitter );
81 std::string value = line1.substr( splitter + 1 );
83 if( key == "key.directory" ) {
86 } else if( key == "sql.host" ) {
88 } else if( key == "sql.user" ) {
90 } else if( key == "sql.password" ) {
92 } else if( key == "sql.database" ) {
96 if( key.compare( 0, 8, "profile." ) == 0 ) {
97 int numE = key.find( ".", 9 );
100 std::cout << "invalid line: " << line1 << std::endl;
104 unsigned int i = atoi( key.substr( 8, numE - 8 ).c_str() );
105 std::string rest = key.substr( numE + 1 );
107 if( i + 1 > profiles.size() ) {
108 profiles.resize( i + 1 );
111 if( rest == "key" ) {
112 profiles[i].key = value;
113 } else if( rest == "cert" ) {
114 profiles[i].cert = value;
116 std::cout << "invalid line: " << line1 << std::endl;
122 std::cout << profiles.size() << " profiles loaded." << std::endl;
125 std::cerr << "Missing config property key.directory" << std::endl;
131 std::shared_ptr<JobProvider> jp( new MySQLJobProvider( sqlHost, sqlUser, sqlPass, sqlDB ) );
132 std::shared_ptr<Signer> sign( new SimpleOpensslSigner() );
135 std::shared_ptr<Job> job = jp->fetchJob();
138 std::cout << "Nothing to work on" << std::endl;
143 if( job->task == "sign" ) {
145 std::shared_ptr<TBSCertificate> cert = jp->fetchTBSCert( job );
148 std::cout << "wasn't able to load CSR" << std::endl;
152 std::cout << "Found a CSR at '" << cert->csr << "' signing" << std::endl;
153 std::ifstream t( cert->csr );
154 cert->csr_content = std::string( std::istreambuf_iterator<char>( t ), std::istreambuf_iterator<char>() );
156 std::shared_ptr<SignedCertificate> res = sign->sign( cert );
157 std::string fn = writeBackFile( atoi( job->target.c_str() ), res->certificate );
159 jp->writeBack( job, res );
160 } catch( const char* c ) {
161 std::cerr << c << std::endl;
165 std::cout << "Unknown job type" << job->task << std::endl;
168 if( DAEMON && !jp->finishJob( job ) ) {