app_rand.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /*
  2. * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (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. void app_RAND_load_conf(CONF *c, const char *section)
  16. {
  17. const char *randfile = NCONF_get_string(c, section, "RANDFILE");
  18. if (randfile == NULL) {
  19. ERR_clear_error();
  20. return;
  21. }
  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. }
  29. static int loadfiles(char *name)
  30. {
  31. char *p;
  32. int last, ret = 1;
  33. for ( ; ; ) {
  34. last = 0;
  35. for (p = name; *p != '\0' && *p != LIST_SEPARATOR_CHAR; p++)
  36. continue;
  37. if (*p == '\0')
  38. last = 1;
  39. *p = '\0';
  40. if (RAND_load_file(name, -1) < 0) {
  41. BIO_printf(bio_err, "Can't load %s into RNG\n", name);
  42. ERR_print_errors(bio_err);
  43. ret = 0;
  44. }
  45. if (last)
  46. break;
  47. name = p + 1;
  48. if (*name == '\0')
  49. break;
  50. }
  51. return ret;
  52. }
  53. void app_RAND_write(void)
  54. {
  55. if (save_rand_file == NULL)
  56. return;
  57. if (RAND_write_file(save_rand_file) == -1) {
  58. BIO_printf(bio_err, "Cannot write random bytes:\n");
  59. ERR_print_errors(bio_err);
  60. }
  61. OPENSSL_free(save_rand_file);
  62. save_rand_file = NULL;
  63. }
  64. /*
  65. * See comments in opt_verify for explanation of this.
  66. */
  67. enum r_range { OPT_R_ENUM };
  68. int opt_rand(int opt)
  69. {
  70. switch ((enum r_range)opt) {
  71. case OPT_R__FIRST:
  72. case OPT_R__LAST:
  73. break;
  74. case OPT_R_RAND:
  75. return loadfiles(opt_arg());
  76. break;
  77. case OPT_R_WRITERAND:
  78. OPENSSL_free(save_rand_file);
  79. save_rand_file = OPENSSL_strdup(opt_arg());
  80. break;
  81. }
  82. return 1;
  83. }