Paul Bakker | 43b7e35 | 2011-01-18 15:27:19 +0000 | [diff] [blame] | 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 | * |
Paul Bakker | 6e339b5 | 2013-07-03 13:37:05 +0200 | [diff] [blame] | 8 | * Copyright (C) 2006-2013, Brainspark B.V. |
Paul Bakker | 43b7e35 | 2011-01-18 15:27:19 +0000 | [diff] [blame] | 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) |
Steffan Karger | 28d81a0 | 2013-11-13 16:57:58 +0100 | [diff] [blame^] | 33 | #include "polarssl/md.h" |
| 34 | #include "polarssl/oid.h" |
| 35 | #include "polarssl/x509_crt.h" |
Paul Bakker | 43b7e35 | 2011-01-18 15:27:19 +0000 | [diff] [blame] | 36 | |
Paul Bakker | 6e339b5 | 2013-07-03 13:37:05 +0200 | [diff] [blame] | 37 | #if defined(POLARSSL_MEMORY_C) |
| 38 | #include "polarssl/memory.h" |
| 39 | #else |
| 40 | #define polarssl_malloc malloc |
| 41 | #define polarssl_free free |
| 42 | #endif |
| 43 | |
Paul Bakker | 43b7e35 | 2011-01-18 15:27:19 +0000 | [diff] [blame] | 44 | #include <stdlib.h> |
Paul Bakker | 43b7e35 | 2011-01-18 15:27:19 +0000 | [diff] [blame] | 45 | |
Paul Bakker | c559c7a | 2013-09-18 14:13:26 +0200 | [diff] [blame] | 46 | int pkcs11_x509_cert_init( x509_crt *cert, pkcs11h_certificate_t pkcs11_cert ) |
Paul Bakker | 43b7e35 | 2011-01-18 15:27:19 +0000 | [diff] [blame] | 47 | { |
| 48 | int ret = 1; |
| 49 | unsigned char *cert_blob = NULL; |
| 50 | size_t cert_blob_size = 0; |
| 51 | |
| 52 | if( cert == NULL ) |
| 53 | { |
| 54 | ret = 2; |
| 55 | goto cleanup; |
| 56 | } |
| 57 | |
| 58 | if( pkcs11h_certificate_getCertificateBlob( pkcs11_cert, NULL, &cert_blob_size ) != CKR_OK ) |
| 59 | { |
| 60 | ret = 3; |
| 61 | goto cleanup; |
| 62 | } |
| 63 | |
Paul Bakker | 6e339b5 | 2013-07-03 13:37:05 +0200 | [diff] [blame] | 64 | cert_blob = polarssl_malloc( cert_blob_size ); |
Paul Bakker | 43b7e35 | 2011-01-18 15:27:19 +0000 | [diff] [blame] | 65 | if( NULL == cert_blob ) |
| 66 | { |
| 67 | ret = 4; |
| 68 | goto cleanup; |
| 69 | } |
| 70 | |
| 71 | if( pkcs11h_certificate_getCertificateBlob( pkcs11_cert, cert_blob, &cert_blob_size ) != CKR_OK ) |
| 72 | { |
| 73 | ret = 5; |
| 74 | goto cleanup; |
| 75 | } |
| 76 | |
Paul Bakker | c559c7a | 2013-09-18 14:13:26 +0200 | [diff] [blame] | 77 | if( 0 != x509_crt_parse(cert, cert_blob, cert_blob_size ) ) |
Paul Bakker | 43b7e35 | 2011-01-18 15:27:19 +0000 | [diff] [blame] | 78 | { |
| 79 | ret = 6; |
| 80 | goto cleanup; |
| 81 | } |
| 82 | |
| 83 | ret = 0; |
| 84 | |
| 85 | cleanup: |
| 86 | if( NULL != cert_blob ) |
Paul Bakker | 6e339b5 | 2013-07-03 13:37:05 +0200 | [diff] [blame] | 87 | polarssl_free( cert_blob ); |
Paul Bakker | 43b7e35 | 2011-01-18 15:27:19 +0000 | [diff] [blame] | 88 | |
| 89 | return ret; |
| 90 | } |
| 91 | |
| 92 | |
| 93 | int pkcs11_priv_key_init( pkcs11_context *priv_key, |
| 94 | pkcs11h_certificate_t pkcs11_cert ) |
| 95 | { |
| 96 | int ret = 1; |
Paul Bakker | c559c7a | 2013-09-18 14:13:26 +0200 | [diff] [blame] | 97 | x509_crt cert; |
Paul Bakker | 43b7e35 | 2011-01-18 15:27:19 +0000 | [diff] [blame] | 98 | |
Paul Bakker | c559c7a | 2013-09-18 14:13:26 +0200 | [diff] [blame] | 99 | x509_crt_init( &cert ); |
Paul Bakker | 43b7e35 | 2011-01-18 15:27:19 +0000 | [diff] [blame] | 100 | |
| 101 | if( priv_key == NULL ) |
| 102 | goto cleanup; |
| 103 | |
| 104 | if( 0 != pkcs11_x509_cert_init( &cert, pkcs11_cert ) ) |
| 105 | goto cleanup; |
| 106 | |
Steffan Karger | 28d81a0 | 2013-11-13 16:57:58 +0100 | [diff] [blame^] | 107 | priv_key->len = pk_get_len(&cert.pk); |
Paul Bakker | 43b7e35 | 2011-01-18 15:27:19 +0000 | [diff] [blame] | 108 | priv_key->pkcs11h_cert = pkcs11_cert; |
| 109 | |
| 110 | ret = 0; |
| 111 | |
| 112 | cleanup: |
Paul Bakker | c559c7a | 2013-09-18 14:13:26 +0200 | [diff] [blame] | 113 | x509_crt_free( &cert ); |
Paul Bakker | 43b7e35 | 2011-01-18 15:27:19 +0000 | [diff] [blame] | 114 | |
| 115 | return ret; |
| 116 | } |
| 117 | |
| 118 | void pkcs11_priv_key_free( pkcs11_context *priv_key ) |
| 119 | { |
| 120 | if( NULL != priv_key ) |
| 121 | pkcs11h_certificate_freeCertificate( priv_key->pkcs11h_cert ); |
| 122 | } |
| 123 | |
| 124 | int pkcs11_decrypt( pkcs11_context *ctx, |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 125 | int mode, size_t *olen, |
Paul Bakker | 43b7e35 | 2011-01-18 15:27:19 +0000 | [diff] [blame] | 126 | const unsigned char *input, |
| 127 | unsigned char *output, |
Paul Bakker | 9a73632 | 2012-11-14 12:39:52 +0000 | [diff] [blame] | 128 | size_t output_max_len ) |
Paul Bakker | 43b7e35 | 2011-01-18 15:27:19 +0000 | [diff] [blame] | 129 | { |
| 130 | size_t input_len, output_len; |
| 131 | |
| 132 | if( NULL == ctx ) |
| 133 | return( POLARSSL_ERR_RSA_BAD_INPUT_DATA ); |
| 134 | |
Steffan Karger | 28d81a0 | 2013-11-13 16:57:58 +0100 | [diff] [blame^] | 135 | if( RSA_PRIVATE != mode ) |
Paul Bakker | 43b7e35 | 2011-01-18 15:27:19 +0000 | [diff] [blame] | 136 | return( POLARSSL_ERR_RSA_BAD_INPUT_DATA ); |
| 137 | |
| 138 | output_len = input_len = ctx->len; |
| 139 | |
| 140 | if( input_len < 16 || input_len > output_max_len ) |
| 141 | return( POLARSSL_ERR_RSA_BAD_INPUT_DATA ); |
| 142 | |
| 143 | /* Determine size of output buffer */ |
| 144 | if( pkcs11h_certificate_decryptAny( ctx->pkcs11h_cert, CKM_RSA_PKCS, input, |
| 145 | input_len, NULL, &output_len ) != CKR_OK ) |
| 146 | { |
| 147 | return( POLARSSL_ERR_RSA_BAD_INPUT_DATA ); |
| 148 | } |
| 149 | |
| 150 | if( output_len > output_max_len ) |
| 151 | return( POLARSSL_ERR_RSA_OUTPUT_TOO_LARGE ); |
| 152 | |
| 153 | if( pkcs11h_certificate_decryptAny( ctx->pkcs11h_cert, CKM_RSA_PKCS, input, |
| 154 | input_len, output, &output_len ) != CKR_OK ) |
| 155 | { |
| 156 | return( POLARSSL_ERR_RSA_BAD_INPUT_DATA ); |
| 157 | } |
| 158 | *olen = output_len; |
| 159 | return( 0 ); |
| 160 | } |
| 161 | |
| 162 | int pkcs11_sign( pkcs11_context *ctx, |
| 163 | int mode, |
Steffan Karger | 28d81a0 | 2013-11-13 16:57:58 +0100 | [diff] [blame^] | 164 | md_type_t md_alg, |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 165 | unsigned int hashlen, |
Paul Bakker | 43b7e35 | 2011-01-18 15:27:19 +0000 | [diff] [blame] | 166 | const unsigned char *hash, |
| 167 | unsigned char *sig ) |
| 168 | { |
Steffan Karger | 28d81a0 | 2013-11-13 16:57:58 +0100 | [diff] [blame^] | 169 | size_t olen, asn_len = 0, oid_size = 0; |
Paul Bakker | 43b7e35 | 2011-01-18 15:27:19 +0000 | [diff] [blame] | 170 | unsigned char *p = sig; |
Steffan Karger | 28d81a0 | 2013-11-13 16:57:58 +0100 | [diff] [blame^] | 171 | const char *oid; |
Paul Bakker | 43b7e35 | 2011-01-18 15:27:19 +0000 | [diff] [blame] | 172 | |
| 173 | if( NULL == ctx ) |
| 174 | return POLARSSL_ERR_RSA_BAD_INPUT_DATA; |
| 175 | |
Steffan Karger | 28d81a0 | 2013-11-13 16:57:58 +0100 | [diff] [blame^] | 176 | if( RSA_PRIVATE != mode ) |
Paul Bakker | 43b7e35 | 2011-01-18 15:27:19 +0000 | [diff] [blame] | 177 | return POLARSSL_ERR_RSA_BAD_INPUT_DATA; |
| 178 | |
| 179 | olen = ctx->len; |
| 180 | |
Steffan Karger | 28d81a0 | 2013-11-13 16:57:58 +0100 | [diff] [blame^] | 181 | if( md_alg != POLARSSL_MD_NONE ) |
Paul Bakker | 43b7e35 | 2011-01-18 15:27:19 +0000 | [diff] [blame] | 182 | { |
Steffan Karger | 28d81a0 | 2013-11-13 16:57:58 +0100 | [diff] [blame^] | 183 | const md_info_t *md_info = md_info_from_type( md_alg ); |
| 184 | if( md_info == NULL ) |
Paul Bakker | 43b7e35 | 2011-01-18 15:27:19 +0000 | [diff] [blame] | 185 | return( POLARSSL_ERR_RSA_BAD_INPUT_DATA ); |
Steffan Karger | 28d81a0 | 2013-11-13 16:57:58 +0100 | [diff] [blame^] | 186 | |
| 187 | if( oid_get_oid_by_md( md_alg, &oid, &oid_size ) != 0 ) |
| 188 | return( POLARSSL_ERR_RSA_BAD_INPUT_DATA ); |
| 189 | |
| 190 | hashlen = md_get_size( md_info ); |
| 191 | } |
| 192 | |
| 193 | if( md_alg == POLARSSL_MD_NONE ) |
| 194 | { |
| 195 | memcpy( p, hash, hashlen ); |
| 196 | } |
| 197 | else |
| 198 | { |
| 199 | /* |
| 200 | * DigestInfo ::= SEQUENCE { |
| 201 | * digestAlgorithm DigestAlgorithmIdentifier, |
| 202 | * digest Digest } |
| 203 | * |
| 204 | * DigestAlgorithmIdentifier ::= AlgorithmIdentifier |
| 205 | * |
| 206 | * Digest ::= OCTET STRING |
| 207 | */ |
| 208 | *p++ = ASN1_SEQUENCE | ASN1_CONSTRUCTED; |
| 209 | *p++ = (unsigned char) ( 0x08 + oid_size + hashlen ); |
| 210 | *p++ = ASN1_SEQUENCE | ASN1_CONSTRUCTED; |
| 211 | *p++ = (unsigned char) ( 0x04 + oid_size ); |
| 212 | *p++ = ASN1_OID; |
| 213 | *p++ = oid_size & 0xFF; |
| 214 | memcpy( p, oid, oid_size ); |
| 215 | p += oid_size; |
| 216 | *p++ = ASN1_NULL; |
| 217 | *p++ = 0x00; |
| 218 | *p++ = ASN1_OCTET_STRING; |
| 219 | *p++ = hashlen; |
| 220 | |
| 221 | /* Determine added ASN length */ |
| 222 | asn_len = p - sig; |
| 223 | |
| 224 | memcpy( p, hash, hashlen ); |
Paul Bakker | 43b7e35 | 2011-01-18 15:27:19 +0000 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | if( pkcs11h_certificate_signAny( ctx->pkcs11h_cert, CKM_RSA_PKCS, sig, |
| 228 | asn_len + hashlen, sig, &olen ) != CKR_OK ) |
| 229 | { |
| 230 | return( POLARSSL_ERR_RSA_BAD_INPUT_DATA ); |
| 231 | } |
| 232 | |
| 233 | return( 0 ); |
| 234 | } |
| 235 | |
| 236 | #endif /* defined(POLARSSL_PKCS11_C) */ |