Coverage Report

Created: 2025-06-13 06:57

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