pkcs11.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /**
  2. * \file pkcs11.h
  3. *
  4. * \brief Wrapper for PKCS#11 library libpkcs11-helper
  5. *
  6. * \author Adriaan de Jong <[email protected]>
  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 {
  47. pkcs11h_certificate_t pkcs11h_cert;
  48. int len;
  49. } mbedtls_pkcs11_context;
  50. /**
  51. * Initialize a mbedtls_pkcs11_context.
  52. * (Just making memory references valid.)
  53. */
  54. void mbedtls_pkcs11_init( mbedtls_pkcs11_context *ctx );
  55. /**
  56. * Fill in a mbed TLS certificate, based on the given PKCS11 helper certificate.
  57. *
  58. * \param cert X.509 certificate to fill
  59. * \param pkcs11h_cert PKCS #11 helper certificate
  60. *
  61. * \return 0 on success.
  62. */
  63. int mbedtls_pkcs11_x509_cert_bind( mbedtls_x509_crt *cert, pkcs11h_certificate_t pkcs11h_cert );
  64. /**
  65. * Set up a mbedtls_pkcs11_context storing the given certificate. Note that the
  66. * mbedtls_pkcs11_context will take over control of the certificate, freeing it when
  67. * done.
  68. *
  69. * \param priv_key Private key structure to fill.
  70. * \param pkcs11_cert PKCS #11 helper certificate
  71. *
  72. * \return 0 on success
  73. */
  74. int mbedtls_pkcs11_priv_key_bind( mbedtls_pkcs11_context *priv_key,
  75. pkcs11h_certificate_t pkcs11_cert );
  76. /**
  77. * Free the contents of the given private key context. Note that the structure
  78. * itself is not freed.
  79. *
  80. * \param priv_key Private key structure to cleanup
  81. */
  82. void mbedtls_pkcs11_priv_key_free( mbedtls_pkcs11_context *priv_key );
  83. /**
  84. * \brief Do an RSA private key decrypt, then remove the message
  85. * padding
  86. *
  87. * \param ctx PKCS #11 context
  88. * \param mode must be MBEDTLS_RSA_PRIVATE, for compatibility with rsa.c's signature
  89. * \param input buffer holding the encrypted data
  90. * \param output buffer that will hold the plaintext
  91. * \param olen will contain the plaintext length
  92. * \param output_max_len maximum length of the output buffer
  93. *
  94. * \return 0 if successful, or an MBEDTLS_ERR_RSA_XXX error code
  95. *
  96. * \note The output buffer must be as large as the size
  97. * of ctx->N (eg. 128 bytes if RSA-1024 is used) otherwise
  98. * an error is thrown.
  99. */
  100. int mbedtls_pkcs11_decrypt( mbedtls_pkcs11_context *ctx,
  101. int mode, size_t *olen,
  102. const unsigned char *input,
  103. unsigned char *output,
  104. size_t output_max_len );
  105. /**
  106. * \brief Do a private RSA to sign a message digest
  107. *
  108. * \param ctx PKCS #11 context
  109. * \param mode must be MBEDTLS_RSA_PRIVATE, for compatibility with rsa.c's signature
  110. * \param md_alg a MBEDTLS_MD_XXX (use MBEDTLS_MD_NONE for signing raw data)
  111. * \param hashlen message digest length (for MBEDTLS_MD_NONE only)
  112. * \param hash buffer holding the message digest
  113. * \param sig buffer that will hold the ciphertext
  114. *
  115. * \return 0 if the signing operation was successful,
  116. * or an MBEDTLS_ERR_RSA_XXX error code
  117. *
  118. * \note The "sig" buffer must be as large as the size
  119. * of ctx->N (eg. 128 bytes if RSA-1024 is used).
  120. */
  121. int mbedtls_pkcs11_sign( mbedtls_pkcs11_context *ctx,
  122. int mode,
  123. mbedtls_md_type_t md_alg,
  124. unsigned int hashlen,
  125. const unsigned char *hash,
  126. unsigned char *sig );
  127. /**
  128. * SSL/TLS wrappers for PKCS#11 functions
  129. */
  130. static inline int mbedtls_ssl_pkcs11_decrypt( void *ctx, int mode, size_t *olen,
  131. const unsigned char *input, unsigned char *output,
  132. size_t output_max_len )
  133. {
  134. return mbedtls_pkcs11_decrypt( (mbedtls_pkcs11_context *) ctx, mode, olen, input, output,
  135. output_max_len );
  136. }
  137. static inline int mbedtls_ssl_pkcs11_sign( void *ctx,
  138. int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
  139. int mode, mbedtls_md_type_t md_alg, unsigned int hashlen,
  140. const unsigned char *hash, unsigned char *sig )
  141. {
  142. ((void) f_rng);
  143. ((void) p_rng);
  144. return mbedtls_pkcs11_sign( (mbedtls_pkcs11_context *) ctx, mode, md_alg,
  145. hashlen, hash, sig );
  146. }
  147. static inline size_t mbedtls_ssl_pkcs11_key_len( void *ctx )
  148. {
  149. return ( (mbedtls_pkcs11_context *) ctx )->len;
  150. }
  151. #ifdef __cplusplus
  152. }
  153. #endif
  154. #endif /* MBEDTLS_PKCS11_C */
  155. #endif /* MBEDTLS_PKCS11_H */