pkcs11.c 7.1 KB

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