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

Fix the encoding of SM2 keys #22529

Closed
wants to merge 6 commits into from

Conversation

levitte
Copy link
Member

@levitte levitte commented Oct 27, 2023

OpenSSL's encoding of SM2 keys used the SM2 OID for the algorithm OID
where an AlgorithmIdentifier is encoded (for encoding into the structures
PrivateKeyInfo and SubjectPublicKeyInfo).

Such keys should be encoded as ECC keys.

Fixes #22184

@levitte levitte added branch: master Merge to master branch branch: 3.0 Merge to openssl-3.0 branch branch: 3.1 Merge to openssl-3.1 branch: 3.2 Merge to openssl-3.2 labels Oct 27, 2023
@levitte
Copy link
Member Author

levitte commented Oct 27, 2023

This may have unforeseen consequences for our decoders. I haven't
investigated that yet, and this PR will remain a draft until I have.

@mattcaswell mattcaswell added the hold: need otc decision The OTC needs to make a decision label Oct 27, 2023
@mattcaswell
Copy link
Member

I was under the impression that the SM2 OID is the preferred way to do this now - but maybe we're the only tool actually using it. It might be we need some option for this for interoperability purposes.

OTC Question: Which OID should we be using for the SM2 AlgorithmIdentifier?

@levitte
Copy link
Member Author

levitte commented Oct 27, 2023

A question is also, how does 1.1.1 encode SM2 keys? (I haven't looked at that for a few years and can't remember for the life of me)

So the question is, have we caused a regression of some sort? Compared to our own older versions? Compared to the rest of the world (#22184 at least indicates that we don't agree with what others expect)? Compared to actual standards?

@mattcaswell
Copy link
Member

Compared to actual standards?

I don't think there is a standard for this AFAIK - which is part of the problem (or at least there wasn't - maybe there is now somewhere).

@levitte
Copy link
Member Author

levitte commented Oct 27, 2023

Compared to actual standards?

I don't think there is a standard for this AFAIK - which is part of the problem (or at least there wasn't - maybe there is now somewhere).

Well, yes and no. SM2 is piggy backed on ECC, and the key itself is really an ECC key, just with a very specific curve, and with a slightly different algorithm to sign and verify.

So I'm leaning on this being a confusion, at least in terms of AlgorithmIndentifier. "there is no standard" could very well be "there is an ECC encoding standard, use that".

@romen, could I ask for a comment from you?

@levitte
Copy link
Member Author

levitte commented Oct 27, 2023

Seeing further discussions in #22184, and digging into what standards we have available, this PR may turn out to be quite wrong.

@slontis
Copy link
Member

slontis commented Oct 29, 2023

Any thoughts on this @dghgit?

@romen
Copy link
Member

romen commented Oct 30, 2023

@levitte maybe this can also help?

https://gist.github.com/romen/d752b8d1897bc1a0009017511770de06

As far as I recall, the encoding of SM2 keys, for compatibility with 1.1.1 and the other software stacks that use it, SHOULD use the EC OID even though they are not ECDSA keys: the only way to distinguish that one should use the SM2DSA algorithm rather than proper ECDSA is the combination of ECC OID and then the SM2 OID for the named curve inside the envelope.

I have complained about the mess that the poor outcome of the SM2 standardization reached, and One could argue that it is actually violating the ECC standards, by abusing the OID reserved for ECC for an algorithm other than ECDSA/DH.
This is clear when thinking that one could use either the ECDSA or SM2DSA algorithm on the SM2 curve, leading to different results, and there isn't really a way to distinguish between the 2 things, given SM2DSA key encodings abuse the ECDSA OID.

@t8m t8m added the triaged: bug The issue/pr is/fixes a bug label Oct 30, 2023
@dghgit
Copy link

dghgit commented Oct 31, 2023

So we've always done it as an EC key with the regular EC OID, years ago there was an attempt to produce RFCs for these algorithms and that's what was in used at the time. SM2 signing is also carried out using P-256 not just the specific curve, so it makes sense that it the SM2 curve was designed as an EC curve first. Since we first provided SM2 we have had feedback about some unexpected surprises in the algorithm (such as the default ID string) in order to make our generated certificates compliant with the Chinese based CAs, but nothing related to the manner we are encoding public keys.

