Skip to content

Conversation

@popovec
Copy link
Member

@popovec popovec commented Mar 21, 2021

supported mechanisms: AES-ECB, AES-CBC, AES-CBC-PAD
support for initialization vector

Tested - softhsm2, (test-pkcs11-tool-sym-crypt-test.sh)

modified:   doc/tools/pkcs11-tool.1.xml
modified:   src/tools/pkcs11-tool.c
modified:   tests/Makefile.am
new file:   tests/test-pkcs11-tool-sym-crypt-test.sh

Copy link
Member

@Jakuje Jakuje left a comment

Choose a reason for hiding this comment

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

noticed just couple of typos, but otherwise looks good.

@popovec
Copy link
Member Author

popovec commented Mar 21, 2021

@Jakuje Thanks for the review, I will do a force push with more features - key unwrap and wrap.

@popovec popovec force-pushed the pkcs11-tool-sym-crypt branch from 355ac6c to 09d068b Compare March 21, 2021 16:26
</varlistentry>
<varlistentry>
<term>
<option>--Unwrap</option>,
Copy link
Member

Choose a reason for hiding this comment

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

lowercase 'u'

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks for the review, I'll fix it.

But I have question.. Is command line format (for unwrap) acceptable (especially use of --application-id --application-label)? Use key id 22 to unwrap file input.key as AES key with ID 90 and label unwraped-key:

pkcs11-tool --unwrap --mechanism RSA-PKCS --id 22 -i input.key --key-type AES: --application-id 90 --applicatin-label unwraped-key

If appropriate, I can add another switch for specification of the unwrapped key.

For the wrap operation, I have similar question.. I suggest the following command line format (use key id 22 to wrap key id 90):

pkcs11-tool --wrap --mechanism RSA-PKCS --id 22 --application-id 90 -o output.key

I'll add the the wrap code soon...

Copy link
Member

Choose a reason for hiding this comment

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

(sorry for a delay)
I do not like much the reuse of applicatin-id here. This is originally used for data objects. I think we will need a new switches for this not to confuse users. But I am not sure what would be the best terminology. --new-id and --new-label or --wrapped-key-id and --wrapped-key-label are just some ideas.

Copy link
Member Author

Choose a reason for hiding this comment

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

I am aware that --application_id and --application_labelis not used in its original meaning. As an alternative, I still like it the most:

a) unwrap: use the --new-label and --new-id switches (alternative --new-key-label, --new-key-id)
example:

pkcs11-tool --unwrap --mechanism RSA-PKCS --id 22 -i input.key --key-type AES: --new-key-id 90 --new-key-label unwraped-key

b) wrap: it is possible to use the keyword --wrap with an argument, where either the id or label of the key that will be the subject of the wrapping can be used.

example:

pkcs11-tool --wrap 90 --mechanism RSA-PKCS --id 22  -o output.key

pkcs11-tool --wrap key_sign1  --mechanism RSA-PKCS --id 22  -o output.key

I would like other OpenSC developers to comment on this before applying this PR.

Thanks.

Copy link
Member

Choose a reason for hiding this comment

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

@frankmorgner what do you think?

@popovec popovec force-pushed the pkcs11-tool-sym-crypt branch 2 times, most recently from 98b8d43 to e9dc9dc Compare March 27, 2021 18:28
exit 77;
fi
# The Ubuntu has old softhsm version not supporting this feature
grep "Ubuntu 18.04" /etc/issue && echo "WARNING: Not supported on Ubuntu 18.04" && exit 77
Copy link
Member

Choose a reason for hiding this comment

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

Travis already supports also Ubuntu 20.04: https://docs.travis-ci.com/user/reference/focal/ Does it already work there?

Would it make sense to try to migrate the CI to new version or add one more target which would execute also this test case? Or just write a new one in github actions, which should allow running in more up-to-date systems?

Copy link
Member

Choose a reason for hiding this comment

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

We have new Github Actions in place already, based on Ubuntu 20 so this should work there.

