]> git.sur5r.net Git - freertos/blob - FreeRTOS-Labs/Source/mbedtls/include/mbedtls/pkcs11.h
Add the Labs projects provided in the V10.2.1_191129 zip file.
[freertos] / FreeRTOS-Labs / Source / mbedtls / include / mbedtls / pkcs11.h
1 /**\r
2  * \file pkcs11.h\r
3  *\r
4  * \brief Wrapper for PKCS#11 library libpkcs11-helper\r
5  *\r
6  * \author Adriaan de Jong <dejong@fox-it.com>\r
7  */\r
8 /*\r
9  *  Copyright (C) 2006-2015, ARM Limited, All Rights Reserved\r
10  *  SPDX-License-Identifier: Apache-2.0\r
11  *\r
12  *  Licensed under the Apache License, Version 2.0 (the "License"); you may\r
13  *  not use this file except in compliance with the License.\r
14  *  You may obtain a copy of the License at\r
15  *\r
16  *  http://www.apache.org/licenses/LICENSE-2.0\r
17  *\r
18  *  Unless required by applicable law or agreed to in writing, software\r
19  *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT\r
20  *  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
21  *  See the License for the specific language governing permissions and\r
22  *  limitations under the License.\r
23  *\r
24  *  This file is part of mbed TLS (https://tls.mbed.org)\r
25  */\r
26 #ifndef MBEDTLS_PKCS11_H\r
27 #define MBEDTLS_PKCS11_H\r
28 \r
29 #if !defined(MBEDTLS_CONFIG_FILE)\r
30 #include "config.h"\r
31 #else\r
32 #include MBEDTLS_CONFIG_FILE\r
33 #endif\r
34 \r
35 #if defined(MBEDTLS_PKCS11_C)\r
36 \r
37 #include "x509_crt.h"\r
38 \r
39 #include <pkcs11-helper-1.0/pkcs11h-certificate.h>\r
40 \r
41 #if ( defined(__ARMCC_VERSION) || defined(_MSC_VER) ) && \\r
42     !defined(inline) && !defined(__cplusplus)\r
43 #define inline __inline\r
44 #endif\r
45 \r
46 #ifdef __cplusplus\r
47 extern "C" {\r
48 #endif\r
49 \r
50 /**\r
51  * Context for PKCS #11 private keys.\r
52  */\r
53 typedef struct mbedtls_pkcs11_context\r
54 {\r
55         pkcs11h_certificate_t pkcs11h_cert;\r
56         int len;\r
57 } mbedtls_pkcs11_context;\r
58 \r
59 /**\r
60  * Initialize a mbedtls_pkcs11_context.\r
61  * (Just making memory references valid.)\r
62  */\r
63 void mbedtls_pkcs11_init( mbedtls_pkcs11_context *ctx );\r
64 \r
65 /**\r
66  * Fill in a mbed TLS certificate, based on the given PKCS11 helper certificate.\r
67  *\r
68  * \param cert          X.509 certificate to fill\r
69  * \param pkcs11h_cert  PKCS #11 helper certificate\r
70  *\r
71  * \return              0 on success.\r
72  */\r
73 int mbedtls_pkcs11_x509_cert_bind( mbedtls_x509_crt *cert, pkcs11h_certificate_t pkcs11h_cert );\r
74 \r
75 /**\r
76  * Set up a mbedtls_pkcs11_context storing the given certificate. Note that the\r
77  * mbedtls_pkcs11_context will take over control of the certificate, freeing it when\r
78  * done.\r
79  *\r
80  * \param priv_key      Private key structure to fill.\r
81  * \param pkcs11_cert   PKCS #11 helper certificate\r
82  *\r
83  * \return              0 on success\r
84  */\r
85 int mbedtls_pkcs11_priv_key_bind( mbedtls_pkcs11_context *priv_key,\r
86         pkcs11h_certificate_t pkcs11_cert );\r
87 \r
88 /**\r
89  * Free the contents of the given private key context. Note that the structure\r
90  * itself is not freed.\r
91  *\r
92  * \param priv_key      Private key structure to cleanup\r
93  */\r
94 void mbedtls_pkcs11_priv_key_free( mbedtls_pkcs11_context *priv_key );\r
95 \r
96 /**\r
97  * \brief          Do an RSA private key decrypt, then remove the message\r
98  *                 padding\r
99  *\r
100  * \param ctx      PKCS #11 context\r
101  * \param mode     must be MBEDTLS_RSA_PRIVATE, for compatibility with rsa.c's signature\r
102  * \param input    buffer holding the encrypted data\r
103  * \param output   buffer that will hold the plaintext\r
104  * \param olen     will contain the plaintext length\r
105  * \param output_max_len    maximum length of the output buffer\r
106  *\r
107  * \return         0 if successful, or an MBEDTLS_ERR_RSA_XXX error code\r
108  *\r
109  * \note           The output buffer must be as large as the size\r
110  *                 of ctx->N (eg. 128 bytes if RSA-1024 is used) otherwise\r
111  *                 an error is thrown.\r
112  */\r
113 int mbedtls_pkcs11_decrypt( mbedtls_pkcs11_context *ctx,\r
114                        int mode, size_t *olen,\r
115                        const unsigned char *input,\r
116                        unsigned char *output,\r
117                        size_t output_max_len );\r
118 \r
119 /**\r
120  * \brief          Do a private RSA to sign a message digest\r
121  *\r
122  * \param ctx      PKCS #11 context\r
123  * \param mode     must be MBEDTLS_RSA_PRIVATE, for compatibility with rsa.c's signature\r
124  * \param md_alg   a MBEDTLS_MD_XXX (use MBEDTLS_MD_NONE for signing raw data)\r
125  * \param hashlen  message digest length (for MBEDTLS_MD_NONE only)\r
126  * \param hash     buffer holding the message digest\r
127  * \param sig      buffer that will hold the ciphertext\r
128  *\r
129  * \return         0 if the signing operation was successful,\r
130  *                 or an MBEDTLS_ERR_RSA_XXX error code\r
131  *\r
132  * \note           The "sig" buffer must be as large as the size\r
133  *                 of ctx->N (eg. 128 bytes if RSA-1024 is used).\r
134  */\r
135 int mbedtls_pkcs11_sign( mbedtls_pkcs11_context *ctx,\r
136                     int mode,\r
137                     mbedtls_md_type_t md_alg,\r
138                     unsigned int hashlen,\r
139                     const unsigned char *hash,\r
140                     unsigned char *sig );\r
141 \r
142 /**\r
143  * SSL/TLS wrappers for PKCS#11 functions\r
144  */\r
145 static inline int mbedtls_ssl_pkcs11_decrypt( void *ctx, int mode, size_t *olen,\r
146                         const unsigned char *input, unsigned char *output,\r
147                         size_t output_max_len )\r
148 {\r
149     return mbedtls_pkcs11_decrypt( (mbedtls_pkcs11_context *) ctx, mode, olen, input, output,\r
150                            output_max_len );\r
151 }\r
152 \r
153 static inline int mbedtls_ssl_pkcs11_sign( void *ctx,\r
154                      int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,\r
155                      int mode, mbedtls_md_type_t md_alg, unsigned int hashlen,\r
156                      const unsigned char *hash, unsigned char *sig )\r
157 {\r
158     ((void) f_rng);\r
159     ((void) p_rng);\r
160     return mbedtls_pkcs11_sign( (mbedtls_pkcs11_context *) ctx, mode, md_alg,\r
161                         hashlen, hash, sig );\r
162 }\r
163 \r
164 static inline size_t mbedtls_ssl_pkcs11_key_len( void *ctx )\r
165 {\r
166     return ( (mbedtls_pkcs11_context *) ctx )->len;\r
167 }\r
168 \r
169 #ifdef __cplusplus\r
170 }\r
171 #endif\r
172 \r
173 #endif /* MBEDTLS_PKCS11_C */\r
174 \r
175 #endif /* MBEDTLS_PKCS11_H */\r