xref: /netbsd-src/crypto/external/cpl/trousers/dist/src/tspi/ssl_ui.c (revision b5c47949a45ac972130c38cf13dfd8afb1f09285)
1 
2 #include <sys/types.h>
3 #include <string.h>
4 
5 #include <openssl/ui.h>
6 
7 #include "trousers/tss.h"
8 #include "spi_utils.h"
9 
10 static TSS_RESULT do_ui(BYTE *string, UINT32 *string_len, BYTE *popup, int verify)
11 {
12 	char pin_buf[UI_MAX_SECRET_STRING_LENGTH + 1];
13 	char verify_buf[UI_MAX_SECRET_STRING_LENGTH + 1];
14 	char *popup_nl;
15 	UI *ui;
16 	BYTE *unicode;
17 	TSS_RESULT ret = TSS_E_FAIL;
18 
19 	popup_nl = malloc(strlen((char *)popup) + 2);
20 	if (!popup_nl)
21 		return TSS_E_OUTOFMEMORY;
22 
23 	ui = UI_new();
24 	if (!ui)
25 		goto no_ui;
26 
27 	sprintf(popup_nl, "%s\n", (char *)popup);
28 	if (!UI_add_info_string(ui, popup_nl)) {
29 		printf("add info fail\n");
30 		goto out;
31 	}
32 
33 	/* UI_add_input_string() doesn't count for the null terminator in its last */
34 	/* parameter, that's why we statically allocated 1 more byte to pin_buf	   */
35 	if (!UI_add_input_string(ui, "Enter PIN:", 0, pin_buf, 1, UI_MAX_SECRET_STRING_LENGTH)) {
36 		printf("add input fail\n");
37 		goto out;
38 	}
39 
40 	if (verify &&
41 	    !UI_add_verify_string(ui, "Verify PIN:", 0, verify_buf, 1, UI_MAX_SECRET_STRING_LENGTH, pin_buf)) {
42 		printf("Add verify fail\n");
43 		goto out;
44 	}
45 
46 	if (UI_process(ui))
47 		goto out;
48 
49 	ret = TSS_SUCCESS;
50 
51 	unicode = Trspi_Native_To_UNICODE((BYTE *)pin_buf, string_len);
52 	__tspi_memset(string, 0, UI_MAX_SECRET_STRING_LENGTH);
53 	memcpy(string, unicode, *string_len);
54 	free(unicode);
55  out:
56 	UI_free(ui);
57  no_ui:
58 	free(popup_nl);
59 	return ret;
60 }
61 
62 /*
63  * DisplayPINWindow()
64  *
65  * Popup the dialog to collect an existing password.
66  *
67  * string - buffer that the password will be passed back to caller in
68  * popup - UTF-8 string to be displayed in the title bar of the dialog box
69  *
70  */
71 TSS_RESULT DisplayPINWindow(BYTE *string, UINT32 *string_len, BYTE *popup)
72 {
73 	return do_ui(string, string_len, popup, 0);
74 }
75 /*
76  * DisplayNewPINWindow()
77  *
78  * Popup the dialog to collect a new password.
79  *
80  * string - buffer that the password will be passed back to caller in
81  * popup - UTF-8 string to be displayed in the title bar of the dialog box
82  *
83  */
84 TSS_RESULT DisplayNewPINWindow(BYTE *string, UINT32 *string_len, BYTE *popup)
85 {
86 	return do_ui(string, string_len, popup, 1);
87 }
88