Skip to content

Conversation

@ya-isakov
Copy link
Contributor

@ya-isakov ya-isakov commented Apr 4, 2021

OpenPGP Card 3.4+ supports getting list of algos from the new Algoritm Information DO
https://gnupg.org/ftp/specs/OpenPGP-smart-card-application-3.4.pdf, 4.4.3.11
If at least one algo is in this list, use it, and ignore hardcoded.

Tested on Yubikey 5 NFC with FW 5.2.4. Handled few problems with Yubikey not
following the spec.

Also, fixed problem with algorithm find, which will return EC algorithm with the same
key size, even if curve is not the same (e.g., curves 1.3.132.0.34 and 1.3.36.3.3.2.8.1.1.11 has
the same keysize of 384 bits, so it was possible that one returned in place of other)

This is needed for Yubikey to support more algorithms, than are used in Algorithm Attributes DO,
in case it was initialized with gpg, and not pkcs15-tool.

Checklist
  • PKCS#11 module is tested
  • Windows minidriver is tested
  • macOS tokend is tested

@ya-isakov ya-isakov changed the title OpenPGP: Get list of supported algorithms from DO FA OpenPGP: Get list of supported algorithms from Algorithm Information Apr 4, 2021
@ya-isakov ya-isakov force-pushed the do_fa branch 3 times, most recently from f923d32 to d6f519d Compare April 4, 2021 10:22
@ya-isakov ya-isakov changed the title OpenPGP: Get list of supported algorithms from Algorithm Information OpenPGP 3.4+: Get list of supported algorithms from Algorithm Information Apr 4, 2021
@ya-isakov ya-isakov force-pushed the do_fa branch 7 times, most recently from cfe0c2b to e1d5ce0 Compare April 4, 2021 18:08
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.

Looks good. Just couple of comments around the code style. Tested with Nitrokey Start and I did not notice any issues.

@ya-isakov
Copy link
Contributor Author

@Jakuje thank you for review. IIRC, Nitrokeys are based on GNUK, which OpenPGP Card 2.0 with some extensions, so, my code should not work for it. But it's good to know that I haven't broke anything for other tokens :) I've changed logic a little bit (now, DO FA should be checked independent from DO 6E, but, still, if any algo is found by it, code in DO 6E will ignore info from DOs 7E -> C1-C3. Could you, please, test it again?

@Jakuje
Copy link
Member

Jakuje commented Apr 7, 2021

@Jakuje thank you for review. IIRC, Nitrokeys are based on GNUK, which OpenPGP Card 2.0 with some extensions, so, my code should not work for it.

Right. I was writing the initial support for ed25519 in opensc around the gnuk.

But it's good to know that I haven't broke anything for other tokens :) I've changed logic a little bit (now, DO FA should be checked independent from DO 6E, but, still, if any algo is found by it, code in DO 6E will ignore info from DOs 7E -> C1-C3. Could you, please, test it again?

Retested current version and looks working fine.

@ya-isakov ya-isakov marked this pull request as draft April 7, 2021 13:49
@ya-isakov
Copy link
Contributor Author

After some research, I found that OpenSC is parsing Import-Format byte, but then it's not storing it anywhere. So, it's set in pgp_parse_algo_attr_blob, but the struct, where it is set, is created in pgp_get_card_features, but nowhere In this function I can see usage of keyformat field (e.g, it is not added as algo field) Then, keyformat is checked only once - in pgp_build_extended_header_list, and this function is called in openpgp_store_key. In the same function, key_info is created, but no keyformat field is set.

So, I think I can assume, that as this setting is ignored, my current approach is fine - for tokens < 3.4, the setting will be read from DO 6E, and ignored. For 3.4+ tokens, if DO FA has this byte set for any algo, it will be ignored, again...

@ya-isakov ya-isakov marked this pull request as ready for review April 7, 2021 18:57
@dengert
Copy link
Member

dengert commented Apr 7, 2021

Looks like the keyformat deals with the format of the private key on the card.

