blob: 5d4cc1fe5e6ddd51fade776d1f37c7804906a05c [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é-Gonnarda658a402015-01-23 09:45:19 +00008 * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved
Paul Bakker43b7e352011-01-18 15:27:19 +00009 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000010 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker43b7e352011-01-18 15:27:19 +000011 *
Paul Bakker43b7e352011-01-18 15:27:19 +000012 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License along
23 * with this program; if not, write to the Free Software Foundation, Inc.,
24 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 */
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)
Paul Bakker314052f2011-08-15 09:07:52 +000030#include "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
Paul Bakkerc559c7a2013-09-18 14:13:26 +020037#include "x509_crt.h"
Paul Bakker43b7e352011-01-18 15:27:19 +000038
39#include <pkcs11-helper-1.0/pkcs11h-certificate.h>
40
Paul Bakkereb2c6582012-09-27 19:15:01 +000041#if defined(_MSC_VER) && !defined(inline)
42#define inline _inline
43#else
44#if defined(__ARMCC_VERSION) && !defined(inline)
45#define inline __inline
46#endif /* __ARMCC_VERSION */
47#endif /*_MSC_VER */
48
Paul Bakker407a0da2013-06-27 14:29:21 +020049#ifdef __cplusplus
50extern "C" {
51#endif
52
Paul Bakker43b7e352011-01-18 15:27:19 +000053/**
54 * Context for PKCS #11 private keys.
55 */
56typedef struct {
57 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
61/**
Manuel Pégourié-Gonnardeab147c2015-04-29 01:10:10 +020062 * Initialize a mbetls_pkcs11_context.
63 * (Just making memory references valid.)
64 */
65void mbedtls_pkcs11_init( mbedtls_pkcs11_context *ctx );
66
67/**
Manuel Pégourié-Gonnardb4fe3cb2015-01-22 16:11:05 +000068 * Fill in a mbed TLS certificate, based on the given PKCS11 helper certificate.
Paul Bakker43b7e352011-01-18 15:27:19 +000069 *
70 * \param cert X.509 certificate to fill
71 * \param pkcs11h_cert PKCS #11 helper certificate
72 *
73 * \return 0 on success.
74 */
Manuel Pégourié-Gonnardeab147c2015-04-29 01:10:10 +020075int mbedtls_pkcs11_x509_cert_bind( mbedtls_x509_crt *cert, pkcs11h_certificate_t pkcs11h_cert );
Paul Bakker43b7e352011-01-18 15:27:19 +000076
77/**
Manuel Pégourié-Gonnardeab147c2015-04-29 01:10:10 +020078 * Set up a mbedtls_pkcs11_context storing the given certificate. Note that the
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020079 * mbedtls_pkcs11_context will take over control of the certificate, freeing it when
Paul Bakker43b7e352011-01-18 15:27:19 +000080 * done.
81 *
82 * \param priv_key Private key structure to fill.
83 * \param pkcs11_cert PKCS #11 helper certificate
84 *
85 * \return 0 on success
86 */
Manuel Pégourié-Gonnardeab147c2015-04-29 01:10:10 +020087int mbedtls_pkcs11_priv_key_bind( mbedtls_pkcs11_context *priv_key,
Paul Bakker43b7e352011-01-18 15:27:19 +000088 pkcs11h_certificate_t pkcs11_cert );
89
90/**
91 * Free the contents of the given private key context. Note that the structure
92 * itself is not freed.
93 *
94 * \param priv_key Private key structure to cleanup
95 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020096void mbedtls_pkcs11_priv_key_free( mbedtls_pkcs11_context *priv_key );
Paul Bakker43b7e352011-01-18 15:27:19 +000097
98/**
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +020099 * \brief Do an RSA private key decrypt, then remove the message
100 * padding
Paul Bakker43b7e352011-01-18 15:27:19 +0000101 *
102 * \param ctx PKCS #11 context
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200103 * \param mode must be MBEDTLS_RSA_PRIVATE, for compatibility with rsa.c's signature
Paul Bakker43b7e352011-01-18 15:27:19 +0000104 * \param input buffer holding the encrypted data
105 * \param output buffer that will hold the plaintext
106 * \param olen will contain the plaintext length
107 * \param output_max_len maximum length of the output buffer
108 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200109 * \return 0 if successful, or an MBEDTLS_ERR_RSA_XXX error code
Paul Bakker43b7e352011-01-18 15:27:19 +0000110 *
111 * \note The output buffer must be as large as the size
112 * of ctx->N (eg. 128 bytes if RSA-1024 is used) otherwise
113 * an error is thrown.
114 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200115int mbedtls_pkcs11_decrypt( mbedtls_pkcs11_context *ctx,
Paul Bakker23986e52011-04-24 08:57:21 +0000116 int mode, size_t *olen,
Paul Bakker43b7e352011-01-18 15:27:19 +0000117 const unsigned char *input,
118 unsigned char *output,
Paul Bakker9a736322012-11-14 12:39:52 +0000119 size_t output_max_len );
Paul Bakker43b7e352011-01-18 15:27:19 +0000120
121/**
122 * \brief Do a private RSA to sign a message digest
123 *
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
126 * \param md_alg a MBEDTLS_MD_XXX (use MBEDTLS_MD_NONE for signing raw data)
127 * \param hashlen message digest length (for MBEDTLS_MD_NONE only)
Paul Bakker43b7e352011-01-18 15:27:19 +0000128 * \param hash buffer holding the message digest
129 * \param sig buffer that will hold the ciphertext
130 *
131 * \return 0 if the signing operation was successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200132 * or an MBEDTLS_ERR_RSA_XXX error code
Paul Bakker43b7e352011-01-18 15:27:19 +0000133 *
134 * \note The "sig" buffer must be as large as the size
135 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
136 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200137int mbedtls_pkcs11_sign( mbedtls_pkcs11_context *ctx,
Paul Bakker43b7e352011-01-18 15:27:19 +0000138 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200139 mbedtls_md_type_t md_alg,
Paul Bakker23986e52011-04-24 08:57:21 +0000140 unsigned int hashlen,
Paul Bakker43b7e352011-01-18 15:27:19 +0000141 const unsigned char *hash,
142 unsigned char *sig );
143
Paul Bakkereb2c6582012-09-27 19:15:01 +0000144/**
145 * SSL/TLS wrappers for PKCS#11 functions
146 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200147static inline int mbedtls_ssl_pkcs11_decrypt( void *ctx, int mode, size_t *olen,
Paul Bakkereb2c6582012-09-27 19:15:01 +0000148 const unsigned char *input, unsigned char *output,
Paul Bakker9a736322012-11-14 12:39:52 +0000149 size_t output_max_len )
Paul Bakkereb2c6582012-09-27 19:15:01 +0000150{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200151 return mbedtls_pkcs11_decrypt( (mbedtls_pkcs11_context *) ctx, mode, olen, input, output,
Paul Bakkereb2c6582012-09-27 19:15:01 +0000152 output_max_len );
153}
154
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200155static inline int mbedtls_ssl_pkcs11_sign( void *ctx,
Paul Bakkereb2c6582012-09-27 19:15:01 +0000156 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200157 int mode, mbedtls_md_type_t md_alg, unsigned int hashlen,
Paul Bakkereb2c6582012-09-27 19:15:01 +0000158 const unsigned char *hash, unsigned char *sig )
159{
160 ((void) f_rng);
161 ((void) p_rng);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200162 return mbedtls_pkcs11_sign( (mbedtls_pkcs11_context *) ctx, mode, md_alg,
Paul Bakkereb2c6582012-09-27 19:15:01 +0000163 hashlen, hash, sig );
164}
165
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200166static inline size_t mbedtls_ssl_pkcs11_key_len( void *ctx )
Paul Bakkereb2c6582012-09-27 19:15:01 +0000167{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200168 return ( (mbedtls_pkcs11_context *) ctx )->len;
Paul Bakkereb2c6582012-09-27 19:15:01 +0000169}
170
Paul Bakker407a0da2013-06-27 14:29:21 +0200171#ifdef __cplusplus
172}
173#endif
174
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200175#endif /* MBEDTLS_PKCS11_C */
Paul Bakker43b7e352011-01-18 15:27:19 +0000176
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200177#endif /* MBEDTLS_PKCS11_H */