Skip to content

Commit d114bed

Browse files
committed
Fix sm2utl bugs
1 parent 22027d6 commit d114bed

6 files changed

Lines changed: 5469 additions & 5435 deletions

File tree

apps/sm2.c

Lines changed: 37 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ NON_EMPTY_TRANSLATION_UNIT
6969
# include <openssl/pem.h>
7070
# include <openssl/sm2.h>
7171
# include <openssl/objects.h>
72+
# include "../crypto/ec/ec_lcl.h"
7273
# include "apps.h"
7374

7475
static OPT_PAIR conv_forms[] = {
@@ -129,7 +130,7 @@ int sm2_main(int argc, char **argv)
129130
ENGINE *e = NULL;
130131
EC_KEY *eckey = NULL;
131132
const EC_GROUP *group;
132-
const EVP_CIPHER *enc = NULL;
133+
const EVP_CIPHER *enc = EVP_sms4_cbc();
133134
point_conversion_form_t form = POINT_CONVERSION_UNCOMPRESSED;
134135
char *infile = NULL, *outfile = NULL, *prog;
135136
char *passin = NULL, *passout = NULL, *passinarg = NULL, *passoutarg = NULL;
@@ -292,6 +293,10 @@ int sm2_main(int argc, char **argv)
292293
unsigned char buf[256] = {0};
293294
unsigned char *key = NULL;
294295
long keylen;
296+
if (!(eckey = EC_KEY_new_by_curve_name(NID_sm2p256v1))) {
297+
ERR_print_errors(bio_err);
298+
goto end;
299+
}
295300
if (BIO_read(in, buf, sizeof(buf) - 1) <= 0) {
296301
ERR_print_errors(bio_err);
297302
OPENSSL_cleanse(buf, sizeof(buf));
@@ -304,16 +309,25 @@ int sm2_main(int argc, char **argv)
304309
}
305310
OPENSSL_cleanse(buf, sizeof(buf));
306311
if (keylen != 32) {
307-
BIO_printf(bio_err, "invalid private key in hex format\n");
312+
BIO_printf(bio_err, "Invalid private key in hex format\n");
313+
BIO_printf(bio_err, "Key length is %ld, not 32 byte\n", keylen);
308314
OPENSSL_cleanse(key, keylen);
309315
goto end;
310316
}
311-
if (!EC_KEY_oct2key(eckey, key, sizeof(key), NULL)) {
317+
if (!EC_KEY_oct2priv(eckey, key, keylen)) {
312318
ERR_print_errors(bio_err);
313319
OPENSSL_cleanse(key, keylen);
314320
goto end;
315321
}
316322
OPENSSL_cleanse(key, keylen);
323+
324+
if (eckey->group->meth->keygenpub == NULL
325+
|| eckey->group->meth->keygenpub(eckey) == 0) {
326+
BIO_printf(bio_err, "Generate public key from private key failed\n");
327+
ERR_print_errors(bio_err);
328+
goto end;
329+
}
330+
317331
} else if (informat == FORMAT_ENGINE) {
318332
EVP_PKEY *pkey;
319333
if (pubin)
@@ -378,6 +392,24 @@ int sm2_main(int argc, char **argv)
378392
}
379393
}
380394

395+
if (genzid) {
396+
unsigned char z[64];
397+
size_t zlen = sizeof(z);
398+
if (!id) {
399+
id = SM2_DEFAULT_ID;
400+
BIO_printf(bio_err, "use default identity '%s'\n", id);
401+
}
402+
if (!SM2_compute_id_digest(EVP_sm3(), id, strlen(id), z, &zlen, eckey)) {
403+
goto end;
404+
}
405+
BIO_printf(out, "id: %s\n", id);
406+
BIO_puts(out, "Z:\n");
407+
ASN1_buf_print(out, z, zlen, 4);
408+
409+
410+
BIO_printf(out, "\n");
411+
}
412+
381413
if (noout) {
382414
ret = 0;
383415
goto end;
@@ -400,26 +432,12 @@ int sm2_main(int argc, char **argv)
400432
i = PEM_write_bio_EC_PUBKEY(out, eckey);
401433
else {
402434
assert(private);
435+
//FIXME: use PKCS#8
403436
i = PEM_write_bio_ECPrivateKey(out, eckey, enc,
404437
NULL, 0, NULL, passout);
438+
405439
}
406440
}
407-
if (genzid) {
408-
unsigned char z[64];
409-
size_t zlen = sizeof(z);
410-
if (!id) {
411-
id = SM2_DEFAULT_ID;
412-
BIO_printf(bio_err, "use default identity '%s'\n", id);
413-
}
414-
if (!SM2_compute_id_digest(EVP_sm3(), id, strlen(id), z, &zlen, eckey)) {
415-
goto end;
416-
}
417-
BIO_printf(out, "Z = ");
418-
for (i = 0; i < zlen; i++) {
419-
BIO_printf(out, "%02X", z[i]);
420-
}
421-
BIO_printf(out, "\n");
422-
}
423441