#define SC_OPENPGP_KEYFORMAT_RSA_STD	0    /* See 4.3.3.6 Algorithm Attributes */
#define SC_OPENPGP_KEYFORMAT_RSA_STDN	1    /* OpenPGP card spec v2 */
#define SC_OPENPGP_KEYFORMAT_RSA_CRT	2
#define SC_OPENPGP_KEYFORMAT_RSA_CRTN	3
#define SC_OPENPGP_KEYFORMAT_EC_STD	0
#define SC_OPENPGP_KEYFORMAT_EC_STDPUB	0xFF

And in OpenPGP-3.4.1 it shows how the STD and CRT are used.

So unless you are loading or reading a private key, you should not care, because you will never see it. src/tools/openpgp-tool.c uses it in pgp_store_key and only supports some of them.

@ya-isakov
Copy link
Contributor Author

ya-isakov commented Apr 10, 2021

I've fixed problem with multiple ECDH-DERIVE mechanisms, which surfaced after DO FA parsing. Now, derive works for X25519 (with MRs #2286 and #2293)

Copy link
Member

@frankmorgner frankmorgner left a comment

Choose a reason for hiding this comment

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

The code looks good, just a formatting issue

@Jakuje
Copy link
Member

Jakuje commented Sep 30, 2021

@ya-isakov can you rebase the patch set on the current master and resolve the conflicts?

@ya-isakov
Copy link
Contributor Author

@Jakuje, Sure, I'll do it on this weekend.

…tion

OpenPGP Card 3.4+ supports getting list of algos from the new Algoritm Information DO
https://gnupg.org/ftp/specs/OpenPGP-smart-card-application-3.4.pdf, 4.4.3.11
If at least one algo is in this list, use it, and ignore hardcoded.

Tested on Yubikey 5 NFC with FW 5.2.4. Handled few problems with Yubikey not
following the spec.

Also, fixed problem with algorithm find, which will return EC algorithm with the same
key size, even if curve is not the same.
@ya-isakov ya-isakov force-pushed the do_fa branch 2 times, most recently from 6afc361 to 0f122ee Compare October 4, 2021 18:30
If we're finding EC algo, and curve is not matched, another algo
with the same key length shoud not be returned.
- Now it's checked independent from DO 6E
- If no algos can be parsed from DO FA, code will fallback to other ways
- Indentation fixes
After parsing DO FA, algorithm info could have both X25519 and
older EC curves. This leads to register of two mechanisms with same type,
CKM_ECDH1_DERIVE, but different key types. This could lead to a situation,
that C_DeriveKey will throw an error, while user is trying to derive
using X25519 key. This commit fixes this, to have a list of key types,
and registering the second mechanism will just add key type to list in
the existing mechanism, and updates it's mech_info.
@ya-isakov
Copy link
Contributor Author

I've rebased my branch, fixed leftover comments, but I need to check, if it is still working :) Will do it soon.

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.

just minor nits. I think we are good to go with this. Tested locally with Nitrokey start and all looks fine.

@szszszsz
Copy link

szszszsz commented Dec 1, 2021

@Jakuje thank you for review. IIRC, Nitrokeys are based on GNUK, which OpenPGP Card 2.0 with some extensions, so, my code should not work for it.

A bit off topic: Nitrokey Pro and Nitrokey Storage contain OpenPGP v3.3 card. Nitrokey Start is indeed v2.0+ (with chosen parts of v3.3 spec) software OpenPGP implementation, based on GNUK.

@Jakuje
Copy link
Member

Jakuje commented Dec 1, 2021

@szszszsz thanks for clarification and thanks for reminder I wanted to merge this finally.

It would be great if we could get the nits I mentioned last month fixed. But anyway, if there will be no further concerns, I will merge this during the next week.

@Jakuje Jakuje merged commit 2cc7b10 into OpenSC:master Dec 4, 2021
@Jakuje
Copy link
Member

Jakuje commented Dec 4, 2021

Thank you!

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.

5 participants