Init commit of gmssl-v3

This commit is contained in:
Zhi Guan
2021-07-13 19:21:43 +08:00
commit 0af5775be3
157 changed files with 60619 additions and 0 deletions

3
tools/README.md Normal file
View File

@@ -0,0 +1,3 @@
# Command Line Tools

278
tools/certgen.c Normal file
View File

@@ -0,0 +1,278 @@
/*
* Copyright (c) 2021 - 2021 The GmSSL Project. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. All advertising materials mentioning features or use of this
* software must display the following acknowledgment:
* "This product includes software developed by the GmSSL Project.
* (http://gmssl.org/)"
*
* 4. The name "GmSSL Project" must not be used to endorse or promote
* products derived from this software without prior written
* permission. For written permission, please contact
* guanzhi1980@gmail.com.
*
* 5. Products derived from this software may not be called "GmSSL"
* nor may "GmSSL" appear in their names without prior written
* permission of the GmSSL Project.
*
* 6. Redistributions of any form whatsoever must retain the following
* acknowledgment:
* "This product includes software developed by the GmSSL Project
* (http://gmssl.org/)"
*
* THIS SOFTWARE IS PROVIDED BY THE GmSSL PROJECT ``AS IS'' AND ANY
* EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE GmSSL PROJECT OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <gmssl/pem.h>
#include <gmssl/x509.h>
#include <gmssl/pkcs8.h>
#include <gmssl/rand.h>
#include <gmssl/error.h>
/*
from RFC 2253
String X.500 AttributeType
------------------------------
CN commonName
L localityName
ST stateOrProvinceName
O organizationName
OU organizationalUnitName
C countryName
STREET streetAddress
DC domainComponent
UID userid
*/
void print_usage(const char *prog)
{
printf("Usage: %s command [options] ...\n", prog);
printf("\n");
printf("Options:\n");
printf(" -C <str> country name\n");
printf(" -O <str> orgnization name\n");
printf(" -OU <str> orgnizational unit name\n");
printf(" -CN <str> common name\n");
printf(" -L <str> locality name\n");
printf(" -ST <str> state of province name\n");
printf(" -days <num> validity days\n");
printf(" -keyfile <file> private key file\n");
}
int main(int argc, char **argv)
{
int ret = -1;
char *prog = basename(argv[0]);
char *country = NULL;
char *state = NULL;
char *org = NULL;
char *org_unit = NULL;
char *common_name = NULL;
char *keyfile = NULL;
int days = 0;
FILE *keyfp = NULL;
X509_CERTIFICATE cert;
char *pass;
uint8_t serial[12];
X509_NAME name;
time_t not_before;
SM2_KEY sm2_key; // 这个应该是从文件中读取的!
uint8_t uniq_id[32];
uint8_t buf[1024];
const uint8_t *cp = buf;
uint8_t *p = buf;
size_t len = 0;
int kp[] = {
OID_kp_serverAuth,
OID_kp_clientAuth,
OID_kp_codeSigning,
OID_kp_emailProtection,
OID_kp_timeStamping,
OID_kp_OCSPSigning,
};
if (argc < 2) {
print_usage(prog);
return 0;
}
argc--;
argv++;
while (argc >= 1) {
if (!strcmp(*argv, "-help")) {
print_usage(prog);
return 0;
} 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, "-keyfile")) {
if (--argc < 1) goto bad;
keyfile = *(++argv);
} else if (!strcmp(*argv, "-days")) {
if (--argc < 1) goto bad;
days = atoi(*(++argv));
} else {
print_usage(prog);
return 0;
break;
}
argc--;
argv++;
}
if (days <= 0 && !keyfile) {
goto bad;
}
if (!(keyfp = fopen(keyfile, "r"))) {
goto bad;
}
pass = getpass("Password : ");
if (sm2_enced_private_key_info_from_pem(&sm2_key, pass, keyfp) != 1) {
error_print();
goto end;
}
rand_bytes(serial, sizeof(serial));
memset(&name, 0, sizeof(name));
if (country) {
if (x509_name_set_country(&name, country) != 1) {
error_print();
goto end;
}
}
if (state) {
if (x509_name_set_state_or_province(&name, state) != 1) {
error_print();
goto end;
}
}
if (org) {
if (x509_name_set_organization(&name, org) != 1) {
error_print();
goto end;
}
}
if (org_unit) {
if (x509_name_set_organizational_unit(&name, org_unit) != 1) {
error_print();
goto end;
}
}
if (!common_name) {
error_print();
goto end;
} else {
if (x509_name_set_common_name(&name, common_name) != 1) {
error_print();
goto end;
}
}
time(&not_before);
memset(&cert, 0, sizeof(cert));
x509_certificate_set_version(&cert, X509_version_v3);
x509_certificate_set_serial_number(&cert, serial, sizeof(serial));
x509_certificate_set_signature_algor(&cert, OID_sm2sign_with_sm3);
x509_certificate_set_issuer(&cert, &name);
x509_certificate_set_subject(&cert, &name);
x509_certificate_set_validity(&cert, not_before, days);
x509_certificate_set_subject_public_key_info_sm2(&cert, &sm2_key);
x509_certificate_set_issuer_unique_id_from_public_key(&cert, &sm2_key);
x509_certificate_set_subject_unique_id_from_public_key(&cert, &sm2_key);
x509_certificate_set_basic_constraints(&cert, ASN1_TRUE, ASN1_TRUE, 6);
x509_certificate_set_ext_key_usage(&cert, ASN1_TRUE, kp, sizeof(kp)/sizeof(kp[0]));
x509_certificate_generate_subject_key_identifier(&cert, ASN1_TRUE);
x509_certificate_set_inhibit_any_policy(&cert, ASN1_TRUE, 20);
x509_certificate_set_policy_constraints(&cert, ASN1_FALSE, 5, 5);
x509_certificate_sign_sm2(&cert, &sm2_key);
x509_certificate_to_pem(&cert, stdout);
ret = 0;
goto end;
bad:
fprintf(stderr, "%s: commands should not be used together\n", prog);
end:
return ret;
}

194
tools/certverify.c Normal file
View File

