2011-01-18 16:27:19 +01:00
|
|
|
/**
|
|
|
|
* \file pkcs11.c
|
|
|
|
*
|
|
|
|
* \brief Wrapper for PKCS#11 library libpkcs11-helper
|
|
|
|
*
|
|
|
|
* \author Adriaan de Jong <dejong@fox-it.com>
|
|
|
|
*
|
2015-07-27 11:11:48 +02:00
|
|
|
* Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
|
2015-09-04 14:21:07 +02:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
2011-01-18 16:27:19 +01:00
|
|
|
*
|
2015-09-04 14:21:07 +02:00
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); you may
|
|
|
|
* not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
2011-01-18 16:27:19 +01:00
|
|
|
*
|
2015-09-04 14:21:07 +02:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2011-01-18 16:27:19 +01:00
|
|
|
*
|
2015-09-04 14:21:07 +02:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
2011-01-18 16:27:19 +01:00
|
|
|
*
|
2015-09-04 14:21:07 +02:00
|
|
|
* This file is part of mbed TLS (https://tls.mbed.org)
|
2011-01-18 16:27:19 +01:00
|
|
|
*/
|
|
|
|
|
2015-03-09 18:05:11 +01:00
|
|
|
#include "mbedtls/pkcs11.h"
|
2011-01-18 16:27:19 +01:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_PKCS11_C)
|
2015-02-06 14:43:58 +01:00
|
|
|
|
2015-03-09 18:05:11 +01:00
|
|
|
#include "mbedtls/md.h"
|
|
|
|
#include "mbedtls/oid.h"
|
|
|
|
#include "mbedtls/x509_crt.h"
|
2011-01-18 16:27:19 +01:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_PLATFORM_C)
|
2015-03-09 18:05:11 +01:00
|
|
|
#include "mbedtls/platform.h"
|
2013-07-03 13:37:05 +02:00
|
|
|
#else
|
2014-02-01 22:50:26 +01:00
|
|
|
#include <stdlib.h>
|
2015-05-26 16:04:06 +02:00
|
|
|
#define mbedtls_calloc calloc
|
2015-04-08 12:49:31 +02:00
|
|
|
#define mbedtls_free free
|
2013-07-03 13:37:05 +02:00
|
|
|
#endif
|
|
|
|
|
2015-05-27 17:09:21 +02:00
|
|
|
#include <string.h>
|
|
|
|
|
2015-04-29 01:10:10 +02:00
|
|
|
void mbedtls_pkcs11_init( mbedtls_pkcs11_context *ctx )
|
|
|
|
{
|
2019-10-02 14:47:01 +02:00
|
|
|
mbedtls_platform_memset( ctx, 0, sizeof( mbedtls_pkcs11_context ) );
|
2015-04-29 01:10:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int mbedtls_pkcs11_x509_cert_bind( mbedtls_x509_crt *cert, pkcs11h_certificate_t pkcs11_cert )
|
2011-01-18 16:27:19 +01:00
|
|
|
{
|
|
|
|
int ret = 1;
|
|
|
|
unsigned char *cert_blob = NULL;
|
|
|
|
size_t cert_blob_size = 0;
|
|
|
|
|
|
|
|
if( cert == NULL )
|
|
|
|
{
|
|
|
|
ret = 2;
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
2014-05-01 14:18:25 +02:00
|
|
|
if( pkcs11h_certificate_getCertificateBlob( pkcs11_cert, NULL,
|
|
|
|
&cert_blob_size ) != CKR_OK )
|
2011-01-18 16:27:19 +01:00
|
|
|
{
|
|
|
|
ret = 3;
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
2015-05-26 16:04:06 +02:00
|
|
|
cert_blob = mbedtls_calloc( 1, cert_blob_size );
|
2011-01-18 16:27:19 +01:00
|
|
|
if( NULL == cert_blob )
|
|
|
|
{
|
|
|
|
ret = 4;
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
2014-05-01 14:18:25 +02:00
|
|
|
if( pkcs11h_certificate_getCertificateBlob( pkcs11_cert, cert_blob,
|
|
|
|
&cert_blob_size ) != CKR_OK )
|
2011-01-18 16:27:19 +01:00
|
|
|
{
|
|
|
|
ret = 5;
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
if( 0 != mbedtls_x509_crt_parse( cert, cert_blob, cert_blob_size ) )
|
2011-01-18 16:27:19 +01:00
|
|
|
{
|
|
|
|
ret = 6;
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = 0;
|
|
|
|
|
|
|
|
cleanup:
|
|
|
|
if( NULL != cert_blob )
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_free( cert_blob );
|
2011-01-18 16:27:19 +01:00
|
|
|
|
2014-06-17 14:06:49 +02:00
|
|
|
return( ret );
|
2011-01-18 16:27:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-04-29 01:10:10 +02:00
|
|
|
int mbedtls_pkcs11_priv_key_bind( mbedtls_pkcs11_context *priv_key,
|
2011-01-18 16:27:19 +01:00
|
|
|
pkcs11h_certificate_t pkcs11_cert )
|
|
|
|
{
|
|
|
|
int ret = 1;
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_x509_crt cert;
|
2011-01-18 16:27:19 +01:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_x509_crt_init( &cert );
|
2011-01-18 16:27:19 +01:00
|
|
|
|
|
|
|
if( priv_key == NULL )
|
|
|
|
goto cleanup;
|
|
|
|
|
2015-05-27 17:09:21 +02:00
|
|
|
if( 0 != mbedtls_pkcs11_x509_cert_bind( &cert, pkcs11_cert ) )
|
2011-01-18 16:27:19 +01:00
|
|
|
goto cleanup;
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
priv_key->len = mbedtls_pk_get_len( &cert.pk );
|
2011-01-18 16:27:19 +01:00
|
|
|
priv_key->pkcs11h_cert = pkcs11_cert;
|
|
|
|
|
|
|
|
ret = 0;
|
|
|
|
|
|
|
|
cleanup:
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_x509_crt_free( &cert );
|
2011-01-18 16:27:19 +01:00
|
|
|
|
2014-06-17 14:06:49 +02:00
|
|
|
return( ret );
|
2011-01-18 16:27:19 +01:00
|
|
|
}
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
void mbedtls_pkcs11_priv_key_free( mbedtls_pkcs11_context *priv_key )
|
2011-01-18 16:27:19 +01:00
|
|
|
{
|
|
|
|
if( NULL != priv_key )
|
|
|
|
pkcs11h_certificate_freeCertificate( priv_key->pkcs11h_cert );
|
|
|
|
}
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
int mbedtls_pkcs11_decrypt( mbedtls_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
|
|
|
{
|
|
|
|
size_t input_len, output_len;
|
|
|
|
|
|
|
|
if( NULL == ctx )
|
2015-04-08 12:49:31 +02:00
|
|
|
return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
|
2011-01-18 16:27:19 +01:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
if( MBEDTLS_RSA_PRIVATE != mode )
|
|
|
|
return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
|
2011-01-18 16:27:19 +01:00
|
|
|
|
|
|
|
output_len = input_len = ctx->len;
|
|
|
|
|
|
|
|
if( input_len < 16 || input_len > output_max_len )
|
2015-04-08 12:49:31 +02:00
|
|
|
return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
|
2011-01-18 16:27:19 +01:00
|
|
|
|
|
|
|
/* Determine size of output buffer */
|
|
|
|
if( pkcs11h_certificate_decryptAny( ctx->pkcs11h_cert, CKM_RSA_PKCS, input,
|
|
|
|
input_len, NULL, &output_len ) != CKR_OK )
|
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
|
2011-01-18 16:27:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if( output_len > output_max_len )
|
2015-04-08 12:49:31 +02:00
|
|
|
return( MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE );
|
2011-01-18 16:27:19 +01:00
|
|
|
|
|
|
|
if( pkcs11h_certificate_decryptAny( ctx->pkcs11h_cert, CKM_RSA_PKCS, input,
|
|
|
|
input_len, output, &output_len ) != CKR_OK )
|
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
|
2011-01-18 16:27:19 +01:00
|
|
|
}
|
|
|
|
*olen = output_len;
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
int mbedtls_pkcs11_sign( mbedtls_pkcs11_context *ctx,
|
2011-01-18 16:27:19 +01:00
|
|
|
int mode,
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_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 )
|
|
|
|
{
|
2014-03-26 14:53:47 +01:00
|
|
|
size_t sig_len = 0, asn_len = 0, oid_size = 0;
|
2011-01-18 16:27:19 +01:00
|
|
|
unsigned char *p = sig;
|
2013-11-13 16:57:58 +01:00
|
|
|
const char *oid;
|
2011-01-18 16:27:19 +01:00
|
|
|
|
|
|
|
if( NULL == ctx )
|
2015-04-08 12:49:31 +02:00
|
|
|
return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
|
2011-01-18 16:27:19 +01:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
if( MBEDTLS_RSA_PRIVATE != mode )
|
|
|
|
return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
|
2011-01-18 16:27:19 +01:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
if( md_alg != MBEDTLS_MD_NONE )
|
2011-01-18 16:27:19 +01:00
|
|
|
{
|
2019-07-17 12:21:02 +02:00
|
|
|
mbedtls_md_handle_t md_info = mbedtls_md_info_from_type( md_alg );
|
|
|
|
if( md_info == MBEDTLS_MD_INVALID_HANDLE )
|
2015-04-08 12:49:31 +02:00
|
|
|
return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
|
2013-11-13 16:57:58 +01:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
if( mbedtls_oid_get_oid_by_md( md_alg, &oid, &oid_size ) != 0 )
|
|
|
|
return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
|
2013-11-13 16:57:58 +01:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
hashlen = mbedtls_md_get_size( md_info );
|
2014-03-26 14:53:47 +01:00
|
|
|
asn_len = 10 + oid_size;
|
2013-11-13 16:57:58 +01:00
|
|
|
}
|
|
|
|
|
2014-03-26 14:53:47 +01:00
|
|
|
sig_len = ctx->len;
|
2014-06-17 16:39:18 +02:00
|
|
|
if( hashlen > sig_len || asn_len > sig_len ||
|
|
|
|
hashlen + asn_len > sig_len )
|
2013-11-13 16:57:58 +01:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
|
2013-11-13 16:57:58 +01:00
|
|
|
}
|
2014-03-26 14:53:47 +01:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
if( md_alg != MBEDTLS_MD_NONE )
|
2013-11-13 16:57:58 +01:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
* DigestInfo ::= SEQUENCE {
|
|
|
|
* digestAlgorithm DigestAlgorithmIdentifier,
|
|
|
|
* digest Digest }
|
|
|
|
*
|
|
|
|
* DigestAlgorithmIdentifier ::= AlgorithmIdentifier
|
|
|
|
*
|
|
|
|
* Digest ::= OCTET STRING
|
|
|
|
*/
|
2015-04-08 12:49:31 +02:00
|
|
|
*p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
|
2013-11-13 16:57:58 +01:00
|
|
|
*p++ = (unsigned char) ( 0x08 + oid_size + hashlen );
|
2015-04-08 12:49:31 +02:00
|
|
|
*p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
|
2013-11-13 16:57:58 +01:00
|
|
|
*p++ = (unsigned char) ( 0x04 + oid_size );
|
2015-04-08 12:49:31 +02:00
|
|
|
*p++ = MBEDTLS_ASN1_OID;
|
2013-11-13 16:57:58 +01:00
|
|
|
*p++ = oid_size & 0xFF;
|
|
|
|
memcpy( p, oid, oid_size );
|
|
|
|
p += oid_size;
|
2015-04-08 12:49:31 +02:00
|
|
|
*p++ = MBEDTLS_ASN1_NULL;
|
2013-11-13 16:57:58 +01:00
|
|
|
*p++ = 0x00;
|
2015-04-08 12:49:31 +02:00
|
|
|
*p++ = MBEDTLS_ASN1_OCTET_STRING;
|
2013-11-13 16:57:58 +01:00
|
|
|
*p++ = hashlen;
|
2011-01-18 16:27:19 +01:00
|
|
|
}
|
|
|
|
|
2014-03-26 14:53:47 +01:00
|
|
|
memcpy( p, hash, hashlen );
|
|
|
|
|
2011-01-18 16:27:19 +01:00
|
|
|
if( pkcs11h_certificate_signAny( ctx->pkcs11h_cert, CKM_RSA_PKCS, sig,
|
2014-03-26 14:53:47 +01:00
|
|
|
asn_len + hashlen, sig, &sig_len ) != CKR_OK )
|
2011-01-18 16:27:19 +01:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
|
2011-01-18 16:27:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#endif /* defined(MBEDTLS_PKCS11_C) */
|