2018-02-08 18:04:43 +00:00
|
|
|
/**
|
|
|
|
* \file pkcs11.c
|
|
|
|
*
|
|
|
|
* \brief Wrapper for PKCS#11 library libpkcs11-helper
|
|
|
|
*
|
|
|
|
* \author Adriaan de Jong <dejong@fox-it.com>
|
|
|
|
*
|
2020-09-05 10:53:20 +00:00
|
|
|
* Copyright The Mbed TLS Contributors
|
2024-01-30 13:09:13 +00:00
|
|
|
* SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
|
2018-02-08 18:04:43 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "mbedtls/pkcs11.h"
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_PKCS11_C)
|
|
|
|
|
|
|
|
#include "mbedtls/md.h"
|
|
|
|
#include "mbedtls/oid.h"
|
|
|
|
#include "mbedtls/x509_crt.h"
|
|
|
|
|
|
|
|
#include "mbedtls/platform.h"
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
2023-04-18 08:38:24 +00:00
|
|
|
void mbedtls_pkcs11_init(mbedtls_pkcs11_context *ctx)
|
2018-02-08 18:04:43 +00:00
|
|
|
{
|
2023-04-18 08:38:24 +00:00
|
|
|
memset(ctx, 0, sizeof(mbedtls_pkcs11_context));
|
2018-02-08 18:04:43 +00:00
|
|
|
}
|
|
|
|
|
2023-04-18 08:38:24 +00:00
|
|
|
int mbedtls_pkcs11_x509_cert_bind(mbedtls_x509_crt *cert, pkcs11h_certificate_t pkcs11_cert)
|
2018-02-08 18:04:43 +00:00
|
|
|
{
|
|
|
|
int ret = 1;
|
|
|
|
unsigned char *cert_blob = NULL;
|
|
|
|
size_t cert_blob_size = 0;
|
|
|
|
|
2023-04-18 08:38:24 +00:00
|
|
|
if (cert == NULL) {
|
2018-02-08 18:04:43 +00:00
|
|
|
ret = 2;
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
2023-04-18 08:38:24 +00:00
|
|
|
if (pkcs11h_certificate_getCertificateBlob(pkcs11_cert, NULL,
|
|
|
|
&cert_blob_size) != CKR_OK) {
|
2018-02-08 18:04:43 +00:00
|
|
|
ret = 3;
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
2023-04-18 08:38:24 +00:00
|
|
|
cert_blob = mbedtls_calloc(1, cert_blob_size);
|
|
|
|
if (NULL == cert_blob) {
|
2018-02-08 18:04:43 +00:00
|
|
|
ret = 4;
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
2023-04-18 08:38:24 +00:00
|
|
|
if (pkcs11h_certificate_getCertificateBlob(pkcs11_cert, cert_blob,
|
|
|
|
&cert_blob_size) != CKR_OK) {
|
2018-02-08 18:04:43 +00:00
|
|
|
ret = 5;
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
2023-04-18 08:38:24 +00:00
|
|
|
if (0 != mbedtls_x509_crt_parse(cert, cert_blob, cert_blob_size)) {
|
2018-02-08 18:04:43 +00:00
|
|
|
ret = 6;
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = 0;
|
|
|
|
|
|
|
|
cleanup:
|
2023-04-18 08:38:24 +00:00
|
|
|
if (NULL != cert_blob) {
|
|
|
|
mbedtls_free(cert_blob);
|
|
|
|
}
|
2018-02-08 18:04:43 +00:00
|
|
|
|
2023-04-18 08:38:24 +00:00
|
|
|
return ret;
|
2018-02-08 18:04:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-04-18 08:38:24 +00:00
|
|
|
int mbedtls_pkcs11_priv_key_bind(mbedtls_pkcs11_context *priv_key,
|
|
|
|
pkcs11h_certificate_t pkcs11_cert)
|
2018-02-08 18:04:43 +00:00
|
|
|
{
|
|
|
|
int ret = 1;
|
|
|
|
mbedtls_x509_crt cert;
|
|
|
|
|
2023-04-18 08:38:24 +00:00
|
|
|
mbedtls_x509_crt_init(&cert);
|
2018-02-08 18:04:43 +00:00
|
|
|
|
2023-04-18 08:38:24 +00:00
|
|
|
if (priv_key == NULL) {
|
2018-02-08 18:04:43 +00:00
|
|
|
goto cleanup;
|
2023-04-18 08:38:24 +00:00
|
|
|
}
|
2018-02-08 18:04:43 +00:00
|
|
|
|
2023-04-18 08:38:24 +00:00
|
|
|
if (0 != mbedtls_pkcs11_x509_cert_bind(&cert, pkcs11_cert)) {
|
2018-02-08 18:04:43 +00:00
|
|
|
goto cleanup;
|
2023-04-18 08:38:24 +00:00
|
|
|
}
|
2018-02-08 18:04:43 +00:00
|
|
|
|
2023-04-18 08:38:24 +00:00
|
|
|
priv_key->len = mbedtls_pk_get_len(&cert.pk);
|
2018-02-08 18:04:43 +00:00
|
|
|
priv_key->pkcs11h_cert = pkcs11_cert;
|
|
|
|
|
|
|
|
ret = 0;
|
|
|
|
|
|
|
|
cleanup:
|
2023-04-18 08:38:24 +00:00
|
|
|
mbedtls_x509_crt_free(&cert);
|
2018-02-08 18:04:43 +00:00
|
|
|
|
2023-04-18 08:38:24 +00:00
|
|
|
return ret;
|
2018-02-08 18:04:43 +00:00
|
|
|
}
|
|
|
|
|
2023-04-18 08:38:24 +00:00
|
|
|
void mbedtls_pkcs11_priv_key_free(mbedtls_pkcs11_context *priv_key)
|
2018-02-08 18:04:43 +00:00
|
|
|
{
|
2023-04-18 08:38:24 +00:00
|
|
|
if (NULL != priv_key) {
|
|
|
|
pkcs11h_certificate_freeCertificate(priv_key->pkcs11h_cert);
|
|
|
|
}
|
2018-02-08 18:04:43 +00:00
|
|
|
}
|
|
|
|
|
2023-04-18 08:38:24 +00:00
|
|
|
int mbedtls_pkcs11_decrypt(mbedtls_pkcs11_context *ctx,
|
|
|
|
int mode, size_t *olen,
|
|
|
|
const unsigned char *input,
|
|
|
|
unsigned char *output,
|
|
|
|
size_t output_max_len)
|
2018-02-08 18:04:43 +00:00
|
|
|
{
|
|
|
|
size_t input_len, output_len;
|
|
|
|
|
2023-04-18 08:38:24 +00:00
|
|
|
if (NULL == ctx) {
|
|
|
|
return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
|
|
|
|
}
|
2018-02-08 18:04:43 +00:00
|
|
|
|
2023-04-18 08:38:24 +00:00
|
|
|
if (MBEDTLS_RSA_PRIVATE != mode) {
|
|
|
|
return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
|
|
|
|
}
|
2018-02-08 18:04:43 +00:00
|
|
|
|
|
|
|
output_len = input_len = ctx->len;
|
|
|
|
|
2023-04-18 08:38:24 +00:00
|
|
|
if (input_len < 16 || input_len > output_max_len) {
|
|
|
|
return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
|
|
|
|
}
|
2018-02-08 18:04:43 +00:00
|
|
|
|
|
|
|
/* Determine size of output buffer */
|
2023-04-18 08:38:24 +00:00
|
|
|
if (pkcs11h_certificate_decryptAny(ctx->pkcs11h_cert, CKM_RSA_PKCS, input,
|
|
|
|
input_len, NULL, &output_len) != CKR_OK) {
|
|
|
|
return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
|
2018-02-08 18:04:43 +00:00
|
|
|
}
|
|
|
|
|
2023-04-18 08:38:24 +00:00
|
|
|
if (output_len > output_max_len) {
|
|
|
|
return MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE;
|
|
|
|
}
|
2018-02-08 18:04:43 +00:00
|
|
|
|
2023-04-18 08:38:24 +00:00
|
|
|
if (pkcs11h_certificate_decryptAny(ctx->pkcs11h_cert, CKM_RSA_PKCS, input,
|
|
|
|
input_len, output, &output_len) != CKR_OK) {
|
|
|
|
return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
|
2018-02-08 18:04:43 +00:00
|
|
|
}
|
|
|
|
*olen = output_len;
|
2023-04-18 08:38:24 +00:00
|
|
|
return 0;
|
2018-02-08 18:04:43 +00:00
|
|
|
}
|
|
|
|
|
2023-04-18 08:38:24 +00:00
|
|
|
int 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)
|
2018-02-08 18:04:43 +00:00
|
|
|
{
|
|
|
|
size_t sig_len = 0, asn_len = 0, oid_size = 0;
|
|
|
|
unsigned char *p = sig;
|
|
|
|
const char *oid;
|
|
|
|
|
2023-04-18 08:38:24 +00:00
|
|
|
if (NULL == ctx) {
|
|
|
|
return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
|
|
|
|
}
|
2018-02-08 18:04:43 +00:00
|
|
|
|
2023-04-18 08:38:24 +00:00
|
|
|
if (MBEDTLS_RSA_PRIVATE != mode) {
|
|
|
|
return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
|
|
|
|
}
|
2018-02-08 18:04:43 +00:00
|
|
|
|
2023-04-18 08:38:24 +00:00
|
|
|
if (md_alg != MBEDTLS_MD_NONE) {
|
|
|
|
const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type(md_alg);
|
|
|
|
if (md_info == NULL) {
|
|
|
|
return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
|
|
|
|
}
|
2018-02-08 18:04:43 +00:00
|
|
|
|
2023-04-18 08:38:24 +00:00
|
|
|
if (mbedtls_oid_get_oid_by_md(md_alg, &oid, &oid_size) != 0) {
|
|
|
|
return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
|
|
|
|
}
|
2018-02-08 18:04:43 +00:00
|
|
|
|
2023-04-18 08:38:24 +00:00
|
|
|
hashlen = mbedtls_md_get_size(md_info);
|
2018-02-08 18:04:43 +00:00
|
|
|
asn_len = 10 + oid_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
sig_len = ctx->len;
|
2023-04-18 08:38:24 +00:00
|
|
|
if (hashlen > sig_len || asn_len > sig_len ||
|
|
|
|
hashlen + asn_len > sig_len) {
|
|
|
|
return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
|
2018-02-08 18:04:43 +00:00
|
|
|
}
|
|
|
|
|
2023-04-18 08:38:24 +00:00
|
|
|
if (md_alg != MBEDTLS_MD_NONE) {
|
2018-02-08 18:04:43 +00:00
|
|
|
/*
|
|
|
|
* DigestInfo ::= SEQUENCE {
|
|
|
|
* digestAlgorithm DigestAlgorithmIdentifier,
|
|
|
|
* digest Digest }
|
|
|
|
*
|
|
|
|
* DigestAlgorithmIdentifier ::= AlgorithmIdentifier
|
|
|
|
*
|
|
|
|
* Digest ::= OCTET STRING
|
|
|
|
*/
|
|
|
|
*p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
|
2023-04-18 08:38:24 +00:00
|
|
|
*p++ = (unsigned char) (0x08 + oid_size + hashlen);
|
2018-02-08 18:04:43 +00:00
|
|
|
*p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
|
2023-04-18 08:38:24 +00:00
|
|
|
*p++ = (unsigned char) (0x04 + oid_size);
|
2018-02-08 18:04:43 +00:00
|
|
|
*p++ = MBEDTLS_ASN1_OID;
|
|
|
|
*p++ = oid_size & 0xFF;
|
2023-04-18 08:38:24 +00:00
|
|
|
memcpy(p, oid, oid_size);
|
2018-02-08 18:04:43 +00:00
|
|
|
p += oid_size;
|
|
|
|
*p++ = MBEDTLS_ASN1_NULL;
|
|
|
|
*p++ = 0x00;
|
|
|
|
*p++ = MBEDTLS_ASN1_OCTET_STRING;
|
|
|
|
*p++ = hashlen;
|
|
|
|
}
|
|
|
|
|
2023-04-18 08:38:24 +00:00
|
|
|
memcpy(p, hash, hashlen);
|
2018-02-08 18:04:43 +00:00
|
|
|
|
2023-04-18 08:38:24 +00:00
|
|
|
if (pkcs11h_certificate_signAny(ctx->pkcs11h_cert, CKM_RSA_PKCS, sig,
|
|
|
|
asn_len + hashlen, sig, &sig_len) != CKR_OK) {
|
|
|
|
return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
|
2018-02-08 18:04:43 +00:00
|
|
|
}
|
|
|
|
|
2023-04-18 08:38:24 +00:00
|
|
|
return 0;
|
2018-02-08 18:04:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* defined(MBEDTLS_PKCS11_C) */
|