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

Which SM2 private key PEM file generated by openssl 3.x is correct? #20973

Open
Eno-CN opened this issue May 16, 2023 · 7 comments
Open

Which SM2 private key PEM file generated by openssl 3.x is correct? #20973

Eno-CN opened this issue May 16, 2023 · 7 comments
Assignees
Labels
inactive triaged: question The issue contains a question

Comments

@Eno-CN
Copy link

Eno-CN commented May 16, 2023

OpenSSL Version

OpenSSL 3.1.0 14 Mar 2023 (Library: OpenSSL 3.1.0 14 Mar 2023)

Description

I generated two different SM2 private key PEM files using two different methods.
When I use a tool to parse their ASN1 structures, I found that their OID identifiers are different, so I don't know which PEM file is generated correctly.

First Method

openssl genpkey -algorithm sm2 -text

PEM content:

-----BEGIN PRIVATE KEY-----
MIGIAgEAMBQGCCqBHM9VAYItBggqgRzPVQGCLQRtMGsCAQEEIPwnYlC0IWz13PYD
xisvfTJmzqJaKvVffqr0PGeQlEa9oUQDQgAEokJlXF9eQgJO6hvnvps0YQ8HPZot
6MCvh13WqDddbpZ0qey387GJ2PFBUghhucF9bu0bBZa52a6/Dbqc9gYfyA==
-----END PRIVATE KEY-----

ASN1 Structure:
image

Second Method

openssl genpkey -algorithm ec -pkeyopt ec_paramgen_curve:SM2 -text

PEM content:

-----BEGIN PRIVATE KEY-----
MIGHAgEAMBMGByqGSM49AgEGCCqBHM9VAYItBG0wawIBAQQgAzy83ltheFTNmJZX
bA9uR0d4hclB6BHdM3lnPglCLr6hRANCAARMGqLrNj095QswcJR3CEAZ2dvpAvyE
bWMBdn0woEtx9gjt+B6UperoEJfUVqm0g9VSdPrQbQEUcJzoq22b2eu9
-----END PRIVATE KEY-----

ASN1 Structure:
image

Additional Notes

However, I know that many applications in China support the second method of generating PEM files and cannot recognize the first PEM file.
The ASN1 structure of the SM2 private key PEM file generated by version 1.1.1 is the same as the one generated by the second method.

@Eno-CN Eno-CN added the issue: question The issue was opened to ask a question label May 16, 2023
@t8m t8m added triaged: question The issue contains a question and removed issue: question The issue was opened to ask a question labels May 16, 2023
@t8m
Copy link
Member

t8m commented May 16, 2023

Maybe @InfoHunter could answer this?

@Eno-CN
Copy link
Author

Eno-CN commented May 21, 2023

@InfoHunter Could you please help me answer a question if you have some time?

@zzl360
Copy link

zzl360 commented Jun 5, 2023

I think #11328 could explained it.
In fact, the second method is the right way to satisfy chinese standard like GM/T 0015-2012 《Digital certificate format based on sm2 algorithm》。
this is one piece from that standard.
image

@Eno-CN
Copy link
Author

Eno-CN commented Jun 5, 2023

I think #11328 could explained it.
In fact, the second method is the right way to satisfy chinese standard like GM/T 0015-2012 《Digital certificate format based on sm2 algorithm》。
this is one piece from that standard.
image

Ok, thank you so much.

@slontis
Copy link
Member

slontis commented Dec 6, 2023

I thought it was already established that 2 was correct. This will also interop with Bouncy Castle if we revert it back to this..

@lirc571
Copy link

lirc571 commented Apr 19, 2024

In the same document (GM/T 0015-2012), table C.3 uses method 1's format, although it seems that most implementations use method 2.

@nhorman
Copy link
Contributor

nhorman commented Jun 7, 2024

It seems like there is consensus that method 2 is correct, despite some documentation still using method 1.

As such, marking this as inactive, to be closed at the end of 3.4. Please comment further if there is more discussion needed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
inactive triaged: question The issue contains a question
Projects
None yet
Development

No branches or pull requests

7 participants