@popovec popovec force-pushed the pkcs11-tool-sym-crypt branch 3 times, most recently from 06810ef to b0d1db1 Compare April 2, 2021 07:19
@popovec
Copy link
Member Author

popovec commented Apr 2, 2021

Tests for symmetric encryption and wrap / unwrap operations are fully functional, tested on Ubuntu 20, against softhsm2.
More at https://travis-ci.org/github/OpenSC/OpenSC/jobs/765595173

One of the tests that is not affected by this PR does not work (FAIL: test-pkcs11-tool-allowed-mechanisms.sh).
The bug is probably on the softhsm2 side, probably a bug in the key listing. The following modification works for me on the Debian 10 system:

diff --git a/tests/test-pkcs11-tool-allowed-mechanisms.sh b/tests/test-pkcs11-tool-allowed-mechanisms.sh
index 4c9c62d5..7dcb5590 100755
--- a/tests/test-pkcs11-tool-allowed-mechanisms.sh
+++ b/tests/test-pkcs11-tool-allowed-mechanisms.sh
@@ -22,7 +22,7 @@ MECHANISMS="RSA-PKCS,SHA1-RSA-PKCS,RSA-PKCS-PSS"
 # Generate key pair
 $PKCS11_TOOL --keypairgen --key-type="RSA:" --login --pin=$PIN \
        --module="$P11LIB" --label="test" --id="$ID" \
-       --allowed-mechanisms="$MECHANISMS"
+       --allowed-mechanisms="${MECHANISMS},SHA384-RSA-PKCS"
 assert $? "Failed to Generate RSA key pair"
 
 # Check the attributes are visible

If necessary, I can include it in this PR.

@Jakuje
Copy link
Member

Jakuje commented Apr 6, 2021

Thank you for checking the new version. It looks like some bug in softhsm in Ubuntu 20. Locally in Fedora 34 it works for me fine (with or without your patch) so please, include it in the PR so we have it passing in CI.

I also see that even though the check fails, the pipeline passes, which is wrong. If I am right, it is because of the log is printed, which mangles the exit code. Something like this should address this problem:

diff --git a/.travis.yml b/.travis.yml
index e449f182..b87751d7 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -251,7 +251,7 @@ script:
       fi;
     fi
   - if [ -z "$HOST" -a "${DO_COVERITY_SCAN}" != "yes" -a -z "$DO_SIMULATION" ]; then
