HALF ECHO LEARN / REFERENCE COLLECTION

v262.2

src/shared/pkcs11-util.c

86,914 bytesGit mode 100644Download original fileLGPL-2.1-or-later

Git blob: 88cdffa4b6128d0680badc0bb5f078f07a8ed062

Lines 1–1000 of 2,140

/* SPDX-License-Identifier: LGPL-2.1-or-later */
 
#include "alloc-util.h"
#include "ask-password-api.h"
#include "crypto-util.h"
#include "dlfcn-util.h"
#include "env-util.h"
#include "escape.h"
#include "format-table.h"
#include "log.h"
#include "memory-util.h"
#include "pkcs11-util.h"
#include "random-util.h"
#include "string-table.h"
#include "string-util.h"
#include "strv.h"
 
bool pkcs11_uri_valid(const char *uri) {
        const char *p;
 
        /* A very superficial checker for RFC7512 PKCS#11 URI syntax */
 
        if (isempty(uri))
                return false;
 
        p = startswith(uri, "pkcs11:");
        if (!p)
                return false;
 
        if (isempty(p))
                return false;
 
        if (!in_charset(p, ALPHANUMERICAL ".~/-_?;&%="))
                return false;
 
        return true;
}
 
static const char* const pkcs11_rsa_padding_table[_PKCS11_RSA_PADDING_MAX] = {
        [PKCS11_RSA_PADDING_PKCS1V15]    = "pkcs1",
        [PKCS11_RSA_PADDING_OAEP_SHA1]   = "oaep-sha1",
        [PKCS11_RSA_PADDING_OAEP_SHA256] = "oaep-sha256",
};
 
DEFINE_STRING_TABLE_LOOKUP(pkcs11_rsa_padding, Pkcs11RsaPadding);
 
#if HAVE_P11KIT
 
DLSYM_PROTOTYPE(p11_kit_module_get_name) = NULL;
DLSYM_PROTOTYPE(p11_kit_modules_finalize_and_release) = NULL;
DLSYM_PROTOTYPE(p11_kit_modules_load_and_initialize) = NULL;
DLSYM_PROTOTYPE(p11_kit_strerror) = NULL;
DLSYM_PROTOTYPE(p11_kit_uri_format) = NULL;
DLSYM_PROTOTYPE(p11_kit_uri_free) = NULL;
DLSYM_PROTOTYPE(p11_kit_uri_get_attributes) = NULL;
DLSYM_PROTOTYPE(p11_kit_uri_get_attribute) = NULL;
DLSYM_PROTOTYPE(p11_kit_uri_set_attribute) = NULL;
DLSYM_PROTOTYPE(p11_kit_uri_get_module_info) = NULL;
DLSYM_PROTOTYPE(p11_kit_uri_get_slot_info) = NULL;
DLSYM_PROTOTYPE(p11_kit_uri_get_token_info) = NULL;
DLSYM_PROTOTYPE(p11_kit_uri_match_token_info) = NULL;
DLSYM_PROTOTYPE(p11_kit_uri_message) = NULL;
DLSYM_PROTOTYPE(p11_kit_uri_new) = NULL;
DLSYM_PROTOTYPE(p11_kit_uri_parse) = NULL;
 
int uri_from_string(const char *p, P11KitUri **ret) {
        _cleanup_(p11_kit_uri_freep) P11KitUri *uri = NULL;
        int r;
 
        assert(p);
        assert(ret);
 
        r = dlopen_p11kit(LOG_DEBUG);
        if (r < 0)
                return r;
 
        uri = sym_p11_kit_uri_new();
        if (!uri)
                return -ENOMEM;
 
        if (sym_p11_kit_uri_parse(p, P11_KIT_URI_FOR_ANY, uri) != P11_KIT_URI_OK)
                return -EINVAL;
 
        *ret = TAKE_PTR(uri);
        return 0;
}
 
P11KitUri *uri_from_module_info(const CK_INFO *info) {
        P11KitUri *uri;
 
        assert(info);
 
        if (dlopen_p11kit(LOG_DEBUG) < 0)
                return NULL;
 
        uri = sym_p11_kit_uri_new();
        if (!uri)
                return NULL;
 
        *sym_p11_kit_uri_get_module_info(uri) = *info;
        return uri;
}
 
P11KitUri *uri_from_slot_info(const CK_SLOT_INFO *slot_info) {
        P11KitUri *uri;
 
        assert(slot_info);
 
        if (dlopen_p11kit(LOG_DEBUG) < 0)
                return NULL;
 
        uri = sym_p11_kit_uri_new();
        if (!uri)
                return NULL;
 
        *sym_p11_kit_uri_get_slot_info(uri) = *slot_info;
        return uri;
}
 
P11KitUri *uri_from_token_info(const CK_TOKEN_INFO *token_info) {
        P11KitUri *uri;
 
        assert(token_info);
 
        if (dlopen_p11kit(LOG_DEBUG) < 0)
                return NULL;
 
        uri = sym_p11_kit_uri_new();
        if (!uri)
                return NULL;
 
        *sym_p11_kit_uri_get_token_info(uri) = *token_info;
        return uri;
}
 
