mirror of
https://github.com/guanzhi/GmSSL.git
synced 2026-05-12 11:26:25 +08:00
Update sdf and skf commands
This commit is contained in:
@@ -199,7 +199,8 @@ int pkeyutl_main(int argc, char **argv)
|
||||
if (argc != 0)
|
||||
goto opthelp;
|
||||
|
||||
//BIO_printf(bio_err, "Using configuration from %s\n", configfile);
|
||||
if (e)
|
||||
BIO_printf(bio_err, "Using configuration from %s\n", configfile);
|
||||
|
||||
if ((conf = app_load_config(configfile)) == NULL)
|
||||
goto end;
|
||||
|
||||
254
apps/sdf.c
254
apps/sdf.c
@@ -65,28 +65,40 @@ NON_EMPTY_TRANSLATION_UNIT
|
||||
|
||||
typedef enum OPTION_choice {
|
||||
OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
|
||||
OPT_SO_PATH, OPT_DEVINFO, OPT_KEY, OPT_PASS,
|
||||
OPT_IMPORT, OPT_EXPORT, OPT_DELETE, OPT_LABEL,
|
||||
OPT_LIB, OPT_VENDOR, OPT_PRINTDEVINFO,
|
||||
OPT_PRINTSM2SIGN, OPT_PRINTSM2ENC,
|
||||
OPT_PRINTRSASIGN, OPT_PRINTRSAENC,
|
||||
OPT_ACCESSKEY, OPT_PASS,
|
||||
OPT_IMPORTOBJ, OPT_EXPORTOBJ, OPT_DELOBJ,
|
||||
OPT_IN, OPT_OUT
|
||||
} OPTION_CHOICE;
|
||||
|
||||
# define FILE_OP_NONE 0
|
||||
# define FILE_OP_IMPORT 1
|
||||
# define FILE_OP_EXPORT 2
|
||||
# define FILE_OP_DELETE 3
|
||||
|
||||
# define DEFAULT_SO_PATH "libsdf.so"
|
||||
# define OP_NONE 0
|
||||
# define OP_PRINTDEVINFO 1
|
||||
# define OP_PRINTSM2SIGN 2
|
||||
# define OP_PRINTSM2ENC 3
|
||||
# define OP_PRINTRSASIGN 4
|
||||
# define OP_PRINTRSAENC 5
|
||||
# define OP_ACCESSKEY 6
|
||||
# define OP_IMPORTOBJ 7
|
||||
# define OP_EXPORTOBJ 8
|
||||
# define OP_DELOBJ 9
|
||||
|
||||
OPTIONS sdf_options[] = {
|
||||
{"help", OPT_HELP, '-', "Display this summary"},
|
||||
{"so_path", OPT_SO_PATH, 's', "Vendor's dynamic library"},
|
||||
{"devinfo", OPT_DEVINFO, '-', "Print device information"},
|
||||
{"key", OPT_KEY, 's', "Access private key with the key index"},
|
||||
{"pass", OPT_PASS, 's', "Passphrase source"},
|
||||
{"import", OPT_IMPORT, '-', "Import data object into device"},
|
||||
{"export", OPT_EXPORT, '-', "Export data object from device"},
|
||||
{"delete", OPT_DELETE, '-', "Delete data object from device"},
|
||||
{"label", OPT_LABEL, 's', "Data object label"},
|
||||
{"lib", OPT_LIB, 's', "Vendor's SDF dynamic library"},
|
||||
{"vendor", OPT_VENDOR, 's', "Vendor name"},
|
||||
{"printdevinfo", OPT_PRINTDEVINFO, '-', "Print device information"},
|
||||
{"printsm2sign", OPT_PRINTSM2SIGN, 's', "Print SM2 signing key with key index"},
|
||||
{"printsm2enc", OPT_PRINTSM2ENC, 's', "Print SM2 encryption key with key index"},
|
||||
{"printrsasign", OPT_PRINTRSASIGN, 's', "Print RSA signing key with key index"},
|
||||
{"printrsaenc", OPT_PRINTRSAENC, 's', "Print RSA encryption key with key index"},
|
||||
{"accesskey", OPT_ACCESSKEY, 's', "Access private key with the key index number"},
|
||||
{"pass", OPT_PASS, 's', "Passphrase source for accessing private key"},
|
||||
{"importobj", OPT_IMPORTOBJ, 's', "Import data object into device"},
|
||||
{"exportobj", OPT_EXPORTOBJ, 's', "Export data object from device"},
|
||||
{"delobj", OPT_DELOBJ, 's', "Delete data object from device"},
|
||||
{"in", OPT_IN, '<', "File to be imported from"},
|
||||
{"out", OPT_OUT, '>', "File to be exported to"},
|
||||
{NULL}
|
||||
@@ -96,19 +108,16 @@ int sdf_main(int argc, char **argv)
|
||||
{
|
||||
int ret = 1;
|
||||
char *infile = NULL, *outfile = NULL, *prog;
|
||||
char *label = NULL, *passarg = NULL, *pass = NULL;
|
||||
char *objname = NULL, *passarg = NULL, *pass = NULL;
|
||||
BIO *in = NULL, *out = NULL;
|
||||
char *lib = NULL, *vendor = NULL;
|
||||
unsigned char *buf = NULL;
|
||||
unsigned int ulen;
|
||||
int len, key_idx;
|
||||
OPTION_CHOICE o;
|
||||
char *so_path = NULL;
|
||||
int print_devinfo = 0;
|
||||
int key_idx = -1;
|
||||
int file_op = FILE_OP_NONE;
|
||||
int op = OP_NONE;
|
||||
void *hDev = NULL;
|
||||
void *hSession = NULL;
|
||||
unsigned char *buf = NULL;
|
||||
int len = 0;
|
||||
int rv;
|
||||
unsigned int ulen;
|
||||
|
||||
prog = opt_init(argc, argv, sdf_options);
|
||||
while ((o = opt_next()) != OPT_EOF) {
|
||||
@@ -122,29 +131,64 @@ opthelp:
|
||||
opt_help(sdf_options);
|
||||
ret = 0;
|
||||
goto end;
|
||||
case OPT_SO_PATH:
|
||||
so_path = opt_arg();
|
||||
case OPT_LIB:
|
||||
lib = opt_arg();
|
||||
break;
|
||||
case OPT_DEVINFO:
|
||||
print_devinfo = 1;
|
||||
case OPT_VENDOR:
|
||||
vendor = opt_arg();
|
||||
break;
|
||||
case OPT_IMPORT:
|
||||
if (file_op)
|
||||
case OPT_PRINTDEVINFO:
|
||||
if (op)
|
||||
goto opthelp;
|
||||
file_op = FILE_OP_IMPORT;
|
||||
op = OP_PRINTDEVINFO;
|
||||
break;
|
||||
case OPT_EXPORT:
|
||||
if (file_op)
|
||||
case OPT_PRINTSM2SIGN:
|
||||
if (op)
|
||||
goto opthelp;
|
||||
file_op = FILE_OP_EXPORT;
|
||||
op = OP_PRINTSM2SIGN;
|
||||
key_idx = atoi(opt_arg());
|
||||
break;
|
||||
case OPT_DELETE:
|
||||
if (file_op)
|
||||
case OPT_PRINTSM2ENC:
|
||||
if (op)
|
||||
goto opthelp;
|
||||
file_op = FILE_OP_DELETE;
|
||||
op = OP_PRINTSM2ENC;
|
||||
key_idx = atoi(opt_arg());
|
||||
break;
|
||||
case OPT_LABEL:
|
||||
label = opt_arg();
|
||||
case OPT_PRINTRSASIGN:
|
||||
if (op)
|
||||
goto opthelp;
|
||||
op = OP_PRINTRSASIGN;
|
||||
key_idx = atoi(opt_arg());
|
||||
break;
|
||||
case OPT_PRINTRSAENC:
|
||||
if (op)
|
||||
goto opthelp;
|
||||
op = OP_PRINTRSAENC;
|
||||
key_idx = atoi(opt_arg());
|
||||
break;
|
||||
case OPT_ACCESSKEY:
|
||||
key_idx = atoi(opt_arg());
|
||||
break;
|
||||
case OPT_PASS:
|
||||
passarg = opt_arg();
|
||||
break;
|
||||
case OPT_IMPORTOBJ:
|
||||
if (op)
|
||||
goto opthelp;
|
||||
op = OP_IMPORTOBJ;
|
||||
objname = opt_arg();
|
||||
break;
|
||||
case OPT_EXPORTOBJ:
|
||||
if (op)
|
||||
goto opthelp;
|
||||
op = OP_EXPORTOBJ;
|
||||
objname = opt_arg();
|
||||
break;
|
||||
case OPT_DELOBJ:
|
||||
if (op)
|
||||
goto opthelp;
|
||||
op = OP_DELOBJ;
|
||||
objname = opt_arg();
|
||||
break;
|
||||
case OPT_IN:
|
||||
infile = opt_arg();
|
||||
@@ -152,32 +196,23 @@ opthelp:
|
||||
case OPT_OUT:
|
||||
outfile = opt_arg();
|
||||
break;
|
||||
case OPT_PASS:
|
||||
passarg = opt_arg();
|
||||
break;
|
||||
case OPT_KEY:
|
||||
if ((key_idx = atoi(opt_arg())) < 0) {
|
||||
BIO_printf(bio_err, "Invalid key index\n");
|
||||
goto end;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
argc = opt_num_rest();
|
||||
if (argc != 0)
|
||||
goto opthelp;
|
||||
|
||||
if (!so_path) {
|
||||
BIO_printf(bio_err, "Vendor's SDF dynmaic library required\n");
|
||||
if (!lib) {
|
||||
BIO_printf(bio_err, "Option '-lib' required\n");
|
||||
goto opthelp;
|
||||
}
|
||||
if (SDF_LoadLibrary(so_path, NULL) != SDR_OK) {
|
||||
if (SDF_LoadLibrary(lib, vendor) != SDR_OK) {
|
||||
ERR_print_errors(bio_err);
|
||||
goto end;
|
||||
}
|
||||
|
||||
/* no operation specified */
|
||||
if (!print_devinfo && key_idx < 0 && !file_op) {
|
||||
if (op == OP_NONE) {
|
||||
ret = 0;
|
||||
goto end;
|
||||
}
|
||||
|
||||
@@ -187,20 +222,84 @@ opthelp:
|
||||
goto end;
|
||||
}
|
||||
|
||||
if (print_devinfo) {
|
||||
DEVICEINFO devInfo;
|
||||
if (SDF_GetDeviceInfo(hSession, &devInfo) != SDR_OK
|
||||
|| SDF_PrintDeviceInfo(&devInfo) != SDR_OK) {
|
||||
ERR_print_errors(bio_err);
|
||||
goto end;
|
||||
switch (op) {
|
||||
case OP_PRINTDEVINFO:
|
||||
case OP_PRINTSM2SIGN:
|
||||
case OP_PRINTSM2ENC:
|
||||
case OP_PRINTRSASIGN:
|
||||
case OP_PRINTRSAENC:
|
||||
if (!(out = bio_open_default(outfile, 'w', FORMAT_TEXT))) {
|
||||
goto opthelp;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
if (key_idx >= 0) {
|
||||
switch (op) {
|
||||
case OP_PRINTSM2SIGN:
|
||||
case OP_PRINTSM2ENC:
|
||||
case OP_PRINTRSASIGN:
|
||||
case OP_PRINTRSAENC:
|
||||
case OP_ACCESSKEY:
|
||||
if (key_idx < SDF_MIN_KEY_INDEX || key_idx > SDF_MAX_KEY_INDEX) {
|
||||
BIO_printf(bio_err, "Invalid key index\n");
|
||||
goto end;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
if (op == OP_PRINTDEVINFO) {
|
||||
DEVICEINFO devInfo;
|
||||
if (SDF_GetDeviceInfo(hSession, &devInfo) != SDR_OK
|
||||
|| SDF_PrintDeviceInfo(out, &devInfo) != SDR_OK) {
|
||||
ERR_print_errors(bio_err);
|
||||
goto end;
|
||||
}
|
||||
|
||||
} else if (op == OP_PRINTSM2SIGN || op == OP_PRINTSM2ENC) {
|
||||
ECCrefPublicKey publicKey;
|
||||
if (op == OP_PRINTSM2SIGN) {
|
||||
if (SDF_ExportSignPublicKey_ECC(hSession,
|
||||
key_idx, &publicKey) != SDR_OK) {
|
||||
ERR_print_errors(bio_err);
|
||||
goto end;
|
||||
}
|
||||
BIO_puts(out, "SM2 Signing Public Key:\n");
|
||||
} else {
|
||||
if (SDF_ExportEncPublicKey_ECC(hSession,
|
||||
key_idx, &publicKey) != SDR_OK) {
|
||||
ERR_print_errors(bio_err);
|
||||
goto end;
|
||||
}
|
||||
BIO_puts(out, "SM2 Encryption Public Key:\n");
|
||||
}
|
||||
if (SDF_PrintECCPublicKey(out, &publicKey) != SDR_OK) {
|
||||
ERR_print_errors(bio_err);
|
||||
goto end;
|
||||
}
|
||||
|
||||
} else if (op == OP_PRINTRSASIGN || op == OP_PRINTRSAENC) {
|
||||
RSArefPublicKey publicKey;
|
||||
if (op == OP_PRINTRSASIGN) {
|
||||
if (SDF_ExportSignPublicKey_RSA(hSession,
|
||||
key_idx, &publicKey) != SDR_OK) {
|
||||
ERR_print_errors(bio_err);
|
||||
goto end;
|
||||
}
|
||||
BIO_puts(out, "RSA Signing Public Key:\n");
|
||||
} else {
|
||||
if (SDF_ExportEncPublicKey_RSA(hSession,
|
||||
key_idx, &publicKey) != SDR_OK) {
|
||||
ERR_print_errors(bio_err);
|
||||
goto end;
|
||||
}
|
||||
BIO_puts(out, "RSA Encryption Public Key:\n");
|
||||
}
|
||||
if (SDF_PrintRSAPublicKey(out, &publicKey) != SDR_OK) {
|
||||
ERR_print_errors(bio_err);
|
||||
goto end;
|
||||
}
|
||||
|
||||
} else if (op == OP_ACCESSKEY) {
|
||||
if (!app_passwd(passarg, NULL, &pass, NULL)) {
|
||||
BIO_printf(bio_err, "Error getting password\n");
|
||||
goto end;
|
||||
@@ -210,15 +309,10 @@ opthelp:
|
||||
OPENSSL_cleanse(pass, sizeof(pass));
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
(void)SDF_ReleasePrivateKeyAccessRight(hSession, (unsigned int)key_idx);
|
||||
BIO_printf(bio_err, "Access private key %d success\n", key_idx);
|
||||
|
||||
if (file_op && !label) {
|
||||
BIO_printf(bio_err, "Data object label is not assigned\n");
|
||||
goto end;
|
||||
}
|
||||
|
||||
switch (file_op) {
|
||||
case FILE_OP_IMPORT:
|
||||
} else if (op == OP_IMPORTOBJ) {
|
||||
if (!(in = bio_open_default(infile, 'r', FORMAT_BINARY))) {
|
||||
goto opthelp;
|
||||
}
|
||||
@@ -226,34 +320,34 @@ opthelp:
|
||||
BIO_printf(bio_err, "Error reading data object content\n");
|
||||
goto end;
|
||||
}
|
||||
if (SDF_CreateFile(hSession, (unsigned char *)label, strlen(label), len) != SDR_OK
|
||||
|| SDF_WriteFile(hSession, (unsigned char *)label, strlen(label), 0, len, buf) != SDR_OK) {
|
||||
if (SDF_CreateFile(hSession, (unsigned char *)objname, strlen(objname), len) != SDR_OK
|
||||
|| SDF_WriteFile(hSession, (unsigned char *)objname, strlen(objname), 0, len, buf) != SDR_OK) {
|
||||
ERR_print_errors(bio_err);
|
||||
goto end;
|
||||
}
|
||||
break;
|
||||
BIO_printf(bio_err, "Object '%s' (%d bytes) created\n", objname, len);
|
||||
|
||||
case FILE_OP_EXPORT:
|
||||
} else if (op == OP_EXPORTOBJ) {
|
||||
if (!(out = bio_open_default(outfile, 'w', FORMAT_BINARY))) {
|
||||
goto opthelp;
|
||||
}
|
||||
if (!(buf = OPENSSL_zalloc(SDF_MAX_FILE_SIZE))
|
||||
|| SDF_ReadFile(hSession, (unsigned char *)label, strlen(label), 0, &ulen, buf) != SDR_OK
|
||||
|| SDF_ReadFile(hSession, (unsigned char *)objname, strlen(objname), 0, &ulen, buf) != SDR_OK
|
||||
|| BIO_write(out, buf, ulen) != ulen) {
|
||||
ERR_print_errors(bio_err);
|
||||
goto end;
|
||||
}
|
||||
break;
|
||||
BIO_printf(bio_err, "Object '%s' (%u bytes) exported\n", objname, ulen);
|
||||
|
||||
case FILE_OP_DELETE:
|
||||
if (SDF_DeleteFile(hSession, (unsigned char *)label, strlen(label)) != SDR_OK) {
|
||||
} else if (op == OP_DELOBJ) {
|
||||
if (SDF_DeleteFile(hSession, (unsigned char *)objname, strlen(objname)) != SDR_OK) {
|
||||
ERR_print_errors(bio_err);
|
||||
goto end;
|
||||
}
|
||||
break;
|
||||
BIO_printf(bio_err, "Object '%s' deleted\n", objname);
|
||||
|
||||
case FILE_OP_NONE:
|
||||
break;
|
||||
} else {
|
||||
goto end;
|
||||
}
|
||||
|
||||
ret = 0;
|
||||
@@ -265,7 +359,7 @@ end:
|
||||
OPENSSL_free(pass);
|
||||
if (hSession) (void)SDF_CloseSession(hSession);
|
||||
if (hDev) (void)SDF_CloseDevice(hDev);
|
||||
if (so_path) SDF_UnloadLibrary();
|
||||
if (lib) SDF_UnloadLibrary();
|
||||
return ret;
|
||||
}
|
||||
#endif
|
||||
|
||||
992
apps/skf.c
992
apps/skf.c
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user