blob: 3b07c84972fd9024ea69dc3b7335e952cba68d0d [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
Manuel Pégourié-Gonnardcfd1ba92015-10-05 14:57:01 +010040#endif
Paul Bakkereb2c6582012-09-27 19:15:01 +000041
Paul Bakker43b7e352011-01-18 15:27:19 +000042/**
43 * Context for PKCS #11 private keys.
44 */
45typedef struct {
46 pkcs11h_certificate_t pkcs11h_cert;
47 int len;
48} pkcs11_context;
49
50/**
51 * Fill in a PolarSSL certificate, based on the given PKCS11 helper certificate.
52 *
53 * \param cert X.509 certificate to fill
54 * \param pkcs11h_cert PKCS #11 helper certificate
55 *
56 * \return 0 on success.
57 */
58int pkcs11_x509_cert_init( x509_cert *cert, pkcs11h_certificate_t pkcs11h_cert );
59
60/**
61 * Initialise a pkcs11_context, storing the given certificate. Note that the
62 * pkcs11_context will take over control of the certificate, freeing it when
63 * done.
64 *
65 * \param priv_key Private key structure to fill.
66 * \param pkcs11_cert PKCS #11 helper certificate
67 *
68 * \return 0 on success
69 */
70int pkcs11_priv_key_init( pkcs11_context *priv_key,
71 pkcs11h_certificate_t pkcs11_cert );
72
73/**
74 * Free the contents of the given private key context. Note that the structure
75 * itself is not freed.
76 *
77 * \param priv_key Private key structure to cleanup
78 */
79void pkcs11_priv_key_free( pkcs11_context *priv_key );
80
81/**
82 * \brief Do an RSA private key decrypt, then remove the message padding
83 *
84 * \param ctx PKCS #11 context
85 * \param mode must be RSA_PRIVATE, for compatibility with rsa.c's signature
86 * \param input buffer holding the encrypted data
87 * \param output buffer that will hold the plaintext
88 * \param olen will contain the plaintext length
89 * \param output_max_len maximum length of the output buffer
90 *
91 * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code
92 *
93 * \note The output buffer must be as large as the size
94 * of ctx->N (eg. 128 bytes if RSA-1024 is used) otherwise
95 * an error is thrown.
96 */
97int pkcs11_decrypt( pkcs11_context *ctx,
Paul Bakker23986e52011-04-24 08:57:21 +000098 int mode, size_t *olen,
Paul Bakker43b7e352011-01-18 15:27:19 +000099 const unsigned char *input,
100 unsigned char *output,
Paul Bakker9a736322012-11-14 12:39:52 +0000101 size_t output_max_len );
Paul Bakker43b7e352011-01-18 15:27:19 +0000102
103/**
104 * \brief Do a private RSA to sign a message digest
105 *
106 * \param ctx PKCS #11 context
107 * \param mode must be RSA_PRIVATE, for compatibility with rsa.c's signature
108 * \param hash_id SIG_RSA_RAW, SIG_RSA_MD{2,4,5} or SIG_RSA_SHA{1,224,256,384,512}
109 * \param hashlen message digest length (for SIG_RSA_RAW only)
110 * \param hash buffer holding the message digest
111 * \param sig buffer that will hold the ciphertext
112 *
113 * \return 0 if the signing operation was successful,
114 * or an POLARSSL_ERR_RSA_XXX error code
115 *
116 * \note The "sig" buffer must be as large as the size
117 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
118 */
119int pkcs11_sign( pkcs11_context *ctx,
120 int mode,
121 int hash_id,
Paul Bakker23986e52011-04-24 08:57:21 +0000122 unsigned int hashlen,
Paul Bakker43b7e352011-01-18 15:27:19 +0000123 const unsigned char *hash,
124 unsigned char *sig );
125
Paul Bakkereb2c6582012-09-27 19:15:01 +0000126/**
127 * SSL/TLS wrappers for PKCS#11 functions
128 */
Paul Bakker495830d2013-10-04 11:01:27 +0200129static inline int ssl_pkcs11_decrypt( void *ctx,
130 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
131 int mode, size_t *olen, const unsigned char *input,
132 unsigned char *output, size_t output_max_len )
Paul Bakkereb2c6582012-09-27 19:15:01 +0000133{
Paul Bakker495830d2013-10-04 11:01:27 +0200134 ((void) f_rng);
135 ((void) p_rng);
Paul Bakkereb2c6582012-09-27 19:15:01 +0000136 return pkcs11_decrypt( (pkcs11_context *) ctx, mode, olen, input, output,
137 output_max_len );
138}
139
Paul Bakker495830d2013-10-04 11:01:27 +0200140static inline int ssl_pkcs11_sign( void *ctx,
Paul Bakkereb2c6582012-09-27 19:15:01 +0000141 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
142 int mode, int hash_id, unsigned int hashlen,
143 const unsigned char *hash, unsigned char *sig )
144{
145 ((void) f_rng);
146 ((void) p_rng);
147 return pkcs11_sign( (pkcs11_context *) ctx, mode, hash_id,
148 hashlen, hash, sig );
149}
150
151static inline size_t ssl_pkcs11_key_len( void *ctx )
152{
153 return ( (pkcs11_context *) ctx )->len;
154}
155
Paul Bakker43b7e352011-01-18 15:27:19 +0000156#endif /* POLARSSL_PKCS11_C */
157
Paul Bakkercce9d772011-11-18 14:26:47 +0000158#endif /* POLARSSL_PKCS11_H */