blob: 394354149c1e2b8a7f7458bcbcd283b502c23316 [file] [log] [blame]
Paul Bakker43b7e352011-01-18 15:27:19 +00001/**
2 * \file pkcs11.c
3 *
4 * \brief Wrapper for PKCS#11 library libpkcs11-helper
5 *
6 * \author Adriaan de Jong <dejong@fox-it.com>
7 *
Manuel Pégourié-Gonnarda658a402015-01-23 09:45:19 +00008 * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved
Paul Bakker43b7e352011-01-18 15:27:19 +00009 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000010 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker43b7e352011-01-18 15:27:19 +000011 *
Paul Bakker43b7e352011-01-18 15:27:19 +000012 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License along
23 * with this program; if not, write to the Free Software Foundation, Inc.,
24 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 */
26
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000027#include "mbedtls/pkcs11.h"
Paul Bakker43b7e352011-01-18 15:27:19 +000028
29#if defined(POLARSSL_PKCS11_C)
Rich Evans00ab4702015-02-06 13:43:58 +000030
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000031#include "mbedtls/md.h"
32#include "mbedtls/oid.h"
33#include "mbedtls/x509_crt.h"
Paul Bakker43b7e352011-01-18 15:27:19 +000034
Paul Bakker7dc4c442014-02-01 22:50:26 +010035#if defined(POLARSSL_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000036#include "mbedtls/platform.h"
Paul Bakker6e339b52013-07-03 13:37:05 +020037#else
Paul Bakker7dc4c442014-02-01 22:50:26 +010038#include <stdlib.h>
Paul Bakker6e339b52013-07-03 13:37:05 +020039#define polarssl_malloc malloc
40#define polarssl_free free
41#endif
42
Paul Bakkerc559c7a2013-09-18 14:13:26 +020043int pkcs11_x509_cert_init( x509_crt *cert, pkcs11h_certificate_t pkcs11_cert )
Paul Bakker43b7e352011-01-18 15:27:19 +000044{
45 int ret = 1;
46 unsigned char *cert_blob = NULL;
47 size_t cert_blob_size = 0;
48
49 if( cert == NULL )
50 {
51 ret = 2;
52 goto cleanup;
53 }
54
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +020055 if( pkcs11h_certificate_getCertificateBlob( pkcs11_cert, NULL,
56 &cert_blob_size ) != CKR_OK )
Paul Bakker43b7e352011-01-18 15:27:19 +000057 {
58 ret = 3;
59 goto cleanup;
60 }
61
Paul Bakker6e339b52013-07-03 13:37:05 +020062 cert_blob = polarssl_malloc( cert_blob_size );
Paul Bakker43b7e352011-01-18 15:27:19 +000063 if( NULL == cert_blob )
64 {
65 ret = 4;
66 goto cleanup;
67 }
68
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +020069 if( pkcs11h_certificate_getCertificateBlob( pkcs11_cert, cert_blob,
70 &cert_blob_size ) != CKR_OK )
Paul Bakker43b7e352011-01-18 15:27:19 +000071 {
72 ret = 5;
73 goto cleanup;
74 }
75
Paul Bakker66d5d072014-06-17 16:39:18 +020076 if( 0 != x509_crt_parse( cert, cert_blob, cert_blob_size ) )
Paul Bakker43b7e352011-01-18 15:27:19 +000077 {
78 ret = 6;
79 goto cleanup;
80 }
81
82 ret = 0;
83
84cleanup:
85 if( NULL != cert_blob )
Paul Bakker6e339b52013-07-03 13:37:05 +020086 polarssl_free( cert_blob );
Paul Bakker43b7e352011-01-18 15:27:19 +000087
Paul Bakkerd8bb8262014-06-17 14:06:49 +020088 return( ret );
Paul Bakker43b7e352011-01-18 15:27:19 +000089}
90
91
92int pkcs11_priv_key_init( pkcs11_context *priv_key,
93 pkcs11h_certificate_t pkcs11_cert )
94{
95 int ret = 1;
Paul Bakkerc559c7a2013-09-18 14:13:26 +020096 x509_crt cert;
Paul Bakker43b7e352011-01-18 15:27:19 +000097
Paul Bakkerc559c7a2013-09-18 14:13:26 +020098 x509_crt_init( &cert );
Paul Bakker43b7e352011-01-18 15:27:19 +000099
100 if( priv_key == NULL )
101 goto cleanup;
102
103 if( 0 != pkcs11_x509_cert_init( &cert, pkcs11_cert ) )
104 goto cleanup;
105
Paul Bakker66d5d072014-06-17 16:39:18 +0200106 priv_key->len = pk_get_len( &cert.pk );
Paul Bakker43b7e352011-01-18 15:27:19 +0000107 priv_key->pkcs11h_cert = pkcs11_cert;
108
109 ret = 0;
110
111cleanup:
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200112 x509_crt_free( &cert );
Paul Bakker43b7e352011-01-18 15:27:19 +0000113
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200114 return( ret );
Paul Bakker43b7e352011-01-18 15:27:19 +0000115}
116
117void pkcs11_priv_key_free( pkcs11_context *priv_key )
118{
119 if( NULL != priv_key )
120 pkcs11h_certificate_freeCertificate( priv_key->pkcs11h_cert );
121}
122
123int pkcs11_decrypt( pkcs11_context *ctx,
Paul Bakker23986e52011-04-24 08:57:21 +0000124 int mode, size_t *olen,
Paul Bakker43b7e352011-01-18 15:27:19 +0000125 const unsigned char *input,
126 unsigned char *output,
Paul Bakker9a736322012-11-14 12:39:52 +0000127 size_t output_max_len )
Paul Bakker43b7e352011-01-18 15:27:19 +0000128{
129 size_t input_len, output_len;
130
131 if( NULL == ctx )
132 return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
133
Steffan Karger28d81a02013-11-13 16:57:58 +0100134 if( RSA_PRIVATE != mode )
Paul Bakker43b7e352011-01-18 15:27:19 +0000135 return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
136
137 output_len = input_len = ctx->len;
138
139 if( input_len < 16 || input_len > output_max_len )
140 return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
141
142 /* Determine size of output buffer */
143 if( pkcs11h_certificate_decryptAny( ctx->pkcs11h_cert, CKM_RSA_PKCS, input,
144 input_len, NULL, &output_len ) != CKR_OK )
145 {
146 return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
147 }
148
149 if( output_len > output_max_len )
150 return( POLARSSL_ERR_RSA_OUTPUT_TOO_LARGE );
151
152 if( pkcs11h_certificate_decryptAny( ctx->pkcs11h_cert, CKM_RSA_PKCS, input,
153 input_len, output, &output_len ) != CKR_OK )
154 {
155 return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
156 }
157 *olen = output_len;
158 return( 0 );
159}
160
161int pkcs11_sign( pkcs11_context *ctx,
162 int mode,
Steffan Karger28d81a02013-11-13 16:57:58 +0100163 md_type_t md_alg,
Paul Bakker23986e52011-04-24 08:57:21 +0000164 unsigned int hashlen,
Paul Bakker43b7e352011-01-18 15:27:19 +0000165 const unsigned char *hash,
166 unsigned char *sig )
167{
Paul Bakkerdb1f0592014-03-26 14:53:47 +0100168 size_t sig_len = 0, asn_len = 0, oid_size = 0;
Paul Bakker43b7e352011-01-18 15:27:19 +0000169 unsigned char *p = sig;
Steffan Karger28d81a02013-11-13 16:57:58 +0100170 const char *oid;
Paul Bakker43b7e352011-01-18 15:27:19 +0000171
172 if( NULL == ctx )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200173 return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker43b7e352011-01-18 15:27:19 +0000174
Steffan Karger28d81a02013-11-13 16:57:58 +0100175 if( RSA_PRIVATE != mode )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200176 return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker43b7e352011-01-18 15:27:19 +0000177
Steffan Karger28d81a02013-11-13 16:57:58 +0100178 if( md_alg != POLARSSL_MD_NONE )
Paul Bakker43b7e352011-01-18 15:27:19 +0000179 {
Steffan Karger28d81a02013-11-13 16:57:58 +0100180 const md_info_t *md_info = md_info_from_type( md_alg );
181 if( md_info == NULL )
Paul Bakker43b7e352011-01-18 15:27:19 +0000182 return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
Steffan Karger28d81a02013-11-13 16:57:58 +0100183
184 if( oid_get_oid_by_md( md_alg, &oid, &oid_size ) != 0 )
185 return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
186
187 hashlen = md_get_size( md_info );
Paul Bakkerdb1f0592014-03-26 14:53:47 +0100188 asn_len = 10 + oid_size;
Steffan Karger28d81a02013-11-13 16:57:58 +0100189 }
190
Paul Bakkerdb1f0592014-03-26 14:53:47 +0100191 sig_len = ctx->len;
Paul Bakker66d5d072014-06-17 16:39:18 +0200192 if( hashlen > sig_len || asn_len > sig_len ||
193 hashlen + asn_len > sig_len )
Steffan Karger28d81a02013-11-13 16:57:58 +0100194 {
Paul Bakkerdb1f0592014-03-26 14:53:47 +0100195 return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
Steffan Karger28d81a02013-11-13 16:57:58 +0100196 }
Paul Bakkerdb1f0592014-03-26 14:53:47 +0100197
Paul Bakker66d5d072014-06-17 16:39:18 +0200198 if( md_alg != POLARSSL_MD_NONE )
Steffan Karger28d81a02013-11-13 16:57:58 +0100199 {
200 /*
201 * DigestInfo ::= SEQUENCE {
202 * digestAlgorithm DigestAlgorithmIdentifier,
203 * digest Digest }
204 *
205 * DigestAlgorithmIdentifier ::= AlgorithmIdentifier
206 *
207 * Digest ::= OCTET STRING
208 */
209 *p++ = ASN1_SEQUENCE | ASN1_CONSTRUCTED;
210 *p++ = (unsigned char) ( 0x08 + oid_size + hashlen );
211 *p++ = ASN1_SEQUENCE | ASN1_CONSTRUCTED;
212 *p++ = (unsigned char) ( 0x04 + oid_size );
213 *p++ = ASN1_OID;
214 *p++ = oid_size & 0xFF;
215 memcpy( p, oid, oid_size );
216 p += oid_size;
217 *p++ = ASN1_NULL;
218 *p++ = 0x00;
219 *p++ = ASN1_OCTET_STRING;
220 *p++ = hashlen;
Paul Bakker43b7e352011-01-18 15:27:19 +0000221 }
222
Paul Bakkerdb1f0592014-03-26 14:53:47 +0100223 memcpy( p, hash, hashlen );
224
Paul Bakker43b7e352011-01-18 15:27:19 +0000225 if( pkcs11h_certificate_signAny( ctx->pkcs11h_cert, CKM_RSA_PKCS, sig,
Paul Bakkerdb1f0592014-03-26 14:53:47 +0100226 asn_len + hashlen, sig, &sig_len ) != CKR_OK )
Paul Bakker43b7e352011-01-18 15:27:19 +0000227 {
228 return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
229 }
230
231 return( 0 );
232}
233
234#endif /* defined(POLARSSL_PKCS11_C) */