Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

X25519 improvements 2 in PKCS11-tool, PKCS15 routines and tools and openpgp #3090

Open
wants to merge 62 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
Show all changes
62 commits
Select commit Hold shift + click to select a range
dcc7346
pkcs11-tool.c - fix tag len in OID
dengert Dec 8, 2023
c060f7d
pkcs11-tool.c - Incorrect OID for edwards25519
dengert Dec 9, 2023
d85b52c
pkcs11-tool.c - CKK_GENERIC_SECRET do not have CKA_ENCRYPT or CKA_DEC…
dengert Dec 8, 2023
43d58d4
pkcs11-tool.c Add derive key support for CKK_MONTGOMERY
dengert Dec 9, 2023
a633845
pkcs11-tool.c - Update EC type curves
dengert Dec 10, 2023
584e7cc
pkcs11-tool.c - EVP_KEY_X448 not defined in LibreSSL
dengert Jan 21, 2024
2a63099
pkcs11-tool.c calculate size in bits for eddsa and xeddsa CKA_EC_POINT
dengert Jan 21, 2024
f8ce754
pkcs11-tool.c - EC_POINT DER in BIT STRING or OCTET STRING
dengert Jan 29, 2024
f83208b
pkcs11-tool.c - remore wrap/unwrap from template
dengert Apr 17, 2024
556c0cc
pkcs15.h - PKCS11 ecparams are used by CKK_EC, CKK_EDWARDS and CKK_MO…
dengert Dec 9, 2023
e8bbf37
pkcs15-prkey.c pkcs15-pubkey.c - Use common EC params
dengert Dec 9, 2023
732eda2
card-openpgp.c - Use common "ec_pointQ" for EC, EDDSA and XEDDSA
dengert Dec 9, 2023
c9e1de3
pkcs15init/pkcs15-lib.c pkcs15init/pkcs15-openpgp.c - EDDSA and XEDDSA
dengert Dec 10, 2023
a1c1fb6
card-openpgp.c - add SC_ALGORITHM_ONBOARD_KEY_GEN
dengert Dec 10, 2023
ad86f56
pkcs15-pubkey.c update ec curves and map printable strings
dengert Dec 10, 2023
61df246
pkcs15-init.c squash adding additional if statement
dengert Jan 21, 2024
b428eb0
pkcs15-algo.c - add Ed448, X448 and openpgp oids
dengert Jan 15, 2024
5fe507f
OpenPGP - Move mapping of OpenPGP specifix OIDs
dengert Jan 15, 2024
1c8bff8
pkcs15init/pkcs15-opensc.c unused variable
dengert Jan 15, 2024
82b7213
tools/pkcs15-init.c accept and case and convert to correct case
dengert Jan 20, 2024
80effc1
card-openpgp.c card-openpgp.h
dengert Jan 20, 2024
fba9f26
doc/tools/pkcs15-init.1.xml document Curve names for eddsa and xeddsa
dengert Jan 21, 2024
3042e6b
framework-pkcs15.c - Support CKA_PUBKEY_KEY_INFO
dengert Jan 28, 2024
491977c
WIP to accept pkcs11 ec_point encoded in bit string or byte string
dengert Feb 9, 2024
70d2496
pkcs15-pubkey.c make default for CKA_EC_POINT to return old OCTET S…
dengert Feb 16, 2024
1493774
test-oseid.sh use restart-pcscd.sh
dengert Feb 16, 2024
7e84589
Revert "test-oseid.sh use restart-pcscd.sh"
dengert Mar 28, 2024
a4543d4
pkcs15-pubkey.c fix bug found by fuzzer
dengert Mar 28, 2024
58ccef4
pkcs15-pubkey.c - no need to test if key is null before free
dengert Mar 28, 2024
a4265fd
pkcs15-pubkey.c - comments, whitespace and code style
dengert Mar 30, 2024
e0800ed
card-openpgp.c - whitespace and code style
dengert Mar 30, 2024
4d454fc
framework-pkcs15,c - whitespace and code style
dengert Mar 30, 2024
19b56b3
pkcs15-alg.c whitespace and code style
dengert Mar 30, 2024
7f5931e
pkcs15-init.c whitespace and code style
dengert Mar 30, 2024
2dfbe4d
files.html, tools.html - rebuild
dengert Mar 30, 2024
95696f0
pkcs15-pubkey.c - readability of mapped_string
dengert Apr 17, 2024
57c1f6f
card-openpgp.c - renove TODO comments as code was added
dengert Apr 18, 2024
a45ab3e
pkcs15-algo.c - removed comments and added asn1 calls
dengert Apr 18, 2024
064b805
pkcs15-prkey.c - moved comment
dengert Apr 18, 2024
30ec795
pkcs11-tool.c - formating
dengert Apr 19, 2024
8ef8507
pkcs15-pubkey.c - Use sc_asn1_read_tag for printable string
dengert Apr 21, 2024
64a7100
pkcs15-pubkey.c opensc.h - Add SC_ALGORITHM_* to ec_curve_info
dengert Apr 22, 2024
2009bfc
card-openpgp.c card-openpgp.h - Add SC_ALGORITHM_* to ec_curves_open…
dengert Apr 22, 2024
fc7c6d2
card.c - when adding an EC type alg call sc_pkcs15_fix_ec_parameters
dengert Apr 23, 2024
818d26a
pkcs15-pubkey.c - FIXUP order of key_type in sc_curve_info
dengert Apr 23, 2024
be1e3d9
card.c - fix bug in sc_copy_ec_params
dengert Apr 23, 2024
17834c5
pkcs15init/pkcs15-openpgp.c - test id and ec type keys
dengert Apr 24, 2024
97a8d3a
card.c when copying sc_ec_parameters get new key_type too
dengert Apr 24, 2024
9c7c398
pkcs15init/pkcs15-openpgp.c - set key_info.algorithm so EDDSA and XE…
dengert Apr 24, 2024
9b1d102
card-openpgp.c - fix invalid read found by valgrind
dengert Apr 26, 2024
951567b
pkcs15init/pkcs15-lib.c - fix double free
dengert Apr 27, 2024
8f55e45
cardctl.h - add key_type as SC_ALGORITHM_* to sc_cardctl_openpgp_keyg…
dengert Apr 28, 2024
d8546f2
opensc.h - add
dengert May 16, 2024
8550203
opensc.h - add sc_clear_ec_params
dengert May 16, 2024
bb099fb
pkcs11-tool.c - CKK_EC_EDWARDS and CKK_EC_MONTGOMERY improvements
dengert May 16, 2024
5df6fc8
card.c - add sc_clear_ec_params expand sc_card_find_alg
dengert May 16, 2024
3215bcf
pkcs15init/pkcs15-lib.c - Changes for sc_clear_ec_params
dengert May 16, 2024
4856c56
card-openpgp.c pkcs15init/pkcs15-openpgp.c - fixes
dengert May 16, 2024
42b82bf
libopensc.exports - export sc_clear_ec_params
dengert May 16, 2024
d0da85d
framework-pkcs15.c - added support for more 448 size keys
dengert May 16, 2024
1f2ada9
pkcs15-isoApplet.c - use sc_clear_ec_params
dengert May 16, 2024
3fa1d48
pkcs15-algo.c, pkcs15-prkey.c and pkcs15-pubkey.c
dengert May 16, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Prev Previous commit
Next Next commit
pkcs11-tool.c - EC_POINT DER in BIT STRING or OCTET STRING
Accept either encoding.

 On branch X25519-improvements-2

 Changes to be committed:
	modified:   src/tools/pkcs11-tool.c
  • Loading branch information
