]> WPIA git - cassiopeia.git/blob - src/crypto/simpleOpensslSigner.cpp
add: Initial code to implement revocation
[cassiopeia.git] / src / crypto / simpleOpensslSigner.cpp
1 #include "simpleOpensslSigner.h"
2
3 #include <iostream>
4 #include <sstream>
5 #include <unordered_map>
6
7 #include <openssl/ssl.h>
8 #include <openssl/err.h>
9 #include <openssl/bio.h>
10 #include <openssl/bn.h>
11 #include <openssl/engine.h>
12 #include <openssl/x509v3.h>
13
14 #include "X509.h"
15 #include "util.h"
16 #include "sslUtil.h"
17
18 extern std::unordered_map<std::string, Profile> profiles;
19
20 std::shared_ptr<int> SimpleOpensslSigner::lib_ref = ssl_lib_ref;
21
22 SimpleOpensslSigner::SimpleOpensslSigner() {
23 }
24
25 SimpleOpensslSigner::~SimpleOpensslSigner() {
26 }
27
28 std::pair<std::shared_ptr<BIGNUM>, std::string> SimpleOpensslSigner::nextSerial( Profile& prof ) {
29     uint16_t profile = prof.id;
30     std::string res = readFile( prof.ca->path + "/serial" );
31
32     BIGNUM* bn = 0;
33
34     if( res == "" ) {
35         bn = BN_new();
36
37         if( !bn ) {
38             throw "Initing serial failed";
39         }
40     } else {
41         if( !BN_hex2bn( &bn, res.c_str() + 1 ) ) {
42             throw "Parsing serial failed.";
43         }
44     }
45
46     std::shared_ptr<BIGNUM> serial = std::shared_ptr<BIGNUM>( bn, BN_free );
47
48     std::shared_ptr<unsigned char> data = std::shared_ptr<unsigned char>( ( unsigned char* ) malloc( BN_num_bytes( serial.get() ) + 20 ), free );
49     int len = BN_bn2bin( serial.get(), data.get() );
50
51     data.get()[len] = 0x0;
52     data.get()[len + 1] = 0x0; // signer id
53
54     data.get()[len + 2] = profile >> 8;
55     data.get()[len + 3] = profile & 0xFF; // profile id
56
57     if( !RAND_bytes( data.get() + len + 4, 16 ) || !BN_add_word( serial.get(), 1 ) ) {
58         throw "Big number math failed while calcing serials.";
59     }
60
61     std::shared_ptr<char> serStr = std::shared_ptr<char>(
62         BN_bn2hex( serial.get() ),
63         []( char* ref ) {
64             OPENSSL_free( ref );
65         } );
66     writeFile( prof.ca->path + "/serial", serStr.get() );
67
68     return std::pair<std::shared_ptr<BIGNUM>, std::string>( std::shared_ptr<BIGNUM>( BN_bin2bn( data.get(), len + 4 + 16 , 0 ), BN_free ), std::string( serStr.get() ) );
69 }
70
71 std::shared_ptr<SignedCertificate> SimpleOpensslSigner::sign( std::shared_ptr<TBSCertificate> cert ) {
72     std::stringstream signlog;
73
74     signlog << "FINE: profile is " << cert->profile << std::endl;
75
76     Profile& prof = profiles.at( cert->profile );
77
78     if( !prof.ca ) {
79         throw "CA-key not found";
80     }
81
82     signlog << "FINE: CA-key is correctly loaded." << std::endl;
83     signlog << "FINE: Profile id is: " << prof.id << std::endl;
84     signlog << "FINE: ku is: " << prof.ku << std::endl;
85     signlog << "FINE: eku is: " << prof.eku << std::endl;
86
87     std::shared_ptr<X509Req> req;
88
89     if( cert->csr_type == "SPKAC" ) {
90         req = X509Req::parseSPKAC( cert->csr_content );
91     } else if( cert->csr_type == "CSR" ) {
92         req = X509Req::parseCSR( cert->csr_content );
93     } else {
94         throw "Error, unknown REQ rype " + ( cert->csr_type );
95     }
96
97     int i = req->verify();
98
99     if( i < 0 ) {
100         throw "Signature problems ... ";
101     } else if( i == 0 ) {
102         throw "Signature did not match";
103     } else {
104         signlog << "FINE: Signature ok" << std::endl;
105     }
106
107     // Construct the Certificate
108     X509Cert c = X509Cert();
109     std::shared_ptr<X509> retsh = std::shared_ptr<X509>( X509_new(), X509_free );
110     X509* ret = retsh.get();
111
112     if( !ret ) {
113         throw "Creating X509 failed.";
114     }
115
116     X509_NAME* subjectP = X509_NAME_new();
117
118     if( !subjectP ) {
119         throw "malloc failure";
120     }
121
122     for( std::shared_ptr<AVA> a : cert->AVAs ) {
123         signlog << "Addings RDN: " << a->name << ": " << a->value << std::endl;
124
125         if( a->name == "CN" ) {
126             c.addRDN( NID_commonName, a->value );
127         } else if( a->name == "EMAIL" ) {
128             c.addRDN( NID_pkcs9_emailAddress, a->value );
129         } else if( a->name == "C" ) {
130             c.addRDN( NID_countryName, a->value );
131         } else if( a->name == "L" ) {
132             c.addRDN( NID_localityName, a->value );
133         } else if( a->name == "ST" ) {
134             c.addRDN( NID_stateOrProvinceName, a->value );
135         } else if( a->name == "O" ) {
136             c.addRDN( NID_organizationName, a->value );
137         } else if( a->name == "OU" ) {
138             c.addRDN( NID_organizationalUnitName, a->value );
139         } else {
140             throw "unknown AVA-type";
141         }
142     }
143
144     c.setIssuerNameFrom( prof.ca->ca );
145     c.setPubkeyFrom( req );
146
147     std::shared_ptr<BIGNUM> ser;
148     std::string num;
149     std::tie( ser, num ) = nextSerial( prof );
150     c.setSerialNumber( ser.get() );
151     c.setTimes( 0, 60 * 60 * 24 * 10 );
152     signlog << "FINE: Setting extensions." << std::endl;
153     c.setExtensions( prof.ca->ca, cert->SANs, prof );
154     signlog << "FINE: Signed" << std::endl;
155     std::shared_ptr<SignedCertificate> output = c.sign( prof.ca->caKey, cert->md );
156     signlog << "FINE: all went well" << std::endl;
157     signlog << "FINE: crt went to: " << writeBackFile( num, output->certificate, prof.ca->path ) << std::endl;
158     output->log = signlog.str();
159     return output;
160 }
161
162
163 std::shared_ptr<X509_CRL> SimpleOpensslSigner::revoke( std::shared_ptr<CAConfig> ca, std::string serial ) {
164     std::string crlpath = ca->path + "/ca.crl";
165
166     std::shared_ptr<BIO> bio( BIO_new_file( crlpath.c_str(), "r" ), free );
167     std::shared_ptr<X509_CRL> crl( PEM_read_bio_X509_CRL( bio.get(), 0, NULL, 0 ), X509_CRL_free );
168     std::cout << "Starting revocation" << std::endl;
169
170     if( !crl ) {
171         std::cout << "CRL was not loaded" << std::endl;
172         crl = std::shared_ptr<X509_CRL>( X509_CRL_new(), X509_CRL_free );
173     }
174
175     BIGNUM* serBN = 0;
176
177     if( ! BN_hex2bn( &serBN, serial.c_str() ) ) {
178         //error
179     }
180
181     std::shared_ptr<BIGNUM> serBNP( serBN, BN_free );
182     std::shared_ptr<ASN1_INTEGER> ser( BN_to_ASN1_INTEGER( serBN, NULL ), ASN1_INTEGER_free );
183
184     if( !ser ) {
185         // error
186     }
187
188     std::shared_ptr<ASN1_TIME> tmptm( ASN1_TIME_new(), ASN1_TIME_free );
189
190     if( !tmptm ) {
191         // error
192     }
193
194     X509_gmtime_adj( tmptm.get(), 0 );
195
196     X509_REVOKED* rev = X509_REVOKED_new();
197     X509_REVOKED_set_serialNumber( rev, ser.get() );
198     X509_REVOKED_set_revocationDate( rev, tmptm.get() );
199
200     X509_CRL_add0_revoked( crl.get(), rev );
201
202     if( !X509_CRL_set_issuer_name( crl.get(), X509_get_subject_name( ca->ca.get() ) ) ) {
203         // error
204     }
205
206     X509_CRL_set_lastUpdate( crl.get(), tmptm.get() );
207
208     if( !X509_time_adj_ex( tmptm.get(), 1, 10, NULL ) ) {
209         // error
210     }
211
212     X509_CRL_set_nextUpdate( crl.get(), tmptm.get() );
213
214
215     std::cout << "Signing" << std::endl;
216     X509_CRL_sort( crl.get() );
217     X509_CRL_sign( crl.get(), ca->caKey.get(), EVP_sha256() );
218
219     std::cout << "writing bio" << std::endl;
220     std::shared_ptr<BIO> bioOut( BIO_new_file( crlpath.c_str(), "w" ), BIO_free );
221     PEM_write_bio_X509_CRL( bioOut.get(), crl.get() );
222     std::cout << "finished crl" << std::endl;
223
224     return crl;
225 }