1 #include "simpleOpensslSigner.h"
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>
16 extern std::vector<Profile> profiles;
18 std::shared_ptr<int> SimpleOpensslSigner::lib_ref = ssl_lib_ref;
20 SimpleOpensslSigner::SimpleOpensslSigner( Profile& prof ) : prof( prof ) {
23 SimpleOpensslSigner::~SimpleOpensslSigner() {
26 std::shared_ptr<BIGNUM> SimpleOpensslSigner::nextSerial( uint16_t profile ) {
27 std::string res = readFile( "serial" );
35 throw "Initing serial failed";
38 if( !BN_hex2bn( &bn, res.c_str() + 1 ) ) {
39 throw "Parsing serial failed.";
43 std::shared_ptr<BIGNUM> serial = std::shared_ptr<BIGNUM>( bn, BN_free );
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() );
48 data.get()[len] = 0x0;
49 data.get()[len + 1] = 0x0; // signer id
51 data.get()[len + 2] = profile >> 8;
52 data.get()[len + 3] = profile & 0xFF; // profile id
54 if( !RAND_bytes( data.get() + len + 4, 16 ) || !BN_add_word( serial.get(), 1 ) ) {
55 throw "Big number math failed while calcing serials.";
58 std::shared_ptr<char> serStr = std::shared_ptr<char>(
59 BN_bn2hex( serial.get() ),
63 writeFile( "serial", serStr.get() );
65 return std::shared_ptr<BIGNUM>( BN_bin2bn( data.get(), len + 4 + 16 , 0 ), BN_free );
68 std::shared_ptr<SignedCertificate> SimpleOpensslSigner::sign( std::shared_ptr<TBSCertificate> cert ) {
70 throw "CA-key not found";
73 std::shared_ptr<X509Req> req;
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 );
80 throw "Error, unknown REQ rype " + ( cert->csr_type );
83 int i = req->verify();
86 throw "Signature problems ... ";
88 throw "Signature did not match";
90 std::cerr << "Signature ok" << std::endl;
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();
99 throw "Creating X509 failed.";
102 X509_NAME* subjectP = X509_NAME_new();
105 throw "malloc failure";
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 );
124 throw "unknown AVA-type";
128 c.setIssuerNameFrom( prof.ca );
129 c.setPubkeyFrom( req );
130 long int profile = strtol( cert->profile.c_str(), 0, 10 );
132 if( profile > 0xFFFF || profile < 0 || ( profile == 0 && cert->profile != "0" ) ) {
133 throw "invalid profile id";
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 );
141 std::shared_ptr<SignedCertificate> output = c.sign( prof.caKey, cert->md );