Paul Bakker | 43b7e35 | 2011-01-18 15:27:19 +0000 | [diff] [blame] | 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 | * |
Bence Szépkúti | 1e14827 | 2020-08-07 13:07:28 +0200 | [diff] [blame] | 8 | * Copyright The Mbed TLS Contributors |
Manuel Pégourié-Gonnard | 37ff140 | 2015-09-04 14:21:07 +0200 | [diff] [blame] | 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. |
Paul Bakker | 43b7e35 | 2011-01-18 15:27:19 +0000 | [diff] [blame] | 22 | */ |
| 23 | |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 24 | #include "mbedtls/pkcs11.h" |
Paul Bakker | 43b7e35 | 2011-01-18 15:27:19 +0000 | [diff] [blame] | 25 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 26 | #if defined(MBEDTLS_PKCS11_C) |
Rich Evans | 00ab470 | 2015-02-06 13:43:58 +0000 | [diff] [blame] | 27 | |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 28 | #include "mbedtls/md.h" |
| 29 | #include "mbedtls/oid.h" |
| 30 | #include "mbedtls/x509_crt.h" |
Paul Bakker | 43b7e35 | 2011-01-18 15:27:19 +0000 | [diff] [blame] | 31 | |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 32 | #include "mbedtls/platform.h" |
Paul Bakker | 6e339b5 | 2013-07-03 13:37:05 +0200 | [diff] [blame] | 33 | |
Manuel Pégourié-Gonnard | 3a89559 | 2015-05-27 17:09:21 +0200 | [diff] [blame] | 34 | #include <string.h> |
| 35 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 36 | void mbedtls_pkcs11_init(mbedtls_pkcs11_context *ctx) |
Manuel Pégourié-Gonnard | eab147c | 2015-04-29 01:10:10 +0200 | [diff] [blame] | 37 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 38 | memset(ctx, 0, sizeof(mbedtls_pkcs11_context)); |
Manuel Pégourié-Gonnard | eab147c | 2015-04-29 01:10:10 +0200 | [diff] [blame] | 39 | } |
| 40 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 41 | int mbedtls_pkcs11_x509_cert_bind(mbedtls_x509_crt *cert, pkcs11h_certificate_t pkcs11_cert) |
Paul Bakker | 43b7e35 | 2011-01-18 15:27:19 +0000 | [diff] [blame] | 42 | { |
| 43 | int ret = 1; |
| 44 | unsigned char *cert_blob = NULL; |
| 45 | size_t cert_blob_size = 0; |
| 46 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 47 | if (cert == NULL) { |
Paul Bakker | 43b7e35 | 2011-01-18 15:27:19 +0000 | [diff] [blame] | 48 | ret = 2; |
| 49 | goto cleanup; |
| 50 | } |
| 51 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 52 | if (pkcs11h_certificate_getCertificateBlob(pkcs11_cert, NULL, |
| 53 | &cert_blob_size) != CKR_OK) { |
Paul Bakker | 43b7e35 | 2011-01-18 15:27:19 +0000 | [diff] [blame] | 54 | ret = 3; |
| 55 | goto cleanup; |
| 56 | } |
| 57 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 58 | cert_blob = mbedtls_calloc(1, cert_blob_size); |
| 59 | if (NULL == cert_blob) { |
Paul Bakker | 43b7e35 | 2011-01-18 15:27:19 +0000 | [diff] [blame] | 60 | ret = 4; |
| 61 | goto cleanup; |
| 62 | } |
| 63 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 64 | if (pkcs11h_certificate_getCertificateBlob(pkcs11_cert, cert_blob, |
| 65 | &cert_blob_size) != CKR_OK) { |
Paul Bakker | 43b7e35 | 2011-01-18 15:27:19 +0000 | [diff] [blame] | 66 | ret = 5; |
| 67 | goto cleanup; |
| 68 | } |
| 69 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 70 | if (0 != mbedtls_x509_crt_parse(cert, cert_blob, cert_blob_size)) { |
Paul Bakker | 43b7e35 | 2011-01-18 15:27:19 +0000 | [diff] [blame] | 71 | ret = 6; |
| 72 | goto cleanup; |
| 73 | } |
| 74 | |
| 75 | ret = 0; |
| 76 | |
| 77 | cleanup: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 78 | if (NULL != cert_blob) { |
| 79 | mbedtls_free(cert_blob); |
| 80 | } |
Paul Bakker | 43b7e35 | 2011-01-18 15:27:19 +0000 | [diff] [blame] | 81 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 82 | return ret; |
Paul Bakker | 43b7e35 | 2011-01-18 15:27:19 +0000 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 86 | int mbedtls_pkcs11_priv_key_bind(mbedtls_pkcs11_context *priv_key, |
| 87 | pkcs11h_certificate_t pkcs11_cert) |
Paul Bakker | 43b7e35 | 2011-01-18 15:27:19 +0000 | [diff] [blame] | 88 | { |
| 89 | int ret = 1; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 90 | mbedtls_x509_crt cert; |
Paul Bakker | 43b7e35 | 2011-01-18 15:27:19 +0000 | [diff] [blame] | 91 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 92 | mbedtls_x509_crt_init(&cert); |
Paul Bakker | 43b7e35 | 2011-01-18 15:27:19 +0000 | [diff] [blame] | 93 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 94 | if (priv_key == NULL) { |
Paul Bakker | 43b7e35 | 2011-01-18 15:27:19 +0000 | [diff] [blame] | 95 | goto cleanup; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 96 | } |
Paul Bakker | 43b7e35 | 2011-01-18 15:27:19 +0000 | [diff] [blame] | 97 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 98 | if (0 != mbedtls_pkcs11_x509_cert_bind(&cert, pkcs11_cert)) { |
Paul Bakker | 43b7e35 | 2011-01-18 15:27:19 +0000 | [diff] [blame] | 99 | goto cleanup; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 100 | } |
Paul Bakker | 43b7e35 | 2011-01-18 15:27:19 +0000 | [diff] [blame] | 101 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 102 | priv_key->len = mbedtls_pk_get_len(&cert.pk); |
Paul Bakker | 43b7e35 | 2011-01-18 15:27:19 +0000 | [diff] [blame] | 103 | priv_key->pkcs11h_cert = pkcs11_cert; |
| 104 | |
| 105 | ret = 0; |
| 106 | |
| 107 | cleanup: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 108 | mbedtls_x509_crt_free(&cert); |
Paul Bakker | 43b7e35 | 2011-01-18 15:27:19 +0000 | [diff] [blame] | 109 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 110 | return ret; |
Paul Bakker | 43b7e35 | 2011-01-18 15:27:19 +0000 | [diff] [blame] | 111 | } |
| 112 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 113 | void mbedtls_pkcs11_priv_key_free(mbedtls_pkcs11_context *priv_key) |
Paul Bakker | 43b7e35 | 2011-01-18 15:27:19 +0000 | [diff] [blame] | 114 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 115 | if (NULL != priv_key) { |
| 116 | pkcs11h_certificate_freeCertificate(priv_key->pkcs11h_cert); |
| 117 | } |
Paul Bakker | 43b7e35 | 2011-01-18 15:27:19 +0000 | [diff] [blame] | 118 | } |
| 119 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 120 | int mbedtls_pkcs11_decrypt(mbedtls_pkcs11_context *ctx, |
| 121 | int mode, size_t *olen, |
| 122 | const unsigned char *input, |
| 123 | unsigned char *output, |
| 124 | size_t output_max_len) |
Paul Bakker | 43b7e35 | 2011-01-18 15:27:19 +0000 | [diff] [blame] | 125 | { |
| 126 | size_t input_len, output_len; |
| 127 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 128 | if (NULL == ctx) { |
| 129 | return MBEDTLS_ERR_RSA_BAD_INPUT_DATA; |
| 130 | } |
Paul Bakker | 43b7e35 | 2011-01-18 15:27:19 +0000 | [diff] [blame] | 131 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 132 | if (MBEDTLS_RSA_PRIVATE != mode) { |
| 133 | return MBEDTLS_ERR_RSA_BAD_INPUT_DATA; |
| 134 | } |
Paul Bakker | 43b7e35 | 2011-01-18 15:27:19 +0000 | [diff] [blame] | 135 | |
| 136 | output_len = input_len = ctx->len; |
| 137 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 138 | if (input_len < 16 || input_len > output_max_len) { |
| 139 | return MBEDTLS_ERR_RSA_BAD_INPUT_DATA; |
| 140 | } |
Paul Bakker | 43b7e35 | 2011-01-18 15:27:19 +0000 | [diff] [blame] | 141 | |
| 142 | /* Determine size of output buffer */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 143 | if (pkcs11h_certificate_decryptAny(ctx->pkcs11h_cert, CKM_RSA_PKCS, input, |
| 144 | input_len, NULL, &output_len) != CKR_OK) { |
| 145 | return MBEDTLS_ERR_RSA_BAD_INPUT_DATA; |
Paul Bakker | 43b7e35 | 2011-01-18 15:27:19 +0000 | [diff] [blame] | 146 | } |
| 147 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 148 | if (output_len > output_max_len) { |
| 149 | return MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE; |
| 150 | } |
Paul Bakker | 43b7e35 | 2011-01-18 15:27:19 +0000 | [diff] [blame] | 151 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 152 | if (pkcs11h_certificate_decryptAny(ctx->pkcs11h_cert, CKM_RSA_PKCS, input, |
| 153 | input_len, output, &output_len) != CKR_OK) { |
| 154 | return MBEDTLS_ERR_RSA_BAD_INPUT_DATA; |
Paul Bakker | 43b7e35 | 2011-01-18 15:27:19 +0000 | [diff] [blame] | 155 | } |
| 156 | *olen = output_len; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 157 | return 0; |
Paul Bakker | 43b7e35 | 2011-01-18 15:27:19 +0000 | [diff] [blame] | 158 | } |
| 159 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 160 | int mbedtls_pkcs11_sign(mbedtls_pkcs11_context *ctx, |
| 161 | int mode, |
| 162 | mbedtls_md_type_t md_alg, |
| 163 | unsigned int hashlen, |
| 164 | const unsigned char *hash, |
| 165 | unsigned char *sig) |
Paul Bakker | 43b7e35 | 2011-01-18 15:27:19 +0000 | [diff] [blame] | 166 | { |
Paul Bakker | db1f059 | 2014-03-26 14:53:47 +0100 | [diff] [blame] | 167 | size_t sig_len = 0, asn_len = 0, oid_size = 0; |
Paul Bakker | 43b7e35 | 2011-01-18 15:27:19 +0000 | [diff] [blame] | 168 | unsigned char *p = sig; |
Steffan Karger | 28d81a0 | 2013-11-13 16:57:58 +0100 | [diff] [blame] | 169 | const char *oid; |
Paul Bakker | 43b7e35 | 2011-01-18 15:27:19 +0000 | [diff] [blame] | 170 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 171 | if (NULL == ctx) { |
| 172 | return MBEDTLS_ERR_RSA_BAD_INPUT_DATA; |
| 173 | } |
Paul Bakker | 43b7e35 | 2011-01-18 15:27:19 +0000 | [diff] [blame] | 174 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 175 | if (MBEDTLS_RSA_PRIVATE != mode) { |
| 176 | return MBEDTLS_ERR_RSA_BAD_INPUT_DATA; |
| 177 | } |
Paul Bakker | 43b7e35 | 2011-01-18 15:27:19 +0000 | [diff] [blame] | 178 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 179 | if (md_alg != MBEDTLS_MD_NONE) { |
| 180 | const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type(md_alg); |
| 181 | if (md_info == NULL) { |
| 182 | return MBEDTLS_ERR_RSA_BAD_INPUT_DATA; |
| 183 | } |
Steffan Karger | 28d81a0 | 2013-11-13 16:57:58 +0100 | [diff] [blame] | 184 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 185 | if (mbedtls_oid_get_oid_by_md(md_alg, &oid, &oid_size) != 0) { |
| 186 | return MBEDTLS_ERR_RSA_BAD_INPUT_DATA; |
| 187 | } |
Steffan Karger | 28d81a0 | 2013-11-13 16:57:58 +0100 | [diff] [blame] | 188 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 189 | hashlen = mbedtls_md_get_size(md_info); |
Paul Bakker | db1f059 | 2014-03-26 14:53:47 +0100 | [diff] [blame] | 190 | asn_len = 10 + oid_size; |
Steffan Karger | 28d81a0 | 2013-11-13 16:57:58 +0100 | [diff] [blame] | 191 | } |
| 192 | |
Paul Bakker | db1f059 | 2014-03-26 14:53:47 +0100 | [diff] [blame] | 193 | sig_len = ctx->len; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 194 | if (hashlen > sig_len || asn_len > sig_len || |
| 195 | hashlen + asn_len > sig_len) { |
| 196 | return MBEDTLS_ERR_RSA_BAD_INPUT_DATA; |
Steffan Karger | 28d81a0 | 2013-11-13 16:57:58 +0100 | [diff] [blame] | 197 | } |
Paul Bakker | db1f059 | 2014-03-26 14:53:47 +0100 | [diff] [blame] | 198 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 199 | if (md_alg != MBEDTLS_MD_NONE) { |
Steffan Karger | 28d81a0 | 2013-11-13 16:57:58 +0100 | [diff] [blame] | 200 | /* |
| 201 | * DigestInfo ::= SEQUENCE { |
| 202 | * digestAlgorithm DigestAlgorithmIdentifier, |
| 203 | * digest Digest } |
| 204 | * |
| 205 | * DigestAlgorithmIdentifier ::= AlgorithmIdentifier |
| 206 | * |
| 207 | * Digest ::= OCTET STRING |
| 208 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 209 | *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 210 | *p++ = (unsigned char) (0x08 + oid_size + hashlen); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 211 | *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 212 | *p++ = (unsigned char) (0x04 + oid_size); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 213 | *p++ = MBEDTLS_ASN1_OID; |
Steffan Karger | 28d81a0 | 2013-11-13 16:57:58 +0100 | [diff] [blame] | 214 | *p++ = oid_size & 0xFF; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 215 | memcpy(p, oid, oid_size); |
Steffan Karger | 28d81a0 | 2013-11-13 16:57:58 +0100 | [diff] [blame] | 216 | p += oid_size; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 217 | *p++ = MBEDTLS_ASN1_NULL; |
Steffan Karger | 28d81a0 | 2013-11-13 16:57:58 +0100 | [diff] [blame] | 218 | *p++ = 0x00; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 219 | *p++ = MBEDTLS_ASN1_OCTET_STRING; |
Steffan Karger | 28d81a0 | 2013-11-13 16:57:58 +0100 | [diff] [blame] | 220 | *p++ = hashlen; |
Paul Bakker | 43b7e35 | 2011-01-18 15:27:19 +0000 | [diff] [blame] | 221 | } |
| 222 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 223 | memcpy(p, hash, hashlen); |
Paul Bakker | db1f059 | 2014-03-26 14:53:47 +0100 | [diff] [blame] | 224 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 225 | if (pkcs11h_certificate_signAny(ctx->pkcs11h_cert, CKM_RSA_PKCS, sig, |
| 226 | asn_len + hashlen, sig, &sig_len) != CKR_OK) { |
| 227 | return MBEDTLS_ERR_RSA_BAD_INPUT_DATA; |
Paul Bakker | 43b7e35 | 2011-01-18 15:27:19 +0000 | [diff] [blame] | 228 | } |
| 229 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 230 | return 0; |
Paul Bakker | 43b7e35 | 2011-01-18 15:27:19 +0000 | [diff] [blame] | 231 | } |
| 232 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 233 | #endif /* defined(MBEDTLS_PKCS11_C) */ |