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

Starcos 3.2 #1347

Closed
wants to merge 3 commits into from
Closed

Starcos 3.2 #1347

wants to merge 3 commits into from

Conversation

jpecholt
Copy link

Added support for Starcos 3.2. cards.
Encryption, signature and decryption functionalities were implemented.
GetSerialnumber and PIN-info are supported.
Superfluous "User Pin" removed from card label.

Starcos 3.2. cards were used during testing.

  • Documentation is added and updated
  • PKCS#11 module is tested. Note: Starcos 3.2. only generates random numbers of 8-bytes length, which causes the generateRandom test case (./pkcs11-tool --test) to fail, since it excepts a different length.

@frankmorgner
Copy link
Member

Well, the current (and past) approach seems very hacky. Though we could continue with it until we hit a starcos version that's used in multiple configurations, it is also possible to try and be more generic, at least with Starcos 3.X. For example, which operation is allowed with what key, is specified in EF.KEYD. If parsed, it indicates which exact operation and mechanism to use for signature and encryption...

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.

Although I'd better like to see the real card versions integrated instead of more and more card profiles (which by accident are based on different card versions), I'm aware that it's hard to perform a full restart with the given code base. Hence, I would accept your hack, but please have a look at the inline comments...

sc_debug(ctx, SC_LOG_DEBUG_NORMAL, "Found alg ref id%02x\n",env->algorithm_ref & 0xFF );
*p++ = 0x80;
*p++ = 0x01;
*p++ = env->algorithm_ref & 0xFF;
Copy link
Member

Choose a reason for hiding this comment

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

