xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/keyctl/keyctl09.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (c) 2022 Google, Inc.
4  * Copyright (c) Linux Test Project, 2023
5  */
6 
7 /*\
8  * [Description]
9  *
10  * Test that encrypted keys can be instantiated using user-provided decrypted
11  * data that is hex-ascii encoded.
12  */
13 
14 #include "tst_test.h"
15 #include "lapi/keyctl.h"
16 
17 #define ENCRYPTED_KEY_VALID_PAYLOAD	"new enc32 user:masterkey 32 abcdefABCDEF1234567890aaaaaaaaaaabcdefABCDEF1234567890aaaaaaaaaa"
18 #define ENCRYPTED_KEY_INVALID_PAYLOAD	"new enc32 user:masterkey 32 plaintext123@123!123@123!123@123plaintext123@123!123@123!123@123"
19 
do_test(void)20 static void do_test(void)
21 {
22 	char buffer[128];
23 
24 	TST_EXP_POSITIVE(add_key("user", "user:masterkey", "foo", 3,
25 			    KEY_SPEC_PROCESS_KEYRING));
26 
27 	if (!TST_PASS)
28 		return;
29 
30 	TST_EXP_POSITIVE(add_key("encrypted", "ltptestkey1",
31 			    ENCRYPTED_KEY_VALID_PAYLOAD,
32 			    strlen(ENCRYPTED_KEY_VALID_PAYLOAD),
33 			    KEY_SPEC_PROCESS_KEYRING));
34 
35 	if (!TST_PASS)
36 		return;
37 
38 	TST_EXP_POSITIVE(keyctl(KEYCTL_READ, TST_RET, buffer, sizeof(buffer)));
39 
40 	if (!TST_PASS)
41 		return;
42 
43 	TST_EXP_FAIL2(add_key("encrypted", "ltptestkey2",
44 			    ENCRYPTED_KEY_INVALID_PAYLOAD,
45 			    strlen(ENCRYPTED_KEY_INVALID_PAYLOAD),
46 			    KEY_SPEC_PROCESS_KEYRING), EINVAL);
47 
48 	keyctl(KEYCTL_CLEAR, KEY_SPEC_PROCESS_KEYRING);
49 }
50 
51 static struct tst_test test = {
52 	.test_all = do_test,
53 	.needs_kconfigs = (const char *[]) {
54 		"CONFIG_USER_DECRYPTED_DATA=y",
55 		NULL
56 	},
57 	.tags = (const struct tst_tag[]) {
58 		{ "linux-git", "5adedd42245af"},
59 		{}
60 	}
61 };
62