CK_RV pkcs11_get_slot_list_malloc(
                CK_FUNCTION_LIST *m,
                CK_SLOT_ID **ret_slotids,
                CK_ULONG *ret_n_slotids) {
 
        CK_RV rv;
 
        assert(m);
        assert(ret_slotids);
        assert(ret_n_slotids);
 
        for (unsigned tries = 0; tries < 16; tries++) {
                _cleanup_free_ CK_SLOT_ID *slotids = NULL;
                CK_ULONG n_slotids = 0;
 
                rv = m->C_GetSlotList(0, NULL, &n_slotids);
                if (rv != CKR_OK)
                        return rv;
                if (n_slotids == 0) {
                        *ret_slotids = NULL;
                        *ret_n_slotids = 0;
                        return CKR_OK;
                }
 
                slotids = new(CK_SLOT_ID, n_slotids);
                if (!slotids)
                        return CKR_HOST_MEMORY;
 
                rv = m->C_GetSlotList(0, slotids, &n_slotids);
                if (rv == CKR_OK) {
                        *ret_slotids = TAKE_PTR(slotids);
                        *ret_n_slotids = n_slotids;
                        return CKR_OK;
                }
 
                if (rv != CKR_BUFFER_TOO_SMALL)
                        return rv;
 
                /* Hu? Maybe somebody plugged something in and things changed? Let's try again */
        }
 
        return CKR_BUFFER_TOO_SMALL;
}
 
char* pkcs11_token_label(const CK_TOKEN_INFO *token_info) {
        char *t;
 
        /* The label is not NUL terminated and likely padded with spaces, let's make a copy here, so that we
         * can strip that. */
        t = strndup((char*) token_info->label, sizeof(token_info->label));
        if (!t)
                return NULL;
 
        strstrip(t);
        return t;
}
 
char* pkcs11_token_manufacturer_id(const CK_TOKEN_INFO *token_info) {
        char *t;
 
        t = strndup((char*) token_info->manufacturerID, sizeof(token_info->manufacturerID));
        if (!t)
                return NULL;
 
        strstrip(t);
        return t;
}
 
char* pkcs11_token_model(const CK_TOKEN_INFO *token_info) {
        char *t;
 
        t = strndup((char*) token_info->model, sizeof(token_info->model));
        if (!t)
                return NULL;
 
        strstrip(t);
        return t;
}
 
int pkcs11_token_login_by_pin(
                CK_FUNCTION_LIST *m,
                CK_SESSION_HANDLE session,
                const CK_TOKEN_INFO *token_info,
                const char *token_label,
                const void *pin,
                size_t pin_size) {
 
        CK_RV rv;
        int r;
 
        assert(m);
        assert(token_info);
 
        r = dlopen_p11kit(LOG_DEBUG);
        if (r < 0)
                return r;
 
        if (FLAGS_SET(token_info->flags, CKF_PROTECTED_AUTHENTICATION_PATH)) {
                rv = m->C_Login(session, CKU_USER, NULL, 0);
                if (rv != CKR_OK)
                        return log_error_errno(SYNTHETIC_ERRNO(EIO),
                                               "Failed to log into security token '%s': %s", token_label, sym_p11_kit_strerror(rv));
 
                log_info("Successfully logged into security token '%s' via protected authentication path.", token_label);
                return 0;
        }
 
        if (!FLAGS_SET(token_info->flags, CKF_LOGIN_REQUIRED)) {
                log_info("No login into security token '%s' required.", token_label);
                return 0;
        }
 
        if (!pin)
                return -ENOANO;
 
        rv = m->C_Login(session, CKU_USER, (CK_UTF8CHAR*) pin, pin_size);
        if (rv == CKR_OK)  {
                log_info("Successfully logged into security token '%s'.", token_label);
                return 0;
        }
 
        if (rv == CKR_PIN_LOCKED)
                return log_error_errno(SYNTHETIC_ERRNO(EPERM),
                                       "PIN has been locked, please reset PIN of security token '%s'.", token_label);
        if (!IN_SET(rv, CKR_PIN_INCORRECT, CKR_PIN_LEN_RANGE))
                return log_error_errno(SYNTHETIC_ERRNO(EIO),
                                       "Failed to log into security token '%s': %s", token_label, sym_p11_kit_strerror(rv));
 
        return log_notice_errno(SYNTHETIC_ERRNO(ENOLCK),
                                "PIN for token '%s' is incorrect, please try again.",
                                token_label);
}
 
