libopensc: introduce 'key-params' data type that contains pointer, size and 'free' handler ...
http://www.opensc-project.org/pipermail/opensc-devel/2011-April/016441.html git-svn-id: https://www.opensc-project.org/svnp/opensc/trunk@5413 c6295689-39f2-0310-b995-f0e70906c6a9
This commit is contained in:
parent
08c9328e62
commit
5e50a14b3f
|
@ -180,6 +180,7 @@ sc_pkcs15_free_cert_info
|
|||
sc_pkcs15_free_certificate
|
||||
sc_pkcs15_free_data_info
|
||||
sc_pkcs15_free_data_object
|
||||
sc_pkcs15_free_key_params
|
||||
sc_pkcs15_free_object
|
||||
sc_pkcs15_free_pin_info
|
||||
sc_pkcs15_free_prkey
|
||||
|
|
|
@ -191,13 +191,13 @@ int sc_pkcs15_decode_prkdf_entry(struct sc_pkcs15_card *p15card,
|
|||
obj->type = SC_PKCS15_TYPE_PRKEY_GOSTR3410;
|
||||
assert(info.modulus_length == 0);
|
||||
info.modulus_length = SC_PKCS15_GOSTR3410_KEYSIZE;
|
||||
assert(info.params_len == 0);
|
||||
info.params_len = sizeof(struct sc_pkcs15_keyinfo_gostparams);
|
||||
info.params = malloc(info.params_len);
|
||||
if (info.params == NULL)
|
||||
assert(info.params.len == 0);
|
||||
info.params.len = sizeof(struct sc_pkcs15_keyinfo_gostparams);
|
||||
info.params.data = malloc(info.params.len);
|
||||
if (info.params.data == NULL)
|
||||
SC_FUNC_RETURN(ctx, SC_LOG_DEBUG_NORMAL, SC_ERROR_OUT_OF_MEMORY);
|
||||
assert(sizeof(*keyinfo_gostparams) == info.params_len);
|
||||
keyinfo_gostparams = info.params;
|
||||
assert(sizeof(*keyinfo_gostparams) == info.params.len);
|
||||
keyinfo_gostparams = info.params.data;
|
||||
keyinfo_gostparams->gostr3410 = gostr3410_params[0];
|
||||
keyinfo_gostparams->gostr3411 = gostr3410_params[1];
|
||||
keyinfo_gostparams->gost28147 = gostr3410_params[2];
|
||||
|
@ -209,8 +209,7 @@ int sc_pkcs15_decode_prkdf_entry(struct sc_pkcs15_card *p15card,
|
|||
if (!p15card->app || !p15card->app->ddo.aid.len) {
|
||||
r = sc_pkcs15_make_absolute_path(&p15card->file_app->path, &info.path);
|
||||
if (r < 0) {
|
||||
if (info.params)
|
||||
free(info.params);
|
||||
sc_pkcs15_free_key_params(&info.params);
|
||||
return r;
|
||||
}
|
||||
}
|
||||
|
@ -227,8 +226,7 @@ int sc_pkcs15_decode_prkdf_entry(struct sc_pkcs15_card *p15card,
|
|||
|
||||
obj->data = malloc(sizeof(info));
|
||||
if (obj->data == NULL) {
|
||||
if (info.params)
|
||||
free(info.params);
|
||||
sc_pkcs15_free_key_params(&info.params);
|
||||
SC_FUNC_RETURN(ctx, SC_LOG_DEBUG_NORMAL, SC_ERROR_OUT_OF_MEMORY);
|
||||
}
|
||||
memcpy(obj->data, &info, sizeof(info));
|
||||
|
@ -300,9 +298,9 @@ int sc_pkcs15_encode_prkdf_entry(sc_context_t *ctx,
|
|||
sc_format_asn1_entry(asn1_prkey + 2, &gostr3410_prkey_obj, NULL, 1);
|
||||
sc_format_asn1_entry(asn1_prk_gostr3410_attr + 0, asn1_gostr3410key_attr, NULL, 1);
|
||||
sc_format_asn1_entry(asn1_gostr3410key_attr + 0, &prkey->path, NULL, 1);
|
||||
if (prkey->params_len == sizeof(*keyinfo_gostparams))
|
||||
if (prkey->params.len == sizeof(*keyinfo_gostparams))
|
||||
{
|
||||
keyinfo_gostparams = prkey->params;
|
||||
keyinfo_gostparams = prkey->params.data;
|
||||
sc_format_asn1_entry(asn1_gostr3410key_attr + 1,
|
||||
&keyinfo_gostparams->gostr3410, NULL, 1);
|
||||
sc_format_asn1_entry(asn1_gostr3410key_attr + 2,
|
||||
|
@ -524,7 +522,8 @@ void sc_pkcs15_free_prkey_info(sc_pkcs15_prkey_info_t *key)
|
|||
{
|
||||
if (key->subject.value)
|
||||
free(key->subject.value);
|
||||
if (key->params)
|
||||
free(key->params);
|
||||
|
||||
sc_pkcs15_free_key_params(&key->params);
|
||||
|
||||
free(key);
|
||||
}
|
||||
|
|
|
@ -188,13 +188,13 @@ int sc_pkcs15_decode_pukdf_entry(struct sc_pkcs15_card *p15card,
|
|||
obj->type = SC_PKCS15_TYPE_PUBKEY_GOSTR3410;
|
||||
assert(info.modulus_length == 0);
|
||||
info.modulus_length = SC_PKCS15_GOSTR3410_KEYSIZE;
|
||||
assert(info.params_len == 0);
|
||||
info.params_len = sizeof(struct sc_pkcs15_keyinfo_gostparams);
|
||||
info.params = malloc(info.params_len);
|
||||
if (info.params == NULL)
|
||||
assert(info.params.len == 0);
|
||||
info.params.len = sizeof(struct sc_pkcs15_keyinfo_gostparams);
|
||||
info.params.data = malloc(info.params.len);
|
||||
if (info.params.data == NULL)
|
||||
SC_FUNC_RETURN(ctx, SC_LOG_DEBUG_NORMAL, SC_ERROR_OUT_OF_MEMORY);
|
||||
assert(sizeof(*keyinfo_gostparams) == info.params_len);
|
||||
keyinfo_gostparams = info.params;
|
||||
assert(sizeof(*keyinfo_gostparams) == info.params.len);
|
||||
keyinfo_gostparams = info.params.data;
|
||||
keyinfo_gostparams->gostr3410 = (unsigned int)gostr3410_params[0];
|
||||
keyinfo_gostparams->gostr3411 = (unsigned int)gostr3410_params[1];
|
||||
keyinfo_gostparams->gost28147 = (unsigned int)gostr3410_params[2];
|
||||
|
@ -204,8 +204,7 @@ int sc_pkcs15_decode_pukdf_entry(struct sc_pkcs15_card *p15card,
|
|||
if (!p15card->app || !p15card->app->ddo.aid.len) {
|
||||
r = sc_pkcs15_make_absolute_path(&p15card->file_app->path, &info.path);
|
||||
if (r < 0) {
|
||||
if (info.params)
|
||||
free(info.params);
|
||||
sc_pkcs15_free_key_params(&info.params);
|
||||
return r;
|
||||
}
|
||||
}
|
||||
|
@ -222,8 +221,7 @@ int sc_pkcs15_decode_pukdf_entry(struct sc_pkcs15_card *p15card,
|
|||
|
||||
obj->data = malloc(sizeof(info));
|
||||
if (obj->data == NULL) {
|
||||
if (info.params)
|
||||
free(info.params);
|
||||
sc_pkcs15_free_key_params(&info.params);
|
||||
SC_FUNC_RETURN(ctx, SC_LOG_DEBUG_NORMAL, SC_ERROR_OUT_OF_MEMORY);
|
||||
}
|
||||
memcpy(obj->data, &info, sizeof(info));
|
||||
|
@ -296,9 +294,9 @@ int sc_pkcs15_encode_pukdf_entry(sc_context_t *ctx,
|
|||
sc_format_asn1_entry(asn1_gostr3410_type_attr + 0, asn1_gostr3410key_attr, NULL, 1);
|
||||
|
||||
sc_format_asn1_entry(asn1_gostr3410key_attr + 0, &pubkey->path, NULL, 1);
|
||||
if (pubkey->params_len == sizeof(*keyinfo_gostparams))
|
||||
if (pubkey->params.len == sizeof(*keyinfo_gostparams))
|
||||
{
|
||||
keyinfo_gostparams = pubkey->params;
|
||||
keyinfo_gostparams = pubkey->params.data;
|
||||
sc_format_asn1_entry(asn1_gostr3410key_attr + 1,
|
||||
&keyinfo_gostparams->gostr3410, NULL, 1);
|
||||
sc_format_asn1_entry(asn1_gostr3410key_attr + 2,
|
||||
|
@ -778,8 +776,7 @@ void sc_pkcs15_free_pubkey_info(sc_pkcs15_pubkey_info_t *key)
|
|||
{
|
||||
if (key->subject.value)
|
||||
free(key->subject.value);
|
||||
if (key->params)
|
||||
free(key->params);
|
||||
sc_pkcs15_free_key_params(&key->params);
|
||||
free(key);
|
||||
}
|
||||
|
||||
|
|
|
@ -2213,3 +2213,15 @@ sc_pkcs15_get_guid(struct sc_pkcs15_card *p15card, const struct sc_pkcs15_object
|
|||
return sc_pkcs15_serialize_guid(guid_bin, id.len + serialnr.len, out, out_size);
|
||||
}
|
||||
|
||||
void sc_pkcs15_free_key_params(struct sc_pkcs15_key_params *params)
|
||||
{
|
||||
if (!params)
|
||||
return;
|
||||
if (params->data && params->free_params)
|
||||
params->free_params(params->data);
|
||||
else if (params->data)
|
||||
free(params->data);
|
||||
|
||||
params->data = NULL;
|
||||
}
|
||||
|
||||
|
|
|
@ -319,6 +319,12 @@ struct sc_pkcs15_accessrule {
|
|||
typedef struct sc_pkcs15_accessrule sc_pkcs15_accessrule_t;
|
||||
|
||||
|
||||
struct sc_pkcs15_key_params {
|
||||
void *data;
|
||||
size_t len;
|
||||
void (*free_params)(void *);
|
||||
};
|
||||
|
||||
struct sc_pkcs15_prkey_info {
|
||||
struct sc_pkcs15_id id; /* correlates to public certificate id */
|
||||
unsigned int usage, access_flags;
|
||||
|
@ -331,8 +337,7 @@ struct sc_pkcs15_prkey_info {
|
|||
|
||||
struct sc_pkcs15_der subject;
|
||||
|
||||
void *params;
|
||||
size_t params_len;
|
||||
struct sc_pkcs15_key_params params;
|
||||
|
||||
struct sc_path path;
|
||||
};
|
||||
|
@ -350,8 +355,7 @@ struct sc_pkcs15_pubkey_info {
|
|||
|
||||
struct sc_pkcs15_der subject;
|
||||
|
||||
void *params;
|
||||
size_t params_len;
|
||||
struct sc_pkcs15_key_params params;
|
||||
|
||||
struct sc_path path;
|
||||
};
|
||||
|
@ -592,6 +596,7 @@ int sc_pkcs15_encode_prkey(struct sc_context *,
|
|||
u8 **, size_t *);
|
||||
void sc_pkcs15_erase_prkey(struct sc_pkcs15_prkey *prkey);
|
||||
void sc_pkcs15_free_prkey(struct sc_pkcs15_prkey *prkey);
|
||||
void sc_pkcs15_free_key_params(struct sc_pkcs15_key_params *params);
|
||||
|
||||
int sc_pkcs15_read_data_object(struct sc_pkcs15_card *p15card,
|
||||
const struct sc_pkcs15_data_info *info,
|
||||
|
|
|
@ -2507,9 +2507,9 @@ static CK_RV pkcs15_prkey_get_attribute(struct sc_pkcs11_session *session,
|
|||
attr->ulValueLen = 0;
|
||||
return CKR_OK;
|
||||
case CKA_GOSTR3410_PARAMS:
|
||||
if (prkey->prv_info && prkey->prv_info->params_len)
|
||||
return get_gostr3410_params(prkey->prv_info->params,
|
||||
prkey->prv_info->params_len, attr);
|
||||
if (prkey->prv_info && prkey->prv_info->params.len)
|
||||
return get_gostr3410_params(prkey->prv_info->params.data,
|
||||
prkey->prv_info->params.len, attr);
|
||||
else
|
||||
return CKR_ATTRIBUTE_TYPE_INVALID;
|
||||
case CKA_EC_PARAMS:
|
||||
|
@ -2865,9 +2865,9 @@ static CK_RV pkcs15_pubkey_get_attribute(struct sc_pkcs11_session *session,
|
|||
}
|
||||
break;
|
||||
case CKA_GOSTR3410_PARAMS:
|
||||
if (pubkey->pub_info && pubkey->pub_info->params_len)
|
||||
return get_gostr3410_params(pubkey->pub_info->params,
|
||||
pubkey->pub_info->params_len, attr);
|
||||
if (pubkey->pub_info && pubkey->pub_info->params.len)
|
||||
return get_gostr3410_params(pubkey->pub_info->params.data,
|
||||
pubkey->pub_info->params.len, attr);
|
||||
else
|
||||
return CKR_ATTRIBUTE_TYPE_INVALID;
|
||||
case CKA_EC_PARAMS:
|
||||
|
|
|
@ -1129,13 +1129,13 @@ sc_pkcs15init_init_prkdf(struct sc_pkcs15_card *p15card,
|
|||
key_info->id = keyargs->id;
|
||||
|
||||
if (key->algorithm == SC_ALGORITHM_GOSTR3410) {
|
||||
key_info->params_len = sizeof(*keyinfo_gostparams);
|
||||
key_info->params.len = sizeof(*keyinfo_gostparams);
|
||||
/* FIXME: malloc() call in pkcs15init, but free() call
|
||||
* in libopensc (sc_pkcs15_free_prkey_info) */
|
||||
key_info->params = malloc(key_info->params_len);
|
||||
if (!key_info->params)
|
||||
key_info->params.data = malloc(key_info->params.len);
|
||||
if (!key_info->params.data)
|
||||
LOG_TEST_RET(ctx, SC_ERROR_OUT_OF_MEMORY, "Cannot allocate memory for GOST parameters");
|
||||
keyinfo_gostparams = key_info->params;
|
||||
keyinfo_gostparams = key_info->params.data;
|
||||
keyinfo_gostparams->gostr3410 = keyargs->params.gost.gostr3410;
|
||||
keyinfo_gostparams->gostr3411 = keyargs->params.gost.gostr3411;
|
||||
keyinfo_gostparams->gost28147 = keyargs->params.gost.gost28147;
|
||||
|
@ -1444,13 +1444,13 @@ sc_pkcs15init_store_public_key(struct sc_pkcs15_card *p15card,
|
|||
key_info->modulus_length = keybits;
|
||||
|
||||
if (key.algorithm == SC_ALGORITHM_GOSTR3410) {
|
||||
key_info->params_len = sizeof(*keyinfo_gostparams);
|
||||
key_info->params.len = sizeof(*keyinfo_gostparams);
|
||||
/* FIXME: malloc() call in pkcs15init, but free() call
|
||||
* in libopensc (sc_pkcs15_free_prkey_info) */
|
||||
key_info->params = malloc(key_info->params_len);
|
||||
if (!key_info->params)
|
||||
key_info->params.data = malloc(key_info->params.len);
|
||||
if (!key_info->params.data)
|
||||
return SC_ERROR_OUT_OF_MEMORY;
|
||||
keyinfo_gostparams = key_info->params;
|
||||
keyinfo_gostparams = key_info->params.data;
|
||||
keyinfo_gostparams->gostr3410 = keyargs->params.gost.gostr3410;
|
||||
keyinfo_gostparams->gostr3411 = keyargs->params.gost.gostr3411;
|
||||
keyinfo_gostparams->gost28147 = keyargs->params.gost.gost28147;
|
||||
|
|
|
@ -299,12 +299,12 @@ static int rtecp_create_key(sc_profile_t *profile, sc_pkcs15_card_t *p15card,
|
|||
}
|
||||
if (obj->type == SC_PKCS15_TYPE_PRKEY_GOSTR3410)
|
||||
{
|
||||
if (key_info->params_len < sizeof(int))
|
||||
if (key_info->params.len < sizeof(int))
|
||||
return SC_ERROR_INVALID_ARGUMENTS;
|
||||
if (((int*)key_info->params)[0] < 1
|
||||
|| ((int*)key_info->params)[0] > 3)
|
||||
if (((int*)key_info->params.data)[0] < 1
|
||||
|| ((int*)key_info->params.data)[0] > 3)
|
||||
return SC_ERROR_INVALID_ARGUMENTS;
|
||||
paramset = ((unsigned int*)key_info->params)[0] & 0x03;
|
||||
paramset = ((unsigned int*)key_info->params.data)[0] & 0x03;
|
||||
assert(sizeof(prgkey_prop)/sizeof(prgkey_prop[0]) > 1);
|
||||
assert(sizeof(pbgkey_prop)/sizeof(pbgkey_prop[0]) > 1);
|
||||
prgkey_prop[1] = 0x10 + (paramset << 4);
|
||||
|
|
Loading…
Reference in New Issue