xref: /netbsd-src/crypto/external/cpl/trousers/dist/src/tspi/ssl_ui.c (revision 63aea4bd5b445e491ff0389fe27ec78b3099dba3)
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 	memset(string, 0, UI_MAX_SECRET_STRING_LENGTH);
53 	memcpy(string, unicode, *string_len);
54  out:
55 	UI_free(ui);
56  no_ui:
57 	free(popup_nl);
58 	return ret;
59 }
60 
61 /*
62  * DisplayPINWindow()
63  *
64  * Popup the dialog to collect an existing password.
65  *
66  * string - buffer that the password will be passed back to caller in
67  * popup - UTF-8 string to be displayed in the title bar of the dialog box
68  *
69  */
70 TSS_RESULT DisplayPINWindow(BYTE *string, UINT32 *string_len, BYTE *popup)
71 {
72 	return do_ui(string, string_len, popup, 0);
73 }
74 /*
75  * DisplayNewPINWindow()
76  *
77  * Popup the dialog to collect a new password.
78  *
79  * string - buffer that the password will be passed back to caller in
80  * popup - UTF-8 string to be displayed in the title bar of the dialog box
81  *
82  */
83 TSS_RESULT DisplayNewPINWindow(BYTE *string, UINT32 *string_len, BYTE *popup)
84 {
85 	return do_ui(string, string_len, popup, 1);
86 }
87