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

私钥密码中包含"^"时无法使用sm2_private_key_info_decrypt_from_pem读取证书 #1688

Open
finaldusk opened this issue May 30, 2024 · 3 comments

Comments

@finaldusk
Copy link

证书生成命令:gmssl.exe sm2keygen -pass v^2 -out D:\key\private -pubout D:\key\public

私钥内容:

-----BEGIN ENCRYPTED PRIVATE KEY-----
MIIBBjBhBgkqhkiG9w0BBQ0wVDA0BgkqhkiG9w0BBQwwJwQQ1DT1GCM+nUn11JDz
Q6aCGwIDAQAAAgEQMAsGCSqBHM9VAYMRAjAcBggqgRzPVQFoAgQQWJl4ueiEXtGS
Ywtjhs1aRASBoO1OEwFk7P6R/HDZBBDXaaOY/gte3akpQ1EzWK19C4x7HkVxrOG5
iLy7y7BEvkzWJPiyeUTdC8MsJWfFV+d7PZIX9CZZfkvlO5heVnUIKnQRhgWJJOAA
k9HXZ+c0Xu2OE3g1IqfYzm4cQbWX8c/1bGl3eK9URbaCDUCXfg9jH0ABcz3rvzs0
UwKTlLz5wao0ZUIb8X46IfTHs46tMDCrBkA=
-----END ENCRYPTED PRIVATE KEY-----

读取私钥代码:

	SM2_KEY sm2_key;
	const char* password = "v^2";
	unsigned char buf[512];
	unsigned char* p;
	size_t len;

	FILE *f;
	fopen_s(&f,"D:\\key\\private", "r");

	printf("Read SM2 private key file (PEM) from stdin ...\n");
	if (sm2_private_key_info_decrypt_from_pem(&sm2_key, password, f) != 1) {
		fprintf(stderr, "error\n");
		return 1;
	}

	p = buf;
	len = 0;
	if (sm2_private_key_to_der(&sm2_key, &p, &len) != 1) {
		fprintf(stderr, "error\n");
		return 1;
	}
	format_bytes(stdout, 0, 0, "buf", buf, len);
	sm2_key_print(stdout, 0, 0, "SM2PrivateKey", &sm2_key);

输出信息:

Read SM2 private key file (PEM) from stdin ...
C:\GmSSL-3.1.1\src\sm4_modes.c:86:sm4_cbc_padding_decrypt():
C:\GmSSL-3.1.1\src\sm2_key.c:640:sm2_private_key_info_decrypt_from_der():
C:\GmSSL-3.1.1\src\sm2_key.c:687:sm2_private_key_info_decrypt_from_pem():
error

测试时发现当密码中包含"^"时会触发此问题。
另外当直接使用"^"作为密码时会报错:

gmssl.exe sm2keygen -pass ^ -out D:\key\private -pubout D:\key\public
sm2keygen: illegal option 'D:\key\private'
@SmartisanR1
Copy link

你是使用的哪个版本?我用的发行版3.1.1没有这个问题,我在Linux下测试的

@lpilp
Copy link

lpilp commented Jun 4, 2024

你是使用的哪个版本?我用的发行版3.1.1没有这个问题,我在Linux下测试的

楼主使用的是winodws下的cmd, 在cmd下 “^” 有特殊含义,在命令行中间的话会被忽略,楼主的例子中那个私钥使用密码: v2 是可以解出来的,如确实需要使用带“^”的密码,在生成密钥的时候添加双引号就可 gmssl.exe sm2keygen -pass “v^2” -out D:\key\private -pubout D:\key\public 或使用 powershell ,gitbash也可以

@finaldusk
Copy link
Author

已确认为windows 下cmd问题与本仓库代码无关

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

No branches or pull requests

3 participants