]> WPIA git - cassiopeia.git/blob - src/crypto/CRL.cpp
f7aa6a5e2417a70af2ff8640f369a637b851ddcc
[cassiopeia.git] / src / crypto / CRL.cpp
1 #include "CRL.h"
2
3 #include <openssl/ssl.h>
4 #include <log/logger.hpp>
5 #include <exception>
6
7 CRL::CRL( std::string path ) {
8     std::shared_ptr<BIO> bio( BIO_new_file( path.c_str(), "r" ), BIO_free );
9     crl = std::shared_ptr<X509_CRL>( PEM_read_bio_X509_CRL( bio.get(), 0, NULL, 0 ), X509_CRL_free );
10
11     if( !crl ) {
12         crl = std::shared_ptr<X509_CRL>( X509_CRL_new(), X509_CRL_free );
13     }
14 }
15
16 std::string CRL::revoke( std::string serial, std::string time ) {
17     BIGNUM* serBN = 0;
18
19     logger::note("parsing serial");
20     if( ! BN_hex2bn( &serBN, serial.c_str() ) ) {
21         throw std::runtime_error("hex2bn malloc fail");
22     }
23
24     std::shared_ptr<BIGNUM> serBNP( serBN, BN_free );
25     std::shared_ptr<ASN1_INTEGER> ser( BN_to_ASN1_INTEGER( serBN, NULL ), ASN1_INTEGER_free );
26
27     if( !ser ) {
28         throw std::runtime_error("BN Malloc fail");
29     }
30
31     logger::note("building current time");
32     std::shared_ptr<ASN1_TIME> tmptm( ASN1_TIME_new(), ASN1_TIME_free );
33
34     if( !tmptm ) {
35         throw std::runtime_error("ASN1-Time Malloc fail");
36     }
37
38     X509_gmtime_adj( tmptm.get(), 0 );
39
40     logger::note("creating entry");
41     X509_REVOKED* rev = X509_REVOKED_new();
42     X509_REVOKED_set_serialNumber( rev, ser.get() );
43
44     if( time != "" ) {
45       ASN1_TIME_set_string( tmptm.get(), time.data() );
46     }
47     X509_REVOKED_set_revocationDate( rev, tmptm.get() );
48
49     X509_CRL_add0_revoked( crl.get(), rev );
50
51     int len = i2d_ASN1_TIME( tmptm.get(), NULL );
52     unsigned char* buffer = ( unsigned char* ) OPENSSL_malloc( len );
53     unsigned char* pos = buffer;
54     i2d_ASN1_TIME( tmptm.get(), &pos );
55     std::string rettime = std::string( ( char* ) buffer, len );
56     OPENSSL_free( buffer );
57     return rettime;
58 }
59
60 void CRL::sign( std::shared_ptr<CAConfig> ca ) {
61     // Updating necessary CRL props
62     std::shared_ptr<ASN1_TIME> tmptm( ASN1_TIME_new(), ASN1_TIME_free );
63
64     if( !tmptm ) {
65         throw std::runtime_error("ASN1-Time Malloc fail");
66     }
67
68     X509_gmtime_adj( tmptm.get(), 0 );
69
70     logger::note("setting issuer");
71     if( !X509_CRL_set_issuer_name( crl.get(), X509_get_subject_name( ca->ca.get() ) ) ) {
72         throw std::runtime_error("Setting issuer failed");
73     }
74
75     logger::note("setting update");
76     X509_CRL_set_lastUpdate( crl.get(), tmptm.get() );
77
78     if( !X509_time_adj_ex( tmptm.get(), 1, 10, NULL ) ) {
79         throw std::runtime_error("Updating time failed");
80     }
81
82     logger::note("setting next update");
83     X509_CRL_set_nextUpdate( crl.get(), tmptm.get() );
84
85     logger::note("sorting");
86     // Sorting and signing
87     X509_CRL_sort( crl.get() );
88     logger::note("signing");
89     X509_CRL_sign( crl.get(), ca->caKey.get(), EVP_sha256() );
90 }
91
92 bool CRL::verify( std::shared_ptr<CAConfig> ca ) {
93     std::shared_ptr<EVP_PKEY> pk( X509_get_pubkey( ca->ca.get() ), EVP_PKEY_free );
94     return X509_CRL_verify( crl.get(), pk.get() ) > 0;
95 }
96
97 std::string CRL::toString() {
98     // Write out the new CRL
99     std::shared_ptr<BIO> mem( BIO_new( BIO_s_mem() ), BIO_free );
100     PEM_write_bio_X509_CRL( mem.get(), crl.get() );
101     BUF_MEM* bptr;
102     BIO_get_mem_ptr( mem.get(), &bptr );
103     std::string newCRL( bptr->data, bptr->length );
104     return newCRL;
105 }
106
107 std::string CRL::getSignature() {
108     const X509_ALGOR *palg;
109     const ASN1_BIT_STRING *psig;
110
111     X509_CRL_get0_signature(crl.get(), &psig, &palg);
112     int len = i2d_X509_ALGOR( const_cast<X509_ALGOR*>(palg), NULL );
113     len += i2d_ASN1_BIT_STRING( const_cast<ASN1_BIT_STRING*>(psig), NULL );
114     len += i2d_ASN1_TIME( const_cast<ASN1_TIME*>(X509_CRL_get0_lastUpdate(crl.get())), NULL );
115     len += i2d_ASN1_TIME( const_cast<ASN1_TIME*>(X509_CRL_get0_nextUpdate(crl.get())), NULL );
116
117     unsigned char* buffer = ( unsigned char* ) OPENSSL_malloc( len );
118     unsigned char* pos = buffer;
119     i2d_X509_ALGOR( const_cast<X509_ALGOR*>(palg), &pos );
120     i2d_ASN1_BIT_STRING( const_cast<ASN1_BIT_STRING*>(psig), &pos );
121     i2d_ASN1_TIME( const_cast<ASN1_TIME*>(X509_CRL_get0_lastUpdate(crl.get())), &pos );
122     i2d_ASN1_TIME( const_cast<ASN1_TIME*>(X509_CRL_get0_nextUpdate(crl.get())), &pos );
123     std::string res = std::string( ( char* ) buffer, len );
124     OPENSSL_free( buffer );
125
126     return res;
127 }
128
129 void CRL::setSignature( std::string signature ) {
130     X509_CRL_sort( crl.get() );
131     X509_ALGOR *palg;
132     ASN1_BIT_STRING *psig;
133     // this is not intended use of the OPENSSL-API but API-limitations leave us with no other options.
134     X509_CRL_get0_signature(crl.get(), const_cast<const ASN1_BIT_STRING **>(&psig), const_cast<const X509_ALGOR**>(&palg));
135
136     const unsigned char* data = ( unsigned char* )( signature.data() );
137     const unsigned char* buffer = data;
138     X509_ALGOR *alg = d2i_X509_ALGOR( NULL, &buffer, signature.size() );
139     ASN1_BIT_STRING *sig = d2i_ASN1_BIT_STRING( NULL, &buffer, signature.size() + data - buffer );
140     ASN1_TIME *a1 = d2i_ASN1_TIME( NULL, &buffer, signature.size() + data - buffer );
141     ASN1_TIME *a2 = d2i_ASN1_TIME( NULL, &buffer, signature.size() + data - buffer );
142     *palg = *alg;
143     *psig = *sig;
144     X509_CRL_set1_lastUpdate( crl.get(), a1);
145     X509_CRL_set1_nextUpdate( crl.get(), a2);
146
147     //X509_ALGOR_free(alg);
148     //ASN1_BIT_STRING_free(sig);
149     ASN1_TIME_free(a1);
150     ASN1_TIME_free(a2);
151 }
152
153 bool CRL::needsResign() {
154     time_t current;
155     time( &current );
156     current += 60 * 60;// 1 hour
157     auto time = X509_CRL_get0_nextUpdate( crl.get() );
158
159     if( !time ) {
160         return true;
161     }
162
163     int cmp =  X509_cmp_time( time, &current );
164     return cmp < 0;
165 }