]> WPIA git - cassiopeia.git/blob - src/io/recordHandler.cpp
2eb8358a567c55f19c117c231f6fc8ca8c1c28c3
[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             {
118                 size_t pos = data.find( "," );
119
120                 if( pos == std::string::npos ) {
121                     // error
122                 } else {
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 );
127                 }
128             }
129             break;
130
131         case RecordHeader::SignerCommand::ADD_AVA:
132             {
133                 size_t pos = data.find( "," );
134
135                 if( pos == std::string::npos ) {
136                     // error
137                 } else {
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 );
142                 }
143             }
144             break;
145
146         case RecordHeader::SignerCommand::ADD_PROOF_LINE:
147             break;
148
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 );
154             break;
155
156         case RecordHeader::SignerCommand::LOG_SAVED:
157             if( result ) {
158                 respondCommand( RecordHeader::SignerResult::SIGNING_CA, result->ca_name );
159                 respondCommand( RecordHeader::SignerResult::CERTIFICATE, result->certificate );
160             }
161
162             logger::note( "Shutting down SSL" );
163
164             if( !SSL_shutdown( ssl.get() ) && !SSL_shutdown( ssl.get() ) ) {
165                 logger::warn( "ERROR: SSL shutdown failed." );
166             }
167
168             io->ctrl( BIO_CTRL_FLUSH, 0, NULL );
169             logger::note( "Shutted down SSL" );
170
171             parent->reset(); // Connection ended
172
173             break;
174
175         case RecordHeader::SignerCommand::ADD_SERIAL:
176             serials.push_back( data );
177             break;
178
179         case RecordHeader::SignerCommand::REVOKE:
180             {
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;
186                 std::string date;
187                 std::tie( crl, date ) = signer->revoke( reqCA, serials );
188
189                 respondCommand( RecordHeader::SignerResult::REVOKED, date + crl->getSignature() );
190             }
191             break;
192
193         case RecordHeader::SignerCommand::GET_FULL_CRL:
194             {
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() );
199                 
200                 logger::note( "Shutting down SSL" );
201                 if( !SSL_shutdown( ssl.get() ) && !SSL_shutdown( ssl.get() ) ) {
202                     logger::error( "ERROR: SSL shutdown failed." );
203                 }
204                 io->ctrl( BIO_CTRL_FLUSH, 0, NULL );
205                 logger::note( "Shutted down SSL" );
206
207                 parent->reset(); // Connection ended
208             }
209             break;
210
211         default:
212             throw std::runtime_error( "Unimplemented" );
213         }
214     }
215 };
216
217 DefaultRecordHandler::DefaultRecordHandler( std::shared_ptr<Signer> signer, std::shared_ptr<BIO> bio ) :
218     bio( bio ), ctx( generateSSLContext( true ) ), signer( signer ), currentSession() {
219 }
220
221 void DefaultRecordHandler::reset() {
222     currentSession = std::shared_ptr<RecordHandlerSession>();
223 }
224
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 );
230     }
231
232     try {
233         currentSession->work();
234     } catch( eof_exception e ) {
235         reset();
236     }
237 }