wc_pkcs11.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /* wc_pkcs11.h
  2. *
  3. * Copyright (C) 2006-2022 wolfSSL Inc.
  4. *
  5. * This file is part of wolfSSL.
  6. *
  7. * wolfSSL is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * wolfSSL is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
  20. */
  21. #ifndef _WOLFPKCS11_H_
  22. #define _WOLFPKCS11_H_
  23. #include <wolfssl/wolfcrypt/types.h>
  24. #ifdef HAVE_PKCS11
  25. #ifndef WOLF_CRYPTO_CB
  26. #error PKCS11 support requires ./configure --enable-cryptocb or WOLF_CRYPTO_CB to be defined
  27. #endif
  28. #include <wolfssl/wolfcrypt/cryptocb.h>
  29. #include <wolfssl/wolfcrypt/pkcs11.h>
  30. #ifdef __cplusplus
  31. extern "C" {
  32. #endif
  33. typedef struct Pkcs11Dev {
  34. #ifndef HAVE_PKCS11_STATIC
  35. void* dlHandle; /* Handle to library */
  36. #endif
  37. CK_FUNCTION_LIST* func; /* Array of functions */
  38. void* heap;
  39. } Pkcs11Dev;
  40. typedef struct Pkcs11Token {
  41. CK_FUNCTION_LIST* func; /* Table of PKCS#11 function from lib */
  42. CK_SLOT_ID slotId; /* Id of slot to use */
  43. CK_SESSION_HANDLE handle; /* Handle to active session */
  44. CK_UTF8CHAR_PTR userPin; /* User's PIN to login with */
  45. CK_ULONG userPinSz; /* Size of user's PIN in bytes */
  46. } Pkcs11Token;
  47. typedef struct Pkcs11Session {
  48. CK_FUNCTION_LIST* func; /* Table of PKCS#11 function from lib */
  49. CK_SLOT_ID slotId; /* Id of slot to use */
  50. CK_SESSION_HANDLE handle; /* Handle to active session */
  51. } Pkcs11Session;
  52. /* Types of keys that can be stored. */
  53. enum Pkcs11KeyType {
  54. PKCS11_KEY_TYPE_AES_GCM,
  55. PKCS11_KEY_TYPE_AES_CBC,
  56. PKCS11_KEY_TYPE_HMAC,
  57. PKCS11_KEY_TYPE_RSA,
  58. PKCS11_KEY_TYPE_EC,
  59. };
  60. WOLFSSL_API int wc_Pkcs11_Initialize(Pkcs11Dev* dev, const char* library,
  61. void* heap);
  62. WOLFSSL_API void wc_Pkcs11_Finalize(Pkcs11Dev* dev);
  63. WOLFSSL_API int wc_Pkcs11Token_Init(Pkcs11Token* token, Pkcs11Dev* dev,
  64. int slotId, const char* tokenName, const unsigned char *userPin,
  65. int userPinSz);
  66. WOLFSSL_API int wc_Pkcs11Token_InitName(Pkcs11Token* token, Pkcs11Dev* dev,
  67. const char* tokenName, int tokenSz,
  68. const unsigned char* userPin, int userPinSz);
  69. WOLFSSL_API void wc_Pkcs11Token_Final(Pkcs11Token* token);
  70. WOLFSSL_API int wc_Pkcs11Token_Open(Pkcs11Token* token, int readWrite);
  71. WOLFSSL_API void wc_Pkcs11Token_Close(Pkcs11Token* token);
  72. WOLFSSL_API int wc_Pkcs11StoreKey(Pkcs11Token* token, int type, int clear,
  73. void* key);
  74. WOLFSSL_API int wc_Pkcs11_CryptoDevCb(int devId, wc_CryptoInfo* info,
  75. void* ctx);
  76. #ifdef __cplusplus
  77. } /* extern "C" */
  78. #endif
  79. #endif /* HAVE_PKCS11 */
  80. #endif /* _WOLFPKCS11_H_ */