1 #include "io/recordHandler.h"
6 #include <unordered_map>
8 #include <openssl/ssl.h>
10 #include "io/record.h"
11 #include "io/opensslBIO.h"
12 #include "io/slipBio.h"
14 #include "db/database.h"
15 #include "crypto/remoteSigner.h"
16 #include "crypto/sslUtil.h"
18 #include "crypto/simpleOpensslSigner.h"
20 extern std::vector<Profile> profiles;
21 extern std::unordered_map<std::string, std::shared_ptr<CAConfig>> CAs;
23 class RecordHandlerSession {
26 uint32_t lastCommandCount;
28 std::shared_ptr<TBSCertificate> tbs;
29 std::shared_ptr<SignedCertificate> result;
31 std::shared_ptr<SSL> ssl;
33 std::shared_ptr<OpensslBIOWrapper> io;
34 DefaultRecordHandler* parent;
35 std::shared_ptr<Signer> signer;
37 std::shared_ptr<std::ofstream> log;
38 std::vector<std::string> serials;
40 RecordHandlerSession( DefaultRecordHandler* parent, std::shared_ptr<Signer> signer, std::shared_ptr<SSL_CTX> ctx, std::shared_ptr<BIO> output ) :
42 lastCommandCount( 0 ),
43 tbs( new TBSCertificate() ){
44 this->parent = parent;
45 this->signer = signer;
48 if( time( &c_time ) == -1 ) {
49 throw "Error while fetching time?";
52 log = std::shared_ptr<std::ofstream>(
53 new std::ofstream( std::string( "logs/log_" ) + std::to_string( c_time ) ),
54 []( std::ofstream * ptr ) {
59 ssl = std::shared_ptr<SSL>( SSL_new( ctx.get() ), SSL_free );
60 std::shared_ptr<BIO> bio(
61 BIO_new( BIO_f_ssl() ),
65 SSL_set_accept_state( ssl.get() );
66 SSL_set_bio( ssl.get(), output.get(), output.get() );
67 BIO_set_ssl( bio.get(), ssl.get(), BIO_NOCLOSE );
68 io = std::shared_ptr<OpensslBIOWrapper>( new OpensslBIOWrapper( bio ) );
71 void respondCommand( RecordHeader::SignerResult res, std::string payload ) {
73 rh.command = ( uint16_t ) res;
75 rh.command_count = 0; // TODO i++
76 rh.totalLength = payload.size();
77 sendCommand( rh, payload, io, log );
81 std::vector<char> buffer( 2048, 0 );
82 int res = io->read( buffer.data(), buffer.capacity() );
85 ( *log ) << "Stream error, resetting SSL" << std::endl;
90 std::string content( buffer.data(), res );
94 std::string payload = parseCommand( head, content, log );
95 execute( head, payload );
96 } catch( const char* msg ) {
98 ( *log ) << "ERROR: " << msg << std::endl;
106 void execute( RecordHeader& head, std::string data ) {
107 if( head.totalLength != head.payloadLength || head.offset != 0 ) {
108 throw "Error, chunking not supported yet";
111 switch( ( RecordHeader::SignerCommand ) head.command ) {
112 case RecordHeader::SignerCommand::SET_CSR:
113 tbs->csr_content = data;
114 tbs->csr_type = "CSR";
115 ( *log ) << "INFO: CSR read: " << tbs->csr_content << std::endl;
118 case RecordHeader::SignerCommand::SET_SPKAC:
119 tbs->csr_content = data;
120 tbs->csr_type = "SPKAC";
121 ( *log ) << "INFO: SPKAC read: " << tbs->csr_content << std::endl;
124 case RecordHeader::SignerCommand::SET_SIGNATURE_TYPE:
128 case RecordHeader::SignerCommand::SET_PROFILE:
133 case RecordHeader::SignerCommand::SET_WISH_FROM:
134 tbs->wishFrom = data;
137 case RecordHeader::SignerCommand::SET_WISH_TO:
141 case RecordHeader::SignerCommand::ADD_SAN: {
142 size_t pos = data.find( "," );
144 if( pos == std::string::npos ) {
147 std::shared_ptr<SAN> san( new SAN() );
148 san->type = data.substr( 0, pos );
149 san->content = data.substr( pos + 1 );
150 tbs->SANs.push_back( san );
155 case RecordHeader::SignerCommand::ADD_AVA: {
156 size_t pos = data.find( "," );
158 if( pos == std::string::npos ) {
161 std::shared_ptr<AVA> ava( new AVA() );
162 ava->name = data.substr( 0, pos );
163 ava->value = data.substr( pos + 1 );
164 tbs->AVAs.push_back( ava );
169 case RecordHeader::SignerCommand::ADD_PROOF_LINE:
172 case RecordHeader::SignerCommand::SIGN:
173 result = signer->sign( tbs );
174 ( *log ) << "INFO: signlog: " << result->log << std::endl;
175 ( *log ) << "INFO: res: " << result->certificate << std::endl;
176 respondCommand( RecordHeader::SignerResult::SAVE_LOG, result->log );
179 case RecordHeader::SignerCommand::LOG_SAVED:
181 respondCommand( RecordHeader::SignerResult::SIGNING_CA, result->ca_name );
182 respondCommand( RecordHeader::SignerResult::CERTIFICATE, result->certificate );
185 if( !SSL_shutdown( ssl.get() ) && !SSL_shutdown( ssl.get() ) ) {
186 ( *log ) << "ERROR: SSL close failed" << std::endl;
191 case RecordHeader::SignerCommand::ADD_SERIAL:
192 serials.push_back( data );
195 case RecordHeader::SignerCommand::REVOKE: {
196 std::string ca = data;
197 auto reqCA = CAs.at( ca );
198 ( *log ) << "CA found" << std::endl;
199 std::shared_ptr<CRL> crl;
201 std::tie<std::shared_ptr<CRL>, std::string>( crl, date ) = signer->revoke( reqCA, serials );
203 respondCommand( RecordHeader::SignerResult::REVOKED, date + crl->getSignature() );
208 case RecordHeader::SignerCommand::GET_FULL_CRL: {
209 auto ca = CAs.at( data );
210 CRL c( ca->path + "/ca.crl" );
211 respondCommand( RecordHeader::SignerResult::FULL_CRL, c.toString() );
213 if( !SSL_shutdown( ssl.get() ) && !SSL_shutdown( ssl.get() ) ) {
214 ( *log ) << "ERROR: SSL close failed" << std::endl;
221 throw "Unimplemented";
226 DefaultRecordHandler::DefaultRecordHandler( std::shared_ptr<Signer> signer, std::shared_ptr<BIO> bio )
227 : bio( bio ), ctx( generateSSLContext( true ) ), signer( signer ), currentSession() {
230 void DefaultRecordHandler::reset() {
231 currentSession = std::shared_ptr<RecordHandlerSession>();
234 void DefaultRecordHandler::handle() {
235 if( !currentSession ) {
236 std::cout << "session allocated" << std::endl;
237 currentSession = std::shared_ptr<RecordHandlerSession>( new RecordHandlerSession( this, signer, ctx, bio ) );
240 currentSession->work();