blob: c0515e67c446227c2afed9c316ad537f6fce8cb3 [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 Bakker407a0da2013-06-27 14:29:21 +02008 * Copyright (C) 2006-2013, Brainspark B.V.
Paul Bakker43b7e352011-01-18 15:27:19 +00009 *
10 * This file is part of PolarSSL (http://www.polarssl.org)
11 * 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
Paul Bakker314052f2011-08-15 09:07:52 +000032#include "config.h"
Paul Bakker43b7e352011-01-18 15:27:19 +000033
34#if defined(POLARSSL_PKCS11_C)
35
Paul Bakkerc559c7a2013-09-18 14:13:26 +020036#include "x509_crt.h"
Paul Bakker43b7e352011-01-18 15:27:19 +000037
38#include <pkcs11-helper-1.0/pkcs11h-certificate.h>
39
Paul Bakkereb2c6582012-09-27 19:15:01 +000040#if defined(_MSC_VER) && !defined(inline)
41#define inline _inline
42#else
43#if defined(__ARMCC_VERSION) && !defined(inline)
44#define inline __inline
45#endif /* __ARMCC_VERSION */
46#endif /*_MSC_VER */
47
Paul Bakker407a0da2013-06-27 14:29:21 +020048#ifdef __cplusplus
49extern "C" {
50#endif
51
Paul Bakker43b7e352011-01-18 15:27:19 +000052/**
53 * Context for PKCS #11 private keys.
54 */
55typedef struct {
56 pkcs11h_certificate_t pkcs11h_cert;
57 int len;
58} pkcs11_context;
59
60/**
61 * Fill in a PolarSSL certificate, based on the given PKCS11 helper certificate.
62 *
63 * \param cert X.509 certificate to fill
64 * \param pkcs11h_cert PKCS #11 helper certificate
65 *
66 * \return 0 on success.
67 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +020068int pkcs11_x509_cert_init( x509_crt *cert, pkcs11h_certificate_t pkcs11h_cert );
Paul Bakker43b7e352011-01-18 15:27:19 +000069
70/**
71 * Initialise a pkcs11_context, storing the given certificate. Note that the
72 * pkcs11_context will take over control of the certificate, freeing it when
73 * done.
74 *
75 * \param priv_key Private key structure to fill.
76 * \param pkcs11_cert PKCS #11 helper certificate
77 *
78 * \return 0 on success
79 */
80int pkcs11_priv_key_init( pkcs11_context *priv_key,
81 pkcs11h_certificate_t pkcs11_cert );
82
83/**
84 * Free the contents of the given private key context. Note that the structure
85 * itself is not freed.
86 *
87 * \param priv_key Private key structure to cleanup
88 */
89void pkcs11_priv_key_free( pkcs11_context *priv_key );
90
91/**
92 * \brief Do an RSA private key decrypt, then remove the message padding
93 *
94 * \param ctx PKCS #11 context
95 * \param mode must be RSA_PRIVATE, for compatibility with rsa.c's signature
96 * \param input buffer holding the encrypted data
97 * \param output buffer that will hold the plaintext
98 * \param olen will contain the plaintext length
99 * \param output_max_len maximum length of the output buffer
100 *
101 * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code
102 *
103 * \note The output buffer must be as large as the size
104 * of ctx->N (eg. 128 bytes if RSA-1024 is used) otherwise
105 * an error is thrown.
106 */
107int pkcs11_decrypt( pkcs11_context *ctx,
Paul Bakker23986e52011-04-24 08:57:21 +0000108 int mode, size_t *olen,
Paul Bakker43b7e352011-01-18 15:27:19 +0000109 const unsigned char *input,
110 unsigned char *output,
Paul Bakker9a736322012-11-14 12:39:52 +0000111 size_t output_max_len );
Paul Bakker43b7e352011-01-18 15:27:19 +0000112
113/**
114 * \brief Do a private RSA to sign a message digest
115 *
116 * \param ctx PKCS #11 context
117 * \param mode must be RSA_PRIVATE, for compatibility with rsa.c's signature
118 * \param hash_id SIG_RSA_RAW, SIG_RSA_MD{2,4,5} or SIG_RSA_SHA{1,224,256,384,512}
119 * \param hashlen message digest length (for SIG_RSA_RAW only)
120 * \param hash buffer holding the message digest
121 * \param sig buffer that will hold the ciphertext
122 *
123 * \return 0 if the signing operation was successful,
124 * or an POLARSSL_ERR_RSA_XXX error code
125 *
126 * \note The "sig" buffer must be as large as the size
127 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
128 */
129int pkcs11_sign( pkcs11_context *ctx,
130 int mode,
131 int hash_id,
Paul Bakker23986e52011-04-24 08:57:21 +0000132 unsigned int hashlen,
Paul Bakker43b7e352011-01-18 15:27:19 +0000133 const unsigned char *hash,
134 unsigned char *sig );
135
Paul Bakkereb2c6582012-09-27 19:15:01 +0000136/**
137 * SSL/TLS wrappers for PKCS#11 functions
138 */
139static inline int ssl_pkcs11_decrypt( void *ctx, int mode, size_t *olen,
140 const unsigned char *input, unsigned char *output,
Paul Bakker9a736322012-11-14 12:39:52 +0000141 size_t output_max_len )
Paul Bakkereb2c6582012-09-27 19:15:01 +0000142{
143 return pkcs11_decrypt( (pkcs11_context *) ctx, mode, olen, input, output,
144 output_max_len );
145}
146
147static inline int ssl_pkcs11_sign( void *ctx,
148 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
149 int mode, int hash_id, unsigned int hashlen,
150 const unsigned char *hash, unsigned char *sig )
151{
152 ((void) f_rng);
153 ((void) p_rng);
154 return pkcs11_sign( (pkcs11_context *) ctx, mode, hash_id,
155 hashlen, hash, sig );
156}
157
158static inline size_t ssl_pkcs11_key_len( void *ctx )
159{
160 return ( (pkcs11_context *) ctx )->len;
161}
162
Paul Bakker407a0da2013-06-27 14:29:21 +0200163#ifdef __cplusplus
164}
165#endif
166
Paul Bakker43b7e352011-01-18 15:27:19 +0000167#endif /* POLARSSL_PKCS11_C */
168
Paul Bakkercce9d772011-11-18 14:26:47 +0000169#endif /* POLARSSL_PKCS11_H */