4 * \brief Wrapper for PKCS#11 library libpkcs11-helper
6 * \author Adriaan de Jong <dejong@fox-it.com>
8 * Copyright (C) 2006-2010, Brainspark B.V.
10 * This file is part of PolarSSL (http://www.polarssl.org)
11 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
13 * All rights reserved.
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.
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.
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.
32 #if defined(POLARSSL_PKCS11_C)
36 int pkcs11_x509_cert_init( x509_cert
*cert
, pkcs11h_certificate_t pkcs11_cert
)
39 unsigned char *cert_blob
= NULL
;
40 size_t cert_blob_size
= 0;
48 if( pkcs11h_certificate_getCertificateBlob( pkcs11_cert
, NULL
, &cert_blob_size
) != CKR_OK
)
54 cert_blob
= malloc( cert_blob_size
);
55 if( NULL
== cert_blob
)
61 if( pkcs11h_certificate_getCertificateBlob( pkcs11_cert
, cert_blob
, &cert_blob_size
) != CKR_OK
)
67 if( 0 != x509parse_crt(cert
, cert_blob
, cert_blob_size
) )
76 if( NULL
!= cert_blob
)
83 int pkcs11_priv_key_init( pkcs11_context
*priv_key
,
84 pkcs11h_certificate_t pkcs11_cert
)
89 memset( &cert
, 0, sizeof( cert
) );
91 if( priv_key
== NULL
)
94 if( 0 != pkcs11_x509_cert_init( &cert
, pkcs11_cert
) )
97 priv_key
->len
= cert
.rsa
.len
;
98 priv_key
->pkcs11h_cert
= pkcs11_cert
;
108 void pkcs11_priv_key_free( pkcs11_context
*priv_key
)
110 if( NULL
!= priv_key
)
111 pkcs11h_certificate_freeCertificate( priv_key
->pkcs11h_cert
);
114 int pkcs11_decrypt( pkcs11_context
*ctx
,
115 int mode
, size_t *olen
,
116 const unsigned char *input
,
117 unsigned char *output
,
118 size_t output_max_len
)
120 size_t input_len
, output_len
;
123 return( POLARSSL_ERR_RSA_BAD_INPUT_DATA
);
125 if( RSA_PUBLIC
== mode
)
126 return( POLARSSL_ERR_RSA_BAD_INPUT_DATA
);
128 output_len
= input_len
= ctx
->len
;
130 if( input_len
< 16 || input_len
> output_max_len
)
131 return( POLARSSL_ERR_RSA_BAD_INPUT_DATA
);
133 /* Determine size of output buffer */
134 if( pkcs11h_certificate_decryptAny( ctx
->pkcs11h_cert
, CKM_RSA_PKCS
, input
,
135 input_len
, NULL
, &output_len
) != CKR_OK
)
137 return( POLARSSL_ERR_RSA_BAD_INPUT_DATA
);
140 if( output_len
> output_max_len
)
141 return( POLARSSL_ERR_RSA_OUTPUT_TOO_LARGE
);
143 if( pkcs11h_certificate_decryptAny( ctx
->pkcs11h_cert
, CKM_RSA_PKCS
, input
,
144 input_len
, output
, &output_len
) != CKR_OK
)
146 return( POLARSSL_ERR_RSA_BAD_INPUT_DATA
);
152 int pkcs11_sign( pkcs11_context
*ctx
,
155 unsigned int hashlen
,
156 const unsigned char *hash
,
159 size_t olen
, asn_len
;
160 unsigned char *p
= sig
;
163 return POLARSSL_ERR_RSA_BAD_INPUT_DATA
;
165 if( RSA_PUBLIC
== mode
)
166 return POLARSSL_ERR_RSA_BAD_INPUT_DATA
;
174 memcpy( p
, hash
, hashlen
);
178 asn_len
= OID_SIZE(ASN1_HASH_MDX
);
179 memcpy( p
, ASN1_HASH_MDX
, asn_len
);
180 memcpy( p
+ asn_len
, hash
, hashlen
);
184 asn_len
= OID_SIZE(ASN1_HASH_MDX
);
185 memcpy( p
, ASN1_HASH_MDX
, asn_len
);
186 memcpy( p
+ asn_len
, hash
, hashlen
);
190 asn_len
= OID_SIZE(ASN1_HASH_MDX
);
191 memcpy( p
, ASN1_HASH_MDX
, asn_len
);
192 memcpy( p
+ asn_len
, hash
, hashlen
);
196 asn_len
= OID_SIZE(ASN1_HASH_SHA1
);
197 memcpy( p
, ASN1_HASH_SHA1
, asn_len
);
198 memcpy( p
+ 15, hash
, hashlen
);
202 asn_len
= OID_SIZE(ASN1_HASH_SHA2X
);
203 memcpy( p
, ASN1_HASH_SHA2X
, asn_len
);
204 memcpy( p
+ asn_len
, hash
, hashlen
);
205 p
[1] += hashlen
; p
[14] = 4; p
[18] += hashlen
; break;
208 asn_len
= OID_SIZE(ASN1_HASH_SHA2X
);
209 memcpy( p
, ASN1_HASH_SHA2X
, asn_len
);
210 memcpy( p
+ asn_len
, hash
, hashlen
);
211 p
[1] += hashlen
; p
[14] = 1; p
[18] += hashlen
; break;
214 asn_len
= OID_SIZE(ASN1_HASH_SHA2X
);
215 memcpy( p
, ASN1_HASH_SHA2X
, asn_len
);
216 memcpy( p
+ asn_len
, hash
, hashlen
);
217 p
[1] += hashlen
; p
[14] = 2; p
[18] += hashlen
; break;
220 asn_len
= OID_SIZE(ASN1_HASH_SHA2X
);
221 memcpy( p
, ASN1_HASH_SHA2X
, asn_len
);
222 memcpy( p
+ asn_len
, hash
, hashlen
);
223 p
[1] += hashlen
; p
[14] = 3; p
[18] += hashlen
; break;
226 return( POLARSSL_ERR_RSA_BAD_INPUT_DATA
);
229 if( pkcs11h_certificate_signAny( ctx
->pkcs11h_cert
, CKM_RSA_PKCS
, sig
,
230 asn_len
+ hashlen
, sig
, &olen
) != CKR_OK
)
232 return( POLARSSL_ERR_RSA_BAD_INPUT_DATA
);
238 #endif /* defined(POLARSSL_PKCS11_C) */