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)
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;
#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;
/* 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);
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
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
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,
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);
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);
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");
}
};
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,
#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,
}
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;
|| !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);
{
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",
if (!TEST_true(test_sm2_sign(
test_group,
- "ALICE123@YAHOO.COM",
+ test_alice_id, sizeof(test_alice_id),
"128B2FA8BD433C6C068C8D803DFF79792A519A55171B1B650C23661D15897263",
"message digest",
"006CB28D99385C175C94F94E934817663FC176D925DD72B727260DBAAE1FB2F96F"
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 */
/* 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 */