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