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  *
8  *  Copyright The Mbed TLS Contributors
9  *  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.
22  */
23 
24 #include "mbedtls/pkcs11.h"
25 
26 #if defined(MBEDTLS_PKCS11_C)
27 
28 #include "mbedtls/md.h"
29 #include "mbedtls/oid.h"
30 #include "mbedtls/x509_crt.h"
31 
32 #if defined(MBEDTLS_PLATFORM_C)
33 #include "mbedtls/platform.h"
34 #else
35 #include <stdlib.h>
36 #define mbedtls_calloc    calloc
37 #define mbedtls_free       free
38 #endif
39 
40 #include <string.h>
41 
mbedtls_pkcs11_init(mbedtls_pkcs11_context * ctx)42 void mbedtls_pkcs11_init( mbedtls_pkcs11_context *ctx )
43 {
44     memset( ctx, 0, sizeof( mbedtls_pkcs11_context ) );
45 }
46 
mbedtls_pkcs11_x509_cert_bind(mbedtls_x509_crt * cert,pkcs11h_certificate_t pkcs11_cert)47 int mbedtls_pkcs11_x509_cert_bind( mbedtls_x509_crt *cert, pkcs11h_certificate_t pkcs11_cert )
48 {
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 
59     if( pkcs11h_certificate_getCertificateBlob( pkcs11_cert, NULL,
60                                                 &cert_blob_size ) != CKR_OK )
61     {
62         ret = 3;
63         goto cleanup;
64     }
65 
66     cert_blob = mbedtls_calloc( 1, cert_blob_size );
67     if( NULL == cert_blob )
68     {
69         ret = 4;
70         goto cleanup;
71     }
72 
73     if( pkcs11h_certificate_getCertificateBlob( pkcs11_cert, cert_blob,
74                                                 &cert_blob_size ) != CKR_OK )
75     {
76         ret = 5;
77         goto cleanup;
78     }
79 
80     if( 0 != mbedtls_x509_crt_parse( cert, cert_blob, cert_blob_size ) )
81     {
82         ret = 6;
83         goto cleanup;
84     }
85 
86     ret = 0;
87 
88 cleanup:
89     if( NULL != cert_blob )
90         mbedtls_free( cert_blob );
91 
92     return( ret );
93 }
94 
95 
mbedtls_pkcs11_priv_key_bind(mbedtls_pkcs11_context * priv_key,pkcs11h_certificate_t pkcs11_cert)96 int mbedtls_pkcs11_priv_key_bind( mbedtls_pkcs11_context *priv_key,
97         pkcs11h_certificate_t pkcs11_cert )
98 {
99     int ret = 1;
100     mbedtls_x509_crt cert;
101 
102     mbedtls_x509_crt_init( &cert );
103 
104     if( priv_key == NULL )
105         goto cleanup;
106 
107     if( 0 != mbedtls_pkcs11_x509_cert_bind( &cert, pkcs11_cert ) )
108         goto cleanup;
109 
110     priv_key->len = mbedtls_pk_get_len( &cert.pk );
111     priv_key->pkcs11h_cert = pkcs11_cert;
112 
113     ret = 0;
114 
115 cleanup:
116     mbedtls_x509_crt_free( &cert );
117 
118     return( ret );
119 }
120 
mbedtls_pkcs11_priv_key_free(mbedtls_pkcs11_context * priv_key)121 void mbedtls_pkcs11_priv_key_free( mbedtls_pkcs11_context *priv_key )
122 {
123     if( NULL != priv_key )
124         pkcs11h_certificate_freeCertificate( priv_key->pkcs11h_cert );
125 }
126 
mbedtls_pkcs11_decrypt(mbedtls_pkcs11_context * ctx,int mode,size_t * olen,const unsigned char * input,unsigned char * output,size_t output_max_len)127 int mbedtls_pkcs11_decrypt( mbedtls_pkcs11_context *ctx,
128                        int mode, size_t *olen,
129                        const unsigned char *input,
130                        unsigned char *output,
131                        size_t output_max_len )
132 {
133     size_t input_len, output_len;
134 
135     if( NULL == ctx )
136         return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
137 
138     if( MBEDTLS_RSA_PRIVATE != mode )
139         return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
140 
141     output_len = input_len = ctx->len;
142 
143     if( input_len < 16 || input_len > output_max_len )
144         return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
145 
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     {
150         return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
151     }
152 
153     if( output_len > output_max_len )
154         return( MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE );
155 
156     if( pkcs11h_certificate_decryptAny( ctx->pkcs11h_cert, CKM_RSA_PKCS, input,
157             input_len, output, &output_len ) != CKR_OK )
158     {
159         return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
160     }
161     *olen = output_len;
162     return( 0 );
163 }
164 
mbedtls_pkcs11_sign(mbedtls_pkcs11_context * ctx,int mode,mbedtls_md_type_t md_alg,unsigned int hashlen,const unsigned char * hash,unsigned char * sig)165 int mbedtls_pkcs11_sign( mbedtls_pkcs11_context *ctx,
166                     int mode,
167                     mbedtls_md_type_t md_alg,
168                     unsigned int hashlen,
169                     const unsigned char *hash,
170                     unsigned char *sig )
171 {
172     size_t sig_len = 0, asn_len = 0, oid_size = 0;
173     unsigned char *p = sig;
174     const char *oid;
175 
176     if( NULL == ctx )
177         return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
178 
179     if( MBEDTLS_RSA_PRIVATE != mode )
180         return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
181 
182     if( md_alg != MBEDTLS_MD_NONE )
183     {
184         const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
185         if( md_info == NULL )
186             return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
187 
188         if( mbedtls_oid_get_oid_by_md( md_alg, &oid, &oid_size ) != 0 )
189             return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
190 
191         hashlen = mbedtls_md_get_size( md_info );
192         asn_len = 10 + oid_size;
193     }
194 
195     sig_len = ctx->len;
196     if( hashlen > sig_len || asn_len > sig_len ||
197         hashlen + asn_len > sig_len )
198     {
199         return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
200     }
201 
202     if( md_alg != MBEDTLS_MD_NONE )
203     {
204         /*
205          * DigestInfo ::= SEQUENCE {
206          *   digestAlgorithm DigestAlgorithmIdentifier,
207          *   digest Digest }
208          *
209          * DigestAlgorithmIdentifier ::= AlgorithmIdentifier
210          *
211          * Digest ::= OCTET STRING
212          */
213         *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
214         *p++ = (unsigned char) ( 0x08 + oid_size + hashlen );
215         *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
216         *p++ = (unsigned char) ( 0x04 + oid_size );
217         *p++ = MBEDTLS_ASN1_OID;
218         *p++ = oid_size & 0xFF;
219         memcpy( p, oid, oid_size );
220         p += oid_size;
221         *p++ = MBEDTLS_ASN1_NULL;
222         *p++ = 0x00;
223         *p++ = MBEDTLS_ASN1_OCTET_STRING;
224         *p++ = hashlen;
225     }
226 
227     memcpy( p, hash, hashlen );
228 
229     if( pkcs11h_certificate_signAny( ctx->pkcs11h_cert, CKM_RSA_PKCS, sig,
230             asn_len + hashlen, sig, &sig_len ) != CKR_OK )
231     {
232         return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
233     }
234 
235     return( 0 );
236 }
237 
238 #endif /* defined(MBEDTLS_PKCS11_C) */
239