Add a UI utility function with which to wrap pem_callback_cb in a UI_METHOD
[oweals/openssl.git] / crypto / ui / ui_util.c
1 /*
2  * Copyright 2002-2016 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the OpenSSL license (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 "ui_locl.h"
12
13 #ifndef BUFSIZ
14 #define BUFSIZ 256
15 #endif
16
17 int UI_UTIL_read_pw_string(char *buf, int length, const char *prompt,
18                            int verify)
19 {
20     char buff[BUFSIZ];
21     int ret;
22
23     ret =
24         UI_UTIL_read_pw(buf, buff, (length > BUFSIZ) ? BUFSIZ : length,
25                         prompt, verify);
26     OPENSSL_cleanse(buff, BUFSIZ);
27     return (ret);
28 }
29
30 int UI_UTIL_read_pw(char *buf, char *buff, int size, const char *prompt,
31                     int verify)
32 {
33     int ok = 0;
34     UI *ui;
35
36     if (size < 1)
37         return -1;
38
39     ui = UI_new();
40     if (ui != NULL) {
41         ok = UI_add_input_string(ui, prompt, 0, buf, 0, size - 1);
42         if (ok >= 0 && verify)
43             ok = UI_add_verify_string(ui, prompt, 0, buff, 0, size - 1, buf);
44         if (ok >= 0)
45             ok = UI_process(ui);
46         UI_free(ui);
47     }
48     if (ok > 0)
49         ok = 0;
50     return (ok);
51 }
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,
72                               void *from_d, int idx, long argl, void *argp)
73 {
74     void **pptr = (void **)from_d;
75     if (*pptr != NULL)
76         *pptr = OPENSSL_memdup(*pptr, sizeof(struct pem_password_cb_data));
77     return 1;
78 }
79
80 static void ui_free_method_data(void *parent, void *ptr, CRYPTO_EX_DATA *ad,
81                                 int idx, long argl, void *argp)
82 {
83     OPENSSL_free(ptr);
84 }
85
86 static int ui_method_data_index()
87 {
88     static int idx = -1;
89
90     if (idx == -1)
91         idx = CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_UI_METHOD,
92                                       0, NULL,
93                                       ui_new_method_data,
94                                       ui_dup_method_data,
95                                       ui_free_method_data);
96
97     return idx;
98 }
99
100 static int ui_open(UI *ui)
101 {
102     return 1;
103 }
104 static int ui_read(UI *ui, UI_STRING *uis)
105 {
106     switch (UI_get_string_type(uis)) {
107     case UIT_PROMPT:
108         {
109             char result[PEM_BUFSIZE];
110             const struct pem_password_cb_data *data =
111                 UI_method_get_ex_data(UI_get_method(ui),
112                                       ui_method_data_index());
113             int maxsize = UI_get_result_maxsize(uis);
114             int len = data->cb(result,
115                                maxsize > PEM_BUFSIZE ? PEM_BUFSIZE : maxsize,
116                                data->rwflag, UI_get0_user_data(ui));
117
118             if (len <= 0)
119                 return len;
120             if (UI_set_result(ui, uis, result) >= 0)
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
153         || UI_method_set_ex_data(ui_method, ui_method_data_index(), data) < 0) {
154         UI_destroy_method(ui_method);
155         OPENSSL_free(data);
156         return NULL;
157     }
158     data->rwflag = rwflag;
159     data->cb = cb;
160
161     return ui_method;
162 }