Coverage Report

Created: 2023-06-07 07:24

/src/openssl/crypto/info.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 2019-2022 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
#if defined(__arm__) || defined(__arm) || defined(__aarch64__)
19
# include "arm_arch.h"
20
# define CPU_INFO_STR_LEN 128
21
#elif defined(__s390__) || defined(__s390x__)
22
# include "s390x_arch.h"
23
# define CPU_INFO_STR_LEN 2048
24
#else
25
# define CPU_INFO_STR_LEN 128
26
#endif
27
28
/* extern declaration to avoid warning */
29
extern char ossl_cpu_info_str[];
30
31
static char *seed_sources = NULL;
32
33
char ossl_cpu_info_str[CPU_INFO_STR_LEN] = "";
34
0
#define CPUINFO_PREFIX "CPUINFO: "
35
36
static CRYPTO_ONCE init_info = CRYPTO_ONCE_STATIC_INIT;
37
38
DEFINE_RUN_ONCE_STATIC(init_info_strings)
39
0
{
40
0
#if defined(OPENSSL_CPUID_OBJ)
41
0
# if defined(__i386)   || defined(__i386__)   || defined(_M_IX86) || \
42
0
     defined(__x86_64) || defined(__x86_64__) || \
43
0
     defined(_M_AMD64) || defined(_M_X64)
44
0
    const char *env;
45
46
0
    BIO_snprintf(ossl_cpu_info_str, sizeof(ossl_cpu_info_str),
47
0
                 CPUINFO_PREFIX "OPENSSL_ia32cap=0x%llx:0x%llx",
48
0
                 (unsigned long long)OPENSSL_ia32cap_P[0] |
49
0
                 (unsigned long long)OPENSSL_ia32cap_P[1] << 32,
50
0
                 (unsigned long long)OPENSSL_ia32cap_P[2] |
51
0
                 (unsigned long long)OPENSSL_ia32cap_P[3] << 32);
52
0
    if ((env = getenv("OPENSSL_ia32cap")) != NULL)
53
0
        BIO_snprintf(ossl_cpu_info_str + strlen(ossl_cpu_info_str),
54
0
                     sizeof(ossl_cpu_info_str) - strlen(ossl_cpu_info_str),
55
0
                     " env:%s", env);
56
# elif defined(__arm__) || defined(__arm) || defined(__aarch64__)
57
    const char *env;
58
59
    BIO_snprintf(ossl_cpu_info_str, sizeof(ossl_cpu_info_str),
60
                 CPUINFO_PREFIX "OPENSSL_armcap=0x%x", OPENSSL_armcap_P);
61
    if ((env = getenv("OPENSSL_armcap")) != 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(__s390__) || defined(__s390x__)
66
    const char *env;
67
68
    BIO_snprintf(ossl_cpu_info_str, sizeof(ossl_cpu_info_str),
69
                 CPUINFO_PREFIX "OPENSSL_s390xcap="
70
                 "stfle:0x%llx:0x%llx:0x%llx:0x%llx:"
71
                 "kimd:0x%llx:0x%llx:"
72
                 "klmd:0x%llx:0x%llx:"
73
                 "km:0x%llx:0x%llx:"
74
                 "kmc:0x%llx:0x%llx:"
75
                 "kmac:0x%llx:0x%llx:"
76
                 "kmctr:0x%llx:0x%llx:"
77
                 "kmo:0x%llx:0x%llx:"
78
                 "kmf:0x%llx:0x%llx:"
79
                 "prno:0x%llx:0x%llx:"
80
                 "kma:0x%llx:0x%llx:"
81
                 "pcc:0x%llx:0x%llx:"
82
                 "kdsa:0x%llx:0x%llx",
83
                 OPENSSL_s390xcap_P.stfle[0], OPENSSL_s390xcap_P.stfle[1],
84
                 OPENSSL_s390xcap_P.stfle[2], OPENSSL_s390xcap_P.stfle[3],
85
                 OPENSSL_s390xcap_P.kimd[0], OPENSSL_s390xcap_P.kimd[1],
86
                 OPENSSL_s390xcap_P.klmd[0], OPENSSL_s390xcap_P.klmd[1],
87
                 OPENSSL_s390xcap_P.km[0], OPENSSL_s390xcap_P.km[1],
88
                 OPENSSL_s390xcap_P.kmc[0], OPENSSL_s390xcap_P.kmc[1],
89
                 OPENSSL_s390xcap_P.kmac[0], OPENSSL_s390xcap_P.kmac[1],
90
                 OPENSSL_s390xcap_P.kmctr[0], OPENSSL_s390xcap_P.kmctr[1],
91
                 OPENSSL_s390xcap_P.kmo[0], OPENSSL_s390xcap_P.kmo[1],
92
                 OPENSSL_s390xcap_P.kmf[0], OPENSSL_s390xcap_P.kmf[1],
93
                 OPENSSL_s390xcap_P.prno[0], OPENSSL_s390xcap_P.prno[1],
94
                 OPENSSL_s390xcap_P.kma[0], OPENSSL_s390xcap_P.kma[1],
95
                 OPENSSL_s390xcap_P.pcc[0], OPENSSL_s390xcap_P.pcc[1],
96
                 OPENSSL_s390xcap_P.kdsa[0], OPENSSL_s390xcap_P.kdsa[1]);
97
    if ((env = getenv("OPENSSL_s390xcap")) != NULL)
98
        BIO_snprintf(ossl_cpu_info_str + strlen(ossl_cpu_info_str),
99
                     sizeof(ossl_cpu_info_str) - strlen(ossl_cpu_info_str),
100
                     " env:%s", env);
101
# endif
102
0
#endif
103
104
0
    {
105
0
        static char seeds[512] = "";
106
107
0
#define add_seeds_string(str)                                           \
108
0
        do {                                                            \
109
0
            if (seeds[0] != '\0')                                       \
110
0
                OPENSSL_strlcat(seeds, " ", sizeof(seeds));             \
111
0
            OPENSSL_strlcat(seeds, str, sizeof(seeds));                 \
112
0
        } while (0)
113
0
#define add_seeds_stringlist(label, strlist)                            \
114
0
        do {                                                            \
115
0
            add_seeds_string(label "(");                                \
116
0
            {                                                           \
117
0
                const char *dev[] =  { strlist, NULL };                 \
118
0
                const char **p;                                         \
119
0
                int first = 1;                                          \
120
0
                                                                        \
121
0
                for (p = dev; *p != NULL; p++) {                        \
122
0
                    if (!first)                                         \
123
0
                        OPENSSL_strlcat(seeds, " ", sizeof(seeds));     \
124
0
                    first = 0;                                          \
125
0
                    OPENSSL_strlcat(seeds, *p, sizeof(seeds));          \
126
0
                }                                                       \
127
0
            }                                                           \
128
0
            OPENSSL_strlcat(seeds, ")", sizeof(seeds));                 \
129
0
        } while (0)
130
131
#ifdef OPENSSL_RAND_SEED_NONE
132
        add_seeds_string("none");
133
#endif
134
#ifdef OPENSSL_RAND_SEED_RDTSC
135
        add_seeds_string("rdtsc");
136
#endif
137
#ifdef OPENSSL_RAND_SEED_RDCPU
138
# ifdef __aarch64__
139
        add_seeds_string("rndr ( rndrrs rndr )");
140
# else
141
        add_seeds_string("rdrand ( rdseed rdrand )");
142
# endif
143
#endif
144
#ifdef OPENSSL_RAND_SEED_LIBRANDOM
145
        add_seeds_string("C-library-random");
146
#endif
147
#ifdef OPENSSL_RAND_SEED_GETRANDOM
148
        add_seeds_string("getrandom-syscall");
149
#endif
150
#ifdef OPENSSL_RAND_SEED_DEVRANDOM
151
        add_seeds_stringlist("random-device", DEVRANDOM);
152
#endif
153
#ifdef OPENSSL_RAND_SEED_EGD
154
        add_seeds_stringlist("EGD", DEVRANDOM_EGD);
155
#endif
156
0
#ifdef OPENSSL_RAND_SEED_OS
157
0
        add_seeds_string("os-specific");
158
0
#endif
159
0
        seed_sources = seeds;
160
0
    }
161
0
    return 1;
162
0
}
163
164
const char *OPENSSL_info(int t)
165
0
{
166
    /*
167
     * We don't care about the result.  Worst case scenario, the strings
168
     * won't be initialised, i.e. remain NULL, which means that the info
169
     * isn't available anyway...
170
     */
171
0
    (void)RUN_ONCE(&init_info, init_info_strings);
172
173
0
    switch (t) {
174
0
    case OPENSSL_INFO_CONFIG_DIR:
175
0
        return OPENSSLDIR;
176
0
    case OPENSSL_INFO_ENGINES_DIR:
177
0
        return ENGINESDIR;
178
0
    case OPENSSL_INFO_MODULES_DIR:
179
0
        return MODULESDIR;
180
0
    case OPENSSL_INFO_DSO_EXTENSION:
181
0
        return DSO_EXTENSION;
182
0
    case OPENSSL_INFO_DIR_FILENAME_SEPARATOR:
183
#if defined(_WIN32)
184
        return "\\";
185
#elif defined(__VMS)
186
        return "";
187
#else  /* Assume POSIX */
188
0
        return "/";
189
0
#endif
190
0
    case OPENSSL_INFO_LIST_SEPARATOR:
191
0
        {
192
0
            static const char list_sep[] = { LIST_SEPARATOR_CHAR, '\0' };
193
0
            return list_sep;
194
0
        }
195
0
    case OPENSSL_INFO_SEED_SOURCE:
196
0
        return seed_sources;
197
0
    case OPENSSL_INFO_CPU_SETTINGS:
198
        /*
199
         * If successfully initialized, ossl_cpu_info_str will start
200
         * with CPUINFO_PREFIX, if failed it will be an empty string.
201
         * Strip away the CPUINFO_PREFIX which we don't need here.
202
         */
203
0
        if (ossl_cpu_info_str[0] != '\0')
204
0
            return ossl_cpu_info_str + strlen(CPUINFO_PREFIX);
205
0
        break;
206
0
    default:
207
0
        break;
208
0
    }
209
    /* Not an error */
210
0
    return NULL;
211
0
}