blob: a7207cfc930fe7b7862f62ca281217976548869a [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é-Gonnard7f809972015-03-09 17:05:11 +000032#include "mbedtls/platform.h"
Paul Bakker6e339b52013-07-03 13:37:05 +020033
Manuel Pégourié-Gonnard3a895592015-05-27 17:09:21 +020034#include <string.h>
35
Manuel Pégourié-Gonnardeab147c2015-04-29 01:10:10 +020036void mbedtls_pkcs11_init( mbedtls_pkcs11_context *ctx )
37{
38 memset( ctx, 0, sizeof( mbedtls_pkcs11_context ) );
39}
40
41int mbedtls_pkcs11_x509_cert_bind( mbedtls_x509_crt *cert, pkcs11h_certificate_t pkcs11_cert )
Paul Bakker43b7e352011-01-18 15:27:19 +000042{
43 int ret = 1;
44 unsigned char *cert_blob = NULL;
45 size_t cert_blob_size = 0;
46
47 if( cert == NULL )
48 {
49 ret = 2;
50 goto cleanup;
51 }
52
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +020053 if( pkcs11h_certificate_getCertificateBlob( pkcs11_cert, NULL,
54 &cert_blob_size ) != CKR_OK )
Paul Bakker43b7e352011-01-18 15:27:19 +000055 {
56 ret = 3;
57 goto cleanup;
58 }
59
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +020060 cert_blob = mbedtls_calloc( 1, cert_blob_size );
Paul Bakker43b7e352011-01-18 15:27:19 +000061 if( NULL == cert_blob )
62 {
63 ret = 4;
64 goto cleanup;
65 }
66
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +020067 if( pkcs11h_certificate_getCertificateBlob( pkcs11_cert, cert_blob,
68 &cert_blob_size ) != CKR_OK )
Paul Bakker43b7e352011-01-18 15:27:19 +000069 {
70 ret = 5;
71 goto cleanup;
72 }
73
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020074 if( 0 != mbedtls_x509_crt_parse( cert, cert_blob, cert_blob_size ) )
Paul Bakker43b7e352011-01-18 15:27:19 +000075 {
76 ret = 6;
77 goto cleanup;
78 }
79
80 ret = 0;
81
82cleanup:
83 if( NULL != cert_blob )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020084 mbedtls_free( cert_blob );
Paul Bakker43b7e352011-01-18 15:27:19 +000085
Paul Bakkerd8bb8262014-06-17 14:06:49 +020086 return( ret );
Paul Bakker43b7e352011-01-18 15:27:19 +000087}
88
89
Manuel Pégourié-Gonnardeab147c2015-04-29 01:10:10 +020090int mbedtls_pkcs11_priv_key_bind( mbedtls_pkcs11_context *priv_key,
Paul Bakker43b7e352011-01-18 15:27:19 +000091 pkcs11h_certificate_t pkcs11_cert )
92{
93 int ret = 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020094 mbedtls_x509_crt cert;
Paul Bakker43b7e352011-01-18 15:27:19 +000095
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020096 mbedtls_x509_crt_init( &cert );
Paul Bakker43b7e352011-01-18 15:27:19 +000097
98 if( priv_key == NULL )
99 goto cleanup;
100
Manuel Pégourié-Gonnard3a895592015-05-27 17:09:21 +0200101 if( 0 != mbedtls_pkcs11_x509_cert_bind( &cert, pkcs11_cert ) )
Paul Bakker43b7e352011-01-18 15:27:19 +0000102 goto cleanup;
103
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200104 priv_key->len = mbedtls_pk_get_len( &cert.pk );
Paul Bakker43b7e352011-01-18 15:27:19 +0000105 priv_key->pkcs11h_cert = pkcs11_cert;
106
107 ret = 0;
108
109cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200110 mbedtls_x509_crt_free( &cert );
Paul Bakker43b7e352011-01-18 15:27:19 +0000111
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200112 return( ret );
Paul Bakker43b7e352011-01-18 15:27:19 +0000113}
114
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200115void mbedtls_pkcs11_priv_key_free( mbedtls_pkcs11_context *priv_key )
Paul Bakker43b7e352011-01-18 15:27:19 +0000116{
117 if( NULL != priv_key )
118 pkcs11h_certificate_freeCertificate( priv_key->pkcs11h_cert );
119}
120
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200121int mbedtls_pkcs11_decrypt( mbedtls_pkcs11_context *ctx,
Paul Bakker23986e52011-04-24 08:57:21 +0000122 int mode, size_t *olen,
Paul Bakker43b7e352011-01-18 15:27:19 +0000123 const unsigned char *input,
124 unsigned char *output,
Paul Bakker9a736322012-11-14 12:39:52 +0000125 size_t output_max_len )
Paul Bakker43b7e352011-01-18 15:27:19 +0000126{
127 size_t input_len, output_len;
128
129 if( NULL == ctx )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200130 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker43b7e352011-01-18 15:27:19 +0000131
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200132 if( MBEDTLS_RSA_PRIVATE != mode )
133 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker43b7e352011-01-18 15:27:19 +0000134
135 output_len = input_len = ctx->len;
136
137 if( input_len < 16 || input_len > output_max_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200138 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker43b7e352011-01-18 15:27:19 +0000139
140 /* Determine size of output buffer */
141 if( pkcs11h_certificate_decryptAny( ctx->pkcs11h_cert, CKM_RSA_PKCS, input,
142 input_len, NULL, &output_len ) != CKR_OK )
143 {
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
147 if( output_len > output_max_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200148 return( MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE );
Paul Bakker43b7e352011-01-18 15:27:19 +0000149
150 if( pkcs11h_certificate_decryptAny( ctx->pkcs11h_cert, CKM_RSA_PKCS, input,
151 input_len, output, &output_len ) != CKR_OK )
152 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200153 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker43b7e352011-01-18 15:27:19 +0000154 }
155 *olen = output_len;
156 return( 0 );
157}
158
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200159int mbedtls_pkcs11_sign( mbedtls_pkcs11_context *ctx,
Paul Bakker43b7e352011-01-18 15:27:19 +0000160 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200161 mbedtls_md_type_t md_alg,
Paul Bakker23986e52011-04-24 08:57:21 +0000162 unsigned int hashlen,
Paul Bakker43b7e352011-01-18 15:27:19 +0000163 const unsigned char *hash,
164 unsigned char *sig )
165{
Paul Bakkerdb1f0592014-03-26 14:53:47 +0100166 size_t sig_len = 0, asn_len = 0, oid_size = 0;
Paul Bakker43b7e352011-01-18 15:27:19 +0000167 unsigned char *p = sig;
Steffan Karger28d81a02013-11-13 16:57:58 +0100168 const char *oid;
Paul Bakker43b7e352011-01-18 15:27:19 +0000169
170 if( NULL == ctx )
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
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200173 if( MBEDTLS_RSA_PRIVATE != mode )
174 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker43b7e352011-01-18 15:27:19 +0000175
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200176 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakker43b7e352011-01-18 15:27:19 +0000177 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200178 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
Steffan Karger28d81a02013-11-13 16:57:58 +0100179 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200180 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Steffan Karger28d81a02013-11-13 16:57:58 +0100181
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200182 if( mbedtls_oid_get_oid_by_md( md_alg, &oid, &oid_size ) != 0 )
183 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Steffan Karger28d81a02013-11-13 16:57:58 +0100184
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200185 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerdb1f0592014-03-26 14:53:47 +0100186 asn_len = 10 + oid_size;
Steffan Karger28d81a02013-11-13 16:57:58 +0100187 }
188
Paul Bakkerdb1f0592014-03-26 14:53:47 +0100189 sig_len = ctx->len;
Paul Bakker66d5d072014-06-17 16:39:18 +0200190 if( hashlen > sig_len || asn_len > sig_len ||
191 hashlen + asn_len > sig_len )
Steffan Karger28d81a02013-11-13 16:57:58 +0100192 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200193 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Steffan Karger28d81a02013-11-13 16:57:58 +0100194 }
Paul Bakkerdb1f0592014-03-26 14:53:47 +0100195
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200196 if( md_alg != MBEDTLS_MD_NONE )
Steffan Karger28d81a02013-11-13 16:57:58 +0100197 {
198 /*
199 * DigestInfo ::= SEQUENCE {
200 * digestAlgorithm DigestAlgorithmIdentifier,
201 * digest Digest }
202 *
203 * DigestAlgorithmIdentifier ::= AlgorithmIdentifier
204 *
205 * Digest ::= OCTET STRING
206 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200207 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Steffan Karger28d81a02013-11-13 16:57:58 +0100208 *p++ = (unsigned char) ( 0x08 + oid_size + hashlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200209 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Steffan Karger28d81a02013-11-13 16:57:58 +0100210 *p++ = (unsigned char) ( 0x04 + oid_size );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200211 *p++ = MBEDTLS_ASN1_OID;
Steffan Karger28d81a02013-11-13 16:57:58 +0100212 *p++ = oid_size & 0xFF;
213 memcpy( p, oid, oid_size );
214 p += oid_size;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200215 *p++ = MBEDTLS_ASN1_NULL;
Steffan Karger28d81a02013-11-13 16:57:58 +0100216 *p++ = 0x00;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200217 *p++ = MBEDTLS_ASN1_OCTET_STRING;
Steffan Karger28d81a02013-11-13 16:57:58 +0100218 *p++ = hashlen;
Paul Bakker43b7e352011-01-18 15:27:19 +0000219 }
220
Paul Bakkerdb1f0592014-03-26 14:53:47 +0100221 memcpy( p, hash, hashlen );
222
Paul Bakker43b7e352011-01-18 15:27:19 +0000223 if( pkcs11h_certificate_signAny( ctx->pkcs11h_cert, CKM_RSA_PKCS, sig,
Paul Bakkerdb1f0592014-03-26 14:53:47 +0100224 asn_len + hashlen, sig, &sig_len ) != CKR_OK )
Paul Bakker43b7e352011-01-18 15:27:19 +0000225 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200226 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker43b7e352011-01-18 15:27:19 +0000227 }
228
229 return( 0 );
230}
231
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200232#endif /* defined(MBEDTLS_PKCS11_C) */