app_rand.c 2.7 KB

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