Add options to check TLS signing hashes
[openssl.git] / test / uitest.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 <stdio.h>
11 #include <string.h>
12 #include <openssl/opensslconf.h>
13 #include <openssl/err.h>
14
15 /*
16  * We know that on VMS, the [.apps] object files are compiled with uppercased
17  * symbols.  We must therefore follow suit, or there will be linking errors.
18  * Additionally, the VMS build does stdio via a socketpair.
19  */
20 #ifdef __VMS
21 # pragma names save
22 # pragma names uppercase, truncated
23
24 # include "../apps/vms_term_sock.h"
25 #endif
26
27 #include "../apps/apps.h"
28
29 #ifdef __VMS
30 # pragma names restore
31 #endif
32
33 #include "testutil.h"
34 #include "test_main_custom.h"
35
36 /* apps/apps.c depend on these */
37 char *default_config_file = NULL;
38 BIO *bio_err = NULL;
39
40 #ifndef OPENSSL_NO_UI
41 # include <openssl/ui.h>
42
43 /* Old style PEM password callback */
44 static int test_pem_password_cb(char *buf, int size, int rwflag, void *userdata)
45 {
46     OPENSSL_strlcpy(buf, (char *)userdata, (size_t)size);
47     return 1;
48 }
49
50 /*
51  * Test wrapping old style PEM password callback in a UI method through the
52  * use of UI utility functions
53  */
54 static int test_old()
55 {
56     UI_METHOD *ui_method = NULL;
57     UI *ui = NULL;
58     char defpass[] = "password";
59     char pass[16];
60     int ok = 0;
61
62     if ((ui_method =
63          UI_UTIL_wrap_read_pem_callback(test_pem_password_cb, 0)) == NULL
64         || (ui = UI_new_method(ui_method)) == NULL)
65         goto err;
66
67     /* The wrapper passes the UI userdata as the callback userdata param */
68     UI_add_user_data(ui, defpass);
69
70     if (!UI_add_input_string(ui, "prompt", UI_INPUT_FLAG_DEFAULT_PWD,
71                              pass, 0, sizeof(pass) - 1))
72         goto err;
73
74     switch (UI_process(ui)) {
75     case -2:
76         BIO_printf(bio_err, "test_old: UI process interrupted or cancelled\n");
77         /* fall through */
78     case -1:
79         goto err;
80     default:
81         break;
82     }
83
84     if (strcmp(pass, defpass) == 0)
85         ok = 1;
86     else
87         BIO_printf(bio_err, "test_old: password failure\n");
88
89  err:
90     if (!ok)
91         ERR_print_errors_fp(stderr);
92     UI_free(ui);
93     UI_destroy_method(ui_method);
94
95     return ok;
96 }
97
98 /* Test of UI.  This uses the UI method defined in apps/apps.c */
99 static int test_new_ui()
100 {
101     PW_CB_DATA cb_data = {
102         "password",
103         "prompt"
104     };
105     char pass[16];
106     int ok = 0;
107
108     setup_ui_method();
109     if (password_callback(pass, sizeof(pass), 0, &cb_data) > 0
110         && strcmp(pass, cb_data.password) == 0)
111         ok = 1;
112     else
113         BIO_printf(bio_err, "test_new: password failure\n");
114
115     if (!ok)
116         ERR_print_errors_fp(stderr);
117
118     destroy_ui_method();
119     return ok;
120 }
121
122 #endif
123
124 int test_main(int argc, char *argv[])
125 {
126     int ret;
127
128     bio_err = dup_bio_err(FORMAT_TEXT);
129
130 #ifndef OPENSSL_NO_UI
131     ADD_TEST(test_old);
132     ADD_TEST(test_new_ui);
133 #endif
134
135     ret = run_tests(argv[0]);
136
137     (void)BIO_flush(bio_err);
138     BIO_free(bio_err);
139
140     return ret;
141 }