blob: c61287a0c0894b5458609d7467f4f2147764a82c [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
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +020057 if( pkcs11h_certificate_getCertificateBlob( pkcs11_cert, NULL,
58 &cert_blob_size ) != CKR_OK )
Paul Bakker43b7e352011-01-18 15:27:19 +000059 {
60 ret = 3;
61 goto cleanup;
62 }
63
Paul Bakker6e339b52013-07-03 13:37:05 +020064 cert_blob = polarssl_malloc( cert_blob_size );
Paul Bakker43b7e352011-01-18 15:27:19 +000065 if( NULL == cert_blob )
66 {
67 ret = 4;
68 goto cleanup;
69 }
70
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +020071 if( pkcs11h_certificate_getCertificateBlob( pkcs11_cert, cert_blob,
72 &cert_blob_size ) != CKR_OK )
Paul Bakker43b7e352011-01-18 15:27:19 +000073 {
74 ret = 5;
75 goto cleanup;
76 }
77
Paul Bakkerc559c7a2013-09-18 14:13:26 +020078 if( 0 != x509_crt_parse(cert, cert_blob, cert_blob_size ) )
Paul Bakker43b7e352011-01-18 15:27:19 +000079 {
80 ret = 6;
81 goto cleanup;
82 }
83
84 ret = 0;
85
86cleanup:
87 if( NULL != cert_blob )
Paul Bakker6e339b52013-07-03 13:37:05 +020088 polarssl_free( cert_blob );
Paul Bakker43b7e352011-01-18 15:27:19 +000089
90 return ret;
91}
92
93
94int pkcs11_priv_key_init( pkcs11_context *priv_key,
95 pkcs11h_certificate_t pkcs11_cert )
96{
97 int ret = 1;
Paul Bakkerc559c7a2013-09-18 14:13:26 +020098 x509_crt cert;
Paul Bakker43b7e352011-01-18 15:27:19 +000099
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200100 x509_crt_init( &cert );
Paul Bakker43b7e352011-01-18 15:27:19 +0000101
102 if( priv_key == NULL )
103 goto cleanup;
104
105 if( 0 != pkcs11_x509_cert_init( &cert, pkcs11_cert ) )
106 goto cleanup;
107
Steffan Karger28d81a02013-11-13 16:57:58 +0100108 priv_key->len = pk_get_len(&cert.pk);
Paul Bakker43b7e352011-01-18 15:27:19 +0000109 priv_key->pkcs11h_cert = pkcs11_cert;
110
111 ret = 0;
112
113cleanup:
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200114 x509_crt_free( &cert );
Paul Bakker43b7e352011-01-18 15:27:19 +0000115
116 return ret;
117}
118
119void pkcs11_priv_key_free( pkcs11_context *priv_key )
120{
121 if( NULL != priv_key )
122 pkcs11h_certificate_freeCertificate( priv_key->pkcs11h_cert );
123}
124
125int pkcs11_decrypt( pkcs11_context *ctx,
Paul Bakker23986e52011-04-24 08:57:21 +0000126 int mode, size_t *olen,
Paul Bakker43b7e352011-01-18 15:27:19 +0000127 const unsigned char *input,
128 unsigned char *output,
Paul Bakker9a736322012-11-14 12:39:52 +0000129 size_t output_max_len )
Paul Bakker43b7e352011-01-18 15:27:19 +0000130{
131 size_t input_len, output_len;
132
133 if( NULL == ctx )
134 return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
135
Steffan Karger28d81a02013-11-13 16:57:58 +0100136 if( RSA_PRIVATE != mode )
Paul Bakker43b7e352011-01-18 15:27:19 +0000137 return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
138
139 output_len = input_len = ctx->len;
140
141 if( input_len < 16 || input_len > output_max_len )
142 return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
143
144 /* Determine size of output buffer */
145 if( pkcs11h_certificate_decryptAny( ctx->pkcs11h_cert, CKM_RSA_PKCS, input,
146 input_len, NULL, &output_len ) != CKR_OK )
147 {
148 return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
149 }
150
151 if( output_len > output_max_len )
152 return( POLARSSL_ERR_RSA_OUTPUT_TOO_LARGE );
153
154 if( pkcs11h_certificate_decryptAny( ctx->pkcs11h_cert, CKM_RSA_PKCS, input,
155 input_len, output, &output_len ) != CKR_OK )
156 {
157 return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
158 }
159 *olen = output_len;
160 return( 0 );
161}
162
163int pkcs11_sign( pkcs11_context *ctx,
164 int mode,
Steffan Karger28d81a02013-11-13 16:57:58 +0100165 md_type_t md_alg,
Paul Bakker23986e52011-04-24 08:57:21 +0000166 unsigned int hashlen,
Paul Bakker43b7e352011-01-18 15:27:19 +0000167 const unsigned char *hash,
168 unsigned char *sig )
169{
Paul Bakkerdb1f0592014-03-26 14:53:47 +0100170 size_t sig_len = 0, asn_len = 0, oid_size = 0;
Paul Bakker43b7e352011-01-18 15:27:19 +0000171 unsigned char *p = sig;
Steffan Karger28d81a02013-11-13 16:57:58 +0100172 const char *oid;
Paul Bakker43b7e352011-01-18 15:27:19 +0000173
174 if( NULL == ctx )
175 return POLARSSL_ERR_RSA_BAD_INPUT_DATA;
176
Steffan Karger28d81a02013-11-13 16:57:58 +0100177 if( RSA_PRIVATE != mode )
Paul Bakker43b7e352011-01-18 15:27:19 +0000178 return POLARSSL_ERR_RSA_BAD_INPUT_DATA;
179
Steffan Karger28d81a02013-11-13 16:57:58 +0100180 if( md_alg != POLARSSL_MD_NONE )
Paul Bakker43b7e352011-01-18 15:27:19 +0000181 {
Steffan Karger28d81a02013-11-13 16:57:58 +0100182 const md_info_t *md_info = md_info_from_type( md_alg );
183 if( md_info == NULL )
Paul Bakker43b7e352011-01-18 15:27:19 +0000184 return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
Steffan Karger28d81a02013-11-13 16:57:58 +0100185
186 if( oid_get_oid_by_md( md_alg, &oid, &oid_size ) != 0 )
187 return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
188
189 hashlen = md_get_size( md_info );
Paul Bakkerdb1f0592014-03-26 14:53:47 +0100190 asn_len = 10 + oid_size;
Steffan Karger28d81a02013-11-13 16:57:58 +0100191 }
192
Paul Bakkerdb1f0592014-03-26 14:53:47 +0100193 sig_len = ctx->len;
Paul Bakker4ffcd2f2014-04-25 11:44:12 +0200194 if ( hashlen > sig_len || asn_len > sig_len ||
Paul Bakkerdb1f0592014-03-26 14:53:47 +0100195 hashlen + asn_len > sig_len )
Steffan Karger28d81a02013-11-13 16:57:58 +0100196 {
Paul Bakkerdb1f0592014-03-26 14:53:47 +0100197 return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
Steffan Karger28d81a02013-11-13 16:57:58 +0100198 }
Paul Bakkerdb1f0592014-03-26 14:53:47 +0100199
200 if( md_alg != POLARSSL_MD_NONE)
Steffan Karger28d81a02013-11-13 16:57:58 +0100201 {
202 /*
203 * DigestInfo ::= SEQUENCE {
204 * digestAlgorithm DigestAlgorithmIdentifier,
205 * digest Digest }
206 *
207 * DigestAlgorithmIdentifier ::= AlgorithmIdentifier
208 *
209 * Digest ::= OCTET STRING
210 */
211 *p++ = ASN1_SEQUENCE | ASN1_CONSTRUCTED;
212 *p++ = (unsigned char) ( 0x08 + oid_size + hashlen );
213 *p++ = ASN1_SEQUENCE | ASN1_CONSTRUCTED;
214 *p++ = (unsigned char) ( 0x04 + oid_size );
215 *p++ = ASN1_OID;
216 *p++ = oid_size & 0xFF;
217 memcpy( p, oid, oid_size );
218 p += oid_size;
219 *p++ = ASN1_NULL;
220 *p++ = 0x00;
221 *p++ = ASN1_OCTET_STRING;
222 *p++ = hashlen;
Paul Bakker43b7e352011-01-18 15:27:19 +0000223 }
224
Paul Bakkerdb1f0592014-03-26 14:53:47 +0100225 memcpy( p, hash, hashlen );
226
Paul Bakker43b7e352011-01-18 15:27:19 +0000227 if( pkcs11h_certificate_signAny( ctx->pkcs11h_cert, CKM_RSA_PKCS, sig,
Paul Bakkerdb1f0592014-03-26 14:53:47 +0100228 asn_len + hashlen, sig, &sig_len ) != CKR_OK )
Paul Bakker43b7e352011-01-18 15:27:19 +0000229 {
230 return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
231 }
232
233 return( 0 );
234}
235
236#endif /* defined(POLARSSL_PKCS11_C) */