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