app_rand.c 3.0 KB

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