]> WPIA git - cassiopeia.git/blob - src/main.cpp
add: Aim for initial interoperatibility with Gigi
[cassiopeia.git] / src / main.cpp
1 /*
2     Cassiopeia - CAcert signing module
3     Copyright (C) 2014  CAcert Inc.
4
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.
9
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.
14
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.
18  */
19 #include <sys/stat.h>
20
21 #include <iostream>
22 #include <fstream>
23 #include <streambuf>
24
25 #include "database.h"
26 #include "mysql.h"
27 #include "simpleOpensslSigner.h"
28
29 std::string writeBackFile( uint32_t serial, std::string cert ) {
30     std::string filename = "keys";
31     mkdir( filename.c_str(), 0755 );
32     filename += "/crt";
33     mkdir( filename.c_str(), 0755 );
34     filename += "/" + std::to_string( serial / 1000 );
35     mkdir( filename.c_str(), 0755 );
36     filename += "/" + std::to_string( serial ) + ".crt";
37     std::ofstream file;
38     file.open( filename.c_str() );
39     file << cert.c_str();
40     file.close();
41     return filename;
42 }
43
44 int main( int argc, const char* argv[] ) {
45     if( argc < 2 ) {
46         std::cout << argv[0] << " password" << std::endl;
47         return 1;
48     }
49
50     std::shared_ptr<JobProvider> jp( new MySQLJobProvider( "localhost", "cacert", argv[1], "cacert" ) );
51     std::shared_ptr<Signer> sign( new SimpleOpensslSigner() );
52     std::shared_ptr<Job> job = jp->fetchJob();
53
54     if( !job ) {
55         std::cout << "Nothing to work on" << std::endl;
56         return 2;
57     }
58
59     if( job->task == "sign" ) {
60         try {
61             std::shared_ptr<TBSCertificate> cert = jp->fetchTBSCert( job );
62
63             if( !cert ) {
64                 std::cout << "wasn't able to load CSR" << std::endl;
65                 return 2;
66             }
67
68             std::cout << "Found a CSR at '" << cert->csr << "' signing" << std::endl;
69             std::ifstream t( cert->csr );
70             cert->csr_content = std::string( std::istreambuf_iterator<char>( t ), std::istreambuf_iterator<char>() );
71
72             std::shared_ptr<SignedCertificate> res = sign->sign( cert );
73             std::string fn = writeBackFile( res->serial, res->certificate );
74             res->crt_name = fn;
75             jp->writeBack( job, res );
76         } catch( const char* c ) {
77             std::cerr << c << std::endl;
78             return 2;
79         }
80     }
81
82     if( !jp->finishJob( job ) ) {
83         return 1;
84     }
85
86     return 0;
87 }