pkcs11.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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: Apache-2.0
  10. *
  11. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  12. * not use this file except in compliance with the License.
  13. * You may obtain a copy of the License at
  14. *
  15. * http://www.apache.org/licenses/LICENSE-2.0
  16. *
  17. * Unless required by applicable law or agreed to in writing, software
  18. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  19. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  20. * See the License for the specific language governing permissions and
  21. * limitations under the License.
  22. *
  23. * This file is part of mbed TLS (https://tls.mbed.org)
  24. */
  25. #include "mbedtls/pkcs11.h"
  26. #if defined(MBEDTLS_PKCS11_C)
  27. #include "mbedtls/md.h"
  28. #include "mbedtls/oid.h"
  29. #include "mbedtls/x509_crt.h"
  30. #if defined(MBEDTLS_PLATFORM_C)
  31. #include "mbedtls/platform.h"
  32. #else
  33. #include <stdlib.h>
  34. #define mbedtls_calloc calloc
  35. #define mbedtls_free free
  36. #endif
  37. #include <string.h>
  38. void mbedtls_pkcs11_init( mbedtls_pkcs11_context *ctx )
  39. {
  40. memset( ctx, 0, sizeof( mbedtls_pkcs11_context ) );
  41. }
  42. int mbedtls_pkcs11_x509_cert_bind( mbedtls_x509_crt *cert, pkcs11h_certificate_t pkcs11_cert )
  43. {
  44. int ret = 1;
  45. unsigned char *cert_blob = NULL;
  46. size_t cert_blob_size = 0;
  47. if( cert == NULL )
  48. {
  49. ret = 2;
  50. goto cleanup;
  51. }
  52. if( pkcs11h_certificate_getCertificateBlob( pkcs11_cert, NULL,
  53. &cert_blob_size ) != CKR_OK )
  54. {
  55. ret = 3;
  56. goto cleanup;
  57. }
  58. cert_blob = mbedtls_calloc( 1, cert_blob_size );
  59. if( NULL == cert_blob )
  60. {
  61. ret = 4;
  62. goto cleanup;
  63. }
  64. if( pkcs11h_certificate_getCertificateBlob( pkcs11_cert, cert_blob,
  65. &cert_blob_size ) != CKR_OK )
  66. {
  67. ret = 5;
  68. goto cleanup;
  69. }
  70. if( 0 != mbedtls_x509_crt_parse( cert, cert_blob, cert_blob_size ) )
  71. {
  72. ret = 6;
  73. goto cleanup;
  74. }
  75. ret = 0;
  76. cleanup:
  77. if( NULL != cert_blob )
  78. mbedtls_free( cert_blob );
  79. return( ret );
  80. }
  81. int mbedtls_pkcs11_priv_key_bind( mbedtls_pkcs11_context *priv_key,
  82. pkcs11h_certificate_t pkcs11_cert )
  83. {
  84. int ret = 1;
  85. mbedtls_x509_crt cert;
  86. mbedtls_x509_crt_init( &cert );
  87. if( priv_key == NULL )
  88. goto cleanup;
  89. if( 0 != mbedtls_pkcs11_x509_cert_bind( &cert, pkcs11_cert ) )
  90. goto cleanup;
  91. priv_key->len = mbedtls_pk_get_len( &cert.pk );
  92. priv_key->pkcs11h_cert = pkcs11_cert;
  93. ret = 0;
  94. cleanup:
  95. mbedtls_x509_crt_free( &cert );
  96. return( ret );
  97. }
  98. void mbedtls_pkcs11_priv_key_free( mbedtls_pkcs11_context *priv_key )
  99. {
  100. if( NULL != priv_key )
  101. pkcs11h_certificate_freeCertificate( priv_key->pkcs11h_cert );
  102. }
  103. int mbedtls_pkcs11_decrypt( mbedtls_pkcs11_context *ctx,
  104. int mode, size_t *olen,
  105. const unsigned char *input,
  106. unsigned char *output,
  107. size_t output_max_len )
  108. {
  109. size_t input_len, output_len;
  110. if( NULL == ctx )
  111. return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
  112. if( MBEDTLS_RSA_PRIVATE != mode )
  113. return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
  114. output_len = input_len = ctx->len;
  115. if( input_len < 16 || input_len > output_max_len )
  116. return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
  117. /* Determine size of output buffer */
  118. if( pkcs11h_certificate_decryptAny( ctx->pkcs11h_cert, CKM_RSA_PKCS, input,
  119. input_len, NULL, &output_len ) != CKR_OK )
  120. {
  121. return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
  122. }
  123. if( output_len > output_max_len )
  124. return( MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE );
  125. if( pkcs11h_certificate_decryptAny( ctx->pkcs11h_cert, CKM_RSA_PKCS, input,
  126. input_len, output, &output_len ) != CKR_OK )
  127. {
  128. return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
  129. }
  130. *olen = output_len;
  131. return( 0 );
  132. }
  133. int mbedtls_pkcs11_sign( mbedtls_pkcs11_context *ctx,
  134. int mode,
  135. mbedtls_md_type_t md_alg,
  136. unsigned int hashlen,
  137. const unsigned char *hash,
  138. unsigned char *sig )
  139. {
  140. size_t sig_len = 0, asn_len = 0, oid_size = 0;
  141. unsigned char *p = sig;
  142. const char *oid;
  143. if( NULL == ctx )
  144. return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
  145. if( MBEDTLS_RSA_PRIVATE != mode )
  146. return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
  147. if( md_alg != MBEDTLS_MD_NONE )
  148. {
  149. const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
  150. if( md_info == NULL )
  151. return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
  152. if( mbedtls_oid_get_oid_by_md( md_alg, &oid, &oid_size ) != 0 )
  153. return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
  154. hashlen = mbedtls_md_get_size( md_info );
  155. asn_len = 10 + oid_size;
  156. }
  157. sig_len = ctx->len;
  158. if( hashlen > sig_len || asn_len > sig_len ||
  159. hashlen + asn_len > sig_len )
  160. {
  161. return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
  162. }
  163. if( md_alg != MBEDTLS_MD_NONE )
  164. {
  165. /*
  166. * DigestInfo ::= SEQUENCE {
  167. * digestAlgorithm DigestAlgorithmIdentifier,
  168. * digest Digest }
  169. *
  170. * DigestAlgorithmIdentifier ::= AlgorithmIdentifier
  171. *
  172. * Digest ::= OCTET STRING
  173. */
  174. *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
  175. *p++ = (unsigned char) ( 0x08 + oid_size + hashlen );
  176. *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
  177. *p++ = (unsigned char) ( 0x04 + oid_size );
  178. *p++ = MBEDTLS_ASN1_OID;
  179. *p++ = oid_size & 0xFF;
  180. memcpy( p, oid, oid_size );
  181. p += oid_size;
  182. *p++ = MBEDTLS_ASN1_NULL;
  183. *p++ = 0x00;
  184. *p++ = MBEDTLS_ASN1_OCTET_STRING;
  185. *p++ = hashlen;
  186. }
  187. memcpy( p, hash, hashlen );
  188. if( pkcs11h_certificate_signAny( ctx->pkcs11h_cert, CKM_RSA_PKCS, sig,
  189. asn_len + hashlen, sig, &sig_len ) != CKR_OK )
  190. {
  191. return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
  192. }
  193. return( 0 );
  194. }
  195. #endif /* defined(MBEDTLS_PKCS11_C) */