2
0

ui_util.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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 <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 *from_d, int idx, long argl, void *argp)
  65. {
  66. void **pptr = (void **)from_d;
  67. if (*pptr != NULL)
  68. *pptr = OPENSSL_memdup(*pptr, sizeof(struct pem_password_cb_data));
  69. return 1;
  70. }
  71. static void ui_free_method_data(void *parent, void *ptr, CRYPTO_EX_DATA *ad,
  72. int idx, long argl, void *argp)
  73. {
  74. OPENSSL_free(ptr);
  75. }
  76. static CRYPTO_ONCE get_index_once = CRYPTO_ONCE_STATIC_INIT;
  77. static int ui_method_data_index = -1;
  78. DEFINE_RUN_ONCE_STATIC(ui_method_data_index_init)
  79. {
  80. ui_method_data_index = CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_UI_METHOD,
  81. 0, NULL, ui_new_method_data,
  82. ui_dup_method_data,
  83. ui_free_method_data);
  84. return 1;
  85. }
  86. static int ui_open(UI *ui)
  87. {
  88. return 1;
  89. }
  90. static int ui_read(UI *ui, UI_STRING *uis)
  91. {
  92. switch (UI_get_string_type(uis)) {
  93. case UIT_PROMPT:
  94. {
  95. char result[PEM_BUFSIZE + 1];
  96. const struct pem_password_cb_data *data =
  97. UI_method_get_ex_data(UI_get_method(ui), ui_method_data_index);
  98. int maxsize = UI_get_result_maxsize(uis);
  99. int len = data->cb(result,
  100. maxsize > PEM_BUFSIZE ? PEM_BUFSIZE : maxsize,
  101. data->rwflag, UI_get0_user_data(ui));
  102. if (len >= 0)
  103. result[len] = '\0';
  104. if (len <= 0)
  105. return len;
  106. if (UI_set_result_ex(ui, uis, result, len) >= 0)
  107. return 1;
  108. return 0;
  109. }
  110. case UIT_VERIFY:
  111. case UIT_NONE:
  112. case UIT_BOOLEAN:
  113. case UIT_INFO:
  114. case UIT_ERROR:
  115. break;
  116. }
  117. return 1;
  118. }
  119. static int ui_write(UI *ui, UI_STRING *uis)
  120. {
  121. return 1;
  122. }
  123. static int ui_close(UI *ui)
  124. {
  125. return 1;
  126. }
  127. UI_METHOD *UI_UTIL_wrap_read_pem_callback(pem_password_cb *cb, int rwflag)
  128. {
  129. struct pem_password_cb_data *data = NULL;
  130. UI_METHOD *ui_method = NULL;
  131. if ((data = OPENSSL_zalloc(sizeof(*data))) == NULL
  132. || (ui_method = UI_create_method("PEM password callback wrapper")) == NULL
  133. || UI_method_set_opener(ui_method, ui_open) < 0
  134. || UI_method_set_reader(ui_method, ui_read) < 0
  135. || UI_method_set_writer(ui_method, ui_write) < 0
  136. || UI_method_set_closer(ui_method, ui_close) < 0
  137. || !RUN_ONCE(&get_index_once, ui_method_data_index_init)
  138. || UI_method_set_ex_data(ui_method, ui_method_data_index, data) < 0) {
  139. UI_destroy_method(ui_method);
  140. OPENSSL_free(data);
  141. return NULL;
  142. }
  143. data->rwflag = rwflag;
  144. data->cb = cb != NULL ? cb : PEM_def_callback;
  145. return ui_method;
  146. }