Skip to content

Conversation

@karel-m
Copy link
Member

@karel-m karel-m commented Apr 4, 2017

This PR was extracted from #65

I have written this code in 2014 (just an excuse in case it starts to seem that I am not completely sure what these changes are good for).

The main reason for this patch was ASN.1 support for importing/exporting EC keys which are stored in ASN.1 structures using esoteric stuff like context specific tags and optional items in ASN.1 sequences.

You will notice on the first sight the ugly duplication in API that I have introduced (to keep backwards compatibility of "non *_ex" functions):

der_decode_subject_public_key_info
der_decode_subject_public_key_info_ex

and

der_length_sequence
der_length_sequence_ex

@karel-m karel-m self-assigned this Apr 4, 2017
@karel-m karel-m requested a review from sjaeckel April 4, 2017 09:25
case LTC_ASN1_BOOLEAN:
z = inlen;
if ((err = der_decode_boolean(in + x, z, ((int *)data))) != CRYPT_OK) {
if (!ordered || list[i].optional) { continue; }
Copy link
Member

Choose a reason for hiding this comment

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

was this a bug before or this one now?

Copy link
Member Author

Choose a reason for hiding this comment

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

Well, I am not sure.

Copy link
Member Author

Choose a reason for hiding this comment

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

Looking at the code I believe that missing if (!ordered) { continue; } was a bug

goto LBL_ERR;
}
y = 0; z = 0;
if ((err = der_length_sequence_ex(list, inlen, &y, &z)) != CRYPT_OK) return CRYPT_INVALID_ARG;
Copy link
Member

Choose a reason for hiding this comment

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

👍 I like this change!

Copy link
Member Author

Choose a reason for hiding this comment

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

Can I extract this single change as a separate "small fix" PR?

Copy link
Member

Choose a reason for hiding this comment

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

sure!

case LTC_ASN1_CONTEXT_SPECIFIC:
case LTC_ASN1_EOL:
case LTC_ASN1_TELETEX_STRING:
default:
Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Member Author

Choose a reason for hiding this comment

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

fixed

@coveralls
Copy link

Coverage Status

Coverage decreased (-0.2%) to 87.605% when pulling c666bc4 on pr/ecc-asn1-part into fbe7d22 on develop.

@sjaeckel
Copy link
Member

sjaeckel commented Apr 4, 2017

IIUC this implements context-specific tag encoding differently than in the flexi decoder (it's stored in tag and we currently use the used field to store). Should we then also adjust the flexi decoder or is this not the same?

@karel-m
Copy link
Member Author

karel-m commented Apr 4, 2017

Ad context-specific tag encoding - when I added tag I probably did not notice that used might already exist for the same reason.

I am in no way an expert at ASN.1, when I created the patch back in 2014 the only goal was to be able to load/save EC keys which I somehow managed. But some parts are rather hacks/workarounds.

If you want some testing keys, go to https://github.com/DCIT/perl-CryptX/tree/master/t/data and take any of ECC related *.der files.

tmptag[4] = (unsigned char)(z&255);
y = 5;
}
memmove(out + x + y, out + x, z);
Copy link
Member

Choose a reason for hiding this comment

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

XMEMMOVE()?

@sjaeckel
Copy link
Member

sjaeckel commented Apr 5, 2017

Ad context-specific tag encoding - when I added tag I probably did not notice that used might already exist for the same reason.

As of the comment on the used field we could just re-use it instead of adding tag. Most likely it'd make sense to rename it to something else. Initially it was just kind of a hack so the struct definition won't change. As I don't see a way to somehow mangle the optional part without changing the struct I think a rename would be fine.

I am in no way an expert at ASN.1, when I created the patch back in 2014 the only goal was to be able to load/save EC keys which I somehow managed. But some parts are rather hacks/workarounds.

If you want some testing keys, go to https://github.com/DCIT/perl-CryptX/tree/master/t/data and take any of ECC related *.der files.

I'm also no expert at ASN.1 but I'll have a look when I have the time.

Would it probably make sense to split this PR up into two parts? One with the changes for tag and one for optional and 'the rest'?

break;
}

/* some items may be optional during import */
Copy link
Member

Choose a reason for hiding this comment

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

is this one correct? this looks fishy to me as the used field is only used when decoding but der_length_sequence_ex() is used when encoding

Copy link
Member Author

Choose a reason for hiding this comment

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

The der_length_sequence (which calls der_length_sequence_ex) is called during decoding - see der_decode_sequence_ex.c.

Possible theory: it seems that during decode operation I use optional to indicate that it is ok if the item is missing and used to indicate whether it was really decoded.

Copy link
Member

Choose a reason for hiding this comment

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

Yeah sure, I missed that. Sorry.

@sjaeckel sjaeckel modified the milestone: v2.0.0 Apr 7, 2017
@karel-m karel-m force-pushed the pr/ecc-asn1-part branch 4 times, most recently from b12bef7 to eb3efc3 Compare May 3, 2017 15:28
@karel-m karel-m force-pushed the pr/ecc-asn1-part branch from eb3efc3 to c258a84 Compare May 11, 2017 22:40
@sjaeckel sjaeckel modified the milestones: v1.18.0, next Jun 8, 2017
@karel-m karel-m force-pushed the pr/ecc-asn1-part branch 2 times, most recently from cb50664 to c4024b6 Compare June 14, 2017 07:31
@karel-m karel-m force-pushed the pr/ecc-asn1-part branch 2 times, most recently from a3114ec to f9f8364 Compare June 21, 2017 12:46
@karel-m karel-m force-pushed the pr/ecc-asn1-part branch from 5c3a455 to 1162ac9 Compare June 21, 2017 20:58
@karel-m karel-m changed the title ASN.1 changes needed for ECC enhancements ECC enhancements part 2 (ASN.1 changes) Jun 21, 2017
@karel-m
Copy link
Member Author

