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