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