xref: /freebsd/crypto/openssl/test/uitest.c (revision e0c4386e)
1*e0c4386eSCy Schubert /*
2*e0c4386eSCy Schubert  * Copyright 2002-2022 The OpenSSL Project Authors. All Rights Reserved.
3*e0c4386eSCy Schubert  *
4*e0c4386eSCy Schubert  * Licensed under the Apache License 2.0 (the "License").  You may not use
5*e0c4386eSCy Schubert  * this file except in compliance with the License.  You can obtain a copy
6*e0c4386eSCy Schubert  * in the file LICENSE in the source distribution or at
7*e0c4386eSCy Schubert  * https://www.openssl.org/source/license.html
8*e0c4386eSCy Schubert  */
9*e0c4386eSCy Schubert 
10*e0c4386eSCy Schubert #include <stdio.h>
11*e0c4386eSCy Schubert #include <string.h>
12*e0c4386eSCy Schubert #include <openssl/opensslconf.h>
13*e0c4386eSCy Schubert #include <openssl/err.h>
14*e0c4386eSCy Schubert #include "apps_ui.h"
15*e0c4386eSCy Schubert #include "testutil.h"
16*e0c4386eSCy Schubert 
17*e0c4386eSCy Schubert 
18*e0c4386eSCy Schubert #include <openssl/ui.h>
19*e0c4386eSCy Schubert 
20*e0c4386eSCy Schubert /* Old style PEM password callback */
test_pem_password_cb(char * buf,int size,int rwflag,void * userdata)21*e0c4386eSCy Schubert static int test_pem_password_cb(char *buf, int size, int rwflag, void *userdata)
22*e0c4386eSCy Schubert {
23*e0c4386eSCy Schubert     OPENSSL_strlcpy(buf, (char *)userdata, (size_t)size);
24*e0c4386eSCy Schubert     return strlen(buf);
25*e0c4386eSCy Schubert }
26*e0c4386eSCy Schubert 
27*e0c4386eSCy Schubert /*
28*e0c4386eSCy Schubert  * Test wrapping old style PEM password callback in a UI method through the
29*e0c4386eSCy Schubert  * use of UI utility functions
30*e0c4386eSCy Schubert  */
test_old(void)31*e0c4386eSCy Schubert static int test_old(void)
32*e0c4386eSCy Schubert {
33*e0c4386eSCy Schubert     UI_METHOD *ui_method = NULL;
34*e0c4386eSCy Schubert     UI *ui = NULL;
35*e0c4386eSCy Schubert     char defpass[] = "password";
36*e0c4386eSCy Schubert     char pass[16];
37*e0c4386eSCy Schubert     int ok = 0;
38*e0c4386eSCy Schubert 
39*e0c4386eSCy Schubert     if (!TEST_ptr(ui_method =
40*e0c4386eSCy Schubert                   UI_UTIL_wrap_read_pem_callback( test_pem_password_cb, 0))
41*e0c4386eSCy Schubert             || !TEST_ptr(ui = UI_new_method(ui_method)))
42*e0c4386eSCy Schubert         goto err;
43*e0c4386eSCy Schubert 
44*e0c4386eSCy Schubert     /* The wrapper passes the UI userdata as the callback userdata param */
45*e0c4386eSCy Schubert     UI_add_user_data(ui, defpass);
46*e0c4386eSCy Schubert 
47*e0c4386eSCy Schubert     if (UI_add_input_string(ui, "prompt", UI_INPUT_FLAG_DEFAULT_PWD,
48*e0c4386eSCy Schubert                              pass, 0, sizeof(pass) - 1) <= 0)
49*e0c4386eSCy Schubert         goto err;
50*e0c4386eSCy Schubert 
51*e0c4386eSCy Schubert     switch (UI_process(ui)) {
52*e0c4386eSCy Schubert     case -2:
53*e0c4386eSCy Schubert         TEST_info("test_old: UI process interrupted or cancelled");
54*e0c4386eSCy Schubert         /* fall through */
55*e0c4386eSCy Schubert     case -1:
56*e0c4386eSCy Schubert         goto err;
57*e0c4386eSCy Schubert     default:
58*e0c4386eSCy Schubert         break;
59*e0c4386eSCy Schubert     }
60*e0c4386eSCy Schubert 
61*e0c4386eSCy Schubert     if (TEST_str_eq(pass, defpass))
62*e0c4386eSCy Schubert         ok = 1;
63*e0c4386eSCy Schubert 
64*e0c4386eSCy Schubert  err:
65*e0c4386eSCy Schubert     UI_free(ui);
66*e0c4386eSCy Schubert     UI_destroy_method(ui_method);
67*e0c4386eSCy Schubert 
68*e0c4386eSCy Schubert     return ok;
69*e0c4386eSCy Schubert }
70*e0c4386eSCy Schubert 
71*e0c4386eSCy Schubert /* Test of UI.  This uses the UI method defined in apps/apps.c */
test_new_ui(void)72*e0c4386eSCy Schubert static int test_new_ui(void)
73*e0c4386eSCy Schubert {
74*e0c4386eSCy Schubert     PW_CB_DATA cb_data = {
75*e0c4386eSCy Schubert         "password",
76*e0c4386eSCy Schubert         "prompt"
77*e0c4386eSCy Schubert     };
78*e0c4386eSCy Schubert     char pass[16];
79*e0c4386eSCy Schubert     int ok = 0;
80*e0c4386eSCy Schubert 
81*e0c4386eSCy Schubert     (void)setup_ui_method();
82*e0c4386eSCy Schubert     if (TEST_int_gt(password_callback(pass, sizeof(pass), 0, &cb_data), 0)
83*e0c4386eSCy Schubert             && TEST_str_eq(pass, cb_data.password))
84*e0c4386eSCy Schubert         ok = 1;
85*e0c4386eSCy Schubert     destroy_ui_method();
86*e0c4386eSCy Schubert     return ok;
87*e0c4386eSCy Schubert }
88*e0c4386eSCy Schubert 
setup_tests(void)89*e0c4386eSCy Schubert int setup_tests(void)
90*e0c4386eSCy Schubert {
91*e0c4386eSCy Schubert     ADD_TEST(test_old);
92*e0c4386eSCy Schubert     ADD_TEST(test_new_ui);
93*e0c4386eSCy Schubert     return 1;
94*e0c4386eSCy Schubert }
95