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

@@ -14,14 +14,15 @@
#include <stdlib.h>
#include <gmssl/mem.h>
#include <gmssl/sm2.h>
#include "passwd.h"
static const char *usage = "-pass str [-out pem] [-pubout pem]\n";
static const char *usage = "[-pass str] [-out pem] [-pubout pem]\n";
static const char *options =
"Options\n"
"\n"
" -pass pass Password to encrypt the private key\n"
" -pass pass Password to encrypt the private key, prompt if not given\n"
" -out pem Output password-encrypted PKCS #8 private key in PEM format\n"
" -pubout pem Output public key in PEM format\n"
"\n"
@@ -36,6 +37,7 @@ int sm2keygen_main(int argc, char **argv)
int ret = 1;
char *prog = argv[0];
char *pass = NULL;
char passbuf[GMSSL_PASSWORD_MAX_SIZE] = {0};
char *outfile = NULL;
char *puboutfile = NULL;
FILE *outfp = stdout;
@@ -85,8 +87,8 @@ bad:
argv++;
}
if (!pass) {
fprintf(stderr, "gmssl %s: `-pass` option required\n", prog);
if (gmssl_tool_get_password(prog, "Password to encrypt private key", outfile, &pass,
passbuf, sizeof(passbuf)) != 1) {
goto end;
}
@@ -100,6 +102,7 @@ bad:
end:
gmssl_secure_clear(&key, sizeof(key));
gmssl_secure_clear(passbuf, sizeof(passbuf));
if (outfile && outfp) fclose(outfp);
if (puboutfile && puboutfp) fclose(puboutfp);
return ret;