When we select the currently selected ADF, we actually select 7fff, which is an alias for the currently selected ADF. Let's express this also more clearly in the log output.
OmapiCallbackHandlerVpcd: fix extration of DF-Name (AID)
When the DF-Name (AID) is extracted from the SELECT TPDU, the length of the TPDU is not checked properly, which may lead to an exception in case no DF-Name (AID) is supplied. Let's put proper length checks in place to filter corner cases and to ensure that the DF-Name (AID) is properly extracted in case it is supplied.
OmapiCallbackHandlerVpcd: fetch SELECT response from OMAPI
At the moment we get the select response by selecting 7FFF, which is an alias for the currently selected application. This returns a select response when an ISIM or USIM application is selected. Other applications may react differently here.
OMAPI has a getSelectResponse method through which we can get the actual response that was received from the card when the application was selected when openLogicalChannel was called. This is far more accurate, so lets use getSelectResponse instead of selecting 7FFF.
OmapiCallbackHandlerVpcd: pass SELECT parameter P2 to OMAPI
The OMAPI method openLogicalChannel may take an optional parameter p2, This parameter. Among other function, this paramter in particular controls how to deal with short DF-Names that match the AIDs of several applications.
Since we always know this parameter from the TPDU that we receive from VPCD, let's pass it on to OMAPI when we create a new channel.
OmapiCallbackHandlerVpcd: simplify/fix SELECT by DF-Name (aid)
When a TPDU with a SELECT by DF-Name is received from the VPCD end, it cannot be transparently passed through the OMAPI channel as OMAPI will block those TDPUs for security reasons. To overcome this, we close the current OMAPI channel and re-open a new one under the new DF-Name (AID).
To reduce the likelyhood for unexpected behaviour and possible loss of state we have replaced the SELECT by DF-Name with a SELECT to 7fff (alias for the currently selected application), in case the SELECT by DF-Name would target the currently selected application.
This workaround requires preceise tracking of which application is currently selected. Unfortunately this has proven as difficult and error prone.
After looking closer at the problem we noticed that we do not even need the aforementioned workaround. The opening and closing of the OMAPI channel just opens and closes logical channels on the card. It does not perform a reset. This in particular means that the ADM or PIN verification state is retained. (states like the currently selected file, the current tag and the current record are reset by SELECT anyway).
So let's remove the workaround and re-open the OMAPI channel each time a SELECT by DF-Name is received.