passphrase.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. /*
  2. * Copyright 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 <openssl/err.h>
  10. #include <openssl/ui.h>
  11. #include <openssl/core_names.h>
  12. #include "internal/cryptlib.h"
  13. #include "internal/passphrase.h"
  14. void ossl_pw_clear_passphrase_data(struct ossl_passphrase_data_st *data)
  15. {
  16. if (data != NULL) {
  17. if (data->type == is_expl_passphrase)
  18. OPENSSL_clear_free(data->_.expl_passphrase.passphrase_copy,
  19. data->_.expl_passphrase.passphrase_len);
  20. ossl_pw_clear_passphrase_cache(data);
  21. memset(data, 0, sizeof(*data));
  22. }
  23. }
  24. void ossl_pw_clear_passphrase_cache(struct ossl_passphrase_data_st *data)
  25. {
  26. OPENSSL_clear_free(data->cached_passphrase, data->cached_passphrase_len);
  27. data->cached_passphrase = NULL;
  28. }
  29. int ossl_pw_set_passphrase(struct ossl_passphrase_data_st *data,
  30. const unsigned char *passphrase,
  31. size_t passphrase_len)
  32. {
  33. if (!ossl_assert(data != NULL && passphrase != NULL)) {
  34. ERR_raise(ERR_LIB_CRYPTO, ERR_R_PASSED_NULL_PARAMETER);
  35. return 0;
  36. }
  37. ossl_pw_clear_passphrase_data(data);
  38. data->type = is_expl_passphrase;
  39. data->_.expl_passphrase.passphrase_copy =
  40. OPENSSL_memdup(passphrase, passphrase_len);
  41. if (data->_.expl_passphrase.passphrase_copy == NULL) {
  42. ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
  43. return 0;
  44. }
  45. data->_.expl_passphrase.passphrase_len = passphrase_len;
  46. return 1;
  47. }
  48. int ossl_pw_set_pem_password_cb(struct ossl_passphrase_data_st *data,
  49. pem_password_cb *cb, void *cbarg)
  50. {
  51. if (!ossl_assert(data != NULL && cb != NULL)) {
  52. ERR_raise(ERR_LIB_CRYPTO, ERR_R_PASSED_NULL_PARAMETER);
  53. return 0;
  54. }
  55. ossl_pw_clear_passphrase_data(data);
  56. data->type = is_pem_password;
  57. data->_.pem_password.password_cb = cb;
  58. data->_.pem_password.password_cbarg = cbarg;
  59. return 1;
  60. }
  61. int ossl_pw_set_ossl_passphrase_cb(struct ossl_passphrase_data_st *data,
  62. OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
  63. {
  64. if (!ossl_assert(data != NULL && cb != NULL)) {
  65. ERR_raise(ERR_LIB_CRYPTO, ERR_R_PASSED_NULL_PARAMETER);
  66. return 0;
  67. }
  68. ossl_pw_clear_passphrase_data(data);
  69. data->type = is_ossl_passphrase;
  70. data->_.ossl_passphrase.passphrase_cb = cb;
  71. data->_.ossl_passphrase.passphrase_cbarg = cbarg;
  72. return 1;
  73. }
  74. int ossl_pw_set_ui_method(struct ossl_passphrase_data_st *data,
  75. const UI_METHOD *ui_method, void *ui_data)
  76. {
  77. if (!ossl_assert(data != NULL && ui_method != NULL)) {
  78. ERR_raise(ERR_LIB_CRYPTO, ERR_R_PASSED_NULL_PARAMETER);
  79. return 0;
  80. }
  81. ossl_pw_clear_passphrase_data(data);
  82. data->type = is_ui_method;
  83. data->_.ui_method.ui_method = ui_method;
  84. data->_.ui_method.ui_method_data = ui_data;
  85. return 1;
  86. }
  87. int ossl_pw_enable_passphrase_caching(struct ossl_passphrase_data_st *data)
  88. {
  89. data->flag_cache_passphrase = 1;
  90. return 1;
  91. }
  92. int ossl_pw_disable_passphrase_caching(struct ossl_passphrase_data_st *data)
  93. {
  94. data->flag_cache_passphrase = 0;
  95. return 1;
  96. }
  97. /*-
  98. * UI_METHOD processor. It differs from UI_UTIL_read_pw() like this:
  99. *
  100. * 1. It constructs a prompt on its own, based on |prompt_info|.
  101. * 2. It allocates a buffer for verification on its own.
  102. * 3. It raises errors.
  103. * 4. It reports back the length of the prompted pass phrase.
  104. */
  105. static int do_ui_passphrase(char *pass, size_t pass_size, size_t *pass_len,
  106. const char *prompt_info, int verify,
  107. const UI_METHOD *ui_method, void *ui_data)
  108. {
  109. char *prompt = NULL, *vpass = NULL;
  110. int prompt_idx = -1, verify_idx = -1;
  111. UI *ui = NULL;
  112. int ret = 0;
  113. if (!ossl_assert(pass != NULL && pass_size != 0 && pass_len != NULL)) {
  114. ERR_raise(ERR_LIB_CRYPTO, ERR_R_PASSED_NULL_PARAMETER);
  115. return 0;
  116. }
  117. if ((ui = UI_new()) == NULL) {
  118. ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
  119. return 0;
  120. }
  121. if (ui_method != NULL) {
  122. UI_set_method(ui, ui_method);
  123. if (ui_data != NULL)
  124. UI_add_user_data(ui, ui_data);
  125. }
  126. /* Get an application constructed prompt */
  127. prompt = UI_construct_prompt(ui, "pass phrase", prompt_info);
  128. if (prompt == NULL) {
  129. ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
  130. goto end;
  131. }
  132. prompt_idx = UI_add_input_string(ui, prompt,
  133. UI_INPUT_FLAG_DEFAULT_PWD,
  134. pass, 0, pass_size - 1) - 1;
  135. if (prompt_idx < 0) {
  136. ERR_raise(ERR_LIB_CRYPTO, ERR_R_UI_LIB);
  137. goto end;
  138. }
  139. if (verify) {
  140. /* Get a buffer for verification prompt */
  141. vpass = OPENSSL_zalloc(pass_size);
  142. if (vpass == NULL) {
  143. ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
  144. goto end;
  145. }
  146. verify_idx = UI_add_verify_string(ui, prompt,
  147. UI_INPUT_FLAG_DEFAULT_PWD,
  148. vpass, 0, pass_size - 1,
  149. pass) - 1;
  150. if (verify_idx < 0) {
  151. ERR_raise(ERR_LIB_CRYPTO, ERR_R_UI_LIB);
  152. goto end;
  153. }
  154. }
  155. switch (UI_process(ui)) {
  156. case -2:
  157. ERR_raise(ERR_LIB_CRYPTO, ERR_R_INTERRUPTED_OR_CANCELLED);
  158. break;
  159. case -1:
  160. ERR_raise(ERR_LIB_CRYPTO, ERR_R_UI_LIB);
  161. break;
  162. default:
  163. *pass_len = (size_t)UI_get_result_length(ui, prompt_idx);
  164. ret = 1;
  165. break;
  166. }
  167. end:
  168. OPENSSL_free(vpass);
  169. OPENSSL_free(prompt);
  170. UI_free(ui);
  171. return ret;
  172. }
  173. /* Central pw prompting dispatcher */
  174. int ossl_pw_get_passphrase(char *pass, size_t pass_size, size_t *pass_len,
  175. const OSSL_PARAM params[], int verify,
  176. struct ossl_passphrase_data_st *data)
  177. {
  178. const char *source = NULL;
  179. size_t source_len = 0;
  180. const char *prompt_info = NULL;
  181. const UI_METHOD *ui_method = NULL;
  182. UI_METHOD *allocated_ui_method = NULL;
  183. void *ui_data = NULL;
  184. const OSSL_PARAM *p = NULL;
  185. int ret;
  186. /* Handle explicit and cached passphrases */
  187. if (data->type == is_expl_passphrase) {
  188. source = data->_.expl_passphrase.passphrase_copy;
  189. source_len = data->_.expl_passphrase.passphrase_len;
  190. } else if (data->flag_cache_passphrase && data->cached_passphrase != NULL) {
  191. source = data->cached_passphrase;
  192. source_len = data->cached_passphrase_len;
  193. }
  194. if (source != NULL) {
  195. if (source_len > pass_size)
  196. source_len = pass_size;
  197. memcpy(pass, source, source_len);
  198. *pass_len = source_len;
  199. return 1;
  200. }
  201. /* Handle the is_ossl_passphrase case... that's pretty direct */
  202. if (data->type == is_ossl_passphrase) {
  203. OSSL_PASSPHRASE_CALLBACK *cb = data->_.ossl_passphrase.passphrase_cb;
  204. void *cbarg = data->_.ossl_passphrase.passphrase_cbarg;
  205. ret = cb(pass, pass_size, pass_len, params, cbarg);
  206. goto do_cache;
  207. }
  208. /* Handle the is_pem_password and is_ui_method cases */
  209. if ((p = OSSL_PARAM_locate_const(params,
  210. OSSL_PASSPHRASE_PARAM_INFO)) != NULL) {
  211. if (p->data_type != OSSL_PARAM_UTF8_STRING) {
  212. ERR_raise_data(ERR_LIB_CRYPTO, ERR_R_PASSED_INVALID_ARGUMENT,
  213. "Prompt info data type incorrect");
  214. return 0;
  215. }
  216. prompt_info = p->data;
  217. }
  218. if (data->type == is_pem_password) {
  219. /* We use a UI wrapper for PEM */
  220. pem_password_cb *cb = data->_.pem_password.password_cb;
  221. ui_method = allocated_ui_method =
  222. UI_UTIL_wrap_read_pem_callback(cb, verify);
  223. ui_data = data->_.pem_password.password_cbarg;
  224. if (ui_method == NULL) {
  225. ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
  226. return 0;
  227. }
  228. } else if (data->type == is_ui_method) {
  229. ui_method = data->_.ui_method.ui_method;
  230. ui_data = data->_.ui_method.ui_method_data;
  231. }
  232. if (ui_method == NULL) {
  233. ERR_raise(ERR_LIB_CRYPTO, ERR_R_INTERNAL_ERROR);
  234. return 0;
  235. }
  236. ret = do_ui_passphrase(pass, pass_size, pass_len, prompt_info, verify,
  237. ui_method, ui_data);
  238. UI_destroy_method(allocated_ui_method);
  239. do_cache:
  240. if (ret && data->flag_cache_passphrase) {
  241. if (data->cached_passphrase == NULL
  242. || *pass_len > data->cached_passphrase_len) {
  243. void *new_cache =
  244. OPENSSL_clear_realloc(data->cached_passphrase,
  245. data->cached_passphrase_len,
  246. *pass_len + 1);
  247. if (new_cache == NULL) {
  248. OPENSSL_cleanse(pass, *pass_len);
  249. ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
  250. return 0;
  251. }
  252. data->cached_passphrase = new_cache;
  253. }
  254. memcpy(data->cached_passphrase, pass, *pass_len);
  255. data->cached_passphrase[*pass_len] = '\0';
  256. data->cached_passphrase_len = *pass_len;
  257. }
  258. return ret;
  259. }
  260. int ossl_pw_pem_password(char *buf, int size, int rwflag, void *userdata)
  261. {
  262. size_t password_len = 0;
  263. OSSL_PARAM params[] = {
  264. OSSL_PARAM_utf8_string(OSSL_PASSPHRASE_PARAM_INFO, NULL, 0),
  265. OSSL_PARAM_END
  266. };
  267. params[0].data = "PEM";
  268. if (ossl_pw_get_passphrase(buf, (size_t)size, &password_len, params,
  269. rwflag, userdata))
  270. return (int)password_len;
  271. return -1;
  272. }
  273. int ossl_pw_passphrase_callback_enc(char *pass, size_t pass_size,
  274. size_t *pass_len,
  275. const OSSL_PARAM params[], void *arg)
  276. {
  277. return ossl_pw_get_passphrase(pass, pass_size, pass_len, params, 1, arg);
  278. }
  279. int ossl_pw_passphrase_callback_dec(char *pass, size_t pass_size,
  280. size_t *pass_len,
  281. const OSSL_PARAM params[], void *arg)
  282. {
  283. return ossl_pw_get_passphrase(pass, pass_size, pass_len, params, 0, arg);
  284. }