]> WPIA git - cassiopeia.git/blob - src/crypto/CRL.cpp
41fbe8b6472464af1dbcfab4ed5aec66b7cf44bd
[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         const unsigned char* data = ( unsigned char* )( time.data() );
46         d2i_ASN1_TIME( &rev->revocationDate, &data, time.size() );
47     } else {
48         X509_REVOKED_set_revocationDate( rev, tmptm.get() );
49     }
50
51     X509_CRL_add0_revoked( crl.get(), rev );
52
53     int len = i2d_ASN1_TIME( tmptm.get(), NULL );
54     unsigned char* buffer = ( unsigned char* ) OPENSSL_malloc( len );
55     unsigned char* pos = buffer;
56     i2d_ASN1_TIME( tmptm.get(), &pos );
57     std::string rettime = std::string( ( char* ) buffer, len );
58     OPENSSL_free( buffer );
59     return rettime;
60 }
61
62 void CRL::sign( std::shared_ptr<CAConfig> ca ) {
63     // Updating necessary CRL props
64     std::shared_ptr<ASN1_TIME> tmptm( ASN1_TIME_new(), ASN1_TIME_free );
65
66     if( !tmptm ) {
67         throw std::runtime_error("ASN1-Time Malloc fail");
68     }
69
70     X509_gmtime_adj( tmptm.get(), 0 );
71
72     logger::note("setting issuer");
73     if( !X509_CRL_set_issuer_name( crl.get(), X509_get_subject_name( ca->ca.get() ) ) ) {
74         throw std::runtime_error("Setting issuer failed");
75     }
76
77     logger::note("setting update");
78     X509_CRL_set_lastUpdate( crl.get(), tmptm.get() );
79
80     if( !X509_time_adj_ex( tmptm.get(), 1, 10, NULL ) ) {
81         throw std::runtime_error("Updating time failed");
82     }
83
84     logger::note("setting next update");
85     X509_CRL_set_nextUpdate( crl.get(), tmptm.get() );
86
87     logger::note("sorting");
88     // Sorting and signing
89     X509_CRL_sort( crl.get() );
90     logger::note("signing");
91     X509_CRL_sign( crl.get(), ca->caKey.get(), EVP_sha256() );
92 }
93
94 bool CRL::verify( std::shared_ptr<CAConfig> ca ) {
95     std::shared_ptr<EVP_PKEY> pk( X509_get_pubkey( ca->ca.get() ), EVP_PKEY_free );
96     return X509_CRL_verify( crl.get(), pk.get() ) > 0;
97 }
98
99 std::string CRL::toString() {
100     // Write out the new CRL
101     std::shared_ptr<BIO> mem( BIO_new( BIO_s_mem() ), BIO_free );
102     PEM_write_bio_X509_CRL( mem.get(), crl.get() );
103     BUF_MEM* bptr;
104     BIO_get_mem_ptr( mem.get(), &bptr );
105     std::string newCRL( bptr->data, bptr->length );
106     return newCRL;
107 }
108
109 std::string CRL::getSignature() {
110     int len = i2d_X509_ALGOR( crl->sig_alg, NULL );
111     len += i2d_ASN1_BIT_STRING( crl->signature, NULL );
112     len += i2d_ASN1_TIME( crl->crl->lastUpdate, NULL );
113     len += i2d_ASN1_TIME( crl->crl->nextUpdate, NULL );
114
115     unsigned char* buffer = ( unsigned char* ) OPENSSL_malloc( len );
116     unsigned char* pos = buffer;
117     i2d_X509_ALGOR( crl->sig_alg, &pos );
118     i2d_ASN1_BIT_STRING( crl->signature, &pos );
119     i2d_ASN1_TIME( crl->crl->lastUpdate, &pos );
120     i2d_ASN1_TIME( crl->crl->nextUpdate, &pos );
121     std::string res = std::string( ( char* ) buffer, len );
122     OPENSSL_free( buffer );
123
124     return res;
125 }
126
127 void CRL::setSignature( std::string signature ) {
128     X509_CRL_sort( crl.get() );
129     const unsigned char* data = ( unsigned char* )( signature.data() );
130     const unsigned char* buffer = data;
131     d2i_X509_ALGOR( &crl->sig_alg, &buffer, signature.size() );
132     d2i_ASN1_BIT_STRING( &crl->signature, &buffer, signature.size() + data - buffer );
133     d2i_ASN1_TIME( &crl->crl->lastUpdate, &buffer, signature.size() + data - buffer );
134     d2i_ASN1_TIME( &crl->crl->nextUpdate, &buffer, signature.size() + data - buffer );
135 }
136
137 bool CRL::needsResign() {
138     time_t current;
139     time( &current );
140     current += 60 * 60;// 1 hour
141     auto time = X509_CRL_get_nextUpdate( crl.get() );
142
143     if( !time ) {
144         return true;
145     }
146
147     int cmp =  X509_cmp_time( time, &current );
148     return cmp < 0;
149 }