forked from gpellicci/Secure-File-Storage
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcertificate.h
More file actions
58 lines (50 loc) · 1.22 KB
/
certificate.h
File metadata and controls
58 lines (50 loc) · 1.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include <openssl/x509.h>
#include <openssl/x509_vfy.h>
#include <openssl/pem.h>
using namespace std;
bool readCertificate(string filename, X509* &cert){
FILE* file = fopen(filename.c_str(), "r");
if(!file)
return false;
cert = PEM_read_X509(file, NULL, NULL, NULL);
if(!cert)
return false;
if(fclose(file) != 0)
return false;
return true;
}
bool readCrl(string filename, X509_CRL* &crl){
FILE* file = fopen(filename.c_str(), "r");
if(!file)
return false;
crl = PEM_read_X509_CRL(file, NULL, NULL, NULL);
if(!crl)
return false;
if(fclose(file) != 0)
return false;
return true;
}
bool buildStore(X509* ca_cert, X509_CRL* crl, X509_STORE*& store){
store = X509_STORE_new();
if( !store )
return false;
if( !X509_STORE_add_cert(store, ca_cert) )
return false;
if( !X509_STORE_add_crl(store, crl) )
return false;
if( !X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK) )
return false;
return true;
}
bool verifyCertificate(X509_STORE* store, X509* cert){
X509_STORE_CTX* ctx = X509_STORE_CTX_new();
if( !ctx )
return false;
if( !X509_STORE_CTX_init(ctx, store, cert, NULL) )
return false;
int ret = X509_verify_cert(ctx);
X509_STORE_CTX_free(ctx);
if(ret != 1)
return false;
return true;
}