blob: 5fd952d929e3b410238851488745b004e2fb9872 [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 Bakker530927b2015-02-13 14:24:10 +01008 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Paul Bakker43b7e352011-01-18 15:27:19 +00009 *
Manuel Pégourié-Gonnarde12abf92015-01-28 17:13:45 +000010 * This file is part of mbed TLS (https://polarssl.org)
Paul Bakker43b7e352011-01-18 15:27:19 +000011 *
12 * 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
Paul Bakker314052f2011-08-15 09:07:52 +000029#include "config.h"
Paul Bakker43b7e352011-01-18 15:27:19 +000030
31#if defined(POLARSSL_PKCS11_C)
32
Paul Bakker314052f2011-08-15 09:07:52 +000033#include "x509.h"
Paul Bakker43b7e352011-01-18 15:27:19 +000034
35#include <pkcs11-helper-1.0/pkcs11h-certificate.h>
36
Manuel Pégourié-Gonnard01234052015-10-05 11:40:01 +010037#if ( defined(__ARMCC_VERSION) || defined(_MSC_VER) ) && \
38 !defined(inline) && !defined(__cplusplus)
Paul Bakkereb2c6582012-09-27 19:15:01 +000039#define inline __inline
40#endif /* __ARMCC_VERSION */
41#endif /*_MSC_VER */
42
Paul Bakker43b7e352011-01-18 15:27:19 +000043/**
44 * Context for PKCS #11 private keys.
45 */
46typedef struct {
47 pkcs11h_certificate_t pkcs11h_cert;
48 int len;
49} pkcs11_context;
50
51/**
52 * Fill in a PolarSSL certificate, based on the given PKCS11 helper certificate.
53 *
54 * \param cert X.509 certificate to fill
55 * \param pkcs11h_cert PKCS #11 helper certificate
56 *
57 * \return 0 on success.
58 */
59int pkcs11_x509_cert_init( x509_cert *cert, pkcs11h_certificate_t pkcs11h_cert );
60
61/**
62 * Initialise a pkcs11_context, storing the given certificate. Note that the
63 * pkcs11_context will take over control of the certificate, freeing it when
64 * done.
65 *
66 * \param priv_key Private key structure to fill.
67 * \param pkcs11_cert PKCS #11 helper certificate
68 *
69 * \return 0 on success
70 */
71int pkcs11_priv_key_init( pkcs11_context *priv_key,
72 pkcs11h_certificate_t pkcs11_cert );
73
74/**
75 * Free the contents of the given private key context. Note that the structure
76 * itself is not freed.
77 *
78 * \param priv_key Private key structure to cleanup
79 */
80void pkcs11_priv_key_free( pkcs11_context *priv_key );
81
82/**
83 * \brief Do an RSA private key decrypt, then remove the message padding
84 *
85 * \param ctx PKCS #11 context
86 * \param mode must be RSA_PRIVATE, for compatibility with rsa.c's signature
87 * \param input buffer holding the encrypted data
88 * \param output buffer that will hold the plaintext
89 * \param olen will contain the plaintext length
90 * \param output_max_len maximum length of the output buffer
91 *
92 * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code
93 *
94 * \note The output buffer must be as large as the size
95 * of ctx->N (eg. 128 bytes if RSA-1024 is used) otherwise
96 * an error is thrown.
97 */
98int pkcs11_decrypt( pkcs11_context *ctx,
Paul Bakker23986e52011-04-24 08:57:21 +000099 int mode, size_t *olen,
Paul Bakker43b7e352011-01-18 15:27:19 +0000100 const unsigned char *input,
101 unsigned char *output,
Paul Bakker9a736322012-11-14 12:39:52 +0000102 size_t output_max_len );
Paul Bakker43b7e352011-01-18 15:27:19 +0000103
104/**
105 * \brief Do a private RSA to sign a message digest
106 *
107 * \param ctx PKCS #11 context
108 * \param mode must be RSA_PRIVATE, for compatibility with rsa.c's signature
109 * \param hash_id SIG_RSA_RAW, SIG_RSA_MD{2,4,5} or SIG_RSA_SHA{1,224,256,384,512}
110 * \param hashlen message digest length (for SIG_RSA_RAW only)
111 * \param hash buffer holding the message digest
112 * \param sig buffer that will hold the ciphertext
113 *
114 * \return 0 if the signing operation was successful,
115 * or an POLARSSL_ERR_RSA_XXX error code
116 *
117 * \note The "sig" buffer must be as large as the size
118 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
119 */
120int pkcs11_sign( pkcs11_context *ctx,
121 int mode,
122 int hash_id,
Paul Bakker23986e52011-04-24 08:57:21 +0000123 unsigned int hashlen,
Paul Bakker43b7e352011-01-18 15:27:19 +0000124 const unsigned char *hash,
125 unsigned char *sig );
126
Paul Bakkereb2c6582012-09-27 19:15:01 +0000127/**
128 * SSL/TLS wrappers for PKCS#11 functions
129 */
Paul Bakker495830d2013-10-04 11:01:27 +0200130static inline int ssl_pkcs11_decrypt( void *ctx,
131 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
132 int mode, size_t *olen, const unsigned char *input,
133 unsigned char *output, size_t output_max_len )
Paul Bakkereb2c6582012-09-27 19:15:01 +0000134{
Paul Bakker495830d2013-10-04 11:01:27 +0200135 ((void) f_rng);
136 ((void) p_rng);
Paul Bakkereb2c6582012-09-27 19:15:01 +0000137 return pkcs11_decrypt( (pkcs11_context *) ctx, mode, olen, input, output,
138 output_max_len );
139}
140
Paul Bakker495830d2013-10-04 11:01:27 +0200141static inline int ssl_pkcs11_sign( void *ctx,
Paul Bakkereb2c6582012-09-27 19:15:01 +0000142 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
143 int mode, int hash_id, unsigned int hashlen,
144 const unsigned char *hash, unsigned char *sig )
145{
146 ((void) f_rng);
147 ((void) p_rng);
148 return pkcs11_sign( (pkcs11_context *) ctx, mode, hash_id,
149 hashlen, hash, sig );
150}
151
152static inline size_t ssl_pkcs11_key_len( void *ctx )
153{
154 return ( (pkcs11_context *) ctx )->len;
155}
156
Paul Bakker43b7e352011-01-18 15:27:19 +0000157#endif /* POLARSSL_PKCS11_C */
158
Paul Bakkercce9d772011-11-18 14:26:47 +0000159#endif /* POLARSSL_PKCS11_H */