mbed TLS upgraded to 2.6.0

Fork of mbedtls by Mark Radbourne

Committer:
Jasper Wallace
Date:
Fri Sep 29 19:50:30 2017 +0100
Revision:
2:bbdeda018a3c
Parent:
0:cdf462088d13
Update to mbedtls 2.6.0, many changes.

Changes to mbedtls sources made:

in include/mbedtls/config.h comment out:

#define MBEDTLS_FS_IO
#define MBEDTLS_NET_C
#define MBEDTLS_TIMING_C

uncomment:

#define MBEDTLS_NO_PLATFORM_ENTROPY

remove the following directorys:

programs
yotta
visualc

Who changed what in which revision?

UserRevisionLine numberNew contents of line
markrad 0:cdf462088d13 1 /**
markrad 0:cdf462088d13 2 * \file pkcs11.c
markrad 0:cdf462088d13 3 *
markrad 0:cdf462088d13 4 * \brief Wrapper for PKCS#11 library libpkcs11-helper
markrad 0:cdf462088d13 5 *
markrad 0:cdf462088d13 6 * \author Adriaan de Jong <dejong@fox-it.com>
markrad 0:cdf462088d13 7 *
markrad 0:cdf462088d13 8 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
markrad 0:cdf462088d13 9 * SPDX-License-Identifier: Apache-2.0
markrad 0:cdf462088d13 10 *
markrad 0:cdf462088d13 11 * Licensed under the Apache License, Version 2.0 (the "License"); you may
markrad 0:cdf462088d13 12 * not use this file except in compliance with the License.
markrad 0:cdf462088d13 13 * You may obtain a copy of the License at
markrad 0:cdf462088d13 14 *
markrad 0:cdf462088d13 15 * http://www.apache.org/licenses/LICENSE-2.0
markrad 0:cdf462088d13 16 *
markrad 0:cdf462088d13 17 * Unless required by applicable law or agreed to in writing, software
markrad 0:cdf462088d13 18 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
markrad 0:cdf462088d13 19 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
markrad 0:cdf462088d13 20 * See the License for the specific language governing permissions and
markrad 0:cdf462088d13 21 * limitations under the License.
markrad 0:cdf462088d13 22 *
markrad 0:cdf462088d13 23 * This file is part of mbed TLS (https://tls.mbed.org)
markrad 0:cdf462088d13 24 */
markrad 0:cdf462088d13 25
markrad 0:cdf462088d13 26 #include "mbedtls/pkcs11.h"
markrad 0:cdf462088d13 27
markrad 0:cdf462088d13 28 #if defined(MBEDTLS_PKCS11_C)
markrad 0:cdf462088d13 29
markrad 0:cdf462088d13 30 #include "mbedtls/md.h"
markrad 0:cdf462088d13 31 #include "mbedtls/oid.h"
markrad 0:cdf462088d13 32 #include "mbedtls/x509_crt.h"
markrad 0:cdf462088d13 33
markrad 0:cdf462088d13 34 #if defined(MBEDTLS_PLATFORM_C)
markrad 0:cdf462088d13 35 #include "mbedtls/platform.h"
markrad 0:cdf462088d13 36 #else
markrad 0:cdf462088d13 37 #include <stdlib.h>
markrad 0:cdf462088d13 38 #define mbedtls_calloc calloc
markrad 0:cdf462088d13 39 #define mbedtls_free free
markrad 0:cdf462088d13 40 #endif
markrad 0:cdf462088d13 41
markrad 0:cdf462088d13 42 #include <string.h>
markrad 0:cdf462088d13 43
markrad 0:cdf462088d13 44 void mbedtls_pkcs11_init( mbedtls_pkcs11_context *ctx )
markrad 0:cdf462088d13 45 {
markrad 0:cdf462088d13 46 memset( ctx, 0, sizeof( mbedtls_pkcs11_context ) );
markrad 0:cdf462088d13 47 }
markrad 0:cdf462088d13 48
markrad 0:cdf462088d13 49 int mbedtls_pkcs11_x509_cert_bind( mbedtls_x509_crt *cert, pkcs11h_certificate_t pkcs11_cert )
markrad 0:cdf462088d13 50 {
markrad 0:cdf462088d13 51 int ret = 1;
markrad 0:cdf462088d13 52 unsigned char *cert_blob = NULL;
markrad 0:cdf462088d13 53 size_t cert_blob_size = 0;
markrad 0:cdf462088d13 54
markrad 0:cdf462088d13 55 if( cert == NULL )
markrad 0:cdf462088d13 56 {
markrad 0:cdf462088d13 57 ret = 2;
markrad 0:cdf462088d13 58 goto cleanup;
markrad 0:cdf462088d13 59 }
markrad 0:cdf462088d13 60
markrad 0:cdf462088d13 61 if( pkcs11h_certificate_getCertificateBlob( pkcs11_cert, NULL,
markrad 0:cdf462088d13 62 &cert_blob_size ) != CKR_OK )
markrad 0:cdf462088d13 63 {
markrad 0:cdf462088d13 64 ret = 3;
markrad 0:cdf462088d13 65 goto cleanup;
markrad 0:cdf462088d13 66 }
markrad 0:cdf462088d13 67
markrad 0:cdf462088d13 68 cert_blob = mbedtls_calloc( 1, cert_blob_size );
markrad 0:cdf462088d13 69 if( NULL == cert_blob )
markrad 0:cdf462088d13 70 {
markrad 0:cdf462088d13 71 ret = 4;
markrad 0:cdf462088d13 72 goto cleanup;
markrad 0:cdf462088d13 73 }
markrad 0:cdf462088d13 74
markrad 0:cdf462088d13 75 if( pkcs11h_certificate_getCertificateBlob( pkcs11_cert, cert_blob,
markrad 0:cdf462088d13 76 &cert_blob_size ) != CKR_OK )
markrad 0:cdf462088d13 77 {
markrad 0:cdf462088d13 78 ret = 5;
markrad 0:cdf462088d13 79 goto cleanup;
markrad 0:cdf462088d13 80 }
markrad 0:cdf462088d13 81
markrad 0:cdf462088d13 82 if( 0 != mbedtls_x509_crt_parse( cert, cert_blob, cert_blob_size ) )
markrad 0:cdf462088d13 83 {
markrad 0:cdf462088d13 84 ret = 6;
markrad 0:cdf462088d13 85 goto cleanup;
markrad 0:cdf462088d13 86 }
markrad 0:cdf462088d13 87
markrad 0:cdf462088d13 88 ret = 0;
markrad 0:cdf462088d13 89
markrad 0:cdf462088d13 90 cleanup:
markrad 0:cdf462088d13 91 if( NULL != cert_blob )
markrad 0:cdf462088d13 92 mbedtls_free( cert_blob );
markrad 0:cdf462088d13 93
markrad 0:cdf462088d13 94 return( ret );
markrad 0:cdf462088d13 95 }
markrad 0:cdf462088d13 96
markrad 0:cdf462088d13 97
markrad 0:cdf462088d13 98 int mbedtls_pkcs11_priv_key_bind( mbedtls_pkcs11_context *priv_key,
markrad 0:cdf462088d13 99 pkcs11h_certificate_t pkcs11_cert )
markrad 0:cdf462088d13 100 {
markrad 0:cdf462088d13 101 int ret = 1;
markrad 0:cdf462088d13 102 mbedtls_x509_crt cert;
markrad 0:cdf462088d13 103
markrad 0:cdf462088d13 104 mbedtls_x509_crt_init( &cert );
markrad 0:cdf462088d13 105
markrad 0:cdf462088d13 106 if( priv_key == NULL )
markrad 0:cdf462088d13 107 goto cleanup;
markrad 0:cdf462088d13 108
markrad 0:cdf462088d13 109 if( 0 != mbedtls_pkcs11_x509_cert_bind( &cert, pkcs11_cert ) )
markrad 0:cdf462088d13 110 goto cleanup;
markrad 0:cdf462088d13 111
markrad 0:cdf462088d13 112 priv_key->len = mbedtls_pk_get_len( &cert.pk );
markrad 0:cdf462088d13 113 priv_key->pkcs11h_cert = pkcs11_cert;
markrad 0:cdf462088d13 114
markrad 0:cdf462088d13 115 ret = 0;
markrad 0:cdf462088d13 116
markrad 0:cdf462088d13 117 cleanup:
markrad 0:cdf462088d13 118 mbedtls_x509_crt_free( &cert );
markrad 0:cdf462088d13 119
markrad 0:cdf462088d13 120 return( ret );
markrad 0:cdf462088d13 121 }
markrad 0:cdf462088d13 122
markrad 0:cdf462088d13 123 void mbedtls_pkcs11_priv_key_free( mbedtls_pkcs11_context *priv_key )
markrad 0:cdf462088d13 124 {
markrad 0:cdf462088d13 125 if( NULL != priv_key )
markrad 0:cdf462088d13 126 pkcs11h_certificate_freeCertificate( priv_key->pkcs11h_cert );
markrad 0:cdf462088d13 127 }
markrad 0:cdf462088d13 128
markrad 0:cdf462088d13 129 int mbedtls_pkcs11_decrypt( mbedtls_pkcs11_context *ctx,
markrad 0:cdf462088d13 130 int mode, size_t *olen,
markrad 0:cdf462088d13 131 const unsigned char *input,
markrad 0:cdf462088d13 132 unsigned char *output,
markrad 0:cdf462088d13 133 size_t output_max_len )
markrad 0:cdf462088d13 134 {
markrad 0:cdf462088d13 135 size_t input_len, output_len;
markrad 0:cdf462088d13 136
markrad 0:cdf462088d13 137 if( NULL == ctx )
markrad 0:cdf462088d13 138 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
markrad 0:cdf462088d13 139
markrad 0:cdf462088d13 140 if( MBEDTLS_RSA_PRIVATE != mode )
markrad 0:cdf462088d13 141 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
markrad 0:cdf462088d13 142
markrad 0:cdf462088d13 143 output_len = input_len = ctx->len;
markrad 0:cdf462088d13 144
markrad 0:cdf462088d13 145 if( input_len < 16 || input_len > output_max_len )
markrad 0:cdf462088d13 146 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
markrad 0:cdf462088d13 147
markrad 0:cdf462088d13 148 /* Determine size of output buffer */
markrad 0:cdf462088d13 149 if( pkcs11h_certificate_decryptAny( ctx->pkcs11h_cert, CKM_RSA_PKCS, input,
markrad 0:cdf462088d13 150 input_len, NULL, &output_len ) != CKR_OK )
markrad 0:cdf462088d13 151 {
markrad 0:cdf462088d13 152 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
markrad 0:cdf462088d13 153 }
markrad 0:cdf462088d13 154
markrad 0:cdf462088d13 155 if( output_len > output_max_len )
markrad 0:cdf462088d13 156 return( MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE );
markrad 0:cdf462088d13 157
markrad 0:cdf462088d13 158 if( pkcs11h_certificate_decryptAny( ctx->pkcs11h_cert, CKM_RSA_PKCS, input,
markrad 0:cdf462088d13 159 input_len, output, &output_len ) != CKR_OK )
markrad 0:cdf462088d13 160 {
markrad 0:cdf462088d13 161 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
markrad 0:cdf462088d13 162 }
markrad 0:cdf462088d13 163 *olen = output_len;
markrad 0:cdf462088d13 164 return( 0 );
markrad 0:cdf462088d13 165 }
markrad 0:cdf462088d13 166
markrad 0:cdf462088d13 167 int mbedtls_pkcs11_sign( mbedtls_pkcs11_context *ctx,
markrad 0:cdf462088d13 168 int mode,
markrad 0:cdf462088d13 169 mbedtls_md_type_t md_alg,
markrad 0:cdf462088d13 170 unsigned int hashlen,
markrad 0:cdf462088d13 171 const unsigned char *hash,
markrad 0:cdf462088d13 172 unsigned char *sig )
markrad 0:cdf462088d13 173 {
markrad 0:cdf462088d13 174 size_t sig_len = 0, asn_len = 0, oid_size = 0;
markrad 0:cdf462088d13 175 unsigned char *p = sig;
markrad 0:cdf462088d13 176 const char *oid;
markrad 0:cdf462088d13 177
markrad 0:cdf462088d13 178 if( NULL == ctx )
markrad 0:cdf462088d13 179 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
markrad 0:cdf462088d13 180
markrad 0:cdf462088d13 181 if( MBEDTLS_RSA_PRIVATE != mode )
markrad 0:cdf462088d13 182 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
markrad 0:cdf462088d13 183
markrad 0:cdf462088d13 184 if( md_alg != MBEDTLS_MD_NONE )
markrad 0:cdf462088d13 185 {
markrad 0:cdf462088d13 186 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
markrad 0:cdf462088d13 187 if( md_info == NULL )
markrad 0:cdf462088d13 188 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
markrad 0:cdf462088d13 189
markrad 0:cdf462088d13 190 if( mbedtls_oid_get_oid_by_md( md_alg, &oid, &oid_size ) != 0 )
markrad 0:cdf462088d13 191 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
markrad 0:cdf462088d13 192
markrad 0:cdf462088d13 193 hashlen = mbedtls_md_get_size( md_info );
markrad 0:cdf462088d13 194 asn_len = 10 + oid_size;
markrad 0:cdf462088d13 195 }
markrad 0:cdf462088d13 196
markrad 0:cdf462088d13 197 sig_len = ctx->len;
markrad 0:cdf462088d13 198 if( hashlen > sig_len || asn_len > sig_len ||
markrad 0:cdf462088d13 199 hashlen + asn_len > sig_len )
markrad 0:cdf462088d13 200 {
markrad 0:cdf462088d13 201 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
markrad 0:cdf462088d13 202 }
markrad 0:cdf462088d13 203
markrad 0:cdf462088d13 204 if( md_alg != MBEDTLS_MD_NONE )
markrad 0:cdf462088d13 205 {
markrad 0:cdf462088d13 206 /*
markrad 0:cdf462088d13 207 * DigestInfo ::= SEQUENCE {
markrad 0:cdf462088d13 208 * digestAlgorithm DigestAlgorithmIdentifier,
markrad 0:cdf462088d13 209 * digest Digest }
markrad 0:cdf462088d13 210 *
markrad 0:cdf462088d13 211 * DigestAlgorithmIdentifier ::= AlgorithmIdentifier
markrad 0:cdf462088d13 212 *
markrad 0:cdf462088d13 213 * Digest ::= OCTET STRING
markrad 0:cdf462088d13 214 */
markrad 0:cdf462088d13 215 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
markrad 0:cdf462088d13 216 *p++ = (unsigned char) ( 0x08 + oid_size + hashlen );
markrad 0:cdf462088d13 217 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
markrad 0:cdf462088d13 218 *p++ = (unsigned char) ( 0x04 + oid_size );
markrad 0:cdf462088d13 219 *p++ = MBEDTLS_ASN1_OID;
markrad 0:cdf462088d13 220 *p++ = oid_size & 0xFF;
markrad 0:cdf462088d13 221 memcpy( p, oid, oid_size );
markrad 0:cdf462088d13 222 p += oid_size;
markrad 0:cdf462088d13 223 *p++ = MBEDTLS_ASN1_NULL;
markrad 0:cdf462088d13 224 *p++ = 0x00;
markrad 0:cdf462088d13 225 *p++ = MBEDTLS_ASN1_OCTET_STRING;
markrad 0:cdf462088d13 226 *p++ = hashlen;
markrad 0:cdf462088d13 227 }
markrad 0:cdf462088d13 228
markrad 0:cdf462088d13 229 memcpy( p, hash, hashlen );
markrad 0:cdf462088d13 230
markrad 0:cdf462088d13 231 if( pkcs11h_certificate_signAny( ctx->pkcs11h_cert, CKM_RSA_PKCS, sig,
markrad 0:cdf462088d13 232 asn_len + hashlen, sig, &sig_len ) != CKR_OK )
markrad 0:cdf462088d13 233 {
markrad 0:cdf462088d13 234 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
markrad 0:cdf462088d13 235 }
markrad 0:cdf462088d13 236
markrad 0:cdf462088d13 237 return( 0 );
markrad 0:cdf462088d13 238 }
markrad 0:cdf462088d13 239
markrad 0:cdf462088d13 240 #endif /* defined(MBEDTLS_PKCS11_C) */