xref: /netbsd-src/crypto/external/bsd/openssl/dist/test/uitest.c (revision b0d1725196a7921d003d2c66a14f186abda4176b)
113d40330Schristos /*
2*b0d17251Schristos  * Copyright 2002-2022 The OpenSSL Project Authors. All Rights Reserved.
313d40330Schristos  *
4*b0d17251Schristos  * Licensed under the Apache License 2.0 (the "License").  You may not use
513d40330Schristos  * this file except in compliance with the License.  You can obtain a copy
613d40330Schristos  * in the file LICENSE in the source distribution or at
713d40330Schristos  * https://www.openssl.org/source/license.html
813d40330Schristos  */
913d40330Schristos 
1013d40330Schristos #include <stdio.h>
1113d40330Schristos #include <string.h>
1213d40330Schristos #include <openssl/opensslconf.h>
1313d40330Schristos #include <openssl/err.h>
14*b0d17251Schristos #include "apps_ui.h"
1513d40330Schristos #include "testutil.h"
1613d40330Schristos 
1713d40330Schristos 
1813d40330Schristos #include <openssl/ui.h>
1913d40330Schristos 
2013d40330Schristos /* Old style PEM password callback */
test_pem_password_cb(char * buf,int size,int rwflag,void * userdata)2113d40330Schristos static int test_pem_password_cb(char *buf, int size, int rwflag, void *userdata)
2213d40330Schristos {
2313d40330Schristos     OPENSSL_strlcpy(buf, (char *)userdata, (size_t)size);
2413d40330Schristos     return strlen(buf);
2513d40330Schristos }
2613d40330Schristos 
2713d40330Schristos /*
2813d40330Schristos  * Test wrapping old style PEM password callback in a UI method through the
2913d40330Schristos  * use of UI utility functions
3013d40330Schristos  */
test_old(void)3113d40330Schristos static int test_old(void)
3213d40330Schristos {
3313d40330Schristos     UI_METHOD *ui_method = NULL;
3413d40330Schristos     UI *ui = NULL;
3513d40330Schristos     char defpass[] = "password";
3613d40330Schristos     char pass[16];
3713d40330Schristos     int ok = 0;
3813d40330Schristos 
3913d40330Schristos     if (!TEST_ptr(ui_method =
4013d40330Schristos                   UI_UTIL_wrap_read_pem_callback( test_pem_password_cb, 0))
4113d40330Schristos             || !TEST_ptr(ui = UI_new_method(ui_method)))
4213d40330Schristos         goto err;
4313d40330Schristos 
4413d40330Schristos     /* The wrapper passes the UI userdata as the callback userdata param */
4513d40330Schristos     UI_add_user_data(ui, defpass);
4613d40330Schristos 
47*b0d17251Schristos     if (UI_add_input_string(ui, "prompt", UI_INPUT_FLAG_DEFAULT_PWD,
48*b0d17251Schristos                              pass, 0, sizeof(pass) - 1) <= 0)
4913d40330Schristos         goto err;
5013d40330Schristos 
5113d40330Schristos     switch (UI_process(ui)) {
5213d40330Schristos     case -2:
5313d40330Schristos         TEST_info("test_old: UI process interrupted or cancelled");
5413d40330Schristos         /* fall through */
5513d40330Schristos     case -1:
5613d40330Schristos         goto err;
5713d40330Schristos     default:
5813d40330Schristos         break;
5913d40330Schristos     }
6013d40330Schristos 
6113d40330Schristos     if (TEST_str_eq(pass, defpass))
6213d40330Schristos         ok = 1;
6313d40330Schristos 
6413d40330Schristos  err:
6513d40330Schristos     UI_free(ui);
6613d40330Schristos     UI_destroy_method(ui_method);
6713d40330Schristos 
6813d40330Schristos     return ok;
6913d40330Schristos }
7013d40330Schristos 
7113d40330Schristos /* Test of UI.  This uses the UI method defined in apps/apps.c */
test_new_ui(void)7213d40330Schristos static int test_new_ui(void)
7313d40330Schristos {
7413d40330Schristos     PW_CB_DATA cb_data = {
7513d40330Schristos         "password",
7613d40330Schristos         "prompt"
7713d40330Schristos     };
7813d40330Schristos     char pass[16];
7913d40330Schristos     int ok = 0;
8013d40330Schristos 
81*b0d17251Schristos     (void)setup_ui_method();
8213d40330Schristos     if (TEST_int_gt(password_callback(pass, sizeof(pass), 0, &cb_data), 0)
8313d40330Schristos             && TEST_str_eq(pass, cb_data.password))
8413d40330Schristos         ok = 1;
8513d40330Schristos     destroy_ui_method();
8613d40330Schristos     return ok;
8713d40330Schristos }
8813d40330Schristos 
setup_tests(void)8913d40330Schristos int setup_tests(void)
9013d40330Schristos {
9113d40330Schristos     ADD_TEST(test_old);
9213d40330Schristos     ADD_TEST(test_new_ui);
9313d40330Schristos     return 1;
9413d40330Schristos }
95