]> git.ipfire.org Git - thirdparty/openssl.git/commitdiff
Implement default SM2 distinguished identifier
authorViktor Dukhovni <openssl-users@dukhovni.org>
Wed, 11 Feb 2026 18:49:33 +0000 (05:49 +1100)
committerViktor Dukhovni <openssl-users@dukhovni.org>
Sat, 21 Feb 2026 13:24:20 +0000 (00:24 +1100)
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)

crypto/evp/pmeth_lib.c
crypto/sm2/sm2_sign.c
doc/man1/openssl-ca.pod.in
doc/man1/openssl-req.pod.in
include/crypto/sm2.h
providers/implementations/signature/sm2_sig.c
test/evp_extra_test.c
test/recipes/25-test_req.t
test/recipes/80-test_ca.t
test/sm2_internal_test.c

index cfc5648d26ca66b0eed97aad41dbd45d7835520f..66735d628ed5674112c493ca689a7dce5ffd7ab3 100644 (file)
@@ -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;
index 590a7309748f83b7f3b0fc09a32c4d249a259d66..8b2a16ba72620e44f0b74faf1f83477309699693 100644 (file)
 #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);
index a499dcc7bfd12a7652059d1d3228f274fc20f95d..aba61c82ffeb22d3b219d2219a391ce2af53ae0e 100644 (file)
@@ -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
index ae9724b16e32e198c1f3403be987dd3b95894c0d..30099e772edd1a47e44bed1ea966e1cfe54a7d12 100644 (file)
@@ -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
index 246d644c27974f7f9d3bd4cdc1d6a4fb823ef356..d04cc33728de57738d4c763ab355b2d7a0eabb71 100644 (file)
@@ -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,
index 1c0433f67777deef1d2d1191d4c4e4ac474041e8..96d1c6892aaafb534c66caf929511cd92fdbec3e 100644 (file)
@@ -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);
index 70f17a9d0b65162e97b7d6a390ad6898c5ba35a0..d2979dca0fec42d8b851ca218a132ec2b3a34747 100644 (file)
@@ -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);
index 7dfbe02778e76dbbd43b0895409b0405a6182684..a10aef86287e97647b632c7fd469bd9a1caa9947 100644 (file)
@@ -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");
     }
 };
index 5fc620a1399e5c57734beb3f112a5c31253011bd..f33a5d51b3751537849b4b61a346a7c2f26a4c2e 100644 (file)
@@ -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,
index 684e3ac269684d431ca083881aa48da948527362..c1590938427b4a2dc748e80f1b7202d2bdb2000d 100644 (file)
@@ -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 */