dengert committed May 17, 2024
commit f8ce754ed52c2a740fd15e82698e138096971b4c
189 changes: 100 additions & 89 deletions src/tools/pkcs11-tool.c
Original file line number Diff line number Diff line change
Expand Up @@ -4871,15 +4871,17 @@ derive_ec_key(CK_SESSION_HANDLE session, CK_OBJECT_HANDLE key, CK_MECHANISM_TYPE
CK_BBOOL _true = TRUE;
CK_BBOOL _false = FALSE;
CK_OBJECT_HANDLE newkey = 0;
//clang-format off
CK_ATTRIBUTE newkey_template[20] = {
{CKA_TOKEN, &_false, sizeof(_false)}, /* session only object */
{CKA_CLASS, &newkey_class, sizeof(newkey_class)},
{CKA_KEY_TYPE, &newkey_type, sizeof(newkey_type)},
{CKA_SENSITIVE, &_false, sizeof(_false)},
{CKA_EXTRACTABLE, &_true, sizeof(_true)},
{CKA_WRAP, &_true, sizeof(_true)},
{CKA_UNWRAP, &_true, sizeof(_true)}
};
{CKA_TOKEN, &_false, sizeof(_false) }, /* session only object */
{CKA_CLASS, &newkey_class, sizeof(newkey_class)},
{CKA_KEY_TYPE, &newkey_type, sizeof(newkey_type) },
{CKA_SENSITIVE, &_false, sizeof(_false) },
{CKA_EXTRACTABLE, &_true, sizeof(_true) },
{CKA_WRAP, &_true, sizeof(_true) },
{CKA_UNWRAP, &_true, sizeof(_true) },
};
//clang-format on
int n_attrs = 7;
CK_ECDH1_DERIVE_PARAMS ecdh_parms;
CK_RV rv;
Expand Down Expand Up @@ -4923,29 +4925,27 @@ derive_ec_key(CK_SESSION_HANDLE session, CK_OBJECT_HANDLE key, CK_MECHANISM_TYPE

key_id = EVP_PKEY_id(pkey);

switch(key_id) {
case EVP_PKEY_EC: /* CKK_EC*/
switch (key_id) {
case EVP_PKEY_EC: /* CKK_EC*/

#if OPENSSL_VERSION_NUMBER < 0x30000000L
eckey = EVP_PKEY_get0_EC_KEY(pkey);
ecpoint = EC_KEY_get0_public_key(eckey);
ecgroup = EC_KEY_get0_group(eckey);
eckey = EVP_PKEY_get0_EC_KEY(pkey);
ecpoint = EC_KEY_get0_public_key(eckey);
ecgroup = EC_KEY_get0_group(eckey);

if (!ecpoint || !ecgroup)
util_fatal("Failed to parse peer EC key from %s", opt_input);
if (!ecpoint || !ecgroup)
util_fatal("Failed to parse peer EC key from %s", opt_input);
#else
if (EVP_PKEY_get_group_name(pkey, name, sizeof(name), &len) != 1
|| (nid = OBJ_txt2nid(name)) == NID_undef
|| (ecgroup = EC_GROUP_new_by_curve_name(nid)) == NULL)
util_fatal("Failed to parse peer EC key from %s", opt_input);
if (EVP_PKEY_get_group_name(pkey, name, sizeof(name), &len) != 1 || (nid = OBJ_txt2nid(name)) == NID_undef || (ecgroup = EC_GROUP_new_by_curve_name(nid)) == NULL)
dengert marked this conversation as resolved.
Show resolved Hide resolved
util_fatal("Failed to parse peer EC key from %s", opt_input);
#endif

/* both eckeys must be same curve */
key_len = (EC_GROUP_get_degree(ecgroup) + 7) / 8;
FILL_ATTR(newkey_template[n_attrs], CKA_VALUE_LEN, &key_len, sizeof(key_len));
n_attrs++;
break;
}
/* both eckeys must be same curve */
key_len = (EC_GROUP_get_degree(ecgroup) + 7) / 8;
FILL_ATTR(newkey_template[n_attrs], CKA_VALUE_LEN, &key_len, sizeof(key_len));
n_attrs++;
break;
}

if (opt_allowed_mechanisms_len > 0) {
FILL_ATTR(newkey_template[n_attrs],
Expand All @@ -4955,28 +4955,28 @@ derive_ec_key(CK_SESSION_HANDLE session, CK_OBJECT_HANDLE key, CK_MECHANISM_TYPE
}

#if OPENSSL_VERSION_NUMBER < 0x30000000L
switch(key_id) {
case EVP_PKEY_EC:
buf_size = EC_POINT_point2oct(ecgroup, ecpoint, POINT_CONVERSION_UNCOMPRESSED, NULL, 0, NULL);
buf = (unsigned char *)malloc(buf_size);
if (buf == NULL)
util_fatal("malloc() failure\n");
buf_size = EC_POINT_point2oct(ecgroup, ecpoint, POINT_CONVERSION_UNCOMPRESSED, buf, buf_size, NULL);
break;
case EVP_PKEY_X25519:
switch (key_id) {
case EVP_PKEY_EC:
buf_size = EC_POINT_point2oct(ecgroup, ecpoint, POINT_CONVERSION_UNCOMPRESSED, NULL, 0, NULL);
buf = (unsigned char *)malloc(buf_size);
if (buf == NULL)
util_fatal("malloc() failure\n");
buf_size = EC_POINT_point2oct(ecgroup, ecpoint, POINT_CONVERSION_UNCOMPRESSED, buf, buf_size, NULL);
break;
case EVP_PKEY_X25519:
#if defined(EVP_PKEY_X448)
case EVP_PKEY_X448:
case EVP_PKEY_X448:
#endif
EVP_PKEY_get_raw_public_key(pkey, NULL, &buf_size);
if (buf_size == 0)
util_fatal("Unable to get of peer key\n");
buf = (unsigned char *)malloc(buf_size);
if (buf == NULL)
util_fatal("malloc() failure\n");
EVP_PKEY_get_raw_public_key(pkey, buf, &buf_size);
break;
default:
util_fatal("Unknown EVP_PKEY_id\n");
EVP_PKEY_get_raw_public_key(pkey, NULL, &buf_size);
if (buf_size == 0)
util_fatal("Unable to get of peer key\n");
buf = (unsigned char *)malloc(buf_size);
if (buf == NULL)
util_fatal("malloc() failure\n");
EVP_PKEY_get_raw_public_key(pkey, buf, &buf_size);
break;
default:
util_fatal("Unknown EVP_PKEY_id\n");
}
#else
EC_GROUP_free(ecgroup);
Expand All @@ -4991,21 +4991,21 @@ derive_ec_key(CK_SESSION_HANDLE session, CK_OBJECT_HANDLE key, CK_MECHANISM_TYPE
#endif

switch (key_id) {
case EVP_PKEY_EC: /* CKK_EC*/
if (mech_mech != CKM_ECDH1_DERIVE && mech_mech != CKM_ECDH1_COFACTOR_DERIVE)
util_fatal("Peer key %s not usable with %s", "CKK_EC", p11_mechanism_to_name(mech_mech));
break;
case EVP_PKEY_X25519: /* "CKK_EC_MONTGOMERY */
case EVP_PKEY_EC: /* CKK_EC*/
if (mech_mech != CKM_ECDH1_DERIVE && mech_mech != CKM_ECDH1_COFACTOR_DERIVE)
util_fatal("Peer key %s not usable with %s", "CKK_EC", p11_mechanism_to_name(mech_mech));
break;
case EVP_PKEY_X25519: /* "CKK_EC_MONTGOMERY */
#if defined(EVP_PKEY_X448)
case EVP_PKEY_X448:
case EVP_PKEY_X448:
#endif
if (mech_mech != CKM_ECDH1_DERIVE)
util_fatal("Peer key %s not usable with %s", "CKK_EC_MONTGOMERY", p11_mechanism_to_name(mech_mech));
break;
default:
util_fatal("Peer key not usable with derive or unknown %i", key_id);
break;
}
if (mech_mech != CKM_ECDH1_DERIVE)
util_fatal("Peer key %s not usable with %s", "CKK_EC_MONTGOMERY", p11_mechanism_to_name(mech_mech));
break;
default:
util_fatal("Peer key not usable with derive or unknown %i", key_id);
break;
}

if (opt_derive_pass_der) {
octet = ASN1_OCTET_STRING_new();
Expand Down Expand Up @@ -5067,10 +5067,10 @@ derive_key(CK_SLOT_ID slot, CK_SESSION_HANDLE session, CK_OBJECT_HANDLE key)
if (!find_mechanism(slot, CKF_DERIVE|opt_allow_sw, NULL, 0, &opt_mechanism))
util_fatal("Derive mechanism not supported");

switch(key_type) {
case CKK_EC:
case CKK_EC_MONTGOMERY:
derived_key = derive_ec_key(session, key, opt_mechanism);
switch (key_type) {
case CKK_EC:
case CKK_EC_MONTGOMERY:
derived_key = derive_ec_key(session, key, opt_mechanism);
break;
default:
util_fatal("Key type %lu does not support derive", key_type);
Expand Down Expand Up @@ -5196,39 +5196,50 @@ show_key(CK_SESSION_HANDLE sess, CK_OBJECT_HANDLE obj)
}
if (pub) {
unsigned char *bytes = NULL;
unsigned long ksize;
unsigned long ksize = 0;
unsigned int n;
unsigned long body_len = 0;

bytes = getEC_POINT(sess, obj, &ksize);
/*
* simple parse of DER BIT STRING 0x03 or OCTET STRING 0x04
* good to 65K bytes
*/
if (ksize > 3 && (bytes[0] == 0x03 || bytes[0] == 0x04)) {
if (bytes[1] <= 127 && ksize == (unsigned long)(bytes[1] + 2)) {
body_len = ksize - 2;
} else if (bytes[1] == 0x81 && size == ((unsigned long)bytes[2] + 3)) {
body_len = ksize - 3;
} else if (bytes[1] == 0x82 && size == ((unsigned long)(bytes[2] << 8) + (unsigned long)bytes[3] + 4)) {
body_len = ksize - 4;
}
}
/* With BIT STRING remove unused bits in last byte indicator */
if (body_len > 0 && bytes[0] == 0x03)
body_len--;

bytes = getEC_POINT(sess, obj, &size);
if (key_type == CKK_EC) {
if (key_type == CKK_EC && body_len > 0) {
/*
* (We only support uncompressed for now)
* Uncompressed EC_POINT is DER OCTET STRING of "04||x||y"
* So a "256" bit key has x and y of 32 bytes each
* something like: "04 41 04||x||y"
* Do simple size calculation based on DER encoding
*/
if ((size - 2) <= 127)
ksize = (size - 3) * 4;
else if ((size - 3) <= 255)
ksize = (size - 4) * 4;
else
ksize = (size - 5) * 4;
} else {
/*
* EDDSA and XEDDSA in PKCS11 are in bit strings.
* need to drop '03' tag, len (in bytes) and 00 bits in last byte.
* (We only support uncompressed for now)
* Uncompressed EC_POINT is DER OCTET STRING
* or DER BIT STRING "04||x||y"
* So a "256" bit key has x and y of 32 bytes each
* something like: "03 42 00 04|x|y" or "04 41 04||x||y"
* Do simple size calculation based on DER encoding
*/
if ((size - 3) < 127)
ksize = (size - 3) * 8;
else if ((size - 4) <= 255)
ksize = (size - 4) * 8;
else
ksize = (size - 5) * 8;

ksize = (body_len - 1) * 4;
} else if (body_len > 0) {
/*
* EDDSA and XEDDSA in PKCS11 and only one coordinate
*/
ksize = (body_len) * 8;
}

printf(" EC_POINT %lu bits\n", ksize);
if (ksize)
printf(" EC_POINT %lu bits\n", ksize);
else
printf(" EC_POINT size unknown");

if (bytes) {
if ((CK_LONG)size > 0) { /* Will print the point here */
printf(" EC_POINT: ");
Expand Down