2011-01-18 16:27:19 +01:00
|
|
|
/**
|
|
|
|
* \file pkcs11.h
|
|
|
|
*
|
|
|
|
* \brief Wrapper for PKCS#11 library libpkcs11-helper
|
|
|
|
*
|
|
|
|
* \author Adriaan de Jong <dejong@fox-it.com>
|
|
|
|
*
|
2015-01-23 10:45:19 +01:00
|
|
|
* Copyright (C) 2006-2014, ARM Limited, All Rights Reserved
|
2011-01-18 16:27:19 +01:00
|
|
|
*
|
2015-01-23 12:06:27 +01:00
|
|
|
* This file is part of mbed TLS (https://www.polarssl.org)
|
2011-01-18 16:27:19 +01:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*/
|
2011-11-18 15:26:47 +01:00
|
|
|
#ifndef POLARSSL_PKCS11_H
|
|
|
|
#define POLARSSL_PKCS11_H
|
2011-01-18 16:27:19 +01:00
|
|
|
|
2014-04-29 12:39:06 +02:00
|
|
|
#if !defined(POLARSSL_CONFIG_FILE)
|
2011-08-15 11:07:52 +02:00
|
|
|
#include "config.h"
|
2014-04-29 12:39:06 +02:00
|
|
|
#else
|
|
|
|
#include POLARSSL_CONFIG_FILE
|
|
|
|
#endif
|
2011-01-18 16:27:19 +01:00
|
|
|
|
|
|
|
#if defined(POLARSSL_PKCS11_C)
|
|
|
|
|
2013-09-18 14:13:26 +02:00
|
|
|
#include "x509_crt.h"
|
2011-01-18 16:27:19 +01:00
|
|
|
|
|
|
|
#include <pkcs11-helper-1.0/pkcs11h-certificate.h>
|
|
|
|
|
2012-09-27 21:15:01 +02:00
|
|
|
#if defined(_MSC_VER) && !defined(inline)
|
|
|
|
#define inline _inline
|
|
|
|
#else
|
|
|
|
#if defined(__ARMCC_VERSION) && !defined(inline)
|
|
|
|
#define inline __inline
|
|
|
|
#endif /* __ARMCC_VERSION */
|
|
|
|
#endif /*_MSC_VER */
|
|
|
|
|
2013-06-27 14:29:21 +02:00
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
2011-01-18 16:27:19 +01:00
|
|
|
/**
|
|
|
|
* Context for PKCS #11 private keys.
|
|
|
|
*/
|
|
|
|
typedef struct {
|
|
|
|
pkcs11h_certificate_t pkcs11h_cert;
|
|
|
|
int len;
|
|
|
|
} pkcs11_context;
|
|
|
|
|
|
|
|
/**
|
2015-01-22 17:11:05 +01:00
|
|
|
* Fill in a mbed TLS certificate, based on the given PKCS11 helper certificate.
|
2011-01-18 16:27:19 +01:00
|
|
|
*
|
|
|
|
* \param cert X.509 certificate to fill
|
|
|
|
* \param pkcs11h_cert PKCS #11 helper certificate
|
|
|
|
*
|
|
|
|
* \return 0 on success.
|
|
|
|
*/
|
2013-09-18 14:13:26 +02:00
|
|
|
int pkcs11_x509_cert_init( x509_crt *cert, pkcs11h_certificate_t pkcs11h_cert );
|
2011-01-18 16:27:19 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Initialise a pkcs11_context, storing the given certificate. Note that the
|
|
|
|
* pkcs11_context will take over control of the certificate, freeing it when
|
|
|
|
* done.
|
|
|
|
*
|
|
|
|
* \param priv_key Private key structure to fill.
|
|
|
|
* \param pkcs11_cert PKCS #11 helper certificate
|
|
|
|
*
|
|
|
|
* \return 0 on success
|
|
|
|
*/
|
|
|
|
int pkcs11_priv_key_init( pkcs11_context *priv_key,
|
|
|
|
pkcs11h_certificate_t pkcs11_cert );
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Free the contents of the given private key context. Note that the structure
|
|
|
|
* itself is not freed.
|
|
|
|
*
|
|
|
|
* \param priv_key Private key structure to cleanup
|
|
|
|
*/
|
|
|
|
void pkcs11_priv_key_free( pkcs11_context *priv_key );
|
|
|
|
|
|
|
|
/**
|
2014-05-01 14:18:25 +02:00
|
|
|
* \brief Do an RSA private key decrypt, then remove the message
|
|
|
|
* padding
|
2011-01-18 16:27:19 +01:00
|
|
|
*
|
|
|
|
* \param ctx PKCS #11 context
|
|
|
|
* \param mode must be RSA_PRIVATE, for compatibility with rsa.c's signature
|
|
|
|
* \param input buffer holding the encrypted data
|
|
|
|
* \param output buffer that will hold the plaintext
|
|
|
|
* \param olen will contain the plaintext length
|
|
|
|
* \param output_max_len maximum length of the output buffer
|
|
|
|
*
|
|
|
|
* \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code
|
|
|
|
*
|
|
|
|
* \note The output buffer must be as large as the size
|
|
|
|
* of ctx->N (eg. 128 bytes if RSA-1024 is used) otherwise
|
|
|
|
* an error is thrown.
|
|
|
|
*/
|
|
|
|
int pkcs11_decrypt( pkcs11_context *ctx,
|
2011-04-24 10:57:21 +02:00
|
|
|
int mode, size_t *olen,
|
2011-01-18 16:27:19 +01:00
|
|
|
const unsigned char *input,
|
|
|
|
unsigned char *output,
|
2012-11-14 13:39:52 +01:00
|
|
|
size_t output_max_len );
|
2011-01-18 16:27:19 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* \brief Do a private RSA to sign a message digest
|
|
|
|
*
|
|
|
|
* \param ctx PKCS #11 context
|
|
|
|
* \param mode must be RSA_PRIVATE, for compatibility with rsa.c's signature
|
2014-05-01 14:18:25 +02:00
|
|
|
* \param md_alg a POLARSSL_MD_* (use POLARSSL_MD_NONE for signing raw data)
|
|
|
|
* \param hashlen message digest length (for POLARSSL_MD_NONE only)
|
2011-01-18 16:27:19 +01:00
|
|
|
* \param hash buffer holding the message digest
|
|
|
|
* \param sig buffer that will hold the ciphertext
|
|
|
|
*
|
|
|
|
* \return 0 if the signing operation was successful,
|
|
|
|
* or an POLARSSL_ERR_RSA_XXX error code
|
|
|
|
*
|
|
|
|
* \note The "sig" buffer must be as large as the size
|
|
|
|
* of ctx->N (eg. 128 bytes if RSA-1024 is used).
|
|
|
|
*/
|
|
|
|
int pkcs11_sign( pkcs11_context *ctx,
|
|
|
|
int mode,
|
2013-11-13 16:57:58 +01:00
|
|
|
md_type_t md_alg,
|
2011-04-24 10:57:21 +02:00
|
|
|
unsigned int hashlen,
|
2011-01-18 16:27:19 +01:00
|
|
|
const unsigned char *hash,
|
|
|
|
unsigned char *sig );
|
|
|
|
|
2012-09-27 21:15:01 +02:00
|
|
|
/**
|
|
|
|
* SSL/TLS wrappers for PKCS#11 functions
|
|
|
|
*/
|
|
|
|
static inline int ssl_pkcs11_decrypt( void *ctx, int mode, size_t *olen,
|
|
|
|
const unsigned char *input, unsigned char *output,
|
2012-11-14 13:39:52 +01:00
|
|
|
size_t output_max_len )
|
2012-09-27 21:15:01 +02:00
|
|
|
{
|
|
|
|
return pkcs11_decrypt( (pkcs11_context *) ctx, mode, olen, input, output,
|
|
|
|
output_max_len );
|
|
|
|
}
|
|
|
|
|
2014-05-01 13:03:14 +02:00
|
|
|
static inline int ssl_pkcs11_sign( void *ctx,
|
2012-09-27 21:15:01 +02:00
|
|
|
int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
|
2013-11-13 16:57:58 +01:00
|
|
|
int mode, md_type_t md_alg, unsigned int hashlen,
|
2012-09-27 21:15:01 +02:00
|
|
|
const unsigned char *hash, unsigned char *sig )
|
|
|
|
{
|
|
|
|
((void) f_rng);
|
|
|
|
((void) p_rng);
|
2013-11-13 16:57:58 +01:00
|
|
|
return pkcs11_sign( (pkcs11_context *) ctx, mode, md_alg,
|
2012-09-27 21:15:01 +02:00
|
|
|
hashlen, hash, sig );
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline size_t ssl_pkcs11_key_len( void *ctx )
|
|
|
|
{
|
|
|
|
return ( (pkcs11_context *) ctx )->len;
|
|
|
|
}
|
|
|
|
|
2013-06-27 14:29:21 +02:00
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2011-01-18 16:27:19 +01:00
|
|
|
#endif /* POLARSSL_PKCS11_C */
|
|
|
|
|
2011-11-18 15:26:47 +01:00
|
|
|
#endif /* POLARSSL_PKCS11_H */
|