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