int pkcs11_token_login(
                CK_FUNCTION_LIST *m,
                CK_SESSION_HANDLE session,
                CK_SLOT_ID slotid,
                const CK_TOKEN_INFO *token_info,
                const char *friendly_name,
                const char *ask_password_icon,
                const char *ask_password_keyring,
                const char *ask_password_credential,
                usec_t until,
                AskPasswordFlags ask_password_flags,
                char **ret_used_pin) {
 
        _cleanup_free_ char *token_uri_string = NULL, *token_uri_escaped = NULL, *id = NULL, *token_label = NULL;
        _cleanup_(p11_kit_uri_freep) P11KitUri *token_uri = NULL;
        CK_TOKEN_INFO updated_token_info;
        int uri_result, r;
        CK_RV rv;
 
        assert(m);
        assert(token_info);
 
        r = dlopen_p11kit(LOG_DEBUG);
        if (r < 0)
                return r;
 
        token_label = pkcs11_token_label(token_info);
        if (!token_label)
                return log_oom();
 
        token_uri = uri_from_token_info(token_info);
        if (!token_uri)
                return log_oom();
 
        uri_result = sym_p11_kit_uri_format(token_uri, P11_KIT_URI_FOR_ANY, &token_uri_string);
        if (uri_result != P11_KIT_URI_OK)
                return log_warning_errno(SYNTHETIC_ERRNO(EAGAIN), "Failed to format slot URI: %s", sym_p11_kit_uri_message(uri_result));
 
        r = pkcs11_token_login_by_pin(m, session, token_info, token_label, /* pin= */ NULL, 0);
        if (r == 0 && ret_used_pin)
                *ret_used_pin = NULL;
 
        if (r != -ENOANO) /* pin required */
                return r;
 
        token_uri_escaped = cescape(token_uri_string);
        if (!token_uri_escaped)
                return log_oom();
 
        id = strjoin("pkcs11:", token_uri_escaped);
        if (!id)
                return log_oom();
 
        for (unsigned tries = 0; tries < 3; tries++) {
                _cleanup_strv_free_erase_ char **passwords = NULL;
                _cleanup_(erase_and_freep) char *envpin = NULL;
 
                r = getenv_steal_erase("PIN", &envpin);
                if (r < 0)
                        return log_error_errno(r, "Failed to acquire PIN from environment: %m");
                if (r > 0) {
                        passwords = strv_new(envpin);
                        if (!passwords)
                                return log_oom();
 
                } else if (FLAGS_SET(ask_password_flags, ASK_PASSWORD_HEADLESS))
                        return log_error_errno(SYNTHETIC_ERRNO(ENOPKG), "PIN querying disabled via 'headless' option. Use the 'PIN' environment variable.");
                else {
                        _cleanup_free_ char *text = NULL;
 
                        if (FLAGS_SET(token_info->flags, CKF_USER_PIN_FINAL_TRY))
                                r = asprintf(&text,
                                             "Please enter correct PIN for security token '%s' in order to unlock %s (final try):",
                                             token_label, friendly_name);
                        else if (FLAGS_SET(token_info->flags, CKF_USER_PIN_COUNT_LOW))
                                r = asprintf(&text,
                                             "PIN has been entered incorrectly previously, please enter correct PIN for security token '%s' in order to unlock %s:",
                                             token_label, friendly_name);
                        else if (tries == 0)
                                r = asprintf(&text,
                                             "Please enter PIN for security token '%s' in order to unlock %s:",
                                             token_label, friendly_name);
                        else
                                r = asprintf(&text,
                                             "Please enter PIN for security token '%s' in order to unlock %s (try #%u):",
                                             token_label, friendly_name, tries+1);
                        if (r < 0)
                                return log_oom();
 
                        AskPasswordRequest req = {
                                .tty_fd = -EBADF,
                                .message = text,
                                .icon = ask_password_icon,
                                .id = id,
                                .keyring = ask_password_keyring,
                                .credential = ask_password_credential,
                                .until = until,
                                .hup_fd = -EBADF,
                        };
 
                        /* We never cache PINs, simply because it's fatal if we use wrong PINs, since usually there are only 3 tries */
                        r = ask_password_auto(&req, ask_password_flags, &passwords);
                        if (r < 0)
                                return log_error_errno(r, "Failed to query PIN for security token '%s': %m", token_label);
                }
 
                STRV_FOREACH(i, passwords) {
                        r = pkcs11_token_login_by_pin(m, session, token_info, token_label, *i, strlen(*i));
                        if (r == 0 && ret_used_pin) {
                                char *c;
 
                                c = strdup(*i);
                                if (!c)
                                        return log_oom();
 
                                *ret_used_pin = c;
                        }
 
                        if (r != -ENOLCK)
                                return r;
 
                        /* Refresh the token info, so that we can prompt knowing the new flags if they changed. */
                        rv = m->C_GetTokenInfo(slotid, &updated_token_info);
                        if (rv != CKR_OK)
                                return log_error_errno(SYNTHETIC_ERRNO(EIO),
                                                       "Failed to acquire updated security token information for slot %lu: %s",
                                                       slotid, sym_p11_kit_strerror(rv));
 
                        token_info = &updated_token_info;
                }
        }
 
        return log_error_errno(SYNTHETIC_ERRNO(EPERM), "Too many attempts to log into token '%s'.", token_label);
}
 
#if HAVE_OPENSSL
static int read_public_key_info(
                CK_FUNCTION_LIST *m,
                CK_SESSION_HANDLE session,
                CK_OBJECT_HANDLE object,
                EVP_PKEY **ret_pkey) {
 
        assert(ret_pkey);
 
        CK_ATTRIBUTE attribute = { CKA_PUBLIC_KEY_INFO, NULL_PTR, 0 };
        _cleanup_(EVP_PKEY_freep) EVP_PKEY *pkey = NULL;
        CK_RV rv;
 
        rv = m->C_GetAttributeValue(session, object, &attribute, 1);
        if (rv != CKR_OK)
                return log_debug_errno(SYNTHETIC_ERRNO(EINVAL),
                                "Failed to get size of CKA_PUBLIC_KEY_INFO: %s", sym_p11_kit_strerror(rv));
 
        if (attribute.ulValueLen == 0)
                return log_debug_errno(SYNTHETIC_ERRNO(ENOENT), "CKA_PUBLIC_KEY_INFO is empty");
 
        _cleanup_free_ void *buffer = malloc(attribute.ulValueLen);
        if (!buffer)
                return log_oom_debug();
 
        attribute.pValue = buffer;
 
        rv = m->C_GetAttributeValue(session, object, &attribute, 1);
        if (rv != CKR_OK)
                return log_debug_errno(SYNTHETIC_ERRNO(EIO),
                        "Failed to read CKA_PUBLIC_KEY_INFO: %s", sym_p11_kit_strerror(rv));
 
        const unsigned char *value = attribute.pValue;
        pkey = sym_d2i_PUBKEY(NULL, &value, attribute.ulValueLen);
        if (!pkey)
                return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG), "Failed to parse CKA_PUBLIC_KEY_INFO");
 
        *ret_pkey = TAKE_PTR(pkey);
        return 0;
}
 
