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