@@ -0,0 +1,194 @@
/*
* Copyright (c) 2021 - 2021 The GmSSL Project. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. All advertising materials mentioning features or use of this
* software must display the following acknowledgment:
* "This product includes software developed by the GmSSL Project.
* (http://gmssl.org/)"
*
* 4. The name "GmSSL Project" must not be used to endorse or promote
* products derived from this software without prior written
* permission. For written permission, please contact
* guanzhi1980@gmail.com.
*
* 5. Products derived from this software may not be called "GmSSL"
* nor may "GmSSL" appear in their names without prior written
* permission of the GmSSL Project.
*
* 6. Redistributions of any form whatsoever must retain the following
* acknowledgment:
* "This product includes software developed by the GmSSL Project
* (http://gmssl.org/)"
*
* THIS SOFTWARE IS PROVIDED BY THE GmSSL PROJECT ``AS IS'' AND ANY
* EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE GmSSL PROJECT OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <gmssl/pem.h>
#include <gmssl/x509.h>
#include <gmssl/pkcs8.h>
#include <gmssl/rand.h>
#include <gmssl/error.h>
static int verify_cert(const X509_CERTIFICATE *cert, const X509_CERTIFICATE *cacert)
{
int ret;
SM2_KEY ca_pubkey;
if (x509_name_equ(&cert->tbs_certificate.issuer, &cacert->tbs_certificate.subject) != 1) {
error_print();
return -1;
}
if (x509_certificate_get_public_key(cacert, &ca_pubkey) != 1) {
error_print();
return -1;
}
if ((ret = x509_certificate_verify(cert, &ca_pubkey)) < 0) {
error_print();
return -1;
}
return ret;
}
static int find_cacert(X509_CERTIFICATE *cacert, FILE *fp, const X509_NAME *issuer)
{
int ret;
for (;;) {
if ((ret = x509_certificate_from_pem(cacert, fp)) != 1) {
if (ret < 0) error_print();
return ret;
}
if (x509_name_equ(&cacert->tbs_certificate.subject, issuer) == 1) {
return 1;
}
}
return 0;
}
void print_usage(const char *prog)
{
printf("Usage: %s command [options] ...\n", prog);
printf("\n");
printf("Options:\n");
printf(" -cert <file> PKCS #10 certificate request file\n");
printf(" -cacert <file> CA certificate file\n");
}
int main(int argc, char **argv)
{
int ret = -1;
char *prog = basename(argv[0]);
char *certfile = NULL;
char *cacertfile = NULL;
FILE *certfp = NULL;
FILE *cacertfp = NULL;
X509_CERTIFICATE cert1;
X509_CERTIFICATE cert2;
X509_CERTIFICATE *cert = &cert1;
X509_CERTIFICATE *cacert = &cert2;
X509_CERTIFICATE *tmpcert;
SM2_KEY ca_pubkey;
argc--;
argv++;
while (argc >= 1) {
if (!strcmp(*argv, "-help")) {
print_usage(prog);
return 0;
} else if (!strcmp(*argv, "-cert")) {
if (--argc < 1) goto bad;
certfile = *(++argv);
if (!(certfp = fopen(certfile, "r"))) {
error_print();
return -1;
}
} else if (!strcmp(*argv, "-cacert")) {
if (--argc < 1) goto bad;
cacertfile = *(++argv);
if (!(cacertfp = fopen(cacertfile, "r"))) {
error_print();
return -1;
}
} else {
print_usage(prog);
return 0;
break;
}
argc--;
argv++;
}
if (!certfp || !cacertfp) {
print_usage(prog);
return -1;
}
if (x509_certificate_from_pem(cert, certfp) != 1) {
error_print();
return -1;
}
for (;;) {
if ((ret = x509_certificate_from_pem(cacert, certfp)) != 1) {
if (ret < 0) error_print();
break;
}
if (verify_cert(cert, cacert) != 1) {
error_print();
return -1;
}
tmpcert = cacert;
cert = cacert;
cacert = tmpcert;
}
if (find_cacert(cacert, cacertfp, &cert->tbs_certificate.issuer) != 1) {
error_print();
return -1;
}
if ((ret = verify_cert(cert, cacert)) < 0) {
error_print();
return -1;
}
printf("Verification %s\n", ret ? "success" : "failure");
ret = 0;
goto end;
bad:
fprintf(stderr, "%s: commands should not be used together\n", prog);
end:
return ret;
}

76
tools/certview.c Normal file
View File

@@ -0,0 +1,76 @@
/*
* Copyright (c) 2020 - 2021 The GmSSL Project. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. All advertising materials mentioning features or use of this
* software must display the following acknowledgment:
* "This product includes software developed by the GmSSL Project.
* (http://gmssl.org/)"
*
* 4. The name "GmSSL Project" must not be used to endorse or promote
* products derived from this software without prior written
* permission. For written permission, please contact
* guanzhi1980@gmail.com.
*
* 5. Products derived from this software may not be called "GmSSL"
* nor may "GmSSL" appear in their names without prior written
* permission of the GmSSL Project.
*
* 6. Redistributions of any form whatsoever must retain the following
* acknowledgment:
* "This product includes software developed by the GmSSL Project
* (http://gmssl.org/)"
*
* THIS SOFTWARE IS PROVIDED BY THE GmSSL PROJECT ``AS IS'' AND ANY
* EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE GmSSL PROJECT OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <gmssl/pem.h>
#include <gmssl/x509.h>
#include <gmssl/error.h>
int main(void)
{
X509_CERTIFICATE cert;
for (;;) {
int ret = x509_certificate_from_pem(&cert, stdin);
if (ret < 0) {
error_print();
return -1;
}
if (ret == 0) {
goto end;
}
fprintf(stdout, "Certificate\n");
x509_certificate_print(stdout, &cert, 0, 0);
x509_certificate_to_pem(&cert, stdout);
fprintf(stdout, "\n");
}
end:
return 0;
}

226
tools/digest.c Normal file
View File

@@ -0,0 +1,226 @@
/*
* Copyright (c) 2020 - 2021 The GmSSL Project. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. All advertising materials mentioning features or use of this
* software must display the following acknowledgment:
* "This product includes software developed by the GmSSL Project.
* (http://gmssl.org/)"
*
* 4. The name "GmSSL Project" must not be used to endorse or promote
* products derived from this software without prior written
* permission. For written permission, please contact
* guanzhi1980@gmail.com.
*
* 5. Products derived from this software may not be called "GmSSL"
* nor may "GmSSL" appear in their names without prior written
* permission of the GmSSL Project.
*
* 6. Redistributions of any form whatsoever must retain the following
* acknowledgment:
* "This product includes software developed by the GmSSL Project
* (http://gmssl.org/)"
*
* THIS SOFTWARE IS PROVIDED BY THE GmSSL PROJECT ``AS IS'' AND ANY
* EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE GmSSL PROJECT OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <libgen.h>
#include <gmssl/digest.h>
#define FORMAT_HEX 1
#define FORMAT_BIN 2
void print_usage(FILE *out, const char *prog)
{
fprintf(out, "Usage: %s command [options] ...\n", prog);
fprintf(out, "\n");
fprintf(out, "Commands:\n");
fprintf(out, " -help print the usage message\n");
fprintf(out, " -sm3 use SM3\n");
fprintf(out, " -md5 use MD5\n");
fprintf(out, " -sha1 use SHA-1\n");
fprintf(out, " -sha224 use SHA-224\n");
fprintf(out, " -sha256 use SHA-256\n");
fprintf(out, " -sha384 use SHA-384\n");
fprintf(out, " -sha512 use SHA-512\n");
fprintf(out, " -hex generate hex output\n");
fprintf(out, " -binary generate binary output\n");
fprintf(out, " -out file set output filename\n");
}
int main(int argc, char **argv)
{
int ret = -1;
char *prog = basename(argv[0]);
int help = 0;
const DIGEST *digest = NULL;
int format = FORMAT_HEX;
char *infile = NULL;
char *outfile = NULL;
FILE *in = stdin;
FILE *out = stdout;
DIGEST_CTX ctx;
unsigned char dgst[64];
unsigned char buf[4096];
size_t len;
size_t dgstlen, i;
argc--;
argv++;
while (argc >= 1) {
if (!strcmp(*argv, "-help")) {
print_usage(stdout, prog);
goto end;
} else if (!strcmp(*argv, "-sm3")) {
if (digest) goto bad;
digest = DIGEST_sm3();
} else if (!strcmp(*argv, "-md5")) {
if (digest) goto bad;
digest = DIGEST_md5();
} else if (!strcmp(*argv, "-sha1")) {
if (digest) goto bad;
digest = DIGEST_sha1();
} else if (!strcmp(*argv, "-sha224")) {
if (digest) goto bad;
digest = DIGEST_sha224();
} else if (!strcmp(*argv, "-sha256")) {
if (digest) goto bad;
digest = DIGEST_sha256();
} else if (!strcmp(*argv, "-sha384")) {
if (digest) goto bad;
digest = DIGEST_sha384();
} else if (!strcmp(*argv, "-sha512")) {
if (digest) goto bad;
digest = DIGEST_sha512();
} else if (!strcmp(*argv, "-hex")) {
format = FORMAT_HEX;
} else if (!strcmp(*argv, "-binary")) {
format = FORMAT_BIN;
} else if (!strcmp(*argv, "-out")) {
if (--argc < 1) goto bad;
outfile = *(++argv);
} else {
break;
}
argc--;
argv++;
}
if (!digest) {
fprintf(stderr, "%s: digest algorithm not speicified\n", prog);
fprintf(stderr, "usage: %s -[sm2|sha224|sha256|sha384|sha512] ...\n", prog);
return 1;
}
if (outfile) {
if (!(out = fopen(outfile, "wb"))) {
fprintf(stderr, "%s: can not open %s\n", prog, outfile);
return 1;
}
}
digest_ctx_init(&ctx);
if (!argc) {
if (!digest_init(&ctx, digest)) {
goto end;
}
while ((len = fread(buf, 1, sizeof(buf), stdin)) > 0) {
if (!digest_update(&ctx, buf, len)) {
goto end;
}
}
if (!digest_finish(&ctx, dgst, &len)) {
goto end;
}
if (format == FORMAT_BIN) {
fwrite(dgst, 1, len, out);
} else {
for (i = 0; i < len; i++) {
printf("%02x", dgst[i]);
}
printf("\n");
}
ret = 0;
goto end;
}
// 多个输出文件,输出文件名和二进制输出有冲突
while (argc > 0) {
infile = *argv++;
if (!(in = fopen(infile, "rb"))) {
fprintf(stderr, "%s: can not open input file %s\n", prog, infile);
goto end;
}
if (!digest_init(&ctx, digest)) {
goto end;
}
while ((len = fread(buf, 1, sizeof(buf), in)) > 0) {
if (!digest_update(&ctx, buf, len)) {
goto end;
}
}
fclose(in);
if (!digest_finish(&ctx, dgst, &dgstlen)) {
goto end;
}
for (i = 0; i < dgstlen; i++) {
printf("%02x", dgst[i]);
}
printf(" %s\n", infile);
argc--;
}
ret = 0;
goto end;
bad:
fprintf(stderr, "%s: commands should not be used together\n", prog);
end:
digest_ctx_cleanup(&ctx);
fclose(out);
return ret;
}

199
tools/hmac.c Normal file
View File

@@ -0,0 +1,199 @@
/*
* Copyright (c) 2020 - 2021 The GmSSL Project. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. All advertising materials mentioning features or use of this
* software must display the following acknowledgment:
* "This product includes software developed by the GmSSL Project.
* (http://gmssl.org/)"
*
* 4. The name "GmSSL Project" must not be used to endorse or promote
* products derived from this software without prior written
* permission. For written permission, please contact
* guanzhi1980@gmail.com.
*
* 5. Products derived from this software may not be called "GmSSL"
* nor may "GmSSL" appear in their names without prior written
* permission of the GmSSL Project.
*
* 6. Redistributions of any form whatsoever must retain the following
* acknowledgment:
* "This product includes software developed by the GmSSL Project
* (http://gmssl.org/)"
*
* THIS SOFTWARE IS PROVIDED BY THE GmSSL PROJECT ``AS IS'' AND ANY
* EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE GmSSL PROJECT OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <libgen.h>
#include <gmssl/digest.h>
#define FORMAT_HEX 1
#define FORMAT_BIN 2
void print_usage(FILE *out, const char *prog)
{
fprintf(out, "Usage: %s command [options] ...\n", prog);
fprintf(out, "\n");
fprintf(out, "Commands:\n");
fprintf(out, " -help print the usage message\n");
fprintf(out, " -digest algor print the usage message\n");
fprintf(out, " -key hex set the key in hex\n");
fprintf(out, " -hex generate hex output\n");
fprintf(out, " -binary generate binary output\n");
fprintf(out, " -out file set output filename\n");
}
int main(int argc, char **argv)
{
int ret = -1;
char *prog = basename(argv[0]);
int help = 0;
const DIGEST *digest = NULL;
int format = FORMAT_HEX;
char *infile = NULL;
char *outfile = NULL;
FILE *in = stdin;
FILE *out = stdout;
DIGEST_CTX ctx;
unsigned char dgst[64];
unsigned char buf[4096];
size_t len;
size_t dgstlen, i;
argc--;
argv++;
while (argc >= 1) {
if (!strcmp(*argv, "-help")) {
print_usage(stdout, prog);
goto end;
} else if (!strcmp(*argv, "-digest")) {
if (--argc < 1) goto bad;
algor = *(++argv);
} else if (!strcmp(*argv, "-key")) {
if (--argc < 1) goto bad;
algor = *(++argv);
} else if (!strcmp(*argv, "-hex")) {
format = FORMAT_HEX;
} else if (!strcmp(*argv, "-binary")) {
format = FORMAT_BIN;
} else if (!strcmp(*argv, "-out")) {
if (--argc < 1) goto bad;
outfile = *(++argv);
} else {
break;
}
argc--;
argv++;
}
if (!algor) {
}
if (outfile) {
if (!(out = fopen(outfile, "wb"))) {
fprintf(stderr, "%s: can not open %s\n", prog, outfile);
return 1;
}
}
digest_ctx_init(&ctx);
if (!argc) {
if (!digest_init(&ctx, digest)) {
goto end;
}
while ((len = fread(buf, 1, sizeof(buf), stdin)) > 0) {
if (!digest_update(&ctx, buf, len)) {
goto end;
}
}
if (!digest_finish(&ctx, dgst, &len)) {
goto end;
}
if (format == FORMAT_BIN) {
fwrite(dgst, 1, len, out);
} else {
for (i = 0; i < len; i++) {
printf("%02x", dgst[i]);
}
printf("\n");
}
ret = 0;
goto end;
}
// 多个输出文件,输出文件名和二进制输出有冲突
while (argc > 0) {
infile = *argv++;
if (!(in = fopen(infile, "rb"))) {
fprintf(stderr, "%s: can not open input file %s\n", prog, infile);
goto end;
}
if (!digest_init(&ctx, digest)) {
goto end;
}
while ((len = fread(buf, 1, sizeof(buf), in)) > 0) {
if (!digest_update(&ctx, buf, len)) {
goto end;
}
}
fclose(in);
if (!digest_finish(&ctx, dgst, &dgstlen)) {
goto end;
}
for (i = 0; i < dgstlen; i++) {
printf("%02x", dgst[i]);
}
printf(" %s\n", infile);
argc--;
}
ret = 0;
goto end;
bad:
fprintf(stderr, "%s: commands should not be used together\n", prog);
end:
digest_ctx_cleanup(&ctx);
fclose(out);
return ret;
}

113
tools/oid.c Normal file
View File

@@ -0,0 +1,113 @@
/*
* Copyright (c) 2020 - 2021 The GmSSL Project. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. All advertising materials mentioning features or use of this
* software must display the following acknowledgment:
* "This product includes software developed by the GmSSL Project.
* (http://gmssl.org/)"
*
* 4. The name "GmSSL Project" must not be used to endorse or promote
* products derived from this software without prior written
* permission. For written permission, please contact
* guanzhi1980@gmail.com.
*
* 5. Products derived from this software may not be called "GmSSL"
* nor may "GmSSL" appear in their names without prior written
* permission of the GmSSL Project.
*
* 6. Redistributions of any form whatsoever must retain the following
* acknowledgment:
* "This product includes software developed by the GmSSL Project
* (http://gmssl.org/)"
*
* THIS SOFTWARE IS PROVIDED BY THE GmSSL PROJECT ``AS IS'' AND ANY
* EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE GmSSL PROJECT OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <gmssl/oid.h>
#include <gmssl/asn1.h>
// 对OID的几种类型进行转换
// name, oid int值DER编码 -oid 112 -name -der
// 这里我们要做类型转换
/*
oid -name secp256k1
secp256k1 : 1.2.3.4 : 030201220308 : hello world
*/
void print_usage(FILE *out, const char *prog)
{
fprintf(out, "Usage: %s command [options] ...\n", prog);
fprintf(out, "\n");
fprintf(out, "Commands:\n");
fprintf(out, " -help print the usage message\n");
fprintf(out, " -name <str> oid name string\n");
fprintf(out, " -oid <int> oid value\n");
fprintf(out, " -der <hex> oid der encoding in hex\n");
}
int oid_vec_to_der(const unsigned int *oid, size_t oidlen, uint8_t *out, size_t *outlen)
{
if (oidlen < 2) {
return -1;
}
*out++ = oid[0] * 40 + oid[1];
oidlen -= 2;
*outlen++;
while (oidlen > 0) {
id (*oid < 128) {
*out++ = (uint8_t)(*oid);
} else {
*out++ = 0x80 & ((uint8_t)(*oid >> 8) << 1);
*out++ = 0x80 | (uint8_t)(*oid);
}
oid++;
oidlen--;
}
}
int main(int argc, char **argv)
{
int ret = -1;
char *prog = basename(argv[0]);
int help = 0;
argc--;
argv++;
while (argc >= 0) {
}
}

