passphrase.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. /*
  2. * Copyright 2020-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 <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. passphrase_len != 0 ? OPENSSL_memdup(passphrase, passphrase_len)
  41. : OPENSSL_malloc(1);
  42. if (data->_.expl_passphrase.passphrase_copy == NULL)
  43. return 0;
  44. data->_.expl_passphrase.passphrase_len = passphrase_len;
  45. return 1;
  46. }
  47. int ossl_pw_set_pem_password_cb(struct ossl_passphrase_data_st *data,
  48. pem_password_cb *cb, void *cbarg)
  49. {
  50. if (!ossl_assert(data != NULL && cb != NULL)) {
  51. ERR_raise(ERR_LIB_CRYPTO, ERR_R_PASSED_NULL_PARAMETER);
  52. return 0;
  53. }
  54. ossl_pw_clear_passphrase_data(data);
  55. data->type = is_pem_password;
  56. data->_.pem_password.password_cb = cb;
  57. data->_.pem_password.password_cbarg = cbarg;
  58. return 1;
  59. }
  60. int ossl_pw_set_ossl_passphrase_cb(struct ossl_passphrase_data_st *data,
  61. OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
  62. {
  63. if (!ossl_assert(data != NULL && cb != NULL)) {
  64. ERR_raise(ERR_LIB_CRYPTO, ERR_R_PASSED_NULL_PARAMETER);
  65. return 0;
  66. }
  67. ossl_pw_clear_passphrase_data(data);
  68. data->type = is_ossl_passphrase;
  69. data->_.ossl_passphrase.passphrase_cb = cb;
  70. data->_.ossl_passphrase.passphrase_cbarg = cbarg;
  71. return 1;
  72. }
  73. int ossl_pw_set_ui_method(struct ossl_passphrase_data_st *data,
  74. const UI_METHOD *ui_method, void *ui_data)
  75. {
  76. if (!ossl_assert(data != NULL && ui_method != NULL)) {
  77. ERR_raise(ERR_LIB_CRYPTO, ERR_R_PASSED_NULL_PARAMETER);
  78. return 0;
  79. }
  80. ossl_pw_clear_passphrase_data(data);
  81. data->type = is_ui_method;
  82. data->_.ui_method.ui_method = ui_method;
  83. data->_.ui_method.ui_method_data = ui_data;
  84. return 1;
  85. }
  86. int ossl_pw_enable_passphrase_caching(struct ossl_passphrase_data_st *data)
  87. {
  88. data->flag_cache_passphrase = 1;
  89. return 1;
  90. }
  91. int ossl_pw_disable_passphrase_caching(struct ossl_passphrase_data_st *data)
  92. {
  93. data->flag_cache_passphrase = 0;
  94. return 1;
  95. }
  96. /*-
  97. * UI_METHOD processor. It differs from UI_UTIL_read_pw() like this:
  98. *
  99. * 1. It constructs a prompt on its own, based on |prompt_info|.
  100. * 2. It allocates a buffer for password and verification on its own
  101. * to compensate for NUL terminator in UI password strings.
  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, *ipass = NULL, *vpass = NULL;
  110. int prompt_idx = -1, verify_idx = -1, res;
  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_UI_LIB);
  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_UI_LIB);
  130. goto end;
  131. }
  132. /* Get a buffer for verification prompt */
  133. ipass = OPENSSL_zalloc(pass_size + 1);
  134. if (ipass == NULL)
  135. goto end;
  136. prompt_idx = UI_add_input_string(ui, prompt,
  137. UI_INPUT_FLAG_DEFAULT_PWD,
  138. ipass, 0, pass_size) - 1;
  139. if (prompt_idx < 0) {
  140. ERR_raise(ERR_LIB_CRYPTO, ERR_R_UI_LIB);
  141. goto end;
  142. }
  143. if (verify) {
  144. /* Get a buffer for verification prompt */
  145. vpass = OPENSSL_zalloc(pass_size + 1);
  146. if (vpass == NULL)
  147. goto end;
  148. verify_idx = UI_add_verify_string(ui, prompt,
  149. UI_INPUT_FLAG_DEFAULT_PWD,
  150. vpass, 0, pass_size,
  151. ipass) - 1;
  152. if (verify_idx < 0) {
  153. ERR_raise(ERR_LIB_CRYPTO, ERR_R_UI_LIB);
  154. goto end;
  155. }
  156. }
  157. switch (UI_process(ui)) {
  158. case -2:
  159. ERR_raise(ERR_LIB_CRYPTO, ERR_R_INTERRUPTED_OR_CANCELLED);
  160. break;
  161. case -1:
  162. ERR_raise(ERR_LIB_CRYPTO, ERR_R_UI_LIB);
  163. break;
  164. default:
  165. res = UI_get_result_length(ui, prompt_idx);
  166. if (res < 0) {
  167. ERR_raise(ERR_LIB_CRYPTO, ERR_R_UI_LIB);
  168. break;
  169. }
  170. *pass_len = (size_t)res;
  171. memcpy(pass, ipass, *pass_len);
  172. ret = 1;
  173. break;
  174. }
  175. end:
  176. OPENSSL_clear_free(vpass, pass_size + 1);
  177. OPENSSL_clear_free(ipass, pass_size + 1);
  178. OPENSSL_free(prompt);
  179. UI_free(ui);
  180. return ret;
  181. }
  182. /* Central pw prompting dispatcher */
  183. int ossl_pw_get_passphrase(char *pass, size_t pass_size, size_t *pass_len,
  184. const OSSL_PARAM params[], int verify,
  185. struct ossl_passphrase_data_st *data)
  186. {
  187. const char *source = NULL;
  188. size_t source_len = 0;
  189. const char *prompt_info = NULL;
  190. const UI_METHOD *ui_method = NULL;
  191. UI_METHOD *allocated_ui_method = NULL;
  192. void *ui_data = NULL;
  193. const OSSL_PARAM *p = NULL;
  194. int ret;
  195. /* Handle explicit and cached passphrases */
  196. if (data->type == is_expl_passphrase) {
  197. source = data->_.expl_passphrase.passphrase_copy;
  198. source_len = data->_.expl_passphrase.passphrase_len;
  199. } else if (data->flag_cache_passphrase && data->cached_passphrase != NULL) {
  200. source = data->cached_passphrase;
  201. source_len = data->cached_passphrase_len;
  202. }
  203. if (source != NULL) {
  204. if (source_len > pass_size)
  205. source_len = pass_size;
  206. memcpy(pass, source, source_len);
  207. *pass_len = source_len;
  208. return 1;
  209. }
  210. /* Handle the is_ossl_passphrase case... that's pretty direct */
  211. if (data->type == is_ossl_passphrase) {
  212. OSSL_PASSPHRASE_CALLBACK *cb = data->_.ossl_passphrase.passphrase_cb;
  213. void *cbarg = data->_.ossl_passphrase.passphrase_cbarg;
  214. ret = cb(pass, pass_size, pass_len, params, cbarg);
  215. goto do_cache;
  216. }
  217. /* Handle the is_pem_password and is_ui_method cases */
  218. if ((p = OSSL_PARAM_locate_const(params,
  219. OSSL_PASSPHRASE_PARAM_INFO)) != NULL) {
  220. if (p->data_type != OSSL_PARAM_UTF8_STRING) {
  221. ERR_raise_data(ERR_LIB_CRYPTO, ERR_R_PASSED_INVALID_ARGUMENT,
  222. "Prompt info data type incorrect");
  223. return 0;
  224. }
  225. prompt_info = p->data;
  226. }
  227. if (data->type == is_pem_password) {
  228. /* We use a UI wrapper for PEM */
  229. pem_password_cb *cb = data->_.pem_password.password_cb;
  230. ui_method = allocated_ui_method =
  231. UI_UTIL_wrap_read_pem_callback(cb, verify);
  232. ui_data = data->_.pem_password.password_cbarg;
  233. if (ui_method == NULL) {
  234. ERR_raise(ERR_LIB_CRYPTO, ERR_R_UI_LIB);
  235. return 0;
  236. }
  237. } else if (data->type == is_ui_method) {
  238. ui_method = data->_.ui_method.ui_method;
  239. ui_data = data->_.ui_method.ui_method_data;
  240. }
  241. if (ui_method == NULL) {
  242. ERR_raise_data(ERR_LIB_CRYPTO, ERR_R_PASSED_INVALID_ARGUMENT,
  243. "No password method specified");
  244. return 0;
  245. }
  246. ret = do_ui_passphrase(pass, pass_size, pass_len, prompt_info, verify,
  247. ui_method, ui_data);
  248. UI_destroy_method(allocated_ui_method);
  249. do_cache:
  250. if (ret && data->flag_cache_passphrase) {
  251. if (data->cached_passphrase == NULL
  252. || *pass_len > data->cached_passphrase_len) {
  253. void *new_cache =
  254. OPENSSL_clear_realloc(data->cached_passphrase,
  255. data->cached_passphrase_len,
  256. *pass_len + 1);
  257. if (new_cache == NULL) {
  258. OPENSSL_cleanse(pass, *pass_len);
  259. return 0;
  260. }
  261. data->cached_passphrase = new_cache;
  262. }
  263. memcpy(data->cached_passphrase, pass, *pass_len);
  264. data->cached_passphrase[*pass_len] = '\0';
  265. data->cached_passphrase_len = *pass_len;
  266. }
  267. return ret;
  268. }
  269. static int ossl_pw_get_password(char *buf, int size, int rwflag,
  270. void *userdata, const char *info)
  271. {
  272. size_t password_len = 0;
  273. OSSL_PARAM params[] = {
  274. OSSL_PARAM_utf8_string(OSSL_PASSPHRASE_PARAM_INFO, NULL, 0),
  275. OSSL_PARAM_END
  276. };
  277. params[0].data = (void *)info;
  278. if (ossl_pw_get_passphrase(buf, (size_t)size, &password_len, params,
  279. rwflag, userdata))
  280. return (int)password_len;
  281. return -1;
  282. }
  283. int ossl_pw_pem_password(char *buf, int size, int rwflag, void *userdata)
  284. {
  285. return ossl_pw_get_password(buf, size, rwflag, userdata, "PEM");
  286. }
  287. int ossl_pw_pvk_password(char *buf, int size, int rwflag, void *userdata)
  288. {
  289. return ossl_pw_get_password(buf, size, rwflag, userdata, "PVK");
  290. }
  291. int ossl_pw_passphrase_callback_enc(char *pass, size_t pass_size,
  292. size_t *pass_len,
  293. const OSSL_PARAM params[], void *arg)
  294. {
  295. return ossl_pw_get_passphrase(pass, pass_size, pass_len, params, 1, arg);
  296. }
  297. int ossl_pw_passphrase_callback_dec(char *pass, size_t pass_size,
  298. size_t *pass_len,
  299. const OSSL_PARAM params[], void *arg)
  300. {
  301. return ossl_pw_get_passphrase(pass, pass_size, pass_len, params, 0, arg);
  302. }