blob: cf8d8c429785e201badd6b21bb8123bf905bb60d [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/*
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02009 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
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é-Gonnardfe446432015-03-06 13:17:10 +000024 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker43b7e352011-01-18 15:27:19 +000025 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020026#ifndef MBEDTLS_PKCS11_H
27#define MBEDTLS_PKCS11_H
Paul Bakker43b7e352011-01-18 15:27:19 +000028
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020029#if !defined(MBEDTLS_CONFIG_FILE)
Jaeden Amero6609aef2019-07-04 20:01:14 +010030#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020031#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020032#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020033#endif
Paul Bakker43b7e352011-01-18 15:27:19 +000034
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020035#if defined(MBEDTLS_PKCS11_C)
Paul Bakker43b7e352011-01-18 15:27:19 +000036
Jaeden Amero6609aef2019-07-04 20:01:14 +010037#include "mbedtls/x509_crt.h"
Paul Bakker43b7e352011-01-18 15:27:19 +000038
39#include <pkcs11-helper-1.0/pkcs11h-certificate.h>
40
Manuel Pégourié-Gonnard0223ab92015-10-05 11:40:01 +010041#if ( defined(__ARMCC_VERSION) || defined(_MSC_VER) ) && \
42 !defined(inline) && !defined(__cplusplus)
Paul Bakkereb2c6582012-09-27 19:15:01 +000043#define inline __inline
Manuel Pégourié-Gonnard20af64d2015-07-07 18:33:39 +020044#endif
Paul Bakkereb2c6582012-09-27 19:15:01 +000045
Paul Bakker407a0da2013-06-27 14:29:21 +020046#ifdef __cplusplus
47extern "C" {
48#endif
49
Andres Amaya Garcia9fc82ef2019-01-15 19:08:12 +000050#if defined(MBEDTLS_DEPRECATED_REMOVED)
51
Paul Bakker43b7e352011-01-18 15:27:19 +000052/**
53 * Context for PKCS #11 private keys.
54 */
Dawid Drozd428cc522018-07-24 10:02:47 +020055typedef struct mbedtls_pkcs11_context
56{
Paul Bakker43b7e352011-01-18 15:27:19 +000057 pkcs11h_certificate_t pkcs11h_cert;
58 int len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020059} mbedtls_pkcs11_context;
Paul Bakker43b7e352011-01-18 15:27:19 +000060
Andres Amaya Garcia0d6e1082018-12-05 20:52:29 +000061#if defined(MBEDTLS_DEPRECATED_WARNING)
62#define MBEDTLS_DEPRECATED __attribute__((deprecated))
63#else
64#define MBEDTLS_DEPRECATED
65#endif
66
Paul Bakker43b7e352011-01-18 15:27:19 +000067/**
Tillmann Karras588ad502015-09-25 04:27:22 +020068 * Initialize a mbedtls_pkcs11_context.
Manuel Pégourié-Gonnardeab147c2015-04-29 01:10:10 +020069 * (Just making memory references valid.)
Andres Amaya Garcia0d6e1082018-12-05 20:52:29 +000070 *
Andres Amaya Garcia99fc3872019-01-15 19:07:43 +000071 * \deprecated This function is deprecated and will be removed in a
72 * future version of the library.
Manuel Pégourié-Gonnardeab147c2015-04-29 01:10:10 +020073 */
Andres Amaya Garcia0d6e1082018-12-05 20:52:29 +000074MBEDTLS_DEPRECATED void mbedtls_pkcs11_init( mbedtls_pkcs11_context *ctx );
Manuel Pégourié-Gonnardeab147c2015-04-29 01:10:10 +020075
76/**
Manuel Pégourié-Gonnardb4fe3cb2015-01-22 16:11:05 +000077 * Fill in a mbed TLS certificate, based on the given PKCS11 helper certificate.
Paul Bakker43b7e352011-01-18 15:27:19 +000078 *
Andres Amaya Garcia99fc3872019-01-15 19:07:43 +000079 * \deprecated This function is deprecated and will be removed in a
80 * future version of the library.
Andres Amaya Garcia0d6e1082018-12-05 20:52:29 +000081 *
Paul Bakker43b7e352011-01-18 15:27:19 +000082 * \param cert X.509 certificate to fill
83 * \param pkcs11h_cert PKCS #11 helper certificate
84 *
85 * \return 0 on success.
86 */
Andres Amaya Garcia0d6e1082018-12-05 20:52:29 +000087MBEDTLS_DEPRECATED int mbedtls_pkcs11_x509_cert_bind( mbedtls_x509_crt *cert,
88 pkcs11h_certificate_t pkcs11h_cert );
Paul Bakker43b7e352011-01-18 15:27:19 +000089
90/**
Manuel Pégourié-Gonnardeab147c2015-04-29 01:10:10 +020091 * Set up a mbedtls_pkcs11_context storing the given certificate. Note that the
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020092 * mbedtls_pkcs11_context will take over control of the certificate, freeing it when
Paul Bakker43b7e352011-01-18 15:27:19 +000093 * done.
94 *
Andres Amaya Garcia99fc3872019-01-15 19:07:43 +000095 * \deprecated This function is deprecated and will be removed in a
96 * future version of the library.
Andres Amaya Garcia0d6e1082018-12-05 20:52:29 +000097 *
Paul Bakker43b7e352011-01-18 15:27:19 +000098 * \param priv_key Private key structure to fill.
99 * \param pkcs11_cert PKCS #11 helper certificate
100 *
101 * \return 0 on success
102 */
Andres Amaya Garcia0d6e1082018-12-05 20:52:29 +0000103MBEDTLS_DEPRECATED int mbedtls_pkcs11_priv_key_bind(
104 mbedtls_pkcs11_context *priv_key,
105 pkcs11h_certificate_t pkcs11_cert );
Paul Bakker43b7e352011-01-18 15:27:19 +0000106
107/**
108 * Free the contents of the given private key context. Note that the structure
109 * itself is not freed.
110 *
Andres Amaya Garcia99fc3872019-01-15 19:07:43 +0000111 * \deprecated This function is deprecated and will be removed in a
112 * future version of the library.
Andres Amaya Garcia0d6e1082018-12-05 20:52:29 +0000113 *
Paul Bakker43b7e352011-01-18 15:27:19 +0000114 * \param priv_key Private key structure to cleanup
115 */
Andres Amaya Garcia0d6e1082018-12-05 20:52:29 +0000116MBEDTLS_DEPRECATED void mbedtls_pkcs11_priv_key_free(
117 mbedtls_pkcs11_context *priv_key );
Paul Bakker43b7e352011-01-18 15:27:19 +0000118
119/**
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200120 * \brief Do an RSA private key decrypt, then remove the message
121 * padding
Paul Bakker43b7e352011-01-18 15:27:19 +0000122 *
Andres Amaya Garcia99fc3872019-01-15 19:07:43 +0000123 * \deprecated This function is deprecated and will be removed in a future
124 * version of the library.
Andres Amaya Garcia0d6e1082018-12-05 20:52:29 +0000125 *
Paul Bakker43b7e352011-01-18 15:27:19 +0000126 * \param ctx PKCS #11 context
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200127 * \param mode must be MBEDTLS_RSA_PRIVATE, for compatibility with rsa.c's signature
Paul Bakker43b7e352011-01-18 15:27:19 +0000128 * \param input buffer holding the encrypted data
129 * \param output buffer that will hold the plaintext
130 * \param olen will contain the plaintext length
131 * \param output_max_len maximum length of the output buffer
132 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200133 * \return 0 if successful, or an MBEDTLS_ERR_RSA_XXX error code
Paul Bakker43b7e352011-01-18 15:27:19 +0000134 *
135 * \note The output buffer must be as large as the size
136 * of ctx->N (eg. 128 bytes if RSA-1024 is used) otherwise
137 * an error is thrown.
138 */
Andres Amaya Garcia0d6e1082018-12-05 20:52:29 +0000139MBEDTLS_DEPRECATED int mbedtls_pkcs11_decrypt( mbedtls_pkcs11_context *ctx,
140 int mode, size_t *olen,
141 const unsigned char *input,
142 unsigned char *output,
143 size_t output_max_len );
Paul Bakker43b7e352011-01-18 15:27:19 +0000144
145/**
146 * \brief Do a private RSA to sign a message digest
147 *
Andres Amaya Garcia99fc3872019-01-15 19:07:43 +0000148 * \deprecated This function is deprecated and will be removed in a future
149 * version of the library.
Andres Amaya Garcia0d6e1082018-12-05 20:52:29 +0000150 *
Paul Bakker43b7e352011-01-18 15:27:19 +0000151 * \param ctx PKCS #11 context
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200152 * \param mode must be MBEDTLS_RSA_PRIVATE, for compatibility with rsa.c's signature
153 * \param md_alg a MBEDTLS_MD_XXX (use MBEDTLS_MD_NONE for signing raw data)
154 * \param hashlen message digest length (for MBEDTLS_MD_NONE only)
Paul Bakker43b7e352011-01-18 15:27:19 +0000155 * \param hash buffer holding the message digest
156 * \param sig buffer that will hold the ciphertext
157 *
158 * \return 0 if the signing operation was successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200159 * or an MBEDTLS_ERR_RSA_XXX error code
Paul Bakker43b7e352011-01-18 15:27:19 +0000160 *
161 * \note The "sig" buffer must be as large as the size
162 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
163 */
Andres Amaya Garcia0d6e1082018-12-05 20:52:29 +0000164MBEDTLS_DEPRECATED int mbedtls_pkcs11_sign( mbedtls_pkcs11_context *ctx,
165 int mode,
166 mbedtls_md_type_t md_alg,
167 unsigned int hashlen,
168 const unsigned char *hash,
169 unsigned char *sig );
Paul Bakker43b7e352011-01-18 15:27:19 +0000170
Paul Bakkereb2c6582012-09-27 19:15:01 +0000171/**
172 * SSL/TLS wrappers for PKCS#11 functions
Andres Amaya Garcia0d6e1082018-12-05 20:52:29 +0000173 *
Andres Amaya Garcia99fc3872019-01-15 19:07:43 +0000174 * \deprecated This function is deprecated and will be removed in a future
175 * version of the library.
Paul Bakkereb2c6582012-09-27 19:15:01 +0000176 */
Andres Amaya Garcia0d6e1082018-12-05 20:52:29 +0000177MBEDTLS_DEPRECATED static inline int mbedtls_ssl_pkcs11_decrypt( void *ctx,
178 int mode, size_t *olen,
179 const unsigned char *input, unsigned char *output,
180 size_t output_max_len )
Paul Bakkereb2c6582012-09-27 19:15:01 +0000181{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200182 return mbedtls_pkcs11_decrypt( (mbedtls_pkcs11_context *) ctx, mode, olen, input, output,
Paul Bakkereb2c6582012-09-27 19:15:01 +0000183 output_max_len );
184}
185
Andres Amaya Garciab37268d2019-01-15 19:23:42 +0000186/**
187 * \brief This function signs a message digest using RSA.
188 *
189 * \deprecated This function is deprecated and will be removed in a future
190 * version of the library.
191 *
192 * \param ctx The PKCS #11 context.
193 * \param f_rng The RNG function. This parameter is unused.
194 * \param p_rng The RNG context. This parameter is unused.
195 * \param mode The operation to run. This must be set to
196 * MBEDTLS_RSA_PRIVATE, for compatibility with rsa.c's
197 * signature.
198 * \param md_alg The message digest algorithm. One of the MBEDTLS_MD_XXX
199 * must be passed to this function and MBEDTLS_MD_NONE can be
200 * used for signing raw data.
201 * \param hashlen The message digest length (for MBEDTLS_MD_NONE only).
202 * \param hash The buffer holding the message digest.
203 * \param sig The buffer that will hold the ciphertext.
204 *
205 * \return \c 0 if the signing operation was successful.
206 * \return A non-zero error code on failure.
207 *
208 * \note The \p sig buffer must be as large as the size of
Andres Amaya Garcia312431b2019-01-16 19:14:02 +0000209 * <code>ctx->N</code>. For example, 128 bytes if RSA-1024 is
Andres Amaya Garciab37268d2019-01-15 19:23:42 +0000210 * used.
211 */
Andres Amaya Garcia0d6e1082018-12-05 20:52:29 +0000212MBEDTLS_DEPRECATED static inline int mbedtls_ssl_pkcs11_sign( void *ctx,
213 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
214 int mode, mbedtls_md_type_t md_alg, unsigned int hashlen,
215 const unsigned char *hash, unsigned char *sig )
Paul Bakkereb2c6582012-09-27 19:15:01 +0000216{
217 ((void) f_rng);
218 ((void) p_rng);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200219 return mbedtls_pkcs11_sign( (mbedtls_pkcs11_context *) ctx, mode, md_alg,
Paul Bakkereb2c6582012-09-27 19:15:01 +0000220 hashlen, hash, sig );
221}
222
Andres Amaya Garciab37268d2019-01-15 19:23:42 +0000223/**
224 * This function gets the length of the private key.
225 *
226 * \deprecated This function is deprecated and will be removed in a future
227 * version of the library.
228 *
229 * \param ctx The PKCS #11 context.
230 *
231 * \return The length of the private key.
232 */
Andres Amaya Garcia0d6e1082018-12-05 20:52:29 +0000233MBEDTLS_DEPRECATED static inline size_t mbedtls_ssl_pkcs11_key_len( void *ctx )
Paul Bakkereb2c6582012-09-27 19:15:01 +0000234{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200235 return ( (mbedtls_pkcs11_context *) ctx )->len;
Paul Bakkereb2c6582012-09-27 19:15:01 +0000236}
237
Andres Amaya Garcia0d6e1082018-12-05 20:52:29 +0000238#undef MBEDTLS_DEPRECATED
239
Andres Amaya Garcia9fc82ef2019-01-15 19:08:12 +0000240#endif /* MBEDTLS_DEPRECATED_REMOVED */
241
Paul Bakker407a0da2013-06-27 14:29:21 +0200242#ifdef __cplusplus
243}
244#endif
245
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200246#endif /* MBEDTLS_PKCS11_C */
Paul Bakker43b7e352011-01-18 15:27:19 +0000247
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200248#endif /* MBEDTLS_PKCS11_H */