]> WPIA git - cassiopeia.git/blob - src/crypto/sslUtil.h
2ecba42b9874686014d7c72ac77919de321a1b63
[cassiopeia.git] / src / crypto / sslUtil.h
1 #pragma once
2
3 #include <memory>
4 #include <string>
5 #include <string.h>
6 #include <vector>
7 #include <cinttypes>
8 #include <ctime>
9
10 #include <openssl/ssl.h>
11
12 #include "db/database.h"
13
14 struct CAConfig {
15     std::string path;
16     std::string name;
17     std::string crlURL;
18     std::string crtURL;
19
20     std::shared_ptr<X509> ca;
21     std::shared_ptr<EVP_PKEY> caKey;
22     std::shared_ptr<ASN1_TIME> notBefore;
23
24     CAConfig( const std::string& name );
25
26     bool crlNeedsResign();
27 };
28
29 struct Profile {
30     uint16_t id;
31
32     std::string eku;
33     std::string ku;
34
35     std::vector<std::shared_ptr<CAConfig>> ca;
36     std::time_t maxValidity;
37     std::shared_ptr<CAConfig> getCA() {
38         std::shared_ptr<CAConfig> min = nullptr;
39
40         for( auto it = ca.rbegin(); it != ca.rend(); it++ ) {
41             if( X509_cmp_current_time( ( *it )->notBefore.get() ) < 0 ) {
42                 if( min != nullptr ) {
43                     if( strcmp( min->name.c_str(), ( *it )->name.c_str() ) < 0 ) {
44                         min = *it;
45                     }
46                 } else {
47                     min = *it;
48                 }
49             }
50         }
51
52         return min ? min : ca[0];
53     }
54 };
55
56 extern std::shared_ptr<int> ssl_lib_ref;
57
58 std::shared_ptr<X509> loadX509FromFile( const std::string& filename );
59 std::shared_ptr<EVP_PKEY> loadPkeyFromFile( const std::string& filename );
60
61 std::shared_ptr<SSL_CTX> generateSSLContext( bool server );
62 std::shared_ptr<BIO> openSerial( const std::string& name );
63 std::string timeToString( std::shared_ptr<ASN1_TIME> time );
64
65 void extractTimes( std::shared_ptr<X509> source, std::shared_ptr<SignedCertificate> cert );