pkcs11.c 6.2 KB

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