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" );
163 if( !SSL_shutdown( ssl.get() ) && !SSL_shutdown( ssl.get() ) ) {
164 logger::warn( "ERROR: SSL shutdown failed." );
166 io->ctrl( BIO_CTRL_FLUSH, 0, NULL );
167 logger::note( "Shutted down SSL" );
169 parent->reset(); // Connection ended
173 case RecordHeader::SignerCommand::ADD_SERIAL:
174 serials.push_back( data );
177 case RecordHeader::SignerCommand::REVOKE:
179 logger::note("Revoking: ", data);
180 std::string ca = data;
181 auto reqCA = CAs.at( ca );
182 logger::note( "CA found in recordHandler" );
183 std::shared_ptr<CRL> crl;
185 std::tie( crl, date ) = signer->revoke( reqCA, serials );
187 respondCommand( RecordHeader::SignerResult::REVOKED, date + crl->getSignature() );
191 case RecordHeader::SignerCommand::GET_FULL_CRL:
193 logger::note("Requesting full CRL: ", data);
194 auto ca = CAs.at( data );
195 CRL c( ca->path + "/ca.crl" );
196 respondCommand( RecordHeader::SignerResult::FULL_CRL, c.toString() );
198 logger::note( "Shutting down SSL" );
199 if( !SSL_shutdown( ssl.get() ) && !SSL_shutdown( ssl.get() ) ) {
200 logger::error( "ERROR: SSL shutdown failed." );
202 io->ctrl( BIO_CTRL_FLUSH, 0, NULL );
203 logger::note( "Shutted down SSL" );
205 parent->reset(); // Connection ended
210 throw std::runtime_error("Unimplemented");
215 DefaultRecordHandler::DefaultRecordHandler( std::shared_ptr<Signer> signer, std::shared_ptr<BIO> bio ) :
216 bio( bio ), ctx( generateSSLContext( true ) ), signer( signer ), currentSession() {
219 void DefaultRecordHandler::reset() {
220 currentSession = std::shared_ptr<RecordHandlerSession>();
223 void DefaultRecordHandler::handle() {
224 if( !currentSession ) {
225 (void) BIO_reset( bio.get() );
226 logger::note( "New session allocated." );
227 currentSession = std::make_shared<RecordHandlerSession>( this, signer, ctx, bio );
230 currentSession->work();
231 } catch( eof_exception e ){