pkcs11_library.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /*
  2. * Copyright (C) 2011 Tobias Brunner
  3. * HSR Hochschule fuer Technik Rapperswil
  4. *
  5. * Copyright (C) 2010 Martin Willi
  6. * Copyright (C) 2010 revosec AG
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms of the GNU General Public License as published by the
  10. * Free Software Foundation; either version 2 of the License, or (at your
  11. * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
  12. *
  13. * This program is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  15. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  16. * for more details.
  17. */
  18. /**
  19. * @defgroup pkcs11_library pkcs11_library
  20. * @{ @ingroup pkcs11
  21. */
  22. #ifndef PKCS11_LIBRARY_H_
  23. #define PKCS11_LIBRARY_H_
  24. typedef enum pkcs11_feature_t pkcs11_feature_t;
  25. typedef struct pkcs11_library_t pkcs11_library_t;
  26. #include "pkcs11.h"
  27. #include <utils/utils.h>
  28. #include <utils/chunk.h>
  29. #include <collections/enumerator.h>
  30. /**
  31. * Optional PKCS#11 features some libraries support, some not
  32. */
  33. enum pkcs11_feature_t {
  34. /** CKA_TRUSTED attribute supported for certificate objects */
  35. PKCS11_TRUSTED_CERTS = (1<<0),
  36. /** CKA_ALWAYS_AUTHENTICATE attribute supported for private keys */
  37. PKCS11_ALWAYS_AUTH_KEYS = (1<<1),
  38. };
  39. /**
  40. * A loaded and initialized PKCS#11 library.
  41. */
  42. struct pkcs11_library_t {
  43. /**
  44. * PKCS#11 function list, as returned by C_GetFunctionList
  45. */
  46. CK_FUNCTION_LIST_PTR f;
  47. /**
  48. * Get the name this instance was created with.
  49. *
  50. * @return name, as passed to constructor
  51. */
  52. char* (*get_name)(pkcs11_library_t *this);
  53. /**
  54. * Get the feature set supported by this library.
  55. *
  56. * @return ORed set of features supported
  57. */
  58. pkcs11_feature_t (*get_features)(pkcs11_library_t *this);
  59. /**
  60. * Create an enumerator over CK_OBJECT_HANDLE using a search template.
  61. *
  62. * An optional attribute array is automatically filled in with the
  63. * objects associated attributes. If the value of an output attribute
  64. * is NULL, the value gets allocated/freed during enumeration.
  65. *
  66. * @param session session to use
  67. * @param tmpl search template
  68. * @param tcount number of attributes in the search template
  69. * @param attr attributes to read from object
  70. * @param acount number of attributes to read
  71. */
  72. enumerator_t* (*create_object_enumerator)(pkcs11_library_t *this,
  73. CK_SESSION_HANDLE session, CK_ATTRIBUTE_PTR tmpl, CK_ULONG tcount,
  74. CK_ATTRIBUTE_PTR attr, CK_ULONG acount);
  75. /**
  76. * This is very similar to the object enumerator but is only used to
  77. * easily retrieve multiple attributes from a single object for which
  78. * a handle is already known.
  79. *
  80. * The given attribute array is automatically filled in with the
  81. * associated attributes. If the value of an output attribute is NULL,
  82. * the required memory gets allocated/freed during enumeration.
  83. *
  84. * @param session session to use
  85. * @param object object handle
  86. * @param attr attributes to read from object
  87. * @param count number of attributes to read
  88. */
  89. enumerator_t* (*create_object_attr_enumerator)(pkcs11_library_t *this,
  90. CK_SESSION_HANDLE session, CK_OBJECT_HANDLE object,
  91. CK_ATTRIBUTE_PTR attr, CK_ULONG count);
  92. /**
  93. * Create an enumerator over supported mechanisms of a token.
  94. *
  95. * The resulting enumerator enumerates over the mechanism type, and if
  96. * a non-NULL pointer is given, over the mechanism info details.
  97. *
  98. * @param slot slot of the token
  99. * @return enumerator over (CK_MECHANISM_TYPE, CK_MECHANISM_INFO)
  100. */
  101. enumerator_t* (*create_mechanism_enumerator)(pkcs11_library_t *this,
  102. CK_SLOT_ID slot);
  103. /**
  104. * Retrieve a single attribute from the given object.
  105. *
  106. * Memory for the data is allocated.
  107. *
  108. * @param session session with the PKCS#11 library
  109. * @param obj object handle
  110. * @param type attribute type to extract
  111. * @param data extracted data
  112. * @return TRUE if successful
  113. */
  114. bool (*get_ck_attribute)(pkcs11_library_t *this, CK_SESSION_HANDLE session,
  115. CK_OBJECT_HANDLE obj, CK_ATTRIBUTE_TYPE type,
  116. chunk_t *data);
  117. /**
  118. * Destroy a pkcs11_library_t.
  119. */
  120. void (*destroy)(pkcs11_library_t *this);
  121. };
  122. /**
  123. * Enum names for CK_RV return values
  124. */
  125. extern enum_name_t *ck_rv_names;
  126. /**
  127. * Enum names for CK_MECHANISM_TYPE values
  128. */
  129. extern enum_name_t *ck_mech_names;
  130. /**
  131. * Enum names for CK_ATTRIBUTE_TYPE values
  132. */
  133. extern enum_name_t *ck_attr_names;
  134. /**
  135. * Trim/null terminate a string returned by the various PKCS#11 functions.
  136. *
  137. * @param str string to trim
  138. * @param len max length of the string
  139. */
  140. void pkcs11_library_trim(char *str, int len);
  141. /**
  142. * Create a pkcs11_library instance.
  143. *
  144. * @param name an arbitrary name (for debugging), cloned
  145. * @param file pkcs11 library file to dlopen()
  146. * @param os_lock enforce OS Locking for this library
  147. * @return library abstraction
  148. */
  149. pkcs11_library_t *pkcs11_library_create(char *name, char *file, bool os_lock);
  150. #endif /** PKCS11_LIBRARY_H_ @}*/