info.c 5.8 KB

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