Skip to content

Commit

Permalink
debug
Browse files Browse the repository at this point in the history
Signed-off-by: Zoltan Fridrich <[email protected]>
  • Loading branch information
ZoltanFridrich committed Sep 15, 2023
1 parent 938d349 commit 3238000
Showing 1 changed file with 138 additions and 8 deletions.
146 changes: 138 additions & 8 deletions p11-kit/generate-keypair.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@
#include <stdlib.h>
#include <string.h>

//TODO
#include <stdio.h>
#include "constants.h"

#ifdef ENABLE_NLS
#include <libintl.h>
#define _(x) dgettext(PACKAGE_NAME, x)
Expand Down Expand Up @@ -147,6 +151,85 @@ check_args (CK_MECHANISM_TYPE type,
return true;
}

static bool
get_templates (const char *label,
CK_MECHANISM_TYPE type,
const CK_ULONG *bits,
const uint8_t *ec_params,
size_t ec_params_len,
CK_ATTRIBUTE *pubkey,
CK_ULONG *pubkey_len,
CK_ATTRIBUTE *privkey,
CK_ULONG *privkey_len)
{
static const CK_BBOOL TVAL = CK_TRUE, FVAL = CK_FALSE;
int i = 0, j = 0;

pubkey[i].type = privkey[j].type = CKA_TOKEN;
pubkey[i].pValue = privkey[j].pValue = (void *)&TVAL;
pubkey[i].ulValueLen = privkey[j].ulValueLen = sizeof (CK_BBOOL);
++i; ++j;

pubkey[i].type = privkey[j].type = CKA_PRIVATE;
pubkey[i].pValue = (void *)&FVAL; privkey[j].pValue = (void *)&TVAL;
pubkey[i].ulValueLen = privkey[j].ulValueLen = sizeof (CK_BBOOL);
++i; ++j;

pubkey[i].type = privkey[j].type = CKA_SIGN;
pubkey[i].pValue = privkey[j].pValue = (void *)&TVAL;
pubkey[i].ulValueLen = privkey[j].ulValueLen = sizeof (CK_BBOOL);
++i; ++j;

pubkey[i].type = privkey[j].type = CKA_VERIFY;
pubkey[i].pValue = privkey[j].pValue = (void *)&TVAL;
pubkey[i].ulValueLen = privkey[j].ulValueLen = sizeof (CK_BBOOL);
++i; ++j;

if (label != NULL) {
pubkey[i].type = privkey[j].type = CKA_LABEL;
pubkey[i].pValue = privkey[j].pValue = (void *)label;
pubkey[i].ulValueLen = privkey[j].ulValueLen = strlen (label);
++i; ++j;
}

switch (type) {
#ifdef P11_KIT_TESTABLE
case CKM_MOCK_GENERATE:
break;
#endif
case CKM_RSA_PKCS_KEY_PAIR_GEN:
privkey[j].type = CKA_DECRYPT;
privkey[j].pValue = (void *)&TVAL;
privkey[j].ulValueLen = sizeof (CK_BBOOL);
++j;

pubkey[i].type = CKA_ENCRYPT;
pubkey[i].pValue = (void *)&TVAL;
pubkey[i].ulValueLen = sizeof (CK_BBOOL);
++i;

pubkey[i].type = CKA_MODULUS_BITS;
pubkey[i].pValue = (void *)bits;
pubkey[i].ulValueLen = sizeof (CK_ULONG);
++i;
break;
case CKM_ECDSA_KEY_PAIR_GEN:
case CKM_EC_EDWARDS_KEY_PAIR_GEN:
pubkey[i].type = CKA_EC_PARAMS;
pubkey[i].pValue = (void *)ec_params;
pubkey[i].ulValueLen = ec_params_len;
++i;
break;
default:
return false;
}

*pubkey_len = i;
*privkey_len = j;

return true;
}
/*
static bool
get_templates (const char *label,
CK_MECHANISM_TYPE type,
Expand Down Expand Up @@ -239,7 +322,7 @@ get_templates (const char *label,
return false;
}

*/
static int
generate_keypair (const char *token_str,
const char *label,
Expand All @@ -257,11 +340,12 @@ generate_keypair (const char *token_str,
CK_FUNCTION_LIST *module = NULL;
CK_SESSION_HANDLE session = 0;
CK_SLOT_ID slot = 0;
CK_ATTRIBUTE *pubkey = NULL, *privkey = NULL;
CK_ATTRIBUTE pubkey[8], privkey[8];
CK_ULONG pubkey_len = 0, privkey_len = 0;
CK_OBJECT_HANDLE pubkey_obj, privkey_obj;

if (!get_templates (label, mechanism.mechanism, bits,
ec_params, ec_params_len, &pubkey, &privkey)) {
if (!get_templates (label, mechanism.mechanism, &bits, ec_params, ec_params_len,
pubkey, &pubkey_len, privkey, &privkey_len)) {
p11_message (_("failed to create key templates"));
goto cleanup;
}
Expand Down Expand Up @@ -323,9 +407,55 @@ generate_keypair (const char *token_str,
}
}

// TODO remove
/*
printf ("session = %lu\n\n", session);
printf ("mechanism.mechanism = %s\n", p11_constant_nick (p11_constant_mechanisms, mechanism.mechanism));
printf ("mechanism.pParameter = %s\n", (char *)mechanism.pParameter);
printf ("mechanism.ulParameterLen = %lu\n\n", mechanism.ulParameterLen);
printf ("pubkey =\n");
CK_ULONG k = p11_attrs_count (pubkey);
for (int i = 0; i < k; ++i) {
printf ("type = %s\n", p11_constant_nick (p11_constant_types, pubkey[i].type));
printf ("pValue = ");
if (pubkey[i].type == CKA_LABEL) {
for (int j = 0; j < pubkey[i].ulValueLen; ++j)
printf ("%c", ((char *)pubkey[i].pValue)[j]);
} else if (pubkey[i].type == CKA_MODULUS_BITS) {
printf ("%lu", *((CK_ULONG *)pubkey[i].pValue));
} else {
for (int j = 0; j < pubkey[i].ulValueLen; ++j)
printf ("%02X ", ((unsigned char *)pubkey[i].pValue)[j]);
}
printf ("\n");
printf ("ulValueLen = %lu\n", pubkey[i].ulValueLen);
}
printf ("\npubkey_len = %lu\n\n", k);
printf ("privkey =\n");
k = p11_attrs_count (privkey);
for (int i = 0; i < k; ++i) {
printf ("type = %s\n", p11_constant_nick (p11_constant_types, privkey[i].type));
printf ("pValue = ");
if (privkey[i].type == CKA_LABEL) {
for (int j = 0; j < privkey[i].ulValueLen; ++j)
printf ("%c", ((char *)privkey[i].pValue)[j]);
} else if (privkey[i].type == CKA_MODULUS_BITS) {
printf ("%lu", *((CK_ULONG *)privkey[i].pValue));
} else {
for (int j = 0; j < privkey[i].ulValueLen; ++j)
printf ("%02X ", ((unsigned char *)privkey[i].pValue)[j]);
}
printf ("\n");
printf ("ulValueLen = %lu\n", privkey[i].ulValueLen);
}
printf ("\nprivkey_len = %lu\n", k);
*/
rv = module->C_GenerateKeyPair (session, &mechanism,
pubkey, p11_attrs_count (pubkey),
privkey, p11_attrs_count (privkey),
pubkey, pubkey_len,
privkey, privkey_len,
&pubkey_obj, &privkey_obj);
if (rv != CKR_OK) {
p11_message (_("key-pair generation failed: %s"), p11_kit_strerror (rv));
Expand All @@ -337,8 +467,8 @@ generate_keypair (const char *token_str,
cleanup:
if (session != 0)
module->C_CloseSession (session);
p11_attrs_free (pubkey);
p11_attrs_free (privkey);
// p11_attrs_free (pubkey);
// p11_attrs_free (privkey);
p11_kit_iter_free (iter);
p11_kit_uri_free (uri);
if (modules != NULL)
Expand Down

0 comments on commit 3238000

Please sign in to comment.