BBR 1 Ebene

Committer:
borlanic
Date:
Mon May 14 11:29:06 2018 +0000
Revision:
0:fbdae7e6d805
BBR

Who changed what in which revision?

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