int pkcs11_token_read_public_key(
                CK_FUNCTION_LIST *m,
                CK_SESSION_HANDLE session,
                CK_OBJECT_HANDLE object,
                EVP_PKEY **ret_pkey) {
 
        _cleanup_(EVP_PKEY_freep) EVP_PKEY *pkey = NULL;
        CK_RV rv;
        int r;
 
        assert(ret_pkey);
 
        r = dlopen_libcrypto(LOG_DEBUG);
        if (r < 0)
                return r;
 
        r = read_public_key_info(m, session, object, &pkey);
        if (r >= 0) {
                *ret_pkey = TAKE_PTR(pkey);
                return 0;
        }
 
        CK_KEY_TYPE key_type;
        CK_ATTRIBUTE attribute = { CKA_KEY_TYPE, &key_type, sizeof(key_type) };
 
        rv = m->C_GetAttributeValue(session, object, &attribute, 1);
        if (rv != CKR_OK)
                return log_debug_errno(SYNTHETIC_ERRNO(EIO),
                                "Failed to get CKA_KEY_TYPE of a public key: %s", sym_p11_kit_strerror(rv));
 
        switch (key_type) {
        case CKK_RSA: {
                CK_ATTRIBUTE rsa_attributes[] = {
                        { CKA_MODULUS,         NULL_PTR, 0 },
                        { CKA_PUBLIC_EXPONENT, NULL_PTR, 0 },
                };
 
                rv = m->C_GetAttributeValue(session, object, rsa_attributes, ELEMENTSOF(rsa_attributes));
                if (rv != CKR_OK)
                        return log_debug_errno(SYNTHETIC_ERRNO(EIO),
                                "Failed to get size of attributes of an RSA public key: %s", sym_p11_kit_strerror(rv));
 
                if (rsa_attributes[0].ulValueLen == 0)
                        return log_debug_errno(SYNTHETIC_ERRNO(EINVAL), "An RSA public key has empty CKA_MODULUS.");
 
                _cleanup_free_ void *modulus = malloc(rsa_attributes[0].ulValueLen);
                if (!modulus)
                        return log_oom_debug();
 
                rsa_attributes[0].pValue = modulus;
 
                if (rsa_attributes[1].ulValueLen == 0)
                        return log_debug_errno(SYNTHETIC_ERRNO(EINVAL), "An RSA public key has empty CKA_PUBLIC_EXPONENT.");
 
                _cleanup_free_ void *public_exponent = malloc(rsa_attributes[1].ulValueLen);
                if (!public_exponent)
                        return log_oom_debug();
 
                rsa_attributes[1].pValue = public_exponent;
 
                rv = m->C_GetAttributeValue(session, object, rsa_attributes, ELEMENTSOF(rsa_attributes));
                if (rv != CKR_OK)
                        return log_debug_errno(SYNTHETIC_ERRNO(EIO),
                                "Failed to get attributes of an RSA public key: %s", sym_p11_kit_strerror(rv));
 
                size_t n_size = rsa_attributes[0].ulValueLen, e_size = rsa_attributes[1].ulValueLen;
                r = rsa_pkey_from_n_e(rsa_attributes[0].pValue, n_size, rsa_attributes[1].pValue, e_size, &pkey);
                if (r < 0)
                        return log_debug_errno(r, "Failed to create an EVP_PKEY from RSA parameters.");
 
                break;
        }
        case CKK_EC: {
                CK_ATTRIBUTE ec_attributes[] = {
                        { CKA_EC_PARAMS, NULL_PTR, 0 },
                        { CKA_EC_POINT,  NULL_PTR, 0 },
                };
 
                rv = m->C_GetAttributeValue(session, object, ec_attributes, ELEMENTSOF(ec_attributes));
                if (rv != CKR_OK)
                        return log_debug_errno(SYNTHETIC_ERRNO(EIO),
                                "Failed to get size of attributes of an EC public key: %s", sym_p11_kit_strerror(rv));
 
                if (ec_attributes[0].ulValueLen == 0)
                        return log_debug_errno(SYNTHETIC_ERRNO(EINVAL), "An EC public key has empty CKA_EC_PARAMS.");
 
                _cleanup_free_ void *ec_group = malloc(ec_attributes[0].ulValueLen);
                if (!ec_group)
                        return log_oom_debug();
 
                ec_attributes[0].pValue = ec_group;
 
                if (ec_attributes[1].ulValueLen == 0)
                        return log_debug_errno(SYNTHETIC_ERRNO(EINVAL), "An EC public key has empty CKA_EC_POINT.");
 
                _cleanup_free_ void *ec_point = malloc(ec_attributes[1].ulValueLen);
                if (!ec_point)
                        return log_oom_debug();
 
                ec_attributes[1].pValue = ec_point;
 
                rv = m->C_GetAttributeValue(session, object, ec_attributes, ELEMENTSOF(ec_attributes));
                if (rv != CKR_OK)
                        return log_debug_errno(SYNTHETIC_ERRNO(EIO),
                                "Failed to get attributes of an EC public key: %s", sym_p11_kit_strerror(rv));
 
                _cleanup_(EC_GROUP_freep) EC_GROUP *group = NULL;
                _cleanup_(ASN1_OCTET_STRING_freep) ASN1_OCTET_STRING *os = NULL;
 
                const unsigned char *ec_params_value = ec_attributes[0].pValue;
                group = sym_d2i_ECPKParameters(NULL, &ec_params_value, ec_attributes[0].ulValueLen);
                if (!group)
                        return log_debug_errno(SYNTHETIC_ERRNO(EINVAL), "Unable to decode CKA_EC_PARAMS.");
 
                const unsigned char *ec_point_value = ec_attributes[1].pValue;
                os = sym_d2i_ASN1_OCTET_STRING(NULL, &ec_point_value, ec_attributes[1].ulValueLen);
                if (!os)
                        return log_debug_errno(SYNTHETIC_ERRNO(EINVAL), "Unable to decode CKA_EC_POINT.");
 
                _cleanup_(EVP_PKEY_CTX_freep) EVP_PKEY_CTX *ctx = sym_EVP_PKEY_CTX_new_from_name(NULL, "EC", NULL);
                if (!ctx)
                        return log_openssl_errors(LOG_DEBUG, "Failed to create an EVP_PKEY_CTX for EC.");
 
                if (sym_EVP_PKEY_fromdata_init(ctx) != 1)
                        return log_openssl_errors(LOG_DEBUG, "Failed to init an EVP_PKEY_CTX for EC.");
 
                OSSL_PARAM ec_params[8] = {
                        /* We need to drop the const from the data param, because ec_params is
                         * modified below. But we'll not modify ec_params[0]. */
                        OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_PUB_KEY,
                                                (unsigned char *) sym_ASN1_STRING_get0_data(os),
                                                sym_ASN1_STRING_length(os)),
                };
 
                _cleanup_free_ void *order = NULL, *p = NULL, *a = NULL, *b = NULL, *generator = NULL;
                size_t order_size, p_size, a_size, b_size, generator_size;
 
                int nid = sym_EC_GROUP_get_curve_name(group);
                if (nid != NID_undef) {
                        const char* name = sym_OSSL_EC_curve_nid2name(nid);
                        ec_params[1] = sym_OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_GROUP_NAME, (char*)name, strlen(name));
                        ec_params[2] = sym_OSSL_PARAM_construct_end();
                } else {
                        const char *field_type = sym_EC_GROUP_get_field_type(group) == NID_X9_62_prime_field ?
                                "prime-field" : "characteristic-two-field";
 
                        const BIGNUM *bn_order = sym_EC_GROUP_get0_order(group);
 
                        _cleanup_(BN_CTX_freep) BN_CTX *bnctx = sym_BN_CTX_new();
                        if (!bnctx)
                                return log_oom_debug();
 
                        _cleanup_(BN_freep) BIGNUM *bn_p = sym_BN_new();
                        if (!bn_p)
                                return log_oom_debug();
 
                        _cleanup_(BN_freep) BIGNUM *bn_a = sym_BN_new();
                        if (!bn_a)
                                return log_oom_debug();
 
                        _cleanup_(BN_freep) BIGNUM *bn_b = sym_BN_new();
                        if (!bn_b)
                                return log_oom_debug();
 
                        if (sym_EC_GROUP_get_curve(group, bn_p, bn_a, bn_b, bnctx) != 1)
                                return log_openssl_errors(LOG_DEBUG, "Failed to extract EC parameters from EC_GROUP.");
 
                        order_size = sym_BN_num_bytes(bn_order);
                        p_size = sym_BN_num_bytes(bn_p);
                        a_size = sym_BN_num_bytes(bn_a);
                        b_size = sym_BN_num_bytes(bn_b);
 
                        order = malloc(order_size);
                        if (!order)
                                return log_oom_debug();
 
                        p = malloc(p_size);
                        if (!p)
                                return log_oom_debug();
 
                        a = malloc(a_size);
                        if (!a)
                                return log_oom_debug();
 
                        b = malloc(b_size);
                        if (!b)
                                return log_oom_debug();
 
                        if (sym_BN_bn2nativepad(bn_order, order, order_size) <= 0 ||
                            sym_BN_bn2nativepad(bn_p, p, p_size) <= 0 ||
                            sym_BN_bn2nativepad(bn_a, a, a_size) <= 0 ||
                            sym_BN_bn2nativepad(bn_b, b, b_size) <= 0 )
                                return log_openssl_errors(LOG_DEBUG, "Failed to store EC parameters in native byte order.");
 
                        const EC_POINT *point_gen = sym_EC_GROUP_get0_generator(group);
                        generator_size = sym_EC_POINT_point2oct(group, point_gen, POINT_CONVERSION_UNCOMPRESSED, NULL, 0, bnctx);
                        if (generator_size == 0)
                                return log_openssl_errors(LOG_DEBUG, "Failed to determine size of a EC generator.");
 
                        generator = malloc(generator_size);
                        if (!generator)
                                return log_oom_debug();
 
                        generator_size = sym_EC_POINT_point2oct(group, point_gen, POINT_CONVERSION_UNCOMPRESSED, generator, generator_size, bnctx);
                        if (generator_size == 0)
                                return log_openssl_errors(LOG_DEBUG, "Failed to convert a EC generator to octet string.");
 
                        ec_params[1] = sym_OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_EC_FIELD_TYPE, (char*)field_type, strlen(field_type));
                        ec_params[2] = sym_OSSL_PARAM_construct_octet_string(OSSL_PKEY_PARAM_EC_GENERATOR, generator, generator_size);
                        ec_params[3] = sym_OSSL_PARAM_construct_BN(OSSL_PKEY_PARAM_EC_ORDER, order, order_size);
                        ec_params[4] = sym_OSSL_PARAM_construct_BN(OSSL_PKEY_PARAM_EC_P, p, p_size);
                        ec_params[5] = sym_OSSL_PARAM_construct_BN(OSSL_PKEY_PARAM_EC_A, a, a_size);
                        ec_params[6] = sym_OSSL_PARAM_construct_BN(OSSL_PKEY_PARAM_EC_B, b, b_size);
                        ec_params[7] = sym_OSSL_PARAM_construct_end();
                }
 
                if (sym_EVP_PKEY_fromdata(ctx, &pkey, EVP_PKEY_PUBLIC_KEY, ec_params) != 1)
                        return log_openssl_errors(LOG_DEBUG, "Failed to create EVP_PKEY from EC parameters.");
                break;
        }
        default:
                return log_debug_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "Unsupported type of public key: %lu", key_type);
        }
 
        *ret_pkey = TAKE_PTR(pkey);
        return 0;
}
 
