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