info.c 5.7 KB

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