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>
|
|
|
|
*
|
2020-08-19 14:37:36 +00:00
|
|
|
* Copyright The Mbed TLS Contributors
|
Update license headers to Apache-2.0 OR GPL-2.0-or-later
This will allow us to ship the LTS branches in a single archive
This commit was generated using the following script:
# ========================
#!/bin/sh
header1='\ * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later\
*\
* This file is provided under the Apache License 2.0, or the\
* GNU General Public License v2.0 or later.\
*\
* **********\
* Apache License 2.0:\
*\
* 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\
*\
* http://www.apache.org/licenses/LICENSE-2.0\
*\
* 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.\
*\
* **********\
*\
* **********\
* GNU General Public License v2.0 or later:\
*\
* 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.\
*\
* **********'
find -path './.git' -prune -o '(' -name '*.c' -o -name '*.cpp' -o -name '*.fmt' -o -name '*.h' ')' -print | xargs sed -i "
# Normalize the first line of the copyright headers (no text on the first line of a block comment)
/^\/\*.*Copyright.*Arm/I s/\/\*/&\n */
# Insert new copyright header
/SPDX-License-Identifier/ i\
$header1
# Delete old copyright header
/SPDX-License-Identifier/,$ {
# Delete lines until the one preceding the mbedtls declaration
N
1,/This file is part of/ {
/This file is part of/! D
}
}
"
# Format copyright header for inclusion into scripts
header2=$(echo "$header1" | sed 's/^\\\? \* \?/#/')
find -path './.git' -prune -o '(' -name '*.gdb' -o -name '*.pl' -o -name '*.py' -o -name '*.sh' ')' -print | xargs sed -i "
# Insert new copyright header
/SPDX-License-Identifier/ i\
$header2
# Delete old copyright header
/SPDX-License-Identifier/,$ {
# Delete lines until the one preceding the mbedtls declaration
N
1,/This file is part of/ {
/This file is part of/! D
}
}
"
# ========================
Signed-off-by: Bence Szépkúti <bence.szepkuti@arm.com>
2020-06-05 11:02:18 +00:00
|
|
|
* SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
|
|
|
|
*
|
|
|
|
* This file is provided under the Apache License 2.0, or the
|
|
|
|
* GNU General Public License v2.0 or later.
|
|
|
|
*
|
|
|
|
* **********
|
|
|
|
* Apache License 2.0:
|
2011-01-18 15:27:19 +00:00
|
|
|
*
|
2015-09-04 12:21:07 +00: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 15:27:19 +00:00
|
|
|
*
|
2015-09-04 12:21:07 +00:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2011-01-18 15:27:19 +00:00
|
|
|
*
|
2015-09-04 12:21:07 +00: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 15:27:19 +00:00
|
|
|
*
|
Update license headers to Apache-2.0 OR GPL-2.0-or-later
This will allow us to ship the LTS branches in a single archive
This commit was generated using the following script:
# ========================
#!/bin/sh
header1='\ * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later\
*\
* This file is provided under the Apache License 2.0, or the\
* GNU General Public License v2.0 or later.\
*\
* **********\
* Apache License 2.0:\
*\
* 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\
*\
* http://www.apache.org/licenses/LICENSE-2.0\
*\
* 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.\
*\
* **********\
*\
* **********\
* GNU General Public License v2.0 or later:\
*\
* 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.\
*\
* **********'
find -path './.git' -prune -o '(' -name '*.c' -o -name '*.cpp' -o -name '*.fmt' -o -name '*.h' ')' -print | xargs sed -i "
# Normalize the first line of the copyright headers (no text on the first line of a block comment)
/^\/\*.*Copyright.*Arm/I s/\/\*/&\n */
# Insert new copyright header
/SPDX-License-Identifier/ i\
$header1
# Delete old copyright header
/SPDX-License-Identifier/,$ {
# Delete lines until the one preceding the mbedtls declaration
N
1,/This file is part of/ {
/This file is part of/! D
}
}
"
# Format copyright header for inclusion into scripts
header2=$(echo "$header1" | sed 's/^\\\? \* \?/#/')
find -path './.git' -prune -o '(' -name '*.gdb' -o -name '*.pl' -o -name '*.py' -o -name '*.sh' ')' -print | xargs sed -i "
# Insert new copyright header
/SPDX-License-Identifier/ i\
$header2
# Delete old copyright header
/SPDX-License-Identifier/,$ {
# Delete lines until the one preceding the mbedtls declaration
N
1,/This file is part of/ {
/This file is part of/! D
}
}
"
# ========================
Signed-off-by: Bence Szépkúti <bence.szepkuti@arm.com>
2020-06-05 11:02:18 +00:00
|
|
|
* **********
|
|
|
|
*
|
|
|
|
* **********
|
|
|
|
* GNU General Public License v2.0 or later:
|
|
|
|
*
|
|
|
|
* 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-01-18 15:27:19 +00:00
|
|
|
*/
|
|
|
|
|
2015-03-09 17:05:11 +00:00
|
|
|
#include "mbedtls/pkcs11.h"
|
2011-01-18 15:27:19 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
#if defined(MBEDTLS_PKCS11_C)
|
2015-02-06 13:43:58 +00:00
|
|
|
|
2015-03-09 17:05:11 +00:00
|
|
|
#include "mbedtls/md.h"
|
|
|
|
#include "mbedtls/oid.h"
|
|
|
|
#include "mbedtls/x509_crt.h"
|
2011-01-18 15:27:19 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
#if defined(MBEDTLS_PLATFORM_C)
|
2015-03-09 17:05:11 +00:00
|
|
|
#include "mbedtls/platform.h"
|
2013-07-03 11:37:05 +00:00
|
|
|
#else
|
2014-02-01 21:50:26 +00:00
|
|
|
#include <stdlib.h>
|
2015-05-26 14:04:06 +00:00
|
|
|
#define mbedtls_calloc calloc
|
2015-04-08 10:49:31 +00:00
|
|
|
#define mbedtls_free free
|
2013-07-03 11:37:05 +00:00
|
|
|
#endif
|
|
|
|
|
2015-05-27 15:09:21 +00:00
|
|
|
#include <string.h>
|
|
|
|
|
2015-04-28 23:10:10 +00:00
|
|
|
void mbedtls_pkcs11_init( mbedtls_pkcs11_context *ctx )
|
|
|
|
{
|
|
|
|
memset( ctx, 0, sizeof( mbedtls_pkcs11_context ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
int mbedtls_pkcs11_x509_cert_bind( mbedtls_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;
|
|
|
|
}
|
|
|
|
|
2014-05-01 12:18:25 +00:00
|
|
|
if( pkcs11h_certificate_getCertificateBlob( pkcs11_cert, NULL,
|
|
|
|
&cert_blob_size ) != CKR_OK )
|
2011-01-18 15:27:19 +00:00
|
|
|
{
|
|
|
|
ret = 3;
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
2015-05-26 14:04:06 +00:00
|
|
|
cert_blob = mbedtls_calloc( 1, cert_blob_size );
|
2011-01-18 15:27:19 +00:00
|
|
|
if( NULL == cert_blob )
|
|
|
|
{
|
|
|
|
ret = 4;
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
2014-05-01 12:18:25 +00:00
|
|
|
if( pkcs11h_certificate_getCertificateBlob( pkcs11_cert, cert_blob,
|
|
|
|
&cert_blob_size ) != CKR_OK )
|
2011-01-18 15:27:19 +00:00
|
|
|
{
|
|
|
|
ret = 5;
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
if( 0 != mbedtls_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 )
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_free( cert_blob );
|
2011-01-18 15:27:19 +00:00
|
|
|
|
2014-06-17 12:06:49 +00:00
|
|
|
return( ret );
|
2011-01-18 15:27:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-04-28 23:10:10 +00:00
|
|
|
int mbedtls_pkcs11_priv_key_bind( mbedtls_pkcs11_context *priv_key,
|
2011-01-18 15:27:19 +00:00
|
|
|
pkcs11h_certificate_t pkcs11_cert )
|
|
|
|
{
|
|
|
|
int ret = 1;
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_x509_crt cert;
|
2011-01-18 15:27:19 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_x509_crt_init( &cert );
|
2011-01-18 15:27:19 +00:00
|
|
|
|
|
|
|
if( priv_key == NULL )
|
|
|
|
goto cleanup;
|
|
|
|
|
2015-05-27 15:09:21 +00:00
|
|
|
if( 0 != mbedtls_pkcs11_x509_cert_bind( &cert, pkcs11_cert ) )
|
2011-01-18 15:27:19 +00:00
|
|
|
goto cleanup;
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
priv_key->len = mbedtls_pk_get_len( &cert.pk );
|
2011-01-18 15:27:19 +00:00
|
|
|
priv_key->pkcs11h_cert = pkcs11_cert;
|
|
|
|
|
|
|
|
ret = 0;
|
|
|
|
|
|
|
|
cleanup:
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_x509_crt_free( &cert );
|
2011-01-18 15:27:19 +00:00
|
|
|
|
2014-06-17 12:06:49 +00:00
|
|
|
return( ret );
|
2011-01-18 15:27:19 +00:00
|
|
|
}
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
void mbedtls_pkcs11_priv_key_free( mbedtls_pkcs11_context *priv_key )
|
2011-01-18 15:27:19 +00:00
|
|
|
{
|
|
|
|
if( NULL != priv_key )
|
|
|
|
pkcs11h_certificate_freeCertificate( priv_key->pkcs11h_cert );
|
|
|
|
}
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
int mbedtls_pkcs11_decrypt( mbedtls_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 )
|
2015-04-08 10:49:31 +00:00
|
|
|
return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
|
2011-01-18 15:27:19 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
if( MBEDTLS_RSA_PRIVATE != mode )
|
|
|
|
return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
|
2011-01-18 15:27:19 +00:00
|
|
|
|
|
|
|
output_len = input_len = ctx->len;
|
|
|
|
|
|
|
|
if( input_len < 16 || input_len > output_max_len )
|
2015-04-08 10:49:31 +00:00
|
|
|
return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
|
2011-01-18 15:27:19 +00: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 10:49:31 +00:00
|
|
|
return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
|
2011-01-18 15:27:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if( output_len > output_max_len )
|
2015-04-08 10:49:31 +00:00
|
|
|
return( MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE );
|
2011-01-18 15:27:19 +00:00
|
|
|
|
|
|
|
if( pkcs11h_certificate_decryptAny( ctx->pkcs11h_cert, CKM_RSA_PKCS, input,
|
|
|
|
input_len, output, &output_len ) != CKR_OK )
|
|
|
|
{
|
2015-04-08 10:49:31 +00:00
|
|
|
return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
|
2011-01-18 15:27:19 +00:00
|
|
|
}
|
|
|
|
*olen = output_len;
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
int mbedtls_pkcs11_sign( mbedtls_pkcs11_context *ctx,
|
2011-01-18 15:27:19 +00:00
|
|
|
int mode,
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_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 )
|
2015-04-08 10:49:31 +00:00
|
|
|
return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
|
2011-01-18 15:27:19 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
if( MBEDTLS_RSA_PRIVATE != mode )
|
|
|
|
return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
|
2011-01-18 15:27:19 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
if( md_alg != MBEDTLS_MD_NONE )
|
2011-01-18 15:27:19 +00:00
|
|
|
{
|
2015-04-08 10:49:31 +00:00
|
|
|
const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
|
2013-11-13 15:57:58 +00:00
|
|
|
if( md_info == NULL )
|
2015-04-08 10:49:31 +00:00
|
|
|
return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
|
2013-11-13 15:57:58 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
if( mbedtls_oid_get_oid_by_md( md_alg, &oid, &oid_size ) != 0 )
|
|
|
|
return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
|
2013-11-13 15:57:58 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
hashlen = mbedtls_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;
|
2014-06-17 14:39:18 +00:00
|
|
|
if( hashlen > sig_len || asn_len > sig_len ||
|
|
|
|
hashlen + asn_len > sig_len )
|
2013-11-13 15:57:58 +00:00
|
|
|
{
|
2015-04-08 10:49:31 +00:00
|
|
|
return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
|
2013-11-13 15:57:58 +00:00
|
|
|
}
|
2014-03-26 13:53:47 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
if( md_alg != MBEDTLS_MD_NONE )
|
2013-11-13 15:57:58 +00:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
* DigestInfo ::= SEQUENCE {
|
|
|
|
* digestAlgorithm DigestAlgorithmIdentifier,
|
|
|
|
* digest Digest }
|
|
|
|
*
|
|
|
|
* DigestAlgorithmIdentifier ::= AlgorithmIdentifier
|
|
|
|
*
|
|
|
|
* Digest ::= OCTET STRING
|
|
|
|
*/
|
2015-04-08 10:49:31 +00:00
|
|
|
*p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
|
2013-11-13 15:57:58 +00:00
|
|
|
*p++ = (unsigned char) ( 0x08 + oid_size + hashlen );
|
2015-04-08 10:49:31 +00:00
|
|
|
*p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
|
2013-11-13 15:57:58 +00:00
|
|
|
*p++ = (unsigned char) ( 0x04 + oid_size );
|
2015-04-08 10:49:31 +00:00
|
|
|
*p++ = MBEDTLS_ASN1_OID;
|
2013-11-13 15:57:58 +00:00
|
|
|
*p++ = oid_size & 0xFF;
|
|
|
|
memcpy( p, oid, oid_size );
|
|
|
|
p += oid_size;
|
2015-04-08 10:49:31 +00:00
|
|
|
*p++ = MBEDTLS_ASN1_NULL;
|
2013-11-13 15:57:58 +00:00
|
|
|
*p++ = 0x00;
|
2015-04-08 10:49:31 +00:00
|
|
|
*p++ = MBEDTLS_ASN1_OCTET_STRING;
|
2013-11-13 15:57:58 +00:00
|
|
|
*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
|
|
|
{
|
2015-04-08 10:49:31 +00:00
|
|
|
return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
|
2011-01-18 15:27:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
#endif /* defined(MBEDTLS_PKCS11_C) */
|