int pkcs11_token_read_x509_certificate(
                CK_FUNCTION_LIST *m,
                CK_SESSION_HANDLE session,
                CK_OBJECT_HANDLE object,
                X509 **ret_cert) {
 
        CK_ATTRIBUTE attribute = {
                .type = CKA_VALUE
        };
        CK_RV rv;
        int r;
 
        assert(ret_cert);
 
        r = dlopen_p11kit(LOG_DEBUG);
        if (r < 0)
                return r;
 
        rv = m->C_GetAttributeValue(session, object, &attribute, 1);
        if (rv != CKR_OK)
                return log_debug_errno(SYNTHETIC_ERRNO(EIO),
                                       "Failed to read X.509 certificate size off token: %s", sym_p11_kit_strerror(rv));
 
        _cleanup_free_ void *buffer = malloc(attribute.ulValueLen);
        if (!buffer)
                return log_oom_debug();
 
        attribute.pValue = buffer;
 
        rv = m->C_GetAttributeValue(session, object, &attribute, 1);
        if (rv != CKR_OK)
                return log_debug_errno(SYNTHETIC_ERRNO(EIO),
                                       "Failed to read X.509 certificate data off token: %s", sym_p11_kit_strerror(rv));
 
        r = dlopen_libcrypto(LOG_DEBUG);
        if (r < 0)
                return r;
 
        const unsigned char *p = attribute.pValue;
        _cleanup_(X509_freep) X509 *x509 = sym_d2i_X509(NULL, &p, attribute.ulValueLen);
        if (!x509)
                return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG), "Failed to parse X.509 certificate.");
 
        const X509_NAME *name = sym_X509_get_subject_name(x509);
        if (!name)
                return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG), "Failed to acquire X.509 subject name.");
 
        _cleanup_free_ char *t = sym_X509_NAME_oneline(name, NULL, 0);
        if (!t)
                return log_openssl_errors(LOG_DEBUG, "Failed to format X.509 subject name as string.");
 
        log_debug("Using X.509 certificate issued for '%s'.", t);
 
        *ret_cert = TAKE_PTR(x509);
        return 0;
}
#endif
 
