pkcs11.c 6.37 KB
Newer Older
1 2 3 4 5 6 7
/**
 * \file pkcs11.c
 *
 * \brief Wrapper for PKCS#11 library libpkcs11-helper
 *
 * \author Adriaan de Jong <dejong@fox-it.com>
 *
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
8
 *  Copyright (C) 2006-2014, ARM Limited, All Rights Reserved
9
 *
10
 *  This file is part of mbed TLS (https://tls.mbed.org)
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License along
 *  with this program; if not, write to the Free Software Foundation, Inc.,
 *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

27
#include "mbedtls/pkcs11.h"
28

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
29
#if defined(MBEDTLS_PKCS11_C)
30

31 32 33
#include "mbedtls/md.h"
#include "mbedtls/oid.h"
#include "mbedtls/x509_crt.h"
34

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
35
#if defined(MBEDTLS_PLATFORM_C)
36
#include "mbedtls/platform.h"
37
#else
38
#include <stdlib.h>
39
#define mbedtls_calloc    calloc
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
40
#define mbedtls_free       free
41 42
#endif

43 44
#include <string.h>

45 46 47 48 49 50
void mbedtls_pkcs11_init( mbedtls_pkcs11_context *ctx )
{
    memset( ctx, 0, sizeof( mbedtls_pkcs11_context ) );
}

int mbedtls_pkcs11_x509_cert_bind( mbedtls_x509_crt *cert, pkcs11h_certificate_t pkcs11_cert )
51 52 53 54 55 56 57 58 59 60 61
{
    int ret = 1;
    unsigned char *cert_blob = NULL;
    size_t cert_blob_size = 0;

    if( cert == NULL )
    {
        ret = 2;
        goto cleanup;
    }

62 63
    if( pkcs11h_certificate_getCertificateBlob( pkcs11_cert, NULL,
                                                &cert_blob_size ) != CKR_OK )
64 65 66 67 68
    {
        ret = 3;
        goto cleanup;
    }

69
    cert_blob = mbedtls_calloc( 1, cert_blob_size );
70 71 72 73 74 75
    if( NULL == cert_blob )
    {
        ret = 4;
        goto cleanup;
    }

76 77
    if( pkcs11h_certificate_getCertificateBlob( pkcs11_cert, cert_blob,
                                                &cert_blob_size ) != CKR_OK )
78 79 80 81 82
    {
        ret = 5;
        goto cleanup;
    }

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
83
    if( 0 != mbedtls_x509_crt_parse( cert, cert_blob, cert_blob_size ) )
84 85 86 87 88 89 90 91 92
    {
        ret = 6;
        goto cleanup;
    }

    ret = 0;

cleanup:
    if( NULL != cert_blob )
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
93
        mbedtls_free( cert_blob );
94

95
    return( ret );
96 97 98
}


99
int mbedtls_pkcs11_priv_key_bind( mbedtls_pkcs11_context *priv_key,
100 101 102
        pkcs11h_certificate_t pkcs11_cert )
{
    int ret = 1;
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
103
    mbedtls_x509_crt cert;
104

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
105
    mbedtls_x509_crt_init( &cert );
106 107 108 109

    if( priv_key == NULL )
        goto cleanup;

110
    if( 0 != mbedtls_pkcs11_x509_cert_bind( &cert, pkcs11_cert ) )
111 112
        goto cleanup;

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
113
    priv_key->len = mbedtls_pk_get_len( &cert.pk );
114 115 116 117 118
    priv_key->pkcs11h_cert = pkcs11_cert;

    ret = 0;

cleanup:
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
119
    mbedtls_x509_crt_free( &cert );
120

121
    return( ret );
122 123
}

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
124
void mbedtls_pkcs11_priv_key_free( mbedtls_pkcs11_context *priv_key )
125 126 127 128 129
{
    if( NULL != priv_key )
        pkcs11h_certificate_freeCertificate( priv_key->pkcs11h_cert );
}

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
130
int mbedtls_pkcs11_decrypt( mbedtls_pkcs11_context *ctx,
131
                       int mode, size_t *olen,
132 133
                       const unsigned char *input,
                       unsigned char *output,
134
                       size_t output_max_len )
135 136 137 138
{
    size_t input_len, output_len;

    if( NULL == ctx )
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
139
        return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
140

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
141 142
    if( MBEDTLS_RSA_PRIVATE != mode )
        return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
143 144 145 146

    output_len = input_len = ctx->len;

    if( input_len < 16 || input_len > output_max_len )
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
147
        return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
148 149 150 151 152

    /* Determine size of output buffer */
    if( pkcs11h_certificate_decryptAny( ctx->pkcs11h_cert, CKM_RSA_PKCS, input,
            input_len, NULL, &output_len ) != CKR_OK )
    {
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
153
        return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
154 155 156
    }

    if( output_len > output_max_len )
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
157
        return( MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE );
