From 6f6286de99a62ab0d2edf6023f39cfa8db139420 Mon Sep 17 00:00:00 2001 From: Viktor Tarasov Date: Tue, 24 Feb 2015 17:38:39 +0100 Subject: [PATCH] pkcs11: generate EC key: use allocated EC params For internal use allocate and copy the EC params data from the caller's template, rather then use them directly as a pointer in internal public key data. --- src/pkcs11/framework-pkcs15.c | 2 +- src/pkcs11/misc.c | 24 ++++++++++++++++++++++++ src/pkcs11/sc-pkcs11.h | 1 + 3 files changed, 26 insertions(+), 1 deletion(-) diff --git a/src/pkcs11/framework-pkcs15.c b/src/pkcs11/framework-pkcs15.c index d3001658..c5809753 100644 --- a/src/pkcs11/framework-pkcs15.c +++ b/src/pkcs11/framework-pkcs15.c @@ -2766,7 +2766,7 @@ pkcs15_gen_keypair(struct sc_pkcs11_slot *slot, CK_MECHANISM_PTR pMechanism, struct sc_lv_data *der = &keygen_args.prkey_args.key.u.ec.params.der; der->len = sizeof(struct sc_object_id); - rv = attr_find_ptr(pPubTpl, ulPubCnt, CKA_EC_PARAMS, (void **)&der->value, &der->len); + rv = attr_find_and_allocate_ptr(pPubTpl, ulPubCnt, CKA_EC_PARAMS, (void **)&der->value, &der->len); if (rv != CKR_OK) { sc_unlock(p11card->card); return sc_to_cryptoki_error(rc, "C_GenerateKeyPair"); diff --git a/src/pkcs11/misc.c b/src/pkcs11/misc.c index 8aab05cc..a2422db4 100644 --- a/src/pkcs11/misc.c +++ b/src/pkcs11/misc.c @@ -251,6 +251,30 @@ CK_RV attr_find2(CK_ATTRIBUTE_PTR pTemp1, CK_ULONG ulCount1, return rv; } +CK_RV attr_find_and_allocate_ptr(CK_ATTRIBUTE_PTR pTemplate, CK_ULONG ulCount, CK_ULONG type, void **out, size_t *out_len) +{ + void *ptr; + size_t len; + CK_RV rv; + + if (!out || !out_len) + return CKR_ARGUMENTS_BAD; + len = *out_len; + + rv = attr_find_ptr(pTemplate, ulCount, type, &ptr, &len); + if (rv != CKR_OK) + return rv; + + *out = calloc(1, len); + if (*out == NULL) + return CKR_HOST_MEMORY; + + memcpy(*out, ptr, len); + *out_len = len; + + return CKR_OK; +} + CK_RV attr_find_ptr(CK_ATTRIBUTE_PTR pTemplate, CK_ULONG ulCount, CK_ULONG type, void **ptr, size_t * sizep) { unsigned int n; diff --git a/src/pkcs11/sc-pkcs11.h b/src/pkcs11/sc-pkcs11.h index 3783fde8..9bac0630 100644 --- a/src/pkcs11/sc-pkcs11.h +++ b/src/pkcs11/sc-pkcs11.h @@ -369,6 +369,7 @@ CK_RV attr_find(CK_ATTRIBUTE_PTR, CK_ULONG, CK_ULONG, void *, size_t *); CK_RV attr_find2(CK_ATTRIBUTE_PTR, CK_ULONG, CK_ATTRIBUTE_PTR, CK_ULONG, CK_ULONG, void *, size_t *); CK_RV attr_find_ptr(CK_ATTRIBUTE_PTR, CK_ULONG, CK_ULONG, void **, size_t *); +CK_RV attr_find_and_allocate_ptr(CK_ATTRIBUTE_PTR, CK_ULONG, CK_ULONG, void **, size_t *); CK_RV attr_find_var(CK_ATTRIBUTE_PTR, CK_ULONG, CK_ULONG, void *, size_t *); CK_RV attr_extract(CK_ATTRIBUTE_PTR, void *, size_t *);