int pkcs11_token_find_private_key(
                CK_FUNCTION_LIST *m,
                CK_SESSION_HANDLE session,
                P11KitUri *search_uri,
                CK_OBJECT_HANDLE *ret_object) {
 
        bool found_class = false;
        _cleanup_free_ CK_ATTRIBUTE *attributes_buffer = NULL;
        CK_KEY_TYPE key_type;
        CK_BBOOL decrypt_value, derive_value;
        CK_ATTRIBUTE optional_attributes[] = {
                { CKA_KEY_TYPE, &key_type,      sizeof(key_type)      },
                { CKA_DECRYPT,  &decrypt_value, sizeof(decrypt_value) },
                { CKA_DERIVE,   &derive_value,  sizeof(derive_value)  },
        };
        uint8_t n_private_keys = 0;
        CK_OBJECT_HANDLE private_key = CK_INVALID_HANDLE;
        CK_RV rv;
 
        assert(m);
        assert(search_uri);
        assert(ret_object);
 
        CK_ULONG n_attributes;
        CK_ATTRIBUTE *attributes = sym_p11_kit_uri_get_attributes(search_uri, &n_attributes);
        for (CK_ULONG i = 0; i < n_attributes; i++) {
 
                /* We use the URI's included match attributes, but make them more strict. This allows users
                 * to specify a token URL instead of an object URL and the right thing should happen if
                 * there's only one suitable key on the token. */
 
                switch (attributes[i].type) {
                case CKA_CLASS: {
                        if (attributes[i].ulValueLen != sizeof(CK_OBJECT_CLASS))
                                return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Invalid PKCS#11 CKA_CLASS attribute size.");
 
                        CK_OBJECT_CLASS *class = (CK_OBJECT_CLASS*) attributes[i].pValue;
                        if (*class != CKO_PRIVATE_KEY)
                                return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
                                                       "Selected PKCS#11 object is not a private key, refusing.");
 
                        found_class = true;
                        break;
                }}
        }
 
        if (!found_class) {
                /* Hmm, let's slightly extend the attribute list we search for */
                static const CK_OBJECT_CLASS required_class = CKO_PRIVATE_KEY;
 
                attributes_buffer = new(CK_ATTRIBUTE, n_attributes + 1);
                if (!attributes_buffer)
                        return log_oom();
 
                memcpy(attributes_buffer, attributes, sizeof(CK_ATTRIBUTE) * n_attributes);
 
                attributes_buffer[n_attributes++] = (CK_ATTRIBUTE) {
                        .type = CKA_CLASS,
                        .pValue = (CK_OBJECT_CLASS*) &required_class,
                        .ulValueLen = sizeof(required_class),
                };
 
                attributes = attributes_buffer;
        }
 
        rv = m->C_FindObjectsInit(session, attributes, n_attributes);
        if (rv != CKR_OK)
                return log_error_errno(SYNTHETIC_ERRNO(EIO),
                                       "Failed to initialize object find call: %s", sym_p11_kit_strerror(rv));
 
        for (;;) {
                CK_ULONG b;
                CK_OBJECT_HANDLE candidate;
                rv = m->C_FindObjects(session, &candidate, 1, &b);
                if (rv != CKR_OK)
                        return log_error_errno(SYNTHETIC_ERRNO(EIO),
                                "Failed to find objects: %s", sym_p11_kit_strerror(rv));
 
                if (b == 0)
                        break;
 
                optional_attributes[0].ulValueLen = sizeof(key_type);
                optional_attributes[1].ulValueLen = sizeof(decrypt_value);
                optional_attributes[2].ulValueLen = sizeof(derive_value);
 
                rv = m->C_GetAttributeValue(session, candidate, optional_attributes, ELEMENTSOF(optional_attributes));
                if (!IN_SET(rv, CKR_OK, CKR_ATTRIBUTE_TYPE_INVALID))
                        return log_error_errno(SYNTHETIC_ERRNO(EIO),
                                "Failed to get attributes of a found private key: %s", sym_p11_kit_strerror(rv));
 
                if (optional_attributes[0].ulValueLen == CK_UNAVAILABLE_INFORMATION) {
                        log_debug("A found private key does not have CKA_KEY_TYPE, rejecting the key.");
                        continue;
                }
 
                if (key_type == CKK_RSA)
                        if (optional_attributes[1].ulValueLen == CK_UNAVAILABLE_INFORMATION || decrypt_value == CK_FALSE) {
                                log_debug("A found private RSA key can't decrypt, rejecting the key.");
                                continue;
                        }
 
                if (key_type == CKK_EC)
                        if (optional_attributes[2].ulValueLen == CK_UNAVAILABLE_INFORMATION || derive_value == CK_FALSE) {
                                log_debug("A found private EC key can't derive, rejecting the key.");
                                continue;
                        }
 
                n_private_keys++;
                if (n_private_keys > 1)
                        break;
                private_key = candidate;
        }
 
        rv = m->C_FindObjectsFinal(session);
        if (rv != CKR_OK)
                return log_error_errno(SYNTHETIC_ERRNO(EIO),
                        "Failed to finalize object find call: %s", sym_p11_kit_strerror(rv));
 
        if (n_private_keys == 0)
                return log_error_errno(SYNTHETIC_ERRNO(ENOENT),
                        "Failed to find selected private key suitable for decryption or derivation on token.");
 
        if (n_private_keys > 1)
                return log_error_errno(SYNTHETIC_ERRNO(ENOTUNIQ),
                        "Configured private key URI matches multiple keys, refusing.");
 
        *ret_object = private_key;
        return 0;
}
 
