blob: 754c75671faf42e994cb7fac3bc6a5d98b730995 [file] [log] [blame]
xf.libfc6e712025-02-07 01:54:34 -08001/*
2 * Copyright 2002-2023 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
10#include <string.h>
11#include <openssl/pem.h> /* PEM_def_callback() */
12#include "internal/thread_once.h"
13#include "ui_local.h"
14
15#ifndef BUFSIZ
16#define BUFSIZ 256
17#endif
18
19int UI_UTIL_read_pw_string(char *buf, int length, const char *prompt,
20 int verify)
21{
22 char buff[BUFSIZ];
23 int ret;
24
25 ret =
26 UI_UTIL_read_pw(buf, buff, (length > BUFSIZ) ? BUFSIZ : length,
27 prompt, verify);
28 OPENSSL_cleanse(buff, BUFSIZ);
29 return ret;
30}
31
32int UI_UTIL_read_pw(char *buf, char *buff, int size, const char *prompt,
33 int verify)
34{
35 int ok = -2;
36 UI *ui;
37
38 if (size < 1)
39 return -1;
40
41 ui = UI_new();
42 if (ui != NULL) {
43 ok = UI_add_input_string(ui, prompt, 0, buf, 0, size - 1);
44 if (ok >= 0 && verify)
45 ok = UI_add_verify_string(ui, prompt, 0, buff, 0, size - 1, buf);
46 if (ok >= 0)
47 ok = UI_process(ui);
48 UI_free(ui);
49 }
50 return ok;
51}
52
53/*
54 * Wrapper around pem_password_cb, a method to help older APIs use newer
55 * ones.
56 */
57struct pem_password_cb_data {
58 pem_password_cb *cb;
59 int rwflag;
60};
61
62static void ui_new_method_data(void *parent, void *ptr, CRYPTO_EX_DATA *ad,
63 int idx, long argl, void *argp)
64{
65 /*
66 * Do nothing, the data is allocated externally and assigned later with
67 * CRYPTO_set_ex_data()
68 */
69}
70
71static int ui_dup_method_data(CRYPTO_EX_DATA *to, const CRYPTO_EX_DATA *from,
72 void *from_d, int idx, long argl, void *argp)
73{
74 void **pptr = (void **)from_d;
75 if (*pptr != NULL)
76 *pptr = OPENSSL_memdup(*pptr, sizeof(struct pem_password_cb_data));
77 return 1;
78}
79
80static void ui_free_method_data(void *parent, void *ptr, CRYPTO_EX_DATA *ad,
81 int idx, long argl, void *argp)
82{
83 OPENSSL_free(ptr);
84}
85
86static CRYPTO_ONCE get_index_once = CRYPTO_ONCE_STATIC_INIT;
87static int ui_method_data_index = -1;
88DEFINE_RUN_ONCE_STATIC(ui_method_data_index_init)
89{
90 ui_method_data_index = CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_UI_METHOD,
91 0, NULL, ui_new_method_data,
92 ui_dup_method_data,
93 ui_free_method_data);
94 return 1;
95}
96
97static int ui_open(UI *ui)
98{
99 return 1;
100}
101static int ui_read(UI *ui, UI_STRING *uis)
102{
103 switch (UI_get_string_type(uis)) {
104 case UIT_PROMPT:
105 {
106 char result[PEM_BUFSIZE + 1];
107 const struct pem_password_cb_data *data =
108 UI_method_get_ex_data(UI_get_method(ui), ui_method_data_index);
109 int maxsize = UI_get_result_maxsize(uis);
110 int len = data->cb(result,
111 maxsize > PEM_BUFSIZE ? PEM_BUFSIZE : maxsize,
112 data->rwflag, UI_get0_user_data(ui));
113
114 if (len >= 0)
115 result[len] = '\0';
116 if (len <= 0)
117 return len;
118 if (UI_set_result_ex(ui, uis, result, len) >= 0)
119 return 1;
120 return 0;
121 }
122 case UIT_VERIFY:
123 case UIT_NONE:
124 case UIT_BOOLEAN:
125 case UIT_INFO:
126 case UIT_ERROR:
127 break;
128 }
129 return 1;
130}
131static int ui_write(UI *ui, UI_STRING *uis)
132{
133 return 1;
134}
135static int ui_close(UI *ui)
136{
137 return 1;
138}
139
140UI_METHOD *UI_UTIL_wrap_read_pem_callback(pem_password_cb *cb, int rwflag)
141{
142 struct pem_password_cb_data *data = NULL;
143 UI_METHOD *ui_method = NULL;
144
145 if ((data = OPENSSL_zalloc(sizeof(*data))) == NULL
146 || (ui_method = UI_create_method("PEM password callback wrapper")) == NULL
147 || UI_method_set_opener(ui_method, ui_open) < 0
148 || UI_method_set_reader(ui_method, ui_read) < 0
149 || UI_method_set_writer(ui_method, ui_write) < 0
150 || UI_method_set_closer(ui_method, ui_close) < 0
151 || !RUN_ONCE(&get_index_once, ui_method_data_index_init)
152 || UI_method_set_ex_data(ui_method, ui_method_data_index, data) < 0) {
153 UI_destroy_method(ui_method);
154 OPENSSL_free(data);
155 return NULL;
156 }
157 data->rwflag = rwflag;
158 data->cb = cb != NULL ? cb : PEM_def_callback;
159
160 return ui_method;
161}