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