@levitte
Copy link
Member Author

levitte commented Oct 31, 2023

I did a quick bit of reading up, and based on RFC 5915 "Elliptic Curve Private Key Structure" - 1. Introduction and RFC 5480 "ECC SubjectPublicKeyInfo Format" - 2.1. Elliptic Curve Cryptography Public Key Algorithm Identifiers, one might draw the conclusion that either encoding could be seen as legitimate:

  • With the AlgorithmIdentifier.algorithm OID id-ecPublicKey (1.2.840.10045.2.1), it would be an unrestricted ECC key that just happens to be in the SM2 curve.

  • With the AlgorithmIdentifier.algorithm OID SM2 (1.2.156.10197.1.301), it would be an ECC key restricted to be used with the SM2 signature scheme, the same way that id-ecDH restricts the use to the ECDH key exchange scheme, or id-ecMQV restricts the use to the ECMQV key agreement scheme.

Am I reading this right? It does seem to leave things up to some level of interpretation, and makes it harder to come with a categorically correct answer...

@levitte
Copy link
Member Author

levitte commented Nov 4, 2023

In the discussion comment #22184 (reply in thread), a few appendices with example was pointed at. The example in appendix D.2 of GM/T 0015-2012 is the clearest to me, very clearly showing a public key that has the AlgorithmIdentifier.algorithm set to ecPublicnumber, not SM2.

@levitte levitte marked this pull request as ready for review November 5, 2023 04:58
@t8m t8m added hold: needs tests The PR needs tests to be added to it and removed hold: need otc decision The OTC needs to make a decision labels Nov 7, 2023
@t8m
Copy link
Member

t8m commented Nov 7, 2023

OTC: This is a regression. We need to test the backwards compatibility with keys encoded both ways.

@InfoHunter
Copy link
Member

Deliberately moved away from using the ECC OID for SM2. The ECC OID is for ECDSA which SM2 is not. We used to use the ECC OID for this but I believe we deliberately changed it to use the SM2 OID.

This is also what I recall of move

@levitte
Copy link
Member Author

levitte commented Nov 7, 2023

The ECC OID is for ECDSA

Actually, this is what RFC 5480 "ECC SubjectPublicKeyInfo Format" - 2.1. Elliptic Curve Cryptography Public Key Algorithm Identifiers has to say on that specific topic:

  • id-ecPublicKey indicates that the algorithms that can be used with the subject public key are unrestricted. ...

It does go on to say that this is the OID used for ECDSA keys, but this introduction indicates that id-ecPublicKey isn't exclusively reserved for ECDSA.

And thank you @romen for your comment.

@mattcaswell
Copy link
Member

This is also what I recall of move

I think there is confusion between the OID used for signatures and the OID used for keys. So I now think my earlier recollection of this was about the OID for signatures. The commit which made the change for keys (f2db052) seems to make no mention of this change of behaviour and I now believe it was an accidental side effect.

@t8m
Copy link
Member

t8m commented Nov 22, 2023

This IMO also needs a CHANGES.md entry.

@t8m
Copy link
Member

t8m commented Nov 22, 2023

We also need #21037 and #20488

@levitte
Copy link
Member Author

levitte commented Nov 22, 2023

I disagree re #21037 (see comment I just made there)

@Eno-CN
Copy link

Eno-CN commented Dec 6, 2023

I want to know this. #20973

@levitte
Copy link
Member Author

levitte commented Jan 11, 2024

@slontis, I've added a CHANGES.md change

@slontis slontis added approval: done This pull request has the required number of approvals and removed approval: review pending This pull request needs review by a committer labels Jan 11, 2024
@mattcaswell mattcaswell added approval: review pending This pull request needs review by a committer and removed approval: done This pull request has the required number of approvals labels Jan 11, 2024
@mattcaswell
Copy link
Member

Needs reconfirmation from @t8m

@mattcaswell mattcaswell requested a review from t8m January 11, 2024 08:55
CHANGES.md Outdated Show resolved Hide resolved
@t8m
Copy link
Member

t8m commented Jan 11, 2024

Assuming @slontis approval holds.

