1 #include "io/recordHandler.h"
7 #include <unordered_map>
9 #include <openssl/ssl.h>
12 #include "io/record.h"
13 #include "io/opensslBIO.h"
14 #include "io/slipBio.h"
16 #include "db/database.h"
17 #include "crypto/remoteSigner.h"
18 #include "crypto/sslUtil.h"
19 #include "crypto/simpleOpensslSigner.h"
21 #include "log/logger.hpp"
23 extern std::vector<Profile> profiles;
24 extern std::unordered_map<std::string, std::shared_ptr<CAConfig>> CAs;
26 class RecordHandlerSession {
29 uint32_t lastCommandCount = 0;
31 std::shared_ptr<TBSCertificate> tbs;
32 std::shared_ptr<SignedCertificate> result;
34 std::shared_ptr<SSL> ssl;
36 std::shared_ptr<OpensslBIOWrapper> io;
37 DefaultRecordHandler* parent;
38 std::shared_ptr<Signer> signer;
40 std::unique_ptr<std::ofstream> logFile;
41 //std::stringstream sessionlog;
42 std::vector<std::string> serials;
43 logger::logger_set logger;
46 RecordHandlerSession( DefaultRecordHandler* parent, std::shared_ptr<Signer> signer, std::shared_ptr<SSL_CTX> ctx, std::shared_ptr<BIO> output ) :
47 tbs( std::make_shared<TBSCertificate>() ),
48 logFile( openLogfile( "logs/log_" + timestamp() ) ),
49 logger{ std::cout, *logFile } {
50 this->parent = parent;
51 this->signer = signer;
53 ssl = std::shared_ptr<SSL>( SSL_new( ctx.get() ), SSL_free );
54 std::shared_ptr<BIO> bio(
55 BIO_new( BIO_f_ssl() ),
59 SSL_set_accept_state( ssl.get() );
60 SSL_set_bio( ssl.get(), output.get(), output.get() );
61 BIO_set_ssl( bio.get(), ssl.get(), BIO_NOCLOSE );
62 io = std::make_shared<OpensslBIOWrapper>( bio );
65 void respondCommand( RecordHeader::SignerResult res, std::string payload ) {
67 rh.command = static_cast<uint16_t>( res );
69 rh.command_count = 0; // TODO i++
70 sendCommand( rh, payload, io );
76 std::string all = parseCommandChunked( head, io );
77 execute( static_cast<RecordHeader::SignerCommand>( head.command ), all );
78 } catch( const std::exception& msg ) {
79 logger::error( "ERROR: ", msg.what() );
85 void execute( RecordHeader::SignerCommand command, std::string data ) {
87 case RecordHeader::SignerCommand::SET_CSR:
88 tbs->csr_content = data;
89 tbs->csr_type = "CSR";
90 logger::note( "INFO: CSR read:\n", tbs->csr_content );
93 case RecordHeader::SignerCommand::SET_SPKAC:
94 tbs->csr_content = data;
95 tbs->csr_type = "SPKAC";
96 logger::note( "INFO: SPKAC read:\n", tbs->csr_content );
99 case RecordHeader::SignerCommand::SET_SIGNATURE_TYPE:
103 case RecordHeader::SignerCommand::SET_PROFILE:
108 case RecordHeader::SignerCommand::SET_WISH_FROM:
109 tbs->wishFrom = data;
112 case RecordHeader::SignerCommand::SET_WISH_TO:
116 case RecordHeader::SignerCommand::ADD_SAN:
118 size_t pos = data.find( "," );
120 if( pos == std::string::npos ) {
123 auto san = std::make_shared<SAN>();
124 san->type = data.substr( 0, pos );
125 san->content = data.substr( pos + 1 );
126 tbs->SANs.push_back( san );
131 case RecordHeader::SignerCommand::ADD_AVA:
133 size_t pos = data.find( "," );
135 if( pos == std::string::npos ) {
138 auto ava = std::make_shared<AVA>();
139 ava->name = data.substr( 0, pos );
140 ava->value = data.substr( pos + 1 );
141 tbs->AVAs.push_back( ava );
146 case RecordHeader::SignerCommand::ADD_PROOF_LINE:
149 case RecordHeader::SignerCommand::SIGN:
150 result = signer->sign( tbs );
151 logger::note( "INFO: signlog:\n", result->log );
152 logger::note( "INFO: res:\n", result->certificate );
153 respondCommand( RecordHeader::SignerResult::SAVE_LOG, result->log );
156 case RecordHeader::SignerCommand::LOG_SAVED:
158 respondCommand( RecordHeader::SignerResult::SIGNING_CA, result->ca_name );
159 respondCommand( RecordHeader::SignerResult::CERTIFICATE, result->certificate );
162 logger::note( "Shutting down SSL" );
164 if( !SSL_shutdown( ssl.get() ) && !SSL_shutdown( ssl.get() ) ) {
165 logger::warn( "ERROR: SSL shutdown failed." );
168 io->ctrl( BIO_CTRL_FLUSH, 0, NULL );
169 logger::note( "Shutted down SSL" );
171 parent->reset(); // Connection ended
175 case RecordHeader::SignerCommand::ADD_SERIAL:
176 serials.push_back( data );
179 case RecordHeader::SignerCommand::REVOKE:
181 logger::note("Revoking: ", data);
182 std::string ca = data;
183 auto reqCA = CAs.at( ca );
184 logger::note( "CA found in recordHandler" );
185 std::shared_ptr<CRL> crl;
187 std::tie( crl, date ) = signer->revoke( reqCA, serials );
189 respondCommand( RecordHeader::SignerResult::REVOKED, date + crl->getSignature() );
193 case RecordHeader::SignerCommand::GET_FULL_CRL:
195 logger::note("Requesting full CRL: ", data);
196 auto ca = CAs.at( data );
197 CRL c( ca->path + "/ca.crl" );
198 respondCommand( RecordHeader::SignerResult::FULL_CRL, c.toString() );
200 logger::note( "Shutting down SSL" );
201 if( !SSL_shutdown( ssl.get() ) && !SSL_shutdown( ssl.get() ) ) {
202 logger::error( "ERROR: SSL shutdown failed." );
204 io->ctrl( BIO_CTRL_FLUSH, 0, NULL );
205 logger::note( "Shutted down SSL" );
207 parent->reset(); // Connection ended
212 throw std::runtime_error( "Unimplemented" );
217 DefaultRecordHandler::DefaultRecordHandler( std::shared_ptr<Signer> signer, std::shared_ptr<BIO> bio ) :
218 bio( bio ), ctx( generateSSLContext( true ) ), signer( signer ), currentSession() {
221 void DefaultRecordHandler::reset() {
222 currentSession = std::shared_ptr<RecordHandlerSession>();
225 void DefaultRecordHandler::handle() {
226 if( !currentSession ) {
227 ( void ) BIO_reset( bio.get() );
228 logger::note( "New session allocated." );
229 currentSession = std::make_shared<RecordHandlerSession>( this, signer, ctx, bio );
233 currentSession->work();
234 } catch( eof_exception e ) {