ui_util.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /*
  2. * Copyright 2002-2020 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 <string.h>
  10. #include <openssl/pem.h> /* PEM_def_callback() */
  11. #include "internal/thread_once.h"
  12. #include "ui_local.h"
  13. #ifndef BUFSIZ
  14. #define BUFSIZ 256
  15. #endif
  16. int UI_UTIL_read_pw_string(char *buf, int length, const char *prompt,
  17. int verify)
  18. {
  19. char buff[BUFSIZ];
  20. int ret;
  21. ret =
  22. UI_UTIL_read_pw(buf, buff, (length > BUFSIZ) ? BUFSIZ : length,
  23. prompt, verify);
  24. OPENSSL_cleanse(buff, BUFSIZ);
  25. return ret;
  26. }
  27. int UI_UTIL_read_pw(char *buf, char *buff, int size, const char *prompt,
  28. int verify)
  29. {
  30. int ok = 0;
  31. UI *ui;
  32. if (size < 1)
  33. return -1;
  34. ui = UI_new();
  35. if (ui != NULL) {
  36. ok = UI_add_input_string(ui, prompt, 0, buf, 0, size - 1);
  37. if (ok >= 0 && verify)
  38. ok = UI_add_verify_string(ui, prompt, 0, buff, 0, size - 1, buf);
  39. if (ok >= 0)
  40. ok = UI_process(ui);
  41. UI_free(ui);
  42. }
  43. if (ok > 0)
  44. ok = 0;
  45. return ok;
  46. }
  47. /*
  48. * Wrapper around pem_password_cb, a method to help older APIs use newer
  49. * ones.
  50. */
  51. struct pem_password_cb_data {
  52. pem_password_cb *cb;
  53. int rwflag;
  54. };
  55. static void ui_new_method_data(void *parent, void *ptr, CRYPTO_EX_DATA *ad,
  56. int idx, long argl, void *argp)
  57. {
  58. /*
  59. * Do nothing, the data is allocated externally and assigned later with
  60. * CRYPTO_set_ex_data()
  61. */
  62. }
  63. static int ui_dup_method_data(CRYPTO_EX_DATA *to, const CRYPTO_EX_DATA *from,
  64. void **pptr, int idx, long argl, void *argp)
  65. {
  66. if (*pptr != NULL)
  67. *pptr = OPENSSL_memdup(*pptr, sizeof(struct pem_password_cb_data));
  68. return 1;
  69. }
  70. static void ui_free_method_data(void *parent, void *ptr, CRYPTO_EX_DATA *ad,
  71. int idx, long argl, void *argp)
  72. {
  73. OPENSSL_free(ptr);
  74. }
  75. static CRYPTO_ONCE get_index_once = CRYPTO_ONCE_STATIC_INIT;
  76. static int ui_method_data_index = -1;
  77. DEFINE_RUN_ONCE_STATIC(ui_method_data_index_init)
  78. {
  79. ui_method_data_index = CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_UI_METHOD,
  80. 0, NULL, ui_new_method_data,
  81. ui_dup_method_data,
  82. ui_free_method_data);
  83. return 1;
  84. }
  85. static int ui_open(UI *ui)
  86. {
  87. return 1;
  88. }
  89. static int ui_read(UI *ui, UI_STRING *uis)
  90. {
  91. switch (UI_get_string_type(uis)) {
  92. case UIT_PROMPT:
  93. {
  94. char result[PEM_BUFSIZE + 1];
  95. const struct pem_password_cb_data *data =
  96. UI_method_get_ex_data(UI_get_method(ui), ui_method_data_index);
  97. int maxsize = UI_get_result_maxsize(uis);
  98. int len = data->cb(result,
  99. maxsize > PEM_BUFSIZE ? PEM_BUFSIZE : maxsize,
  100. data->rwflag, UI_get0_user_data(ui));
  101. if (len >= 0)
  102. result[len] = '\0';
  103. if (len <= 0)
  104. return len;
  105. if (UI_set_result_ex(ui, uis, result, len) >= 0)
  106. return 1;
  107. return 0;
  108. }
  109. case UIT_VERIFY:
  110. case UIT_NONE:
  111. case UIT_BOOLEAN:
  112. case UIT_INFO:
  113. case UIT_ERROR:
  114. break;
  115. }
  116. return 1;
  117. }
  118. static int ui_write(UI *ui, UI_STRING *uis)
  119. {
  120. return 1;
  121. }
  122. static int ui_close(UI *ui)
  123. {
  124. return 1;
  125. }
  126. UI_METHOD *UI_UTIL_wrap_read_pem_callback(pem_password_cb *cb, int rwflag)
  127. {
  128. struct pem_password_cb_data *data = NULL;
  129. UI_METHOD *ui_method = NULL;
  130. if ((data = OPENSSL_zalloc(sizeof(*data))) == NULL
  131. || (ui_method = UI_create_method("PEM password callback wrapper")) == NULL
  132. || UI_method_set_opener(ui_method, ui_open) < 0
  133. || UI_method_set_reader(ui_method, ui_read) < 0
  134. || UI_method_set_writer(ui_method, ui_write) < 0
  135. || UI_method_set_closer(ui_method, ui_close) < 0
  136. || !RUN_ONCE(&get_index_once, ui_method_data_index_init)
  137. || UI_method_set_ex_data(ui_method, ui_method_data_index, data) < 0) {
  138. UI_destroy_method(ui_method);
  139. OPENSSL_free(data);
  140. return NULL;
  141. }
  142. data->rwflag = rwflag;
  143. data->cb = cb != NULL ? cb : PEM_def_callback;
  144. return ui_method;
  145. }