Skip to content

Commit

Permalink
x
Browse files Browse the repository at this point in the history
Signed-off-by: Zoltan Fridrich <[email protected]>
  • Loading branch information
ZoltanFridrich committed Sep 14, 2023
1 parent 54d490a commit 40cfc9d
Showing 1 changed file with 95 additions and 0 deletions.
95 changes: 95 additions & 0 deletions p11-kit/generate-keypair.c
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,101 @@ check_args (CK_MECHANISM_TYPE type,
return true;
}

static bool
get_templates (const char *label,
CK_MECHANISM_TYPE type,
const CK_ULONG *bits,
const p11_buffer *ec_params,
CK_ATTRIBUTE **pubkey,
CK_ATTRIBUTE **privkey)
{
CK_BBOOL tval = CK_TRUE, fval = CK_FALSE;
CK_ATTRIBUTE *pub = NULL, *priv = NULL, *tmp;
CK_ATTRIBUTE attr_token = { CKA_TOKEN, &tval, sizeof (tval) };
CK_ATTRIBUTE attr_private_true = { CKA_PRIVATE, &tval, sizeof (tval) };
CK_ATTRIBUTE attr_private_false = { CKA_PRIVATE, &fval, sizeof (fval) };
CK_ATTRIBUTE attr_sign = { CKA_SIGN, &tval, sizeof (tval) };
CK_ATTRIBUTE attr_verify = { CKA_VERIFY, &tval, sizeof (tval) };

tmp = p11_attrs_build (pub, &attr_token, &attr_private_false, &attr_sign, &attr_verify);
if (tmp == NULL) {
p11_message (_("failed to allocate memory"));
goto error;
}
pub = tmp;

tmp = p11_attrs_build (priv, &attr_token, &attr_private_true, &attr_sign, &attr_verify);
if (tmp == NULL) {
p11_message (_("failed to allocate memory"));
goto error;
}
priv = tmp;

if (label != NULL) {
CK_ATTRIBUTE attr_label = { CKA_LABEL, (void *)label, strlen (label) };

tmp = p11_attrs_build (pub, &attr_label);
if (tmp == NULL) {
p11_message (_("failed to allocate memory"));
goto error;
}
pub = tmp;

tmp = p11_attrs_build (priv, &attr_label);
if (tmp == NULL) {
p11_message (_("failed to allocate memory"));
goto error;
}
priv = tmp;
}

switch (type) {
#ifdef P11_KIT_TESTABLE
case CKM_MOCK_GENERATE:
break;
#endif
case CKM_RSA_PKCS_KEY_PAIR_GEN:
CK_ATTRIBUTE attr_decrypt = { CKA_DECRYPT, &tval, sizeof (tval) };
CK_ATTRIBUTE attr_encrypt = { CKA_ENCRYPT, &tval, sizeof (tval) };
CK_ATTRIBUTE attr_modulus = { CKA_MODULUS_BITS, (void *)bits, sizeof (*bits) };

tmp = p11_attrs_build (pub, &attr_encrypt, &attr_modulus);
if (tmp == NULL) {
p11_message (_("failed to allocate memory"));
goto error;
}
pub = tmp;

tmp = p11_attrs_build (priv, &attr_decrypt);
if (tmp == NULL) {
p11_message (_("failed to allocate memory"));
goto error;
}
priv = tmp;
break;
case CKM_ECDSA_KEY_PAIR_GEN:
case CKM_EC_EDWARDS_KEY_PAIR_GEN:
CK_ATTRIBUTE attr_ec_params = { CKA_EC_PARAMS, (void *)bits, sizeof (*bits) },

tmp = p11_attrs_build (pub, &attr_ec_params);
if (tmp == NULL) {
p11_message (_("failed to allocate memory"));
goto error;
}
pub = tmp;
break;
default:
goto error;
}

*pubkey = pub;
*privkey = priv;
return true;
error:
p11_attrs_free (pub);
p11_attrs_free (priv);
return false;
}
static bool
get_templates (const char *label,
CK_MECHANISM_TYPE type,
Expand Down

0 comments on commit 40cfc9d

Please sign in to comment.