Commit 522ee714fd for openssl.org
commit 522ee714fd2d58b50f2b0f3068b7f5d5b71c08e1
Author: Viktor Dukhovni <openssl-users@dukhovni.org>
Date: Thu Feb 12 05:49:33 2026 +1100
Implement default SM2 distinguished identifier
This is needed for certificate verification to work correctly.
Removed unnecessary explicit instances of the distid in most tests, and
documentation.
Reviewed-by: Tim Hudson <tjh@openssl.org>
Reviewed-by: Matt Caswell <matt@openssl.org>
Reviewed-by: Tomas Mraz <tomas@openssl.org>
Reviewed-by: Paul Yang <paulyang.inf@gmail.com>
MergeDate: Sat Feb 21 13:25:30 2026
(Merged from https://github.com/openssl/openssl/pull/29953)
diff --git a/crypto/evp/pmeth_lib.c b/crypto/evp/pmeth_lib.c
index cfc5648d26..66735d628e 100644
--- a/crypto/evp/pmeth_lib.c
+++ b/crypto/evp/pmeth_lib.c
@@ -1216,7 +1216,7 @@ static int evp_pkey_ctx_store_cached_data(EVP_PKEY_CTX *ctx,
if (ctx->cached_parameters.dist_id_name == NULL)
return 0;
}
- if (data_len > 0) {
+ if (data != NULL) {
ctx->cached_parameters.dist_id = OPENSSL_memdup(data, data_len);
if (ctx->cached_parameters.dist_id == NULL)
return 0;
diff --git a/crypto/sm2/sm2_sign.c b/crypto/sm2/sm2_sign.c
index 590a730974..8b2a16ba72 100644
--- a/crypto/sm2/sm2_sign.c
+++ b/crypto/sm2/sm2_sign.c
@@ -20,10 +20,25 @@
#include <openssl/bn.h>
#include <string.h>
+/*
+ * [SM2 Signature Scheme]
+ * (https://datatracker.ietf.org/doc/html/rfc8998#section-3.2.1)
+ *
+ * If either a client or a server needs to verify the peer's SM2 certificate
+ * contained in the Certificate message, then the following ASCII string value
+ * MUST be used as the SM2 identifier according to [GMT.0009-2012]:
+ *
+ * 1234567812345678
+ */
+static const uint8_t default_sm2_id[] = {
+ 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38,
+ 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38
+};
+
int ossl_sm2_compute_z_digest(uint8_t *out,
const EVP_MD *digest,
const uint8_t *id,
- const size_t id_len,
+ size_t id_len,
const EC_KEY *key)
{
int rc = 0;
@@ -80,6 +95,11 @@ int ossl_sm2_compute_z_digest(uint8_t *out,
/* Z = h(ENTL || ID || a || b || xG || yG || xA || yA) */
+ if (id == NULL) {
+ id = default_sm2_id;
+ id_len = sizeof(default_sm2_id);
+ }
+
if (id_len >= (UINT16_MAX / 8)) {
/* too large */
ERR_raise(ERR_LIB_SM2, SM2_R_ID_TOO_LARGE);
diff --git a/doc/man1/openssl-ca.pod.in b/doc/man1/openssl-ca.pod.in
index a499dcc7bf..aba61c82ff 100644
--- a/doc/man1/openssl-ca.pod.in
+++ b/doc/man1/openssl-ca.pod.in
@@ -665,12 +665,6 @@ Sign a certificate request:
openssl ca -in req.pem -out newcert.pem
-Sign an SM2 certificate request:
-
- openssl ca -in sm2.csr -out sm2.crt -md sm3 \
- -sigopt "distid:1234567812345678" \
- -vfyopt "distid:1234567812345678"
-
Sign a certificate request, using CA extensions:
openssl ca -in req.pem -extensions v3_ca -out newcert.pem
diff --git a/doc/man1/openssl-req.pod.in b/doc/man1/openssl-req.pod.in
index ae9724b16e..30099e772e 100644
--- a/doc/man1/openssl-req.pod.in
+++ b/doc/man1/openssl-req.pod.in
@@ -669,15 +669,6 @@ Generate a self-signed root certificate:
openssl req -x509 -newkey rsa:2048 -keyout key.pem -out req.pem
-Create an SM2 private key and then generate a certificate request from it:
-
- openssl ecparam -genkey -name SM2 -out sm2.key
- openssl req -new -key sm2.key -out sm2.csr -sm3 -sigopt "distid:1234567812345678"
-
-Examine and verify an SM2 certificate request:
-
- openssl req -verify -in sm2.csr -sm3 -vfyopt "distid:1234567812345678"
-
Example of a file pointed to by the B<oid_file> option:
1.2.3.4 shortName A longer Name
diff --git a/include/crypto/sm2.h b/include/crypto/sm2.h
index 246d644c27..d04cc33728 100644
--- a/include/crypto/sm2.h
+++ b/include/crypto/sm2.h
@@ -22,9 +22,6 @@
int ossl_sm2_key_private_check(const EC_KEY *eckey);
-/* The default user id as specified in GM/T 0009-2012 */
-#define SM2_DEFAULT_USERID "1234567812345678"
-
int ossl_sm2_compute_z_digest(uint8_t *out,
const EVP_MD *digest,
const uint8_t *id,
diff --git a/providers/implementations/signature/sm2_sig.c b/providers/implementations/signature/sm2_sig.c
index 1c0433f677..96d1c6892a 100644
--- a/providers/implementations/signature/sm2_sig.c
+++ b/providers/implementations/signature/sm2_sig.c
@@ -449,7 +449,7 @@ static int sm2sig_set_ctx_params(void *vpsm2ctx, const OSSL_PARAM params[])
if (!psm2ctx->flag_compute_z_digest)
return 0;
- if (p.distid->data_size != 0
+ if ((p.distid->data != NULL)
&& !OSSL_PARAM_get_octet_string(p.distid, &tmp_id, 0, &tmp_idlen))
return 0;
OPENSSL_free(psm2ctx->id);
diff --git a/test/evp_extra_test.c b/test/evp_extra_test.c
index 70f17a9d0b..d2979dca0f 100644
--- a/test/evp_extra_test.c
+++ b/test/evp_extra_test.c
@@ -2683,7 +2683,9 @@ static int test_EVP_SM2(void)
EVP_MD_CTX *md_ctx_verify = NULL;
EVP_PKEY_CTX *cctx = NULL;
EVP_MD *check_md = NULL;
- uint8_t sm2_id[] = { 1, 2, 3, 4, 'l', 'e', 't', 't', 'e', 'r' };
+ uint8_t sm2_id[] = {
+ 0x01, 0x02, 0x03, 0x04, 0x6c, 0x65, 0x74, 0x74, 0x65, 0x72
+ };
#ifndef OPENSSL_NO_X963KDF
uint8_t ciphertext[128];
size_t ctext_len = sizeof(ciphertext);
diff --git a/test/recipes/25-test_req.t b/test/recipes/25-test_req.t
index 7dfbe02778..a10aef8628 100644
--- a/test/recipes/25-test_req.t
+++ b/test/recipes/25-test_req.t
@@ -330,27 +330,25 @@ subtest "generating SM2 certificate requests" => sub {
ok(run(app(["openssl", "req",
"-config", srctop_file("test", "test.cnf"),
"-new", "-key", srctop_file(@certs, "sm2.key"),
- "-sigopt", "distid:1234567812345678",
- "-out", "testreq-sm2.pem", "-sm3"])),
+ "-out", "testreq-sm2.pem"])),
"Generating SM2 certificate request");
ok(run(app(["openssl", "req",
"-config", srctop_file("test", "test.cnf"),
- "-verify", "-in", "testreq-sm2.pem", "-noout",
- "-vfyopt", "distid:1234567812345678", "-sm3"])),
+ "-verify", "-in", "testreq-sm2.pem", "-noout"])),
"Verifying signature on SM2 certificate request");
ok(run(app(["openssl", "req",
"-config", srctop_file("test", "test.cnf"),
"-new", "-key", srctop_file(@certs, "sm2.key"),
"-sigopt", "hexdistid:DEADBEEF",
- "-out", "testreq-sm2.pem", "-sm3"])),
+ "-out", "testreq-sm2.pem"])),
"Generating SM2 certificate request with hex id");
ok(run(app(["openssl", "req",
"-config", srctop_file("test", "test.cnf"),
"-verify", "-in", "testreq-sm2.pem", "-noout",
- "-vfyopt", "hexdistid:DEADBEEF", "-sm3"])),
+ "-vfyopt", "hexdistid:DEADBEEF"])),
"Verifying signature on SM2 certificate request");
}
};
diff --git a/test/recipes/80-test_ca.t b/test/recipes/80-test_ca.t
index 5fc620a139..f33a5d51b3 100644
--- a/test/recipes/80-test_ca.t
+++ b/test/recipes/80-test_ca.t
@@ -72,10 +72,7 @@ SKIP: {
is(yes(cmdstr(app(["openssl", "ca", "-config",
$cnf,
"-in", src_file("sm2-csr.pem"),
- "-out", "sm2-test.crt",
- "-sigopt", "distid:1234567812345678",
- "-vfyopt", "distid:1234567812345678",
- "-md", "sm3",
+ "-out", "sm2-test.crt", "-md", "sm3",
"-cert", src_file("sm2-root.crt"),
"-keyfile", src_file("sm2-root.key")]))),
0,
diff --git a/test/sm2_internal_test.c b/test/sm2_internal_test.c
index 684e3ac269..c159093842 100644
--- a/test/sm2_internal_test.c
+++ b/test/sm2_internal_test.c
@@ -298,7 +298,8 @@ done:
#endif /* OPENSSL_NO_X963KDF */
static int test_sm2_sign(const EC_GROUP *group,
- const char *userid,
+ const uint8_t *userid,
+ size_t userid_len,
const char *privkey_hex,
const char *message,
const char *k_hex,
@@ -335,8 +336,8 @@ static int test_sm2_sign(const EC_GROUP *group,
}
start_fake_rand(k_hex);
- sig = ossl_sm2_do_sign(key, EVP_sm3(), (const uint8_t *)userid,
- strlen(userid), (const uint8_t *)message, msg_len);
+ sig = ossl_sm2_do_sign(key, EVP_sm3(), userid,
+ userid_len, (const uint8_t *)message, msg_len);
if (!TEST_ptr(sig)) {
restore_rand();
goto done;
@@ -351,8 +352,8 @@ static int test_sm2_sign(const EC_GROUP *group,
|| !TEST_BN_eq(s, sig_s))
goto done;
- ok = ossl_sm2_do_verify(key, EVP_sm3(), sig, (const uint8_t *)userid,
- strlen(userid), (const uint8_t *)message, msg_len);
+ ok = ossl_sm2_do_verify(key, EVP_sm3(), sig, userid,
+ userid_len, (const uint8_t *)message, msg_len);
/* We goto done whether this passes or fails */
TEST_true(ok);
@@ -372,6 +373,11 @@ static int sm2_sig_test(void)
{
int testresult = 0;
EC_GROUP *gm_group = NULL;
+ /* ALICE123@YAHOO.COM */
+ static const uint8_t test_alice_id[] = {
+ 0x41, 0x4c, 0x49, 0x43, 0x45, 0x31, 0x32, 0x33, 0x40,
+ 0x59, 0x41, 0x48, 0x4f, 0x4f, 0x2e, 0x43, 0x4f, 0x4d
+ };
/* From draft-shen-sm2-ecdsa-02 */
EC_GROUP *test_group = create_EC_group("8542D69E4C044F18E8B92435BF6FF7DE457283915C45517D722EDB8B08F1DFC3",
"787968B4FA32C3FD2417842E73BBFEFF2F3C848B6831D7E0EC65228B3937E498",
@@ -386,7 +392,7 @@ static int sm2_sig_test(void)
if (!TEST_true(test_sm2_sign(
test_group,
- "ALICE123@YAHOO.COM",
+ test_alice_id, sizeof(test_alice_id),
"128B2FA8BD433C6C068C8D803DFF79792A519A55171B1B650C23661D15897263",
"message digest",
"006CB28D99385C175C94F94E934817663FC176D925DD72B727260DBAAE1FB2F96F"
@@ -410,8 +416,8 @@ static int sm2_sig_test(void)
if (!TEST_true(test_sm2_sign(
gm_group,
- /* the default ID specified in GM/T 0009-2012 (Sec. 10).*/
- SM2_DEFAULT_USERID,
+ /* Use the default ID. */
+ NULL, 0,
/* privkey */
"3945208F7B2144B13F36E38AC6D39F95889393692860B51A42FB81EF4DF7C5B8",
/* plaintext message */
@@ -429,8 +435,8 @@ static int sm2_sig_test(void)
/* Make sure we fail if we omit the public portion of the key */
if (!TEST_false(test_sm2_sign(
gm_group,
- /* the default ID specified in GM/T 0009-2012 (Sec. 10).*/
- SM2_DEFAULT_USERID,
+ /* Use the default ID. */
+ NULL, 0,
/* privkey */
"3945208F7B2144B13F36E38AC6D39F95889393692860B51A42FB81EF4DF7C5B8",
/* plaintext message */