mbed TLS library
Dependents: HTTPClient-SSL WS_SERVER
polarssl/pkcs11.c@0:137634ff4186, 2015-06-11 (annotated)
- Committer:
- ansond
- Date:
- Thu Jun 11 03:27:03 2015 +0000
- Revision:
- 0:137634ff4186
initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ansond | 0:137634ff4186 | 1 | /** |
ansond | 0:137634ff4186 | 2 | * \file pkcs11.c |
ansond | 0:137634ff4186 | 3 | * |
ansond | 0:137634ff4186 | 4 | * \brief Wrapper for PKCS#11 library libpkcs11-helper |
ansond | 0:137634ff4186 | 5 | * |
ansond | 0:137634ff4186 | 6 | * \author Adriaan de Jong <dejong@fox-it.com> |
ansond | 0:137634ff4186 | 7 | * |
ansond | 0:137634ff4186 | 8 | * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved |
ansond | 0:137634ff4186 | 9 | * |
ansond | 0:137634ff4186 | 10 | * This file is part of mbed TLS (https://tls.mbed.org) |
ansond | 0:137634ff4186 | 11 | * |
ansond | 0:137634ff4186 | 12 | * This program is free software; you can redistribute it and/or modify |
ansond | 0:137634ff4186 | 13 | * it under the terms of the GNU General Public License as published by |
ansond | 0:137634ff4186 | 14 | * the Free Software Foundation; either version 2 of the License, or |
ansond | 0:137634ff4186 | 15 | * (at your option) any later version. |
ansond | 0:137634ff4186 | 16 | * |
ansond | 0:137634ff4186 | 17 | * This program is distributed in the hope that it will be useful, |
ansond | 0:137634ff4186 | 18 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
ansond | 0:137634ff4186 | 19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
ansond | 0:137634ff4186 | 20 | * GNU General Public License for more details. |
ansond | 0:137634ff4186 | 21 | * |
ansond | 0:137634ff4186 | 22 | * You should have received a copy of the GNU General Public License along |
ansond | 0:137634ff4186 | 23 | * with this program; if not, write to the Free Software Foundation, Inc., |
ansond | 0:137634ff4186 | 24 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
ansond | 0:137634ff4186 | 25 | */ |
ansond | 0:137634ff4186 | 26 | |
ansond | 0:137634ff4186 | 27 | #include "polarssl/pkcs11.h" |
ansond | 0:137634ff4186 | 28 | |
ansond | 0:137634ff4186 | 29 | #if defined(POLARSSL_PKCS11_C) |
ansond | 0:137634ff4186 | 30 | |
ansond | 0:137634ff4186 | 31 | #include "polarssl/md.h" |
ansond | 0:137634ff4186 | 32 | #include "polarssl/oid.h" |
ansond | 0:137634ff4186 | 33 | #include "polarssl/x509_crt.h" |
ansond | 0:137634ff4186 | 34 | |
ansond | 0:137634ff4186 | 35 | #if defined(POLARSSL_PLATFORM_C) |
ansond | 0:137634ff4186 | 36 | #include "polarssl/platform.h" |
ansond | 0:137634ff4186 | 37 | #else |
ansond | 0:137634ff4186 | 38 | #include <stdlib.h> |
ansond | 0:137634ff4186 | 39 | #define polarssl_malloc malloc |
ansond | 0:137634ff4186 | 40 | #define polarssl_free free |
ansond | 0:137634ff4186 | 41 | #endif |
ansond | 0:137634ff4186 | 42 | |
ansond | 0:137634ff4186 | 43 | int pkcs11_x509_cert_init( x509_crt *cert, pkcs11h_certificate_t pkcs11_cert ) |
ansond | 0:137634ff4186 | 44 | { |
ansond | 0:137634ff4186 | 45 | int ret = 1; |
ansond | 0:137634ff4186 | 46 | unsigned char *cert_blob = NULL; |
ansond | 0:137634ff4186 | 47 | size_t cert_blob_size = 0; |
ansond | 0:137634ff4186 | 48 | |
ansond | 0:137634ff4186 | 49 | if( cert == NULL ) |
ansond | 0:137634ff4186 | 50 | { |
ansond | 0:137634ff4186 | 51 | ret = 2; |
ansond | 0:137634ff4186 | 52 | goto cleanup; |
ansond | 0:137634ff4186 | 53 | } |
ansond | 0:137634ff4186 | 54 | |
ansond | 0:137634ff4186 | 55 | if( pkcs11h_certificate_getCertificateBlob( pkcs11_cert, NULL, |
ansond | 0:137634ff4186 | 56 | &cert_blob_size ) != CKR_OK ) |
ansond | 0:137634ff4186 | 57 | { |
ansond | 0:137634ff4186 | 58 | ret = 3; |
ansond | 0:137634ff4186 | 59 | goto cleanup; |
ansond | 0:137634ff4186 | 60 | } |
ansond | 0:137634ff4186 | 61 | |
ansond | 0:137634ff4186 | 62 | cert_blob = polarssl_malloc( cert_blob_size ); |
ansond | 0:137634ff4186 | 63 | if( NULL == cert_blob ) |
ansond | 0:137634ff4186 | 64 | { |
ansond | 0:137634ff4186 | 65 | ret = 4; |
ansond | 0:137634ff4186 | 66 | goto cleanup; |
ansond | 0:137634ff4186 | 67 | } |
ansond | 0:137634ff4186 | 68 | |
ansond | 0:137634ff4186 | 69 | if( pkcs11h_certificate_getCertificateBlob( pkcs11_cert, cert_blob, |
ansond | 0:137634ff4186 | 70 | &cert_blob_size ) != CKR_OK ) |
ansond | 0:137634ff4186 | 71 | { |
ansond | 0:137634ff4186 | 72 | ret = 5; |
ansond | 0:137634ff4186 | 73 | goto cleanup; |
ansond | 0:137634ff4186 | 74 | } |
ansond | 0:137634ff4186 | 75 | |
ansond | 0:137634ff4186 | 76 | if( 0 != x509_crt_parse( cert, cert_blob, cert_blob_size ) ) |
ansond | 0:137634ff4186 | 77 | { |
ansond | 0:137634ff4186 | 78 | ret = 6; |
ansond | 0:137634ff4186 | 79 | goto cleanup; |
ansond | 0:137634ff4186 | 80 | } |
ansond | 0:137634ff4186 | 81 | |
ansond | 0:137634ff4186 | 82 | ret = 0; |
ansond | 0:137634ff4186 | 83 | |
ansond | 0:137634ff4186 | 84 | cleanup: |
ansond | 0:137634ff4186 | 85 | if( NULL != cert_blob ) |
ansond | 0:137634ff4186 | 86 | polarssl_free( cert_blob ); |
ansond | 0:137634ff4186 | 87 | |
ansond | 0:137634ff4186 | 88 | return( ret ); |
ansond | 0:137634ff4186 | 89 | } |
ansond | 0:137634ff4186 | 90 | |
ansond | 0:137634ff4186 | 91 | |
ansond | 0:137634ff4186 | 92 | int pkcs11_priv_key_init( pkcs11_context *priv_key, |
ansond | 0:137634ff4186 | 93 | pkcs11h_certificate_t pkcs11_cert ) |
ansond | 0:137634ff4186 | 94 | { |
ansond | 0:137634ff4186 | 95 | int ret = 1; |
ansond | 0:137634ff4186 | 96 | x509_crt cert; |
ansond | 0:137634ff4186 | 97 | |
ansond | 0:137634ff4186 | 98 | x509_crt_init( &cert ); |
ansond | 0:137634ff4186 | 99 | |
ansond | 0:137634ff4186 | 100 | if( priv_key == NULL ) |
ansond | 0:137634ff4186 | 101 | goto cleanup; |
ansond | 0:137634ff4186 | 102 | |
ansond | 0:137634ff4186 | 103 | if( 0 != pkcs11_x509_cert_init( &cert, pkcs11_cert ) ) |
ansond | 0:137634ff4186 | 104 | goto cleanup; |
ansond | 0:137634ff4186 | 105 | |
ansond | 0:137634ff4186 | 106 | priv_key->len = pk_get_len( &cert.pk ); |
ansond | 0:137634ff4186 | 107 | priv_key->pkcs11h_cert = pkcs11_cert; |
ansond | 0:137634ff4186 | 108 | |
ansond | 0:137634ff4186 | 109 | ret = 0; |
ansond | 0:137634ff4186 | 110 | |
ansond | 0:137634ff4186 | 111 | cleanup: |
ansond | 0:137634ff4186 | 112 | x509_crt_free( &cert ); |
ansond | 0:137634ff4186 | 113 | |
ansond | 0:137634ff4186 | 114 | return( ret ); |
ansond | 0:137634ff4186 | 115 | } |
ansond | 0:137634ff4186 | 116 | |
ansond | 0:137634ff4186 | 117 | void pkcs11_priv_key_free( pkcs11_context *priv_key ) |
ansond | 0:137634ff4186 | 118 | { |
ansond | 0:137634ff4186 | 119 | if( NULL != priv_key ) |
ansond | 0:137634ff4186 | 120 | pkcs11h_certificate_freeCertificate( priv_key->pkcs11h_cert ); |
ansond | 0:137634ff4186 | 121 | } |
ansond | 0:137634ff4186 | 122 | |
ansond | 0:137634ff4186 | 123 | int pkcs11_decrypt( pkcs11_context *ctx, |
ansond | 0:137634ff4186 | 124 | int mode, size_t *olen, |
ansond | 0:137634ff4186 | 125 | const unsigned char *input, |
ansond | 0:137634ff4186 | 126 | unsigned char *output, |
ansond | 0:137634ff4186 | 127 | size_t output_max_len ) |
ansond | 0:137634ff4186 | 128 | { |
ansond | 0:137634ff4186 | 129 | size_t input_len, output_len; |
ansond | 0:137634ff4186 | 130 | |
ansond | 0:137634ff4186 | 131 | if( NULL == ctx ) |
ansond | 0:137634ff4186 | 132 | return( POLARSSL_ERR_RSA_BAD_INPUT_DATA ); |
ansond | 0:137634ff4186 | 133 | |
ansond | 0:137634ff4186 | 134 | if( RSA_PRIVATE != mode ) |
ansond | 0:137634ff4186 | 135 | return( POLARSSL_ERR_RSA_BAD_INPUT_DATA ); |
ansond | 0:137634ff4186 | 136 | |
ansond | 0:137634ff4186 | 137 | output_len = input_len = ctx->len; |
ansond | 0:137634ff4186 | 138 | |
ansond | 0:137634ff4186 | 139 | if( input_len < 16 || input_len > output_max_len ) |
ansond | 0:137634ff4186 | 140 | return( POLARSSL_ERR_RSA_BAD_INPUT_DATA ); |
ansond | 0:137634ff4186 | 141 | |
ansond | 0:137634ff4186 | 142 | /* Determine size of output buffer */ |
ansond | 0:137634ff4186 | 143 | if( pkcs11h_certificate_decryptAny( ctx->pkcs11h_cert, CKM_RSA_PKCS, input, |
ansond | 0:137634ff4186 | 144 | input_len, NULL, &output_len ) != CKR_OK ) |
ansond | 0:137634ff4186 | 145 | { |
ansond | 0:137634ff4186 | 146 | return( POLARSSL_ERR_RSA_BAD_INPUT_DATA ); |
ansond | 0:137634ff4186 | 147 | } |
ansond | 0:137634ff4186 | 148 | |
ansond | 0:137634ff4186 | 149 | if( output_len > output_max_len ) |
ansond | 0:137634ff4186 | 150 | return( POLARSSL_ERR_RSA_OUTPUT_TOO_LARGE ); |
ansond | 0:137634ff4186 | 151 | |
ansond | 0:137634ff4186 | 152 | if( pkcs11h_certificate_decryptAny( ctx->pkcs11h_cert, CKM_RSA_PKCS, input, |
ansond | 0:137634ff4186 | 153 | input_len, output, &output_len ) != CKR_OK ) |
ansond | 0:137634ff4186 | 154 | { |
ansond | 0:137634ff4186 | 155 | return( POLARSSL_ERR_RSA_BAD_INPUT_DATA ); |
ansond | 0:137634ff4186 | 156 | } |
ansond | 0:137634ff4186 | 157 | *olen = output_len; |
ansond | 0:137634ff4186 | 158 | return( 0 ); |
ansond | 0:137634ff4186 | 159 | } |
ansond | 0:137634ff4186 | 160 | |
ansond | 0:137634ff4186 | 161 | int pkcs11_sign( pkcs11_context *ctx, |
ansond | 0:137634ff4186 | 162 | int mode, |
ansond | 0:137634ff4186 | 163 | md_type_t md_alg, |
ansond | 0:137634ff4186 | 164 | unsigned int hashlen, |
ansond | 0:137634ff4186 | 165 | const unsigned char *hash, |
ansond | 0:137634ff4186 | 166 | unsigned char *sig ) |
ansond | 0:137634ff4186 | 167 | { |
ansond | 0:137634ff4186 | 168 | size_t sig_len = 0, asn_len = 0, oid_size = 0; |
ansond | 0:137634ff4186 | 169 | unsigned char *p = sig; |
ansond | 0:137634ff4186 | 170 | const char *oid; |
ansond | 0:137634ff4186 | 171 | |
ansond | 0:137634ff4186 | 172 | if( NULL == ctx ) |
ansond | 0:137634ff4186 | 173 | return( POLARSSL_ERR_RSA_BAD_INPUT_DATA ); |
ansond | 0:137634ff4186 | 174 | |
ansond | 0:137634ff4186 | 175 | if( RSA_PRIVATE != mode ) |
ansond | 0:137634ff4186 | 176 | return( POLARSSL_ERR_RSA_BAD_INPUT_DATA ); |
ansond | 0:137634ff4186 | 177 | |
ansond | 0:137634ff4186 | 178 | if( md_alg != POLARSSL_MD_NONE ) |
ansond | 0:137634ff4186 | 179 | { |
ansond | 0:137634ff4186 | 180 | const md_info_t *md_info = md_info_from_type( md_alg ); |
ansond | 0:137634ff4186 | 181 | if( md_info == NULL ) |
ansond | 0:137634ff4186 | 182 | return( POLARSSL_ERR_RSA_BAD_INPUT_DATA ); |
ansond | 0:137634ff4186 | 183 | |
ansond | 0:137634ff4186 | 184 | if( oid_get_oid_by_md( md_alg, &oid, &oid_size ) != 0 ) |
ansond | 0:137634ff4186 | 185 | return( POLARSSL_ERR_RSA_BAD_INPUT_DATA ); |
ansond | 0:137634ff4186 | 186 | |
ansond | 0:137634ff4186 | 187 | hashlen = md_get_size( md_info ); |
ansond | 0:137634ff4186 | 188 | asn_len = 10 + oid_size; |
ansond | 0:137634ff4186 | 189 | } |
ansond | 0:137634ff4186 | 190 | |
ansond | 0:137634ff4186 | 191 | sig_len = ctx->len; |
ansond | 0:137634ff4186 | 192 | if( hashlen > sig_len || asn_len > sig_len || |
ansond | 0:137634ff4186 | 193 | hashlen + asn_len > sig_len ) |
ansond | 0:137634ff4186 | 194 | { |
ansond | 0:137634ff4186 | 195 | return( POLARSSL_ERR_RSA_BAD_INPUT_DATA ); |
ansond | 0:137634ff4186 | 196 | } |
ansond | 0:137634ff4186 | 197 | |
ansond | 0:137634ff4186 | 198 | if( md_alg != POLARSSL_MD_NONE ) |
ansond | 0:137634ff4186 | 199 | { |
ansond | 0:137634ff4186 | 200 | /* |
ansond | 0:137634ff4186 | 201 | * DigestInfo ::= SEQUENCE { |
ansond | 0:137634ff4186 | 202 | * digestAlgorithm DigestAlgorithmIdentifier, |
ansond | 0:137634ff4186 | 203 | * digest Digest } |
ansond | 0:137634ff4186 | 204 | * |
ansond | 0:137634ff4186 | 205 | * DigestAlgorithmIdentifier ::= AlgorithmIdentifier |
ansond | 0:137634ff4186 | 206 | * |
ansond | 0:137634ff4186 | 207 | * Digest ::= OCTET STRING |
ansond | 0:137634ff4186 | 208 | */ |
ansond | 0:137634ff4186 | 209 | *p++ = ASN1_SEQUENCE | ASN1_CONSTRUCTED; |
ansond | 0:137634ff4186 | 210 | *p++ = (unsigned char) ( 0x08 + oid_size + hashlen ); |
ansond | 0:137634ff4186 | 211 | *p++ = ASN1_SEQUENCE | ASN1_CONSTRUCTED; |
ansond | 0:137634ff4186 | 212 | *p++ = (unsigned char) ( 0x04 + oid_size ); |
ansond | 0:137634ff4186 | 213 | *p++ = ASN1_OID; |
ansond | 0:137634ff4186 | 214 | *p++ = oid_size & 0xFF; |
ansond | 0:137634ff4186 | 215 | memcpy( p, oid, oid_size ); |
ansond | 0:137634ff4186 | 216 | p += oid_size; |
ansond | 0:137634ff4186 | 217 | *p++ = ASN1_NULL; |
ansond | 0:137634ff4186 | 218 | *p++ = 0x00; |
ansond | 0:137634ff4186 | 219 | *p++ = ASN1_OCTET_STRING; |
ansond | 0:137634ff4186 | 220 | *p++ = hashlen; |
ansond | 0:137634ff4186 | 221 | } |
ansond | 0:137634ff4186 | 222 | |
ansond | 0:137634ff4186 | 223 | memcpy( p, hash, hashlen ); |
ansond | 0:137634ff4186 | 224 | |
ansond | 0:137634ff4186 | 225 | if( pkcs11h_certificate_signAny( ctx->pkcs11h_cert, CKM_RSA_PKCS, sig, |
ansond | 0:137634ff4186 | 226 | asn_len + hashlen, sig, &sig_len ) != CKR_OK ) |
ansond | 0:137634ff4186 | 227 | { |
ansond | 0:137634ff4186 | 228 | return( POLARSSL_ERR_RSA_BAD_INPUT_DATA ); |
ansond | 0:137634ff4186 | 229 | } |
ansond | 0:137634ff4186 | 230 | |
ansond | 0:137634ff4186 | 231 | return( 0 ); |
ansond | 0:137634ff4186 | 232 | } |
ansond | 0:137634ff4186 | 233 | |
ansond | 0:137634ff4186 | 234 | #endif /* defined(POLARSSL_PKCS11_C) */ |
ansond | 0:137634ff4186 | 235 |