-
Notifications
You must be signed in to change notification settings - Fork 803
Introduce use of custom ossl libctx with OpenSSL >= 3.0 #2712
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
Conversation
|
I marked this as draft because it will probably require some discussion to figure out if the approach is correct. I ran make check locally and it did not regress the number of failures (my local make check is not clean with the master tree). @dengert PTAL. |
|
OpenSC can be built using OpenSSL 1.1x and OpenSSL-3.0.x LiberSSL so differet ifdefs are needed. I have identified in OpenSC, these OpenSSL routines (or the _ex version) can use a libctx: Some of these are used in many places. We may not need to worry about BIO_new or BN_CTX_new. What we have done in the past is to add defines or macros in src/libopensc/sc-ossl-compat.h so if OpenSSL 3 is being used the define or macro would then add point to a libctx and call the appropiate OpenSSL 3 routine. |
|
@dengert please check the commits |
|
Btw you mised the two most important changes needed |
|
EVP_default_properties_is_fips_enabled) is used directly at a high level in the tools, so it probably does not need its own context, applications can be left unchanged. |
|
What I did was for Then greped for these 79 functions and 24 functions without "_ex" to see what is in OpenSC source. So there might be others that should be included. |
Could |
|
It cause the issue because: But once we cut the loop in opensc-pkcs11 we will be fine, there is really no need to modify pkcs11-tool itself, only libraries/modules. |
|
The call From OpenSSL code: Passing the context returned by that call is equal to passing NULL. |
Be nice if you could take a quick look at the patches I am piling on, to see if I am going in the right direction ... there should be enough meat now to get a sense of what this change entails. |
|
@dengert how stable need libsm interface need to be? This library has two problems.
|
|
Looks to me that libsm is a static library while libssm is a public one, so I will liberally change also libsm functions ... |
|
Unfortunately the same problem is present in libssm ... I will break it and rthen we can figure out how to pick up the pieces |
|
Checked out #2712 bd1fc42 We really need to get pkcs11-tool to force the OpenSSL to init before pkcs11-tool calls C_Initialize. Since pkcs11-tool loads libopensc but only to access some hex print routines and other utility functions, |
Yes, that we have to do, but it should be quite simple, any call into openssl should force that. |
|
@dengert see commit: Explicitly init OpenSSL in pkcs11-tool |
|
Note that the pkcs11-tool commit is just a temporary workaround, it is not the correct fix, because it does not help any other application that may load opensc-pkcs11 in simialr situations (ie before openssl is initialized). |
|
An option could be to provide a custom configuration file for opensc-pkcs11, that would have the advantage of letting users customize it if they need, including adding or removing the legacy provider. Alternatively the configuration file might be optional and could be sourced from opensc.conf only if needed ... |
|
Sadly it seems that the only way to init openssl without reading the config would then interfere with an application expecting a normal config to happen ... I will leave this here for now, I think this patch is still something that needs to be introduced, if for nothing else because the current code is mucking with the default config by surreptitiously loading the legacy provider. But this code alone does not, in fact, resolve the potential C_initialize loop ... |
|
Using pkcs11-tool->EVP_default_properties_is_fips_enabled->...->OPENSSL_init_crypto->loads pkcs11-provider->loads-opensc-pkcs11-> pkcs11-provider->C_Initialize ... C_initialize returns OPENSSL_init_returns EVP_default_properties_is_fips_enabled returns pkcs11-tool loads opensc-pkcs11, then calls C_Initialize which returns then goes on to says "Invalid signature" Without this change the path was opensc-pkcs11 while in C_Initialize to cause OpenSSL to init, anp provider, load opensc-pkcs11 and provider calls C_Initialize which is holding a lock. PKCS11 C_Initialize can only return: CKR_ARGUMENTS_BAD, CKR_CANT_LOCK, CKR_CRYPTOKI_ALREADY_INITIALIZED, CKR_FUNCTION_FAILED, CKR_GENERAL_ERROR, CKR_HOST_MEMORY, CKR_NEED_TO_CREATE_THREADS, CKR_OK. And it does make any sense to me, that a PKCS11 application would ever use the pkcs11-provider to load the same PKCS11 module the application has loaded. But there maybe a way around that. If while in C_Initialize, C_Initalize is called by same thread, it should return CKR_FUNCTION_FAILED or maybe some vender specific CKR, which would then cause the provider not run. I can look at this Thursday afternoon or Friday. while in C_initialize causes provider to load |
|
I think another way to deal with this could be to just call OPENSSL_init_crypto(0 in C_initialize, before it takes the lock. It will return CKR_CRYPTOKI_ALREADY_INITIALIZED if nothing else is done. Ideally we have a way to avoid getting OpenSSL to initialize, but the the code that detect cards always will end up calling something that makes openssl initialize apparently. Sounds like a no-win scenario :-( |
|
@dengert after thinking a bit about this I think it should be relatively easy to change C_Initialize so that we make the second initialization in OpenSSL either fail or return an alredy initialized error which pkcs11-provider currently accept as ok. What should be the actual behavior? However we should not cause hard error if two initialization happen and they are not nested IMO (because otherwise you might get a hard error in the application if openssl initializes first through some other library load). I'll propose a patch that handles just the nest (same process) case. |
|
I didn't use atomics so this may fail to trigger if mutliple threads call C_Initialize concurrently, but then for threads there is the mutex protecting later. |
|
Changed as requested, compiles here, but haven't run tests myself. |
frankmorgner
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thank you
Allow testing PIV SM with or without github.com/OpenSC/pull/2712 On branch PIV-4-extensions Changes to be committed: modified: ../tools/pkcs11-tool.c
Allow testing PIV SM with or without github.com/OpenSC/pull/2712 On branch PIV-4-extensions Changes to be committed: modified: card-piv.c
Allow testing PIV SM with or without github.com/OpenSC/pull/2712 On branch PIV-4-extensions Changes to be committed: modified: card-piv.c
|
Thank you for the patience. Tested locally and I did not notice any issues. Merging. |
This is in addition to "Introduce use of custom ossl libctx with OpenSSL >= 3.0" OpenSC#2712 pkcs11-tool uses some functions found in libopensc, but does not create a sc_context like other OpenSC tools as the pkcs11 module can be any pkscs11 module. There is one OpenSSL function "d2i_PUBKEY_bio" that does not have an equivalent "d2i_PUBKEY_ex_bio" in 3.0.8. It is listed in OpenSSL master. See: openssl/openssl#18427 On branch ossl_lib_ctx-pkcs11-tool Changes to be committed: modified: src/tools/pkcs11-tool.c
This is in addition to "Introduce use of custom ossl libctx with OpenSSL >= 3.0" #2712 pkcs11-tool uses some functions found in libopensc, but does not create a sc_context like other OpenSC tools as the pkcs11 module can be any pkscs11 module. There is one OpenSSL function "d2i_PUBKEY_bio" that does not have an equivalent "d2i_PUBKEY_ex_bio" in 3.0.8. It is listed in OpenSSL master. See: openssl/openssl#18427 On branch ossl_lib_ctx-pkcs11-tool Changes to be committed: modified: src/tools/pkcs11-tool.c
Allow testing PIV SM with or without github.com/OpenSC/pull/2712 On branch PIV-4-extensions Changes to be committed: modified: card-piv.c
Allow testing PIV SM with or without github.com/OpenSC/pull/2712 On branch PIV-4-extensions Changes to be committed: modified: card-piv.c
Allow testing PIV SM with or without github.com/OpenSC/pull/2712 On branch PIV-4-extensions Changes to be committed: modified: card-piv.c
Allow testing PIV SM with or without github.com/OpenSC/pull/2712 On branch PIV-4-extensions Changes to be committed: modified: card-piv.c
Allow testing PIV SM with or without github.com/OpenSC/pull/2712 On branch PIV-4-extensions Changes to be committed: modified: card-piv.c
Update PIV conf and env in opensc.conf.5.xml.in Improved card match and testing for SM cards Allow force of SC_CARD_TYPE_PIV_II_BASE, which will test for all posible type of cards tested including 800-74-4 supported features. Tested with ID-One with SM, Older NIST beta cards: Gemalto and Oberthur, YubiKey: 4 and 5 NFC and PIVKey C910. Allow testing PIV SM with or without github.com/OpenSC/pull/2712 Clear CVC contents if CVC fails to encode In responses to OpenSC#2053 (comment) For example, if the CVC can not be parsed, clear it by calling piv_clear_cvc_content Add PIV SM functions prototypes as static PIV Use piv_free_sm_apdu to cleanup if piv_encode_apdu fails This is in response to: OpenSC#2053 (comment) and OpenSC#2053 (comment) PIV Improve testing of AuthCryptogram This is in response to: OpenSC#2053 (comment) PIV goto err if AuthCryptogram check fails PIV Add check for plain->resp == NULL Handle case where apdu resp == NULL and resplen > 0 which would be a programming error. card-piv.c With SM and no data returned set plain->resplen=0 Fixes OpenSC#2053 (comment) PIV fix checking of padding Fixes: OpenSC#2053 (comment) PIV SM - Unzip SM Certificate Signer Certificate With SM, the Cert Signer certificate may be ziped. card-piv.c needs to extract the public key before pkcs15 emulation is setup. Call sc_decompress_alloc. Changes to be committed: modified: doc/files/opensc.conf.5.xml.in modified: src/libopensc/card-piv.c
Update PIV conf and env in opensc.conf.5.xml.in Improved card match and testing for SM cards Allow force of SC_CARD_TYPE_PIV_II_BASE, which will test for all posible type of cards tested including 800-74-4 supported features. Tested with ID-One with SM, Older NIST beta cards: Gemalto and Oberthur, YubiKey: 4 and 5 NFC and PIVKey C910. Allow testing PIV SM with or without github.com/OpenSC/pull/2712 Clear CVC contents if CVC fails to encode In responses to OpenSC#2053 (comment) For example, if the CVC can not be parsed, clear it by calling piv_clear_cvc_content Add PIV SM functions prototypes as static PIV Use piv_free_sm_apdu to cleanup if piv_encode_apdu fails This is in response to: OpenSC#2053 (comment) and OpenSC#2053 (comment) PIV Improve testing of AuthCryptogram This is in response to: OpenSC#2053 (comment) PIV goto err if AuthCryptogram check fails PIV Add check for plain->resp == NULL Handle case where apdu resp == NULL and resplen > 0 which would be a programming error. card-piv.c With SM and no data returned set plain->resplen=0 Fixes OpenSC#2053 (comment) PIV fix checking of padding Fixes: OpenSC#2053 (comment) PIV SM - Unzip SM Certificate Signer Certificate With SM, the Cert Signer certificate may be ziped. card-piv.c needs to extract the public key before pkcs15 emulation is setup. Call sc_decompress_alloc. Changes to be committed: modified: doc/files/opensc.conf.5.xml.in modified: src/libopensc/card-piv.c
Update PIV conf and env in opensc.conf.5.xml.in Improved card match and testing for SM cards Allow force of SC_CARD_TYPE_PIV_II_BASE, which will test for all posible type of cards tested including 800-74-4 supported features. Tested with ID-One with SM, Older NIST beta cards: Gemalto and Oberthur, YubiKey: 4 and 5 NFC and PIVKey C910. Allow testing PIV SM with or without github.com//pull/2712 Clear CVC contents if CVC fails to encode In responses to #2053 (comment) For example, if the CVC can not be parsed, clear it by calling piv_clear_cvc_content Add PIV SM functions prototypes as static PIV Use piv_free_sm_apdu to cleanup if piv_encode_apdu fails This is in response to: #2053 (comment) and #2053 (comment) PIV Improve testing of AuthCryptogram This is in response to: #2053 (comment) PIV goto err if AuthCryptogram check fails PIV Add check for plain->resp == NULL Handle case where apdu resp == NULL and resplen > 0 which would be a programming error. card-piv.c With SM and no data returned set plain->resplen=0 Fixes #2053 (comment) PIV fix checking of padding Fixes: #2053 (comment) PIV SM - Unzip SM Certificate Signer Certificate With SM, the Cert Signer certificate may be ziped. card-piv.c needs to extract the public key before pkcs15 emulation is setup. Call sc_decompress_alloc. Changes to be committed: modified: doc/files/opensc.conf.5.xml.in modified: src/libopensc/card-piv.c
This is a first shot at adding a custom (private) OpenSSL libctx in libopensc.
Fixes #2711
Checklist