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