Includes library modifications to allow access to AIN_4 (AIN_0 / 5)

Committer:
bryantaylor
Date:
Tue Sep 20 21:26:12 2016 +0000
Revision:
0:eafc3fd41f75
hackathon

Who changed what in which revision?

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