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