blob: 18c3370fce5e0f3685fdfd0a987013437f58b16a [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 */
Paul Bakkercce9d772011-11-18 14:26:47 +000026#ifndef POLARSSL_PKCS11_H
27#define POLARSSL_PKCS11_H
Paul Bakker43b7e352011-01-18 15:27:19 +000028
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020029#if !defined(POLARSSL_CONFIG_FILE)
Paul Bakker314052f2011-08-15 09:07:52 +000030#include "config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020031#else
32#include POLARSSL_CONFIG_FILE
33#endif
Paul Bakker43b7e352011-01-18 15:27:19 +000034
35#if defined(POLARSSL_PKCS11_C)
36
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;
59} pkcs11_context;
60
61/**
Manuel Pégourié-Gonnardb4fe3cb2015-01-22 16:11:05 +000062 * Fill in a mbed TLS certificate, based on the given PKCS11 helper certificate.
Paul Bakker43b7e352011-01-18 15:27:19 +000063 *
64 * \param cert X.509 certificate to fill
65 * \param pkcs11h_cert PKCS #11 helper certificate
66 *
67 * \return 0 on success.
68 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +020069int pkcs11_x509_cert_init( x509_crt *cert, pkcs11h_certificate_t pkcs11h_cert );
Paul Bakker43b7e352011-01-18 15:27:19 +000070
71/**
72 * Initialise a pkcs11_context, storing the given certificate. Note that the
73 * pkcs11_context will take over control of the certificate, freeing it when
74 * done.
75 *
76 * \param priv_key Private key structure to fill.
77 * \param pkcs11_cert PKCS #11 helper certificate
78 *
79 * \return 0 on success
80 */
81int pkcs11_priv_key_init( pkcs11_context *priv_key,
82 pkcs11h_certificate_t pkcs11_cert );
83
84/**
85 * Free the contents of the given private key context. Note that the structure
86 * itself is not freed.
87 *
88 * \param priv_key Private key structure to cleanup
89 */
90void pkcs11_priv_key_free( pkcs11_context *priv_key );
91
92/**
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +020093 * \brief Do an RSA private key decrypt, then remove the message
94 * padding
Paul Bakker43b7e352011-01-18 15:27:19 +000095 *
96 * \param ctx PKCS #11 context
97 * \param mode must be RSA_PRIVATE, for compatibility with rsa.c's signature
98 * \param input buffer holding the encrypted data
99 * \param output buffer that will hold the plaintext
100 * \param olen will contain the plaintext length
101 * \param output_max_len maximum length of the output buffer
102 *
103 * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code
104 *
105 * \note The output buffer must be as large as the size
106 * of ctx->N (eg. 128 bytes if RSA-1024 is used) otherwise
107 * an error is thrown.
108 */
109int pkcs11_decrypt( pkcs11_context *ctx,
Paul Bakker23986e52011-04-24 08:57:21 +0000110 int mode, size_t *olen,
Paul Bakker43b7e352011-01-18 15:27:19 +0000111 const unsigned char *input,
112 unsigned char *output,
Paul Bakker9a736322012-11-14 12:39:52 +0000113 size_t output_max_len );
Paul Bakker43b7e352011-01-18 15:27:19 +0000114
115/**
116 * \brief Do a private RSA to sign a message digest
117 *
118 * \param ctx PKCS #11 context
119 * \param mode must be RSA_PRIVATE, for compatibility with rsa.c's signature
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200120 * \param md_alg a POLARSSL_MD_* (use POLARSSL_MD_NONE for signing raw data)
121 * \param hashlen message digest length (for POLARSSL_MD_NONE only)
Paul Bakker43b7e352011-01-18 15:27:19 +0000122 * \param hash buffer holding the message digest
123 * \param sig buffer that will hold the ciphertext
124 *
125 * \return 0 if the signing operation was successful,
126 * or an POLARSSL_ERR_RSA_XXX error code
127 *
128 * \note The "sig" buffer must be as large as the size
129 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
130 */
131int pkcs11_sign( pkcs11_context *ctx,
132 int mode,
Steffan Karger28d81a02013-11-13 16:57:58 +0100133 md_type_t md_alg,
Paul Bakker23986e52011-04-24 08:57:21 +0000134 unsigned int hashlen,
Paul Bakker43b7e352011-01-18 15:27:19 +0000135 const unsigned char *hash,
136 unsigned char *sig );
137
Paul Bakkereb2c6582012-09-27 19:15:01 +0000138/**
139 * SSL/TLS wrappers for PKCS#11 functions
140 */
141static inline int ssl_pkcs11_decrypt( void *ctx, int mode, size_t *olen,
142 const unsigned char *input, unsigned char *output,
Paul Bakker9a736322012-11-14 12:39:52 +0000143 size_t output_max_len )
Paul Bakkereb2c6582012-09-27 19:15:01 +0000144{
145 return pkcs11_decrypt( (pkcs11_context *) ctx, mode, olen, input, output,
146 output_max_len );
147}
148
Paul Bakker9af723c2014-05-01 13:03:14 +0200149static inline int ssl_pkcs11_sign( void *ctx,
Paul Bakkereb2c6582012-09-27 19:15:01 +0000150 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
Steffan Karger28d81a02013-11-13 16:57:58 +0100151 int mode, md_type_t md_alg, unsigned int hashlen,
Paul Bakkereb2c6582012-09-27 19:15:01 +0000152 const unsigned char *hash, unsigned char *sig )
153{
154 ((void) f_rng);
155 ((void) p_rng);
Steffan Karger28d81a02013-11-13 16:57:58 +0100156 return pkcs11_sign( (pkcs11_context *) ctx, mode, md_alg,
Paul Bakkereb2c6582012-09-27 19:15:01 +0000157 hashlen, hash, sig );
158}
159
160static inline size_t ssl_pkcs11_key_len( void *ctx )
161{
162 return ( (pkcs11_context *) ctx )->len;
163}
164
Paul Bakker407a0da2013-06-27 14:29:21 +0200165#ifdef __cplusplus
166}
167#endif
168
Paul Bakker43b7e352011-01-18 15:27:19 +0000169#endif /* POLARSSL_PKCS11_C */
170
Paul Bakkercce9d772011-11-18 14:26:47 +0000171#endif /* POLARSSL_PKCS11_H */