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

Test operations with pem keys #428

Merged
merged 3 commits into from
Aug 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .reuse/dep5
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ Files: .github/*
docs/*
tests/lsan.supp
tools/openssl*.cnf
tests/*.pem
Copyright: (C) 2022 Simo Sorce <[email protected]>
License: Apache-2.0

Expand Down
2 changes: 1 addition & 1 deletion src/objects.c
Original file line number Diff line number Diff line change
Expand Up @@ -2762,7 +2762,7 @@ static CK_RV return_dup_key(P11PROV_OBJ *dst, P11PROV_OBJ *src)
dst->cka_token = src->cka_token;
dst->data.key = src->data.key;

dst->attrs = OPENSSL_malloc(sizeof(CK_ATTRIBUTE) * src->numattrs);
dst->attrs = OPENSSL_zalloc(sizeof(CK_ATTRIBUTE) * src->numattrs);
if (!dst->attrs) {
rv = CKR_HOST_MEMORY;
P11PROV_raise(dst->ctx, rv, "Failed allocation");
Expand Down
4 changes: 3 additions & 1 deletion src/util.c
Original file line number Diff line number Diff line change
Expand Up @@ -1016,8 +1016,10 @@ CK_RV p11prov_copy_attr(CK_ATTRIBUTE *dst, CK_ATTRIBUTE *src)
return CKR_HOST_MEMORY;
}
memcpy(dst->pValue, src->pValue, src->ulValueLen);
dst->ulValueLen = src->ulValueLen;
} else {
dst->pValue = NULL;
}
dst->ulValueLen = src->ulValueLen;
dst->type = src->type;

return CKR_OK;
Expand Down
3 changes: 3 additions & 0 deletions tests/helpers.sh
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ title()
shift 1
echo ""
echo "## $*"
if [ -f "${PPDBGFILE}" ]; then
echo "[TEST]: $*" >> "${PPDBGFILE}"
fi
;;
"LINE")
shift 1
Expand Down
1 change: 1 addition & 0 deletions tests/setup-kryoptic.sh
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,7 @@ title LINE "Export test variables to ${TMPPDIR}/testvars"
cat >> "${TMPPDIR}/testvars" <<DBGSCRIPT
export P11LIB=${P11LIB}
export PKCS11_PROVIDER_MODULE=${P11LIB}
export PPDBGFILE=${TMPPDIR}/p11prov-debug.log
export PKCS11_PROVIDER_DEBUG="file:${TMPPDIR}/p11prov-debug.log"
export OPENSSL_CONF="${OPENSSL_CONF}"
export KRYOPTIC_CONF="${TMPPDIR}/tokens/kryoptic.sql"
Expand Down
1 change: 1 addition & 0 deletions tests/setup-softhsm.sh
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,7 @@ title LINE "Export test variables to ${TMPPDIR}/testvars"
cat >> "${TMPPDIR}/testvars" <<DBGSCRIPT
export P11LIB=${P11LIB}
export PKCS11_PROVIDER_MODULE=${P11LIB}
export PPDBGFILE=${TMPPDIR}/p11prov-debug.log
export PKCS11_PROVIDER_DEBUG="file:${TMPPDIR}/p11prov-debug.log"
export OPENSSL_CONF="${OPENSSL_CONF}"
export SOFTHSM2_CONF=${TMPPDIR}/softhsm.conf
Expand Down
1 change: 1 addition & 0 deletions tests/setup-softokn.sh
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ sed -e "s|@libtoollibs@|${LIBSPATH}|g" \

title LINE "Export tests variables to ${TMPPDIR}/testvars"
cat > "${TMPPDIR}/testvars" <<DBGSCRIPT
export PPDBGFILE=${TMPPDIR}/p11prov-debug.log
export PKCS11_PROVIDER_DEBUG="file:${TMPPDIR}/p11prov-debug.log"
export PKCS11_PROVIDER_MODULE="${SOFTOKNPATH%%/}/libsoftokn3${SHARED_EXT}"
export OPENSSL_CONF="${OPENSSL_CONF}"
Expand Down
27 changes: 27 additions & 0 deletions tests/tecdh
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,31 @@ pkeyutl -derive -inkey ${ECBASEURI}
-peerkey ${ECPEERPUBURI}
-out ${TMPPDIR}/secret.ecdh.bin'


# Now test by forcing all operations on the token
title PARA "ECDH Exchange forcing PKCS11 Provider"
ORIG_OPENSSL_CONF=${OPENSSL_CONF}
sed -e "s/#MORECONF/alg_section = algorithm_sec\n\n[algorithm_sec]\ndefault_properties = ?provider=pkcs11/" \
"${OPENSSL_CONF}" > "${OPENSSL_CONF}.forcetoken"
OPENSSL_CONF=${OPENSSL_CONF}.forcetoken
title PARA "ECDH Exchange forced: public key in file"
ossl '
pkeyutl -derive -inkey ${ECBASEURI}
-peerkey ${TESTSSRCDIR}/testp256.pub.pem
-out ${TMPPDIR}/forced.pub.ecdh.bin'

### Private EC Key import not supported yet
#title PARA "ECDH Exchange forced: private key in file"
#ossl '
#pkeyutl -derive -inkey ${TESTSSRCDIR}/testp256.pri.pem
# -peerkey ${ECPEERPUBURI}
# -out ${TMPPDIR}/forced.pri.ecdh.bin'
Comment on lines +27 to +31
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How much work is adding the support for EC private key export as URI PEM file?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not about export or URI PEM, this is about importing an actual Private key in a PEM file as a session object for use in operations.

Ii is not a commonly needed operation but was one of the goals for using a pkcs11 module as a FIPS token.
It is also necessary if you need to use a private key in a file when default_properties=?pkcs11_provider


#title PARA "ECDH Exchange forced: both key in file"
#ossl '
#pkeyutl -derive -inkey ${TESTSSRCDIR}/testp256.pri.pem
# -peerkey ${TESTSSRCDIR}/testp256.pub.pem
# -out ${TMPPDIR}/forced.both.ecdh.bin'
OPENSSL_CONF=${ORIG_OPENSSL_CONF}

exit 0
5 changes: 5 additions & 0 deletions tests/testp256.pri.pem
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
-----BEGIN PRIVATE KEY-----
MIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQgQj80pwUNIHHjzQaJ
yP+vAPE8KPBmrVwafor5xar9sq+hRANCAATXOFIB00W2LsAwzDxBpg/uFzFu4uIK
5otxalZiroOusrSBYA/vS2MC/6vaR+zrdnxRlYoHIbhe7H+PlEHPuq/a
-----END PRIVATE KEY-----
4 changes: 4 additions & 0 deletions tests/testp256.pub.pem
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
-----BEGIN PUBLIC KEY-----
MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAE1zhSAdNFti7AMMw8QaYP7hcxbuLi
CuaLcWpWYq6DrrK0gWAP70tjAv+r2kfs63Z8UZWKByG4Xux/j5RBz7qv2g==
-----END PUBLIC KEY-----
Loading