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