Add sdfsign command

This commit is contained in:
Zhi Guan
2024-06-07 19:45:10 +08:00
parent 4363740477
commit c4e5cf0deb
6 changed files with 339 additions and 164 deletions

View File

@@ -16,22 +16,19 @@
#include <gmssl/sdf.h>
static const char *usage = "-lib so_path {-sign_public_key|-encrypt_public_key} -index num [-out file]";
static const char *usage = "-lib so_path -key num [-out file]";
static const char *options =
"\n"
"Options\n"
"\n"
" -lib so_path Vendor's SDF dynamic library\n"
" -sign_public_key Export signing public key\n"
" -encrypt_public_key Export encryption public key\n"
" -index num Private key index number\n"
" -key num Private key index number\n"
" -out file | stdout Output public key in PEM format\n"
"\n"
"Examples\n"
"\n"
" $ gmssl sdfexport -sign_public_key -index 1 -out sm2signpub.pem\n"
" $ gmssl sdfexport -encrypt_public_key -index 1 -out sm2encpub.pem\n"
" $ gmssl sdfexport -key 1 -out sm2signpub.pem\n"
"\n";
@@ -65,19 +62,7 @@ int sdfexport_main(int argc, char **argv)
} else if (!strcmp(*argv, "-lib")) {
if (--argc < 1) goto bad;
lib = *(++argv);
} else if (!strcmp(*argv, "-sign_public_key")) {
if (enc_public_key) {
fprintf(stderr, "gmssl %s: '-sign_public_key' and '-encrypt_public_key' should not used together\n", prog);
goto end;
}
sign_public_key = 1;
} else if (!strcmp(*argv, "-encrypt_public_key")) {
if (sign_public_key) {
fprintf(stderr, "gmssl %s: '-sign_public_key' and '-encrypt_public_key' should not used together\n", prog);
goto end;
}
enc_public_key = 1;
} else if (!strcmp(*argv, "-index")) {
} else if (!strcmp(*argv, "-key")) {
if (--argc < 1) goto bad;
index = atoi(*(++argv));
if (index < 0) {
@@ -107,10 +92,6 @@ bad:
fprintf(stderr, "gmssl %s: option '-lib' required\n", prog);
goto end;
}
if (!sign_public_key && !enc_public_key) {
fprintf(stderr, "gmssl %s: option '-sign_public_key' or '-encrypt_public_key' is required\n", prog);
goto end;
}
if (index < 0) {
fprintf(stderr, "gmssl %s: '-index' option required\n", prog);
goto end;
@@ -125,18 +106,10 @@ bad:
goto end;
}
if (sign_public_key) {
if (sdf_export_sign_public_key(&dev, index, &sm2_key) != 1) {
fprintf(stderr, "%s: load sign key failed\n", prog);
goto end;
}
} else {
if (sdf_export_enc_public_key(&dev, index, &sm2_key) != 1) {
fprintf(stderr, "%s: load sign key failed\n", prog);
goto end;
}
if (sdf_export_sign_public_key(&dev, index, &sm2_key) != 1) {
fprintf(stderr, "%s: load sign key failed\n", prog);
goto end;
}
if (sm2_public_key_info_to_pem(&sm2_key, outfp) != 1) {
fprintf(stderr, "gmssl %s: output public key to PEM failed\n", prog);
goto end;