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