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