pkcs11.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
  9. * SPDX-License-Identifier: GPL-2.0
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or
  14. * (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License along
  22. * with this program; if not, write to the Free Software Foundation, Inc.,
  23. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  24. *
  25. * This file is part of mbed TLS (https://tls.mbed.org)
  26. */
  27. #ifndef MBEDTLS_PKCS11_H
  28. #define MBEDTLS_PKCS11_H
  29. #if !defined(MBEDTLS_CONFIG_FILE)
  30. #include "config.h"
  31. #else
  32. #include MBEDTLS_CONFIG_FILE
  33. #endif
  34. #if defined(MBEDTLS_PKCS11_C)
  35. #include "x509_crt.h"
  36. #include <pkcs11-helper-1.0/pkcs11h-certificate.h>
  37. #if ( defined(__ARMCC_VERSION) || defined(_MSC_VER) ) && \
  38. !defined(inline) && !defined(__cplusplus)
  39. #define inline __inline
  40. #endif
  41. #ifdef __cplusplus
  42. extern "C" {
  43. #endif
  44. /**
  45. * Context for PKCS #11 private keys.
  46. */
  47. typedef struct {
  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 */