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

pkcs11-tool: add GOSTR3410 keypair generation #997

Merged
merged 6 commits into from
Jun 9, 2017

Conversation

konstantinpersidskiy
Copy link
Contributor

Hello

Recently I found out that pkcs11-tool cannot generate keypairs of mechanism GOSTR3410 (although this mechanism is shown as supported in pkcs11-tool -M). This mechanism is a standard mechanism specified by laws in some countries, so I think it is important to make it work properly in OpenSC.

This commit finishes implementing GOSTR3410 keypair generation feature; now pairs can be generated just like they are with other mechanisms.

Example of keypair generation using native OpenSC pkcs15 framework:

$ pkcs11-tool -m GOSTR3410-KEY-PAIR-GEN -k --key-type GOSTR3410:C -d 1234 -l -p '12345678'
Using slot 0 with a present token (0x0)
Key pair generated:
Private Key Object; GOSTR3410 
  PARAMS OID: 06072a850302022303
  label:      Private Key
  ID:         1234
  Usage:      sign
Public Key Object; GOSTR3410 
  PARAMS OID: 06072a850302022303
  VALUE:      044084a5de6a1bfd68ff6b106f6921d4da163244b15ea27dd3a87968d40959a6
              e4170df7674bc7ba098080d7ac2cfe80b29b4ed4369dfedf6e13e42ae32eb7b3
              3233
  label:      Private Key
  ID:         1234
  Usage:      verify

Example of keypair generation using another pkcs module:

$ pkcs11-tool --module "/home/user/Downloads/librtpkcs11ecp.so" -m GOSTR3410-KEY-PAIR-GEN -k --key-type GOSTR3410:B -d 4321 -l -p '12345678'
Using slot 0 with a present token (0x0)
Key pair generated:
Private Key Object; GOSTR3410 
  PARAMS OID: 06072a850302022302
  label:      
  ID:         4321
  Usage:      sign
Public Key Object; GOSTR3410 
  PARAMS OID: 06072a850302022302
  VALUE:      4c0a3a792a9c03cf227be5b4f94d668a11ce34071b29d9da2585fa1d7cbd1c74
              eb874e468245882c31a66a590cec62699b73b225b69e69ed298ff104be7a8c4f
  label:      
  ID:         4321
  Usage:      verify

I believe this commit will be useful. Looking forward for reviews and comments.
Regards, Konstantin Persidskiy

@@ -1081,6 +1081,8 @@ sc_pkcs15_dup_pubkey(struct sc_context *ctx, struct sc_pkcs15_pubkey *key, struc
pubkey->algorithm = key->algorithm;

if (key->alg_id) {
if (key->algorithm == SC_ALGORITHM_GOSTR3410)
key->alg_id->params = &(key->u.gostr3410.params);
Copy link
Member

Choose a reason for hiding this comment

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

Could you please check whether we could improve initializing of the data structures instead?

keygen_args.prkey_args.key.algorithm = SC_ALGORITHM_GOSTR3410;
pub_args.key.algorithm = SC_ALGORITHM_GOSTR3410;
set_gost_params(&keygen_args.prkey_args.params.gost, &pub_args.params.gost,
pPubTpl, ulPubCnt, pPrivTpl, ulPrivCnt);
keybits = SC_PKCS15_GOSTR3410_KEYSIZE;
memcpy(&keygen_args.prkey_args.key.u.gostr3410.params.key, kp, 8*sizeof(kp[0]));
memcpy(&keygen_args.prkey_args.key.u.gostr3410.params.hash, hp, 8*sizeof(kp[0]));
Copy link
Member

Choose a reason for hiding this comment

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

an int is not guaranteed to have exactly 8 bytes.

else if (!strcmp("B", p_param_set))
memcpy(gost_params, (CK_BYTE[]) {0x06, 0x07, 0x2a, 0x85, 0x03, 0x02, 0x02, 0x23, 0x02}, PARAMS_SIZE);
else if (!strcmp("C", p_param_set))
memcpy(gost_params, (CK_BYTE[]) {0x06, 0x07, 0x2a, 0x85, 0x03, 0x02, 0x02, 0x23, 0x03}, PARAMS_SIZE);
Copy link
Member

Choose a reason for hiding this comment

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

Please don't use magic values here; at least use a good name for them.

@@ -2741,11 +2741,17 @@ pkcs15_gen_keypair(struct sc_pkcs11_slot *slot, CK_MECHANISM_PTR pMechanism,
goto kpgen_done;

if (keytype == CKK_GOSTR3410) {
unsigned int kp[] = {1, 2, 643, 2, 2, 35, 1, (unsigned int)-1};
unsigned int hp[] = {1, 2, 643, 2, 2, 30, 1, (unsigned int)-1};
Copy link
Member

Choose a reason for hiding this comment

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

please use a better name for your OIDs.

Why are they needed on the PKCS#11 level?

@frankmorgner
Copy link
Member

Please also check the CI errors

@konstantinpersidskiy
Copy link
Contributor Author

Thanks for the review!
I improved the things you pointed to (and some others). Now the code is more readable and changes are placed in right components.

@frankmorgner
Copy link
Member

Thanks for the update, please have a look at the CI error.

@@ -856,6 +856,19 @@ sc_pkcs15_card_clear(struct sc_pkcs15_card *p15card)
}


int sc_copy_gost_params(struct sc_pkcs15_gost_parameters *dst, struct sc_pkcs15_gost_parameters *src)
Copy link
Member

Choose a reason for hiding this comment

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

Why don't you move this implementation to src/pkcs15init/pkcs15-lib.c, which is the only place where this is used?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, you are right. Moved it.

@konstantinpersidskiy
Copy link
Contributor Author

@frankmorgner , I've made changes to the code you mentioned, can you please check it out?

@frankmorgner
Copy link
Member

can you be more specific? CI errors were fixed with 49d876b, but I think you didn't look at the length check.

@konstantinpersidskiy
Copy link
Contributor Author

@frankmorgner , I think i don't understand what you mean by "length check". I thought I've fixed all the code you asked in the series of commits (and the last commit also passes CI checks).
If I collapse these commits in one resulting commit, will that be OK? Is this what you mean? Or otherwise could you please point out what exactly is wrong with my code so I can fix that?

if (second_params)
second_params->gostr3410 = gostr3410_param_oid[i].param;
for (param_index = 0; param_index < nn; ++param_index) {
if (!memcmp(gost_params_oid_from_template, gostr3410_param_oid[param_index].encoded_oid, len))
Copy link
Member

Choose a reason for hiding this comment

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

I think you need to check len != (sizeof gostr3410_param_oid[param_index].encoded_oid).

return CKR_ATTRIBUTE_VALUE_INVALID;

for (hash_index = 0; hash_index < nn; ++hash_index) {
if (!memcmp(gost_hash_params_oid_from_template, gostr3410_hash_param_oid[hash_index].encoded_oid, len))
Copy link
Member

Choose a reason for hiding this comment

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

I think you need to check len != (sizeof gostr3410_param_oid[param_index].encoded_oid).

@frankmorgner
Copy link
Member

sorry, I just realized that the comments were pending and I never finished the review...

@frankmorgner frankmorgner added this to In Progress in Release 0.17.0 May 31, 2017
@frankmorgner frankmorgner merged commit 083cec8 into OpenSC:master Jun 9, 2017
@frankmorgner frankmorgner moved this from In Progress to Done in Release 0.17.0 Jun 9, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
No open projects
Development

Successfully merging this pull request may close these issues.

None yet

2 participants