76
tools/pkcs8gen.c Normal file
View File

@@ -0,0 +1,76 @@
/*
* Copyright (c) 2021 - 2021 The GmSSL Project. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. All advertising materials mentioning features or use of this
* software must display the following acknowledgment:
* "This product includes software developed by the GmSSL Project.
* (http://gmssl.org/)"
*
* 4. The name "GmSSL Project" must not be used to endorse or promote
* products derived from this software without prior written
* permission. For written permission, please contact
* guanzhi1980@gmail.com.
*
* 5. Products derived from this software may not be called "GmSSL"
* nor may "GmSSL" appear in their names without prior written
* permission of the GmSSL Project.
*
* 6. Redistributions of any form whatsoever must retain the following
* acknowledgment:
* "This product includes software developed by the GmSSL Project
* (http://gmssl.org/)"
*
* THIS SOFTWARE IS PROVIDED BY THE GmSSL PROJECT ``AS IS'' AND ANY
* EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE GmSSL PROJECT OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <gmssl/sm2.h>
#include <gmssl/pem.h>
#include <gmssl/pkcs8.h>
#include <gmssl/error.h>
int main(void)
{
SM2_KEY key;
char *pass = NULL;
char passbuf[64] = {0};
pass = getpass("Encryption Password : ");
strncpy(passbuf, pass, sizeof(passbuf));
pass = getpass("Encryption Password (Again) : ");
if (strcmp(passbuf, pass) != 0) {
fprintf(stderr, "error: passwords not match\n");
return -1;
}
sm2_keygen(&key);
sm2_enced_private_key_info_to_pem(&key, pass, stdout);
return 0;
}

72
tools/pkcs8view.c Normal file
View File

@@ -0,0 +1,72 @@
/*
* Copyright (c) 2020 - 2021 The GmSSL Project. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. All advertising materials mentioning features or use of this
* software must display the following acknowledgment:
* "This product includes software developed by the GmSSL Project.
* (http://gmssl.org/)"
*
* 4. The name "GmSSL Project" must not be used to endorse or promote
* products derived from this software without prior written
* permission. For written permission, please contact
* guanzhi1980@gmail.com.
*
* 5. Products derived from this software may not be called "GmSSL"
* nor may "GmSSL" appear in their names without prior written
* permission of the GmSSL Project.
*
* 6. Redistributions of any form whatsoever must retain the following
* acknowledgment:
* "This product includes software developed by the GmSSL Project
* (http://gmssl.org/)"
*
* THIS SOFTWARE IS PROVIDED BY THE GmSSL PROJECT ``AS IS'' AND ANY
* EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE GmSSL PROJECT OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <pwd.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <gmssl/sm2.h>
#include <gmssl/pem.h>
#include <gmssl/pkcs8.h>
#include <gmssl/error.h>
int main(void)
{
SM2_KEY key;
const char *pass = NULL;
pass = getpass("Password : ");
if (sm2_enced_private_key_info_from_pem(&key, pass, stdin) != 1) {
error_print();
return -1;
}
sm2_key_print(stdout, &key, 0, 0);
sm2_public_key_info_to_pem(&key, stdout);
return 0;
}

217
tools/reqgen.c Normal file
View File

@@ -0,0 +1,217 @@
/*
* Copyright (c) 2021 - 2021 The GmSSL Project. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. All advertising materials mentioning features or use of this
* software must display the following acknowledgment:
* "This product includes software developed by the GmSSL Project.
* (http://gmssl.org/)"
*
* 4. The name "GmSSL Project" must not be used to endorse or promote
* products derived from this software without prior written
* permission. For written permission, please contact
* guanzhi1980@gmail.com.
*
* 5. Products derived from this software may not be called "GmSSL"
* nor may "GmSSL" appear in their names without prior written
* permission of the GmSSL Project.
*
* 6. Redistributions of any form whatsoever must retain the following
* acknowledgment:
* "This product includes software developed by the GmSSL Project
* (http://gmssl.org/)"
*
* THIS SOFTWARE IS PROVIDED BY THE GmSSL PROJECT ``AS IS'' AND ANY
* EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE GmSSL PROJECT OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <gmssl/pem.h>
#include <gmssl/x509.h>
#include <gmssl/pkcs8.h>
#include <gmssl/rand.h>
#include <gmssl/error.h>
void print_usage(const char *prog)
{
printf("Usage: %s command [options] ...\n", prog);
printf("\n");
printf("Options:\n");
printf(" -C <str> country name\n");
printf(" -O <str> orgnization name\n");
printf(" -OU <str> orgnizational unit name\n");
printf(" -CN <str> common name\n");
printf(" -L <str> locality name\n");
printf(" -ST <str> state of province name\n");
printf("\n");
printf(" -days <num> validity days\n");
printf(" -keyfile <file> private key file\n");
}
int main(int argc, char **argv)
{
int ret = -1;
char *prog = basename(argv[0]);
char *country = NULL;
char *state = NULL;
char *org = NULL;
char *org_unit = NULL;
char *common_name = NULL;
char *keyfile = NULL;
int days = 0;
FILE *keyfp = NULL;
X509_CERT_REQUEST req;
char *pass;
X509_NAME name;
SM2_KEY sm2_key; // 这个应该是从文件中读取的!
if (argc < 2) {
print_usage(prog);
return 0;
}
argc--;
argv++;
while (argc >= 1) {
if (!strcmp(*argv, "-help")) {
print_usage(prog);
return 0;
} 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, "-keyfile")) {
if (--argc < 1) goto bad;
keyfile = *(++argv);
} else if (!strcmp(*argv, "-days")) {
if (--argc < 1) goto bad;
days = atoi(*(++argv));
} else {
print_usage(prog);
return 0;
break;
}
argc--;
argv++;
}
if (days <= 0 && !keyfile) {
goto bad;
}
if (!(keyfp = fopen(keyfile, "r"))) {
goto bad;
}
pass = getpass("Password : ");
if (sm2_enced_private_key_info_from_pem(&sm2_key, pass, keyfp) != 1) {
error_print();
goto end;
}
memset(&name, 0, sizeof(name));
if (country) {
if (x509_name_set_country(&name, country) != 1) {
error_print();
goto end;
}
}
if (state) {
if (x509_name_set_state_or_province(&name, state) != 1) {
error_print();
goto end;
}
}
if (org) {
if (x509_name_set_organization(&name, org) != 1) {
error_print();
goto end;
}
}
if (org_unit) {
if (x509_name_set_organizational_unit(&name, org_unit) != 1) {
error_print();
goto end;
}
}
if (!common_name) {
error_print();
goto end;
} else {
if (x509_name_set_common_name(&name, common_name) != 1) {
error_print();
goto end;
}
}
memset(&req, 0, sizeof(req));
x509_cert_request_set(&req, &name, &sm2_key);
x509_cert_request_sign(&req, &sm2_key);
x509_cert_request_to_pem(&req, stdout);
ret = 0;
goto end;
bad:
fprintf(stderr, "%s: commands should not be used together\n", prog);
end:
return ret;
}

225
tools/reqsign.c Normal file
View File

@@ -0,0 +1,225 @@
/*
* Copyright (c) 2021 - 2021 The GmSSL Project. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. All advertising materials mentioning features or use of this
* software must display the following acknowledgment:
* "This product includes software developed by the GmSSL Project.
* (http://gmssl.org/)"
*
* 4. The name "GmSSL Project" must not be used to endorse or promote
* products derived from this software without prior written
* permission. For written permission, please contact
* guanzhi1980@gmail.com.
*
* 5. Products derived from this software may not be called "GmSSL"
* nor may "GmSSL" appear in their names without prior written
* permission of the GmSSL Project.
*
* 6. Redistributions of any form whatsoever must retain the following
* acknowledgment:
* "This product includes software developed by the GmSSL Project
* (http://gmssl.org/)"
*
* THIS SOFTWARE IS PROVIDED BY THE GmSSL PROJECT ``AS IS'' AND ANY
* EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE GmSSL PROJECT OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <gmssl/pem.h>
#include <gmssl/x509.h>
#include <gmssl/pkcs8.h>
#include <gmssl/rand.h>
#include <gmssl/error.h>
void print_usage(const char *prog)
{
printf("Usage: %s command [options] ...\n", prog);
printf("\n");
printf("Options:\n");
printf(" -req <file> PKCS #10 certificate request file\n");
printf(" -cacert <file> CA certificate file\n");
printf(" -keyfile <file> private key of cacert\n");
}
int main(int argc, char **argv)
{
int ret = -1;
char *prog = basename(argv[0]);
char *keyfile = NULL;
FILE *keyfp = NULL;
X509_CERTIFICATE cert;
char *pass;
uint8_t serial[12];
X509_NAME name;
time_t not_before;
SM2_KEY sm2_key; // 这个应该是从文件中读取的!
uint8_t uniq_id[32];
uint8_t buf[1024];
const uint8_t *cp = buf;
uint8_t *p = buf;
size_t len = 0;
if (argc < 2) {
print_usage(prog);
return 0;
}
argc--;
argv++;
while (argc >= 1) {
if (!strcmp(*argv, "-help")) {
print_usage(prog);
return 0;
} 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, "-keyfile")) {
if (--argc < 1) goto bad;
keyfile = *(++argv);
} else if (!strcmp(*argv, "-days")) {
if (--argc < 1) goto bad;
days = atoi(*(++argv));
} else {
print_usage(prog);
return 0;
break;
}
argc--;
argv++;
}
if (days <= 0 && !keyfile) {
goto bad;
}
if (!(keyfp = fopen(keyfile, "r"))) {
goto bad;
}
pass = getpass("Password : ");
if (sm2_enced_private_key_info_from_pem(&sm2_key, pass, keyfp) != 1) {
error_print();
goto end;
}
rand_bytes(serial, sizeof(serial));
memset(&name, 0, sizeof(name));
if (country) {
if (x509_name_set_country(&name, country) != 1) {
error_print();
goto end;
}
}
if (state) {
if (x509_name_set_state_or_province(&name, state) != 1) {
error_print();
goto end;
}
}
if (org) {
if (x509_name_set_organization(&name, org) != 1) {
error_print();
goto end;
}
}
if (org_unit) {
if (x509_name_set_organizational_unit(&name, org_unit) != 1) {
error_print();
goto end;
}
}
if (!common_name) {
error_print();
goto end;
} else {
if (x509_name_set_common_name(&name, common_name) != 1) {
error_print();
goto end;
}
}
time(&not_before);
memset(&cert, 0, sizeof(cert));
x509_certificate_set_version(&cert, X509_version_v3);
x509_certificate_set_serial_number(&cert, serial, sizeof(serial));
x509_certificate_set_signature_algor(&cert, OID_sm2sign_with_sm3);
x509_certificate_set_issuer(&cert, &name);
x509_certificate_set_subject(&cert, &name);
x509_certificate_set_validity(&cert, not_before, days);
x509_certificate_set_subject_public_key_info_sm2(&cert, &sm2_key);
x509_certificate_set_issuer_unique_id(&cert, uniq_id, sizeof(uniq_id));
x509_certificate_set_subject_unique_id(&cert, uniq_id, sizeof(uniq_id));
x509_certificate_sign_sm2(&cert, &sm2_key);
x509_certificate_to_pem(&cert, stdout);
ret = 0;
goto end;
bad:
fprintf(stderr, "%s: commands should not be used together\n", prog);
end:
return ret;
}

349
tools/sdfutil.c Normal file
View File

@@ -0,0 +1,349 @@
/*
* Copyright (c) 2014 - 2021 The GmSSL Project. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. All advertising materials mentioning features or use of this
* software must display the following acknowledgment:
* "This product includes software developed by the GmSSL Project.
* (http://gmssl.org/)"
*
* 4. The name "GmSSL Project" must not be used to endorse or promote
* products derived from this software without prior written
* permission. For written permission, please contact
* guanzhi1980@gmail.com.
*
* 5. Products derived from this software may not be called "GmSSL"
* nor may "GmSSL" appear in their names without prior written
* permission of the GmSSL Project.
*
* 6. Redistributions of any form whatsoever must retain the following
* acknowledgment:
* "This product includes software developed by the GmSSL Project
* (http://gmssl.org/)"
*
* THIS SOFTWARE IS PROVIDED BY THE GmSSL PROJECT ``AS IS'' AND ANY
* EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE GmSSL PROJECT OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/
# include <stdio.h>
# include <stdlib.h>
# include <string.h>
# include <openssl/bio.h>
# include <openssl/err.h>
# include <openssl/evp.h>
# include <openssl/pem.h>
# include <openssl/gmsdf.h>
# include <openssl/gmapi.h>
# include "apps.h"
# define OP_NONE 0
# define OP_PRINTDEVINFO 1
# define OP_PRINTSM2SIGN 2
# define OP_PRINTSM2ENC 3
# define OP_PRINTRSASIGN 4
# define OP_PRINTRSAENC 5
# define OP_ACCESSKEY 6
# define OP_IMPORTOBJ 7
# define OP_EXPORTOBJ 8
# define OP_DELOBJ 9
OPTIONS sdf_options[] = {
{"help", OPT_HELP, '-', "Display this summary"},
{"lib", OPT_LIB, 's', "Vendor's SDF dynamic library"},
{"vendor", OPT_VENDOR, 's', "Vendor name"},
{"printdevinfo", OPT_PRINTDEVINFO, '-', "Print device information"},
{"printsm2sign", OPT_PRINTSM2SIGN, 's', "Print SM2 signing key with key index"},
{"printsm2enc", OPT_PRINTSM2ENC, 's', "Print SM2 encryption key with key index"},
{"printrsasign", OPT_PRINTRSASIGN, 's', "Print RSA signing key with key index"},
{"printrsaenc", OPT_PRINTRSAENC, 's', "Print RSA encryption key with key index"},
{"accesskey", OPT_ACCESSKEY, 's', "Access private key with the key index number"},
{"pass", OPT_PASS, 's', "Passphrase source for accessing private key"},
{"importobj", OPT_IMPORTOBJ, 's', "Import data object into device"},
{"exportobj", OPT_EXPORTOBJ, 's', "Export data object from device"},
{"delobj", OPT_DELOBJ, 's', "Delete data object from device"},
{"in", OPT_IN, '<', "File to be imported from"},
{"out", OPT_OUT, '>', "File to be exported to"},
{NULL}
};
int sdf_main(int argc, char **argv)
{
int ret = 1;
char *infile = NULL, *outfile = NULL, *prog;
char *objname = NULL, *passarg = NULL, *pass = NULL;
BIO *in = NULL, *out = NULL;
char *lib = NULL, *vendor = NULL;
unsigned char *buf = NULL;
unsigned int ulen;
int len, key_idx = -1;
OPTION_CHOICE o;
int op = OP_NONE;
void *hDev = NULL;
void *hSession = NULL;
prog = opt_init(argc, argv, sdf_options);
while ((o = opt_next()) != OPT_EOF) {
switch (o) {
case OPT_EOF:
case OPT_ERR:
opthelp:
BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
goto end;
case OPT_HELP:
opt_help(sdf_options);
ret = 0;
goto end;
case OPT_LIB:
lib = opt_arg();
break;
case OPT_VENDOR:
vendor = opt_arg();
break;
case OPT_PRINTDEVINFO:
if (op)
goto opthelp;
op = OP_PRINTDEVINFO;
break;
case OPT_PRINTSM2SIGN:
if (op)
goto opthelp;
op = OP_PRINTSM2SIGN;
key_idx = atoi(opt_arg());
break;
case OPT_PRINTSM2ENC:
if (op)
goto opthelp;
op = OP_PRINTSM2ENC;
key_idx = atoi(opt_arg());
break;
case OPT_PRINTRSASIGN:
if (op)
goto opthelp;
op = OP_PRINTRSASIGN;
key_idx = atoi(opt_arg());
break;
case OPT_PRINTRSAENC:
if (op)
goto opthelp;
op = OP_PRINTRSAENC;
key_idx = atoi(opt_arg());
break;
case OPT_ACCESSKEY:
key_idx = atoi(opt_arg());
break;
case OPT_PASS:
passarg = opt_arg();
break;
case OPT_IMPORTOBJ:
if (op)
goto opthelp;
op = OP_IMPORTOBJ;
objname = opt_arg();
break;
case OPT_EXPORTOBJ:
if (op)
goto opthelp;
op = OP_EXPORTOBJ;
objname = opt_arg();
break;
case OPT_DELOBJ:
if (op)
goto opthelp;
op = OP_DELOBJ;
objname = opt_arg();
break;
case OPT_IN:
infile = opt_arg();
break;
case OPT_OUT:
outfile = opt_arg();
break;
}
}
argc = opt_num_rest();
if (argc != 0)
goto opthelp;
if (!lib) {
BIO_printf(bio_err, "Option '-lib' required\n");
x509 goto opthelp;
}
if (SDF_LoadLibrary(lib, vendor) != SDR_OK) {
ERR_print_errors(bio_err);
goto end;
}
if (op == OP_NONE) {
ret = 0;
goto end;
}
if (SDF_OpenDevice(&hDev) != SDR_OK
|| SDF_OpenSession(hDev, &hSession) != SDR_OK) {
ERR_print_errors(bio_err);
goto end;
}
switch (op) {
case OP_PRINTDEVINFO:
case OP_PRINTSM2SIGN:
case OP_PRINTSM2ENC:
case OP_PRINTRSASIGN:
case OP_PRINTRSAENC:
if (!(out = bio_open_default(outfile, 'w', FORMAT_TEXT))) {
goto opthelp;
}
break;
}
switch (op) {
case OP_PRINTSM2SIGN:
case OP_PRINTSM2ENC:
case OP_PRINTRSASIGN:
case OP_PRINTRSAENC:
case OP_ACCESSKEY:
if (key_idx < SDF_MIN_KEY_INDEX || key_idx > SDF_MAX_KEY_INDEX) {
BIO_printf(bio_err, "Invalid key index\n");
goto end;
}
break;
}
if (op == OP_PRINTDEVINFO) {
DEVICEINFO devInfo;
if (SDF_GetDeviceInfo(hSession, &devInfo) != SDR_OK
|| SDF_PrintDeviceInfo(out, &devInfo) != SDR_OK) {
ERR_print_errors(bio_err);
goto end;
}
} else if (op == OP_PRINTSM2SIGN || op == OP_PRINTSM2ENC) {
ECCrefPublicKey publicKey;
if (op == OP_PRINTSM2SIGN) {
if (SDF_ExportSignPublicKey_ECC(hSession,
key_idx, &publicKey) != SDR_OK) {
ERR_print_errors(bio_err);
goto end;
}
BIO_puts(out, "SM2 Signing Public Key:\n");
} else {
if (SDF_ExportEncPublicKey_ECC(hSession,
key_idx, &publicKey) != SDR_OK) {
ERR_print_errors(bio_err);
goto end;
}
BIO_puts(out, "SM2 Encryption Public Key:\n");
}
if (SDF_PrintECCPublicKey(out, &publicKey) != SDR_OK) {
ERR_print_errors(bio_err);
goto end;
}
} else if (op == OP_PRINTRSASIGN || op == OP_PRINTRSAENC) {
RSArefPublicKey publicKey;
if (op == OP_PRINTRSASIGN) {
if (SDF_ExportSignPublicKey_RSA(hSession,
key_idx, &publicKey) != SDR_OK) {
ERR_print_errors(bio_err);
goto end;
}
BIO_puts(out, "RSA Signing Public Key:\n");
} else {
if (SDF_ExportEncPublicKey_RSA(hSession,
key_idx, &publicKey) != SDR_OK) {
ERR_print_errors(bio_err);
goto end;
}
BIO_puts(out, "RSA Encryption Public Key:\n");
}
if (SDF_PrintRSAPublicKey(out, &publicKey) != SDR_OK) {
ERR_print_errors(bio_err);
goto end;
}
} else if (op == OP_ACCESSKEY) {
if (!app_passwd(passarg, NULL, &pass, NULL)) {
BIO_printf(bio_err, "Error getting password\n");
goto end;
}
if (SDF_GetPrivateKeyAccessRight(hSession, (unsigned int)key_idx,
(unsigned char *)pass, strlen(pass)) != SDR_OK) {
OPENSSL_cleanse(pass, sizeof(pass));
return 0;
}
(void)SDF_ReleasePrivateKeyAccessRight(hSession, (unsigned int)key_idx);
BIO_printf(bio_err, "Access private key %d success\n", key_idx);
} else if (op == OP_IMPORTOBJ) {
if (!(in = bio_open_default(infile, 'r', FORMAT_BINARY))) {
goto opthelp;
}
if ((len = bio_to_mem(&buf, SDF_MAX_FILE_SIZE, in)) < 0) {
BIO_printf(bio_err, "Error reading data object content\n");
goto end;
}
if (SDF_CreateFile(hSession, (unsigned char *)objname, strlen(objname), len) != SDR_OK
|| SDF_WriteFile(hSession, (unsigned char *)objname, strlen(objname), 0, len, buf) != SDR_OK) {
ERR_print_errors(bio_err);
goto end;
}
BIO_printf(bio_err, "Object '%s' (%d bytes) created\n", objname, len);
} else if (op == OP_EXPORTOBJ) {
if (!(out = bio_open_default(outfile, 'w', FORMAT_BINARY))) {
goto opthelp;
}
if (!(buf = OPENSSL_zalloc(SDF_MAX_FILE_SIZE))
|| SDF_ReadFile(hSession, (unsigned char *)objname, strlen(objname), 0, &ulen, buf) != SDR_OK
|| BIO_write(out, buf, ulen) != ulen) {
ERR_print_errors(bio_err);
goto end;
}
BIO_printf(bio_err, "Object '%s' (%u bytes) exported\n", objname, ulen);
} else if (op == OP_DELOBJ) {
if (SDF_DeleteFile(hSession, (unsigned char *)objname, strlen(objname)) != SDR_OK) {
ERR_print_errors(bio_err);
goto end;
}
BIO_printf(bio_err, "Object '%s' deleted\n", objname);
} else {
goto end;
}
ret = 0;
end:
BIO_free(in);
BIO_free(out);
OPENSSL_free(buf);
OPENSSL_free(pass);
if (hSession) (void)SDF_CloseSession(hSession);
if (hDev) (void)SDF_CloseDevice(hDev);
if (lib) SDF_UnloadLibrary();
return ret;
}

1484
tools/skfutil.c Normal file

File diff suppressed because it is too large Load Diff

123
tools/sm2decrypt.c Normal file
View File

@@ -0,0 +1,123 @@
/*
* Copyright (c) 2021 - 2021 The GmSSL Project. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. All advertising materials mentioning features or use of this
* software must display the following acknowledgment:
* "This product includes software developed by the GmSSL Project.
* (http://gmssl.org/)"
*
* 4. The name "GmSSL Project" must not be used to endorse or promote
* products derived from this software without prior written
* permission. For written permission, please contact
* guanzhi1980@gmail.com.
*
* 5. Products derived from this software may not be called "GmSSL"
* nor may "GmSSL" appear in their names without prior written
* permission of the GmSSL Project.
*
* 6. Redistributions of any form whatsoever must retain the following
* acknowledgment:
* "This product includes software developed by the GmSSL Project
* (http://gmssl.org/)"
*
* THIS SOFTWARE IS PROVIDED BY THE GmSSL PROJECT ``AS IS'' AND ANY
* EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE GmSSL PROJECT OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <gmssl/hex.h>
#include <gmssl/sm2.h>
#include <gmssl/pem.h>
#include <gmssl/pkcs8.h>
#include <gmssl/error.h>
int main(int argc, char **argv)
{
char *prog = basename(argv[0]);
const char *keyfile = NULL;
FILE *keyfp = NULL;
const char *pass = NULL;
SM2_KEY key;
char hexbuf[SM2_MAX_CIPHERTEXT_SIZE * 2];
uint8_t inbuf[SM2_MAX_CIPHERTEXT_SIZE * 2];
uint8_t outbuf[SM2_MAX_CIPHERTEXT_SIZE];
size_t hexlen, inlen, outlen;
if (argc < 2) {
bad:
fprintf(stderr, "%s : error options\n", prog);
help:
fprintf(stderr, "usage: %s -key key.pem [-id str] < file\n", prog);
return 1;
}
argc--;
argv++;
while (argc > 1) {
if (!strcmp(*argv, "-help")) {
goto help;
} else if (!strcmp(*argv, "-key")) {
if (--argc < 1) goto bad;
keyfile = *(++argv);
} else {
goto help;
}
argc--;
argv++;
}
if (!keyfile) {
error_print();
return -1;
}
if (!(keyfp = fopen(keyfile, "r"))) {
error_print();
return -1;
}
pass = getpass("Encryption Password : ");
if (sm2_enced_private_key_info_from_pem(&key, pass, keyfp) != 1) {
error_print("private key decryption failure");
return -1;
}
if ((hexlen = fread(hexbuf, 1, sizeof(hexbuf), stdin)) <= 0) {
error_print();
return -1;
}
if (hex2bin(hexbuf, hexlen, inbuf) != 1) {
error_print();
return -1;
}
if (sm2_decrypt(&key, inbuf, hexlen/2, outbuf, &outlen) != 1) {
error_print();
return -1;
}
fwrite(outbuf, 1, outlen, stdout);
return 0;
}

143
tools/sm2encrypt.c Normal file
View File

@@ -0,0 +1,143 @@
/*
* Copyright (c) 2021 - 2021 The GmSSL Project. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. All advertising materials mentioning features or use of this
* software must display the following acknowledgment:
* "This product includes software developed by the GmSSL Project.
* (http://gmssl.org/)"
*
* 4. The name "GmSSL Project" must not be used to endorse or promote
* products derived from this software without prior written
* permission. For written permission, please contact
* guanzhi1980@gmail.com.
*
* 5. Products derived from this software may not be called "GmSSL"
* nor may "GmSSL" appear in their names without prior written
* permission of the GmSSL Project.
*
* 6. Redistributions of any form whatsoever must retain the following
* acknowledgment:
* "This product includes software developed by the GmSSL Project
* (http://gmssl.org/)"
*
* THIS SOFTWARE IS PROVIDED BY THE GmSSL PROJECT ``AS IS'' AND ANY
* EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE GmSSL PROJECT OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <gmssl/hex.h>
#include <gmssl/sm2.h>
#include <gmssl/pem.h>
#include <gmssl/pkcs8.h>
#include <gmssl/x509.h>
#include <gmssl/error.h>
int main(int argc, char **argv)
{
int ret;
char *prog = basename(argv[0]);
const char *keyfile = NULL;
const char *certfile = NULL;
FILE *keyfp = NULL;
FILE *certfp = NULL;
X509_CERTIFICATE cert;
SM2_KEY key;
uint8_t inbuf[SM2_MAX_PLAINTEXT_SIZE];
ssize_t inlen;
uint8_t outbuf[SM2_MAX_CIPHERTEXT_SIZE];
size_t outlen = sizeof(outbuf);
if (argc < 2) {
bad:
fprintf(stderr, "%s : error options\n", prog);
help:
fprintf(stderr, "usage:\n");
fprintf(stderr, " %s -pubkey key.pem < file\n", prog);
fprintf(stderr, " %s -cert cert.pem < file\n", prog);
fprintf(stderr, "\n");
return 1;
}
argc--;
argv++;
while (argc > 1) {
if (!strcmp(*argv, "-help")) {
goto help;
} else if (!strcmp(*argv, "-pubkey")) {
if (--argc < 1) goto bad;
keyfile = *(++argv);
} else if (!strcmp(*argv, "-cert")) {
if (--argc < 1) goto bad;
certfile = *(++argv);
} else {
goto help;
}
argc--;
argv++;
}
if ((!keyfile && !certfile) || (keyfile && certfile)) {
error_print();
return -1;
}
if (keyfile) {
if (!(keyfp = fopen(keyfile, "r"))) {
error_print();
return -1;
}
if (sm2_public_key_info_from_pem(&key, keyfp) != 1) {
error_print();
return -1;
}
} else {
if (!(certfp = fopen(certfile, "r"))) {
error_print();
return -1;
}
if (x509_certificate_from_pem(&cert, certfp) != 1) {
error_print();
return -1;
}
if (x509_certificate_get_public_key(&cert, &key) != 1) {
error_print();
return -1;
}
}
if ((inlen = read(STDIN_FILENO, inbuf, sizeof(inbuf))) <= 0) {
error_print();
return -1;
}
if (sm2_encrypt(&key, inbuf, inlen, outbuf, &outlen) != 1) {
error_print();
return -1;
}
format_bytes(stdout, 0, 0, "", outbuf, outlen);
return 1;
}

70
tools/sm2gen.c Normal file
View File

@@ -0,0 +1,70 @@
/*
* Copyright (c) 2021 - 2021 The GmSSL Project. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. All advertising materials mentioning features or use of this
* software must display the following acknowledgment:
* "This product includes software developed by the GmSSL Project.
* (http://gmssl.org/)"
*
* 4. The name "GmSSL Project" must not be used to endorse or promote
* products derived from this software without prior written
* permission. For written permission, please contact
* guanzhi1980@gmail.com.
*
* 5. Products derived from this software may not be called "GmSSL"
* nor may "GmSSL" appear in their names without prior written
* permission of the GmSSL Project.
*
* 6. Redistributions of any form whatsoever must retain the following
* acknowledgment:
* "This product includes software developed by the GmSSL Project
* (http://gmssl.org/)"
*
* THIS SOFTWARE IS PROVIDED BY THE GmSSL PROJECT ``AS IS'' AND ANY
* EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE GmSSL PROJECT OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <gmssl/sm2.h>
#include <gmssl/pem.h>
#include <gmssl/error.h>
int main(void)
{
SM2_KEY key;
if (sm2_keygen(&key) != 1) {
error_print();
return -1;
}
if (sm2_private_key_to_pem(&key, stdout) != 1) {
error_print();
return -1;
}
return 0;
}

79
tools/sm2pub.c Normal file
View File

@@ -0,0 +1,79 @@
/*
* Copyright (c) 2021 - 2021 The GmSSL Project. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. All advertising materials mentioning features or use of this
* software must display the following acknowledgment:
* "This product includes software developed by the GmSSL Project.
* (http://gmssl.org/)"
*
* 4. The name "GmSSL Project" must not be used to endorse or promote
* products derived from this software without prior written
* permission. For written permission, please contact
* guanzhi1980@gmail.com.
*
* 5. Products derived from this software may not be called "GmSSL"
* nor may "GmSSL" appear in their names without prior written
* permission of the GmSSL Project.
*
* 6. Redistributions of any form whatsoever must retain the following
* acknowledgment:
* "This product includes software developed by the GmSSL Project
* (http://gmssl.org/)"
*
* THIS SOFTWARE IS PROVIDED BY THE GmSSL PROJECT ``AS IS'' AND ANY
* EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE GmSSL PROJECT OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <gmssl/sm2.h>
#include <gmssl/pem.h>
#include <gmssl/pkcs8.h>
#include <gmssl/error.h>
// 输入一个PKCS #8 加密格式的私钥输出一个EC公钥
int main(void)
{
SM2_KEY key;
char *pass = NULL;
char passbuf[64] = {0};
pass = getpass("Encryption Password : ");
strncpy(passbuf, pass, sizeof(passbuf));
pass = getpass("Encryption Password (Again) : ");
if (strcmp(passbuf, pass) != 0) {
fprintf(stderr, "error: passwords not match\n");
return -1;
}
sm2_keygen(&key);
sm2_enced_private_key_info_to_pem(&key, pass, stdout);
return 0;
}

126
tools/sm2sign.c Normal file
View File

@@ -0,0 +1,126 @@
/*
* Copyright (c) 2021 - 2021 The GmSSL Project. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. All advertising materials mentioning features or use of this
* software must display the following acknowledgment:
* "This product includes software developed by the GmSSL Project.
* (http://gmssl.org/)"
*
* 4. The name "GmSSL Project" must not be used to endorse or promote
* products derived from this software without prior written
* permission. For written permission, please contact
* guanzhi1980@gmail.com.
*
* 5. Products derived from this software may not be called "GmSSL"
* nor may "GmSSL" appear in their names without prior written
* permission of the GmSSL Project.
*
* 6. Redistributions of any form whatsoever must retain the following
* acknowledgment:
* "This product includes software developed by the GmSSL Project
* (http://gmssl.org/)"
*
* THIS SOFTWARE IS PROVIDED BY THE GmSSL PROJECT ``AS IS'' AND ANY
* EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE GmSSL PROJECT OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <gmssl/sm2.h>
#include <gmssl/pem.h>
#include <gmssl/pkcs8.h>
#include <gmssl/error.h>
// echo data | sm2sign -id "Alice" -keyfile sm2.pem
// echo data | sm2verify -id "Alice" -keyfile sm2pub.pem -certfile a -cacertfile b
int main(int argc, char **argv)
{
char *prog = basename(argv[0]);
const char *id = SM2_DEFAULT_ID;
const char *keyfile = NULL;
FILE *keyfp = NULL;
const char *pass = NULL;
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;
if (argc < 2) {
bad:
fprintf(stderr, "%s : error options\n", prog);
help:
fprintf(stderr, "usage: %s -key key.pem [-id str] < file\n", prog);
return 1;
}
argc--;
argv++;
while (argc > 1) {
if (!strcmp(*argv, "-help")) {
goto help;
} else if (!strcmp(*argv, "-id")) {
if (--argc < 1) goto bad;
id = *(++argv);
} else if (!strcmp(*argv, "-key")) {
if (--argc < 1) goto bad;
keyfile = *(++argv);
} else {
goto help;
}
argc--;
argv++;
}
if (!keyfile) {
error_print();
return -1;
}
if (!(keyfp = fopen(keyfile, "r"))) {
error_print();
return -1;
}
pass = getpass("Encryption Password : ");
if (sm2_enced_private_key_info_from_pem(&key, pass, keyfp) != 1) {
error_print("private key decryption failure");
return -1;
}
sm2_sign_init(&sign_ctx, &key, id);
while ((len = read(STDIN_FILENO, buf, sizeof(buf))) > 0) {
sm2_sign_update(&sign_ctx, buf, len);
}
sm2_sign_finish(&sign_ctx, sig, &siglen);
format_bytes(stdout, 0, 0, "", sig, siglen);
return 0;
}

164
tools/sm2verify.c Normal file
View File

@@ -0,0 +1,164 @@
/*
* Copyright (c) 2021 - 2021 The GmSSL Project. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. All advertising materials mentioning features or use of this
* software must display the following acknowledgment:
* "This product includes software developed by the GmSSL Project.
* (http://gmssl.org/)"
*
* 4. The name "GmSSL Project" must not be used to endorse or promote
* products derived from this software without prior written
* permission. For written permission, please contact
* guanzhi1980@gmail.com.
*
* 5. Products derived from this software may not be called "GmSSL"
* nor may "GmSSL" appear in their names without prior written
* permission of the GmSSL Project.
*
* 6. Redistributions of any form whatsoever must retain the following
* acknowledgment:
* "This product includes software developed by the GmSSL Project
* (http://gmssl.org/)"
*
* THIS SOFTWARE IS PROVIDED BY THE GmSSL PROJECT ``AS IS'' AND ANY
* EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE GmSSL PROJECT OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <gmssl/hex.h>
#include <gmssl/sm2.h>
#include <gmssl/pem.h>
#include <gmssl/pkcs8.h>
#include <gmssl/x509.h>
#include <gmssl/error.h>
int main(int argc, char **argv)
{
int ret;
char *prog = basename(argv[0]);
const char *sig_hex = NULL;
const char *id = SM2_DEFAULT_ID;
const char *keyfile = NULL;
const char *certfile = NULL;
FILE *keyfp = NULL;
FILE *certfp = NULL;
X509_CERTIFICATE cert;
SM2_KEY key;
SM2_SIGN_CTX verify_ctx;
uint8_t buf[4096];
ssize_t len;
uint8_t sig[SM2_MAX_SIGNATURE_SIZE];
size_t siglen;
if (argc < 2) {
bad:
fprintf(stderr, "%s : error options\n", prog);
help:
fprintf(stderr, "usage:\n");
fprintf(stderr, " %s -sig hex -pubkey key.pem [-id str] < file\n", prog);
fprintf(stderr, " %s -sig hex -cert cert.pem [-id str] < file\n", prog);
fprintf(stderr, "\n");
return 1;
}
argc--;
argv++;
while (argc > 1) {
if (!strcmp(*argv, "-help")) {
goto help;
} else if (!strcmp(*argv, "-id")) {
if (--argc < 1) goto bad;
id = *(++argv);
} else if (!strcmp(*argv, "-pubkey")) {
if (--argc < 1) goto bad;
keyfile = *(++argv);
} else if (!strcmp(*argv, "-sig")) {
if (--argc < 1) goto bad;
sig_hex = *(++argv);
} else if (!strcmp(*argv, "-cert")) {
if (--argc < 1) goto bad;
certfile = *(++argv);
} else {
goto help;
}
argc--;
argv++;
}
if (!sig_hex || (!keyfile && !certfile) || (keyfile && certfile)) {
error_print();
return -1;
}
if (strlen(sig_hex) > SM2_MAX_SIGNATURE_SIZE * 2 || strlen(sig_hex) % 2) {
error_print();
return -1;
}
if (hex2bin(sig_hex, strlen(sig_hex), sig) != 1) {
error_print();
return -1;
}
siglen = strlen(sig_hex)/2;
if (keyfile) {
if (!(keyfp = fopen(keyfile, "r"))) {
error_print();
return -1;
}
if (sm2_public_key_info_from_pem(&key, keyfp) != 1) {
error_print();
return -1;
}
} else {
if (!(certfp = fopen(certfile, "r"))) {
error_print();
return -1;
}
if (x509_certificate_from_pem(&cert, certfp) != 1) {
error_print();
return -1;
}
if (x509_certificate_get_public_key(&cert, &key) != 1) {
error_print();
return -1;
}
}
sm2_verify_init(&verify_ctx, &key, id);
while ((len = read(STDIN_FILENO, buf, sizeof(buf))) > 0) {
sm2_verify_update(&verify_ctx, buf, len);
}
if ((ret = sm2_verify_finish(&verify_ctx, sig, siglen)) < 0) {
error_print();
return -1;
}
fprintf(stdout, "verify : %s\n", ret == 1 ? "success" : "failure");
return ret == 1 ? 0 : -1;
return 1;
}

67
tools/sm2view.c Normal file
View File

@@ -0,0 +1,67 @@
/*
* Copyright (c) 2020 - 2021 The GmSSL Project. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. All advertising materials mentioning features or use of this
* software must display the following acknowledgment:
* "This product includes software developed by the GmSSL Project.
* (http://gmssl.org/)"
*
* 4. The name "GmSSL Project" must not be used to endorse or promote
* products derived from this software without prior written
* permission. For written permission, please contact
* guanzhi1980@gmail.com.
*
* 5. Products derived from this software may not be called "GmSSL"
* nor may "GmSSL" appear in their names without prior written
* permission of the GmSSL Project.
*
* 6. Redistributions of any form whatsoever must retain the following
* acknowledgment:
* "This product includes software developed by the GmSSL Project
* (http://gmssl.org/)"
*
* THIS SOFTWARE IS PROVIDED BY THE GmSSL PROJECT ``AS IS'' AND ANY
* EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE GmSSL PROJECT OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <gmssl/sm2.h>
#include <gmssl/pem.h>
#include <gmssl/error.h>
int main(void)
{
SM2_KEY key;
if (sm2_private_key_from_pem(&key, stdin) != 1) {
error_print();
return -1;
}
sm2_key_print(stdout, &key, 0, 0);
sm2_public_key_info_to_pem(&key, stdout);
return 0;
}

83
tools/sm3sum.c Normal file
View File

@@ -0,0 +1,83 @@
/*
* Copyright (c) 2014 - 2021 The GmSSL Project. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. All advertising materials mentioning features or use of this
* software must display the following acknowledgment:
* "This product includes software developed by the GmSSL Project.
* (http://gmssl.org/)"
*
* 4. The name "GmSSL Project" must not be used to endorse or promote
* products derived from this software without prior written
* permission. For written permission, please contact
* guanzhi1980@gmail.com.
*
* 5. Products derived from this software may not be called "GmSSL"
* nor may "GmSSL" appear in their names without prior written
* permission of the GmSSL Project.
*
* 6. Redistributions of any form whatsoever must retain the following
* acknowledgment:
* "This product includes software developed by the GmSSL Project
* (http://gmssl.org/)"
*
* THIS SOFTWARE IS PROVIDED BY THE GmSSL PROJECT ``AS IS'' AND ANY
* EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE GmSSL PROJECT OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <libgen.h>
#include <gmssl/sm3.h>
int main(int argc, char **argv)
{
char *prog = basename(argv[0]);
SM3_CTX ctx;
uint8_t dgst[32];
uint8_t buf[4096];
ssize_t len;
int i;
if (argc > 1) {
fprintf(stderr, "usage: echo -n \"abc\" | %s\n", prog);
fprintf(stderr, " %s < path/to/file\n", prog);
return 0;
}
sm3_init(&ctx);
while ((len = read(STDIN_FILENO, buf, sizeof(buf))) > 0) {
sm3_update(&ctx, buf, len);
}
sm3_finish(&ctx, dgst);
for (i = 0; i < sizeof(dgst); i++) {
printf("%02x", dgst[i]);
}
printf("\n");
return 0;
}

86
tools/sm4speed.c Normal file
View File

@@ -0,0 +1,86 @@
/* ====================================================================
* Copyright (c) 2014 - 2017 The GmSSL Project. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. All advertising materials mentioning features or use of this
* software must display the following acknowledgment:
* "This product includes software developed by the GmSSL Project.
* (http://gmssl.org/)"
*
* 4. The name "GmSSL Project" must not be used to endorse or promote
* products derived from this software without prior written
* permission. For written permission, please contact
* guanzhi1980@gmail.com.
*
* 5. Products derived from this software may not be called "GmSSL"
* nor may "GmSSL" appear in their names without prior written
* permission of the GmSSL Project.
*
* 6. Redistributions of any form whatsoever must retain the following
* acknowledgment:
* "This product includes software developed by the GmSSL Project
* (http://gmssl.org/)"
*
* THIS SOFTWARE IS PROVIDED BY THE GmSSL PROJECT ``AS IS'' AND ANY
* EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE GmSSL PROJECT OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
* ====================================================================
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <libgen.h>
#include <openmp.h>
#include <gmssl/sm4.h>
int main(int argc, char **argv)
{
SM4_KEY sm4_key;
unsigned char user_key[16] = {
0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88,
0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88,
};
size_t buflen = SM4_BLOCK_SIZE * 8 * 3 * 1000 * 1000;
unsigned char *buf = NULL;
unsigned char *p;
int i;
if (!(buf = (unsigned char *)malloc(buflen))) {
fprintf(stderr, "malloc failed\n");
return -1;
}
sm4_set_encrypt_key(&sm4_key, user_key);
#pragma omp parallel for
for (i = 0, p = buf; i < buflen/(SM4_BLOCK_SIZE * 16); i++, p += SM4_BLOCK_SIZE * 16) {
sm4_encrypt_16blocks(&sms4_key, p, p);
}
return 0;
}

196
tools/tlcp_client.c Normal file
View File

@@ -0,0 +1,196 @@
/*
* Copyright (c) 2021 - 2021 The GmSSL Project. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. All advertising materials mentioning features or use of this
* software must display the following acknowledgment:
* "This product includes software developed by the GmSSL Project.
* (http://gmssl.org/)"
*
* 4. The name "GmSSL Project" must not be used to endorse or promote
* products derived from this software without prior written
* permission. For written permission, please contact
* guanzhi1980@gmail.com.
*
* 5. Products derived from this software may not be called "GmSSL"
* nor may "GmSSL" appear in their names without prior written
* permission of the GmSSL Project.
*
* 6. Redistributions of any form whatsoever must retain the following
* acknowledgment:
* "This product includes software developed by the GmSSL Project
* (http://gmssl.org/)"
*
* THIS SOFTWARE IS PROVIDED BY THE GmSSL PROJECT ``AS IS'' AND ANY
* EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE GmSSL PROJECT OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <libgen.h>
#include <unistd.h>
#include <gmssl/tls.h>
#include <gmssl/error.h>
const char *http_get =
"GET / HTTP/1.1\r\n"
"Hostname: aaa\r\n"
"\r\n\r\n";
void print_usage(const char *prog)
{
printf("Usage: %s [options]\n", prog);
printf(" -host <str>\n");
printf(" -port <num>\n");
printf(" -cacerts <file>\n");
printf(" -cert <file>\n");
printf(" -key <file>\n");
}
int main(int argc , char *argv[])
{
int ret = -1;
char *prog = basename(argv[0]);
char *host = NULL;
int port = 443;
TLS_CONNECT conn;
char buf[100] = {0};
size_t len = sizeof(buf);
char *cacertsfile = NULL;
char *certfile = NULL;
char *keyfile = NULL;
FILE *cacertsfp = NULL;
FILE *certfp = NULL;
FILE *keyfp = NULL;
SM2_KEY sign_key;
if (argc < 2) {
print_usage(prog);
return 0;
}
argc--;
argv++;
while (argc >= 1) {
if (!strcmp(*argv, "-help")) {
print_usage(prog);
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, "-cacerts")) {
if (--argc < 1) goto bad;
cacertsfile = *(++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 {
print_usage(prog);
return 0;
}
argc--;
argv++;
}
if (!host || !certfile || !keyfile) {
print_usage(prog);
return -1;
}
if (cacertsfile) {
if (!(cacertsfp = fopen(cacertsfile, "r"))) {
error_print();
return -1;
}
}
if (certfile) {
if (!(certfp = fopen(certfile, "r"))) {
error_print();
return -1;
}
}
if (keyfile) {
if (!(keyfp = fopen(keyfile, "r"))) {
error_print();
return -1;
}
if (sm2_private_key_from_pem(&sign_key, keyfp) != 1) {
error_print();
return -1;
}
}
memset(&conn, 0, sizeof(conn));
if (tlcp_connect(&conn, host, port, cacertsfp, certfp, &sign_key) != 1) {
error_print();
return -1;
}
// 这个client 发收了一个消息就结束了
if (tls_send(&conn, (uint8_t *)"12345\n", 6) != 1) {
error_print();
return -1;
}
for (;;) {
memset(buf, 0, sizeof(buf));
len = sizeof(buf);
if (tls_recv(&conn, (uint8_t *)buf, &len) != 1) {
error_print();
return -1;
}
if (len > 0) {
printf("%s\n", buf);
break;
}
}
return 1;
bad:
fprintf(stderr, "%s: command error\n", prog);
return 0;
}

194
tools/tlcp_server.c Normal file
View File

@@ -0,0 +1,194 @@
/*
* Copyright (c) 2021 - 2021 The GmSSL Project. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. All advertising materials mentioning features or use of this
* software must display the following acknowledgment:
* "This product includes software developed by the GmSSL Project.
* (http://gmssl.org/)"
*
* 4. The name "GmSSL Project" must not be used to endorse or promote
* products derived from this software without prior written
* permission. For written permission, please contact
* guanzhi1980@gmail.com.
*
* 5. Products derived from this software may not be called "GmSSL"
* nor may "GmSSL" appear in their names without prior written
* permission of the GmSSL Project.
*
* 6. Redistributions of any form whatsoever must retain the following
* acknowledgment:
* "This product includes software developed by the GmSSL Project
* (http://gmssl.org/)"
*
* THIS SOFTWARE IS PROVIDED BY THE GmSSL PROJECT ``AS IS'' AND ANY
* EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE GmSSL PROJECT OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <libgen.h>
#include <unistd.h>
#include <gmssl/sm2.h>
#include <gmssl/tls.h>
#include <gmssl/error.h>
void print_usage(const char *prog)
{
printf("Usage: %s [options]\n", prog);
printf(" -port <num>\n");
printf(" -cert <file>\n");
printf(" -signkey <file>\n");
printf(" -enckey <file>\n");
}
int main(int argc , char *argv[])
{
int ret = -1;
char *prog = basename(argv[0]);
int port = 443;
char *certfile = NULL;
char *signkeyfile = NULL;
char *enckeyfile = NULL;
FILE *certfp = NULL;
FILE *signkeyfp = NULL;
FILE *enckeyfp = NULL;
SM2_KEY signkey;
SM2_KEY enckey;
uint8_t verify_buf[4096];
TLS_CONNECT conn;
char buf[1600] = {0};
size_t len = sizeof(buf);
if (argc < 2) {
print_usage(prog);
return 0;
}
argc--;
argv++;
while (argc >= 1) {
if (!strcmp(*argv, "-help")) {
print_usage(prog);
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, "-signkey")) {
if (--argc < 1) goto bad;
signkeyfile = *(++argv);
} else if (!strcmp(*argv, "-enckey")) {
if (--argc < 1) goto bad;
enckeyfile = *(++argv);
} else {
print_usage(prog);
return 0;
}
argc--;
argv++;
}
if (!certfile || !signkeyfile || !enckeyfile) {
print_usage(prog);
return -1;
}
if (!(certfp = fopen(certfile, "r"))) {
error_print();
return -1;
}
if (!(signkeyfp = fopen(signkeyfile, "r"))) {
error_print();
return -1;
}
if (sm2_private_key_from_pem(&signkey, signkeyfp) != 1) {
error_print();
return -1;
}
if (!(enckeyfp = fopen(enckeyfile, "r"))) {
error_print();
return -1;
}
if (sm2_private_key_from_pem(&enckey, enckeyfp) != 1) {
error_print();
return -1;
}
memset(&conn, 0, sizeof(conn));
if (tlcp_accept(&conn, port, certfp, &signkey, &enckey,
certfp, verify_buf, 4096) != 1) {
error_print();
return -1;
}
// 我要做一个反射的服务器,接收到用户的输入之后,再反射回去
for (;;) {
// 接收一个消息
// 按道理说第二次执行的时候是不可能成功的了,因此客户端没有数据发过来
do {
len = sizeof(buf);
if (tls_recv(&conn, (uint8_t *)buf, &len) != 1) {
error_print();
return -1;
}
} while (!len);
// 把这个消息再发回去
if (tls_send(&conn, (uint8_t *)buf, len) != 1) {
error_print();
return -1;
}
fprintf(stderr, "-----------------\n\n\n\n\n\n");
}
return 1;
bad:
fprintf(stderr, "%s: command error\n", prog);
return 0;
}

194
tools/tls12_client.c Normal file
View File

@@ -0,0 +1,194 @@
/*
* Copyright (c) 2021 - 2021 The GmSSL Project. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. All advertising materials mentioning features or use of this
* software must display the following acknowledgment:
* "This product includes software developed by the GmSSL Project.
* (http://gmssl.org/)"
*
* 4. The name "GmSSL Project" must not be used to endorse or promote
* products derived from this software without prior written
* permission. For written permission, please contact
* guanzhi1980@gmail.com.
*
* 5. Products derived from this software may not be called "GmSSL"
* nor may "GmSSL" appear in their names without prior written
* permission of the GmSSL Project.
*
* 6. Redistributions of any form whatsoever must retain the following
* acknowledgment:
* "This product includes software developed by the GmSSL Project
* (http://gmssl.org/)"
*
* THIS SOFTWARE IS PROVIDED BY THE GmSSL PROJECT ``AS IS'' AND ANY
* EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE GmSSL PROJECT OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <libgen.h>
#include <unistd.h>
#include <gmssl/tls.h>
#include <gmssl/error.h>
const char *http_get =
"GET / HTTP/1.1\r\n"
"Hostname: aaa\r\n"
"\r\n\r\n";
void print_usage(const char *prog)
{
printf("Usage: %s [options]\n", prog);
printf(" -host <str>\n");
printf(" -port <num>\n");
printf(" -cacerts <file>\n");
printf(" -cert <file>\n");
printf(" -key <file>\n");
}
int main(int argc , char *argv[])
{
int ret = -1;
char *prog = basename(argv[0]);
char *host = NULL;
int port = 443;
TLS_CONNECT conn;
char buf[100] = {0};
size_t len = sizeof(buf);
char *cacertsfile = NULL;
char *certfile = NULL;
char *keyfile = NULL;
FILE *cacertsfp = NULL;
FILE *certfp = NULL;
FILE *keyfp = NULL;
SM2_KEY sign_key;
if (argc < 2) {
print_usage(prog);
return 0;
}
argc--;
argv++;
while (argc >= 1) {
if (!strcmp(*argv, "-help")) {
print_usage(prog);
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, "-cacerts")) {
if (--argc < 1) goto bad;
cacertsfile = *(++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 {
print_usage(prog);
return 0;
}
argc--;
argv++;
}
if (!host || !certfile || !keyfile) {
print_usage(prog);
return -1;
}
if (cacertsfile) {
if (!(cacertsfp = fopen(cacertsfile, "r"))) {
error_print();
return -1;
}
}
if (certfile) {
if (!(certfp = fopen(certfile, "r"))) {
error_print();
return -1;
}
}
if (keyfile) {
if (!(keyfp = fopen(keyfile, "r"))) {
error_print();
return -1;
}
if (sm2_private_key_from_pem(&sign_key, keyfp) != 1) {
error_print();
return -1;
}
}
memset(&conn, 0, sizeof(conn));
if (tls12_connect(&conn, host, port, cacertsfp, certfp, &sign_key) != 1) {
error_print();
return -1;
}
// 这个client 发收了一个消息就结束了
if (tls_send(&conn, (uint8_t *)"12345\n", 6) != 1) {
error_print();
return -1;
}
for (;;) {
memset(buf, 0, sizeof(buf));
len = sizeof(buf);
if (tls_recv(&conn, (uint8_t *)buf, &len) != 1) {
error_print();
return -1;
}
if (len > 0) {
printf("%s\n", buf);
break;
}
}
return 1;
bad:
fprintf(stderr, "%s: command error\n", prog);
return 0;
}

175
tools/tls12_server.c Normal file
View File

@@ -0,0 +1,175 @@
/*
* Copyright (c) 2021 - 2021 The GmSSL Project. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. All advertising materials mentioning features or use of this
* software must display the following acknowledgment:
* "This product includes software developed by the GmSSL Project.
* (http://gmssl.org/)"
*
* 4. The name "GmSSL Project" must not be used to endorse or promote
* products derived from this software without prior written
* permission. For written permission, please contact
* guanzhi1980@gmail.com.
*
* 5. Products derived from this software may not be called "GmSSL"
* nor may "GmSSL" appear in their names without prior written
* permission of the GmSSL Project.
*
* 6. Redistributions of any form whatsoever must retain the following
* acknowledgment:
* "This product includes software developed by the GmSSL Project
* (http://gmssl.org/)"
*
* THIS SOFTWARE IS PROVIDED BY THE GmSSL PROJECT ``AS IS'' AND ANY
* EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE GmSSL PROJECT OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <libgen.h>
#include <unistd.h>
#include <gmssl/sm2.h>
#include <gmssl/tls.h>
#include <gmssl/error.h>
void print_usage(const char *prog)
{
printf("Usage: %s [options]\n", prog);
printf(" -port <num>\n");
printf(" -cert <file>\n");
printf(" -signkey <file>\n");
}
int main(int argc , char *argv[])
{
int ret = -1;
char *prog = basename(argv[0]);
int port = 443;
char *certfile = NULL;
char *signkeyfile = NULL;
FILE *certfp = NULL;
FILE *signkeyfp = NULL;
SM2_KEY signkey;
uint8_t verify_buf[4096];
TLS_CONNECT conn;
char buf[1600] = {0};
size_t len = sizeof(buf);
if (argc < 2) {
print_usage(prog);
return 0;
}
argc--;
argv++;
while (argc >= 1) {
if (!strcmp(*argv, "-help")) {
print_usage(prog);
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, "-signkey")) {
if (--argc < 1) goto bad;
signkeyfile = *(++argv);
} else {
print_usage(prog);
return 0;
}
argc--;
argv++;
}
if (!certfile || !signkeyfile) {
print_usage(prog);
return -1;
}
if (!(certfp = fopen(certfile, "r"))) {
error_print();
return -1;
}
if (!(signkeyfp = fopen(signkeyfile, "r"))) {
error_print();
return -1;
}
if (sm2_private_key_from_pem(&signkey, signkeyfp) != 1) {
error_print();
return -1;
}
memset(&conn, 0, sizeof(conn));
if (tls12_accept(&conn, port, certfp, &signkey,
certfp, verify_buf, 4096) != 1) {
error_print();
return -1;
}
// 我要做一个反射的服务器,接收到用户的输入之后,再反射回去
for (;;) {
// 接收一个消息
// 按道理说第二次执行的时候是不可能成功的了,因此客户端没有数据发过来
do {
len = sizeof(buf);
if (tls_recv(&conn, (uint8_t *)buf, &len) != 1) {
error_print();
return -1;
}
} while (!len);
// 把这个消息再发回去
if (tls_send(&conn, (uint8_t *)buf, len) != 1) {
error_print();
return -1;
}
fprintf(stderr, "-----------------\n\n\n\n\n\n");
}
return 1;
bad:
fprintf(stderr, "%s: command error\n", prog);
return 0;
}