]> WPIA git - cassiopeia.git/blob - src/crypto/remoteSigner.cpp
fix: Make CppCheck happy by fixing the code
[cassiopeia.git] / src / crypto / remoteSigner.cpp
1 #include "remoteSigner.h"
2 #include "util.h"
3
4 #include <iostream>
5
6 #include <openssl/ssl.h>
7 #include <openssl/bn.h>
8
9 RemoteSigner::RemoteSigner( std::shared_ptr<BIO> target, std::shared_ptr<SSL_CTX> ctx ) : target( target ), ctx( ctx ) {
10 }
11
12 RemoteSigner::~RemoteSigner() {
13 }
14
15 void RemoteSigner::send( std::shared_ptr<OpensslBIOWrapper> bio, RecordHeader& head, RecordHeader::SignerCommand cmd, std::string data ) {
16     head.command = ( uint16_t ) cmd;
17     head.command_count++;
18     head.totalLength = data.size();
19     sendCommand( head, data, bio, log );
20
21 }
22
23 std::shared_ptr<SignedCertificate> RemoteSigner::sign( std::shared_ptr<TBSCertificate> cert ) {
24     ( void )BIO_reset( target.get() );
25
26     std::shared_ptr<SSL> ssl( SSL_new( ctx.get() ), SSL_free );
27     std::shared_ptr<BIO> bio( BIO_new( BIO_f_ssl() ), BIO_free );
28     SSL_set_connect_state( ssl.get() );
29     SSL_set_bio( ssl.get(), target.get(), target.get() );
30     BIO_set_ssl( bio.get(), ssl.get(), BIO_NOCLOSE );
31     std::shared_ptr<OpensslBIOWrapper> conn( new OpensslBIOWrapper( bio ) );
32     RecordHeader head;
33     head.flags = 0;
34     head.sessid = 13;
35
36     if( cert->csr_type == "CSR" ) {
37         send( conn, head, RecordHeader::SignerCommand::SET_CSR, cert->csr_content );
38     } else if( cert->csr_type == "SPKAC" ) {
39         send( conn, head, RecordHeader::SignerCommand::SET_SPKAC, cert->csr_content );
40     } else {
41         std::cout << "Unknown csr_type: " << cert->csr_type;
42         return std::shared_ptr<SignedCertificate>();
43     }
44
45     send( conn, head, RecordHeader::SignerCommand::SET_SIGNATURE_TYPE, cert->md );
46     send( conn, head, RecordHeader::SignerCommand::SET_PROFILE, cert->profile );
47     send( conn, head, RecordHeader::SignerCommand::SET_WISH_FROM, cert->wishFrom );
48     send( conn, head, RecordHeader::SignerCommand::SET_WISH_TO, cert->wishTo );
49
50     for( auto ava : cert->AVAs ) {
51         if( ava->name.find( "," ) != std::string::npos ) {
52             // invalid ava
53             return std::shared_ptr<SignedCertificate>();
54         }
55
56         send( conn, head, RecordHeader::SignerCommand::ADD_AVA, ava->name + "," + ava->value );
57     }
58
59     for( auto san : cert->SANs ) {
60         if( san->type.find( "," ) != std::string::npos ) {
61             // invalid ava
62             return std::shared_ptr<SignedCertificate>();
63         }
64
65         send( conn, head, RecordHeader::SignerCommand::ADD_SAN, san->type + "," + san->content );
66     }
67
68     send( conn, head, RecordHeader::SignerCommand::SIGN, "" );
69     send( conn, head, RecordHeader::SignerCommand::LOG_SAVED, "" );
70     std::shared_ptr<SignedCertificate> result = std::shared_ptr<SignedCertificate>( new SignedCertificate() );
71     std::vector<char> buffer( 2048 * 4 );
72
73     for( int i = 0; i < 3; i++ ) {
74         try {
75             int length = conn->read( buffer.data(), buffer.size() );
76
77             if( length <= 0 ) {
78                 std::cout << "Error, no response data" << std::endl;
79                 result = std::shared_ptr<SignedCertificate>();
80                 break;
81             }
82
83             RecordHeader head;
84             std::string payload = parseCommand( head, std::string( buffer.data(), length ), log );
85
86             switch( ( RecordHeader::SignerResult ) head.command ) {
87             case RecordHeader::SignerResult::CERTIFICATE:
88                 result->certificate = payload;
89                 break;
90
91             case RecordHeader::SignerResult::SAVE_LOG:
92                 result->log = payload;
93                 break;
94
95             case RecordHeader::SignerResult::SIGNING_CA:
96                 result->ca_name = payload;
97                 break;
98
99             default:
100                 std::cout << "Invalid Message" << std::endl;
101                 break;
102             }
103         } catch( const char* msg ) {
104             std::cout << msg << std::endl;
105             return std::shared_ptr<SignedCertificate>();
106         }
107     }
108
109     if( result ) {
110         std::shared_ptr<BIO> bios( BIO_new( BIO_s_mem() ), BIO_free );
111         const char* buf = result->certificate.data();
112         unsigned int len = result->certificate.size();
113
114         while( len > 0 ) {
115             int dlen = BIO_write( bios.get(), buf, len );
116
117             if( dlen <= 0 ) {
118                 throw "Memory error.";
119             }
120
121             len -= dlen;
122             buf += dlen;
123         }
124
125         std::shared_ptr<X509> pem( PEM_read_bio_X509( bios.get(), NULL, 0, NULL ) );
126
127         if( !pem ) {
128             throw "Pem was not readable";
129         }
130
131         std::shared_ptr<BIGNUM> ser( ASN1_INTEGER_to_BN( pem->cert_info->serialNumber, NULL ), BN_free );
132         std::shared_ptr<char> serStr(
133             BN_bn2hex( ser.get() ),
134             []( char* p ) {
135                 OPENSSL_free( p );
136             } ); // OPENSSL_free is a macro...
137
138         extractTimes( pem, result );
139
140         result->serial = std::string( serStr.get() );
141     }
142
143     if( !SSL_shutdown( ssl.get() ) && !SSL_shutdown( ssl.get() ) ) { // need to close the connection twice
144         std::cout << "SSL shutdown failed" << std::endl;
145     }
146
147     return result;
148 }
149
150 std::pair<std::shared_ptr<CRL>, std::string> RemoteSigner::revoke( std::shared_ptr<CAConfig> ca, std::vector<std::string> serials ) {
151     ( void )BIO_reset( target.get() );
152
153     std::shared_ptr<SSL> ssl( SSL_new( ctx.get() ), SSL_free );
154     std::shared_ptr<BIO> bio( BIO_new( BIO_f_ssl() ), BIO_free );
155     SSL_set_connect_state( ssl.get() );
156     SSL_set_bio( ssl.get(), target.get(), target.get() );
157     BIO_set_ssl( bio.get(), ssl.get(), BIO_NOCLOSE );
158     std::shared_ptr<OpensslBIOWrapper> conn( new OpensslBIOWrapper( bio ) );
159
160     RecordHeader head;
161     head.flags = 0;
162     head.sessid = 13;
163
164     for( std::string serial : serials ) {
165         send( conn, head, RecordHeader::SignerCommand::ADD_SERIAL, serial );
166     }
167
168     std::string payload = ca->name;
169     send( conn, head, RecordHeader::SignerCommand::REVOKE, payload );
170
171     std::vector<char> buffer( 2048 * 4 );
172     int length = conn->read( buffer.data(), buffer.size() );
173
174     if( length <= 0 ) {
175         throw "Error, no response data";
176     }
177
178     payload = parseCommand( head, std::string( buffer.data(), length ), log );
179
180     std::shared_ptr<CRL> crl( new CRL( ca->path + std::string( "/ca.crl" ) ) );
181     std::string date;
182
183     if( ( RecordHeader::SignerResult ) head.command != RecordHeader::SignerResult::REVOKED ) {
184         throw "Protocol violation";
185     }
186
187     const unsigned char* buffer2 = ( const unsigned char* ) payload.data();
188     const unsigned char* pos = buffer2;
189     ASN1_TIME* time = d2i_ASN1_TIME( NULL, &pos, payload.size() );
190     ASN1_TIME_free( time );
191     date = payload.substr( 0, pos - buffer2 );
192     std::string rest = payload.substr( pos - buffer2 );
193
194     for( std::string serial : serials ) {
195         crl->revoke( serial, date );
196     }
197
198     crl->setSignature( rest );
199     bool ok = crl->verify( ca );
200
201     if( ok ) {
202         ( *log ) << "CRL verificated successfully" << std::endl;
203         writeFile( ca->path + std::string( "/ca.crl" ), crl->toString() );
204     } else {
205         ( *log ) << "CRL is broken" << std::endl;
206         send( conn, head, RecordHeader::SignerCommand::GET_FULL_CRL, ca->name );
207         length = conn->read( buffer.data(), buffer.size() );
208
209         if( length <= 0 ) {
210             throw "Error, no response data";
211         }
212
213         payload = parseCommand( head, std::string( buffer.data(), length ), log );
214
215         if( ( RecordHeader::SignerResult ) head.command != RecordHeader::SignerResult::FULL_CRL ) {
216             throw "Protocol violation";
217         }
218
219         writeFile( ca->path + std::string( "/ca.crl.bak" ), payload );
220         crl = std::shared_ptr<CRL>( new CRL( ca->path + std::string( "/ca.crl.bak" ) ) );
221
222         if( crl->verify( ca ) ) {
223             writeFile( ca->path + std::string( "/ca.crl" ), crl->toString() );
224             ( *log ) << "CRL is now valid" << std::endl;
225         } else {
226             ( *log ) << "CRL is still broken... Please, help me" << std::endl;
227         }
228
229     }
230
231     ( *log ) << "CRL: " << std::endl << crl->toString() << std::endl;
232
233     if( !SSL_shutdown( ssl.get() ) && !SSL_shutdown( ssl.get() ) ) { // need to close the connection twice
234         std::cout << "SSL shutdown failed" << std::endl;
235     }
236
237     return std::pair<std::shared_ptr<CRL>, std::string>( crl, date );
238 }
239
240 void RemoteSigner::setLog( std::shared_ptr<std::ostream> target ) {
241     this->log = target;
242 }