clearing the ecx private key memory
[openssl.git] / crypto / info.c
1 /*
2  * Copyright 2019 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 "internal/dso_conf.h"
12 #include "internal/thread_once.h"
13 #include "internal/cryptlib.h"
14 #include "e_os.h"
15 #include "buildinf.h"
16
17 #if defined(__arm__) || defined(__arm) || defined(__aarch64__)
18 # include "arm_arch.h"
19 #endif
20
21 /* extern declaration to avoid warning */
22 extern char ossl_cpu_info_str[];
23
24 static char *seed_sources = NULL;
25
26 char ossl_cpu_info_str[128] = "";
27 #define CPUINFO_PREFIX "CPUINFO: "
28
29 static CRYPTO_ONCE init_info = CRYPTO_ONCE_STATIC_INIT;
30
31 DEFINE_RUN_ONCE_STATIC(init_info_strings)
32 {
33 #if defined(OPENSSL_CPUID_OBJ)
34 # if defined(__i386)   || defined(__i386__)   || defined(_M_IX86) || \
35      defined(__x86_64) || defined(__x86_64__) || \
36      defined(_M_AMD64) || defined(_M_X64)
37     const char *env;
38
39     BIO_snprintf(ossl_cpu_info_str, sizeof(ossl_cpu_info_str),
40                  CPUINFO_PREFIX "OPENSSL_ia32cap=0x%llx:0x%llx",
41                  (long long)OPENSSL_ia32cap_P[0] |
42                  (long long)OPENSSL_ia32cap_P[1] << 32,
43                  (long long)OPENSSL_ia32cap_P[2] |
44                  (long long)OPENSSL_ia32cap_P[3] << 32);
45     if ((env = getenv("OPENSSL_ia32cap")) != NULL)
46         BIO_snprintf(ossl_cpu_info_str + strlen(ossl_cpu_info_str),
47                      sizeof(ossl_cpu_info_str) - strlen(ossl_cpu_info_str),
48                      " env:%s", env);
49 # elif defined(__arm__) || defined(__arm) || defined(__aarch64__)
50     const char *env;
51
52     BIO_snprintf(ossl_cpu_info_str, sizeof(ossl_cpu_info_str),
53                  CPUINFO_PREFIX "OPENSSL_armcap=0x%x", OPENSSL_armcap_P);
54     if ((env = getenv("OPENSSL_armcap")) != NULL)
55         BIO_snprintf(ossl_cpu_info_str + strlen(ossl_cpu_info_str),
56                      sizeof(ossl_cpu_info_str) - strlen(ossl_cpu_info_str),
57                      " env:%s", env);
58 # endif
59 #endif
60
61     {
62         static char seeds[512] = "";
63
64 #define add_seeds_string(str)                                           \
65         do {                                                            \
66             if (seeds[0] != '\0')                                       \
67                 OPENSSL_strlcat(seeds, " ", sizeof(seeds));             \
68             OPENSSL_strlcat(seeds, str, sizeof(seeds));                 \
69         } while (0)
70 #define add_seeds_stringlist(label, strlist)                            \
71         do {                                                            \
72             add_seeds_string(label "(");                                \
73             {                                                           \
74                 const char *dev[] = strlist;                            \
75                 int first = 1;                                          \
76                                                                         \
77                 for (; *dev != NULL; dev++) {                           \
78                     if (!first)                                         \
79                         OPENSSL_strlcat(seeds, " ", sizeof(seeds));     \
80                     first = 0;                                          \
81                     OPENSSL_strlcat(seeds, *dev, sizeof(seeds));        \
82                 }                                                       \
83             }                                                           \
84             OPENSSL_strlcat(seeds, ")", sizeof(seeds));                 \
85         } while (0)
86
87 #ifdef OPENSSL_RAND_SEED_NONE
88         add_seeds_string("none");
89 #endif
90 #ifdef OPENSSL_RAND_SEED_RTDSC
91         add_seeds_string("stdsc");
92 #endif
93 #ifdef OPENSSL_RAND_SEED_RDCPU
94         add_seeds_string("rdrand ( rdseed rdrand )");
95 #endif
96 #ifdef OPENSSL_RAND_SEED_LIBRANDOM
97         add_seeds_string("C-library-random");
98 #endif
99 #ifdef OPENSSL_RAND_SEED_GETRANDOM
100         add_seeds_string("getrandom-syscall");
101 #endif
102 #ifdef OPENSSL_RAND_SEED_DEVRANDOM
103         add_seeds_stringlist("random-device", { DEVRANDOM, NULL });
104 #endif
105 #ifdef OPENSSL_RAND_SEED_EGD
106         add_seeds_stringlist("EGD", { DEVRANDOM_EGD, NULL });
107 #endif
108 #ifdef OPENSSL_RAND_SEED_OS
109         add_seeds_string("os-specific");
110 #endif
111         seed_sources = seeds;
112     }
113     return 1;
114 }
115
116 const char *OPENSSL_info(int t)
117 {
118     /*
119      * We don't care about the result.  Worst case scenario, the strings
120      * won't be initialised, i.e. remain NULL, which means that the info
121      * isn't available anyway...
122      */
123     (void)RUN_ONCE(&init_info, init_info_strings);
124
125     switch (t) {
126     case OPENSSL_INFO_CONFIG_DIR:
127         return OPENSSLDIR;
128     case OPENSSL_INFO_ENGINES_DIR:
129         return ENGINESDIR;
130     case OPENSSL_INFO_MODULES_DIR:
131         return MODULESDIR;
132     case OPENSSL_INFO_DSO_EXTENSION:
133         return DSO_EXTENSION;
134     case OPENSSL_INFO_DIR_FILENAME_SEPARATOR:
135 #if defined(_WIN32)
136         return "\\";
137 #elif defined(__VMS)
138         return "";
139 #else  /* Assume POSIX */
140         return "/";
141 #endif
142     case OPENSSL_INFO_LIST_SEPARATOR:
143         {
144             static const char list_sep[] = { LIST_SEPARATOR_CHAR, '\0' };
145             return list_sep;
146         }
147     case OPENSSL_INFO_SEED_SOURCE:
148         return seed_sources;
149     case OPENSSL_INFO_CPU_SETTINGS:
150         /*
151          * If successfully initialized, ossl_cpu_info_str will start
152          * with CPUINFO_PREFIX, if failed it will be an empty string.
153          * Strip away the CPUINFO_PREFIX which we don't need here.
154          */
155         if (ossl_cpu_info_str[0] != '\0')
156             return ossl_cpu_info_str + strlen(CPUINFO_PREFIX);
157         break;
158     default:
159         break;
160     }
161     /* Not an error */
162     return NULL;
163 }