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