Coverage Report

Created: 2024-11-21 07:03

/src/openssl/crypto/defaults.c
Line
Count
Source (jump to first uncovered line)
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 <openssl/opensslv.h>
12
#include "internal/thread_once.h"
13
#include "internal/cryptlib.h"
14
#include "internal/e_os.h"
15
16
#if defined(_WIN32) && defined(OSSL_WINCTX)
17
18
# define TOSTR(x) #x
19
# define MAKESTR(x) TOSTR(x)
20
# define NOQUOTE(x) x
21
# if defined(OSSL_WINCTX)
22
# define REGISTRY_KEY "SOFTWARE\\WOW6432Node\\OpenSSL" ##"-"## MAKESTR(OPENSSL_VERSION_MAJOR) ##"."## MAKESTR(OPENSSL_VERSION_MINOR) ##"-"## MAKESTR(OSSL_WINCTX)
23
# endif
24
25
/**
26
 * @brief The directory where OpenSSL is installed.
27
 */
28
static char openssldir[MAX_PATH + 1];
29
30
/**
31
 * @brief The pointer to the openssldir buffer
32
 */
33
static char *openssldirptr = NULL;
34
35
/**
36
 * @brief The directory where OpenSSL engines are located.
37
 */
38
39
static char enginesdir[MAX_PATH + 1];
40
41
/**
42
 * @brief The pointer to the enginesdir buffer
43
 */
44
static char *enginesdirptr = NULL;
45
46
/**
47
 * @brief The directory where OpenSSL modules are located.
48
 */
49
static char modulesdir[MAX_PATH + 1];
50
51
/**
52
 * @brief The pointer to the modulesdir buffer
53
 */
54
static char *modulesdirptr = NULL;
55
56
/**
57
 * @brief Get the list of Windows registry directories.
58
 *
59
 * This function retrieves a list of Windows registry directories.
60
 *
61
 * @return A pointer to a char array containing the registry directories.
62
 */
63
static char *get_windows_regdirs(char *dst, LPCTSTR valuename)
64
{
65
    char *retval = NULL;
66
# ifdef REGISTRY_KEY
67
    DWORD keysize;
68
    DWORD ktype;
69
    HKEY hkey;
70
    LSTATUS ret;
71
    DWORD index = 0;
72
    LPCTCH tempstr = NULL;
73
   
74
    ret = RegOpenKeyEx(HKEY_LOCAL_MACHINE,
75
                       TEXT(REGISTRY_KEY), KEY_WOW64_32KEY,
76
                       KEY_QUERY_VALUE, &hkey);
77
    if (ret != ERROR_SUCCESS)
78
        goto out;
79
80
    ret = RegQueryValueEx(hkey, valuename, NULL, &ktype, NULL,
81
                          &keysize);
82
    if (ret != ERROR_SUCCESS)
83
        goto out;
84
    if (ktype != REG_EXPAND_SZ)
85
        goto out;
86
    if (keysize > MAX_PATH)
87
        goto out;
88
89
    keysize++;
90
    tempstr = OPENSSL_zalloc(keysize * sizeof(TCHAR));
91
92
    if (tempstr == NULL)
93
        goto out;
94
95
    if (RegQueryValueEx(hkey, valuename,
96
                        NULL, &ktype, tempstr, &keysize) != ERROR_SUCCESS)
97
        goto out;
98
99
    if (!WideCharToMultiByte(CP_UTF8, 0, tempstr, -1, dst, keysize,
100
                             NULL, NULL)) 
101
        goto out;
102
103
    retval = dst;
104
out:
105
    OPENSSL_free(tempstr);
106
    RegCloseKey(hkey);
107
# endif /* REGISTRY_KEY */
108
    return retval;
109
}
110
111
static CRYPTO_ONCE defaults_setup_init = CRYPTO_ONCE_STATIC_INIT;
112
113
/**
114
 * @brief Function to setup default values to run once.
115
 * Only used in Windows environments.  Does run time initialization
116
 * of openssldir/modulesdir/enginesdir from the registry
117
 */
118
DEFINE_RUN_ONCE_STATIC(do_defaults_setup)
119
{
120
    get_windows_regdirs(openssldir, TEXT("OPENSSLDIR"));
121
    get_windows_regdirs(enginesdir, TEXT("ENGINESDIR"));
122
    get_windows_regdirs(modulesdir, TEXT("MODULESDIR"));
123
124
    /*
125
     * Set our pointers only if the directories are fetched properly
126
     */
127
    if (strlen(openssldir) > 0)
128
        openssldirptr = openssldir;
129
130
    if (strlen(enginesdir) > 0)
131
        enginesdirptr = enginesdir;
132
133
    if (strlen(modulesdir) > 0)
134
        modulesdirptr = modulesdir;
135
136
    return 1;
137
}
138
#endif /* defined(_WIN32) && defined(OSSL_WINCTX) */
139
140
/**
141
 * @brief Get the directory where OpenSSL is installed.
142
 *
143
 * @return A pointer to a string containing the OpenSSL directory path.
144
 */
145
const char *ossl_get_openssldir(void)
146
0
{
147
#if defined(_WIN32) && defined (OSSL_WINCTX)
148
    if (!RUN_ONCE(&defaults_setup_init, do_defaults_setup))
149
        return NULL;
150
    return (const char *)openssldirptr;
151
# else
152
0
    return OPENSSLDIR;
153
0
#endif
154
0
}
155
156
/**
157
 * @brief Get the directory where OpenSSL engines are located.
158
 *
159
 * @return A pointer to a string containing the engines directory path.
160
 */
161
const char *ossl_get_enginesdir(void)
162
0
{
163
#if defined(_WIN32) && defined (OSSL_WINCTX)
164
    if (!RUN_ONCE(&defaults_setup_init, do_defaults_setup))
165
        return NULL;
166
    return (const char *)enginesdirptr;
167
#else
168
0
    return ENGINESDIR;
169
0
#endif
170
0
}
171
172
/**
173
 * @brief Get the directory where OpenSSL modules are located.
174
 *
175
 * @return A pointer to a string containing the modules directory path.
176
 */
177
const char *ossl_get_modulesdir(void)
178
0
{
179
#if defined(_WIN32) && defined(OSSL_WINCTX)
180
    if (!RUN_ONCE(&defaults_setup_init, do_defaults_setup))
181
        return NULL;
182
    return (const char *)modulesdirptr;
183
#else
184
0
    return MODULESDIR;
185
0
#endif
186
0
}
187
188
/**
189
 * @brief Get the build time defined windows installer context
190
 *
191
 * @return A char pointer to a string representing the windows install context
192
 */
193
const char *ossl_get_wininstallcontext(void)
194
0
{
195
#if defined(_WIN32) && defined (OSSL_WINCTX)
196
    return MAKESTR(OSSL_WINCTX);
197
#else
198
0
    return "Undefined";
199
0
#endif
200
0
}