158 159 160 161

    if( pkcs11h_certificate_decryptAny( ctx->pkcs11h_cert, CKM_RSA_PKCS, input,
            input_len, output, &output_len ) != CKR_OK )
    {
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
162
        return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
163 164 165 166 167
    }
    *olen = output_len;
    return( 0 );
}

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
168
int mbedtls_pkcs11_sign( mbedtls_pkcs11_context *ctx,
169
                    int mode,
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
170
                    mbedtls_md_type_t md_alg,
171
                    unsigned int hashlen,
172 173 174
                    const unsigned char *hash,
                    unsigned char *sig )
{
175
    size_t sig_len = 0, asn_len = 0, oid_size = 0;
176
    unsigned char *p = sig;
177
    const char *oid;
178 179

    if( NULL == ctx )
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
180
        return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
181

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
182 183
    if( MBEDTLS_RSA_PRIVATE != mode )
        return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
184

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
185
    if( md_alg != MBEDTLS_MD_NONE )
186
    {
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
187
        const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
188
        if( md_info == NULL )
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
189
            return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
190

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
191 192
        if( mbedtls_oid_get_oid_by_md( md_alg, &oid, &oid_size ) != 0 )
            return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
193

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
194
        hashlen = mbedtls_md_get_size( md_info );
195
        asn_len = 10 + oid_size;
196 197
    }

198
    sig_len = ctx->len;
199 200
    if( hashlen > sig_len || asn_len > sig_len ||
        hashlen + asn_len > sig_len )
201
    {
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
202
        return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
203
    }
204

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
205
    if( md_alg != MBEDTLS_MD_NONE )
206 207 208 209 210 211 212 213 214 215
    {
        /*
         * DigestInfo ::= SEQUENCE {
         *   digestAlgorithm DigestAlgorithmIdentifier,
         *   digest Digest }
         *
         * DigestAlgorithmIdentifier ::= AlgorithmIdentifier
         *
         * Digest ::= OCTET STRING
         */
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
216
        *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
217
        *p++ = (unsigned char) ( 0x08 + oid_size + hashlen );
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
218
        *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
219
        *p++ = (unsigned char) ( 0x04 + oid_size );
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
220
        *p++ = MBEDTLS_ASN1_OID;
221 222 223
        *p++ = oid_size & 0xFF;
        memcpy( p, oid, oid_size );
        p += oid_size;
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
224
        *p++ = MBEDTLS_ASN1_NULL;
225
        *p++ = 0x00;
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
226
        *p++ = MBEDTLS_ASN1_OCTET_STRING;
227
        *p++ = hashlen;
228 229
    }

230 231
    memcpy( p, hash, hashlen );

232
    if( pkcs11h_certificate_signAny( ctx->pkcs11h_cert, CKM_RSA_PKCS, sig,
233
            asn_len + hashlen, sig, &sig_len ) != CKR_OK )
234
    {
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
235
        return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
236 237 238 239 240
    }

    return( 0 );
}

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
241
#endif /* defined(MBEDTLS_PKCS11_C) */