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