Add sm2 and otp command

try `gmssl sm2 -help` and `gmssl otp -help`
This commit is contained in:
Zhi Guan
2018-12-19 10:01:38 +08:00
parent 5bc22a1f3e
commit 74a729aa77
17 changed files with 376 additions and 5597 deletions

View File

@@ -11,7 +11,7 @@ IF[{- !$disabled{apps} -}]
s_client.c s_server.c s_time.c sess_id.c smime.c speed.c spkac.c \
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 sm9.c otp.c \
app_rand.c cpk.c sm2.c sm9.c otp.c \
{- $target{apps_aux_src} -}
INCLUDE[gmssl]=.. ../include
DEPEND[gmssl]=../libssl

View File

@@ -1,5 +1,5 @@
/* ====================================================================
* Copyright (c) 2014 - 2017 The GmSSL Project. All rights reserved.
* Copyright (c) 2014 - 2018 The GmSSL Project. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
@@ -46,32 +46,129 @@
* OF THE POSSIBILITY OF SUCH DAMAGE.
* ====================================================================
*/
/*
* gmssl otp -genkey -seed data
*/
#include "apps.h"
#include <openssl/opensslconf.h>
#ifdef OPENSSL_NO_OTP
NON_EMPTY_TRANSLATION_UNIT
#else
#include <ctype.h>
#include <stdio.h>
#include <string.h>
#include <openssl/bio.h>
#include <openssl/err.h>
#include <openssl/evp.h>
#include <openssl/otp.h>
# include <ctype.h>
# include <stdio.h>
# include <string.h>
# include <openssl/bio.h>
# include <openssl/err.h>
# include <openssl/evp.h>
# include <openssl/otp.h>
# include "apps.h"
typedef enum OPTION_choice {
OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
OPT_SETUP, OPT_GENKEY
OPT_IN, OPT_OUT, OPT_SETUP, OPT_GENKEY
} OPTION_CHOICE;
OPTIONS otp_options[] = {
{"help", OPT_HELP, '-', "Display this summary"},
{"in", OPT_IN, '<', "Input seed file - default stdin"},
{"out", OPT_OUT, '>', "Output seed file - default stdout"},
{"setup", OPT_SETUP, '-', "Generate seed"},
{"genkey", OPT_GENKEY, '-', "Generate one-time password"},
{NULL}
};
int otp_main(int argc, char **argv)
{
printf("otp not implemented\n");
return 0;
int ret = 1;
BIO *bio = NULL;
char *infile = NULL, *outfile = NULL, *prog;
OPTION_CHOICE o;
int setup = 0, genkey = 0;
unsigned char key[32];
prog = opt_init(argc, argv, otp_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(otp_options);
ret = 0;
goto end;
case OPT_IN:
infile = opt_arg();
break;
case OPT_OUT:
outfile = opt_arg();
break;
case OPT_SETUP:
setup = 1;
break;
case OPT_GENKEY:
genkey = 1;
break;
}
}
argc = opt_num_rest();
if (argc != 0)
goto opthelp;
if (!(setup ^ genkey))
goto opthelp;
if (setup) {
unsigned char key[32];
if (!RAND_bytes(key, sizeof(key))) {
ERR_print_errors_fp(stderr);
goto end;
}
bio = bio_open_default(outfile, 'w', FORMAT_BINARY);
if (!bio)
goto end;
if (BIO_write(bio, key, sizeof(key)) != sizeof(key)) {
goto end;
}
printf("generate OTP seed in '%s'\n", outfile);
} else if (genkey) {
OTP_PARAMS params;
unsigned char event[] = "gmssl otp event default";
unsigned int otp;
params.type = NID_sm3;
params.te = 1;
params.option = NULL;
params.option_size = 0;
params.otp_digits = 6;
bio = bio_open_default(infile, 'r', FORMAT_BINARY);
if (!bio)
goto end;
if (BIO_read(bio, key, sizeof(key)) != sizeof(key)) {
ERR_print_errors_fp(stderr);
goto end;
}
if (!OTP_generate(&params, event, sizeof(event), &otp, key, sizeof(key))) {
ERR_print_errors_fp(stderr);
goto end;
}
printf("%06u\n", otp);
ret = 0;
}
end:
BIO_free(bio);
OPENSSL_cleanse(key, sizeof(key));
return ret;
}
#endif

View File

@@ -66,6 +66,7 @@ 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 sess_id_main(int argc, char *argv[]);
extern int sm2_main(int argc, char *argv[]);
extern int sm9_main(int argc, char *argv[]);
extern int smime_main(int argc, char *argv[]);
extern int speed_main(int argc, char *argv[]);
@@ -118,6 +119,7 @@ extern OPTIONS s_client_options[];
extern OPTIONS s_server_options[];
extern OPTIONS s_time_options[];
extern OPTIONS sess_id_options[];
extern OPTIONS sm2_options[];
extern OPTIONS sm9_options[];
extern OPTIONS smime_options[];
extern OPTIONS speed_options[];
@@ -214,6 +216,9 @@ static FUNCTION functions[] = {
{ FT_general, "s_time", s_time_main, s_time_options },
#endif
{ FT_general, "sess_id", sess_id_main, sess_id_options },
#ifndef OPENSSL_NO_SM2
{ FT_general, "sm2", sm2_main, sm2_options },
#endif
#ifndef OPENSSL_NO_SM9
{ FT_general, "sm9", sm9_main, sm9_options },
#endif