xref: /openssl/crypto/x509/x509_def.c (revision 7ed6de99)
1 /*
2  * Copyright 1995-2024 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the Apache License 2.0 (the "License").  You may not use
5  * this file except in compliance with the License.  You can obtain a copy
6  * in the file LICENSE in the source distribution or at
7  * https://www.openssl.org/source/license.html
8  */
9 
10 #include <stdio.h>
11 #include "internal/e_os.h"
12 #include "internal/cryptlib.h"
13 #include "internal/thread_once.h"
14 #include <openssl/crypto.h>
15 #include <openssl/x509.h>
16 
17 #if defined(_WIN32)
18 
19 static char x509_private_dir[MAX_PATH + 1];
20 static char *x509_private_dirptr = NULL;
21 
22 static char x509_cert_area[MAX_PATH + 1];
23 static char *x509_cert_areaptr = NULL;
24 
25 static char x509_cert_dir[MAX_PATH + 1];
26 static char *x509_cert_dirptr = NULL;
27 
28 static char x509_cert_file[MAX_PATH + 1];
29 static char *x509_cert_fileptr = NULL;
30 
get_windows_default_path(char * pathname,const char * suffix)31 static void get_windows_default_path(char *pathname, const char *suffix)
32 {
33     char *ossldir;
34 
35     ossldir = ossl_get_openssldir();
36 
37     if (ossldir == NULL)
38         return;
39 
40     OPENSSL_strlcpy(pathname, ossldir, MAX_PATH - 1);
41     if (MAX_PATH - strlen(pathname) > strlen(suffix))
42         strcat(pathname, suffix);
43 }
44 
45 static CRYPTO_ONCE openssldir_setup_init = CRYPTO_ONCE_STATIC_INIT;
DEFINE_RUN_ONCE_STATIC(do_openssldir_setup)46 DEFINE_RUN_ONCE_STATIC(do_openssldir_setup)
47 {
48     get_windows_default_path(x509_private_dir, "\\private");
49     if (strlen(x509_private_dir) > 0)
50         x509_private_dirptr = x509_private_dir;
51 
52     get_windows_default_path(x509_cert_area, "\\");
53     if (strlen(x509_cert_area) > 0)
54         x509_cert_areaptr = x509_cert_area;
55 
56     get_windows_default_path(x509_cert_dir, "\\certs");
57     if (strlen(x509_cert_dir) > 0)
58         x509_cert_dirptr = x509_cert_dir;
59 
60     get_windows_default_path(x509_cert_file, "\\cert.pem");
61     if (strlen(x509_cert_file) > 0)
62         x509_cert_fileptr = x509_cert_file;
63 
64     return 1;
65 }
66 #endif
67 
X509_get_default_private_dir(void)68 const char *X509_get_default_private_dir(void)
69 {
70 #if defined (_WIN32)
71     RUN_ONCE(&openssldir_setup_init, do_openssldir_setup);
72     return x509_private_dirptr;
73 #else
74     return X509_PRIVATE_DIR;
75 #endif
76 }
77 
X509_get_default_cert_area(void)78 const char *X509_get_default_cert_area(void)
79 {
80 #if defined (_WIN32)
81     RUN_ONCE(&openssldir_setup_init, do_openssldir_setup);
82     return x509_cert_areaptr;
83 #else
84     return X509_CERT_AREA;
85 #endif
86 }
87 
X509_get_default_cert_dir(void)88 const char *X509_get_default_cert_dir(void)
89 {
90 #if defined (_WIN32)
91     RUN_ONCE(&openssldir_setup_init, do_openssldir_setup);
92     return x509_cert_dirptr;
93 #else
94     return X509_CERT_DIR;
95 #endif
96 }
97 
X509_get_default_cert_file(void)98 const char *X509_get_default_cert_file(void)
99 {
100 #if defined (_WIN32)
101     RUN_ONCE(&openssldir_setup_init, do_openssldir_setup);
102     return x509_cert_fileptr;
103 #else
104     return X509_CERT_FILE;
105 #endif
106 }
107 
X509_get_default_cert_dir_env(void)108 const char *X509_get_default_cert_dir_env(void)
109 {
110     return X509_CERT_DIR_EVP;
111 }
112 
X509_get_default_cert_file_env(void)113 const char *X509_get_default_cert_file_env(void)
114 {
115     return X509_CERT_FILE_EVP;
116 }
117