From 0ec5ebeb49c3580598775c47fc1e06251d1ff5f8 Mon Sep 17 00:00:00 2001 From: Tianjia Zhang Date: Mon, 21 Sep 2020 00:21:01 +0800 Subject: [PATCH 2131/2944] X.509: support OSCCA certificate parse to #31026859 commit 254f84f559039b6d6f6e1035fd7645b42671ab48 upstream The digital certificate format based on SM2 crypto algorithm as specified in GM/T 0015-2012. It was published by State Encryption Management Bureau, China. This patch adds the OID object identifier defined by OSCCA. The x509 certificate supports SM2-with-SM3 type certificate parsing. It uses the standard elliptic curve public key, and the sm2 algorithm signs the hash generated by sm3. Signed-off-by: Tianjia Zhang Tested-by: Xufeng Zhang Reviewed-by: Vitaly Chikunov Signed-off-by: Herbert Xu Signed-off-by: Tianjia Zhang Reviewed-by: Jia Zhang --- crypto/asymmetric_keys/x509_cert_parser.c | 20 ++++++++++++++++---- include/linux/oid_registry.h | 6 ++++++ 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/crypto/asymmetric_keys/x509_cert_parser.c b/crypto/asymmetric_keys/x509_cert_parser.c index b6cabac..37af883 100644 --- a/crypto/asymmetric_keys/x509_cert_parser.c +++ b/crypto/asymmetric_keys/x509_cert_parser.c @@ -226,6 +226,11 @@ int x509_note_pkey_algo(void *context, size_t hdrlen, ctx->cert->sig->hash_algo = "sha224"; ctx->cert->sig->pkey_algo = "rsa"; break; + + case OID_SM2_with_SM3: + ctx->cert->sig->hash_algo = "sm3"; + ctx->cert->sig->pkey_algo = "sm2"; + break; } ctx->algo_oid = ctx->last_oid; @@ -249,7 +254,8 @@ int x509_note_signature(void *context, size_t hdrlen, return -EINVAL; } - if (strcmp(ctx->cert->sig->pkey_algo, "rsa") == 0) { + if (strcmp(ctx->cert->sig->pkey_algo, "rsa") == 0 || + strcmp(ctx->cert->sig->pkey_algo, "sm2") == 0) { /* Discard the BIT STRING metadata */ if (vlen < 1 || *(const u8 *)value != 0) return -EBADMSG; @@ -412,10 +418,16 @@ int x509_extract_key_data(void *context, size_t hdrlen, { struct x509_parse_context *ctx = context; - if (ctx->last_oid != OID_rsaEncryption) + switch (ctx->last_oid) { + case OID_rsaEncryption: + ctx->cert->pub->pkey_algo = "rsa"; + break; + case OID_id_ecPublicKey: + ctx->cert->pub->pkey_algo = "sm2"; + break; + default: return -ENOPKG; - - ctx->cert->pub->pkey_algo = "rsa"; + } /* Discard the BIT STRING metadata */ if (vlen < 1 || *(const u8 *)value != 0) diff --git a/include/linux/oid_registry.h b/include/linux/oid_registry.h index d2fa9ca..1bfb7d7 100644 --- a/include/linux/oid_registry.h +++ b/include/linux/oid_registry.h @@ -93,6 +93,12 @@ enum OID { OID_authorityKeyIdentifier, /* 2.5.29.35 */ OID_extKeyUsage, /* 2.5.29.37 */ + /* OSCCA */ + OID_sm2, /* 1.2.156.10197.1.301 */ + OID_sm3, /* 1.2.156.10197.1.401 */ + OID_SM2_with_SM3, /* 1.2.156.10197.1.501 */ + OID_sm3WithRSAEncryption, /* 1.2.156.10197.1.504 */ + OID__NR }; -- 1.8.3.1