diff options
Diffstat (limited to 'src/lib/libcrypto/ui/ui_lib.c')
| -rw-r--r-- | src/lib/libcrypto/ui/ui_lib.c | 95 |
1 files changed, 65 insertions, 30 deletions
diff --git a/src/lib/libcrypto/ui/ui_lib.c b/src/lib/libcrypto/ui/ui_lib.c index cc9de59c19..4562464cc7 100644 --- a/src/lib/libcrypto/ui/ui_lib.c +++ b/src/lib/libcrypto/ui/ui_lib.c | |||
| @@ -1,4 +1,4 @@ | |||
| 1 | /* $OpenBSD: ui_lib.c,v 1.52 2025/05/10 05:54:39 tb Exp $ */ | 1 | /* $OpenBSD: ui_lib.c,v 1.55 2026/07/25 07:33:43 tb Exp $ */ |
| 2 | /* Written by Richard Levitte (richard@levitte.org) for the OpenSSL | 2 | /* Written by Richard Levitte (richard@levitte.org) for the OpenSSL |
| 3 | * project 2001. | 3 | * project 2001. |
| 4 | */ | 4 | */ |
| @@ -68,6 +68,39 @@ | |||
| 68 | 68 | ||
| 69 | static const UI_METHOD *default_UI_meth = NULL; | 69 | static const UI_METHOD *default_UI_meth = NULL; |
| 70 | 70 | ||
| 71 | struct ui_string_st { | ||
| 72 | enum UI_string_types type; /* Input */ | ||
| 73 | const char *out_string; /* Input */ | ||
| 74 | int input_flags; /* Flags from the user */ | ||
| 75 | |||
| 76 | /* The following parameters are completely irrelevant for UIT_INFO, | ||
| 77 | and can therefore be set to 0 or NULL */ | ||
| 78 | char *result_buf; /* Input and Output: If not NULL, user-defined | ||
| 79 | with size in result_maxsize. Otherwise, it | ||
| 80 | may be allocated by the UI routine, meaning | ||
| 81 | result_minsize is going to be overwritten.*/ | ||
| 82 | union { | ||
| 83 | struct { | ||
| 84 | int result_minsize; /* Input: minimum required | ||
| 85 | size of the result. | ||
| 86 | */ | ||
| 87 | int result_maxsize; /* Input: maximum permitted | ||
| 88 | size of the result */ | ||
| 89 | |||
| 90 | const char *test_buf; /* Input: test string to verify | ||
| 91 | against */ | ||
| 92 | } string_data; | ||
| 93 | struct { | ||
| 94 | const char *action_desc; /* Input */ | ||
| 95 | const char *ok_chars; /* Input */ | ||
| 96 | const char *cancel_chars; /* Input */ | ||
| 97 | } boolean_data; | ||
| 98 | } u; | ||
| 99 | |||
| 100 | #define OUT_STRING_FREEABLE 0x01 | ||
| 101 | int flags; /* flags for internal use */ | ||
| 102 | }; | ||
| 103 | |||
| 71 | UI * | 104 | UI * |
| 72 | UI_new(void) | 105 | UI_new(void) |
| 73 | { | 106 | { |
| @@ -101,9 +134,9 @@ free_string(UI_STRING *uis) | |||
| 101 | free((char *) uis->out_string); | 134 | free((char *) uis->out_string); |
| 102 | switch (uis->type) { | 135 | switch (uis->type) { |
| 103 | case UIT_BOOLEAN: | 136 | case UIT_BOOLEAN: |
| 104 | free((char *)uis->_.boolean_data.action_desc); | 137 | free((char *)uis->u.boolean_data.action_desc); |
| 105 | free((char *)uis->_.boolean_data.ok_chars); | 138 | free((char *)uis->u.boolean_data.ok_chars); |
| 106 | free((char *)uis->_.boolean_data.cancel_chars); | 139 | free((char *)uis->u.boolean_data.cancel_chars); |
| 107 | break; | 140 | break; |
| 108 | default: | 141 | default: |
| 109 | break; | 142 | break; |
| @@ -186,9 +219,9 @@ general_allocate_string(UI *ui, const char *prompt, int dup_prompt, | |||
| 186 | if ((s = general_allocate_prompt(prompt, dup_prompt, type, input_flags, | 219 | if ((s = general_allocate_prompt(prompt, dup_prompt, type, input_flags, |
| 187 | result_buf)) == NULL) | 220 | result_buf)) == NULL) |
| 188 | goto err; | 221 | goto err; |
| 189 | s->_.string_data.result_minsize = minsize; | 222 | s->u.string_data.result_minsize = minsize; |
| 190 | s->_.string_data.result_maxsize = maxsize; | 223 | s->u.string_data.result_maxsize = maxsize; |
| 191 | s->_.string_data.test_buf = test_buf; | 224 | s->u.string_data.test_buf = test_buf; |
| 192 | 225 | ||
| 193 | if (allocate_string_stack(ui) < 0) | 226 | if (allocate_string_stack(ui) < 0) |
| 194 | goto err; | 227 | goto err; |
| @@ -225,25 +258,25 @@ general_allocate_boolean(UI *ui, const char *prompt, const char *action_desc, | |||
| 225 | 258 | ||
| 226 | if (dup_strings) { | 259 | if (dup_strings) { |
| 227 | if (action_desc != NULL) { | 260 | if (action_desc != NULL) { |
| 228 | if ((s->_.boolean_data.action_desc = | 261 | if ((s->u.boolean_data.action_desc = |
| 229 | strdup(action_desc)) == NULL) { | 262 | strdup(action_desc)) == NULL) { |
| 230 | UIerror(ERR_R_MALLOC_FAILURE); | 263 | UIerror(ERR_R_MALLOC_FAILURE); |
| 231 | goto err; | 264 | goto err; |
| 232 | } | 265 | } |
| 233 | } | 266 | } |
| 234 | if ((s->_.boolean_data.ok_chars = strdup(ok_chars)) == NULL) { | 267 | if ((s->u.boolean_data.ok_chars = strdup(ok_chars)) == NULL) { |
| 235 | UIerror(ERR_R_MALLOC_FAILURE); | 268 | UIerror(ERR_R_MALLOC_FAILURE); |
| 236 | goto err; | 269 | goto err; |
| 237 | } | 270 | } |
| 238 | if ((s->_.boolean_data.cancel_chars = strdup(cancel_chars)) == | 271 | if ((s->u.boolean_data.cancel_chars = strdup(cancel_chars)) == |
| 239 | NULL) { | 272 | NULL) { |
| 240 | UIerror(ERR_R_MALLOC_FAILURE); | 273 | UIerror(ERR_R_MALLOC_FAILURE); |
| 241 | goto err; | 274 | goto err; |
| 242 | } | 275 | } |
| 243 | } else { | 276 | } else { |
| 244 | s->_.boolean_data.action_desc = action_desc; | 277 | s->u.boolean_data.action_desc = action_desc; |
| 245 | s->_.boolean_data.ok_chars = ok_chars; | 278 | s->u.boolean_data.ok_chars = ok_chars; |
| 246 | s->_.boolean_data.cancel_chars = cancel_chars; | 279 | s->u.boolean_data.cancel_chars = cancel_chars; |
| 247 | } | 280 | } |
| 248 | 281 | ||
| 249 | if (allocate_string_stack(ui) < 0) | 282 | if (allocate_string_stack(ui) < 0) |
| @@ -428,8 +461,10 @@ UI_process(UI *ui) | |||
| 428 | { | 461 | { |
| 429 | int i, ok = 0; | 462 | int i, ok = 0; |
| 430 | 463 | ||
| 431 | if (ui->meth->ui_open_session && !ui->meth->ui_open_session(ui)) | 464 | if (ui->meth->ui_open_session && !ui->meth->ui_open_session(ui)) { |
| 432 | return -1; | 465 | ok = -1; |
| 466 | goto err; | ||
| 467 | } | ||
| 433 | 468 | ||
| 434 | if (ui->flags & UI_FLAG_PRINT_ERRORS) | 469 | if (ui->flags & UI_FLAG_PRINT_ERRORS) |
| 435 | ERR_print_errors_cb(print_error, ui); | 470 | ERR_print_errors_cb(print_error, ui); |
| @@ -772,7 +807,7 @@ UI_get0_action_string(UI_STRING *uis) | |||
| 772 | switch (uis->type) { | 807 | switch (uis->type) { |
| 773 | case UIT_PROMPT: | 808 | case UIT_PROMPT: |
| 774 | case UIT_BOOLEAN: | 809 | case UIT_BOOLEAN: |
| 775 | return uis->_.boolean_data.action_desc; | 810 | return uis->u.boolean_data.action_desc; |
| 776 | default: | 811 | default: |
| 777 | return NULL; | 812 | return NULL; |
| 778 | } | 813 | } |
| @@ -803,7 +838,7 @@ UI_get0_test_string(UI_STRING *uis) | |||
| 803 | 838 | ||
| 804 | switch (uis->type) { | 839 | switch (uis->type) { |
| 805 | case UIT_VERIFY: | 840 | case UIT_VERIFY: |
| 806 | return uis->_.string_data.test_buf; | 841 | return uis->u.string_data.test_buf; |
| 807 | default: | 842 | default: |
| 808 | return NULL; | 843 | return NULL; |
| 809 | } | 844 | } |
| @@ -819,7 +854,7 @@ UI_get_result_minsize(UI_STRING *uis) | |||
| 819 | switch (uis->type) { | 854 | switch (uis->type) { |
| 820 | case UIT_PROMPT: | 855 | case UIT_PROMPT: |
| 821 | case UIT_VERIFY: | 856 | case UIT_VERIFY: |
| 822 | return uis->_.string_data.result_minsize; | 857 | return uis->u.string_data.result_minsize; |
| 823 | default: | 858 | default: |
| 824 | return -1; | 859 | return -1; |
| 825 | } | 860 | } |
| @@ -835,7 +870,7 @@ UI_get_result_maxsize(UI_STRING *uis) | |||
| 835 | switch (uis->type) { | 870 | switch (uis->type) { |
| 836 | case UIT_PROMPT: | 871 | case UIT_PROMPT: |
| 837 | case UIT_VERIFY: | 872 | case UIT_VERIFY: |
| 838 | return uis->_.string_data.result_maxsize; | 873 | return uis->u.string_data.result_maxsize; |
| 839 | default: | 874 | default: |
| 840 | return -1; | 875 | return -1; |
| 841 | } | 876 | } |
| @@ -856,22 +891,22 @@ UI_set_result(UI *ui, UI_STRING *uis, const char *result) | |||
| 856 | switch (uis->type) { | 891 | switch (uis->type) { |
| 857 | case UIT_PROMPT: | 892 | case UIT_PROMPT: |
| 858 | case UIT_VERIFY: | 893 | case UIT_VERIFY: |
| 859 | if (l < uis->_.string_data.result_minsize) { | 894 | if (l < uis->u.string_data.result_minsize) { |
| 860 | ui->flags |= UI_FLAG_REDOABLE; | 895 | ui->flags |= UI_FLAG_REDOABLE; |
| 861 | UIerror(UI_R_RESULT_TOO_SMALL); | 896 | UIerror(UI_R_RESULT_TOO_SMALL); |
| 862 | ERR_asprintf_error_data | 897 | ERR_asprintf_error_data |
| 863 | ("You must type in %d to %d characters", | 898 | ("You must type in %d to %d characters", |
| 864 | uis->_.string_data.result_minsize, | 899 | uis->u.string_data.result_minsize, |
| 865 | uis->_.string_data.result_maxsize); | 900 | uis->u.string_data.result_maxsize); |
| 866 | return -1; | 901 | return -1; |
| 867 | } | 902 | } |
| 868 | if (l > uis->_.string_data.result_maxsize) { | 903 | if (l > uis->u.string_data.result_maxsize) { |
| 869 | ui->flags |= UI_FLAG_REDOABLE; | 904 | ui->flags |= UI_FLAG_REDOABLE; |
| 870 | UIerror(UI_R_RESULT_TOO_LARGE); | 905 | UIerror(UI_R_RESULT_TOO_LARGE); |
| 871 | ERR_asprintf_error_data | 906 | ERR_asprintf_error_data |
| 872 | ("You must type in %d to %d characters", | 907 | ("You must type in %d to %d characters", |
| 873 | uis->_.string_data.result_minsize, | 908 | uis->u.string_data.result_minsize, |
| 874 | uis->_.string_data.result_maxsize); | 909 | uis->u.string_data.result_maxsize); |
| 875 | return -1; | 910 | return -1; |
| 876 | } | 911 | } |
| 877 | if (!uis->result_buf) { | 912 | if (!uis->result_buf) { |
| @@ -879,7 +914,7 @@ UI_set_result(UI *ui, UI_STRING *uis, const char *result) | |||
| 879 | return -1; | 914 | return -1; |
| 880 | } | 915 | } |
| 881 | strlcpy(uis->result_buf, result, | 916 | strlcpy(uis->result_buf, result, |
| 882 | uis->_.string_data.result_maxsize + 1); | 917 | uis->u.string_data.result_maxsize + 1); |
| 883 | break; | 918 | break; |
| 884 | case UIT_BOOLEAN: | 919 | case UIT_BOOLEAN: |
| 885 | if (!uis->result_buf) { | 920 | if (!uis->result_buf) { |
| @@ -888,14 +923,14 @@ UI_set_result(UI *ui, UI_STRING *uis, const char *result) | |||
| 888 | } | 923 | } |
| 889 | uis->result_buf[0] = '\0'; | 924 | uis->result_buf[0] = '\0'; |
| 890 | for (p = result; *p; p++) { | 925 | for (p = result; *p; p++) { |
| 891 | if (strchr(uis->_.boolean_data.ok_chars, *p)) { | 926 | if (strchr(uis->u.boolean_data.ok_chars, *p)) { |
| 892 | uis->result_buf[0] = | 927 | uis->result_buf[0] = |
| 893 | uis->_.boolean_data.ok_chars[0]; | 928 | uis->u.boolean_data.ok_chars[0]; |
| 894 | break; | 929 | break; |
| 895 | } | 930 | } |
| 896 | if (strchr(uis->_.boolean_data.cancel_chars, *p)) { | 931 | if (strchr(uis->u.boolean_data.cancel_chars, *p)) { |
| 897 | uis->result_buf[0] = | 932 | uis->result_buf[0] = |
| 898 | uis->_.boolean_data.cancel_chars[0]; | 933 | uis->u.boolean_data.cancel_chars[0]; |
| 899 | break; | 934 | break; |
| 900 | } | 935 | } |
| 901 | } | 936 | } |
