app_rand.c 3.0 KB

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