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