]> WPIA git - cassiopeia.git/blob - src/remoteSigner.cpp
add: write remoteSigner serial back
[cassiopeia.git] / src / remoteSigner.cpp
1 #include "remoteSigner.h"
2
3 #include <iostream>
4
5 #include <openssl/ssl.h>
6 #include <openssl/bn.h>
7
8 RemoteSigner::RemoteSigner( std::shared_ptr<BIO> target, std::shared_ptr<SSL_CTX> ctx ) {
9     this->target = target;
10     this->ctx = ctx;
11 }
12
13 RemoteSigner::~RemoteSigner() {
14 }
15
16 void RemoteSigner::send( std::shared_ptr<OpensslBIOWrapper> bio, RecordHeader& head, RecordHeader::SignerCommand cmd, std::string data ) {
17     head.command = ( uint16_t ) cmd;
18     head.command_count++;
19     head.totalLength = data.size();
20     sendCommand( head, data, bio, log );
21
22 }
23
24 std::shared_ptr<SignedCertificate> RemoteSigner::sign( std::shared_ptr<TBSCertificate> cert ) {
25     ( void )BIO_reset( target.get() );
26
27     std::shared_ptr<SSL> ssl( SSL_new( ctx.get() ), SSL_free );
28     std::shared_ptr<BIO> bio( BIO_new( BIO_f_ssl() ), BIO_free );
29     SSL_set_connect_state( ssl.get() );
30     SSL_set_bio( ssl.get(), target.get(), target.get() );
31     BIO_set_ssl( bio.get(), ssl.get(), BIO_NOCLOSE );
32     std::shared_ptr<OpensslBIOWrapper> conn( new OpensslBIOWrapper( bio ) );
33     RecordHeader head;
34     head.flags = 0;
35     head.sessid = 13;
36
37     if( cert->csr_type == "CSR" ) {
38         send( conn, head, RecordHeader::SignerCommand::SET_CSR, cert->csr_content );
39     } else {
40         std::cout << "Unknown csr_type: " << cert->csr_type;
41         return std::shared_ptr<SignedCertificate>();
42     }
43
44     send( conn, head, RecordHeader::SignerCommand::SET_SIGNATURE_TYPE, cert->md );
45     send( conn, head, RecordHeader::SignerCommand::SET_PROFILE, cert->profile );
46
47     for( auto ava : cert->AVAs ) {
48         if( ava->name.find( "," ) != std::string::npos ) {
49             // invalid ava
50             return std::shared_ptr<SignedCertificate>();
51         }
52
53         send( conn, head, RecordHeader::SignerCommand::ADD_AVA, ava->name + "," + ava->value );
54     }
55
56     for( auto san : cert->SANs ) {
57         if( san->type.find( "," ) != std::string::npos ) {
58             // invalid ava
59             return std::shared_ptr<SignedCertificate>();
60         }
61
62         send( conn, head, RecordHeader::SignerCommand::ADD_SAN, san->type + "," + san->content );
63     }
64
65     send( conn, head, RecordHeader::SignerCommand::SIGN, "" );
66     send( conn, head, RecordHeader::SignerCommand::LOG_SAVED, "" );
67     std::shared_ptr<SignedCertificate> result = std::shared_ptr<SignedCertificate>( new SignedCertificate() );
68     std::vector<char> buffer( 2048 * 4 );
69
70     for( int i = 0; i < 2; i++ ) {
71         try {
72             int length = conn->read( buffer.data(), buffer.size() );
73
74             if( length <= 0 ) {
75                 std::cout << "Error, no response data" << std::endl;
76                 result = std::shared_ptr<SignedCertificate>();
77                 break;
78             }
79
80             RecordHeader head;
81             std::string payload = parseCommand( head, std::string( buffer.data(), length ), log );
82
83             switch( ( RecordHeader::SignerResult ) head.command ) {
84             case RecordHeader::SignerResult::CERTIFICATE:
85                 result->certificate = payload;
86                 break;
87
88             case RecordHeader::SignerResult::SAVE_LOG:
89                 result->log = payload;
90                 break;
91             }
92         } catch( const char* msg ) {
93             std::cout << msg << std::endl;
94             return std::shared_ptr<SignedCertificate>();
95         }
96     }
97
98     if( result ) {
99         std::shared_ptr<BIO> bios( BIO_new( BIO_s_mem() ), BIO_free );
100         const char* buf = result->certificate.data();
101         unsigned int len = result->certificate.size();
102
103         while( len > 0 ) {
104             int dlen = BIO_write( bios.get(), buf, len );
105
106             if( dlen <= 0 ) {
107                 throw "Memory error.";
108             }
109
110             len -= dlen;
111             buf += dlen;
112         }
113
114         std::shared_ptr<X509> pem( PEM_read_bio_X509( bios.get(), NULL, 0, NULL ) );
115
116         if( !pem ) {
117             throw "Pem was not readable";
118         }
119
120         std::shared_ptr<BIGNUM> ser( ASN1_INTEGER_to_BN( pem->cert_info->serialNumber, NULL ), BN_free );
121         std::shared_ptr<char> serStr(
122             BN_bn2hex( ser.get() ),
123             []( char* p ) {
124                 OPENSSL_free( p );
125             } ); // OPENSSL_free is a macro...
126         result->serial = std::string( serStr.get() );
127     }
128
129     if( !SSL_shutdown( ssl.get() ) && !SSL_shutdown( ssl.get() ) ) { // need to close the connection twice
130         std::cout << "SSL shutdown failed" << std::endl;
131     }
132
133     return result;
134 }
135
136 void RemoteSigner::setLog( std::shared_ptr<std::ostream> target ) {
137     this->log = target;
138 }