]> WPIA git - cassiopeia.git/blob - src/X509.cpp
add: Include support for signing based on a SPKAC request
[cassiopeia.git] / src / X509.cpp
1 #include "X509.h"
2
3 #include <fstream>
4 #include <iostream>
5
6 #include <openssl/ssl.h>
7 #include <openssl/bio.h>
8 #include <openssl/x509v3.h>
9
10 X509Req::X509Req( X509_REQ* csr ) {
11     req = std::shared_ptr<X509_REQ>( csr, X509_REQ_free );
12     EVP_PKEY* pkt = X509_REQ_get_pubkey( req.get() );
13
14     if( !pkt ) {
15         throw "Error extracting public key";
16     }
17
18     pk = std::shared_ptr<EVP_PKEY>( pkt, EVP_PKEY_free );
19 }
20
21 X509Req::X509Req( std::string spkac ) {
22     if( spkac.compare( 0, 6, "SPKAC=" ) != 0 ) {
23         throw "Error: not a SPKAC";
24     }
25
26     spkac = spkac.substr( 6 );
27     NETSCAPE_SPKI* spki_p = NETSCAPE_SPKI_b64_decode( spkac.c_str(), spkac.size() );
28
29     if( !spki_p ) {
30         throw "Error: decode failed";
31     }
32
33     spki = std::shared_ptr<NETSCAPE_SPKI>( spki_p, NETSCAPE_SPKI_free );
34     EVP_PKEY* pkt_p = NETSCAPE_SPKI_get_pubkey( spki.get() );
35
36     if( !pkt_p ) {
37         throw "Error: reading SPKAC Pubkey failed";
38     }
39
40     pk = std::shared_ptr<EVP_PKEY>( pkt_p, EVP_PKEY_free );
41 }
42
43 int X509Req::verify() {
44     if( !req ) {
45         return NETSCAPE_SPKI_verify( spki.get(), pk.get() );
46     }
47
48     return X509_REQ_verify( req.get(), pk.get() );
49 }
50
51 std::shared_ptr<EVP_PKEY> X509Req::getPkey() {
52     return pk;
53 }
54
55 std::shared_ptr<X509Req> X509Req::parse( std::string filename ) {
56     std::shared_ptr<BIO> in = std::shared_ptr<BIO>( BIO_new_mem_buf( const_cast<char*>( filename.c_str() ), -1 ), BIO_free );
57     X509_REQ* req = PEM_read_bio_X509_REQ( in.get(), NULL, NULL, NULL );
58
59     if( !req ) {
60         throw "Error parsing CSR";
61     }
62
63     return std::shared_ptr<X509Req>( new X509Req( req ) );
64 }
65
66 std::shared_ptr<X509Req> X509Req::parseSPKAC( std::string content ) {
67     return std::shared_ptr<X509Req>( new X509Req( content ) );
68 }
69
70 int add_ext( std::shared_ptr<X509> issuer, std::shared_ptr<X509> subj, int nid, const char* value ) {
71     X509_EXTENSION* ex;
72     X509V3_CTX ctx;
73
74     /* This sets the 'context' of the extensions. */
75     /* No configuration database */
76     X509V3_set_ctx_nodb( &ctx );
77
78     /* Issuer and subject certs: both the target since it is self signed,
79      * no request and no CRL
80      */
81     X509V3_set_ctx( &ctx, issuer.get(), subj.get(), NULL, NULL, 0 );
82     ex = X509V3_EXT_conf_nid( NULL, &ctx, nid, const_cast<char*>( value ) );
83
84     if( !ex ) {
85         return 0;
86     }
87
88     X509_add_ext( subj.get(), ex, -1 );
89     X509_EXTENSION_free( ex );
90
91     return 1;
92 }
93
94 X509Cert::X509Cert() {
95     X509* c = X509_new();
96
97     if( !c ) {
98         throw "malloc failed";
99     }
100
101     target = std::shared_ptr<X509>( c, X509_free );
102
103     if( !X509_set_version( c, 2 ) ) {
104         throw "Setting X509-version to 3 failed";
105     }
106 }
107
108 void X509Cert::setIssuerNameFrom( std::shared_ptr<X509> caCert ) {
109     if( !X509_set_issuer_name( target.get(), X509_get_subject_name( caCert.get() ) ) ) {
110         throw "Error setting Issuer name";
111     }
112 }
113
114 void X509Cert::setPubkeyFrom( std::shared_ptr<X509Req> req ) {
115     std::shared_ptr<EVP_PKEY> pktmp = req->getPkey();
116
117     if( !X509_set_pubkey( target.get(), pktmp.get() ) ) {
118         throw "Setting public key failed.";
119     }
120 }
121
122 void X509Cert::setSerialNumber( int num ) {
123     ASN1_INTEGER_set( target.get()->cert_info->serialNumber, num );
124 }
125
126 void X509Cert::setTimes( uint32_t before, uint32_t after ) {
127     X509_gmtime_adj( X509_get_notBefore( target.get() ), before );
128     X509_gmtime_adj( X509_get_notAfter( target.get() ), after );
129 }
130
131 static X509_EXTENSION* do_ext_i2d( int ext_nid, int crit, ASN1_VALUE* ext_struc ) {
132     unsigned char* ext_der;
133     int ext_len;
134     ASN1_OCTET_STRING* ext_oct;
135     X509_EXTENSION* ext;
136     /* Convert internal representation to DER */
137     ext_der = NULL;
138     ext_len = ASN1_item_i2d( ext_struc, &ext_der, ASN1_ITEM_ptr( ASN1_ITEM_ref( GENERAL_NAMES ) ) );
139
140     if( ext_len < 0 ) {
141         goto merr;
142     }
143
144     if( !( ext_oct = M_ASN1_OCTET_STRING_new() ) ) {
145         goto merr;
146     }
147
148     ext_oct->data = ext_der;
149     ext_oct->length = ext_len;
150
151     ext = X509_EXTENSION_create_by_NID( NULL, ext_nid, crit, ext_oct );
152
153     if( !ext ) {
154         goto merr;
155     }
156
157     M_ASN1_OCTET_STRING_free( ext_oct );
158     return ext;
159
160 merr:
161     throw "memerr";
162 }
163
164 void X509Cert::setExtensions( std::shared_ptr<X509> caCert, std::vector<std::shared_ptr<SAN>>& sans ) {
165     add_ext( caCert, target, NID_basic_constraints, "critical,CA:FALSE" );
166     add_ext( caCert, target, NID_subject_key_identifier, "hash" );
167     add_ext( caCert, target, NID_authority_key_identifier, "keyid,issuer:always" );
168     add_ext( caCert, target, NID_key_usage, "critical,nonRepudiation,digitalSignature,keyEncipherment" );
169     add_ext( caCert, target, NID_ext_key_usage, "clientAuth, serverAuth" );
170     add_ext( caCert, target, NID_info_access, "OCSP;URI:http://ocsp.cacert.org" );
171     add_ext( caCert, target, NID_crl_distribution_points, "URI:http://crl.cacert.org/class3-revoke.crl" );
172
173     std::shared_ptr<GENERAL_NAMES> gens = std::shared_ptr<GENERAL_NAMES>(
174         sk_GENERAL_NAME_new_null(),
175         []( GENERAL_NAMES * ref ) {
176             if( ref ) {
177                 sk_GENERAL_NAME_pop_free( ref, GENERAL_NAME_free );
178             }
179         } );
180
181     for( auto& name : sans ) {
182         GENERAL_NAME* gen = GENERAL_NAME_new();
183
184         if( !gen ) {
185             throw "Malloc failure.";
186         }
187
188         gen->type = name->type == "DNS" ? GEN_DNS : name->type == "email" ? GEN_EMAIL : 0; // GEN_EMAIL;
189
190         if( !gen->type
191                 || !( gen->d.ia5 = M_ASN1_IA5STRING_new() )
192                 || !ASN1_STRING_set( gen->d.ia5, name->content.data(), name->content.size() ) ) {
193             GENERAL_NAME_free( gen );
194             throw "initing iasting5 failed";
195         }
196
197         sk_GENERAL_NAME_push( gens.get(), gen );
198     }
199
200     X509_EXTENSION* ext = do_ext_i2d( NID_subject_alt_name, 0/*critical*/, ( ASN1_VALUE* )gens.get() );
201
202     X509_add_ext( target.get(), ext, -1 );
203     X509_EXTENSION_free( ext );
204 }
205
206 std::shared_ptr<SignedCertificate> X509Cert::sign( std::shared_ptr<EVP_PKEY> caKey ) {
207     if( !X509_sign( target.get(), caKey.get(), EVP_sha512() ) ) {
208         throw "Signing failed.";
209     }
210
211     //X509_print_fp( stdout, target.get() );
212
213     std::shared_ptr<BIO> mem = std::shared_ptr<BIO>( BIO_new( BIO_s_mem() ), BIO_free );
214     PEM_write_bio_X509( mem.get(), target.get() );
215     BUF_MEM* buf;
216     BIO_get_mem_ptr( mem.get(), &buf );
217     std::shared_ptr<SignedCertificate> res = std::shared_ptr<SignedCertificate>( new SignedCertificate() );
218     res->certificate = std::string( buf->data, buf->data + buf->length );
219     res->serial = ASN1_INTEGER_get( target.get()->cert_info->serialNumber );
220     return res;
221 }