Debian Codegraph @

ext/tls/load_system_cert.c

raw
1 /*
2 * 'system' certificate loader. This is included from both tls.c and
3 * tls-mbed.c.
4 * We can't consolidate it, for tls-mbed.c may be compiled into a separate
5 * DLL, and it is cumbersome on Windows to do cross-DLL reference.
6 */
7
8 #ifdef HAVE_WINCRYPT_H
9 static ScmObj system_cert_loader(ScmTLS *t,
10 int (*mem_loader)(ScmTLS*, BYTE*, DWORD))
11 {
12 const HCERTSTORE h = CertOpenStore(CERT_STORE_PROV_SYSTEM,
13 X509_ASN_ENCODING,
14 0,
15 (CERT_STORE_SHARE_STORE_FLAG |
16 CERT_STORE_SHARE_CONTEXT_FLAG |
17 CERT_STORE_OPEN_EXISTING_FLAG |
18 CERT_STORE_READONLY_FLAG |
19 CERT_SYSTEM_STORE_LOCAL_MACHINE),
20 TEXT("Root"));
21 if (h == NULL) {
22 Scm_Warn("Can't open certificate store");
23 return SCM_FALSE;
24 }
25
26 if(!CertControlStore(h, 0, CERT_STORE_CTRL_AUTO_RESYNC, NULL)) {
27 Scm_Warn("Can't resync certificate store");
28 CertCloseStore(h, 0);
29 return SCM_FALSE;
30 }
31
32
33 PCCERT_CONTEXT ctx = NULL;
34 int num_loaded_certs = 0;
35 while(1) {
36 ctx = CertEnumCertificatesInStore(h, ctx);
37
38 if (ctx == NULL) { break; }
39
40 /* We allow loading certs fail, for it is merely unsupported. */
41 int st = mem_loader(t, ctx->pbCertEncoded, ctx->cbCertEncoded);
42 if(st == 0) num_loaded_certs++;
43 }
44 if (num_loaded_certs == 0) {
45 Scm_Warn("No valid certificate found in the system's cert store");
46 }
47 CertCloseStore(h, 0);
48 return SCM_TRUE;
49 }
50 #else
51
52 /* Get in_gauche_cacert_path() */
53 #include "in_gauche_cacert_path.c"
54
55 static ScmObj system_cert_loader(ScmTLS *t,
56 int (*file_loader)(ScmTLS*, const char *))
57 {
58 static const char *cert_path = NULL;
59
60 if (cert_path == NULL) {
61 const char *cacert_paths[] = {
62 SYSTEM_CA_CERT_PATHS,
63 in_gauche_cacert_path(),
64 NULL
65 };
66 for (const char **p = cacert_paths; *p != NULL; p++) {
67 int st = file_loader(t, *p);
68 if (st == SSL_OK) {
69 cert_path = *p;
70 return SCM_TRUE;
71 }
72 }
73 } else {
74 int st = file_loader(t, cert_path);
75 if (st == SSL_OK) return SCM_TRUE;
76 }
77 return SCM_FALSE;
78 }
79 #endif