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 | */ |
Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame] | 26 | #ifndef POLARSSL_PKCS11_H |
| 27 | #define POLARSSL_PKCS11_H |
Paul Bakker | 43b7e35 | 2011-01-18 15:27:19 +0000 | [diff] [blame] | 28 | |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 29 | #if !defined(POLARSSL_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 |
| 32 | #include POLARSSL_CONFIG_FILE |
| 33 | #endif |
Paul Bakker | 43b7e35 | 2011-01-18 15:27:19 +0000 | [diff] [blame] | 34 | |
| 35 | #if defined(POLARSSL_PKCS11_C) |
| 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 | |
Manuel Pégourié-Gonnard | 20607bb | 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 |
| 44 | #endif /* __ARMCC_VERSION */ |
| 45 | #endif /*_MSC_VER */ |
| 46 | |
Paul Bakker | 407a0da | 2013-06-27 14:29:21 +0200 | [diff] [blame] | 47 | #ifdef __cplusplus |
| 48 | extern "C" { |
| 49 | #endif |
| 50 | |
Paul Bakker | 43b7e35 | 2011-01-18 15:27:19 +0000 | [diff] [blame] | 51 | /** |
| 52 | * Context for PKCS #11 private keys. |
| 53 | */ |
| 54 | typedef struct { |
| 55 | pkcs11h_certificate_t pkcs11h_cert; |
| 56 | int len; |
| 57 | } pkcs11_context; |
| 58 | |
| 59 | /** |
Manuel Pégourié-Gonnard | b4fe3cb | 2015-01-22 16:11:05 +0000 | [diff] [blame] | 60 | * 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] | 61 | * |
| 62 | * \param cert X.509 certificate to fill |
| 63 | * \param pkcs11h_cert PKCS #11 helper certificate |
| 64 | * |
| 65 | * \return 0 on success. |
| 66 | */ |
Paul Bakker | c559c7a | 2013-09-18 14:13:26 +0200 | [diff] [blame] | 67 | int pkcs11_x509_cert_init( x509_crt *cert, pkcs11h_certificate_t pkcs11h_cert ); |
Paul Bakker | 43b7e35 | 2011-01-18 15:27:19 +0000 | [diff] [blame] | 68 | |
| 69 | /** |
| 70 | * Initialise a pkcs11_context, storing the given certificate. Note that the |
| 71 | * pkcs11_context will take over control of the certificate, freeing it when |
| 72 | * done. |
| 73 | * |
| 74 | * \param priv_key Private key structure to fill. |
| 75 | * \param pkcs11_cert PKCS #11 helper certificate |
| 76 | * |
| 77 | * \return 0 on success |
| 78 | */ |
| 79 | int pkcs11_priv_key_init( pkcs11_context *priv_key, |
| 80 | pkcs11h_certificate_t pkcs11_cert ); |
| 81 | |
| 82 | /** |
| 83 | * Free the contents of the given private key context. Note that the structure |
| 84 | * itself is not freed. |
| 85 | * |
| 86 | * \param priv_key Private key structure to cleanup |
| 87 | */ |
| 88 | void pkcs11_priv_key_free( pkcs11_context *priv_key ); |
| 89 | |
| 90 | /** |
Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 91 | * \brief Do an RSA private key decrypt, then remove the message |
| 92 | * padding |
Paul Bakker | 43b7e35 | 2011-01-18 15:27:19 +0000 | [diff] [blame] | 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 | */ |
| 107 | int pkcs11_decrypt( pkcs11_context *ctx, |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 108 | int mode, size_t *olen, |
Paul Bakker | 43b7e35 | 2011-01-18 15:27:19 +0000 | [diff] [blame] | 109 | const unsigned char *input, |
| 110 | unsigned char *output, |
Paul Bakker | 9a73632 | 2012-11-14 12:39:52 +0000 | [diff] [blame] | 111 | size_t output_max_len ); |
Paul Bakker | 43b7e35 | 2011-01-18 15:27:19 +0000 | [diff] [blame] | 112 | |
| 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 |
Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 118 | * \param md_alg a POLARSSL_MD_* (use POLARSSL_MD_NONE for signing raw data) |
| 119 | * \param hashlen message digest length (for POLARSSL_MD_NONE only) |
Paul Bakker | 43b7e35 | 2011-01-18 15:27:19 +0000 | [diff] [blame] | 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 | */ |
| 129 | int pkcs11_sign( pkcs11_context *ctx, |
| 130 | int mode, |
Steffan Karger | 28d81a0 | 2013-11-13 16:57:58 +0100 | [diff] [blame] | 131 | md_type_t md_alg, |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 132 | unsigned int hashlen, |
Paul Bakker | 43b7e35 | 2011-01-18 15:27:19 +0000 | [diff] [blame] | 133 | const unsigned char *hash, |
| 134 | unsigned char *sig ); |
| 135 | |
Paul Bakker | eb2c658 | 2012-09-27 19:15:01 +0000 | [diff] [blame] | 136 | /** |
| 137 | * SSL/TLS wrappers for PKCS#11 functions |
| 138 | */ |
| 139 | static inline int ssl_pkcs11_decrypt( void *ctx, int mode, size_t *olen, |
| 140 | const unsigned char *input, unsigned char *output, |
Paul Bakker | 9a73632 | 2012-11-14 12:39:52 +0000 | [diff] [blame] | 141 | size_t output_max_len ) |
Paul Bakker | eb2c658 | 2012-09-27 19:15:01 +0000 | [diff] [blame] | 142 | { |
| 143 | return pkcs11_decrypt( (pkcs11_context *) ctx, mode, olen, input, output, |
| 144 | output_max_len ); |
| 145 | } |
| 146 | |
Paul Bakker | 9af723c | 2014-05-01 13:03:14 +0200 | [diff] [blame] | 147 | static inline int ssl_pkcs11_sign( void *ctx, |
Paul Bakker | eb2c658 | 2012-09-27 19:15:01 +0000 | [diff] [blame] | 148 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng, |
Steffan Karger | 28d81a0 | 2013-11-13 16:57:58 +0100 | [diff] [blame] | 149 | int mode, md_type_t md_alg, unsigned int hashlen, |
Paul Bakker | eb2c658 | 2012-09-27 19:15:01 +0000 | [diff] [blame] | 150 | const unsigned char *hash, unsigned char *sig ) |
| 151 | { |
| 152 | ((void) f_rng); |
| 153 | ((void) p_rng); |
Steffan Karger | 28d81a0 | 2013-11-13 16:57:58 +0100 | [diff] [blame] | 154 | return pkcs11_sign( (pkcs11_context *) ctx, mode, md_alg, |
Paul Bakker | eb2c658 | 2012-09-27 19:15:01 +0000 | [diff] [blame] | 155 | hashlen, hash, sig ); |
| 156 | } |
| 157 | |
| 158 | static inline size_t ssl_pkcs11_key_len( void *ctx ) |
| 159 | { |
| 160 | return ( (pkcs11_context *) ctx )->len; |
| 161 | } |
| 162 | |
Paul Bakker | 407a0da | 2013-06-27 14:29:21 +0200 | [diff] [blame] | 163 | #ifdef __cplusplus |
| 164 | } |
| 165 | #endif |
| 166 | |
Paul Bakker | 43b7e35 | 2011-01-18 15:27:19 +0000 | [diff] [blame] | 167 | #endif /* POLARSSL_PKCS11_C */ |
| 168 | |
Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame] | 169 | #endif /* POLARSSL_PKCS11_H */ |