pkcs11.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. /**
  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. #include "polarssl/pkcs11.h"
  30. #if defined(POLARSSL_PKCS11_C)
  31. #include <stdlib.h>
  32. int pkcs11_x509_cert_init( x509_cert *cert, pkcs11h_certificate_t pkcs11_cert )
  33. {
  34. int ret = 1;
  35. unsigned char *cert_blob = NULL;
  36. size_t cert_blob_size = 0;
  37. if( cert == NULL )
  38. {
  39. ret = 2;
  40. goto cleanup;
  41. }
  42. if( pkcs11h_certificate_getCertificateBlob( pkcs11_cert, NULL, &cert_blob_size ) != CKR_OK )
  43. {
  44. ret = 3;
  45. goto cleanup;
  46. }
  47. cert_blob = malloc( cert_blob_size );
  48. if( NULL == cert_blob )
  49. {
  50. ret = 4;
  51. goto cleanup;
  52. }
  53. if( pkcs11h_certificate_getCertificateBlob( pkcs11_cert, cert_blob, &cert_blob_size ) != CKR_OK )
  54. {
  55. ret = 5;
  56. goto cleanup;
  57. }
  58. if( 0 != x509parse_crt(cert, cert_blob, cert_blob_size ) )
  59. {
  60. ret = 6;
  61. goto cleanup;
  62. }
  63. ret = 0;
  64. cleanup:
  65. if( NULL != cert_blob )
  66. free( cert_blob );
  67. return ret;
  68. }
  69. int pkcs11_priv_key_init( pkcs11_context *priv_key,
  70. pkcs11h_certificate_t pkcs11_cert )
  71. {
  72. int ret = 1;
  73. x509_cert cert;
  74. memset( &cert, 0, sizeof( cert ) );
  75. if( priv_key == NULL )
  76. goto cleanup;
  77. if( 0 != pkcs11_x509_cert_init( &cert, pkcs11_cert ) )
  78. goto cleanup;
  79. priv_key->len = cert.rsa.len;
  80. priv_key->pkcs11h_cert = pkcs11_cert;
  81. ret = 0;
  82. cleanup:
  83. x509_free( &cert );
  84. return ret;
  85. }
  86. void pkcs11_priv_key_free( pkcs11_context *priv_key )
  87. {
  88. if( NULL != priv_key )
  89. pkcs11h_certificate_freeCertificate( priv_key->pkcs11h_cert );
  90. }
  91. int pkcs11_decrypt( pkcs11_context *ctx,
  92. int mode, size_t *olen,
  93. const unsigned char *input,
  94. unsigned char *output,
  95. unsigned int output_max_len )
  96. {
  97. size_t input_len, output_len;
  98. if( NULL == ctx )
  99. return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
  100. if( RSA_PUBLIC == mode )
  101. return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
  102. output_len = input_len = ctx->len;
  103. if( input_len < 16 || input_len > output_max_len )
  104. return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
  105. /* Determine size of output buffer */
  106. if( pkcs11h_certificate_decryptAny( ctx->pkcs11h_cert, CKM_RSA_PKCS, input,
  107. input_len, NULL, &output_len ) != CKR_OK )
  108. {
  109. return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
  110. }
  111. if( output_len > output_max_len )
  112. return( POLARSSL_ERR_RSA_OUTPUT_TOO_LARGE );
  113. if( pkcs11h_certificate_decryptAny( ctx->pkcs11h_cert, CKM_RSA_PKCS, input,
  114. input_len, output, &output_len ) != CKR_OK )
  115. {
  116. return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
  117. }
  118. *olen = output_len;
  119. return( 0 );
  120. }
  121. int pkcs11_sign( pkcs11_context *ctx,
  122. int mode,
  123. int hash_id,
  124. unsigned int hashlen,
  125. const unsigned char *hash,
  126. unsigned char *sig )
  127. {
  128. size_t olen, asn_len;
  129. unsigned char *p = sig;
  130. if( NULL == ctx )
  131. return POLARSSL_ERR_RSA_BAD_INPUT_DATA;
  132. if( RSA_PUBLIC == mode )
  133. return POLARSSL_ERR_RSA_BAD_INPUT_DATA;
  134. olen = ctx->len;
  135. switch( hash_id )
  136. {
  137. case SIG_RSA_RAW:
  138. asn_len = 0;
  139. memcpy( p, hash, hashlen );
  140. break;
  141. case SIG_RSA_MD2:
  142. asn_len = OID_SIZE(ASN1_HASH_MDX);
  143. memcpy( p, ASN1_HASH_MDX, asn_len );
  144. memcpy( p + asn_len, hash, hashlen );
  145. p[13] = 2; break;
  146. case SIG_RSA_MD4:
  147. asn_len = OID_SIZE(ASN1_HASH_MDX);
  148. memcpy( p, ASN1_HASH_MDX, asn_len );
  149. memcpy( p + asn_len, hash, hashlen );
  150. p[13] = 4; break;
  151. case SIG_RSA_MD5:
  152. asn_len = OID_SIZE(ASN1_HASH_MDX);
  153. memcpy( p, ASN1_HASH_MDX, asn_len );
  154. memcpy( p + asn_len, hash, hashlen );
  155. p[13] = 5; break;
  156. case SIG_RSA_SHA1:
  157. asn_len = OID_SIZE(ASN1_HASH_SHA1);
  158. memcpy( p, ASN1_HASH_SHA1, asn_len );
  159. memcpy( p + 15, hash, hashlen );
  160. break;
  161. case SIG_RSA_SHA224:
  162. asn_len = OID_SIZE(ASN1_HASH_SHA2X);
  163. memcpy( p, ASN1_HASH_SHA2X, asn_len );
  164. memcpy( p + asn_len, hash, hashlen );
  165. p[1] += hashlen; p[14] = 4; p[18] += hashlen; break;
  166. case SIG_RSA_SHA256:
  167. asn_len = OID_SIZE(ASN1_HASH_SHA2X);
  168. memcpy( p, ASN1_HASH_SHA2X, asn_len );
  169. memcpy( p + asn_len, hash, hashlen );
  170. p[1] += hashlen; p[14] = 1; p[18] += hashlen; break;
  171. case SIG_RSA_SHA384:
  172. asn_len = OID_SIZE(ASN1_HASH_SHA2X);
  173. memcpy( p, ASN1_HASH_SHA2X, asn_len );
  174. memcpy( p + asn_len, hash, hashlen );
  175. p[1] += hashlen; p[14] = 2; p[18] += hashlen; break;
  176. case SIG_RSA_SHA512:
  177. asn_len = OID_SIZE(ASN1_HASH_SHA2X);
  178. memcpy( p, ASN1_HASH_SHA2X, asn_len );
  179. memcpy( p + asn_len, hash, hashlen );
  180. p[1] += hashlen; p[14] = 3; p[18] += hashlen; break;
  181. default:
  182. return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
  183. }
  184. if( pkcs11h_certificate_signAny( ctx->pkcs11h_cert, CKM_RSA_PKCS, sig,
  185. asn_len + hashlen, sig, &olen ) != CKR_OK )
  186. {
  187. return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
  188. }
  189. return( 0 );
  190. }
  191. #endif /* defined(POLARSSL_PKCS11_C) */