ui_util.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /*
  2. * Copyright 2002-2016 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 <string.h>
  10. #include "internal/thread_once.h"
  11. #include "ui_locl.h"
  12. #ifndef BUFSIZ
  13. #define BUFSIZ 256
  14. #endif
  15. int UI_UTIL_read_pw_string(char *buf, int length, const char *prompt,
  16. int verify)
  17. {
  18. char buff[BUFSIZ];
  19. int ret;
  20. ret =
  21. UI_UTIL_read_pw(buf, buff, (length > BUFSIZ) ? BUFSIZ : length,
  22. prompt, verify);
  23. OPENSSL_cleanse(buff, BUFSIZ);
  24. return ret;
  25. }
  26. int UI_UTIL_read_pw(char *buf, char *buff, int size, const char *prompt,
  27. int verify)
  28. {
  29. int ok = 0;
  30. UI *ui;
  31. if (size < 1)
  32. return -1;
  33. ui = UI_new();
  34. if (ui != NULL) {
  35. ok = UI_add_input_string(ui, prompt, 0, buf, 0, size - 1);
  36. if (ok >= 0 && verify)
  37. ok = UI_add_verify_string(ui, prompt, 0, buff, 0, size - 1, buf);
  38. if (ok >= 0)
  39. ok = UI_process(ui);
  40. UI_free(ui);
  41. }
  42. if (ok > 0)
  43. ok = 0;
  44. return ok;
  45. }
  46. /*
  47. * Wrapper around pem_password_cb, a method to help older APIs use newer
  48. * ones.
  49. */
  50. struct pem_password_cb_data {
  51. pem_password_cb *cb;
  52. int rwflag;
  53. };
  54. static void ui_new_method_data(void *parent, void *ptr, CRYPTO_EX_DATA *ad,
  55. int idx, long argl, void *argp)
  56. {
  57. /*
  58. * Do nothing, the data is allocated externally and assigned later with
  59. * CRYPTO_set_ex_data()
  60. */
  61. }
  62. static int ui_dup_method_data(CRYPTO_EX_DATA *to, const CRYPTO_EX_DATA *from,
  63. void *from_d, int idx, long argl, void *argp)
  64. {
  65. void **pptr = (void **)from_d;
  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;
  144. return ui_method;
  145. }