-      make check && make dist || cat tests/*log;
+      make check && make dist || (cat tests/*log src/tests/unittests/*log && exit 1);
     fi
   - if [ ! -z "$HOST" -a "${DO_COVERITY_SCAN}" != "yes" ]; then
       make install;
@@ -336,7 +336,7 @@ script:
     fi
   - if [ "${DO_SIMULATION}" = "cac" ]; then
       cd $TRAVIS_BUILD_DIR;
-      make check && sudo make install || (cat tests/*log src/tests/unittests/*log && exit);
+      make check && sudo make install || (cat tests/*log src/tests/unittests/*log && exit 1);
       export LD_LIBRARY_PATH=/usr/local/lib;
       cd src/tests/p11test/;
       ./p11test -s 0 -p 12345678 -i &

popovec added a commit to popovec/OpenSC that referenced this pull request Apr 6, 2021
@popovec popovec force-pushed the pkcs11-tool-sym-crypt branch 3 times, most recently from f865bfe to 0a03a6d Compare April 13, 2021 14:28
@Jakuje
Copy link
Member

Jakuje commented Aug 24, 2021

Can you rebase the changes on current master, which introduced HMAC sigh&verify to pkcs11-tool? Otherwise I think this is good to go for the next release.

supported mechanisms: AES-ECB, AES-CBC, AES-CBC-PAD
support for initialization vector

Tested - softhsm2, (test-pkcs11-tool-sym-crypt-test.sh)

	modified:   doc/tools/pkcs11-tool.1.xml
	modified:   src/tools/pkcs11-tool.c
	modified:   tests/Makefile.am
	new file:   tests/test-pkcs11-tool-sym-crypt-test.sh
support for unwrapping GENERIC SECRET / AES key
Tested: softhsm, RSA-PKCS mechanism.
Tested: MyEID 4.5.5, only partial test due to missing support
                     for symmetric encryption/decryption

	modified:   doc/tools/pkcs11-tool.1.xml
	modified:   src/tools/pkcs11-tool.c
	modified:   tests/Makefile.am
	new file:   tests/test-pkcs11-tool-unwrap-test.sh
support for key wrap operation
Tested: softhsm, GENERIC SECRET wrapped by RSA-PKCS mechanism

	modified:   doc/tools/pkcs11-tool.1.xml
	modified:   src/tools/pkcs11-tool.c
	modified:   tests/Makefile.am
	renamed:    tests/test-pkcs11-tool-unwrap-test.sh -> tests/test-pkcs11-tool-unwrap-wrap-test.sh
Use C_DecryptUpdate/C_EncryptUpdate for large input files.
	modified:   src/tools/pkcs11-tool.c
	modified:   tests/test-pkcs11-tool-sym-crypt-test.sh
@popovec popovec force-pushed the pkcs11-tool-sym-crypt branch from 0a03a6d to 3ceba4b Compare August 24, 2021 14:45
@Jakuje Jakuje merged commit 8b94d04 into OpenSC:master Nov 19, 2021
LDVG added a commit to LDVG/OpenSC that referenced this pull request Jan 26, 2022
When support for unwrapping secret keys was added in 9136878 (as part
of OpenSC#2268), the `--usage-decrypt` and `--usage-wrap` options were used to
toggle the `CKA_{ENCRYPT,DECRYPT}` and `CKA_{WRAP,UNWRAP}` attributes in
the object template passed to the module.

In contrast, when a secret key object is generated (`--keygen`) or
created (`--write-object`), the same attributes are unconditionally set
to true or omitted respectively, regardless of any specified `--usage-*`
option.

To make this handling consistent, use the approach introduced by the unwrap
command and let the user specify the attributes, defaulting to only setting
`CKA_{ENCRYPT,DECRYPT}` if no usage was specified.

The documentation was amended to reflect the behavior of `--usage-decrypt`.
Jakuje pushed a commit that referenced this pull request Feb 8, 2022
When support for unwrapping secret keys was added in 9136878 (as part
of #2268), the `--usage-decrypt` and `--usage-wrap` options were used to
toggle the `CKA_{ENCRYPT,DECRYPT}` and `CKA_{WRAP,UNWRAP}` attributes in
the object template passed to the module.

In contrast, when a secret key object is generated (`--keygen`) or
created (`--write-object`), the same attributes are unconditionally set
to true or omitted respectively, regardless of any specified `--usage-*`
option.

To make this handling consistent, use the approach introduced by the unwrap
command and let the user specify the attributes, defaulting to only setting
`CKA_{ENCRYPT,DECRYPT}` if no usage was specified.

The documentation was amended to reflect the behavior of `--usage-decrypt`.
AlexandreGonzalo pushed a commit to AlexandreGonzalo/OpenSC that referenced this pull request Nov 14, 2022
When support for unwrapping secret keys was added in 9136878 (as part
of OpenSC#2268), the `--usage-decrypt` and `--usage-wrap` options were used to
toggle the `CKA_{ENCRYPT,DECRYPT}` and `CKA_{WRAP,UNWRAP}` attributes in
the object template passed to the module.

In contrast, when a secret key object is generated (`--keygen`) or
created (`--write-object`), the same attributes are unconditionally set
to true or omitted respectively, regardless of any specified `--usage-*`
option.

To make this handling consistent, use the approach introduced by the unwrap
command and let the user specify the attributes, defaulting to only setting
`CKA_{ENCRYPT,DECRYPT}` if no usage was specified.

The documentation was amended to reflect the behavior of `--usage-decrypt`.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants