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
|
#include "MSX509CertStore.h"
#include "X509CertStore_p.h"
#include "../../log.h"
#include <Windows.h>
#include <openssl/err.h>
using namespace digidoc;
/**
* Loads all certificates from system store and adds these to the certificate store.
*
* @throws IOException exception is throws if failed to open certstore
*/
MSX509CertStore::MSX509CertStore() throw(IOException)
{
loadCerts("ROOT");
loadCerts("CA");
INFO("Loaded %d certificates into certificate store.", sk_X509_num(d->stack));
}
/**
* Load all certificates found in CertStore and adds these to the cert store.
*
* @param provider name.
* @throws IOException exception is throws if failed open CertStore.
*/
void MSX509CertStore::loadCerts(const std::string &provider) throw(IOException)
{
HCERTSTORE s = CertOpenStore(CERT_STORE_PROV_SYSTEM_A,
X509_ASN_ENCODING, 0, CERT_SYSTEM_STORE_CURRENT_USER, provider.c_str() );
if(!s)
THROW_IOEXCEPTION("Failed to ope CertStore with provider %s, can not load cert store.", provider.c_str());
PCCERT_CONTEXT pc = 0;
while((pc = CertEnumCertificatesInStore(s, pc)))
{
const unsigned char *pBytes = pc->pbCertEncoded;
X509 *c = d2i_X509(0, &pBytes, pc->cbCertEncoded);
if(!c)
WARN("Cant add cert %ld to X509_STORE, %s", ASN1_INTEGER_get(X509_get_serialNumber(c)), ERR_reason_error_string(ERR_get_error()));
sk_X509_push(d->stack, c);
if(!X509_STORE_add_cert(d->store, c))
WARN("Cant add cert %ld to X509_STORE, %s", ASN1_INTEGER_get(X509_get_serialNumber(c)), ERR_reason_error_string(ERR_get_error()));
}
CertCloseStore(s, 0);
}
|