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