static const char* object_class_to_string(CK_OBJECT_CLASS class) {
        switch (class) {
        case CKO_CERTIFICATE:
                return "CKO_CERTIFICATE";
        case CKO_PUBLIC_KEY:
                return "CKO_PUBLIC_KEY";
        case CKO_PRIVATE_KEY:
                return "CKO_PRIVATE_KEY";
        case CKO_SECRET_KEY:
                return "CKO_SECRET_KEY";
        default:
                return NULL;
        }
}
 
/* Returns an object with the given class and the same CKA_ID or CKA_LABEL as prototype */
int pkcs11_token_find_related_object(
                CK_FUNCTION_LIST *m,
                CK_SESSION_HANDLE session,
                CK_OBJECT_HANDLE prototype,
                CK_OBJECT_CLASS class,
                CK_OBJECT_HANDLE *ret_object ) {
 
        _cleanup_free_ void *buffer = NULL;
        CK_ATTRIBUTE attributes[] = {
                { CKA_ID,    NULL_PTR, 0 },
                { CKA_LABEL, NULL_PTR, 0 }
        };
        CK_OBJECT_CLASS search_class = class;
        CK_ATTRIBUTE search_attributes[2] = {
                { CKA_CLASS, &search_class, sizeof(search_class) }
        };
        CK_ULONG n_objects;
        CK_OBJECT_HANDLE objects[2];
        CK_RV rv;
 
        assert(ret_object);
 
        rv = m->C_GetAttributeValue(session, prototype, attributes, ELEMENTSOF(attributes));
        if (!IN_SET(rv, CKR_OK, CKR_ATTRIBUTE_TYPE_INVALID))
                return log_debug_errno(SYNTHETIC_ERRNO(EIO), "Failed to retrieve length of attributes: %s", sym_p11_kit_strerror(rv));
 
        if (attributes[0].ulValueLen != CK_UNAVAILABLE_INFORMATION) {
                buffer = malloc(attributes[0].ulValueLen);
                if (!buffer)
                        return log_oom();
 
                attributes[0].pValue = buffer;
                rv = m->C_GetAttributeValue(session, prototype, &attributes[0], 1);
                if (rv != CKR_OK)
                        return log_debug_errno(SYNTHETIC_ERRNO(EIO),
                                "Failed to retrieve CKA_ID: %s", sym_p11_kit_strerror(rv));
 
                search_attributes[1] = attributes[0];
 
        } else if (attributes[1].ulValueLen != CK_UNAVAILABLE_INFORMATION) {
                buffer = malloc(attributes[1].ulValueLen);
                if (!buffer)
                        return log_oom();
 
                attributes[1].pValue = buffer;
                rv = m->C_GetAttributeValue(session, prototype, &attributes[1], 1);
                if (rv != CKR_OK)
                        return log_debug_errno(SYNTHETIC_ERRNO(EIO),
                                "Failed to retrieve CKA_LABEL: %s", sym_p11_kit_strerror(rv));
 
                search_attributes[1] = attributes[1];
 
        } else
                return log_debug_errno(SYNTHETIC_ERRNO(EINVAL), "The prototype does not have CKA_ID or CKA_LABEL");
 
        rv = m->C_FindObjectsInit(session, search_attributes, 2);
        if (rv != CKR_OK)
                return log_debug_errno(SYNTHETIC_ERRNO(EIO),
                        "Failed to initialize object find call: %s", sym_p11_kit_strerror(rv));
 
        rv = m->C_FindObjects(session, objects, 2, &n_objects);
        if (rv != CKR_OK)
                return log_debug_errno(SYNTHETIC_ERRNO(EIO),
                        "Failed to find objects: %s", sym_p11_kit_strerror(rv));
 
        rv = m->C_FindObjectsFinal(session);
        if (rv != CKR_OK)
                return log_debug_errno(SYNTHETIC_ERRNO(EIO),
                        "Failed to finalize object find call: %s", sym_p11_kit_strerror(rv));
 
         if (n_objects == 0)
                return log_debug_errno(SYNTHETIC_ERRNO(ENOENT),
                        "Failed to find a related object with class %s", object_class_to_string(class));
 
         if (n_objects > 1)
                log_warning("Found multiple related objects with class %s, using the first object.",
                        object_class_to_string(class));
 
        *ret_object = objects[0];
        return 0;
}
 
