blob: 4deccf3f607183b603796de6880c8aed8b2e95f6 [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 *
Bence Szépkúti1e148272020-08-07 13:07:28 +02008 * Copyright The Mbed TLS Contributors
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02009 * SPDX-License-Identifier: Apache-2.0
10 *
11 * Licensed under the Apache License, Version 2.0 (the "License"); you may
12 * not use this file except in compliance with the License.
13 * You may obtain a copy of the License at
14 *
15 * http://www.apache.org/licenses/LICENSE-2.0
16 *
17 * Unless required by applicable law or agreed to in writing, software
18 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
19 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20 * See the License for the specific language governing permissions and
21 * limitations under the License.
Paul Bakker43b7e352011-01-18 15:27:19 +000022 */
23
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000024#include "mbedtls/pkcs11.h"
Paul Bakker43b7e352011-01-18 15:27:19 +000025
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020026#if defined(MBEDTLS_PKCS11_C)
Rich Evans00ab4702015-02-06 13:43:58 +000027
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000028#include "mbedtls/md.h"
29#include "mbedtls/oid.h"
30#include "mbedtls/x509_crt.h"
Paul Bakker43b7e352011-01-18 15:27:19 +000031
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020032#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000033#include "mbedtls/platform.h"
Paul Bakker6e339b52013-07-03 13:37:05 +020034#else
Paul Bakker7dc4c442014-02-01 22:50:26 +010035#include <stdlib.h>
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +020036#define mbedtls_calloc calloc
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020037#define mbedtls_free free
Paul Bakker6e339b52013-07-03 13:37:05 +020038#endif
39
Manuel Pégourié-Gonnard3a895592015-05-27 17:09:21 +020040#include <string.h>
41
Manuel Pégourié-Gonnardeab147c2015-04-29 01:10:10 +020042void mbedtls_pkcs11_init( mbedtls_pkcs11_context *ctx )
43{
44 memset( ctx, 0, sizeof( mbedtls_pkcs11_context ) );
45}
46
47int mbedtls_pkcs11_x509_cert_bind( mbedtls_x509_crt *cert, pkcs11h_certificate_t pkcs11_cert )
Paul Bakker43b7e352011-01-18 15:27:19 +000048{
49 int ret = 1;
50 unsigned char *cert_blob = NULL;
51 size_t cert_blob_size = 0;
52
53 if( cert == NULL )
54 {
55 ret = 2;
56 goto cleanup;
57 }
58
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +020059 if( pkcs11h_certificate_getCertificateBlob( pkcs11_cert, NULL,
60 &cert_blob_size ) != CKR_OK )
Paul Bakker43b7e352011-01-18 15:27:19 +000061 {
62 ret = 3;
63 goto cleanup;
64 }
65
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +020066 cert_blob = mbedtls_calloc( 1, cert_blob_size );
Paul Bakker43b7e352011-01-18 15:27:19 +000067 if( NULL == cert_blob )
68 {
69 ret = 4;
70 goto cleanup;
71 }
72
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +020073 if( pkcs11h_certificate_getCertificateBlob( pkcs11_cert, cert_blob,
74 &cert_blob_size ) != CKR_OK )
Paul Bakker43b7e352011-01-18 15:27:19 +000075 {
76 ret = 5;
77 goto cleanup;
78 }
79
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020080 if( 0 != mbedtls_x509_crt_parse( cert, cert_blob, cert_blob_size ) )
Paul Bakker43b7e352011-01-18 15:27:19 +000081 {
82 ret = 6;
83 goto cleanup;
84 }
85
86 ret = 0;
87
88cleanup:
89 if( NULL != cert_blob )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020090 mbedtls_free( cert_blob );
Paul Bakker43b7e352011-01-18 15:27:19 +000091
Paul Bakkerd8bb8262014-06-17 14:06:49 +020092 return( ret );
Paul Bakker43b7e352011-01-18 15:27:19 +000093}
94
95
Manuel Pégourié-Gonnardeab147c2015-04-29 01:10:10 +020096int mbedtls_pkcs11_priv_key_bind( mbedtls_pkcs11_context *priv_key,
Paul Bakker43b7e352011-01-18 15:27:19 +000097 pkcs11h_certificate_t pkcs11_cert )
98{
99 int ret = 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200100 mbedtls_x509_crt cert;
Paul Bakker43b7e352011-01-18 15:27:19 +0000101
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200102 mbedtls_x509_crt_init( &cert );
Paul Bakker43b7e352011-01-18 15:27:19 +0000103
104 if( priv_key == NULL )
105 goto cleanup;
106
Manuel Pégourié-Gonnard3a895592015-05-27 17:09:21 +0200107 if( 0 != mbedtls_pkcs11_x509_cert_bind( &cert, pkcs11_cert ) )
Paul Bakker43b7e352011-01-18 15:27:19 +0000108 goto cleanup;
109
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200110 priv_key->len = mbedtls_pk_get_len( &cert.pk );
Paul Bakker43b7e352011-01-18 15:27:19 +0000111 priv_key->pkcs11h_cert = pkcs11_cert;
112
113 ret = 0;
114
115cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200116 mbedtls_x509_crt_free( &cert );
Paul Bakker43b7e352011-01-18 15:27:19 +0000117
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200118 return( ret );
Paul Bakker43b7e352011-01-18 15:27:19 +0000119}
120
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200121void mbedtls_pkcs11_priv_key_free( mbedtls_pkcs11_context *priv_key )
Paul Bakker43b7e352011-01-18 15:27:19 +0000122{
123 if( NULL != priv_key )
124 pkcs11h_certificate_freeCertificate( priv_key->pkcs11h_cert );
125}
126
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200127int mbedtls_pkcs11_decrypt( mbedtls_pkcs11_context *ctx,
Paul Bakker23986e52011-04-24 08:57:21 +0000128 int mode, size_t *olen,
Paul Bakker43b7e352011-01-18 15:27:19 +0000129 const unsigned char *input,
130 unsigned char *output,
Paul Bakker9a736322012-11-14 12:39:52 +0000131 size_t output_max_len )
Paul Bakker43b7e352011-01-18 15:27:19 +0000132{
133 size_t input_len, output_len;
134
135 if( NULL == ctx )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200136 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker43b7e352011-01-18 15:27:19 +0000137
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200138 if( MBEDTLS_RSA_PRIVATE != mode )
139 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker43b7e352011-01-18 15:27:19 +0000140
141 output_len = input_len = ctx->len;
142
143 if( input_len < 16 || input_len > output_max_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200144 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker43b7e352011-01-18 15:27:19 +0000145
146 /* Determine size of output buffer */
147 if( pkcs11h_certificate_decryptAny( ctx->pkcs11h_cert, CKM_RSA_PKCS, input,
148 input_len, NULL, &output_len ) != CKR_OK )
149 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200150 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker43b7e352011-01-18 15:27:19 +0000151 }
152
153 if( output_len > output_max_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200154 return( MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE );
Paul Bakker43b7e352011-01-18 15:27:19 +0000155
156 if( pkcs11h_certificate_decryptAny( ctx->pkcs11h_cert, CKM_RSA_PKCS, input,
157 input_len, output, &output_len ) != CKR_OK )
158 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200159 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker43b7e352011-01-18 15:27:19 +0000160 }
161 *olen = output_len;
162 return( 0 );
163}
164
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200165int mbedtls_pkcs11_sign( mbedtls_pkcs11_context *ctx,
Paul Bakker43b7e352011-01-18 15:27:19 +0000166 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200167 mbedtls_md_type_t md_alg,
Paul Bakker23986e52011-04-24 08:57:21 +0000168 unsigned int hashlen,
Paul Bakker43b7e352011-01-18 15:27:19 +0000169 const unsigned char *hash,
170 unsigned char *sig )
171{
Paul Bakkerdb1f0592014-03-26 14:53:47 +0100172 size_t sig_len = 0, asn_len = 0, oid_size = 0;
Paul Bakker43b7e352011-01-18 15:27:19 +0000173 unsigned char *p = sig;
Steffan Karger28d81a02013-11-13 16:57:58 +0100174 const char *oid;
Paul Bakker43b7e352011-01-18 15:27:19 +0000175
176 if( NULL == ctx )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200177 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker43b7e352011-01-18 15:27:19 +0000178
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200179 if( MBEDTLS_RSA_PRIVATE != mode )
180 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker43b7e352011-01-18 15:27:19 +0000181
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200182 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakker43b7e352011-01-18 15:27:19 +0000183 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200184 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
Steffan Karger28d81a02013-11-13 16:57:58 +0100185 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200186 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Steffan Karger28d81a02013-11-13 16:57:58 +0100187
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200188 if( mbedtls_oid_get_oid_by_md( md_alg, &oid, &oid_size ) != 0 )
189 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Steffan Karger28d81a02013-11-13 16:57:58 +0100190
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200191 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerdb1f0592014-03-26 14:53:47 +0100192 asn_len = 10 + oid_size;
Steffan Karger28d81a02013-11-13 16:57:58 +0100193 }
194
Paul Bakkerdb1f0592014-03-26 14:53:47 +0100195 sig_len = ctx->len;
Paul Bakker66d5d072014-06-17 16:39:18 +0200196 if( hashlen > sig_len || asn_len > sig_len ||
197 hashlen + asn_len > sig_len )
Steffan Karger28d81a02013-11-13 16:57:58 +0100198 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200199 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Steffan Karger28d81a02013-11-13 16:57:58 +0100200 }
Paul Bakkerdb1f0592014-03-26 14:53:47 +0100201
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200202 if( md_alg != MBEDTLS_MD_NONE )
Steffan Karger28d81a02013-11-13 16:57:58 +0100203 {
204 /*
205 * DigestInfo ::= SEQUENCE {
206 * digestAlgorithm DigestAlgorithmIdentifier,
207 * digest Digest }
208 *
209 * DigestAlgorithmIdentifier ::= AlgorithmIdentifier
210 *
211 * Digest ::= OCTET STRING
212 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200213 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Steffan Karger28d81a02013-11-13 16:57:58 +0100214 *p++ = (unsigned char) ( 0x08 + oid_size + hashlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200215 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Steffan Karger28d81a02013-11-13 16:57:58 +0100216 *p++ = (unsigned char) ( 0x04 + oid_size );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200217 *p++ = MBEDTLS_ASN1_OID;
Steffan Karger28d81a02013-11-13 16:57:58 +0100218 *p++ = oid_size & 0xFF;
219 memcpy( p, oid, oid_size );
220 p += oid_size;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200221 *p++ = MBEDTLS_ASN1_NULL;
Steffan Karger28d81a02013-11-13 16:57:58 +0100222 *p++ = 0x00;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200223 *p++ = MBEDTLS_ASN1_OCTET_STRING;
Steffan Karger28d81a02013-11-13 16:57:58 +0100224 *p++ = hashlen;
Paul Bakker43b7e352011-01-18 15:27:19 +0000225 }
226
Paul Bakkerdb1f0592014-03-26 14:53:47 +0100227 memcpy( p, hash, hashlen );
228
Paul Bakker43b7e352011-01-18 15:27:19 +0000229 if( pkcs11h_certificate_signAny( ctx->pkcs11h_cert, CKM_RSA_PKCS, sig,
Paul Bakkerdb1f0592014-03-26 14:53:47 +0100230 asn_len + hashlen, sig, &sig_len ) != CKR_OK )
Paul Bakker43b7e352011-01-18 15:27:19 +0000231 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200232 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker43b7e352011-01-18 15:27:19 +0000233 }
234
235 return( 0 );
236}
237
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200238#endif /* defined(MBEDTLS_PKCS11_C) */