Update CLI -pass option

This commit is contained in:
Zhi Guan
2026-06-26 14:23:08 +08:00
parent d804ea4e9b
commit e43f9e306d
36 changed files with 602 additions and 115 deletions

View File

@@ -19,6 +19,7 @@
#include <gmssl/x509.h>
#include <gmssl/x509_req.h>
#include <gmssl/x509_alg.h>
#include "passwd.h"
static const char *options =
@@ -39,7 +40,7 @@ static char *usage =
" * xmss-hashsig\n"
" * xmssmt-hashsig\n"
" * shpincs-hashsig\n"
" -pass pass Password for decrypting private key file\n"
" -pass pass Password for decrypting private key file, prompt if not given\n"
" -sig_alg str Signature algorithm OID name, default sm2sign-with-sm3\n"
" -sm2_id str Signer's ID in SM2 signature algorithm\n"
" -sm2_id_hex hex Signer's ID in hex format\n"
@@ -90,7 +91,9 @@ int reqgen_main(int argc, char **argv)
// Private Key
FILE *keyfp = NULL;
char *keyfile = NULL;
char *pass = NULL;
char passbuf[GMSSL_PASSWORD_MAX_SIZE] = {0};
X509_KEY x509_key;
int algor = OID_ec_public_key;
int sign_algor = OID_sm2sign_with_sm3;
@@ -140,6 +143,7 @@ int reqgen_main(int argc, char **argv)
} else if (!strcmp(*argv, "-key")) {
if (--argc < 1) goto bad;
str = *(++argv);
keyfile = str;
if (!(keyfp = fopen(str, "rb"))) {
fprintf(stderr, "%s: open '%s' failure : %s\n", prog, str, strerror(errno));
goto end;
@@ -213,9 +217,10 @@ bad:
}
if (!pass && algor == OID_ec_public_key) {
fprintf(stderr, "%s: `-pass` option required\n", prog);
printf("usage: gmssl %s %s\n\n", prog, options);
goto end;
if (gmssl_tool_get_password(prog, "Password to decrypt private key", keyfile, &pass,
passbuf, sizeof(passbuf)) != 1) {
goto end;
}
}
if (x509_private_key_from_file(&x509_key, algor, pass, keyfp) != 1) {
@@ -251,6 +256,7 @@ bad:
ret = 0;
end:
x509_key_cleanup(&x509_key);
gmssl_secure_clear(passbuf, sizeof(passbuf));
if (keyfp) fclose(keyfp);
if (outfile && outfp) fclose(outfp);
return ret;