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