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 | 0edee5e | 2015-01-26 15:29:40 +0000 | [diff] [blame^] | 8 | * Copyright (C) 2006-2011, ARM Limited, All Rights Reserved |
Paul Bakker | 43b7e35 | 2011-01-18 15:27:19 +0000 | [diff] [blame] | 9 | * |
Manuel Pégourié-Gonnard | 0edee5e | 2015-01-26 15:29:40 +0000 | [diff] [blame^] | 10 | * This file is part of mbed TLS (https://www.polarssl.org) |
Paul Bakker | 43b7e35 | 2011-01-18 15:27:19 +0000 | [diff] [blame] | 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 | */ |
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 | |
Paul Bakker | 314052f | 2011-08-15 09:07:52 +0000 | [diff] [blame] | 29 | #include "config.h" |
Paul Bakker | 43b7e35 | 2011-01-18 15:27:19 +0000 | [diff] [blame] | 30 | |
| 31 | #if defined(POLARSSL_PKCS11_C) |
| 32 | |
Paul Bakker | 314052f | 2011-08-15 09:07:52 +0000 | [diff] [blame] | 33 | #include "x509.h" |
Paul Bakker | 43b7e35 | 2011-01-18 15:27:19 +0000 | [diff] [blame] | 34 | |
| 35 | #include <pkcs11-helper-1.0/pkcs11h-certificate.h> |
| 36 | |
Paul Bakker | eb2c658 | 2012-09-27 19:15:01 +0000 | [diff] [blame] | 37 | #if defined(_MSC_VER) && !defined(inline) |
| 38 | #define inline _inline |
| 39 | #else |
| 40 | #if defined(__ARMCC_VERSION) && !defined(inline) |
| 41 | #define inline __inline |
| 42 | #endif /* __ARMCC_VERSION */ |
| 43 | #endif /*_MSC_VER */ |
| 44 | |
Paul Bakker | 43b7e35 | 2011-01-18 15:27:19 +0000 | [diff] [blame] | 45 | /** |
| 46 | * Context for PKCS #11 private keys. |
| 47 | */ |
| 48 | typedef struct { |
| 49 | pkcs11h_certificate_t pkcs11h_cert; |
| 50 | int len; |
| 51 | } pkcs11_context; |
| 52 | |
| 53 | /** |
| 54 | * Fill in a PolarSSL certificate, based on the given PKCS11 helper certificate. |
| 55 | * |
| 56 | * \param cert X.509 certificate to fill |
| 57 | * \param pkcs11h_cert PKCS #11 helper certificate |
| 58 | * |
| 59 | * \return 0 on success. |
| 60 | */ |
| 61 | int pkcs11_x509_cert_init( x509_cert *cert, pkcs11h_certificate_t pkcs11h_cert ); |
| 62 | |
| 63 | /** |
| 64 | * Initialise a pkcs11_context, storing the given certificate. Note that the |
| 65 | * pkcs11_context will take over control of the certificate, freeing it when |
| 66 | * done. |
| 67 | * |
| 68 | * \param priv_key Private key structure to fill. |
| 69 | * \param pkcs11_cert PKCS #11 helper certificate |
| 70 | * |
| 71 | * \return 0 on success |
| 72 | */ |
| 73 | int pkcs11_priv_key_init( pkcs11_context *priv_key, |
| 74 | pkcs11h_certificate_t pkcs11_cert ); |
| 75 | |
| 76 | /** |
| 77 | * Free the contents of the given private key context. Note that the structure |
| 78 | * itself is not freed. |
| 79 | * |
| 80 | * \param priv_key Private key structure to cleanup |
| 81 | */ |
| 82 | void pkcs11_priv_key_free( pkcs11_context *priv_key ); |
| 83 | |
| 84 | /** |
| 85 | * \brief Do an RSA private key decrypt, then remove the message padding |
| 86 | * |
| 87 | * \param ctx PKCS #11 context |
| 88 | * \param mode must be RSA_PRIVATE, for compatibility with rsa.c's signature |
| 89 | * \param input buffer holding the encrypted data |
| 90 | * \param output buffer that will hold the plaintext |
| 91 | * \param olen will contain the plaintext length |
| 92 | * \param output_max_len maximum length of the output buffer |
| 93 | * |
| 94 | * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code |
| 95 | * |
| 96 | * \note The output buffer must be as large as the size |
| 97 | * of ctx->N (eg. 128 bytes if RSA-1024 is used) otherwise |
| 98 | * an error is thrown. |
| 99 | */ |
| 100 | int pkcs11_decrypt( pkcs11_context *ctx, |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 101 | int mode, size_t *olen, |
Paul Bakker | 43b7e35 | 2011-01-18 15:27:19 +0000 | [diff] [blame] | 102 | const unsigned char *input, |
| 103 | unsigned char *output, |
Paul Bakker | 9a73632 | 2012-11-14 12:39:52 +0000 | [diff] [blame] | 104 | size_t output_max_len ); |
Paul Bakker | 43b7e35 | 2011-01-18 15:27:19 +0000 | [diff] [blame] | 105 | |
| 106 | /** |
| 107 | * \brief Do a private RSA to sign a message digest |
| 108 | * |
| 109 | * \param ctx PKCS #11 context |
| 110 | * \param mode must be RSA_PRIVATE, for compatibility with rsa.c's signature |
| 111 | * \param hash_id SIG_RSA_RAW, SIG_RSA_MD{2,4,5} or SIG_RSA_SHA{1,224,256,384,512} |
| 112 | * \param hashlen message digest length (for SIG_RSA_RAW only) |
| 113 | * \param hash buffer holding the message digest |
| 114 | * \param sig buffer that will hold the ciphertext |
| 115 | * |
| 116 | * \return 0 if the signing operation was successful, |
| 117 | * or an POLARSSL_ERR_RSA_XXX error code |
| 118 | * |
| 119 | * \note The "sig" buffer must be as large as the size |
| 120 | * of ctx->N (eg. 128 bytes if RSA-1024 is used). |
| 121 | */ |
| 122 | int pkcs11_sign( pkcs11_context *ctx, |
| 123 | int mode, |
| 124 | int hash_id, |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 125 | unsigned int hashlen, |
Paul Bakker | 43b7e35 | 2011-01-18 15:27:19 +0000 | [diff] [blame] | 126 | const unsigned char *hash, |
| 127 | unsigned char *sig ); |
| 128 | |
Paul Bakker | eb2c658 | 2012-09-27 19:15:01 +0000 | [diff] [blame] | 129 | /** |
| 130 | * SSL/TLS wrappers for PKCS#11 functions |
| 131 | */ |
Paul Bakker | 495830d | 2013-10-04 11:01:27 +0200 | [diff] [blame] | 132 | static inline int ssl_pkcs11_decrypt( void *ctx, |
| 133 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng, |
| 134 | int mode, size_t *olen, const unsigned char *input, |
| 135 | unsigned char *output, size_t output_max_len ) |
Paul Bakker | eb2c658 | 2012-09-27 19:15:01 +0000 | [diff] [blame] | 136 | { |
Paul Bakker | 495830d | 2013-10-04 11:01:27 +0200 | [diff] [blame] | 137 | ((void) f_rng); |
| 138 | ((void) p_rng); |
Paul Bakker | eb2c658 | 2012-09-27 19:15:01 +0000 | [diff] [blame] | 139 | return pkcs11_decrypt( (pkcs11_context *) ctx, mode, olen, input, output, |
| 140 | output_max_len ); |
| 141 | } |
| 142 | |
Paul Bakker | 495830d | 2013-10-04 11:01:27 +0200 | [diff] [blame] | 143 | static inline int ssl_pkcs11_sign( void *ctx, |
Paul Bakker | eb2c658 | 2012-09-27 19:15:01 +0000 | [diff] [blame] | 144 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng, |
| 145 | int mode, int hash_id, unsigned int hashlen, |
| 146 | const unsigned char *hash, unsigned char *sig ) |
| 147 | { |
| 148 | ((void) f_rng); |
| 149 | ((void) p_rng); |
| 150 | return pkcs11_sign( (pkcs11_context *) ctx, mode, hash_id, |
| 151 | hashlen, hash, sig ); |
| 152 | } |
| 153 | |
| 154 | static inline size_t ssl_pkcs11_key_len( void *ctx ) |
| 155 | { |
| 156 | return ( (pkcs11_context *) ctx )->len; |
| 157 | } |
| 158 | |
Paul Bakker | 43b7e35 | 2011-01-18 15:27:19 +0000 | [diff] [blame] | 159 | #endif /* POLARSSL_PKCS11_C */ |
| 160 | |
Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame] | 161 | #endif /* POLARSSL_PKCS11_H */ |