1 #include "remoteSigner.h"
6 #include <openssl/ssl.h>
7 #include <openssl/bn.h>
9 RemoteSigner::RemoteSigner( std::shared_ptr<BIO> target, std::shared_ptr<SSL_CTX> ctx ) {
10 this->target = target;
14 RemoteSigner::~RemoteSigner() {
17 void RemoteSigner::send( std::shared_ptr<OpensslBIOWrapper> bio, RecordHeader& head, RecordHeader::SignerCommand cmd, std::string data ) {
18 head.command = ( uint16_t ) cmd;
20 head.totalLength = data.size();
21 sendCommand( head, data, bio, log );
25 std::shared_ptr<SignedCertificate> RemoteSigner::sign( std::shared_ptr<TBSCertificate> cert ) {
26 ( void )BIO_reset( target.get() );
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 ) );
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 );
43 std::cout << "Unknown csr_type: " << cert->csr_type;
44 return std::shared_ptr<SignedCertificate>();
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 );
52 for( auto ava : cert->AVAs ) {
53 if( ava->name.find( "," ) != std::string::npos ) {
55 return std::shared_ptr<SignedCertificate>();
58 send( conn, head, RecordHeader::SignerCommand::ADD_AVA, ava->name + "," + ava->value );
61 for( auto san : cert->SANs ) {
62 if( san->type.find( "," ) != std::string::npos ) {
64 return std::shared_ptr<SignedCertificate>();
67 send( conn, head, RecordHeader::SignerCommand::ADD_SAN, san->type + "," + san->content );
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 );
75 for( int i = 0; i < 3; i++ ) {
77 int length = conn->read( buffer.data(), buffer.size() );
80 std::cout << "Error, no response data" << std::endl;
81 result = std::shared_ptr<SignedCertificate>();
86 std::string payload = parseCommand( head, std::string( buffer.data(), length ), log );
88 switch( ( RecordHeader::SignerResult ) head.command ) {
89 case RecordHeader::SignerResult::CERTIFICATE:
90 result->certificate = payload;
93 case RecordHeader::SignerResult::SAVE_LOG:
94 result->log = payload;
97 case RecordHeader::SignerResult::SIGNING_CA:
98 result->ca_name = payload;
102 std::cout << "Invalid Message" << std::endl;
105 } catch( const char* msg ) {
106 std::cout << msg << std::endl;
107 return std::shared_ptr<SignedCertificate>();
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();
117 int dlen = BIO_write( bios.get(), buf, len );
120 throw "Memory error.";
127 std::shared_ptr<X509> pem( PEM_read_bio_X509( bios.get(), NULL, 0, NULL ) );
130 throw "Pem was not readable";
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() ),
138 } ); // OPENSSL_free is a macro...
140 extractTimes( pem, result );
142 result->serial = std::string( serStr.get() );
145 if( !SSL_shutdown( ssl.get() ) && !SSL_shutdown( ssl.get() ) ) { // need to close the connection twice
146 std::cout << "SSL shutdown failed" << std::endl;
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() );
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 ) );
166 for( std::string serial : serials ) {
167 send( conn, head, RecordHeader::SignerCommand::ADD_SERIAL, serial );
170 std::string payload = ca->name;
171 send( conn, head, RecordHeader::SignerCommand::REVOKE, payload );
173 std::vector<char> buffer( 2048 * 4 );
174 int length = conn->read( buffer.data(), buffer.size() );
177 throw "Error, no response data";
180 payload = parseCommand( head, std::string( buffer.data(), length ), log );
182 std::shared_ptr<CRL> crl( new CRL( ca->path + std::string( "/ca.crl" ) ) );
185 if( ( RecordHeader::SignerResult ) head.command != RecordHeader::SignerResult::REVOKED ) {
186 throw "Protocol violation";
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 );
196 for( std::string serial : serials ) {
197 crl->revoke( serial, date );
200 crl->setSignature( rest );
201 bool ok = crl->verify( ca );
204 ( *log ) << "CRL verificated successfully" << std::endl;
205 writeFile( ca->path + std::string( "/ca.crl" ), crl->toString() );
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() );
212 throw "Error, no response data";
215 payload = parseCommand( head, std::string( buffer.data(), length ), log );
217 if( ( RecordHeader::SignerResult ) head.command != RecordHeader::SignerResult::FULL_CRL ) {
218 throw "Protocol violation";
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" ) ) );
224 if( crl->verify( ca ) ) {
225 writeFile( ca->path + std::string( "/ca.crl" ), crl->toString() );
226 ( *log ) << "CRL is now valid" << std::endl;
228 ( *log ) << "CRL is still broken... Please, help me" << std::endl;
233 ( *log ) << "CRL: " << std::endl << crl->toString() << std::endl;
235 if( !SSL_shutdown( ssl.get() ) && !SSL_shutdown( ssl.get() ) ) { // need to close the connection twice
236 std::cout << "SSL shutdown failed" << std::endl;
239 return std::pair<std::shared_ptr<CRL>, std::string>( crl, date );
242 void RemoteSigner::setLog( std::shared_ptr<std::ostream> target ) {