Add sdf and skf options

This commit is contained in:
Zhi Guan
2019-01-02 21:09:41 +08:00
parent 7f6165a3f9
commit 8f54e70ada
10 changed files with 5616 additions and 5420 deletions

View File

@@ -12,7 +12,7 @@ IF[{- !$disabled{apps} -}]
srp.c ts.c verify.c version.c x509.c rehash.c \
apps.c opt.c s_cb.c s_socket.c \
app_rand.c cpk.c otp.c fpe.c \
sm2.c sm2utl.c \
sm2.c sm2utl.c sdf.c skf.c \
sm9param.c gensm9.c sm9.c sm9utl.c \
{- $target{apps_aux_src} -}
INCLUDE[gmssl]=.. ../include

View File

@@ -67,7 +67,9 @@ extern int rsautl_main(int argc, char *argv[]);
extern int s_client_main(int argc, char *argv[]);
extern int s_server_main(int argc, char *argv[]);
extern int s_time_main(int argc, char *argv[]);
extern int sdf_main(int argc, char *argv[]);
extern int sess_id_main(int argc, char *argv[]);
extern int skf_main(int argc, char *argv[]);
extern int sm2_main(int argc, char *argv[]);
extern int sm2utl_main(int argc, char *argv[]);
extern int sm9_main(int argc, char *argv[]);
@@ -125,7 +127,9 @@ extern OPTIONS rsautl_options[];
extern OPTIONS s_client_options[];
extern OPTIONS s_server_options[];
extern OPTIONS s_time_options[];
extern OPTIONS sdf_options[];
extern OPTIONS sess_id_options[];
extern OPTIONS skf_options[];
extern OPTIONS sm2_options[];
extern OPTIONS sm2utl_options[];
extern OPTIONS sm9_options[];
@@ -226,8 +230,14 @@ static FUNCTION functions[] = {
#endif
#ifndef OPENSSL_NO_SOCK
{ FT_general, "s_time", s_time_main, s_time_options },
#endif
#ifndef OPENSSL_NO_SDF
{ FT_general, "sdf", sdf_main, sdf_options },
#endif
{ FT_general, "sess_id", sess_id_main, sess_id_options },
#ifndef OPENSSL_NO_SKF
{ FT_general, "skf", skf_main, skf_options },
#endif
#ifndef OPENSSL_NO_SM2
{ FT_general, "sm2", sm2_main, sm2_options },
#endif

View File

@@ -63,19 +63,149 @@ NON_EMPTY_TRANSLATION_UNIT
# include "apps.h"
typedef enum OPTION_choice {
OPT_ERR = -1,
OPT_EOF = 0,
OPT_HELP
OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
OPT_IN, OPT_OUT, OPT_INFORM, OPT_OUTFORM,
OPT_PASSIN, OPT_PASSOUT, OPT_TEXT, OPT_NOOUT
} OPTION_CHOICE;
OPTIONS sm9_options[] = {
{"help", OPT_HELP, '-', "Display this summary"},
{"in", OPT_IN, 's', "Input key"},
{"out", OPT_OUT, '>', "Output file"},
{"inform", OPT_INFORM, 'f', "Input format (DER or PEM)"},
{"outform", OPT_OUTFORM, 'F', "Output format (DER or PEM)"},
{"passin", OPT_PASSIN, 's', "Input file pass phrase source"},
{"passout", OPT_PASSOUT, 's', "Output file pass phrase source"},
{"text", OPT_TEXT, '-', "Output in plaintext as well"},
{"noout", OPT_NOOUT, '-', "Don't output the key"},
{NULL}
};
int sm9_main(int argc, char **argv)
{
printf("sm9 not implemented\n");
return 0;
int ret = 1;
SM9_KEY *key = NULL;
BIO *in = NULL, *out = NULL;
char *infile = NULL, *outfile = NULL, *prog;
char *passin = NULL, *passout = NULL;
char *passinarg = NULL, *passoutarg = NULL;
OPTION_CHOICE o;
int informat = FORMAT_PEM, outformat = FORMAT_PEM;
int text = 0, noout = 0;
prog = opt_init(argc, argv, sm9_options);
while ((o = opt_next()) != OPT_EOF) {
switch (o) {
case OPT_EOF:
case OPT_ERR:
opthelp:
BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
goto end;
case OPT_HELP:
opt_help(sm9_options);
ret = 0;
goto end;
case OPT_IN:
infile = opt_arg();
break;
case OPT_OUT:
outfile = opt_arg();
break;
case OPT_INFORM:
if (!opt_format(opt_arg(), OPT_FMT_ANY, &informat))
goto opthelp;
break;
case OPT_OUTFORM:
if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &outformat))
goto opthelp;
break;
case OPT_PASSIN:
passinarg = opt_arg();
break;
case OPT_PASSOUT:
passoutarg = opt_arg();
break;
case OPT_TEXT:
text = 1;
break;
case OPT_NOOUT:
noout = 1;
break;
}
}
argc = opt_num_rest();
if (argc != 0)
goto opthelp;
if (!app_passwd(passinarg, passoutarg, &passin, &passout)) {
BIO_printf(bio_err, "Error getting passwords\n");
goto end;
}
if (informat != FORMAT_ENGINE) {
if (!(in = bio_open_default(infile, 'r', informat)))
goto end;
}
BIO_printf(bio_err, "read SM9 key\n");
if (informat == FORMAT_ASN1) {
if (!(key = d2i_SM9PrivateKey_bio(in, NULL))) {
BIO_printf(bio_err, "unable to load Key\n");
ERR_print_errors(bio_err);
goto end;
}
} else if (informat == FORMAT_PEM) {
if (!(key = PEM_read_bio_SM9PrivateKey(in, NULL, NULL, passin))) {
BIO_printf(bio_err, "unable to load Key\n");
ERR_print_errors(bio_err);
goto end;
}
} else {
BIO_printf(bio_err, "supported key format\n");
goto end;
}
if (!(out = bio_open_owner(outfile, outformat, 1))) {
goto end;
}
if (!noout) {
BIO_printf(bio_err, "writing SM9 key\n");
if (outformat == FORMAT_ASN1) {
if (!i2d_SM9PrivateKey_bio(out, key)) {
BIO_printf(bio_err, "unable to write private key\n");
ERR_print_errors(bio_err);
goto end;
}
} else if (outformat == FORMAT_PEM) {
if (!PEM_write_bio_SM9PrivateKey(out, key, EVP_sms4_cbc(), NULL, 0, NULL, passout)) {
BIO_printf(bio_err, "unable to write private key\n");
ERR_print_errors(bio_err);
goto end;
}
} else {
BIO_printf(bio_err, "unsupported key format\n");
goto end;
}
}
/* sm9 api does not support public key print, do it with evp api */
if (text) {
if (!SM9_KEY_print(out, key, 0)) {
perror(outfile);
ERR_print_errors(bio_err);
goto end;
}
}
ret = 0;
end:
SM9_KEY_free(key);
BIO_free(in);
BIO_free_all(out);
OPENSSL_free(passin);
OPENSSL_free(passout);
return ret;
}
#endif