2011-01-18 15:27:19 +00:00
|
|
|
/**
|
|
|
|
* \file pkcs11.c
|
|
|
|
*
|
|
|
|
* \brief Wrapper for PKCS#11 library libpkcs11-helper
|
|
|
|
*
|
|
|
|
* \author Adriaan de Jong <dejong@fox-it.com>
|
|
|
|
*
|
2014-02-01 21:50:26 +00:00
|
|
|
* Copyright (C) 2006-2014, Brainspark B.V.
|
2011-01-18 15:27:19 +00:00
|
|
|
*
|
|
|
|
* This file is part of PolarSSL (http://www.polarssl.org)
|
|
|
|
* Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
|
|
|
|
*
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "polarssl/pkcs11.h"
|
|
|
|
|
|
|
|
#if defined(POLARSSL_PKCS11_C)
|
2013-11-13 15:57:58 +00:00
|
|
|
#include "polarssl/md.h"
|
|
|
|
#include "polarssl/oid.h"
|
|
|
|
#include "polarssl/x509_crt.h"
|
2011-01-18 15:27:19 +00:00
|
|
|
|
2014-02-01 21:50:26 +00:00
|
|
|
#if defined(POLARSSL_PLATFORM_C)
|
|
|
|
#include "polarssl/platform.h"
|
2013-07-03 11:37:05 +00:00
|
|
|
#else
|
2014-02-01 21:50:26 +00:00
|
|
|
#include <stdlib.h>
|
2013-07-03 11:37:05 +00:00
|
|
|
#define polarssl_malloc malloc
|
|
|
|
#define polarssl_free free
|
|
|
|
#endif
|
|
|
|
|
2013-09-18 12:13:26 +00:00
|
|
|
int pkcs11_x509_cert_init( x509_crt *cert, pkcs11h_certificate_t pkcs11_cert )
|
2011-01-18 15:27:19 +00:00
|
|
|
{
|
|
|
|
int ret = 1;
|
|
|
|
unsigned char *cert_blob = NULL;
|
|
|
|
size_t cert_blob_size = 0;
|
|
|
|
|
|
|
|
if( cert == NULL )
|
|
|
|
{
|
|
|
|
ret = 2;
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( pkcs11h_certificate_getCertificateBlob( pkcs11_cert, NULL, &cert_blob_size ) != CKR_OK )
|
|
|
|
{
|
|
|
|
ret = 3;
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
2013-07-03 11:37:05 +00:00
|
|
|
cert_blob = polarssl_malloc( cert_blob_size );
|
2011-01-18 15:27:19 +00:00
|
|
|
if( NULL == cert_blob )
|
|
|
|
{
|
|
|
|
ret = 4;
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( pkcs11h_certificate_getCertificateBlob( pkcs11_cert, cert_blob, &cert_blob_size ) != CKR_OK )
|
|
|
|
{
|
|
|
|
ret = 5;
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
2013-09-18 12:13:26 +00:00
|
|
|
if( 0 != x509_crt_parse(cert, cert_blob, cert_blob_size ) )
|
2011-01-18 15:27:19 +00:00
|
|
|
{
|
|
|
|
ret = 6;
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = 0;
|
|
|
|
|
|
|
|
cleanup:
|
|
|
|
if( NULL != cert_blob )
|
2013-07-03 11:37:05 +00:00
|
|
|
polarssl_free( cert_blob );
|
2011-01-18 15:27:19 +00:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int pkcs11_priv_key_init( pkcs11_context *priv_key,
|
|
|
|
pkcs11h_certificate_t pkcs11_cert )
|
|
|
|
{
|
|
|
|
int ret = 1;
|
2013-09-18 12:13:26 +00:00
|
|
|
x509_crt cert;
|
2011-01-18 15:27:19 +00:00
|
|
|
|
2013-09-18 12:13:26 +00:00
|
|
|
x509_crt_init( &cert );
|
2011-01-18 15:27:19 +00:00
|
|
|
|
|
|
|
if( priv_key == NULL )
|
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
if( 0 != pkcs11_x509_cert_init( &cert, pkcs11_cert ) )
|
|
|
|
goto cleanup;
|
|
|
|
|
2013-11-13 15:57:58 +00:00
|
|
|
priv_key->len = pk_get_len(&cert.pk);
|
2011-01-18 15:27:19 +00:00
|
|
|
priv_key->pkcs11h_cert = pkcs11_cert;
|
|
|
|
|
|
|
|
ret = 0;
|
|
|
|
|
|
|
|
cleanup:
|
2013-09-18 12:13:26 +00:00
|
|
|
x509_crt_free( &cert );
|
2011-01-18 15:27:19 +00:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
void pkcs11_priv_key_free( pkcs11_context *priv_key )
|
|
|
|
{
|
|
|
|
if( NULL != priv_key )
|
|
|
|
pkcs11h_certificate_freeCertificate( priv_key->pkcs11h_cert );
|
|
|
|
}
|
|
|
|
|
|
|
|
int pkcs11_decrypt( pkcs11_context *ctx,
|
2011-04-24 08:57:21 +00:00
|
|
|
int mode, size_t *olen,
|
2011-01-18 15:27:19 +00:00
|
|
|
const unsigned char *input,
|
|
|
|
unsigned char *output,
|
2012-11-14 12:39:52 +00:00
|
|
|
size_t output_max_len )
|
2011-01-18 15:27:19 +00:00
|
|
|
{
|
|
|
|
size_t input_len, output_len;
|
|
|
|
|
|
|
|
if( NULL == ctx )
|
|
|
|
return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
|
|
|
|
|
2013-11-13 15:57:58 +00:00
|
|
|
if( RSA_PRIVATE != mode )
|
2011-01-18 15:27:19 +00:00
|
|
|
return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
|
|
|
|
|
|
|
|
output_len = input_len = ctx->len;
|
|
|
|
|
|
|
|
if( input_len < 16 || input_len > output_max_len )
|
|
|
|
return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
|
|
|
|
|
|
|
|
/* Determine size of output buffer */
|
|
|
|
if( pkcs11h_certificate_decryptAny( ctx->pkcs11h_cert, CKM_RSA_PKCS, input,
|
|
|
|
input_len, NULL, &output_len ) != CKR_OK )
|
|
|
|
{
|
|
|
|
return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
|
|
|
|
}
|
|
|
|
|
|
|
|
if( output_len > output_max_len )
|
|
|
|
return( POLARSSL_ERR_RSA_OUTPUT_TOO_LARGE );
|
|
|
|
|
|
|
|
if( pkcs11h_certificate_decryptAny( ctx->pkcs11h_cert, CKM_RSA_PKCS, input,
|
|
|
|
input_len, output, &output_len ) != CKR_OK )
|
|
|
|
{
|
|
|
|
return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
|
|
|
|
}
|
|
|
|
*olen = output_len;
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
|
|
|
int pkcs11_sign( pkcs11_context *ctx,
|
|
|
|
int mode,
|
2013-11-13 15:57:58 +00:00
|
|
|
md_type_t md_alg,
|
2011-04-24 08:57:21 +00:00
|
|
|
unsigned int hashlen,
|
2011-01-18 15:27:19 +00:00
|
|
|
const unsigned char *hash,
|
|
|
|
unsigned char *sig )
|
|
|
|
{
|
2014-03-26 13:53:47 +00:00
|
|
|
size_t sig_len = 0, asn_len = 0, oid_size = 0;
|
2011-01-18 15:27:19 +00:00
|
|
|
unsigned char *p = sig;
|
2013-11-13 15:57:58 +00:00
|
|
|
const char *oid;
|
2011-01-18 15:27:19 +00:00
|
|
|
|
|
|
|
if( NULL == ctx )
|
|
|
|
return POLARSSL_ERR_RSA_BAD_INPUT_DATA;
|
|
|
|
|
2013-11-13 15:57:58 +00:00
|
|
|
if( RSA_PRIVATE != mode )
|
2011-01-18 15:27:19 +00:00
|
|
|
return POLARSSL_ERR_RSA_BAD_INPUT_DATA;
|
|
|
|
|
2013-11-13 15:57:58 +00:00
|
|
|
if( md_alg != POLARSSL_MD_NONE )
|
2011-01-18 15:27:19 +00:00
|
|
|
{
|
2013-11-13 15:57:58 +00:00
|
|
|
const md_info_t *md_info = md_info_from_type( md_alg );
|
|
|
|
if( md_info == NULL )
|
2011-01-18 15:27:19 +00:00
|
|
|
return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
|
2013-11-13 15:57:58 +00:00
|
|
|
|
|
|
|
if( oid_get_oid_by_md( md_alg, &oid, &oid_size ) != 0 )
|
|
|
|
return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
|
|
|
|
|
|
|
|
hashlen = md_get_size( md_info );
|
2014-03-26 13:53:47 +00:00
|
|
|
asn_len = 10 + oid_size;
|
2013-11-13 15:57:58 +00:00
|
|
|
}
|
|
|
|
|
2014-03-26 13:53:47 +00:00
|
|
|
sig_len = ctx->len;
|
|
|
|
if ( hashlen > ctx_len || asn_len > sig_len ||
|
|
|
|
hashlen + asn_len > sig_len )
|
2013-11-13 15:57:58 +00:00
|
|
|
{
|
2014-03-26 13:53:47 +00:00
|
|
|
return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
|
2013-11-13 15:57:58 +00:00
|
|
|
}
|
2014-03-26 13:53:47 +00:00
|
|
|
|
|
|
|
if( md_alg != POLARSSL_MD_NONE)
|
2013-11-13 15:57:58 +00:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
* DigestInfo ::= SEQUENCE {
|
|
|
|
* digestAlgorithm DigestAlgorithmIdentifier,
|
|
|
|
* digest Digest }
|
|
|
|
*
|
|
|
|
* DigestAlgorithmIdentifier ::= AlgorithmIdentifier
|
|
|
|
*
|
|
|
|
* Digest ::= OCTET STRING
|
|
|
|
*/
|
|
|
|
*p++ = ASN1_SEQUENCE | ASN1_CONSTRUCTED;
|
|
|
|
*p++ = (unsigned char) ( 0x08 + oid_size + hashlen );
|
|
|
|
*p++ = ASN1_SEQUENCE | ASN1_CONSTRUCTED;
|
|
|
|
*p++ = (unsigned char) ( 0x04 + oid_size );
|
|
|
|
*p++ = ASN1_OID;
|
|
|
|
*p++ = oid_size & 0xFF;
|
|
|
|
memcpy( p, oid, oid_size );
|
|
|
|
p += oid_size;
|
|
|
|
*p++ = ASN1_NULL;
|
|
|
|
*p++ = 0x00;
|
|
|
|
*p++ = ASN1_OCTET_STRING;
|
|
|
|
*p++ = hashlen;
|
2011-01-18 15:27:19 +00:00
|
|
|
}
|
|
|
|
|
2014-03-26 13:53:47 +00:00
|
|
|
memcpy( p, hash, hashlen );
|
|
|
|
|
2011-01-18 15:27:19 +00:00
|
|
|
if( pkcs11h_certificate_signAny( ctx->pkcs11h_cert, CKM_RSA_PKCS, sig,
|
2014-03-26 13:53:47 +00:00
|
|
|
asn_len + hashlen, sig, &sig_len ) != CKR_OK )
|
2011-01-18 15:27:19 +00:00
|
|
|
{
|
|
|
|
return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
|
|
|
|
}
|
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* defined(POLARSSL_PKCS11_C) */
|