ui_util.c 4.6 KB

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