blob: e84550fda79b427e2738fba7edfce04297334787 [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 *
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02008 * Copyright (C) 2006-2014, Brainspark B.V.
Paul Bakker43b7e352011-01-18 15:27:19 +00009 *
Manuel Pégourié-Gonnard967a2a52015-01-22 14:28:16 +000010 * This file is part of mbed TLS (http://www.polarssl.org)
Paul Bakker43b7e352011-01-18 15:27:19 +000011 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
12 *
13 * All rights reserved.
14 *
15 * This program is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation; either version 2 of the License, or
18 * (at your option) any later version.
19 *
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 *
25 * You should have received a copy of the GNU General Public License along
26 * with this program; if not, write to the Free Software Foundation, Inc.,
27 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
28 */
Paul Bakkercce9d772011-11-18 14:26:47 +000029#ifndef POLARSSL_PKCS11_H
30#define POLARSSL_PKCS11_H
Paul Bakker43b7e352011-01-18 15:27:19 +000031
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020032#if !defined(POLARSSL_CONFIG_FILE)
Paul Bakker314052f2011-08-15 09:07:52 +000033#include "config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020034#else
35#include POLARSSL_CONFIG_FILE
36#endif
Paul Bakker43b7e352011-01-18 15:27:19 +000037
38#if defined(POLARSSL_PKCS11_C)
39
Paul Bakkerc559c7a2013-09-18 14:13:26 +020040#include "x509_crt.h"
Paul Bakker43b7e352011-01-18 15:27:19 +000041
42#include <pkcs11-helper-1.0/pkcs11h-certificate.h>
43
Paul Bakkereb2c6582012-09-27 19:15:01 +000044#if defined(_MSC_VER) && !defined(inline)
45#define inline _inline
46#else
47#if defined(__ARMCC_VERSION) && !defined(inline)
48#define inline __inline
49#endif /* __ARMCC_VERSION */
50#endif /*_MSC_VER */
51
Paul Bakker407a0da2013-06-27 14:29:21 +020052#ifdef __cplusplus
53extern "C" {
54#endif
55
Paul Bakker43b7e352011-01-18 15:27:19 +000056/**
57 * Context for PKCS #11 private keys.
58 */
59typedef struct {
60 pkcs11h_certificate_t pkcs11h_cert;
61 int len;
62} pkcs11_context;
63
64/**
Manuel Pégourié-Gonnardb4fe3cb2015-01-22 16:11:05 +000065 * Fill in a mbed TLS certificate, based on the given PKCS11 helper certificate.
Paul Bakker43b7e352011-01-18 15:27:19 +000066 *
67 * \param cert X.509 certificate to fill
68 * \param pkcs11h_cert PKCS #11 helper certificate
69 *
70 * \return 0 on success.
71 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +020072int pkcs11_x509_cert_init( x509_crt *cert, pkcs11h_certificate_t pkcs11h_cert );
Paul Bakker43b7e352011-01-18 15:27:19 +000073
74/**
75 * Initialise a pkcs11_context, storing the given certificate. Note that the
76 * pkcs11_context will take over control of the certificate, freeing it when
77 * done.
78 *
79 * \param priv_key Private key structure to fill.
80 * \param pkcs11_cert PKCS #11 helper certificate
81 *
82 * \return 0 on success
83 */
84int pkcs11_priv_key_init( pkcs11_context *priv_key,
85 pkcs11h_certificate_t pkcs11_cert );
86
87/**
88 * Free the contents of the given private key context. Note that the structure
89 * itself is not freed.
90 *
91 * \param priv_key Private key structure to cleanup
92 */
93void pkcs11_priv_key_free( pkcs11_context *priv_key );
94
95/**
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +020096 * \brief Do an RSA private key decrypt, then remove the message
97 * padding
Paul Bakker43b7e352011-01-18 15:27:19 +000098 *
99 * \param ctx PKCS #11 context
100 * \param mode must be RSA_PRIVATE, for compatibility with rsa.c's signature
101 * \param input buffer holding the encrypted data
102 * \param output buffer that will hold the plaintext
103 * \param olen will contain the plaintext length
104 * \param output_max_len maximum length of the output buffer
105 *
106 * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code
107 *
108 * \note The output buffer must be as large as the size
109 * of ctx->N (eg. 128 bytes if RSA-1024 is used) otherwise
110 * an error is thrown.
111 */
112int pkcs11_decrypt( pkcs11_context *ctx,
Paul Bakker23986e52011-04-24 08:57:21 +0000113 int mode, size_t *olen,
Paul Bakker43b7e352011-01-18 15:27:19 +0000114 const unsigned char *input,
115 unsigned char *output,
Paul Bakker9a736322012-11-14 12:39:52 +0000116 size_t output_max_len );
Paul Bakker43b7e352011-01-18 15:27:19 +0000117
118/**
119 * \brief Do a private RSA to sign a message digest
120 *
121 * \param ctx PKCS #11 context
122 * \param mode must be RSA_PRIVATE, for compatibility with rsa.c's signature
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200123 * \param md_alg a POLARSSL_MD_* (use POLARSSL_MD_NONE for signing raw data)
124 * \param hashlen message digest length (for POLARSSL_MD_NONE only)
Paul Bakker43b7e352011-01-18 15:27:19 +0000125 * \param hash buffer holding the message digest
126 * \param sig buffer that will hold the ciphertext
127 *
128 * \return 0 if the signing operation was successful,
129 * or an POLARSSL_ERR_RSA_XXX error code
130 *
131 * \note The "sig" buffer must be as large as the size
132 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
133 */
134int pkcs11_sign( pkcs11_context *ctx,
135 int mode,
Steffan Karger28d81a02013-11-13 16:57:58 +0100136 md_type_t md_alg,
Paul Bakker23986e52011-04-24 08:57:21 +0000137 unsigned int hashlen,
Paul Bakker43b7e352011-01-18 15:27:19 +0000138 const unsigned char *hash,
139 unsigned char *sig );
140
Paul Bakkereb2c6582012-09-27 19:15:01 +0000141/**
142 * SSL/TLS wrappers for PKCS#11 functions
143 */
144static inline int ssl_pkcs11_decrypt( void *ctx, int mode, size_t *olen,
145 const unsigned char *input, unsigned char *output,
Paul Bakker9a736322012-11-14 12:39:52 +0000146 size_t output_max_len )
Paul Bakkereb2c6582012-09-27 19:15:01 +0000147{
148 return pkcs11_decrypt( (pkcs11_context *) ctx, mode, olen, input, output,
149 output_max_len );
150}
151
Paul Bakker9af723c2014-05-01 13:03:14 +0200152static inline int ssl_pkcs11_sign( void *ctx,
Paul Bakkereb2c6582012-09-27 19:15:01 +0000153 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
Steffan Karger28d81a02013-11-13 16:57:58 +0100154 int mode, md_type_t md_alg, unsigned int hashlen,
Paul Bakkereb2c6582012-09-27 19:15:01 +0000155 const unsigned char *hash, unsigned char *sig )
156{
157 ((void) f_rng);
158 ((void) p_rng);
Steffan Karger28d81a02013-11-13 16:57:58 +0100159 return pkcs11_sign( (pkcs11_context *) ctx, mode, md_alg,
Paul Bakkereb2c6582012-09-27 19:15:01 +0000160 hashlen, hash, sig );
161}
162
163static inline size_t ssl_pkcs11_key_len( void *ctx )
164{
165 return ( (pkcs11_context *) ctx )->len;
166}
167
Paul Bakker407a0da2013-06-27 14:29:21 +0200168#ifdef __cplusplus
169}
170#endif
171
Paul Bakker43b7e352011-01-18 15:27:19 +0000172#endif /* POLARSSL_PKCS11_C */
173
Paul Bakkercce9d772011-11-18 14:26:47 +0000174#endif /* POLARSSL_PKCS11_H */