]> WPIA git - cassiopeia.git/blob - src/simpleOpensslSigner.cpp
chg: Build two binaries for the signer
[cassiopeia.git] / src / simpleOpensslSigner.cpp
1 #include "simpleOpensslSigner.h"
2
3 #include <iostream>
4
5 #include <openssl/ssl.h>
6 #include <openssl/err.h>
7 #include <openssl/bio.h>
8 #include <openssl/bn.h>
9 #include <openssl/engine.h>
10 #include <openssl/x509v3.h>
11
12 #include "X509.h"
13 #include "util.h"
14 #include "sslUtil.h"
15
16 extern std::vector<Profile> profiles;
17
18 std::shared_ptr<int> SimpleOpensslSigner::lib_ref = ssl_lib_ref;
19
20 SimpleOpensslSigner::SimpleOpensslSigner( Profile& prof ) : prof( prof ) {
21 }
22
23 SimpleOpensslSigner::~SimpleOpensslSigner() {
24 }
25
26 std::shared_ptr<BIGNUM> SimpleOpensslSigner::nextSerial( uint16_t profile ) {
27     std::string res = readFile( "serial" );
28
29     BIGNUM* bn = 0;
30
31     if( res == "" ) {
32         bn = BN_new();
33
34         if( !bn ) {
35             throw "Initing serial failed";
36         }
37     } else {
38         if( !BN_hex2bn( &bn, res.c_str() + 1 ) ) {
39             throw "Parsing serial failed.";
40         }
41     }
42
43     std::shared_ptr<BIGNUM> serial = std::shared_ptr<BIGNUM>( bn, BN_free );
44
45     std::shared_ptr<unsigned char> data = std::shared_ptr<unsigned char>( ( unsigned char* ) malloc( BN_num_bytes( serial.get() ) + 20 ), free );
46     int len = BN_bn2bin( serial.get(), data.get() );
47
48     data.get()[len] = 0x0;
49     data.get()[len + 1] = 0x0; // signer id
50
51     data.get()[len + 2] = profile >> 8;
52     data.get()[len + 3] = profile & 0xFF; // profile id
53
54     if( !RAND_bytes( data.get() + len + 4, 16 ) || !BN_add_word( serial.get(), 1 ) ) {
55         throw "Big number math failed while calcing serials.";
56     }
57
58     std::shared_ptr<char> serStr = std::shared_ptr<char>(
59         BN_bn2hex( serial.get() ),
60         []( char* ref ) {
61             OPENSSL_free( ref );
62         } );
63     writeFile( "serial", serStr.get() );
64
65     return std::shared_ptr<BIGNUM>( BN_bin2bn( data.get(), len + 4 + 16 , 0 ), BN_free );
66 }
67
68 std::shared_ptr<SignedCertificate> SimpleOpensslSigner::sign( std::shared_ptr<TBSCertificate> cert ) {
69     if( !prof.ca ) {
70         throw "CA-key not found";
71     }
72
73     std::shared_ptr<X509Req> req;
74
75     if( cert->csr_type == "SPKAC" ) {
76         req = X509Req::parseSPKAC( cert->csr_content );
77     } else if( cert->csr_type == "CSR" ) {
78         req = X509Req::parseCSR( cert->csr_content );
79     } else {
80         throw "Error, unknown REQ rype " + ( cert->csr_type );
81     }
82
83     int i = req->verify();
84
85     if( i < 0 ) {
86         throw "Signature problems ... ";
87     } else if( i == 0 ) {
88         throw "Signature did not match";
89     } else {
90         std::cerr << "Signature ok" << std::endl;
91     }
92
93     // Construct the Certificate
94     X509Cert c = X509Cert();
95     std::shared_ptr<X509> retsh = std::shared_ptr<X509>( X509_new(), X509_free );
96     X509* ret = retsh.get();
97
98     if( !ret ) {
99         throw "Creating X509 failed.";
100     }
101
102     X509_NAME* subjectP = X509_NAME_new();
103
104     if( !subjectP ) {
105         throw "malloc failure";
106     }
107
108     for( std::shared_ptr<AVA> a : cert->AVAs ) {
109         if( a->name == "CN" ) {
110             c.addRDN( NID_commonName, a->value );
111         } else if( a->name == "EMAIL" ) {
112             c.addRDN( NID_pkcs9_emailAddress, a->value );
113         } else if( a->name == "C" ) {
114             c.addRDN( NID_countryName, a->value );
115         } else if( a->name == "L" ) {
116             c.addRDN( NID_localityName, a->value );
117         } else if( a->name == "ST" ) {
118             c.addRDN( NID_stateOrProvinceName, a->value );
119         } else if( a->name == "O" ) {
120             c.addRDN( NID_organizationName, a->value );
121         } else if( a->name == "OU" ) {
122             c.addRDN( NID_organizationalUnitName, a->value );
123         } else {
124             throw "unknown AVA-type";
125         }
126     }
127
128     c.setIssuerNameFrom( prof.ca );
129     c.setPubkeyFrom( req );
130     long int profile = strtol( cert->profile.c_str(), 0, 10 );
131
132     if( profile > 0xFFFF || profile < 0 || ( profile == 0 && cert->profile != "0" ) ) {
133         throw "invalid profile id";
134     }
135
136     std::shared_ptr<BIGNUM> ser = nextSerial( profile );
137     c.setSerialNumber( ser.get() );
138     c.setTimes( 0, 60 * 60 * 24 * 10 );
139     c.setExtensions( prof.ca, cert->SANs );
140
141     std::shared_ptr<SignedCertificate> output = c.sign( prof.caKey, cert->md );
142
143     return output;
144 }