Paul Bakker | 43b7e35 | 2011-01-18 15:27:19 +0000 | [diff] [blame] | 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> |
Darryl Green | a40a101 | 2018-01-05 15:33:17 +0000 | [diff] [blame] | 7 | */ |
| 8 | /* |
Manuel Pégourié-Gonnard | 6fb8187 | 2015-07-27 11:11:48 +0200 | [diff] [blame] | 9 | * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved |
Manuel Pégourié-Gonnard | 37ff140 | 2015-09-04 14:21:07 +0200 | [diff] [blame] | 10 | * SPDX-License-Identifier: Apache-2.0 |
| 11 | * |
| 12 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 13 | * not use this file except in compliance with the License. |
| 14 | * You may obtain a copy of the License at |
| 15 | * |
| 16 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 17 | * |
| 18 | * Unless required by applicable law or agreed to in writing, software |
| 19 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 20 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 21 | * See the License for the specific language governing permissions and |
| 22 | * limitations under the License. |
Paul Bakker | 43b7e35 | 2011-01-18 15:27:19 +0000 | [diff] [blame] | 23 | * |
Manuel Pégourié-Gonnard | fe44643 | 2015-03-06 13:17:10 +0000 | [diff] [blame] | 24 | * This file is part of mbed TLS (https://tls.mbed.org) |
Paul Bakker | 43b7e35 | 2011-01-18 15:27:19 +0000 | [diff] [blame] | 25 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 26 | #ifndef MBEDTLS_PKCS11_H |
| 27 | #define MBEDTLS_PKCS11_H |
Paul Bakker | 43b7e35 | 2011-01-18 15:27:19 +0000 | [diff] [blame] | 28 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 29 | #if !defined(MBEDTLS_CONFIG_FILE) |
Jaeden Amero | 6609aef | 2019-07-04 20:01:14 +0100 | [diff] [blame] | 30 | #include "mbedtls/config.h" |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 31 | #else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 32 | #include MBEDTLS_CONFIG_FILE |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 33 | #endif |
Paul Bakker | 43b7e35 | 2011-01-18 15:27:19 +0000 | [diff] [blame] | 34 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 35 | #if defined(MBEDTLS_PKCS11_C) |
Paul Bakker | 43b7e35 | 2011-01-18 15:27:19 +0000 | [diff] [blame] | 36 | |
Jaeden Amero | 6609aef | 2019-07-04 20:01:14 +0100 | [diff] [blame] | 37 | #include "mbedtls/x509_crt.h" |
Paul Bakker | 43b7e35 | 2011-01-18 15:27:19 +0000 | [diff] [blame] | 38 | |
| 39 | #include <pkcs11-helper-1.0/pkcs11h-certificate.h> |
| 40 | |
Manuel Pégourié-Gonnard | 0223ab9 | 2015-10-05 11:40:01 +0100 | [diff] [blame] | 41 | #if ( defined(__ARMCC_VERSION) || defined(_MSC_VER) ) && \ |
| 42 | !defined(inline) && !defined(__cplusplus) |
Paul Bakker | eb2c658 | 2012-09-27 19:15:01 +0000 | [diff] [blame] | 43 | #define inline __inline |
Manuel Pégourié-Gonnard | 20af64d | 2015-07-07 18:33:39 +0200 | [diff] [blame] | 44 | #endif |
Paul Bakker | eb2c658 | 2012-09-27 19:15:01 +0000 | [diff] [blame] | 45 | |
Paul Bakker | 407a0da | 2013-06-27 14:29:21 +0200 | [diff] [blame] | 46 | #ifdef __cplusplus |
| 47 | extern "C" { |
| 48 | #endif |
| 49 | |
Andres Amaya Garcia | 9fc82ef | 2019-01-15 19:08:12 +0000 | [diff] [blame^] | 50 | #if defined(MBEDTLS_DEPRECATED_REMOVED) |
| 51 | |
Paul Bakker | 43b7e35 | 2011-01-18 15:27:19 +0000 | [diff] [blame] | 52 | /** |
| 53 | * Context for PKCS #11 private keys. |
| 54 | */ |
Dawid Drozd | 428cc52 | 2018-07-24 10:02:47 +0200 | [diff] [blame] | 55 | typedef struct mbedtls_pkcs11_context |
| 56 | { |
Paul Bakker | 43b7e35 | 2011-01-18 15:27:19 +0000 | [diff] [blame] | 57 | pkcs11h_certificate_t pkcs11h_cert; |
| 58 | int len; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 59 | } mbedtls_pkcs11_context; |
Paul Bakker | 43b7e35 | 2011-01-18 15:27:19 +0000 | [diff] [blame] | 60 | |
Andres Amaya Garcia | 0d6e108 | 2018-12-05 20:52:29 +0000 | [diff] [blame] | 61 | #if defined(MBEDTLS_DEPRECATED_WARNING) |
| 62 | #define MBEDTLS_DEPRECATED __attribute__((deprecated)) |
| 63 | #else |
| 64 | #define MBEDTLS_DEPRECATED |
| 65 | #endif |
| 66 | |
Paul Bakker | 43b7e35 | 2011-01-18 15:27:19 +0000 | [diff] [blame] | 67 | /** |
Tillmann Karras | 588ad50 | 2015-09-25 04:27:22 +0200 | [diff] [blame] | 68 | * Initialize a mbedtls_pkcs11_context. |
Manuel Pégourié-Gonnard | eab147c | 2015-04-29 01:10:10 +0200 | [diff] [blame] | 69 | * (Just making memory references valid.) |
Andres Amaya Garcia | 0d6e108 | 2018-12-05 20:52:29 +0000 | [diff] [blame] | 70 | * |
Andres Amaya Garcia | 99fc387 | 2019-01-15 19:07:43 +0000 | [diff] [blame] | 71 | * \deprecated This function is deprecated and will be removed in a |
| 72 | * future version of the library. |
Manuel Pégourié-Gonnard | eab147c | 2015-04-29 01:10:10 +0200 | [diff] [blame] | 73 | */ |
Andres Amaya Garcia | 0d6e108 | 2018-12-05 20:52:29 +0000 | [diff] [blame] | 74 | MBEDTLS_DEPRECATED void mbedtls_pkcs11_init( mbedtls_pkcs11_context *ctx ); |
Manuel Pégourié-Gonnard | eab147c | 2015-04-29 01:10:10 +0200 | [diff] [blame] | 75 | |
| 76 | /** |
Manuel Pégourié-Gonnard | b4fe3cb | 2015-01-22 16:11:05 +0000 | [diff] [blame] | 77 | * Fill in a mbed TLS certificate, based on the given PKCS11 helper certificate. |
Paul Bakker | 43b7e35 | 2011-01-18 15:27:19 +0000 | [diff] [blame] | 78 | * |
Andres Amaya Garcia | 99fc387 | 2019-01-15 19:07:43 +0000 | [diff] [blame] | 79 | * \deprecated This function is deprecated and will be removed in a |
| 80 | * future version of the library. |
Andres Amaya Garcia | 0d6e108 | 2018-12-05 20:52:29 +0000 | [diff] [blame] | 81 | * |
Paul Bakker | 43b7e35 | 2011-01-18 15:27:19 +0000 | [diff] [blame] | 82 | * \param cert X.509 certificate to fill |
| 83 | * \param pkcs11h_cert PKCS #11 helper certificate |
| 84 | * |
| 85 | * \return 0 on success. |
| 86 | */ |
Andres Amaya Garcia | 0d6e108 | 2018-12-05 20:52:29 +0000 | [diff] [blame] | 87 | MBEDTLS_DEPRECATED int mbedtls_pkcs11_x509_cert_bind( mbedtls_x509_crt *cert, |
| 88 | pkcs11h_certificate_t pkcs11h_cert ); |
Paul Bakker | 43b7e35 | 2011-01-18 15:27:19 +0000 | [diff] [blame] | 89 | |
| 90 | /** |
Manuel Pégourié-Gonnard | eab147c | 2015-04-29 01:10:10 +0200 | [diff] [blame] | 91 | * Set up a mbedtls_pkcs11_context storing the given certificate. Note that the |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 92 | * mbedtls_pkcs11_context will take over control of the certificate, freeing it when |
Paul Bakker | 43b7e35 | 2011-01-18 15:27:19 +0000 | [diff] [blame] | 93 | * done. |
| 94 | * |
Andres Amaya Garcia | 99fc387 | 2019-01-15 19:07:43 +0000 | [diff] [blame] | 95 | * \deprecated This function is deprecated and will be removed in a |
| 96 | * future version of the library. |
Andres Amaya Garcia | 0d6e108 | 2018-12-05 20:52:29 +0000 | [diff] [blame] | 97 | * |
Paul Bakker | 43b7e35 | 2011-01-18 15:27:19 +0000 | [diff] [blame] | 98 | * \param priv_key Private key structure to fill. |
| 99 | * \param pkcs11_cert PKCS #11 helper certificate |
| 100 | * |
| 101 | * \return 0 on success |
| 102 | */ |
Andres Amaya Garcia | 0d6e108 | 2018-12-05 20:52:29 +0000 | [diff] [blame] | 103 | MBEDTLS_DEPRECATED int mbedtls_pkcs11_priv_key_bind( |
| 104 | mbedtls_pkcs11_context *priv_key, |
| 105 | pkcs11h_certificate_t pkcs11_cert ); |
Paul Bakker | 43b7e35 | 2011-01-18 15:27:19 +0000 | [diff] [blame] | 106 | |
| 107 | /** |
| 108 | * Free the contents of the given private key context. Note that the structure |
| 109 | * itself is not freed. |
| 110 | * |
Andres Amaya Garcia | 99fc387 | 2019-01-15 19:07:43 +0000 | [diff] [blame] | 111 | * \deprecated This function is deprecated and will be removed in a |
| 112 | * future version of the library. |
Andres Amaya Garcia | 0d6e108 | 2018-12-05 20:52:29 +0000 | [diff] [blame] | 113 | * |
Paul Bakker | 43b7e35 | 2011-01-18 15:27:19 +0000 | [diff] [blame] | 114 | * \param priv_key Private key structure to cleanup |
| 115 | */ |
Andres Amaya Garcia | 0d6e108 | 2018-12-05 20:52:29 +0000 | [diff] [blame] | 116 | MBEDTLS_DEPRECATED void mbedtls_pkcs11_priv_key_free( |
| 117 | mbedtls_pkcs11_context *priv_key ); |
Paul Bakker | 43b7e35 | 2011-01-18 15:27:19 +0000 | [diff] [blame] | 118 | |
| 119 | /** |
Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 120 | * \brief Do an RSA private key decrypt, then remove the message |
| 121 | * padding |
Paul Bakker | 43b7e35 | 2011-01-18 15:27:19 +0000 | [diff] [blame] | 122 | * |
Andres Amaya Garcia | 99fc387 | 2019-01-15 19:07:43 +0000 | [diff] [blame] | 123 | * \deprecated This function is deprecated and will be removed in a future |
| 124 | * version of the library. |
Andres Amaya Garcia | 0d6e108 | 2018-12-05 20:52:29 +0000 | [diff] [blame] | 125 | * |
Paul Bakker | 43b7e35 | 2011-01-18 15:27:19 +0000 | [diff] [blame] | 126 | * \param ctx PKCS #11 context |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 127 | * \param mode must be MBEDTLS_RSA_PRIVATE, for compatibility with rsa.c's signature |
Paul Bakker | 43b7e35 | 2011-01-18 15:27:19 +0000 | [diff] [blame] | 128 | * \param input buffer holding the encrypted data |
| 129 | * \param output buffer that will hold the plaintext |
| 130 | * \param olen will contain the plaintext length |
| 131 | * \param output_max_len maximum length of the output buffer |
| 132 | * |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 133 | * \return 0 if successful, or an MBEDTLS_ERR_RSA_XXX error code |
Paul Bakker | 43b7e35 | 2011-01-18 15:27:19 +0000 | [diff] [blame] | 134 | * |
| 135 | * \note The output buffer must be as large as the size |
| 136 | * of ctx->N (eg. 128 bytes if RSA-1024 is used) otherwise |
| 137 | * an error is thrown. |
| 138 | */ |
Andres Amaya Garcia | 0d6e108 | 2018-12-05 20:52:29 +0000 | [diff] [blame] | 139 | MBEDTLS_DEPRECATED int mbedtls_pkcs11_decrypt( mbedtls_pkcs11_context *ctx, |
| 140 | int mode, size_t *olen, |
| 141 | const unsigned char *input, |
| 142 | unsigned char *output, |
| 143 | size_t output_max_len ); |
Paul Bakker | 43b7e35 | 2011-01-18 15:27:19 +0000 | [diff] [blame] | 144 | |
| 145 | /** |
| 146 | * \brief Do a private RSA to sign a message digest |
| 147 | * |
Andres Amaya Garcia | 99fc387 | 2019-01-15 19:07:43 +0000 | [diff] [blame] | 148 | * \deprecated This function is deprecated and will be removed in a future |
| 149 | * version of the library. |
Andres Amaya Garcia | 0d6e108 | 2018-12-05 20:52:29 +0000 | [diff] [blame] | 150 | * |
Paul Bakker | 43b7e35 | 2011-01-18 15:27:19 +0000 | [diff] [blame] | 151 | * \param ctx PKCS #11 context |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 152 | * \param mode must be MBEDTLS_RSA_PRIVATE, for compatibility with rsa.c's signature |
| 153 | * \param md_alg a MBEDTLS_MD_XXX (use MBEDTLS_MD_NONE for signing raw data) |
| 154 | * \param hashlen message digest length (for MBEDTLS_MD_NONE only) |
Paul Bakker | 43b7e35 | 2011-01-18 15:27:19 +0000 | [diff] [blame] | 155 | * \param hash buffer holding the message digest |
| 156 | * \param sig buffer that will hold the ciphertext |
| 157 | * |
| 158 | * \return 0 if the signing operation was successful, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 159 | * or an MBEDTLS_ERR_RSA_XXX error code |
Paul Bakker | 43b7e35 | 2011-01-18 15:27:19 +0000 | [diff] [blame] | 160 | * |
| 161 | * \note The "sig" buffer must be as large as the size |
| 162 | * of ctx->N (eg. 128 bytes if RSA-1024 is used). |
| 163 | */ |
Andres Amaya Garcia | 0d6e108 | 2018-12-05 20:52:29 +0000 | [diff] [blame] | 164 | MBEDTLS_DEPRECATED int mbedtls_pkcs11_sign( mbedtls_pkcs11_context *ctx, |
| 165 | int mode, |
| 166 | mbedtls_md_type_t md_alg, |
| 167 | unsigned int hashlen, |
| 168 | const unsigned char *hash, |
| 169 | unsigned char *sig ); |
Paul Bakker | 43b7e35 | 2011-01-18 15:27:19 +0000 | [diff] [blame] | 170 | |
Paul Bakker | eb2c658 | 2012-09-27 19:15:01 +0000 | [diff] [blame] | 171 | /** |
| 172 | * SSL/TLS wrappers for PKCS#11 functions |
Andres Amaya Garcia | 0d6e108 | 2018-12-05 20:52:29 +0000 | [diff] [blame] | 173 | * |
Andres Amaya Garcia | 99fc387 | 2019-01-15 19:07:43 +0000 | [diff] [blame] | 174 | * \deprecated This function is deprecated and will be removed in a future |
| 175 | * version of the library. |
Paul Bakker | eb2c658 | 2012-09-27 19:15:01 +0000 | [diff] [blame] | 176 | */ |
Andres Amaya Garcia | 0d6e108 | 2018-12-05 20:52:29 +0000 | [diff] [blame] | 177 | MBEDTLS_DEPRECATED static inline int mbedtls_ssl_pkcs11_decrypt( void *ctx, |
| 178 | int mode, size_t *olen, |
| 179 | const unsigned char *input, unsigned char *output, |
| 180 | size_t output_max_len ) |
Paul Bakker | eb2c658 | 2012-09-27 19:15:01 +0000 | [diff] [blame] | 181 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 182 | return mbedtls_pkcs11_decrypt( (mbedtls_pkcs11_context *) ctx, mode, olen, input, output, |
Paul Bakker | eb2c658 | 2012-09-27 19:15:01 +0000 | [diff] [blame] | 183 | output_max_len ); |
| 184 | } |
| 185 | |
Andres Amaya Garcia | 0d6e108 | 2018-12-05 20:52:29 +0000 | [diff] [blame] | 186 | MBEDTLS_DEPRECATED static inline int mbedtls_ssl_pkcs11_sign( void *ctx, |
| 187 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng, |
| 188 | int mode, mbedtls_md_type_t md_alg, unsigned int hashlen, |
| 189 | const unsigned char *hash, unsigned char *sig ) |
Paul Bakker | eb2c658 | 2012-09-27 19:15:01 +0000 | [diff] [blame] | 190 | { |
| 191 | ((void) f_rng); |
| 192 | ((void) p_rng); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 193 | return mbedtls_pkcs11_sign( (mbedtls_pkcs11_context *) ctx, mode, md_alg, |
Paul Bakker | eb2c658 | 2012-09-27 19:15:01 +0000 | [diff] [blame] | 194 | hashlen, hash, sig ); |
| 195 | } |
| 196 | |
Andres Amaya Garcia | 0d6e108 | 2018-12-05 20:52:29 +0000 | [diff] [blame] | 197 | MBEDTLS_DEPRECATED static inline size_t mbedtls_ssl_pkcs11_key_len( void *ctx ) |
Paul Bakker | eb2c658 | 2012-09-27 19:15:01 +0000 | [diff] [blame] | 198 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 199 | return ( (mbedtls_pkcs11_context *) ctx )->len; |
Paul Bakker | eb2c658 | 2012-09-27 19:15:01 +0000 | [diff] [blame] | 200 | } |
| 201 | |
Andres Amaya Garcia | 0d6e108 | 2018-12-05 20:52:29 +0000 | [diff] [blame] | 202 | #undef MBEDTLS_DEPRECATED |
| 203 | |
Andres Amaya Garcia | 9fc82ef | 2019-01-15 19:08:12 +0000 | [diff] [blame^] | 204 | #endif /* MBEDTLS_DEPRECATED_REMOVED */ |
| 205 | |
Paul Bakker | 407a0da | 2013-06-27 14:29:21 +0200 | [diff] [blame] | 206 | #ifdef __cplusplus |
| 207 | } |
| 208 | #endif |
| 209 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 210 | #endif /* MBEDTLS_PKCS11_C */ |
Paul Bakker | 43b7e35 | 2011-01-18 15:27:19 +0000 | [diff] [blame] | 211 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 212 | #endif /* MBEDTLS_PKCS11_H */ |