1 /**
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 */
8 /*
9 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
10 * SPDX-License-Identifier: GPL-2.0
11 *
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 *
26 * This file is part of mbed TLS (https://tls.mbed.org)
27 */
28 #ifndef MBEDTLS_PKCS11_H
29 #define MBEDTLS_PKCS11_H
30
31 #if !defined(MBEDTLS_CONFIG_FILE)
32 #include "config.h"
33 #else
34 #include MBEDTLS_CONFIG_FILE
35 #endif
36
37 #if defined(MBEDTLS_PKCS11_C)
38
39 #include "x509_crt.h"
40
41 #include <pkcs11-helper-1.0/pkcs11h-certificate.h>
42
43 #if ( defined(__ARMCC_VERSION) || defined(_MSC_VER) ) && \
44 !defined(inline) && !defined(__cplusplus)
45 #define inline __inline
46 #endif
47
48 #ifdef __cplusplus
49 extern "C" {
50 #endif
51
52 /**
53 * Context for PKCS #11 private keys.
54 */
55 typedef struct {
56 pkcs11h_certificate_t pkcs11h_cert;
57 int len;
58 } mbedtls_pkcs11_context;
59
60 /**
61 * Initialize a mbedtls_pkcs11_context.
62 * (Just making memory references valid.)
63 */
64 void mbedtls_pkcs11_init( mbedtls_pkcs11_context *ctx );
65
66 /**
67 * Fill in a mbed TLS certificate, based on the given PKCS11 helper certificate.
68 *
69 * \param cert X.509 certificate to fill
70 * \param pkcs11h_cert PKCS #11 helper certificate
71 *
72 * \return 0 on success.
73 */
74 int mbedtls_pkcs11_x509_cert_bind( mbedtls_x509_crt *cert, pkcs11h_certificate_t pkcs11h_cert );
75
76 /**
77 * Set up a mbedtls_pkcs11_context storing the given certificate. Note that the
78 * mbedtls_pkcs11_context will take over control of the certificate, freeing it when
79 * done.
80 *
81 * \param priv_key Private key structure to fill.
82 * \param pkcs11_cert PKCS #11 helper certificate
83 *
84 * \return 0 on success
85 */
86 int mbedtls_pkcs11_priv_key_bind( mbedtls_pkcs11_context *priv_key,
87 pkcs11h_certificate_t pkcs11_cert );
88
89 /**
90 * Free the contents of the given private key context. Note that the structure
91 * itself is not freed.
92 *
93 * \param priv_key Private key structure to cleanup
94 */
95 void mbedtls_pkcs11_priv_key_free( mbedtls_pkcs11_context *priv_key );
96
97 /**
98 * \brief Do an RSA private key decrypt, then remove the message
99 * padding
100 *
101 * \param ctx PKCS #11 context
102 * \param mode must be MBEDTLS_RSA_PRIVATE, for compatibility with rsa.c's signature
103 * \param input buffer holding the encrypted data
104 * \param output buffer that will hold the plaintext
105 * \param olen will contain the plaintext length
106 * \param output_max_len maximum length of the output buffer
107 *
108 * \return 0 if successful, or an MBEDTLS_ERR_RSA_XXX error code
109 *
110 * \note The output buffer must be as large as the size
111 * of ctx->N (eg. 128 bytes if RSA-1024 is used) otherwise
112 * an error is thrown.
113 */
114 int mbedtls_pkcs11_decrypt( mbedtls_pkcs11_context *ctx,
115 int mode, size_t *olen,
116 const unsigned char *input,
117 unsigned char *output,
118 size_t output_max_len );
119
120 /**
121 * \brief Do a private RSA to sign a message digest
122 *
123 * \param ctx PKCS #11 context
124 * \param mode must be MBEDTLS_RSA_PRIVATE, for compatibility with rsa.c's signature
125 * \param md_alg a MBEDTLS_MD_XXX (use MBEDTLS_MD_NONE for signing raw data)
126 * \param hashlen message digest length (for MBEDTLS_MD_NONE only)
127 * \param hash buffer holding the message digest
128 * \param sig buffer that will hold the ciphertext
129 *
130 * \return 0 if the signing operation was successful,
131 * or an MBEDTLS_ERR_RSA_XXX error code
132 *
133 * \note The "sig" buffer must be as large as the size
134 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
135 */
136 int mbedtls_pkcs11_sign( mbedtls_pkcs11_context *ctx,
137 int mode,
138 mbedtls_md_type_t md_alg,
139 unsigned int hashlen,
140 const unsigned char *hash,
141 unsigned char *sig );
142
143 /**
144 * SSL/TLS wrappers for PKCS#11 functions
145 */
mbedtls_ssl_pkcs11_decrypt(void * ctx,int mode,size_t * olen,const unsigned char * input,unsigned char * output,size_t output_max_len)146 static inline int mbedtls_ssl_pkcs11_decrypt( void *ctx, int mode, size_t *olen,
147 const unsigned char *input, unsigned char *output,
148 size_t output_max_len )
149 {
150 return mbedtls_pkcs11_decrypt( (mbedtls_pkcs11_context *) ctx, mode, olen, input, output,
151 output_max_len );
152 }
153
mbedtls_ssl_pkcs11_sign(void * ctx,int (* f_rng)(void *,unsigned char *,size_t),void * p_rng,int mode,mbedtls_md_type_t md_alg,unsigned int hashlen,const unsigned char * hash,unsigned char * sig)154 static inline int mbedtls_ssl_pkcs11_sign( void *ctx,
155 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
156 int mode, mbedtls_md_type_t md_alg, unsigned int hashlen,
157 const unsigned char *hash, unsigned char *sig )
158 {
159 ((void) f_rng);
160 ((void) p_rng);
161 return mbedtls_pkcs11_sign( (mbedtls_pkcs11_context *) ctx, mode, md_alg,
162 hashlen, hash, sig );
163 }
164
mbedtls_ssl_pkcs11_key_len(void * ctx)165 static inline size_t mbedtls_ssl_pkcs11_key_len( void *ctx )
166 {
167 return ( (mbedtls_pkcs11_context *) ctx )->len;
168 }
169
170 #ifdef __cplusplus
171 }
172 #endif
173
174 #endif /* MBEDTLS_PKCS11_C */
175
176 #endif /* MBEDTLS_PKCS11_H */
177