]> WPIA git - cassiopeia.git/blob - src/io/recordHandler.cpp
2d58be0b7d1e195e62350ebaadd4cc9191074b4f
[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         rh.totalLength = payload.size();
71         sendCommand( rh, payload, io );
72     }
73
74     void work() {
75         std::string content = io->readLine();
76
77         try {
78             RecordHeader head;
79             std::string payload = parseCommand( head, content );
80             execute( head, payload );
81         } catch( const char* msg ) {
82             logger::error( "ERROR: ", msg );
83             parent->reset();
84             return;
85         }
86     }
87
88     void execute( RecordHeader& head, std::string data ) {
89         if( head.totalLength != head.payloadLength || head.offset != 0 ) {
90             throw "Error, chunking not supported yet";
91         }
92
93         switch( static_cast<RecordHeader::SignerCommand>( head.command )) {
94         case RecordHeader::SignerCommand::SET_CSR:
95             tbs->csr_content = data;
96             tbs->csr_type = "CSR";
97             logger::note( "INFO: CSR read:\n", tbs->csr_content );
98             break;
99
100         case RecordHeader::SignerCommand::SET_SPKAC:
101             tbs->csr_content = data;
102             tbs->csr_type = "SPKAC";
103             logger::note( "INFO: SPKAC read:\n", tbs->csr_content );
104             break;
105
106         case RecordHeader::SignerCommand::SET_SIGNATURE_TYPE:
107             tbs->md = data;
108             break;
109
110         case RecordHeader::SignerCommand::SET_PROFILE:
111             // TODO
112             tbs->profile = data;
113             break;
114
115         case RecordHeader::SignerCommand::SET_WISH_FROM:
116             tbs->wishFrom = data;
117             break;
118
119         case RecordHeader::SignerCommand::SET_WISH_TO:
120             tbs->wishTo = data;
121             break;
122
123         case RecordHeader::SignerCommand::ADD_SAN:
124             {
125                 size_t pos = data.find( "," );
126
127                 if( pos == std::string::npos ) {
128                     // error
129                 } else {
130                     auto san = std::make_shared<SAN>();
131                     san->type = data.substr( 0, pos );
132                     san->content = data.substr( pos + 1 );
133                     tbs->SANs.push_back( san );
134                 }
135             }
136             break;
137
138         case RecordHeader::SignerCommand::ADD_AVA:
139             {
140                 size_t pos = data.find( "," );
141
142                 if( pos == std::string::npos ) {
143                     // error
144                 } else {
145                     auto ava = std::make_shared<AVA>();
146                     ava->name = data.substr( 0, pos );
147                     ava->value = data.substr( pos + 1 );
148                     tbs->AVAs.push_back( ava );
149                 }
150             }
151             break;
152
153         case RecordHeader::SignerCommand::ADD_PROOF_LINE:
154             break;
155
156         case RecordHeader::SignerCommand::SIGN:
157             result = signer->sign( tbs );
158             logger::note( "INFO: signlog:\n", result->log );
159             logger::note( "INFO: res:\n", result->certificate );
160             respondCommand( RecordHeader::SignerResult::SAVE_LOG, result->log );
161             break;
162
163         case RecordHeader::SignerCommand::LOG_SAVED:
164             if( result ) {
165                 respondCommand( RecordHeader::SignerResult::SIGNING_CA, result->ca_name );
166                 respondCommand( RecordHeader::SignerResult::CERTIFICATE, result->certificate );
167             }
168
169             logger::note( "Shutting down SSL" );
170             if( !SSL_shutdown( ssl.get() ) && !SSL_shutdown( ssl.get() ) ) {
171                 logger::warn( "ERROR: SSL shutdown failed." );
172             }
173             io->ctrl( BIO_CTRL_FLUSH, 0, NULL );
174             logger::note( "Shutted down SSL" );
175
176             parent->reset(); // Connection ended
177
178             break;
179
180         case RecordHeader::SignerCommand::ADD_SERIAL:
181             serials.push_back( data );
182             break;
183
184         case RecordHeader::SignerCommand::REVOKE:
185             {
186                 logger::note("Revoking: ", data);
187                 std::string ca = data;
188                 auto reqCA = CAs.at( ca );
189                 logger::note( "CA found in recordHandler" );
190                 std::shared_ptr<CRL> crl;
191                 std::string date;
192                 std::tie( crl, date ) = signer->revoke( reqCA, serials );
193
194                 respondCommand( RecordHeader::SignerResult::REVOKED, date + crl->getSignature() );
195             }
196             break;
197
198         case RecordHeader::SignerCommand::GET_FULL_CRL:
199             {
200                 logger::note("Requesting full CRL: ", data);
201                 auto ca = CAs.at( data );
202                 CRL c( ca->path + "/ca.crl" );
203                 respondCommand( RecordHeader::SignerResult::FULL_CRL, c.toString() );
204                 
205                 logger::note( "Shutting down SSL" );
206                 if( !SSL_shutdown( ssl.get() ) && !SSL_shutdown( ssl.get() ) ) {
207                     logger::error( "ERROR: SSL shutdown failed." );
208                 }
209                 io->ctrl( BIO_CTRL_FLUSH, 0, NULL );
210                 logger::note( "Shutted down SSL" );
211
212                 parent->reset(); // Connection ended
213             }
214             break;
215
216         default:
217             throw "Unimplemented";
218         }
219     }
220 };
221
222 DefaultRecordHandler::DefaultRecordHandler( std::shared_ptr<Signer> signer, std::shared_ptr<BIO> bio ) :
223     bio( bio ), ctx( generateSSLContext( true ) ), signer( signer ), currentSession() {
224 }
225
226 void DefaultRecordHandler::reset() {
227     currentSession = std::shared_ptr<RecordHandlerSession>();
228 }
229
230 void DefaultRecordHandler::handle() {
231     if( !currentSession ) {
232         (void) BIO_reset( bio.get() );
233         logger::note( "New session allocated." );
234         currentSession = std::make_shared<RecordHandlerSession>( this, signer, ctx, bio );
235     }
236     try {
237         currentSession->work();
238     } catch( EOFException e ){
239         reset();
240     }
241 }