pkcs11_private.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. #ifndef AWS_IO_PKCS11_PRIVATE_H
  2. #define AWS_IO_PKCS11_PRIVATE_H
  3. /**
  4. * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
  5. * SPDX-License-Identifier: Apache-2.0.
  6. */
  7. #include <aws/io/tls_channel_handler.h>
  8. /* These defines must exist before the official PKCS#11 headers are included */
  9. #define CK_PTR *
  10. #define NULL_PTR 0
  11. #define CK_DEFINE_FUNCTION(returnType, name) returnType name
  12. #define CK_DECLARE_FUNCTION(returnType, name) returnType name
  13. #define CK_DECLARE_FUNCTION_POINTER(returnType, name) returnType(CK_PTR name)
  14. #define CK_CALLBACK_FUNCTION(returnType, name) returnType(CK_PTR name)
  15. #include "pkcs11/v2.40/pkcs11.h"
  16. /**
  17. * pkcs11_private.h
  18. * This file declares symbols that are private to aws-c-io but need to be
  19. * accessed from multiple .c files.
  20. *
  21. * NOTE: Not putting this file under `include/private/...` like we usually
  22. * do with private headers because it breaks aws-crt-swift. Swift was trying
  23. * to compile each file under include/, but the official PKCS#11 header files
  24. * are too weird break it.
  25. */
  26. struct aws_pkcs11_lib;
  27. struct aws_pkcs11_tls_key_handler;
  28. struct aws_string;
  29. AWS_EXTERN_C_BEGIN
  30. /**
  31. * Return c-string for PKCS#11 CKR_* constant.
  32. * For use in tests only.
  33. */
  34. AWS_IO_API
  35. const char *aws_pkcs11_ckr_str(CK_RV rv);
  36. /**
  37. * Return the raw function list.
  38. * For use in tests only.
  39. */
  40. AWS_IO_API
  41. CK_FUNCTION_LIST *aws_pkcs11_lib_get_function_list(struct aws_pkcs11_lib *pkcs11_lib);
  42. /**
  43. * Find the slot that meets all criteria:
  44. * - has a token
  45. * - if match_slot_id is non-null, then slot IDs must match
  46. * - if match_token_label is non-null, then labels must match
  47. * The function fails unless it finds exactly one slot meeting all criteria.
  48. */
  49. AWS_IO_API
  50. int aws_pkcs11_lib_find_slot_with_token(
  51. struct aws_pkcs11_lib *pkcs11_lib,
  52. const uint64_t *match_slot_id,
  53. const struct aws_string *match_token_label,
  54. CK_SLOT_ID *out_slot_id);
  55. AWS_IO_API
  56. int aws_pkcs11_lib_open_session(
  57. struct aws_pkcs11_lib *pkcs11_lib,
  58. CK_SLOT_ID slot_id,
  59. CK_SESSION_HANDLE *out_session_handle);
  60. AWS_IO_API
  61. void aws_pkcs11_lib_close_session(struct aws_pkcs11_lib *pkcs11_lib, CK_SESSION_HANDLE session_handle);
  62. AWS_IO_API
  63. int aws_pkcs11_lib_login_user(
  64. struct aws_pkcs11_lib *pkcs11_lib,
  65. CK_SESSION_HANDLE session_handle,
  66. const struct aws_string *optional_user_pin);
  67. /**
  68. * Find the object that meets all criteria:
  69. * - is private key
  70. * - if match_label is non-null, then labels must match
  71. * The function fails unless it finds exactly one object meeting all criteria.
  72. */
  73. AWS_IO_API
  74. int aws_pkcs11_lib_find_private_key(
  75. struct aws_pkcs11_lib *pkcs11_lib,
  76. CK_SESSION_HANDLE session_handle,
  77. const struct aws_string *match_label,
  78. CK_OBJECT_HANDLE *out_key_handle,
  79. CK_KEY_TYPE *out_key_type);
  80. /**
  81. * Decrypt the encrypted data.
  82. * out_data should be passed in uninitialized.
  83. * If successful, out_data will be initialized and contain the recovered data.
  84. */
  85. AWS_IO_API
  86. int aws_pkcs11_lib_decrypt(
  87. struct aws_pkcs11_lib *pkcs11_lib,
  88. CK_SESSION_HANDLE session_handle,
  89. CK_OBJECT_HANDLE key_handle,
  90. CK_KEY_TYPE key_type,
  91. struct aws_byte_cursor encrypted_data,
  92. struct aws_allocator *allocator,
  93. struct aws_byte_buf *out_data);
  94. /**
  95. * Sign a digest with the private key during TLS negotiation.
  96. * out_signature should be passed in uninitialized.
  97. * If successful, out_signature will be initialized and contain the signature.
  98. */
  99. AWS_IO_API
  100. int aws_pkcs11_lib_sign(
  101. struct aws_pkcs11_lib *pkcs11_lib,
  102. CK_SESSION_HANDLE session_handle,
  103. CK_OBJECT_HANDLE key_handle,
  104. CK_KEY_TYPE key_type,
  105. struct aws_byte_cursor digest_data,
  106. struct aws_allocator *allocator,
  107. enum aws_tls_hash_algorithm digest_alg,
  108. enum aws_tls_signature_algorithm signature_alg,
  109. struct aws_byte_buf *out_signature);
  110. /**
  111. * Get the DER encoded DigestInfo value to be prefixed to the hash, used for RSA signing
  112. * See https://tools.ietf.org/html/rfc3447#page-43
  113. */
  114. AWS_IO_API
  115. int aws_get_prefix_to_rsa_sig(enum aws_tls_hash_algorithm digest_alg, struct aws_byte_cursor *out_prefix);
  116. /**
  117. * ASN.1 DER encode a big unsigned integer. Note that the source integer may be zero padded. It may also have
  118. * most significant bit set. The encoded format is canonical and unambiguous - that is, most significant
  119. * bit is never set.
  120. */
  121. AWS_IO_API
  122. int aws_pkcs11_asn1_enc_ubigint(struct aws_byte_buf *const buffer, struct aws_byte_cursor bigint);
  123. /**
  124. * Creates a new PKCS11 TLS operation handler with an associated aws_custom_key_op_handler
  125. * with a reference count set to 1.
  126. *
  127. * The PKCS11 TLS operation handler will automatically be destroyed when the reference count reaches zero
  128. * on the aws_custom_key_op_handler.
  129. */
  130. AWS_IO_API
  131. struct aws_custom_key_op_handler *aws_pkcs11_tls_op_handler_new(
  132. struct aws_allocator *allocator,
  133. struct aws_pkcs11_lib *pkcs11_lib,
  134. const struct aws_byte_cursor *user_pin,
  135. const struct aws_byte_cursor *match_token_label,
  136. const struct aws_byte_cursor *match_private_key_label,
  137. const uint64_t *match_slot_id);
  138. AWS_EXTERN_C_END
  139. #endif /* AWS_IO_PKCS11_PRIVATE_H */