blob: c620722e383bf7b7022ee5e25ef6abe99b755015 [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úti44bfbe32020-08-19 16:54:51 +02009 * Copyright The Mbed TLS Contributors
Bence Szépkúti4e9f7122020-06-05 13:02:18 +020010 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
11 *
12 * This file is provided under the Apache License 2.0, or the
13 * GNU General Public License v2.0 or later.
14 *
15 * **********
16 * Apache License 2.0:
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +020017 *
18 * Licensed under the Apache License, Version 2.0 (the "License"); you may
19 * not use this file except in compliance with the License.
20 * You may obtain a copy of the License at
21 *
22 * http://www.apache.org/licenses/LICENSE-2.0
23 *
24 * Unless required by applicable law or agreed to in writing, software
25 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
26 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
27 * See the License for the specific language governing permissions and
28 * limitations under the License.
Paul Bakker43b7e352011-01-18 15:27:19 +000029 *
Bence Szépkúti4e9f7122020-06-05 13:02:18 +020030 * **********
31 *
32 * **********
33 * GNU General Public License v2.0 or later:
34 *
35 * This program is free software; you can redistribute it and/or modify
36 * it under the terms of the GNU General Public License as published by
37 * the Free Software Foundation; either version 2 of the License, or
38 * (at your option) any later version.
39 *
40 * This program is distributed in the hope that it will be useful,
41 * but WITHOUT ANY WARRANTY; without even the implied warranty of
42 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
43 * GNU General Public License for more details.
44 *
45 * You should have received a copy of the GNU General Public License along
46 * with this program; if not, write to the Free Software Foundation, Inc.,
47 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
48 *
49 * **********
Paul Bakker43b7e352011-01-18 15:27:19 +000050 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020051#ifndef MBEDTLS_PKCS11_H
52#define MBEDTLS_PKCS11_H
Paul Bakker43b7e352011-01-18 15:27:19 +000053
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020054#if !defined(MBEDTLS_CONFIG_FILE)
Paul Bakker314052f2011-08-15 09:07:52 +000055#include "config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020056#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020057#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020058#endif
Paul Bakker43b7e352011-01-18 15:27:19 +000059
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020060#if defined(MBEDTLS_PKCS11_C)
Paul Bakker43b7e352011-01-18 15:27:19 +000061
Paul Bakkerc559c7a2013-09-18 14:13:26 +020062#include "x509_crt.h"
Paul Bakker43b7e352011-01-18 15:27:19 +000063
64#include <pkcs11-helper-1.0/pkcs11h-certificate.h>
65
Manuel Pégourié-Gonnard0223ab92015-10-05 11:40:01 +010066#if ( defined(__ARMCC_VERSION) || defined(_MSC_VER) ) && \
67 !defined(inline) && !defined(__cplusplus)
Paul Bakkereb2c6582012-09-27 19:15:01 +000068#define inline __inline
Manuel Pégourié-Gonnard20af64d2015-07-07 18:33:39 +020069#endif
Paul Bakkereb2c6582012-09-27 19:15:01 +000070
Paul Bakker407a0da2013-06-27 14:29:21 +020071#ifdef __cplusplus
72extern "C" {
73#endif
74
Paul Bakker43b7e352011-01-18 15:27:19 +000075/**
76 * Context for PKCS #11 private keys.
77 */
78typedef struct {
79 pkcs11h_certificate_t pkcs11h_cert;
80 int len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020081} mbedtls_pkcs11_context;
Paul Bakker43b7e352011-01-18 15:27:19 +000082
83/**
Tillmann Karras588ad502015-09-25 04:27:22 +020084 * Initialize a mbedtls_pkcs11_context.
Manuel Pégourié-Gonnardeab147c2015-04-29 01:10:10 +020085 * (Just making memory references valid.)
86 */
87void mbedtls_pkcs11_init( mbedtls_pkcs11_context *ctx );
88
89/**
Manuel Pégourié-Gonnardb4fe3cb2015-01-22 16:11:05 +000090 * Fill in a mbed TLS certificate, based on the given PKCS11 helper certificate.
Paul Bakker43b7e352011-01-18 15:27:19 +000091 *
92 * \param cert X.509 certificate to fill
93 * \param pkcs11h_cert PKCS #11 helper certificate
94 *
95 * \return 0 on success.
96 */
Manuel Pégourié-Gonnardeab147c2015-04-29 01:10:10 +020097int mbedtls_pkcs11_x509_cert_bind( mbedtls_x509_crt *cert, pkcs11h_certificate_t pkcs11h_cert );
Paul Bakker43b7e352011-01-18 15:27:19 +000098
99/**
Manuel Pégourié-Gonnardeab147c2015-04-29 01:10:10 +0200100 * Set up a mbedtls_pkcs11_context storing the given certificate. Note that the
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200101 * mbedtls_pkcs11_context will take over control of the certificate, freeing it when
Paul Bakker43b7e352011-01-18 15:27:19 +0000102 * done.
103 *
104 * \param priv_key Private key structure to fill.
105 * \param pkcs11_cert PKCS #11 helper certificate
106 *
107 * \return 0 on success
108 */
Manuel Pégourié-Gonnardeab147c2015-04-29 01:10:10 +0200109int mbedtls_pkcs11_priv_key_bind( mbedtls_pkcs11_context *priv_key,
Paul Bakker43b7e352011-01-18 15:27:19 +0000110 pkcs11h_certificate_t pkcs11_cert );
111
112/**
113 * Free the contents of the given private key context. Note that the structure
114 * itself is not freed.
115 *
116 * \param priv_key Private key structure to cleanup
117 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200118void mbedtls_pkcs11_priv_key_free( mbedtls_pkcs11_context *priv_key );
Paul Bakker43b7e352011-01-18 15:27:19 +0000119
120/**
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200121 * \brief Do an RSA private key decrypt, then remove the message
122 * padding
Paul Bakker43b7e352011-01-18 15:27:19 +0000123 *
124 * \param ctx PKCS #11 context
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200125 * \param mode must be MBEDTLS_RSA_PRIVATE, for compatibility with rsa.c's signature
Paul Bakker43b7e352011-01-18 15:27:19 +0000126 * \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é-Gonnard2cf5a7c2015-04-08 12:49:31 +0200131 * \return 0 if successful, or an MBEDTLS_ERR_RSA_XXX error code
Paul Bakker43b7e352011-01-18 15:27:19 +0000132 *
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 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200137int mbedtls_pkcs11_decrypt( mbedtls_pkcs11_context *ctx,
Paul Bakker23986e52011-04-24 08:57:21 +0000138 int mode, size_t *olen,
Paul Bakker43b7e352011-01-18 15:27:19 +0000139 const unsigned char *input,
140 unsigned char *output,
Paul Bakker9a736322012-11-14 12:39:52 +0000141 size_t output_max_len );
Paul Bakker43b7e352011-01-18 15:27:19 +0000142
143/**
144 * \brief Do a private RSA to sign a message digest
145 *
146 * \param ctx PKCS #11 context
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200147 * \param mode must be MBEDTLS_RSA_PRIVATE, for compatibility with rsa.c's signature
148 * \param md_alg a MBEDTLS_MD_XXX (use MBEDTLS_MD_NONE for signing raw data)
149 * \param hashlen message digest length (for MBEDTLS_MD_NONE only)
Paul Bakker43b7e352011-01-18 15:27:19 +0000150 * \param hash buffer holding the message digest
151 * \param sig buffer that will hold the ciphertext
152 *
153 * \return 0 if the signing operation was successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200154 * or an MBEDTLS_ERR_RSA_XXX error code
Paul Bakker43b7e352011-01-18 15:27:19 +0000155 *
156 * \note The "sig" buffer must be as large as the size
157 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
158 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200159int mbedtls_pkcs11_sign( mbedtls_pkcs11_context *ctx,
Paul Bakker43b7e352011-01-18 15:27:19 +0000160 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200161 mbedtls_md_type_t md_alg,
Paul Bakker23986e52011-04-24 08:57:21 +0000162 unsigned int hashlen,
Paul Bakker43b7e352011-01-18 15:27:19 +0000163 const unsigned char *hash,
164 unsigned char *sig );
165
Paul Bakkereb2c6582012-09-27 19:15:01 +0000166/**
167 * SSL/TLS wrappers for PKCS#11 functions
168 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200169static inline int mbedtls_ssl_pkcs11_decrypt( void *ctx, int mode, size_t *olen,
Paul Bakkereb2c6582012-09-27 19:15:01 +0000170 const unsigned char *input, unsigned char *output,
Paul Bakker9a736322012-11-14 12:39:52 +0000171 size_t output_max_len )
Paul Bakkereb2c6582012-09-27 19:15:01 +0000172{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200173 return mbedtls_pkcs11_decrypt( (mbedtls_pkcs11_context *) ctx, mode, olen, input, output,
Paul Bakkereb2c6582012-09-27 19:15:01 +0000174 output_max_len );
175}
176
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200177static inline int mbedtls_ssl_pkcs11_sign( void *ctx,
Paul Bakkereb2c6582012-09-27 19:15:01 +0000178 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200179 int mode, mbedtls_md_type_t md_alg, unsigned int hashlen,
Paul Bakkereb2c6582012-09-27 19:15:01 +0000180 const unsigned char *hash, unsigned char *sig )
181{
182 ((void) f_rng);
183 ((void) p_rng);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200184 return mbedtls_pkcs11_sign( (mbedtls_pkcs11_context *) ctx, mode, md_alg,
Paul Bakkereb2c6582012-09-27 19:15:01 +0000185 hashlen, hash, sig );
186}
187
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200188static inline size_t mbedtls_ssl_pkcs11_key_len( void *ctx )
Paul Bakkereb2c6582012-09-27 19:15:01 +0000189{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200190 return ( (mbedtls_pkcs11_context *) ctx )->len;
Paul Bakkereb2c6582012-09-27 19:15:01 +0000191}
192
Paul Bakker407a0da2013-06-27 14:29:21 +0200193#ifdef __cplusplus
194}
195#endif
196
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200197#endif /* MBEDTLS_PKCS11_C */
Paul Bakker43b7e352011-01-18 15:27:19 +0000198
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200199#endif /* MBEDTLS_PKCS11_H */