]> WPIA git - cassiopeia.git/blob - src/crypto/simpleOpensslSigner.cpp
add: Implement signing based on requested "wish time"
[cassiopeia.git] / src / crypto / simpleOpensslSigner.cpp
1 #include "simpleOpensslSigner.h"
2
3 #include <sstream>
4 #include <unordered_map>
5
6 #include <openssl/ssl.h>
7 #include <openssl/err.h>
8 #include <openssl/bio.h>
9 #include <openssl/bn.h>
10 #include <openssl/engine.h>
11 #include <openssl/x509v3.h>
12
13 #include "X509.h"
14 #include "util.h"
15 #include "sslUtil.h"
16
17 extern std::unordered_map<std::string, Profile> profiles;
18
19 std::shared_ptr<int> SimpleOpensslSigner::lib_ref = ssl_lib_ref;
20
21 SimpleOpensslSigner::SimpleOpensslSigner() {
22 }
23
24 SimpleOpensslSigner::~SimpleOpensslSigner() {
25 }
26
27 std::pair<std::shared_ptr<BIGNUM>, std::string> SimpleOpensslSigner::nextSerial( Profile& prof, std::shared_ptr<CAConfig> ca ) {
28     uint16_t profile = prof.id;
29     std::string res = readFile( ca->path + "/serial" );
30
31     BIGNUM* bn = 0;
32
33     if( res == "" ) {
34         bn = BN_new();
35
36         if( !bn ) {
37             throw "Initing serial failed";
38         }
39     } else {
40         if( !BN_hex2bn( &bn, res.c_str() ) ) {
41             throw "Parsing serial failed.";
42         }
43     }
44
45     std::shared_ptr<BIGNUM> serial = std::shared_ptr<BIGNUM>( bn, BN_free );
46
47     std::shared_ptr<unsigned char> data = std::shared_ptr<unsigned char>( ( unsigned char* ) malloc( BN_num_bytes( serial.get() ) + 20 ), free );
48     int len = BN_bn2bin( serial.get(), data.get() );
49
50     data.get()[len] = 0x0;
51     data.get()[len + 1] = 0x0; // signer id
52
53     data.get()[len + 2] = profile >> 8;
54     data.get()[len + 3] = profile & 0xFF; // profile id
55
56     if( !RAND_bytes( data.get() + len + 4, 16 ) || !BN_add_word( serial.get(), 1 ) ) {
57         throw "Big number math failed while calcing serials.";
58     }
59
60     std::shared_ptr<char> serStr = std::shared_ptr<char>(
61         BN_bn2hex( serial.get() ),
62         []( char* ref ) {
63             OPENSSL_free( ref );
64         } );
65
66     writeFile( 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     std::shared_ptr<CAConfig> ca = prof.getCA();
78
79     if( !ca ) {
80         throw "CA-key not found";
81     }
82
83     signlog << "FINE: CA-key is correctly loaded." << std::endl;
84     signlog << "FINE: Profile id is: " << prof.id << std::endl;
85     signlog << "FINE: ku is: " << prof.ku << std::endl;
86     signlog << "FINE: eku is: " << prof.eku << std::endl;
87     signlog << "FINE: Signing is wanted from: " << cert->wishFrom << std::endl;
88     signlog << "FINE: Signing is wanted to: " << cert->wishTo << std::endl;
89
90     std::shared_ptr<X509Req> req;
91
92     if( cert->csr_type == "SPKAC" ) {
93         req = X509Req::parseSPKAC( cert->csr_content );
94     } else if( cert->csr_type == "CSR" ) {
95         req = X509Req::parseCSR( cert->csr_content );
96     } else {
97         throw "Error, unknown REQ rype " + ( cert->csr_type );
98     }
99
100     int i = req->verify();
101
102     if( i < 0 ) {
103         throw "Signature problems ... ";
104     } else if( i == 0 ) {
105         throw "Signature did not match";
106     } else {
107         signlog << "FINE: Signature ok" << std::endl;
108     }
109
110     // Construct the Certificate
111     X509Cert c = X509Cert();
112     std::shared_ptr<X509> retsh = std::shared_ptr<X509>( X509_new(), X509_free );
113     X509* ret = retsh.get();
114
115     if( !ret ) {
116         throw "Creating X509 failed.";
117     }
118
119     X509_NAME* subjectP = X509_NAME_new();
120
121     if( !subjectP ) {
122         throw "malloc failure";
123     }
124
125     for( std::shared_ptr<AVA> a : cert->AVAs ) {
126         signlog << "Addings RDN: " << a->name << ": " << a->value << std::endl;
127
128         if( a->name == "CN" ) {
129             c.addRDN( NID_commonName, a->value );
130         } else if( a->name == "EMAIL" ) {
131             c.addRDN( NID_pkcs9_emailAddress, a->value );
132         } else if( a->name == "C" ) {
133             c.addRDN( NID_countryName, a->value );
134         } else if( a->name == "L" ) {
135             c.addRDN( NID_localityName, a->value );
136         } else if( a->name == "ST" ) {
137             c.addRDN( NID_stateOrProvinceName, a->value );
138         } else if( a->name == "O" ) {
139             c.addRDN( NID_organizationName, a->value );
140         } else if( a->name == "OU" ) {
141             c.addRDN( NID_organizationalUnitName, a->value );
142         } else {
143             throw "unknown AVA-type";
144         }
145     }
146
147     c.setIssuerNameFrom( ca->ca );
148     c.setPubkeyFrom( req );
149
150     std::shared_ptr<BIGNUM> ser;
151     std::string num;
152     std::tie( ser, num ) = nextSerial( prof, ca );
153     c.setSerialNumber( ser.get() );
154
155     std::time_t from, to;
156     std::time_t now = time( 0 );
157     std::pair<bool, std::time_t> parsed;
158
159     if( ( parsed = parseDate( cert->wishFrom ) ).first /* is of yyyy-mm-dd */ ) {
160         if( parsed.second > now ) {
161             from = parsed.second;
162         } else { // fail
163             from = now;
164         }
165     } else {
166         from = now;
167     }
168
169     if( from - now > /* 2 Weeks */ 2 * 7 * 24 * 60 * 60 || now - from >= 0 ) {
170         from = now;
171     }
172
173     if( ( parsed = parseDate( cert->wishTo ) ).first /*is of yyyy-mm-dd */ ) {
174         if( parsed.second > from ) {
175             to = parsed.second;
176         } else {
177             to = from + /*2 Years */ 2 * 365 * 24 * 60 * 60;
178         }
179     } else if( ( parsed = parseYearInterval( from, cert->wishTo ) ).first /*is of [0-9]+y */ ) {
180         to = parsed.second;
181     } else if( ( parsed = parseMonthInterval( from, cert->wishTo ) ).first /*is of [0-9]+m */ ) {
182         to = parsed.second;
183     } else {
184         to = from + /*2 Years */ 2 * 365 * 24 * 60 * 60;
185     }
186
187     time_t limit = /*2 Years (max possible) */ 2 * 366 * 24 * 60 * 60;
188
189     if( to - from > limit || to - from < 0 ) {
190         to = from + limit;
191     }
192
193     c.setTimes( from, to );
194     signlog << "FINE: Setting extensions." << std::endl;
195     c.setExtensions( ca->ca, cert->SANs, prof );
196     signlog << "FINE: Signed" << std::endl;
197     std::shared_ptr<SignedCertificate> output = c.sign( ca->caKey, cert->md );
198     signlog << "FINE: all went well" << std::endl;
199     signlog << "FINE: crt went to: " << writeBackFile( num, output->certificate, ca->path ) << std::endl;
200     output->ca_name = ca->name;
201     output->log = signlog.str();
202     return output;
203 }
204
205 std::pair<std::shared_ptr<CRL>, std::string> SimpleOpensslSigner::revoke( std::shared_ptr<CAConfig> ca, std::vector<std::string> serials ) {
206     std::string crlpath = ca->path + "/ca.crl";
207
208     std::shared_ptr<CRL> crl( new CRL( crlpath ) );
209     std::string date = "";
210
211     for( std::string serial : serials ) {
212         date = crl->revoke( serial, "" );
213     }
214
215     crl->sign( ca );
216     writeFile( crlpath, crl->toString() );
217     return std::pair<std::shared_ptr<CRL>, std::string>( crl, date );
218 }