xref: /optee_os/ta/pkcs11/src/pkcs11_token.h (revision b56b3d071d79537f0b9c86d26c033d9ed5c0206a)
1 /* SPDX-License-Identifier: BSD-2-Clause */
2 /*
3  * Copyright (c) 2017-2020, Linaro Limited
4  */
5 #ifndef PKCS11_TA_PKCS11_TOKEN_H
6 #define PKCS11_TA_PKCS11_TOKEN_H
7 
8 #include <sys/queue.h>
9 #include <tee_api_types.h>
10 #include <tee_internal_api.h>
11 #include <utee_defines.h>
12 
13 #include "handle.h"
14 #include "object.h"
15 #include "pkcs11_attributes.h"
16 
17 /* Hard coded description */
18 #define PKCS11_SLOT_DESCRIPTION		"OP-TEE PKCS11 TA"
19 #define PKCS11_SLOT_MANUFACTURER	"Linaro"
20 #define PKCS11_SLOT_HW_VERSION		{ 0, 0 }
21 #define PKCS11_SLOT_FW_VERSION		{ PKCS11_TA_VERSION_MAJOR, \
22 					  PKCS11_TA_VERSION_MINOR }
23 
24 #define PKCS11_TOKEN_LABEL		"OP-TEE PKCS#11 TA token"
25 #define PKCS11_TOKEN_MANUFACTURER	PKCS11_SLOT_MANUFACTURER
26 #define PKCS11_TOKEN_MODEL		"OP-TEE TA"
27 #define PKCS11_TOKEN_HW_VERSION		PKCS11_SLOT_HW_VERSION
28 #define PKCS11_TOKEN_FW_VERSION		PKCS11_SLOT_FW_VERSION
29 
30 enum pkcs11_token_state {
31 	PKCS11_TOKEN_RESET = 0,
32 	PKCS11_TOKEN_READ_WRITE,
33 	PKCS11_TOKEN_READ_ONLY,
34 };
35 
36 TAILQ_HEAD(client_list, pkcs11_client);
37 TAILQ_HEAD(session_list, pkcs11_session);
38 
39 struct pkcs11_client;
40 
41 #define PKCS11_MAX_USERS		2
42 #define PKCS11_TOKEN_PIN_SIZE_MAX	128
43 #define PKCS11_TOKEN_PIN_SIZE_MIN	10
44 #define PKCS11_TOKEN_SO_PIN_COUNT_MAX	7
45 #define PKCS11_TOKEN_USER_PIN_COUNT_MAX	7
46 
47 /*
48  * Persistent state of the token
49  *
50  * @version - currently unused...
51  * @label - pkcs11 formatted token label, set by client
52  * @flags - pkcs11 token flags
53  * @so_pin_count - counter on security officer login failure
54  * @so_pin_salt - stores salt in hash of SO PIN, 0 if not set
55  * @so_pin_hash - stores hash of SO PIN
56  * @user_pin_count - counter on user login failure
57  * @user_pin_salt - stores salt in hash of user PIN, 0 if not set
58  * @user_pin_hash - stores hash of user PIN
59  */
60 struct token_persistent_main {
61 	uint32_t version;
62 	uint8_t label[PKCS11_TOKEN_LABEL_SIZE];
63 	uint32_t flags;
64 	uint32_t so_pin_count;
65 	uint32_t so_pin_salt;
66 	uint8_t so_pin_hash[TEE_MAX_HASH_SIZE];
67 	uint32_t user_pin_count;
68 	uint32_t user_pin_salt;
69 	uint8_t user_pin_hash[TEE_MAX_HASH_SIZE];
70 };
71 
72 /*
73  * Runtime state of the token, complies with pkcs11
74  *
75  * @state - Pkcs11 login is public, user, SO or custom
76  * @session_count - Counter for opened Pkcs11 sessions
77  * @rw_session_count - Count for opened Pkcs11 read/write sessions
78  * @object_list - List of the object owned by the token
79  * @db_main - Volatile copy of the persistent main database
80  */
81 struct ck_token {
82 	enum pkcs11_token_state state;
83 	uint32_t session_count;
84 	uint32_t rw_session_count;
85 	struct object_list object_list;
86 	/* Copy in RAM of the persistent database */
87 	struct token_persistent_main *db_main;
88 };
89 
90 /*
91  * Structure tracking the PKCS#11 sessions
92  *
93  * @link - List of the session belonging to a client
94  * @client - Client the session belongs to
95  * @token - Token this session belongs to
96  * @handle - Identifier of the session published to the client
97  * @object_list - Entry of the session objects list
98  * @object_handle_db - Database for object handles published by the session
99  * @state - R/W SO, R/W user, RO user, R/W public, RO public.
100  */
101 struct pkcs11_session {
102 	TAILQ_ENTRY(pkcs11_session) link;
103 	struct pkcs11_client *client;
104 	struct ck_token *token;
105 	uint32_t handle;
106 	struct object_list object_list;
107 	struct handle_db object_handle_db;
108 	enum pkcs11_session_state state;
109 };
110 
111 /* Initialize static token instance(s) from default/persistent database */
112 TEE_Result pkcs11_init(void);
113 void pkcs11_deinit(void);
114 
115 /* Speculation safe lookup of token instance from token identifier */
116 struct ck_token *get_token(unsigned int token_id);
117 
118 /* Return token identified from token instance address */
119 unsigned int get_token_id(struct ck_token *token);
120 
121 /* Access to persistent database */
122 struct ck_token *init_persistent_db(unsigned int token_id);
123 void update_persistent_db(struct ck_token *token);
124 void close_persistent_db(struct ck_token *token);
125 
126 enum pkcs11_rc hash_pin(enum pkcs11_user_type user, const uint8_t *pin,
127 			size_t pin_size, uint32_t *salt,
128 			uint8_t hash[TEE_MAX_HASH_SIZE]);
129 enum pkcs11_rc verify_pin(enum pkcs11_user_type user, const uint8_t *pin,
130 			  size_t pin_size, uint32_t salt,
131 			  const uint8_t hash[TEE_MAX_HASH_SIZE]);
132 
133 /*
134  * Pkcs11 session support
135  */
136 struct pkcs11_client *tee_session2client(void *tee_session);
137 struct pkcs11_client *register_client(void);
138 void unregister_client(struct pkcs11_client *client);
139 
140 struct pkcs11_session *pkcs11_handle2session(uint32_t handle,
141 					     struct pkcs11_client *client);
142 
143 static inline bool pkcs11_session_is_read_write(struct pkcs11_session *session)
144 {
145 	return session->state == PKCS11_CKS_RW_PUBLIC_SESSION ||
146 	       session->state == PKCS11_CKS_RW_USER_FUNCTIONS ||
147 	       session->state == PKCS11_CKS_RW_SO_FUNCTIONS;
148 }
149 
150 static inline bool pkcs11_session_is_public(struct pkcs11_session *session)
151 {
152 	return session->state == PKCS11_CKS_RO_PUBLIC_SESSION ||
153 	       session->state == PKCS11_CKS_RW_PUBLIC_SESSION;
154 }
155 
156 static inline bool pkcs11_session_is_user(struct pkcs11_session *session)
157 {
158 	return session->state == PKCS11_CKS_RO_USER_FUNCTIONS ||
159 	       session->state == PKCS11_CKS_RW_USER_FUNCTIONS;
160 }
161 
162 static inline bool pkcs11_session_is_so(struct pkcs11_session *session)
163 {
164 	return session->state == PKCS11_CKS_RW_SO_FUNCTIONS;
165 }
166 
167 static inline
168 struct object_list *pkcs11_get_session_objects(struct pkcs11_session *session)
169 {
170 	return &session->object_list;
171 }
172 
173 static inline
174 struct ck_token *pkcs11_session2token(struct pkcs11_session *session)
175 {
176 	return session->token;
177 }
178 
179 /* Entry point for the TA commands */
180 enum pkcs11_rc entry_ck_slot_list(uint32_t ptypes, TEE_Param *params);
181 enum pkcs11_rc entry_ck_slot_info(uint32_t ptypes, TEE_Param *params);
182 enum pkcs11_rc entry_ck_token_info(uint32_t ptypes, TEE_Param *params);
183 enum pkcs11_rc entry_ck_token_mecha_ids(uint32_t ptypes, TEE_Param *params);
184 enum pkcs11_rc entry_ck_token_mecha_info(uint32_t ptypes, TEE_Param *params);
185 enum pkcs11_rc entry_ck_open_session(struct pkcs11_client *client,
186 				     uint32_t ptypes, TEE_Param *params);
187 enum pkcs11_rc entry_ck_close_session(struct pkcs11_client *client,
188 				      uint32_t ptypes, TEE_Param *params);
189 enum pkcs11_rc entry_ck_close_all_sessions(struct pkcs11_client *client,
190 					   uint32_t ptypes, TEE_Param *params);
191 enum pkcs11_rc entry_ck_session_info(struct pkcs11_client *client,
192 				     uint32_t ptypes, TEE_Param *params);
193 enum pkcs11_rc entry_ck_token_initialize(uint32_t ptypes, TEE_Param *params);
194 enum pkcs11_rc entry_ck_init_pin(struct pkcs11_client *client,
195 				 uint32_t ptypes, TEE_Param *params);
196 enum pkcs11_rc entry_ck_set_pin(struct pkcs11_client *client,
197 				uint32_t ptypes, TEE_Param *params);
198 enum pkcs11_rc entry_ck_login(struct pkcs11_client *client,
199 			      uint32_t ptypes, TEE_Param *params);
200 enum pkcs11_rc entry_ck_logout(struct pkcs11_client *client,
201 			       uint32_t ptypes, TEE_Param *params);
202 
203 #endif /*PKCS11_TA_PKCS11_TOKEN_H*/
204