OPENSSL_info(): add the item OPENSSL_INFO_SEED_SOURCE and use it
[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 <stddef.h>
11 #include <openssl/crypto.h>
12 #include "internal/dso_conf.h"
13 #include "e_os.h"
14 #include "buildinf.h"
15 #include "internal/thread_once.h"
16
17 static char *seed_sources = NULL;
18 static CRYPTO_ONCE init_info = CRYPTO_ONCE_STATIC_INIT;
19
20 DEFINE_RUN_ONCE_STATIC(init_info_strings)
21 {
22     {
23         static char seeds[512] = "";
24
25 #define add_seeds_string(str)                                           \
26         do {                                                            \
27             if (seeds[0] != '\0')                                       \
28                 OPENSSL_strlcat(seeds, " ", sizeof(seeds));             \
29             OPENSSL_strlcat(seeds, str, sizeof(seeds));                 \
30         } while (0)
31 #define add_seeds_stringlist(label, strlist)                            \
32         do {                                                            \
33             add_seeds_string(label "(");                                \
34             {                                                           \
35                 const char *dev[] = strlist;                            \
36                 int first = 1;                                          \
37                                                                         \
38                 for (; *dev != NULL; dev++) {                           \
39                     if (!first)                                         \
40                         OPENSSL_strlcat(seeds, " ", sizeof(seeds));     \
41                     first = 0;                                          \
42                     OPENSSL_strlcat(seeds, *dev, sizeof(seeds));        \
43                 }                                                       \
44             }                                                           \
45             OPENSSL_strlcat(seeds, ")", sizeof(seeds));                 \
46         } while (0)
47
48 #ifdef OPENSSL_RAND_SEED_NONE
49         add_seeds_string("none");
50 #endif
51 #ifdef OPENSSL_RAND_SEED_RTDSC
52         add_seeds_string("stdsc");
53 #endif
54 #ifdef OPENSSL_RAND_SEED_RDCPU
55         add_seeds_string("rdrand ( rdseed rdrand )");
56 #endif
57 #ifdef OPENSSL_RAND_SEED_LIBRANDOM
58         add_seeds_string("C-library-random");
59 #endif
60 #ifdef OPENSSL_RAND_SEED_GETRANDOM
61         add_seeds_string("getrandom-syscall");
62 #endif
63 #ifdef OPENSSL_RAND_SEED_DEVRANDOM
64         add_seeds_stringlist("random-device", { DEVRANDOM, NULL });
65 #endif
66 #ifdef OPENSSL_RAND_SEED_EGD
67         add_seeds_stringlist("EGD", { DEVRANDOM_EGD, NULL });
68 #endif
69 #ifdef OPENSSL_RAND_SEED_OS
70         add_seeds_string("os-specific");
71 #endif
72         seed_sources = seeds;
73     }
74     return 1;
75 }
76
77 const char *OPENSSL_info(int t)
78 {
79     /*
80      * We don't care about the result.  Worst case scenario, the strings
81      * won't be initialised, i.e. remain NULL, which means that the info
82      * isn't available anyway...
83      */
84     (void)RUN_ONCE(&init_info, init_info_strings);
85
86     switch (t) {
87     case OPENSSL_INFO_CONFIG_DIR:
88         return OPENSSLDIR;
89     case OPENSSL_INFO_ENGINES_DIR:
90         return ENGINESDIR;
91     case OPENSSL_INFO_MODULES_DIR:
92         return MODULESDIR;
93     case OPENSSL_INFO_DSO_EXTENSION:
94         return DSO_EXTENSION;
95     case OPENSSL_INFO_DIR_FILENAME_SEPARATOR:
96 #if defined(_WIN32)
97         return "\\";
98 #elif defined(__VMS)
99         return "";
100 #else  /* Assume POSIX */
101         return "/";
102 #endif
103     case OPENSSL_INFO_LIST_SEPARATOR:
104         {
105             static const char list_sep[] = { LIST_SEPARATOR_CHAR, '\0' };
106             return list_sep;
107         }
108     case OPENSSL_INFO_SEED_SOURCE:
109         return seed_sources;
110     default:
111         break;
112     }
113     /* Not an error */
114     return NULL;
115 }