@t8m t8m added approval: done This pull request has the required number of approvals and removed approval: review pending This pull request needs review by a committer labels Jan 11, 2024
@openssl-machine openssl-machine added approval: ready to merge The 24 hour grace period has passed, ready to merge and removed approval: done This pull request has the required number of approvals labels Jan 12, 2024
@openssl-machine
Copy link
Collaborator

This pull request is ready to merge

openssl-machine pushed a commit that referenced this pull request Jan 12, 2024
OpenSSL's encoding of SM2 keys used the SM2 OID for the algorithm OID
where an AlgorithmIdentifier is encoded (for encoding into the structures
PrivateKeyInfo and SubjectPublicKeyInfo).

Such keys should be encoded as ECC keys.

Fixes #22184

Reviewed-by: Shane Lontis <shane.lontis@oracle.com>
Reviewed-by: Tomas Mraz <tomas@openssl.org>
(Merged from #22529)

(cherry picked from commit 1d49069)
openssl-machine pushed a commit that referenced this pull request Jan 12, 2024
Reviewed-by: Shane Lontis <shane.lontis@oracle.com>
Reviewed-by: Tomas Mraz <tomas@openssl.org>
(Merged from #22529)

(cherry picked from commit d4d9b57)
openssl-machine pushed a commit that referenced this pull request Jan 12, 2024
OpenSSL's encoding of SM2 keys used the SM2 OID for the algorithm OID
where an AlgorithmIdentifier is encoded (for encoding into the structures
PrivateKeyInfo and SubjectPublicKeyInfo).

Such keys should be encoded as ECC keys.

Fixes #22184

Reviewed-by: Shane Lontis <shane.lontis@oracle.com>
Reviewed-by: Tomas Mraz <tomas@openssl.org>
(Merged from #22529)
openssl-machine pushed a commit that referenced this pull request Jan 12, 2024
Reviewed-by: Shane Lontis <shane.lontis@oracle.com>
Reviewed-by: Tomas Mraz <tomas@openssl.org>
(Merged from #22529)
openssl-machine pushed a commit that referenced this pull request Jan 12, 2024
OpenSSL's encoding of SM2 keys used the SM2 OID for the algorithm OID
where an AlgorithmIdentifier is encoded (for encoding into the structures
PrivateKeyInfo and SubjectPublicKeyInfo).

Such keys should be encoded as ECC keys.

Fixes #22184

Reviewed-by: Shane Lontis <shane.lontis@oracle.com>
Reviewed-by: Tomas Mraz <tomas@openssl.org>
(Merged from #22529)

(cherry picked from commit 1d49069)
openssl-machine pushed a commit that referenced this pull request Jan 12, 2024
Reviewed-by: Shane Lontis <shane.lontis@oracle.com>
Reviewed-by: Tomas Mraz <tomas@openssl.org>
(Merged from #22529)

(cherry picked from commit d4d9b57)
openssl-machine pushed a commit that referenced this pull request Jan 12, 2024
OpenSSL's encoding of SM2 keys used the SM2 OID for the algorithm OID
where an AlgorithmIdentifier is encoded (for encoding into the structures
PrivateKeyInfo and SubjectPublicKeyInfo).

Such keys should be encoded as ECC keys.

Fixes #22184

Reviewed-by: Shane Lontis <shane.lontis@oracle.com>
Reviewed-by: Tomas Mraz <tomas@openssl.org>
(Merged from #22529)

(cherry picked from commit 1d49069)
openssl-machine pushed a commit that referenced this pull request Jan 12, 2024
Reviewed-by: Shane Lontis <shane.lontis@oracle.com>
Reviewed-by: Tomas Mraz <tomas@openssl.org>
(Merged from #22529)

(cherry picked from commit d4d9b57)
@t8m
Copy link
Member

t8m commented Jan 12, 2024

Merged to master, 3.2, 3.1 and 3.0 branches. Thank you.

@t8m t8m closed this Jan 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approval: ready to merge The 24 hour grace period has passed, ready to merge branch: master Merge to master branch branch: 3.0 Merge to openssl-3.0 branch branch: 3.1 Merge to openssl-3.1 branch: 3.2 Merge to openssl-3.2 tests: present The PR has suitable tests present triaged: bug The issue/pr is/fixes a bug
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

None yet

10 participants