424442
if (!i) {
425443
BIO_printf(bio_err, "unable to write private key\n");

apps/sm2utl.c

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,17 @@ int sm2utl_main(int argc, char **argv)
288288
goto end;
289289
}
290290

291+
switch (op) {
292+
case OP_DGST:
293+
case OP_SIGN:
294+
case OP_VERIFY:
295+
if (!id) {
296+
BIO_printf(bio_err, "Option '-id' required\n");
297+
goto end;
298+
}
299+
break;
300+
}
301+
291302
switch (op) {
292303
case OP_DGST:
293304
return sm2utl_sign(md, in, out, id, e, ec_key, 0);
@@ -330,7 +341,7 @@ static int sm2utl_sign(const EVP_MD *md, BIO *in, BIO *out, const char *id,
330341
ERR_print_errors(bio_err);
331342
goto end;
332343
}
333-
while ((len = BIO_read(in, buf, sizeof(buf))) <= 0) {
344+
while ((len = BIO_read(in, buf, sizeof(buf))) > 0) {
334345
if (!EVP_DigestUpdate(md_ctx, buf, len)) {
335346
ERR_print_errors(bio_err);
336347
goto end;
@@ -386,7 +397,7 @@ static int sm2utl_verify(const EVP_MD *md, BIO *in, BIO *out, BIO *sig,
386397
ERR_print_errors(bio_err);
387398
goto end;
388399
}
389-
while ((len = BIO_read(in, buf, sizeof(buf))) <= 0) {
400+
while ((len = BIO_read(in, buf, sizeof(buf))) > 0) {
390401
if (!EVP_DigestUpdate(md_ctx, buf, len)) {
391402
ERR_print_errors(bio_err);
392403
goto end;
@@ -420,6 +431,9 @@ static int sm2utl_encrypt(const EVP_MD *md, BIO *in, BIO *out, EC_KEY *ec_key)
420431
int len;
421432

422433
if (!(len = bio_to_mem(&buf, SM2_MAX_PLAINTEXT_LENGTH, in))) {
434+
ERR_print_errors(bio_err);
435+
BIO_printf(bio_err, "Error reading plaintext\n");
436+
goto end;
423437
}
424438
if (!(cval = SM2_do_encrypt(md, buf, len, ec_key))
425439
|| i2d_SM2CiphertextValue_bio(out, cval) <= 0) {

crypto/ec/ec_key.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,8 @@ int ec_key_simple_generate_key(EC_KEY *eckey)
250250

251251
int ec_key_simple_generate_public_key(EC_KEY *eckey)
252252
{
253+
if (eckey->pub_key == NULL)
254+
eckey->pub_key = EC_POINT_new(eckey->group);
253255
return EC_POINT_mul(eckey->group, eckey->pub_key, eckey->priv_key, NULL,
254256
NULL, NULL);
255257
}

include/openssl/opensslv.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,9 @@ extern "C" {
4141
*/
4242
# define OPENSSL_VERSION_NUMBER 0x1010004fL
4343
# ifdef OPENSSL_FIPS
44-
# define OPENSSL_VERSION_TEXT "GmSSL 2.4.5 - OpenSSL 1.1.0d-fips 1 Feb 2019"
44+
# define OPENSSL_VERSION_TEXT "GmSSL 2.4.5 - OpenSSL 1.1.0d-fips 2 Feb 2019"
4545
# else
46-
# define OPENSSL_VERSION_TEXT "GmSSL 2.4.5 - OpenSSL 1.1.0d 1 Feb 2019"
46+
# define OPENSSL_VERSION_TEXT "GmSSL 2.4.5 - OpenSSL 1.1.0d 2 Feb 2019"
4747
# endif
4848

4949
/*-

0 commit comments

Comments
 (0)