blob: 7632929cb6463ac1077bbec739ae4d7c74399287 [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>
7 *
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02008 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02009 * SPDX-License-Identifier: Apache-2.0
10 *
11 * Licensed under the Apache License, Version 2.0 (the "License"); you may
12 * not use this file except in compliance with the License.
13 * You may obtain a copy of the License at
14 *
15 * http://www.apache.org/licenses/LICENSE-2.0
16 *
17 * Unless required by applicable law or agreed to in writing, software
18 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
19 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20 * See the License for the specific language governing permissions and
21 * limitations under the License.
Paul Bakker43b7e352011-01-18 15:27:19 +000022 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000023 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker43b7e352011-01-18 15:27:19 +000024 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020025#ifndef MBEDTLS_PKCS11_H
26#define MBEDTLS_PKCS11_H
Paul Bakker43b7e352011-01-18 15:27:19 +000027
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020028#if !defined(MBEDTLS_CONFIG_FILE)
Paul Bakker314052f2011-08-15 09:07:52 +000029#include "config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020030#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020031#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020032#endif
Paul Bakker43b7e352011-01-18 15:27:19 +000033
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020034#if defined(MBEDTLS_PKCS11_C)
Paul Bakker43b7e352011-01-18 15:27:19 +000035
Paul Bakkerc559c7a2013-09-18 14:13:26 +020036#include "x509_crt.h"
Paul Bakker43b7e352011-01-18 15:27:19 +000037
38#include <pkcs11-helper-1.0/pkcs11h-certificate.h>
39
Manuel Pégourié-Gonnard20af64d2015-07-07 18:33:39 +020040#if ( defined(__ARMCC_VERSION) || defined(_MSC_VER) ) && !defined(inline)
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
Paul Bakker43b7e352011-01-18 15:27:19 +000048/**
49 * Context for PKCS #11 private keys.
50 */
51typedef struct {
52 pkcs11h_certificate_t pkcs11h_cert;
53 int len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020054} mbedtls_pkcs11_context;
Paul Bakker43b7e352011-01-18 15:27:19 +000055
56/**
Tillmann Karras588ad502015-09-25 04:27:22 +020057 * Initialize a mbedtls_pkcs11_context.
Manuel Pégourié-Gonnardeab147c2015-04-29 01:10:10 +020058 * (Just making memory references valid.)
59 */
60void mbedtls_pkcs11_init( mbedtls_pkcs11_context *ctx );
61
62/**
Manuel Pégourié-Gonnardb4fe3cb2015-01-22 16:11:05 +000063 * Fill in a mbed TLS certificate, based on the given PKCS11 helper certificate.
Paul Bakker43b7e352011-01-18 15:27:19 +000064 *
65 * \param cert X.509 certificate to fill
66 * \param pkcs11h_cert PKCS #11 helper certificate
67 *
68 * \return 0 on success.
69 */
Manuel Pégourié-Gonnardeab147c2015-04-29 01:10:10 +020070int mbedtls_pkcs11_x509_cert_bind( mbedtls_x509_crt *cert, pkcs11h_certificate_t pkcs11h_cert );
Paul Bakker43b7e352011-01-18 15:27:19 +000071
72/**
Manuel Pégourié-Gonnardeab147c2015-04-29 01:10:10 +020073 * Set up a mbedtls_pkcs11_context storing the given certificate. Note that the
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020074 * mbedtls_pkcs11_context will take over control of the certificate, freeing it when
Paul Bakker43b7e352011-01-18 15:27:19 +000075 * done.
76 *
77 * \param priv_key Private key structure to fill.
78 * \param pkcs11_cert PKCS #11 helper certificate
79 *
80 * \return 0 on success
81 */
Manuel Pégourié-Gonnardeab147c2015-04-29 01:10:10 +020082int mbedtls_pkcs11_priv_key_bind( mbedtls_pkcs11_context *priv_key,
Paul Bakker43b7e352011-01-18 15:27:19 +000083 pkcs11h_certificate_t pkcs11_cert );
84
85/**
86 * Free the contents of the given private key context. Note that the structure
87 * itself is not freed.
88 *
89 * \param priv_key Private key structure to cleanup
90 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020091void mbedtls_pkcs11_priv_key_free( mbedtls_pkcs11_context *priv_key );
Paul Bakker43b7e352011-01-18 15:27:19 +000092
93/**
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +020094 * \brief Do an RSA private key decrypt, then remove the message
95 * padding
Paul Bakker43b7e352011-01-18 15:27:19 +000096 *
97 * \param ctx PKCS #11 context
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020098 * \param mode must be MBEDTLS_RSA_PRIVATE, for compatibility with rsa.c's signature
Paul Bakker43b7e352011-01-18 15:27:19 +000099 * \param input buffer holding the encrypted data
100 * \param output buffer that will hold the plaintext
101 * \param olen will contain the plaintext length
102 * \param output_max_len maximum length of the output buffer
103 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200104 * \return 0 if successful, or an MBEDTLS_ERR_RSA_XXX error code
Paul Bakker43b7e352011-01-18 15:27:19 +0000105 *
106 * \note The output buffer must be as large as the size
107 * of ctx->N (eg. 128 bytes if RSA-1024 is used) otherwise
108 * an error is thrown.
109 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200110int mbedtls_pkcs11_decrypt( mbedtls_pkcs11_context *ctx,
Paul Bakker23986e52011-04-24 08:57:21 +0000111 int mode, size_t *olen,
Paul Bakker43b7e352011-01-18 15:27:19 +0000112 const unsigned char *input,
113 unsigned char *output,
Paul Bakker9a736322012-11-14 12:39:52 +0000114 size_t output_max_len );
Paul Bakker43b7e352011-01-18 15:27:19 +0000115
116/**
117 * \brief Do a private RSA to sign a message digest
118 *
119 * \param ctx PKCS #11 context
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200120 * \param mode must be MBEDTLS_RSA_PRIVATE, for compatibility with rsa.c's signature
121 * \param md_alg a MBEDTLS_MD_XXX (use MBEDTLS_MD_NONE for signing raw data)
122 * \param hashlen message digest length (for MBEDTLS_MD_NONE only)
Paul Bakker43b7e352011-01-18 15:27:19 +0000123 * \param hash buffer holding the message digest
124 * \param sig buffer that will hold the ciphertext
125 *
126 * \return 0 if the signing operation was successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200127 * or an MBEDTLS_ERR_RSA_XXX error code
Paul Bakker43b7e352011-01-18 15:27:19 +0000128 *
129 * \note The "sig" buffer must be as large as the size
130 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
131 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200132int mbedtls_pkcs11_sign( mbedtls_pkcs11_context *ctx,
Paul Bakker43b7e352011-01-18 15:27:19 +0000133 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200134 mbedtls_md_type_t md_alg,
Paul Bakker23986e52011-04-24 08:57:21 +0000135 unsigned int hashlen,
Paul Bakker43b7e352011-01-18 15:27:19 +0000136 const unsigned char *hash,
137 unsigned char *sig );
138
Paul Bakkereb2c6582012-09-27 19:15:01 +0000139/**
140 * SSL/TLS wrappers for PKCS#11 functions
141 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200142static inline int mbedtls_ssl_pkcs11_decrypt( void *ctx, int mode, size_t *olen,
Paul Bakkereb2c6582012-09-27 19:15:01 +0000143 const unsigned char *input, unsigned char *output,
Paul Bakker9a736322012-11-14 12:39:52 +0000144 size_t output_max_len )
Paul Bakkereb2c6582012-09-27 19:15:01 +0000145{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200146 return mbedtls_pkcs11_decrypt( (mbedtls_pkcs11_context *) ctx, mode, olen, input, output,
Paul Bakkereb2c6582012-09-27 19:15:01 +0000147 output_max_len );
148}
149
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200150static inline int mbedtls_ssl_pkcs11_sign( void *ctx,
Paul Bakkereb2c6582012-09-27 19:15:01 +0000151 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200152 int mode, mbedtls_md_type_t md_alg, unsigned int hashlen,
Paul Bakkereb2c6582012-09-27 19:15:01 +0000153 const unsigned char *hash, unsigned char *sig )
154{
155 ((void) f_rng);
156 ((void) p_rng);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200157 return mbedtls_pkcs11_sign( (mbedtls_pkcs11_context *) ctx, mode, md_alg,
Paul Bakkereb2c6582012-09-27 19:15:01 +0000158 hashlen, hash, sig );
159}
160
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200161static inline size_t mbedtls_ssl_pkcs11_key_len( void *ctx )
Paul Bakkereb2c6582012-09-27 19:15:01 +0000162{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200163 return ( (mbedtls_pkcs11_context *) ctx )->len;
Paul Bakkereb2c6582012-09-27 19:15:01 +0000164}
165
Paul Bakker407a0da2013-06-27 14:29:21 +0200166#ifdef __cplusplus
167}
168#endif
169
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200170#endif /* MBEDTLS_PKCS11_C */
Paul Bakker43b7e352011-01-18 15:27:19 +0000171
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200172#endif /* MBEDTLS_PKCS11_H */