missing length check

}
if(env->algorithm == SC_ALGORITHM_DES){
return -1; //for now, not supported
/**p++= 0x89;
Copy link
Member

Choose a reason for hiding this comment

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

please remove dead code

*p++= 0x13;//signature
*p++= 0x23;//PKCS RSA (standard)
if (env->algorithm_flags & SC_ALGORITHM_RSA_HASH_SHA1){
//TODO: not tested yet
Copy link
Member

Choose a reason for hiding this comment

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

please remove untested code

@@ -268,7 +406,7 @@ static int process_fci(sc_context_t *ctx, sc_file_t *file,
return SC_SUCCESS;
}

static int process_fci_v3_4(sc_context_t *ctx, sc_file_t *file,
static int process_fci_v3_2_v3_4_v3_5(sc_context_t *ctx, sc_file_t *file,
Copy link
Member

Choose a reason for hiding this comment

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

maybe renaming this to process_fci_v3 would be useful

Copy link
Author

Choose a reason for hiding this comment

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

Starcos 3.1 and 3.6 are not included, that's the reason for this naming scheme. Maybe process_fci_v3_2-5 ? Or solely put this information in a comment above the function and name it process_fci_v3?

Copy link
Member

Choose a reason for hiding this comment

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

there is no 3.1 or 3.6 at the moment, hence there is only 3.x and everything else. just change the naming.

@@ -304,7 +442,7 @@ static int process_fci_v3_4(sc_context_t *ctx, sc_file_t *file,
return SC_SUCCESS;
}

static int process_fcp_v3_4(sc_context_t *ctx, sc_file_t *file,
static int process_fcp_v3_2_v3_4_v3_5(sc_context_t *ctx, sc_file_t *file,
Copy link
Member

Choose a reason for hiding this comment

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

see above comment on the naming

src/libopensc/card-starcos.c Show resolved Hide resolved
src/libopensc/card-starcos.c Show resolved Hide resolved
@@ -1857,7 +2165,8 @@ static struct sc_card_driver * sc_get_driver(void)
starcos_ops.card_ctl = starcos_card_ctl;
starcos_ops.logout = starcos_logout;
starcos_ops.pin_cmd = starcos_pin_cmd;

starcos_ops.list_files = NULL;
Copy link
Member

Choose a reason for hiding this comment

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

this is not needed

@@ -1071,7 +1071,7 @@ pkcs15_init_slot(struct sc_pkcs15_card *p15card, struct sc_pkcs11_slot *slot,
pin_info = NULL;
}
else {
if (auth->label[0] && strncmp(auth->label, "PIN", 4) != 0)
if (auth->label[0] && strncmp(auth->label, "PIN", 4) != 0 && strncmp(auth->label, "User Pin", 9) != 0)
Copy link
Member

Choose a reason for hiding this comment

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

what do you need this change for?

Copy link
Member

Choose a reason for hiding this comment

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

The term User PIN is normally used to differentiate from SO PIN or Signature PIN.

Copy link
Author

Choose a reason for hiding this comment

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

Without this string compare, the label is copied into the tokeninfo label, which causes the inserted card to be displayed as "User Pin()" instead of "". At the same time, the closing bracket is often cut off. With the additional clause, the string is not copied and hence not shown.

Copy link
Member

Choose a reason for hiding this comment

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

It's intended to be shown as PIN LABEL (CARD LABEL). It seems your card doesn't have set p15card->tokeninfo->label. A better approach would be to only append (CARD LABEL) if p15card->tokeninfo->label is not empty.

Copy link
Author

Choose a reason for hiding this comment

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

Both auth->label and p15card->tokeninfo->label are not empty, Github just removed the strings surrounded by angle brackets from my example. What the output shows is "User Pin(Example card" -where the closing bracket is oftentimes missing- instead of the desired "Example card".

Copy link
Member

Choose a reason for hiding this comment

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

As explained above, I don't think this should be done for all cards. Either do this specifically for your card in something like pkcs15-starcos.c or create a configuration option that allows setting the pkcs#11 slot labels.

Copy link
Author

Choose a reason for hiding this comment

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

I am not as well-versed in the code structure and order of execution of certain functions: Is either one of the pkcs15-starcos.c::starcos_init_card( ) or pkcs15-starcos.c::starcos_finalize_card( ) functions definitely going to be called After the above function pkcs15_init_slot( ) ? In that case, one could change the card label in pkcs15-starcos.c, otherwise it might be overwritten again.

Copy link
Member

Choose a reason for hiding this comment

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

please remove this change. We'll think about configuring the slot naming seperately.

@OpenSC OpenSC deleted a comment from jpecholt Jul 4, 2018
@frankmorgner
Copy link
Member

If you look at the inline comments, we can merge this for the next release...

@jpecholt
Copy link
Author

I undid changes concerning the renaming of the card lable in framework-pkcs15.c:1089 and integrated all other requests. I hope this satisfies all requirements.

@frankmorgner
Copy link
Member

There's some error handling missing with malloc

@jpecholt
Copy link
Author

As far as I could see, after each malloc, a pointer check is performed (Though this is not shown here, it is visible in the file)?

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 result of malloc was checked, indeed. But there is still some dead code lying around, for which I don't see a good reason.

sc_format_apdu(card, &apdu, SC_APDU_CASE_3_SHORT, 0x22, 0x81,
0xb8);
// adapt last byte : FIXME DIRTY HACK (somehow works?)
p += env->key_ref_len -1;
Copy link
Member

Choose a reason for hiding this comment

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

what are you trying to achieve? As a general rule, don't use hacks!

Copy link
Author

@jpecholt jpecholt Sep 24, 2018

Choose a reason for hiding this comment

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

There is a discrepancy between provided key reference number by the card and reference number expected by it. This was tested on multiple cards and compared to other drivers which also receive the same numbers by the card. Even though e.g. a 0x84 is given as a key reference number when the card is read out, it produces an error when later specifying this exact key reference number for an operation. For an unknown reason, it always expects a number which is smaller by three, 0x81 in the example. The reason for this could not be found after a thorough in depth search, so we left it at that. I see that there needs to be an additional check concerning a zero length key reference number, though.

Copy link
Member

Choose a reason for hiding this comment

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

What is the exact key reference and the exact value you need to code in the MSE? What's the content of EF_KEYD?

I think understanding EF_KEYD is key in avoiding hacks like this. Please read the documentation and try to find some hints.

Copy link
Author

Choose a reason for hiding this comment

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

I am fairly sure I tried the following, but I will look this up to confirm it:
I was not able to access any EF_KEYD file as an additional source of information. Neither was this file accessed by any other driver, implementation or program which could be used as a reference. The documentation did not provide any further helpful information. Additionally, OpenSC chooses a key with the appropriate usage flags (see e.g. pkcs15-framework.c:pkcs15_prkey_sign, line 3773), whose reference number is written into the key-ref-pointer. This number is the exact same as the one which was initially read out from the Starcos Smart-Card. It is later not accepted as a valid ref-number anymore, the reported error code deeming the key not suitable or the number invalid. No information in the documentation suggests a change in or an adaption of the key reference number.

Copy link
Member

Choose a reason for hiding this comment

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

Did you read the starcos reference manual? Did you search for EF_KEYD?

Sorry for being picky, but this hack is a little too problematic. Unfortunately, I currently don't have more time to implement this myself...

Copy link
Author

Choose a reason for hiding this comment

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

I completely understand. I spent an extensive amount of time myself trying to find the reason for this. This also lead to the fact that I compared the complete communication process between driver and card for different implementations, none was making use of this EF_KEYD file or gaining any further relevant information. I am certain that the information found in the manual was not sufficient, but I am going to check that again tomorrow and come back to you.

* Not supported yet
* TODO: check one-sided symmetric
* double-sided symmetric */
return 0;
Copy link
Member

Choose a reason for hiding this comment

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

Here is more dead code

"not supported for STARCOS 3.2 cards"); \
return SC_ERROR_NOT_SUPPORTED; \
} \
} while (0);
Copy link
Member

Choose a reason for hiding this comment

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

should be merged with CHECK_NOT_SUPPORTED_V3_4

Copy link
Author

Choose a reason for hiding this comment

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

There is one additional operation which was not tested on Starcos 3.4. Apart from this, one could merge both checks and implement a manual error check at that point.

} else if(card->type == SC_CARD_TYPE_STARCOS_V3_2) {
//TODO: for now: not supported (Internal authenticate used instead)
return SC_ERROR_NOT_SUPPORTED;
#if 0
Copy link
Member

Choose a reason for hiding this comment

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

remove dead code

if(env->flags & SC_SEC_ENV_KEY_REF_SYMMETRIC){
/* For now, signature sometimes runs into this case -> return 0, not -1 to continue signature procedures
* Not supported yet. */
return 0;
Copy link
Member

Choose a reason for hiding this comment

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

should be an error

Copy link
Author

Choose a reason for hiding this comment

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

I'll update the comment, thanks.

} while (0);

/*Finds appropriate alg based on sec environment and writes these into the ptr
* returns number of bytes which were added, 0 if no alg specified. -1 if not supported
Copy link
Member

Choose a reason for hiding this comment

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

-1 or an error code?

Copy link
Author

Choose a reason for hiding this comment

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

Error code, thank you.

src/libopensc/card-starcos.c Show resolved Hide resolved
@rumpelsepp rumpelsepp mentioned this pull request Apr 16, 2020
@frankmorgner
Copy link
Member

Closing this issue due to inactivity. Please re-open the ticket if more input is available.

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.

None yet

2 participants