Add a warning stipulating how things should be coded in ossl_init_base
[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 }