mbedtls ported to mbed-classic

Fork of mbedtls by Christopher Haster

Committer:
Brian Daniels
Date:
Thu Apr 07 11:11:18 2016 +0100
Revision:
4:bef26f687287
Parent:
1:24750b9ad5ef
Adding ported selftest test case

Who changed what in which revision?

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