2
0

app_rand.c 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /*
  2. * Copyright 1995-2017 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. return;
  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. void app_RAND_write(void)
  55. {
  56. if (save_rand_file == NULL)
  57. return;
  58. if (RAND_write_file(save_rand_file) == -1) {
  59. BIO_printf(bio_err, "Cannot write random bytes:\n");
  60. ERR_print_errors(bio_err);
  61. }
  62. OPENSSL_free(save_rand_file);
  63. save_rand_file = NULL;
  64. }
  65. /*
  66. * See comments in opt_verify for explanation of this.
  67. */
  68. enum r_range { OPT_R_ENUM };
  69. int opt_rand(int opt)
  70. {
  71. switch ((enum r_range)opt) {
  72. case OPT_R__FIRST:
  73. case OPT_R__LAST:
  74. break;
  75. case OPT_R_RAND:
  76. return loadfiles(opt_arg());
  77. break;
  78. case OPT_R_WRITERAND:
  79. OPENSSL_free(save_rand_file);
  80. save_rand_file = OPENSSL_strdup(opt_arg());
  81. break;
  82. }
  83. return 1;
  84. }