blob: 30d045bf18e2b7fe86b04fd659e6e6ac4d18a37a [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 *
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02008 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Bence Szépkúti4e9f7122020-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úti4e9f7122020-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 * **********
49 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000050 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker43b7e352011-01-18 15:27:19 +000051 */
52
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000053#include "mbedtls/pkcs11.h"
Paul Bakker43b7e352011-01-18 15:27:19 +000054
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020055#if defined(MBEDTLS_PKCS11_C)
Rich Evans00ab4702015-02-06 13:43:58 +000056
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000057#include "mbedtls/md.h"
58#include "mbedtls/oid.h"
59#include "mbedtls/x509_crt.h"
Paul Bakker43b7e352011-01-18 15:27:19 +000060
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020061#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000062#include "mbedtls/platform.h"
Paul Bakker6e339b52013-07-03 13:37:05 +020063#else
Paul Bakker7dc4c442014-02-01 22:50:26 +010064#include <stdlib.h>
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +020065#define mbedtls_calloc calloc
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020066#define mbedtls_free free
Paul Bakker6e339b52013-07-03 13:37:05 +020067#endif
68
Manuel Pégourié-Gonnard3a895592015-05-27 17:09:21 +020069#include <string.h>
70
Manuel Pégourié-Gonnardeab147c2015-04-29 01:10:10 +020071void mbedtls_pkcs11_init( mbedtls_pkcs11_context *ctx )
72{
73 memset( ctx, 0, sizeof( mbedtls_pkcs11_context ) );
74}
75
76int mbedtls_pkcs11_x509_cert_bind( mbedtls_x509_crt *cert, pkcs11h_certificate_t pkcs11_cert )
Paul Bakker43b7e352011-01-18 15:27:19 +000077{
78 int ret = 1;
79 unsigned char *cert_blob = NULL;
80 size_t cert_blob_size = 0;
81
82 if( cert == NULL )
83 {
84 ret = 2;
85 goto cleanup;
86 }
87
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +020088 if( pkcs11h_certificate_getCertificateBlob( pkcs11_cert, NULL,
89 &cert_blob_size ) != CKR_OK )
Paul Bakker43b7e352011-01-18 15:27:19 +000090 {
91 ret = 3;
92 goto cleanup;
93 }
94
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +020095 cert_blob = mbedtls_calloc( 1, cert_blob_size );
Paul Bakker43b7e352011-01-18 15:27:19 +000096 if( NULL == cert_blob )
97 {
98 ret = 4;
99 goto cleanup;
100 }
101
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200102 if( pkcs11h_certificate_getCertificateBlob( pkcs11_cert, cert_blob,
103 &cert_blob_size ) != CKR_OK )
Paul Bakker43b7e352011-01-18 15:27:19 +0000104 {
105 ret = 5;
106 goto cleanup;
107 }
108
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200109 if( 0 != mbedtls_x509_crt_parse( cert, cert_blob, cert_blob_size ) )
Paul Bakker43b7e352011-01-18 15:27:19 +0000110 {
111 ret = 6;
112 goto cleanup;
113 }
114
115 ret = 0;
116
117cleanup:
118 if( NULL != cert_blob )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200119 mbedtls_free( cert_blob );
Paul Bakker43b7e352011-01-18 15:27:19 +0000120
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200121 return( ret );
Paul Bakker43b7e352011-01-18 15:27:19 +0000122}
123
124
Manuel Pégourié-Gonnardeab147c2015-04-29 01:10:10 +0200125int mbedtls_pkcs11_priv_key_bind( mbedtls_pkcs11_context *priv_key,
Paul Bakker43b7e352011-01-18 15:27:19 +0000126 pkcs11h_certificate_t pkcs11_cert )
127{
128 int ret = 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200129 mbedtls_x509_crt cert;
Paul Bakker43b7e352011-01-18 15:27:19 +0000130
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200131 mbedtls_x509_crt_init( &cert );
Paul Bakker43b7e352011-01-18 15:27:19 +0000132
133 if( priv_key == NULL )
134 goto cleanup;
135
Manuel Pégourié-Gonnard3a895592015-05-27 17:09:21 +0200136 if( 0 != mbedtls_pkcs11_x509_cert_bind( &cert, pkcs11_cert ) )
Paul Bakker43b7e352011-01-18 15:27:19 +0000137 goto cleanup;
138
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200139 priv_key->len = mbedtls_pk_get_len( &cert.pk );
Paul Bakker43b7e352011-01-18 15:27:19 +0000140 priv_key->pkcs11h_cert = pkcs11_cert;
141
142 ret = 0;
143
144cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200145 mbedtls_x509_crt_free( &cert );
Paul Bakker43b7e352011-01-18 15:27:19 +0000146
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200147 return( ret );
Paul Bakker43b7e352011-01-18 15:27:19 +0000148}
149
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200150void mbedtls_pkcs11_priv_key_free( mbedtls_pkcs11_context *priv_key )
Paul Bakker43b7e352011-01-18 15:27:19 +0000151{
152 if( NULL != priv_key )
153 pkcs11h_certificate_freeCertificate( priv_key->pkcs11h_cert );
154}
155
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200156int mbedtls_pkcs11_decrypt( mbedtls_pkcs11_context *ctx,
Paul Bakker23986e52011-04-24 08:57:21 +0000157 int mode, size_t *olen,
Paul Bakker43b7e352011-01-18 15:27:19 +0000158 const unsigned char *input,
159 unsigned char *output,
Paul Bakker9a736322012-11-14 12:39:52 +0000160 size_t output_max_len )
Paul Bakker43b7e352011-01-18 15:27:19 +0000161{
162 size_t input_len, output_len;
163
164 if( NULL == ctx )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200165 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker43b7e352011-01-18 15:27:19 +0000166
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200167 if( MBEDTLS_RSA_PRIVATE != mode )
168 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker43b7e352011-01-18 15:27:19 +0000169
170 output_len = input_len = ctx->len;
171
172 if( input_len < 16 || input_len > output_max_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200173 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker43b7e352011-01-18 15:27:19 +0000174
175 /* Determine size of output buffer */
176 if( pkcs11h_certificate_decryptAny( ctx->pkcs11h_cert, CKM_RSA_PKCS, input,
177 input_len, NULL, &output_len ) != CKR_OK )
178 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200179 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker43b7e352011-01-18 15:27:19 +0000180 }
181
182 if( output_len > output_max_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200183 return( MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE );
Paul Bakker43b7e352011-01-18 15:27:19 +0000184
185 if( pkcs11h_certificate_decryptAny( ctx->pkcs11h_cert, CKM_RSA_PKCS, input,
186 input_len, output, &output_len ) != CKR_OK )
187 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200188 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker43b7e352011-01-18 15:27:19 +0000189 }
190 *olen = output_len;
191 return( 0 );
192}
193
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200194int mbedtls_pkcs11_sign( mbedtls_pkcs11_context *ctx,
Paul Bakker43b7e352011-01-18 15:27:19 +0000195 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200196 mbedtls_md_type_t md_alg,
Paul Bakker23986e52011-04-24 08:57:21 +0000197 unsigned int hashlen,
Paul Bakker43b7e352011-01-18 15:27:19 +0000198 const unsigned char *hash,
199 unsigned char *sig )
200{
Paul Bakkerdb1f0592014-03-26 14:53:47 +0100201 size_t sig_len = 0, asn_len = 0, oid_size = 0;
Paul Bakker43b7e352011-01-18 15:27:19 +0000202 unsigned char *p = sig;
Steffan Karger28d81a02013-11-13 16:57:58 +0100203 const char *oid;
Paul Bakker43b7e352011-01-18 15:27:19 +0000204
205 if( NULL == ctx )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200206 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker43b7e352011-01-18 15:27:19 +0000207
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200208 if( MBEDTLS_RSA_PRIVATE != mode )
209 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker43b7e352011-01-18 15:27:19 +0000210
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200211 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakker43b7e352011-01-18 15:27:19 +0000212 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200213 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
Steffan Karger28d81a02013-11-13 16:57:58 +0100214 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200215 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Steffan Karger28d81a02013-11-13 16:57:58 +0100216
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200217 if( mbedtls_oid_get_oid_by_md( md_alg, &oid, &oid_size ) != 0 )
218 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Steffan Karger28d81a02013-11-13 16:57:58 +0100219
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200220 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerdb1f0592014-03-26 14:53:47 +0100221 asn_len = 10 + oid_size;
Steffan Karger28d81a02013-11-13 16:57:58 +0100222 }
223
Paul Bakkerdb1f0592014-03-26 14:53:47 +0100224 sig_len = ctx->len;
Paul Bakker66d5d072014-06-17 16:39:18 +0200225 if( hashlen > sig_len || asn_len > sig_len ||
226 hashlen + asn_len > sig_len )
Steffan Karger28d81a02013-11-13 16:57:58 +0100227 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200228 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Steffan Karger28d81a02013-11-13 16:57:58 +0100229 }
Paul Bakkerdb1f0592014-03-26 14:53:47 +0100230
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200231 if( md_alg != MBEDTLS_MD_NONE )
Steffan Karger28d81a02013-11-13 16:57:58 +0100232 {
233 /*
234 * DigestInfo ::= SEQUENCE {
235 * digestAlgorithm DigestAlgorithmIdentifier,
236 * digest Digest }
237 *
238 * DigestAlgorithmIdentifier ::= AlgorithmIdentifier
239 *
240 * Digest ::= OCTET STRING
241 */
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) ( 0x08 + oid_size + hashlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200244 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Steffan Karger28d81a02013-11-13 16:57:58 +0100245 *p++ = (unsigned char) ( 0x04 + oid_size );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200246 *p++ = MBEDTLS_ASN1_OID;
Steffan Karger28d81a02013-11-13 16:57:58 +0100247 *p++ = oid_size & 0xFF;
248 memcpy( p, oid, oid_size );
249 p += oid_size;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200250 *p++ = MBEDTLS_ASN1_NULL;
Steffan Karger28d81a02013-11-13 16:57:58 +0100251 *p++ = 0x00;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200252 *p++ = MBEDTLS_ASN1_OCTET_STRING;
Steffan Karger28d81a02013-11-13 16:57:58 +0100253 *p++ = hashlen;
Paul Bakker43b7e352011-01-18 15:27:19 +0000254 }
255
Paul Bakkerdb1f0592014-03-26 14:53:47 +0100256 memcpy( p, hash, hashlen );
257
Paul Bakker43b7e352011-01-18 15:27:19 +0000258 if( pkcs11h_certificate_signAny( ctx->pkcs11h_cert, CKM_RSA_PKCS, sig,
Paul Bakkerdb1f0592014-03-26 14:53:47 +0100259 asn_len + hashlen, sig, &sig_len ) != CKR_OK )
Paul Bakker43b7e352011-01-18 15:27:19 +0000260 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200261 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker43b7e352011-01-18 15:27:19 +0000262 }
263
264 return( 0 );
265}
266
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200267#endif /* defined(MBEDTLS_PKCS11_C) */