]>
Commit | Line | Data |
---|---|---|
aa6bb135 | 1 | /* |
fecb3aae | 2 | * Copyright 2002-2022 The OpenSSL Project Authors. All Rights Reserved. |
44bdb056 | 3 | * |
55e0593c | 4 | * Licensed under the Apache License 2.0 (the "License"). You may not use |
aa6bb135 RS |
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 | |
44bdb056 RL |
8 | */ |
9 | ||
10 | #include <string.h> | |
72a5412b | 11 | #include <openssl/pem.h> /* PEM_def_callback() */ |
7eb26c49 | 12 | #include "internal/thread_once.h" |
706457b7 | 13 | #include "ui_local.h" |
44bdb056 | 14 | |
984d6c60 DW |
15 | #ifndef BUFSIZ |
16 | #define BUFSIZ 256 | |
17 | #endif | |
18 | ||
0f113f3e MC |
19 | int UI_UTIL_read_pw_string(char *buf, int length, const char *prompt, |
20 | int verify) | |
21 | { | |
22 | char buff[BUFSIZ]; | |
23 | int ret; | |
44bdb056 | 24 | |
0f113f3e MC |
25 | ret = |
26 | UI_UTIL_read_pw(buf, buff, (length > BUFSIZ) ? BUFSIZ : length, | |
27 | prompt, verify); | |
28 | OPENSSL_cleanse(buff, BUFSIZ); | |
26a7d938 | 29 | return ret; |
0f113f3e | 30 | } |
44bdb056 | 31 | |
0f113f3e MC |
32 | int UI_UTIL_read_pw(char *buf, char *buff, int size, const char *prompt, |
33 | int verify) | |
34 | { | |
a64c48cf | 35 | int ok = -2; |
0f113f3e | 36 | UI *ui; |
44bdb056 | 37 | |
0f113f3e MC |
38 | if (size < 1) |
39 | return -1; | |
b57c98df | 40 | |
0f113f3e | 41 | ui = UI_new(); |
90945fa3 | 42 | if (ui != NULL) { |
0f113f3e MC |
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 | } | |
26a7d938 | 50 | return ok; |
0f113f3e | 51 | } |
0fe1fc85 RL |
52 | |
53 | /* | |
54 | * Wrapper around pem_password_cb, a method to help older APIs use newer | |
55 | * ones. | |
56 | */ | |
57 | struct pem_password_cb_data { | |
58 | pem_password_cb *cb; | |
59 | int rwflag; | |
60 | }; | |
61 | ||
62 | static 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 | ||
71 | static int ui_dup_method_data(CRYPTO_EX_DATA *to, const CRYPTO_EX_DATA *from, | |
712e8deb | 72 | void **pptr, int idx, long argl, void *argp) |
0fe1fc85 | 73 | { |
3f6a12a0 | 74 | if (*pptr != NULL) { |
0fe1fc85 | 75 | *pptr = OPENSSL_memdup(*pptr, sizeof(struct pem_password_cb_data)); |
3f6a12a0 JJ |
76 | if (*pptr != NULL) |
77 | return 1; | |
78 | } | |
79 | return 0; | |
0fe1fc85 RL |
80 | } |
81 | ||
82 | static void ui_free_method_data(void *parent, void *ptr, CRYPTO_EX_DATA *ad, | |
83 | int idx, long argl, void *argp) | |
84 | { | |
85 | OPENSSL_free(ptr); | |
86 | } | |
87 | ||
7eb26c49 | 88 | static CRYPTO_ONCE get_index_once = CRYPTO_ONCE_STATIC_INIT; |
37cbabbd RL |
89 | static int ui_method_data_index = -1; |
90 | DEFINE_RUN_ONCE_STATIC(ui_method_data_index_init) | |
0fe1fc85 | 91 | { |
37cbabbd RL |
92 | ui_method_data_index = CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_UI_METHOD, |
93 | 0, NULL, ui_new_method_data, | |
94 | ui_dup_method_data, | |
95 | ui_free_method_data); | |
96 | return 1; | |
0fe1fc85 RL |
97 | } |
98 | ||
99 | static int ui_open(UI *ui) | |
100 | { | |
101 | return 1; | |
102 | } | |
103 | static int ui_read(UI *ui, UI_STRING *uis) | |
104 | { | |
105 | switch (UI_get_string_type(uis)) { | |
106 | case UIT_PROMPT: | |
107 | { | |
3816be5d | 108 | char result[PEM_BUFSIZE + 1]; |
0fe1fc85 | 109 | const struct pem_password_cb_data *data = |
37cbabbd | 110 | UI_method_get_ex_data(UI_get_method(ui), ui_method_data_index); |
0fe1fc85 RL |
111 | int maxsize = UI_get_result_maxsize(uis); |
112 | int len = data->cb(result, | |
113 | maxsize > PEM_BUFSIZE ? PEM_BUFSIZE : maxsize, | |
114 | data->rwflag, UI_get0_user_data(ui)); | |
115 | ||
3816be5d RL |
116 | if (len >= 0) |
117 | result[len] = '\0'; | |
59ccb72c | 118 | if (len < 0) |
0fe1fc85 | 119 | return len; |
4e049e2c | 120 | if (UI_set_result_ex(ui, uis, result, len) >= 0) |
0fe1fc85 RL |
121 | return 1; |
122 | return 0; | |
123 | } | |
124 | case UIT_VERIFY: | |
125 | case UIT_NONE: | |
126 | case UIT_BOOLEAN: | |
127 | case UIT_INFO: | |
128 | case UIT_ERROR: | |
129 | break; | |
130 | } | |
131 | return 1; | |
132 | } | |
133 | static int ui_write(UI *ui, UI_STRING *uis) | |
134 | { | |
135 | return 1; | |
136 | } | |
137 | static int ui_close(UI *ui) | |
138 | { | |
139 | return 1; | |
140 | } | |
141 | ||
142 | UI_METHOD *UI_UTIL_wrap_read_pem_callback(pem_password_cb *cb, int rwflag) | |
143 | { | |
144 | struct pem_password_cb_data *data = NULL; | |
145 | UI_METHOD *ui_method = NULL; | |
146 | ||
147 | if ((data = OPENSSL_zalloc(sizeof(*data))) == NULL | |
148 | || (ui_method = UI_create_method("PEM password callback wrapper")) == NULL | |
149 | || UI_method_set_opener(ui_method, ui_open) < 0 | |
150 | || UI_method_set_reader(ui_method, ui_read) < 0 | |
151 | || UI_method_set_writer(ui_method, ui_write) < 0 | |
152 | || UI_method_set_closer(ui_method, ui_close) < 0 | |
37cbabbd | 153 | || !RUN_ONCE(&get_index_once, ui_method_data_index_init) |
1aef2c10 | 154 | || !UI_method_set_ex_data(ui_method, ui_method_data_index, data)) { |
0fe1fc85 RL |
155 | UI_destroy_method(ui_method); |
156 | OPENSSL_free(data); | |
157 | return NULL; | |
158 | } | |
159 | data->rwflag = rwflag; | |
72a5412b | 160 | data->cb = cb != NULL ? cb : PEM_def_callback; |
0fe1fc85 RL |
161 | |
162 | return ui_method; | |
163 | } |