blob: 2247bf792d1b63047723024fffe148e151e01ed3 [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
Paul Bakker43b7e352011-01-18 15:27:19 +000050/**
51 * Context for PKCS #11 private keys.
52 */
Dawid Drozd428cc522018-07-24 10:02:47 +020053typedef struct mbedtls_pkcs11_context
54{
Paul Bakker43b7e352011-01-18 15:27:19 +000055 pkcs11h_certificate_t pkcs11h_cert;
56 int len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020057} mbedtls_pkcs11_context;
Paul Bakker43b7e352011-01-18 15:27:19 +000058
Andres Amaya Garcia0d6e1082018-12-05 20:52:29 +000059/*
60 * NOTE: It is not necessary to enclose the definitions with
61 * MBEDTLS_DEPRECATED_REMOVED because we already have a check for the full
62 * MBEDTLS_PKCS11_C feature that makes compilation fail even before we get
63 * here.
64 */
65#if defined(MBEDTLS_DEPRECATED_WARNING)
66#define MBEDTLS_DEPRECATED __attribute__((deprecated))
67#else
68#define MBEDTLS_DEPRECATED
69#endif
70
Paul Bakker43b7e352011-01-18 15:27:19 +000071/**
Tillmann Karras588ad502015-09-25 04:27:22 +020072 * Initialize a mbedtls_pkcs11_context.
Manuel Pégourié-Gonnardeab147c2015-04-29 01:10:10 +020073 * (Just making memory references valid.)
Andres Amaya Garcia0d6e1082018-12-05 20:52:29 +000074 *
Andres Amaya Garcia99fc3872019-01-15 19:07:43 +000075 * \deprecated This function is deprecated and will be removed in a
76 * future version of the library.
Manuel Pégourié-Gonnardeab147c2015-04-29 01:10:10 +020077 */
Andres Amaya Garcia0d6e1082018-12-05 20:52:29 +000078MBEDTLS_DEPRECATED void mbedtls_pkcs11_init( mbedtls_pkcs11_context *ctx );
Manuel Pégourié-Gonnardeab147c2015-04-29 01:10:10 +020079
80/**
Manuel Pégourié-Gonnardb4fe3cb2015-01-22 16:11:05 +000081 * Fill in a mbed TLS certificate, based on the given PKCS11 helper certificate.
Paul Bakker43b7e352011-01-18 15:27:19 +000082 *
Andres Amaya Garcia99fc3872019-01-15 19:07:43 +000083 * \deprecated This function is deprecated and will be removed in a
84 * future version of the library.
Andres Amaya Garcia0d6e1082018-12-05 20:52:29 +000085 *
Paul Bakker43b7e352011-01-18 15:27:19 +000086 * \param cert X.509 certificate to fill
87 * \param pkcs11h_cert PKCS #11 helper certificate
88 *
89 * \return 0 on success.
90 */
Andres Amaya Garcia0d6e1082018-12-05 20:52:29 +000091MBEDTLS_DEPRECATED int mbedtls_pkcs11_x509_cert_bind( mbedtls_x509_crt *cert,
92 pkcs11h_certificate_t pkcs11h_cert );
Paul Bakker43b7e352011-01-18 15:27:19 +000093
94/**
Manuel Pégourié-Gonnardeab147c2015-04-29 01:10:10 +020095 * Set up a mbedtls_pkcs11_context storing the given certificate. Note that the
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020096 * mbedtls_pkcs11_context will take over control of the certificate, freeing it when
Paul Bakker43b7e352011-01-18 15:27:19 +000097 * done.
98 *
Andres Amaya Garcia99fc3872019-01-15 19:07:43 +000099 * \deprecated This function is deprecated and will be removed in a
100 * future version of the library.
Andres Amaya Garcia0d6e1082018-12-05 20:52:29 +0000101 *
Paul Bakker43b7e352011-01-18 15:27:19 +0000102 * \param priv_key Private key structure to fill.
103 * \param pkcs11_cert PKCS #11 helper certificate
104 *
105 * \return 0 on success
106 */
Andres Amaya Garcia0d6e1082018-12-05 20:52:29 +0000107MBEDTLS_DEPRECATED int mbedtls_pkcs11_priv_key_bind(
108 mbedtls_pkcs11_context *priv_key,
109 pkcs11h_certificate_t pkcs11_cert );
Paul Bakker43b7e352011-01-18 15:27:19 +0000110
111/**
112 * Free the contents of the given private key context. Note that the structure
113 * itself is not freed.
114 *
Andres Amaya Garcia99fc3872019-01-15 19:07:43 +0000115 * \deprecated This function is deprecated and will be removed in a
116 * future version of the library.
Andres Amaya Garcia0d6e1082018-12-05 20:52:29 +0000117 *
Paul Bakker43b7e352011-01-18 15:27:19 +0000118 * \param priv_key Private key structure to cleanup
119 */
Andres Amaya Garcia0d6e1082018-12-05 20:52:29 +0000120MBEDTLS_DEPRECATED void mbedtls_pkcs11_priv_key_free(
121 mbedtls_pkcs11_context *priv_key );
Paul Bakker43b7e352011-01-18 15:27:19 +0000122
123/**
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200124 * \brief Do an RSA private key decrypt, then remove the message
125 * padding
Paul Bakker43b7e352011-01-18 15:27:19 +0000126 *
Andres Amaya Garcia99fc3872019-01-15 19:07:43 +0000127 * \deprecated This function is deprecated and will be removed in a future
128 * version of the library.
Andres Amaya Garcia0d6e1082018-12-05 20:52:29 +0000129 *
Paul Bakker43b7e352011-01-18 15:27:19 +0000130 * \param ctx PKCS #11 context
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200131 * \param mode must be MBEDTLS_RSA_PRIVATE, for compatibility with rsa.c's signature
Paul Bakker43b7e352011-01-18 15:27:19 +0000132 * \param input buffer holding the encrypted data
133 * \param output buffer that will hold the plaintext
134 * \param olen will contain the plaintext length
135 * \param output_max_len maximum length of the output buffer
136 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200137 * \return 0 if successful, or an MBEDTLS_ERR_RSA_XXX error code
Paul Bakker43b7e352011-01-18 15:27:19 +0000138 *
139 * \note The output buffer must be as large as the size
140 * of ctx->N (eg. 128 bytes if RSA-1024 is used) otherwise
141 * an error is thrown.
142 */
Andres Amaya Garcia0d6e1082018-12-05 20:52:29 +0000143MBEDTLS_DEPRECATED int mbedtls_pkcs11_decrypt( mbedtls_pkcs11_context *ctx,
144 int mode, size_t *olen,
145 const unsigned char *input,
146 unsigned char *output,
147 size_t output_max_len );
Paul Bakker43b7e352011-01-18 15:27:19 +0000148
149/**
150 * \brief Do a private RSA to sign a message digest
151 *
Andres Amaya Garcia99fc3872019-01-15 19:07:43 +0000152 * \deprecated This function is deprecated and will be removed in a future
153 * version of the library.
Andres Amaya Garcia0d6e1082018-12-05 20:52:29 +0000154 *
Paul Bakker43b7e352011-01-18 15:27:19 +0000155 * \param ctx PKCS #11 context
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200156 * \param mode must be MBEDTLS_RSA_PRIVATE, for compatibility with rsa.c's signature
157 * \param md_alg a MBEDTLS_MD_XXX (use MBEDTLS_MD_NONE for signing raw data)
158 * \param hashlen message digest length (for MBEDTLS_MD_NONE only)
Paul Bakker43b7e352011-01-18 15:27:19 +0000159 * \param hash buffer holding the message digest
160 * \param sig buffer that will hold the ciphertext
161 *
162 * \return 0 if the signing operation was successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200163 * or an MBEDTLS_ERR_RSA_XXX error code
Paul Bakker43b7e352011-01-18 15:27:19 +0000164 *
165 * \note The "sig" buffer must be as large as the size
166 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
167 */
Andres Amaya Garcia0d6e1082018-12-05 20:52:29 +0000168MBEDTLS_DEPRECATED int mbedtls_pkcs11_sign( mbedtls_pkcs11_context *ctx,
169 int mode,
170 mbedtls_md_type_t md_alg,
171 unsigned int hashlen,
172 const unsigned char *hash,
173 unsigned char *sig );
Paul Bakker43b7e352011-01-18 15:27:19 +0000174
Paul Bakkereb2c6582012-09-27 19:15:01 +0000175/**
176 * SSL/TLS wrappers for PKCS#11 functions
Andres Amaya Garcia0d6e1082018-12-05 20:52:29 +0000177 *
Andres Amaya Garcia99fc3872019-01-15 19:07:43 +0000178 * \deprecated This function is deprecated and will be removed in a future
179 * version of the library.
Paul Bakkereb2c6582012-09-27 19:15:01 +0000180 */
Andres Amaya Garcia0d6e1082018-12-05 20:52:29 +0000181MBEDTLS_DEPRECATED static inline int mbedtls_ssl_pkcs11_decrypt( void *ctx,
182 int mode, size_t *olen,
183 const unsigned char *input, unsigned char *output,
184 size_t output_max_len )
Paul Bakkereb2c6582012-09-27 19:15:01 +0000185{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200186 return mbedtls_pkcs11_decrypt( (mbedtls_pkcs11_context *) ctx, mode, olen, input, output,
Paul Bakkereb2c6582012-09-27 19:15:01 +0000187 output_max_len );
188}
189
Andres Amaya Garcia0d6e1082018-12-05 20:52:29 +0000190MBEDTLS_DEPRECATED static inline int mbedtls_ssl_pkcs11_sign( void *ctx,
191 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
192 int mode, mbedtls_md_type_t md_alg, unsigned int hashlen,
193 const unsigned char *hash, unsigned char *sig )
Paul Bakkereb2c6582012-09-27 19:15:01 +0000194{
195 ((void) f_rng);
196 ((void) p_rng);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200197 return mbedtls_pkcs11_sign( (mbedtls_pkcs11_context *) ctx, mode, md_alg,
Paul Bakkereb2c6582012-09-27 19:15:01 +0000198 hashlen, hash, sig );
199}
200
Andres Amaya Garcia0d6e1082018-12-05 20:52:29 +0000201MBEDTLS_DEPRECATED static inline size_t mbedtls_ssl_pkcs11_key_len( void *ctx )
Paul Bakkereb2c6582012-09-27 19:15:01 +0000202{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200203 return ( (mbedtls_pkcs11_context *) ctx )->len;
Paul Bakkereb2c6582012-09-27 19:15:01 +0000204}
205
Andres Amaya Garcia0d6e1082018-12-05 20:52:29 +0000206#undef MBEDTLS_DEPRECATED
207
Paul Bakker407a0da2013-06-27 14:29:21 +0200208#ifdef __cplusplus
209}
210#endif
211
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200212#endif /* MBEDTLS_PKCS11_C */
Paul Bakker43b7e352011-01-18 15:27:19 +0000213
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200214#endif /* MBEDTLS_PKCS11_H */