1 #include "io/recordHandler.h"
6 #include <unordered_map>
8 #include <openssl/ssl.h>
11 #include "io/record.h"
12 #include "io/opensslBIO.h"
13 #include "io/slipBio.h"
15 #include "db/database.h"
16 #include "crypto/remoteSigner.h"
17 #include "crypto/sslUtil.h"
18 #include "crypto/simpleOpensslSigner.h"
20 #include "log/logger.hpp"
22 extern std::vector<Profile> profiles;
23 extern std::unordered_map<std::string, std::shared_ptr<CAConfig>> CAs;
25 class RecordHandlerSession {
28 uint32_t lastCommandCount;
30 std::shared_ptr<TBSCertificate> tbs;
31 std::shared_ptr<SignedCertificate> result;
33 std::shared_ptr<SSL> ssl;
35 std::shared_ptr<OpensslBIOWrapper> io;
36 DefaultRecordHandler* parent;
37 std::shared_ptr<Signer> signer;
39 std::shared_ptr<std::ofstream> log;
40 std::vector<std::string> serials;
42 RecordHandlerSession( DefaultRecordHandler* parent, std::shared_ptr<Signer> signer, std::shared_ptr<SSL_CTX> ctx, std::shared_ptr<BIO> output ) :
44 lastCommandCount( 0 ),
45 tbs( new TBSCertificate() ) {
46 this->parent = parent;
47 this->signer = signer;
50 if( time( &c_time ) == -1 ) {
51 throw "Error while fetching time?";
54 log = openLogfile( std::string( "logs/log_" ) + std::to_string( c_time ) );
56 ssl = std::shared_ptr<SSL>( SSL_new( ctx.get() ), SSL_free );
57 std::shared_ptr<BIO> bio(
58 BIO_new( BIO_f_ssl() ),
62 SSL_set_accept_state( ssl.get() );
63 SSL_set_bio( ssl.get(), output.get(), output.get() );
64 BIO_set_ssl( bio.get(), ssl.get(), BIO_NOCLOSE );
65 io = std::shared_ptr<OpensslBIOWrapper>( new OpensslBIOWrapper( bio ) );
68 void respondCommand( RecordHeader::SignerResult res, std::string payload ) {
70 rh.command = ( uint16_t ) res;
72 rh.command_count = 0; // TODO i++
73 rh.totalLength = payload.size();
74 sendCommand( rh, payload, io, log );
78 std::vector<char> buffer( 2048, 0 );
79 int res = io->read( buffer.data(), buffer.capacity() );
82 logger::error( "Stream error, resetting SSL" );
87 std::string content( buffer.data(), res );
91 std::string payload = parseCommand( head, content, log );
92 execute( head, payload );
93 } catch( const char* msg ) {
95 logger::error( "ERROR: ", msg );
103 void execute( RecordHeader& head, std::string data ) {
104 if( head.totalLength != head.payloadLength || head.offset != 0 ) {
105 throw "Error, chunking not supported yet";
108 switch( ( RecordHeader::SignerCommand ) head.command ) {
109 case RecordHeader::SignerCommand::SET_CSR:
110 tbs->csr_content = data;
111 tbs->csr_type = "CSR";
112 logger::note( "INFO: CSR read:\n", tbs->csr_content );
115 case RecordHeader::SignerCommand::SET_SPKAC:
116 tbs->csr_content = data;
117 tbs->csr_type = "SPKAC";
118 logger::note( "INFO: SPKAC read:\n", tbs->csr_content );
121 case RecordHeader::SignerCommand::SET_SIGNATURE_TYPE:
125 case RecordHeader::SignerCommand::SET_PROFILE:
130 case RecordHeader::SignerCommand::SET_WISH_FROM:
131 tbs->wishFrom = data;
134 case RecordHeader::SignerCommand::SET_WISH_TO:
138 case RecordHeader::SignerCommand::ADD_SAN: {
139 size_t pos = data.find( "," );
141 if( pos == std::string::npos ) {
144 std::shared_ptr<SAN> san( new SAN() );
145 san->type = data.substr( 0, pos );
146 san->content = data.substr( pos + 1 );
147 tbs->SANs.push_back( san );
152 case RecordHeader::SignerCommand::ADD_AVA: {
153 size_t pos = data.find( "," );
155 if( pos == std::string::npos ) {
158 std::shared_ptr<AVA> ava( new AVA() );
159 ava->name = data.substr( 0, pos );
160 ava->value = data.substr( pos + 1 );
161 tbs->AVAs.push_back( ava );
166 case RecordHeader::SignerCommand::ADD_PROOF_LINE:
169 case RecordHeader::SignerCommand::SIGN:
170 result = signer->sign( tbs );
171 logger::note( "INFO: signlog:\n", result->log );
172 logger::note( "INFO: res:\n", result->certificate );
173 respondCommand( RecordHeader::SignerResult::SAVE_LOG, result->log );
176 case RecordHeader::SignerCommand::LOG_SAVED:
178 respondCommand( RecordHeader::SignerResult::SIGNING_CA, result->ca_name );
179 respondCommand( RecordHeader::SignerResult::CERTIFICATE, result->certificate );
182 if( !SSL_shutdown( ssl.get() ) && !SSL_shutdown( ssl.get() ) ) {
183 logger::warn( "ERROR: SSL shutdown failed." );
186 parent->reset(); // Connection ended
190 case RecordHeader::SignerCommand::ADD_SERIAL:
191 serials.push_back( data );
194 case RecordHeader::SignerCommand::REVOKE: {
195 std::string ca = data;
196 auto reqCA = CAs.at( ca );
197 logger::note( "CA found" );
198 std::shared_ptr<CRL> crl;
200 std::tie<std::shared_ptr<CRL>, std::string>( crl, date ) = signer->revoke( reqCA, serials );
202 respondCommand( RecordHeader::SignerResult::REVOKED, date + crl->getSignature() );
207 case RecordHeader::SignerCommand::GET_FULL_CRL: {
208 auto ca = CAs.at( data );
209 CRL c( ca->path + "/ca.crl" );
210 respondCommand( RecordHeader::SignerResult::FULL_CRL, c.toString() );
212 if( !SSL_shutdown( ssl.get() ) && !SSL_shutdown( ssl.get() ) ) {
213 logger::error( "ERROR: SSL shutdown failed." );
216 parent->reset(); // Connection ended
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 logger::note( "New session allocated." );
237 currentSession = std::shared_ptr<RecordHandlerSession>( new RecordHandlerSession( this, signer, ctx, bio ) );
240 currentSession->work();