xref: /optee_os/ta/pkcs11/src/entry.c (revision b6afa13a5fe7d94666ba7a5e2c98ba78dc22092d)
1 // SPDX-License-Identifier: BSD-2-Clause
2 /*
3  * Copyright (c) 2018-2020, Linaro Limited
4  */
5 
6 #include <compiler.h>
7 #include <pkcs11_ta.h>
8 #include <tee_internal_api.h>
9 
10 TEE_Result TA_CreateEntryPoint(void)
11 {
12 	return TEE_SUCCESS;
13 }
14 
15 void TA_DestroyEntryPoint(void)
16 {
17 }
18 
19 TEE_Result TA_OpenSessionEntryPoint(uint32_t __unused param_types,
20 				    TEE_Param __unused params[4],
21 				    void **session)
22 {
23 	*session = NULL;
24 
25 	return TEE_SUCCESS;
26 }
27 
28 void TA_CloseSessionEntryPoint(void *session __unused)
29 {
30 }
31 
32 /*
33  * Entry point for invocation command PKCS11_CMD_PING
34  *
35  * @ctrl - param memref[0] or NULL: expected NULL
36  * @in - param memref[1] or NULL: expected NULL
37  * @out - param memref[2] or NULL
38  *
39  * Return a TEE_Result value
40  */
41 static TEE_Result entry_ping(TEE_Param *ctrl, TEE_Param *in, TEE_Param *out)
42 {
43 	const uint32_t ver[] = {
44 		PKCS11_TA_VERSION_MAJOR,
45 		PKCS11_TA_VERSION_MINOR,
46 		PKCS11_TA_VERSION_PATCH,
47 	};
48 	size_t size = 0;
49 
50 	if (ctrl || in)
51 		return TEE_ERROR_BAD_PARAMETERS;
52 
53 	if (!out)
54 		return TEE_SUCCESS;
55 
56 	size = out->memref.size;
57 	out->memref.size = sizeof(ver);
58 
59 	if (size < sizeof(ver))
60 		return TEE_ERROR_SHORT_BUFFER;
61 
62 	TEE_MemMove(out->memref.buffer, ver, sizeof(ver));
63 
64 	return TEE_SUCCESS;
65 }
66 
67 /*
68  * Entry point for PKCS11 TA commands
69  */
70 TEE_Result TA_InvokeCommandEntryPoint(void *tee_session __unused, uint32_t cmd,
71 				      uint32_t ptypes,
72 				      TEE_Param params[TEE_NUM_PARAMS])
73 {
74 	TEE_Param *ctrl = NULL;
75 	TEE_Param *p1_in = NULL;
76 	TEE_Param __maybe_unused *p2_in = NULL;
77 	TEE_Param *p2_out = NULL;
78 	TEE_Result res = TEE_ERROR_GENERIC;
79 
80 	/* Param#0: none or in-out buffer with serialized arguments */
81 	switch (TEE_PARAM_TYPE_GET(ptypes, 0)) {
82 	case TEE_PARAM_TYPE_NONE:
83 		break;
84 	case TEE_PARAM_TYPE_MEMREF_INOUT:
85 		ctrl = &params[0];
86 		break;
87 	default:
88 		return TEE_ERROR_BAD_PARAMETERS;
89 	}
90 
91 	/* Param#1: none or input data buffer */
92 	switch (TEE_PARAM_TYPE_GET(ptypes, 1)) {
93 	case TEE_PARAM_TYPE_NONE:
94 		break;
95 	case TEE_PARAM_TYPE_MEMREF_INPUT:
96 		p1_in = &params[1];
97 		break;
98 	default:
99 		return TEE_ERROR_BAD_PARAMETERS;
100 	}
101 
102 	/* Param#2: none or input data buffer */
103 	switch (TEE_PARAM_TYPE_GET(ptypes, 2)) {
104 	case TEE_PARAM_TYPE_NONE:
105 		break;
106 	case TEE_PARAM_TYPE_MEMREF_INPUT:
107 		p2_in = &params[2];
108 		break;
109 	case TEE_PARAM_TYPE_MEMREF_OUTPUT:
110 		p2_out = &params[2];
111 		break;
112 	default:
113 		return TEE_ERROR_BAD_PARAMETERS;
114 	}
115 
116 	/* Param#3: currently unused */
117 	switch (TEE_PARAM_TYPE_GET(ptypes, 3)) {
118 	case TEE_PARAM_TYPE_NONE:
119 		break;
120 	default:
121 		return TEE_ERROR_BAD_PARAMETERS;
122 	}
123 
124 	switch (cmd) {
125 	case PKCS11_CMD_PING:
126 		res = entry_ping(ctrl, p1_in, p2_out);
127 		break;
128 
129 	default:
130 		EMSG("Command 0x%"PRIx32" is not supported", cmd);
131 		return TEE_ERROR_NOT_SUPPORTED;
132 	}
133 
134 	/* Currently no output data stored in output param#0 */
135 	if (TEE_PARAM_TYPE_GET(ptypes, 0) == TEE_PARAM_TYPE_MEMREF_INOUT)
136 		ctrl->memref.size = 0;
137 
138 	return res;
139 }
140