blob: cf484b86eb49c4acbd34f532f2c1111a014939be [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útia2947ac2020-08-19 16:37:36 +02008 * Copyright The Mbed TLS Contributors
Bence Szépkútif744bd72020-06-05 13:02:18 +02009 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
10 *
11 * This file is provided under the Apache License 2.0, or the
12 * GNU General Public License v2.0 or later.
13 *
14 * **********
15 * Apache License 2.0:
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +020016 *
17 * Licensed under the Apache License, Version 2.0 (the "License"); you may
18 * not use this file except in compliance with the License.
19 * You may obtain a copy of the License at
20 *
21 * http://www.apache.org/licenses/LICENSE-2.0
22 *
23 * Unless required by applicable law or agreed to in writing, software
24 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
25 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
26 * See the License for the specific language governing permissions and
27 * limitations under the License.
Paul Bakker43b7e352011-01-18 15:27:19 +000028 *
Bence Szépkútif744bd72020-06-05 13:02:18 +020029 * **********
30 *
31 * **********
32 * GNU General Public License v2.0 or later:
33 *
34 * This program is free software; you can redistribute it and/or modify
35 * it under the terms of the GNU General Public License as published by
36 * the Free Software Foundation; either version 2 of the License, or
37 * (at your option) any later version.
38 *
39 * This program is distributed in the hope that it will be useful,
40 * but WITHOUT ANY WARRANTY; without even the implied warranty of
41 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
42 * GNU General Public License for more details.
43 *
44 * You should have received a copy of the GNU General Public License along
45 * with this program; if not, write to the Free Software Foundation, Inc.,
46 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
47 *
48 * **********
Paul Bakker43b7e352011-01-18 15:27:19 +000049 */
50
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000051#include "mbedtls/pkcs11.h"
Paul Bakker43b7e352011-01-18 15:27:19 +000052
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020053#if defined(MBEDTLS_PKCS11_C)
Rich Evans00ab4702015-02-06 13:43:58 +000054
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000055#include "mbedtls/md.h"
56#include "mbedtls/oid.h"
57#include "mbedtls/x509_crt.h"
Paul Bakker43b7e352011-01-18 15:27:19 +000058
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020059#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000060#include "mbedtls/platform.h"
Paul Bakker6e339b52013-07-03 13:37:05 +020061#else
Paul Bakker7dc4c442014-02-01 22:50:26 +010062#include <stdlib.h>
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +020063#define mbedtls_calloc calloc
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020064#define mbedtls_free free
Paul Bakker6e339b52013-07-03 13:37:05 +020065#endif
66
Manuel Pégourié-Gonnard3a895592015-05-27 17:09:21 +020067#include <string.h>
68
Manuel Pégourié-Gonnardeab147c2015-04-29 01:10:10 +020069void mbedtls_pkcs11_init( mbedtls_pkcs11_context *ctx )
70{
71 memset( ctx, 0, sizeof( mbedtls_pkcs11_context ) );
72}
73
74int mbedtls_pkcs11_x509_cert_bind( mbedtls_x509_crt *cert, pkcs11h_certificate_t pkcs11_cert )
Paul Bakker43b7e352011-01-18 15:27:19 +000075{
76 int ret = 1;
77 unsigned char *cert_blob = NULL;
78 size_t cert_blob_size = 0;
79
80 if( cert == NULL )
81 {
82 ret = 2;
83 goto cleanup;
84 }
85
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +020086 if( pkcs11h_certificate_getCertificateBlob( pkcs11_cert, NULL,
87 &cert_blob_size ) != CKR_OK )
Paul Bakker43b7e352011-01-18 15:27:19 +000088 {
89 ret = 3;
90 goto cleanup;
91 }
92
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +020093 cert_blob = mbedtls_calloc( 1, cert_blob_size );
Paul Bakker43b7e352011-01-18 15:27:19 +000094 if( NULL == cert_blob )
95 {
96 ret = 4;
97 goto cleanup;
98 }
99
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200100 if( pkcs11h_certificate_getCertificateBlob( pkcs11_cert, cert_blob,
101 &cert_blob_size ) != CKR_OK )
Paul Bakker43b7e352011-01-18 15:27:19 +0000102 {
103 ret = 5;
104 goto cleanup;
105 }
106
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200107 if( 0 != mbedtls_x509_crt_parse( cert, cert_blob, cert_blob_size ) )
Paul Bakker43b7e352011-01-18 15:27:19 +0000108 {
109 ret = 6;
110 goto cleanup;
111 }
112
113 ret = 0;
114
115cleanup:
116 if( NULL != cert_blob )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200117 mbedtls_free( cert_blob );
Paul Bakker43b7e352011-01-18 15:27:19 +0000118
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200119 return( ret );
Paul Bakker43b7e352011-01-18 15:27:19 +0000120}
121
122
Manuel Pégourié-Gonnardeab147c2015-04-29 01:10:10 +0200123int mbedtls_pkcs11_priv_key_bind( mbedtls_pkcs11_context *priv_key,
Paul Bakker43b7e352011-01-18 15:27:19 +0000124 pkcs11h_certificate_t pkcs11_cert )
125{
126 int ret = 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200127 mbedtls_x509_crt cert;
Paul Bakker43b7e352011-01-18 15:27:19 +0000128
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200129 mbedtls_x509_crt_init( &cert );
Paul Bakker43b7e352011-01-18 15:27:19 +0000130
131 if( priv_key == NULL )
132 goto cleanup;
133
Manuel Pégourié-Gonnard3a895592015-05-27 17:09:21 +0200134 if( 0 != mbedtls_pkcs11_x509_cert_bind( &cert, pkcs11_cert ) )
Paul Bakker43b7e352011-01-18 15:27:19 +0000135 goto cleanup;
136
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200137 priv_key->len = mbedtls_pk_get_len( &cert.pk );
Paul Bakker43b7e352011-01-18 15:27:19 +0000138 priv_key->pkcs11h_cert = pkcs11_cert;
139
140 ret = 0;
141
142cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200143 mbedtls_x509_crt_free( &cert );
Paul Bakker43b7e352011-01-18 15:27:19 +0000144
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200145 return( ret );
Paul Bakker43b7e352011-01-18 15:27:19 +0000146}
147
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200148void mbedtls_pkcs11_priv_key_free( mbedtls_pkcs11_context *priv_key )
Paul Bakker43b7e352011-01-18 15:27:19 +0000149{
150 if( NULL != priv_key )
151 pkcs11h_certificate_freeCertificate( priv_key->pkcs11h_cert );
152}
153
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200154int mbedtls_pkcs11_decrypt( mbedtls_pkcs11_context *ctx,
Paul Bakker23986e52011-04-24 08:57:21 +0000155 int mode, size_t *olen,
Paul Bakker43b7e352011-01-18 15:27:19 +0000156 const unsigned char *input,
157 unsigned char *output,
Paul Bakker9a736322012-11-14 12:39:52 +0000158 size_t output_max_len )
Paul Bakker43b7e352011-01-18 15:27:19 +0000159{
160 size_t input_len, output_len;
161
162 if( NULL == ctx )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200163 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker43b7e352011-01-18 15:27:19 +0000164
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200165 if( MBEDTLS_RSA_PRIVATE != mode )
166 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker43b7e352011-01-18 15:27:19 +0000167
168 output_len = input_len = ctx->len;
169
170 if( input_len < 16 || input_len > output_max_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200171 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker43b7e352011-01-18 15:27:19 +0000172
173 /* Determine size of output buffer */
174 if( pkcs11h_certificate_decryptAny( ctx->pkcs11h_cert, CKM_RSA_PKCS, input,
175 input_len, NULL, &output_len ) != CKR_OK )
176 {
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 }
179
180 if( output_len > output_max_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200181 return( MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE );
Paul Bakker43b7e352011-01-18 15:27:19 +0000182
183 if( pkcs11h_certificate_decryptAny( ctx->pkcs11h_cert, CKM_RSA_PKCS, input,
184 input_len, output, &output_len ) != CKR_OK )
185 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200186 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker43b7e352011-01-18 15:27:19 +0000187 }
188 *olen = output_len;
189 return( 0 );
190}
191
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200192int mbedtls_pkcs11_sign( mbedtls_pkcs11_context *ctx,
Paul Bakker43b7e352011-01-18 15:27:19 +0000193 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200194 mbedtls_md_type_t md_alg,
Paul Bakker23986e52011-04-24 08:57:21 +0000195 unsigned int hashlen,
Paul Bakker43b7e352011-01-18 15:27:19 +0000196 const unsigned char *hash,
197 unsigned char *sig )
198{
Paul Bakkerdb1f0592014-03-26 14:53:47 +0100199 size_t sig_len = 0, asn_len = 0, oid_size = 0;
Paul Bakker43b7e352011-01-18 15:27:19 +0000200 unsigned char *p = sig;
Steffan Karger28d81a02013-11-13 16:57:58 +0100201 const char *oid;
Paul Bakker43b7e352011-01-18 15:27:19 +0000202
203 if( NULL == ctx )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200204 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker43b7e352011-01-18 15:27:19 +0000205
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200206 if( MBEDTLS_RSA_PRIVATE != mode )
207 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker43b7e352011-01-18 15:27:19 +0000208
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200209 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakker43b7e352011-01-18 15:27:19 +0000210 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200211 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
Steffan Karger28d81a02013-11-13 16:57:58 +0100212 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200213 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Steffan Karger28d81a02013-11-13 16:57:58 +0100214
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200215 if( mbedtls_oid_get_oid_by_md( md_alg, &oid, &oid_size ) != 0 )
216 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Steffan Karger28d81a02013-11-13 16:57:58 +0100217
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200218 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerdb1f0592014-03-26 14:53:47 +0100219 asn_len = 10 + oid_size;
Steffan Karger28d81a02013-11-13 16:57:58 +0100220 }
221
Paul Bakkerdb1f0592014-03-26 14:53:47 +0100222 sig_len = ctx->len;
Paul Bakker66d5d072014-06-17 16:39:18 +0200223 if( hashlen > sig_len || asn_len > sig_len ||
224 hashlen + asn_len > sig_len )
Steffan Karger28d81a02013-11-13 16:57:58 +0100225 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200226 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Steffan Karger28d81a02013-11-13 16:57:58 +0100227 }
Paul Bakkerdb1f0592014-03-26 14:53:47 +0100228
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200229 if( md_alg != MBEDTLS_MD_NONE )
Steffan Karger28d81a02013-11-13 16:57:58 +0100230 {
231 /*
232 * DigestInfo ::= SEQUENCE {
233 * digestAlgorithm DigestAlgorithmIdentifier,
234 * digest Digest }
235 *
236 * DigestAlgorithmIdentifier ::= AlgorithmIdentifier
237 *
238 * Digest ::= OCTET STRING
239 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200240 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Steffan Karger28d81a02013-11-13 16:57:58 +0100241 *p++ = (unsigned char) ( 0x08 + oid_size + hashlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200242 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Steffan Karger28d81a02013-11-13 16:57:58 +0100243 *p++ = (unsigned char) ( 0x04 + oid_size );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200244 *p++ = MBEDTLS_ASN1_OID;
Steffan Karger28d81a02013-11-13 16:57:58 +0100245 *p++ = oid_size & 0xFF;
246 memcpy( p, oid, oid_size );
247 p += oid_size;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200248 *p++ = MBEDTLS_ASN1_NULL;
Steffan Karger28d81a02013-11-13 16:57:58 +0100249 *p++ = 0x00;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200250 *p++ = MBEDTLS_ASN1_OCTET_STRING;
Steffan Karger28d81a02013-11-13 16:57:58 +0100251 *p++ = hashlen;
Paul Bakker43b7e352011-01-18 15:27:19 +0000252 }
253
Paul Bakkerdb1f0592014-03-26 14:53:47 +0100254 memcpy( p, hash, hashlen );
255
Paul Bakker43b7e352011-01-18 15:27:19 +0000256 if( pkcs11h_certificate_signAny( ctx->pkcs11h_cert, CKM_RSA_PKCS, sig,
Paul Bakkerdb1f0592014-03-26 14:53:47 +0100257 asn_len + hashlen, sig, &sig_len ) != CKR_OK )
Paul Bakker43b7e352011-01-18 15:27:19 +0000258 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200259 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker43b7e352011-01-18 15:27:19 +0000260 }
261
262 return( 0 );
263}
264
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200265#endif /* defined(MBEDTLS_PKCS11_C) */