]> WPIA git - cassiopeia.git/blob - src/io/recordHandler.cpp
8dc105ee737f7ee0a1cb8c14c6ab87476e4636b8
[cassiopeia.git] / src / io / recordHandler.cpp
1 #include "io/recordHandler.h"
2
3 #include <iostream>
4 #include <sstream>
5 #include <fstream>
6 #include <ctime>
7 #include <unordered_map>
8
9 #include <openssl/ssl.h>
10
11 #include "util.h"
12 #include "io/record.h"
13 #include "io/opensslBIO.h"
14 #include "io/slipBio.h"
15
16 #include "db/database.h"
17 #include "crypto/remoteSigner.h"
18 #include "crypto/sslUtil.h"
19 #include "crypto/simpleOpensslSigner.h"
20
21 #include "log/logger.hpp"
22
23 extern std::vector<Profile> profiles;
24 extern std::unordered_map<std::string, std::shared_ptr<CAConfig>> CAs;
25
26 class RecordHandlerSession {
27 public:
28     uint32_t sessid = 0;
29     uint32_t lastCommandCount = 0;
30
31     std::shared_ptr<TBSCertificate> tbs;
32     std::shared_ptr<SignedCertificate> result;
33
34     std::shared_ptr<SSL> ssl;
35
36     std::shared_ptr<OpensslBIOWrapper> io;
37     DefaultRecordHandler *parent;
38     std::shared_ptr<Signer> signer;
39
40     std::unique_ptr<std::ofstream> logFile;
41     //std::stringstream sessionlog;
42     std::vector<std::string> serials;
43     logger::logger_set logger;
44
45
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;
52
53         ssl = std::shared_ptr<SSL>( SSL_new( ctx.get() ), SSL_free );
54         std::shared_ptr<BIO> bio(
55             BIO_new( BIO_f_ssl() ),
56             [output]( BIO * p ) {
57                 BIO_free( p );
58             } );
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 );
63     }
64
65     void respondCommand( RecordHeader::SignerResult res, std::string payload ) {
66         RecordHeader rh;
67         rh.command = static_cast<uint16_t>( res );
68         rh.flags = 0;
69         rh.command_count = 0; // TODO i++
70         sendCommand( rh, payload, io );
71     }
72
73     void work() {
74         try {
75             RecordHeader head;
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() );
80             parent->reset();
81             return;
82         }
83     }
84
85     void execute( RecordHeader::SignerCommand command, std::string data ) {
86         switch( command ) {
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 );
91             break;
92
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 );
97             break;
98
99         case RecordHeader::SignerCommand::SET_SIGNATURE_TYPE:
100             tbs->md = data;
101             break;
102
103         case RecordHeader::SignerCommand::SET_PROFILE:
104             // TODO
105             tbs->profile = data;
106             break;
107
108         case RecordHeader::SignerCommand::SET_WISH_FROM:
109             tbs->wishFrom = data;
110             break;
111
112         case RecordHeader::SignerCommand::SET_WISH_TO:
113             tbs->wishTo = data;
114             break;
115
116         case RecordHeader::SignerCommand::ADD_SAN: {
117             size_t pos = data.find( "," );
118
119             if( pos == std::string::npos ) {
120                 // error
121             } else {
122                 auto san = std::make_shared<SAN>();
123                 san->type = data.substr( 0, pos );
124                 san->content = data.substr( pos + 1 );
125                 tbs->SANs.push_back( san );
126             }
127         }
128         break;
129
130         case RecordHeader::SignerCommand::ADD_AVA: {
131             size_t pos = data.find( "," );
132
133             if( pos == std::string::npos ) {
134                 // error
135             } else {
136                 auto ava = std::make_shared<AVA>();
137                 ava->name = data.substr( 0, pos );
138                 ava->value = data.substr( pos + 1 );
139                 tbs->AVAs.push_back( ava );
140             }
141         }
142         break;
143
144         case RecordHeader::SignerCommand::ADD_PROOF_LINE:
145             break;
146
147         case RecordHeader::SignerCommand::SIGN:
148             result = signer->sign( tbs );
149             logger::note( "INFO: signlog:\n", result->log );
150             logger::note( "INFO: res:\n", result->certificate );
151             respondCommand( RecordHeader::SignerResult::SAVE_LOG, result->log );
152             break;
153
154         case RecordHeader::SignerCommand::LOG_SAVED:
155             if( result ) {
156                 respondCommand( RecordHeader::SignerResult::SIGNING_CA, result->ca_name );
157                 respondCommand( RecordHeader::SignerResult::CERTIFICATE, result->certificate );
158             }
159
160             logger::note( "Shutting down SSL" );
161
162             if( !SSL_shutdown( ssl.get() ) && !SSL_shutdown( ssl.get() ) ) {
163                 logger::warn( "ERROR: SSL shutdown failed." );
164             }
165
166             io->ctrl( BIO_CTRL_FLUSH, 0, NULL );
167             logger::note( "Shutted down SSL" );
168
169             parent->reset(); // Connection ended
170
171             break;
172
173         case RecordHeader::SignerCommand::ADD_SERIAL:
174             serials.push_back( data );
175             break;
176
177         case RecordHeader::SignerCommand::REVOKE: {
178             logger::note( "Revoking: ", data );
179             std::string ca = data;
180             auto reqCA = CAs.at( ca );
181             logger::note( "CA found in recordHandler" );
182             std::shared_ptr<CRL> crl;
183             std::string date;
184             std::tie( crl, date ) = signer->revoke( reqCA, serials );
185
186             respondCommand( RecordHeader::SignerResult::REVOKED, date + crl->getSignature() );
187         }
188         break;
189
190         case RecordHeader::SignerCommand::GET_FULL_CRL: {
191             logger::note( "Requesting full CRL: ", data );
192             auto ca = CAs.at( data );
193             CRL c( ca->path + "/ca.crl" );
194             respondCommand( RecordHeader::SignerResult::FULL_CRL, c.toString() );
195
196             logger::note( "Shutting down SSL" );
197
198             if( !SSL_shutdown( ssl.get() ) && !SSL_shutdown( ssl.get() ) ) {
199                 logger::error( "ERROR: SSL shutdown failed." );
200             }
201
202             io->ctrl( BIO_CTRL_FLUSH, 0, NULL );
203             logger::note( "Shutted down SSL" );
204
205             parent->reset(); // Connection ended
206         }
207         break;
208
209         default:
210             throw std::runtime_error( "Unimplemented" );
211         }
212     }
213 };
214
215 DefaultRecordHandler::DefaultRecordHandler( std::shared_ptr<Signer> signer, std::shared_ptr<BIO> bio ) :
216     bio( bio ), ctx( generateSSLContext( true ) ), signer( signer ), currentSession() {
217 }
218
219 void DefaultRecordHandler::reset() {
220     currentSession = std::shared_ptr<RecordHandlerSession>();
221 }
222
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 );
228     }
229
230     try {
231         currentSession->work();
232     } catch( eof_exception e ) {
233         reset();
234     }
235 }