info.c 7.9 KB

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