mbed os with nrf51 internal bandgap enabled to read battery level

Dependents:   BLE_file_test BLE_Blink ExternalEncoder

Committer:
elessair
Date:
Sun Oct 23 15:10:02 2016 +0000
Revision:
0:f269e3021894
Initial commit

Who changed what in which revision?

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