blob: 054fdb78398035bce724ae6f1c393138592bfd94 [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 *
Paul Bakker7dc4c442014-02-01 22:50:26 +01008 * Copyright (C) 2006-2014, Brainspark B.V.
Paul Bakker43b7e352011-01-18 15:27:19 +00009 *
10 * This file is part of PolarSSL (http://www.polarssl.org)
11 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
12 *
13 * All rights reserved.
14 *
15 * This program is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation; either version 2 of the License, or
18 * (at your option) any later version.
19 *
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 *
25 * You should have received a copy of the GNU General Public License along
26 * with this program; if not, write to the Free Software Foundation, Inc.,
27 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
28 */
29
30#include "polarssl/pkcs11.h"
31
32#if defined(POLARSSL_PKCS11_C)
Steffan Karger28d81a02013-11-13 16:57:58 +010033#include "polarssl/md.h"
34#include "polarssl/oid.h"
35#include "polarssl/x509_crt.h"
Paul Bakker43b7e352011-01-18 15:27:19 +000036
Paul Bakker7dc4c442014-02-01 22:50:26 +010037#if defined(POLARSSL_PLATFORM_C)
38#include "polarssl/platform.h"
Paul Bakker6e339b52013-07-03 13:37:05 +020039#else
Paul Bakker7dc4c442014-02-01 22:50:26 +010040#include <stdlib.h>
Paul Bakker6e339b52013-07-03 13:37:05 +020041#define polarssl_malloc malloc
42#define polarssl_free free
43#endif
44
Paul Bakkerc559c7a2013-09-18 14:13:26 +020045int pkcs11_x509_cert_init( x509_crt *cert, pkcs11h_certificate_t pkcs11_cert )
Paul Bakker43b7e352011-01-18 15:27:19 +000046{
47 int ret = 1;
48 unsigned char *cert_blob = NULL;
49 size_t cert_blob_size = 0;
50
51 if( cert == NULL )
52 {
53 ret = 2;
54 goto cleanup;
55 }
56
57 if( pkcs11h_certificate_getCertificateBlob( pkcs11_cert, NULL, &cert_blob_size ) != CKR_OK )
58 {
59 ret = 3;
60 goto cleanup;
61 }
62
Paul Bakker6e339b52013-07-03 13:37:05 +020063 cert_blob = polarssl_malloc( cert_blob_size );
Paul Bakker43b7e352011-01-18 15:27:19 +000064 if( NULL == cert_blob )
65 {
66 ret = 4;
67 goto cleanup;
68 }
69
70 if( pkcs11h_certificate_getCertificateBlob( pkcs11_cert, cert_blob, &cert_blob_size ) != CKR_OK )
71 {
72 ret = 5;
73 goto cleanup;
74 }
75
Paul Bakkerc559c7a2013-09-18 14:13:26 +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
88 return ret;
89}
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
Steffan Karger28d81a02013-11-13 16:57:58 +0100106 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
114 return ret;
115}
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 )
173 return POLARSSL_ERR_RSA_BAD_INPUT_DATA;
174
Steffan Karger28d81a02013-11-13 16:57:58 +0100175 if( RSA_PRIVATE != mode )
Paul Bakker43b7e352011-01-18 15:27:19 +0000176 return POLARSSL_ERR_RSA_BAD_INPUT_DATA;
177
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;
192 if ( hashlen > ctx_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
198 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) */