karel-m commented Jun 21, 2017

Now rebased on top of ECC enhancements part 1 (curves y^2 = x^3 + ax + b) #236

@karel-m karel-m force-pushed the pr/ecc-non-asn1-part branch 2 times, most recently from 54ba133 to e0c7170 Compare May 4, 2018 08:30
@karel-m karel-m force-pushed the pr/ecc-non-asn1-part branch from e0c7170 to 885f81d Compare May 5, 2018 16:03
@karel-m karel-m force-pushed the pr/ecc-non-asn1-part branch 2 times, most recently from 0cd0205 to 362275e Compare May 13, 2018 09:33
@karel-m karel-m force-pushed the pr/ecc-non-asn1-part branch from 362275e to 24c0eb8 Compare May 22, 2018 21:03
@karel-m karel-m changed the base branch from pr/ecc-non-asn1-part to develop June 3, 2018 17:59
@karel-m
Copy link
Member Author

karel-m commented Jun 3, 2018

@sjaeckel we should rebase this one onto a develop

I have created a branch pr/ecc-asn1-part-BACKUP because AFAIK you have some uncommitted stuff related to this PR.

@sjaeckel
Copy link
Member

sjaeckel commented Jun 3, 2018

IIUC that's only 1 commit which is left

and I've already rebased locally, so go ahead

@karel-m
Copy link
Member Author

karel-m commented Jun 3, 2018

Don't you have some pkcs8 changes/improvements locally?

I have not done any coding in this PR/branche since you have asked me whether you can work on pkcs8. Therefore I think it would be better if you push --force your local changes.

@sjaeckel
Copy link
Member

sjaeckel commented Jun 3, 2018

Don't you have some pkcs8 changes/improvements locally?

yeah, but nothing ready for publishing yet as I didn't really have the time ...

@karel-m karel-m force-pushed the pr/ecc-asn1-part branch from 24c3dea to dc396e8 Compare June 3, 2018 20:06
@karel-m
Copy link
Member Author

karel-m commented Jun 3, 2018

OK, now rebased.

Is it worth splitting this PR into:

  • ecc_export_openssl + ecc_import_openssl + ecc_import_x509 (which is IMO ready for being merged into develop, except maybe a discussion whether the name _openssl is proper here or we can find something better)
  • ecc_import_pkcs8 (here are definitely some opportunities for improvement + I think the rsa_import_pkcs8 deserves a similar facelift as well)

?

@karel-m karel-m force-pushed the pr/ecc-asn1-part branch 5 times, most recently from d9d203a to 50ffa85 Compare June 4, 2018 16:44
@karel-m
Copy link
Member Author

karel-m commented Jun 4, 2018

@sjaeckel could you please review: ecc_export_openssl + ecc_import_openssl + ecc_import_x509?

@karel-m karel-m force-pushed the pr/ecc-asn1-part branch 2 times, most recently from 2a446c5 to 95fe5ea Compare June 7, 2018 19:41
@karel-m
Copy link
Member Author

karel-m commented Jun 7, 2018

@sjaeckel I have moved pkcs8 stuff to #403 - could you please review/approve what's left in this PR? (ecc_export_openssl + ecc_import_openssl + ecc_import_x509)


len_xy = sizeof(bin_xy);
len_oid = 16;
err = x509_decode_subject_public_key_info(in, inlen, PKA_EC, bin_xy, &len_xy,
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 think about factoring-out those tries in their own functions?

_ecc_import_x509_with_oid()
_ecc_import_x509_with_curve()
ecc_import_subject_public_key_info() /* private API which calls the two above */
ecc_import_private_with_oid() /* private API */
ecc_import_private_with_curve() /* private API */

This would also allow to call ecc_import_subject_public_key_info() from ecc_import_x509() instead of this multi-purpose import function.

Copy link
Member Author

Choose a reason for hiding this comment

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

Frankly, it looks like too much work for more of less no gain. As it will not be part of public API we can change it any-time later.

My doubts here are more about whether ecc_export_openssl or ecc_export_<something_else> (and the same for ecc_import_openssl) would be better. Or generally whether the new API is fine as changing/renaming it later will be a pain.

As you can see from the code the available options for saving/loading an ecc key are pretty wide. For ecc_import_openssl I have chosen the approach "load whatever DER the openssl is able to save" therefore the name and therefore a single, maybe slightly overloaded, import function.

Copy link
Member

Choose a reason for hiding this comment

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

Frankly, it looks like too much work for more of less no gain

I think 30 minutes for a bit more readability is well invested.

Why I really did it was that I didn't like the fact that it would've been possible to import a private-key from a SubjectPublicKeyInfo.

Copy link
Member Author

Choose a reason for hiding this comment

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

👍 Looks good. Thank you.

@sjaeckel sjaeckel merged commit 6238b63 into develop Jun 11, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants