/src/openssl30/crypto/ui/ui_lib.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright 2001-2025 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 "internal/cryptlib.h" |
12 | | #include <openssl/e_os2.h> |
13 | | #include <openssl/buffer.h> |
14 | | #include <openssl/ui.h> |
15 | | #include <openssl/err.h> |
16 | | #include "ui_local.h" |
17 | | |
18 | | UI *UI_new(void) |
19 | 0 | { |
20 | 0 | return UI_new_method(NULL); |
21 | 0 | } |
22 | | |
23 | | UI *UI_new_method(const UI_METHOD *method) |
24 | 0 | { |
25 | 0 | UI *ret = OPENSSL_zalloc(sizeof(*ret)); |
26 | |
|
27 | 0 | if (ret == NULL) { |
28 | 0 | ERR_raise(ERR_LIB_UI, ERR_R_MALLOC_FAILURE); |
29 | 0 | return NULL; |
30 | 0 | } |
31 | | |
32 | 0 | ret->lock = CRYPTO_THREAD_lock_new(); |
33 | 0 | if (ret->lock == NULL) { |
34 | 0 | ERR_raise(ERR_LIB_UI, ERR_R_MALLOC_FAILURE); |
35 | 0 | OPENSSL_free(ret); |
36 | 0 | return NULL; |
37 | 0 | } |
38 | | |
39 | 0 | if (method == NULL) |
40 | 0 | method = UI_get_default_method(); |
41 | 0 | if (method == NULL) |
42 | 0 | method = UI_null(); |
43 | 0 | ret->meth = method; |
44 | |
|
45 | 0 | if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_UI, ret, &ret->ex_data)) { |
46 | 0 | UI_free(ret); |
47 | 0 | return NULL; |
48 | 0 | } |
49 | 0 | return ret; |
50 | 0 | } |
51 | | |
52 | | static void free_string(UI_STRING *uis) |
53 | 0 | { |
54 | 0 | if (uis->flags & OUT_STRING_FREEABLE) { |
55 | 0 | OPENSSL_free((char *)uis->out_string); |
56 | 0 | switch (uis->type) { |
57 | 0 | case UIT_BOOLEAN: |
58 | 0 | OPENSSL_free((char *)uis->_.boolean_data.action_desc); |
59 | 0 | OPENSSL_free((char *)uis->_.boolean_data.ok_chars); |
60 | 0 | OPENSSL_free((char *)uis->_.boolean_data.cancel_chars); |
61 | 0 | break; |
62 | 0 | case UIT_NONE: |
63 | 0 | case UIT_PROMPT: |
64 | 0 | case UIT_VERIFY: |
65 | 0 | case UIT_ERROR: |
66 | 0 | case UIT_INFO: |
67 | 0 | break; |
68 | 0 | } |
69 | 0 | } |
70 | 0 | OPENSSL_free(uis); |
71 | 0 | } |
72 | | |
73 | | void UI_free(UI *ui) |
74 | 0 | { |
75 | 0 | if (ui == NULL) |
76 | 0 | return; |
77 | 0 | if ((ui->flags & UI_FLAG_DUPL_DATA) != 0) { |
78 | 0 | ui->meth->ui_destroy_data(ui, ui->user_data); |
79 | 0 | } |
80 | 0 | sk_UI_STRING_pop_free(ui->strings, free_string); |
81 | 0 | CRYPTO_free_ex_data(CRYPTO_EX_INDEX_UI, ui, &ui->ex_data); |
82 | 0 | CRYPTO_THREAD_lock_free(ui->lock); |
83 | 0 | OPENSSL_free(ui); |
84 | 0 | } |
85 | | |
86 | | static int allocate_string_stack(UI *ui) |
87 | 0 | { |
88 | 0 | if (ui->strings == NULL) { |
89 | 0 | ui->strings = sk_UI_STRING_new_null(); |
90 | 0 | if (ui->strings == NULL) { |
91 | 0 | return -1; |
92 | 0 | } |
93 | 0 | } |
94 | 0 | return 0; |
95 | 0 | } |
96 | | |
97 | | static UI_STRING *general_allocate_prompt(UI *ui, const char *prompt, |
98 | | int prompt_freeable, |
99 | | enum UI_string_types type, |
100 | | int input_flags, char *result_buf) |
101 | 0 | { |
102 | 0 | UI_STRING *ret = NULL; |
103 | |
|
104 | 0 | if (prompt == NULL) { |
105 | 0 | ERR_raise(ERR_LIB_UI, ERR_R_PASSED_NULL_PARAMETER); |
106 | 0 | } else if ((type == UIT_PROMPT || type == UIT_VERIFY |
107 | 0 | || type == UIT_BOOLEAN) |
108 | 0 | && result_buf == NULL) { |
109 | 0 | ERR_raise(ERR_LIB_UI, UI_R_NO_RESULT_BUFFER); |
110 | 0 | } else if ((ret = OPENSSL_zalloc(sizeof(*ret))) != NULL) { |
111 | 0 | ret->out_string = prompt; |
112 | 0 | ret->flags = prompt_freeable ? OUT_STRING_FREEABLE : 0; |
113 | 0 | ret->input_flags = input_flags; |
114 | 0 | ret->type = type; |
115 | 0 | ret->result_buf = result_buf; |
116 | 0 | } |
117 | 0 | return ret; |
118 | 0 | } |
119 | | |
120 | | static int general_allocate_string(UI *ui, const char *prompt, |
121 | | int prompt_freeable, |
122 | | enum UI_string_types type, int input_flags, |
123 | | char *result_buf, int minsize, int maxsize, |
124 | | const char *test_buf) |
125 | 0 | { |
126 | 0 | int ret = -1; |
127 | 0 | UI_STRING *s = general_allocate_prompt(ui, prompt, prompt_freeable, |
128 | 0 | type, input_flags, result_buf); |
129 | |
|
130 | 0 | if (s != NULL) { |
131 | 0 | if (allocate_string_stack(ui) >= 0) { |
132 | 0 | s->_.string_data.result_minsize = minsize; |
133 | 0 | s->_.string_data.result_maxsize = maxsize; |
134 | 0 | s->_.string_data.test_buf = test_buf; |
135 | 0 | ret = sk_UI_STRING_push(ui->strings, s); |
136 | | /* sk_push() returns 0 on error. Let's adapt that */ |
137 | 0 | if (ret <= 0) { |
138 | 0 | ret--; |
139 | 0 | free_string(s); |
140 | 0 | } |
141 | 0 | } else |
142 | 0 | free_string(s); |
143 | 0 | } |
144 | 0 | return ret; |
145 | 0 | } |
146 | | |
147 | | static int general_allocate_boolean(UI *ui, |
148 | | const char *prompt, |
149 | | const char *action_desc, |
150 | | const char *ok_chars, |
151 | | const char *cancel_chars, |
152 | | int prompt_freeable, |
153 | | enum UI_string_types type, |
154 | | int input_flags, char *result_buf) |
155 | 0 | { |
156 | 0 | int ret = -1; |
157 | 0 | UI_STRING *s; |
158 | 0 | const char *p; |
159 | |
|
160 | 0 | if (ok_chars == NULL) { |
161 | 0 | ERR_raise(ERR_LIB_UI, ERR_R_PASSED_NULL_PARAMETER); |
162 | 0 | } else if (cancel_chars == NULL) { |
163 | 0 | ERR_raise(ERR_LIB_UI, ERR_R_PASSED_NULL_PARAMETER); |
164 | 0 | } else { |
165 | 0 | for (p = ok_chars; *p != '\0'; p++) { |
166 | 0 | if (strchr(cancel_chars, *p) != NULL) { |
167 | 0 | ERR_raise(ERR_LIB_UI, UI_R_COMMON_OK_AND_CANCEL_CHARACTERS); |
168 | 0 | } |
169 | 0 | } |
170 | |
|
171 | 0 | s = general_allocate_prompt(ui, prompt, prompt_freeable, |
172 | 0 | type, input_flags, result_buf); |
173 | |
|
174 | 0 | if (s != NULL) { |
175 | 0 | if (allocate_string_stack(ui) >= 0) { |
176 | 0 | s->_.boolean_data.action_desc = action_desc; |
177 | 0 | s->_.boolean_data.ok_chars = ok_chars; |
178 | 0 | s->_.boolean_data.cancel_chars = cancel_chars; |
179 | 0 | ret = sk_UI_STRING_push(ui->strings, s); |
180 | | /* |
181 | | * sk_push() returns 0 on error. Let's adapt that |
182 | | */ |
183 | 0 | if (ret <= 0) { |
184 | 0 | ret--; |
185 | 0 | free_string(s); |
186 | 0 | } |
187 | 0 | } else |
188 | 0 | free_string(s); |
189 | 0 | } |
190 | 0 | } |
191 | 0 | return ret; |
192 | 0 | } |
193 | | |
194 | | /* |
195 | | * Returns the index to the place in the stack or -1 for error. Uses a |
196 | | * direct reference to the prompt. |
197 | | */ |
198 | | int UI_add_input_string(UI *ui, const char *prompt, int flags, |
199 | | char *result_buf, int minsize, int maxsize) |
200 | 0 | { |
201 | 0 | return general_allocate_string(ui, prompt, 0, |
202 | 0 | UIT_PROMPT, flags, result_buf, minsize, |
203 | 0 | maxsize, NULL); |
204 | 0 | } |
205 | | |
206 | | /* Same as UI_add_input_string(), excepts it takes a copy of the prompt */ |
207 | | int UI_dup_input_string(UI *ui, const char *prompt, int flags, |
208 | | char *result_buf, int minsize, int maxsize) |
209 | 0 | { |
210 | 0 | char *prompt_copy = NULL; |
211 | 0 | int ret; |
212 | |
|
213 | 0 | if (prompt != NULL) { |
214 | 0 | prompt_copy = OPENSSL_strdup(prompt); |
215 | 0 | if (prompt_copy == NULL) { |
216 | 0 | ERR_raise(ERR_LIB_UI, ERR_R_MALLOC_FAILURE); |
217 | 0 | return 0; |
218 | 0 | } |
219 | 0 | } |
220 | | |
221 | 0 | ret = general_allocate_string(ui, prompt_copy, 1, |
222 | 0 | UIT_PROMPT, flags, result_buf, minsize, |
223 | 0 | maxsize, NULL); |
224 | 0 | if (ret <= 0) |
225 | 0 | OPENSSL_free(prompt_copy); |
226 | |
|
227 | 0 | return ret; |
228 | 0 | } |
229 | | |
230 | | int UI_add_verify_string(UI *ui, const char *prompt, int flags, |
231 | | char *result_buf, int minsize, int maxsize, |
232 | | const char *test_buf) |
233 | 0 | { |
234 | 0 | return general_allocate_string(ui, prompt, 0, |
235 | 0 | UIT_VERIFY, flags, result_buf, minsize, |
236 | 0 | maxsize, test_buf); |
237 | 0 | } |
238 | | |
239 | | int UI_dup_verify_string(UI *ui, const char *prompt, int flags, |
240 | | char *result_buf, int minsize, int maxsize, |
241 | | const char *test_buf) |
242 | 0 | { |
243 | 0 | char *prompt_copy = NULL; |
244 | 0 | int ret; |
245 | |
|
246 | 0 | if (prompt != NULL) { |
247 | 0 | prompt_copy = OPENSSL_strdup(prompt); |
248 | 0 | if (prompt_copy == NULL) { |
249 | 0 | ERR_raise(ERR_LIB_UI, ERR_R_MALLOC_FAILURE); |
250 | 0 | return -1; |
251 | 0 | } |
252 | 0 | } |
253 | | |
254 | 0 | ret = general_allocate_string(ui, prompt_copy, 1, |
255 | 0 | UIT_VERIFY, flags, result_buf, minsize, |
256 | 0 | maxsize, test_buf); |
257 | 0 | if (ret <= 0) |
258 | 0 | OPENSSL_free(prompt_copy); |
259 | 0 | return ret; |
260 | 0 | } |
261 | | |
262 | | int UI_add_input_boolean(UI *ui, const char *prompt, const char *action_desc, |
263 | | const char *ok_chars, const char *cancel_chars, |
264 | | int flags, char *result_buf) |
265 | 0 | { |
266 | 0 | return general_allocate_boolean(ui, prompt, action_desc, |
267 | 0 | ok_chars, cancel_chars, 0, UIT_BOOLEAN, |
268 | 0 | flags, result_buf); |
269 | 0 | } |
270 | | |
271 | | int UI_dup_input_boolean(UI *ui, const char *prompt, const char *action_desc, |
272 | | const char *ok_chars, const char *cancel_chars, |
273 | | int flags, char *result_buf) |
274 | 0 | { |
275 | 0 | char *prompt_copy = NULL; |
276 | 0 | char *action_desc_copy = NULL; |
277 | 0 | char *ok_chars_copy = NULL; |
278 | 0 | char *cancel_chars_copy = NULL; |
279 | 0 | int ret; |
280 | |
|
281 | 0 | if (prompt != NULL) { |
282 | 0 | prompt_copy = OPENSSL_strdup(prompt); |
283 | 0 | if (prompt_copy == NULL) { |
284 | 0 | ERR_raise(ERR_LIB_UI, ERR_R_MALLOC_FAILURE); |
285 | 0 | goto err; |
286 | 0 | } |
287 | 0 | } |
288 | | |
289 | 0 | if (action_desc != NULL) { |
290 | 0 | action_desc_copy = OPENSSL_strdup(action_desc); |
291 | 0 | if (action_desc_copy == NULL) { |
292 | 0 | ERR_raise(ERR_LIB_UI, ERR_R_MALLOC_FAILURE); |
293 | 0 | goto err; |
294 | 0 | } |
295 | 0 | } |
296 | | |
297 | 0 | if (ok_chars != NULL) { |
298 | 0 | ok_chars_copy = OPENSSL_strdup(ok_chars); |
299 | 0 | if (ok_chars_copy == NULL) { |
300 | 0 | ERR_raise(ERR_LIB_UI, ERR_R_MALLOC_FAILURE); |
301 | 0 | goto err; |
302 | 0 | } |
303 | 0 | } |
304 | | |
305 | 0 | if (cancel_chars != NULL) { |
306 | 0 | cancel_chars_copy = OPENSSL_strdup(cancel_chars); |
307 | 0 | if (cancel_chars_copy == NULL) { |
308 | 0 | ERR_raise(ERR_LIB_UI, ERR_R_MALLOC_FAILURE); |
309 | 0 | goto err; |
310 | 0 | } |
311 | 0 | } |
312 | | |
313 | 0 | ret = general_allocate_boolean(ui, prompt_copy, action_desc_copy, |
314 | 0 | ok_chars_copy, cancel_chars_copy, 1, |
315 | 0 | UIT_BOOLEAN, flags, result_buf); |
316 | 0 | if (ret <= 0) |
317 | 0 | goto err; |
318 | | |
319 | 0 | return ret; |
320 | | |
321 | 0 | err: |
322 | 0 | OPENSSL_free(prompt_copy); |
323 | 0 | OPENSSL_free(action_desc_copy); |
324 | 0 | OPENSSL_free(ok_chars_copy); |
325 | 0 | OPENSSL_free(cancel_chars_copy); |
326 | 0 | return -1; |
327 | 0 | } |
328 | | |
329 | | int UI_add_info_string(UI *ui, const char *text) |
330 | 0 | { |
331 | 0 | return general_allocate_string(ui, text, 0, UIT_INFO, 0, NULL, 0, 0, |
332 | 0 | NULL); |
333 | 0 | } |
334 | | |
335 | | int UI_dup_info_string(UI *ui, const char *text) |
336 | 0 | { |
337 | 0 | char *text_copy = NULL; |
338 | 0 | int ret; |
339 | |
|
340 | 0 | if (text != NULL) { |
341 | 0 | text_copy = OPENSSL_strdup(text); |
342 | 0 | if (text_copy == NULL) { |
343 | 0 | ERR_raise(ERR_LIB_UI, ERR_R_MALLOC_FAILURE); |
344 | 0 | return -1; |
345 | 0 | } |
346 | 0 | } |
347 | | |
348 | 0 | ret = general_allocate_string(ui, text_copy, 1, UIT_INFO, 0, NULL, |
349 | 0 | 0, 0, NULL); |
350 | 0 | if (ret <= 0) |
351 | 0 | OPENSSL_free(text_copy); |
352 | 0 | return ret; |
353 | 0 | } |
354 | | |
355 | | int UI_add_error_string(UI *ui, const char *text) |
356 | 0 | { |
357 | 0 | return general_allocate_string(ui, text, 0, UIT_ERROR, 0, NULL, 0, 0, |
358 | 0 | NULL); |
359 | 0 | } |
360 | | |
361 | | int UI_dup_error_string(UI *ui, const char *text) |
362 | 0 | { |
363 | 0 | char *text_copy = NULL; |
364 | 0 | int ret; |
365 | |
|
366 | 0 | if (text != NULL) { |
367 | 0 | text_copy = OPENSSL_strdup(text); |
368 | 0 | if (text_copy == NULL) { |
369 | 0 | ERR_raise(ERR_LIB_UI, ERR_R_MALLOC_FAILURE); |
370 | 0 | return -1; |
371 | 0 | } |
372 | 0 | } |
373 | | |
374 | 0 | ret = general_allocate_string(ui, text_copy, 1, UIT_ERROR, 0, NULL, |
375 | 0 | 0, 0, NULL); |
376 | 0 | if (ret <= 0) |
377 | 0 | OPENSSL_free(text_copy); |
378 | 0 | return ret; |
379 | 0 | } |
380 | | |
381 | | char *UI_construct_prompt(UI *ui, const char *phrase_desc, |
382 | | const char *object_name) |
383 | 0 | { |
384 | 0 | char *prompt = NULL; |
385 | |
|
386 | 0 | if (ui != NULL && ui->meth != NULL && ui->meth->ui_construct_prompt != NULL) |
387 | 0 | prompt = ui->meth->ui_construct_prompt(ui, phrase_desc, object_name); |
388 | 0 | else { |
389 | 0 | char prompt1[] = "Enter "; |
390 | 0 | char prompt2[] = " for "; |
391 | 0 | char prompt3[] = ":"; |
392 | 0 | int len = 0; |
393 | |
|
394 | 0 | if (phrase_desc == NULL) |
395 | 0 | return NULL; |
396 | 0 | len = sizeof(prompt1) - 1 + strlen(phrase_desc); |
397 | 0 | if (object_name != NULL) |
398 | 0 | len += sizeof(prompt2) - 1 + strlen(object_name); |
399 | 0 | len += sizeof(prompt3) - 1; |
400 | |
|
401 | 0 | if ((prompt = OPENSSL_malloc(len + 1)) == NULL) { |
402 | 0 | ERR_raise(ERR_LIB_UI, ERR_R_MALLOC_FAILURE); |
403 | 0 | return NULL; |
404 | 0 | } |
405 | 0 | OPENSSL_strlcpy(prompt, prompt1, len + 1); |
406 | 0 | OPENSSL_strlcat(prompt, phrase_desc, len + 1); |
407 | 0 | if (object_name != NULL) { |
408 | 0 | OPENSSL_strlcat(prompt, prompt2, len + 1); |
409 | 0 | OPENSSL_strlcat(prompt, object_name, len + 1); |
410 | 0 | } |
411 | 0 | OPENSSL_strlcat(prompt, prompt3, len + 1); |
412 | 0 | } |
413 | 0 | return prompt; |
414 | 0 | } |
415 | | |
416 | | void *UI_add_user_data(UI *ui, void *user_data) |
417 | 0 | { |
418 | 0 | void *old_data = ui->user_data; |
419 | |
|
420 | 0 | if ((ui->flags & UI_FLAG_DUPL_DATA) != 0) { |
421 | 0 | ui->meth->ui_destroy_data(ui, old_data); |
422 | 0 | old_data = NULL; |
423 | 0 | } |
424 | 0 | ui->user_data = user_data; |
425 | 0 | ui->flags &= ~UI_FLAG_DUPL_DATA; |
426 | 0 | return old_data; |
427 | 0 | } |
428 | | |
429 | | int UI_dup_user_data(UI *ui, void *user_data) |
430 | 0 | { |
431 | 0 | void *duplicate = NULL; |
432 | |
|
433 | 0 | if (ui->meth->ui_duplicate_data == NULL |
434 | 0 | || ui->meth->ui_destroy_data == NULL) { |
435 | 0 | ERR_raise(ERR_LIB_UI, UI_R_USER_DATA_DUPLICATION_UNSUPPORTED); |
436 | 0 | return -1; |
437 | 0 | } |
438 | | |
439 | 0 | duplicate = ui->meth->ui_duplicate_data(ui, user_data); |
440 | 0 | if (duplicate == NULL) { |
441 | 0 | ERR_raise(ERR_LIB_UI, ERR_R_MALLOC_FAILURE); |
442 | 0 | return -1; |
443 | 0 | } |
444 | | |
445 | 0 | (void)UI_add_user_data(ui, duplicate); |
446 | 0 | ui->flags |= UI_FLAG_DUPL_DATA; |
447 | |
|
448 | 0 | return 0; |
449 | 0 | } |
450 | | |
451 | | void *UI_get0_user_data(UI *ui) |
452 | 0 | { |
453 | 0 | return ui->user_data; |
454 | 0 | } |
455 | | |
456 | | const char *UI_get0_result(UI *ui, int i) |
457 | 0 | { |
458 | 0 | if (i < 0) { |
459 | 0 | ERR_raise(ERR_LIB_UI, UI_R_INDEX_TOO_SMALL); |
460 | 0 | return NULL; |
461 | 0 | } |
462 | 0 | if (i >= sk_UI_STRING_num(ui->strings)) { |
463 | 0 | ERR_raise(ERR_LIB_UI, UI_R_INDEX_TOO_LARGE); |
464 | 0 | return NULL; |
465 | 0 | } |
466 | 0 | return UI_get0_result_string(sk_UI_STRING_value(ui->strings, i)); |
467 | 0 | } |
468 | | |
469 | | int UI_get_result_length(UI *ui, int i) |
470 | 0 | { |
471 | 0 | if (i < 0) { |
472 | 0 | ERR_raise(ERR_LIB_UI, UI_R_INDEX_TOO_SMALL); |
473 | 0 | return -1; |
474 | 0 | } |
475 | 0 | if (i >= sk_UI_STRING_num(ui->strings)) { |
476 | 0 | ERR_raise(ERR_LIB_UI, UI_R_INDEX_TOO_LARGE); |
477 | 0 | return -1; |
478 | 0 | } |
479 | 0 | return UI_get_result_string_length(sk_UI_STRING_value(ui->strings, i)); |
480 | 0 | } |
481 | | |
482 | | static int print_error(const char *str, size_t len, UI *ui) |
483 | 0 | { |
484 | 0 | UI_STRING uis; |
485 | |
|
486 | 0 | memset(&uis, 0, sizeof(uis)); |
487 | 0 | uis.type = UIT_ERROR; |
488 | 0 | uis.out_string = str; |
489 | |
|
490 | 0 | if (ui->meth->ui_write_string != NULL |
491 | 0 | && ui->meth->ui_write_string(ui, &uis) <= 0) |
492 | 0 | return -1; |
493 | 0 | return 0; |
494 | 0 | } |
495 | | |
496 | | int UI_process(UI *ui) |
497 | 0 | { |
498 | 0 | int i, ok = 0; |
499 | 0 | const char *state = "processing"; |
500 | |
|
501 | 0 | if (ui->meth->ui_open_session != NULL |
502 | 0 | && ui->meth->ui_open_session(ui) <= 0) { |
503 | 0 | state = "opening session"; |
504 | 0 | ok = -1; |
505 | 0 | goto err; |
506 | 0 | } |
507 | | |
508 | 0 | if (ui->flags & UI_FLAG_PRINT_ERRORS) |
509 | 0 | ERR_print_errors_cb((int (*)(const char *, size_t, void *)) |
510 | 0 | print_error, |
511 | 0 | (void *)ui); |
512 | |
|
513 | 0 | for (i = 0; i < sk_UI_STRING_num(ui->strings); i++) { |
514 | 0 | if (ui->meth->ui_write_string != NULL |
515 | 0 | && (ui->meth->ui_write_string(ui, |
516 | 0 | sk_UI_STRING_value(ui->strings, i)) |
517 | 0 | <= 0)) { |
518 | 0 | state = "writing strings"; |
519 | 0 | ok = -1; |
520 | 0 | goto err; |
521 | 0 | } |
522 | 0 | } |
523 | | |
524 | 0 | if (ui->meth->ui_flush != NULL) |
525 | 0 | switch (ui->meth->ui_flush(ui)) { |
526 | 0 | case -1: /* Interrupt/Cancel/something... */ |
527 | 0 | ui->flags &= ~UI_FLAG_REDOABLE; |
528 | 0 | ok = -2; |
529 | 0 | goto err; |
530 | 0 | case 0: /* Errors */ |
531 | 0 | state = "flushing"; |
532 | 0 | ok = -1; |
533 | 0 | goto err; |
534 | 0 | default: /* Success */ |
535 | 0 | ok = 0; |
536 | 0 | break; |
537 | 0 | } |
538 | | |
539 | 0 | for (i = 0; i < sk_UI_STRING_num(ui->strings); i++) { |
540 | 0 | if (ui->meth->ui_read_string != NULL) { |
541 | 0 | switch (ui->meth->ui_read_string(ui, |
542 | 0 | sk_UI_STRING_value(ui->strings, |
543 | 0 | i))) { |
544 | 0 | case -1: /* Interrupt/Cancel/something... */ |
545 | 0 | ui->flags &= ~UI_FLAG_REDOABLE; |
546 | 0 | ok = -2; |
547 | 0 | goto err; |
548 | 0 | case 0: /* Errors */ |
549 | 0 | state = "reading strings"; |
550 | 0 | ok = -1; |
551 | 0 | goto err; |
552 | 0 | default: /* Success */ |
553 | 0 | ok = 0; |
554 | 0 | break; |
555 | 0 | } |
556 | 0 | } else { |
557 | 0 | ui->flags &= ~UI_FLAG_REDOABLE; |
558 | 0 | ok = -2; |
559 | 0 | goto err; |
560 | 0 | } |
561 | 0 | } |
562 | | |
563 | 0 | state = NULL; |
564 | 0 | err: |
565 | 0 | if (ui->meth->ui_close_session != NULL |
566 | 0 | && ui->meth->ui_close_session(ui) <= 0) { |
567 | 0 | if (state == NULL) |
568 | 0 | state = "closing session"; |
569 | 0 | ok = -1; |
570 | 0 | } |
571 | |
|
572 | 0 | if (ok == -1) |
573 | 0 | ERR_raise_data(ERR_LIB_UI, UI_R_PROCESSING_ERROR, "while %s", state); |
574 | 0 | return ok; |
575 | 0 | } |
576 | | |
577 | | int UI_ctrl(UI *ui, int cmd, long i, void *p, void (*f)(void)) |
578 | 0 | { |
579 | 0 | if (ui == NULL) { |
580 | 0 | ERR_raise(ERR_LIB_UI, ERR_R_PASSED_NULL_PARAMETER); |
581 | 0 | return -1; |
582 | 0 | } |
583 | 0 | switch (cmd) { |
584 | 0 | case UI_CTRL_PRINT_ERRORS: { |
585 | 0 | int save_flag = !!(ui->flags & UI_FLAG_PRINT_ERRORS); |
586 | 0 | if (i) |
587 | 0 | ui->flags |= UI_FLAG_PRINT_ERRORS; |
588 | 0 | else |
589 | 0 | ui->flags &= ~UI_FLAG_PRINT_ERRORS; |
590 | 0 | return save_flag; |
591 | 0 | } |
592 | 0 | case UI_CTRL_IS_REDOABLE: |
593 | 0 | return !!(ui->flags & UI_FLAG_REDOABLE); |
594 | 0 | default: |
595 | 0 | break; |
596 | 0 | } |
597 | 0 | ERR_raise(ERR_LIB_UI, UI_R_UNKNOWN_CONTROL_COMMAND); |
598 | 0 | return -1; |
599 | 0 | } |
600 | | |
601 | | int UI_set_ex_data(UI *r, int idx, void *arg) |
602 | 0 | { |
603 | 0 | return CRYPTO_set_ex_data(&r->ex_data, idx, arg); |
604 | 0 | } |
605 | | |
606 | | void *UI_get_ex_data(const UI *r, int idx) |
607 | 0 | { |
608 | 0 | return CRYPTO_get_ex_data(&r->ex_data, idx); |
609 | 0 | } |
610 | | |
611 | | const UI_METHOD *UI_get_method(UI *ui) |
612 | 0 | { |
613 | 0 | return ui->meth; |
614 | 0 | } |
615 | | |
616 | | const UI_METHOD *UI_set_method(UI *ui, const UI_METHOD *meth) |
617 | 0 | { |
618 | 0 | ui->meth = meth; |
619 | 0 | return ui->meth; |
620 | 0 | } |
621 | | |
622 | | UI_METHOD *UI_create_method(const char *name) |
623 | 0 | { |
624 | 0 | UI_METHOD *ui_method = NULL; |
625 | |
|
626 | 0 | if ((ui_method = OPENSSL_zalloc(sizeof(*ui_method))) == NULL |
627 | 0 | || (ui_method->name = OPENSSL_strdup(name)) == NULL |
628 | 0 | || !CRYPTO_new_ex_data(CRYPTO_EX_INDEX_UI_METHOD, ui_method, |
629 | 0 | &ui_method->ex_data)) { |
630 | 0 | if (ui_method) |
631 | 0 | OPENSSL_free(ui_method->name); |
632 | 0 | OPENSSL_free(ui_method); |
633 | 0 | ERR_raise(ERR_LIB_UI, ERR_R_MALLOC_FAILURE); |
634 | 0 | return NULL; |
635 | 0 | } |
636 | 0 | return ui_method; |
637 | 0 | } |
638 | | |
639 | | /* |
640 | | * BIG FSCKING WARNING!!!! If you use this on a statically allocated method |
641 | | * (that is, it hasn't been allocated using UI_create_method(), you deserve |
642 | | * anything Murphy can throw at you and more! You have been warned. |
643 | | */ |
644 | | void UI_destroy_method(UI_METHOD *ui_method) |
645 | 0 | { |
646 | 0 | if (ui_method == NULL) |
647 | 0 | return; |
648 | 0 | CRYPTO_free_ex_data(CRYPTO_EX_INDEX_UI_METHOD, ui_method, |
649 | 0 | &ui_method->ex_data); |
650 | 0 | OPENSSL_free(ui_method->name); |
651 | 0 | ui_method->name = NULL; |
652 | 0 | OPENSSL_free(ui_method); |
653 | 0 | } |
654 | | |
655 | | int UI_method_set_opener(UI_METHOD *method, int (*opener)(UI *ui)) |
656 | 0 | { |
657 | 0 | if (method != NULL) { |
658 | 0 | method->ui_open_session = opener; |
659 | 0 | return 0; |
660 | 0 | } |
661 | 0 | return -1; |
662 | 0 | } |
663 | | |
664 | | int UI_method_set_writer(UI_METHOD *method, |
665 | | int (*writer)(UI *ui, UI_STRING *uis)) |
666 | 0 | { |
667 | 0 | if (method != NULL) { |
668 | 0 | method->ui_write_string = writer; |
669 | 0 | return 0; |
670 | 0 | } |
671 | 0 | return -1; |
672 | 0 | } |
673 | | |
674 | | int UI_method_set_flusher(UI_METHOD *method, int (*flusher)(UI *ui)) |
675 | 0 | { |
676 | 0 | if (method != NULL) { |
677 | 0 | method->ui_flush = flusher; |
678 | 0 | return 0; |
679 | 0 | } |
680 | 0 | return -1; |
681 | 0 | } |
682 | | |
683 | | int UI_method_set_reader(UI_METHOD *method, |
684 | | int (*reader)(UI *ui, UI_STRING *uis)) |
685 | 0 | { |
686 | 0 | if (method != NULL) { |
687 | 0 | method->ui_read_string = reader; |
688 | 0 | return 0; |
689 | 0 | } |
690 | 0 | return -1; |
691 | 0 | } |
692 | | |
693 | | int UI_method_set_closer(UI_METHOD *method, int (*closer)(UI *ui)) |
694 | 0 | { |
695 | 0 | if (method != NULL) { |
696 | 0 | method->ui_close_session = closer; |
697 | 0 | return 0; |
698 | 0 | } |
699 | 0 | return -1; |
700 | 0 | } |
701 | | |
702 | | int UI_method_set_data_duplicator(UI_METHOD *method, |
703 | | void *(*duplicator)(UI *ui, void *ui_data), |
704 | | void (*destructor)(UI *ui, void *ui_data)) |
705 | 0 | { |
706 | 0 | if (method != NULL) { |
707 | 0 | method->ui_duplicate_data = duplicator; |
708 | 0 | method->ui_destroy_data = destructor; |
709 | 0 | return 0; |
710 | 0 | } |
711 | 0 | return -1; |
712 | 0 | } |
713 | | |
714 | | int UI_method_set_prompt_constructor(UI_METHOD *method, |
715 | | char *(*prompt_constructor)(UI *ui, |
716 | | const char *, |
717 | | const char *)) |
718 | 0 | { |
719 | 0 | if (method != NULL) { |
720 | 0 | method->ui_construct_prompt = prompt_constructor; |
721 | 0 | return 0; |
722 | 0 | } |
723 | 0 | return -1; |
724 | 0 | } |
725 | | |
726 | | int UI_method_set_ex_data(UI_METHOD *method, int idx, void *data) |
727 | 0 | { |
728 | 0 | return CRYPTO_set_ex_data(&method->ex_data, idx, data); |
729 | 0 | } |
730 | | |
731 | | int (*UI_method_get_opener(const UI_METHOD *method))(UI *) |
732 | 0 | { |
733 | 0 | if (method != NULL) |
734 | 0 | return method->ui_open_session; |
735 | 0 | return NULL; |
736 | 0 | } |
737 | | |
738 | | int (*UI_method_get_writer(const UI_METHOD *method))(UI *, UI_STRING *) |
739 | 0 | { |
740 | 0 | if (method != NULL) |
741 | 0 | return method->ui_write_string; |
742 | 0 | return NULL; |
743 | 0 | } |
744 | | |
745 | | int (*UI_method_get_flusher(const UI_METHOD *method))(UI *) |
746 | 0 | { |
747 | 0 | if (method != NULL) |
748 | 0 | return method->ui_flush; |
749 | 0 | return NULL; |
750 | 0 | } |
751 | | |
752 | | int (*UI_method_get_reader(const UI_METHOD *method))(UI *, UI_STRING *) |
753 | 0 | { |
754 | 0 | if (method != NULL) |
755 | 0 | return method->ui_read_string; |
756 | 0 | return NULL; |
757 | 0 | } |
758 | | |
759 | | int (*UI_method_get_closer(const UI_METHOD *method))(UI *) |
760 | 0 | { |
761 | 0 | if (method != NULL) |
762 | 0 | return method->ui_close_session; |
763 | 0 | return NULL; |
764 | 0 | } |
765 | | |
766 | | char *(*UI_method_get_prompt_constructor(const UI_METHOD *method))(UI *, const char *, const char *) |
767 | 0 | { |
768 | 0 | if (method != NULL) |
769 | 0 | return method->ui_construct_prompt; |
770 | 0 | return NULL; |
771 | 0 | } |
772 | | |
773 | | void *(*UI_method_get_data_duplicator(const UI_METHOD *method))(UI *, void *) |
774 | 0 | { |
775 | 0 | if (method != NULL) |
776 | 0 | return method->ui_duplicate_data; |
777 | 0 | return NULL; |
778 | 0 | } |
779 | | |
780 | | void (*UI_method_get_data_destructor(const UI_METHOD *method))(UI *, void *) |
781 | 0 | { |
782 | 0 | if (method != NULL) |
783 | 0 | return method->ui_destroy_data; |
784 | 0 | return NULL; |
785 | 0 | } |
786 | | |
787 | | const void *UI_method_get_ex_data(const UI_METHOD *method, int idx) |
788 | 0 | { |
789 | 0 | return CRYPTO_get_ex_data(&method->ex_data, idx); |
790 | 0 | } |
791 | | |
792 | | enum UI_string_types UI_get_string_type(UI_STRING *uis) |
793 | 0 | { |
794 | 0 | return uis->type; |
795 | 0 | } |
796 | | |
797 | | int UI_get_input_flags(UI_STRING *uis) |
798 | 0 | { |
799 | 0 | return uis->input_flags; |
800 | 0 | } |
801 | | |
802 | | const char *UI_get0_output_string(UI_STRING *uis) |
803 | 0 | { |
804 | 0 | return uis->out_string; |
805 | 0 | } |
806 | | |
807 | | const char *UI_get0_action_string(UI_STRING *uis) |
808 | 0 | { |
809 | 0 | switch (uis->type) { |
810 | 0 | case UIT_BOOLEAN: |
811 | 0 | return uis->_.boolean_data.action_desc; |
812 | 0 | case UIT_PROMPT: |
813 | 0 | case UIT_NONE: |
814 | 0 | case UIT_VERIFY: |
815 | 0 | case UIT_INFO: |
816 | 0 | case UIT_ERROR: |
817 | 0 | break; |
818 | 0 | } |
819 | 0 | return NULL; |
820 | 0 | } |
821 | | |
822 | | const char *UI_get0_result_string(UI_STRING *uis) |
823 | 0 | { |
824 | 0 | switch (uis->type) { |
825 | 0 | case UIT_PROMPT: |
826 | 0 | case UIT_VERIFY: |
827 | 0 | return uis->result_buf; |
828 | 0 | case UIT_NONE: |
829 | 0 | case UIT_BOOLEAN: |
830 | 0 | case UIT_INFO: |
831 | 0 | case UIT_ERROR: |
832 | 0 | break; |
833 | 0 | } |
834 | 0 | return NULL; |
835 | 0 | } |
836 | | |
837 | | int UI_get_result_string_length(UI_STRING *uis) |
838 | 0 | { |
839 | 0 | switch (uis->type) { |
840 | 0 | case UIT_PROMPT: |
841 | 0 | case UIT_VERIFY: |
842 | 0 | return uis->result_len; |
843 | 0 | case UIT_NONE: |
844 | 0 | case UIT_BOOLEAN: |
845 | 0 | case UIT_INFO: |
846 | 0 | case UIT_ERROR: |
847 | 0 | break; |
848 | 0 | } |
849 | 0 | return -1; |
850 | 0 | } |
851 | | |
852 | | const char *UI_get0_test_string(UI_STRING *uis) |
853 | 0 | { |
854 | 0 | switch (uis->type) { |
855 | 0 | case UIT_VERIFY: |
856 | 0 | return uis->_.string_data.test_buf; |
857 | 0 | case UIT_NONE: |
858 | 0 | case UIT_BOOLEAN: |
859 | 0 | case UIT_INFO: |
860 | 0 | case UIT_ERROR: |
861 | 0 | case UIT_PROMPT: |
862 | 0 | break; |
863 | 0 | } |
864 | 0 | return NULL; |
865 | 0 | } |
866 | | |
867 | | int UI_get_result_minsize(UI_STRING *uis) |
868 | 0 | { |
869 | 0 | switch (uis->type) { |
870 | 0 | case UIT_PROMPT: |
871 | 0 | case UIT_VERIFY: |
872 | 0 | return uis->_.string_data.result_minsize; |
873 | 0 | case UIT_NONE: |
874 | 0 | case UIT_INFO: |
875 | 0 | case UIT_ERROR: |
876 | 0 | case UIT_BOOLEAN: |
877 | 0 | break; |
878 | 0 | } |
879 | 0 | return -1; |
880 | 0 | } |
881 | | |
882 | | int UI_get_result_maxsize(UI_STRING *uis) |
883 | 0 | { |
884 | 0 | switch (uis->type) { |
885 | 0 | case UIT_PROMPT: |
886 | 0 | case UIT_VERIFY: |
887 | 0 | return uis->_.string_data.result_maxsize; |
888 | 0 | case UIT_NONE: |
889 | 0 | case UIT_INFO: |
890 | 0 | case UIT_ERROR: |
891 | 0 | case UIT_BOOLEAN: |
892 | 0 | break; |
893 | 0 | } |
894 | 0 | return -1; |
895 | 0 | } |
896 | | |
897 | | int UI_set_result(UI *ui, UI_STRING *uis, const char *result) |
898 | 0 | { |
899 | 0 | return UI_set_result_ex(ui, uis, result, strlen(result)); |
900 | 0 | } |
901 | | |
902 | | int UI_set_result_ex(UI *ui, UI_STRING *uis, const char *result, int len) |
903 | 0 | { |
904 | 0 | ui->flags &= ~UI_FLAG_REDOABLE; |
905 | |
|
906 | 0 | switch (uis->type) { |
907 | 0 | case UIT_PROMPT: |
908 | 0 | case UIT_VERIFY: |
909 | 0 | if (len < uis->_.string_data.result_minsize) { |
910 | 0 | ui->flags |= UI_FLAG_REDOABLE; |
911 | 0 | ERR_raise_data(ERR_LIB_UI, UI_R_RESULT_TOO_SMALL, |
912 | 0 | "You must type in %d to %d characters", |
913 | 0 | uis->_.string_data.result_minsize, |
914 | 0 | uis->_.string_data.result_maxsize); |
915 | 0 | return -1; |
916 | 0 | } |
917 | 0 | if (len > uis->_.string_data.result_maxsize) { |
918 | 0 | ui->flags |= UI_FLAG_REDOABLE; |
919 | 0 | ERR_raise_data(ERR_LIB_UI, UI_R_RESULT_TOO_LARGE, |
920 | 0 | "You must type in %d to %d characters", |
921 | 0 | uis->_.string_data.result_minsize, |
922 | 0 | uis->_.string_data.result_maxsize); |
923 | 0 | return -1; |
924 | 0 | } |
925 | | |
926 | 0 | if (uis->result_buf == NULL) { |
927 | 0 | ERR_raise(ERR_LIB_UI, UI_R_NO_RESULT_BUFFER); |
928 | 0 | return -1; |
929 | 0 | } |
930 | | |
931 | 0 | memcpy(uis->result_buf, result, len); |
932 | 0 | if (len <= uis->_.string_data.result_maxsize) |
933 | 0 | uis->result_buf[len] = '\0'; |
934 | 0 | uis->result_len = len; |
935 | 0 | break; |
936 | 0 | case UIT_BOOLEAN: { |
937 | 0 | const char *p; |
938 | |
|
939 | 0 | if (uis->result_buf == NULL) { |
940 | 0 | ERR_raise(ERR_LIB_UI, UI_R_NO_RESULT_BUFFER); |
941 | 0 | return -1; |
942 | 0 | } |
943 | | |
944 | 0 | uis->result_buf[0] = '\0'; |
945 | 0 | for (p = result; *p; p++) { |
946 | 0 | if (strchr(uis->_.boolean_data.ok_chars, *p)) { |
947 | 0 | uis->result_buf[0] = uis->_.boolean_data.ok_chars[0]; |
948 | 0 | break; |
949 | 0 | } |
950 | 0 | if (strchr(uis->_.boolean_data.cancel_chars, *p)) { |
951 | 0 | uis->result_buf[0] = uis->_.boolean_data.cancel_chars[0]; |
952 | 0 | break; |
953 | 0 | } |
954 | 0 | } |
955 | 0 | } |
956 | 0 | case UIT_NONE: |
957 | 0 | case UIT_INFO: |
958 | 0 | case UIT_ERROR: |
959 | 0 | break; |
960 | 0 | } |
961 | 0 | return 0; |
962 | 0 | } |