1 | /*
|
---|
2 | * Copyright 2019-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 <openssl/crypto.h>
|
---|
11 | #include "crypto/rand.h"
|
---|
12 | #include "crypto/dso_conf.h"
|
---|
13 | #include "internal/thread_once.h"
|
---|
14 | #include "internal/cryptlib.h"
|
---|
15 | #include "internal/e_os.h"
|
---|
16 | #include "buildinf.h"
|
---|
17 |
|
---|
18 | #ifndef OPENSSL_NO_JITTER
|
---|
19 | # include <stdio.h>
|
---|
20 | # include <jitterentropy.h>
|
---|
21 | #endif
|
---|
22 |
|
---|
23 | #if defined(__arm__) || defined(__arm) || defined(__aarch64__)
|
---|
24 | # include "arm_arch.h"
|
---|
25 | # define CPU_INFO_STR_LEN 128
|
---|
26 | #elif defined(__s390__) || defined(__s390x__)
|
---|
27 | # include "s390x_arch.h"
|
---|
28 | # define CPU_INFO_STR_LEN 2048
|
---|
29 | #elif defined(__riscv)
|
---|
30 | # include "crypto/riscv_arch.h"
|
---|
31 | # define CPU_INFO_STR_LEN 2048
|
---|
32 | #else
|
---|
33 | # define CPU_INFO_STR_LEN 128
|
---|
34 | #endif
|
---|
35 |
|
---|
36 | /* extern declaration to avoid warning */
|
---|
37 | extern char ossl_cpu_info_str[];
|
---|
38 |
|
---|
39 | static char *seed_sources = NULL;
|
---|
40 |
|
---|
41 | char ossl_cpu_info_str[CPU_INFO_STR_LEN] = "";
|
---|
42 | #define CPUINFO_PREFIX "CPUINFO: "
|
---|
43 |
|
---|
44 | #ifndef VBOX
|
---|
45 | static CRYPTO_ONCE init_info = CRYPTO_ONCE_STATIC_INIT;
|
---|
46 |
|
---|
47 | DEFINE_RUN_ONCE_STATIC(init_info_strings)
|
---|
48 | {
|
---|
49 | #if defined(OPENSSL_CPUID_OBJ)
|
---|
50 | # if defined(__i386) || defined(__i386__) || defined(_M_IX86) || \
|
---|
51 | defined(__x86_64) || defined(__x86_64__) || \
|
---|
52 | defined(_M_AMD64) || defined(_M_X64)
|
---|
53 | const char *env;
|
---|
54 |
|
---|
55 | BIO_snprintf(ossl_cpu_info_str, sizeof(ossl_cpu_info_str),
|
---|
56 | CPUINFO_PREFIX "OPENSSL_ia32cap=0x%llx:0x%llx",
|
---|
57 | (unsigned long long)OPENSSL_ia32cap_P[0] |
|
---|
58 | (unsigned long long)OPENSSL_ia32cap_P[1] << 32,
|
---|
59 | (unsigned long long)OPENSSL_ia32cap_P[2] |
|
---|
60 | (unsigned long long)OPENSSL_ia32cap_P[3] << 32);
|
---|
61 | if ((env = getenv("OPENSSL_ia32cap")) != NULL)
|
---|
62 | BIO_snprintf(ossl_cpu_info_str + strlen(ossl_cpu_info_str),
|
---|
63 | sizeof(ossl_cpu_info_str) - strlen(ossl_cpu_info_str),
|
---|
64 | " env:%s", env);
|
---|
65 | # elif defined(__arm__) || defined(__arm) || defined(__aarch64__)
|
---|
66 | const char *env;
|
---|
67 |
|
---|
68 | BIO_snprintf(ossl_cpu_info_str, sizeof(ossl_cpu_info_str),
|
---|
69 | CPUINFO_PREFIX "OPENSSL_armcap=0x%x", OPENSSL_armcap_P);
|
---|
70 | if ((env = getenv("OPENSSL_armcap")) != NULL)
|
---|
71 | BIO_snprintf(ossl_cpu_info_str + strlen(ossl_cpu_info_str),
|
---|
72 | sizeof(ossl_cpu_info_str) - strlen(ossl_cpu_info_str),
|
---|
73 | " env:%s", env);
|
---|
74 | # elif defined(__s390__) || defined(__s390x__)
|
---|
75 | const char *env;
|
---|
76 |
|
---|
77 | BIO_snprintf(ossl_cpu_info_str, sizeof(ossl_cpu_info_str),
|
---|
78 | CPUINFO_PREFIX "OPENSSL_s390xcap="
|
---|
79 | "stfle:0x%llx:0x%llx:0x%llx:0x%llx:"
|
---|
80 | "kimd:0x%llx:0x%llx:"
|
---|
81 | "klmd:0x%llx:0x%llx:"
|
---|
82 | "km:0x%llx:0x%llx:"
|
---|
83 | "kmc:0x%llx:0x%llx:"
|
---|
84 | "kmac:0x%llx:0x%llx:"
|
---|
85 | "kmctr:0x%llx:0x%llx:"
|
---|
86 | "kmo:0x%llx:0x%llx:"
|
---|
87 | "kmf:0x%llx:0x%llx:"
|
---|
88 | "prno:0x%llx:0x%llx:"
|
---|
89 | "kma:0x%llx:0x%llx:"
|
---|
90 | "pcc:0x%llx:0x%llx:"
|
---|
91 | "kdsa:0x%llx:0x%llx",
|
---|
92 | OPENSSL_s390xcap_P.stfle[0], OPENSSL_s390xcap_P.stfle[1],
|
---|
93 | OPENSSL_s390xcap_P.stfle[2], OPENSSL_s390xcap_P.stfle[3],
|
---|
94 | OPENSSL_s390xcap_P.kimd[0], OPENSSL_s390xcap_P.kimd[1],
|
---|
95 | OPENSSL_s390xcap_P.klmd[0], OPENSSL_s390xcap_P.klmd[1],
|
---|
96 | OPENSSL_s390xcap_P.km[0], OPENSSL_s390xcap_P.km[1],
|
---|
97 | OPENSSL_s390xcap_P.kmc[0], OPENSSL_s390xcap_P.kmc[1],
|
---|
98 | OPENSSL_s390xcap_P.kmac[0], OPENSSL_s390xcap_P.kmac[1],
|
---|
99 | OPENSSL_s390xcap_P.kmctr[0], OPENSSL_s390xcap_P.kmctr[1],
|
---|
100 | OPENSSL_s390xcap_P.kmo[0], OPENSSL_s390xcap_P.kmo[1],
|
---|
101 | OPENSSL_s390xcap_P.kmf[0], OPENSSL_s390xcap_P.kmf[1],
|
---|
102 | OPENSSL_s390xcap_P.prno[0], OPENSSL_s390xcap_P.prno[1],
|
---|
103 | OPENSSL_s390xcap_P.kma[0], OPENSSL_s390xcap_P.kma[1],
|
---|
104 | OPENSSL_s390xcap_P.pcc[0], OPENSSL_s390xcap_P.pcc[1],
|
---|
105 | OPENSSL_s390xcap_P.kdsa[0], OPENSSL_s390xcap_P.kdsa[1]);
|
---|
106 | if ((env = getenv("OPENSSL_s390xcap")) != NULL)
|
---|
107 | BIO_snprintf(ossl_cpu_info_str + strlen(ossl_cpu_info_str),
|
---|
108 | sizeof(ossl_cpu_info_str) - strlen(ossl_cpu_info_str),
|
---|
109 | " env:%s", env);
|
---|
110 | # elif defined(__riscv)
|
---|
111 | const char *env;
|
---|
112 | char sep = '=';
|
---|
113 |
|
---|
114 | BIO_snprintf(ossl_cpu_info_str, sizeof(ossl_cpu_info_str),
|
---|
115 | CPUINFO_PREFIX "OPENSSL_riscvcap");
|
---|
116 | for (size_t i = 0; i < kRISCVNumCaps; ++i) {
|
---|
117 | if (OPENSSL_riscvcap_P[RISCV_capabilities[i].index]
|
---|
118 | & (1 << RISCV_capabilities[i].bit_offset)) {
|
---|
119 | /* Match, display the name */
|
---|
120 | BIO_snprintf(ossl_cpu_info_str + strlen(ossl_cpu_info_str),
|
---|
121 | sizeof(ossl_cpu_info_str) - strlen(ossl_cpu_info_str),
|
---|
122 | "%c%s", sep, RISCV_capabilities[i].name);
|
---|
123 | /* Only the first sep is '=' */
|
---|
124 | sep = '_';
|
---|
125 | }
|
---|
126 | }
|
---|
127 | /* If no capability is found, add back the = */
|
---|
128 | if (sep == '=') {
|
---|
129 | BIO_snprintf(ossl_cpu_info_str + strlen(ossl_cpu_info_str),
|
---|
130 | sizeof(ossl_cpu_info_str) - strlen(ossl_cpu_info_str),
|
---|
131 | "%c", sep);
|
---|
132 | }
|
---|
133 | if ((env = getenv("OPENSSL_riscvcap")) != NULL)
|
---|
134 | BIO_snprintf(ossl_cpu_info_str + strlen(ossl_cpu_info_str),
|
---|
135 | sizeof(ossl_cpu_info_str) - strlen(ossl_cpu_info_str),
|
---|
136 | " env:%s", env);
|
---|
137 | # endif
|
---|
138 | #endif
|
---|
139 |
|
---|
140 | {
|
---|
141 | static char seeds[512] = "";
|
---|
142 |
|
---|
143 | #define add_seeds_string(str) \
|
---|
144 | do { \
|
---|
145 | if (seeds[0] != '\0') \
|
---|
146 | OPENSSL_strlcat(seeds, " ", sizeof(seeds)); \
|
---|
147 | OPENSSL_strlcat(seeds, str, sizeof(seeds)); \
|
---|
148 | } while (0)
|
---|
149 | #define add_seeds_stringlist(label, strlist) \
|
---|
150 | do { \
|
---|
151 | add_seeds_string(label "("); \
|
---|
152 | { \
|
---|
153 | const char *dev[] = { strlist, NULL }; \
|
---|
154 | const char **p; \
|
---|
155 | int first = 1; \
|
---|
156 | \
|
---|
157 | for (p = dev; *p != NULL; p++) { \
|
---|
158 | if (!first) \
|
---|
159 | OPENSSL_strlcat(seeds, " ", sizeof(seeds)); \
|
---|
160 | first = 0; \
|
---|
161 | OPENSSL_strlcat(seeds, *p, sizeof(seeds)); \
|
---|
162 | } \
|
---|
163 | } \
|
---|
164 | OPENSSL_strlcat(seeds, ")", sizeof(seeds)); \
|
---|
165 | } while (0)
|
---|
166 |
|
---|
167 | #ifdef OPENSSL_RAND_SEED_NONE
|
---|
168 | add_seeds_string("none");
|
---|
169 | #endif
|
---|
170 | #ifdef OPENSSL_RAND_SEED_RDTSC
|
---|
171 | add_seeds_string("rdtsc");
|
---|
172 | #endif
|
---|
173 | #ifdef OPENSSL_RAND_SEED_RDCPU
|
---|
174 | # ifdef __aarch64__
|
---|
175 | add_seeds_string("rndr ( rndrrs rndr )");
|
---|
176 | # else
|
---|
177 | add_seeds_string("rdrand ( rdseed rdrand )");
|
---|
178 | # endif
|
---|
179 | #endif
|
---|
180 | #ifdef OPENSSL_RAND_SEED_GETRANDOM
|
---|
181 | add_seeds_string("getrandom-syscall");
|
---|
182 | #endif
|
---|
183 | #ifdef OPENSSL_RAND_SEED_DEVRANDOM
|
---|
184 | add_seeds_stringlist("random-device", DEVRANDOM);
|
---|
185 | #endif
|
---|
186 | #ifdef OPENSSL_RAND_SEED_EGD
|
---|
187 | add_seeds_stringlist("EGD", DEVRANDOM_EGD);
|
---|
188 | #endif
|
---|
189 | #ifdef OPENSSL_RAND_SEED_OS
|
---|
190 | add_seeds_string("os-specific");
|
---|
191 | #endif
|
---|
192 | #ifndef OPENSSL_NO_JITTER
|
---|
193 | {
|
---|
194 | char buf[32];
|
---|
195 |
|
---|
196 | BIO_snprintf(buf, sizeof(buf), "JITTER (%d)", jent_version());
|
---|
197 | add_seeds_string(buf);
|
---|
198 | }
|
---|
199 | #endif
|
---|
200 | seed_sources = seeds;
|
---|
201 | }
|
---|
202 | return 1;
|
---|
203 | }
|
---|
204 | #endif /* VBOX */
|
---|
205 |
|
---|
206 | const char *OPENSSL_info(int t)
|
---|
207 | {
|
---|
208 | #ifndef VBOX
|
---|
209 | /*
|
---|
210 | * We don't care about the result. Worst case scenario, the strings
|
---|
211 | * won't be initialised, i.e. remain NULL, which means that the info
|
---|
212 | * isn't available anyway...
|
---|
213 | */
|
---|
214 | (void)RUN_ONCE(&init_info, init_info_strings);
|
---|
215 |
|
---|
216 | switch (t) {
|
---|
217 | case OPENSSL_INFO_CONFIG_DIR:
|
---|
218 | return ossl_get_openssldir();
|
---|
219 | case OPENSSL_INFO_ENGINES_DIR:
|
---|
220 | return ossl_get_enginesdir();
|
---|
221 | case OPENSSL_INFO_MODULES_DIR:
|
---|
222 | return ossl_get_modulesdir();
|
---|
223 | case OPENSSL_INFO_DSO_EXTENSION:
|
---|
224 | return DSO_EXTENSION;
|
---|
225 | case OPENSSL_INFO_DIR_FILENAME_SEPARATOR:
|
---|
226 | #if defined(_WIN32)
|
---|
227 | return "\\";
|
---|
228 | #elif defined(__VMS)
|
---|
229 | return "";
|
---|
230 | #else /* Assume POSIX */
|
---|
231 | return "/";
|
---|
232 | #endif
|
---|
233 | case OPENSSL_INFO_LIST_SEPARATOR:
|
---|
234 | {
|
---|
235 | static const char list_sep[] = { LIST_SEPARATOR_CHAR, '\0' };
|
---|
236 | return list_sep;
|
---|
237 | }
|
---|
238 | case OPENSSL_INFO_SEED_SOURCE:
|
---|
239 | return seed_sources;
|
---|
240 | case OPENSSL_INFO_CPU_SETTINGS:
|
---|
241 | /*
|
---|
242 | * If successfully initialized, ossl_cpu_info_str will start
|
---|
243 | * with CPUINFO_PREFIX, if failed it will be an empty string.
|
---|
244 | * Strip away the CPUINFO_PREFIX which we don't need here.
|
---|
245 | */
|
---|
246 | if (ossl_cpu_info_str[0] != '\0')
|
---|
247 | return ossl_cpu_info_str + strlen(CPUINFO_PREFIX);
|
---|
248 | break;
|
---|
249 | case OPENSSL_INFO_WINDOWS_CONTEXT:
|
---|
250 | return ossl_get_wininstallcontext();
|
---|
251 | default:
|
---|
252 | break;
|
---|
253 | }
|
---|
254 | #endif
|
---|
255 | /* Not an error */
|
---|
256 | return NULL;
|
---|
257 | }
|
---|