pkcs11.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /**
  2. * \file pkcs11.h
  3. *
  4. * \brief Wrapper for PKCS#11 library libpkcs11-helper
  5. *
  6. * \author Adriaan de Jong <dejong@fox-it.com>
  7. */
  8. /*
  9. * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
  10. * SPDX-License-Identifier: Apache-2.0
  11. *
  12. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  13. * not use this file except in compliance with the License.
  14. * You may obtain a copy of the License at
  15. *
  16. * http://www.apache.org/licenses/LICENSE-2.0
  17. *
  18. * Unless required by applicable law or agreed to in writing, software
  19. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  20. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  21. * See the License for the specific language governing permissions and
  22. * limitations under the License.
  23. *
  24. * This file is part of mbed TLS (https://tls.mbed.org)
  25. */
  26. #ifndef MBEDTLS_PKCS11_H
  27. #define MBEDTLS_PKCS11_H
  28. #if !defined(MBEDTLS_CONFIG_FILE)
  29. #include "config.h"
  30. #else
  31. #include MBEDTLS_CONFIG_FILE
  32. #endif
  33. #if defined(MBEDTLS_PKCS11_C)
  34. #include "x509_crt.h"
  35. #include <pkcs11-helper-1.0/pkcs11h-certificate.h>
  36. #if ( defined(__ARMCC_VERSION) || defined(_MSC_VER) ) && \
  37. !defined(inline) && !defined(__cplusplus)
  38. #define inline __inline
  39. #endif
  40. #ifdef __cplusplus
  41. extern "C" {
  42. #endif
  43. /**
  44. * Context for PKCS #11 private keys.
  45. */
  46. typedef struct mbedtls_pkcs11_context
  47. {
  48. pkcs11h_certificate_t pkcs11h_cert;
  49. int len;
  50. } mbedtls_pkcs11_context;
  51. /**
  52. * Initialize a mbedtls_pkcs11_context.
  53. * (Just making memory references valid.)
  54. */
  55. void mbedtls_pkcs11_init( mbedtls_pkcs11_context *ctx );
  56. /**
  57. * Fill in a mbed TLS certificate, based on the given PKCS11 helper certificate.
  58. *
  59. * \param cert X.509 certificate to fill
  60. * \param pkcs11h_cert PKCS #11 helper certificate
  61. *
  62. * \return 0 on success.
  63. */
  64. int mbedtls_pkcs11_x509_cert_bind( mbedtls_x509_crt *cert, pkcs11h_certificate_t pkcs11h_cert );
  65. /**
  66. * Set up a mbedtls_pkcs11_context storing the given certificate. Note that the
  67. * mbedtls_pkcs11_context will take over control of the certificate, freeing it when
  68. * done.
  69. *
  70. * \param priv_key Private key structure to fill.
  71. * \param pkcs11_cert PKCS #11 helper certificate
  72. *
  73. * \return 0 on success
  74. */
  75. int mbedtls_pkcs11_priv_key_bind( mbedtls_pkcs11_context *priv_key,
  76. pkcs11h_certificate_t pkcs11_cert );
  77. /**
  78. * Free the contents of the given private key context. Note that the structure
  79. * itself is not freed.
  80. *
  81. * \param priv_key Private key structure to cleanup
  82. */
  83. void mbedtls_pkcs11_priv_key_free( mbedtls_pkcs11_context *priv_key );
  84. /**
  85. * \brief Do an RSA private key decrypt, then remove the message
  86. * padding
  87. *
  88. * \param ctx PKCS #11 context
  89. * \param mode must be MBEDTLS_RSA_PRIVATE, for compatibility with rsa.c's signature
  90. * \param input buffer holding the encrypted data
  91. * \param output buffer that will hold the plaintext
  92. * \param olen will contain the plaintext length
  93. * \param output_max_len maximum length of the output buffer
  94. *
  95. * \return 0 if successful, or an MBEDTLS_ERR_RSA_XXX error code
  96. *
  97. * \note The output buffer must be as large as the size
  98. * of ctx->N (eg. 128 bytes if RSA-1024 is used) otherwise
  99. * an error is thrown.
  100. */
  101. int mbedtls_pkcs11_decrypt( mbedtls_pkcs11_context *ctx,
  102. int mode, size_t *olen,
  103. const unsigned char *input,
  104. unsigned char *output,
  105. size_t output_max_len );
  106. /**
  107. * \brief Do a private RSA to sign a message digest
  108. *
  109. * \param ctx PKCS #11 context
  110. * \param mode must be MBEDTLS_RSA_PRIVATE, for compatibility with rsa.c's signature
  111. * \param md_alg a MBEDTLS_MD_XXX (use MBEDTLS_MD_NONE for signing raw data)
  112. * \param hashlen message digest length (for MBEDTLS_MD_NONE only)
  113. * \param hash buffer holding the message digest
  114. * \param sig buffer that will hold the ciphertext
  115. *
  116. * \return 0 if the signing operation was successful,
  117. * or an MBEDTLS_ERR_RSA_XXX error code
  118. *
  119. * \note The "sig" buffer must be as large as the size
  120. * of ctx->N (eg. 128 bytes if RSA-1024 is used).
  121. */
  122. int mbedtls_pkcs11_sign( mbedtls_pkcs11_context *ctx,
  123. int mode,
  124. mbedtls_md_type_t md_alg,
  125. unsigned int hashlen,
  126. const unsigned char *hash,
  127. unsigned char *sig );
  128. /**
  129. * SSL/TLS wrappers for PKCS#11 functions
  130. */
  131. static inline int mbedtls_ssl_pkcs11_decrypt( void *ctx, int mode, size_t *olen,
  132. const unsigned char *input, unsigned char *output,
  133. size_t output_max_len )
  134. {
  135. return mbedtls_pkcs11_decrypt( (mbedtls_pkcs11_context *) ctx, mode, olen, input, output,
  136. output_max_len );
  137. }
  138. static inline int mbedtls_ssl_pkcs11_sign( void *ctx,
  139. int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
  140. int mode, mbedtls_md_type_t md_alg, unsigned int hashlen,
  141. const unsigned char *hash, unsigned char *sig )
  142. {
  143. ((void) f_rng);
  144. ((void) p_rng);
  145. return mbedtls_pkcs11_sign( (mbedtls_pkcs11_context *) ctx, mode, md_alg,
  146. hashlen, hash, sig );
  147. }
  148. static inline size_t mbedtls_ssl_pkcs11_key_len( void *ctx )
  149. {
  150. return ( (mbedtls_pkcs11_context *) ctx )->len;
  151. }
  152. #ifdef __cplusplus
  153. }
  154. #endif
  155. #endif /* MBEDTLS_PKCS11_C */
  156. #endif /* MBEDTLS_PKCS11_H */