blob: 80a8a9c423c601debb8c7e26c4d16862ce904a78 [file] [log] [blame]
Paul Bakker43b7e352011-01-18 15:27:19 +00001/**
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 Greena40a1012018-01-05 15:33:17 +00007 */
8/*
Bence Szépkúti1e148272020-08-07 13:07:28 +02009 * Copyright The Mbed TLS Contributors
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +020010 * 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 Bakker43b7e352011-01-18 15:27:19 +000023 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020024#ifndef MBEDTLS_PKCS11_H
25#define MBEDTLS_PKCS11_H
Paul Bakker43b7e352011-01-18 15:27:19 +000026
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020027#if !defined(MBEDTLS_CONFIG_FILE)
Jaeden Amero6609aef2019-07-04 20:01:14 +010028#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020029#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020030#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020031#endif
Paul Bakker43b7e352011-01-18 15:27:19 +000032
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020033#if defined(MBEDTLS_PKCS11_C)
Paul Bakker43b7e352011-01-18 15:27:19 +000034
Jaeden Amero6609aef2019-07-04 20:01:14 +010035#include "mbedtls/x509_crt.h"
Paul Bakker43b7e352011-01-18 15:27:19 +000036
37#include <pkcs11-helper-1.0/pkcs11h-certificate.h>
38
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010039#if (defined(__ARMCC_VERSION) || defined(_MSC_VER)) && \
Manuel Pégourié-Gonnard0223ab92015-10-05 11:40:01 +010040 !defined(inline) && !defined(__cplusplus)
Paul Bakkereb2c6582012-09-27 19:15:01 +000041#define inline __inline
Manuel Pégourié-Gonnard20af64d2015-07-07 18:33:39 +020042#endif
Paul Bakkereb2c6582012-09-27 19:15:01 +000043
Paul Bakker407a0da2013-06-27 14:29:21 +020044#ifdef __cplusplus
45extern "C" {
46#endif
47
Andres Amaya Garcia9fc82ef2019-01-15 19:08:12 +000048#if defined(MBEDTLS_DEPRECATED_REMOVED)
49
Paul Bakker43b7e352011-01-18 15:27:19 +000050/**
51 * Context for PKCS #11 private keys.
52 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010053typedef struct mbedtls_pkcs11_context {
54 pkcs11h_certificate_t pkcs11h_cert;
55 int len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020056} mbedtls_pkcs11_context;
Paul Bakker43b7e352011-01-18 15:27:19 +000057
Andres Amaya Garcia0d6e1082018-12-05 20:52:29 +000058#if defined(MBEDTLS_DEPRECATED_WARNING)
59#define MBEDTLS_DEPRECATED __attribute__((deprecated))
60#else
61#define MBEDTLS_DEPRECATED
62#endif
63
Paul Bakker43b7e352011-01-18 15:27:19 +000064/**
Tillmann Karras588ad502015-09-25 04:27:22 +020065 * Initialize a mbedtls_pkcs11_context.
Manuel Pégourié-Gonnardeab147c2015-04-29 01:10:10 +020066 * (Just making memory references valid.)
Andres Amaya Garcia0d6e1082018-12-05 20:52:29 +000067 *
Andres Amaya Garcia99fc3872019-01-15 19:07:43 +000068 * \deprecated This function is deprecated and will be removed in a
69 * future version of the library.
Manuel Pégourié-Gonnardeab147c2015-04-29 01:10:10 +020070 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010071MBEDTLS_DEPRECATED void mbedtls_pkcs11_init(mbedtls_pkcs11_context *ctx);
Manuel Pégourié-Gonnardeab147c2015-04-29 01:10:10 +020072
73/**
Manuel Pégourié-Gonnardb4fe3cb2015-01-22 16:11:05 +000074 * Fill in a mbed TLS certificate, based on the given PKCS11 helper certificate.
Paul Bakker43b7e352011-01-18 15:27:19 +000075 *
Andres Amaya Garcia99fc3872019-01-15 19:07:43 +000076 * \deprecated This function is deprecated and will be removed in a
77 * future version of the library.
Andres Amaya Garcia0d6e1082018-12-05 20:52:29 +000078 *
Paul Bakker43b7e352011-01-18 15:27:19 +000079 * \param cert X.509 certificate to fill
80 * \param pkcs11h_cert PKCS #11 helper certificate
81 *
82 * \return 0 on success.
83 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010084MBEDTLS_DEPRECATED int mbedtls_pkcs11_x509_cert_bind(mbedtls_x509_crt *cert,
85 pkcs11h_certificate_t pkcs11h_cert);
Paul Bakker43b7e352011-01-18 15:27:19 +000086
87/**
Manuel Pégourié-Gonnardeab147c2015-04-29 01:10:10 +020088 * Set up a mbedtls_pkcs11_context storing the given certificate. Note that the
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020089 * mbedtls_pkcs11_context will take over control of the certificate, freeing it when
Paul Bakker43b7e352011-01-18 15:27:19 +000090 * done.
91 *
Andres Amaya Garcia99fc3872019-01-15 19:07:43 +000092 * \deprecated This function is deprecated and will be removed in a
93 * future version of the library.
Andres Amaya Garcia0d6e1082018-12-05 20:52:29 +000094 *
Paul Bakker43b7e352011-01-18 15:27:19 +000095 * \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 Garcia0d6e1082018-12-05 20:52:29 +0000100MBEDTLS_DEPRECATED int mbedtls_pkcs11_priv_key_bind(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100101 mbedtls_pkcs11_context *priv_key,
102 pkcs11h_certificate_t pkcs11_cert);
Paul Bakker43b7e352011-01-18 15:27:19 +0000103
104/**
105 * Free the contents of the given private key context. Note that the structure
106 * itself is not freed.
107 *
Andres Amaya Garcia99fc3872019-01-15 19:07:43 +0000108 * \deprecated This function is deprecated and will be removed in a
109 * future version of the library.
Andres Amaya Garcia0d6e1082018-12-05 20:52:29 +0000110 *
Paul Bakker43b7e352011-01-18 15:27:19 +0000111 * \param priv_key Private key structure to cleanup
112 */
Andres Amaya Garcia0d6e1082018-12-05 20:52:29 +0000113MBEDTLS_DEPRECATED void mbedtls_pkcs11_priv_key_free(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100114 mbedtls_pkcs11_context *priv_key);
Paul Bakker43b7e352011-01-18 15:27:19 +0000115
116/**
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200117 * \brief Do an RSA private key decrypt, then remove the message
118 * padding
Paul Bakker43b7e352011-01-18 15:27:19 +0000119 *
Andres Amaya Garcia99fc3872019-01-15 19:07:43 +0000120 * \deprecated This function is deprecated and will be removed in a future
121 * version of the library.
Andres Amaya Garcia0d6e1082018-12-05 20:52:29 +0000122 *
Paul Bakker43b7e352011-01-18 15:27:19 +0000123 * \param ctx PKCS #11 context
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200124 * \param mode must be MBEDTLS_RSA_PRIVATE, for compatibility with rsa.c's signature
Paul Bakker43b7e352011-01-18 15:27:19 +0000125 * \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é-Gonnard2cf5a7c2015-04-08 12:49:31 +0200130 * \return 0 if successful, or an MBEDTLS_ERR_RSA_XXX error code
Paul Bakker43b7e352011-01-18 15:27:19 +0000131 *
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 Peskine1b6c09a2023-01-11 14:52:35 +0100136MBEDTLS_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 Bakker43b7e352011-01-18 15:27:19 +0000141
142/**
143 * \brief Do a private RSA to sign a message digest
144 *
Andres Amaya Garcia99fc3872019-01-15 19:07:43 +0000145 * \deprecated This function is deprecated and will be removed in a future
146 * version of the library.
Andres Amaya Garcia0d6e1082018-12-05 20:52:29 +0000147 *
Paul Bakker43b7e352011-01-18 15:27:19 +0000148 * \param ctx PKCS #11 context
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200149 * \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 Bakker43b7e352011-01-18 15:27:19 +0000152 * \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é-Gonnard2cf5a7c2015-04-08 12:49:31 +0200156 * or an MBEDTLS_ERR_RSA_XXX error code
Paul Bakker43b7e352011-01-18 15:27:19 +0000157 *
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 Peskine1b6c09a2023-01-11 14:52:35 +0100161MBEDTLS_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 Bakker43b7e352011-01-18 15:27:19 +0000167
Paul Bakkereb2c6582012-09-27 19:15:01 +0000168/**
169 * SSL/TLS wrappers for PKCS#11 functions
Andres Amaya Garcia0d6e1082018-12-05 20:52:29 +0000170 *
Andres Amaya Garcia99fc3872019-01-15 19:07:43 +0000171 * \deprecated This function is deprecated and will be removed in a future
172 * version of the library.
Paul Bakkereb2c6582012-09-27 19:15:01 +0000173 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100174MBEDTLS_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 Bakkereb2c6582012-09-27 19:15:01 +0000180{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100181 return mbedtls_pkcs11_decrypt((mbedtls_pkcs11_context *) ctx, mode, olen, input, output,
182 output_max_len);
Paul Bakkereb2c6582012-09-27 19:15:01 +0000183}
184
Andres Amaya Garciab37268d2019-01-15 19:23:42 +0000185/**
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 Garcia312431b2019-01-16 19:14:02 +0000208 * <code>ctx->N</code>. For example, 128 bytes if RSA-1024 is
Andres Amaya Garciab37268d2019-01-15 19:23:42 +0000209 * used.
210 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100211MBEDTLS_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 Bakkereb2c6582012-09-27 19:15:01 +0000221{
222 ((void) f_rng);
223 ((void) p_rng);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100224 return mbedtls_pkcs11_sign((mbedtls_pkcs11_context *) ctx, mode, md_alg,
225 hashlen, hash, sig);
Paul Bakkereb2c6582012-09-27 19:15:01 +0000226}
227
Andres Amaya Garciab37268d2019-01-15 19:23:42 +0000228/**
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 Peskine1b6c09a2023-01-11 14:52:35 +0100238MBEDTLS_DEPRECATED static inline size_t mbedtls_ssl_pkcs11_key_len(void *ctx)
Paul Bakkereb2c6582012-09-27 19:15:01 +0000239{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100240 return ((mbedtls_pkcs11_context *) ctx)->len;
Paul Bakkereb2c6582012-09-27 19:15:01 +0000241}
242
Andres Amaya Garcia0d6e1082018-12-05 20:52:29 +0000243#undef MBEDTLS_DEPRECATED
244
Andres Amaya Garcia9fc82ef2019-01-15 19:08:12 +0000245#endif /* MBEDTLS_DEPRECATED_REMOVED */
246
Paul Bakker407a0da2013-06-27 14:29:21 +0200247#ifdef __cplusplus
248}
249#endif
250
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200251#endif /* MBEDTLS_PKCS11_C */
Paul Bakker43b7e352011-01-18 15:27:19 +0000252
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200253#endif /* MBEDTLS_PKCS11_H */