#if HAVE_OPENSSL
static int ecc_convert_to_compressed(
                CK_FUNCTION_LIST *m,
                CK_SESSION_HANDLE session,
                CK_OBJECT_HANDLE object,
                const void *uncompressed_point,
                size_t uncompressed_point_size,
                void **ret_compressed_point,
                size_t *ret_compressed_point_size) {
 
        _cleanup_free_ void *ec_params_buffer = NULL;
        CK_ATTRIBUTE ec_params_attr = { CKA_EC_PARAMS, NULL_PTR, 0 };
        CK_RV rv;
        int r;
 
        assert(ret_compressed_point);
        assert(ret_compressed_point_size);
 
        rv = m->C_GetAttributeValue(session, object, &ec_params_attr, 1);
        if (!IN_SET(rv, CKR_OK, CKR_ATTRIBUTE_TYPE_INVALID))
                return log_error_errno(SYNTHETIC_ERRNO(EIO),
                        "Failed to retrieve length of CKA_EC_PARAMS: %s", sym_p11_kit_strerror(rv));
 
        if (ec_params_attr.ulValueLen != CK_UNAVAILABLE_INFORMATION) {
                ec_params_buffer = malloc(ec_params_attr.ulValueLen);
                if (!ec_params_buffer)
                        return log_oom();
 
                ec_params_attr.pValue = ec_params_buffer;
                rv = m->C_GetAttributeValue(session, object, &ec_params_attr, 1);
                if (rv != CKR_OK)
                        return log_error_errno(SYNTHETIC_ERRNO(EIO),
                                "Failed to retrieve CKA_EC_PARAMS from a private key: %s", sym_p11_kit_strerror(rv));
        } else {
                CK_OBJECT_HANDLE public_key;
                r = pkcs11_token_find_related_object(m, session, object, CKO_PUBLIC_KEY, &public_key);
                if (r < 0)
                        return log_error_errno(r, "Failed to find a public key for compressing a EC point");
 
                ec_params_attr.ulValueLen = 0;
                rv = m->C_GetAttributeValue(session, public_key, &ec_params_attr, 1);
                if (!IN_SET(rv, CKR_OK, CKR_ATTRIBUTE_TYPE_INVALID))