app_rand.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*
  2. * Copyright 1995-2023 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 "internal/e_os.h" /* LIST_SEPARATOR_CHAR */
  10. #include "apps.h"
  11. #include <openssl/bio.h>
  12. #include <openssl/err.h>
  13. #include <openssl/rand.h>
  14. #include <openssl/conf.h>
  15. static char *save_rand_file;
  16. static STACK_OF(OPENSSL_STRING) *randfiles;
  17. void app_RAND_load_conf(CONF *c, const char *section)
  18. {
  19. const char *randfile = app_conf_try_string(c, section, "RANDFILE");
  20. if (randfile == NULL)
  21. return;
  22. if (RAND_load_file(randfile, -1) < 0) {
  23. BIO_printf(bio_err, "Can't load %s into RNG\n", randfile);
  24. ERR_print_errors(bio_err);
  25. }
  26. if (save_rand_file == NULL) {
  27. save_rand_file = OPENSSL_strdup(randfile);
  28. /* If some internal memory errors have occurred */
  29. if (save_rand_file == NULL) {
  30. BIO_printf(bio_err, "Can't duplicate %s\n", randfile);
  31. ERR_print_errors(bio_err);
  32. }
  33. }
  34. }
  35. static int loadfiles(char *name)
  36. {
  37. char *p;
  38. int last, ret = 1;
  39. for (;;) {
  40. last = 0;
  41. for (p = name; *p != '\0' && *p != LIST_SEPARATOR_CHAR; p++)
  42. continue;
  43. if (*p == '\0')
  44. last = 1;
  45. *p = '\0';
  46. if (RAND_load_file(name, -1) < 0) {
  47. BIO_printf(bio_err, "Can't load %s into RNG\n", name);
  48. ERR_print_errors(bio_err);
  49. ret = 0;
  50. }
  51. if (last)
  52. break;
  53. name = p + 1;
  54. if (*name == '\0')
  55. break;
  56. }
  57. return ret;
  58. }
  59. int app_RAND_load(void)
  60. {
  61. char *p;
  62. int i, ret = 1;
  63. for (i = 0; i < sk_OPENSSL_STRING_num(randfiles); i++) {
  64. p = sk_OPENSSL_STRING_value(randfiles, i);
  65. if (!loadfiles(p))
  66. ret = 0;
  67. }
  68. sk_OPENSSL_STRING_free(randfiles);
  69. return ret;
  70. }
  71. int app_RAND_write(void)
  72. {
  73. int ret = 1;
  74. if (save_rand_file == NULL)
  75. return 1;
  76. if (RAND_write_file(save_rand_file) == -1) {
  77. BIO_printf(bio_err, "Cannot write random bytes:\n");
  78. ERR_print_errors(bio_err);
  79. ret = 0;
  80. }
  81. OPENSSL_free(save_rand_file);
  82. save_rand_file = NULL;
  83. return ret;
  84. }
  85. /*
  86. * See comments in opt_verify for explanation of this.
  87. */
  88. enum r_range { OPT_R_ENUM };
  89. int opt_rand(int opt)
  90. {
  91. switch ((enum r_range)opt) {
  92. case OPT_R__FIRST:
  93. case OPT_R__LAST:
  94. break;
  95. case OPT_R_RAND:
  96. if (randfiles == NULL
  97. && (randfiles = sk_OPENSSL_STRING_new_null()) == NULL)
  98. return 0;
  99. if (!sk_OPENSSL_STRING_push(randfiles, opt_arg()))
  100. return 0;
  101. break;
  102. case OPT_R_WRITERAND:
  103. OPENSSL_free(save_rand_file);
  104. save_rand_file = OPENSSL_strdup(opt_arg());
  105. if (save_rand_file == NULL)
  106. return 0;
  107. break;
  108. }
  109. return 1;
  110. }