]> WPIA git - cassiopeia.git/blob - src/crypto/simpleOpensslSigner.cpp
fix: Fix memory-leak in CRL.cpp, revocation from DB, multiple CAs
[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, std::shared_ptr<CAConfig> ca ) {
29     uint16_t profile = prof.id;
30     std::string res = readFile( 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
67     writeFile( ca->path + "/serial", serStr.get() );
68
69     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() ) );
70 }
71
72 std::shared_ptr<SignedCertificate> SimpleOpensslSigner::sign( std::shared_ptr<TBSCertificate> cert ) {
73     std::stringstream signlog;
74
75     signlog << "FINE: profile is " << cert->profile << std::endl;
76
77     Profile& prof = profiles.at( cert->profile );
78     std::shared_ptr<CAConfig> ca = prof.getCA();
79
80     if( !ca ) {
81         throw "CA-key not found";
82     }
83
84     signlog << "FINE: CA-key is correctly loaded." << std::endl;
85     signlog << "FINE: Profile id is: " << prof.id << std::endl;
86     signlog << "FINE: ku is: " << prof.ku << std::endl;
87     signlog << "FINE: eku is: " << prof.eku << std::endl;
88
89     std::shared_ptr<X509Req> req;
90
91     if( cert->csr_type == "SPKAC" ) {
92         req = X509Req::parseSPKAC( cert->csr_content );
93     } else if( cert->csr_type == "CSR" ) {
94         req = X509Req::parseCSR( cert->csr_content );
95     } else {
96         throw "Error, unknown REQ rype " + ( cert->csr_type );
97     }
98
99     int i = req->verify();
100
101     if( i < 0 ) {
102         throw "Signature problems ... ";
103     } else if( i == 0 ) {
104         throw "Signature did not match";
105     } else {
106         signlog << "FINE: Signature ok" << std::endl;
107     }
108
109     // Construct the Certificate
110     X509Cert c = X509Cert();
111     std::shared_ptr<X509> retsh = std::shared_ptr<X509>( X509_new(), X509_free );
112     X509* ret = retsh.get();
113
114     if( !ret ) {
115         throw "Creating X509 failed.";
116     }
117
118     X509_NAME* subjectP = X509_NAME_new();
119
120     if( !subjectP ) {
121         throw "malloc failure";
122     }
123
124     for( std::shared_ptr<AVA> a : cert->AVAs ) {
125         signlog << "Addings RDN: " << a->name << ": " << a->value << std::endl;
126
127         if( a->name == "CN" ) {
128             c.addRDN( NID_commonName, a->value );
129         } else if( a->name == "EMAIL" ) {
130             c.addRDN( NID_pkcs9_emailAddress, a->value );
131         } else if( a->name == "C" ) {
132             c.addRDN( NID_countryName, a->value );
133         } else if( a->name == "L" ) {
134             c.addRDN( NID_localityName, a->value );
135         } else if( a->name == "ST" ) {
136             c.addRDN( NID_stateOrProvinceName, a->value );
137         } else if( a->name == "O" ) {
138             c.addRDN( NID_organizationName, a->value );
139         } else if( a->name == "OU" ) {
140             c.addRDN( NID_organizationalUnitName, a->value );
141         } else {
142             throw "unknown AVA-type";
143         }
144     }
145
146     c.setIssuerNameFrom( ca->ca );
147     c.setPubkeyFrom( req );
148
149     std::shared_ptr<BIGNUM> ser;
150     std::string num;
151     std::tie( ser, num ) = nextSerial( prof, ca );
152     c.setSerialNumber( ser.get() );
153     c.setTimes( 0, 60 * 60 * 24 * 10 );
154     signlog << "FINE: Setting extensions." << std::endl;
155     c.setExtensions( ca->ca, cert->SANs, prof );
156     signlog << "FINE: Signed" << std::endl;
157     std::shared_ptr<SignedCertificate> output = c.sign( ca->caKey, cert->md );
158     signlog << "FINE: all went well" << std::endl;
159     signlog << "FINE: crt went to: " << writeBackFile( num, output->certificate, ca->path ) << std::endl;
160     output->ca_name = ca->name;
161     output->log = signlog.str();
162     return output;
163 }
164
165 std::pair<std::shared_ptr<CRL>, std::string> SimpleOpensslSigner::revoke( std::shared_ptr<CAConfig> ca, std::string serial ) {
166     std::string crlpath = ca->path + "/ca.crl";
167
168     std::shared_ptr<CRL> crl( new CRL( crlpath ) );
169     std::string date = crl->revoke( serial, "" );
170     crl->sign( ca );
171     writeFile( crlpath, crl->toString() );
172     return std::pair<std::shared_ptr<CRL>, std::string>( crl, date );
173 }