pkcs11.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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-2010, Brainspark B.V.
  9. *
  10. * This file is part of PolarSSL (http://www.polarssl.org)
  11. * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
  12. *
  13. * All rights reserved.
  14. *
  15. * This program is free software; you can redistribute it and/or modify
  16. * it under the terms of the GNU General Public License as published by
  17. * the Free Software Foundation; either version 2 of the License, or
  18. * (at your option) any later version.
  19. *
  20. * This program is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU General Public License along
  26. * with this program; if not, write to the Free Software Foundation, Inc.,
  27. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  28. */
  29. #ifndef PKCS11_H_
  30. #define PKCS11_H_
  31. #include "config.h"
  32. #if defined(POLARSSL_PKCS11_C)
  33. #include "polarssl/x509.h"
  34. #include <pkcs11-helper-1.0/pkcs11h-certificate.h>
  35. /**
  36. * Context for PKCS #11 private keys.
  37. */
  38. typedef struct {
  39. pkcs11h_certificate_t pkcs11h_cert;
  40. int len;
  41. } pkcs11_context;
  42. /**
  43. * Fill in a PolarSSL certificate, based on the given PKCS11 helper certificate.
  44. *
  45. * \param cert X.509 certificate to fill
  46. * \param pkcs11h_cert PKCS #11 helper certificate
  47. *
  48. * \return 0 on success.
  49. */
  50. int pkcs11_x509_cert_init( x509_cert *cert, pkcs11h_certificate_t pkcs11h_cert );
  51. /**
  52. * Initialise a pkcs11_context, storing the given certificate. Note that the
  53. * pkcs11_context will take over control of the certificate, freeing it when
  54. * done.
  55. *
  56. * \param priv_key Private key structure to fill.
  57. * \param pkcs11_cert PKCS #11 helper certificate
  58. *
  59. * \return 0 on success
  60. */
  61. int pkcs11_priv_key_init( pkcs11_context *priv_key,
  62. pkcs11h_certificate_t pkcs11_cert );
  63. /**
  64. * Free the contents of the given private key context. Note that the structure
  65. * itself is not freed.
  66. *
  67. * \param priv_key Private key structure to cleanup
  68. */
  69. void pkcs11_priv_key_free( pkcs11_context *priv_key );
  70. /**
  71. * \brief Do an RSA private key decrypt, then remove the message padding
  72. *
  73. * \param ctx PKCS #11 context
  74. * \param mode must be RSA_PRIVATE, for compatibility with rsa.c's signature
  75. * \param input buffer holding the encrypted data
  76. * \param output buffer that will hold the plaintext
  77. * \param olen will contain the plaintext length
  78. * \param output_max_len maximum length of the output buffer
  79. *
  80. * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code
  81. *
  82. * \note The output buffer must be as large as the size
  83. * of ctx->N (eg. 128 bytes if RSA-1024 is used) otherwise
  84. * an error is thrown.
  85. */
  86. int pkcs11_decrypt( pkcs11_context *ctx,
  87. int mode, size_t *olen,
  88. const unsigned char *input,
  89. unsigned char *output,
  90. unsigned int output_max_len );
  91. /**
  92. * \brief Do a private RSA to sign a message digest
  93. *
  94. * \param ctx PKCS #11 context
  95. * \param mode must be RSA_PRIVATE, for compatibility with rsa.c's signature
  96. * \param hash_id SIG_RSA_RAW, SIG_RSA_MD{2,4,5} or SIG_RSA_SHA{1,224,256,384,512}
  97. * \param hashlen message digest length (for SIG_RSA_RAW only)
  98. * \param hash buffer holding the message digest
  99. * \param sig buffer that will hold the ciphertext
  100. *
  101. * \return 0 if the signing operation was successful,
  102. * or an POLARSSL_ERR_RSA_XXX error code
  103. *
  104. * \note The "sig" buffer must be as large as the size
  105. * of ctx->N (eg. 128 bytes if RSA-1024 is used).
  106. */
  107. int pkcs11_sign( pkcs11_context *ctx,
  108. int mode,
  109. int hash_id,
  110. unsigned int hashlen,
  111. const unsigned char *hash,
  112. unsigned char *sig );
  113. #endif /* POLARSSL_PKCS11_C */
  114. #endif /* PKCS11_H_ */