pkcs11.c 6.1 KB

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