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