1 /* $NetBSD: pk11.h,v 1.6 2022/09/23 12:15:33 christos Exp $ */ 2 3 /* 4 * Copyright (C) Internet Systems Consortium, Inc. ("ISC") 5 * 6 * SPDX-License-Identifier: MPL-2.0 7 * 8 * This Source Code Form is subject to the terms of the Mozilla Public 9 * License, v. 2.0. If a copy of the MPL was not distributed with this 10 * file, you can obtain one at https://mozilla.org/MPL/2.0/. 11 * 12 * See the COPYRIGHT file distributed with this work for additional 13 * information regarding copyright ownership. 14 */ 15 16 #ifndef PK11_PK11_H 17 #define PK11_PK11_H 1 18 19 /*! \file pk11/pk11.h */ 20 21 #include <stdbool.h> 22 #include <unistd.h> 23 24 #include <isc/lang.h> 25 #include <isc/magic.h> 26 #include <isc/types.h> 27 28 #define PK11_FATALCHECK(func, args) \ 29 ((void)(((rv = (func)args) == CKR_OK) || \ 30 ((pk11_error_fatalcheck)(__FILE__, __LINE__, #func, rv), 0))) 31 32 #include <pk11/site.h> 33 #include <pkcs11/pkcs11.h> 34 35 ISC_LANG_BEGINDECLS 36 37 #define SES_MAGIC ISC_MAGIC('P', 'K', 'S', 'S') 38 #define TOK_MAGIC ISC_MAGIC('P', 'K', 'T', 'K') 39 40 #define VALID_SES(x) ISC_MAGIC_VALID(x, SES_MAGIC) 41 #define VALID_TOK(x) ISC_MAGIC_VALID(x, TOK_MAGIC) 42 43 typedef struct pk11_context pk11_context_t; 44 45 struct pk11_object { 46 CK_OBJECT_HANDLE object; 47 CK_SLOT_ID slot; 48 CK_BBOOL ontoken; 49 CK_BBOOL reqlogon; 50 CK_BYTE attrcnt; 51 CK_ATTRIBUTE *repr; 52 }; 53 54 struct pk11_context { 55 void *handle; 56 CK_SESSION_HANDLE session; 57 CK_BBOOL ontoken; 58 CK_OBJECT_HANDLE object; 59 }; 60 61 typedef struct pk11_object pk11_object_t; 62 63 typedef enum { 64 OP_ANY = 0, 65 OP_RSA = 1, 66 OP_DH = 3, 67 OP_ECDSA = 4, 68 OP_EDDSA = 5, 69 OP_MAX = 6 70 } pk11_optype_t; 71 72 /*% 73 * Global flag to make choose_slots() verbose 74 */ 75 LIBISC_EXTERNAL_DATA extern bool pk11_verbose_init; 76 77 /*% 78 * Function prototypes 79 */ 80 81 void 82 pk11_set_lib_name(const char *lib_name); 83 /*%< 84 * Set the PKCS#11 provider (aka library) path/name. 85 */ 86 87 isc_result_t 88 pk11_initialize(isc_mem_t *mctx, const char *engine); 89 /*%< 90 * Initialize PKCS#11 device 91 * 92 * mctx: memory context to attach to pk11_mctx. 93 * engine: PKCS#11 provider (aka library) path/name. 94 * 95 * returns: 96 * ISC_R_SUCCESS 97 * PK11_R_NOPROVIDER: can't load the provider 98 * PK11_R_INITFAILED: C_Initialize() failed 99 * PK11_R_NORANDOMSERVICE: can't find required random service 100 * PK11_R_NODIGESTSERVICE: can't find required digest service 101 * PK11_R_NOAESSERVICE: can't find required AES service 102 */ 103 104 isc_result_t 105 pk11_get_session(pk11_context_t *ctx, pk11_optype_t optype, bool need_services, 106 bool rw, bool logon, const char *pin, CK_SLOT_ID slot); 107 /*%< 108 * Initialize PKCS#11 device and acquire a session. 109 * 110 * need_services: 111 * if true, this session requires full PKCS#11 API 112 * support including random and digest services, and 113 * the lack of these services will cause the session not 114 * to be initialized. If false, the function will return 115 * an error code indicating the missing service, but the 116 * session will be usable for other purposes. 117 * rw: if true, session will be read/write (useful for 118 * generating or destroying keys); otherwise read-only. 119 * login: indicates whether to log in to the device 120 * pin: optional PIN, overriding any PIN currently associated 121 * with the 122 * slot: device slot ID 123 */ 124 125 void 126 pk11_return_session(pk11_context_t *ctx); 127 /*%< 128 * Release an active PKCS#11 session for reuse. 129 */ 130 131 isc_result_t 132 pk11_finalize(void); 133 /*%< 134 * Shut down PKCS#11 device and free all sessions. 135 */ 136 137 isc_result_t 138 pk11_parse_uri(pk11_object_t *obj, const char *label, isc_mem_t *mctx, 139 pk11_optype_t optype); 140 141 ISC_PLATFORM_NORETURN_PRE void 142 pk11_error_fatalcheck(const char *file, int line, const char *funcname, 143 CK_RV rv) ISC_PLATFORM_NORETURN_POST; 144 145 void 146 pk11_dump_tokens(void); 147 148 CK_RV 149 pkcs_C_Initialize(CK_VOID_PTR pReserved); 150 151 char * 152 pk11_get_load_error_message(void); 153 154 CK_RV 155 pkcs_C_Finalize(CK_VOID_PTR pReserved); 156 157 CK_RV 158 pkcs_C_GetSlotList(CK_BBOOL tokenPresent, CK_SLOT_ID_PTR pSlotList, 159 CK_ULONG_PTR pulCount); 160 161 CK_RV 162 pkcs_C_GetTokenInfo(CK_SLOT_ID slotID, CK_TOKEN_INFO_PTR pInfo); 163 164 CK_RV 165 pkcs_C_GetMechanismInfo(CK_SLOT_ID slotID, CK_MECHANISM_TYPE type, 166 CK_MECHANISM_INFO_PTR pInfo); 167 168 CK_RV 169 pkcs_C_OpenSession(CK_SLOT_ID slotID, CK_FLAGS flags, CK_VOID_PTR pApplication, 170 CK_RV (*Notify)(CK_SESSION_HANDLE hSession, 171 CK_NOTIFICATION event, 172 CK_VOID_PTR pApplication), 173 CK_SESSION_HANDLE_PTR phSession); 174 175 CK_RV 176 pkcs_C_CloseSession(CK_SESSION_HANDLE hSession); 177 178 CK_RV 179 pkcs_C_Login(CK_SESSION_HANDLE hSession, CK_USER_TYPE userType, 180 CK_CHAR_PTR pPin, CK_ULONG usPinLen); 181 182 CK_RV 183 pkcs_C_Logout(CK_SESSION_HANDLE hSession); 184 185 CK_RV 186 pkcs_C_CreateObject(CK_SESSION_HANDLE hSession, CK_ATTRIBUTE_PTR pTemplate, 187 CK_ULONG usCount, CK_OBJECT_HANDLE_PTR phObject); 188 189 CK_RV 190 pkcs_C_DestroyObject(CK_SESSION_HANDLE hSession, CK_OBJECT_HANDLE hObject); 191 192 CK_RV 193 pkcs_C_GetAttributeValue(CK_SESSION_HANDLE hSession, CK_OBJECT_HANDLE hObject, 194 CK_ATTRIBUTE_PTR pTemplate, CK_ULONG usCount); 195 196 CK_RV 197 pkcs_C_SetAttributeValue(CK_SESSION_HANDLE hSession, CK_OBJECT_HANDLE hObject, 198 CK_ATTRIBUTE_PTR pTemplate, CK_ULONG usCount); 199 200 CK_RV 201 pkcs_C_FindObjectsInit(CK_SESSION_HANDLE hSession, CK_ATTRIBUTE_PTR pTemplate, 202 CK_ULONG usCount); 203 204 CK_RV 205 pkcs_C_FindObjects(CK_SESSION_HANDLE hSession, CK_OBJECT_HANDLE_PTR phObject, 206 CK_ULONG usMaxObjectCount, CK_ULONG_PTR pusObjectCount); 207 208 CK_RV 209 pkcs_C_FindObjectsFinal(CK_SESSION_HANDLE hSession); 210 211 CK_RV 212 pkcs_C_EncryptInit(CK_SESSION_HANDLE hSession, CK_MECHANISM_PTR pMechanism, 213 CK_OBJECT_HANDLE hKey); 214 215 CK_RV 216 pkcs_C_Encrypt(CK_SESSION_HANDLE hSession, CK_BYTE_PTR pData, 217 CK_ULONG ulDataLen, CK_BYTE_PTR pEncryptedData, 218 CK_ULONG_PTR pulEncryptedDataLen); 219 220 CK_RV 221 pkcs_C_DigestInit(CK_SESSION_HANDLE hSession, CK_MECHANISM_PTR pMechanism); 222 223 CK_RV 224 pkcs_C_DigestUpdate(CK_SESSION_HANDLE hSession, CK_BYTE_PTR pPart, 225 CK_ULONG ulPartLen); 226 227 CK_RV 228 pkcs_C_DigestFinal(CK_SESSION_HANDLE hSession, CK_BYTE_PTR pDigest, 229 CK_ULONG_PTR pulDigestLen); 230 231 CK_RV 232 pkcs_C_SignInit(CK_SESSION_HANDLE hSession, CK_MECHANISM_PTR pMechanism, 233 CK_OBJECT_HANDLE hKey); 234 235 CK_RV 236 pkcs_C_Sign(CK_SESSION_HANDLE hSession, CK_BYTE_PTR pData, CK_ULONG ulDataLen, 237 CK_BYTE_PTR pSignature, CK_ULONG_PTR pulSignatureLen); 238 239 CK_RV 240 pkcs_C_SignUpdate(CK_SESSION_HANDLE hSession, CK_BYTE_PTR pPart, 241 CK_ULONG ulPartLen); 242 243 CK_RV 244 pkcs_C_SignFinal(CK_SESSION_HANDLE hSession, CK_BYTE_PTR pSignature, 245 CK_ULONG_PTR pulSignatureLen); 246 247 CK_RV 248 pkcs_C_VerifyInit(CK_SESSION_HANDLE hSession, CK_MECHANISM_PTR pMechanism, 249 CK_OBJECT_HANDLE hKey); 250 251 CK_RV 252 pkcs_C_Verify(CK_SESSION_HANDLE hSession, CK_BYTE_PTR pData, CK_ULONG ulDataLen, 253 CK_BYTE_PTR pSignature, CK_ULONG ulSignatureLen); 254 255 CK_RV 256 pkcs_C_VerifyUpdate(CK_SESSION_HANDLE hSession, CK_BYTE_PTR pPart, 257 CK_ULONG ulPartLen); 258 259 CK_RV 260 pkcs_C_VerifyFinal(CK_SESSION_HANDLE hSession, CK_BYTE_PTR pSignature, 261 CK_ULONG ulSignatureLen); 262 263 CK_RV 264 pkcs_C_GenerateKey(CK_SESSION_HANDLE hSession, CK_MECHANISM_PTR pMechanism, 265 CK_ATTRIBUTE_PTR pTemplate, CK_ULONG ulCount, 266 CK_OBJECT_HANDLE_PTR phKey); 267 268 CK_RV 269 pkcs_C_GenerateKeyPair(CK_SESSION_HANDLE hSession, CK_MECHANISM_PTR pMechanism, 270 CK_ATTRIBUTE_PTR pPublicKeyTemplate, 271 CK_ULONG usPublicKeyAttributeCount, 272 CK_ATTRIBUTE_PTR pPrivateKeyTemplate, 273 CK_ULONG usPrivateKeyAttributeCount, 274 CK_OBJECT_HANDLE_PTR phPrivateKey, 275 CK_OBJECT_HANDLE_PTR phPublicKey); 276 277 CK_RV 278 pkcs_C_DeriveKey(CK_SESSION_HANDLE hSession, CK_MECHANISM_PTR pMechanism, 279 CK_OBJECT_HANDLE hBaseKey, CK_ATTRIBUTE_PTR pTemplate, 280 CK_ULONG ulAttributeCount, CK_OBJECT_HANDLE_PTR phKey); 281 282 CK_RV 283 pkcs_C_SeedRandom(CK_SESSION_HANDLE hSession, CK_BYTE_PTR pSeed, 284 CK_ULONG ulSeedLen); 285 286 CK_RV 287 pkcs_C_GenerateRandom(CK_SESSION_HANDLE hSession, CK_BYTE_PTR RandomData, 288 CK_ULONG ulRandomLen); 289 290 ISC_LANG_ENDDECLS 291 292 #endif /* PK11_PK11_H */ 293