blob: b4f478763ec191eedfefdbd8b241545a87104934 [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 *
8 * Copyright (C) 2006-2010, Brainspark B.V.
9 *
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)
33
34#include <stdlib.h>
35#include <string.h>
36
37int pkcs11_x509_cert_init( x509_cert *cert, pkcs11h_certificate_t pkcs11_cert )
38{
39 int ret = 1;
40 unsigned char *cert_blob = NULL;
41 size_t cert_blob_size = 0;
42
43 if( cert == NULL )
44 {
45 ret = 2;
46 goto cleanup;
47 }
48
49 if( pkcs11h_certificate_getCertificateBlob( pkcs11_cert, NULL, &cert_blob_size ) != CKR_OK )
50 {
51 ret = 3;
52 goto cleanup;
53 }
54
55 cert_blob = malloc( cert_blob_size );
56 if( NULL == cert_blob )
57 {
58 ret = 4;
59 goto cleanup;
60 }
61
62 if( pkcs11h_certificate_getCertificateBlob( pkcs11_cert, cert_blob, &cert_blob_size ) != CKR_OK )
63 {
64 ret = 5;
65 goto cleanup;
66 }
67
68 if( 0 != x509parse_crt(cert, cert_blob, cert_blob_size ) )
69 {
70 ret = 6;
71 goto cleanup;
72 }
73
74 ret = 0;
75
76cleanup:
77 if( NULL != cert_blob )
78 free( cert_blob );
79
80 return ret;
81}
82
83
84int pkcs11_priv_key_init( pkcs11_context *priv_key,
85 pkcs11h_certificate_t pkcs11_cert )
86{
87 int ret = 1;
88 x509_cert cert;
89
90 memset( &cert, 0, sizeof( cert ) );
91
92 if( priv_key == NULL )
93 goto cleanup;
94
95 if( 0 != pkcs11_x509_cert_init( &cert, pkcs11_cert ) )
96 goto cleanup;
97
98 priv_key->len = cert.rsa.len;
99 priv_key->pkcs11h_cert = pkcs11_cert;
100
101 ret = 0;
102
103cleanup:
104 x509_free( &cert );
105
106 return ret;
107}
108
109void pkcs11_priv_key_free( pkcs11_context *priv_key )
110{
111 if( NULL != priv_key )
112 pkcs11h_certificate_freeCertificate( priv_key->pkcs11h_cert );
113}
114
115int pkcs11_decrypt( pkcs11_context *ctx,
116 int mode, int *olen,
117 const unsigned char *input,
118 unsigned char *output,
Paul Bakkerd61e7d92011-01-18 16:17:47 +0000119 unsigned int output_max_len )
Paul Bakker43b7e352011-01-18 15:27:19 +0000120{
121 size_t input_len, output_len;
122
123 if( NULL == ctx )
124 return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
125
126 if( RSA_PUBLIC == mode )
127 return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
128
129 output_len = input_len = ctx->len;
130
131 if( input_len < 16 || input_len > output_max_len )
132 return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
133
134 /* Determine size of output buffer */
135 if( pkcs11h_certificate_decryptAny( ctx->pkcs11h_cert, CKM_RSA_PKCS, input,
136 input_len, NULL, &output_len ) != CKR_OK )
137 {
138 return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
139 }
140
141 if( output_len > output_max_len )
142 return( POLARSSL_ERR_RSA_OUTPUT_TOO_LARGE );
143
144 if( pkcs11h_certificate_decryptAny( ctx->pkcs11h_cert, CKM_RSA_PKCS, input,
145 input_len, output, &output_len ) != CKR_OK )
146 {
147 return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
148 }
149 *olen = output_len;
150 return( 0 );
151}
152
153int pkcs11_sign( pkcs11_context *ctx,
154 int mode,
155 int hash_id,
156 int hashlen,
157 const unsigned char *hash,
158 unsigned char *sig )
159{
160 size_t olen, asn_len;
161 unsigned char *p = sig;
162
163 if( NULL == ctx )
164 return POLARSSL_ERR_RSA_BAD_INPUT_DATA;
165
166 if( RSA_PUBLIC == mode )
167 return POLARSSL_ERR_RSA_BAD_INPUT_DATA;
168
169 olen = ctx->len;
170
171 switch( hash_id )
172 {
173 case SIG_RSA_RAW:
174 asn_len = 0;
175 memcpy( p, hash, hashlen );
176 break;
177
178 case SIG_RSA_MD2:
179 asn_len = OID_SIZE(ASN1_HASH_MDX);
180 memcpy( p, ASN1_HASH_MDX, asn_len );
181 memcpy( p + asn_len, hash, hashlen );
182 p[13] = 2; break;
183
184 case SIG_RSA_MD4:
185 asn_len = OID_SIZE(ASN1_HASH_MDX);
186 memcpy( p, ASN1_HASH_MDX, asn_len );
187 memcpy( p + asn_len, hash, hashlen );
188 p[13] = 4; break;
189
190 case SIG_RSA_MD5:
191 asn_len = OID_SIZE(ASN1_HASH_MDX);
192 memcpy( p, ASN1_HASH_MDX, asn_len );
193 memcpy( p + asn_len, hash, hashlen );
194 p[13] = 5; break;
195
196 case SIG_RSA_SHA1:
197 asn_len = OID_SIZE(ASN1_HASH_SHA1);
198 memcpy( p, ASN1_HASH_SHA1, asn_len );
199 memcpy( p + 15, hash, hashlen );
200 break;
201
202 case SIG_RSA_SHA224:
203 asn_len = OID_SIZE(ASN1_HASH_SHA2X);
204 memcpy( p, ASN1_HASH_SHA2X, asn_len );
205 memcpy( p + asn_len, hash, hashlen );
206 p[1] += hashlen; p[14] = 4; p[18] += hashlen; break;
207
208 case SIG_RSA_SHA256:
209 asn_len = OID_SIZE(ASN1_HASH_SHA2X);
210 memcpy( p, ASN1_HASH_SHA2X, asn_len );
211 memcpy( p + asn_len, hash, hashlen );
212 p[1] += hashlen; p[14] = 1; p[18] += hashlen; break;
213
214 case SIG_RSA_SHA384:
215 asn_len = OID_SIZE(ASN1_HASH_SHA2X);
216 memcpy( p, ASN1_HASH_SHA2X, asn_len );
217 memcpy( p + asn_len, hash, hashlen );
218 p[1] += hashlen; p[14] = 2; p[18] += hashlen; break;
219
220 case SIG_RSA_SHA512:
221 asn_len = OID_SIZE(ASN1_HASH_SHA2X);
222 memcpy( p, ASN1_HASH_SHA2X, asn_len );
223 memcpy( p + asn_len, hash, hashlen );
224 p[1] += hashlen; p[14] = 3; p[18] += hashlen; break;
225
226 default:
227 return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
228 }
229
230 if( pkcs11h_certificate_signAny( ctx->pkcs11h_cert, CKM_RSA_PKCS, sig,
231 asn_len + hashlen, sig, &olen ) != CKR_OK )
232 {
233 return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
234 }
235
236 return( 0 );
237}
238
239#endif /* defined(POLARSSL_PKCS11_C) */