Paul Bakker | 43b7e35 | 2011-01-18 15:27:19 +0000 | [diff] [blame] | 1 | /** |
| 2 | * \file pkcs11.h |
| 3 | * |
| 4 | * \brief Wrapper for PKCS#11 library libpkcs11-helper |
| 5 | * |
| 6 | * \author Adriaan de Jong <dejong@fox-it.com> |
Darryl Green | a40a101 | 2018-01-05 15:33:17 +0000 | [diff] [blame] | 7 | */ |
| 8 | /* |
Bence Szépkúti | 1e14827 | 2020-08-07 13:07:28 +0200 | [diff] [blame] | 9 | * Copyright The Mbed TLS Contributors |
Manuel Pégourié-Gonnard | 37ff140 | 2015-09-04 14:21:07 +0200 | [diff] [blame] | 10 | * SPDX-License-Identifier: Apache-2.0 |
| 11 | * |
| 12 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 13 | * not use this file except in compliance with the License. |
| 14 | * You may obtain a copy of the License at |
| 15 | * |
| 16 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 17 | * |
| 18 | * Unless required by applicable law or agreed to in writing, software |
| 19 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 20 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 21 | * See the License for the specific language governing permissions and |
| 22 | * limitations under the License. |
Paul Bakker | 43b7e35 | 2011-01-18 15:27:19 +0000 | [diff] [blame] | 23 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 24 | #ifndef MBEDTLS_PKCS11_H |
| 25 | #define MBEDTLS_PKCS11_H |
Paul Bakker | 43b7e35 | 2011-01-18 15:27:19 +0000 | [diff] [blame] | 26 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 27 | #if !defined(MBEDTLS_CONFIG_FILE) |
Jaeden Amero | 6609aef | 2019-07-04 20:01:14 +0100 | [diff] [blame] | 28 | #include "mbedtls/config.h" |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 29 | #else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 30 | #include MBEDTLS_CONFIG_FILE |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 31 | #endif |
Paul Bakker | 43b7e35 | 2011-01-18 15:27:19 +0000 | [diff] [blame] | 32 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 33 | #if defined(MBEDTLS_PKCS11_C) |
Paul Bakker | 43b7e35 | 2011-01-18 15:27:19 +0000 | [diff] [blame] | 34 | |
Jaeden Amero | 6609aef | 2019-07-04 20:01:14 +0100 | [diff] [blame] | 35 | #include "mbedtls/x509_crt.h" |
Paul Bakker | 43b7e35 | 2011-01-18 15:27:19 +0000 | [diff] [blame] | 36 | |
| 37 | #include <pkcs11-helper-1.0/pkcs11h-certificate.h> |
| 38 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 39 | #if (defined(__ARMCC_VERSION) || defined(_MSC_VER)) && \ |
Manuel Pégourié-Gonnard | 0223ab9 | 2015-10-05 11:40:01 +0100 | [diff] [blame] | 40 | !defined(inline) && !defined(__cplusplus) |
Paul Bakker | eb2c658 | 2012-09-27 19:15:01 +0000 | [diff] [blame] | 41 | #define inline __inline |
Manuel Pégourié-Gonnard | 20af64d | 2015-07-07 18:33:39 +0200 | [diff] [blame] | 42 | #endif |
Paul Bakker | eb2c658 | 2012-09-27 19:15:01 +0000 | [diff] [blame] | 43 | |
Paul Bakker | 407a0da | 2013-06-27 14:29:21 +0200 | [diff] [blame] | 44 | #ifdef __cplusplus |
| 45 | extern "C" { |
| 46 | #endif |
| 47 | |
Andres Amaya Garcia | 9fc82ef | 2019-01-15 19:08:12 +0000 | [diff] [blame] | 48 | #if defined(MBEDTLS_DEPRECATED_REMOVED) |
| 49 | |
Paul Bakker | 43b7e35 | 2011-01-18 15:27:19 +0000 | [diff] [blame] | 50 | /** |
| 51 | * Context for PKCS #11 private keys. |
| 52 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 53 | typedef struct mbedtls_pkcs11_context { |
| 54 | pkcs11h_certificate_t pkcs11h_cert; |
| 55 | int len; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 56 | } mbedtls_pkcs11_context; |
Paul Bakker | 43b7e35 | 2011-01-18 15:27:19 +0000 | [diff] [blame] | 57 | |
Andres Amaya Garcia | 0d6e108 | 2018-12-05 20:52:29 +0000 | [diff] [blame] | 58 | #if defined(MBEDTLS_DEPRECATED_WARNING) |
| 59 | #define MBEDTLS_DEPRECATED __attribute__((deprecated)) |
| 60 | #else |
| 61 | #define MBEDTLS_DEPRECATED |
| 62 | #endif |
| 63 | |
Paul Bakker | 43b7e35 | 2011-01-18 15:27:19 +0000 | [diff] [blame] | 64 | /** |
Tillmann Karras | 588ad50 | 2015-09-25 04:27:22 +0200 | [diff] [blame] | 65 | * Initialize a mbedtls_pkcs11_context. |
Manuel Pégourié-Gonnard | eab147c | 2015-04-29 01:10:10 +0200 | [diff] [blame] | 66 | * (Just making memory references valid.) |
Andres Amaya Garcia | 0d6e108 | 2018-12-05 20:52:29 +0000 | [diff] [blame] | 67 | * |
Andres Amaya Garcia | 99fc387 | 2019-01-15 19:07:43 +0000 | [diff] [blame] | 68 | * \deprecated This function is deprecated and will be removed in a |
| 69 | * future version of the library. |
Manuel Pégourié-Gonnard | eab147c | 2015-04-29 01:10:10 +0200 | [diff] [blame] | 70 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 71 | MBEDTLS_DEPRECATED void mbedtls_pkcs11_init(mbedtls_pkcs11_context *ctx); |
Manuel Pégourié-Gonnard | eab147c | 2015-04-29 01:10:10 +0200 | [diff] [blame] | 72 | |
| 73 | /** |
Manuel Pégourié-Gonnard | b4fe3cb | 2015-01-22 16:11:05 +0000 | [diff] [blame] | 74 | * Fill in a mbed TLS certificate, based on the given PKCS11 helper certificate. |
Paul Bakker | 43b7e35 | 2011-01-18 15:27:19 +0000 | [diff] [blame] | 75 | * |
Andres Amaya Garcia | 99fc387 | 2019-01-15 19:07:43 +0000 | [diff] [blame] | 76 | * \deprecated This function is deprecated and will be removed in a |
| 77 | * future version of the library. |
Andres Amaya Garcia | 0d6e108 | 2018-12-05 20:52:29 +0000 | [diff] [blame] | 78 | * |
Paul Bakker | 43b7e35 | 2011-01-18 15:27:19 +0000 | [diff] [blame] | 79 | * \param cert X.509 certificate to fill |
| 80 | * \param pkcs11h_cert PKCS #11 helper certificate |
| 81 | * |
| 82 | * \return 0 on success. |
| 83 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 84 | MBEDTLS_DEPRECATED int mbedtls_pkcs11_x509_cert_bind(mbedtls_x509_crt *cert, |
| 85 | pkcs11h_certificate_t pkcs11h_cert); |
Paul Bakker | 43b7e35 | 2011-01-18 15:27:19 +0000 | [diff] [blame] | 86 | |
| 87 | /** |
Manuel Pégourié-Gonnard | eab147c | 2015-04-29 01:10:10 +0200 | [diff] [blame] | 88 | * Set up a mbedtls_pkcs11_context storing the given certificate. Note that the |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 89 | * mbedtls_pkcs11_context will take over control of the certificate, freeing it when |
Paul Bakker | 43b7e35 | 2011-01-18 15:27:19 +0000 | [diff] [blame] | 90 | * done. |
| 91 | * |
Andres Amaya Garcia | 99fc387 | 2019-01-15 19:07:43 +0000 | [diff] [blame] | 92 | * \deprecated This function is deprecated and will be removed in a |
| 93 | * future version of the library. |
Andres Amaya Garcia | 0d6e108 | 2018-12-05 20:52:29 +0000 | [diff] [blame] | 94 | * |
Paul Bakker | 43b7e35 | 2011-01-18 15:27:19 +0000 | [diff] [blame] | 95 | * \param priv_key Private key structure to fill. |
| 96 | * \param pkcs11_cert PKCS #11 helper certificate |
| 97 | * |
| 98 | * \return 0 on success |
| 99 | */ |
Andres Amaya Garcia | 0d6e108 | 2018-12-05 20:52:29 +0000 | [diff] [blame] | 100 | MBEDTLS_DEPRECATED int mbedtls_pkcs11_priv_key_bind( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 101 | mbedtls_pkcs11_context *priv_key, |
| 102 | pkcs11h_certificate_t pkcs11_cert); |
Paul Bakker | 43b7e35 | 2011-01-18 15:27:19 +0000 | [diff] [blame] | 103 | |
| 104 | /** |
| 105 | * Free the contents of the given private key context. Note that the structure |
| 106 | * itself is not freed. |
| 107 | * |
Andres Amaya Garcia | 99fc387 | 2019-01-15 19:07:43 +0000 | [diff] [blame] | 108 | * \deprecated This function is deprecated and will be removed in a |
| 109 | * future version of the library. |
Andres Amaya Garcia | 0d6e108 | 2018-12-05 20:52:29 +0000 | [diff] [blame] | 110 | * |
Paul Bakker | 43b7e35 | 2011-01-18 15:27:19 +0000 | [diff] [blame] | 111 | * \param priv_key Private key structure to cleanup |
| 112 | */ |
Andres Amaya Garcia | 0d6e108 | 2018-12-05 20:52:29 +0000 | [diff] [blame] | 113 | MBEDTLS_DEPRECATED void mbedtls_pkcs11_priv_key_free( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 114 | mbedtls_pkcs11_context *priv_key); |
Paul Bakker | 43b7e35 | 2011-01-18 15:27:19 +0000 | [diff] [blame] | 115 | |
| 116 | /** |
Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 117 | * \brief Do an RSA private key decrypt, then remove the message |
| 118 | * padding |
Paul Bakker | 43b7e35 | 2011-01-18 15:27:19 +0000 | [diff] [blame] | 119 | * |
Andres Amaya Garcia | 99fc387 | 2019-01-15 19:07:43 +0000 | [diff] [blame] | 120 | * \deprecated This function is deprecated and will be removed in a future |
| 121 | * version of the library. |
Andres Amaya Garcia | 0d6e108 | 2018-12-05 20:52:29 +0000 | [diff] [blame] | 122 | * |
Paul Bakker | 43b7e35 | 2011-01-18 15:27:19 +0000 | [diff] [blame] | 123 | * \param ctx PKCS #11 context |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 124 | * \param mode must be MBEDTLS_RSA_PRIVATE, for compatibility with rsa.c's signature |
Paul Bakker | 43b7e35 | 2011-01-18 15:27:19 +0000 | [diff] [blame] | 125 | * \param input buffer holding the encrypted data |
| 126 | * \param output buffer that will hold the plaintext |
| 127 | * \param olen will contain the plaintext length |
| 128 | * \param output_max_len maximum length of the output buffer |
| 129 | * |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 130 | * \return 0 if successful, or an MBEDTLS_ERR_RSA_XXX error code |
Paul Bakker | 43b7e35 | 2011-01-18 15:27:19 +0000 | [diff] [blame] | 131 | * |
| 132 | * \note The output buffer must be as large as the size |
| 133 | * of ctx->N (eg. 128 bytes if RSA-1024 is used) otherwise |
| 134 | * an error is thrown. |
| 135 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 136 | MBEDTLS_DEPRECATED int mbedtls_pkcs11_decrypt(mbedtls_pkcs11_context *ctx, |
| 137 | int mode, size_t *olen, |
| 138 | const unsigned char *input, |
| 139 | unsigned char *output, |
| 140 | size_t output_max_len); |
Paul Bakker | 43b7e35 | 2011-01-18 15:27:19 +0000 | [diff] [blame] | 141 | |
| 142 | /** |
| 143 | * \brief Do a private RSA to sign a message digest |
| 144 | * |
Andres Amaya Garcia | 99fc387 | 2019-01-15 19:07:43 +0000 | [diff] [blame] | 145 | * \deprecated This function is deprecated and will be removed in a future |
| 146 | * version of the library. |
Andres Amaya Garcia | 0d6e108 | 2018-12-05 20:52:29 +0000 | [diff] [blame] | 147 | * |
Paul Bakker | 43b7e35 | 2011-01-18 15:27:19 +0000 | [diff] [blame] | 148 | * \param ctx PKCS #11 context |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 149 | * \param mode must be MBEDTLS_RSA_PRIVATE, for compatibility with rsa.c's signature |
| 150 | * \param md_alg a MBEDTLS_MD_XXX (use MBEDTLS_MD_NONE for signing raw data) |
| 151 | * \param hashlen message digest length (for MBEDTLS_MD_NONE only) |
Paul Bakker | 43b7e35 | 2011-01-18 15:27:19 +0000 | [diff] [blame] | 152 | * \param hash buffer holding the message digest |
| 153 | * \param sig buffer that will hold the ciphertext |
| 154 | * |
| 155 | * \return 0 if the signing operation was successful, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 156 | * or an MBEDTLS_ERR_RSA_XXX error code |
Paul Bakker | 43b7e35 | 2011-01-18 15:27:19 +0000 | [diff] [blame] | 157 | * |
| 158 | * \note The "sig" buffer must be as large as the size |
| 159 | * of ctx->N (eg. 128 bytes if RSA-1024 is used). |
| 160 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 161 | MBEDTLS_DEPRECATED int mbedtls_pkcs11_sign(mbedtls_pkcs11_context *ctx, |
| 162 | int mode, |
| 163 | mbedtls_md_type_t md_alg, |
| 164 | unsigned int hashlen, |
| 165 | const unsigned char *hash, |
| 166 | unsigned char *sig); |
Paul Bakker | 43b7e35 | 2011-01-18 15:27:19 +0000 | [diff] [blame] | 167 | |
Paul Bakker | eb2c658 | 2012-09-27 19:15:01 +0000 | [diff] [blame] | 168 | /** |
| 169 | * SSL/TLS wrappers for PKCS#11 functions |
Andres Amaya Garcia | 0d6e108 | 2018-12-05 20:52:29 +0000 | [diff] [blame] | 170 | * |
Andres Amaya Garcia | 99fc387 | 2019-01-15 19:07:43 +0000 | [diff] [blame] | 171 | * \deprecated This function is deprecated and will be removed in a future |
| 172 | * version of the library. |
Paul Bakker | eb2c658 | 2012-09-27 19:15:01 +0000 | [diff] [blame] | 173 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 174 | MBEDTLS_DEPRECATED static inline int mbedtls_ssl_pkcs11_decrypt(void *ctx, |
| 175 | int mode, |
| 176 | size_t *olen, |
| 177 | const unsigned char *input, |
| 178 | unsigned char *output, |
| 179 | size_t output_max_len) |
Paul Bakker | eb2c658 | 2012-09-27 19:15:01 +0000 | [diff] [blame] | 180 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 181 | return mbedtls_pkcs11_decrypt((mbedtls_pkcs11_context *) ctx, mode, olen, input, output, |
| 182 | output_max_len); |
Paul Bakker | eb2c658 | 2012-09-27 19:15:01 +0000 | [diff] [blame] | 183 | } |
| 184 | |
Andres Amaya Garcia | b37268d | 2019-01-15 19:23:42 +0000 | [diff] [blame] | 185 | /** |
| 186 | * \brief This function signs a message digest using RSA. |
| 187 | * |
| 188 | * \deprecated This function is deprecated and will be removed in a future |
| 189 | * version of the library. |
| 190 | * |
| 191 | * \param ctx The PKCS #11 context. |
| 192 | * \param f_rng The RNG function. This parameter is unused. |
| 193 | * \param p_rng The RNG context. This parameter is unused. |
| 194 | * \param mode The operation to run. This must be set to |
| 195 | * MBEDTLS_RSA_PRIVATE, for compatibility with rsa.c's |
| 196 | * signature. |
| 197 | * \param md_alg The message digest algorithm. One of the MBEDTLS_MD_XXX |
| 198 | * must be passed to this function and MBEDTLS_MD_NONE can be |
| 199 | * used for signing raw data. |
| 200 | * \param hashlen The message digest length (for MBEDTLS_MD_NONE only). |
| 201 | * \param hash The buffer holding the message digest. |
| 202 | * \param sig The buffer that will hold the ciphertext. |
| 203 | * |
| 204 | * \return \c 0 if the signing operation was successful. |
| 205 | * \return A non-zero error code on failure. |
| 206 | * |
| 207 | * \note The \p sig buffer must be as large as the size of |
Andres Amaya Garcia | 312431b | 2019-01-16 19:14:02 +0000 | [diff] [blame] | 208 | * <code>ctx->N</code>. For example, 128 bytes if RSA-1024 is |
Andres Amaya Garcia | b37268d | 2019-01-15 19:23:42 +0000 | [diff] [blame] | 209 | * used. |
| 210 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 211 | MBEDTLS_DEPRECATED static inline int mbedtls_ssl_pkcs11_sign(void *ctx, |
| 212 | int (*f_rng)(void *, |
| 213 | unsigned char *, |
| 214 | size_t), |
| 215 | void *p_rng, |
| 216 | int mode, |
| 217 | mbedtls_md_type_t md_alg, |
| 218 | unsigned int hashlen, |
| 219 | const unsigned char *hash, |
| 220 | unsigned char *sig) |
Paul Bakker | eb2c658 | 2012-09-27 19:15:01 +0000 | [diff] [blame] | 221 | { |
| 222 | ((void) f_rng); |
| 223 | ((void) p_rng); |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 224 | return mbedtls_pkcs11_sign((mbedtls_pkcs11_context *) ctx, mode, md_alg, |
| 225 | hashlen, hash, sig); |
Paul Bakker | eb2c658 | 2012-09-27 19:15:01 +0000 | [diff] [blame] | 226 | } |
| 227 | |
Andres Amaya Garcia | b37268d | 2019-01-15 19:23:42 +0000 | [diff] [blame] | 228 | /** |
| 229 | * This function gets the length of the private key. |
| 230 | * |
| 231 | * \deprecated This function is deprecated and will be removed in a future |
| 232 | * version of the library. |
| 233 | * |
| 234 | * \param ctx The PKCS #11 context. |
| 235 | * |
| 236 | * \return The length of the private key. |
| 237 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 238 | MBEDTLS_DEPRECATED static inline size_t mbedtls_ssl_pkcs11_key_len(void *ctx) |
Paul Bakker | eb2c658 | 2012-09-27 19:15:01 +0000 | [diff] [blame] | 239 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 240 | return ((mbedtls_pkcs11_context *) ctx)->len; |
Paul Bakker | eb2c658 | 2012-09-27 19:15:01 +0000 | [diff] [blame] | 241 | } |
| 242 | |
Andres Amaya Garcia | 0d6e108 | 2018-12-05 20:52:29 +0000 | [diff] [blame] | 243 | #undef MBEDTLS_DEPRECATED |
| 244 | |
Andres Amaya Garcia | 9fc82ef | 2019-01-15 19:08:12 +0000 | [diff] [blame] | 245 | #endif /* MBEDTLS_DEPRECATED_REMOVED */ |
| 246 | |
Paul Bakker | 407a0da | 2013-06-27 14:29:21 +0200 | [diff] [blame] | 247 | #ifdef __cplusplus |
| 248 | } |
| 249 | #endif |
| 250 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 251 | #endif /* MBEDTLS_PKCS11_C */ |
Paul Bakker | 43b7e35 | 2011-01-18 15:27:19 +0000 | [diff] [blame] | 252 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 253 | #endif /* MBEDTLS_PKCS11_H */ |