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> |
| 7 | * |
Manuel Pégourié-Gonnard | a658a40 | 2015-01-23 09:45:19 +0000 | [diff] [blame] | 8 | * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved |
Paul Bakker | 43b7e35 | 2011-01-18 15:27:19 +0000 | [diff] [blame] | 9 | * |
Manuel Pégourié-Gonnard | fe44643 | 2015-03-06 13:17:10 +0000 | [diff] [blame] | 10 | * This file is part of mbed TLS (https://tls.mbed.org) |
Paul Bakker | 43b7e35 | 2011-01-18 15:27:19 +0000 | [diff] [blame] | 11 | * |
Paul Bakker | 43b7e35 | 2011-01-18 15:27:19 +0000 | [diff] [blame] | 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 | */ |
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) |
Paul Bakker | 314052f | 2011-08-15 09:07:52 +0000 | [diff] [blame] | 30 | #include "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 | |
Paul Bakker | c559c7a | 2013-09-18 14:13:26 +0200 | [diff] [blame] | 37 | #include "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 | |
Paul Bakker | eb2c658 | 2012-09-27 19:15:01 +0000 | [diff] [blame] | 41 | #if defined(__ARMCC_VERSION) && !defined(inline) |
| 42 | #define inline __inline |
| 43 | #endif /* __ARMCC_VERSION */ |
Paul Bakker | eb2c658 | 2012-09-27 19:15:01 +0000 | [diff] [blame] | 44 | |
Paul Bakker | 407a0da | 2013-06-27 14:29:21 +0200 | [diff] [blame] | 45 | #ifdef __cplusplus |
| 46 | extern "C" { |
| 47 | #endif |
| 48 | |
Paul Bakker | 43b7e35 | 2011-01-18 15:27:19 +0000 | [diff] [blame] | 49 | /** |
| 50 | * Context for PKCS #11 private keys. |
| 51 | */ |
| 52 | typedef struct { |
| 53 | pkcs11h_certificate_t pkcs11h_cert; |
| 54 | int len; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 55 | } mbedtls_pkcs11_context; |
Paul Bakker | 43b7e35 | 2011-01-18 15:27:19 +0000 | [diff] [blame] | 56 | |
| 57 | /** |
Manuel Pégourié-Gonnard | eab147c | 2015-04-29 01:10:10 +0200 | [diff] [blame] | 58 | * Initialize a mbetls_pkcs11_context. |
| 59 | * (Just making memory references valid.) |
| 60 | */ |
| 61 | void mbedtls_pkcs11_init( mbedtls_pkcs11_context *ctx ); |
| 62 | |
| 63 | /** |
Manuel Pégourié-Gonnard | b4fe3cb | 2015-01-22 16:11:05 +0000 | [diff] [blame] | 64 | * 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] | 65 | * |
| 66 | * \param cert X.509 certificate to fill |
| 67 | * \param pkcs11h_cert PKCS #11 helper certificate |
| 68 | * |
| 69 | * \return 0 on success. |
| 70 | */ |
Manuel Pégourié-Gonnard | eab147c | 2015-04-29 01:10:10 +0200 | [diff] [blame] | 71 | int mbedtls_pkcs11_x509_cert_bind( mbedtls_x509_crt *cert, pkcs11h_certificate_t pkcs11h_cert ); |
Paul Bakker | 43b7e35 | 2011-01-18 15:27:19 +0000 | [diff] [blame] | 72 | |
| 73 | /** |
Manuel Pégourié-Gonnard | eab147c | 2015-04-29 01:10:10 +0200 | [diff] [blame] | 74 | * 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] | 75 | * 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] | 76 | * done. |
| 77 | * |
| 78 | * \param priv_key Private key structure to fill. |
| 79 | * \param pkcs11_cert PKCS #11 helper certificate |
| 80 | * |
| 81 | * \return 0 on success |
| 82 | */ |
Manuel Pégourié-Gonnard | eab147c | 2015-04-29 01:10:10 +0200 | [diff] [blame] | 83 | int mbedtls_pkcs11_priv_key_bind( mbedtls_pkcs11_context *priv_key, |
Paul Bakker | 43b7e35 | 2011-01-18 15:27:19 +0000 | [diff] [blame] | 84 | pkcs11h_certificate_t pkcs11_cert ); |
| 85 | |
| 86 | /** |
| 87 | * Free the contents of the given private key context. Note that the structure |
| 88 | * itself is not freed. |
| 89 | * |
| 90 | * \param priv_key Private key structure to cleanup |
| 91 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 92 | void mbedtls_pkcs11_priv_key_free( mbedtls_pkcs11_context *priv_key ); |
Paul Bakker | 43b7e35 | 2011-01-18 15:27:19 +0000 | [diff] [blame] | 93 | |
| 94 | /** |
Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 95 | * \brief Do an RSA private key decrypt, then remove the message |
| 96 | * padding |
Paul Bakker | 43b7e35 | 2011-01-18 15:27:19 +0000 | [diff] [blame] | 97 | * |
| 98 | * \param ctx PKCS #11 context |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 99 | * \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] | 100 | * \param input buffer holding the encrypted data |
| 101 | * \param output buffer that will hold the plaintext |
| 102 | * \param olen will contain the plaintext length |
| 103 | * \param output_max_len maximum length of the output buffer |
| 104 | * |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 105 | * \return 0 if successful, or an MBEDTLS_ERR_RSA_XXX error code |
Paul Bakker | 43b7e35 | 2011-01-18 15:27:19 +0000 | [diff] [blame] | 106 | * |
| 107 | * \note The output buffer must be as large as the size |
| 108 | * of ctx->N (eg. 128 bytes if RSA-1024 is used) otherwise |
| 109 | * an error is thrown. |
| 110 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 111 | int mbedtls_pkcs11_decrypt( mbedtls_pkcs11_context *ctx, |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 112 | int mode, size_t *olen, |
Paul Bakker | 43b7e35 | 2011-01-18 15:27:19 +0000 | [diff] [blame] | 113 | const unsigned char *input, |
| 114 | unsigned char *output, |
Paul Bakker | 9a73632 | 2012-11-14 12:39:52 +0000 | [diff] [blame] | 115 | size_t output_max_len ); |
Paul Bakker | 43b7e35 | 2011-01-18 15:27:19 +0000 | [diff] [blame] | 116 | |
| 117 | /** |
| 118 | * \brief Do a private RSA to sign a message digest |
| 119 | * |
| 120 | * \param ctx PKCS #11 context |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 121 | * \param mode must be MBEDTLS_RSA_PRIVATE, for compatibility with rsa.c's signature |
| 122 | * \param md_alg a MBEDTLS_MD_XXX (use MBEDTLS_MD_NONE for signing raw data) |
| 123 | * \param hashlen message digest length (for MBEDTLS_MD_NONE only) |
Paul Bakker | 43b7e35 | 2011-01-18 15:27:19 +0000 | [diff] [blame] | 124 | * \param hash buffer holding the message digest |
| 125 | * \param sig buffer that will hold the ciphertext |
| 126 | * |
| 127 | * \return 0 if the signing operation was successful, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 128 | * or an MBEDTLS_ERR_RSA_XXX error code |
Paul Bakker | 43b7e35 | 2011-01-18 15:27:19 +0000 | [diff] [blame] | 129 | * |
| 130 | * \note The "sig" buffer must be as large as the size |
| 131 | * of ctx->N (eg. 128 bytes if RSA-1024 is used). |
| 132 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 133 | int mbedtls_pkcs11_sign( mbedtls_pkcs11_context *ctx, |
Paul Bakker | 43b7e35 | 2011-01-18 15:27:19 +0000 | [diff] [blame] | 134 | int mode, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 135 | mbedtls_md_type_t md_alg, |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 136 | unsigned int hashlen, |
Paul Bakker | 43b7e35 | 2011-01-18 15:27:19 +0000 | [diff] [blame] | 137 | const unsigned char *hash, |
| 138 | unsigned char *sig ); |
| 139 | |
Paul Bakker | eb2c658 | 2012-09-27 19:15:01 +0000 | [diff] [blame] | 140 | /** |
| 141 | * SSL/TLS wrappers for PKCS#11 functions |
| 142 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 143 | static inline int mbedtls_ssl_pkcs11_decrypt( void *ctx, int mode, size_t *olen, |
Paul Bakker | eb2c658 | 2012-09-27 19:15:01 +0000 | [diff] [blame] | 144 | const unsigned char *input, unsigned char *output, |
Paul Bakker | 9a73632 | 2012-11-14 12:39:52 +0000 | [diff] [blame] | 145 | size_t output_max_len ) |
Paul Bakker | eb2c658 | 2012-09-27 19:15:01 +0000 | [diff] [blame] | 146 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 147 | return mbedtls_pkcs11_decrypt( (mbedtls_pkcs11_context *) ctx, mode, olen, input, output, |
Paul Bakker | eb2c658 | 2012-09-27 19:15:01 +0000 | [diff] [blame] | 148 | output_max_len ); |
| 149 | } |
| 150 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 151 | static inline int mbedtls_ssl_pkcs11_sign( void *ctx, |
Paul Bakker | eb2c658 | 2012-09-27 19:15:01 +0000 | [diff] [blame] | 152 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 153 | int mode, mbedtls_md_type_t md_alg, unsigned int hashlen, |
Paul Bakker | eb2c658 | 2012-09-27 19:15:01 +0000 | [diff] [blame] | 154 | const unsigned char *hash, unsigned char *sig ) |
| 155 | { |
| 156 | ((void) f_rng); |
| 157 | ((void) p_rng); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 158 | return mbedtls_pkcs11_sign( (mbedtls_pkcs11_context *) ctx, mode, md_alg, |
Paul Bakker | eb2c658 | 2012-09-27 19:15:01 +0000 | [diff] [blame] | 159 | hashlen, hash, sig ); |
| 160 | } |
| 161 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 162 | static inline size_t mbedtls_ssl_pkcs11_key_len( void *ctx ) |
Paul Bakker | eb2c658 | 2012-09-27 19:15:01 +0000 | [diff] [blame] | 163 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 164 | return ( (mbedtls_pkcs11_context *) ctx )->len; |
Paul Bakker | eb2c658 | 2012-09-27 19:15:01 +0000 | [diff] [blame] | 165 | } |
| 166 | |
Paul Bakker | 407a0da | 2013-06-27 14:29:21 +0200 | [diff] [blame] | 167 | #ifdef __cplusplus |
| 168 | } |
| 169 | #endif |
| 170 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 171 | #endif /* MBEDTLS_PKCS11_C */ |
Paul Bakker | 43b7e35 | 2011-01-18 15:27:19 +0000 | [diff] [blame] | 172 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 173 | #endif /* MBEDTLS_PKCS11_H */ |