pkcs11.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. /**
  2. * \file pkcs11.c
  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. #include "mbedtls/pkcs11.h"
  28. #if defined(MBEDTLS_PKCS11_C)
  29. #include "mbedtls/md.h"
  30. #include "mbedtls/oid.h"
  31. #include "mbedtls/x509_crt.h"
  32. #if defined(MBEDTLS_PLATFORM_C)
  33. #include "mbedtls/platform.h"
  34. #else
  35. #include <stdlib.h>
  36. #define mbedtls_calloc calloc
  37. #define mbedtls_free free
  38. #endif
  39. #include <string.h>
  40. void mbedtls_pkcs11_init( mbedtls_pkcs11_context *ctx )
  41. {
  42. memset( ctx, 0, sizeof( mbedtls_pkcs11_context ) );
  43. }
  44. int mbedtls_pkcs11_x509_cert_bind( mbedtls_x509_crt *cert, pkcs11h_certificate_t pkcs11_cert )
  45. {
  46. int ret = 1;
  47. unsigned char *cert_blob = NULL;
  48. size_t cert_blob_size = 0;
  49. if( cert == NULL )
  50. {
  51. ret = 2;
  52. goto cleanup;
  53. }
  54. if( pkcs11h_certificate_getCertificateBlob( pkcs11_cert, NULL,
  55. &cert_blob_size ) != CKR_OK )
  56. {
  57. ret = 3;
  58. goto cleanup;
  59. }
  60. cert_blob = mbedtls_calloc( 1, cert_blob_size );
  61. if( NULL == cert_blob )
  62. {
  63. ret = 4;
  64. goto cleanup;
  65. }
  66. if( pkcs11h_certificate_getCertificateBlob( pkcs11_cert, cert_blob,
  67. &cert_blob_size ) != CKR_OK )
  68. {
  69. ret = 5;
  70. goto cleanup;
  71. }
  72. if( 0 != mbedtls_x509_crt_parse( cert, cert_blob, cert_blob_size ) )
  73. {
  74. ret = 6;
  75. goto cleanup;
  76. }
  77. ret = 0;
  78. cleanup:
  79. if( NULL != cert_blob )
  80. mbedtls_free( cert_blob );
  81. return( ret );
  82. }
  83. int mbedtls_pkcs11_priv_key_bind( mbedtls_pkcs11_context *priv_key,
  84. pkcs11h_certificate_t pkcs11_cert )
  85. {
  86. int ret = 1;
  87. mbedtls_x509_crt cert;
  88. mbedtls_x509_crt_init( &cert );
  89. if( priv_key == NULL )
  90. goto cleanup;
  91. if( 0 != mbedtls_pkcs11_x509_cert_bind( &cert, pkcs11_cert ) )
  92. goto cleanup;
  93. priv_key->len = mbedtls_pk_get_len( &cert.pk );
  94. priv_key->pkcs11h_cert = pkcs11_cert;
  95. ret = 0;
  96. cleanup:
  97. mbedtls_x509_crt_free( &cert );
  98. return( ret );
  99. }
  100. void mbedtls_pkcs11_priv_key_free( mbedtls_pkcs11_context *priv_key )
  101. {
  102. if( NULL != priv_key )
  103. pkcs11h_certificate_freeCertificate( priv_key->pkcs11h_cert );
  104. }
  105. int mbedtls_pkcs11_decrypt( mbedtls_pkcs11_context *ctx,
  106. int mode, size_t *olen,
  107. const unsigned char *input,
  108. unsigned char *output,
  109. size_t output_max_len )
  110. {
  111. size_t input_len, output_len;
  112. if( NULL == ctx )
  113. return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
  114. if( MBEDTLS_RSA_PRIVATE != mode )
  115. return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
  116. output_len = input_len = ctx->len;
  117. if( input_len < 16 || input_len > output_max_len )
  118. return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
  119. /* Determine size of output buffer */
  120. if( pkcs11h_certificate_decryptAny( ctx->pkcs11h_cert, CKM_RSA_PKCS, input,
  121. input_len, NULL, &output_len ) != CKR_OK )
  122. {
  123. return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
  124. }
  125. if( output_len > output_max_len )
  126. return( MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE );
  127. if( pkcs11h_certificate_decryptAny( ctx->pkcs11h_cert, CKM_RSA_PKCS, input,
  128. input_len, output, &output_len ) != CKR_OK )
  129. {
  130. return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
  131. }
  132. *olen = output_len;
  133. return( 0 );
  134. }
  135. int mbedtls_pkcs11_sign( mbedtls_pkcs11_context *ctx,
  136. int mode,
  137. mbedtls_md_type_t md_alg,
  138. unsigned int hashlen,
  139. const unsigned char *hash,
  140. unsigned char *sig )
  141. {
  142. size_t sig_len = 0, asn_len = 0, oid_size = 0;
  143. unsigned char *p = sig;
  144. const char *oid;
  145. if( NULL == ctx )
  146. return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
  147. if( MBEDTLS_RSA_PRIVATE != mode )
  148. return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
  149. if( md_alg != MBEDTLS_MD_NONE )
  150. {
  151. const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
  152. if( md_info == NULL )
  153. return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
  154. if( mbedtls_oid_get_oid_by_md( md_alg, &oid, &oid_size ) != 0 )
  155. return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
  156. hashlen = mbedtls_md_get_size( md_info );
  157. asn_len = 10 + oid_size;
  158. }
  159. sig_len = ctx->len;
  160. if( hashlen > sig_len || asn_len > sig_len ||
  161. hashlen + asn_len > sig_len )
  162. {
  163. return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
  164. }
  165. if( md_alg != MBEDTLS_MD_NONE )
  166. {
  167. /*
  168. * DigestInfo ::= SEQUENCE {
  169. * digestAlgorithm DigestAlgorithmIdentifier,
  170. * digest Digest }
  171. *
  172. * DigestAlgorithmIdentifier ::= AlgorithmIdentifier
  173. *
  174. * Digest ::= OCTET STRING
  175. */
  176. *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
  177. *p++ = (unsigned char) ( 0x08 + oid_size + hashlen );
  178. *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
  179. *p++ = (unsigned char) ( 0x04 + oid_size );
  180. *p++ = MBEDTLS_ASN1_OID;
  181. *p++ = oid_size & 0xFF;
  182. memcpy( p, oid, oid_size );
  183. p += oid_size;
  184. *p++ = MBEDTLS_ASN1_NULL;
  185. *p++ = 0x00;
  186. *p++ = MBEDTLS_ASN1_OCTET_STRING;
  187. *p++ = hashlen;
  188. }
  189. memcpy( p, hash, hashlen );
  190. if( pkcs11h_certificate_signAny( ctx->pkcs11h_cert, CKM_RSA_PKCS, sig,
  191. asn_len + hashlen, sig, &sig_len ) != CKR_OK )
  192. {
  193. return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
  194. }
  195. return( 0 );
  196. }
  197. #endif /* defined(MBEDTLS_PKCS11_C) */