mirror of
https://github.com/guanzhi/GmSSL.git
synced 2026-09-21 12:53:39 +08:00
@@ -1,41 +1,41 @@
|
||||
# 命令行工具
|
||||
|
||||
注意:
|
||||
|
||||
* 命令行工具接口在v3版本正式发布前还会有较大调整
|
||||
* SM2, SM3, SM4等算法的命令相对比较底层,是对C语言接口的简单封装,命令行的应用开发者需要组合使用这些指令
|
||||
|
||||
命令行工具:
|
||||
|
||||
* `sm3` 计算SM3杂凑值,支持带公钥和ID的Z值计算
|
||||
* `sm3hmac` 计算SM3-HMAC值
|
||||
* `sm2keygen` 生成SM2密钥对,以PKCS #8口令加密的PEM格式存储
|
||||
* `sm2sign`,`sm2verify` SM2签名和验证,生成DER二进制编码的SM2签名值
|
||||
* `sm2encrypt`,`sm2decrypt` SM2加解密,注意只支持较短的消息加密
|
||||
* `reqgen` 生成PKCS #10证书签名请求PEM文件
|
||||
* `reqparse` 解析打印REQ文件
|
||||
* `reqsign` CA用私钥对REQ文件签名,生成证书
|
||||
* `certgen`生成自签名证书
|
||||
* `certparse` 解析打印证书
|
||||
* `certverify` 验证证书链
|
||||
|
||||
TLS功能
|
||||
|
||||
* `tlcp_client`
|
||||
* `tlcp_server`
|
||||
* `tls12_client`
|
||||
* `tls12_server`
|
||||
* `tls13_client`
|
||||
* `tls13_server`
|
||||
|
||||
私钥总是默认以口令加密的方式存储
|
||||
SM3/HMAC-SM3 以二进制的格式输出
|
||||
签名和SM2Ciphertext以DER编码输出
|
||||
|
||||
|
||||
应该提供一个口令导出密钥的算法,由口令导出密钥
|
||||
|
||||
SM4加密需要外部提供key, iv
|
||||
HMAC-SM3可以用命令行的方式拼合
|
||||
因此没必要提供一个单独的SM4-CBC-HMAC-SM3
|
||||
|
||||
# 命令行工具
|
||||
|
||||
注意:
|
||||
|
||||
* 命令行工具接口在v3版本正式发布前还会有较大调整
|
||||
* SM2, SM3, SM4等算法的命令相对比较底层,是对C语言接口的简单封装,命令行的应用开发者需要组合使用这些指令
|
||||
|
||||
命令行工具:
|
||||
|
||||
* `sm3` 计算SM3杂凑值,支持带公钥和ID的Z值计算
|
||||
* `sm3hmac` 计算SM3-HMAC值
|
||||
* `sm2keygen` 生成SM2密钥对,以PKCS #8口令加密的PEM格式存储
|
||||
* `sm2sign`,`sm2verify` SM2签名和验证,生成DER二进制编码的SM2签名值
|
||||
* `sm2encrypt`,`sm2decrypt` SM2加解密,注意只支持较短的消息加密
|
||||
* `reqgen` 生成PKCS #10证书签名请求PEM文件
|
||||
* `reqparse` 解析打印REQ文件
|
||||
* `reqsign` CA用私钥对REQ文件签名,生成证书
|
||||
* `certgen`生成自签名证书
|
||||
* `certparse` 解析打印证书
|
||||
* `certverify` 验证证书链
|
||||
|
||||
TLS功能
|
||||
|
||||
* `tlcp_client`
|
||||
* `tlcp_server`
|
||||
* `tls12_client`
|
||||
* `tls12_server`
|
||||
* `tls13_client`
|
||||
* `tls13_server`
|
||||
|
||||
私钥总是默认以口令加密的方式存储
|
||||
SM3/HMAC-SM3 以二进制的格式输出
|
||||
签名和SM2Ciphertext以DER编码输出
|
||||
|
||||
|
||||
应该提供一个口令导出密钥的算法,由口令导出密钥
|
||||
|
||||
SM4加密需要外部提供key, iv
|
||||
HMAC-SM3可以用命令行的方式拼合
|
||||
因此没必要提供一个单独的SM4-CBC-HMAC-SM3
|
||||
|
||||
|
||||
405
tools/certgen.c
405
tools/certgen.c
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2014-2022 The GmSSL Project. All Rights Reserved.
|
||||
* Copyright 2022 The GmSSL Project. All Rights Reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the License); you may
|
||||
* not use this file except in compliance with the License.
|
||||
@@ -7,205 +7,204 @@
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*/
|
||||
|
||||
|
||||
|
||||
#include <stdio.h>
|
||||
#include <errno.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <gmssl/mem.h>
|
||||
#include <gmssl/rand.h>
|
||||
#include <gmssl/pkcs8.h>
|
||||
#include <gmssl/error.h>
|
||||
#include <gmssl/x509.h>
|
||||
#include <gmssl/x509_ext.h>
|
||||
|
||||
|
||||
static const char *options =
|
||||
"[-C str] [-ST str] [-L str] [-O str] [-OU str] -CN str -days num "
|
||||
"-key file [-pass pass] "
|
||||
"[-key_usage str]* [-out file]";
|
||||
|
||||
|
||||
static int ext_key_usage_set(int *usages, const char *usage_name)
|
||||
{
|
||||
int flag;
|
||||
if (x509_key_usage_from_name(&flag, usage_name) != 1) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
*usages |= flag;
|
||||
return 1;
|
||||
}
|
||||
|
||||
int certgen_main(int argc, char **argv)
|
||||
{
|
||||
int ret = 1;
|
||||
char *prog = argv[0];
|
||||
char *country = NULL;
|
||||
char *state = NULL;
|
||||
char *locality = NULL;
|
||||
char *org = NULL;
|
||||
char *org_unit = NULL;
|
||||
char *common_name = NULL;
|
||||
int days = 0;
|
||||
int key_usage = 0;
|
||||
char *keyfile = NULL;
|
||||
char *pass = NULL;
|
||||
char *outfile = NULL;
|
||||
|
||||
uint8_t serial[12];
|
||||
uint8_t name[256];
|
||||
size_t namelen;
|
||||
time_t not_before;
|
||||
time_t not_after;
|
||||
uint8_t uniq_id[32];
|
||||
uint8_t exts[512];
|
||||
size_t extslen = 0;
|
||||
FILE *keyfp = NULL;
|
||||
SM2_KEY sm2_key;
|
||||
uint8_t cert[1024];
|
||||
size_t certlen;
|
||||
FILE *outfp = stdout;
|
||||
|
||||
argc--;
|
||||
argv++;
|
||||
|
||||
if (argc < 1) {
|
||||
fprintf(stderr, "usage: %s %s\n", prog, options);
|
||||
return 1;
|
||||
}
|
||||
|
||||
while (argc > 0) {
|
||||
if (!strcmp(*argv, "-help")) {
|
||||
printf("usage: %s %s\n", prog, options);
|
||||
ret = 0;
|
||||
goto end;
|
||||
} else if (!strcmp(*argv, "-CN")) {
|
||||
if (--argc < 1) goto bad;
|
||||
common_name = *(++argv);
|
||||
} else if (!strcmp(*argv, "-O")) {
|
||||
if (--argc < 1) goto bad;
|
||||
org = *(++argv);
|
||||
} else if (!strcmp(*argv, "-OU")) {
|
||||
if (--argc < 1) goto bad;
|
||||
org_unit = *(++argv);
|
||||
} else if (!strcmp(*argv, "-C")) {
|
||||
if (--argc < 1) goto bad;
|
||||
country = *(++argv);
|
||||
} else if (!strcmp(*argv, "-ST")) {
|
||||
if (--argc < 1) goto bad;
|
||||
state = *(++argv);
|
||||
} else if (!strcmp(*argv, "-L")) {
|
||||
if (--argc < 1) goto bad;
|
||||
locality = *(++argv);
|
||||
} else if (!strcmp(*argv, "-days")) {
|
||||
if (--argc < 1) goto bad;
|
||||
days = atoi(*(++argv));
|
||||
if (days <= 0) {
|
||||
fprintf(stderr, "%s: invalid '-days' value\n", prog);
|
||||
goto end;
|
||||
}
|
||||
} else if (!strcmp(*argv, "-key_usage")) {
|
||||
char *usage;
|
||||
if (--argc < 1) goto bad;
|
||||
usage = *(++argv);
|
||||
if (ext_key_usage_set(&key_usage, usage) != 1) {
|
||||
fprintf(stderr, "%s: invalid -key_usage value '%s'\n", prog, usage);
|
||||
goto end;
|
||||
}
|
||||
} else if (!strcmp(*argv, "-key")) {
|
||||
if (--argc < 1) goto bad;
|
||||
keyfile = *(++argv);
|
||||
if (!(keyfp = fopen(keyfile, "r"))) {
|
||||
fprintf(stderr, "%s: open '%s' failure : %s\n", prog, keyfile, strerror(errno));
|
||||
goto end;
|
||||
}
|
||||
} else if (!strcmp(*argv, "-pass")) {
|
||||
if (--argc < 1) goto bad;
|
||||
pass = *(++argv);
|
||||
} else if (!strcmp(*argv, "-out")) {
|
||||
if (--argc < 1) goto bad;
|
||||
outfile = *(++argv);
|
||||
if (!(outfp = fopen(outfile, "w"))) {
|
||||
fprintf(stderr, "%s: open '%s' failure : %s\n", prog, outfile, strerror(errno));
|
||||
goto end;
|
||||
}
|
||||
} else {
|
||||
fprintf(stderr, "%s: illegal option '%s'\n", prog, *argv);
|
||||
goto end;
|
||||
bad:
|
||||
fprintf(stderr, "%s: '%s' option value missing\n", prog, *argv);
|
||||
goto end;
|
||||
}
|
||||
|
||||
argc--;
|
||||
argv++;
|
||||
}
|
||||
|
||||
if (!common_name) {
|
||||
fprintf(stderr, "%s: '-CN' option required\n", prog);
|
||||
goto end;
|
||||
}
|
||||
if (!days) {
|
||||
fprintf(stderr, "%s: '-days' option required\n", prog);
|
||||
goto end;
|
||||
}
|
||||
if (!keyfile) {
|
||||
fprintf(stderr, "%s: '-key' option required\n", prog);
|
||||
goto end;
|
||||
}
|
||||
if (!pass) {
|
||||
fprintf(stderr, "%s: '-pass' option required\n", prog);
|
||||
goto end;
|
||||
}
|
||||
if (!key_usage) {
|
||||
fprintf(stderr, "%s: '-key_usage' option required\n", prog);
|
||||
goto end;
|
||||
}
|
||||
|
||||
if (sm2_private_key_info_decrypt_from_pem(&sm2_key, pass, keyfp) != 1) {
|
||||
fprintf(stderr, "%s: load private key failed\n", prog);
|
||||
goto end;
|
||||
}
|
||||
|
||||
if (x509_exts_add_key_usage(exts, &extslen, sizeof(exts), 1, key_usage) != 1
|
||||
|| x509_exts_add_basic_constraints(exts, &extslen, sizeof(exts), 1, 1, -1) != 1
|
||||
|| x509_exts_add_default_authority_key_identifier(exts, &extslen, sizeof(exts), &sm2_key) != 1) {
|
||||
fprintf(stderr, "%s: inner error\n", prog);
|
||||
goto end;
|
||||
}
|
||||
|
||||
time(¬_before);
|
||||
if (rand_bytes(serial, sizeof(serial)) != 1
|
||||
|| x509_name_set(name, &namelen, sizeof(name),
|
||||
country, state, locality, org, org_unit, common_name) != 1
|
||||
|| x509_validity_add_days(¬_after, not_before, days) != 1
|
||||
|| x509_cert_sign(
|
||||
cert, &certlen, sizeof(cert),
|
||||
X509_version_v3,
|
||||
serial, sizeof(serial),
|
||||
OID_sm2sign_with_sm3,
|
||||
name, namelen,
|
||||
not_before, not_after,
|
||||
name, namelen,
|
||||
&sm2_key,
|
||||
NULL, 0,
|
||||
NULL, 0,
|
||||
exts, extslen,
|
||||
&sm2_key, SM2_DEFAULT_ID, strlen(SM2_DEFAULT_ID)) != 1) {
|
||||
fprintf(stderr, "%s: inner error\n", prog);
|
||||
goto end;
|
||||
}
|
||||
if (x509_cert_to_pem(cert, certlen, outfp) != 1) {
|
||||
fprintf(stderr, "%s: output certificate failed\n", prog);
|
||||
goto end;
|
||||
}
|
||||
ret = 0;
|
||||
|
||||
end:
|
||||
gmssl_secure_clear(&sm2_key, sizeof(SM2_KEY));
|
||||
if (keyfp) fclose(keyfp);
|
||||
if (outfile && outfp) fclose(outfp);
|
||||
return ret;
|
||||
}
|
||||
|
||||
#include <stdio.h>
|
||||
#include <errno.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <gmssl/mem.h>
|
||||
#include <gmssl/rand.h>
|
||||
#include <gmssl/pkcs8.h>
|
||||
#include <gmssl/error.h>
|
||||
#include <gmssl/x509.h>
|
||||
#include <gmssl/x509_ext.h>
|
||||
|
||||
|
||||
static const char *options =
|
||||
"[-C str] [-ST str] [-L str] [-O str] [-OU str] -CN str -days num "
|
||||
"-key file [-pass pass] "
|
||||
"[-key_usage str]* [-out file]";
|
||||
|
||||
|
||||
static int ext_key_usage_set(int *usages, const char *usage_name)
|
||||
{
|
||||
int flag;
|
||||
if (x509_key_usage_from_name(&flag, usage_name) != 1) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
*usages |= flag;
|
||||
return 1;
|
||||
}
|
||||
|
||||
int certgen_main(int argc, char **argv)
|
||||
{
|
||||
int ret = 1;
|
||||
char *prog = argv[0];
|
||||
char *country = NULL;
|
||||
char *state = NULL;
|
||||
char *locality = NULL;
|
||||
char *org = NULL;
|
||||
char *org_unit = NULL;
|
||||
char *common_name = NULL;
|
||||
int days = 0;
|
||||
int key_usage = 0;
|
||||
char *keyfile = NULL;
|
||||
char *pass = NULL;
|
||||
char *outfile = NULL;
|
||||
|
||||
uint8_t serial[12];
|
||||
uint8_t name[256];
|
||||
size_t namelen;
|
||||
time_t not_before;
|
||||
time_t not_after;
|
||||
uint8_t uniq_id[32];
|
||||
uint8_t exts[512];
|
||||
size_t extslen = 0;
|
||||
FILE *keyfp = NULL;
|
||||
SM2_KEY sm2_key;
|
||||
uint8_t cert[1024];
|
||||
size_t certlen;
|
||||
FILE *outfp = stdout;
|
||||
|
||||
argc--;
|
||||
argv++;
|
||||
|
||||
if (argc < 1) {
|
||||
fprintf(stderr, "usage: %s %s\n", prog, options);
|
||||
return 1;
|
||||
}
|
||||
|
||||
while (argc > 0) {
|
||||
if (!strcmp(*argv, "-help")) {
|
||||
printf("usage: %s %s\n", prog, options);
|
||||
ret = 0;
|
||||
goto end;
|
||||
} else if (!strcmp(*argv, "-CN")) {
|
||||
if (--argc < 1) goto bad;
|
||||
common_name = *(++argv);
|
||||
} else if (!strcmp(*argv, "-O")) {
|
||||
if (--argc < 1) goto bad;
|
||||
org = *(++argv);
|
||||
} else if (!strcmp(*argv, "-OU")) {
|
||||
if (--argc < 1) goto bad;
|
||||
org_unit = *(++argv);
|
||||
} else if (!strcmp(*argv, "-C")) {
|
||||
if (--argc < 1) goto bad;
|
||||
country = *(++argv);
|
||||
} else if (!strcmp(*argv, "-ST")) {
|
||||
if (--argc < 1) goto bad;
|
||||
state = *(++argv);
|
||||
} else if (!strcmp(*argv, "-L")) {
|
||||
if (--argc < 1) goto bad;
|
||||
locality = *(++argv);
|
||||
} else if (!strcmp(*argv, "-days")) {
|
||||
if (--argc < 1) goto bad;
|
||||
days = atoi(*(++argv));
|
||||
if (days <= 0) {
|
||||
fprintf(stderr, "%s: invalid '-days' value\n", prog);
|
||||
goto end;
|
||||
}
|
||||
} else if (!strcmp(*argv, "-key_usage")) {
|
||||
char *usage;
|
||||
if (--argc < 1) goto bad;
|
||||
usage = *(++argv);
|
||||
if (ext_key_usage_set(&key_usage, usage) != 1) {
|
||||
fprintf(stderr, "%s: invalid -key_usage value '%s'\n", prog, usage);
|
||||
goto end;
|
||||
}
|
||||
} else if (!strcmp(*argv, "-key")) {
|
||||
if (--argc < 1) goto bad;
|
||||
keyfile = *(++argv);
|
||||
if (!(keyfp = fopen(keyfile, "r"))) {
|
||||
fprintf(stderr, "%s: open '%s' failure : %s\n", prog, keyfile, strerror(errno));
|
||||
goto end;
|
||||
}
|
||||
} else if (!strcmp(*argv, "-pass")) {
|
||||
if (--argc < 1) goto bad;
|
||||
pass = *(++argv);
|
||||
} else if (!strcmp(*argv, "-out")) {
|
||||
if (--argc < 1) goto bad;
|
||||
outfile = *(++argv);
|
||||
if (!(outfp = fopen(outfile, "w"))) {
|
||||
fprintf(stderr, "%s: open '%s' failure : %s\n", prog, outfile, strerror(errno));
|
||||
goto end;
|
||||
}
|
||||
} else {
|
||||
fprintf(stderr, "%s: illegal option '%s'\n", prog, *argv);
|
||||
goto end;
|
||||
bad:
|
||||
fprintf(stderr, "%s: '%s' option value missing\n", prog, *argv);
|
||||
goto end;
|
||||
}
|
||||
|
||||
argc--;
|
||||
argv++;
|
||||
}
|
||||
|
||||
if (!common_name) {
|
||||
fprintf(stderr, "%s: '-CN' option required\n", prog);
|
||||
goto end;
|
||||
}
|
||||
if (!days) {
|
||||
fprintf(stderr, "%s: '-days' option required\n", prog);
|
||||
goto end;
|
||||
}
|
||||
if (!keyfile) {
|
||||
fprintf(stderr, "%s: '-key' option required\n", prog);
|
||||
goto end;
|
||||
}
|
||||
if (!pass) {
|
||||
fprintf(stderr, "%s: '-pass' option required\n", prog);
|
||||
goto end;
|
||||
}
|
||||
if (!key_usage) {
|
||||
fprintf(stderr, "%s: '-key_usage' option required\n", prog);
|
||||
goto end;
|
||||
}
|
||||
|
||||
if (sm2_private_key_info_decrypt_from_pem(&sm2_key, pass, keyfp) != 1) {
|
||||
fprintf(stderr, "%s: load private key failed\n", prog);
|
||||
goto end;
|
||||
}
|
||||
|
||||
if (x509_exts_add_key_usage(exts, &extslen, sizeof(exts), 1, key_usage) != 1
|
||||
|| x509_exts_add_basic_constraints(exts, &extslen, sizeof(exts), 1, 1, -1) != 1
|
||||
|| x509_exts_add_default_authority_key_identifier(exts, &extslen, sizeof(exts), &sm2_key) != 1) {
|
||||
fprintf(stderr, "%s: inner error\n", prog);
|
||||
goto end;
|
||||
}
|
||||
|
||||
time(¬_before);
|
||||
if (rand_bytes(serial, sizeof(serial)) != 1
|
||||
|| x509_name_set(name, &namelen, sizeof(name),
|
||||
country, state, locality, org, org_unit, common_name) != 1
|
||||
|| x509_validity_add_days(¬_after, not_before, days) != 1
|
||||
|| x509_cert_sign(
|
||||
cert, &certlen, sizeof(cert),
|
||||
X509_version_v3,
|
||||
serial, sizeof(serial),
|
||||
OID_sm2sign_with_sm3,
|
||||
name, namelen,
|
||||
not_before, not_after,
|
||||
name, namelen,
|
||||
&sm2_key,
|
||||
NULL, 0,
|
||||
NULL, 0,
|
||||
exts, extslen,
|
||||
&sm2_key, SM2_DEFAULT_ID, strlen(SM2_DEFAULT_ID)) != 1) {
|
||||
fprintf(stderr, "%s: inner error\n", prog);
|
||||
goto end;
|
||||
}
|
||||
if (x509_cert_to_pem(cert, certlen, outfp) != 1) {
|
||||
fprintf(stderr, "%s: output certificate failed\n", prog);
|
||||
goto end;
|
||||
}
|
||||
ret = 0;
|
||||
|
||||
end:
|
||||
gmssl_secure_clear(&sm2_key, sizeof(SM2_KEY));
|
||||
if (keyfp) fclose(keyfp);
|
||||
if (outfile && outfp) fclose(outfp);
|
||||
return ret;
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2014-2022 The GmSSL Project. All Rights Reserved.
|
||||
* Copyright 2022 The GmSSL Project. All Rights Reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the License); you may
|
||||
* not use this file except in compliance with the License.
|
||||
@@ -7,78 +7,77 @@
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*/
|
||||
|
||||
|
||||
|
||||
#include <stdio.h>
|
||||
#include <errno.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <gmssl/pem.h>
|
||||
#include <gmssl/x509.h>
|
||||
|
||||
|
||||
static const char *options = "[-in file] [-out file]";
|
||||
|
||||
int certparse_main(int argc, char **argv)
|
||||
{
|
||||
int ret = 1;
|
||||
char *prog = argv[0];
|
||||
char *infile = NULL;
|
||||
char *outfile = NULL;
|
||||
FILE *infp = stdin;
|
||||
FILE *outfp = stdout;
|
||||
uint8_t cert[18192];
|
||||
size_t certlen;
|
||||
|
||||
argc--;
|
||||
argv++;
|
||||
|
||||
while (argc > 0) {
|
||||
if (!strcmp(*argv, "-help")) {
|
||||
printf("usage: %s %s\n", prog, options);
|
||||
goto end;
|
||||
} else if (!strcmp(*argv, "-in")) {
|
||||
if (--argc < 1) goto bad;
|
||||
infile = *(++argv);
|
||||
if (!(infp = fopen(infile, "r"))) {
|
||||
fprintf(stderr, "%s: open '%s' failure : %s\n", prog, infile, strerror(errno));
|
||||
goto end;
|
||||
}
|
||||
} else if (!strcmp(*argv, "-out")) {
|
||||
if (--argc < 1) goto bad;
|
||||
outfile = *(++argv);
|
||||
if (!(outfp = fopen(outfile, "w"))) {
|
||||
fprintf(stderr, "%s: open '%s' failure : %s\n", prog, outfile, strerror(errno));
|
||||
goto end;
|
||||
}
|
||||
} else {
|
||||
fprintf(stderr, "%s: illegal option '%s'\n", prog, *argv);
|
||||
goto end;
|
||||
bad:
|
||||
fprintf(stderr, "%s: '%s' option value missing\n", prog, *argv);
|
||||
goto end;
|
||||
}
|
||||
|
||||
argc--;
|
||||
argv++;
|
||||
}
|
||||
|
||||
for (;;) {
|
||||
int rv;
|
||||
if ((rv = x509_cert_from_pem(cert, &certlen, sizeof(cert), infp)) != 1) {
|
||||
if (rv < 0) fprintf(stderr, "%s: read certificate failure\n", prog);
|
||||
else ret = 0;
|
||||
goto end;
|
||||
}
|
||||
x509_cert_print(outfp, 0, 0, "Certificate", cert, certlen);
|
||||
if (x509_cert_to_pem(cert, certlen, outfp) != 1) {
|
||||
fprintf(stderr, "%s: output certficate failure\n", prog);
|
||||
goto end;
|
||||
}
|
||||
}
|
||||
|
||||
end:
|
||||
if (infile && infp) fclose(infp);
|
||||
if (outfile && outfp) fclose(outfp);
|
||||
return ret;
|
||||
}
|
||||
|
||||
#include <stdio.h>
|
||||
#include <errno.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <gmssl/pem.h>
|
||||
#include <gmssl/x509.h>
|
||||
|
||||
|
||||
static const char *options = "[-in file] [-out file]";
|
||||
|
||||
int certparse_main(int argc, char **argv)
|
||||
{
|
||||
int ret = 1;
|
||||
char *prog = argv[0];
|
||||
char *infile = NULL;
|
||||
char *outfile = NULL;
|
||||
FILE *infp = stdin;
|
||||
FILE *outfp = stdout;
|
||||
uint8_t cert[18192];
|
||||
size_t certlen;
|
||||
|
||||
argc--;
|
||||
argv++;
|
||||
|
||||
while (argc > 0) {
|
||||
if (!strcmp(*argv, "-help")) {
|
||||
printf("usage: %s %s\n", prog, options);
|
||||
goto end;
|
||||
} else if (!strcmp(*argv, "-in")) {
|
||||
if (--argc < 1) goto bad;
|
||||
infile = *(++argv);
|
||||
if (!(infp = fopen(infile, "r"))) {
|
||||
fprintf(stderr, "%s: open '%s' failure : %s\n", prog, infile, strerror(errno));
|
||||
goto end;
|
||||
}
|
||||
} else if (!strcmp(*argv, "-out")) {
|
||||
if (--argc < 1) goto bad;
|
||||
outfile = *(++argv);
|
||||
if (!(outfp = fopen(outfile, "w"))) {
|
||||
fprintf(stderr, "%s: open '%s' failure : %s\n", prog, outfile, strerror(errno));
|
||||
goto end;
|
||||
}
|
||||
} else {
|
||||
fprintf(stderr, "%s: illegal option '%s'\n", prog, *argv);
|
||||
goto end;
|
||||
bad:
|
||||
fprintf(stderr, "%s: '%s' option value missing\n", prog, *argv);
|
||||
goto end;
|
||||
}
|
||||
|
||||
argc--;
|
||||
argv++;
|
||||
}
|
||||
|
||||
for (;;) {
|
||||
int rv;
|
||||
if ((rv = x509_cert_from_pem(cert, &certlen, sizeof(cert), infp)) != 1) {
|
||||
if (rv < 0) fprintf(stderr, "%s: read certificate failure\n", prog);
|
||||
else ret = 0;
|
||||
goto end;
|
||||
}
|
||||
x509_cert_print(outfp, 0, 0, "Certificate", cert, certlen);
|
||||
if (x509_cert_to_pem(cert, certlen, outfp) != 1) {
|
||||
fprintf(stderr, "%s: output certficate failure\n", prog);
|
||||
goto end;
|
||||
}
|
||||
}
|
||||
|
||||
end:
|
||||
if (infile && infp) fclose(infp);
|
||||
if (outfile && outfp) fclose(outfp);
|
||||
return ret;
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2014-2022 The GmSSL Project. All Rights Reserved.
|
||||
* Copyright 2022 The GmSSL Project. All Rights Reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the License); you may
|
||||
* not use this file except in compliance with the License.
|
||||
@@ -7,170 +7,169 @@
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*/
|
||||
|
||||
|
||||
|
||||
#include <stdio.h>
|
||||
#include <errno.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <gmssl/x509.h>
|
||||
|
||||
|
||||
static const char *options = "-in pem [-double_certs] -cacert pem\n";
|
||||
|
||||
int certverify_main(int argc, char **argv)
|
||||
{
|
||||
int ret = 1;
|
||||
char *prog = argv[0];
|
||||
char *infile = NULL;
|
||||
char *cacertfile = NULL;
|
||||
FILE *infp = stdin;
|
||||
FILE *cacertfp = NULL;
|
||||
uint8_t cert[1024];
|
||||
size_t certlen;
|
||||
uint8_t cacert[1024];
|
||||
size_t cacertlen;
|
||||
const uint8_t *subject;
|
||||
size_t subject_len;
|
||||
const uint8_t *subj;
|
||||
size_t subj_len;
|
||||
|
||||
int double_certs = 0;
|
||||
uint8_t enc_cert[1024];
|
||||
size_t enc_cert_len;
|
||||
int rv;
|
||||
|
||||
argc--;
|
||||
argv++;
|
||||
|
||||
if (argc < 1) {
|
||||
fprintf(stderr, "usage: %s %s\n", prog, options);
|
||||
return 1;
|
||||
}
|
||||
|
||||
while (argc > 0) {
|
||||
if (!strcmp(*argv, "-help")) {
|
||||
printf("usage: %s %s\n", prog, options);
|
||||
ret = 0;
|
||||
goto end;
|
||||
} else if (!strcmp(*argv, "-in")) {
|
||||
if (--argc < 1) goto bad;
|
||||
infile = *(++argv);
|
||||
if (!(infp = fopen(infile, "r"))) {
|
||||
fprintf(stderr, "%s: open '%s' failure : %s\n", prog, infile, strerror(errno));
|
||||
goto end;
|
||||
}
|
||||
} else if (!strcmp(*argv, "-double_certs")) {
|
||||
double_certs = 1;
|
||||
} else if (!strcmp(*argv, "-cacert")) {
|
||||
if (--argc < 1) goto bad;
|
||||
cacertfile = *(++argv);
|
||||
if (!(cacertfp = fopen(cacertfile, "r"))) {
|
||||
fprintf(stderr, "%s: open '%s' failure : %s\n", prog, cacertfile, strerror(errno));
|
||||
goto end;
|
||||
}
|
||||
} else {
|
||||
fprintf(stderr, "%s: illegal option '%s'\n", prog, *argv);
|
||||
goto end;
|
||||
bad:
|
||||
fprintf(stderr, "%s: '%s' option value missing\n", prog, *argv);
|
||||
goto end;
|
||||
}
|
||||
|
||||
argc--;
|
||||
argv++;
|
||||
}
|
||||
|
||||
|
||||
if (!infile) {
|
||||
fprintf(stderr, "%s: '-in' option required\n", prog);
|
||||
goto end;
|
||||
}
|
||||
if (!cacertfile) {
|
||||
fprintf(stderr, "%s: '-cacert' option required\n", prog);
|
||||
goto end;
|
||||
}
|
||||
|
||||
if (x509_cert_from_pem(cert, &certlen, sizeof(cert), infp) != 1
|
||||
|| x509_cert_get_subject(cert, certlen, &subject, &subject_len) != 1) {
|
||||
fprintf(stderr, "%s: read certificate failure\n", prog);
|
||||
goto end;
|
||||
}
|
||||
x509_name_print(stdout, 0, 0, "Certificate", subject, subject_len);
|
||||
|
||||
if (double_certs) {
|
||||
|
||||
if (x509_cert_from_pem(enc_cert, &enc_cert_len, sizeof(enc_cert), infp) != 1
|
||||
|| x509_cert_get_subject(enc_cert, enc_cert_len, &subj, &subj_len) != 1) {
|
||||
fprintf(stderr, "%s: read encryption certficate failure\n", prog);
|
||||
goto end;
|
||||
}
|
||||
|
||||
if (subj_len != subject_len
|
||||
|| memcmp(subject, subj, subj_len) != 0) {
|
||||
fprintf(stderr, "%s: double certificates not compatible\n", prog);
|
||||
goto end;
|
||||
}
|
||||
}
|
||||
|
||||
for (;;) {
|
||||
if ((rv = x509_cert_from_pem(cacert, &cacertlen, sizeof(cacert), infp)) != 1) {
|
||||
if (rv < 0) goto end;
|
||||
goto final;
|
||||
}
|
||||
if (x509_cert_get_subject(cacert, cacertlen, &subject, &subject_len) != 1) {
|
||||
goto end;
|
||||
}
|
||||
|
||||
if ((rv = x509_cert_verify_by_ca_cert(cert, certlen, cacert, cacertlen, SM2_DEFAULT_ID, strlen(SM2_DEFAULT_ID))) < 0) {
|
||||
fprintf(stderr, "%s: inner error\n", prog);
|
||||
goto end;
|
||||
}
|
||||
printf("Verification %s\n", rv ? "success" : "failure");
|
||||
|
||||
if (double_certs) {
|
||||
x509_name_print(stdout, 0, 0, "Certificate", subj, subj_len);
|
||||
|
||||
if ((rv = x509_cert_verify_by_ca_cert(enc_cert, enc_cert_len, cacert, cacertlen, SM2_DEFAULT_ID, strlen(SM2_DEFAULT_ID))) < 0) {
|
||||
fprintf(stderr, "%s: inner error\n", prog);
|
||||
goto end;
|
||||
}
|
||||
printf("Verification %s\n", rv ? "success" : "failure");
|
||||
double_certs = 0;
|
||||
}
|
||||
x509_name_print(stdout, 0, 0, "Signed by", subject, subject_len);
|
||||
|
||||
memcpy(cert, cacert, cacertlen);
|
||||
certlen = cacertlen;
|
||||
}
|
||||
|
||||
final:
|
||||
if (x509_cert_get_issuer(cert, certlen, &subject, &subject_len) != 1) {
|
||||
fprintf(stderr, "%s: parse certificate error\n", prog);
|
||||
goto end;
|
||||
}
|
||||
if (x509_cert_from_pem_by_subject(cacert, &cacertlen, sizeof(cacert), subject, subject_len, cacertfp) != 1) {
|
||||
fprintf(stderr, "%s: load CA certificate failure\n", prog);
|
||||
goto end;
|
||||
}
|
||||
if ((rv = x509_cert_verify_by_ca_cert(cert, certlen, cacert, cacertlen, SM2_DEFAULT_ID, strlen(SM2_DEFAULT_ID))) < 0) {
|
||||
fprintf(stderr, "%s: inner error\n", prog);
|
||||
goto end;
|
||||
}
|
||||
printf("Verification %s\n", rv ? "success" : "failure");
|
||||
x509_name_print(stdout, 0, 0, "Signed by", subject, subject_len);
|
||||
|
||||
if (double_certs) {
|
||||
if ((rv = x509_cert_verify_by_ca_cert(enc_cert, enc_cert_len, cacert, cacertlen, SM2_DEFAULT_ID, strlen(SM2_DEFAULT_ID))) < 0) {
|
||||
fprintf(stderr, "%s: inner error\n", prog);
|
||||
goto end;
|
||||
}
|
||||
printf("Verification %s\n", rv ? "success" : "failure");
|
||||
}
|
||||
|
||||
ret = 0;
|
||||
end:
|
||||
if (infile && infp) fclose(infp);
|
||||
if (cacertfp) fclose(cacertfp);
|
||||
return ret;
|
||||
}
|
||||
|
||||
#include <stdio.h>
|
||||
#include <errno.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <gmssl/x509.h>
|
||||
|
||||
|
||||
static const char *options = "-in pem [-double_certs] -cacert pem\n";
|
||||
|
||||
int certverify_main(int argc, char **argv)
|
||||
{
|
||||
int ret = 1;
|
||||
char *prog = argv[0];
|
||||
char *infile = NULL;
|
||||
char *cacertfile = NULL;
|
||||
FILE *infp = stdin;
|
||||
FILE *cacertfp = NULL;
|
||||
uint8_t cert[1024];
|
||||
size_t certlen;
|
||||
uint8_t cacert[1024];
|
||||
size_t cacertlen;
|
||||
const uint8_t *subject;
|
||||
size_t subject_len;
|
||||
const uint8_t *subj;
|
||||
size_t subj_len;
|
||||
|
||||
int double_certs = 0;
|
||||
uint8_t enc_cert[1024];
|
||||
size_t enc_cert_len;
|
||||
int rv;
|
||||
|
||||
argc--;
|
||||
argv++;
|
||||
|
||||
if (argc < 1) {
|
||||
fprintf(stderr, "usage: %s %s\n", prog, options);
|
||||
return 1;
|
||||
}
|
||||
|
||||
while (argc > 0) {
|
||||
if (!strcmp(*argv, "-help")) {
|
||||
printf("usage: %s %s\n", prog, options);
|
||||
ret = 0;
|
||||
goto end;
|
||||
} else if (!strcmp(*argv, "-in")) {
|
||||
if (--argc < 1) goto bad;
|
||||
infile = *(++argv);
|
||||
if (!(infp = fopen(infile, "r"))) {
|
||||
fprintf(stderr, "%s: open '%s' failure : %s\n", prog, infile, strerror(errno));
|
||||
goto end;
|
||||
}
|
||||
} else if (!strcmp(*argv, "-double_certs")) {
|
||||
double_certs = 1;
|
||||
} else if (!strcmp(*argv, "-cacert")) {
|
||||
if (--argc < 1) goto bad;
|
||||
cacertfile = *(++argv);
|
||||
if (!(cacertfp = fopen(cacertfile, "r"))) {
|
||||
fprintf(stderr, "%s: open '%s' failure : %s\n", prog, cacertfile, strerror(errno));
|
||||
goto end;
|
||||
}
|
||||
} else {
|
||||
fprintf(stderr, "%s: illegal option '%s'\n", prog, *argv);
|
||||
goto end;
|
||||
bad:
|
||||
fprintf(stderr, "%s: '%s' option value missing\n", prog, *argv);
|
||||
goto end;
|
||||
}
|
||||
|
||||
argc--;
|
||||
argv++;
|
||||
}
|
||||
|
||||
|
||||
if (!infile) {
|
||||
fprintf(stderr, "%s: '-in' option required\n", prog);
|
||||
goto end;
|
||||
}
|
||||
if (!cacertfile) {
|
||||
fprintf(stderr, "%s: '-cacert' option required\n", prog);
|
||||
goto end;
|
||||
}
|
||||
|
||||
if (x509_cert_from_pem(cert, &certlen, sizeof(cert), infp) != 1
|
||||
|| x509_cert_get_subject(cert, certlen, &subject, &subject_len) != 1) {
|
||||
fprintf(stderr, "%s: read certificate failure\n", prog);
|
||||
goto end;
|
||||
}
|
||||
x509_name_print(stdout, 0, 0, "Certificate", subject, subject_len);
|
||||
|
||||
if (double_certs) {
|
||||
|
||||
if (x509_cert_from_pem(enc_cert, &enc_cert_len, sizeof(enc_cert), infp) != 1
|
||||
|| x509_cert_get_subject(enc_cert, enc_cert_len, &subj, &subj_len) != 1) {
|
||||
fprintf(stderr, "%s: read encryption certficate failure\n", prog);
|
||||
goto end;
|
||||
}
|
||||
|
||||
if (subj_len != subject_len
|
||||
|| memcmp(subject, subj, subj_len) != 0) {
|
||||
fprintf(stderr, "%s: double certificates not compatible\n", prog);
|
||||
goto end;
|
||||
}
|
||||
}
|
||||
|
||||
for (;;) {
|
||||
if ((rv = x509_cert_from_pem(cacert, &cacertlen, sizeof(cacert), infp)) != 1) {
|
||||
if (rv < 0) goto end;
|
||||
goto final;
|
||||
}
|
||||
if (x509_cert_get_subject(cacert, cacertlen, &subject, &subject_len) != 1) {
|
||||
goto end;
|
||||
}
|
||||
|
||||
if ((rv = x509_cert_verify_by_ca_cert(cert, certlen, cacert, cacertlen, SM2_DEFAULT_ID, strlen(SM2_DEFAULT_ID))) < 0) {
|
||||
fprintf(stderr, "%s: inner error\n", prog);
|
||||
goto end;
|
||||
}
|
||||
printf("Verification %s\n", rv ? "success" : "failure");
|
||||
|
||||
if (double_certs) {
|
||||
x509_name_print(stdout, 0, 0, "Certificate", subj, subj_len);
|
||||
|
||||
if ((rv = x509_cert_verify_by_ca_cert(enc_cert, enc_cert_len, cacert, cacertlen, SM2_DEFAULT_ID, strlen(SM2_DEFAULT_ID))) < 0) {
|
||||
fprintf(stderr, "%s: inner error\n", prog);
|
||||
goto end;
|
||||
}
|
||||
printf("Verification %s\n", rv ? "success" : "failure");
|
||||
double_certs = 0;
|
||||
}
|
||||
x509_name_print(stdout, 0, 0, "Signed by", subject, subject_len);
|
||||
|
||||
memcpy(cert, cacert, cacertlen);
|
||||
certlen = cacertlen;
|
||||
}
|
||||
|
||||
final:
|
||||
if (x509_cert_get_issuer(cert, certlen, &subject, &subject_len) != 1) {
|
||||
fprintf(stderr, "%s: parse certificate error\n", prog);
|
||||
goto end;
|
||||
}
|
||||
if (x509_cert_from_pem_by_subject(cacert, &cacertlen, sizeof(cacert), subject, subject_len, cacertfp) != 1) {
|
||||
fprintf(stderr, "%s: load CA certificate failure\n", prog);
|
||||
goto end;
|
||||
}
|
||||
if ((rv = x509_cert_verify_by_ca_cert(cert, certlen, cacert, cacertlen, SM2_DEFAULT_ID, strlen(SM2_DEFAULT_ID))) < 0) {
|
||||
fprintf(stderr, "%s: inner error\n", prog);
|
||||
goto end;
|
||||
}
|
||||
printf("Verification %s\n", rv ? "success" : "failure");
|
||||
x509_name_print(stdout, 0, 0, "Signed by", subject, subject_len);
|
||||
|
||||
if (double_certs) {
|
||||
if ((rv = x509_cert_verify_by_ca_cert(enc_cert, enc_cert_len, cacert, cacertlen, SM2_DEFAULT_ID, strlen(SM2_DEFAULT_ID))) < 0) {
|
||||
fprintf(stderr, "%s: inner error\n", prog);
|
||||
goto end;
|
||||
}
|
||||
printf("Verification %s\n", rv ? "success" : "failure");
|
||||
}
|
||||
|
||||
ret = 0;
|
||||
end:
|
||||
if (infile && infp) fclose(infp);
|
||||
if (cacertfp) fclose(cacertfp);
|
||||
return ret;
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2014-2022 The GmSSL Project. All Rights Reserved.
|
||||
* Copyright 2022 The GmSSL Project. All Rights Reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the License); you may
|
||||
* not use this file except in compliance with the License.
|
||||
@@ -7,172 +7,171 @@
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*/
|
||||
|
||||
|
||||
|
||||
#include <stdio.h>
|
||||
#include <errno.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <sys/stat.h>
|
||||
#include <gmssl/x509.h>
|
||||
#include <gmssl/cms.h>
|
||||
|
||||
|
||||
|
||||
static const char *options = "-key file -pass str -cert file -in file [-out file]";
|
||||
|
||||
int cmsdecrypt_main(int argc, char **argv)
|
||||
{
|
||||
int ret = 1;
|
||||
char *prog = argv[0];
|
||||
char *keyfile = NULL;
|
||||
char *pass = NULL;
|
||||
char *certfile = NULL;
|
||||
char *infile = NULL;
|
||||
char *outfile = NULL;
|
||||
FILE *keyfp = NULL;
|
||||
FILE *certfp = NULL;
|
||||
FILE *infp = NULL;
|
||||
FILE *outfp = stdout;
|
||||
uint8_t cert[1024];
|
||||
size_t certlen;
|
||||
struct stat st;
|
||||
uint8_t *cms = NULL;
|
||||
size_t cmslen, cms_maxlen;
|
||||
SM2_KEY key;
|
||||
int content_type;
|
||||
uint8_t *content = NULL;
|
||||
size_t content_len;
|
||||
const uint8_t *rcpt_infos;
|
||||
size_t rcpt_infos_len;
|
||||
const uint8_t *shared_info1;
|
||||
const uint8_t *shared_info2;
|
||||
size_t shared_info1_len, shared_info2_len;
|
||||
|
||||
argc--;
|
||||
argv++;
|
||||
|
||||
if (argc < 1) {
|
||||
fprintf(stderr, "usage: %s %s\n", prog, options);
|
||||
return 1;
|
||||
}
|
||||
|
||||
while (argc > 1) {
|
||||
if (!strcmp(*argv, "-help")) {
|
||||
printf("usage: %s %s\n", prog, options);
|
||||
ret = 0;
|
||||
goto end;
|
||||
} else if (!strcmp(*argv, "-key")) {
|
||||
if (--argc < 1) goto bad;
|
||||
keyfile = *(++argv);
|
||||
if (!(keyfp = fopen(keyfile, "r"))) {
|
||||
fprintf(stderr, "%s: open '%s' failure : %s\n", prog, keyfile, strerror(errno));
|
||||
goto end;
|
||||
}
|
||||
} else if (!strcmp(*argv, "-pass")) {
|
||||
if (--argc < 1) goto bad;
|
||||
pass = *(++argv);
|
||||
} else if (!strcmp(*argv, "-cert")) {
|
||||
if (--argc < 1) goto bad;
|
||||
certfile = *(++argv);
|
||||
if (!(certfp = fopen(certfile, "r"))) {
|
||||
fprintf(stderr, "%s: open '%s' failure : %s\n", prog, certfile, strerror(errno));
|
||||
goto end;
|
||||
}
|
||||
} else if (!strcmp(*argv, "-in")) {
|
||||
if (--argc < 1) goto bad;
|
||||
infile = *(++argv);
|
||||
if (!(infp = fopen(infile, "r"))) {
|
||||
fprintf(stderr, "%s: open '%s' failure : %s\n", prog, infile, strerror(errno));
|
||||
goto end;
|
||||
}
|
||||
} else if (!strcmp(*argv, "-out")) {
|
||||
if (--argc < 1) goto bad;
|
||||
outfile = *(++argv);
|
||||
if (!(outfp = fopen(outfile, "w"))) {
|
||||
fprintf(stderr, "%s: open '%s' failure : %s\n", prog, outfile, strerror(errno));
|
||||
goto end;
|
||||
}
|
||||
} else {
|
||||
fprintf(stderr, "%s: illegal option '%s'\n", prog, *argv);
|
||||
goto end;
|
||||
bad:
|
||||
fprintf(stderr, "%s: '%s' option value missing\n", prog, *argv);
|
||||
goto end;
|
||||
}
|
||||
|
||||
argc--;
|
||||
argv++;
|
||||
}
|
||||
|
||||
if (!keyfile) {
|
||||
fprintf(stderr, "%s: '-key' option required\n", prog);
|
||||
goto end;
|
||||
}
|
||||
if (!pass) {
|
||||
fprintf(stderr, "%s: '-pass' option required\n", prog);
|
||||
goto end;
|
||||
}
|
||||
if (!certfile) {
|
||||
fprintf(stderr, "%s: '-cert' option required\n", prog);
|
||||
goto end;
|
||||
}
|
||||
if (!infile) {
|
||||
fprintf(stderr, "%s: '-in' option required\n", prog);
|
||||
goto end;
|
||||
}
|
||||
|
||||
if (sm2_private_key_info_decrypt_from_pem(&key, pass, keyfp) != 1) {
|
||||
fprintf(stderr, "%s: private key decryption failure\n", prog);
|
||||
goto end;
|
||||
}
|
||||
if (x509_cert_from_pem(cert, &certlen, sizeof(cert), certfp) != 1) {
|
||||
fprintf(stderr, "%s: load certificate failure\n", prog);
|
||||
goto end;
|
||||
}
|
||||
|
||||
fstat(fileno(infp), &st);
|
||||
cms_maxlen = (st.st_size * 3)/4 + 1;
|
||||
if (!(cms = malloc(cms_maxlen))) {
|
||||
fprintf(stderr, "%s: malloc failure\n", prog);
|
||||
goto end;
|
||||
}
|
||||
if (cms_from_pem(cms, &cmslen, cms_maxlen, infp) != 1) {
|
||||
fprintf(stderr, "%s: read CMS failure\n", prog);
|
||||
goto end;
|
||||
}
|
||||
|
||||
if (!(content = malloc(cmslen))) {
|
||||
fprintf(stderr, "%s: malloc failure\n", prog);
|
||||
goto end;
|
||||
}
|
||||
|
||||
if (cms_deenvelop(cms, cmslen,
|
||||
&key, cert, certlen,
|
||||
&content_type, content, &content_len,
|
||||
&rcpt_infos, &rcpt_infos_len,
|
||||
&shared_info1, &shared_info1_len,
|
||||
&shared_info2, &shared_info2_len) != 1) {
|
||||
fprintf(stderr, "%s: decryption failure\n", prog);
|
||||
goto end;
|
||||
}
|
||||
if (content_type != OID_cms_data) {
|
||||
fprintf(stderr, "%s: invalid CMS content type: %s\n", prog, cms_content_type_name(content_type));
|
||||
goto end;
|
||||
}
|
||||
|
||||
if (fwrite(content, 1, content_len, outfp) != content_len) {
|
||||
fprintf(stderr, "%s: output failure : %s\n", prog, strerror(errno));
|
||||
goto end;
|
||||
}
|
||||
|
||||
ret = 0;
|
||||
|
||||
end:
|
||||
if (infile && infp) fclose(infp);
|
||||
if (outfile && outfp) fclose(outfp);
|
||||
if (keyfile && keyfp) fclose(keyfp);
|
||||
if (cms) free(cms);
|
||||
if (content) free(content);
|
||||
return ret;
|
||||
}
|
||||
|
||||
#include <stdio.h>
|
||||
#include <errno.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <sys/stat.h>
|
||||
#include <gmssl/x509.h>
|
||||
#include <gmssl/cms.h>
|
||||
|
||||
|
||||
|
||||
static const char *options = "-key file -pass str -cert file -in file [-out file]";
|
||||
|
||||
int cmsdecrypt_main(int argc, char **argv)
|
||||
{
|
||||
int ret = 1;
|
||||
char *prog = argv[0];
|
||||
char *keyfile = NULL;
|
||||
char *pass = NULL;
|
||||
char *certfile = NULL;
|
||||
char *infile = NULL;
|
||||
char *outfile = NULL;
|
||||
FILE *keyfp = NULL;
|
||||
FILE *certfp = NULL;
|
||||
FILE *infp = NULL;
|
||||
FILE *outfp = stdout;
|
||||
uint8_t cert[1024];
|
||||
size_t certlen;
|
||||
struct stat st;
|
||||
uint8_t *cms = NULL;
|
||||
size_t cmslen, cms_maxlen;
|
||||
SM2_KEY key;
|
||||
int content_type;
|
||||
uint8_t *content = NULL;
|
||||
size_t content_len;
|
||||
const uint8_t *rcpt_infos;
|
||||
size_t rcpt_infos_len;
|
||||
const uint8_t *shared_info1;
|
||||
const uint8_t *shared_info2;
|
||||
size_t shared_info1_len, shared_info2_len;
|
||||
|
||||
argc--;
|
||||
argv++;
|
||||
|
||||
if (argc < 1) {
|
||||
fprintf(stderr, "usage: %s %s\n", prog, options);
|
||||
return 1;
|
||||
}
|
||||
|
||||
while (argc > 1) {
|
||||
if (!strcmp(*argv, "-help")) {
|
||||
printf("usage: %s %s\n", prog, options);
|
||||
ret = 0;
|
||||
goto end;
|
||||
} else if (!strcmp(*argv, "-key")) {
|
||||
if (--argc < 1) goto bad;
|
||||
keyfile = *(++argv);
|
||||
if (!(keyfp = fopen(keyfile, "r"))) {
|
||||
fprintf(stderr, "%s: open '%s' failure : %s\n", prog, keyfile, strerror(errno));
|
||||
goto end;
|
||||
}
|
||||
} else if (!strcmp(*argv, "-pass")) {
|
||||
if (--argc < 1) goto bad;
|
||||
pass = *(++argv);
|
||||
} else if (!strcmp(*argv, "-cert")) {
|
||||
if (--argc < 1) goto bad;
|
||||
certfile = *(++argv);
|
||||
if (!(certfp = fopen(certfile, "r"))) {
|
||||
fprintf(stderr, "%s: open '%s' failure : %s\n", prog, certfile, strerror(errno));
|
||||
goto end;
|
||||
}
|
||||
} else if (!strcmp(*argv, "-in")) {
|
||||
if (--argc < 1) goto bad;
|
||||
infile = *(++argv);
|
||||
if (!(infp = fopen(infile, "r"))) {
|
||||
fprintf(stderr, "%s: open '%s' failure : %s\n", prog, infile, strerror(errno));
|
||||
goto end;
|
||||
}
|
||||
} else if (!strcmp(*argv, "-out")) {
|
||||
if (--argc < 1) goto bad;
|
||||
outfile = *(++argv);
|
||||
if (!(outfp = fopen(outfile, "w"))) {
|
||||
fprintf(stderr, "%s: open '%s' failure : %s\n", prog, outfile, strerror(errno));
|
||||
goto end;
|
||||
}
|
||||
} else {
|
||||
fprintf(stderr, "%s: illegal option '%s'\n", prog, *argv);
|
||||
goto end;
|
||||
bad:
|
||||
fprintf(stderr, "%s: '%s' option value missing\n", prog, *argv);
|
||||
goto end;
|
||||
}
|
||||
|
||||
argc--;
|
||||
argv++;
|
||||
}
|
||||
|
||||
if (!keyfile) {
|
||||
fprintf(stderr, "%s: '-key' option required\n", prog);
|
||||
goto end;
|
||||
}
|
||||
if (!pass) {
|
||||
fprintf(stderr, "%s: '-pass' option required\n", prog);
|
||||
goto end;
|
||||
}
|
||||
if (!certfile) {
|
||||
fprintf(stderr, "%s: '-cert' option required\n", prog);
|
||||
goto end;
|
||||
}
|
||||
if (!infile) {
|
||||
fprintf(stderr, "%s: '-in' option required\n", prog);
|
||||
goto end;
|
||||
}
|
||||
|
||||
if (sm2_private_key_info_decrypt_from_pem(&key, pass, keyfp) != 1) {
|
||||
fprintf(stderr, "%s: private key decryption failure\n", prog);
|
||||
goto end;
|
||||
}
|
||||
if (x509_cert_from_pem(cert, &certlen, sizeof(cert), certfp) != 1) {
|
||||
fprintf(stderr, "%s: load certificate failure\n", prog);
|
||||
goto end;
|
||||
}
|
||||
|
||||
fstat(fileno(infp), &st);
|
||||
cms_maxlen = (st.st_size * 3)/4 + 1;
|
||||
if (!(cms = malloc(cms_maxlen))) {
|
||||
fprintf(stderr, "%s: malloc failure\n", prog);
|
||||
goto end;
|
||||
}
|
||||
if (cms_from_pem(cms, &cmslen, cms_maxlen, infp) != 1) {
|
||||
fprintf(stderr, "%s: read CMS failure\n", prog);
|
||||
goto end;
|
||||
}
|
||||
|
||||
if (!(content = malloc(cmslen))) {
|
||||
fprintf(stderr, "%s: malloc failure\n", prog);
|
||||
goto end;
|
||||
}
|
||||
|
||||
if (cms_deenvelop(cms, cmslen,
|
||||
&key, cert, certlen,
|
||||
&content_type, content, &content_len,
|
||||
&rcpt_infos, &rcpt_infos_len,
|
||||
&shared_info1, &shared_info1_len,
|
||||
&shared_info2, &shared_info2_len) != 1) {
|
||||
fprintf(stderr, "%s: decryption failure\n", prog);
|
||||
goto end;
|
||||
}
|
||||
if (content_type != OID_cms_data) {
|
||||
fprintf(stderr, "%s: invalid CMS content type: %s\n", prog, cms_content_type_name(content_type));
|
||||
goto end;
|
||||
}
|
||||
|
||||
if (fwrite(content, 1, content_len, outfp) != content_len) {
|
||||
fprintf(stderr, "%s: output failure : %s\n", prog, strerror(errno));
|
||||
goto end;
|
||||
}
|
||||
|
||||
ret = 0;
|
||||
|
||||
end:
|
||||
if (infile && infp) fclose(infp);
|
||||
if (outfile && outfp) fclose(outfp);
|
||||
if (keyfile && keyfp) fclose(keyfp);
|
||||
if (cms) free(cms);
|
||||
if (content) free(content);
|
||||
return ret;
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2014-2022 The GmSSL Project. All Rights Reserved.
|
||||
* Copyright 2022 The GmSSL Project. All Rights Reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the License); you may
|
||||
* not use this file except in compliance with the License.
|
||||
@@ -7,227 +7,226 @@
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*/
|
||||
|
||||
|
||||
|
||||
#include <stdio.h>
|
||||
#include <errno.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <sys/stat.h>
|
||||
#include <gmssl/cms.h>
|
||||
#include <gmssl/x509.h>
|
||||
#include <gmssl/rand.h>
|
||||
|
||||
|
||||
/*
|
||||
|
||||
签名的时候要提供签名者的证书,并且提供签名私钥
|
||||
但是验证的时候假定CMS中已经包含签名者的证书了,但是我们要提供CA证书库
|
||||
|
||||
加密的时候要指定接收者的证书,并且可以有多个接收者
|
||||
解密的时候只提供一个解密私钥,但是最好配合解密者的证书,从这个证书中找到解密者的名字
|
||||
|
||||
如果即加密又签名,那么输出的是SignedAndEnveloped
|
||||
|
||||
CMS有PEM吗?
|
||||
|
||||
cms -encrypt -rcpt a.pem -rcpt b.pem -rcpt c.pem -in file -sign -signcert a.pem -signcert b.pem
|
||||
-rcptcert -rcpt_cert -sign_cert b.pem -signkey
|
||||
|
||||
首先接收者可以有多个证书
|
||||
|
||||
这里面有个问题,因为我们要输出一个加密的对象,因此我们必须把输入的内容读取进来。
|
||||
|
||||
|
||||
EnvelopedData 是一个封装的SEQUENCE中,因此必须读取所有的内容。
|
||||
如果是一个文件,就需要读取所有的文件内容,如果是一个stream ,也需要读取完整的内容到一个足够大的buffer中,如何设置这个buffer的大小呢
|
||||
|
||||
|
||||
|
||||
对于输入文件,如果输入有文件名的话,可以直接通过stat获取文件长度
|
||||
但是如果对于stream的话,实际上我们是没有办法获得输入长度的,那么就直接准备一个buffer好了。
|
||||
不要给自己找麻烦了,直接只支持文件输入吧
|
||||
encrypt
|
||||
|
||||
*/
|
||||
|
||||
static const char *options = "-encrypt (-rcptcert pem)* -in file -out file";
|
||||
|
||||
|
||||
static int get_files_size(int argc, char **argv, const char *option, size_t *len)
|
||||
{
|
||||
char *prog = argv[0];
|
||||
char *file = NULL;
|
||||
FILE *fp = NULL;
|
||||
struct stat st;
|
||||
|
||||
argc--;
|
||||
argv++;
|
||||
|
||||
*len = 0;
|
||||
while (argc > 1) {
|
||||
if (!strcmp(*argv, option)) {
|
||||
if (--argc < 1) {
|
||||
fprintf(stderr, "%s: '%s' option value missing\n", prog, *argv);
|
||||
return -1;
|
||||
}
|
||||
file = *(++argv);
|
||||
if (!(fp = fopen(file, "r"))) {
|
||||
fprintf(stderr, "%s: open '%s' failed : %s\n", prog, file, strerror(errno));
|
||||
return -1;
|
||||
}
|
||||
if (fstat(fileno(fp), &st) < 0) {
|
||||
fprintf(stderr, "%s: access '%s' failed : %s\n", prog, file, strerror(errno));
|
||||
fclose(fp);
|
||||
return -1;
|
||||
}
|
||||
*len += st.st_size;
|
||||
fclose(fp);
|
||||
}
|
||||
argc--;
|
||||
argv++;
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
int cmsencrypt_main(int argc, char **argv)
|
||||
{
|
||||
int ret = 1;
|
||||
char *prog = argv[0];
|
||||
int op = 0;
|
||||
char *infile = NULL;
|
||||
char *outfile = NULL;
|
||||
FILE *infp = stdin;
|
||||
FILE *outfp = stdout;
|
||||
uint8_t *rcpt_certs = NULL;
|
||||
size_t rcpt_certs_len;
|
||||
uint8_t key[16];
|
||||
uint8_t iv[16];
|
||||
uint8_t *inbuf = NULL;
|
||||
size_t inlen;
|
||||
uint8_t *cms = NULL;
|
||||
size_t cmslen;
|
||||
uint8_t *cert;
|
||||
|
||||
if (argc < 2) {
|
||||
fprintf(stderr, "usage: %s %s\n", prog, options);
|
||||
return 1;
|
||||
}
|
||||
|
||||
// 预先统计证书缓冲大小和输入大小
|
||||
if (get_files_size(argc, argv, "-rcptcert", &rcpt_certs_len) != 1) {
|
||||
goto end;
|
||||
}
|
||||
if (rcpt_certs_len <= 0) {
|
||||
fprintf(stderr, "%s: invalid cert length\n", prog);
|
||||
goto end;
|
||||
}
|
||||
rcpt_certs_len = (rcpt_certs_len * 3)/4;
|
||||
if (!(rcpt_certs = malloc(rcpt_certs_len))) {
|
||||
fprintf(stderr, "%s: malloc failure\n", prog);
|
||||
goto end;
|
||||
}
|
||||
cert = rcpt_certs;
|
||||
|
||||
if (get_files_size(argc, argv, "-in", &inlen) != 1) {
|
||||
goto end;
|
||||
}
|
||||
if (inlen <= 0) {
|
||||
fprintf(stderr, "%s: invalid input length\n", prog);
|
||||
goto end;
|
||||
}
|
||||
if (!(inbuf = malloc(inlen))) {
|
||||
fprintf(stderr, "%s: %s\n", prog, strerror(errno));
|
||||
goto end;
|
||||
}
|
||||
|
||||
argc--;
|
||||
argv++;
|
||||
|
||||
while (argc > 1) {
|
||||
if (!strcmp(*argv, "-help")) {
|
||||
printf("usage: %s %s\n", prog, options);
|
||||
ret = 0;
|
||||
goto end;
|
||||
} else if (!strcmp(*argv, "-rcptcert")) {
|
||||
char *certfile;
|
||||
FILE *certfp;
|
||||
size_t certlen;
|
||||
if (--argc < 1) goto bad;
|
||||
certfile = *(++argv);
|
||||
if (!(certfp = fopen(certfile, "r"))) {
|
||||
fprintf(stderr, "%s: open '%s' failure : %s\n", prog, certfile, strerror(errno));
|
||||
goto end;
|
||||
}
|
||||
if (x509_cert_from_pem(cert, &certlen, rcpt_certs_len, certfp) != 1) {
|
||||
fprintf(stderr, "%s: error\n", prog);
|
||||
fclose(certfp);
|
||||
goto end;
|
||||
}
|
||||
cert += certlen;
|
||||
fclose(certfp);
|
||||
} else if (!strcmp(*argv, "-in")) {
|
||||
if (--argc < 1) goto bad;
|
||||
infile = *(++argv);
|
||||
if (!(infp = fopen(infile, "r"))) {
|
||||
fprintf(stderr, "%s: open '%s' failure : %s\n", prog, infile, strerror(errno));
|
||||
goto end;
|
||||
}
|
||||
if ((inlen = fread(inbuf, 1, inlen, infp)) <= 0) {
|
||||
fprintf(stderr, "%s: read data error: %s\n", prog, strerror(errno));
|
||||
goto end;
|
||||
}
|
||||
} else if (!strcmp(*argv, "-out")) {
|
||||
if (--argc < 1) goto bad;
|
||||
outfile = *(++argv);
|
||||
if (!(outfp = fopen(outfile, "w"))) {
|
||||
fprintf(stderr, "%s: open '%s' failure : %s\n", prog, outfile, strerror(errno));
|
||||
goto end;
|
||||
}
|
||||
} else {
|
||||
fprintf(stderr, "%s: illegal option '%s'\n", prog, *argv);
|
||||
goto end;
|
||||
bad:
|
||||
fprintf(stderr, "%s: '%s' option value missing\n", prog, *argv);
|
||||
goto end;
|
||||
}
|
||||
|
||||
argc--;
|
||||
argv++;
|
||||
}
|
||||
|
||||
rcpt_certs_len = cert - rcpt_certs;
|
||||
|
||||
if (rand_bytes(key, sizeof(key)) != 1
|
||||
|| rand_bytes(iv, sizeof(iv)) != 1
|
||||
|| cms_envelop(NULL, &cmslen, rcpt_certs, rcpt_certs_len,
|
||||
OID_sm4_cbc, key, sizeof(key), iv, sizeof(iv),
|
||||
OID_cms_data, inbuf, inlen, NULL, 0, NULL, 0) != 1) {
|
||||
fprintf(stderr, "%s: inner error\n", prog);
|
||||
goto end;
|
||||
}
|
||||
if (!(cms = malloc(cmslen))) {
|
||||
fprintf(stderr, "%s: malloc failure\n", prog);
|
||||
goto end;
|
||||
}
|
||||
if (cms_envelop(cms, &cmslen, rcpt_certs, rcpt_certs_len,
|
||||
OID_sm4_cbc, key, sizeof(key), iv, sizeof(iv),
|
||||
OID_cms_data, inbuf, inlen, NULL, 0, NULL, 0) != 1) {
|
||||
fprintf(stderr, "%s: inner error\n", prog);
|
||||
goto end;
|
||||
}
|
||||
if (cms_to_pem(cms, cmslen, outfp) != 1) {
|
||||
fprintf(stderr, "%s: output CMS failure\n", prog);
|
||||
goto end;
|
||||
}
|
||||
|
||||
ret = 0;
|
||||
|
||||
end:
|
||||
if (infile && infp) fclose(infp);
|
||||
if (outfile && outfp) fclose(outfp);
|
||||
if (rcpt_certs) free(rcpt_certs);
|
||||
if (cms) free(cms);
|
||||
return ret;
|
||||
}
|
||||
|
||||
#include <stdio.h>
|
||||
#include <errno.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <sys/stat.h>
|
||||
#include <gmssl/cms.h>
|
||||
#include <gmssl/x509.h>
|
||||
#include <gmssl/rand.h>
|
||||
|
||||
|
||||
/*
|
||||
|
||||
签名的时候要提供签名者的证书,并且提供签名私钥
|
||||
但是验证的时候假定CMS中已经包含签名者的证书了,但是我们要提供CA证书库
|
||||
|
||||
加密的时候要指定接收者的证书,并且可以有多个接收者
|
||||
解密的时候只提供一个解密私钥,但是最好配合解密者的证书,从这个证书中找到解密者的名字
|
||||
|
||||
如果即加密又签名,那么输出的是SignedAndEnveloped
|
||||
|
||||
CMS有PEM吗?
|
||||
|
||||
cms -encrypt -rcpt a.pem -rcpt b.pem -rcpt c.pem -in file -sign -signcert a.pem -signcert b.pem
|
||||
-rcptcert -rcpt_cert -sign_cert b.pem -signkey
|
||||
|
||||
首先接收者可以有多个证书
|
||||
|
||||
这里面有个问题,因为我们要输出一个加密的对象,因此我们必须把输入的内容读取进来。
|
||||
|
||||
|
||||
EnvelopedData 是一个封装的SEQUENCE中,因此必须读取所有的内容。
|
||||
如果是一个文件,就需要读取所有的文件内容,如果是一个stream ,也需要读取完整的内容到一个足够大的buffer中,如何设置这个buffer的大小呢
|
||||
|
||||
|
||||
|
||||
对于输入文件,如果输入有文件名的话,可以直接通过stat获取文件长度
|
||||
但是如果对于stream的话,实际上我们是没有办法获得输入长度的,那么就直接准备一个buffer好了。
|
||||
不要给自己找麻烦了,直接只支持文件输入吧
|
||||
encrypt
|
||||
|
||||
*/
|
||||
|
||||
static const char *options = "-encrypt (-rcptcert pem)* -in file -out file";
|
||||
|
||||
|
||||
static int get_files_size(int argc, char **argv, const char *option, size_t *len)
|
||||
{
|
||||
char *prog = argv[0];
|
||||
char *file = NULL;
|
||||
FILE *fp = NULL;
|
||||
struct stat st;
|
||||
|
||||
argc--;
|
||||
argv++;
|
||||
|
||||
*len = 0;
|
||||
while (argc > 1) {
|
||||
if (!strcmp(*argv, option)) {
|
||||
if (--argc < 1) {
|
||||
fprintf(stderr, "%s: '%s' option value missing\n", prog, *argv);
|
||||
return -1;
|
||||
}
|
||||
file = *(++argv);
|
||||
if (!(fp = fopen(file, "r"))) {
|
||||
fprintf(stderr, "%s: open '%s' failed : %s\n", prog, file, strerror(errno));
|
||||
return -1;
|
||||
}
|
||||
if (fstat(fileno(fp), &st) < 0) {
|
||||
fprintf(stderr, "%s: access '%s' failed : %s\n", prog, file, strerror(errno));
|
||||
fclose(fp);
|
||||
return -1;
|
||||
}
|
||||
*len += st.st_size;
|
||||
fclose(fp);
|
||||
}
|
||||
argc--;
|
||||
argv++;
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
int cmsencrypt_main(int argc, char **argv)
|
||||
{
|
||||
int ret = 1;
|
||||
char *prog = argv[0];
|
||||
int op = 0;
|
||||
char *infile = NULL;
|
||||
char *outfile = NULL;
|
||||
FILE *infp = stdin;
|
||||
FILE *outfp = stdout;
|
||||
uint8_t *rcpt_certs = NULL;
|
||||
size_t rcpt_certs_len;
|
||||
uint8_t key[16];
|
||||
uint8_t iv[16];
|
||||
uint8_t *inbuf = NULL;
|
||||
size_t inlen;
|
||||
uint8_t *cms = NULL;
|
||||
size_t cmslen;
|
||||
uint8_t *cert;
|
||||
|
||||
if (argc < 2) {
|
||||
fprintf(stderr, "usage: %s %s\n", prog, options);
|
||||
return 1;
|
||||
}
|
||||
|
||||
// 预先统计证书缓冲大小和输入大小
|
||||
if (get_files_size(argc, argv, "-rcptcert", &rcpt_certs_len) != 1) {
|
||||
goto end;
|
||||
}
|
||||
if (rcpt_certs_len <= 0) {
|
||||
fprintf(stderr, "%s: invalid cert length\n", prog);
|
||||
goto end;
|
||||
}
|
||||
rcpt_certs_len = (rcpt_certs_len * 3)/4;
|
||||
if (!(rcpt_certs = malloc(rcpt_certs_len))) {
|
||||
fprintf(stderr, "%s: malloc failure\n", prog);
|
||||
goto end;
|
||||
}
|
||||
cert = rcpt_certs;
|
||||
|
||||
if (get_files_size(argc, argv, "-in", &inlen) != 1) {
|
||||
goto end;
|
||||
}
|
||||
if (inlen <= 0) {
|
||||
fprintf(stderr, "%s: invalid input length\n", prog);
|
||||
goto end;
|
||||
}
|
||||
if (!(inbuf = malloc(inlen))) {
|
||||
fprintf(stderr, "%s: %s\n", prog, strerror(errno));
|
||||
goto end;
|
||||
}
|
||||
|
||||
argc--;
|
||||
argv++;
|
||||
|
||||
while (argc > 1) {
|
||||
if (!strcmp(*argv, "-help")) {
|
||||
printf("usage: %s %s\n", prog, options);
|
||||
ret = 0;
|
||||
goto end;
|
||||
} else if (!strcmp(*argv, "-rcptcert")) {
|
||||
char *certfile;
|
||||
FILE *certfp;
|
||||
size_t certlen;
|
||||
if (--argc < 1) goto bad;
|
||||
certfile = *(++argv);
|
||||
if (!(certfp = fopen(certfile, "r"))) {
|
||||
fprintf(stderr, "%s: open '%s' failure : %s\n", prog, certfile, strerror(errno));
|
||||
goto end;
|
||||
}
|
||||
if (x509_cert_from_pem(cert, &certlen, rcpt_certs_len, certfp) != 1) {
|
||||
fprintf(stderr, "%s: error\n", prog);
|
||||
fclose(certfp);
|
||||
goto end;
|
||||
}
|
||||
cert += certlen;
|
||||
fclose(certfp);
|
||||
} else if (!strcmp(*argv, "-in")) {
|
||||
if (--argc < 1) goto bad;
|
||||
infile = *(++argv);
|
||||
if (!(infp = fopen(infile, "r"))) {
|
||||
fprintf(stderr, "%s: open '%s' failure : %s\n", prog, infile, strerror(errno));
|
||||
goto end;
|
||||
}
|
||||
if ((inlen = fread(inbuf, 1, inlen, infp)) <= 0) {
|
||||
fprintf(stderr, "%s: read data error: %s\n", prog, strerror(errno));
|
||||
goto end;
|
||||
}
|
||||
} else if (!strcmp(*argv, "-out")) {
|
||||
if (--argc < 1) goto bad;
|
||||
outfile = *(++argv);
|
||||
if (!(outfp = fopen(outfile, "w"))) {
|
||||
fprintf(stderr, "%s: open '%s' failure : %s\n", prog, outfile, strerror(errno));
|
||||
goto end;
|
||||
}
|
||||
} else {
|
||||
fprintf(stderr, "%s: illegal option '%s'\n", prog, *argv);
|
||||
goto end;
|
||||
bad:
|
||||
fprintf(stderr, "%s: '%s' option value missing\n", prog, *argv);
|
||||
goto end;
|
||||
}
|
||||
|
||||
argc--;
|
||||
argv++;
|
||||
}
|
||||
|
||||
rcpt_certs_len = cert - rcpt_certs;
|
||||
|
||||
if (rand_bytes(key, sizeof(key)) != 1
|
||||
|| rand_bytes(iv, sizeof(iv)) != 1
|
||||
|| cms_envelop(NULL, &cmslen, rcpt_certs, rcpt_certs_len,
|
||||
OID_sm4_cbc, key, sizeof(key), iv, sizeof(iv),
|
||||
OID_cms_data, inbuf, inlen, NULL, 0, NULL, 0) != 1) {
|
||||
fprintf(stderr, "%s: inner error\n", prog);
|
||||
goto end;
|
||||
}
|
||||
if (!(cms = malloc(cmslen))) {
|
||||
fprintf(stderr, "%s: malloc failure\n", prog);
|
||||
goto end;
|
||||
}
|
||||
if (cms_envelop(cms, &cmslen, rcpt_certs, rcpt_certs_len,
|
||||
OID_sm4_cbc, key, sizeof(key), iv, sizeof(iv),
|
||||
OID_cms_data, inbuf, inlen, NULL, 0, NULL, 0) != 1) {
|
||||
fprintf(stderr, "%s: inner error\n", prog);
|
||||
goto end;
|
||||
}
|
||||
if (cms_to_pem(cms, cmslen, outfp) != 1) {
|
||||
fprintf(stderr, "%s: output CMS failure\n", prog);
|
||||
goto end;
|
||||
}
|
||||
|
||||
ret = 0;
|
||||
|
||||
end:
|
||||
if (infile && infp) fclose(infp);
|
||||
if (outfile && outfp) fclose(outfp);
|
||||
if (rcpt_certs) free(rcpt_certs);
|
||||
if (cms) free(cms);
|
||||
return ret;
|
||||
}
|
||||
|
||||
161
tools/cmsparse.c
161
tools/cmsparse.c
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2014-2022 The GmSSL Project. All Rights Reserved.
|
||||
* Copyright 2022 The GmSSL Project. All Rights Reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the License); you may
|
||||
* not use this file except in compliance with the License.
|
||||
@@ -7,83 +7,82 @@
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*/
|
||||
|
||||
|
||||
|
||||
#include <stdio.h>
|
||||
#include <errno.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <sys/stat.h>
|
||||
#include <gmssl/cms.h>
|
||||
#include <gmssl/x509.h>
|
||||
#include <gmssl/rand.h>
|
||||
|
||||
|
||||
static const char *options = "-in file";
|
||||
|
||||
int cmsparse_main(int argc, char **argv)
|
||||
{
|
||||
int ret = 1;
|
||||
char *prog = argv[0];
|
||||
char *infile = NULL;
|
||||
FILE *infp = stdin;
|
||||
struct stat st;
|
||||
uint8_t *cms = NULL;
|
||||
size_t cms_maxlen, cmslen;
|
||||
|
||||
argc--;
|
||||
argv++;
|
||||
|
||||
if (argc < 1) {
|
||||
fprintf(stderr, "usage: %s %s\n", prog, options);
|
||||
return 1;
|
||||
}
|
||||
while (argc > 1) {
|
||||
if (!strcmp(*argv, "-help")) {
|
||||
printf("usage: %s %s\n", prog, options);
|
||||
ret = 0;
|
||||
goto end;
|
||||
} else if (!strcmp(*argv, "-in")) {
|
||||
if (--argc < 1) goto bad;
|
||||
infile = *(++argv);
|
||||
if (!(infp = fopen(infile, "r"))) {
|
||||
fprintf(stderr, "%s: open '%s' failure : %s\n", prog, infile, strerror(errno));
|
||||
goto end;
|
||||
}
|
||||
} else {
|
||||
fprintf(stderr, "%s: illegal option '%s'\n", prog, *argv);
|
||||
goto end;
|
||||
bad:
|
||||
fprintf(stderr, "%s: '%s' option value missing\n", prog, *argv);
|
||||
goto end;
|
||||
}
|
||||
|
||||
argc--;
|
||||
argv++;
|
||||
}
|
||||
|
||||
if (!infile) {
|
||||
fprintf(stderr, "%s: option '-in' required'\n", prog);
|
||||
goto end;
|
||||
}
|
||||
|
||||
if (fstat(fileno(infp), &st) < 0) {
|
||||
fprintf(stderr, "%s: access '%s' failed : %s\n", prog, infile, strerror(errno));
|
||||
goto end;
|
||||
}
|
||||
cms_maxlen = (st.st_size * 3)/4 + 1;
|
||||
if (!(cms = malloc(cms_maxlen))) {
|
||||
fprintf(stderr, "%s: malloc failure\n", prog);
|
||||
goto end;
|
||||
}
|
||||
if (cms_from_pem(cms, &cmslen, cms_maxlen, infp) != 1) {
|
||||
fprintf(stderr, "%s: parse CMS error\n", prog);
|
||||
goto end;
|
||||
}
|
||||
cms_print(stdout, 0, 0, "CMS", cms, cmslen);
|
||||
ret = 0;
|
||||
end:
|
||||
if (infp) fclose(infp);
|
||||
if (cms) free(cms);
|
||||
return ret;
|
||||
}
|
||||
|
||||
#include <stdio.h>
|
||||
#include <errno.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <sys/stat.h>
|
||||
#include <gmssl/cms.h>
|
||||
#include <gmssl/x509.h>
|
||||
#include <gmssl/rand.h>
|
||||
|
||||
|
||||
static const char *options = "-in file";
|
||||
|
||||
int cmsparse_main(int argc, char **argv)
|
||||
{
|
||||
int ret = 1;
|
||||
char *prog = argv[0];
|
||||
char *infile = NULL;
|
||||
FILE *infp = stdin;
|
||||
struct stat st;
|
||||
uint8_t *cms = NULL;
|
||||
size_t cms_maxlen, cmslen;
|
||||
|
||||
argc--;
|
||||
argv++;
|
||||
|
||||
if (argc < 1) {
|
||||
fprintf(stderr, "usage: %s %s\n", prog, options);
|
||||
return 1;
|
||||
}
|
||||
while (argc > 1) {
|
||||
if (!strcmp(*argv, "-help")) {
|
||||
printf("usage: %s %s\n", prog, options);
|
||||
ret = 0;
|
||||
goto end;
|
||||
} else if (!strcmp(*argv, "-in")) {
|
||||
if (--argc < 1) goto bad;
|
||||
infile = *(++argv);
|
||||
if (!(infp = fopen(infile, "r"))) {
|
||||
fprintf(stderr, "%s: open '%s' failure : %s\n", prog, infile, strerror(errno));
|
||||
goto end;
|
||||
}
|
||||
} else {
|
||||
fprintf(stderr, "%s: illegal option '%s'\n", prog, *argv);
|
||||
goto end;
|
||||
bad:
|
||||
fprintf(stderr, "%s: '%s' option value missing\n", prog, *argv);
|
||||
goto end;
|
||||
}
|
||||
|
||||
argc--;
|
||||
argv++;
|
||||
}
|
||||
|
||||
if (!infile) {
|
||||
fprintf(stderr, "%s: option '-in' required'\n", prog);
|
||||
goto end;
|
||||
}
|
||||
|
||||
if (fstat(fileno(infp), &st) < 0) {
|
||||
fprintf(stderr, "%s: access '%s' failed : %s\n", prog, infile, strerror(errno));
|
||||
goto end;
|
||||
}
|
||||
cms_maxlen = (st.st_size * 3)/4 + 1;
|
||||
if (!(cms = malloc(cms_maxlen))) {
|
||||
fprintf(stderr, "%s: malloc failure\n", prog);
|
||||
goto end;
|
||||
}
|
||||
if (cms_from_pem(cms, &cmslen, cms_maxlen, infp) != 1) {
|
||||
fprintf(stderr, "%s: parse CMS error\n", prog);
|
||||
goto end;
|
||||
}
|
||||
cms_print(stdout, 0, 0, "CMS", cms, cmslen);
|
||||
ret = 0;
|
||||
end:
|
||||
if (infp) fclose(infp);
|
||||
if (cms) free(cms);
|
||||
return ret;
|
||||
}
|
||||
|
||||
401
tools/cmssign.c
401
tools/cmssign.c
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2014-2022 The GmSSL Project. All Rights Reserved.
|
||||
* Copyright 2022 The GmSSL Project. All Rights Reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the License); you may
|
||||
* not use this file except in compliance with the License.
|
||||
@@ -7,203 +7,202 @@
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*/
|
||||
|
||||
|
||||
|
||||
#include <stdio.h>
|
||||
#include <errno.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <sys/stat.h>
|
||||
#include <gmssl/x509.h>
|
||||
#include <gmssl/cms.h>
|
||||
#include <gmssl/error.h>
|
||||
|
||||
|
||||
/*
|
||||
302 typedef struct {
|
||||
303 uint8_t *certs;
|
||||
304 size_t certs_len;
|
||||
305 SM2_KEY *sign_key;
|
||||
306 } CMS_CERTS_AND_KEY;
|
||||
|
||||
|
||||
|
||||
输出长度主要由输入长度和
|
||||
|
||||
*/
|
||||
|
||||
static const char *options = "-key file -pass str -cert file -in file [-out file]";
|
||||
|
||||
int cmssign_main(int argc, char **argv)
|
||||
{
|
||||
int ret = 1;
|
||||
char *prog = argv[0];
|
||||
char *keyfile = NULL;
|
||||
char *pass = NULL;
|
||||
char *certfile = NULL;
|
||||
char *infile = NULL;
|
||||
char *outfile = NULL;
|
||||
FILE *keyfp = NULL;
|
||||
FILE *certfp = NULL;
|
||||
FILE *infp = NULL;
|
||||
FILE *outfp = stdout;
|
||||
SM2_KEY key;
|
||||
uint8_t cert[1024];
|
||||
size_t certlen;
|
||||
struct stat st;
|
||||
uint8_t *in = NULL;
|
||||
size_t inlen;
|
||||
uint8_t *cms = NULL;
|
||||
size_t cmslen, cms_maxlen;
|
||||
CMS_CERTS_AND_KEY cert_and_key;
|
||||
|
||||
int content_type;
|
||||
uint8_t *content = NULL;
|
||||
size_t content_len;
|
||||
|
||||
const uint8_t *rcpt_infos;
|
||||
size_t rcpt_infos_len;
|
||||
const uint8_t *shared_info1;
|
||||
const uint8_t *shared_info2;
|
||||
size_t shared_info1_len, shared_info2_len;
|
||||
|
||||
argc--;
|
||||
argv++;
|
||||
|
||||
if (argc < 1) {
|
||||
fprintf(stderr, "usage: %s %s\n", prog, options);
|
||||
return 1;
|
||||
}
|
||||
|
||||
while (argc > 1) {
|
||||
if (!strcmp(*argv, "-help")) {
|
||||
printf("usage: %s %s\n", prog, options);
|
||||
ret = 0;
|
||||
goto end;
|
||||
} else if (!strcmp(*argv, "-key")) {
|
||||
if (--argc < 1) goto bad;
|
||||
keyfile = *(++argv);
|
||||
if (!(keyfp = fopen(keyfile, "r"))) {
|
||||
fprintf(stderr, "%s: open '%s' failure : %s\n", prog, keyfile, strerror(errno));
|
||||
goto end;
|
||||
}
|
||||
} else if (!strcmp(*argv, "-pass")) {
|
||||
if (--argc < 1) goto bad;
|
||||
pass = *(++argv);
|
||||
} else if (!strcmp(*argv, "-cert")) {
|
||||
if (--argc < 1) goto bad;
|
||||
certfile = *(++argv);
|
||||
if (!(certfp = fopen(certfile, "r"))) {
|
||||
fprintf(stderr, "%s: open '%s' failure : %s\n", prog, certfile, strerror(errno));
|
||||
goto end;
|
||||
}
|
||||
} else if (!strcmp(*argv, "-in")) {
|
||||
if (--argc < 1) goto bad;
|
||||
infile = *(++argv);
|
||||
if (!(infp = fopen(infile, "r"))) {
|
||||
fprintf(stderr, "%s: open '%s' failure : %s\n", prog, infile, strerror(errno));
|
||||
goto end;
|
||||
}
|
||||
} else if (!strcmp(*argv, "-out")) {
|
||||
if (--argc < 1) goto bad;
|
||||
outfile = *(++argv);
|
||||
if (!(outfp = fopen(outfile, "w"))) {
|
||||
fprintf(stderr, "%s: open '%s' failure : %s\n", prog, outfile, strerror(errno));
|
||||
goto end;
|
||||
}
|
||||
} else {
|
||||
fprintf(stderr, "%s: illegal option '%s'\n", prog, *argv);
|
||||
goto end;
|
||||
bad:
|
||||
fprintf(stderr, "%s: '%s' option value missing\n", prog, *argv);
|
||||
goto end;
|
||||
}
|
||||
|
||||
argc--;
|
||||
argv++;
|
||||
}
|
||||
|
||||
if (!keyfile) {
|
||||
fprintf(stderr, "%s: '-key' option required\n", prog);
|
||||
goto end;
|
||||
}
|
||||
if (!pass) {
|
||||
fprintf(stderr, "%s: '-pass' option required\n", prog);
|
||||
goto end;
|
||||
}
|
||||
if (!certfile) {
|
||||
fprintf(stderr, "%s: '-cert' option required\n", prog);
|
||||
goto end;
|
||||
}
|
||||
if (!infile) {
|
||||
fprintf(stderr, "%s: '-in' option required\n", prog);
|
||||
goto end;
|
||||
}
|
||||
|
||||
if (sm2_private_key_info_decrypt_from_pem(&key, pass, keyfp) != 1) {
|
||||
fprintf(stderr, "%s: private key decryption failure\n", prog);
|
||||
goto end;
|
||||
}
|
||||
if (x509_cert_from_pem(cert, &certlen, sizeof(cert), certfp) != 1) {
|
||||
fprintf(stderr, "%s: load certificate failure\n", prog);
|
||||
goto end;
|
||||
}
|
||||
{
|
||||
SM2_KEY public_key;
|
||||
if (x509_cert_get_subject_public_key(cert, certlen, &public_key) != 1) {
|
||||
fprintf(stderr, "%s: parse certficate failure\n", prog);
|
||||
goto end;
|
||||
}
|
||||
if (sm2_public_key_equ(&key, &public_key) != 1) {
|
||||
fprintf(stderr, "%s: key and cert are not match!\n", prog);
|
||||
goto end;
|
||||
}
|
||||
}
|
||||
|
||||
cert_and_key.certs = cert;
|
||||
cert_and_key.certs_len = certlen;
|
||||
cert_and_key.sign_key = &key;
|
||||
|
||||
if (fstat(fileno(infp), &st) < 0) {
|
||||
fprintf(stderr, "%s: access file error : %s\n", prog, strerror(errno));
|
||||
goto end;
|
||||
}
|
||||
if ((inlen = st.st_size) <= 0) {
|
||||
fprintf(stderr, "%s: invalid input length\n", prog);
|
||||
goto end;
|
||||
}
|
||||
if (!(in = malloc(inlen))) {
|
||||
fprintf(stderr, "%s: malloc failure\n", prog);
|
||||
goto end;
|
||||
}
|
||||
if (fread(in, 1, inlen, infp) != inlen) {
|
||||
fprintf(stderr, "%s: read file error : %s\n", prog, strerror(errno));
|
||||
goto end;
|
||||
}
|
||||
|
||||
cms_maxlen = (inlen * 4)/3 + 4096; // 主要由SignerInfos,其中的DN长度决定
|
||||
if (!(cms = malloc(cms_maxlen))) {
|
||||
fprintf(stderr, "%s: malloc failure\n", prog);
|
||||
goto end;
|
||||
}
|
||||
|
||||
if (cms_sign(cms, &cmslen, &cert_and_key, 1, OID_cms_data, in, inlen, NULL, 0) != 1) {
|
||||
fprintf(stderr, "%s: sign failure\n", prog);
|
||||
goto end;
|
||||
}
|
||||
|
||||
if (cms_to_pem(cms, cmslen, outfp) != 1) {
|
||||
fprintf(stderr, "%s: output failure\n", prog);
|
||||
goto end;
|
||||
}
|
||||
|
||||
ret = 0;
|
||||
|
||||
end:
|
||||
if (infile && infp) fclose(infp);
|
||||
if (outfile && outfp) fclose(outfp);
|
||||
if (keyfile && keyfp) fclose(keyfp);
|
||||
if (cms) free(cms);
|
||||
if (in) free(in);
|
||||
return ret;
|
||||
}
|
||||
|
||||
#include <stdio.h>
|
||||
#include <errno.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <sys/stat.h>
|
||||
#include <gmssl/x509.h>
|
||||
#include <gmssl/cms.h>
|
||||
#include <gmssl/error.h>
|
||||
|
||||
|
||||
/*
|
||||
302 typedef struct {
|
||||
303 uint8_t *certs;
|
||||
304 size_t certs_len;
|
||||
305 SM2_KEY *sign_key;
|
||||
306 } CMS_CERTS_AND_KEY;
|
||||
|
||||
|
||||
|
||||
输出长度主要由输入长度和
|
||||
|
||||
*/
|
||||
|
||||
static const char *options = "-key file -pass str -cert file -in file [-out file]";
|
||||
|
||||
int cmssign_main(int argc, char **argv)
|
||||
{
|
||||
int ret = 1;
|
||||
char *prog = argv[0];
|
||||
char *keyfile = NULL;
|
||||
char *pass = NULL;
|
||||
char *certfile = NULL;
|
||||
char *infile = NULL;
|
||||
char *outfile = NULL;
|
||||
FILE *keyfp = NULL;
|
||||
FILE *certfp = NULL;
|
||||
FILE *infp = NULL;
|
||||
FILE *outfp = stdout;
|
||||
SM2_KEY key;
|
||||
uint8_t cert[1024];
|
||||
size_t certlen;
|
||||
struct stat st;
|
||||
uint8_t *in = NULL;
|
||||
size_t inlen;
|
||||
uint8_t *cms = NULL;
|
||||
size_t cmslen, cms_maxlen;
|
||||
CMS_CERTS_AND_KEY cert_and_key;
|
||||
|
||||
int content_type;
|
||||
uint8_t *content = NULL;
|
||||
size_t content_len;
|
||||
|
||||
const uint8_t *rcpt_infos;
|
||||
size_t rcpt_infos_len;
|
||||
const uint8_t *shared_info1;
|
||||
const uint8_t *shared_info2;
|
||||
size_t shared_info1_len, shared_info2_len;
|
||||
|
||||
argc--;
|
||||
argv++;
|
||||
|
||||
if (argc < 1) {
|
||||
fprintf(stderr, "usage: %s %s\n", prog, options);
|
||||
return 1;
|
||||
}
|
||||
|
||||
while (argc > 1) {
|
||||
if (!strcmp(*argv, "-help")) {
|
||||
printf("usage: %s %s\n", prog, options);
|
||||
ret = 0;
|
||||
goto end;
|
||||
} else if (!strcmp(*argv, "-key")) {
|
||||
if (--argc < 1) goto bad;
|
||||
keyfile = *(++argv);
|
||||
if (!(keyfp = fopen(keyfile, "r"))) {
|
||||
fprintf(stderr, "%s: open '%s' failure : %s\n", prog, keyfile, strerror(errno));
|
||||
goto end;
|
||||
}
|
||||
} else if (!strcmp(*argv, "-pass")) {
|
||||
if (--argc < 1) goto bad;
|
||||
pass = *(++argv);
|
||||
} else if (!strcmp(*argv, "-cert")) {
|
||||
if (--argc < 1) goto bad;
|
||||
certfile = *(++argv);
|
||||
if (!(certfp = fopen(certfile, "r"))) {
|
||||
fprintf(stderr, "%s: open '%s' failure : %s\n", prog, certfile, strerror(errno));
|
||||
goto end;
|
||||
}
|
||||
} else if (!strcmp(*argv, "-in")) {
|
||||
if (--argc < 1) goto bad;
|
||||
infile = *(++argv);
|
||||
if (!(infp = fopen(infile, "r"))) {
|
||||
fprintf(stderr, "%s: open '%s' failure : %s\n", prog, infile, strerror(errno));
|
||||
goto end;
|
||||
}
|
||||
} else if (!strcmp(*argv, "-out")) {
|
||||
if (--argc < 1) goto bad;
|
||||
outfile = *(++argv);
|
||||
if (!(outfp = fopen(outfile, "w"))) {
|
||||
fprintf(stderr, "%s: open '%s' failure : %s\n", prog, outfile, strerror(errno));
|
||||
goto end;
|
||||
}
|
||||
} else {
|
||||
fprintf(stderr, "%s: illegal option '%s'\n", prog, *argv);
|
||||
goto end;
|
||||
bad:
|
||||
fprintf(stderr, "%s: '%s' option value missing\n", prog, *argv);
|
||||
goto end;
|
||||
}
|
||||
|
||||
argc--;
|
||||
argv++;
|
||||
}
|
||||
|
||||
if (!keyfile) {
|
||||
fprintf(stderr, "%s: '-key' option required\n", prog);
|
||||
goto end;
|
||||
}
|
||||
if (!pass) {
|
||||
fprintf(stderr, "%s: '-pass' option required\n", prog);
|
||||
goto end;
|
||||
}
|
||||
if (!certfile) {
|
||||
fprintf(stderr, "%s: '-cert' option required\n", prog);
|
||||
goto end;
|
||||
}
|
||||
if (!infile) {
|
||||
fprintf(stderr, "%s: '-in' option required\n", prog);
|
||||
goto end;
|
||||
}
|
||||
|
||||
if (sm2_private_key_info_decrypt_from_pem(&key, pass, keyfp) != 1) {
|
||||
fprintf(stderr, "%s: private key decryption failure\n", prog);
|
||||
goto end;
|
||||
}
|
||||
if (x509_cert_from_pem(cert, &certlen, sizeof(cert), certfp) != 1) {
|
||||
fprintf(stderr, "%s: load certificate failure\n", prog);
|
||||
goto end;
|
||||
}
|
||||
{
|
||||
SM2_KEY public_key;
|
||||
if (x509_cert_get_subject_public_key(cert, certlen, &public_key) != 1) {
|
||||
fprintf(stderr, "%s: parse certficate failure\n", prog);
|
||||
goto end;
|
||||
}
|
||||
if (sm2_public_key_equ(&key, &public_key) != 1) {
|
||||
fprintf(stderr, "%s: key and cert are not match!\n", prog);
|
||||
goto end;
|
||||
}
|
||||
}
|
||||
|
||||
cert_and_key.certs = cert;
|
||||
cert_and_key.certs_len = certlen;
|
||||
cert_and_key.sign_key = &key;
|
||||
|
||||
if (fstat(fileno(infp), &st) < 0) {
|
||||
fprintf(stderr, "%s: access file error : %s\n", prog, strerror(errno));
|
||||
goto end;
|
||||
}
|
||||
if ((inlen = st.st_size) <= 0) {
|
||||
fprintf(stderr, "%s: invalid input length\n", prog);
|
||||
goto end;
|
||||
}
|
||||
if (!(in = malloc(inlen))) {
|
||||
fprintf(stderr, "%s: malloc failure\n", prog);
|
||||
goto end;
|
||||
}
|
||||
if (fread(in, 1, inlen, infp) != inlen) {
|
||||
fprintf(stderr, "%s: read file error : %s\n", prog, strerror(errno));
|
||||
goto end;
|
||||
}
|
||||
|
||||
cms_maxlen = (inlen * 4)/3 + 4096; // 主要由SignerInfos,其中的DN长度决定
|
||||
if (!(cms = malloc(cms_maxlen))) {
|
||||
fprintf(stderr, "%s: malloc failure\n", prog);
|
||||
goto end;
|
||||
}
|
||||
|
||||
if (cms_sign(cms, &cmslen, &cert_and_key, 1, OID_cms_data, in, inlen, NULL, 0) != 1) {
|
||||
fprintf(stderr, "%s: sign failure\n", prog);
|
||||
goto end;
|
||||
}
|
||||
|
||||
if (cms_to_pem(cms, cmslen, outfp) != 1) {
|
||||
fprintf(stderr, "%s: output failure\n", prog);
|
||||
goto end;
|
||||
}
|
||||
|
||||
ret = 0;
|
||||
|
||||
end:
|
||||
if (infile && infp) fclose(infp);
|
||||
if (outfile && outfp) fclose(outfp);
|
||||
if (keyfile && keyfp) fclose(keyfp);
|
||||
if (cms) free(cms);
|
||||
if (in) free(in);
|
||||
return ret;
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2014-2022 The GmSSL Project. All Rights Reserved.
|
||||
* Copyright 2022 The GmSSL Project. All Rights Reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the License); you may
|
||||
* not use this file except in compliance with the License.
|
||||
@@ -7,134 +7,133 @@
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*/
|
||||
|
||||
|
||||
|
||||
#include <stdio.h>
|
||||
#include <errno.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <sys/stat.h>
|
||||
#include <gmssl/cms.h>
|
||||
#include <gmssl/x509.h>
|
||||
#include <gmssl/rand.h>
|
||||
|
||||
|
||||
|
||||
static const char *options = "-in file [-out file]";
|
||||
|
||||
int cmsverify_main(int argc, char **argv)
|
||||
{
|
||||
int ret = 1;
|
||||
char *prog = argv[0];
|
||||
char *infile = NULL;
|
||||
char *outfile = NULL;
|
||||
FILE *infp = NULL;
|
||||
FILE *outfp = NULL;
|
||||
struct stat st;
|
||||
uint8_t *cms = NULL;
|
||||
size_t cmslen, cms_maxlen;
|
||||
|
||||
int content_type;
|
||||
const uint8_t *content;
|
||||
size_t content_len;
|
||||
const uint8_t *certs;
|
||||
size_t certslen;
|
||||
const uint8_t *crls;
|
||||
size_t crlslen;
|
||||
const uint8_t *signer_infos;
|
||||
size_t signer_infos_len;
|
||||
int rv;
|
||||
|
||||
argc--;
|
||||
argv++;
|
||||
|
||||
if (argc < 1) {
|
||||
fprintf(stderr, "usage: %s %s\n", prog, options);
|
||||
return 1;
|
||||
}
|
||||
|
||||
while (argc > 1) {
|
||||
if (!strcmp(*argv, "-help")) {
|
||||
printf("usage: %s %s\n", prog, options);
|
||||
ret = 0;
|
||||
goto end;
|
||||
} else if (!strcmp(*argv, "-in")) {
|
||||
if (--argc < 1) goto bad;
|
||||
infile = *(++argv);
|
||||
if (!(infp = fopen(infile, "r"))) {
|
||||
fprintf(stderr, "%s: open '%s' failure : %s\n", prog, infile, strerror(errno));
|
||||
goto end;
|
||||
}
|
||||
} else if (!strcmp(*argv, "-out")) {
|
||||
if (--argc < 1) goto bad;
|
||||
outfile = *(++argv);
|
||||
if (!(outfp = fopen(outfile, "w"))) {
|
||||
fprintf(stderr, "%s: open '%s' failure : %s\n", prog, outfile, strerror(errno));
|
||||
goto end;
|
||||
}
|
||||
} else {
|
||||
fprintf(stderr, "%s: illegal option '%s'\n", prog, *argv);
|
||||
goto end;
|
||||
bad:
|
||||
fprintf(stderr, "%s: '%s' option value missing\n", prog, *argv);
|
||||
goto end;
|
||||
}
|
||||
|
||||
argc--;
|
||||
argv++;
|
||||
}
|
||||
|
||||
if (!infile) {
|
||||
fprintf(stderr, "%s: '-in' option required\n", prog);
|
||||
goto end;
|
||||
}
|
||||
fstat(fileno(infp), &st);
|
||||
cms_maxlen = (st.st_size * 3)/4 + 1;
|
||||
if (!(cms = malloc(cms_maxlen))) {
|
||||
fprintf(stderr, "%s: malloc failure\n", prog);
|
||||
goto end;
|
||||
}
|
||||
if (cms_from_pem(cms, &cmslen, cms_maxlen, infp) != 1) {
|
||||
fprintf(stderr, "%s: read CMS failure\n", prog);
|
||||
goto end;
|
||||
}
|
||||
|
||||
if ((rv = cms_verify(cms, cmslen, NULL, 0, NULL, 0,
|
||||
&content_type, &content, &content_len,
|
||||
&certs, &certslen, &crls, &crlslen,
|
||||
&signer_infos, &signer_infos_len)) < 0) {
|
||||
fprintf(stderr, "%s: verify error\n", prog);
|
||||
goto end;
|
||||
}
|
||||
printf("verify %s\n", rv ? "success" : "failure");
|
||||
ret = rv ? 0 : 1;
|
||||
|
||||
if (outfile) {
|
||||
const uint8_t *p;
|
||||
size_t len;
|
||||
|
||||
if (content_type == OID_cms_data) {
|
||||
if (asn1_octet_string_from_der(&p, &len, &content, &content_len) != 1
|
||||
|| asn1_length_is_zero(content_len) != 1) {
|
||||
fprintf(stderr, "%s: invalid CMS\n", prog);
|
||||
goto end;
|
||||
}
|
||||
if (len != fwrite(p, 1, len, outfp)) {
|
||||
fprintf(stderr, "%s: output error : %s\n", prog, strerror(errno));
|
||||
goto end;
|
||||
}
|
||||
} else {
|
||||
fprintf(stderr, "%s: error\n", prog);
|
||||
goto end;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
end:
|
||||
if (infile && infp) fclose(infp);
|
||||
if (outfile && outfp) fclose(outfp);
|
||||
if (cms) free(cms);
|
||||
return ret;
|
||||
}
|
||||
|
||||
#include <stdio.h>
|
||||
#include <errno.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <sys/stat.h>
|
||||
#include <gmssl/cms.h>
|
||||
#include <gmssl/x509.h>
|
||||
#include <gmssl/rand.h>
|
||||
|
||||
|
||||
|
||||
static const char *options = "-in file [-out file]";
|
||||
|
||||
int cmsverify_main(int argc, char **argv)
|
||||
{
|
||||
int ret = 1;
|
||||
char *prog = argv[0];
|
||||
char *infile = NULL;
|
||||
char *outfile = NULL;
|
||||
FILE *infp = NULL;
|
||||
FILE *outfp = NULL;
|
||||
struct stat st;
|
||||
uint8_t *cms = NULL;
|
||||
size_t cmslen, cms_maxlen;
|
||||
|
||||
int content_type;
|
||||
const uint8_t *content;
|
||||
size_t content_len;
|
||||
const uint8_t *certs;
|
||||
size_t certslen;
|
||||
const uint8_t *crls;
|
||||
size_t crlslen;
|
||||
const uint8_t *signer_infos;
|
||||
size_t signer_infos_len;
|
||||
int rv;
|
||||
|
||||
argc--;
|
||||
argv++;
|
||||
|
||||
if (argc < 1) {
|
||||
fprintf(stderr, "usage: %s %s\n", prog, options);
|
||||
return 1;
|
||||
}
|
||||
|
||||
while (argc > 1) {
|
||||
if (!strcmp(*argv, "-help")) {
|
||||
printf("usage: %s %s\n", prog, options);
|
||||
ret = 0;
|
||||
goto end;
|
||||
} else if (!strcmp(*argv, "-in")) {
|
||||
if (--argc < 1) goto bad;
|
||||
infile = *(++argv);
|
||||
if (!(infp = fopen(infile, "r"))) {
|
||||
fprintf(stderr, "%s: open '%s' failure : %s\n", prog, infile, strerror(errno));
|
||||
goto end;
|
||||
}
|
||||
} else if (!strcmp(*argv, "-out")) {
|
||||
if (--argc < 1) goto bad;
|
||||
outfile = *(++argv);
|
||||
if (!(outfp = fopen(outfile, "w"))) {
|
||||
fprintf(stderr, "%s: open '%s' failure : %s\n", prog, outfile, strerror(errno));
|
||||
goto end;
|
||||
}
|
||||
} else {
|
||||
fprintf(stderr, "%s: illegal option '%s'\n", prog, *argv);
|
||||
goto end;
|
||||
bad:
|
||||
fprintf(stderr, "%s: '%s' option value missing\n", prog, *argv);
|
||||
goto end;
|
||||
}
|
||||
|
||||
argc--;
|
||||
argv++;
|
||||
}
|
||||
|
||||
if (!infile) {
|
||||
fprintf(stderr, "%s: '-in' option required\n", prog);
|
||||
goto end;
|
||||
}
|
||||
fstat(fileno(infp), &st);
|
||||
cms_maxlen = (st.st_size * 3)/4 + 1;
|
||||
if (!(cms = malloc(cms_maxlen))) {
|
||||
fprintf(stderr, "%s: malloc failure\n", prog);
|
||||
goto end;
|
||||
}
|
||||
if (cms_from_pem(cms, &cmslen, cms_maxlen, infp) != 1) {
|
||||
fprintf(stderr, "%s: read CMS failure\n", prog);
|
||||
goto end;
|
||||
}
|
||||
|
||||
if ((rv = cms_verify(cms, cmslen, NULL, 0, NULL, 0,
|
||||
&content_type, &content, &content_len,
|
||||
&certs, &certslen, &crls, &crlslen,
|
||||
&signer_infos, &signer_infos_len)) < 0) {
|
||||
fprintf(stderr, "%s: verify error\n", prog);
|
||||
goto end;
|
||||
}
|
||||
printf("verify %s\n", rv ? "success" : "failure");
|
||||
ret = rv ? 0 : 1;
|
||||
|
||||
if (outfile) {
|
||||
const uint8_t *p;
|
||||
size_t len;
|
||||
|
||||
if (content_type == OID_cms_data) {
|
||||
if (asn1_octet_string_from_der(&p, &len, &content, &content_len) != 1
|
||||
|| asn1_length_is_zero(content_len) != 1) {
|
||||
fprintf(stderr, "%s: invalid CMS\n", prog);
|
||||
goto end;
|
||||
}
|
||||
if (len != fwrite(p, 1, len, outfp)) {
|
||||
fprintf(stderr, "%s: output error : %s\n", prog, strerror(errno));
|
||||
goto end;
|
||||
}
|
||||
} else {
|
||||
fprintf(stderr, "%s: error\n", prog);
|
||||
goto end;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
end:
|
||||
if (infile && infp) fclose(infp);
|
||||
if (outfile && outfp) fclose(outfp);
|
||||
if (cms) free(cms);
|
||||
return ret;
|
||||
}
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
YEAR=`date "+%Y"`
|
||||
COPYRIGHT=""
|
||||
COPYRIGHT+="/*"$'\n'
|
||||
COPYRIGHT+=" * Copyright 2014-$YEAR The GmSSL Project. All Rights Reserved."$'\n'
|
||||
COPYRIGHT+=" * Copyright $YEAR The GmSSL Project. All Rights Reserved."$'\n'
|
||||
COPYRIGHT+=" *"$'\n'
|
||||
COPYRIGHT+=" * Licensed under the Apache License, Version 2.0 (the "License"); you may"$'\n'
|
||||
COPYRIGHT+=" * not use this file except in compliance with the License."$'\n'
|
||||
@@ -50,4 +50,4 @@ function getDir() {
|
||||
|
||||
getDir ..
|
||||
|
||||
rm -f $COPYRIGHT_FILE
|
||||
rm -f $COPYRIGHT_FILE
|
||||
201
tools/crlparse.c
201
tools/crlparse.c
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2014-2022 The GmSSL Project. All Rights Reserved.
|
||||
* Copyright 2022 The GmSSL Project. All Rights Reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the License); you may
|
||||
* not use this file except in compliance with the License.
|
||||
@@ -7,103 +7,102 @@
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*/
|
||||
|
||||
|
||||
|
||||
#include <stdio.h>
|
||||
#include <errno.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <sys/stat.h>
|
||||
#include <gmssl/x509.h>
|
||||
#include <gmssl/x509_crl.h>
|
||||
|
||||
|
||||
static const char *options = "-in file [-out file]";
|
||||
|
||||
int crlparse_main(int argc, char **argv)
|
||||
{
|
||||
int ret = 1;
|
||||
char *prog = argv[0];
|
||||
char *infile = NULL;
|
||||
char *outfile = NULL;
|
||||
FILE *infp = stdin;
|
||||
FILE *outfp = stdout;
|
||||
struct stat st;
|
||||
uint8_t *in = NULL;
|
||||
size_t inlen;
|
||||
const uint8_t *pin;
|
||||
const uint8_t *crl = NULL;
|
||||
size_t crllen;
|
||||
|
||||
argc--;
|
||||
argv++;
|
||||
|
||||
if (argc < 1) {
|
||||
fprintf(stderr, "usage: %s %s\n", prog, options);
|
||||
return 1;
|
||||
}
|
||||
|
||||
while (argc > 0) {
|
||||
if (!strcmp(*argv, "-help")) {
|
||||
printf("usage: %s %s\n", prog, options);
|
||||
goto end;
|
||||
} else if (!strcmp(*argv, "-in")) {
|
||||
if (--argc < 1) goto bad;
|
||||
infile = *(++argv);
|
||||
if (!(infp = fopen(infile, "r"))) {
|
||||
fprintf(stderr, "%s: open '%s' failure : %s\n", prog, infile, strerror(errno));
|
||||
goto end;
|
||||
}
|
||||
} else if (!strcmp(*argv, "-out")) {
|
||||
if (--argc < 1) goto bad;
|
||||
outfile = *(++argv);
|
||||
if (!(outfp = fopen(outfile, "w"))) {
|
||||
fprintf(stderr, "%s: open '%s' failure : %s\n", prog, outfile, strerror(errno));
|
||||
goto end;
|
||||
}
|
||||
} else {
|
||||
fprintf(stderr, "%s: illegal option '%s'\n", prog, *argv);
|
||||
goto end;
|
||||
bad:
|
||||
fprintf(stderr, "%s: '%s' option value missing\n", prog, *argv);
|
||||
goto end;
|
||||
}
|
||||
|
||||
argc--;
|
||||
argv++;
|
||||
}
|
||||
|
||||
if (!infile) {
|
||||
fprintf(stderr, "%s: '-in' option required\n", prog);
|
||||
goto end;
|
||||
}
|
||||
if (fstat(fileno(infp), &st) < 0) {
|
||||
fprintf(stderr, "%s: access file error : %s\n", prog, strerror(errno));
|
||||
goto end;
|
||||
}
|
||||
if ((inlen = st.st_size) <= 0) {
|
||||
fprintf(stderr, "%s: invalid input length\n", prog);
|
||||
goto end;
|
||||
}
|
||||
if (!(in = malloc(inlen))) {
|
||||
fprintf(stderr, "%s: malloc failure\n", prog);
|
||||
goto end;
|
||||
}
|
||||
if (fread(in, 1, inlen, infp) != inlen) {
|
||||
fprintf(stderr, "%s: read file error : %s\n", prog, strerror(errno));
|
||||
goto end;
|
||||
}
|
||||
pin = in;
|
||||
if (x509_crl_from_der(&crl, &crllen, &pin, &inlen) != 1
|
||||
|| asn1_length_is_zero(inlen) != 1) {
|
||||
fprintf(stderr, "%s: read CRL failure\n", prog);
|
||||
goto end;
|
||||
}
|
||||
x509_crl_print(outfp, 0, 0, "CRL", crl, crllen);
|
||||
|
||||
end:
|
||||
if (infile && infp) fclose(infp);
|
||||
if (outfile && outfp) fclose(outfp);
|
||||
if (in) free(in);
|
||||
return ret;
|
||||
}
|
||||
|
||||
#include <stdio.h>
|
||||
#include <errno.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <sys/stat.h>
|
||||
#include <gmssl/x509.h>
|
||||
#include <gmssl/x509_crl.h>
|
||||
|
||||
|
||||
static const char *options = "-in file [-out file]";
|
||||
|
||||
int crlparse_main(int argc, char **argv)
|
||||
{
|
||||
int ret = 1;
|
||||
char *prog = argv[0];
|
||||
char *infile = NULL;
|
||||
char *outfile = NULL;
|
||||
FILE *infp = stdin;
|
||||
FILE *outfp = stdout;
|
||||
struct stat st;
|
||||
uint8_t *in = NULL;
|
||||
size_t inlen;
|
||||
const uint8_t *pin;
|
||||
const uint8_t *crl = NULL;
|
||||
size_t crllen;
|
||||
|
||||
argc--;
|
||||
argv++;
|
||||
|
||||
if (argc < 1) {
|
||||
fprintf(stderr, "usage: %s %s\n", prog, options);
|
||||
return 1;
|
||||
}
|
||||
|
||||
while (argc > 0) {
|
||||
if (!strcmp(*argv, "-help")) {
|
||||
printf("usage: %s %s\n", prog, options);
|
||||
goto end;
|
||||
} else if (!strcmp(*argv, "-in")) {
|
||||
if (--argc < 1) goto bad;
|
||||
infile = *(++argv);
|
||||
if (!(infp = fopen(infile, "r"))) {
|
||||
fprintf(stderr, "%s: open '%s' failure : %s\n", prog, infile, strerror(errno));
|
||||
goto end;
|
||||
}
|
||||
} else if (!strcmp(*argv, "-out")) {
|
||||
if (--argc < 1) goto bad;
|
||||
outfile = *(++argv);
|
||||
if (!(outfp = fopen(outfile, "w"))) {
|
||||
fprintf(stderr, "%s: open '%s' failure : %s\n", prog, outfile, strerror(errno));
|
||||
goto end;
|
||||
}
|
||||
} else {
|
||||
fprintf(stderr, "%s: illegal option '%s'\n", prog, *argv);
|
||||
goto end;
|
||||
bad:
|
||||
fprintf(stderr, "%s: '%s' option value missing\n", prog, *argv);
|
||||
goto end;
|
||||
}
|
||||
|
||||
argc--;
|
||||
argv++;
|
||||
}
|
||||
|
||||
if (!infile) {
|
||||
fprintf(stderr, "%s: '-in' option required\n", prog);
|
||||
goto end;
|
||||
}
|
||||
if (fstat(fileno(infp), &st) < 0) {
|
||||
fprintf(stderr, "%s: access file error : %s\n", prog, strerror(errno));
|
||||
goto end;
|
||||
}
|
||||
if ((inlen = st.st_size) <= 0) {
|
||||
fprintf(stderr, "%s: invalid input length\n", prog);
|
||||
goto end;
|
||||
}
|
||||
if (!(in = malloc(inlen))) {
|
||||
fprintf(stderr, "%s: malloc failure\n", prog);
|
||||
goto end;
|
||||
}
|
||||
if (fread(in, 1, inlen, infp) != inlen) {
|
||||
fprintf(stderr, "%s: read file error : %s\n", prog, strerror(errno));
|
||||
goto end;
|
||||
}
|
||||
pin = in;
|
||||
if (x509_crl_from_der(&crl, &crllen, &pin, &inlen) != 1
|
||||
|| asn1_length_is_zero(inlen) != 1) {
|
||||
fprintf(stderr, "%s: read CRL failure\n", prog);
|
||||
goto end;
|
||||
}
|
||||
x509_crl_print(outfp, 0, 0, "CRL", crl, crllen);
|
||||
|
||||
end:
|
||||
if (infile && infp) fclose(infp);
|
||||
if (outfile && outfp) fclose(outfp);
|
||||
if (in) free(in);
|
||||
return ret;
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2014-2022 The GmSSL Project. All Rights Reserved.
|
||||
* Copyright 2022 The GmSSL Project. All Rights Reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the License); you may
|
||||
* not use this file except in compliance with the License.
|
||||
@@ -7,151 +7,150 @@
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*/
|
||||
|
||||
|
||||
|
||||
#include <stdio.h>
|
||||
#include <errno.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <sys/stat.h>
|
||||
#include <gmssl/x509.h>
|
||||
#include <gmssl/x509_crl.h>
|
||||
|
||||
|
||||
static const char *options = "-in file -cacert file\n";
|
||||
|
||||
int crlverify_main(int argc, char **argv)
|
||||
{
|
||||
int ret = 1;
|
||||
char *prog = argv[0];
|
||||
char *infile = NULL;
|
||||
char *cacertfile = NULL;
|
||||
FILE *infp = NULL;
|
||||
FILE *cacertfp = NULL;
|
||||
uint8_t *in = NULL;
|
||||
size_t inlen;
|
||||
struct stat st;
|
||||
const uint8_t *pin;
|
||||
const uint8_t *crl = NULL;
|
||||
size_t crllen;
|
||||
const uint8_t *subject;
|
||||
size_t subject_len;
|
||||
uint8_t cacert[1024];
|
||||
size_t cacertlen;
|
||||
int rv;
|
||||
|
||||
argc--;
|
||||
argv++;
|
||||
|
||||
if (argc < 1) {
|
||||
fprintf(stderr, "usage: %s %s\n", prog, options);
|
||||
return 1;
|
||||
}
|
||||
|
||||
while (argc > 0) {
|
||||
if (!strcmp(*argv, "-help")) {
|
||||
printf("usage: %s %s\n", prog, options);
|
||||
ret = 0;
|
||||
goto end;
|
||||
} else if (!strcmp(*argv, "-in")) {
|
||||
if (--argc < 1) goto bad;
|
||||
infile = *(++argv);
|
||||
if (!(infp = fopen(infile, "r"))) {
|
||||
fprintf(stderr, "%s: open '%s' failure : %s\n", prog, infile, strerror(errno));
|
||||
goto end;
|
||||
}
|
||||
} else if (!strcmp(*argv, "-cacert")) {
|
||||
if (--argc < 1) goto bad;
|
||||
cacertfile = *(++argv);
|
||||
if (!(cacertfp = fopen(cacertfile, "r"))) {
|
||||
fprintf(stderr, "%s: open '%s' failure : %s\n", prog, cacertfile, strerror(errno));
|
||||
goto end;
|
||||
}
|
||||
} else {
|
||||
fprintf(stderr, "%s: illegal option '%s'\n", prog, *argv);
|
||||
goto end;
|
||||
bad:
|
||||
fprintf(stderr, "%s: '%s' option value missing\n", prog, *argv);
|
||||
goto end;
|
||||
}
|
||||
|
||||
argc--;
|
||||
argv++;
|
||||
}
|
||||
|
||||
if (!infile) {
|
||||
fprintf(stderr, "%s: '-in' option required\n", prog);
|
||||
goto end;
|
||||
}
|
||||
if (!cacertfile) {
|
||||
fprintf(stderr, "%s: '-cacert' option required\n", prog);
|
||||
goto end;
|
||||
}
|
||||
|
||||
|
||||
if (fstat(fileno(infp), &st) < 0) {
|
||||
fprintf(stderr, "%s: access file error : %s\n", prog, strerror(errno));
|
||||
goto end;
|
||||
}
|
||||
if ((inlen = st.st_size) <= 0) {
|
||||
fprintf(stderr, "%s: invalid input length\n", prog);
|
||||
goto end;
|
||||
}
|
||||
if (!(in = malloc(inlen))) {
|
||||
fprintf(stderr, "%s: malloc failure\n", prog);
|
||||
goto end;
|
||||
}
|
||||
if (fread(in, 1, inlen, infp) != inlen) {
|
||||
fprintf(stderr, "%s: read file error : %s\n", prog, strerror(errno));
|
||||
goto end;
|
||||
}
|
||||
pin = in;
|
||||
if (x509_crl_from_der(&crl, &crllen, &pin, &inlen) != 1
|
||||
|| asn1_length_is_zero(inlen) != 1) {
|
||||
fprintf(stderr, "%s: read CRL failure\n", prog);
|
||||
goto end;
|
||||
}
|
||||
|
||||
if (x509_crl_get_issuer(crl, crllen, &subject, &subject_len) != 1) {
|
||||
fprintf(stderr, "%s: inner error\n", prog);
|
||||
goto end;
|
||||
}
|
||||
if (x509_cert_from_pem_by_subject(cacert, &cacertlen, sizeof(cacert), subject, subject_len, cacertfp) != 1) {
|
||||
fprintf(stderr, "%s: read certificate failure\n", prog);
|
||||
goto end;
|
||||
}
|
||||
if ((rv = x509_crl_verify_by_ca_cert(crl, crllen, cacert, cacertlen, SM2_DEFAULT_ID, strlen(SM2_DEFAULT_ID))) < 0) {
|
||||
fprintf(stderr, "%s: verification inner error\n", prog);
|
||||
goto end;
|
||||
}
|
||||
|
||||
printf("Verification %s\n", rv ? "success" : "failure");
|
||||
if (rv == 1) ret = 0;
|
||||
|
||||
end:
|
||||
if (infile && infp) fclose(infp);
|
||||
if (cacertfp) fclose(cacertfp);
|
||||
if (in) free(in);
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
#include <stdio.h>
|
||||
#include <errno.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <sys/stat.h>
|
||||
#include <gmssl/x509.h>
|
||||
#include <gmssl/x509_crl.h>
|
||||
|
||||
|
||||
static const char *options = "-in file -cacert file\n";
|
||||
|
||||
int crlverify_main(int argc, char **argv)
|
||||
{
|
||||
int ret = 1;
|
||||
char *prog = argv[0];
|
||||
char *infile = NULL;
|
||||
char *cacertfile = NULL;
|
||||
FILE *infp = NULL;
|
||||
FILE *cacertfp = NULL;
|
||||
uint8_t *in = NULL;
|
||||
size_t inlen;
|
||||
struct stat st;
|
||||
const uint8_t *pin;
|
||||
const uint8_t *crl = NULL;
|
||||
size_t crllen;
|
||||
const uint8_t *subject;
|
||||
size_t subject_len;
|
||||
uint8_t cacert[1024];
|
||||
size_t cacertlen;
|
||||
int rv;
|
||||
|
||||
argc--;
|
||||
argv++;
|
||||
|
||||
if (argc < 1) {
|
||||
fprintf(stderr, "usage: %s %s\n", prog, options);
|
||||
return 1;
|
||||
}
|
||||
|
||||
while (argc > 0) {
|
||||
if (!strcmp(*argv, "-help")) {
|
||||
printf("usage: %s %s\n", prog, options);
|
||||
ret = 0;
|
||||
goto end;
|
||||
} else if (!strcmp(*argv, "-in")) {
|
||||
if (--argc < 1) goto bad;
|
||||
infile = *(++argv);
|
||||
if (!(infp = fopen(infile, "r"))) {
|
||||
fprintf(stderr, "%s: open '%s' failure : %s\n", prog, infile, strerror(errno));
|
||||
goto end;
|
||||
}
|
||||
} else if (!strcmp(*argv, "-cacert")) {
|
||||
if (--argc < 1) goto bad;
|
||||
cacertfile = *(++argv);
|
||||
if (!(cacertfp = fopen(cacertfile, "r"))) {
|
||||
fprintf(stderr, "%s: open '%s' failure : %s\n", prog, cacertfile, strerror(errno));
|
||||
goto end;
|
||||
}
|
||||
} else {
|
||||
fprintf(stderr, "%s: illegal option '%s'\n", prog, *argv);
|
||||
goto end;
|
||||
bad:
|
||||
fprintf(stderr, "%s: '%s' option value missing\n", prog, *argv);
|
||||
goto end;
|
||||
}
|
||||
|
||||
argc--;
|
||||
argv++;
|
||||
}
|
||||
|
||||
if (!infile) {
|
||||
fprintf(stderr, "%s: '-in' option required\n", prog);
|
||||
goto end;
|
||||
}
|
||||
if (!cacertfile) {
|
||||
fprintf(stderr, "%s: '-cacert' option required\n", prog);
|
||||
goto end;
|
||||
}
|
||||
|
||||
|
||||
if (fstat(fileno(infp), &st) < 0) {
|
||||
fprintf(stderr, "%s: access file error : %s\n", prog, strerror(errno));
|
||||
goto end;
|
||||
}
|
||||
if ((inlen = st.st_size) <= 0) {
|
||||
fprintf(stderr, "%s: invalid input length\n", prog);
|
||||
goto end;
|
||||
}
|
||||
if (!(in = malloc(inlen))) {
|
||||
fprintf(stderr, "%s: malloc failure\n", prog);
|
||||
goto end;
|
||||
}
|
||||
if (fread(in, 1, inlen, infp) != inlen) {
|
||||
fprintf(stderr, "%s: read file error : %s\n", prog, strerror(errno));
|
||||
goto end;
|
||||
}
|
||||
pin = in;
|
||||
if (x509_crl_from_der(&crl, &crllen, &pin, &inlen) != 1
|
||||
|| asn1_length_is_zero(inlen) != 1) {
|
||||
fprintf(stderr, "%s: read CRL failure\n", prog);
|
||||
goto end;
|
||||
}
|
||||
|
||||
if (x509_crl_get_issuer(crl, crllen, &subject, &subject_len) != 1) {
|
||||
fprintf(stderr, "%s: inner error\n", prog);
|
||||
goto end;
|
||||
}
|
||||
if (x509_cert_from_pem_by_subject(cacert, &cacertlen, sizeof(cacert), subject, subject_len, cacertfp) != 1) {
|
||||
fprintf(stderr, "%s: read certificate failure\n", prog);
|
||||
goto end;
|
||||
}
|
||||
if ((rv = x509_crl_verify_by_ca_cert(crl, crllen, cacert, cacertlen, SM2_DEFAULT_ID, strlen(SM2_DEFAULT_ID))) < 0) {
|
||||
fprintf(stderr, "%s: verification inner error\n", prog);
|
||||
goto end;
|
||||
}
|
||||
|
||||
printf("Verification %s\n", rv ? "success" : "failure");
|
||||
if (rv == 1) ret = 0;
|
||||
|
||||
end:
|
||||
if (infile && infp) fclose(infp);
|
||||
if (cacertfp) fclose(cacertfp);
|
||||
if (in) free(in);
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
405
tools/gmssl.c
405
tools/gmssl.c
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2014-2022 The GmSSL Project. All Rights Reserved.
|
||||
* Copyright 2022 The GmSSL Project. All Rights Reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the License); you may
|
||||
* not use this file except in compliance with the License.
|
||||
@@ -7,205 +7,204 @@
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*/
|
||||
|
||||
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
|
||||
extern int version_main(int argc, char **argv);
|
||||
extern int rand_main(int argc, char **argv);
|
||||
extern int certgen_main(int argc, char **argv);
|
||||
extern int certparse_main(int argc, char **argv);
|
||||
extern int certverify_main(int argc, char **argv);
|
||||
extern int crlparse_main(int argc, char **argv);
|
||||
extern int crlverify_main(int argc, char **argv);
|
||||
extern int pbkdf2_main(int argc, char **argv);
|
||||
extern int reqgen_main(int argc, char **argv);
|
||||
extern int reqparse_main(int argc, char **argv);
|
||||
extern int reqsign_main(int argc, char **argv);
|
||||
extern int sm2keygen_main(int argc, char **argv);
|
||||
extern int sm2sign_main(int argc, char **argv);
|
||||
extern int sm2verify_main(int argc, char **argv);
|
||||
extern int sm2encrypt_main(int argc, char **argv);
|
||||
extern int sm2decrypt_main(int argc, char **argv);
|
||||
extern int sm3_main(int argc, char **argv);
|
||||
extern int sm3hmac_main(int argc, char **argv);
|
||||
extern int sm4_main(int argc, char **argv);
|
||||
extern int zuc_main(int argc, char **argv);
|
||||
extern int sm9setup_main(int argc, char **argv);
|
||||
extern int sm9keygen_main(int argc, char **argv);
|
||||
extern int sm9sign_main(int argc, char **argv);
|
||||
extern int sm9verify_main(int argc, char **argv);
|
||||
extern int sm9encrypt_main(int argc, char **argv);
|
||||
extern int sm9decrypt_main(int argc, char **argv);
|
||||
extern int cmsparse_main(int argc, char **argv);
|
||||
extern int cmsencrypt_main(int argc, char **argv);
|
||||
extern int cmsdecrypt_main(int argc, char **argv);
|
||||
extern int cmssign_main(int argc, char **argv);
|
||||
extern int cmsverify_main(int argc, char **argv);
|
||||
extern int tlcp_client_main(int argc, char **argv);
|
||||
extern int tlcp_server_main(int argc, char **argv);
|
||||
extern int tls12_client_main(int argc, char **argv);
|
||||
extern int tls12_server_main(int argc, char **argv);
|
||||
extern int tls13_client_main(int argc, char **argv);
|
||||
extern int tls13_server_main(int argc, char **argv);
|
||||
extern int sdfutil_main(int argc, char **argv);
|
||||
extern int skfutil_main(int argc, char **argv);
|
||||
|
||||
|
||||
static const char *options =
|
||||
"command [options]\n"
|
||||
"\n"
|
||||
"Commands:\n"
|
||||
" help Print this help message\n"
|
||||
" version Print version\n"
|
||||
" rand Generate random bytes\n"
|
||||
" sm2keygen Generate SM2 keypair\n"
|
||||
" sm2sign Generate SM2 signature\n"
|
||||
" sm2verify Verify SM2 signature\n"
|
||||
" sm2encrypt Encrypt with SM2 public key\n"
|
||||
" sm2decrypt Decrypt with SM2 private key\n"
|
||||
" sm3 Generate SM3 hash\n"
|
||||
" sm3hmac Generate SM3 HMAC tag\n"
|
||||
" sm4 Encrypt or decrypt with SM4\n"
|
||||
" zuc Encrypt or decrypt with ZUC\n"
|
||||
" sm9setup Generate SM9 master secret\n"
|
||||
" sm9keygen Generate SM9 private key\n"
|
||||
" sm9sign Generate SM9 signature\n"
|
||||
" sm9verify Verify SM9 signature\n"
|
||||
" sm9encrypt SM9 public key encryption\n"
|
||||
" sm9decrypt SM9 decryption\n"
|
||||
" pbkdf2 Generate key from password\n"
|
||||
" reqgen Generate certificate signing request (CSR)\n"
|
||||
" reqsign Generate certificate from CSR\n"
|
||||
" reqparse Parse and print a CSR\n"
|
||||
" crlparse Verify a CRL with certificate\n"
|
||||
" crlverify Parse and print CRL\n"
|
||||
" certgen Generate a self-signed certificate\n"
|
||||
" certparse Parse and print certificates\n"
|
||||
" certverify Verify certificate chain\n"
|
||||
" cmsparse Parse cryptographic message syntax (CMS)\n"
|
||||
" cmsencrypt Generate CMS EnvelopedData\n"
|
||||
" cmsdecrypt Decrypt CMS EnvelopedData\n"
|
||||
" cmssign Generate CMS SignedData\n"
|
||||
" cmsverify Verify CMS SignedData\n"
|
||||
" sdfutil SDF crypto device utility\n"
|
||||
" skfutil SKF crypto device utility\n"
|
||||
" tlcp_client TLCP client\n"
|
||||
" tlcp_server TLCP server\n"
|
||||
" tls12_client TLS 1.2 client\n"
|
||||
" tls12_server TLS 1.2 server\n"
|
||||
" tls13_client TLS 1.3 client\n"
|
||||
" tls13_server TLS 1.3 server\n";
|
||||
|
||||
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
int ret = 1;
|
||||
char *prog = argv[0];
|
||||
|
||||
argc--;
|
||||
argv++;
|
||||
|
||||
if (argc < 1) {
|
||||
printf("Usage: %s %s\n", prog, options);
|
||||
return 1;
|
||||
}
|
||||
|
||||
while (argc > 0) {
|
||||
if (!strcmp(*argv, "help")) {
|
||||
printf("usage: %s %s\n", prog, options);
|
||||
return 0;
|
||||
} else if (!strcmp(*argv, "version")) {
|
||||
return version_main(argc, argv);
|
||||
} else if (!strcmp(*argv, "rand")) {
|
||||
return rand_main(argc, argv);
|
||||
} else if (!strcmp(*argv, "certgen")) {
|
||||
return certgen_main(argc, argv);
|
||||
} else if (!strcmp(*argv, "certparse")) {
|
||||
return certparse_main(argc, argv);
|
||||
} else if (!strcmp(*argv, "certverify")) {
|
||||
return certverify_main(argc, argv);
|
||||
} else if (!strcmp(*argv, "crlparse")) {
|
||||
return crlparse_main(argc, argv);
|
||||
} else if (!strcmp(*argv, "crlverify")) {
|
||||
return crlverify_main(argc, argv);
|
||||
} else if (!strcmp(*argv, "reqgen")) {
|
||||
return reqgen_main(argc, argv);
|
||||
} else if (!strcmp(*argv, "reqparse")) {
|
||||
return reqparse_main(argc, argv);
|
||||
} else if (!strcmp(*argv, "reqsign")) {
|
||||
return reqsign_main(argc, argv);
|
||||
} else if (!strcmp(*argv, "pbkdf2")) {
|
||||
return pbkdf2_main(argc, argv);
|
||||
} else if (!strcmp(*argv, "sm2keygen")) {
|
||||
return sm2keygen_main(argc, argv);
|
||||
} else if (!strcmp(*argv, "sm2sign")) {
|
||||
return sm2sign_main(argc, argv);
|
||||
} else if (!strcmp(*argv, "sm2verify")) {
|
||||
return sm2verify_main(argc, argv);
|
||||
} else if (!strcmp(*argv, "sm2encrypt")) {
|
||||
return sm2encrypt_main(argc, argv);
|
||||
} else if (!strcmp(*argv, "sm2decrypt")) {
|
||||
return sm2decrypt_main(argc, argv);
|
||||
} else if (!strcmp(*argv, "sm3")) {
|
||||
return sm3_main(argc, argv);
|
||||
} else if (!strcmp(*argv, "sm3hmac")) {
|
||||
return sm3hmac_main(argc, argv);
|
||||
} else if (!strcmp(*argv, "sm4")) {
|
||||
return sm4_main(argc, argv);
|
||||
} else if (!strcmp(*argv, "zuc")) {
|
||||
return zuc_main(argc, argv);
|
||||
} else if (!strcmp(*argv, "sm9setup")) {
|
||||
return sm9setup_main(argc, argv);
|
||||
} else if (!strcmp(*argv, "sm9keygen")) {
|
||||
return sm9keygen_main(argc, argv);
|
||||
} else if (!strcmp(*argv, "sm9sign")) {
|
||||
return sm9sign_main(argc, argv);
|
||||
} else if (!strcmp(*argv, "sm9verify")) {
|
||||
return sm9verify_main(argc, argv);
|
||||
} else if (!strcmp(*argv, "sm9encrypt")) {
|
||||
return sm9encrypt_main(argc, argv);
|
||||
} else if (!strcmp(*argv, "sm9decrypt")) {
|
||||
return sm9decrypt_main(argc, argv);
|
||||
} else if (!strcmp(*argv, "cmsparse")) {
|
||||
return cmsparse_main(argc, argv);
|
||||
} else if (!strcmp(*argv, "cmsencrypt")) {
|
||||
return cmsencrypt_main(argc, argv);
|
||||
} else if (!strcmp(*argv, "cmsdecrypt")) {
|
||||
return cmsdecrypt_main(argc, argv);
|
||||
} else if (!strcmp(*argv, "cmssign")) {
|
||||
return cmssign_main(argc, argv);
|
||||
} else if (!strcmp(*argv, "cmsverify")) {
|
||||
return cmsverify_main(argc, argv);
|
||||
} else if (!strcmp(*argv, "tlcp_client")) {
|
||||
return tlcp_client_main(argc, argv);
|
||||
} else if (!strcmp(*argv, "tlcp_server")) {
|
||||
return tlcp_server_main(argc, argv);
|
||||
} else if (!strcmp(*argv, "tls12_client")) {
|
||||
return tls12_client_main(argc, argv);
|
||||
} else if (!strcmp(*argv, "tls12_server")) {
|
||||
return tls12_server_main(argc, argv);
|
||||
} else if (!strcmp(*argv, "tls13_client")) {
|
||||
return tls13_client_main(argc, argv);
|
||||
} else if (!strcmp(*argv, "tls13_server")) {
|
||||
return tls13_server_main(argc, argv);
|
||||
} else if (!strcmp(*argv, "sdfutil")) {
|
||||
return sdfutil_main(argc, argv);
|
||||
} else if (!strcmp(*argv, "skfutil")) {
|
||||
return skfutil_main(argc, argv);
|
||||
} else {
|
||||
fprintf(stderr, "%s: illegal option '%s'\n", prog, *argv);
|
||||
fprintf(stderr, "usage: %s %s\n", prog, options);
|
||||
return 1;
|
||||
}
|
||||
|
||||
argc--;
|
||||
argv++;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
|
||||
extern int version_main(int argc, char **argv);
|
||||
extern int rand_main(int argc, char **argv);
|
||||
extern int certgen_main(int argc, char **argv);
|
||||
extern int certparse_main(int argc, char **argv);
|
||||
extern int certverify_main(int argc, char **argv);
|
||||
extern int crlparse_main(int argc, char **argv);
|
||||
extern int crlverify_main(int argc, char **argv);
|
||||
extern int pbkdf2_main(int argc, char **argv);
|
||||
extern int reqgen_main(int argc, char **argv);
|
||||
extern int reqparse_main(int argc, char **argv);
|
||||
extern int reqsign_main(int argc, char **argv);
|
||||
extern int sm2keygen_main(int argc, char **argv);
|
||||
extern int sm2sign_main(int argc, char **argv);
|
||||
extern int sm2verify_main(int argc, char **argv);
|
||||
extern int sm2encrypt_main(int argc, char **argv);
|
||||
extern int sm2decrypt_main(int argc, char **argv);
|
||||
extern int sm3_main(int argc, char **argv);
|
||||
extern int sm3hmac_main(int argc, char **argv);
|
||||
extern int sm4_main(int argc, char **argv);
|
||||
extern int zuc_main(int argc, char **argv);
|
||||
extern int sm9setup_main(int argc, char **argv);
|
||||
extern int sm9keygen_main(int argc, char **argv);
|
||||
extern int sm9sign_main(int argc, char **argv);
|
||||
extern int sm9verify_main(int argc, char **argv);
|
||||
extern int sm9encrypt_main(int argc, char **argv);
|
||||
extern int sm9decrypt_main(int argc, char **argv);
|
||||
extern int cmsparse_main(int argc, char **argv);
|
||||
extern int cmsencrypt_main(int argc, char **argv);
|
||||
extern int cmsdecrypt_main(int argc, char **argv);
|
||||
extern int cmssign_main(int argc, char **argv);
|
||||
extern int cmsverify_main(int argc, char **argv);
|
||||
extern int tlcp_client_main(int argc, char **argv);
|
||||
extern int tlcp_server_main(int argc, char **argv);
|
||||
extern int tls12_client_main(int argc, char **argv);
|
||||
extern int tls12_server_main(int argc, char **argv);
|
||||
extern int tls13_client_main(int argc, char **argv);
|
||||
extern int tls13_server_main(int argc, char **argv);
|
||||
extern int sdfutil_main(int argc, char **argv);
|
||||
extern int skfutil_main(int argc, char **argv);
|
||||
|
||||
|
||||
static const char *options =
|
||||
"command [options]\n"
|
||||
"\n"
|
||||
"Commands:\n"
|
||||
" help Print this help message\n"
|
||||
" version Print version\n"
|
||||
" rand Generate random bytes\n"
|
||||
" sm2keygen Generate SM2 keypair\n"
|
||||
" sm2sign Generate SM2 signature\n"
|
||||
" sm2verify Verify SM2 signature\n"
|
||||
" sm2encrypt Encrypt with SM2 public key\n"
|
||||
" sm2decrypt Decrypt with SM2 private key\n"
|
||||
" sm3 Generate SM3 hash\n"
|
||||
" sm3hmac Generate SM3 HMAC tag\n"
|
||||
" sm4 Encrypt or decrypt with SM4\n"
|
||||
" zuc Encrypt or decrypt with ZUC\n"
|
||||
" sm9setup Generate SM9 master secret\n"
|
||||
" sm9keygen Generate SM9 private key\n"
|
||||
" sm9sign Generate SM9 signature\n"
|
||||
" sm9verify Verify SM9 signature\n"
|
||||
" sm9encrypt SM9 public key encryption\n"
|
||||
" sm9decrypt SM9 decryption\n"
|
||||
" pbkdf2 Generate key from password\n"
|
||||
" reqgen Generate certificate signing request (CSR)\n"
|
||||
" reqsign Generate certificate from CSR\n"
|
||||
" reqparse Parse and print a CSR\n"
|
||||
" crlparse Verify a CRL with certificate\n"
|
||||
" crlverify Parse and print CRL\n"
|
||||
" certgen Generate a self-signed certificate\n"
|
||||
" certparse Parse and print certificates\n"
|
||||
" certverify Verify certificate chain\n"
|
||||
" cmsparse Parse cryptographic message syntax (CMS)\n"
|
||||
" cmsencrypt Generate CMS EnvelopedData\n"
|
||||
" cmsdecrypt Decrypt CMS EnvelopedData\n"
|
||||
" cmssign Generate CMS SignedData\n"
|
||||
" cmsverify Verify CMS SignedData\n"
|
||||
" sdfutil SDF crypto device utility\n"
|
||||
" skfutil SKF crypto device utility\n"
|
||||
" tlcp_client TLCP client\n"
|
||||
" tlcp_server TLCP server\n"
|
||||
" tls12_client TLS 1.2 client\n"
|
||||
" tls12_server TLS 1.2 server\n"
|
||||
" tls13_client TLS 1.3 client\n"
|
||||
" tls13_server TLS 1.3 server\n";
|
||||
|
||||
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
int ret = 1;
|
||||
char *prog = argv[0];
|
||||
|
||||
argc--;
|
||||
argv++;
|
||||
|
||||
if (argc < 1) {
|
||||
printf("Usage: %s %s\n", prog, options);
|
||||
return 1;
|
||||
}
|
||||
|
||||
while (argc > 0) {
|
||||
if (!strcmp(*argv, "help")) {
|
||||
printf("usage: %s %s\n", prog, options);
|
||||
return 0;
|
||||
} else if (!strcmp(*argv, "version")) {
|
||||
return version_main(argc, argv);
|
||||
} else if (!strcmp(*argv, "rand")) {
|
||||
return rand_main(argc, argv);
|
||||
} else if (!strcmp(*argv, "certgen")) {
|
||||
return certgen_main(argc, argv);
|
||||
} else if (!strcmp(*argv, "certparse")) {
|
||||
return certparse_main(argc, argv);
|
||||
} else if (!strcmp(*argv, "certverify")) {
|
||||
return certverify_main(argc, argv);
|
||||
} else if (!strcmp(*argv, "crlparse")) {
|
||||
return crlparse_main(argc, argv);
|
||||
} else if (!strcmp(*argv, "crlverify")) {
|
||||
return crlverify_main(argc, argv);
|
||||
} else if (!strcmp(*argv, "reqgen")) {
|
||||
return reqgen_main(argc, argv);
|
||||
} else if (!strcmp(*argv, "reqparse")) {
|
||||
return reqparse_main(argc, argv);
|
||||
} else if (!strcmp(*argv, "reqsign")) {
|
||||
return reqsign_main(argc, argv);
|
||||
} else if (!strcmp(*argv, "pbkdf2")) {
|
||||
return pbkdf2_main(argc, argv);
|
||||
} else if (!strcmp(*argv, "sm2keygen")) {
|
||||
return sm2keygen_main(argc, argv);
|
||||
} else if (!strcmp(*argv, "sm2sign")) {
|
||||
return sm2sign_main(argc, argv);
|
||||
} else if (!strcmp(*argv, "sm2verify")) {
|
||||
return sm2verify_main(argc, argv);
|
||||
} else if (!strcmp(*argv, "sm2encrypt")) {
|
||||
return sm2encrypt_main(argc, argv);
|
||||
} else if (!strcmp(*argv, "sm2decrypt")) {
|
||||
return sm2decrypt_main(argc, argv);
|
||||
} else if (!strcmp(*argv, "sm3")) {
|
||||
return sm3_main(argc, argv);
|
||||
} else if (!strcmp(*argv, "sm3hmac")) {
|
||||
return sm3hmac_main(argc, argv);
|
||||
} else if (!strcmp(*argv, "sm4")) {
|
||||
return sm4_main(argc, argv);
|
||||
} else if (!strcmp(*argv, "zuc")) {
|
||||
return zuc_main(argc, argv);
|
||||
} else if (!strcmp(*argv, "sm9setup")) {
|
||||
return sm9setup_main(argc, argv);
|
||||
} else if (!strcmp(*argv, "sm9keygen")) {
|
||||
return sm9keygen_main(argc, argv);
|
||||
} else if (!strcmp(*argv, "sm9sign")) {
|
||||
return sm9sign_main(argc, argv);
|
||||
} else if (!strcmp(*argv, "sm9verify")) {
|
||||
return sm9verify_main(argc, argv);
|
||||
} else if (!strcmp(*argv, "sm9encrypt")) {
|
||||
return sm9encrypt_main(argc, argv);
|
||||
} else if (!strcmp(*argv, "sm9decrypt")) {
|
||||
return sm9decrypt_main(argc, argv);
|
||||
} else if (!strcmp(*argv, "cmsparse")) {
|
||||
return cmsparse_main(argc, argv);
|
||||
} else if (!strcmp(*argv, "cmsencrypt")) {
|
||||
return cmsencrypt_main(argc, argv);
|
||||
} else if (!strcmp(*argv, "cmsdecrypt")) {
|
||||
return cmsdecrypt_main(argc, argv);
|
||||
} else if (!strcmp(*argv, "cmssign")) {
|
||||
return cmssign_main(argc, argv);
|
||||
} else if (!strcmp(*argv, "cmsverify")) {
|
||||
return cmsverify_main(argc, argv);
|
||||
} else if (!strcmp(*argv, "tlcp_client")) {
|
||||
return tlcp_client_main(argc, argv);
|
||||
} else if (!strcmp(*argv, "tlcp_server")) {
|
||||
return tlcp_server_main(argc, argv);
|
||||
} else if (!strcmp(*argv, "tls12_client")) {
|
||||
return tls12_client_main(argc, argv);
|
||||
} else if (!strcmp(*argv, "tls12_server")) {
|
||||
return tls12_server_main(argc, argv);
|
||||
} else if (!strcmp(*argv, "tls13_client")) {
|
||||
return tls13_client_main(argc, argv);
|
||||
} else if (!strcmp(*argv, "tls13_server")) {
|
||||
return tls13_server_main(argc, argv);
|
||||
} else if (!strcmp(*argv, "sdfutil")) {
|
||||
return sdfutil_main(argc, argv);
|
||||
} else if (!strcmp(*argv, "skfutil")) {
|
||||
return skfutil_main(argc, argv);
|
||||
} else {
|
||||
fprintf(stderr, "%s: illegal option '%s'\n", prog, *argv);
|
||||
fprintf(stderr, "usage: %s %s\n", prog, options);
|
||||
return 1;
|
||||
}
|
||||
|
||||
argc--;
|
||||
argv++;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
269
tools/pbkdf2.c
269
tools/pbkdf2.c
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2014-2022 The GmSSL Project. All Rights Reserved.
|
||||
* Copyright 2022 The GmSSL Project. All Rights Reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the License); you may
|
||||
* not use this file except in compliance with the License.
|
||||
@@ -7,137 +7,136 @@
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*/
|
||||
|
||||
|
||||
|
||||
#include <stdio.h>
|
||||
#include <errno.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <gmssl/mem.h>
|
||||
#include <gmssl/hex.h>
|
||||
#include <gmssl/pbkdf2.h>
|
||||
|
||||
|
||||
static const char *options = "-pass str -salt hex -iter num -outlen num [-bin|-hex] [-out file]";
|
||||
|
||||
int pbkdf2_main(int argc, char **argv)
|
||||
{
|
||||
int ret = 1;
|
||||
char *prog = argv[0];
|
||||
char *pass = NULL;
|
||||
char *salthex = NULL;
|
||||
uint8_t salt[PBKDF2_MAX_SALT_SIZE];
|
||||
size_t saltlen;
|
||||
int iter = 0;
|
||||
int outlen = 0;
|
||||
int bin = 0;
|
||||
char *outfile = NULL;
|
||||
uint8_t outbuf[64];
|
||||
FILE *outfp = stdout;
|
||||
int i;
|
||||
|
||||
argc--;
|
||||
argv++;
|
||||
|
||||
if (argc < 1) {
|
||||
fprintf(stderr, "usage: %s %s\n", prog, options);
|
||||
return 1;
|
||||
}
|
||||
|
||||
while (argc > 0) {
|
||||
if (!strcmp(*argv, "-help")) {
|
||||
printf("usage: %s %s\n", prog, options);
|
||||
ret = 0;
|
||||
goto end;
|
||||
} else if (!strcmp(*argv, "-pass")) {
|
||||
if (--argc < 1) goto bad;
|
||||
pass = *(++argv);
|
||||
} else if (!strcmp(*argv, "-salt")) {
|
||||
if (--argc < 1) goto bad;
|
||||
salthex = *(++argv);
|
||||
if (strlen(salthex) > sizeof(salt) * 2) {
|
||||
fprintf(stderr, "%s: invalid salt length\n", prog);
|
||||
goto end;
|
||||
}
|
||||
if (hex_to_bytes(salthex, strlen(salthex), salt, &saltlen) != 1) {
|
||||
fprintf(stderr, "%s: invalid HEX digits\n", prog);
|
||||
goto end;
|
||||
}
|
||||
} else if (!strcmp(*argv, "-iter")) {
|
||||
if (--argc < 1) goto bad;
|
||||
iter = atoi(*(++argv));
|
||||
if (iter < PBKDF2_MIN_ITER || iter > INT_MAX) {
|
||||
fprintf(stderr, "%s: invalid '-iter' value\n", prog);
|
||||
goto end;
|
||||
}
|
||||
} else if (!strcmp(*argv, "-outlen")) {
|
||||
if (--argc < 1) goto bad;
|
||||
outlen = atoi(*(++argv));
|
||||
if (outlen < 1 || outlen > sizeof(outbuf)) {
|
||||
fprintf(stderr, "%s: invalid outlen\n", prog);
|
||||
goto end;
|
||||
}
|
||||
} else if (!strcmp(*argv, "-hex")) {
|
||||
bin = 0;
|
||||
} else if (!strcmp(*argv, "-bin")) {
|
||||
bin = 1;
|
||||
} else if (!strcmp(*argv, "-out")) {
|
||||
if (--argc < 1) goto bad;
|
||||
outfile = *(++argv);
|
||||
if (!(outfp = fopen(outfile, "w"))) {
|
||||
fprintf(stderr, "%s: open '%s' failure : %s\n", prog, outfile, strerror(errno));
|
||||
goto end;
|
||||
}
|
||||
} else {
|
||||
fprintf(stderr, "%s: illegal option '%s'\n", prog, *argv);
|
||||
goto end;
|
||||
bad:
|
||||
fprintf(stderr, "%s: '%s' option value missing\n", prog, *argv);
|
||||
goto end;
|
||||
}
|
||||
|
||||
argc--;
|
||||
argv++;
|
||||
}
|
||||
|
||||
if (!pass) {
|
||||
fprintf(stderr, "%s: option '-pass' required\n", prog);
|
||||
goto end;
|
||||
}
|
||||
if (!salthex) {
|
||||
fprintf(stderr, "%s: option '-salt' required\n", prog);
|
||||
goto end;
|
||||
}
|
||||
if (!iter) {
|
||||
fprintf(stderr, "%s: option '-iter' required\n", prog);
|
||||
goto end;
|
||||
}
|
||||
if (!outlen) {
|
||||
fprintf(stderr, "%s: option '-outlen' required\n", prog);
|
||||
goto end;
|
||||
}
|
||||
|
||||
if (pbkdf2_hmac_sm3_genkey(pass, strlen(pass), salt, saltlen, iter, outlen, outbuf) != 1) {
|
||||
fprintf(stderr, "%s: inner error\n", prog);
|
||||
goto end;
|
||||
}
|
||||
|
||||
if (bin) {
|
||||
if (fwrite(outbuf, 1, outlen, outfp) != outlen) {
|
||||
fprintf(stderr, "%s: output failure : %s\n", prog, strerror(errno));
|
||||
goto end;
|
||||
}
|
||||
} else {
|
||||
for (i = 0; i < outlen; i++) {
|
||||
fprintf(outfp, "%02x", outbuf[i]);
|
||||
}
|
||||
fprintf(outfp, "\n");
|
||||
}
|
||||
ret = 0;
|
||||
|
||||
end:
|
||||
gmssl_secure_clear(outbuf, sizeof(outbuf));
|
||||
gmssl_secure_clear(salt, sizeof(salt));
|
||||
if (outfile && outfp) fclose(outfp);
|
||||
return ret;
|
||||
}
|
||||
|
||||
#include <stdio.h>
|
||||
#include <errno.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <gmssl/mem.h>
|
||||
#include <gmssl/hex.h>
|
||||
#include <gmssl/pbkdf2.h>
|
||||
|
||||
|
||||
static const char *options = "-pass str -salt hex -iter num -outlen num [-bin|-hex] [-out file]";
|
||||
|
||||
int pbkdf2_main(int argc, char **argv)
|
||||
{
|
||||
int ret = 1;
|
||||
char *prog = argv[0];
|
||||
char *pass = NULL;
|
||||
char *salthex = NULL;
|
||||
uint8_t salt[PBKDF2_MAX_SALT_SIZE];
|
||||
size_t saltlen;
|
||||
int iter = 0;
|
||||
int outlen = 0;
|
||||
int bin = 0;
|
||||
char *outfile = NULL;
|
||||
uint8_t outbuf[64];
|
||||
FILE *outfp = stdout;
|
||||
int i;
|
||||
|
||||
argc--;
|
||||
argv++;
|
||||
|
||||
if (argc < 1) {
|
||||
fprintf(stderr, "usage: %s %s\n", prog, options);
|
||||
return 1;
|
||||
}
|
||||
|
||||
while (argc > 0) {
|
||||
if (!strcmp(*argv, "-help")) {
|
||||
printf("usage: %s %s\n", prog, options);
|
||||
ret = 0;
|
||||
goto end;
|
||||
} else if (!strcmp(*argv, "-pass")) {
|
||||
if (--argc < 1) goto bad;
|
||||
pass = *(++argv);
|
||||
} else if (!strcmp(*argv, "-salt")) {
|
||||
if (--argc < 1) goto bad;
|
||||
salthex = *(++argv);
|
||||
if (strlen(salthex) > sizeof(salt) * 2) {
|
||||
fprintf(stderr, "%s: invalid salt length\n", prog);
|
||||
goto end;
|
||||
}
|
||||
if (hex_to_bytes(salthex, strlen(salthex), salt, &saltlen) != 1) {
|
||||
fprintf(stderr, "%s: invalid HEX digits\n", prog);
|
||||
goto end;
|
||||
}
|
||||
} else if (!strcmp(*argv, "-iter")) {
|
||||
if (--argc < 1) goto bad;
|
||||
iter = atoi(*(++argv));
|
||||
if (iter < PBKDF2_MIN_ITER || iter > INT_MAX) {
|
||||
fprintf(stderr, "%s: invalid '-iter' value\n", prog);
|
||||
goto end;
|
||||
}
|
||||
} else if (!strcmp(*argv, "-outlen")) {
|
||||
if (--argc < 1) goto bad;
|
||||
outlen = atoi(*(++argv));
|
||||
if (outlen < 1 || outlen > sizeof(outbuf)) {
|
||||
fprintf(stderr, "%s: invalid outlen\n", prog);
|
||||
goto end;
|
||||
}
|
||||
} else if (!strcmp(*argv, "-hex")) {
|
||||
bin = 0;
|
||||
} else if (!strcmp(*argv, "-bin")) {
|
||||
bin = 1;
|
||||
} else if (!strcmp(*argv, "-out")) {
|
||||
if (--argc < 1) goto bad;
|
||||
outfile = *(++argv);
|
||||
if (!(outfp = fopen(outfile, "w"))) {
|
||||
fprintf(stderr, "%s: open '%s' failure : %s\n", prog, outfile, strerror(errno));
|
||||
goto end;
|
||||
}
|
||||
} else {
|
||||
fprintf(stderr, "%s: illegal option '%s'\n", prog, *argv);
|
||||
goto end;
|
||||
bad:
|
||||
fprintf(stderr, "%s: '%s' option value missing\n", prog, *argv);
|
||||
goto end;
|
||||
}
|
||||
|
||||
argc--;
|
||||
argv++;
|
||||
}
|
||||
|
||||
if (!pass) {
|
||||
fprintf(stderr, "%s: option '-pass' required\n", prog);
|
||||
goto end;
|
||||
}
|
||||
if (!salthex) {
|
||||
fprintf(stderr, "%s: option '-salt' required\n", prog);
|
||||
goto end;
|
||||
}
|
||||
if (!iter) {
|
||||
fprintf(stderr, "%s: option '-iter' required\n", prog);
|
||||
goto end;
|
||||
}
|
||||
if (!outlen) {
|
||||
fprintf(stderr, "%s: option '-outlen' required\n", prog);
|
||||
goto end;
|
||||
}
|
||||
|
||||
if (pbkdf2_hmac_sm3_genkey(pass, strlen(pass), salt, saltlen, iter, outlen, outbuf) != 1) {
|
||||
fprintf(stderr, "%s: inner error\n", prog);
|
||||
goto end;
|
||||
}
|
||||
|
||||
if (bin) {
|
||||
if (fwrite(outbuf, 1, outlen, outfp) != outlen) {
|
||||
fprintf(stderr, "%s: output failure : %s\n", prog, strerror(errno));
|
||||
goto end;
|
||||
}
|
||||
} else {
|
||||
for (i = 0; i < outlen; i++) {
|
||||
fprintf(outfp, "%02x", outbuf[i]);
|
||||
}
|
||||
fprintf(outfp, "\n");
|
||||
}
|
||||
ret = 0;
|
||||
|
||||
end:
|
||||
gmssl_secure_clear(outbuf, sizeof(outbuf));
|
||||
gmssl_secure_clear(salt, sizeof(salt));
|
||||
if (outfile && outfp) fclose(outfp);
|
||||
return ret;
|
||||
}
|
||||
|
||||
225
tools/rand.c
225
tools/rand.c
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2014-2022 The GmSSL Project. All Rights Reserved.
|
||||
* Copyright 2022 The GmSSL Project. All Rights Reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the License); you may
|
||||
* not use this file except in compliance with the License.
|
||||
@@ -7,115 +7,114 @@
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*/
|
||||
|
||||
|
||||
|
||||
#include <stdio.h>
|
||||
#include <errno.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <limits.h>
|
||||
#include <gmssl/mem.h>
|
||||
#include <gmssl/rand.h>
|
||||
|
||||
|
||||
static const char *options = "[-hex] [-rdrand] -outlen num [-out file]";
|
||||
|
||||
int rand_main(int argc, char **argv)
|
||||
{
|
||||
int ret = 1;
|
||||
char *prog = argv[0];
|
||||
int hex = 0;
|
||||
int rdrand = 0;
|
||||
int outlen = 0;
|
||||
char *outfile = NULL;
|
||||
FILE *outfp = stdout;
|
||||
uint8_t buf[2048];
|
||||
int i;
|
||||
|
||||
argc--;
|
||||
argv++;
|
||||
|
||||
if (argc < 1) {
|
||||
fprintf(stderr, "usage: %s %s\n", prog, options);
|
||||
return 1;
|
||||
}
|
||||
|
||||
while (argc > 0) {
|
||||
if (!strcmp(*argv, "-help")) {
|
||||
printf("usage: %s %s\n", prog, options);
|
||||
ret = 0;
|
||||
goto end;
|
||||
} else if (!strcmp(*argv, "-hex")) {
|
||||
hex = 1;
|
||||
} else if (!strcmp(*argv, "-rdrand")) {
|
||||
rdrand = 1;
|
||||
} else if (!strcmp(*argv, "-outlen")) {
|
||||
if (--argc < 1) goto bad;
|
||||
outlen = atoi(*(++argv));
|
||||
if (outlen < 1 || outlen > INT_MAX) {
|
||||
fprintf(stderr, "%s: invalid outlen\n", prog);
|
||||
goto end;
|
||||
}
|
||||
} else if (!strcmp(*argv, "-out")) {
|
||||
if (--argc < 1) goto bad;
|
||||
outfile = *(++argv);
|
||||
if (!(outfp = fopen(outfile, "w"))) {
|
||||
fprintf(stderr, "%s: open '%s' failure : %s\n", prog, outfile, strerror(errno));
|
||||
goto end;
|
||||
}
|
||||
} else {
|
||||
fprintf(stderr, "%s: illegal option '%s'\n", prog, *argv);
|
||||
goto end;
|
||||
bad:
|
||||
fprintf(stderr, "%s: '%s' option value missing\n", prog, *argv);
|
||||
goto end;
|
||||
}
|
||||
|
||||
argc--;
|
||||
argv++;
|
||||
}
|
||||
|
||||
if (!outlen) {
|
||||
fprintf(stderr, "%s: option -outlen missing\n", prog);
|
||||
goto end;
|
||||
}
|
||||
|
||||
while (outlen) {
|
||||
size_t len = outlen < sizeof(buf) ? outlen : sizeof(buf);
|
||||
|
||||
if (rdrand) {
|
||||
/*
|
||||
if (rdrand_bytes(buf, len) != 1) {
|
||||
fprintf(stderr, "%s: inner error\n", prog);
|
||||
goto end;
|
||||
}
|
||||
*/
|
||||
} else {
|
||||
if (rand_bytes(buf, len) != 1) {
|
||||
fprintf(stderr, "%s: inner error\n", prog);
|
||||
goto end;
|
||||
}
|
||||
}
|
||||
|
||||
if (hex) {
|
||||
int i;
|
||||
for (i = 0; i < len; i++) {
|
||||
fprintf(outfp, "%02X", buf[i]);
|
||||
}
|
||||
} else {
|
||||
if (fwrite(buf, 1, len, outfp) != len) {
|
||||
fprintf(stderr, "%s: output failure : %s\n", prog, strerror(errno));
|
||||
goto end;
|
||||
}
|
||||
}
|
||||
outlen -= len;
|
||||
}
|
||||
if (hex) {
|
||||
fprintf(outfp, "\n");
|
||||
}
|
||||
ret = 0;
|
||||
end:
|
||||
gmssl_secure_clear(buf, sizeof(buf));
|
||||
if (outfile && outfp) fclose(outfp);
|
||||
return ret;
|
||||
}
|
||||
|
||||
#include <stdio.h>
|
||||
#include <errno.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <limits.h>
|
||||
#include <gmssl/mem.h>
|
||||
#include <gmssl/rand.h>
|
||||
|
||||
|
||||
static const char *options = "[-hex] [-rdrand] -outlen num [-out file]";
|
||||
|
||||
int rand_main(int argc, char **argv)
|
||||
{
|
||||
int ret = 1;
|
||||
char *prog = argv[0];
|
||||
int hex = 0;
|
||||
int rdrand = 0;
|
||||
int outlen = 0;
|
||||
char *outfile = NULL;
|
||||
FILE *outfp = stdout;
|
||||
uint8_t buf[2048];
|
||||
int i;
|
||||
|
||||
argc--;
|
||||
argv++;
|
||||
|
||||
if (argc < 1) {
|
||||
fprintf(stderr, "usage: %s %s\n", prog, options);
|
||||
return 1;
|
||||
}
|
||||
|
||||
while (argc > 0) {
|
||||
if (!strcmp(*argv, "-help")) {
|
||||
printf("usage: %s %s\n", prog, options);
|
||||
ret = 0;
|
||||
goto end;
|
||||
} else if (!strcmp(*argv, "-hex")) {
|
||||
hex = 1;
|
||||
} else if (!strcmp(*argv, "-rdrand")) {
|
||||
rdrand = 1;
|
||||
} else if (!strcmp(*argv, "-outlen")) {
|
||||
if (--argc < 1) goto bad;
|
||||
outlen = atoi(*(++argv));
|
||||
if (outlen < 1 || outlen > INT_MAX) {
|
||||
fprintf(stderr, "%s: invalid outlen\n", prog);
|
||||
goto end;
|
||||
}
|
||||
} else if (!strcmp(*argv, "-out")) {
|
||||
if (--argc < 1) goto bad;
|
||||
outfile = *(++argv);
|
||||
if (!(outfp = fopen(outfile, "w"))) {
|
||||
fprintf(stderr, "%s: open '%s' failure : %s\n", prog, outfile, strerror(errno));
|
||||
goto end;
|
||||
}
|
||||
} else {
|
||||
fprintf(stderr, "%s: illegal option '%s'\n", prog, *argv);
|
||||
goto end;
|
||||
bad:
|
||||
fprintf(stderr, "%s: '%s' option value missing\n", prog, *argv);
|
||||
goto end;
|
||||
}
|
||||
|
||||
argc--;
|
||||
argv++;
|
||||
}
|
||||
|
||||
if (!outlen) {
|
||||
fprintf(stderr, "%s: option -outlen missing\n", prog);
|
||||
goto end;
|
||||
}
|
||||
|
||||
while (outlen) {
|
||||
size_t len = outlen < sizeof(buf) ? outlen : sizeof(buf);
|
||||
|
||||
if (rdrand) {
|
||||
/*
|
||||
if (rdrand_bytes(buf, len) != 1) {
|
||||
fprintf(stderr, "%s: inner error\n", prog);
|
||||
goto end;
|
||||
}
|
||||
*/
|
||||
} else {
|
||||
if (rand_bytes(buf, len) != 1) {
|
||||
fprintf(stderr, "%s: inner error\n", prog);
|
||||
goto end;
|
||||
}
|
||||
}
|
||||
|
||||
if (hex) {
|
||||
int i;
|
||||
for (i = 0; i < len; i++) {
|
||||
fprintf(outfp, "%02X", buf[i]);
|
||||
}
|
||||
} else {
|
||||
if (fwrite(buf, 1, len, outfp) != len) {
|
||||
fprintf(stderr, "%s: output failure : %s\n", prog, strerror(errno));
|
||||
goto end;
|
||||
}
|
||||
}
|
||||
outlen -= len;
|
||||
}
|
||||
if (hex) {
|
||||
fprintf(outfp, "\n");
|
||||
}
|
||||
ret = 0;
|
||||
end:
|
||||
gmssl_secure_clear(buf, sizeof(buf));
|
||||
if (outfile && outfp) fclose(outfp);
|
||||
return ret;
|
||||
}
|
||||
|
||||
303
tools/reqgen.c
303
tools/reqgen.c
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2014-2022 The GmSSL Project. All Rights Reserved.
|
||||
* Copyright 2022 The GmSSL Project. All Rights Reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the License); you may
|
||||
* not use this file except in compliance with the License.
|
||||
@@ -7,154 +7,153 @@
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*/
|
||||
|
||||
|
||||
|
||||
#include <stdio.h>
|
||||
#include <errno.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <gmssl/mem.h>
|
||||
#include <gmssl/rand.h>
|
||||
#include <gmssl/pkcs8.h>
|
||||
#include <gmssl/x509.h>
|
||||
#include <gmssl/x509_req.h>
|
||||
|
||||
|
||||
static const char *options =
|
||||
"[-C str] [-ST str] [-L str] [-O str] [-OU str] -CN str -days num"
|
||||
" -key file [-pass pass] [-out file]";
|
||||
|
||||
int reqgen_main(int argc, char **argv)
|
||||
{
|
||||
int ret = 1;
|
||||
char *prog = argv[0];
|
||||
char *country = NULL;
|
||||
char *state = NULL;
|
||||
char *locality = NULL;
|
||||
char *org = NULL;
|
||||
char *org_unit = NULL;
|
||||
char *common_name = NULL;
|
||||
int days = 0;
|
||||
char *keyfile = NULL;
|
||||
char *pass = NULL;
|
||||
char *outfile = NULL;
|
||||
uint8_t name[256];
|
||||
size_t namelen = 0;
|
||||
FILE *keyfp = NULL;
|
||||
FILE *outfp = stdout;
|
||||
uint8_t req[1024];
|
||||
size_t reqlen = 0;
|
||||
SM2_KEY sm2_key;
|
||||
|
||||
argc--;
|
||||
argv++;
|
||||
|
||||
if (argc < 1) {
|
||||
fprintf(stderr, "usage: %s %s\n", prog, options);
|
||||
return 1;
|
||||
}
|
||||
|
||||
while (argc > 0) {
|
||||
if (!strcmp(*argv, "-help")) {
|
||||
printf("usage: %s %s\n", prog, options);
|
||||
ret = 0;
|
||||
goto end;
|
||||
} else if (!strcmp(*argv, "-C")) {
|
||||
if (--argc < 1) goto bad;
|
||||
country = *(++argv);
|
||||
} else if (!strcmp(*argv, "-ST")) {
|
||||
if (--argc < 1) goto bad;
|
||||
state = *(++argv);
|
||||
} else if (!strcmp(*argv, "-L")) {
|
||||
if (--argc < 1) goto bad;
|
||||
locality = *(++argv);
|
||||
} else if (!strcmp(*argv, "-O")) {
|
||||
if (--argc < 1) goto bad;
|
||||
org = *(++argv);
|
||||
} else if (!strcmp(*argv, "-OU")) {
|
||||
if (--argc < 1) goto bad;
|
||||
org_unit = *(++argv);
|
||||
} else if (!strcmp(*argv, "-CN")) {
|
||||
if (--argc < 1) goto bad;
|
||||
common_name = *(++argv);
|
||||
} else if (!strcmp(*argv, "-days")) {
|
||||
if (--argc < 1) goto bad;
|
||||
days = atoi(*(++argv));
|
||||
if (days <= 0) {
|
||||
fprintf(stderr, "%s: invalid '-days' value\n", prog);
|
||||
goto end;
|
||||
}
|
||||
} else if (!strcmp(*argv, "-key")) {
|
||||
if (--argc < 1) goto bad;
|
||||
keyfile = *(++argv);
|
||||
if (!(keyfp = fopen(keyfile, "r"))) {
|
||||
fprintf(stderr, "%s: open '%s' failure : %s\n", prog, keyfile, strerror(errno));
|
||||
goto end;
|
||||
}
|
||||
} else if (!strcmp(*argv, "-pass")) {
|
||||
if (--argc < 1) goto bad;
|
||||
pass = *(++argv);
|
||||
} else if (!strcmp(*argv, "-out")) {
|
||||
if (--argc < 1) goto bad;
|
||||
outfile = *(++argv);
|
||||
if (!(outfp = fopen(outfile, "w"))) {
|
||||
fprintf(stderr, "%s: open '%s' failure : %s\n", prog, outfile, strerror(errno));
|
||||
goto end;
|
||||
}
|
||||
} else {
|
||||
fprintf(stderr, "%s: illegal option '%s'\n", prog, *argv);
|
||||
goto end;
|
||||
bad:
|
||||
fprintf(stderr, "%s: '%s' option value missing\n", prog, *argv);
|
||||
goto end;
|
||||
}
|
||||
|
||||
argc--;
|
||||
argv++;
|
||||
}
|
||||
|
||||
if (!common_name) {
|
||||
fprintf(stderr, "%s: '-CN' option required\n", prog);
|
||||
goto end;
|
||||
}
|
||||
if (!days) {
|
||||
fprintf(stderr, "%s: '-days' option required\n", prog);
|
||||
goto end;
|
||||
}
|
||||
if (!keyfile) {
|
||||
fprintf(stderr, "%s: '-key' option required\n", prog);
|
||||
goto end;
|
||||
}
|
||||
if (!pass) {
|
||||
fprintf(stderr, "%s: '-pass' option required\n", prog);
|
||||
goto end;
|
||||
}
|
||||
|
||||
if (sm2_private_key_info_decrypt_from_pem(&sm2_key, pass, keyfp) != 1) {
|
||||
fprintf(stderr, "%s: load private key failed\n", prog);
|
||||
goto end;
|
||||
}
|
||||
|
||||
if (x509_name_set(name, &namelen, sizeof(name),
|
||||
country, state, locality, org, org_unit, common_name) != 1
|
||||
|| x509_req_sign(req, &reqlen, sizeof(req),
|
||||
X509_version_v1,
|
||||
name, namelen,
|
||||
&sm2_key,
|
||||
NULL, 0,
|
||||
OID_sm2sign_with_sm3,
|
||||
&sm2_key, SM2_DEFAULT_ID, strlen(SM2_DEFAULT_ID)) != 1) {
|
||||
fprintf(stderr, "%s: inner error\n", prog);
|
||||
goto end;
|
||||
}
|
||||
if (x509_req_to_pem(req, reqlen, outfp) != 1) {
|
||||
fprintf(stderr, "%s: output CSR failed\n", prog);
|
||||
goto end;
|
||||
}
|
||||
ret = 0;
|
||||
end:
|
||||
gmssl_secure_clear(&sm2_key, sizeof(SM2_KEY));
|
||||
if (keyfp) fclose(keyfp);
|
||||
if (outfile && outfp) fclose(outfp);
|
||||
return ret;
|
||||
}
|
||||
|
||||
#include <stdio.h>
|
||||
#include <errno.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <gmssl/mem.h>
|
||||
#include <gmssl/rand.h>
|
||||
#include <gmssl/pkcs8.h>
|
||||
#include <gmssl/x509.h>
|
||||
#include <gmssl/x509_req.h>
|
||||
|
||||
|
||||
static const char *options =
|
||||
"[-C str] [-ST str] [-L str] [-O str] [-OU str] -CN str -days num"
|
||||
" -key file [-pass pass] [-out file]";
|
||||
|
||||
int reqgen_main(int argc, char **argv)
|
||||
{
|
||||
int ret = 1;
|
||||
char *prog = argv[0];
|
||||
char *country = NULL;
|
||||
char *state = NULL;
|
||||
char *locality = NULL;
|
||||
char *org = NULL;
|
||||
char *org_unit = NULL;
|
||||
char *common_name = NULL;
|
||||
int days = 0;
|
||||
char *keyfile = NULL;
|
||||
char *pass = NULL;
|
||||
char *outfile = NULL;
|
||||
uint8_t name[256];
|
||||
size_t namelen = 0;
|
||||
FILE *keyfp = NULL;
|
||||
FILE *outfp = stdout;
|
||||
uint8_t req[1024];
|
||||
size_t reqlen = 0;
|
||||
SM2_KEY sm2_key;
|
||||
|
||||
argc--;
|
||||
argv++;
|
||||
|
||||
if (argc < 1) {
|
||||
fprintf(stderr, "usage: %s %s\n", prog, options);
|
||||
return 1;
|
||||
}
|
||||
|
||||
while (argc > 0) {
|
||||
if (!strcmp(*argv, "-help")) {
|
||||
printf("usage: %s %s\n", prog, options);
|
||||
ret = 0;
|
||||
goto end;
|
||||
} else if (!strcmp(*argv, "-C")) {
|
||||
if (--argc < 1) goto bad;
|
||||
country = *(++argv);
|
||||
} else if (!strcmp(*argv, "-ST")) {
|
||||
if (--argc < 1) goto bad;
|
||||
state = *(++argv);
|
||||
} else if (!strcmp(*argv, "-L")) {
|
||||
if (--argc < 1) goto bad;
|
||||
locality = *(++argv);
|
||||
} else if (!strcmp(*argv, "-O")) {
|
||||
if (--argc < 1) goto bad;
|
||||
org = *(++argv);
|
||||
} else if (!strcmp(*argv, "-OU")) {
|
||||
if (--argc < 1) goto bad;
|
||||
org_unit = *(++argv);
|
||||
} else if (!strcmp(*argv, "-CN")) {
|
||||
if (--argc < 1) goto bad;
|
||||
common_name = *(++argv);
|
||||
} else if (!strcmp(*argv, "-days")) {
|
||||
if (--argc < 1) goto bad;
|
||||
days = atoi(*(++argv));
|
||||
if (days <= 0) {
|
||||
fprintf(stderr, "%s: invalid '-days' value\n", prog);
|
||||
goto end;
|
||||
}
|
||||
} else if (!strcmp(*argv, "-key")) {
|
||||
if (--argc < 1) goto bad;
|
||||
keyfile = *(++argv);
|
||||
if (!(keyfp = fopen(keyfile, "r"))) {
|
||||
fprintf(stderr, "%s: open '%s' failure : %s\n", prog, keyfile, strerror(errno));
|
||||
goto end;
|
||||
}
|
||||
} else if (!strcmp(*argv, "-pass")) {
|
||||
if (--argc < 1) goto bad;
|
||||
pass = *(++argv);
|
||||
} else if (!strcmp(*argv, "-out")) {
|
||||
if (--argc < 1) goto bad;
|
||||
outfile = *(++argv);
|
||||
if (!(outfp = fopen(outfile, "w"))) {
|
||||
fprintf(stderr, "%s: open '%s' failure : %s\n", prog, outfile, strerror(errno));
|
||||
goto end;
|
||||
}
|
||||
} else {
|
||||
fprintf(stderr, "%s: illegal option '%s'\n", prog, *argv);
|
||||
goto end;
|
||||
bad:
|
||||
fprintf(stderr, "%s: '%s' option value missing\n", prog, *argv);
|
||||
goto end;
|
||||
}
|
||||
|
||||
argc--;
|
||||
argv++;
|
||||
}
|
||||
|
||||
if (!common_name) {
|
||||
fprintf(stderr, "%s: '-CN' option required\n", prog);
|
||||
goto end;
|
||||
}
|
||||
if (!days) {
|
||||
fprintf(stderr, "%s: '-days' option required\n", prog);
|
||||
goto end;
|
||||
}
|
||||
if (!keyfile) {
|
||||
fprintf(stderr, "%s: '-key' option required\n", prog);
|
||||
goto end;
|
||||
}
|
||||
if (!pass) {
|
||||
fprintf(stderr, "%s: '-pass' option required\n", prog);
|
||||
goto end;
|
||||
}
|
||||
|
||||
if (sm2_private_key_info_decrypt_from_pem(&sm2_key, pass, keyfp) != 1) {
|
||||
fprintf(stderr, "%s: load private key failed\n", prog);
|
||||
goto end;
|
||||
}
|
||||
|
||||
if (x509_name_set(name, &namelen, sizeof(name),
|
||||
country, state, locality, org, org_unit, common_name) != 1
|
||||
|| x509_req_sign(req, &reqlen, sizeof(req),
|
||||
X509_version_v1,
|
||||
name, namelen,
|
||||
&sm2_key,
|
||||
NULL, 0,
|
||||
OID_sm2sign_with_sm3,
|
||||
&sm2_key, SM2_DEFAULT_ID, strlen(SM2_DEFAULT_ID)) != 1) {
|
||||
fprintf(stderr, "%s: inner error\n", prog);
|
||||
goto end;
|
||||
}
|
||||
if (x509_req_to_pem(req, reqlen, outfp) != 1) {
|
||||
fprintf(stderr, "%s: output CSR failed\n", prog);
|
||||
goto end;
|
||||
}
|
||||
ret = 0;
|
||||
end:
|
||||
gmssl_secure_clear(&sm2_key, sizeof(SM2_KEY));
|
||||
if (keyfp) fclose(keyfp);
|
||||
if (outfile && outfp) fclose(outfp);
|
||||
return ret;
|
||||
}
|
||||
|
||||
143
tools/reqparse.c
143
tools/reqparse.c
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2014-2022 The GmSSL Project. All Rights Reserved.
|
||||
* Copyright 2022 The GmSSL Project. All Rights Reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the License); you may
|
||||
* not use this file except in compliance with the License.
|
||||
@@ -7,74 +7,73 @@
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*/
|
||||
|
||||
|
||||
|
||||
#include <stdio.h>
|
||||
#include <errno.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <gmssl/x509.h>
|
||||
#include <gmssl/x509_req.h>
|
||||
|
||||
|
||||
static const char *options = "[-in file] [-out file]";
|
||||
|
||||
int reqparse_main(int argc, char **argv)
|
||||
{
|
||||
int ret = 1;
|
||||
char *prog = argv[0];
|
||||
char *infile = NULL;
|
||||
char *outfile = NULL;
|
||||
FILE *infp = stdin;
|
||||
FILE *outfp = stdout;
|
||||
uint8_t req[1024];
|
||||
size_t reqlen;
|
||||
|
||||
argc--;
|
||||
argv++;
|
||||
|
||||
while (argc > 0) {
|
||||
if (!strcmp(*argv, "-help")) {
|
||||
printf("usage: %s %s\n", prog, options);
|
||||
goto end;
|
||||
} else if(!strcmp(*argv, "-in")) {
|
||||
if (--argc < 1) goto bad;
|
||||
infile = *(++argv);
|
||||
if (!(infp = fopen(infile, "r"))) {
|
||||
fprintf(stderr, "%s: open '%s' failure : %s\n", prog, infile, strerror(errno));
|
||||
goto end;
|
||||
}
|
||||
} else if (!strcmp(*argv, "-out")) {
|
||||
if (--argc < 1) goto bad;
|
||||
outfile = *(++argv);
|
||||
if (!(outfp = fopen(outfile, "w"))) {
|
||||
fprintf(stderr, "%s: open '%s' failure : %s\n", prog, outfile, strerror(errno));
|
||||
goto end;
|
||||
}
|
||||
} else {
|
||||
fprintf(stderr, "%s: illegal option '%s'\n", prog, *argv);
|
||||
goto end;
|
||||
bad:
|
||||
fprintf(stderr, "%s: '%s' option value missing\n", prog, *argv);
|
||||
goto end;
|
||||
}
|
||||
|
||||
argc--;
|
||||
argv++;
|
||||
}
|
||||
|
||||
if (x509_req_from_pem(req, &reqlen, sizeof(req), infp) != 1) {
|
||||
fprintf(stderr, "%s: read CSR failure\n", prog);
|
||||
goto end;
|
||||
}
|
||||
x509_req_print(outfp, 0, 0, "CertificationRequest", req, reqlen);
|
||||
if (x509_req_to_pem(req, reqlen, outfp) != 1) {
|
||||
fprintf(stderr, "%s: output CSR failure\n", prog);
|
||||
goto end;
|
||||
}
|
||||
ret = 0;
|
||||
end:
|
||||
if (infile && infp) fclose(infp);
|
||||
if (outfile && outfp) fclose(outfp);
|
||||
return ret;
|
||||
}
|
||||
|
||||
#include <stdio.h>
|
||||
#include <errno.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <gmssl/x509.h>
|
||||
#include <gmssl/x509_req.h>
|
||||
|
||||
|
||||
static const char *options = "[-in file] [-out file]";
|
||||
|
||||
int reqparse_main(int argc, char **argv)
|
||||
{
|
||||
int ret = 1;
|
||||
char *prog = argv[0];
|
||||
char *infile = NULL;
|
||||
char *outfile = NULL;
|
||||
FILE *infp = stdin;
|
||||
FILE *outfp = stdout;
|
||||
uint8_t req[1024];
|
||||
size_t reqlen;
|
||||
|
||||
argc--;
|
||||
argv++;
|
||||
|
||||
while (argc > 0) {
|
||||
if (!strcmp(*argv, "-help")) {
|
||||
printf("usage: %s %s\n", prog, options);
|
||||
goto end;
|
||||
} else if(!strcmp(*argv, "-in")) {
|
||||
if (--argc < 1) goto bad;
|
||||
infile = *(++argv);
|
||||
if (!(infp = fopen(infile, "r"))) {
|
||||
fprintf(stderr, "%s: open '%s' failure : %s\n", prog, infile, strerror(errno));
|
||||
goto end;
|
||||
}
|
||||
} else if (!strcmp(*argv, "-out")) {
|
||||
if (--argc < 1) goto bad;
|
||||
outfile = *(++argv);
|
||||
if (!(outfp = fopen(outfile, "w"))) {
|
||||
fprintf(stderr, "%s: open '%s' failure : %s\n", prog, outfile, strerror(errno));
|
||||
goto end;
|
||||
}
|
||||
} else {
|
||||
fprintf(stderr, "%s: illegal option '%s'\n", prog, *argv);
|
||||
goto end;
|
||||
bad:
|
||||
fprintf(stderr, "%s: '%s' option value missing\n", prog, *argv);
|
||||
goto end;
|
||||
}
|
||||
|
||||
argc--;
|
||||
argv++;
|
||||
}
|
||||
|
||||
if (x509_req_from_pem(req, &reqlen, sizeof(req), infp) != 1) {
|
||||
fprintf(stderr, "%s: read CSR failure\n", prog);
|
||||
goto end;
|
||||
}
|
||||
x509_req_print(outfp, 0, 0, "CertificationRequest", req, reqlen);
|
||||
if (x509_req_to_pem(req, reqlen, outfp) != 1) {
|
||||
fprintf(stderr, "%s: output CSR failure\n", prog);
|
||||
goto end;
|
||||
}
|
||||
ret = 0;
|
||||
end:
|
||||
if (infile && infp) fclose(infp);
|
||||
if (outfile && outfp) fclose(outfp);
|
||||
return ret;
|
||||
}
|
||||
|
||||
475
tools/reqsign.c
475
tools/reqsign.c
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2014-2022 The GmSSL Project. All Rights Reserved.
|
||||
* Copyright 2022 The GmSSL Project. All Rights Reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the License); you may
|
||||
* not use this file except in compliance with the License.
|
||||
@@ -7,240 +7,239 @@
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*/
|
||||
|
||||
|
||||
|
||||
#include <stdio.h>
|
||||
#include <errno.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <gmssl/mem.h>
|
||||
#include <gmssl/rand.h>
|
||||
#include <gmssl/x509.h>
|
||||
#include <gmssl/x509_ext.h>
|
||||
#include <gmssl/x509_req.h>
|
||||
|
||||
|
||||
static const char *options = "[-in pem] -days num -cacert pem -key pem [-pass str] [-out pem] "
|
||||
"-key_usage oid -path_len_constraint num -crl_url url\n";
|
||||
|
||||
static int ext_key_usage_set(int *usages, const char *usage_name)
|
||||
{
|
||||
int flag = 0;
|
||||
if (x509_key_usage_from_name(&flag, usage_name) != 1) {
|
||||
return -1;
|
||||
}
|
||||
*usages |= flag;
|
||||
return 1;
|
||||
}
|
||||
|
||||
int reqsign_main(int argc, char **argv)
|
||||
{
|
||||
int ret = 1;
|
||||
char *prog = argv[0];
|
||||
char *infile = NULL;
|
||||
int days = 0;
|
||||
char *cacertfile = NULL;
|
||||
char *keyfile = NULL;
|
||||
char *pass = NULL;
|
||||
char *outfile = NULL;
|
||||
FILE *infp = stdin;
|
||||
FILE *cacertfp = NULL;
|
||||
FILE *keyfp = NULL;
|
||||
FILE *outfp = stdout;
|
||||
|
||||
uint8_t req[512];
|
||||
size_t reqlen;
|
||||
const uint8_t *subject;
|
||||
size_t subject_len;
|
||||
SM2_KEY subject_public_key;
|
||||
|
||||
uint8_t cacert[1024];
|
||||
size_t cacertlen;
|
||||
const uint8_t *issuer;
|
||||
size_t issuer_len;
|
||||
SM2_KEY issuer_public_key;
|
||||
|
||||
SM2_KEY sm2_key;
|
||||
|
||||
uint8_t cert[1024];
|
||||
size_t certlen;
|
||||
uint8_t serial[12];
|
||||
time_t not_before, not_after;
|
||||
uint8_t exts[512];
|
||||
size_t extslen = 0;
|
||||
int key_usage = 0;
|
||||
int path_len_constraint = -1;
|
||||
|
||||
argc--;
|
||||
argv++;
|
||||
|
||||
if (argc < 1) {
|
||||
fprintf(stderr, "usage: %s %s\n", prog, options);
|
||||
return 1;
|
||||
}
|
||||
|
||||
while (argc >= 1) {
|
||||
if (!strcmp(*argv, "-help")) {
|
||||
printf("usage: %s %s\n", prog, options);
|
||||
ret = 0;
|
||||
goto end;
|
||||
} else if (!strcmp(*argv, "-in")) {
|
||||
if (--argc < 1) goto bad;
|
||||
infile = *(++argv);
|
||||
if (!(infp = fopen(infile, "r"))) {
|
||||
fprintf(stderr, "%s: open '%s' failure : %s\n", prog, outfile, strerror(errno));
|
||||
goto end;
|
||||
}
|
||||
} else if (!strcmp(*argv, "-days")) {
|
||||
if (--argc < 1) goto bad;
|
||||
days = atoi(*(++argv));
|
||||
if (days <= 0) {
|
||||
fprintf(stderr, "%s: invalid '-days' value\n", prog);
|
||||
goto end;
|
||||
}
|
||||
} else if (!strcmp(*argv, "-key_usage")) {
|
||||
if (--argc < 1) goto bad;
|
||||
if (ext_key_usage_set(&key_usage, *(++argv)) != 1) {
|
||||
fprintf(stderr, "%s: set KeyUsage extenstion failure\n", prog);
|
||||
goto end;
|
||||
}
|
||||
} else if (!strcmp(*argv, "-path_len_constraint")) {
|
||||
if (--argc < 1) goto bad;
|
||||
path_len_constraint = atoi(*(++argv));
|
||||
if (path_len_constraint < 0) {
|
||||
fprintf(stderr, "%s: invalid value for '-path_len_constraint'\n", prog);
|
||||
goto end;
|
||||
}
|
||||
} else if (!strcmp(*argv, "-crl_url")) {
|
||||
if (--argc < 1) goto bad;
|
||||
//crl_url = *(++argv);
|
||||
} else if (!strcmp(*argv, "-cacert")) {
|
||||
if (--argc < 1) goto bad;
|
||||
cacertfile = *(++argv);
|
||||
if (!(cacertfp = fopen(cacertfile, "r"))) {
|
||||
fprintf(stderr, "%s: invalid -key_usage value\n", prog);
|
||||
goto end;
|
||||
}
|
||||
} else if (!strcmp(*argv, "-key")) {
|
||||
if (--argc < 1) goto bad;
|
||||
keyfile = *(++argv);
|
||||
if (!(keyfp = fopen(keyfile, "r"))) {
|
||||
fprintf(stderr, "%s: open '%s' failure : %s\n", prog, keyfile, strerror(errno));
|
||||
goto end;
|
||||
}
|
||||
} else if (!strcmp(*argv, "-pass")) {
|
||||
if (--argc < 1) goto bad;
|
||||
pass = *(++argv);
|
||||
} else if (!strcmp(*argv, "-out")) {
|
||||
if (--argc < 1) goto bad;
|
||||
outfile = *(++argv);
|
||||
if (!(outfp = fopen(outfile, "w"))) {
|
||||
fprintf(stderr, "%s: open '%s' failure : %s\n", prog, outfile, strerror(errno));
|
||||
goto end;
|
||||
}
|
||||
} else {
|
||||
fprintf(stderr, "%s: illegal option '%s'\n", prog, *argv);
|
||||
goto end;
|
||||
bad:
|
||||
fprintf(stderr, "%s: '%s' option value missing\n", prog, *argv);
|
||||
goto end;
|
||||
}
|
||||
|
||||
argc--;
|
||||
argv++;
|
||||
}
|
||||
|
||||
if (!days) {
|
||||
fprintf(stderr, "%s: '-days' option required\n", prog);
|
||||
goto end;
|
||||
}
|
||||
if (!cacertfile) {
|
||||
fprintf(stderr, "%s: '-cacert' option required\n", prog);
|
||||
goto end;
|
||||
}
|
||||
if (!keyfile) {
|
||||
fprintf(stderr, "%s: '-key' option required\n", prog);
|
||||
goto end;
|
||||
}
|
||||
if (!pass) {
|
||||
fprintf(stderr, "%s: '-pass' option required\n", prog);
|
||||
goto end;
|
||||
}
|
||||
|
||||
|
||||
if (x509_req_from_pem(req, &reqlen, sizeof(req), infp) != 1
|
||||
|| x509_req_get_details(req, reqlen,
|
||||
NULL, &subject, &subject_len, &subject_public_key,
|
||||
NULL, NULL, NULL, NULL, NULL) != 1) {
|
||||
fprintf(stderr, "%s: parse CSR failure\n", prog);
|
||||
goto end;
|
||||
}
|
||||
|
||||
if (x509_cert_from_pem(cacert, &cacertlen, sizeof(cacert), cacertfp) != 1
|
||||
|| x509_cert_get_subject(cacert, cacertlen, &issuer, &issuer_len) != 1
|
||||
|| x509_cert_get_subject_public_key(cacert, cacertlen, &issuer_public_key) != 1) {
|
||||
fprintf(stderr, "%s: parse CA certificate failure\n", prog);
|
||||
goto end;
|
||||
}
|
||||
|
||||
if (sm2_private_key_info_decrypt_from_pem(&sm2_key, pass, keyfp) != 1) {
|
||||
fprintf(stderr, "%s: load private key failure\n", prog);
|
||||
goto end;
|
||||
}
|
||||
if (sm2_public_key_equ(&sm2_key, &issuer_public_key) != 1) {
|
||||
fprintf(stderr, "%s: private key and CA certificate not match\n", prog);
|
||||
goto end;
|
||||
}
|
||||
|
||||
if (rand_bytes(serial, sizeof(serial)) != 1) {
|
||||
fprintf(stderr, "%s: inner error\n", prog);
|
||||
goto end;
|
||||
}
|
||||
time(¬_before);
|
||||
|
||||
|
||||
if (x509_exts_add_key_usage(exts, &extslen, sizeof(exts), 1, key_usage) != 1) {
|
||||
fprintf(stderr, "%s: inner error\n", prog);
|
||||
goto end;
|
||||
}
|
||||
if (path_len_constraint >= 0) {
|
||||
if (x509_exts_add_basic_constraints(exts, &extslen, sizeof(exts), 1, 1, path_len_constraint) != 1) {
|
||||
fprintf(stderr, "%s: inner error\n", prog);
|
||||
goto end;
|
||||
}
|
||||
}
|
||||
if (x509_exts_add_default_authority_key_identifier(exts, &extslen, sizeof(exts), &sm2_key) != 1) {
|
||||
fprintf(stderr, "%s: inner error\n", prog);
|
||||
goto end;
|
||||
}
|
||||
|
||||
if (x509_validity_add_days(¬_after, not_before, days) != 1
|
||||
|| x509_cert_sign(
|
||||
cert, &certlen, sizeof(cert),
|
||||
X509_version_v3,
|
||||
serial, sizeof(serial),
|
||||
OID_sm2sign_with_sm3,
|
||||
issuer, issuer_len,
|
||||
not_before, not_after,
|
||||
subject, subject_len,
|
||||
&subject_public_key,
|
||||
NULL, 0,
|
||||
NULL, 0,
|
||||
exts, extslen,
|
||||
&sm2_key, SM2_DEFAULT_ID, SM2_DEFAULT_ID_LENGTH) != 1) {
|
||||
fprintf(stderr, "%s: inner error\n", prog);
|
||||
goto end;
|
||||
}
|
||||
if (x509_cert_to_pem(cert, certlen, outfp) != 1) {
|
||||
fprintf(stderr, "%s: output certificate failed\n", prog);
|
||||
goto end;
|
||||
}
|
||||
ret = 0;
|
||||
end:
|
||||
gmssl_secure_clear(&sm2_key, sizeof(SM2_KEY));
|
||||
if (keyfp) fclose(keyfp);
|
||||
if (cacertfp) fclose(cacertfp);
|
||||
if (infile && infp) fclose(infp);
|
||||
if (outfile && outfp) fclose(outfp);
|
||||
return ret;
|
||||
}
|
||||
|
||||
#include <stdio.h>
|
||||
#include <errno.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <gmssl/mem.h>
|
||||
#include <gmssl/rand.h>
|
||||
#include <gmssl/x509.h>
|
||||
#include <gmssl/x509_ext.h>
|
||||
#include <gmssl/x509_req.h>
|
||||
|
||||
|
||||
static const char *options = "[-in pem] -days num -cacert pem -key pem [-pass str] [-out pem] "
|
||||
"-key_usage oid -path_len_constraint num -crl_url url\n";
|
||||
|
||||
static int ext_key_usage_set(int *usages, const char *usage_name)
|
||||
{
|
||||
int flag = 0;
|
||||
if (x509_key_usage_from_name(&flag, usage_name) != 1) {
|
||||
return -1;
|
||||
}
|
||||
*usages |= flag;
|
||||
return 1;
|
||||
}
|
||||
|
||||
int reqsign_main(int argc, char **argv)
|
||||
{
|
||||
int ret = 1;
|
||||
char *prog = argv[0];
|
||||
char *infile = NULL;
|
||||
int days = 0;
|
||||
char *cacertfile = NULL;
|
||||
char *keyfile = NULL;
|
||||
char *pass = NULL;
|
||||
char *outfile = NULL;
|
||||
FILE *infp = stdin;
|
||||
FILE *cacertfp = NULL;
|
||||
FILE *keyfp = NULL;
|
||||
FILE *outfp = stdout;
|
||||
|
||||
uint8_t req[512];
|
||||
size_t reqlen;
|
||||
const uint8_t *subject;
|
||||
size_t subject_len;
|
||||
SM2_KEY subject_public_key;
|
||||
|
||||
uint8_t cacert[1024];
|
||||
size_t cacertlen;
|
||||
const uint8_t *issuer;
|
||||
size_t issuer_len;
|
||||
SM2_KEY issuer_public_key;
|
||||
|
||||
SM2_KEY sm2_key;
|
||||
|
||||
uint8_t cert[1024];
|
||||
size_t certlen;
|
||||
uint8_t serial[12];
|
||||
time_t not_before, not_after;
|
||||
uint8_t exts[512];
|
||||
size_t extslen = 0;
|
||||
int key_usage = 0;
|
||||
int path_len_constraint = -1;
|
||||
|
||||
argc--;
|
||||
argv++;
|
||||
|
||||
if (argc < 1) {
|
||||
fprintf(stderr, "usage: %s %s\n", prog, options);
|
||||
return 1;
|
||||
}
|
||||
|
||||
while (argc >= 1) {
|
||||
if (!strcmp(*argv, "-help")) {
|
||||
printf("usage: %s %s\n", prog, options);
|
||||
ret = 0;
|
||||
goto end;
|
||||
} else if (!strcmp(*argv, "-in")) {
|
||||
if (--argc < 1) goto bad;
|
||||
infile = *(++argv);
|
||||
if (!(infp = fopen(infile, "r"))) {
|
||||
fprintf(stderr, "%s: open '%s' failure : %s\n", prog, outfile, strerror(errno));
|
||||
goto end;
|
||||
}
|
||||
} else if (!strcmp(*argv, "-days")) {
|
||||
if (--argc < 1) goto bad;
|
||||
days = atoi(*(++argv));
|
||||
if (days <= 0) {
|
||||
fprintf(stderr, "%s: invalid '-days' value\n", prog);
|
||||
goto end;
|
||||
}
|
||||
} else if (!strcmp(*argv, "-key_usage")) {
|
||||
if (--argc < 1) goto bad;
|
||||
if (ext_key_usage_set(&key_usage, *(++argv)) != 1) {
|
||||
fprintf(stderr, "%s: set KeyUsage extenstion failure\n", prog);
|
||||
goto end;
|
||||
}
|
||||
} else if (!strcmp(*argv, "-path_len_constraint")) {
|
||||
if (--argc < 1) goto bad;
|
||||
path_len_constraint = atoi(*(++argv));
|
||||
if (path_len_constraint < 0) {
|
||||
fprintf(stderr, "%s: invalid value for '-path_len_constraint'\n", prog);
|
||||
goto end;
|
||||
}
|
||||
} else if (!strcmp(*argv, "-crl_url")) {
|
||||
if (--argc < 1) goto bad;
|
||||
//crl_url = *(++argv);
|
||||
} else if (!strcmp(*argv, "-cacert")) {
|
||||
if (--argc < 1) goto bad;
|
||||
cacertfile = *(++argv);
|
||||
if (!(cacertfp = fopen(cacertfile, "r"))) {
|
||||
fprintf(stderr, "%s: invalid -key_usage value\n", prog);
|
||||
goto end;
|
||||
}
|
||||
} else if (!strcmp(*argv, "-key")) {
|
||||
if (--argc < 1) goto bad;
|
||||
keyfile = *(++argv);
|
||||
if (!(keyfp = fopen(keyfile, "r"))) {
|
||||
fprintf(stderr, "%s: open '%s' failure : %s\n", prog, keyfile, strerror(errno));
|
||||
goto end;
|
||||
}
|
||||
} else if (!strcmp(*argv, "-pass")) {
|
||||
if (--argc < 1) goto bad;
|
||||
pass = *(++argv);
|
||||
} else if (!strcmp(*argv, "-out")) {
|
||||
if (--argc < 1) goto bad;
|
||||
outfile = *(++argv);
|
||||
if (!(outfp = fopen(outfile, "w"))) {
|
||||
fprintf(stderr, "%s: open '%s' failure : %s\n", prog, outfile, strerror(errno));
|
||||
goto end;
|
||||
}
|
||||
} else {
|
||||
fprintf(stderr, "%s: illegal option '%s'\n", prog, *argv);
|
||||
goto end;
|
||||
bad:
|
||||
fprintf(stderr, "%s: '%s' option value missing\n", prog, *argv);
|
||||
goto end;
|
||||
}
|
||||
|
||||
argc--;
|
||||
argv++;
|
||||
}
|
||||
|
||||
if (!days) {
|
||||
fprintf(stderr, "%s: '-days' option required\n", prog);
|
||||
goto end;
|
||||
}
|
||||
if (!cacertfile) {
|
||||
fprintf(stderr, "%s: '-cacert' option required\n", prog);
|
||||
goto end;
|
||||
}
|
||||
if (!keyfile) {
|
||||
fprintf(stderr, "%s: '-key' option required\n", prog);
|
||||
goto end;
|
||||
}
|
||||
if (!pass) {
|
||||
fprintf(stderr, "%s: '-pass' option required\n", prog);
|
||||
goto end;
|
||||
}
|
||||
|
||||
|
||||
if (x509_req_from_pem(req, &reqlen, sizeof(req), infp) != 1
|
||||
|| x509_req_get_details(req, reqlen,
|
||||
NULL, &subject, &subject_len, &subject_public_key,
|
||||
NULL, NULL, NULL, NULL, NULL) != 1) {
|
||||
fprintf(stderr, "%s: parse CSR failure\n", prog);
|
||||
goto end;
|
||||
}
|
||||
|
||||
if (x509_cert_from_pem(cacert, &cacertlen, sizeof(cacert), cacertfp) != 1
|
||||
|| x509_cert_get_subject(cacert, cacertlen, &issuer, &issuer_len) != 1
|
||||
|| x509_cert_get_subject_public_key(cacert, cacertlen, &issuer_public_key) != 1) {
|
||||
fprintf(stderr, "%s: parse CA certificate failure\n", prog);
|
||||
goto end;
|
||||
}
|
||||
|
||||
if (sm2_private_key_info_decrypt_from_pem(&sm2_key, pass, keyfp) != 1) {
|
||||
fprintf(stderr, "%s: load private key failure\n", prog);
|
||||
goto end;
|
||||
}
|
||||
if (sm2_public_key_equ(&sm2_key, &issuer_public_key) != 1) {
|
||||
fprintf(stderr, "%s: private key and CA certificate not match\n", prog);
|
||||
goto end;
|
||||
}
|
||||
|
||||
if (rand_bytes(serial, sizeof(serial)) != 1) {
|
||||
fprintf(stderr, "%s: inner error\n", prog);
|
||||
goto end;
|
||||
}
|
||||
time(¬_before);
|
||||
|
||||
|
||||
if (x509_exts_add_key_usage(exts, &extslen, sizeof(exts), 1, key_usage) != 1) {
|
||||
fprintf(stderr, "%s: inner error\n", prog);
|
||||
goto end;
|
||||
}
|
||||
if (path_len_constraint >= 0) {
|
||||
if (x509_exts_add_basic_constraints(exts, &extslen, sizeof(exts), 1, 1, path_len_constraint) != 1) {
|
||||
fprintf(stderr, "%s: inner error\n", prog);
|
||||
goto end;
|
||||
}
|
||||
}
|
||||
if (x509_exts_add_default_authority_key_identifier(exts, &extslen, sizeof(exts), &sm2_key) != 1) {
|
||||
fprintf(stderr, "%s: inner error\n", prog);
|
||||
goto end;
|
||||
}
|
||||
|
||||
if (x509_validity_add_days(¬_after, not_before, days) != 1
|
||||
|| x509_cert_sign(
|
||||
cert, &certlen, sizeof(cert),
|
||||
X509_version_v3,
|
||||
serial, sizeof(serial),
|
||||
OID_sm2sign_with_sm3,
|
||||
issuer, issuer_len,
|
||||
not_before, not_after,
|
||||
subject, subject_len,
|
||||
&subject_public_key,
|
||||
NULL, 0,
|
||||
NULL, 0,
|
||||
exts, extslen,
|
||||
&sm2_key, SM2_DEFAULT_ID, SM2_DEFAULT_ID_LENGTH) != 1) {
|
||||
fprintf(stderr, "%s: inner error\n", prog);
|
||||
goto end;
|
||||
}
|
||||
if (x509_cert_to_pem(cert, certlen, outfp) != 1) {
|
||||
fprintf(stderr, "%s: output certificate failed\n", prog);
|
||||
goto end;
|
||||
}
|
||||
ret = 0;
|
||||
end:
|
||||
gmssl_secure_clear(&sm2_key, sizeof(SM2_KEY));
|
||||
if (keyfp) fclose(keyfp);
|
||||
if (cacertfp) fclose(cacertfp);
|
||||
if (infile && infp) fclose(infp);
|
||||
if (outfile && outfp) fclose(outfp);
|
||||
return ret;
|
||||
}
|
||||
|
||||
417
tools/sdfutil.c
417
tools/sdfutil.c
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2014-2022 The GmSSL Project. All Rights Reserved.
|
||||
* Copyright 2022 The GmSSL Project. All Rights Reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the License); you may
|
||||
* not use this file except in compliance with the License.
|
||||
@@ -7,211 +7,210 @@
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*/
|
||||
|
||||
|
||||
|
||||
|
||||
#include <stdio.h>
|
||||
#include <errno.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <gmssl/mem.h>
|
||||
#include <gmssl/sm2.h>
|
||||
#include <gmssl/sm3.h>
|
||||
#include <gmssl/sdf.h>
|
||||
|
||||
|
||||
#define OP_NONE 0
|
||||
#define OP_DEVINFO 1
|
||||
#define OP_EXPORTPUBKEY 2
|
||||
#define OP_SIGN 3
|
||||
#define OP_RAND 4
|
||||
|
||||
|
||||
static void print_usage(FILE *fp, const char *prog)
|
||||
{
|
||||
fprintf(fp, "usage:\n");
|
||||
fprintf(fp, " %s -lib so_path -devinfo\n", prog);
|
||||
fprintf(fp, " %s -lib so_path -exportpubkey -key index [-out file]\n", prog);
|
||||
fprintf(fp, " %s -lib so_path -sign [-in file] [-out file]\n", prog);
|
||||
fprintf(fp, " %s -lib so_path -rand num [-out file]\n", prog);
|
||||
}
|
||||
|
||||
int sdfutil_main(int argc, char **argv)
|
||||
{
|
||||
int ret = 1;
|
||||
char *prog = argv[0];
|
||||
char *lib = NULL;
|
||||
int op = 0;
|
||||
int keyindex = -1;
|
||||
char *pass = NULL;
|
||||
char *id = SM2_DEFAULT_ID;
|
||||
int num = 0;
|
||||
char *infile = NULL;
|
||||
char *outfile = NULL;
|
||||
FILE *infp = stdin;
|
||||
FILE *outfp = stdout;
|
||||
unsigned char buf[4096];
|
||||
unsigned int ulen;
|
||||
int len;
|
||||
SDF_DEVICE dev;
|
||||
SDF_KEY key;
|
||||
int dev_opened = 0;
|
||||
int key_opened = 0;
|
||||
|
||||
memset(&dev, 0, sizeof(dev));
|
||||
memset(&key, 0, sizeof(key));
|
||||
|
||||
argc--;
|
||||
argv++;
|
||||
|
||||
if (argc < 1) {
|
||||
print_usage(stderr, prog);
|
||||
return 1;
|
||||
}
|
||||
|
||||
while (argc > 0) {
|
||||
if (!strcmp(*argv, "-help")) {
|
||||
print_usage(stdout, prog);
|
||||
goto end;
|
||||
} else if (!strcmp(*argv, "-lib")) {
|
||||
if (--argc < 1) goto bad;
|
||||
lib = *(++argv);
|
||||
} else if (!strcmp(*argv, "-devinfo")) {
|
||||
op = OP_DEVINFO;
|
||||
} else if (!strcmp(*argv, "-exportpubkey")) {
|
||||
op = OP_EXPORTPUBKEY;
|
||||
} else if (!strcmp(*argv, "-sign")) {
|
||||
op = OP_SIGN;
|
||||
} else if (!strcmp(*argv, "-key")) {
|
||||
if (--argc < 1) goto bad;
|
||||
keyindex = atoi(*(++argv));
|
||||
} else if (!strcmp(*argv, "-pass")) {
|
||||
if (--argc < 1) goto bad;
|
||||
pass = *(++argv);
|
||||
} else if (!strcmp(*argv, "-id")) {
|
||||
if (--argc < 1) goto bad;
|
||||
id = *(++argv);
|
||||
} else if (!strcmp(*argv, "-rand")) {
|
||||
if (--argc < 1) goto bad;
|
||||
len = atoi(*(++argv));
|
||||
} else if (!strcmp(*argv, "-in")) {
|
||||
if (--argc < 1) goto bad;
|
||||
infile = *(++argv);
|
||||
if (!(infp = fopen(infile, "r"))) {
|
||||
fprintf(stderr, "%s: open '%s' failure : %s\n", prog, infile, strerror(errno));
|
||||
goto end;
|
||||
}
|
||||
} else if (!strcmp(*argv, "-out")) {
|
||||
if (--argc < 1) goto bad;
|
||||
outfile = *(++argv);
|
||||
if (!(outfp = fopen(outfile, "w"))) {
|
||||
fprintf(stderr, "%s: open '%s' failure : %s\n", prog, outfile, strerror(errno));
|
||||
goto end;
|
||||
}
|
||||
} else {
|
||||
fprintf(stderr, "%s: illegal option '%s'\n", prog, *argv);
|
||||
goto end;
|
||||
bad:
|
||||
fprintf(stderr, "%s: '%s' option value missing\n", prog, *argv);
|
||||
goto end;
|
||||
}
|
||||
|
||||
argc--;
|
||||
argv++;
|
||||
}
|
||||
|
||||
if (!lib) {
|
||||
fprintf(stderr, "%s: option '-lib' required\n", prog);
|
||||
goto end;
|
||||
}
|
||||
if (sdf_load_library(lib, NULL) != 1) {
|
||||
fprintf(stderr, "%s: load library failure\n", prog);
|
||||
goto end;
|
||||
}
|
||||
|
||||
if (sdf_open_device(&dev) != 1) {
|
||||
fprintf(stderr, "%s: open device failure\n", prog);
|
||||
goto end;
|
||||
}
|
||||
dev_opened = 1;
|
||||
|
||||
switch (op) {
|
||||
case OP_DEVINFO:
|
||||
sdf_print_device_info(stdout, 0, 0, "SDF", &dev);
|
||||
break;
|
||||
|
||||
case OP_EXPORTPUBKEY:
|
||||
if (keyindex < 0) {
|
||||
fprintf(stderr, "%s: invalid key index\n", prog);
|
||||
goto end;
|
||||
}
|
||||
if (sdf_load_sign_key(&dev, &key, keyindex, pass) != 1) {
|
||||
fprintf(stderr, "%s: load sign key failed\n", prog);
|
||||
goto end;
|
||||
}
|
||||
key_opened = 1;
|
||||
if (sm2_public_key_info_to_pem(&(key.public_key), outfp) != 1) {
|
||||
fprintf(stderr, "%s: output public key to PEM failed\n", prog);
|
||||
goto end;
|
||||
}
|
||||
break;
|
||||
|
||||
case OP_SIGN:
|
||||
{
|
||||
SM3_CTX sm3_ctx;
|
||||
uint8_t dgst[32];
|
||||
uint8_t sig[SM2_MAX_SIGNATURE_SIZE];
|
||||
size_t siglen;
|
||||
|
||||
if (sdf_load_sign_key(&dev, &key, keyindex, pass) != 1) {
|
||||
fprintf(stderr, "%s: load sign key failed\n", prog);
|
||||
goto end;
|
||||
}
|
||||
key_opened = 1;
|
||||
|
||||
sm3_init(&sm3_ctx);
|
||||
sm2_compute_z(dgst, &(key.public_key.public_key), id, strlen(id));
|
||||
sm3_update(&sm3_ctx, dgst, sizeof(dgst));
|
||||
|
||||
while ((len = fread(buf, 1, sizeof(buf), infp)) > 0) {
|
||||
sm3_update(&sm3_ctx, buf, len);
|
||||
}
|
||||
sm3_finish(&sm3_ctx, dgst);
|
||||
|
||||
if ((ret = sdf_sign(&key, dgst, sig, &siglen)) != 1) {
|
||||
fprintf(stderr, "%s: inner error\n", prog);
|
||||
goto end;
|
||||
}
|
||||
if (fwrite(sig, 1, siglen, outfp) != siglen) {
|
||||
fprintf(stderr, "%s: output failure : %s\n", prog, strerror(errno));
|
||||
goto end;
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case OP_RAND:
|
||||
if (sdf_rand_bytes(&dev, buf, len) != 1) {
|
||||
fprintf(stderr, "%s: inner error\n", prog);
|
||||
goto end;
|
||||
}
|
||||
if (fwrite(buf, 1, len, outfp) != len) {
|
||||
fprintf(stderr, "%s: output failure : %s\n", prog, strerror(errno));
|
||||
goto end;
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
fprintf(stderr, "%s: this should not happen\n", prog);
|
||||
goto end;
|
||||
}
|
||||
ret = 0;
|
||||
|
||||
end:
|
||||
gmssl_secure_clear(buf, sizeof(buf));
|
||||
if (key_opened) sdf_release_key(&key);
|
||||
if (dev_opened) sdf_close_device(&dev);
|
||||
if (lib) sdf_unload_library();
|
||||
if (infile && infp) fclose(infp);
|
||||
if (outfile && outfp) fclose(outfp);
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
#include <stdio.h>
|
||||
#include <errno.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <gmssl/mem.h>
|
||||
#include <gmssl/sm2.h>
|
||||
#include <gmssl/sm3.h>
|
||||
#include <gmssl/sdf.h>
|
||||
|
||||
|
||||
#define OP_NONE 0
|
||||
#define OP_DEVINFO 1
|
||||
#define OP_EXPORTPUBKEY 2
|
||||
#define OP_SIGN 3
|
||||
#define OP_RAND 4
|
||||
|
||||
|
||||
static void print_usage(FILE *fp, const char *prog)
|
||||
{
|
||||
fprintf(fp, "usage:\n");
|
||||
fprintf(fp, " %s -lib so_path -devinfo\n", prog);
|
||||
fprintf(fp, " %s -lib so_path -exportpubkey -key index [-out file]\n", prog);
|
||||
fprintf(fp, " %s -lib so_path -sign [-in file] [-out file]\n", prog);
|
||||
fprintf(fp, " %s -lib so_path -rand num [-out file]\n", prog);
|
||||
}
|
||||
|
||||
int sdfutil_main(int argc, char **argv)
|
||||
{
|
||||
int ret = 1;
|
||||
char *prog = argv[0];
|
||||
char *lib = NULL;
|
||||
int op = 0;
|
||||
int keyindex = -1;
|
||||
char *pass = NULL;
|
||||
char *id = SM2_DEFAULT_ID;
|
||||
int num = 0;
|
||||
char *infile = NULL;
|
||||
char *outfile = NULL;
|
||||
FILE *infp = stdin;
|
||||
FILE *outfp = stdout;
|
||||
unsigned char buf[4096];
|
||||
unsigned int ulen;
|
||||
int len;
|
||||
SDF_DEVICE dev;
|
||||
SDF_KEY key;
|
||||
int dev_opened = 0;
|
||||
int key_opened = 0;
|
||||
|
||||
memset(&dev, 0, sizeof(dev));
|
||||
memset(&key, 0, sizeof(key));
|
||||
|
||||
argc--;
|
||||
argv++;
|
||||
|
||||
if (argc < 1) {
|
||||
print_usage(stderr, prog);
|
||||
return 1;
|
||||
}
|
||||
|
||||
while (argc > 0) {
|
||||
if (!strcmp(*argv, "-help")) {
|
||||
print_usage(stdout, prog);
|
||||
goto end;
|
||||
} else if (!strcmp(*argv, "-lib")) {
|
||||
if (--argc < 1) goto bad;
|
||||
lib = *(++argv);
|
||||
} else if (!strcmp(*argv, "-devinfo")) {
|
||||
op = OP_DEVINFO;
|
||||
} else if (!strcmp(*argv, "-exportpubkey")) {
|
||||
op = OP_EXPORTPUBKEY;
|
||||
} else if (!strcmp(*argv, "-sign")) {
|
||||
op = OP_SIGN;
|
||||
} else if (!strcmp(*argv, "-key")) {
|
||||
if (--argc < 1) goto bad;
|
||||
keyindex = atoi(*(++argv));
|
||||
} else if (!strcmp(*argv, "-pass")) {
|
||||
if (--argc < 1) goto bad;
|
||||
pass = *(++argv);
|
||||
} else if (!strcmp(*argv, "-id")) {
|
||||
if (--argc < 1) goto bad;
|
||||
id = *(++argv);
|
||||
} else if (!strcmp(*argv, "-rand")) {
|
||||
if (--argc < 1) goto bad;
|
||||
len = atoi(*(++argv));
|
||||
} else if (!strcmp(*argv, "-in")) {
|
||||
if (--argc < 1) goto bad;
|
||||
infile = *(++argv);
|
||||
if (!(infp = fopen(infile, "r"))) {
|
||||
fprintf(stderr, "%s: open '%s' failure : %s\n", prog, infile, strerror(errno));
|
||||
goto end;
|
||||
}
|
||||
} else if (!strcmp(*argv, "-out")) {
|
||||
if (--argc < 1) goto bad;
|
||||
outfile = *(++argv);
|
||||
if (!(outfp = fopen(outfile, "w"))) {
|
||||
fprintf(stderr, "%s: open '%s' failure : %s\n", prog, outfile, strerror(errno));
|
||||
goto end;
|
||||
}
|
||||
} else {
|
||||
fprintf(stderr, "%s: illegal option '%s'\n", prog, *argv);
|
||||
goto end;
|
||||
bad:
|
||||
fprintf(stderr, "%s: '%s' option value missing\n", prog, *argv);
|
||||
goto end;
|
||||
}
|
||||
|
||||
argc--;
|
||||
argv++;
|
||||
}
|
||||
|
||||
if (!lib) {
|
||||
fprintf(stderr, "%s: option '-lib' required\n", prog);
|
||||
goto end;
|
||||
}
|
||||
if (sdf_load_library(lib, NULL) != 1) {
|
||||
fprintf(stderr, "%s: load library failure\n", prog);
|
||||
goto end;
|
||||
}
|
||||
|
||||
if (sdf_open_device(&dev) != 1) {
|
||||
fprintf(stderr, "%s: open device failure\n", prog);
|
||||
goto end;
|
||||
}
|
||||
dev_opened = 1;
|
||||
|
||||
switch (op) {
|
||||
case OP_DEVINFO:
|
||||
sdf_print_device_info(stdout, 0, 0, "SDF", &dev);
|
||||
break;
|
||||
|
||||
case OP_EXPORTPUBKEY:
|
||||
if (keyindex < 0) {
|
||||
fprintf(stderr, "%s: invalid key index\n", prog);
|
||||
goto end;
|
||||
}
|
||||
if (sdf_load_sign_key(&dev, &key, keyindex, pass) != 1) {
|
||||
fprintf(stderr, "%s: load sign key failed\n", prog);
|
||||
goto end;
|
||||
}
|
||||
key_opened = 1;
|
||||
if (sm2_public_key_info_to_pem(&(key.public_key), outfp) != 1) {
|
||||
fprintf(stderr, "%s: output public key to PEM failed\n", prog);
|
||||
goto end;
|
||||
}
|
||||
break;
|
||||
|
||||
case OP_SIGN:
|
||||
{
|
||||
SM3_CTX sm3_ctx;
|
||||
uint8_t dgst[32];
|
||||
uint8_t sig[SM2_MAX_SIGNATURE_SIZE];
|
||||
size_t siglen;
|
||||
|
||||
if (sdf_load_sign_key(&dev, &key, keyindex, pass) != 1) {
|
||||
fprintf(stderr, "%s: load sign key failed\n", prog);
|
||||
goto end;
|
||||
}
|
||||
key_opened = 1;
|
||||
|
||||
sm3_init(&sm3_ctx);
|
||||
sm2_compute_z(dgst, &(key.public_key.public_key), id, strlen(id));
|
||||
sm3_update(&sm3_ctx, dgst, sizeof(dgst));
|
||||
|
||||
while ((len = fread(buf, 1, sizeof(buf), infp)) > 0) {
|
||||
sm3_update(&sm3_ctx, buf, len);
|
||||
}
|
||||
sm3_finish(&sm3_ctx, dgst);
|
||||
|
||||
if ((ret = sdf_sign(&key, dgst, sig, &siglen)) != 1) {
|
||||
fprintf(stderr, "%s: inner error\n", prog);
|
||||
goto end;
|
||||
}
|
||||
if (fwrite(sig, 1, siglen, outfp) != siglen) {
|
||||
fprintf(stderr, "%s: output failure : %s\n", prog, strerror(errno));
|
||||
goto end;
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case OP_RAND:
|
||||
if (sdf_rand_bytes(&dev, buf, len) != 1) {
|
||||
fprintf(stderr, "%s: inner error\n", prog);
|
||||
goto end;
|
||||
}
|
||||
if (fwrite(buf, 1, len, outfp) != len) {
|
||||
fprintf(stderr, "%s: output failure : %s\n", prog, strerror(errno));
|
||||
goto end;
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
fprintf(stderr, "%s: this should not happen\n", prog);
|
||||
goto end;
|
||||
}
|
||||
ret = 0;
|
||||
|
||||
end:
|
||||
gmssl_secure_clear(buf, sizeof(buf));
|
||||
if (key_opened) sdf_release_key(&key);
|
||||
if (dev_opened) sdf_close_device(&dev);
|
||||
if (lib) sdf_unload_library();
|
||||
if (infile && infp) fclose(infp);
|
||||
if (outfile && outfp) fclose(outfp);
|
||||
return ret;
|
||||
}
|
||||
|
||||
499
tools/skfutil.c
499
tools/skfutil.c
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2014-2022 The GmSSL Project. All Rights Reserved.
|
||||
* Copyright 2022 The GmSSL Project. All Rights Reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the License); you may
|
||||
* not use this file except in compliance with the License.
|
||||
@@ -7,252 +7,251 @@
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*/
|
||||
|
||||
|
||||
|
||||
|
||||
#include <stdio.h>
|
||||
#include <errno.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <gmssl/mem.h>
|
||||
#include <gmssl/hex.h>
|
||||
#include <gmssl/sm2.h>
|
||||
#include <gmssl/sm3.h>
|
||||
#include <gmssl/skf.h>
|
||||
|
||||
|
||||
#define OP_NONE 0
|
||||
#define OP_DEVINFO 1
|
||||
#define OP_EXPORTPUBKEY 2
|
||||
#define OP_SIGN 3
|
||||
#define OP_RAND 4
|
||||
|
||||
|
||||
|
||||
static void print_usage(FILE *fp, const char *prog)
|
||||
{
|
||||
fprintf(fp, "usage:\n");
|
||||
fprintf(fp, " %s -lib so_path -dev str -devinfo\n", prog);
|
||||
fprintf(fp, " %s -lib so_path -dev str -app str [-pass str] -container str -exportpubkey [-out file]\n", prog);
|
||||
fprintf(fp, " %s -lib so_path -dev str -app str [-pass str] -container str -sign [-in file] [-out file]\n", prog);
|
||||
fprintf(fp, " %s -lib so_path -dev str -rand num [-out file]\n", prog);
|
||||
}
|
||||
|
||||
int skfutil_main(int argc, char **argv)
|
||||
{
|
||||
int ret = 1;
|
||||
char *prog = argv[0];
|
||||
char *lib = NULL;
|
||||
int op = 0;
|
||||
char *devname = NULL;
|
||||
char *authkeystr = NULL;
|
||||
char *appname = NULL;
|
||||
char *container_name = NULL;
|
||||
char *pass = NULL;
|
||||
char *id = SM2_DEFAULT_ID;
|
||||
int num = 0;
|
||||
char *infile = NULL;
|
||||
char *outfile = NULL;
|
||||
FILE *infp = stdin;
|
||||
FILE *outfp = stdout;
|
||||
unsigned char buf[4096];
|
||||
unsigned int ulen;
|
||||
int len;
|
||||
|
||||
uint8_t authkey[16];
|
||||
size_t authkeylen;
|
||||
SKF_DEVICE dev;
|
||||
SKF_KEY key;
|
||||
int dev_opened = 0;
|
||||
int key_opened = 0;
|
||||
|
||||
memset(&dev, 0, sizeof(dev));
|
||||
memset(&key, 0, sizeof(key));
|
||||
|
||||
argc--;
|
||||
argv++;
|
||||
|
||||
if (argc < 1) {
|
||||
print_usage(stderr, prog);
|
||||
return 1;
|
||||
}
|
||||
|
||||
while (argc > 0) {
|
||||
if (!strcmp(*argv, "-help")) {
|
||||
print_usage(stdout, prog);
|
||||
goto end;
|
||||
} else if (!strcmp(*argv, "-lib")) {
|
||||
if (--argc < 1) goto bad;
|
||||
lib = *(++argv);
|
||||
} else if (!strcmp(*argv, "-lib")) {
|
||||
if (--argc < 1) goto bad;
|
||||
devname = *(++argv);
|
||||
} else if (!strcmp(*argv, "-devinfo")) {
|
||||
op = OP_DEVINFO;
|
||||
} else if (!strcmp(*argv, "-dev")) {
|
||||
if (--argc < 1) goto bad;
|
||||
devname = *(++argv);
|
||||
} else if (!strcmp(*argv, "-authkey")) {
|
||||
if (--argc < 1) goto bad;
|
||||
authkeystr = *(++argv);
|
||||
if (strlen(authkeystr) != 32) {
|
||||
fprintf(stderr, "%s: invalid authkey length\n", prog);
|
||||
goto end;
|
||||
}
|
||||
hex_to_bytes(authkeystr, strlen(authkeystr), authkey, &authkeylen);
|
||||
} else if (!strcmp(*argv, "-exportpubkey")) {
|
||||
op = OP_EXPORTPUBKEY;
|
||||
} else if (!strcmp(*argv, "-sign")) {
|
||||
op = OP_SIGN;
|
||||
} else if (!strcmp(*argv, "-app")) {
|
||||
if (--argc < 1) goto bad;
|
||||
appname = *(++argv);
|
||||
} else if (!strcmp(*argv, "-container")) {
|
||||
if (--argc < 1) goto bad;
|
||||
container_name = *(++argv);
|
||||
} else if (!strcmp(*argv, "-pass")) {
|
||||
if (--argc < 1) goto bad;
|
||||
pass = *(++argv);
|
||||
} else if (!strcmp(*argv, "-id")) {
|
||||
if (--argc < 1) goto bad;
|
||||
id = *(++argv);
|
||||
} else if (!strcmp(*argv, "-rand")) {
|
||||
if (--argc < 1) goto bad;
|
||||
len = atoi(*(++argv));
|
||||
} else if (!strcmp(*argv, "-in")) {
|
||||
if (--argc < 1) goto bad;
|
||||
infile = *(++argv);
|
||||
if (!(infp = fopen(infile, "r"))) {
|
||||
fprintf(stderr, "%s: open '%s' failure : %s\n", prog, infile, strerror(errno));
|
||||
goto end;
|
||||
}
|
||||
} else if (!strcmp(*argv, "-out")) {
|
||||
if (--argc < 1) goto bad;
|
||||
outfile = *(++argv);
|
||||
if (!(outfp = fopen(outfile, "w"))) {
|
||||
fprintf(stderr, "%s: open '%s' failure : %s\n", prog, outfile, strerror(errno));
|
||||
goto end;
|
||||
}
|
||||
} else {
|
||||
fprintf(stderr, "%s: illegal option '%s'\n", prog, *argv);
|
||||
goto end;
|
||||
bad:
|
||||
fprintf(stderr, "%s: '%s' option value missing\n", prog, *argv);
|
||||
goto end;
|
||||
}
|
||||
|
||||
argc--;
|
||||
argv++;
|
||||
}
|
||||
|
||||
if (!lib) {
|
||||
fprintf(stderr, "%s: option '-lib' required\n", prog);
|
||||
goto end;
|
||||
}
|
||||
if (skf_load_library(lib, NULL) != 1) {
|
||||
fprintf(stderr, "%s: load library failure\n", prog);
|
||||
goto end;
|
||||
}
|
||||
|
||||
if (!op) {
|
||||
fprintf(stderr, "%s: option of (-devinfo|-exportpubkey|-sign|-rand) required\n", prog);
|
||||
goto end;
|
||||
}
|
||||
|
||||
if (!devname) {
|
||||
fprintf(stderr, "%s: option '-dev' required\n", prog);
|
||||
goto end;
|
||||
}
|
||||
if (op == OP_DEVINFO) {
|
||||
skf_print_device_info(stdout, 0, 0, devname);
|
||||
ret = 0;
|
||||
goto end;
|
||||
}
|
||||
|
||||
if (skf_open_device(&dev, devname, authkey) != 1) {
|
||||
fprintf(stderr, "%s: open device failure\n", prog);
|
||||
goto end;
|
||||
}
|
||||
dev_opened = 1;
|
||||
|
||||
if (op == OP_RAND) {
|
||||
if (skf_rand_bytes(&dev, buf, len) != 1) {
|
||||
fprintf(stderr, "%s: inner error\n", prog);
|
||||
goto end;
|
||||
}
|
||||
if (fwrite(buf, 1, len, outfp) != len) {
|
||||
fprintf(stderr, "%s: output failure : %s\n", prog, strerror(errno));
|
||||
goto end;
|
||||
}
|
||||
ret = 0;
|
||||
goto end;
|
||||
}
|
||||
|
||||
if (!appname) {
|
||||
fprintf(stderr, "%s: option '-app' required\n", prog);
|
||||
goto end;
|
||||
}
|
||||
if (!container_name) {
|
||||
fprintf(stderr, "%s: option '-container' required\n", prog);
|
||||
goto end;
|
||||
}
|
||||
if (!pass) {
|
||||
fprintf(stderr, "%s: option '-pass' required\n", prog);
|
||||
goto end;
|
||||
}
|
||||
|
||||
if (op == OP_EXPORTPUBKEY) {
|
||||
if (skf_load_sign_key(&dev, appname, pass, container_name, &key) != 1) {
|
||||
fprintf(stderr, "%s: load sign key failed\n", prog);
|
||||
goto end;
|
||||
}
|
||||
if (sm2_public_key_info_to_pem(&(key.public_key), outfp) != 1) {
|
||||
fprintf(stderr, "%s: output public key PEM failure\n", prog);
|
||||
goto end;
|
||||
}
|
||||
ret = 0;
|
||||
goto end;
|
||||
}
|
||||
|
||||
if (op == OP_SIGN) {
|
||||
SM3_CTX sm3_ctx;
|
||||
uint8_t dgst[32];
|
||||
uint8_t sig[SM2_MAX_SIGNATURE_SIZE];
|
||||
size_t siglen;
|
||||
|
||||
if (skf_load_sign_key(&dev, appname, pass, container_name, &key) != 1) {
|
||||
fprintf(stderr, "%s: load sign key failed\n", prog);
|
||||
goto end;
|
||||
}
|
||||
key_opened = 1;
|
||||
|
||||
sm3_init(&sm3_ctx);
|
||||
sm2_compute_z(dgst, &(key.public_key.public_key), id, strlen(id));
|
||||
sm3_update(&sm3_ctx, dgst, sizeof(dgst));
|
||||
|
||||
while ((len = fread(buf, 1, sizeof(buf), infp)) > 0) {
|
||||
sm3_update(&sm3_ctx, buf, len);
|
||||
}
|
||||
sm3_finish(&sm3_ctx, dgst);
|
||||
|
||||
if ((ret = skf_sign(&key, dgst, sig, &siglen)) != 1) {
|
||||
fprintf(stderr, "%s: inner error\n", prog);
|
||||
goto end;
|
||||
}
|
||||
ret = 0;
|
||||
goto end;
|
||||
|
||||
} else {
|
||||
fprintf(stderr, "%s: this should not happen\n", prog);
|
||||
goto end;
|
||||
}
|
||||
|
||||
end:
|
||||
gmssl_secure_clear(buf, sizeof(buf));
|
||||
if (key_opened) skf_release_key(&key);
|
||||
if (dev_opened) skf_close_device(&dev);
|
||||
if (lib) skf_unload_library();
|
||||
if (infile && infp) fclose(infp);
|
||||
if (outfile && outfp) fclose(outfp);
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
#include <stdio.h>
|
||||
#include <errno.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <gmssl/mem.h>
|
||||
#include <gmssl/hex.h>
|
||||
#include <gmssl/sm2.h>
|
||||
#include <gmssl/sm3.h>
|
||||
#include <gmssl/skf.h>
|
||||
|
||||
|
||||
#define OP_NONE 0
|
||||
#define OP_DEVINFO 1
|
||||
#define OP_EXPORTPUBKEY 2
|
||||
#define OP_SIGN 3
|
||||
#define OP_RAND 4
|
||||
|
||||
|
||||
|
||||
static void print_usage(FILE *fp, const char *prog)
|
||||
{
|
||||
fprintf(fp, "usage:\n");
|
||||
fprintf(fp, " %s -lib so_path -dev str -devinfo\n", prog);
|
||||
fprintf(fp, " %s -lib so_path -dev str -app str [-pass str] -container str -exportpubkey [-out file]\n", prog);
|
||||
fprintf(fp, " %s -lib so_path -dev str -app str [-pass str] -container str -sign [-in file] [-out file]\n", prog);
|
||||
fprintf(fp, " %s -lib so_path -dev str -rand num [-out file]\n", prog);
|
||||
}
|
||||
|
||||
int skfutil_main(int argc, char **argv)
|
||||
{
|
||||
int ret = 1;
|
||||
char *prog = argv[0];
|
||||
char *lib = NULL;
|
||||
int op = 0;
|
||||
char *devname = NULL;
|
||||
char *authkeystr = NULL;
|
||||
char *appname = NULL;
|
||||
char *container_name = NULL;
|
||||
char *pass = NULL;
|
||||
char *id = SM2_DEFAULT_ID;
|
||||
int num = 0;
|
||||
char *infile = NULL;
|
||||
char *outfile = NULL;
|
||||
FILE *infp = stdin;
|
||||
FILE *outfp = stdout;
|
||||
unsigned char buf[4096];
|
||||
unsigned int ulen;
|
||||
int len;
|
||||
|
||||
uint8_t authkey[16];
|
||||
size_t authkeylen;
|
||||
SKF_DEVICE dev;
|
||||
SKF_KEY key;
|
||||
int dev_opened = 0;
|
||||
int key_opened = 0;
|
||||
|
||||
memset(&dev, 0, sizeof(dev));
|
||||
memset(&key, 0, sizeof(key));
|
||||
|
||||
argc--;
|
||||
argv++;
|
||||
|
||||
if (argc < 1) {
|
||||
print_usage(stderr, prog);
|
||||
return 1;
|
||||
}
|
||||
|
||||
while (argc > 0) {
|
||||
if (!strcmp(*argv, "-help")) {
|
||||
print_usage(stdout, prog);
|
||||
goto end;
|
||||
} else if (!strcmp(*argv, "-lib")) {
|
||||
if (--argc < 1) goto bad;
|
||||
lib = *(++argv);
|
||||
} else if (!strcmp(*argv, "-lib")) {
|
||||
if (--argc < 1) goto bad;
|
||||
devname = *(++argv);
|
||||
} else if (!strcmp(*argv, "-devinfo")) {
|
||||
op = OP_DEVINFO;
|
||||
} else if (!strcmp(*argv, "-dev")) {
|
||||
if (--argc < 1) goto bad;
|
||||
devname = *(++argv);
|
||||
} else if (!strcmp(*argv, "-authkey")) {
|
||||
if (--argc < 1) goto bad;
|
||||
authkeystr = *(++argv);
|
||||
if (strlen(authkeystr) != 32) {
|
||||
fprintf(stderr, "%s: invalid authkey length\n", prog);
|
||||
goto end;
|
||||
}
|
||||
hex_to_bytes(authkeystr, strlen(authkeystr), authkey, &authkeylen);
|
||||
} else if (!strcmp(*argv, "-exportpubkey")) {
|
||||
op = OP_EXPORTPUBKEY;
|
||||
} else if (!strcmp(*argv, "-sign")) {
|
||||
op = OP_SIGN;
|
||||
} else if (!strcmp(*argv, "-app")) {
|
||||
if (--argc < 1) goto bad;
|
||||
appname = *(++argv);
|
||||
} else if (!strcmp(*argv, "-container")) {
|
||||
if (--argc < 1) goto bad;
|
||||
container_name = *(++argv);
|
||||
} else if (!strcmp(*argv, "-pass")) {
|
||||
if (--argc < 1) goto bad;
|
||||
pass = *(++argv);
|
||||
} else if (!strcmp(*argv, "-id")) {
|
||||
if (--argc < 1) goto bad;
|
||||
id = *(++argv);
|
||||
} else if (!strcmp(*argv, "-rand")) {
|
||||
if (--argc < 1) goto bad;
|
||||
len = atoi(*(++argv));
|
||||
} else if (!strcmp(*argv, "-in")) {
|
||||
if (--argc < 1) goto bad;
|
||||
infile = *(++argv);
|
||||
if (!(infp = fopen(infile, "r"))) {
|
||||
fprintf(stderr, "%s: open '%s' failure : %s\n", prog, infile, strerror(errno));
|
||||
goto end;
|
||||
}
|
||||
} else if (!strcmp(*argv, "-out")) {
|
||||
if (--argc < 1) goto bad;
|
||||
outfile = *(++argv);
|
||||
if (!(outfp = fopen(outfile, "w"))) {
|
||||
fprintf(stderr, "%s: open '%s' failure : %s\n", prog, outfile, strerror(errno));
|
||||
goto end;
|
||||
}
|
||||
} else {
|
||||
fprintf(stderr, "%s: illegal option '%s'\n", prog, *argv);
|
||||
goto end;
|
||||
bad:
|
||||
fprintf(stderr, "%s: '%s' option value missing\n", prog, *argv);
|
||||
goto end;
|
||||
}
|
||||
|
||||
argc--;
|
||||
argv++;
|
||||
}
|
||||
|
||||
if (!lib) {
|
||||
fprintf(stderr, "%s: option '-lib' required\n", prog);
|
||||
goto end;
|
||||
}
|
||||
if (skf_load_library(lib, NULL) != 1) {
|
||||
fprintf(stderr, "%s: load library failure\n", prog);
|
||||
goto end;
|
||||
}
|
||||
|
||||
if (!op) {
|
||||
fprintf(stderr, "%s: option of (-devinfo|-exportpubkey|-sign|-rand) required\n", prog);
|
||||
goto end;
|
||||
}
|
||||
|
||||
if (!devname) {
|
||||
fprintf(stderr, "%s: option '-dev' required\n", prog);
|
||||
goto end;
|
||||
}
|
||||
if (op == OP_DEVINFO) {
|
||||
skf_print_device_info(stdout, 0, 0, devname);
|
||||
ret = 0;
|
||||
goto end;
|
||||
}
|
||||
|
||||
if (skf_open_device(&dev, devname, authkey) != 1) {
|
||||
fprintf(stderr, "%s: open device failure\n", prog);
|
||||
goto end;
|
||||
}
|
||||
dev_opened = 1;
|
||||
|
||||
if (op == OP_RAND) {
|
||||
if (skf_rand_bytes(&dev, buf, len) != 1) {
|
||||
fprintf(stderr, "%s: inner error\n", prog);
|
||||
goto end;
|
||||
}
|
||||
if (fwrite(buf, 1, len, outfp) != len) {
|
||||
fprintf(stderr, "%s: output failure : %s\n", prog, strerror(errno));
|
||||
goto end;
|
||||
}
|
||||
ret = 0;
|
||||
goto end;
|
||||
}
|
||||
|
||||
if (!appname) {
|
||||
fprintf(stderr, "%s: option '-app' required\n", prog);
|
||||
goto end;
|
||||
}
|
||||
if (!container_name) {
|
||||
fprintf(stderr, "%s: option '-container' required\n", prog);
|
||||
goto end;
|
||||
}
|
||||
if (!pass) {
|
||||
fprintf(stderr, "%s: option '-pass' required\n", prog);
|
||||
goto end;
|
||||
}
|
||||
|
||||
if (op == OP_EXPORTPUBKEY) {
|
||||
if (skf_load_sign_key(&dev, appname, pass, container_name, &key) != 1) {
|
||||
fprintf(stderr, "%s: load sign key failed\n", prog);
|
||||
goto end;
|
||||
}
|
||||
if (sm2_public_key_info_to_pem(&(key.public_key), outfp) != 1) {
|
||||
fprintf(stderr, "%s: output public key PEM failure\n", prog);
|
||||
goto end;
|
||||
}
|
||||
ret = 0;
|
||||
goto end;
|
||||
}
|
||||
|
||||
if (op == OP_SIGN) {
|
||||
SM3_CTX sm3_ctx;
|
||||
uint8_t dgst[32];
|
||||
uint8_t sig[SM2_MAX_SIGNATURE_SIZE];
|
||||
size_t siglen;
|
||||
|
||||
if (skf_load_sign_key(&dev, appname, pass, container_name, &key) != 1) {
|
||||
fprintf(stderr, "%s: load sign key failed\n", prog);
|
||||
goto end;
|
||||
}
|
||||
key_opened = 1;
|
||||
|
||||
sm3_init(&sm3_ctx);
|
||||
sm2_compute_z(dgst, &(key.public_key.public_key), id, strlen(id));
|
||||
sm3_update(&sm3_ctx, dgst, sizeof(dgst));
|
||||
|
||||
while ((len = fread(buf, 1, sizeof(buf), infp)) > 0) {
|
||||
sm3_update(&sm3_ctx, buf, len);
|
||||
}
|
||||
sm3_finish(&sm3_ctx, dgst);
|
||||
|
||||
if ((ret = skf_sign(&key, dgst, sig, &siglen)) != 1) {
|
||||
fprintf(stderr, "%s: inner error\n", prog);
|
||||
goto end;
|
||||
}
|
||||
ret = 0;
|
||||
goto end;
|
||||
|
||||
} else {
|
||||
fprintf(stderr, "%s: this should not happen\n", prog);
|
||||
goto end;
|
||||
}
|
||||
|
||||
end:
|
||||
gmssl_secure_clear(buf, sizeof(buf));
|
||||
if (key_opened) skf_release_key(&key);
|
||||
if (dev_opened) skf_close_device(&dev);
|
||||
if (lib) skf_unload_library();
|
||||
if (infile && infp) fclose(infp);
|
||||
if (outfile && outfp) fclose(outfp);
|
||||
return ret;
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2014-2022 The GmSSL Project. All Rights Reserved.
|
||||
* Copyright 2022 The GmSSL Project. All Rights Reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the License); you may
|
||||
* not use this file except in compliance with the License.
|
||||
@@ -7,114 +7,113 @@
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*/
|
||||
|
||||
|
||||
|
||||
#include <stdio.h>
|
||||
#include <errno.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <gmssl/mem.h>
|
||||
#include <gmssl/sm2.h>
|
||||
|
||||
|
||||
static const char *options = "-key pem -pass str [-in file] [-out file]";
|
||||
|
||||
int sm2decrypt_main(int argc, char **argv)
|
||||
{
|
||||
int ret = 1;
|
||||
char *prog = argv[0];
|
||||
char *keyfile = NULL;
|
||||
char *pass = NULL;
|
||||
char *infile = NULL;
|
||||
char *outfile = NULL;
|
||||
FILE *keyfp = NULL;
|
||||
FILE *infp = stdin;
|
||||
FILE *outfp = stdout;
|
||||
SM2_KEY key;
|
||||
uint8_t inbuf[SM2_MAX_CIPHERTEXT_SIZE];
|
||||
uint8_t outbuf[SM2_MAX_CIPHERTEXT_SIZE];
|
||||
size_t inlen, outlen;
|
||||
|
||||
argc--;
|
||||
argv++;
|
||||
|
||||
if (argc < 1) {
|
||||
fprintf(stderr, "usage: %s %s\n", prog, options);
|
||||
return 1;
|
||||
}
|
||||
|
||||
while (argc > 0) {
|
||||
if (!strcmp(*argv, "-help")) {
|
||||
printf("usage: %s %s\n", prog, options);
|
||||
ret = 0;
|
||||
goto end;
|
||||
} else if (!strcmp(*argv, "-key")) {
|
||||
if (--argc < 1) goto bad;
|
||||
keyfile = *(++argv);
|
||||
if (!(keyfp = fopen(keyfile, "r"))) {
|
||||
fprintf(stderr, "%s: open '%s' failure : %s\n", prog, keyfile, strerror(errno));
|
||||
goto end;
|
||||
}
|
||||
} else if (!strcmp(*argv, "-pass")) {
|
||||
if (--argc < 1) goto bad;
|
||||
pass = *(++argv);
|
||||
} else if (!strcmp(*argv, "-in")) {
|
||||
if (--argc < 1) goto bad;
|
||||
infile = *(++argv);
|
||||
if (!(infp = fopen(infile, "r"))) {
|
||||
fprintf(stderr, "%s: open '%s' failure : %s\n", prog, infile, strerror(errno));
|
||||
goto end;
|
||||
}
|
||||
} else if (!strcmp(*argv, "-out")) {
|
||||
if (--argc < 1) goto bad;
|
||||
outfile = *(++argv);
|
||||
if (!(outfp = fopen(outfile, "w"))) {
|
||||
fprintf(stderr, "%s: open '%s' failure : %s\n", prog, outfile, strerror(errno));
|
||||
goto end;
|
||||
}
|
||||
} else {
|
||||
fprintf(stderr, "%s: illegal option '%s'\n", prog, *argv);
|
||||
goto end;
|
||||
bad:
|
||||
fprintf(stderr, "%s: '%s' option value missing\n", prog, *argv);
|
||||
goto end;
|
||||
}
|
||||
|
||||
argc--;
|
||||
argv++;
|
||||
}
|
||||
|
||||
if (!keyfile) {
|
||||
fprintf(stderr, "%s: '-key' option required\n", prog);
|
||||
goto end;
|
||||
}
|
||||
if (!pass) {
|
||||
fprintf(stderr, "%s: '-pass' option required\n", prog);
|
||||
goto end;
|
||||
}
|
||||
|
||||
if (sm2_private_key_info_decrypt_from_pem(&key, pass, keyfp) != 1) {
|
||||
fprintf(stderr, "%s: private key decryption failure\n", prog);
|
||||
goto end;
|
||||
}
|
||||
|
||||
if ((inlen = fread(inbuf, 1, sizeof(inbuf), infp)) <= 0) {
|
||||
fprintf(stderr, "%s: read input failed : %s\n", prog, strerror(errno));
|
||||
goto end;
|
||||
}
|
||||
if (sm2_decrypt(&key, inbuf, inlen, outbuf, &outlen) != 1) {
|
||||
fprintf(stderr, "%s: decryption failure\n", prog);
|
||||
goto end;
|
||||
}
|
||||
if (outlen != fwrite(outbuf, 1, outlen, outfp)) {
|
||||
fprintf(stderr, "%s: output plaintext failed : %s\n", prog, strerror(errno));
|
||||
goto end;
|
||||
}
|
||||
ret = 0;
|
||||
end:
|
||||
gmssl_secure_clear(&key, sizeof(key));
|
||||
if (keyfp) fclose(keyfp);
|
||||
if (infile && infp) fclose(infp);
|
||||
if (outfile && outfp) fclose(outfp);
|
||||
return ret;
|
||||
}
|
||||
|
||||
#include <stdio.h>
|
||||
#include <errno.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <gmssl/mem.h>
|
||||
#include <gmssl/sm2.h>
|
||||
|
||||
|
||||
static const char *options = "-key pem -pass str [-in file] [-out file]";
|
||||
|
||||
int sm2decrypt_main(int argc, char **argv)
|
||||
{
|
||||
int ret = 1;
|
||||
char *prog = argv[0];
|
||||
char *keyfile = NULL;
|
||||
char *pass = NULL;
|
||||
char *infile = NULL;
|
||||
char *outfile = NULL;
|
||||
FILE *keyfp = NULL;
|
||||
FILE *infp = stdin;
|
||||
FILE *outfp = stdout;
|
||||
SM2_KEY key;
|
||||
uint8_t inbuf[SM2_MAX_CIPHERTEXT_SIZE];
|
||||
uint8_t outbuf[SM2_MAX_CIPHERTEXT_SIZE];
|
||||
size_t inlen, outlen;
|
||||
|
||||
argc--;
|
||||
argv++;
|
||||
|
||||
if (argc < 1) {
|
||||
fprintf(stderr, "usage: %s %s\n", prog, options);
|
||||
return 1;
|
||||
}
|
||||
|
||||
while (argc > 0) {
|
||||
if (!strcmp(*argv, "-help")) {
|
||||
printf("usage: %s %s\n", prog, options);
|
||||
ret = 0;
|
||||
goto end;
|
||||
} else if (!strcmp(*argv, "-key")) {
|
||||
if (--argc < 1) goto bad;
|
||||
keyfile = *(++argv);
|
||||
if (!(keyfp = fopen(keyfile, "r"))) {
|
||||
fprintf(stderr, "%s: open '%s' failure : %s\n", prog, keyfile, strerror(errno));
|
||||
goto end;
|
||||
}
|
||||
} else if (!strcmp(*argv, "-pass")) {
|
||||
if (--argc < 1) goto bad;
|
||||
pass = *(++argv);
|
||||
} else if (!strcmp(*argv, "-in")) {
|
||||
if (--argc < 1) goto bad;
|
||||
infile = *(++argv);
|
||||
if (!(infp = fopen(infile, "r"))) {
|
||||
fprintf(stderr, "%s: open '%s' failure : %s\n", prog, infile, strerror(errno));
|
||||
goto end;
|
||||
}
|
||||
} else if (!strcmp(*argv, "-out")) {
|
||||
if (--argc < 1) goto bad;
|
||||
outfile = *(++argv);
|
||||
if (!(outfp = fopen(outfile, "w"))) {
|
||||
fprintf(stderr, "%s: open '%s' failure : %s\n", prog, outfile, strerror(errno));
|
||||
goto end;
|
||||
}
|
||||
} else {
|
||||
fprintf(stderr, "%s: illegal option '%s'\n", prog, *argv);
|
||||
goto end;
|
||||
bad:
|
||||
fprintf(stderr, "%s: '%s' option value missing\n", prog, *argv);
|
||||
goto end;
|
||||
}
|
||||
|
||||
argc--;
|
||||
argv++;
|
||||
}
|
||||
|
||||
if (!keyfile) {
|
||||
fprintf(stderr, "%s: '-key' option required\n", prog);
|
||||
goto end;
|
||||
}
|
||||
if (!pass) {
|
||||
fprintf(stderr, "%s: '-pass' option required\n", prog);
|
||||
goto end;
|
||||
}
|
||||
|
||||
if (sm2_private_key_info_decrypt_from_pem(&key, pass, keyfp) != 1) {
|
||||
fprintf(stderr, "%s: private key decryption failure\n", prog);
|
||||
goto end;
|
||||
}
|
||||
|
||||
if ((inlen = fread(inbuf, 1, sizeof(inbuf), infp)) <= 0) {
|
||||
fprintf(stderr, "%s: read input failed : %s\n", prog, strerror(errno));
|
||||
goto end;
|
||||
}
|
||||
if (sm2_decrypt(&key, inbuf, inlen, outbuf, &outlen) != 1) {
|
||||
fprintf(stderr, "%s: decryption failure\n", prog);
|
||||
goto end;
|
||||
}
|
||||
if (outlen != fwrite(outbuf, 1, outlen, outfp)) {
|
||||
fprintf(stderr, "%s: output plaintext failed : %s\n", prog, strerror(errno));
|
||||
goto end;
|
||||
}
|
||||
ret = 0;
|
||||
end:
|
||||
gmssl_secure_clear(&key, sizeof(key));
|
||||
if (keyfp) fclose(keyfp);
|
||||
if (infile && infp) fclose(infp);
|
||||
if (outfile && outfp) fclose(outfp);
|
||||
return ret;
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2014-2022 The GmSSL Project. All Rights Reserved.
|
||||
* Copyright 2022 The GmSSL Project. All Rights Reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the License); you may
|
||||
* not use this file except in compliance with the License.
|
||||
@@ -7,139 +7,138 @@
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*/
|
||||
|
||||
|
||||
|
||||
#include <stdio.h>
|
||||
#include <errno.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <gmssl/sm2.h>
|
||||
#include <gmssl/x509.h>
|
||||
|
||||
|
||||
static const char *options = "(-pubkey pem | -cert pem) [-in file] [-out file]";
|
||||
|
||||
int sm2encrypt_main(int argc, char **argv)
|
||||
{
|
||||
int ret = 1;
|
||||
char *prog = argv[0];
|
||||
char *pubkeyfile = NULL;
|
||||
char *certfile = NULL;
|
||||
char *infile = NULL;
|
||||
char *outfile = NULL;
|
||||
FILE *pubkeyfp = NULL;
|
||||
FILE *certfp = NULL;
|
||||
FILE *infp = stdin;
|
||||
FILE *outfp = stdout;
|
||||
uint8_t cert[1024];
|
||||
size_t certlen;
|
||||
SM2_KEY key;
|
||||
uint8_t inbuf[SM2_MAX_PLAINTEXT_SIZE + 1];
|
||||
uint8_t outbuf[SM2_MAX_CIPHERTEXT_SIZE];
|
||||
size_t inlen, outlen = sizeof(outbuf);
|
||||
|
||||
argc--;
|
||||
argv++;
|
||||
|
||||
if (argc < 1) {
|
||||
fprintf(stderr, "usage: %s %s\n", prog, options);
|
||||
return 1;
|
||||
}
|
||||
|
||||
while (argc > 1) {
|
||||
if (!strcmp(*argv, "-help")) {
|
||||
printf("usage: %s %s\n", prog, options);
|
||||
ret = 0;
|
||||
goto end;
|
||||
} else if (!strcmp(*argv, "-pubkey")) {
|
||||
if (certfile) {
|
||||
fprintf(stderr, "%s: options '-pubkey' '-cert' conflict\n", prog);
|
||||
goto end;
|
||||
}
|
||||
if (--argc < 1) goto bad;
|
||||
pubkeyfile = *(++argv);
|
||||
if (!(pubkeyfp = fopen(pubkeyfile, "r"))) {
|
||||
fprintf(stderr, "%s: open '%s' failure : %s\n", prog, pubkeyfile, strerror(errno));
|
||||
goto end;
|
||||
}
|
||||
} else if (!strcmp(*argv, "-cert")) {
|
||||
if (pubkeyfile) {
|
||||
fprintf(stderr, "%s: options '-pubkey' '-cert' conflict\n", prog);
|
||||
goto end;
|
||||
}
|
||||
if (--argc < 1) goto bad;
|
||||
certfile = *(++argv);
|
||||
if (!(certfp = fopen(certfile, "r"))) {
|
||||
fprintf(stderr, "%s: open '%s' failure : %s\n", prog, certfile, strerror(errno));
|
||||
goto end;
|
||||
}
|
||||
} else if (!strcmp(*argv, "-in")) {
|
||||
if (--argc < 1) goto bad;
|
||||
infile = *(++argv);
|
||||
if (!(infp = fopen(infile, "r"))) {
|
||||
fprintf(stderr, "%s: open '%s' failure : %s\n", prog, infile, strerror(errno));
|
||||
goto end;
|
||||
}
|
||||
} else if (!strcmp(*argv, "-out")) {
|
||||
if (--argc < 1) goto bad;
|
||||
outfile = *(++argv);
|
||||
if (!(outfp = fopen(outfile, "w"))) {
|
||||
fprintf(stderr, "%s: open '%s' failure : %s\n", prog, outfile, strerror(errno));
|
||||
goto end;
|
||||
}
|
||||
} else {
|
||||
fprintf(stderr, "%s: illegal option '%s'\n", prog, *argv);
|
||||
goto end;
|
||||
bad:
|
||||
fprintf(stderr, "%s: '%s' option value missing\n", prog, *argv);
|
||||
goto end;
|
||||
}
|
||||
|
||||
argc--;
|
||||
argv++;
|
||||
}
|
||||
|
||||
|
||||
if (pubkeyfile) {
|
||||
if (sm2_public_key_info_from_pem(&key, pubkeyfp) != 1) {
|
||||
fprintf(stderr, "%s: parse public key failed\n", prog);
|
||||
goto end;
|
||||
}
|
||||
} else if (certfile) {
|
||||
if (x509_cert_from_pem(cert, &certlen, sizeof(cert), certfp) != 1
|
||||
|| x509_cert_get_subject_public_key(cert, certlen, &key) != 1) {
|
||||
fprintf(stderr, "%s: parse certificate failed\n", prog);
|
||||
goto end;
|
||||
}
|
||||
} else {
|
||||
fprintf(stderr, "%s: '-pubkey' or '-cert' option required\n", prog);
|
||||
goto end;
|
||||
}
|
||||
|
||||
if ((inlen = fread(inbuf, 1, sizeof(inbuf), infp)) <= 0) {
|
||||
fprintf(stderr, "%s: read input error : %s\n", prog, strerror(errno));
|
||||
goto end;
|
||||
}
|
||||
if (inlen > SM2_MAX_PLAINTEXT_SIZE) {
|
||||
fprintf(stderr, "%s: input long than SM2_MAX_PLAINTEXT_SIZE (%d)\n", prog, SM2_MAX_PLAINTEXT_SIZE);
|
||||
goto end;
|
||||
}
|
||||
|
||||
if (sm2_encrypt(&key, inbuf, inlen, outbuf, &outlen) != 1) {
|
||||
fprintf(stderr, "%s: inner error\n", prog);
|
||||
goto end;
|
||||
}
|
||||
|
||||
if (outlen != fwrite(outbuf, 1, outlen, outfp)) {
|
||||
fprintf(stderr, "%s: output error : %s\n", prog, strerror(errno));
|
||||
goto end;
|
||||
}
|
||||
ret = 0;
|
||||
|
||||
end:
|
||||
if (infile && infp) fclose(infp);
|
||||
if (outfile && outfp) fclose(outfp);
|
||||
if (pubkeyfp) fclose(pubkeyfp);
|
||||
if (certfp) fclose(certfp);
|
||||
return ret;
|
||||
}
|
||||
|
||||
#include <stdio.h>
|
||||
#include <errno.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <gmssl/sm2.h>
|
||||
#include <gmssl/x509.h>
|
||||
|
||||
|
||||
static const char *options = "(-pubkey pem | -cert pem) [-in file] [-out file]";
|
||||
|
||||
int sm2encrypt_main(int argc, char **argv)
|
||||
{
|
||||
int ret = 1;
|
||||
char *prog = argv[0];
|
||||
char *pubkeyfile = NULL;
|
||||
char *certfile = NULL;
|
||||
char *infile = NULL;
|
||||
char *outfile = NULL;
|
||||
FILE *pubkeyfp = NULL;
|
||||
FILE *certfp = NULL;
|
||||
FILE *infp = stdin;
|
||||
FILE *outfp = stdout;
|
||||
uint8_t cert[1024];
|
||||
size_t certlen;
|
||||
SM2_KEY key;
|
||||
uint8_t inbuf[SM2_MAX_PLAINTEXT_SIZE + 1];
|
||||
uint8_t outbuf[SM2_MAX_CIPHERTEXT_SIZE];
|
||||
size_t inlen, outlen = sizeof(outbuf);
|
||||
|
||||
argc--;
|
||||
argv++;
|
||||
|
||||
if (argc < 1) {
|
||||
fprintf(stderr, "usage: %s %s\n", prog, options);
|
||||
return 1;
|
||||
}
|
||||
|
||||
while (argc > 1) {
|
||||
if (!strcmp(*argv, "-help")) {
|
||||
printf("usage: %s %s\n", prog, options);
|
||||
ret = 0;
|
||||
goto end;
|
||||
} else if (!strcmp(*argv, "-pubkey")) {
|
||||
if (certfile) {
|
||||
fprintf(stderr, "%s: options '-pubkey' '-cert' conflict\n", prog);
|
||||
goto end;
|
||||
}
|
||||
if (--argc < 1) goto bad;
|
||||
pubkeyfile = *(++argv);
|
||||
if (!(pubkeyfp = fopen(pubkeyfile, "r"))) {
|
||||
fprintf(stderr, "%s: open '%s' failure : %s\n", prog, pubkeyfile, strerror(errno));
|
||||
goto end;
|
||||
}
|
||||
} else if (!strcmp(*argv, "-cert")) {
|
||||
if (pubkeyfile) {
|
||||
fprintf(stderr, "%s: options '-pubkey' '-cert' conflict\n", prog);
|
||||
goto end;
|
||||
}
|
||||
if (--argc < 1) goto bad;
|
||||
certfile = *(++argv);
|
||||
if (!(certfp = fopen(certfile, "r"))) {
|
||||
fprintf(stderr, "%s: open '%s' failure : %s\n", prog, certfile, strerror(errno));
|
||||
goto end;
|
||||
}
|
||||
} else if (!strcmp(*argv, "-in")) {
|
||||
if (--argc < 1) goto bad;
|
||||
infile = *(++argv);
|
||||
if (!(infp = fopen(infile, "r"))) {
|
||||
fprintf(stderr, "%s: open '%s' failure : %s\n", prog, infile, strerror(errno));
|
||||
goto end;
|
||||
}
|
||||
} else if (!strcmp(*argv, "-out")) {
|
||||
if (--argc < 1) goto bad;
|
||||
outfile = *(++argv);
|
||||
if (!(outfp = fopen(outfile, "w"))) {
|
||||
fprintf(stderr, "%s: open '%s' failure : %s\n", prog, outfile, strerror(errno));
|
||||
goto end;
|
||||
}
|
||||
} else {
|
||||
fprintf(stderr, "%s: illegal option '%s'\n", prog, *argv);
|
||||
goto end;
|
||||
bad:
|
||||
fprintf(stderr, "%s: '%s' option value missing\n", prog, *argv);
|
||||
goto end;
|
||||
}
|
||||
|
||||
argc--;
|
||||
argv++;
|
||||
}
|
||||
|
||||
|
||||
if (pubkeyfile) {
|
||||
if (sm2_public_key_info_from_pem(&key, pubkeyfp) != 1) {
|
||||
fprintf(stderr, "%s: parse public key failed\n", prog);
|
||||
goto end;
|
||||
}
|
||||
} else if (certfile) {
|
||||
if (x509_cert_from_pem(cert, &certlen, sizeof(cert), certfp) != 1
|
||||
|| x509_cert_get_subject_public_key(cert, certlen, &key) != 1) {
|
||||
fprintf(stderr, "%s: parse certificate failed\n", prog);
|
||||
goto end;
|
||||
}
|
||||
} else {
|
||||
fprintf(stderr, "%s: '-pubkey' or '-cert' option required\n", prog);
|
||||
goto end;
|
||||
}
|
||||
|
||||
if ((inlen = fread(inbuf, 1, sizeof(inbuf), infp)) <= 0) {
|
||||
fprintf(stderr, "%s: read input error : %s\n", prog, strerror(errno));
|
||||
goto end;
|
||||
}
|
||||
if (inlen > SM2_MAX_PLAINTEXT_SIZE) {
|
||||
fprintf(stderr, "%s: input long than SM2_MAX_PLAINTEXT_SIZE (%d)\n", prog, SM2_MAX_PLAINTEXT_SIZE);
|
||||
goto end;
|
||||
}
|
||||
|
||||
if (sm2_encrypt(&key, inbuf, inlen, outbuf, &outlen) != 1) {
|
||||
fprintf(stderr, "%s: inner error\n", prog);
|
||||
goto end;
|
||||
}
|
||||
|
||||
if (outlen != fwrite(outbuf, 1, outlen, outfp)) {
|
||||
fprintf(stderr, "%s: output error : %s\n", prog, strerror(errno));
|
||||
goto end;
|
||||
}
|
||||
ret = 0;
|
||||
|
||||
end:
|
||||
if (infile && infp) fclose(infp);
|
||||
if (outfile && outfp) fclose(outfp);
|
||||
if (pubkeyfp) fclose(pubkeyfp);
|
||||
if (certfp) fclose(certfp);
|
||||
return ret;
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2014-2022 The GmSSL Project. All Rights Reserved.
|
||||
* Copyright 2022 The GmSSL Project. All Rights Reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the License); you may
|
||||
* not use this file except in compliance with the License.
|
||||
@@ -7,87 +7,86 @@
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*/
|
||||
|
||||
|
||||
|
||||
#include <stdio.h>
|
||||
#include <errno.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <gmssl/mem.h>
|
||||
#include <gmssl/sm2.h>
|
||||
|
||||
|
||||
static const char *options = "-pass str [-out pem] [-pubout pem]";
|
||||
|
||||
int sm2keygen_main(int argc, char **argv)
|
||||
{
|
||||
int ret = 1;
|
||||
char *prog = argv[0];
|
||||
char *pass = NULL;
|
||||
char *outfile = NULL;
|
||||
char *puboutfile = NULL;
|
||||
FILE *outfp = stdout;
|
||||
FILE *puboutfp = stdout;
|
||||
SM2_KEY key;
|
||||
|
||||
argc--;
|
||||
argv++;
|
||||
|
||||
if (argc < 1) {
|
||||
fprintf(stderr, "usage: %s %s\n", prog, options);
|
||||
return 1;
|
||||
}
|
||||
|
||||
while (argc > 0) {
|
||||
if (!strcmp(*argv, "-help")) {
|
||||
printf("usage: %s %s\n", prog, options);
|
||||
ret = 0;
|
||||
goto end;
|
||||
} else if (!strcmp(*argv, "-pass")) {
|
||||
if (--argc < 1) goto bad;
|
||||
pass = *(++argv);
|
||||
} else if (!strcmp(*argv, "-out")) {
|
||||
if (--argc < 1) goto bad;
|
||||
outfile = *(++argv);
|
||||
if (!(outfp = fopen(outfile, "w"))) {
|
||||
fprintf(stderr, "%s: open '%s' failure : %s\n", prog, outfile, strerror(errno));
|
||||
goto end;
|
||||
}
|
||||
} else if (!strcmp(*argv, "-pubout")) {
|
||||
if (--argc < 1) goto bad;
|
||||
puboutfile = *(++argv);
|
||||
if (!(puboutfp = fopen(puboutfile, "w"))) {
|
||||
fprintf(stderr, "%s: open '%s' failure : %s\n", prog, outfile, strerror(errno));
|
||||
goto end;
|
||||
}
|
||||
} else {
|
||||
fprintf(stderr, "%s: illegal option '%s'\n", prog, *argv);
|
||||
goto end;
|
||||
bad:
|
||||
fprintf(stderr, "%s: '%s' option value missing\n", prog, *argv);
|
||||
goto end;
|
||||
}
|
||||
|
||||
argc--;
|
||||
argv++;
|
||||
}
|
||||
|
||||
if (!pass) {
|
||||
fprintf(stderr, "%s: '-pass' option required\n", prog);
|
||||
goto end;
|
||||
}
|
||||
|
||||
if (sm2_key_generate(&key) != 1
|
||||
|| sm2_private_key_info_encrypt_to_pem(&key, pass, outfp) != 1
|
||||
|| sm2_public_key_info_to_pem(&key, puboutfp) != 1) {
|
||||
fprintf(stderr, "%s: inner failure\n", prog);
|
||||
goto end;
|
||||
}
|
||||
ret = 0;
|
||||
|
||||
end:
|
||||
gmssl_secure_clear(&key, sizeof(key));
|
||||
if (outfile && outfp) fclose(outfp);
|
||||
if (puboutfile && puboutfp) fclose(puboutfp);
|
||||
return ret;
|
||||
}
|
||||
|
||||
#include <stdio.h>
|
||||
#include <errno.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <gmssl/mem.h>
|
||||
#include <gmssl/sm2.h>
|
||||
|
||||
|
||||
static const char *options = "-pass str [-out pem] [-pubout pem]";
|
||||
|
||||
int sm2keygen_main(int argc, char **argv)
|
||||
{
|
||||
int ret = 1;
|
||||
char *prog = argv[0];
|
||||
char *pass = NULL;
|
||||
char *outfile = NULL;
|
||||
char *puboutfile = NULL;
|
||||
FILE *outfp = stdout;
|
||||
FILE *puboutfp = stdout;
|
||||
SM2_KEY key;
|
||||
|
||||
argc--;
|
||||
argv++;
|
||||
|
||||
if (argc < 1) {
|
||||
fprintf(stderr, "usage: %s %s\n", prog, options);
|
||||
return 1;
|
||||
}
|
||||
|
||||
while (argc > 0) {
|
||||
if (!strcmp(*argv, "-help")) {
|
||||
printf("usage: %s %s\n", prog, options);
|
||||
ret = 0;
|
||||
goto end;
|
||||
} else if (!strcmp(*argv, "-pass")) {
|
||||
if (--argc < 1) goto bad;
|
||||
pass = *(++argv);
|
||||
} else if (!strcmp(*argv, "-out")) {
|
||||
if (--argc < 1) goto bad;
|
||||
outfile = *(++argv);
|
||||
if (!(outfp = fopen(outfile, "w"))) {
|
||||
fprintf(stderr, "%s: open '%s' failure : %s\n", prog, outfile, strerror(errno));
|
||||
goto end;
|
||||
}
|
||||
} else if (!strcmp(*argv, "-pubout")) {
|
||||
if (--argc < 1) goto bad;
|
||||
puboutfile = *(++argv);
|
||||
if (!(puboutfp = fopen(puboutfile, "w"))) {
|
||||
fprintf(stderr, "%s: open '%s' failure : %s\n", prog, outfile, strerror(errno));
|
||||
goto end;
|
||||
}
|
||||
} else {
|
||||
fprintf(stderr, "%s: illegal option '%s'\n", prog, *argv);
|
||||
goto end;
|
||||
bad:
|
||||
fprintf(stderr, "%s: '%s' option value missing\n", prog, *argv);
|
||||
goto end;
|
||||
}
|
||||
|
||||
argc--;
|
||||
argv++;
|
||||
}
|
||||
|
||||
if (!pass) {
|
||||
fprintf(stderr, "%s: '-pass' option required\n", prog);
|
||||
goto end;
|
||||
}
|
||||
|
||||
if (sm2_key_generate(&key) != 1
|
||||
|| sm2_private_key_info_encrypt_to_pem(&key, pass, outfp) != 1
|
||||
|| sm2_public_key_info_to_pem(&key, puboutfp) != 1) {
|
||||
fprintf(stderr, "%s: inner failure\n", prog);
|
||||
goto end;
|
||||
}
|
||||
ret = 0;
|
||||
|
||||
end:
|
||||
gmssl_secure_clear(&key, sizeof(key));
|
||||
if (outfile && outfp) fclose(outfp);
|
||||
if (puboutfile && puboutfp) fclose(puboutfp);
|
||||
return ret;
|
||||
}
|
||||
|
||||
247
tools/sm2sign.c
247
tools/sm2sign.c
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2014-2022 The GmSSL Project. All Rights Reserved.
|
||||
* Copyright 2022 The GmSSL Project. All Rights Reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the License); you may
|
||||
* not use this file except in compliance with the License.
|
||||
@@ -7,126 +7,125 @@
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*/
|
||||
|
||||
|
||||
|
||||
#include <stdio.h>
|
||||
#include <errno.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <gmssl/sm2.h>
|
||||
#include <gmssl/mem.h>
|
||||
|
||||
|
||||
static const char *options = "-key pem -pass str [-id str] [-in file] [-out file]";
|
||||
|
||||
int sm2sign_main(int argc, char **argv)
|
||||
{
|
||||
int ret = 1;
|
||||
char *prog = argv[0];
|
||||
char *keyfile = NULL;
|
||||
char *pass = NULL;
|
||||
char *id = SM2_DEFAULT_ID;
|
||||
char *infile = NULL;
|
||||
char *outfile = NULL;
|
||||
FILE *keyfp = NULL;
|
||||
FILE *infp = stdin;
|
||||
FILE *outfp = stdout;
|
||||
SM2_KEY key;
|
||||
SM2_SIGN_CTX sign_ctx;
|
||||
uint8_t buf[4096];
|
||||
ssize_t len;
|
||||
uint8_t sig[SM2_MAX_SIGNATURE_SIZE];
|
||||
size_t siglen;
|
||||
|
||||
argc--;
|
||||
argv++;
|
||||
|
||||
if (argc < 1) {
|
||||
fprintf(stderr, "usage: %s %s\n", prog, options);
|
||||
return 1;
|
||||
}
|
||||
|
||||
while (argc > 0) {
|
||||
if (!strcmp(*argv, "-help")) {
|
||||
printf("usage: %s %s\n", prog, options);
|
||||
ret = 0;
|
||||
goto end;
|
||||
} else if (!strcmp(*argv, "-key")) {
|
||||
if (--argc < 1) goto bad;
|
||||
keyfile = *(++argv);
|
||||
if (!(keyfp = fopen(keyfile, "r"))) {
|
||||
fprintf(stderr, "%s: open '%s' failure : %s\n", prog, keyfile, strerror(errno));
|
||||
goto end;
|
||||
}
|
||||
} else if (!strcmp(*argv, "-pass")) {
|
||||
if (--argc < 1) goto bad;
|
||||
pass = *(++argv);
|
||||
} else if (!strcmp(*argv, "-id")) {
|
||||
if (--argc < 1) goto bad;
|
||||
id = *(++argv);
|
||||
} else if (!strcmp(*argv, "-in")) {
|
||||
if (--argc < 1) goto bad;
|
||||
infile = *(++argv);
|
||||
if (!(infp = fopen(infile, "r"))) {
|
||||
fprintf(stderr, "%s: open '%s' failure : %s\n", prog, infile, strerror(errno));
|
||||
goto end;
|
||||
}
|
||||
} else if (!strcmp(*argv, "-out")) {
|
||||
if (--argc < 1) goto bad;
|
||||
outfile = *(++argv);
|
||||
if (!(outfp = fopen(outfile, "w"))) {
|
||||
fprintf(stderr, "%s: open '%s' failure : %s\n", prog, outfile, strerror(errno));
|
||||
goto end;
|
||||
}
|
||||
} else {
|
||||
fprintf(stderr, "%s: illegal option '%s'\n", prog, *argv);
|
||||
goto end;
|
||||
bad:
|
||||
fprintf(stderr, "%s: '%s' option value missing\n", prog, *argv);
|
||||
goto end;
|
||||
}
|
||||
|
||||
argc--;
|
||||
argv++;
|
||||
}
|
||||
|
||||
if (!keyfile) {
|
||||
fprintf(stderr, "%s: '-key' option required\n", prog);
|
||||
goto end;
|
||||
}
|
||||
if (!pass) {
|
||||
fprintf(stderr, "%s: '-pass' option required\n", prog);
|
||||
goto end;
|
||||
}
|
||||
if (sm2_private_key_info_decrypt_from_pem(&key, pass, keyfp) != 1) {
|
||||
fprintf(stderr, "%s: private key decryption failure\n", prog);
|
||||
goto end;
|
||||
}
|
||||
|
||||
if (sm2_sign_init(&sign_ctx, &key, id, strlen(id)) != 1) {
|
||||
fprintf(stderr, "%s: inner error\n", prog);
|
||||
goto end;
|
||||
}
|
||||
while ((len = fread(buf, 1, sizeof(buf), infp)) > 0) {
|
||||
if (sm2_sign_update(&sign_ctx, buf, len) != 1) {
|
||||
fprintf(stderr, "%s: inner error\n", prog);
|
||||
goto end;
|
||||
}
|
||||
}
|
||||
if (sm2_sign_finish(&sign_ctx, sig, &siglen) != 1) {
|
||||
fprintf(stderr, "%s: inner error\n", prog);
|
||||
goto end;
|
||||
}
|
||||
if (fwrite(sig, 1, siglen, outfp) != siglen) {
|
||||
fprintf(stderr, "%s: output signature failed : %s\n", prog, strerror(errno));
|
||||
goto end;
|
||||
}
|
||||
ret = 0;
|
||||
end:
|
||||
gmssl_secure_clear(&key, sizeof(key));
|
||||
gmssl_secure_clear(&sign_ctx, sizeof(sign_ctx));
|
||||
if (keyfp) fclose(keyfp);
|
||||
if (infile && infp) fclose(infp);
|
||||
if (outfile && outfp) fclose(outfp);
|
||||
return ret;
|
||||
}
|
||||
|
||||
#include <stdio.h>
|
||||
#include <errno.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <gmssl/sm2.h>
|
||||
#include <gmssl/mem.h>
|
||||
|
||||
|
||||
static const char *options = "-key pem -pass str [-id str] [-in file] [-out file]";
|
||||
|
||||
int sm2sign_main(int argc, char **argv)
|
||||
{
|
||||
int ret = 1;
|
||||
char *prog = argv[0];
|
||||
char *keyfile = NULL;
|
||||
char *pass = NULL;
|
||||
char *id = SM2_DEFAULT_ID;
|
||||
char *infile = NULL;
|
||||
char *outfile = NULL;
|
||||
FILE *keyfp = NULL;
|
||||
FILE *infp = stdin;
|
||||
FILE *outfp = stdout;
|
||||
SM2_KEY key;
|
||||
SM2_SIGN_CTX sign_ctx;
|
||||
uint8_t buf[4096];
|
||||
ssize_t len;
|
||||
uint8_t sig[SM2_MAX_SIGNATURE_SIZE];
|
||||
size_t siglen;
|
||||
|
||||
argc--;
|
||||
argv++;
|
||||
|
||||
if (argc < 1) {
|
||||
fprintf(stderr, "usage: %s %s\n", prog, options);
|
||||
return 1;
|
||||
}
|
||||
|
||||
while (argc > 0) {
|
||||
if (!strcmp(*argv, "-help")) {
|
||||
printf("usage: %s %s\n", prog, options);
|
||||
ret = 0;
|
||||
goto end;
|
||||
} else if (!strcmp(*argv, "-key")) {
|
||||
if (--argc < 1) goto bad;
|
||||
keyfile = *(++argv);
|
||||
if (!(keyfp = fopen(keyfile, "r"))) {
|
||||
fprintf(stderr, "%s: open '%s' failure : %s\n", prog, keyfile, strerror(errno));
|
||||
goto end;
|
||||
}
|
||||
} else if (!strcmp(*argv, "-pass")) {
|
||||
if (--argc < 1) goto bad;
|
||||
pass = *(++argv);
|
||||
} else if (!strcmp(*argv, "-id")) {
|
||||
if (--argc < 1) goto bad;
|
||||
id = *(++argv);
|
||||
} else if (!strcmp(*argv, "-in")) {
|
||||
if (--argc < 1) goto bad;
|
||||
infile = *(++argv);
|
||||
if (!(infp = fopen(infile, "r"))) {
|
||||
fprintf(stderr, "%s: open '%s' failure : %s\n", prog, infile, strerror(errno));
|
||||
goto end;
|
||||
}
|
||||
} else if (!strcmp(*argv, "-out")) {
|
||||
if (--argc < 1) goto bad;
|
||||
outfile = *(++argv);
|
||||
if (!(outfp = fopen(outfile, "w"))) {
|
||||
fprintf(stderr, "%s: open '%s' failure : %s\n", prog, outfile, strerror(errno));
|
||||
goto end;
|
||||
}
|
||||
} else {
|
||||
fprintf(stderr, "%s: illegal option '%s'\n", prog, *argv);
|
||||
goto end;
|
||||
bad:
|
||||
fprintf(stderr, "%s: '%s' option value missing\n", prog, *argv);
|
||||
goto end;
|
||||
}
|
||||
|
||||
argc--;
|
||||
argv++;
|
||||
}
|
||||
|
||||
if (!keyfile) {
|
||||
fprintf(stderr, "%s: '-key' option required\n", prog);
|
||||
goto end;
|
||||
}
|
||||
if (!pass) {
|
||||
fprintf(stderr, "%s: '-pass' option required\n", prog);
|
||||
goto end;
|
||||
}
|
||||
if (sm2_private_key_info_decrypt_from_pem(&key, pass, keyfp) != 1) {
|
||||
fprintf(stderr, "%s: private key decryption failure\n", prog);
|
||||
goto end;
|
||||
}
|
||||
|
||||
if (sm2_sign_init(&sign_ctx, &key, id, strlen(id)) != 1) {
|
||||
fprintf(stderr, "%s: inner error\n", prog);
|
||||
goto end;
|
||||
}
|
||||
while ((len = fread(buf, 1, sizeof(buf), infp)) > 0) {
|
||||
if (sm2_sign_update(&sign_ctx, buf, len) != 1) {
|
||||
fprintf(stderr, "%s: inner error\n", prog);
|
||||
goto end;
|
||||
}
|
||||
}
|
||||
if (sm2_sign_finish(&sign_ctx, sig, &siglen) != 1) {
|
||||
fprintf(stderr, "%s: inner error\n", prog);
|
||||
goto end;
|
||||
}
|
||||
if (fwrite(sig, 1, siglen, outfp) != siglen) {
|
||||
fprintf(stderr, "%s: output signature failed : %s\n", prog, strerror(errno));
|
||||
goto end;
|
||||
}
|
||||
ret = 0;
|
||||
end:
|
||||
gmssl_secure_clear(&key, sizeof(key));
|
||||
gmssl_secure_clear(&sign_ctx, sizeof(sign_ctx));
|
||||
if (keyfp) fclose(keyfp);
|
||||
if (infile && infp) fclose(infp);
|
||||
if (outfile && outfp) fclose(outfp);
|
||||
return ret;
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2014-2022 The GmSSL Project. All Rights Reserved.
|
||||
* Copyright 2022 The GmSSL Project. All Rights Reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the License); you may
|
||||
* not use this file except in compliance with the License.
|
||||
@@ -7,156 +7,155 @@
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*/
|
||||
|
||||
|
||||
|
||||
#include <stdio.h>
|
||||
#include <errno.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <gmssl/sm2.h>
|
||||
#include <gmssl/x509.h>
|
||||
|
||||
|
||||
static const char *options = "(-pubkey pem | -cert pem) [-id str] [-in file] -sig file";
|
||||
|
||||
int sm2verify_main(int argc, char **argv)
|
||||
{
|
||||
int ret = 1;
|
||||
char *prog = argv[0];
|
||||
char *id = SM2_DEFAULT_ID;
|
||||
char *pubkeyfile = NULL;
|
||||
char *certfile = NULL;
|
||||
char *infile = NULL;
|
||||
char *sigfile = NULL;
|
||||
FILE *pubkeyfp = NULL;
|
||||
FILE *certfp = NULL;
|
||||
FILE *infp = stdin;
|
||||
FILE *sigfp = NULL;
|
||||
SM2_KEY key;
|
||||
SM2_SIGN_CTX verify_ctx;
|
||||
uint8_t cert[1024];
|
||||
size_t certlen;
|
||||
uint8_t buf[4096];
|
||||
ssize_t len;
|
||||
uint8_t sig[SM2_MAX_SIGNATURE_SIZE];
|
||||
size_t siglen;
|
||||
int vr;
|
||||
|
||||
argc--;
|
||||
argv++;
|
||||
|
||||
if (argc < 1) {
|
||||
fprintf(stderr, "usage: %s %s\n", prog, options);
|
||||
return 1;
|
||||
}
|
||||
|
||||
while (argc > 0) {
|
||||
if (!strcmp(*argv, "-help")) {
|
||||
printf("usage: %s %s\n", prog, options);
|
||||
ret = 0;
|
||||
goto end;
|
||||
} else if (!strcmp(*argv, "-pubkey")) {
|
||||
if (certfile) {
|
||||
fprintf(stderr, "%s: options '-pubkey' '-cert' conflict\n", prog);
|
||||
goto end;
|
||||
}
|
||||
if (--argc < 1) goto bad;
|
||||
pubkeyfile = *(++argv);
|
||||
if (!(pubkeyfp = fopen(pubkeyfile, "r"))) {
|
||||
fprintf(stderr, "%s: open '%s' failure : %s\n", prog, pubkeyfile, strerror(errno));
|
||||
goto end;
|
||||
}
|
||||
} else if (!strcmp(*argv, "-cert")) {
|
||||
if (pubkeyfile) {
|
||||
fprintf(stderr, "%s: options '-pubkey' '-cert' conflict\n", prog);
|
||||
goto end;
|
||||
}
|
||||
if (--argc < 1) goto bad;
|
||||
certfile = *(++argv);
|
||||
if (!(certfp = fopen(certfile, "r"))) {
|
||||
fprintf(stderr, "%s: open '%s' failure : %s\n", prog, certfile, strerror(errno));
|
||||
goto end;
|
||||
}
|
||||
} else if (!strcmp(*argv, "-id")) {
|
||||
if (--argc < 1) goto bad;
|
||||
id = *(++argv);
|
||||
} else if (!strcmp(*argv, "-in")) {
|
||||
if (--argc < 1) goto bad;
|
||||
infile = *(++argv);
|
||||
if (!(infp = fopen(infile, "r"))) {
|
||||
fprintf(stderr, "%s: open '%s' failure : %s\n", prog, infile, strerror(errno));
|
||||
goto end;
|
||||
}
|
||||
} else if (!strcmp(*argv, "-sig")) {
|
||||
if (--argc < 1) goto bad;
|
||||
sigfile = *(++argv);
|
||||
if (!(sigfp = fopen(sigfile, "r"))) {
|
||||
fprintf(stderr, "%s: open '%s' failure : %s\n", prog, sigfile, strerror(errno));
|
||||
goto end;
|
||||
}
|
||||
|
||||
} else {
|
||||
fprintf(stderr, "%s: illegal option '%s'\n", prog, *argv);
|
||||
goto end;
|
||||
bad:
|
||||
fprintf(stderr, "%s: '%s' option value missing\n", prog, *argv);
|
||||
goto end;
|
||||
}
|
||||
|
||||
argc--;
|
||||
argv++;
|
||||
}
|
||||
|
||||
if (!sigfile) {
|
||||
fprintf(stderr, "%s: '-sig' option required\n", prog);
|
||||
goto end;
|
||||
}
|
||||
if ((siglen = fread(sig, 1, sizeof(sig), sigfp)) <= 0) {
|
||||
fprintf(stderr, "%s: read signature error : %s\n", prog, strerror(errno));
|
||||
goto end;
|
||||
}
|
||||
|
||||
if (pubkeyfile) {
|
||||
if (sm2_public_key_info_from_pem(&key, pubkeyfp) != 1) {
|
||||
fprintf(stderr, "%s: parse public key failed\n", prog);
|
||||
goto end;
|
||||
}
|
||||
} else if (certfile) {
|
||||
if (x509_cert_from_pem(cert, &certlen, sizeof(cert), certfp) != 1
|
||||
|| x509_cert_get_subject_public_key(cert, certlen, &key) != 1) {
|
||||
fprintf(stderr, "%s: parse certificate failed\n", prog);
|
||||
goto end;
|
||||
}
|
||||
} else {
|
||||
fprintf(stderr, "%s: '-pubkey' or '-cert' option required\n", prog);
|
||||
goto end;
|
||||
}
|
||||
|
||||
|
||||
if (sm2_verify_init(&verify_ctx, &key, id, strlen(id)) != 1) {
|
||||
fprintf(stderr, "%s: inner error\n", prog);
|
||||
goto end;
|
||||
}
|
||||
while ((len = fread(buf, 1, sizeof(buf), infp)) > 0) {
|
||||
if (sm2_verify_update(&verify_ctx, buf, len) != 1) {
|
||||
fprintf(stderr, "%s: inner error\n", prog);
|
||||
goto end;
|
||||
}
|
||||
}
|
||||
if ((vr = sm2_verify_finish(&verify_ctx, sig, siglen)) < 0) {
|
||||
fprintf(stderr, "%s: inner error\n", prog);
|
||||
goto end;
|
||||
}
|
||||
|
||||
fprintf(stdout, "verify : %s\n", vr == 1 ? "success" : "failure");
|
||||
if (vr == 1) {
|
||||
ret = 0;
|
||||
}
|
||||
|
||||
end:
|
||||
if (infile && infp) fclose(infp);
|
||||
if (pubkeyfp) fclose(pubkeyfp);
|
||||
if (certfp) fclose(certfp);
|
||||
if (sigfp) fclose(sigfp);
|
||||
return ret;
|
||||
}
|
||||
|
||||
#include <stdio.h>
|
||||
#include <errno.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <gmssl/sm2.h>
|
||||
#include <gmssl/x509.h>
|
||||
|
||||
|
||||
static const char *options = "(-pubkey pem | -cert pem) [-id str] [-in file] -sig file";
|
||||
|
||||
int sm2verify_main(int argc, char **argv)
|
||||
{
|
||||
int ret = 1;
|
||||
char *prog = argv[0];
|
||||
char *id = SM2_DEFAULT_ID;
|
||||
char *pubkeyfile = NULL;
|
||||
char *certfile = NULL;
|
||||
char *infile = NULL;
|
||||
char *sigfile = NULL;
|
||||
FILE *pubkeyfp = NULL;
|
||||
FILE *certfp = NULL;
|
||||
FILE *infp = stdin;
|
||||
FILE *sigfp = NULL;
|
||||
SM2_KEY key;
|
||||
SM2_SIGN_CTX verify_ctx;
|
||||
uint8_t cert[1024];
|
||||
size_t certlen;
|
||||
uint8_t buf[4096];
|
||||
ssize_t len;
|
||||
uint8_t sig[SM2_MAX_SIGNATURE_SIZE];
|
||||
size_t siglen;
|
||||
int vr;
|
||||
|
||||
argc--;
|
||||
argv++;
|
||||
|
||||
if (argc < 1) {
|
||||
fprintf(stderr, "usage: %s %s\n", prog, options);
|
||||
return 1;
|
||||
}
|
||||
|
||||
while (argc > 0) {
|
||||
if (!strcmp(*argv, "-help")) {
|
||||
printf("usage: %s %s\n", prog, options);
|
||||
ret = 0;
|
||||
goto end;
|
||||
} else if (!strcmp(*argv, "-pubkey")) {
|
||||
if (certfile) {
|
||||
fprintf(stderr, "%s: options '-pubkey' '-cert' conflict\n", prog);
|
||||
goto end;
|
||||
}
|
||||
if (--argc < 1) goto bad;
|
||||
pubkeyfile = *(++argv);
|
||||
if (!(pubkeyfp = fopen(pubkeyfile, "r"))) {
|
||||
fprintf(stderr, "%s: open '%s' failure : %s\n", prog, pubkeyfile, strerror(errno));
|
||||
goto end;
|
||||
}
|
||||
} else if (!strcmp(*argv, "-cert")) {
|
||||
if (pubkeyfile) {
|
||||
fprintf(stderr, "%s: options '-pubkey' '-cert' conflict\n", prog);
|
||||
goto end;
|
||||
}
|
||||
if (--argc < 1) goto bad;
|
||||
certfile = *(++argv);
|
||||
if (!(certfp = fopen(certfile, "r"))) {
|
||||
fprintf(stderr, "%s: open '%s' failure : %s\n", prog, certfile, strerror(errno));
|
||||
goto end;
|
||||
}
|
||||
} else if (!strcmp(*argv, "-id")) {
|
||||
if (--argc < 1) goto bad;
|
||||
id = *(++argv);
|
||||
} else if (!strcmp(*argv, "-in")) {
|
||||
if (--argc < 1) goto bad;
|
||||
infile = *(++argv);
|
||||
if (!(infp = fopen(infile, "r"))) {
|
||||
fprintf(stderr, "%s: open '%s' failure : %s\n", prog, infile, strerror(errno));
|
||||
goto end;
|
||||
}
|
||||
} else if (!strcmp(*argv, "-sig")) {
|
||||
if (--argc < 1) goto bad;
|
||||
sigfile = *(++argv);
|
||||
if (!(sigfp = fopen(sigfile, "r"))) {
|
||||
fprintf(stderr, "%s: open '%s' failure : %s\n", prog, sigfile, strerror(errno));
|
||||
goto end;
|
||||
}
|
||||
|
||||
} else {
|
||||
fprintf(stderr, "%s: illegal option '%s'\n", prog, *argv);
|
||||
goto end;
|
||||
bad:
|
||||
fprintf(stderr, "%s: '%s' option value missing\n", prog, *argv);
|
||||
goto end;
|
||||
}
|
||||
|
||||
argc--;
|
||||
argv++;
|
||||
}
|
||||
|
||||
if (!sigfile) {
|
||||
fprintf(stderr, "%s: '-sig' option required\n", prog);
|
||||
goto end;
|
||||
}
|
||||
if ((siglen = fread(sig, 1, sizeof(sig), sigfp)) <= 0) {
|
||||
fprintf(stderr, "%s: read signature error : %s\n", prog, strerror(errno));
|
||||
goto end;
|
||||
}
|
||||
|
||||
if (pubkeyfile) {
|
||||
if (sm2_public_key_info_from_pem(&key, pubkeyfp) != 1) {
|
||||
fprintf(stderr, "%s: parse public key failed\n", prog);
|
||||
goto end;
|
||||
}
|
||||
} else if (certfile) {
|
||||
if (x509_cert_from_pem(cert, &certlen, sizeof(cert), certfp) != 1
|
||||
|| x509_cert_get_subject_public_key(cert, certlen, &key) != 1) {
|
||||
fprintf(stderr, "%s: parse certificate failed\n", prog);
|
||||
goto end;
|
||||
}
|
||||
} else {
|
||||
fprintf(stderr, "%s: '-pubkey' or '-cert' option required\n", prog);
|
||||
goto end;
|
||||
}
|
||||
|
||||
|
||||
if (sm2_verify_init(&verify_ctx, &key, id, strlen(id)) != 1) {
|
||||
fprintf(stderr, "%s: inner error\n", prog);
|
||||
goto end;
|
||||
}
|
||||
while ((len = fread(buf, 1, sizeof(buf), infp)) > 0) {
|
||||
if (sm2_verify_update(&verify_ctx, buf, len) != 1) {
|
||||
fprintf(stderr, "%s: inner error\n", prog);
|
||||
goto end;
|
||||
}
|
||||
}
|
||||
if ((vr = sm2_verify_finish(&verify_ctx, sig, siglen)) < 0) {
|
||||
fprintf(stderr, "%s: inner error\n", prog);
|
||||
goto end;
|
||||
}
|
||||
|
||||
fprintf(stdout, "verify : %s\n", vr == 1 ? "success" : "failure");
|
||||
if (vr == 1) {
|
||||
ret = 0;
|
||||
}
|
||||
|
||||
end:
|
||||
if (infile && infp) fclose(infp);
|
||||
if (pubkeyfp) fclose(pubkeyfp);
|
||||
if (certfp) fclose(certfp);
|
||||
if (sigfp) fclose(sigfp);
|
||||
return ret;
|
||||
}
|
||||
|
||||
261
tools/sm3.c
261
tools/sm3.c
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2014-2022 The GmSSL Project. All Rights Reserved.
|
||||
* Copyright 2022 The GmSSL Project. All Rights Reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the License); you may
|
||||
* not use this file except in compliance with the License.
|
||||
@@ -7,133 +7,132 @@
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*/
|
||||
|
||||
|
||||
|
||||
#include <stdio.h>
|
||||
#include <errno.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <gmssl/sm2.h>
|
||||
#include <gmssl/sm3.h>
|
||||
#include <gmssl/error.h>
|
||||
|
||||
|
||||
static const char *options = "[-hex|-bin] [-pubkey pem [-id str]] [-in file] [-out file]";
|
||||
|
||||
int sm3_main(int argc, char **argv)
|
||||
{
|
||||
int ret = 1;
|
||||
char *prog = argv[0];
|
||||
int bin = 0;
|
||||
char *pubkeyfile = NULL;
|
||||
char *infile = NULL;
|
||||
char *outfile = NULL;
|
||||
char *id = NULL;
|
||||
FILE *pubkeyfp = NULL;
|
||||
FILE *infp = stdin;
|
||||
FILE *outfp = stdout;
|
||||
SM3_CTX sm3_ctx;
|
||||
uint8_t dgst[32];
|
||||
uint8_t buf[4096];
|
||||
ssize_t len;
|
||||
int i;
|
||||
|
||||
argc--;
|
||||
argv++;
|
||||
|
||||
while (argc > 0) {
|
||||
if (!strcmp(*argv, "-help")) {
|
||||
printf("usage: %s %s\n", prog, options);
|
||||
printf("usage: echo -n \"abc\" | %s\n", prog);
|
||||
ret = 0;
|
||||
goto end;
|
||||
} else if (!strcmp(*argv, "-hex")) {
|
||||
if (bin) {
|
||||
error_print();
|
||||
goto end;
|
||||
}
|
||||
bin = 0;
|
||||
} else if (!strcmp(*argv, "-bin")) {
|
||||
bin = 1;
|
||||
} else if (!strcmp(*argv, "-pubkey")) {
|
||||
if (--argc < 1) goto bad;
|
||||
pubkeyfile = *(++argv);
|
||||
if (!(pubkeyfp = fopen(pubkeyfile, "r"))) {
|
||||
fprintf(stderr, "%s: open '%s' failure : %s\n", prog, pubkeyfile, strerror(errno));
|
||||
goto end;
|
||||
}
|
||||
} else if (!strcmp(*argv, "-id")) {
|
||||
if (--argc < 1) goto bad;
|
||||
id = *(++argv);
|
||||
} else if (!strcmp(*argv, "-in")) {
|
||||
if (--argc < 1) goto bad;
|
||||
infile = *(++argv);
|
||||
if (!(infp = fopen(infile, "r"))) {
|
||||
fprintf(stderr, "%s: open '%s' failure : %s\n", prog, infile, strerror(errno));
|
||||
goto end;
|
||||
}
|
||||
} else if (!strcmp(*argv, "-out")) {
|
||||
if (--argc < 1) goto bad;
|
||||
outfile = *(++argv);
|
||||
if (!(outfp = fopen(outfile, "r"))) {
|
||||
fprintf(stderr, "%s: open '%s' failure : %s\n", prog, outfile, strerror(errno));
|
||||
goto end;
|
||||
}
|
||||
} else {
|
||||
fprintf(stderr, "%s: illegal option '%s'\n", prog, *argv);
|
||||
goto end;
|
||||
bad:
|
||||
fprintf(stderr, "%s: '%s' option value missing\n", prog, *argv);
|
||||
goto end;
|
||||
}
|
||||
|
||||
argc--;
|
||||
argv++;
|
||||
}
|
||||
|
||||
sm3_init(&sm3_ctx);
|
||||
|
||||
if (pubkeyfile) {
|
||||
SM2_KEY sm2_key;
|
||||
uint8_t z[32];
|
||||
|
||||
if (sm2_public_key_info_from_pem(&sm2_key, pubkeyfp) != 1) {
|
||||
fprintf(stderr, "%s: parse public key failed\n", prog);
|
||||
goto end;
|
||||
}
|
||||
if (!id) {
|
||||
id = SM2_DEFAULT_ID;
|
||||
}
|
||||
|
||||
sm2_compute_z(z, (SM2_POINT *)&sm2_key, id, strlen(id));
|
||||
sm3_update(&sm3_ctx, z, sizeof(z));
|
||||
} else {
|
||||
if (id) {
|
||||
fprintf(stderr, "%s: option '-id' must be with '-pubkey'\n", prog);
|
||||
goto end;
|
||||
}
|
||||
}
|
||||
|
||||
while ((len = fread(buf, 1, sizeof(buf), infp)) > 0) {
|
||||
sm3_update(&sm3_ctx, buf, len);
|
||||
}
|
||||
sm3_finish(&sm3_ctx, dgst);
|
||||
|
||||
if (bin) {
|
||||
if (fwrite(dgst, 1, sizeof(dgst), outfp) != sizeof(dgst)) {
|
||||
fprintf(stderr, "%s: output failure : %s\n", prog, strerror(errno));
|
||||
goto end;
|
||||
}
|
||||
} else {
|
||||
for (i = 0; i < sizeof(dgst); i++) {
|
||||
fprintf(outfp, "%02x", dgst[i]);
|
||||
}
|
||||
fprintf(outfp, "\n");
|
||||
}
|
||||
ret = 0;
|
||||
end:
|
||||
if (pubkeyfp) fclose(pubkeyfp);
|
||||
if (infile && infp) fclose(infp);
|
||||
if (outfile && outfp) fclose(outfp);
|
||||
return ret;
|
||||
}
|
||||
|
||||
#include <stdio.h>
|
||||
#include <errno.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <gmssl/sm2.h>
|
||||
#include <gmssl/sm3.h>
|
||||
#include <gmssl/error.h>
|
||||
|
||||
|
||||
static const char *options = "[-hex|-bin] [-pubkey pem [-id str]] [-in file] [-out file]";
|
||||
|
||||
int sm3_main(int argc, char **argv)
|
||||
{
|
||||
int ret = 1;
|
||||
char *prog = argv[0];
|
||||
int bin = 0;
|
||||
char *pubkeyfile = NULL;
|
||||
char *infile = NULL;
|
||||
char *outfile = NULL;
|
||||
char *id = NULL;
|
||||
FILE *pubkeyfp = NULL;
|
||||
FILE *infp = stdin;
|
||||
FILE *outfp = stdout;
|
||||
SM3_CTX sm3_ctx;
|
||||
uint8_t dgst[32];
|
||||
uint8_t buf[4096];
|
||||
ssize_t len;
|
||||
int i;
|
||||
|
||||
argc--;
|
||||
argv++;
|
||||
|
||||
while (argc > 0) {
|
||||
if (!strcmp(*argv, "-help")) {
|
||||
printf("usage: %s %s\n", prog, options);
|
||||
printf("usage: echo -n \"abc\" | %s\n", prog);
|
||||
ret = 0;
|
||||
goto end;
|
||||
} else if (!strcmp(*argv, "-hex")) {
|
||||
if (bin) {
|
||||
error_print();
|
||||
goto end;
|
||||
}
|
||||
bin = 0;
|
||||
} else if (!strcmp(*argv, "-bin")) {
|
||||
bin = 1;
|
||||
} else if (!strcmp(*argv, "-pubkey")) {
|
||||
if (--argc < 1) goto bad;
|
||||
pubkeyfile = *(++argv);
|
||||
if (!(pubkeyfp = fopen(pubkeyfile, "r"))) {
|
||||
fprintf(stderr, "%s: open '%s' failure : %s\n", prog, pubkeyfile, strerror(errno));
|
||||
goto end;
|
||||
}
|
||||
} else if (!strcmp(*argv, "-id")) {
|
||||
if (--argc < 1) goto bad;
|
||||
id = *(++argv);
|
||||
} else if (!strcmp(*argv, "-in")) {
|
||||
if (--argc < 1) goto bad;
|
||||
infile = *(++argv);
|
||||
if (!(infp = fopen(infile, "r"))) {
|
||||
fprintf(stderr, "%s: open '%s' failure : %s\n", prog, infile, strerror(errno));
|
||||
goto end;
|
||||
}
|
||||
} else if (!strcmp(*argv, "-out")) {
|
||||
if (--argc < 1) goto bad;
|
||||
outfile = *(++argv);
|
||||
if (!(outfp = fopen(outfile, "r"))) {
|
||||
fprintf(stderr, "%s: open '%s' failure : %s\n", prog, outfile, strerror(errno));
|
||||
goto end;
|
||||
}
|
||||
} else {
|
||||
fprintf(stderr, "%s: illegal option '%s'\n", prog, *argv);
|
||||
goto end;
|
||||
bad:
|
||||
fprintf(stderr, "%s: '%s' option value missing\n", prog, *argv);
|
||||
goto end;
|
||||
}
|
||||
|
||||
argc--;
|
||||
argv++;
|
||||
}
|
||||
|
||||
sm3_init(&sm3_ctx);
|
||||
|
||||
if (pubkeyfile) {
|
||||
SM2_KEY sm2_key;
|
||||
uint8_t z[32];
|
||||
|
||||
if (sm2_public_key_info_from_pem(&sm2_key, pubkeyfp) != 1) {
|
||||
fprintf(stderr, "%s: parse public key failed\n", prog);
|
||||
goto end;
|
||||
}
|
||||
if (!id) {
|
||||
id = SM2_DEFAULT_ID;
|
||||
}
|
||||
|
||||
sm2_compute_z(z, (SM2_POINT *)&sm2_key, id, strlen(id));
|
||||
sm3_update(&sm3_ctx, z, sizeof(z));
|
||||
} else {
|
||||
if (id) {
|
||||
fprintf(stderr, "%s: option '-id' must be with '-pubkey'\n", prog);
|
||||
goto end;
|
||||
}
|
||||
}
|
||||
|
||||
while ((len = fread(buf, 1, sizeof(buf), infp)) > 0) {
|
||||
sm3_update(&sm3_ctx, buf, len);
|
||||
}
|
||||
sm3_finish(&sm3_ctx, dgst);
|
||||
|
||||
if (bin) {
|
||||
if (fwrite(dgst, 1, sizeof(dgst), outfp) != sizeof(dgst)) {
|
||||
fprintf(stderr, "%s: output failure : %s\n", prog, strerror(errno));
|
||||
goto end;
|
||||
}
|
||||
} else {
|
||||
for (i = 0; i < sizeof(dgst); i++) {
|
||||
fprintf(outfp, "%02x", dgst[i]);
|
||||
}
|
||||
fprintf(outfp, "\n");
|
||||
}
|
||||
ret = 0;
|
||||
end:
|
||||
if (pubkeyfp) fclose(pubkeyfp);
|
||||
if (infile && infp) fclose(infp);
|
||||
if (outfile && outfp) fclose(outfp);
|
||||
return ret;
|
||||
}
|
||||
|
||||
231
tools/sm3hmac.c
231
tools/sm3hmac.c
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2014-2022 The GmSSL Project. All Rights Reserved.
|
||||
* Copyright 2022 The GmSSL Project. All Rights Reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the License); you may
|
||||
* not use this file except in compliance with the License.
|
||||
@@ -7,118 +7,117 @@
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*/
|
||||
|
||||
|
||||
|
||||
#include <stdio.h>
|
||||
#include <errno.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <gmssl/mem.h>
|
||||
#include <gmssl/hex.h>
|
||||
#include <gmssl/sm3.h>
|
||||
|
||||
|
||||
static const char *options = "-key hex [-in file] [-bin|-hex] [-out file]";
|
||||
|
||||
int sm3hmac_main(int argc, char **argv)
|
||||
{
|
||||
int ret = 1;
|
||||
char *prog = argv[0];
|
||||
char *keyhex = NULL;
|
||||
int bin = 0;
|
||||
char *infile = NULL;
|
||||
char *outfile = NULL;
|
||||
uint8_t key[SM3_DIGEST_SIZE];
|
||||
size_t keylen;
|
||||
FILE *infp = stdin;
|
||||
FILE *outfp = stdout;
|
||||
uint8_t buf[4096];
|
||||
size_t len;
|
||||
SM3_HMAC_CTX ctx;
|
||||
uint8_t mac[SM3_HMAC_SIZE];
|
||||
size_t i;
|
||||
|
||||
argc--;
|
||||
argv++;
|
||||
|
||||
if (argc < 1) {
|
||||
fprintf(stderr, "usage: %s %s\n", prog, options);
|
||||
return 1;
|
||||
}
|
||||
|
||||
while (argc > 0) {
|
||||
if (!strcmp(*argv, "-help")) {
|
||||
printf("usage: %s %s\n", prog, options);
|
||||
ret = 0;
|
||||
goto end;
|
||||
} else if (!strcmp(*argv, "-key")) {
|
||||
if (--argc < 1) goto bad;
|
||||
keyhex = *(++argv);
|
||||
if (strlen(keyhex) > sizeof(key) * 2) {
|
||||
fprintf(stderr, "%s: key should be less than 64 digits (32 bytes)\n", prog);
|
||||
goto end;
|
||||
}
|
||||
if (hex_to_bytes(keyhex, strlen(keyhex), key, &keylen) != 1) {
|
||||
fprintf(stderr, "%s: invalid HEX digits\n", prog);
|
||||
goto end;
|
||||
}
|
||||
} else if (!strcmp(*argv, "-hex")) {
|
||||
bin = 0;
|
||||
} else if (!strcmp(*argv, "-bin")) {
|
||||
bin = 1;
|
||||
} else if (!strcmp(*argv, "-in")) {
|
||||
if (--argc < 1) goto bad;
|
||||
infile = *(++argv);
|
||||
if (!(infp = fopen(infile, "r"))) {
|
||||
fprintf(stderr, "%s: open '%s' failure : %s\n", prog, infile, strerror(errno));
|
||||
goto end;
|
||||
}
|
||||
} else if (!strcmp(*argv, "-out")) {
|
||||
if (--argc < 1) goto bad;
|
||||
outfile = *(++argv);
|
||||
if (!(outfp = fopen(outfile, "w"))) {
|
||||
fprintf(stderr, "%s: open '%s' failure : %s\n", prog, outfile, strerror(errno));
|
||||
goto end;
|
||||
}
|
||||
} else {
|
||||
fprintf(stderr, "%s: illegal option '%s'\n", prog, *argv);
|
||||
goto end;
|
||||
bad:
|
||||
fprintf(stderr, "%s: '%s' option value missing\n", prog, *argv);
|
||||
goto end;
|
||||
}
|
||||
|
||||
argc--;
|
||||
argv++;
|
||||
}
|
||||
|
||||
if (!keyhex) {
|
||||
fprintf(stderr, "%s: option '-key' required\n", prog);
|
||||
goto end;
|
||||
}
|
||||
|
||||
sm3_hmac_init(&ctx, key, keylen);
|
||||
while ((len = fread(buf, 1, sizeof(buf), infp)) > 0) {
|
||||
sm3_hmac_update(&ctx, buf, len);
|
||||
}
|
||||
sm3_hmac_finish(&ctx, mac);
|
||||
|
||||
if (bin) {
|
||||
if (fwrite(mac, 1, sizeof(mac), outfp) != sizeof(mac)) {
|
||||
fprintf(stderr, "%s: output failure : %s\n", prog, strerror(errno));
|
||||
goto end;
|
||||
}
|
||||
} else {
|
||||
for (i = 0; i < sizeof(mac); i++) {
|
||||
fprintf(outfp, "%02x", mac[i]);
|
||||
}
|
||||
fprintf(outfp, "\n");
|
||||
}
|
||||
ret = 0;
|
||||
end:
|
||||
gmssl_secure_clear(key, sizeof(key));
|
||||
gmssl_secure_clear(&ctx, sizeof(ctx));
|
||||
if (infile && infp) fclose(infp);
|
||||
if (outfile && outfp) fclose(outfp);
|
||||
return ret;
|
||||
}
|
||||
|
||||
#include <stdio.h>
|
||||
#include <errno.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <gmssl/mem.h>
|
||||
#include <gmssl/hex.h>
|
||||
#include <gmssl/sm3.h>
|
||||
|
||||
|
||||
static const char *options = "-key hex [-in file] [-bin|-hex] [-out file]";
|
||||
|
||||
int sm3hmac_main(int argc, char **argv)
|
||||
{
|
||||
int ret = 1;
|
||||
char *prog = argv[0];
|
||||
char *keyhex = NULL;
|
||||
int bin = 0;
|
||||
char *infile = NULL;
|
||||
char *outfile = NULL;
|
||||
uint8_t key[SM3_DIGEST_SIZE];
|
||||
size_t keylen;
|
||||
FILE *infp = stdin;
|
||||
FILE *outfp = stdout;
|
||||
uint8_t buf[4096];
|
||||
size_t len;
|
||||
SM3_HMAC_CTX ctx;
|
||||
uint8_t mac[SM3_HMAC_SIZE];
|
||||
size_t i;
|
||||
|
||||
argc--;
|
||||
argv++;
|
||||
|
||||
if (argc < 1) {
|
||||
fprintf(stderr, "usage: %s %s\n", prog, options);
|
||||
return 1;
|
||||
}
|
||||
|
||||
while (argc > 0) {
|
||||
if (!strcmp(*argv, "-help")) {
|
||||
printf("usage: %s %s\n", prog, options);
|
||||
ret = 0;
|
||||
goto end;
|
||||
} else if (!strcmp(*argv, "-key")) {
|
||||
if (--argc < 1) goto bad;
|
||||
keyhex = *(++argv);
|
||||
if (strlen(keyhex) > sizeof(key) * 2) {
|
||||
fprintf(stderr, "%s: key should be less than 64 digits (32 bytes)\n", prog);
|
||||
goto end;
|
||||
}
|
||||
if (hex_to_bytes(keyhex, strlen(keyhex), key, &keylen) != 1) {
|
||||
fprintf(stderr, "%s: invalid HEX digits\n", prog);
|
||||
goto end;
|
||||
}
|
||||
} else if (!strcmp(*argv, "-hex")) {
|
||||
bin = 0;
|
||||
} else if (!strcmp(*argv, "-bin")) {
|
||||
bin = 1;
|
||||
} else if (!strcmp(*argv, "-in")) {
|
||||
if (--argc < 1) goto bad;
|
||||
infile = *(++argv);
|
||||
if (!(infp = fopen(infile, "r"))) {
|
||||
fprintf(stderr, "%s: open '%s' failure : %s\n", prog, infile, strerror(errno));
|
||||
goto end;
|
||||
}
|
||||
} else if (!strcmp(*argv, "-out")) {
|
||||
if (--argc < 1) goto bad;
|
||||
outfile = *(++argv);
|
||||
if (!(outfp = fopen(outfile, "w"))) {
|
||||
fprintf(stderr, "%s: open '%s' failure : %s\n", prog, outfile, strerror(errno));
|
||||
goto end;
|
||||
}
|
||||
} else {
|
||||
fprintf(stderr, "%s: illegal option '%s'\n", prog, *argv);
|
||||
goto end;
|
||||
bad:
|
||||
fprintf(stderr, "%s: '%s' option value missing\n", prog, *argv);
|
||||
goto end;
|
||||
}
|
||||
|
||||
argc--;
|
||||
argv++;
|
||||
}
|
||||
|
||||
if (!keyhex) {
|
||||
fprintf(stderr, "%s: option '-key' required\n", prog);
|
||||
goto end;
|
||||
}
|
||||
|
||||
sm3_hmac_init(&ctx, key, keylen);
|
||||
while ((len = fread(buf, 1, sizeof(buf), infp)) > 0) {
|
||||
sm3_hmac_update(&ctx, buf, len);
|
||||
}
|
||||
sm3_hmac_finish(&ctx, mac);
|
||||
|
||||
if (bin) {
|
||||
if (fwrite(mac, 1, sizeof(mac), outfp) != sizeof(mac)) {
|
||||
fprintf(stderr, "%s: output failure : %s\n", prog, strerror(errno));
|
||||
goto end;
|
||||
}
|
||||
} else {
|
||||
for (i = 0; i < sizeof(mac); i++) {
|
||||
fprintf(outfp, "%02x", mac[i]);
|
||||
}
|
||||
fprintf(outfp, "\n");
|
||||
}
|
||||
ret = 0;
|
||||
end:
|
||||
gmssl_secure_clear(key, sizeof(key));
|
||||
gmssl_secure_clear(&ctx, sizeof(ctx));
|
||||
if (infile && infp) fclose(infp);
|
||||
if (outfile && outfp) fclose(outfp);
|
||||
return ret;
|
||||
}
|
||||
|
||||
437
tools/sm4.c
437
tools/sm4.c
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2014-2022 The GmSSL Project. All Rights Reserved.
|
||||
* Copyright 2022 The GmSSL Project. All Rights Reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the License); you may
|
||||
* not use this file except in compliance with the License.
|
||||
@@ -7,221 +7,220 @@
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*/
|
||||
|
||||
|
||||
|
||||
#include <stdio.h>
|
||||
#include <errno.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <gmssl/mem.h>
|
||||
#include <gmssl/sm4.h>
|
||||
#include <gmssl/hex.h>
|
||||
|
||||
|
||||
#define SM4_MODE_CBC 1
|
||||
#define SM4_MODE_CTR 2
|
||||
|
||||
static const char *options = "{-cbc|-ctr} {-encrypt|-decrypt} -key hex -iv hex [-in file] [-out file]";
|
||||
|
||||
int sm4_main(int argc, char **argv)
|
||||
{
|
||||
int ret = 1;
|
||||
char *prog = argv[0];
|
||||
char *keyhex = NULL;
|
||||
char *ivhex = NULL;
|
||||
char *infile = NULL;
|
||||
char *outfile = NULL;
|
||||
uint8_t key[16];
|
||||
uint8_t iv[16];
|
||||
size_t keylen = sizeof(key);
|
||||
size_t ivlen = sizeof(iv);
|
||||
FILE *infp = stdin;
|
||||
FILE *outfp = stdout;
|
||||
int mode = 0;
|
||||
int enc = -1;
|
||||
SM4_CBC_CTX cbc_ctx;
|
||||
SM4_CTR_CTX ctr_ctx;
|
||||
uint8_t inbuf[4096];
|
||||
size_t inlen;
|
||||
uint8_t outbuf[4196];
|
||||
size_t outlen;
|
||||
|
||||
argc--;
|
||||
argv++;
|
||||
|
||||
if (argc < 1) {
|
||||
fprintf(stderr, "usage: %s %s\n", prog, options);
|
||||
return 1;
|
||||
}
|
||||
|
||||
while (argc > 0) {
|
||||
if (!strcmp(*argv, "-help")) {
|
||||
printf("usage: %s %s\n", prog, options);
|
||||
ret = 0;
|
||||
goto end;
|
||||
} else if (!strcmp(*argv, "-key")) {
|
||||
if (--argc < 1) goto bad;
|
||||
keyhex = *(++argv);
|
||||
if (strlen(keyhex) != sizeof(key) * 2) {
|
||||
fprintf(stderr, "%s: invalid key length\n", prog);
|
||||
goto end;
|
||||
}
|
||||
if (hex_to_bytes(keyhex, strlen(keyhex), key, &keylen) != 1) {
|
||||
fprintf(stderr, "%s: invalid HEX digits\n", prog);
|
||||
goto end;
|
||||
}
|
||||
} else if (!strcmp(*argv, "-iv")) {
|
||||
if (--argc < 1) goto bad;
|
||||
ivhex = *(++argv);
|
||||
if (strlen(ivhex) != sizeof(iv) * 2) {
|
||||
fprintf(stderr, "%s: invalid IV length\n", prog);
|
||||
goto end;
|
||||
}
|
||||
if (hex_to_bytes(ivhex, strlen(ivhex), iv, &ivlen) != 1) {
|
||||
fprintf(stderr, "%s: invalid HEX digits\n", prog);
|
||||
goto end;
|
||||
}
|
||||
} else if (!strcmp(*argv, "-encrypt")) {
|
||||
enc = 1;
|
||||
} else if (!strcmp(*argv, "-decrypt")) {
|
||||
enc = 0;
|
||||
} else if (!strcmp(*argv, "-cbc")) {
|
||||
if (mode) goto bad;
|
||||
mode = SM4_MODE_CBC;
|
||||
} else if (!strcmp(*argv, "-ctr")) {
|
||||
if (mode) goto bad;
|
||||
mode = SM4_MODE_CTR;
|
||||
} else if (!strcmp(*argv, "-in")) {
|
||||
if (--argc < 1) goto bad;
|
||||
infile = *(++argv);
|
||||
if (!(infp = fopen(infile, "r"))) {
|
||||
fprintf(stderr, "%s: open '%s' failure : %s\n", prog, infile, strerror(errno));
|
||||
goto end;
|
||||
}
|
||||
} else if (!strcmp(*argv, "-out")) {
|
||||
if (--argc < 1) goto bad;
|
||||
outfile = *(++argv);
|
||||
if (!(outfp = fopen(outfile, "w"))) {
|
||||
fprintf(stderr, "%s: open '%s' failure : %s\n", prog, outfile, strerror(errno));
|
||||
goto end;
|
||||
}
|
||||
} else {
|
||||
fprintf(stderr, "%s: illegal option '%s'\n", prog, *argv);
|
||||
goto end;
|
||||
bad:
|
||||
fprintf(stderr, "%s: '%s' option value missing\n", prog, *argv);
|
||||
goto end;
|
||||
}
|
||||
|
||||
argc--;
|
||||
argv++;
|
||||
}
|
||||
|
||||
if (!mode) {
|
||||
fprintf(stderr, "%s: mode not assigned, -cbc or -ctr option required\n", prog);
|
||||
goto end;
|
||||
}
|
||||
if (!keyhex) {
|
||||
fprintf(stderr, "%s: option '-key' missing\n", prog);
|
||||
goto end;
|
||||
}
|
||||
if (!ivhex) {
|
||||
fprintf(stderr, "%s: option '-iv' missing\n", prog);
|
||||
goto end;
|
||||
}
|
||||
|
||||
|
||||
if (mode == SM4_MODE_CTR) {
|
||||
if (sm4_ctr_encrypt_init(&ctr_ctx, key, iv) != 1) {
|
||||
fprintf(stderr, "%s: inner error\n", prog);
|
||||
goto end;
|
||||
}
|
||||
while ((inlen = fread(inbuf, 1, sizeof(inbuf), infp)) > 0) {
|
||||
if (sm4_ctr_encrypt_update(&ctr_ctx, inbuf, inlen, outbuf, &outlen) != 1) {
|
||||
fprintf(stderr, "%s: inner error\n", prog);
|
||||
goto end;
|
||||
}
|
||||
if (fwrite(outbuf, 1, outlen, outfp) != outlen) {
|
||||
fprintf(stderr, "%s: output failure : %s\n", prog, strerror(errno));
|
||||
goto end;
|
||||
}
|
||||
}
|
||||
if (sm4_ctr_encrypt_finish(&ctr_ctx, outbuf, &outlen) != 1) {
|
||||
fprintf(stderr, "%s: inner error\n", prog);
|
||||
goto end;
|
||||
}
|
||||
if (fwrite(outbuf, 1, outlen, outfp) != outlen) {
|
||||
fprintf(stderr, "%s: output failure : %s\n", prog, strerror(errno));
|
||||
goto end;
|
||||
}
|
||||
|
||||
ret = 0;
|
||||
goto end;
|
||||
}
|
||||
|
||||
if (enc < 0) {
|
||||
fprintf(stderr, "%s: option -encrypt or -decrypt should be set\n", prog);
|
||||
goto end;
|
||||
}
|
||||
|
||||
if (enc) {
|
||||
if (sm4_cbc_encrypt_init(&cbc_ctx, key, iv) != 1) {
|
||||
fprintf(stderr, "%s: inner error\n", prog);
|
||||
goto end;
|
||||
}
|
||||
while ((inlen = fread(inbuf, 1, sizeof(inbuf), infp)) > 0) {
|
||||
if (sm4_cbc_encrypt_update(&cbc_ctx, inbuf, inlen, outbuf, &outlen) != 1) {
|
||||
fprintf(stderr, "%s: inner error\n", prog);
|
||||
goto end;
|
||||
}
|
||||
if (fwrite(outbuf, 1, outlen, outfp) != outlen) {
|
||||
fprintf(stderr, "%s: output failure : %s\n", prog, strerror(errno));
|
||||
goto end;
|
||||
}
|
||||
}
|
||||
if (sm4_cbc_encrypt_finish(&cbc_ctx, outbuf, &outlen) != 1) {
|
||||
fprintf(stderr, "%s: inner error\n", prog);
|
||||
goto end;
|
||||
}
|
||||
if (fwrite(outbuf, 1, outlen, outfp) != outlen) {
|
||||
fprintf(stderr, "%s: output failure : %s\n", prog, strerror(errno));
|
||||
goto end;
|
||||
}
|
||||
|
||||
} else {
|
||||
if (sm4_cbc_decrypt_init(&cbc_ctx, key, iv) != 1) {
|
||||
fprintf(stderr, "%s: inner error\n", prog);
|
||||
goto end;
|
||||
}
|
||||
while ((inlen = fread(inbuf, 1, sizeof(inbuf), infp)) > 0) {
|
||||
if (sm4_cbc_decrypt_update(&cbc_ctx, inbuf, inlen, outbuf, &outlen) != 1) {
|
||||
fprintf(stderr, "%s: inner error\n", prog);
|
||||
goto end;
|
||||
}
|
||||
if (fwrite(outbuf, 1, outlen, outfp) != outlen) {
|
||||
fprintf(stderr, "%s: output failure : %s\n", prog, strerror(errno));
|
||||
goto end;
|
||||
}
|
||||
}
|
||||
if (sm4_cbc_decrypt_finish(&cbc_ctx, outbuf, &outlen) != 1) {
|
||||
fprintf(stderr, "%s: inner error\n", prog);
|
||||
goto end;
|
||||
}
|
||||
if (fwrite(outbuf, 1, outlen, outfp) != outlen) {
|
||||
fprintf(stderr, "%s: output failure : %s\n", prog, strerror(errno));
|
||||
goto end;
|
||||
}
|
||||
}
|
||||
ret = 0;
|
||||
|
||||
end:
|
||||
gmssl_secure_clear(&cbc_ctx, sizeof(cbc_ctx));
|
||||
gmssl_secure_clear(&ctr_ctx, sizeof(ctr_ctx));
|
||||
gmssl_secure_clear(key, sizeof(key));
|
||||
gmssl_secure_clear(iv, sizeof(iv));
|
||||
gmssl_secure_clear(inbuf, sizeof(inbuf));
|
||||
gmssl_secure_clear(outbuf, sizeof(outbuf));
|
||||
if (infile && infp) fclose(infp);
|
||||
if (outfile && outfp) fclose(outfp);
|
||||
return ret;
|
||||
}
|
||||
|
||||
#include <stdio.h>
|
||||
#include <errno.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <gmssl/mem.h>
|
||||
#include <gmssl/sm4.h>
|
||||
#include <gmssl/hex.h>
|
||||
|
||||
|
||||
#define SM4_MODE_CBC 1
|
||||
#define SM4_MODE_CTR 2
|
||||
|
||||
static const char *options = "{-cbc|-ctr} {-encrypt|-decrypt} -key hex -iv hex [-in file] [-out file]";
|
||||
|
||||
int sm4_main(int argc, char **argv)
|
||||
{
|
||||
int ret = 1;
|
||||
char *prog = argv[0];
|
||||
char *keyhex = NULL;
|
||||
char *ivhex = NULL;
|
||||
char *infile = NULL;
|
||||
char *outfile = NULL;
|
||||
uint8_t key[16];
|
||||
uint8_t iv[16];
|
||||
size_t keylen = sizeof(key);
|
||||
size_t ivlen = sizeof(iv);
|
||||
FILE *infp = stdin;
|
||||
FILE *outfp = stdout;
|
||||
int mode = 0;
|
||||
int enc = -1;
|
||||
SM4_CBC_CTX cbc_ctx;
|
||||
SM4_CTR_CTX ctr_ctx;
|
||||
uint8_t inbuf[4096];
|
||||
size_t inlen;
|
||||
uint8_t outbuf[4196];
|
||||
size_t outlen;
|
||||
|
||||
argc--;
|
||||
argv++;
|
||||
|
||||
if (argc < 1) {
|
||||
fprintf(stderr, "usage: %s %s\n", prog, options);
|
||||
return 1;
|
||||
}
|
||||
|
||||
while (argc > 0) {
|
||||
if (!strcmp(*argv, "-help")) {
|
||||
printf("usage: %s %s\n", prog, options);
|
||||
ret = 0;
|
||||
goto end;
|
||||
} else if (!strcmp(*argv, "-key")) {
|
||||
if (--argc < 1) goto bad;
|
||||
keyhex = *(++argv);
|
||||
if (strlen(keyhex) != sizeof(key) * 2) {
|
||||
fprintf(stderr, "%s: invalid key length\n", prog);
|
||||
goto end;
|
||||
}
|
||||
if (hex_to_bytes(keyhex, strlen(keyhex), key, &keylen) != 1) {
|
||||
fprintf(stderr, "%s: invalid HEX digits\n", prog);
|
||||
goto end;
|
||||
}
|
||||
} else if (!strcmp(*argv, "-iv")) {
|
||||
if (--argc < 1) goto bad;
|
||||
ivhex = *(++argv);
|
||||
if (strlen(ivhex) != sizeof(iv) * 2) {
|
||||
fprintf(stderr, "%s: invalid IV length\n", prog);
|
||||
goto end;
|
||||
}
|
||||
if (hex_to_bytes(ivhex, strlen(ivhex), iv, &ivlen) != 1) {
|
||||
fprintf(stderr, "%s: invalid HEX digits\n", prog);
|
||||
goto end;
|
||||
}
|
||||
} else if (!strcmp(*argv, "-encrypt")) {
|
||||
enc = 1;
|
||||
} else if (!strcmp(*argv, "-decrypt")) {
|
||||
enc = 0;
|
||||
} else if (!strcmp(*argv, "-cbc")) {
|
||||
if (mode) goto bad;
|
||||
mode = SM4_MODE_CBC;
|
||||
} else if (!strcmp(*argv, "-ctr")) {
|
||||
if (mode) goto bad;
|
||||
mode = SM4_MODE_CTR;
|
||||
} else if (!strcmp(*argv, "-in")) {
|
||||
if (--argc < 1) goto bad;
|
||||
infile = *(++argv);
|
||||
if (!(infp = fopen(infile, "r"))) {
|
||||
fprintf(stderr, "%s: open '%s' failure : %s\n", prog, infile, strerror(errno));
|
||||
goto end;
|
||||
}
|
||||
} else if (!strcmp(*argv, "-out")) {
|
||||
if (--argc < 1) goto bad;
|
||||
outfile = *(++argv);
|
||||
if (!(outfp = fopen(outfile, "w"))) {
|
||||
fprintf(stderr, "%s: open '%s' failure : %s\n", prog, outfile, strerror(errno));
|
||||
goto end;
|
||||
}
|
||||
} else {
|
||||
fprintf(stderr, "%s: illegal option '%s'\n", prog, *argv);
|
||||
goto end;
|
||||
bad:
|
||||
fprintf(stderr, "%s: '%s' option value missing\n", prog, *argv);
|
||||
goto end;
|
||||
}
|
||||
|
||||
argc--;
|
||||
argv++;
|
||||
}
|
||||
|
||||
if (!mode) {
|
||||
fprintf(stderr, "%s: mode not assigned, -cbc or -ctr option required\n", prog);
|
||||
goto end;
|
||||
}
|
||||
if (!keyhex) {
|
||||
fprintf(stderr, "%s: option '-key' missing\n", prog);
|
||||
goto end;
|
||||
}
|
||||
if (!ivhex) {
|
||||
fprintf(stderr, "%s: option '-iv' missing\n", prog);
|
||||
goto end;
|
||||
}
|
||||
|
||||
|
||||
if (mode == SM4_MODE_CTR) {
|
||||
if (sm4_ctr_encrypt_init(&ctr_ctx, key, iv) != 1) {
|
||||
fprintf(stderr, "%s: inner error\n", prog);
|
||||
goto end;
|
||||
}
|
||||
while ((inlen = fread(inbuf, 1, sizeof(inbuf), infp)) > 0) {
|
||||
if (sm4_ctr_encrypt_update(&ctr_ctx, inbuf, inlen, outbuf, &outlen) != 1) {
|
||||
fprintf(stderr, "%s: inner error\n", prog);
|
||||
goto end;
|
||||
}
|
||||
if (fwrite(outbuf, 1, outlen, outfp) != outlen) {
|
||||
fprintf(stderr, "%s: output failure : %s\n", prog, strerror(errno));
|
||||
goto end;
|
||||
}
|
||||
}
|
||||
if (sm4_ctr_encrypt_finish(&ctr_ctx, outbuf, &outlen) != 1) {
|
||||
fprintf(stderr, "%s: inner error\n", prog);
|
||||
goto end;
|
||||
}
|
||||
if (fwrite(outbuf, 1, outlen, outfp) != outlen) {
|
||||
fprintf(stderr, "%s: output failure : %s\n", prog, strerror(errno));
|
||||
goto end;
|
||||
}
|
||||
|
||||
ret = 0;
|
||||
goto end;
|
||||
}
|
||||
|
||||
if (enc < 0) {
|
||||
fprintf(stderr, "%s: option -encrypt or -decrypt should be set\n", prog);
|
||||
goto end;
|
||||
}
|
||||
|
||||
if (enc) {
|
||||
if (sm4_cbc_encrypt_init(&cbc_ctx, key, iv) != 1) {
|
||||
fprintf(stderr, "%s: inner error\n", prog);
|
||||
goto end;
|
||||
}
|
||||
while ((inlen = fread(inbuf, 1, sizeof(inbuf), infp)) > 0) {
|
||||
if (sm4_cbc_encrypt_update(&cbc_ctx, inbuf, inlen, outbuf, &outlen) != 1) {
|
||||
fprintf(stderr, "%s: inner error\n", prog);
|
||||
goto end;
|
||||
}
|
||||
if (fwrite(outbuf, 1, outlen, outfp) != outlen) {
|
||||
fprintf(stderr, "%s: output failure : %s\n", prog, strerror(errno));
|
||||
goto end;
|
||||
}
|
||||
}
|
||||
if (sm4_cbc_encrypt_finish(&cbc_ctx, outbuf, &outlen) != 1) {
|
||||
fprintf(stderr, "%s: inner error\n", prog);
|
||||
goto end;
|
||||
}
|
||||
if (fwrite(outbuf, 1, outlen, outfp) != outlen) {
|
||||
fprintf(stderr, "%s: output failure : %s\n", prog, strerror(errno));
|
||||
goto end;
|
||||
}
|
||||
|
||||
} else {
|
||||
if (sm4_cbc_decrypt_init(&cbc_ctx, key, iv) != 1) {
|
||||
fprintf(stderr, "%s: inner error\n", prog);
|
||||
goto end;
|
||||
}
|
||||
while ((inlen = fread(inbuf, 1, sizeof(inbuf), infp)) > 0) {
|
||||
if (sm4_cbc_decrypt_update(&cbc_ctx, inbuf, inlen, outbuf, &outlen) != 1) {
|
||||
fprintf(stderr, "%s: inner error\n", prog);
|
||||
goto end;
|
||||
}
|
||||
if (fwrite(outbuf, 1, outlen, outfp) != outlen) {
|
||||
fprintf(stderr, "%s: output failure : %s\n", prog, strerror(errno));
|
||||
goto end;
|
||||
}
|
||||
}
|
||||
if (sm4_cbc_decrypt_finish(&cbc_ctx, outbuf, &outlen) != 1) {
|
||||
fprintf(stderr, "%s: inner error\n", prog);
|
||||
goto end;
|
||||
}
|
||||
if (fwrite(outbuf, 1, outlen, outfp) != outlen) {
|
||||
fprintf(stderr, "%s: output failure : %s\n", prog, strerror(errno));
|
||||
goto end;
|
||||
}
|
||||
}
|
||||
ret = 0;
|
||||
|
||||
end:
|
||||
gmssl_secure_clear(&cbc_ctx, sizeof(cbc_ctx));
|
||||
gmssl_secure_clear(&ctr_ctx, sizeof(ctr_ctx));
|
||||
gmssl_secure_clear(key, sizeof(key));
|
||||
gmssl_secure_clear(iv, sizeof(iv));
|
||||
gmssl_secure_clear(inbuf, sizeof(inbuf));
|
||||
gmssl_secure_clear(outbuf, sizeof(outbuf));
|
||||
if (infile && infp) fclose(infp);
|
||||
if (outfile && outfp) fclose(outfp);
|
||||
return ret;
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2014-2022 The GmSSL Project. All Rights Reserved.
|
||||
* Copyright 2022 The GmSSL Project. All Rights Reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the License); you may
|
||||
* not use this file except in compliance with the License.
|
||||
@@ -7,112 +7,111 @@
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*/
|
||||
|
||||
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <gmssl/mem.h>
|
||||
#include <gmssl/sm9.h>
|
||||
#include <gmssl/error.h>
|
||||
|
||||
|
||||
static const char *options = "[-in file] -key file -pass str -id str [-out file]";
|
||||
|
||||
int sm9decrypt_main(int argc, char **argv)
|
||||
{
|
||||
int ret = 1;
|
||||
char *prog = argv[0];
|
||||
char *infile = NULL;
|
||||
char *keyfile = NULL;
|
||||
char *pass = NULL;
|
||||
char *id = NULL;
|
||||
char *outfile = NULL;
|
||||
FILE *keyfp = NULL;
|
||||
FILE *infp = stdin;
|
||||
FILE *outfp = stdout;
|
||||
SM9_ENC_KEY key;
|
||||
uint8_t inbuf[SM9_MAX_CIPHERTEXT_SIZE];
|
||||
uint8_t outbuf[SM9_MAX_CIPHERTEXT_SIZE];
|
||||
size_t inlen, outlen;
|
||||
|
||||
argc--;
|
||||
argv++;
|
||||
|
||||
if (argc < 1) {
|
||||
fprintf(stderr, "usage: %s %s\n", prog, options);
|
||||
return 1;
|
||||
}
|
||||
|
||||
while (argc > 0) {
|
||||
if (!strcmp(*argv, "-help")) {
|
||||
fprintf(stdout, "usage: %s %s\n", prog, options);
|
||||
return 0;
|
||||
} else if (!strcmp(*argv, "-key")) {
|
||||
if (--argc < 1) goto bad;
|
||||
keyfile = *(++argv);
|
||||
if (!(keyfp = fopen(keyfile, "r"))) {
|
||||
error_print();
|
||||
goto end;
|
||||
}
|
||||
} else if (!strcmp(*argv, "-pass")) {
|
||||
if (--argc < 1) goto bad;
|
||||
pass = *(++argv);
|
||||
} else if (!strcmp(*argv, "-id")) {
|
||||
if (--argc < 1) goto bad;
|
||||
id = *(++argv);
|
||||
} else if (!strcmp(*argv, "-in")) {
|
||||
if (--argc < 1) goto bad;
|
||||
infile = *(++argv);
|
||||
if (!(infp = fopen(infile, "r"))) {
|
||||
error_print();
|
||||
goto end;
|
||||
}
|
||||
} else if (!strcmp(*argv, "-out")) {
|
||||
if (--argc < 1) goto bad;
|
||||
outfile = *(++argv);
|
||||
if (!(outfp = fopen(outfile, "w"))) {
|
||||
error_print();
|
||||
goto end;
|
||||
}
|
||||
} else {
|
||||
bad:
|
||||
fprintf(stderr, "%s: illegal option '%s'\n", prog, *argv);
|
||||
return 1;
|
||||
}
|
||||
|
||||
argc--;
|
||||
argv++;
|
||||
}
|
||||
|
||||
if (!keyfile || !pass || !id) {
|
||||
error_print();
|
||||
goto end;
|
||||
}
|
||||
|
||||
if (sm9_enc_key_info_decrypt_from_pem(&key, pass, keyfp) != 1) {
|
||||
error_print();
|
||||
goto end;
|
||||
}
|
||||
if ((inlen = fread(inbuf, 1, sizeof(inbuf), infp)) <= 0) {
|
||||
error_print();
|
||||
goto end;
|
||||
}
|
||||
if (sm9_decrypt(&key, id, strlen(id), inbuf, inlen, outbuf, &outlen) != 1) {
|
||||
error_print();
|
||||
goto end;
|
||||
}
|
||||
if (outlen != fwrite(outbuf, 1, outlen, outfp)) {
|
||||
error_print();
|
||||
goto end;
|
||||
}
|
||||
ret = 0;
|
||||
|
||||
end:
|
||||
gmssl_secure_clear(&key, sizeof(key));
|
||||
gmssl_secure_clear(outbuf, sizeof(outbuf));
|
||||
if (keyfp) fclose(keyfp);
|
||||
if (infile && infp) fclose(infp);
|
||||
if (outfile && outfp) fclose(outfp);
|
||||
return ret;
|
||||
}
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <gmssl/mem.h>
|
||||
#include <gmssl/sm9.h>
|
||||
#include <gmssl/error.h>
|
||||
|
||||
|
||||
static const char *options = "[-in file] -key file -pass str -id str [-out file]";
|
||||
|
||||
int sm9decrypt_main(int argc, char **argv)
|
||||
{
|
||||
int ret = 1;
|
||||
char *prog = argv[0];
|
||||
char *infile = NULL;
|
||||
char *keyfile = NULL;
|
||||
char *pass = NULL;
|
||||
char *id = NULL;
|
||||
char *outfile = NULL;
|
||||
FILE *keyfp = NULL;
|
||||
FILE *infp = stdin;
|
||||
FILE *outfp = stdout;
|
||||
SM9_ENC_KEY key;
|
||||
uint8_t inbuf[SM9_MAX_CIPHERTEXT_SIZE];
|
||||
uint8_t outbuf[SM9_MAX_CIPHERTEXT_SIZE];
|
||||
size_t inlen, outlen;
|
||||
|
||||
argc--;
|
||||
argv++;
|
||||
|
||||
if (argc < 1) {
|
||||
fprintf(stderr, "usage: %s %s\n", prog, options);
|
||||
return 1;
|
||||
}
|
||||
|
||||
while (argc > 0) {
|
||||
if (!strcmp(*argv, "-help")) {
|
||||
fprintf(stdout, "usage: %s %s\n", prog, options);
|
||||
return 0;
|
||||
} else if (!strcmp(*argv, "-key")) {
|
||||
if (--argc < 1) goto bad;
|
||||
keyfile = *(++argv);
|
||||
if (!(keyfp = fopen(keyfile, "r"))) {
|
||||
error_print();
|
||||
goto end;
|
||||
}
|
||||
} else if (!strcmp(*argv, "-pass")) {
|
||||
if (--argc < 1) goto bad;
|
||||
pass = *(++argv);
|
||||
} else if (!strcmp(*argv, "-id")) {
|
||||
if (--argc < 1) goto bad;
|
||||
id = *(++argv);
|
||||
} else if (!strcmp(*argv, "-in")) {
|
||||
if (--argc < 1) goto bad;
|
||||
infile = *(++argv);
|
||||
if (!(infp = fopen(infile, "r"))) {
|
||||
error_print();
|
||||
goto end;
|
||||
}
|
||||
} else if (!strcmp(*argv, "-out")) {
|
||||
if (--argc < 1) goto bad;
|
||||
outfile = *(++argv);
|
||||
if (!(outfp = fopen(outfile, "w"))) {
|
||||
error_print();
|
||||
goto end;
|
||||
}
|
||||
} else {
|
||||
bad:
|
||||
fprintf(stderr, "%s: illegal option '%s'\n", prog, *argv);
|
||||
return 1;
|
||||
}
|
||||
|
||||
argc--;
|
||||
argv++;
|
||||
}
|
||||
|
||||
if (!keyfile || !pass || !id) {
|
||||
error_print();
|
||||
goto end;
|
||||
}
|
||||
|
||||
if (sm9_enc_key_info_decrypt_from_pem(&key, pass, keyfp) != 1) {
|
||||
error_print();
|
||||
goto end;
|
||||
}
|
||||
if ((inlen = fread(inbuf, 1, sizeof(inbuf), infp)) <= 0) {
|
||||
error_print();
|
||||
goto end;
|
||||
}
|
||||
if (sm9_decrypt(&key, id, strlen(id), inbuf, inlen, outbuf, &outlen) != 1) {
|
||||
error_print();
|
||||
goto end;
|
||||
}
|
||||
if (outlen != fwrite(outbuf, 1, outlen, outfp)) {
|
||||
error_print();
|
||||
goto end;
|
||||
}
|
||||
ret = 0;
|
||||
|
||||
end:
|
||||
gmssl_secure_clear(&key, sizeof(key));
|
||||
gmssl_secure_clear(outbuf, sizeof(outbuf));
|
||||
if (keyfp) fclose(keyfp);
|
||||
if (infile && infp) fclose(infp);
|
||||
if (outfile && outfp) fclose(outfp);
|
||||
return ret;
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2014-2022 The GmSSL Project. All Rights Reserved.
|
||||
* Copyright 2022 The GmSSL Project. All Rights Reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the License); you may
|
||||
* not use this file except in compliance with the License.
|
||||
@@ -7,104 +7,103 @@
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*/
|
||||
|
||||
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <gmssl/sm9.h>
|
||||
#include <gmssl/error.h>
|
||||
|
||||
|
||||
static const char *options = "-pubmaster file -id str [-in file] [-out file]";
|
||||
|
||||
|
||||
int sm9encrypt_main(int argc, char **argv)
|
||||
{
|
||||
int ret = 1;
|
||||
char *prog = argv[0];
|
||||
char *mpkfile = NULL;
|
||||
char *id = NULL;
|
||||
char *infile = NULL;
|
||||
char *outfile = NULL;
|
||||
FILE *mpkfp = NULL;
|
||||
FILE *infp = stdin;
|
||||
FILE *outfp = stdout;
|
||||
SM9_ENC_MASTER_KEY mpk;
|
||||
uint8_t inbuf[SM9_MAX_PLAINTEXT_SIZE];
|
||||
uint8_t outbuf[SM9_MAX_CIPHERTEXT_SIZE];
|
||||
size_t inlen, outlen = sizeof(outbuf);
|
||||
|
||||
argc--;
|
||||
argv++;
|
||||
|
||||
if (argc < 1) {
|
||||
fprintf(stderr, "usage: %s %s\n", prog, options);
|
||||
return 1;
|
||||
}
|
||||
|
||||
while (argc > 0) {
|
||||
if (!strcmp(*argv, "-help")) {
|
||||
fprintf(stdout, "usage: %s %s\n", prog, options);
|
||||
return 0;
|
||||
} else if (!strcmp(*argv, "-pubmaster")) {
|
||||
if (--argc < 1) goto bad;
|
||||
mpkfile = *(++argv);
|
||||
if (!(mpkfp = fopen(mpkfile, "r"))) {
|
||||
error_print();
|
||||
goto end;
|
||||
}
|
||||
} else if (!strcmp(*argv, "-id")) {
|
||||
if (--argc < 1) goto bad;
|
||||
id = *(++argv);
|
||||
} else if (!strcmp(*argv, "-in")) {
|
||||
if (--argc < 1) goto bad;
|
||||
infile = *(++argv);
|
||||
if (!(infp = fopen(outfile, "r"))) {
|
||||
error_print();
|
||||
goto end;
|
||||
}
|
||||
} else if (!strcmp(*argv, "-out")) {
|
||||
if (--argc < 1) goto bad;
|
||||
outfile = *(++argv);
|
||||
if (!(outfp = fopen(outfile, "w"))) {
|
||||
error_print();
|
||||
goto end;
|
||||
}
|
||||
} else {
|
||||
bad:
|
||||
fprintf(stderr, "%s: illegal option '%s'\n", prog, *argv);
|
||||
return 1;
|
||||
}
|
||||
|
||||
argc--;
|
||||
argv++;
|
||||
}
|
||||
|
||||
if (!mpkfp || !id) {
|
||||
error_print();
|
||||
goto end;
|
||||
}
|
||||
if (sm9_enc_master_public_key_from_pem(&mpk, mpkfp) != 1) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
if ((inlen = fread(inbuf, 1, sizeof(inbuf), infp)) <= 0) {
|
||||
error_print();
|
||||
goto end;
|
||||
}
|
||||
if (sm9_encrypt(&mpk, id, strlen(id), inbuf, inlen, outbuf, &outlen) != 1) {
|
||||
error_print();
|
||||
goto end;
|
||||
}
|
||||
if (outlen != fwrite(outbuf, 1, outlen, outfp)) {
|
||||
error_print();
|
||||
goto end;
|
||||
}
|
||||
ret = 0;
|
||||
end:
|
||||
if (infile && infp) fclose(infp);
|
||||
if (outfile && outfp) fclose(outfp);
|
||||
if (mpkfp) fclose(mpkfp);
|
||||
return ret;
|
||||
}
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <gmssl/sm9.h>
|
||||
#include <gmssl/error.h>
|
||||
|
||||
|
||||
static const char *options = "-pubmaster file -id str [-in file] [-out file]";
|
||||
|
||||
|
||||
int sm9encrypt_main(int argc, char **argv)
|
||||
{
|
||||
int ret = 1;
|
||||
char *prog = argv[0];
|
||||
char *mpkfile = NULL;
|
||||
char *id = NULL;
|
||||
char *infile = NULL;
|
||||
char *outfile = NULL;
|
||||
FILE *mpkfp = NULL;
|
||||
FILE *infp = stdin;
|
||||
FILE *outfp = stdout;
|
||||
SM9_ENC_MASTER_KEY mpk;
|
||||
uint8_t inbuf[SM9_MAX_PLAINTEXT_SIZE];
|
||||
uint8_t outbuf[SM9_MAX_CIPHERTEXT_SIZE];
|
||||
size_t inlen, outlen = sizeof(outbuf);
|
||||
|
||||
argc--;
|
||||
argv++;
|
||||
|
||||
if (argc < 1) {
|
||||
fprintf(stderr, "usage: %s %s\n", prog, options);
|
||||
return 1;
|
||||
}
|
||||
|
||||
while (argc > 0) {
|
||||
if (!strcmp(*argv, "-help")) {
|
||||
fprintf(stdout, "usage: %s %s\n", prog, options);
|
||||
return 0;
|
||||
} else if (!strcmp(*argv, "-pubmaster")) {
|
||||
if (--argc < 1) goto bad;
|
||||
mpkfile = *(++argv);
|
||||
if (!(mpkfp = fopen(mpkfile, "r"))) {
|
||||
error_print();
|
||||
goto end;
|
||||
}
|
||||
} else if (!strcmp(*argv, "-id")) {
|
||||
if (--argc < 1) goto bad;
|
||||
id = *(++argv);
|
||||
} else if (!strcmp(*argv, "-in")) {
|
||||
if (--argc < 1) goto bad;
|
||||
infile = *(++argv);
|
||||
if (!(infp = fopen(outfile, "r"))) {
|
||||
error_print();
|
||||
goto end;
|
||||
}
|
||||
} else if (!strcmp(*argv, "-out")) {
|
||||
if (--argc < 1) goto bad;
|
||||
outfile = *(++argv);
|
||||
if (!(outfp = fopen(outfile, "w"))) {
|
||||
error_print();
|
||||
goto end;
|
||||
}
|
||||
} else {
|
||||
bad:
|
||||
fprintf(stderr, "%s: illegal option '%s'\n", prog, *argv);
|
||||
return 1;
|
||||
}
|
||||
|
||||
argc--;
|
||||
argv++;
|
||||
}
|
||||
|
||||
if (!mpkfp || !id) {
|
||||
error_print();
|
||||
goto end;
|
||||
}
|
||||
if (sm9_enc_master_public_key_from_pem(&mpk, mpkfp) != 1) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
if ((inlen = fread(inbuf, 1, sizeof(inbuf), infp)) <= 0) {
|
||||
error_print();
|
||||
goto end;
|
||||
}
|
||||
if (sm9_encrypt(&mpk, id, strlen(id), inbuf, inlen, outbuf, &outlen) != 1) {
|
||||
error_print();
|
||||
goto end;
|
||||
}
|
||||
if (outlen != fwrite(outbuf, 1, outlen, outfp)) {
|
||||
error_print();
|
||||
goto end;
|
||||
}
|
||||
ret = 0;
|
||||
end:
|
||||
if (infile && infp) fclose(infp);
|
||||
if (outfile && outfp) fclose(outfp);
|
||||
if (mpkfp) fclose(mpkfp);
|
||||
return ret;
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2014-2022 The GmSSL Project. All Rights Reserved.
|
||||
* Copyright 2022 The GmSSL Project. All Rights Reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the License); you may
|
||||
* not use this file except in compliance with the License.
|
||||
@@ -7,126 +7,125 @@
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*/
|
||||
|
||||
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <gmssl/mem.h>
|
||||
#include <gmssl/oid.h>
|
||||
#include <gmssl/sm9.h>
|
||||
#include <gmssl/error.h>
|
||||
|
||||
static const char *options = "-alg (sm9sign|sm9encrypt) -in master_key.pem -inpass str -id str [-out pem] -outpass str";
|
||||
|
||||
int sm9keygen_main(int argc, char **argv)
|
||||
{
|
||||
int ret = -1;
|
||||
char *prog = argv[0];
|
||||
char *alg = NULL;
|
||||
char *infile = NULL;
|
||||
char *inpass = NULL;
|
||||
char *id = NULL;
|
||||
char *outfile = NULL;
|
||||
char *outpass = NULL;
|
||||
int oid = 0;
|
||||
FILE *infp = stdin;
|
||||
FILE *outfp = stdout;
|
||||
SM9_SIGN_MASTER_KEY sign_msk;
|
||||
SM9_ENC_MASTER_KEY enc_msk;
|
||||
SM9_SIGN_KEY sign_key;
|
||||
SM9_ENC_KEY enc_key;
|
||||
|
||||
argc--;
|
||||
argv++;
|
||||
|
||||
if (argc < 1) {
|
||||
fprintf(stderr, "usage: %s %s\n", prog, options);
|
||||
return 1;
|
||||
}
|
||||
|
||||
while (argc > 0) {
|
||||
if (!strcmp(*argv, "-help")) {
|
||||
fprintf(stdout, "usage: %s %s\n", prog, options);
|
||||
return 0;
|
||||
} else if (!strcmp(*argv, "-alg")) {
|
||||
if (--argc < 1) goto bad;
|
||||
alg = *(++argv);
|
||||
if ((oid = sm9_oid_from_name(alg)) < 1) {
|
||||
fprintf(stdout, "%s: invalid alg '%s', should be sm9sign or sm9encrypt\n", prog, alg);
|
||||
goto end;
|
||||
}
|
||||
} else if (!strcmp(*argv, "-in")) {
|
||||
if (--argc < 1) goto bad;
|
||||
infile = *(++argv);
|
||||
if (!(infp = fopen(infile, "r"))) {
|
||||
error_print();
|
||||
goto end;
|
||||
}
|
||||
} else if (!strcmp(*argv, "-inpass")) {
|
||||
if (--argc < 1) goto bad;
|
||||
inpass = *(++argv);
|
||||
} else if (!strcmp(*argv, "-id")) {
|
||||
if (--argc < 1) goto bad;
|
||||
id = *(++argv);
|
||||
} else if (!strcmp(*argv, "-out")) {
|
||||
if (--argc < 1) goto bad;
|
||||
outfile = *(++argv);
|
||||
if (!(outfp = fopen(outfile, "w"))) {
|
||||
error_print();
|
||||
goto end;
|
||||
}
|
||||
} else if (!strcmp(*argv, "-outpass")) {
|
||||
if (--argc < 1) goto bad;
|
||||
outpass = *(++argv);
|
||||
} else {
|
||||
bad:
|
||||
fprintf(stderr, "%s: illegal option '%s'\n", prog, *argv);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
argc--;
|
||||
argv++;
|
||||
}
|
||||
|
||||
if (!id) {
|
||||
fprintf(stderr, "%s: option '-id' is required\n", prog);
|
||||
goto end;
|
||||
}
|
||||
if (!inpass || !outpass) {
|
||||
error_print();
|
||||
goto end;
|
||||
}
|
||||
|
||||
switch (oid) {
|
||||
case OID_sm9sign:
|
||||
if (sm9_sign_master_key_info_decrypt_from_pem(&sign_msk, inpass, infp) != 1
|
||||
|| sm9_sign_master_key_extract_key(&sign_msk, id, strlen(id), &sign_key) != 1
|
||||
|| sm9_sign_key_info_encrypt_to_pem(&sign_key, outpass, outfp) != 1) {
|
||||
error_print();
|
||||
goto end;
|
||||
}
|
||||
break;
|
||||
case OID_sm9encrypt:
|
||||
if (sm9_enc_master_key_info_decrypt_from_pem(&enc_msk, inpass, infp) != 1
|
||||
|| sm9_enc_master_key_extract_key(&enc_msk, id, strlen(id), &enc_key) != 1
|
||||
|| sm9_enc_key_info_encrypt_to_pem(&enc_key, outpass, outfp) != 1) {
|
||||
error_print();
|
||||
goto end;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
error_print();
|
||||
goto end;
|
||||
}
|
||||
ret = 0;
|
||||
end:
|
||||
gmssl_secure_clear(&sign_msk, sizeof(sign_msk));
|
||||
gmssl_secure_clear(&enc_msk, sizeof(enc_msk));
|
||||
gmssl_secure_clear(&sign_key, sizeof(sign_key));
|
||||
gmssl_secure_clear(&enc_key, sizeof(enc_key));
|
||||
if (infile && infp) fclose(infp);
|
||||
if (outfile && outfp) fclose(outfp);
|
||||
return 1;
|
||||
}
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <gmssl/mem.h>
|
||||
#include <gmssl/oid.h>
|
||||
#include <gmssl/sm9.h>
|
||||
#include <gmssl/error.h>
|
||||
|
||||
static const char *options = "-alg (sm9sign|sm9encrypt) -in master_key.pem -inpass str -id str [-out pem] -outpass str";
|
||||
|
||||
int sm9keygen_main(int argc, char **argv)
|
||||
{
|
||||
int ret = -1;
|
||||
char *prog = argv[0];
|
||||
char *alg = NULL;
|
||||
char *infile = NULL;
|
||||
char *inpass = NULL;
|
||||
char *id = NULL;
|
||||
char *outfile = NULL;
|
||||
char *outpass = NULL;
|
||||
int oid = 0;
|
||||
FILE *infp = stdin;
|
||||
FILE *outfp = stdout;
|
||||
SM9_SIGN_MASTER_KEY sign_msk;
|
||||
SM9_ENC_MASTER_KEY enc_msk;
|
||||
SM9_SIGN_KEY sign_key;
|
||||
SM9_ENC_KEY enc_key;
|
||||
|
||||
argc--;
|
||||
argv++;
|
||||
|
||||
if (argc < 1) {
|
||||
fprintf(stderr, "usage: %s %s\n", prog, options);
|
||||
return 1;
|
||||
}
|
||||
|
||||
while (argc > 0) {
|
||||
if (!strcmp(*argv, "-help")) {
|
||||
fprintf(stdout, "usage: %s %s\n", prog, options);
|
||||
return 0;
|
||||
} else if (!strcmp(*argv, "-alg")) {
|
||||
if (--argc < 1) goto bad;
|
||||
alg = *(++argv);
|
||||
if ((oid = sm9_oid_from_name(alg)) < 1) {
|
||||
fprintf(stdout, "%s: invalid alg '%s', should be sm9sign or sm9encrypt\n", prog, alg);
|
||||
goto end;
|
||||
}
|
||||
} else if (!strcmp(*argv, "-in")) {
|
||||
if (--argc < 1) goto bad;
|
||||
infile = *(++argv);
|
||||
if (!(infp = fopen(infile, "r"))) {
|
||||
error_print();
|
||||
goto end;
|
||||
}
|
||||
} else if (!strcmp(*argv, "-inpass")) {
|
||||
if (--argc < 1) goto bad;
|
||||
inpass = *(++argv);
|
||||
} else if (!strcmp(*argv, "-id")) {
|
||||
if (--argc < 1) goto bad;
|
||||
id = *(++argv);
|
||||
} else if (!strcmp(*argv, "-out")) {
|
||||
if (--argc < 1) goto bad;
|
||||
outfile = *(++argv);
|
||||
if (!(outfp = fopen(outfile, "w"))) {
|
||||
error_print();
|
||||
goto end;
|
||||
}
|
||||
} else if (!strcmp(*argv, "-outpass")) {
|
||||
if (--argc < 1) goto bad;
|
||||
outpass = *(++argv);
|
||||
} else {
|
||||
bad:
|
||||
fprintf(stderr, "%s: illegal option '%s'\n", prog, *argv);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
argc--;
|
||||
argv++;
|
||||
}
|
||||
|
||||
if (!id) {
|
||||
fprintf(stderr, "%s: option '-id' is required\n", prog);
|
||||
goto end;
|
||||
}
|
||||
if (!inpass || !outpass) {
|
||||
error_print();
|
||||
goto end;
|
||||
}
|
||||
|
||||
switch (oid) {
|
||||
case OID_sm9sign:
|
||||
if (sm9_sign_master_key_info_decrypt_from_pem(&sign_msk, inpass, infp) != 1
|
||||
|| sm9_sign_master_key_extract_key(&sign_msk, id, strlen(id), &sign_key) != 1
|
||||
|| sm9_sign_key_info_encrypt_to_pem(&sign_key, outpass, outfp) != 1) {
|
||||
error_print();
|
||||
goto end;
|
||||
}
|
||||
break;
|
||||
case OID_sm9encrypt:
|
||||
if (sm9_enc_master_key_info_decrypt_from_pem(&enc_msk, inpass, infp) != 1
|
||||
|| sm9_enc_master_key_extract_key(&enc_msk, id, strlen(id), &enc_key) != 1
|
||||
|| sm9_enc_key_info_encrypt_to_pem(&enc_key, outpass, outfp) != 1) {
|
||||
error_print();
|
||||
goto end;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
error_print();
|
||||
goto end;
|
||||
}
|
||||
ret = 0;
|
||||
end:
|
||||
gmssl_secure_clear(&sign_msk, sizeof(sign_msk));
|
||||
gmssl_secure_clear(&enc_msk, sizeof(enc_msk));
|
||||
gmssl_secure_clear(&sign_key, sizeof(sign_key));
|
||||
gmssl_secure_clear(&enc_key, sizeof(enc_key));
|
||||
if (infile && infp) fclose(infp);
|
||||
if (outfile && outfp) fclose(outfp);
|
||||
return 1;
|
||||
}
|
||||
|
||||
263
tools/sm9setup.c
263
tools/sm9setup.c
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2014-2022 The GmSSL Project. All Rights Reserved.
|
||||
* Copyright 2022 The GmSSL Project. All Rights Reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the License); you may
|
||||
* not use this file except in compliance with the License.
|
||||
@@ -7,134 +7,133 @@
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*/
|
||||
|
||||
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <gmssl/oid.h>
|
||||
#include <gmssl/mem.h>
|
||||
#include <gmssl/sm9.h>
|
||||
#include <gmssl/error.h>
|
||||
|
||||
static const char *options = "-alg (sm9sign|sm9encrypt) [-pass password] [-out pem] [-pubout pem]";
|
||||
|
||||
int sm9setup_main(int argc, char **argv)
|
||||
{
|
||||
int ret = 1;
|
||||
char *prog = argv[0];
|
||||
char *alg = NULL;
|
||||
char *pass = NULL;
|
||||
char *outfile = NULL;
|
||||
char *puboutfile = NULL;
|
||||
int oid;
|
||||
FILE *outfp = stdout;
|
||||
FILE *puboutfp = stdout;
|
||||
SM9_SIGN_MASTER_KEY sign_msk;
|
||||
SM9_ENC_MASTER_KEY enc_msk;
|
||||
|
||||
argc--;
|
||||
argv++;
|
||||
|
||||
if (argc < 1) {
|
||||
fprintf(stderr, "usage: %s %s\n", prog, options);
|
||||
return 1;
|
||||
}
|
||||
|
||||
while (argc > 0) {
|
||||
if (!strcmp(*argv, "-help")) {
|
||||
fprintf(stdout, "usage: %s %s\n", prog, options);
|
||||
return 0;
|
||||
} else if (!strcmp(*argv, "-alg")) {
|
||||
if (--argc < 1) goto bad;
|
||||
alg = *(++argv);
|
||||
if ((oid = sm9_oid_from_name(alg)) < 1) {
|
||||
fprintf(stdout, "%s: invalid alg '%s', should be sm9sign or sm9encrypt\n", prog, alg);
|
||||
goto end;
|
||||
}
|
||||
} else if (!strcmp(*argv, "-pass")) {
|
||||
if (--argc < 1) goto bad;
|
||||
pass = *(++argv);
|
||||
} else if (!strcmp(*argv, "-out")) {
|
||||
if (--argc < 1) goto bad;
|
||||
outfile = *(++argv);
|
||||
if (!(outfp = fopen(outfile, "w"))) {
|
||||
error_print();
|
||||
goto end;
|
||||
}
|
||||
} else if (!strcmp(*argv, "-pubout")) {
|
||||
if (--argc < 1) goto bad;
|
||||
puboutfile = *(++argv);
|
||||
if (!(puboutfp = fopen(puboutfile, "w"))) {
|
||||
error_print();
|
||||
goto end;
|
||||
}
|
||||
} else {
|
||||
bad:
|
||||
fprintf(stderr, "%s: illegal option '%s'\n", prog, *argv);
|
||||
return 1;
|
||||
}
|
||||
|
||||
argc--;
|
||||
argv++;
|
||||
}
|
||||
|
||||
if (!alg) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
if (!pass) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
|
||||
switch (oid) {
|
||||
case OID_sm9sign:
|
||||
if (sm9_sign_master_key_generate(&sign_msk) != 1
|
||||
|| sm9_sign_master_key_info_encrypt_to_pem(&sign_msk, pass, outfp) != 1
|
||||
|| sm9_sign_master_public_key_to_pem(&sign_msk, puboutfp) != 1) {
|
||||
error_print();
|
||||
goto end;
|
||||
}
|
||||
break;
|
||||
case OID_sm9encrypt:
|
||||
if (sm9_enc_master_key_generate(&enc_msk) != 1
|
||||
|| sm9_enc_master_key_info_encrypt_to_pem(&enc_msk, pass, outfp) != 1
|
||||
|| sm9_enc_master_public_key_to_pem(&enc_msk, puboutfp) != 1) {
|
||||
error_print();
|
||||
goto end;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
error_print();
|
||||
goto end;
|
||||
}
|
||||
ret = 0;
|
||||
|
||||
end:
|
||||
gmssl_secure_clear(&sign_msk, sizeof(sign_msk));
|
||||
gmssl_secure_clear(&enc_msk, sizeof(enc_msk));
|
||||
if (outfile && outfp) fclose(outfp);
|
||||
if (puboutfile && puboutfp) fclose(puboutfp);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <gmssl/oid.h>
|
||||
#include <gmssl/mem.h>
|
||||
#include <gmssl/sm9.h>
|
||||
#include <gmssl/error.h>
|
||||
|
||||
static const char *options = "-alg (sm9sign|sm9encrypt) [-pass password] [-out pem] [-pubout pem]";
|
||||
|
||||
int sm9setup_main(int argc, char **argv)
|
||||
{
|
||||
int ret = 1;
|
||||
char *prog = argv[0];
|
||||
char *alg = NULL;
|
||||
char *pass = NULL;
|
||||
char *outfile = NULL;
|
||||
char *puboutfile = NULL;
|
||||
int oid;
|
||||
FILE *outfp = stdout;
|
||||
FILE *puboutfp = stdout;
|
||||
SM9_SIGN_MASTER_KEY sign_msk;
|
||||
SM9_ENC_MASTER_KEY enc_msk;
|
||||
|
||||
argc--;
|
||||
argv++;
|
||||
|
||||
if (argc < 1) {
|
||||
fprintf(stderr, "usage: %s %s\n", prog, options);
|
||||
return 1;
|
||||
}
|
||||
|
||||
while (argc > 0) {
|
||||
if (!strcmp(*argv, "-help")) {
|
||||
fprintf(stdout, "usage: %s %s\n", prog, options);
|
||||
return 0;
|
||||
} else if (!strcmp(*argv, "-alg")) {
|
||||
if (--argc < 1) goto bad;
|
||||
alg = *(++argv);
|
||||
if ((oid = sm9_oid_from_name(alg)) < 1) {
|
||||
fprintf(stdout, "%s: invalid alg '%s', should be sm9sign or sm9encrypt\n", prog, alg);
|
||||
goto end;
|
||||
}
|
||||
} else if (!strcmp(*argv, "-pass")) {
|
||||
if (--argc < 1) goto bad;
|
||||
pass = *(++argv);
|
||||
} else if (!strcmp(*argv, "-out")) {
|
||||
if (--argc < 1) goto bad;
|
||||
outfile = *(++argv);
|
||||
if (!(outfp = fopen(outfile, "w"))) {
|
||||
error_print();
|
||||
goto end;
|
||||
}
|
||||
} else if (!strcmp(*argv, "-pubout")) {
|
||||
if (--argc < 1) goto bad;
|
||||
puboutfile = *(++argv);
|
||||
if (!(puboutfp = fopen(puboutfile, "w"))) {
|
||||
error_print();
|
||||
goto end;
|
||||
}
|
||||
} else {
|
||||
bad:
|
||||
fprintf(stderr, "%s: illegal option '%s'\n", prog, *argv);
|
||||
return 1;
|
||||
}
|
||||
|
||||
argc--;
|
||||
argv++;
|
||||
}
|
||||
|
||||
if (!alg) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
if (!pass) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
|
||||
switch (oid) {
|
||||
case OID_sm9sign:
|
||||
if (sm9_sign_master_key_generate(&sign_msk) != 1
|
||||
|| sm9_sign_master_key_info_encrypt_to_pem(&sign_msk, pass, outfp) != 1
|
||||
|| sm9_sign_master_public_key_to_pem(&sign_msk, puboutfp) != 1) {
|
||||
error_print();
|
||||
goto end;
|
||||
}
|
||||
break;
|
||||
case OID_sm9encrypt:
|
||||
if (sm9_enc_master_key_generate(&enc_msk) != 1
|
||||
|| sm9_enc_master_key_info_encrypt_to_pem(&enc_msk, pass, outfp) != 1
|
||||
|| sm9_enc_master_public_key_to_pem(&enc_msk, puboutfp) != 1) {
|
||||
error_print();
|
||||
goto end;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
error_print();
|
||||
goto end;
|
||||
}
|
||||
ret = 0;
|
||||
|
||||
end:
|
||||
gmssl_secure_clear(&sign_msk, sizeof(sign_msk));
|
||||
gmssl_secure_clear(&enc_msk, sizeof(enc_msk));
|
||||
if (outfile && outfp) fclose(outfp);
|
||||
if (puboutfile && puboutfp) fclose(puboutfp);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
239
tools/sm9sign.c
239
tools/sm9sign.c
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2014-2022 The GmSSL Project. All Rights Reserved.
|
||||
* Copyright 2022 The GmSSL Project. All Rights Reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the License); you may
|
||||
* not use this file except in compliance with the License.
|
||||
@@ -7,122 +7,121 @@
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*/
|
||||
|
||||
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <gmssl/mem.h>
|
||||
#include <gmssl/sm9.h>
|
||||
#include <gmssl/error.h>
|
||||
|
||||
|
||||
static const char *options = "[-in file] -key file -pass str [-out file]";
|
||||
|
||||
|
||||
int sm9sign_main(int argc, char **argv)
|
||||
{
|
||||
int ret = 1;
|
||||
char *prog = argv[0];
|
||||
char *infile = NULL;
|
||||
char *keyfile = NULL;
|
||||
char *pass = NULL;
|
||||
char *outfile = NULL;
|
||||
FILE *infp = stdin;
|
||||
FILE *keyfp = NULL;
|
||||
FILE *outfp = stdout;
|
||||
SM9_SIGN_KEY key;
|
||||
SM9_SIGN_CTX ctx;
|
||||
uint8_t buf[4096];
|
||||
ssize_t len;
|
||||
uint8_t sig[SM9_SIGNATURE_SIZE];
|
||||
size_t siglen;
|
||||
|
||||
argc--;
|
||||
argv++;
|
||||
|
||||
if (argc < 1) {
|
||||
fprintf(stderr, "usage: %s %s\n", prog, options);
|
||||
return 1;
|
||||
}
|
||||
|
||||
while (argc > 0) {
|
||||
if (!strcmp(*argv, "-help")) {
|
||||
fprintf(stdout, "usage: %s %s\n", prog, options);
|
||||
return 0;
|
||||
} else if (!strcmp(*argv, "-in")) {
|
||||
if (--argc < 1) goto bad;
|
||||
infile = *(++argv);
|
||||
if (!(infp = fopen(infile, "r"))) {
|
||||
error_print();
|
||||
goto end;
|
||||
}
|
||||
} else if (!strcmp(*argv, "-key")) {
|
||||
if (--argc < 1) goto bad;
|
||||
keyfile = *(++argv);
|
||||
if (!(keyfp = fopen(keyfile, "r"))) {
|
||||
error_print();
|
||||
goto end;
|
||||
}
|
||||
} else if (!strcmp(*argv, "-pass")) {
|
||||
if (--argc < 1) goto bad;
|
||||
pass = *(++argv);
|
||||
} else if (!strcmp(*argv, "-out")) {
|
||||
if (--argc < 1) goto bad;
|
||||
outfile = *(++argv);
|
||||
if (!(outfp = fopen(outfile, "w"))) {
|
||||
error_print();
|
||||
goto end;
|
||||
}
|
||||
} else {
|
||||
bad:
|
||||
fprintf(stderr, "%s: illegal option '%s'\n", prog, *argv);
|
||||
return 1;
|
||||
}
|
||||
|
||||
argc--;
|
||||
argv++;
|
||||
}
|
||||
|
||||
if (!keyfile || !pass) {
|
||||
error_print();
|
||||
goto end;
|
||||
}
|
||||
|
||||
if (sm9_sign_key_info_decrypt_from_pem(&key, pass, keyfp) != 1) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (sm9_sign_init(&ctx) != 1) {
|
||||
error_print();
|
||||
goto end;
|
||||
}
|
||||
while ((len = fread(buf, 1, sizeof(buf), infp)) > 0) {
|
||||
if (sm9_sign_update(&ctx, buf, len) != 1) {
|
||||
error_print();
|
||||
goto end;
|
||||
}
|
||||
}
|
||||
if (sm9_sign_finish(&ctx, &key, sig, &siglen) != 1) {
|
||||
error_print();
|
||||
goto end;
|
||||
}
|
||||
|
||||
if (siglen != fwrite(sig, 1, siglen, outfp)) {
|
||||
error_print();
|
||||
goto end;
|
||||
}
|
||||
|
||||
|
||||
|
||||
ret = 0;
|
||||
|
||||
end:
|
||||
gmssl_secure_clear(&key, sizeof(key));
|
||||
gmssl_secure_clear(&ctx, sizeof(ctx));
|
||||
gmssl_secure_clear(buf, sizeof(buf));
|
||||
if (infile && infp) fclose(infp);
|
||||
if (outfile && outfp) fclose(outfp);
|
||||
return ret;
|
||||
}
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <gmssl/mem.h>
|
||||
#include <gmssl/sm9.h>
|
||||
#include <gmssl/error.h>
|
||||
|
||||
|
||||
static const char *options = "[-in file] -key file -pass str [-out file]";
|
||||
|
||||
|
||||
int sm9sign_main(int argc, char **argv)
|
||||
{
|
||||
int ret = 1;
|
||||
char *prog = argv[0];
|
||||
char *infile = NULL;
|
||||
char *keyfile = NULL;
|
||||
char *pass = NULL;
|
||||
char *outfile = NULL;
|
||||
FILE *infp = stdin;
|
||||
FILE *keyfp = NULL;
|
||||
FILE *outfp = stdout;
|
||||
SM9_SIGN_KEY key;
|
||||
SM9_SIGN_CTX ctx;
|
||||
uint8_t buf[4096];
|
||||
ssize_t len;
|
||||
uint8_t sig[SM9_SIGNATURE_SIZE];
|
||||
size_t siglen;
|
||||
|
||||
argc--;
|
||||
argv++;
|
||||
|
||||
if (argc < 1) {
|
||||
fprintf(stderr, "usage: %s %s\n", prog, options);
|
||||
return 1;
|
||||
}
|
||||
|
||||
while (argc > 0) {
|
||||
if (!strcmp(*argv, "-help")) {
|
||||
fprintf(stdout, "usage: %s %s\n", prog, options);
|
||||
return 0;
|
||||
} else if (!strcmp(*argv, "-in")) {
|
||||
if (--argc < 1) goto bad;
|
||||
infile = *(++argv);
|
||||
if (!(infp = fopen(infile, "r"))) {
|
||||
error_print();
|
||||
goto end;
|
||||
}
|
||||
} else if (!strcmp(*argv, "-key")) {
|
||||
if (--argc < 1) goto bad;
|
||||
keyfile = *(++argv);
|
||||
if (!(keyfp = fopen(keyfile, "r"))) {
|
||||
error_print();
|
||||
goto end;
|
||||
}
|
||||
} else if (!strcmp(*argv, "-pass")) {
|
||||
if (--argc < 1) goto bad;
|
||||
pass = *(++argv);
|
||||
} else if (!strcmp(*argv, "-out")) {
|
||||
if (--argc < 1) goto bad;
|
||||
outfile = *(++argv);
|
||||
if (!(outfp = fopen(outfile, "w"))) {
|
||||
error_print();
|
||||
goto end;
|
||||
}
|
||||
} else {
|
||||
bad:
|
||||
fprintf(stderr, "%s: illegal option '%s'\n", prog, *argv);
|
||||
return 1;
|
||||
}
|
||||
|
||||
argc--;
|
||||
argv++;
|
||||
}
|
||||
|
||||
if (!keyfile || !pass) {
|
||||
error_print();
|
||||
goto end;
|
||||
}
|
||||
|
||||
if (sm9_sign_key_info_decrypt_from_pem(&key, pass, keyfp) != 1) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (sm9_sign_init(&ctx) != 1) {
|
||||
error_print();
|
||||
goto end;
|
||||
}
|
||||
while ((len = fread(buf, 1, sizeof(buf), infp)) > 0) {
|
||||
if (sm9_sign_update(&ctx, buf, len) != 1) {
|
||||
error_print();
|
||||
goto end;
|
||||
}
|
||||
}
|
||||
if (sm9_sign_finish(&ctx, &key, sig, &siglen) != 1) {
|
||||
error_print();
|
||||
goto end;
|
||||
}
|
||||
|
||||
if (siglen != fwrite(sig, 1, siglen, outfp)) {
|
||||
error_print();
|
||||
goto end;
|
||||
}
|
||||
|
||||
|
||||
|
||||
ret = 0;
|
||||
|
||||
end:
|
||||
gmssl_secure_clear(&key, sizeof(key));
|
||||
gmssl_secure_clear(&ctx, sizeof(ctx));
|
||||
gmssl_secure_clear(buf, sizeof(buf));
|
||||
if (infile && infp) fclose(infp);
|
||||
if (outfile && outfp) fclose(outfp);
|
||||
return ret;
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2014-2022 The GmSSL Project. All Rights Reserved.
|
||||
* Copyright 2022 The GmSSL Project. All Rights Reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the License); you may
|
||||
* not use this file except in compliance with the License.
|
||||
@@ -7,123 +7,122 @@
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*/
|
||||
|
||||
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <gmssl/sm9.h>
|
||||
#include <gmssl/error.h>
|
||||
|
||||
|
||||
static const char *options = "[-in file] -pubmaster file -id str -sig file";
|
||||
|
||||
int sm9verify_main(int argc, char **argv)
|
||||
{
|
||||
int ret = 1;
|
||||
char *prog = argv[0];
|
||||
char *infile = NULL;
|
||||
char *mpkfile = NULL;
|
||||
char *id = NULL;
|
||||
char *sigfile = NULL;
|
||||
FILE *infp = stdin;
|
||||
FILE *mpkfp = NULL;
|
||||
FILE *sigfp = NULL;
|
||||
SM9_SIGN_MASTER_KEY mpk;
|
||||
SM9_SIGN_CTX ctx;
|
||||
uint8_t buf[4096];
|
||||
ssize_t len;
|
||||
uint8_t sig[SM9_SIGNATURE_SIZE];
|
||||
ssize_t siglen;
|
||||
|
||||
argc--;
|
||||
argv++;
|
||||
|
||||
if (argc < 1) {
|
||||
fprintf(stderr, "usage: %s %s\n", prog, options);
|
||||
return 1;
|
||||
}
|
||||
|
||||
while (argc > 0) {
|
||||
if (!strcmp(*argv, "-help")) {
|
||||
fprintf(stdout, "usage: %s %s\n", prog, options);
|
||||
return 0;
|
||||
} else if (!strcmp(*argv, "-in")) {
|
||||
if (--argc < 1) goto bad;
|
||||
infile = *(++argv);
|
||||
if (!(infp = fopen(infile, "r"))) {
|
||||
error_print();
|
||||
goto end;
|
||||
}
|
||||
} else if (!strcmp(*argv, "-pubmaster")) {
|
||||
if (--argc < 1) goto bad;
|
||||
mpkfile = *(++argv);
|
||||
if (!(mpkfp = fopen(mpkfile, "r"))) {
|
||||
error_print();
|
||||
goto end;
|
||||
}
|
||||
} else if (!strcmp(*argv, "-id")) {
|
||||
if (--argc < 1) goto bad;
|
||||
id = *(++argv);
|
||||
} else if (!strcmp(*argv, "-sig")) {
|
||||
if (--argc < 1) goto bad;
|
||||
sigfile = *(++argv);
|
||||
if (!(sigfp = fopen(sigfile, "r"))) {
|
||||
error_print();
|
||||
goto end;
|
||||
}
|
||||
} else {
|
||||
bad:
|
||||
fprintf(stderr, "%s: illegal option '%s'\n", prog, *argv);
|
||||
return 1;
|
||||
}
|
||||
|
||||
argc--;
|
||||
argv++;
|
||||
}
|
||||
|
||||
if (!mpkfile || !id || !sigfile) {
|
||||
error_print();
|
||||
goto end;
|
||||
}
|
||||
|
||||
if (sm9_sign_master_public_key_from_pem(&mpk, mpkfp) != 1) {
|
||||
error_print();
|
||||
goto end;
|
||||
}
|
||||
|
||||
if ((siglen = fread(sig, 1, sizeof(sig), sigfp)) <= 0) {
|
||||
error_print();
|
||||
goto end;
|
||||
}
|
||||
|
||||
if (sm9_verify_init(&ctx) != 1) {
|
||||
error_print();
|
||||
goto end;
|
||||
}
|
||||
while ((len = fread(buf, 1, sizeof(buf), infp)) > 0) {
|
||||
if (sm9_verify_update(&ctx, buf, len) != 1) {
|
||||
error_print();
|
||||
goto end;
|
||||
}
|
||||
}
|
||||
if ((ret = sm9_verify_finish(&ctx, sig, siglen, &mpk, id, strlen(id))) != 1) {
|
||||
error_print();
|
||||
goto end;
|
||||
}
|
||||
printf("%s %s\n", prog, ret ? "success" : "failure");
|
||||
|
||||
end:
|
||||
if (infile && infp) fclose(infp);
|
||||
if (mpkfile && mpkfp) fclose(mpkfp);
|
||||
if (sigfile && sigfp) fclose(sigfp);
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <gmssl/sm9.h>
|
||||
#include <gmssl/error.h>
|
||||
|
||||
|
||||
static const char *options = "[-in file] -pubmaster file -id str -sig file";
|
||||
|
||||
int sm9verify_main(int argc, char **argv)
|
||||
{
|
||||
int ret = 1;
|
||||
char *prog = argv[0];
|
||||
char *infile = NULL;
|
||||
char *mpkfile = NULL;
|
||||
char *id = NULL;
|
||||
char *sigfile = NULL;
|
||||
FILE *infp = stdin;
|
||||
FILE *mpkfp = NULL;
|
||||
FILE *sigfp = NULL;
|
||||
SM9_SIGN_MASTER_KEY mpk;
|
||||
SM9_SIGN_CTX ctx;
|
||||
uint8_t buf[4096];
|
||||
ssize_t len;
|
||||
uint8_t sig[SM9_SIGNATURE_SIZE];
|
||||
ssize_t siglen;
|
||||
|
||||
argc--;
|
||||
argv++;
|
||||
|
||||
if (argc < 1) {
|
||||
fprintf(stderr, "usage: %s %s\n", prog, options);
|
||||
return 1;
|
||||
}
|
||||
|
||||
while (argc > 0) {
|
||||
if (!strcmp(*argv, "-help")) {
|
||||
fprintf(stdout, "usage: %s %s\n", prog, options);
|
||||
return 0;
|
||||
} else if (!strcmp(*argv, "-in")) {
|
||||
if (--argc < 1) goto bad;
|
||||
infile = *(++argv);
|
||||
if (!(infp = fopen(infile, "r"))) {
|
||||
error_print();
|
||||
goto end;
|
||||
}
|
||||
} else if (!strcmp(*argv, "-pubmaster")) {
|
||||
if (--argc < 1) goto bad;
|
||||
mpkfile = *(++argv);
|
||||
if (!(mpkfp = fopen(mpkfile, "r"))) {
|
||||
error_print();
|
||||
goto end;
|
||||
}
|
||||
} else if (!strcmp(*argv, "-id")) {
|
||||
if (--argc < 1) goto bad;
|
||||
id = *(++argv);
|
||||
} else if (!strcmp(*argv, "-sig")) {
|
||||
if (--argc < 1) goto bad;
|
||||
sigfile = *(++argv);
|
||||
if (!(sigfp = fopen(sigfile, "r"))) {
|
||||
error_print();
|
||||
goto end;
|
||||
}
|
||||
} else {
|
||||
bad:
|
||||
fprintf(stderr, "%s: illegal option '%s'\n", prog, *argv);
|
||||
return 1;
|
||||
}
|
||||
|
||||
argc--;
|
||||
argv++;
|
||||
}
|
||||
|
||||
if (!mpkfile || !id || !sigfile) {
|
||||
error_print();
|
||||
goto end;
|
||||
}
|
||||
|
||||
if (sm9_sign_master_public_key_from_pem(&mpk, mpkfp) != 1) {
|
||||
error_print();
|
||||
goto end;
|
||||
}
|
||||
|
||||
if ((siglen = fread(sig, 1, sizeof(sig), sigfp)) <= 0) {
|
||||
error_print();
|
||||
goto end;
|
||||
}
|
||||
|
||||
if (sm9_verify_init(&ctx) != 1) {
|
||||
error_print();
|
||||
goto end;
|
||||
}
|
||||
while ((len = fread(buf, 1, sizeof(buf), infp)) > 0) {
|
||||
if (sm9_verify_update(&ctx, buf, len) != 1) {
|
||||
error_print();
|
||||
goto end;
|
||||
}
|
||||
}
|
||||
if ((ret = sm9_verify_finish(&ctx, sig, siglen, &mpk, id, strlen(id))) != 1) {
|
||||
error_print();
|
||||
goto end;
|
||||
}
|
||||
printf("%s %s\n", prog, ret ? "success" : "failure");
|
||||
|
||||
end:
|
||||
if (infile && infp) fclose(infp);
|
||||
if (mpkfile && mpkfp) fclose(mpkfp);
|
||||
if (sigfile && sigfp) fclose(sigfp);
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2014-2022 The GmSSL Project. All Rights Reserved.
|
||||
* Copyright 2022 The GmSSL Project. All Rights Reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the License); you may
|
||||
* not use this file except in compliance with the License.
|
||||
@@ -7,198 +7,197 @@
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*/
|
||||
|
||||
|
||||
|
||||
#include <stdio.h>
|
||||
#include <errno.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <netdb.h>
|
||||
#include <sys/types.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <sys/socket.h>
|
||||
#include <netinet/in.h>
|
||||
#include <gmssl/tls.h>
|
||||
#include <gmssl/error.h>
|
||||
|
||||
|
||||
static int client_ciphers[] = { TLS_cipher_ecc_sm4_cbc_sm3, };
|
||||
|
||||
static const char *http_get =
|
||||
"GET / HTTP/1.1\r\n"
|
||||
"Hostname: aaa\r\n"
|
||||
"\r\n\r\n";
|
||||
|
||||
static const char *options = "-host str [-port num] [-cacert file] [-cert file -key file -pass str]";
|
||||
|
||||
int tlcp_client_main(int argc, char *argv[])
|
||||
{
|
||||
int ret = -1;
|
||||
char *prog = argv[0];
|
||||
char *host = NULL;
|
||||
int port = 443;
|
||||
char *cacertfile = NULL;
|
||||
char *certfile = NULL;
|
||||
char *keyfile = NULL;
|
||||
char *pass = NULL;
|
||||
struct hostent *hp;
|
||||
struct sockaddr_in server;
|
||||
int sock;
|
||||
TLS_CTX ctx;
|
||||
TLS_CONNECT conn;
|
||||
char buf[1024] = {0};
|
||||
size_t len = sizeof(buf);
|
||||
char send_buf[1024] = {0};
|
||||
size_t send_len;
|
||||
|
||||
argc--;
|
||||
argv++;
|
||||
if (argc < 1) {
|
||||
fprintf(stderr, "usage: %s %s\n", prog, options);
|
||||
return 1;
|
||||
}
|
||||
while (argc >= 1) {
|
||||
if (!strcmp(*argv, "-help")) {
|
||||
printf("usage: %s %s\n", prog, options);
|
||||
return 0;
|
||||
} else if (!strcmp(*argv, "-host")) {
|
||||
if (--argc < 1) goto bad;
|
||||
host = *(++argv);
|
||||
} else if (!strcmp(*argv, "-port")) {
|
||||
if (--argc < 1) goto bad;
|
||||
port = atoi(*(++argv));
|
||||
} else if (!strcmp(*argv, "-cacert")) {
|
||||
if (--argc < 1) goto bad;
|
||||
cacertfile = *(++argv);
|
||||
} else if (!strcmp(*argv, "-cert")) {
|
||||
if (--argc < 1) goto bad;
|
||||
certfile = *(++argv);
|
||||
} else if (!strcmp(*argv, "-key")) {
|
||||
if (--argc < 1) goto bad;
|
||||
keyfile = *(++argv);
|
||||
} else if (!strcmp(*argv, "-pass")) {
|
||||
if (--argc < 1) goto bad;
|
||||
pass = *(++argv);
|
||||
} else {
|
||||
fprintf(stderr, "%s: invalid option '%s'\n", prog, *argv);
|
||||
return 1;
|
||||
bad:
|
||||
fprintf(stderr, "%s: option '%s' argument required\n", prog, *argv);
|
||||
return 0;
|
||||
}
|
||||
argc--;
|
||||
argv++;
|
||||
}
|
||||
|
||||
if (!host) {
|
||||
fprintf(stderr, "%s: '-in' option required\n", prog);
|
||||
return -1;
|
||||
}
|
||||
if (!(hp = gethostbyname(host))) {
|
||||
herror("tlcp_client: '-host' invalid");
|
||||
goto end;
|
||||
}
|
||||
|
||||
memset(&ctx, 0, sizeof(ctx));
|
||||
memset(&conn, 0, sizeof(conn));
|
||||
|
||||
server.sin_addr = *((struct in_addr *)hp->h_addr_list[0]);
|
||||
server.sin_family = AF_INET;
|
||||
server.sin_port = htons(port);
|
||||
|
||||
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
|
||||
fprintf(stderr, "%s: open socket error : %s\n", prog, strerror(errno));
|
||||
goto end;
|
||||
}
|
||||
if (connect(sock, (struct sockaddr *)&server , sizeof(server)) < 0) {
|
||||
fprintf(stderr, "%s: connect error : %s\n", prog, strerror(errno));
|
||||
goto end;
|
||||
}
|
||||
|
||||
if (tls_ctx_init(&ctx, TLS_protocol_tlcp, TLS_client_mode) != 1
|
||||
|| tls_ctx_set_cipher_suites(&ctx, client_ciphers, sizeof(client_ciphers)/sizeof(client_ciphers[0])) != 1) {
|
||||
fprintf(stderr, "%s: context init error\n", prog);
|
||||
goto end;
|
||||
}
|
||||
if (cacertfile) {
|
||||
if (tls_ctx_set_ca_certificates(&ctx, cacertfile, TLS_DEFAULT_VERIFY_DEPTH) != 1) {
|
||||
fprintf(stderr, "%s: context init error\n", prog);
|
||||
goto end;
|
||||
}
|
||||
}
|
||||
if (certfile) {
|
||||
if (tls_ctx_set_certificate_and_key(&ctx, certfile, keyfile, pass) != 1) {
|
||||
fprintf(stderr, "%s: context init error\n", prog);
|
||||
goto end;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (tls_init(&conn, &ctx) != 1
|
||||
|| tls_set_socket(&conn, sock) != 1
|
||||
|| tls_do_handshake(&conn) != 1) {
|
||||
fprintf(stderr, "%s: error\n", prog);
|
||||
goto end;
|
||||
}
|
||||
|
||||
|
||||
|
||||
for (;;) {
|
||||
fd_set fds;
|
||||
size_t sentlen;
|
||||
|
||||
FD_ZERO(&fds);
|
||||
FD_SET(conn.sock, &fds);
|
||||
FD_SET(STDIN_FILENO, &fds);
|
||||
|
||||
if (select(conn.sock + 1, &fds, NULL, NULL, NULL) < 0) {
|
||||
fprintf(stderr, "%s: select failed\n", prog);
|
||||
goto end;
|
||||
}
|
||||
|
||||
if (FD_ISSET(conn.sock, &fds)) {
|
||||
for (;;) {
|
||||
memset(buf, 0, sizeof(buf));
|
||||
if (tls_recv(&conn, (uint8_t *)buf, sizeof(buf), &len) != 1) {
|
||||
goto end;
|
||||
}
|
||||
fwrite(buf, 1, len, stdout);
|
||||
fflush(stdout);
|
||||
|
||||
// 应该调整tls_recv 逻辑、API或者其他方式
|
||||
if (conn.datalen == 0) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
if (FD_ISSET(STDIN_FILENO, &fds)) {
|
||||
fprintf(stderr, "recv from stdin\n");
|
||||
|
||||
memset(send_buf, 0, sizeof(send_buf));
|
||||
|
||||
if (!fgets(send_buf, sizeof(send_buf), stdin)) {
|
||||
if (feof(stdin)) {
|
||||
tls_shutdown(&conn);
|
||||
goto end;
|
||||
} else {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
if (tls_send(&conn, (uint8_t *)send_buf, strlen(send_buf), &sentlen) != 1) {
|
||||
fprintf(stderr, "%s: send error\n", prog);
|
||||
goto end;
|
||||
}
|
||||
}
|
||||
|
||||
fprintf(stderr, "end of this round\n");
|
||||
}
|
||||
|
||||
|
||||
end:
|
||||
close(sock);
|
||||
tls_ctx_cleanup(&ctx);
|
||||
tls_cleanup(&conn);
|
||||
return 0;
|
||||
}
|
||||
|
||||
#include <stdio.h>
|
||||
#include <errno.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <netdb.h>
|
||||
#include <sys/types.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <sys/socket.h>
|
||||
#include <netinet/in.h>
|
||||
#include <gmssl/tls.h>
|
||||
#include <gmssl/error.h>
|
||||
|
||||
|
||||
static int client_ciphers[] = { TLS_cipher_ecc_sm4_cbc_sm3, };
|
||||
|
||||
static const char *http_get =
|
||||
"GET / HTTP/1.1\r\n"
|
||||
"Hostname: aaa\r\n"
|
||||
"\r\n\r\n";
|
||||
|
||||
static const char *options = "-host str [-port num] [-cacert file] [-cert file -key file -pass str]";
|
||||
|
||||
int tlcp_client_main(int argc, char *argv[])
|
||||
{
|
||||
int ret = -1;
|
||||
char *prog = argv[0];
|
||||
char *host = NULL;
|
||||
int port = 443;
|
||||
char *cacertfile = NULL;
|
||||
char *certfile = NULL;
|
||||
char *keyfile = NULL;
|
||||
char *pass = NULL;
|
||||
struct hostent *hp;
|
||||
struct sockaddr_in server;
|
||||
int sock;
|
||||
TLS_CTX ctx;
|
||||
TLS_CONNECT conn;
|
||||
char buf[1024] = {0};
|
||||
size_t len = sizeof(buf);
|
||||
char send_buf[1024] = {0};
|
||||
size_t send_len;
|
||||
|
||||
argc--;
|
||||
argv++;
|
||||
if (argc < 1) {
|
||||
fprintf(stderr, "usage: %s %s\n", prog, options);
|
||||
return 1;
|
||||
}
|
||||
while (argc >= 1) {
|
||||
if (!strcmp(*argv, "-help")) {
|
||||
printf("usage: %s %s\n", prog, options);
|
||||
return 0;
|
||||
} else if (!strcmp(*argv, "-host")) {
|
||||
if (--argc < 1) goto bad;
|
||||
host = *(++argv);
|
||||
} else if (!strcmp(*argv, "-port")) {
|
||||
if (--argc < 1) goto bad;
|
||||
port = atoi(*(++argv));
|
||||
} else if (!strcmp(*argv, "-cacert")) {
|
||||
if (--argc < 1) goto bad;
|
||||
cacertfile = *(++argv);
|
||||
} else if (!strcmp(*argv, "-cert")) {
|
||||
if (--argc < 1) goto bad;
|
||||
certfile = *(++argv);
|
||||
} else if (!strcmp(*argv, "-key")) {
|
||||
if (--argc < 1) goto bad;
|
||||
keyfile = *(++argv);
|
||||
} else if (!strcmp(*argv, "-pass")) {
|
||||
if (--argc < 1) goto bad;
|
||||
pass = *(++argv);
|
||||
} else {
|
||||
fprintf(stderr, "%s: invalid option '%s'\n", prog, *argv);
|
||||
return 1;
|
||||
bad:
|
||||
fprintf(stderr, "%s: option '%s' argument required\n", prog, *argv);
|
||||
return 0;
|
||||
}
|
||||
argc--;
|
||||
argv++;
|
||||
}
|
||||
|
||||
if (!host) {
|
||||
fprintf(stderr, "%s: '-in' option required\n", prog);
|
||||
return -1;
|
||||
}
|
||||
if (!(hp = gethostbyname(host))) {
|
||||
herror("tlcp_client: '-host' invalid");
|
||||
goto end;
|
||||
}
|
||||
|
||||
memset(&ctx, 0, sizeof(ctx));
|
||||
memset(&conn, 0, sizeof(conn));
|
||||
|
||||
server.sin_addr = *((struct in_addr *)hp->h_addr_list[0]);
|
||||
server.sin_family = AF_INET;
|
||||
server.sin_port = htons(port);
|
||||
|
||||
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
|
||||
fprintf(stderr, "%s: open socket error : %s\n", prog, strerror(errno));
|
||||
goto end;
|
||||
}
|
||||
if (connect(sock, (struct sockaddr *)&server , sizeof(server)) < 0) {
|
||||
fprintf(stderr, "%s: connect error : %s\n", prog, strerror(errno));
|
||||
goto end;
|
||||
}
|
||||
|
||||
if (tls_ctx_init(&ctx, TLS_protocol_tlcp, TLS_client_mode) != 1
|
||||
|| tls_ctx_set_cipher_suites(&ctx, client_ciphers, sizeof(client_ciphers)/sizeof(client_ciphers[0])) != 1) {
|
||||
fprintf(stderr, "%s: context init error\n", prog);
|
||||
goto end;
|
||||
}
|
||||
if (cacertfile) {
|
||||
if (tls_ctx_set_ca_certificates(&ctx, cacertfile, TLS_DEFAULT_VERIFY_DEPTH) != 1) {
|
||||
fprintf(stderr, "%s: context init error\n", prog);
|
||||
goto end;
|
||||
}
|
||||
}
|
||||
if (certfile) {
|
||||
if (tls_ctx_set_certificate_and_key(&ctx, certfile, keyfile, pass) != 1) {
|
||||
fprintf(stderr, "%s: context init error\n", prog);
|
||||
goto end;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (tls_init(&conn, &ctx) != 1
|
||||
|| tls_set_socket(&conn, sock) != 1
|
||||
|| tls_do_handshake(&conn) != 1) {
|
||||
fprintf(stderr, "%s: error\n", prog);
|
||||
goto end;
|
||||
}
|
||||
|
||||
|
||||
|
||||
for (;;) {
|
||||
fd_set fds;
|
||||
size_t sentlen;
|
||||
|
||||
FD_ZERO(&fds);
|
||||
FD_SET(conn.sock, &fds);
|
||||
FD_SET(STDIN_FILENO, &fds);
|
||||
|
||||
if (select(conn.sock + 1, &fds, NULL, NULL, NULL) < 0) {
|
||||
fprintf(stderr, "%s: select failed\n", prog);
|
||||
goto end;
|
||||
}
|
||||
|
||||
if (FD_ISSET(conn.sock, &fds)) {
|
||||
for (;;) {
|
||||
memset(buf, 0, sizeof(buf));
|
||||
if (tls_recv(&conn, (uint8_t *)buf, sizeof(buf), &len) != 1) {
|
||||
goto end;
|
||||
}
|
||||
fwrite(buf, 1, len, stdout);
|
||||
fflush(stdout);
|
||||
|
||||
// 应该调整tls_recv 逻辑、API或者其他方式
|
||||
if (conn.datalen == 0) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
if (FD_ISSET(STDIN_FILENO, &fds)) {
|
||||
fprintf(stderr, "recv from stdin\n");
|
||||
|
||||
memset(send_buf, 0, sizeof(send_buf));
|
||||
|
||||
if (!fgets(send_buf, sizeof(send_buf), stdin)) {
|
||||
if (feof(stdin)) {
|
||||
tls_shutdown(&conn);
|
||||
goto end;
|
||||
} else {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
if (tls_send(&conn, (uint8_t *)send_buf, strlen(send_buf), &sentlen) != 1) {
|
||||
fprintf(stderr, "%s: send error\n", prog);
|
||||
goto end;
|
||||
}
|
||||
}
|
||||
|
||||
fprintf(stderr, "end of this round\n");
|
||||
}
|
||||
|
||||
|
||||
end:
|
||||
close(sock);
|
||||
tls_ctx_cleanup(&ctx);
|
||||
tls_cleanup(&conn);
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2014-2022 The GmSSL Project. All Rights Reserved.
|
||||
* Copyright 2022 The GmSSL Project. All Rights Reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the License); you may
|
||||
* not use this file except in compliance with the License.
|
||||
@@ -7,195 +7,194 @@
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*/
|
||||
|
||||
|
||||
|
||||
#include <stdio.h>
|
||||
#include <errno.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/types.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <sys/socket.h>
|
||||
#include <netinet/in.h>
|
||||
#include <gmssl/mem.h>
|
||||
#include <gmssl/sm2.h>
|
||||
#include <gmssl/tls.h>
|
||||
#include <gmssl/error.h>
|
||||
|
||||
|
||||
static const char *options = "[-port num] -cert file -key file [-pass str] -ex_key file [-ex_pass str] [-cacert file]";
|
||||
|
||||
int tlcp_server_main(int argc , char **argv)
|
||||
{
|
||||
int ret = 1;
|
||||
char *prog = argv[0];
|
||||
int port = 443;
|
||||
char *certfile = NULL;
|
||||
char *signkeyfile = NULL;
|
||||
char *signpass = NULL;
|
||||
char *enckeyfile = NULL;
|
||||
char *encpass = NULL;
|
||||
char *cacertfile = NULL;
|
||||
|
||||
int server_ciphers[] = { TLS_cipher_ecc_sm4_cbc_sm3, };
|
||||
uint8_t verify_buf[4096];
|
||||
|
||||
TLS_CTX ctx;
|
||||
TLS_CONNECT conn;
|
||||
char buf[1600] = {0};
|
||||
size_t len = sizeof(buf);
|
||||
|
||||
int sock;
|
||||
struct sockaddr_in server_addr;
|
||||
struct sockaddr_in client_addr;
|
||||
socklen_t client_addrlen;
|
||||
int conn_sock;
|
||||
|
||||
|
||||
argc--;
|
||||
argv++;
|
||||
|
||||
if (argc < 1) {
|
||||
fprintf(stderr, "usage: %s %s\n", prog, options);
|
||||
return 1;
|
||||
}
|
||||
|
||||
while (argc > 0) {
|
||||
if (!strcmp(*argv, "-help")) {
|
||||
printf("usage: %s %s\n", prog, options);
|
||||
return 0;
|
||||
} else if (!strcmp(*argv, "-port")) {
|
||||
if (--argc < 1) goto bad;
|
||||
port = atoi(*(++argv));
|
||||
} else if (!strcmp(*argv, "-cert")) {
|
||||
if (--argc < 1) goto bad;
|
||||
certfile = *(++argv);
|
||||
} else if (!strcmp(*argv, "-key")) {
|
||||
if (--argc < 1) goto bad;
|
||||
signkeyfile = *(++argv);
|
||||
} else if (!strcmp(*argv, "-pass")) {
|
||||
if (--argc < 1) goto bad;
|
||||
signpass = *(++argv);
|
||||
} else if (!strcmp(*argv, "-ex_key")) {
|
||||
if (--argc < 1) goto bad;
|
||||
enckeyfile = *(++argv);
|
||||
} else if (!strcmp(*argv, "-ex_pass")) {
|
||||
if (--argc < 1) goto bad;
|
||||
encpass = *(++argv);
|
||||
} else if (!strcmp(*argv, "-cacert")) {
|
||||
if (--argc < 1) goto bad;
|
||||
cacertfile = *(++argv);
|
||||
} else {
|
||||
fprintf(stderr, "%s: invalid option '%s'\n", prog, *argv);
|
||||
return 1;
|
||||
bad:
|
||||
fprintf(stderr, "%s: option '%s' argument required\n", prog, *argv);
|
||||
return 1;
|
||||
}
|
||||
argc--;
|
||||
argv++;
|
||||
}
|
||||
if (!certfile) {
|
||||
fprintf(stderr, "%s: '-cert' option required\n", prog);
|
||||
return 1;
|
||||
}
|
||||
if (!signkeyfile) {
|
||||
fprintf(stderr, "%s: '-key' option required\n", prog);
|
||||
return 1;
|
||||
}
|
||||
if (!signpass) {
|
||||
fprintf(stderr, "%s: '-pass' option required\n", prog);
|
||||
return 1;
|
||||
}
|
||||
if (!enckeyfile) {
|
||||
fprintf(stderr, "%s: '-ex_key' option required\n", prog);
|
||||
return 1;
|
||||
}
|
||||
if (!encpass) {
|
||||
fprintf(stderr, "%s: '-ex_pass' option required\n", prog);
|
||||
return 1;
|
||||
}
|
||||
|
||||
memset(&ctx, 0, sizeof(ctx));
|
||||
memset(&conn, 0, sizeof(conn));
|
||||
|
||||
if (tls_ctx_init(&ctx, TLS_protocol_tlcp, TLS_server_mode) != 1
|
||||
|| tls_ctx_set_cipher_suites(&ctx, server_ciphers, sizeof(server_ciphers)/sizeof(int)) != 1
|
||||
|| tls_ctx_set_tlcp_server_certificate_and_keys(&ctx, certfile, signkeyfile, signpass, enckeyfile, encpass) != 1) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
if (cacertfile) {
|
||||
if (tls_ctx_set_ca_certificates(&ctx, cacertfile, TLS_DEFAULT_VERIFY_DEPTH) != 1) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
// Socket
|
||||
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
|
||||
error_print();
|
||||
return 1;
|
||||
}
|
||||
server_addr.sin_family = AF_INET;
|
||||
server_addr.sin_addr.s_addr = INADDR_ANY;
|
||||
server_addr.sin_port = htons(port);
|
||||
if (bind(sock, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0) {
|
||||
error_print();
|
||||
perror("tlcp_accept: bind: ");
|
||||
goto end;
|
||||
}
|
||||
puts("start listen ...\n");
|
||||
listen(sock, 1);
|
||||
|
||||
|
||||
|
||||
restart:
|
||||
|
||||
client_addrlen = sizeof(client_addr);
|
||||
if ((conn_sock = accept(sock, (struct sockaddr *)&client_addr, &client_addrlen)) < 0) {
|
||||
error_print();
|
||||
goto end;
|
||||
}
|
||||
puts("socket connected\n");
|
||||
|
||||
if (tls_init(&conn, &ctx) != 1
|
||||
|| tls_set_socket(&conn, conn_sock) != 1) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (tls_do_handshake(&conn) != 1) {
|
||||
error_print(); // 为什么这个会触发呢?
|
||||
return -1;
|
||||
}
|
||||
|
||||
for (;;) {
|
||||
|
||||
int rv;
|
||||
size_t sentlen;
|
||||
|
||||
do {
|
||||
len = sizeof(buf);
|
||||
if ((rv = tls_recv(&conn, (uint8_t *)buf, sizeof(buf), &len)) != 1) {
|
||||
if (rv < 0) fprintf(stderr, "%s: recv failure\n", prog);
|
||||
else fprintf(stderr, "%s: Disconnected by remote\n", prog);
|
||||
|
||||
//close(conn.sock);
|
||||
tls_cleanup(&conn);
|
||||
goto restart;
|
||||
}
|
||||
} while (!len);
|
||||
|
||||
if (tls_send(&conn, (uint8_t *)buf, len, &sentlen) != 1) {
|
||||
fprintf(stderr, "%s: send failure, close connection\n", prog);
|
||||
close(conn.sock);
|
||||
goto end;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
end:
|
||||
return ret;
|
||||
}
|
||||
|
||||
#include <stdio.h>
|
||||
#include <errno.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/types.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <sys/socket.h>
|
||||
#include <netinet/in.h>
|
||||
#include <gmssl/mem.h>
|
||||
#include <gmssl/sm2.h>
|
||||
#include <gmssl/tls.h>
|
||||
#include <gmssl/error.h>
|
||||
|
||||
|
||||
static const char *options = "[-port num] -cert file -key file [-pass str] -ex_key file [-ex_pass str] [-cacert file]";
|
||||
|
||||
int tlcp_server_main(int argc , char **argv)
|
||||
{
|
||||
int ret = 1;
|
||||
char *prog = argv[0];
|
||||
int port = 443;
|
||||
char *certfile = NULL;
|
||||
char *signkeyfile = NULL;
|
||||
char *signpass = NULL;
|
||||
char *enckeyfile = NULL;
|
||||
char *encpass = NULL;
|
||||
char *cacertfile = NULL;
|
||||
|
||||
int server_ciphers[] = { TLS_cipher_ecc_sm4_cbc_sm3, };
|
||||
uint8_t verify_buf[4096];
|
||||
|
||||
TLS_CTX ctx;
|
||||
TLS_CONNECT conn;
|
||||
char buf[1600] = {0};
|
||||
size_t len = sizeof(buf);
|
||||
|
||||
int sock;
|
||||
struct sockaddr_in server_addr;
|
||||
struct sockaddr_in client_addr;
|
||||
socklen_t client_addrlen;
|
||||
int conn_sock;
|
||||
|
||||
|
||||
argc--;
|
||||
argv++;
|
||||
|
||||
if (argc < 1) {
|
||||
fprintf(stderr, "usage: %s %s\n", prog, options);
|
||||
return 1;
|
||||
}
|
||||
|
||||
while (argc > 0) {
|
||||
if (!strcmp(*argv, "-help")) {
|
||||
printf("usage: %s %s\n", prog, options);
|
||||
return 0;
|
||||
} else if (!strcmp(*argv, "-port")) {
|
||||
if (--argc < 1) goto bad;
|
||||
port = atoi(*(++argv));
|
||||
} else if (!strcmp(*argv, "-cert")) {
|
||||
if (--argc < 1) goto bad;
|
||||
certfile = *(++argv);
|
||||
} else if (!strcmp(*argv, "-key")) {
|
||||
if (--argc < 1) goto bad;
|
||||
signkeyfile = *(++argv);
|
||||
} else if (!strcmp(*argv, "-pass")) {
|
||||
if (--argc < 1) goto bad;
|
||||
signpass = *(++argv);
|
||||
} else if (!strcmp(*argv, "-ex_key")) {
|
||||
if (--argc < 1) goto bad;
|
||||
enckeyfile = *(++argv);
|
||||
} else if (!strcmp(*argv, "-ex_pass")) {
|
||||
if (--argc < 1) goto bad;
|
||||
encpass = *(++argv);
|
||||
} else if (!strcmp(*argv, "-cacert")) {
|
||||
if (--argc < 1) goto bad;
|
||||
cacertfile = *(++argv);
|
||||
} else {
|
||||
fprintf(stderr, "%s: invalid option '%s'\n", prog, *argv);
|
||||
return 1;
|
||||
bad:
|
||||
fprintf(stderr, "%s: option '%s' argument required\n", prog, *argv);
|
||||
return 1;
|
||||
}
|
||||
argc--;
|
||||
argv++;
|
||||
}
|
||||
if (!certfile) {
|
||||
fprintf(stderr, "%s: '-cert' option required\n", prog);
|
||||
return 1;
|
||||
}
|
||||
if (!signkeyfile) {
|
||||
fprintf(stderr, "%s: '-key' option required\n", prog);
|
||||
return 1;
|
||||
}
|
||||
if (!signpass) {
|
||||
fprintf(stderr, "%s: '-pass' option required\n", prog);
|
||||
return 1;
|
||||
}
|
||||
if (!enckeyfile) {
|
||||
fprintf(stderr, "%s: '-ex_key' option required\n", prog);
|
||||
return 1;
|
||||
}
|
||||
if (!encpass) {
|
||||
fprintf(stderr, "%s: '-ex_pass' option required\n", prog);
|
||||
return 1;
|
||||
}
|
||||
|
||||
memset(&ctx, 0, sizeof(ctx));
|
||||
memset(&conn, 0, sizeof(conn));
|
||||
|
||||
if (tls_ctx_init(&ctx, TLS_protocol_tlcp, TLS_server_mode) != 1
|
||||
|| tls_ctx_set_cipher_suites(&ctx, server_ciphers, sizeof(server_ciphers)/sizeof(int)) != 1
|
||||
|| tls_ctx_set_tlcp_server_certificate_and_keys(&ctx, certfile, signkeyfile, signpass, enckeyfile, encpass) != 1) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
if (cacertfile) {
|
||||
if (tls_ctx_set_ca_certificates(&ctx, cacertfile, TLS_DEFAULT_VERIFY_DEPTH) != 1) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
// Socket
|
||||
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
|
||||
error_print();
|
||||
return 1;
|
||||
}
|
||||
server_addr.sin_family = AF_INET;
|
||||
server_addr.sin_addr.s_addr = INADDR_ANY;
|
||||
server_addr.sin_port = htons(port);
|
||||
if (bind(sock, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0) {
|
||||
error_print();
|
||||
perror("tlcp_accept: bind: ");
|
||||
goto end;
|
||||
}
|
||||
puts("start listen ...\n");
|
||||
listen(sock, 1);
|
||||
|
||||
|
||||
|
||||
restart:
|
||||
|
||||
client_addrlen = sizeof(client_addr);
|
||||
if ((conn_sock = accept(sock, (struct sockaddr *)&client_addr, &client_addrlen)) < 0) {
|
||||
error_print();
|
||||
goto end;
|
||||
}
|
||||
puts("socket connected\n");
|
||||
|
||||
if (tls_init(&conn, &ctx) != 1
|
||||
|| tls_set_socket(&conn, conn_sock) != 1) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (tls_do_handshake(&conn) != 1) {
|
||||
error_print(); // 为什么这个会触发呢?
|
||||
return -1;
|
||||
}
|
||||
|
||||
for (;;) {
|
||||
|
||||
int rv;
|
||||
size_t sentlen;
|
||||
|
||||
do {
|
||||
len = sizeof(buf);
|
||||
if ((rv = tls_recv(&conn, (uint8_t *)buf, sizeof(buf), &len)) != 1) {
|
||||
if (rv < 0) fprintf(stderr, "%s: recv failure\n", prog);
|
||||
else fprintf(stderr, "%s: Disconnected by remote\n", prog);
|
||||
|
||||
//close(conn.sock);
|
||||
tls_cleanup(&conn);
|
||||
goto restart;
|
||||
}
|
||||
} while (!len);
|
||||
|
||||
if (tls_send(&conn, (uint8_t *)buf, len, &sentlen) != 1) {
|
||||
fprintf(stderr, "%s: send failure, close connection\n", prog);
|
||||
close(conn.sock);
|
||||
goto end;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
end:
|
||||
return ret;
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2014-2022 The GmSSL Project. All Rights Reserved.
|
||||
* Copyright 2022 The GmSSL Project. All Rights Reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the License); you may
|
||||
* not use this file except in compliance with the License.
|
||||
@@ -7,195 +7,194 @@
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*/
|
||||
|
||||
|
||||
|
||||
#include <stdio.h>
|
||||
#include <errno.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include <unistd.h>
|
||||
#include <netdb.h>
|
||||
#include <sys/types.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <sys/socket.h>
|
||||
#include <netinet/in.h>
|
||||
#include <gmssl/tls.h>
|
||||
#include <gmssl/error.h>
|
||||
|
||||
|
||||
// TLSv1.2客户单和TLCP客户端可能没有什么区别
|
||||
|
||||
static int client_ciphers[] = { TLS_cipher_ecdhe_sm4_cbc_sm3 };
|
||||
|
||||
static const char *http_get =
|
||||
"GET / HTTP/1.1\r\n"
|
||||
"Hostname: aaa\r\n"
|
||||
"\r\n\r\n";
|
||||
|
||||
static const char *options = "-host str [-port num] [-cacert file] [-cert file -key file -pass str]";
|
||||
|
||||
int tls12_client_main(int argc, char *argv[])
|
||||
{
|
||||
int ret = -1;
|
||||
char *prog = argv[0];
|
||||
char *host = NULL;
|
||||
int port = 443;
|
||||
char *cacertfile = NULL;
|
||||
char *certfile = NULL;
|
||||
char *keyfile = NULL;
|
||||
char *pass = NULL;
|
||||
struct hostent *hp;
|
||||
struct sockaddr_in server;
|
||||
int sock;
|
||||
TLS_CTX ctx;
|
||||
TLS_CONNECT conn;
|
||||
char buf[1024] = {0};
|
||||
size_t len = sizeof(buf);
|
||||
char send_buf[1024] = {0};
|
||||
size_t send_len;
|
||||
|
||||
argc--;
|
||||
argv++;
|
||||
if (argc < 1) {
|
||||
fprintf(stderr, "usage: %s %s\n", prog, options);
|
||||
return 1;
|
||||
}
|
||||
while (argc >= 1) {
|
||||
if (!strcmp(*argv, "-help")) {
|
||||
printf("usage: %s %s\n", prog, options);
|
||||
return 0;
|
||||
} else if (!strcmp(*argv, "-host")) {
|
||||
if (--argc < 1) goto bad;
|
||||
host = *(++argv);
|
||||
} else if (!strcmp(*argv, "-port")) {
|
||||
if (--argc < 1) goto bad;
|
||||
port = atoi(*(++argv));
|
||||
} else if (!strcmp(*argv, "-cacert")) {
|
||||
if (--argc < 1) goto bad;
|
||||
cacertfile = *(++argv);
|
||||
} else if (!strcmp(*argv, "-cert")) {
|
||||
if (--argc < 1) goto bad;
|
||||
certfile = *(++argv);
|
||||
} else if (!strcmp(*argv, "-key")) {
|
||||
if (--argc < 1) goto bad;
|
||||
keyfile = *(++argv);
|
||||
} else if (!strcmp(*argv, "-pass")) {
|
||||
if (--argc < 1) goto bad;
|
||||
pass = *(++argv);
|
||||
} else {
|
||||
fprintf(stderr, "%s: invalid option '%s'\n", prog, *argv);
|
||||
return 1;
|
||||
bad:
|
||||
fprintf(stderr, "%s: option '%s' argument required\n", prog, *argv);
|
||||
return 0;
|
||||
}
|
||||
argc--;
|
||||
argv++;
|
||||
}
|
||||
|
||||
if (!host) {
|
||||
fprintf(stderr, "%s: '-in' option required\n", prog);
|
||||
return -1;
|
||||
}
|
||||
if (!(hp = gethostbyname(host))) {
|
||||
herror("tls12_client: '-host' invalid");
|
||||
goto end;
|
||||
}
|
||||
|
||||
memset(&ctx, 0, sizeof(ctx));
|
||||
memset(&conn, 0, sizeof(conn));
|
||||
|
||||
server.sin_addr = *((struct in_addr *)hp->h_addr_list[0]);
|
||||
server.sin_family = AF_INET;
|
||||
server.sin_port = htons(port);
|
||||
|
||||
|
||||
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
|
||||
fprintf(stderr, "%s: open socket error : %s\n", prog, strerror(errno));
|
||||
goto end;
|
||||
}
|
||||
if (connect(sock, (struct sockaddr *)&server , sizeof(server)) < 0) {
|
||||
fprintf(stderr, "%s: connect error : %s\n", prog, strerror(errno));
|
||||
goto end;
|
||||
}
|
||||
|
||||
if (tls_ctx_init(&ctx, TLS_protocol_tls12, TLS_client_mode) != 1
|
||||
|| tls_ctx_set_cipher_suites(&ctx, client_ciphers, sizeof(client_ciphers)/sizeof(client_ciphers[0])) != 1) {
|
||||
fprintf(stderr, "%s: context init error\n", prog);
|
||||
goto end;
|
||||
}
|
||||
if (cacertfile) {
|
||||
if (tls_ctx_set_ca_certificates(&ctx, cacertfile, TLS_DEFAULT_VERIFY_DEPTH) != 1) {
|
||||
fprintf(stderr, "%s: context init error\n", prog);
|
||||
goto end;
|
||||
}
|
||||
}
|
||||
if (certfile) {
|
||||
if (tls_ctx_set_certificate_and_key(&ctx, certfile, keyfile, pass) != 1) {
|
||||
fprintf(stderr, "%s: context init error\n", prog);
|
||||
goto end;
|
||||
}
|
||||
}
|
||||
|
||||
if (tls_init(&conn, &ctx) != 1
|
||||
|| tls_set_socket(&conn, sock) != 1
|
||||
|| tls_do_handshake(&conn) != 1) {
|
||||
fprintf(stderr, "%s: error\n", prog);
|
||||
goto end;
|
||||
}
|
||||
|
||||
for (;;) {
|
||||
fd_set fds;
|
||||
size_t sentlen;
|
||||
|
||||
FD_ZERO(&fds);
|
||||
FD_SET(conn.sock, &fds);
|
||||
FD_SET(STDIN_FILENO, &fds);
|
||||
|
||||
if (select(conn.sock + 1, &fds, NULL, NULL, NULL) < 0) {
|
||||
fprintf(stderr, "%s: select failed\n", prog);
|
||||
goto end;
|
||||
}
|
||||
|
||||
if (FD_ISSET(conn.sock, &fds)) {
|
||||
for (;;) {
|
||||
memset(buf, 0, sizeof(buf));
|
||||
if (tls_recv(&conn, (uint8_t *)buf, sizeof(buf), &len) != 1) {
|
||||
goto end;
|
||||
}
|
||||
fwrite(buf, 1, len, stdout);
|
||||
fflush(stdout);
|
||||
|
||||
// 应该调整tls_recv 逻辑、API或者其他方式
|
||||
if (conn.datalen == 0) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
if (FD_ISSET(STDIN_FILENO, &fds)) {
|
||||
memset(send_buf, 0, sizeof(send_buf));
|
||||
|
||||
if (!fgets(send_buf, sizeof(send_buf), stdin)) {
|
||||
if (feof(stdin)) {
|
||||
tls_shutdown(&conn);
|
||||
goto end;
|
||||
} else {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
if (tls_send(&conn, (uint8_t *)send_buf, strlen(send_buf), &sentlen) != 1) {
|
||||
fprintf(stderr, "%s: send error\n", prog);
|
||||
goto end;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
end:
|
||||
close(sock);
|
||||
tls_ctx_cleanup(&ctx);
|
||||
tls_cleanup(&conn);
|
||||
return 0;
|
||||
}
|
||||
|
||||
#include <stdio.h>
|
||||
#include <errno.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include <unistd.h>
|
||||
#include <netdb.h>
|
||||
#include <sys/types.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <sys/socket.h>
|
||||
#include <netinet/in.h>
|
||||
#include <gmssl/tls.h>
|
||||
#include <gmssl/error.h>
|
||||
|
||||
|
||||
// TLSv1.2客户单和TLCP客户端可能没有什么区别
|
||||
|
||||
static int client_ciphers[] = { TLS_cipher_ecdhe_sm4_cbc_sm3 };
|
||||
|
||||
static const char *http_get =
|
||||
"GET / HTTP/1.1\r\n"
|
||||
"Hostname: aaa\r\n"
|
||||
"\r\n\r\n";
|
||||
|
||||
static const char *options = "-host str [-port num] [-cacert file] [-cert file -key file -pass str]";
|
||||
|
||||
int tls12_client_main(int argc, char *argv[])
|
||||
{
|
||||
int ret = -1;
|
||||
char *prog = argv[0];
|
||||
char *host = NULL;
|
||||
int port = 443;
|
||||
char *cacertfile = NULL;
|
||||
char *certfile = NULL;
|
||||
char *keyfile = NULL;
|
||||
char *pass = NULL;
|
||||
struct hostent *hp;
|
||||
struct sockaddr_in server;
|
||||
int sock;
|
||||
TLS_CTX ctx;
|
||||
TLS_CONNECT conn;
|
||||
char buf[1024] = {0};
|
||||
size_t len = sizeof(buf);
|
||||
char send_buf[1024] = {0};
|
||||
size_t send_len;
|
||||
|
||||
argc--;
|
||||
argv++;
|
||||
if (argc < 1) {
|
||||
fprintf(stderr, "usage: %s %s\n", prog, options);
|
||||
return 1;
|
||||
}
|
||||
while (argc >= 1) {
|
||||
if (!strcmp(*argv, "-help")) {
|
||||
printf("usage: %s %s\n", prog, options);
|
||||
return 0;
|
||||
} else if (!strcmp(*argv, "-host")) {
|
||||
if (--argc < 1) goto bad;
|
||||
host = *(++argv);
|
||||
} else if (!strcmp(*argv, "-port")) {
|
||||
if (--argc < 1) goto bad;
|
||||
port = atoi(*(++argv));
|
||||
} else if (!strcmp(*argv, "-cacert")) {
|
||||
if (--argc < 1) goto bad;
|
||||
cacertfile = *(++argv);
|
||||
} else if (!strcmp(*argv, "-cert")) {
|
||||
if (--argc < 1) goto bad;
|
||||
certfile = *(++argv);
|
||||
} else if (!strcmp(*argv, "-key")) {
|
||||
if (--argc < 1) goto bad;
|
||||
keyfile = *(++argv);
|
||||
} else if (!strcmp(*argv, "-pass")) {
|
||||
if (--argc < 1) goto bad;
|
||||
pass = *(++argv);
|
||||
} else {
|
||||
fprintf(stderr, "%s: invalid option '%s'\n", prog, *argv);
|
||||
return 1;
|
||||
bad:
|
||||
fprintf(stderr, "%s: option '%s' argument required\n", prog, *argv);
|
||||
return 0;
|
||||
}
|
||||
argc--;
|
||||
argv++;
|
||||
}
|
||||
|
||||
if (!host) {
|
||||
fprintf(stderr, "%s: '-in' option required\n", prog);
|
||||
return -1;
|
||||
}
|
||||
if (!(hp = gethostbyname(host))) {
|
||||
herror("tls12_client: '-host' invalid");
|
||||
goto end;
|
||||
}
|
||||
|
||||
memset(&ctx, 0, sizeof(ctx));
|
||||
memset(&conn, 0, sizeof(conn));
|
||||
|
||||
server.sin_addr = *((struct in_addr *)hp->h_addr_list[0]);
|
||||
server.sin_family = AF_INET;
|
||||
server.sin_port = htons(port);
|
||||
|
||||
|
||||
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
|
||||
fprintf(stderr, "%s: open socket error : %s\n", prog, strerror(errno));
|
||||
goto end;
|
||||
}
|
||||
if (connect(sock, (struct sockaddr *)&server , sizeof(server)) < 0) {
|
||||
fprintf(stderr, "%s: connect error : %s\n", prog, strerror(errno));
|
||||
goto end;
|
||||
}
|
||||
|
||||
if (tls_ctx_init(&ctx, TLS_protocol_tls12, TLS_client_mode) != 1
|
||||
|| tls_ctx_set_cipher_suites(&ctx, client_ciphers, sizeof(client_ciphers)/sizeof(client_ciphers[0])) != 1) {
|
||||
fprintf(stderr, "%s: context init error\n", prog);
|
||||
goto end;
|
||||
}
|
||||
if (cacertfile) {
|
||||
if (tls_ctx_set_ca_certificates(&ctx, cacertfile, TLS_DEFAULT_VERIFY_DEPTH) != 1) {
|
||||
fprintf(stderr, "%s: context init error\n", prog);
|
||||
goto end;
|
||||
}
|
||||
}
|
||||
if (certfile) {
|
||||
if (tls_ctx_set_certificate_and_key(&ctx, certfile, keyfile, pass) != 1) {
|
||||
fprintf(stderr, "%s: context init error\n", prog);
|
||||
goto end;
|
||||
}
|
||||
}
|
||||
|
||||
if (tls_init(&conn, &ctx) != 1
|
||||
|| tls_set_socket(&conn, sock) != 1
|
||||
|| tls_do_handshake(&conn) != 1) {
|
||||
fprintf(stderr, "%s: error\n", prog);
|
||||
goto end;
|
||||
}
|
||||
|
||||
for (;;) {
|
||||
fd_set fds;
|
||||
size_t sentlen;
|
||||
|
||||
FD_ZERO(&fds);
|
||||
FD_SET(conn.sock, &fds);
|
||||
FD_SET(STDIN_FILENO, &fds);
|
||||
|
||||
if (select(conn.sock + 1, &fds, NULL, NULL, NULL) < 0) {
|
||||
fprintf(stderr, "%s: select failed\n", prog);
|
||||
goto end;
|
||||
}
|
||||
|
||||
if (FD_ISSET(conn.sock, &fds)) {
|
||||
for (;;) {
|
||||
memset(buf, 0, sizeof(buf));
|
||||
if (tls_recv(&conn, (uint8_t *)buf, sizeof(buf), &len) != 1) {
|
||||
goto end;
|
||||
}
|
||||
fwrite(buf, 1, len, stdout);
|
||||
fflush(stdout);
|
||||
|
||||
// 应该调整tls_recv 逻辑、API或者其他方式
|
||||
if (conn.datalen == 0) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
if (FD_ISSET(STDIN_FILENO, &fds)) {
|
||||
memset(send_buf, 0, sizeof(send_buf));
|
||||
|
||||
if (!fgets(send_buf, sizeof(send_buf), stdin)) {
|
||||
if (feof(stdin)) {
|
||||
tls_shutdown(&conn);
|
||||
goto end;
|
||||
} else {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
if (tls_send(&conn, (uint8_t *)send_buf, strlen(send_buf), &sentlen) != 1) {
|
||||
fprintf(stderr, "%s: send error\n", prog);
|
||||
goto end;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
end:
|
||||
close(sock);
|
||||
tls_ctx_cleanup(&ctx);
|
||||
tls_cleanup(&conn);
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2014-2022 The GmSSL Project. All Rights Reserved.
|
||||
* Copyright 2022 The GmSSL Project. All Rights Reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the License); you may
|
||||
* not use this file except in compliance with the License.
|
||||
@@ -7,179 +7,178 @@
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*/
|
||||
|
||||
|
||||
|
||||
#include <stdio.h>
|
||||
#include <errno.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/types.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <sys/socket.h>
|
||||
#include <netinet/in.h>
|
||||
#include <gmssl/mem.h>
|
||||
#include <gmssl/sm2.h>
|
||||
#include <gmssl/tls.h>
|
||||
#include <gmssl/error.h>
|
||||
|
||||
|
||||
static const char *options = "[-port num] -cert file -key file -pass str [-cacert file]";
|
||||
|
||||
int tls12_server_main(int argc , char **argv)
|
||||
{
|
||||
int ret = 1;
|
||||
char *prog = argv[0];
|
||||
int port = 443;
|
||||
char *certfile = NULL;
|
||||
char *keyfile = NULL;
|
||||
char *pass = NULL;
|
||||
char *cacertfile = NULL;
|
||||
|
||||
int server_ciphers[] = { TLS_cipher_ecdhe_sm4_cbc_sm3, };
|
||||
uint8_t verify_buf[4096];
|
||||
|
||||
TLS_CTX ctx;
|
||||
TLS_CONNECT conn;
|
||||
char buf[1600] = {0};
|
||||
size_t len = sizeof(buf);
|
||||
|
||||
int sock;
|
||||
struct sockaddr_in server_addr;
|
||||
struct sockaddr_in client_addr;
|
||||
socklen_t client_addrlen;
|
||||
int conn_sock;
|
||||
|
||||
|
||||
argc--;
|
||||
argv++;
|
||||
|
||||
if (argc < 1) {
|
||||
fprintf(stderr, "usage: %s %s\n", prog, options);
|
||||
return 1;
|
||||
}
|
||||
|
||||
while (argc > 0) {
|
||||
if (!strcmp(*argv, "-help")) {
|
||||
printf("usage: %s %s\n", prog, options);
|
||||
return 0;
|
||||
} else if (!strcmp(*argv, "-port")) {
|
||||
if (--argc < 1) goto bad;
|
||||
port = atoi(*(++argv));
|
||||
} else if (!strcmp(*argv, "-cert")) {
|
||||
if (--argc < 1) goto bad;
|
||||
certfile = *(++argv);
|
||||
} else if (!strcmp(*argv, "-key")) {
|
||||
if (--argc < 1) goto bad;
|
||||
keyfile = *(++argv);
|
||||
} else if (!strcmp(*argv, "-pass")) {
|
||||
if (--argc < 1) goto bad;
|
||||
pass = *(++argv);
|
||||
} else if (!strcmp(*argv, "-cacert")) {
|
||||
if (--argc < 1) goto bad;
|
||||
cacertfile = *(++argv);
|
||||
} else {
|
||||
fprintf(stderr, "%s: invalid option '%s'\n", prog, *argv);
|
||||
return 1;
|
||||
bad:
|
||||
fprintf(stderr, "%s: option '%s' argument required\n", prog, *argv);
|
||||
return 1;
|
||||
}
|
||||
argc--;
|
||||
argv++;
|
||||
}
|
||||
if (!certfile) {
|
||||
fprintf(stderr, "%s: '-cert' option required\n", prog);
|
||||
return 1;
|
||||
}
|
||||
if (!keyfile) {
|
||||
fprintf(stderr, "%s: '-key' option required\n", prog);
|
||||
return 1;
|
||||
}
|
||||
if (!pass) {
|
||||
fprintf(stderr, "%s: '-pass' option required\n", prog);
|
||||
return 1;
|
||||
}
|
||||
|
||||
memset(&ctx, 0, sizeof(ctx));
|
||||
memset(&conn, 0, sizeof(conn));
|
||||
|
||||
if (tls_ctx_init(&ctx, TLS_protocol_tls12, TLS_server_mode) != 1
|
||||
|| tls_ctx_set_cipher_suites(&ctx, server_ciphers, sizeof(server_ciphers)/sizeof(int)) != 1
|
||||
|| tls_ctx_set_certificate_and_key(&ctx, certfile, keyfile, pass) != 1) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
if (cacertfile) {
|
||||
if (tls_ctx_set_ca_certificates(&ctx, cacertfile, TLS_DEFAULT_VERIFY_DEPTH) != 1) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
// Socket
|
||||
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
|
||||
error_print();
|
||||
return 1;
|
||||
}
|
||||
server_addr.sin_family = AF_INET;
|
||||
server_addr.sin_addr.s_addr = INADDR_ANY;
|
||||
server_addr.sin_port = htons(port);
|
||||
if (bind(sock, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0) {
|
||||
error_print();
|
||||
perror("tlcp_accept: bind: ");
|
||||
goto end;
|
||||
}
|
||||
puts("start listen ...\n");
|
||||
listen(sock, 1);
|
||||
|
||||
|
||||
|
||||
restart:
|
||||
|
||||
client_addrlen = sizeof(client_addr);
|
||||
if ((conn_sock = accept(sock, (struct sockaddr *)&client_addr, &client_addrlen)) < 0) {
|
||||
error_print();
|
||||
goto end;
|
||||
}
|
||||
puts("socket connected\n");
|
||||
|
||||
if (tls_init(&conn, &ctx) != 1
|
||||
|| tls_set_socket(&conn, conn_sock) != 1) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (tls_do_handshake(&conn) != 1) {
|
||||
error_print(); // 为什么这个会触发呢?
|
||||
return -1;
|
||||
}
|
||||
|
||||
for (;;) {
|
||||
|
||||
int rv;
|
||||
size_t sentlen;
|
||||
|
||||
do {
|
||||
len = sizeof(buf);
|
||||
if ((rv = tls_recv(&conn, (uint8_t *)buf, sizeof(buf), &len)) != 1) {
|
||||
if (rv < 0) fprintf(stderr, "%s: recv failure\n", prog);
|
||||
else fprintf(stderr, "%s: Disconnected by remote\n", prog);
|
||||
|
||||
//close(conn.sock);
|
||||
tls_cleanup(&conn);
|
||||
goto restart;
|
||||
}
|
||||
} while (!len);
|
||||
|
||||
if (tls_send(&conn, (uint8_t *)buf, len, &sentlen) != 1) {
|
||||
fprintf(stderr, "%s: send failure, close connection\n", prog);
|
||||
close(conn.sock);
|
||||
goto end;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
end:
|
||||
return ret;
|
||||
}
|
||||
|
||||
#include <stdio.h>
|
||||
#include <errno.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/types.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <sys/socket.h>
|
||||
#include <netinet/in.h>
|
||||
#include <gmssl/mem.h>
|
||||
#include <gmssl/sm2.h>
|
||||
#include <gmssl/tls.h>
|
||||
#include <gmssl/error.h>
|
||||
|
||||
|
||||
static const char *options = "[-port num] -cert file -key file -pass str [-cacert file]";
|
||||
|
||||
int tls12_server_main(int argc , char **argv)
|
||||
{
|
||||
int ret = 1;
|
||||
char *prog = argv[0];
|
||||
int port = 443;
|
||||
char *certfile = NULL;
|
||||
char *keyfile = NULL;
|
||||
char *pass = NULL;
|
||||
char *cacertfile = NULL;
|
||||
|
||||
int server_ciphers[] = { TLS_cipher_ecdhe_sm4_cbc_sm3, };
|
||||
uint8_t verify_buf[4096];
|
||||
|
||||
TLS_CTX ctx;
|
||||
TLS_CONNECT conn;
|
||||
char buf[1600] = {0};
|
||||
size_t len = sizeof(buf);
|
||||
|
||||
int sock;
|
||||
struct sockaddr_in server_addr;
|
||||
struct sockaddr_in client_addr;
|
||||
socklen_t client_addrlen;
|
||||
int conn_sock;
|
||||
|
||||
|
||||
argc--;
|
||||
argv++;
|
||||
|
||||
if (argc < 1) {
|
||||
fprintf(stderr, "usage: %s %s\n", prog, options);
|
||||
return 1;
|
||||
}
|
||||
|
||||
while (argc > 0) {
|
||||
if (!strcmp(*argv, "-help")) {
|
||||
printf("usage: %s %s\n", prog, options);
|
||||
return 0;
|
||||
} else if (!strcmp(*argv, "-port")) {
|
||||
if (--argc < 1) goto bad;
|
||||
port = atoi(*(++argv));
|
||||
} else if (!strcmp(*argv, "-cert")) {
|
||||
if (--argc < 1) goto bad;
|
||||
certfile = *(++argv);
|
||||
} else if (!strcmp(*argv, "-key")) {
|
||||
if (--argc < 1) goto bad;
|
||||
keyfile = *(++argv);
|
||||
} else if (!strcmp(*argv, "-pass")) {
|
||||
if (--argc < 1) goto bad;
|
||||
pass = *(++argv);
|
||||
} else if (!strcmp(*argv, "-cacert")) {
|
||||
if (--argc < 1) goto bad;
|
||||
cacertfile = *(++argv);
|
||||
} else {
|
||||
fprintf(stderr, "%s: invalid option '%s'\n", prog, *argv);
|
||||
return 1;
|
||||
bad:
|
||||
fprintf(stderr, "%s: option '%s' argument required\n", prog, *argv);
|
||||
return 1;
|
||||
}
|
||||
argc--;
|
||||
argv++;
|
||||
}
|
||||
if (!certfile) {
|
||||
fprintf(stderr, "%s: '-cert' option required\n", prog);
|
||||
return 1;
|
||||
}
|
||||
if (!keyfile) {
|
||||
fprintf(stderr, "%s: '-key' option required\n", prog);
|
||||
return 1;
|
||||
}
|
||||
if (!pass) {
|
||||
fprintf(stderr, "%s: '-pass' option required\n", prog);
|
||||
return 1;
|
||||
}
|
||||
|
||||
memset(&ctx, 0, sizeof(ctx));
|
||||
memset(&conn, 0, sizeof(conn));
|
||||
|
||||
if (tls_ctx_init(&ctx, TLS_protocol_tls12, TLS_server_mode) != 1
|
||||
|| tls_ctx_set_cipher_suites(&ctx, server_ciphers, sizeof(server_ciphers)/sizeof(int)) != 1
|
||||
|| tls_ctx_set_certificate_and_key(&ctx, certfile, keyfile, pass) != 1) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
if (cacertfile) {
|
||||
if (tls_ctx_set_ca_certificates(&ctx, cacertfile, TLS_DEFAULT_VERIFY_DEPTH) != 1) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
// Socket
|
||||
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
|
||||
error_print();
|
||||
return 1;
|
||||
}
|
||||
server_addr.sin_family = AF_INET;
|
||||
server_addr.sin_addr.s_addr = INADDR_ANY;
|
||||
server_addr.sin_port = htons(port);
|
||||
if (bind(sock, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0) {
|
||||
error_print();
|
||||
perror("tlcp_accept: bind: ");
|
||||
goto end;
|
||||
}
|
||||
puts("start listen ...\n");
|
||||
listen(sock, 1);
|
||||
|
||||
|
||||
|
||||
restart:
|
||||
|
||||
client_addrlen = sizeof(client_addr);
|
||||
if ((conn_sock = accept(sock, (struct sockaddr *)&client_addr, &client_addrlen)) < 0) {
|
||||
error_print();
|
||||
goto end;
|
||||
}
|
||||
puts("socket connected\n");
|
||||
|
||||
if (tls_init(&conn, &ctx) != 1
|
||||
|| tls_set_socket(&conn, conn_sock) != 1) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (tls_do_handshake(&conn) != 1) {
|
||||
error_print(); // 为什么这个会触发呢?
|
||||
return -1;
|
||||
}
|
||||
|
||||
for (;;) {
|
||||
|
||||
int rv;
|
||||
size_t sentlen;
|
||||
|
||||
do {
|
||||
len = sizeof(buf);
|
||||
if ((rv = tls_recv(&conn, (uint8_t *)buf, sizeof(buf), &len)) != 1) {
|
||||
if (rv < 0) fprintf(stderr, "%s: recv failure\n", prog);
|
||||
else fprintf(stderr, "%s: Disconnected by remote\n", prog);
|
||||
|
||||
//close(conn.sock);
|
||||
tls_cleanup(&conn);
|
||||
goto restart;
|
||||
}
|
||||
} while (!len);
|
||||
|
||||
if (tls_send(&conn, (uint8_t *)buf, len, &sentlen) != 1) {
|
||||
fprintf(stderr, "%s: send failure, close connection\n", prog);
|
||||
close(conn.sock);
|
||||
goto end;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
end:
|
||||
return ret;
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2014-2022 The GmSSL Project. All Rights Reserved.
|
||||
* Copyright 2022 The GmSSL Project. All Rights Reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the License); you may
|
||||
* not use this file except in compliance with the License.
|
||||
@@ -7,193 +7,192 @@
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*/
|
||||
|
||||
|
||||
|
||||
#include <stdio.h>
|
||||
#include <errno.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include <unistd.h>
|
||||
#include <netdb.h>
|
||||
#include <sys/types.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <sys/socket.h>
|
||||
#include <netinet/in.h>
|
||||
#include <gmssl/tls.h>
|
||||
#include <gmssl/error.h>
|
||||
|
||||
|
||||
// TLSv1.2客户单和TLCP客户端可能没有什么区别
|
||||
|
||||
static int client_ciphers[] = { TLS_cipher_sm4_gcm_sm3 };
|
||||
|
||||
static const char *http_get =
|
||||
"GET / HTTP/1.1\r\n"
|
||||
"Hostname: aaa\r\n"
|
||||
"\r\n\r\n";
|
||||
|
||||
static const char *options = "-host str [-port num] [-cacert file] [-cert file -key file -pass str]";
|
||||
|
||||
int tls13_client_main(int argc, char *argv[])
|
||||
{
|
||||
int ret = -1;
|
||||
char *prog = argv[0];
|
||||
char *host = NULL;
|
||||
int port = 443;
|
||||
char *cacertfile = NULL;
|
||||
char *certfile = NULL;
|
||||
char *keyfile = NULL;
|
||||
char *pass = NULL;
|
||||
struct hostent *hp;
|
||||
struct sockaddr_in server;
|
||||
int sock;
|
||||
TLS_CTX ctx;
|
||||
TLS_CONNECT conn;
|
||||
char buf[1024] = {0};
|
||||
size_t len = sizeof(buf);
|
||||
char send_buf[1024] = {0};
|
||||
size_t send_len;
|
||||
|
||||
argc--;
|
||||
argv++;
|
||||
if (argc < 1) {
|
||||
fprintf(stderr, "usage: %s %s\n", prog, options);
|
||||
return 1;
|
||||
}
|
||||
while (argc >= 1) {
|
||||
if (!strcmp(*argv, "-help")) {
|
||||
printf("usage: %s %s\n", prog, options);
|
||||
return 0;
|
||||
} else if (!strcmp(*argv, "-host")) {
|
||||
if (--argc < 1) goto bad;
|
||||
host = *(++argv);
|
||||
} else if (!strcmp(*argv, "-port")) {
|
||||
if (--argc < 1) goto bad;
|
||||
port = atoi(*(++argv));
|
||||
} else if (!strcmp(*argv, "-cacert")) {
|
||||
if (--argc < 1) goto bad;
|
||||
cacertfile = *(++argv);
|
||||
} else if (!strcmp(*argv, "-cert")) {
|
||||
if (--argc < 1) goto bad;
|
||||
certfile = *(++argv);
|
||||
} else if (!strcmp(*argv, "-key")) {
|
||||
if (--argc < 1) goto bad;
|
||||
keyfile = *(++argv);
|
||||
} else if (!strcmp(*argv, "-pass")) {
|
||||
if (--argc < 1) goto bad;
|
||||
pass = *(++argv);
|
||||
} else {
|
||||
fprintf(stderr, "%s: invalid option '%s'\n", prog, *argv);
|
||||
return 1;
|
||||
bad:
|
||||
fprintf(stderr, "%s: option '%s' argument required\n", prog, *argv);
|
||||
return 0;
|
||||
}
|
||||
argc--;
|
||||
argv++;
|
||||
}
|
||||
|
||||
if (!host) {
|
||||
fprintf(stderr, "%s: '-in' option required\n", prog);
|
||||
return -1;
|
||||
}
|
||||
if (!(hp = gethostbyname(host))) {
|
||||
herror("tls13_client: '-host' invalid");
|
||||
goto end;
|
||||
}
|
||||
|
||||
memset(&ctx, 0, sizeof(ctx));
|
||||
memset(&conn, 0, sizeof(conn));
|
||||
|
||||
server.sin_addr = *((struct in_addr *)hp->h_addr_list[0]);
|
||||
server.sin_family = AF_INET;
|
||||
server.sin_port = htons(port);
|
||||
|
||||
|
||||
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
|
||||
fprintf(stderr, "%s: open socket error : %s\n", prog, strerror(errno));
|
||||
goto end;
|
||||
}
|
||||
if (connect(sock, (struct sockaddr *)&server , sizeof(server)) < 0) {
|
||||
fprintf(stderr, "%s: connect error : %s\n", prog, strerror(errno));
|
||||
goto end;
|
||||
}
|
||||
|
||||
if (tls_ctx_init(&ctx, TLS_protocol_tls13, TLS_client_mode) != 1
|
||||
|| tls_ctx_set_cipher_suites(&ctx, client_ciphers, sizeof(client_ciphers)/sizeof(client_ciphers[0])) != 1) {
|
||||
fprintf(stderr, "%s: context init error\n", prog);
|
||||
goto end;
|
||||
}
|
||||
if (cacertfile) {
|
||||
if (tls_ctx_set_ca_certificates(&ctx, cacertfile, TLS_DEFAULT_VERIFY_DEPTH) != 1) {
|
||||
fprintf(stderr, "%s: context init error\n", prog);
|
||||
goto end;
|
||||
}
|
||||
}
|
||||
if (certfile) {
|
||||
if (tls_ctx_set_certificate_and_key(&ctx, certfile, keyfile, pass) != 1) {
|
||||
fprintf(stderr, "%s: context init error\n", prog);
|
||||
goto end;
|
||||
}
|
||||
}
|
||||
if (tls_init(&conn, &ctx) != 1
|
||||
|| tls_set_socket(&conn, sock) != 1
|
||||
|| tls_do_handshake(&conn) != 1) {
|
||||
fprintf(stderr, "%s: error\n", prog);
|
||||
goto end;
|
||||
}
|
||||
|
||||
for (;;) {
|
||||
fd_set fds;
|
||||
size_t sentlen;
|
||||
|
||||
FD_ZERO(&fds);
|
||||
FD_SET(conn.sock, &fds);
|
||||
FD_SET(STDIN_FILENO, &fds);
|
||||
|
||||
if (select(conn.sock + 1, &fds, NULL, NULL, NULL) < 0) {
|
||||
fprintf(stderr, "%s: select failed\n", prog);
|
||||
goto end;
|
||||
}
|
||||
|
||||
if (FD_ISSET(conn.sock, &fds)) {
|
||||
for (;;) {
|
||||
memset(buf, 0, sizeof(buf));
|
||||
if (tls13_recv(&conn, (uint8_t *)buf, sizeof(buf), &len) != 1) {
|
||||
goto end;
|
||||
}
|
||||
fwrite(buf, 1, len, stdout);
|
||||
fflush(stdout);
|
||||
|
||||
// 应该调整tls_recv 逻辑、API或者其他方式
|
||||
if (conn.datalen == 0) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
if (FD_ISSET(STDIN_FILENO, &fds)) {
|
||||
memset(send_buf, 0, sizeof(send_buf));
|
||||
|
||||
if (!fgets(send_buf, sizeof(send_buf), stdin)) {
|
||||
if (feof(stdin)) {
|
||||
tls_shutdown(&conn);
|
||||
goto end;
|
||||
} else {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
if (tls13_send(&conn, (uint8_t *)send_buf, strlen(send_buf), &sentlen) != 1) {
|
||||
fprintf(stderr, "%s: send error\n", prog);
|
||||
goto end;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
end:
|
||||
close(sock);
|
||||
tls_ctx_cleanup(&ctx);
|
||||
tls_cleanup(&conn);
|
||||
return 0;
|
||||
}
|
||||
|
||||
#include <stdio.h>
|
||||
#include <errno.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include <unistd.h>
|
||||
#include <netdb.h>
|
||||
#include <sys/types.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <sys/socket.h>
|
||||
#include <netinet/in.h>
|
||||
#include <gmssl/tls.h>
|
||||
#include <gmssl/error.h>
|
||||
|
||||
|
||||
// TLSv1.2客户单和TLCP客户端可能没有什么区别
|
||||
|
||||
static int client_ciphers[] = { TLS_cipher_sm4_gcm_sm3 };
|
||||
|
||||
static const char *http_get =
|
||||
"GET / HTTP/1.1\r\n"
|
||||
"Hostname: aaa\r\n"
|
||||
"\r\n\r\n";
|
||||
|
||||
static const char *options = "-host str [-port num] [-cacert file] [-cert file -key file -pass str]";
|
||||
|
||||
int tls13_client_main(int argc, char *argv[])
|
||||
{
|
||||
int ret = -1;
|
||||
char *prog = argv[0];
|
||||
char *host = NULL;
|
||||
int port = 443;
|
||||
char *cacertfile = NULL;
|
||||
char *certfile = NULL;
|
||||
char *keyfile = NULL;
|
||||
char *pass = NULL;
|
||||
struct hostent *hp;
|
||||
struct sockaddr_in server;
|
||||
int sock;
|
||||
TLS_CTX ctx;
|
||||
TLS_CONNECT conn;
|
||||
char buf[1024] = {0};
|
||||
size_t len = sizeof(buf);
|
||||
char send_buf[1024] = {0};
|
||||
size_t send_len;
|
||||
|
||||
argc--;
|
||||
argv++;
|
||||
if (argc < 1) {
|
||||
fprintf(stderr, "usage: %s %s\n", prog, options);
|
||||
return 1;
|
||||
}
|
||||
while (argc >= 1) {
|
||||
if (!strcmp(*argv, "-help")) {
|
||||
printf("usage: %s %s\n", prog, options);
|
||||
return 0;
|
||||
} else if (!strcmp(*argv, "-host")) {
|
||||
if (--argc < 1) goto bad;
|
||||
host = *(++argv);
|
||||
} else if (!strcmp(*argv, "-port")) {
|
||||
if (--argc < 1) goto bad;
|
||||
port = atoi(*(++argv));
|
||||
} else if (!strcmp(*argv, "-cacert")) {
|
||||
if (--argc < 1) goto bad;
|
||||
cacertfile = *(++argv);
|
||||
} else if (!strcmp(*argv, "-cert")) {
|
||||
if (--argc < 1) goto bad;
|
||||
certfile = *(++argv);
|
||||
} else if (!strcmp(*argv, "-key")) {
|
||||
if (--argc < 1) goto bad;
|
||||
keyfile = *(++argv);
|
||||
} else if (!strcmp(*argv, "-pass")) {
|
||||
if (--argc < 1) goto bad;
|
||||
pass = *(++argv);
|
||||
} else {
|
||||
fprintf(stderr, "%s: invalid option '%s'\n", prog, *argv);
|
||||
return 1;
|
||||
bad:
|
||||
fprintf(stderr, "%s: option '%s' argument required\n", prog, *argv);
|
||||
return 0;
|
||||
}
|
||||
argc--;
|
||||
argv++;
|
||||
}
|
||||
|
||||
if (!host) {
|
||||
fprintf(stderr, "%s: '-in' option required\n", prog);
|
||||
return -1;
|
||||
}
|
||||
if (!(hp = gethostbyname(host))) {
|
||||
herror("tls13_client: '-host' invalid");
|
||||
goto end;
|
||||
}
|
||||
|
||||
memset(&ctx, 0, sizeof(ctx));
|
||||
memset(&conn, 0, sizeof(conn));
|
||||
|
||||
server.sin_addr = *((struct in_addr *)hp->h_addr_list[0]);
|
||||
server.sin_family = AF_INET;
|
||||
server.sin_port = htons(port);
|
||||
|
||||
|
||||
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
|
||||
fprintf(stderr, "%s: open socket error : %s\n", prog, strerror(errno));
|
||||
goto end;
|
||||
}
|
||||
if (connect(sock, (struct sockaddr *)&server , sizeof(server)) < 0) {
|
||||
fprintf(stderr, "%s: connect error : %s\n", prog, strerror(errno));
|
||||
goto end;
|
||||
}
|
||||
|
||||
if (tls_ctx_init(&ctx, TLS_protocol_tls13, TLS_client_mode) != 1
|
||||
|| tls_ctx_set_cipher_suites(&ctx, client_ciphers, sizeof(client_ciphers)/sizeof(client_ciphers[0])) != 1) {
|
||||
fprintf(stderr, "%s: context init error\n", prog);
|
||||
goto end;
|
||||
}
|
||||
if (cacertfile) {
|
||||
if (tls_ctx_set_ca_certificates(&ctx, cacertfile, TLS_DEFAULT_VERIFY_DEPTH) != 1) {
|
||||
fprintf(stderr, "%s: context init error\n", prog);
|
||||
goto end;
|
||||
}
|
||||
}
|
||||
if (certfile) {
|
||||
if (tls_ctx_set_certificate_and_key(&ctx, certfile, keyfile, pass) != 1) {
|
||||
fprintf(stderr, "%s: context init error\n", prog);
|
||||
goto end;
|
||||
}
|
||||
}
|
||||
if (tls_init(&conn, &ctx) != 1
|
||||
|| tls_set_socket(&conn, sock) != 1
|
||||
|| tls_do_handshake(&conn) != 1) {
|
||||
fprintf(stderr, "%s: error\n", prog);
|
||||
goto end;
|
||||
}
|
||||
|
||||
for (;;) {
|
||||
fd_set fds;
|
||||
size_t sentlen;
|
||||
|
||||
FD_ZERO(&fds);
|
||||
FD_SET(conn.sock, &fds);
|
||||
FD_SET(STDIN_FILENO, &fds);
|
||||
|
||||
if (select(conn.sock + 1, &fds, NULL, NULL, NULL) < 0) {
|
||||
fprintf(stderr, "%s: select failed\n", prog);
|
||||
goto end;
|
||||
}
|
||||
|
||||
if (FD_ISSET(conn.sock, &fds)) {
|
||||
for (;;) {
|
||||
memset(buf, 0, sizeof(buf));
|
||||
if (tls13_recv(&conn, (uint8_t *)buf, sizeof(buf), &len) != 1) {
|
||||
goto end;
|
||||
}
|
||||
fwrite(buf, 1, len, stdout);
|
||||
fflush(stdout);
|
||||
|
||||
// 应该调整tls_recv 逻辑、API或者其他方式
|
||||
if (conn.datalen == 0) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
if (FD_ISSET(STDIN_FILENO, &fds)) {
|
||||
memset(send_buf, 0, sizeof(send_buf));
|
||||
|
||||
if (!fgets(send_buf, sizeof(send_buf), stdin)) {
|
||||
if (feof(stdin)) {
|
||||
tls_shutdown(&conn);
|
||||
goto end;
|
||||
} else {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
if (tls13_send(&conn, (uint8_t *)send_buf, strlen(send_buf), &sentlen) != 1) {
|
||||
fprintf(stderr, "%s: send error\n", prog);
|
||||
goto end;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
end:
|
||||
close(sock);
|
||||
tls_ctx_cleanup(&ctx);
|
||||
tls_cleanup(&conn);
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2014-2022 The GmSSL Project. All Rights Reserved.
|
||||
* Copyright 2022 The GmSSL Project. All Rights Reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the License); you may
|
||||
* not use this file except in compliance with the License.
|
||||
@@ -7,179 +7,178 @@
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*/
|
||||
|
||||
|
||||
|
||||
#include <stdio.h>
|
||||
#include <errno.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/types.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <sys/socket.h>
|
||||
#include <netinet/in.h>
|
||||
#include <gmssl/mem.h>
|
||||
#include <gmssl/sm2.h>
|
||||
#include <gmssl/tls.h>
|
||||
#include <gmssl/error.h>
|
||||
|
||||
|
||||
static const char *options = "[-port num] -cert file -key file -pass str [-cacert file]";
|
||||
|
||||
int tls13_server_main(int argc , char **argv)
|
||||
{
|
||||
int ret = 1;
|
||||
char *prog = argv[0];
|
||||
int port = 443;
|
||||
char *certfile = NULL;
|
||||
char *keyfile = NULL;
|
||||
char *pass = NULL;
|
||||
char *cacertfile = NULL;
|
||||
|
||||
int server_ciphers[] = { TLS_cipher_sm4_gcm_sm3, };
|
||||
uint8_t verify_buf[4096];
|
||||
|
||||
TLS_CTX ctx;
|
||||
TLS_CONNECT conn;
|
||||
char buf[1600] = {0};
|
||||
size_t len = sizeof(buf);
|
||||
|
||||
int sock;
|
||||
struct sockaddr_in server_addr;
|
||||
struct sockaddr_in client_addr;
|
||||
socklen_t client_addrlen;
|
||||
int conn_sock;
|
||||
|
||||
|
||||
argc--;
|
||||
argv++;
|
||||
|
||||
if (argc < 1) {
|
||||
fprintf(stderr, "usage: %s %s\n", prog, options);
|
||||
return 1;
|
||||
}
|
||||
|
||||
while (argc > 0) {
|
||||
if (!strcmp(*argv, "-help")) {
|
||||
printf("usage: %s %s\n", prog, options);
|
||||
return 0;
|
||||
} else if (!strcmp(*argv, "-port")) {
|
||||
if (--argc < 1) goto bad;
|
||||
port = atoi(*(++argv));
|
||||
} else if (!strcmp(*argv, "-cert")) {
|
||||
if (--argc < 1) goto bad;
|
||||
certfile = *(++argv);
|
||||
} else if (!strcmp(*argv, "-key")) {
|
||||
if (--argc < 1) goto bad;
|
||||
keyfile = *(++argv);
|
||||
} else if (!strcmp(*argv, "-pass")) {
|
||||
if (--argc < 1) goto bad;
|
||||
pass = *(++argv);
|
||||
} else if (!strcmp(*argv, "-cacert")) {
|
||||
if (--argc < 1) goto bad;
|
||||
cacertfile = *(++argv);
|
||||
} else {
|
||||
fprintf(stderr, "%s: invalid option '%s'\n", prog, *argv);
|
||||
return 1;
|
||||
bad:
|
||||
fprintf(stderr, "%s: option '%s' argument required\n", prog, *argv);
|
||||
return 1;
|
||||
}
|
||||
argc--;
|
||||
argv++;
|
||||
}
|
||||
if (!certfile) {
|
||||
fprintf(stderr, "%s: '-cert' option required\n", prog);
|
||||
return 1;
|
||||
}
|
||||
if (!keyfile) {
|
||||
fprintf(stderr, "%s: '-key' option required\n", prog);
|
||||
return 1;
|
||||
}
|
||||
if (!pass) {
|
||||
fprintf(stderr, "%s: '-pass' option required\n", prog);
|
||||
return 1;
|
||||
}
|
||||
|
||||
memset(&ctx, 0, sizeof(ctx));
|
||||
memset(&conn, 0, sizeof(conn));
|
||||
|
||||
if (tls_ctx_init(&ctx, TLS_protocol_tls13, TLS_server_mode) != 1
|
||||
|| tls_ctx_set_cipher_suites(&ctx, server_ciphers, sizeof(server_ciphers)/sizeof(int)) != 1
|
||||
|| tls_ctx_set_certificate_and_key(&ctx, certfile, keyfile, pass) != 1) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
if (cacertfile) {
|
||||
if (tls_ctx_set_ca_certificates(&ctx, cacertfile, TLS_DEFAULT_VERIFY_DEPTH) != 1) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
// Socket
|
||||
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
|
||||
error_print();
|
||||
return 1;
|
||||
}
|
||||
server_addr.sin_family = AF_INET;
|
||||
server_addr.sin_addr.s_addr = INADDR_ANY;
|
||||
server_addr.sin_port = htons(port);
|
||||
if (bind(sock, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0) {
|
||||
error_print();
|
||||
perror("tlcp_accept: bind: ");
|
||||
goto end;
|
||||
}
|
||||
puts("start listen ...\n");
|
||||
listen(sock, 1);
|
||||
|
||||
|
||||
|
||||
restart:
|
||||
|
||||
client_addrlen = sizeof(client_addr);
|
||||
if ((conn_sock = accept(sock, (struct sockaddr *)&client_addr, &client_addrlen)) < 0) {
|
||||
error_print();
|
||||
goto end;
|
||||
}
|
||||
puts("socket connected\n");
|
||||
|
||||
if (tls_init(&conn, &ctx) != 1
|
||||
|| tls_set_socket(&conn, conn_sock) != 1) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (tls_do_handshake(&conn) != 1) {
|
||||
error_print(); // 为什么这个会触发呢?
|
||||
return -1;
|
||||
}
|
||||
|
||||
for (;;) {
|
||||
|
||||
int rv;
|
||||
size_t sentlen;
|
||||
|
||||
do {
|
||||
len = sizeof(buf);
|
||||
if ((rv = tls13_recv(&conn, (uint8_t *)buf, sizeof(buf), &len)) != 1) {
|
||||
if (rv < 0) fprintf(stderr, "%s: recv failure\n", prog);
|
||||
else fprintf(stderr, "%s: Disconnected by remote\n", prog);
|
||||
|
||||
//close(conn.sock);
|
||||
tls_cleanup(&conn);
|
||||
goto restart;
|
||||
}
|
||||
} while (!len);
|
||||
|
||||
if (tls13_send(&conn, (uint8_t *)buf, len, &sentlen) != 1) {
|
||||
fprintf(stderr, "%s: send failure, close connection\n", prog);
|
||||
close(conn.sock);
|
||||
goto end;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
end:
|
||||
return ret;
|
||||
}
|
||||
|
||||
#include <stdio.h>
|
||||
#include <errno.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/types.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <sys/socket.h>
|
||||
#include <netinet/in.h>
|
||||
#include <gmssl/mem.h>
|
||||
#include <gmssl/sm2.h>
|
||||
#include <gmssl/tls.h>
|
||||
#include <gmssl/error.h>
|
||||
|
||||
|
||||
static const char *options = "[-port num] -cert file -key file -pass str [-cacert file]";
|
||||
|
||||
int tls13_server_main(int argc , char **argv)
|
||||
{
|
||||
int ret = 1;
|
||||
char *prog = argv[0];
|
||||
int port = 443;
|
||||
char *certfile = NULL;
|
||||
char *keyfile = NULL;
|
||||
char *pass = NULL;
|
||||
char *cacertfile = NULL;
|
||||
|
||||
int server_ciphers[] = { TLS_cipher_sm4_gcm_sm3, };
|
||||
uint8_t verify_buf[4096];
|
||||
|
||||
TLS_CTX ctx;
|
||||
TLS_CONNECT conn;
|
||||
char buf[1600] = {0};
|
||||
size_t len = sizeof(buf);
|
||||
|
||||
int sock;
|
||||
struct sockaddr_in server_addr;
|
||||
struct sockaddr_in client_addr;
|
||||
socklen_t client_addrlen;
|
||||
int conn_sock;
|
||||
|
||||
|
||||
argc--;
|
||||
argv++;
|
||||
|
||||
if (argc < 1) {
|
||||
fprintf(stderr, "usage: %s %s\n", prog, options);
|
||||
return 1;
|
||||
}
|
||||
|
||||
while (argc > 0) {
|
||||
if (!strcmp(*argv, "-help")) {
|
||||
printf("usage: %s %s\n", prog, options);
|
||||
return 0;
|
||||
} else if (!strcmp(*argv, "-port")) {
|
||||
if (--argc < 1) goto bad;
|
||||
port = atoi(*(++argv));
|
||||
} else if (!strcmp(*argv, "-cert")) {
|
||||
if (--argc < 1) goto bad;
|
||||
certfile = *(++argv);
|
||||
} else if (!strcmp(*argv, "-key")) {
|
||||
if (--argc < 1) goto bad;
|
||||
keyfile = *(++argv);
|
||||
} else if (!strcmp(*argv, "-pass")) {
|
||||
if (--argc < 1) goto bad;
|
||||
pass = *(++argv);
|
||||
} else if (!strcmp(*argv, "-cacert")) {
|
||||
if (--argc < 1) goto bad;
|
||||
cacertfile = *(++argv);
|
||||
} else {
|
||||
fprintf(stderr, "%s: invalid option '%s'\n", prog, *argv);
|
||||
return 1;
|
||||
bad:
|
||||
fprintf(stderr, "%s: option '%s' argument required\n", prog, *argv);
|
||||
return 1;
|
||||
}
|
||||
argc--;
|
||||
argv++;
|
||||
}
|
||||
if (!certfile) {
|
||||
fprintf(stderr, "%s: '-cert' option required\n", prog);
|
||||
return 1;
|
||||
}
|
||||
if (!keyfile) {
|
||||
fprintf(stderr, "%s: '-key' option required\n", prog);
|
||||
return 1;
|
||||
}
|
||||
if (!pass) {
|
||||
fprintf(stderr, "%s: '-pass' option required\n", prog);
|
||||
return 1;
|
||||
}
|
||||
|
||||
memset(&ctx, 0, sizeof(ctx));
|
||||
memset(&conn, 0, sizeof(conn));
|
||||
|
||||
if (tls_ctx_init(&ctx, TLS_protocol_tls13, TLS_server_mode) != 1
|
||||
|| tls_ctx_set_cipher_suites(&ctx, server_ciphers, sizeof(server_ciphers)/sizeof(int)) != 1
|
||||
|| tls_ctx_set_certificate_and_key(&ctx, certfile, keyfile, pass) != 1) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
if (cacertfile) {
|
||||
if (tls_ctx_set_ca_certificates(&ctx, cacertfile, TLS_DEFAULT_VERIFY_DEPTH) != 1) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
// Socket
|
||||
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
|
||||
error_print();
|
||||
return 1;
|
||||
}
|
||||
server_addr.sin_family = AF_INET;
|
||||
server_addr.sin_addr.s_addr = INADDR_ANY;
|
||||
server_addr.sin_port = htons(port);
|
||||
if (bind(sock, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0) {
|
||||
error_print();
|
||||
perror("tlcp_accept: bind: ");
|
||||
goto end;
|
||||
}
|
||||
puts("start listen ...\n");
|
||||
listen(sock, 1);
|
||||
|
||||
|
||||
|
||||
restart:
|
||||
|
||||
client_addrlen = sizeof(client_addr);
|
||||
if ((conn_sock = accept(sock, (struct sockaddr *)&client_addr, &client_addrlen)) < 0) {
|
||||
error_print();
|
||||
goto end;
|
||||
}
|
||||
puts("socket connected\n");
|
||||
|
||||
if (tls_init(&conn, &ctx) != 1
|
||||
|| tls_set_socket(&conn, conn_sock) != 1) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (tls_do_handshake(&conn) != 1) {
|
||||
error_print(); // 为什么这个会触发呢?
|
||||
return -1;
|
||||
}
|
||||
|
||||
for (;;) {
|
||||
|
||||
int rv;
|
||||
size_t sentlen;
|
||||
|
||||
do {
|
||||
len = sizeof(buf);
|
||||
if ((rv = tls13_recv(&conn, (uint8_t *)buf, sizeof(buf), &len)) != 1) {
|
||||
if (rv < 0) fprintf(stderr, "%s: recv failure\n", prog);
|
||||
else fprintf(stderr, "%s: Disconnected by remote\n", prog);
|
||||
|
||||
//close(conn.sock);
|
||||
tls_cleanup(&conn);
|
||||
goto restart;
|
||||
}
|
||||
} while (!len);
|
||||
|
||||
if (tls13_send(&conn, (uint8_t *)buf, len, &sentlen) != 1) {
|
||||
fprintf(stderr, "%s: send failure, close connection\n", prog);
|
||||
close(conn.sock);
|
||||
goto end;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
end:
|
||||
return ret;
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2014-2022 The GmSSL Project. All Rights Reserved.
|
||||
* Copyright 2022 The GmSSL Project. All Rights Reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the License); you may
|
||||
* not use this file except in compliance with the License.
|
||||
@@ -7,16 +7,15 @@
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*/
|
||||
|
||||
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <gmssl/version.h>
|
||||
|
||||
|
||||
int version_main(int argc, char **argv)
|
||||
{
|
||||
printf("%s\n", gmssl_version_str());
|
||||
return 0;
|
||||
}
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <gmssl/version.h>
|
||||
|
||||
|
||||
int version_main(int argc, char **argv)
|
||||
{
|
||||
printf("%s\n", gmssl_version_str());
|
||||
return 0;
|
||||
}
|
||||
|
||||
273
tools/zuc.c
273
tools/zuc.c
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2014-2022 The GmSSL Project. All Rights Reserved.
|
||||
* Copyright 2022 The GmSSL Project. All Rights Reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the License); you may
|
||||
* not use this file except in compliance with the License.
|
||||
@@ -7,139 +7,138 @@
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*/
|
||||
|
||||
|
||||
|
||||
#include <stdio.h>
|
||||
#include <errno.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <gmssl/mem.h>
|
||||
#include <gmssl/zuc.h>
|
||||
#include <gmssl/hex.h>
|
||||
|
||||
|
||||
static const char *options = "-key hex -iv hex [-in file] [-out file]";
|
||||
|
||||
int zuc_main(int argc, char **argv)
|
||||
{
|
||||
int ret = 1;
|
||||
char *prog = argv[0];
|
||||
char *keyhex = NULL;
|
||||
char *ivhex = NULL;
|
||||
char *infile = NULL;
|
||||
char *outfile = NULL;
|
||||
uint8_t key[16];
|
||||
uint8_t iv[16];
|
||||
size_t keylen = sizeof(key);
|
||||
size_t ivlen = sizeof(iv);
|
||||
FILE *infp = stdin;
|
||||
FILE *outfp = stdout;
|
||||
ZUC_CTX zuc_ctx;
|
||||
uint8_t inbuf[4096];
|
||||
size_t inlen;
|
||||
uint8_t outbuf[4196];
|
||||
size_t outlen;
|
||||
|
||||
argc--;
|
||||
argv++;
|
||||
|
||||
if (argc < 1) {
|
||||
fprintf(stderr, "usage: %s %s\n", prog, options);
|
||||
return 1;
|
||||
}
|
||||
|
||||
while (argc > 0) {
|
||||
if (!strcmp(*argv, "-help")) {
|
||||
printf("usage: %s %s\n", prog, options);
|
||||
ret = 0;
|
||||
goto end;
|
||||
} else if (!strcmp(*argv, "-key")) {
|
||||
if (--argc < 1) goto bad;
|
||||
keyhex = *(++argv);
|
||||
if (strlen(keyhex) != sizeof(key) * 2) {
|
||||
fprintf(stderr, "%s: invalid key length\n", prog);
|
||||
goto end;
|
||||
}
|
||||
if (hex_to_bytes(keyhex, strlen(keyhex), key, &keylen) != 1) {
|
||||
fprintf(stderr, "%s: invalid HEX digits\n", prog);
|
||||
goto end;
|
||||
}
|
||||
} else if (!strcmp(*argv, "-iv")) {
|
||||
if (--argc < 1) goto bad;
|
||||
ivhex = *(++argv);
|
||||
if (strlen(ivhex) != sizeof(iv) * 2) {
|
||||
fprintf(stderr, "%s: invalid IV length\n", prog);
|
||||
goto end;
|
||||
}
|
||||
if (hex_to_bytes(ivhex, strlen(ivhex), iv, &ivlen) != 1) {
|
||||
fprintf(stderr, "%s: invalid HEX digits\n", prog);
|
||||
goto end;
|
||||
}
|
||||
} else if (!strcmp(*argv, "-in")) {
|
||||
if (--argc < 1) goto bad;
|
||||
infile = *(++argv);
|
||||
if (!(infp = fopen(infile, "r"))) {
|
||||
fprintf(stderr, "%s: open '%s' failure : %s\n", prog, infile, strerror(errno));
|
||||
goto end;
|
||||
}
|
||||
} else if (!strcmp(*argv, "-out")) {
|
||||
if (--argc < 1) goto bad;
|
||||
outfile = *(++argv);
|
||||
if (!(outfp = fopen(outfile, "w"))) {
|
||||
fprintf(stderr, "%s: open '%s' failure : %s\n", prog, outfile, strerror(errno));
|
||||
goto end;
|
||||
}
|
||||
} else {
|
||||
fprintf(stderr, "%s: illegal option '%s'\n", prog, *argv);
|
||||
goto end;
|
||||
bad:
|
||||
fprintf(stderr, "%s: '%s' option value missing\n", prog, *argv);
|
||||
goto end;
|
||||
}
|
||||
|
||||
argc--;
|
||||
argv++;
|
||||
}
|
||||
|
||||
if (!keyhex) {
|
||||
fprintf(stderr, "%s: option '-key' missing\n", prog);
|
||||
goto end;
|
||||
}
|
||||
if (!ivhex) {
|
||||
fprintf(stderr, "%s: option '-iv' missing\n", prog);
|
||||
goto end;
|
||||
}
|
||||
|
||||
if (zuc_encrypt_init(&zuc_ctx, key, iv) != 1) {
|
||||
fprintf(stderr, "%s: inner error\n", prog);
|
||||
goto end;
|
||||
}
|
||||
while ((inlen = fread(inbuf, 1, sizeof(inbuf), infp)) > 0) {
|
||||
if (zuc_encrypt_update(&zuc_ctx, inbuf, inlen, outbuf, &outlen) != 1) {
|
||||
fprintf(stderr, "%s: inner error\n", prog);
|
||||
goto end;
|
||||
}
|
||||
if (fwrite(outbuf, 1, outlen, outfp) != outlen) {
|
||||
fprintf(stderr, "%s: output failure : %s\n", prog, strerror(errno));
|
||||
goto end;
|
||||
}
|
||||
}
|
||||
if (zuc_encrypt_finish(&zuc_ctx, outbuf, &outlen) != 1) {
|
||||
fprintf(stderr, "%s: inner error\n", prog);
|
||||
goto end;
|
||||
}
|
||||
if (fwrite(outbuf, 1, outlen, outfp) != outlen) {
|
||||
fprintf(stderr, "%s: output failure : %s\n", prog, strerror(errno));
|
||||
goto end;
|
||||
}
|
||||
ret = 0;
|
||||
end:
|
||||
gmssl_secure_clear(&zuc_ctx, sizeof(zuc_ctx));
|
||||
gmssl_secure_clear(key, sizeof(key));
|
||||
gmssl_secure_clear(iv, sizeof(iv));
|
||||
gmssl_secure_clear(inbuf, sizeof(inbuf));
|
||||
gmssl_secure_clear(outbuf, sizeof(outbuf));
|
||||
if (infile && infp) fclose(infp);
|
||||
if (outfile && outfp) fclose(outfp);
|
||||
return ret;
|
||||
}
|
||||
|
||||
#include <stdio.h>
|
||||
#include <errno.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <gmssl/mem.h>
|
||||
#include <gmssl/zuc.h>
|
||||
#include <gmssl/hex.h>
|
||||
|
||||
|
||||
static const char *options = "-key hex -iv hex [-in file] [-out file]";
|
||||
|
||||
int zuc_main(int argc, char **argv)
|
||||
{
|
||||
int ret = 1;
|
||||
char *prog = argv[0];
|
||||
char *keyhex = NULL;
|
||||
char *ivhex = NULL;
|
||||
char *infile = NULL;
|
||||
char *outfile = NULL;
|
||||
uint8_t key[16];
|
||||
uint8_t iv[16];
|
||||
size_t keylen = sizeof(key);
|
||||
size_t ivlen = sizeof(iv);
|
||||
FILE *infp = stdin;
|
||||
FILE *outfp = stdout;
|
||||
ZUC_CTX zuc_ctx;
|
||||
uint8_t inbuf[4096];
|
||||
size_t inlen;
|
||||
uint8_t outbuf[4196];
|
||||
size_t outlen;
|
||||
|
||||
argc--;
|
||||
argv++;
|
||||
|
||||
if (argc < 1) {
|
||||
fprintf(stderr, "usage: %s %s\n", prog, options);
|
||||
return 1;
|
||||
}
|
||||
|
||||
while (argc > 0) {
|
||||
if (!strcmp(*argv, "-help")) {
|
||||
printf("usage: %s %s\n", prog, options);
|
||||
ret = 0;
|
||||
goto end;
|
||||
} else if (!strcmp(*argv, "-key")) {
|
||||
if (--argc < 1) goto bad;
|
||||
keyhex = *(++argv);
|
||||
if (strlen(keyhex) != sizeof(key) * 2) {
|
||||
fprintf(stderr, "%s: invalid key length\n", prog);
|
||||
goto end;
|
||||
}
|
||||
if (hex_to_bytes(keyhex, strlen(keyhex), key, &keylen) != 1) {
|
||||
fprintf(stderr, "%s: invalid HEX digits\n", prog);
|
||||
goto end;
|
||||
}
|
||||
} else if (!strcmp(*argv, "-iv")) {
|
||||
if (--argc < 1) goto bad;
|
||||
ivhex = *(++argv);
|
||||
if (strlen(ivhex) != sizeof(iv) * 2) {
|
||||
fprintf(stderr, "%s: invalid IV length\n", prog);
|
||||
goto end;
|
||||
}
|
||||
if (hex_to_bytes(ivhex, strlen(ivhex), iv, &ivlen) != 1) {
|
||||
fprintf(stderr, "%s: invalid HEX digits\n", prog);
|
||||
goto end;
|
||||
}
|
||||
} else if (!strcmp(*argv, "-in")) {
|
||||
if (--argc < 1) goto bad;
|
||||
infile = *(++argv);
|
||||
if (!(infp = fopen(infile, "r"))) {
|
||||
fprintf(stderr, "%s: open '%s' failure : %s\n", prog, infile, strerror(errno));
|
||||
goto end;
|
||||
}
|
||||
} else if (!strcmp(*argv, "-out")) {
|
||||
if (--argc < 1) goto bad;
|
||||
outfile = *(++argv);
|
||||
if (!(outfp = fopen(outfile, "w"))) {
|
||||
fprintf(stderr, "%s: open '%s' failure : %s\n", prog, outfile, strerror(errno));
|
||||
goto end;
|
||||
}
|
||||
} else {
|
||||
fprintf(stderr, "%s: illegal option '%s'\n", prog, *argv);
|
||||
goto end;
|
||||
bad:
|
||||
fprintf(stderr, "%s: '%s' option value missing\n", prog, *argv);
|
||||
goto end;
|
||||
}
|
||||
|
||||
argc--;
|
||||
argv++;
|
||||
}
|
||||
|
||||
if (!keyhex) {
|
||||
fprintf(stderr, "%s: option '-key' missing\n", prog);
|
||||
goto end;
|
||||
}
|
||||
if (!ivhex) {
|
||||
fprintf(stderr, "%s: option '-iv' missing\n", prog);
|
||||
goto end;
|
||||
}
|
||||
|
||||
if (zuc_encrypt_init(&zuc_ctx, key, iv) != 1) {
|
||||
fprintf(stderr, "%s: inner error\n", prog);
|
||||
goto end;
|
||||
}
|
||||
while ((inlen = fread(inbuf, 1, sizeof(inbuf), infp)) > 0) {
|
||||
if (zuc_encrypt_update(&zuc_ctx, inbuf, inlen, outbuf, &outlen) != 1) {
|
||||
fprintf(stderr, "%s: inner error\n", prog);
|
||||
goto end;
|
||||
}
|
||||
if (fwrite(outbuf, 1, outlen, outfp) != outlen) {
|
||||
fprintf(stderr, "%s: output failure : %s\n", prog, strerror(errno));
|
||||
goto end;
|
||||
}
|
||||
}
|
||||
if (zuc_encrypt_finish(&zuc_ctx, outbuf, &outlen) != 1) {
|
||||
fprintf(stderr, "%s: inner error\n", prog);
|
||||
goto end;
|
||||
}
|
||||
if (fwrite(outbuf, 1, outlen, outfp) != outlen) {
|
||||
fprintf(stderr, "%s: output failure : %s\n", prog, strerror(errno));
|
||||
goto end;
|
||||
}
|
||||
ret = 0;
|
||||
end:
|
||||
gmssl_secure_clear(&zuc_ctx, sizeof(zuc_ctx));
|
||||
gmssl_secure_clear(key, sizeof(key));
|
||||
gmssl_secure_clear(iv, sizeof(iv));
|
||||
gmssl_secure_clear(inbuf, sizeof(inbuf));
|
||||
gmssl_secure_clear(outbuf, sizeof(outbuf));
|
||||
if (infile && infp) fclose(infp);
|
||||
if (outfile && outfp) fclose(outfp);
|
||||
return ret;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user