Skip to contentSkip to conversation

Add support for x509 certs with NIST p256 and p192 keys

PATCH Superseded Stefan Berger 1 participant 4 messages Jan 26, 2021 – Jan 27, 2021

Subsystems (4)

v1 of 11 Superseded Latest is v11 →

lore
Share & tools
lore
#0 lore raw

cover Add support for x509 certs with NIST p256 and p192 keys

From: Stefan Berger <[email protected]>

This series of patches adds support for x509 certificates signed by a CA that uses NIST p256 or p192 keys for signing. It also adds support for certificates where the public key is a NIST p256 or p192 key. The math for ECDSA signature verification is also added.

Since self-signed certificates are verified upon loading, the following script can be used for testing:

keyctrl newring test @u

while :; do

	for hash in sha1 sha224 sha256 sha384 sha512; do
		openssl req \
			-x509 \
			-${hash} \
			-newkey ec \
			-pkeyopt ec_paramgen_curve:prime256v1 \
			-keyout key.pem \
			-days 365 \
			-subj '/CN=test' \
			-nodes \
			-outform der \
			-out cert.der
		keyctl padd asymmetric testkey %keyring:test < cert.der
		if [ $? -ne 0 ]; then
			echo "ERROR"
			exit 1
		fi
	done

done

It also works with restricted keyrings where an RSA key is used to sign a NIST P256/P192 key:

The ECDSA signature verification will be used by IMA Appraisal where ECDSA file signatures stored in RPM packages will use substantially less space than if RSA signatures were to be used.

Stefan

Stefan Berger (3):
x509: Add support for parsing x509 certs with NIST p256 keys
x509: Add support for NIST p192 keys in certificates and akcipher
x509: Detect sm2 keys by their parameters OID

 crypto/Makefile                           |   9 +-
 crypto/asymmetric_keys/public_key.c       |  19 ++
 crypto/asymmetric_keys/x509_cert_parser.c |  45 ++-
 crypto/ecc.c                              | 331 ++++++++++++++++++++++
 crypto/ecc.h                              |   2 +
 crypto/ecc_curve_defs.h                   |   4 +
 crypto/eccsignature.asn1                  |   4 +
 include/linux/oid_registry.h              |   6 +
 8 files changed, 417 insertions(+), 3 deletions(-)
 create mode 100644 crypto/eccsignature.asn1
Signature · 1 line
2.25.4
#1 lore raw

PATCH 1/3 x509: Add support for parsing x509 certs with NIST p256 keys

From: Stefan Berger <[email protected]>

This patch adds support for parsing of x509 certificates that contain NIST P256 keys that have been signed by a CA using any of the current SHA hash algorithms. Since self-signed certificates are verified, the ecc math for signature verification is also added.

---
 crypto/Makefile                           |   9 +-
 crypto/asymmetric_keys/public_key.c       |  16 ++
 crypto/asymmetric_keys/x509_cert_parser.c |  39 ++-
 crypto/ecc.c                              | 297 ++++++++++++++++++++++
 crypto/ecc.h                              |   2 +
 crypto/ecc_curve_defs.h                   |   4 +
 crypto/eccsignature.asn1                  |   4 +
 include/linux/oid_registry.h              |   5 +
 8 files changed, 374 insertions(+), 2 deletions(-)
 create mode 100644 crypto/eccsignature.asn1
crypto/Makefile +8 -1
index b279483fba50..09ae06235274 100644
@@ -50,6 +50,14 @@ sm2_generic-y += sm2.o
 
 obj-$(CONFIG_CRYPTO_SM2) += sm2_generic.o
 
+$(obj)/eccsignature.asn1.o: $(obj)/eccsignature.asn1.c $(obj)/eccsignature.asn1.h
+$(obj)/ecc.o: $(obj)/sm2signature.asn1.h
+
+ecc_generic-y += eccsignature.asn1.o
+ecc_generic-y += ecc.o
+
+obj-$(CONFIG_CRYPTO_ECC) += ecc_generic.o
+
 crypto_acompress-y := acompress.o
 crypto_acompress-y += scompress.o
 obj-$(CONFIG_CRYPTO_ACOMP2) += crypto_acompress.o
@@ -172,7 +180,6 @@ obj-$(CONFIG_CRYPTO_USER_API_RNG) += algif_rng.o
 obj-$(CONFIG_CRYPTO_USER_API_AEAD) += algif_aead.o
 obj-$(CONFIG_CRYPTO_ZSTD) += zstd.o
 obj-$(CONFIG_CRYPTO_OFB) += ofb.o
-obj-$(CONFIG_CRYPTO_ECC) += ecc.o
 obj-$(CONFIG_CRYPTO_ESSIV) += essiv.o
 obj-$(CONFIG_CRYPTO_CURVE25519) += curve25519-generic.o
 
crypto/asymmetric_keys/public_key.c +16 -0
index 8892908ad58c..0fcbaec0ded0 100644
@@ -90,6 +90,22 @@ int software_key_determine_akcipher(const char *encoding,
 		return 0;
 	}
 
+	if (strcmp(encoding, "x962") == 0) {
+		enum OID oid;
+
+		if (pkey->paramlen < 2)
+			return -EINVAL;
+
+		oid = look_up_OID(pkey->params + 2, pkey->paramlen - 2);
+		switch (oid) {
+		case OID_id_prime256v1:
+			strcpy(alg_name, "nist_p256");
+			return 0;
+		default:
+			return -EINVAL;
+		}
+	}
+
 	return -ENOPKG;
 }
 
crypto/asymmetric_keys/x509_cert_parser.c +38 -1
index 52c9b455fc7d..44bae5ccb475 100644
@@ -203,6 +203,26 @@ int x509_note_pkey_algo(void *context, size_t hdrlen,
 	default:
 		return -ENOPKG; /* Unsupported combination */
 
+	case OID_id_ecdsa_with_sha1:
+		ctx->cert->sig->hash_algo = "sha1";
+		goto ecdsa;
+
+	case OID_id_ecdsa_with_sha224:
+		ctx->cert->sig->hash_algo = "sha224";
+		goto ecdsa;
+
+	case OID_id_ecdsa_with_sha256:
+		ctx->cert->sig->hash_algo = "sha256";
+		goto ecdsa;
+
+	case OID_id_ecdsa_with_sha384:
+		ctx->cert->sig->hash_algo = "sha384";
+		goto ecdsa;
+
+	case OID_id_ecdsa_with_sha512:
+		ctx->cert->sig->hash_algo = "sha512";
+		goto ecdsa;
+
 	case OID_md4WithRSAEncryption:
 		ctx->cert->sig->hash_algo = "md4";
 		goto rsa_pkcs1;
@@ -255,6 +275,11 @@ int x509_note_pkey_algo(void *context, size_t hdrlen,
 	ctx->cert->sig->encoding = "raw";
 	ctx->algo_oid = ctx->last_oid;
 	return 0;
+ecdsa:
+	ctx->cert->sig->pkey_algo = "ecdsa";
+	ctx->cert->sig->encoding = "x962";
+	ctx->algo_oid = ctx->last_oid;
+	return 0;
 }
 
 /*
@@ -276,7 +301,8 @@ int x509_note_signature(void *context, size_t hdrlen,
 
 	if (strcmp(ctx->cert->sig->pkey_algo, "rsa") == 0 ||
 	    strcmp(ctx->cert->sig->pkey_algo, "ecrdsa") == 0 ||
-	    strcmp(ctx->cert->sig->pkey_algo, "sm2") == 0) {
+	    strcmp(ctx->cert->sig->pkey_algo, "sm2") == 0 ||
+	    strcmp(ctx->cert->sig->pkey_algo, "ecdsa") == 0) {
 		/* Discard the BIT STRING metadata */
 		if (vlen < 1 || *(const u8 *)value != 0)
 			return -EBADMSG;
@@ -471,6 +497,17 @@ int x509_extract_key_data(void *context, size_t hdrlen,
 		break;
 	case OID_id_ecPublicKey:
 		ctx->cert->pub->pkey_algo = "sm2";
+		if (ctx->params_size > 2) {
+			enum OID oid = look_up_OID(ctx->params + 2,
+						   ctx->params_size - 2);
+			switch (oid) {
+			case OID_id_prime256v1:
+				ctx->cert->pub->pkey_algo = "ecdsa";
+				break;
+			default:
+				break;
+			}
+		}
 		break;
 	default:
 		return -ENOPKG;
crypto/ecc.c +297 -0

320 lines not on this page · raw message

crypto/ecc.h +2 -0
index d4e546b9ad79..26d7c83908bc 100644
@@ -33,6 +33,8 @@
 
 #define ECC_DIGITS_TO_BYTES_SHIFT 3
 
+#define ECC_MAX_BYTES (ECC_MAX_DIGITS << ECC_DIGITS_TO_BYTES_SHIFT)
+
 /**
  * struct ecc_point - elliptic curve point in affine coordinates
  *
crypto/ecc_curve_defs.h +4 -0
index 69be6c7d228f..562651f28ef5 100644
@@ -28,6 +28,8 @@ static struct ecc_curve nist_p192 = {
 	.b = nist_p192_b
 };
 
+#define NIST_P192_KEY_SIZE 24
+
 /* NIST P-256: a = p - 3 */
 static u64 nist_p256_g_x[] = { 0xF4A13945D898C296ull, 0x77037D812DEB33A0ull,
 				0xF8BCE6E563A440F2ull, 0x6B17D1F2E12C4247ull };
@@ -54,4 +56,6 @@ static struct ecc_curve nist_p256 = {
 	.b = nist_p256_b
 };
 
+#define NIST_P256_KEY_SIZE 32
+
 #endif
crypto/eccsignature.asn1 new file +4 -0
new file mode 100644
index 000000000000..e6c82381f19d
@@ -0,0 +1,4 @@
+ECDSASignature ::= SEQUENCE {
+	r	INTEGER ({ ecc_get_signature_r }),
+	s	INTEGER ({ ecc_get_signature_s })
+}
include/linux/oid_registry.h +5 -0
index 4462ed2c18cd..9060f19c80eb 100644
@@ -21,6 +21,11 @@ enum OID {
 	OID_id_dsa,			/* 1.2.840.10040.4.1 */
 	OID_id_ecdsa_with_sha1,		/* 1.2.840.10045.4.1 */
 	OID_id_ecPublicKey,		/* 1.2.840.10045.2.1 */
+	OID_id_prime256v1,		/* 1.2.840.10045.3.1.7 */
+	OID_id_ecdsa_with_sha224,	/* 1.2.840.10045.4.3.1 */
+	OID_id_ecdsa_with_sha256,	/* 1.2.840.10045.4.3.2 */
+	OID_id_ecdsa_with_sha384,	/* 1.2.840.10045.4.3.3 */
+	OID_id_ecdsa_with_sha512,	/* 1.2.840.10045.4.3.4 */
 
 	/* PKCS#1 {iso(1) member-body(2) us(840) rsadsi(113549) pkcs(1) pkcs-1(1)} */
 	OID_rsaEncryption,		/* 1.2.840.113549.1.1.1 */
Signature · 1 line
2.25.4
#2 lore raw

PATCH 2/3 x509: Add support for NIST p192 keys in certificates and akcipher

In reply to Stefan Berger

From: Stefan Berger <[email protected]>

Add support for NIST p192 keys in x509 certificates and support it in 'akcipher'.

---
 crypto/asymmetric_keys/public_key.c       |  3 ++
 crypto/asymmetric_keys/x509_cert_parser.c |  1 +
 crypto/ecc.c                              | 36 ++++++++++++++++++++++-
 include/linux/oid_registry.h              |  1 +
 4 files changed, 40 insertions(+), 1 deletion(-)
crypto/asymmetric_keys/public_key.c +3 -0
index 0fcbaec0ded0..bb4a7cc0e3c8 100644
@@ -98,6 +98,9 @@ int software_key_determine_akcipher(const char *encoding,
 
 		oid = look_up_OID(pkey->params + 2, pkey->paramlen - 2);
 		switch (oid) {
+		case OID_id_prime192v1:
+			strcpy(alg_name, "nist_p192");
+			return 0;
 		case OID_id_prime256v1:
 			strcpy(alg_name, "nist_p256");
 			return 0;
crypto/asymmetric_keys/x509_cert_parser.c +1 -0
index 44bae5ccb475..720cc7977077 100644
@@ -501,6 +501,7 @@ int x509_extract_key_data(void *context, size_t hdrlen,
 			enum OID oid = look_up_OID(ctx->params + 2,
 						   ctx->params_size - 2);
 			switch (oid) {
+			case OID_id_prime192v1:
 			case OID_id_prime256v1:
 				ctx->cert->pub->pkey_algo = "ecdsa";
 				break;
crypto/ecc.c +35 -1
index fb8370720350..79df35a23a61 100644
@@ -1826,13 +1826,47 @@ static struct akcipher_alg ecc_nist_p256 = {
 	},
 };
 
+static unsigned int ecc_nist_p192_max_size(struct crypto_akcipher *tfm)
+{
+	return NIST_P192_KEY_SIZE;
+}
+
+static int ecc_nist_p192_init_tfm(struct crypto_akcipher *tfm)
+{
+	struct ecc_ctx *ctx = akcipher_tfm_ctx(tfm);
+
+	return ecc_ec_ctx_init(ctx, ECC_CURVE_NIST_P192);
+}
+
+static struct akcipher_alg ecc_nist_p192 = {
+	.verify = ecdsa_verify,
+	.set_pub_key = ecc_set_pub_key,
+	.max_size = ecc_nist_p192_max_size,
+	.init = ecc_nist_p192_init_tfm,
+	.exit = ecc_exit_tfm,
+	.base = {
+		.cra_name = "nist_p192",
+		.cra_driver_name = "ecc-nist-p192",
+		.cra_priority = 100,
+		.cra_module = THIS_MODULE,
+		.cra_ctxsize = sizeof(struct ecc_ctx),
+	},
+};
+
 static int ecc_init(void)
 {
-	return crypto_register_akcipher(&ecc_nist_p256);
+	int ret;
+
+	ret = crypto_register_akcipher(&ecc_nist_p256);
+	if (ret)
+		return ret;
+
+	return crypto_register_akcipher(&ecc_nist_p192);
 }
 
 static void ecc_exit(void)
 {
+	crypto_unregister_akcipher(&ecc_nist_p192);
 	crypto_unregister_akcipher(&ecc_nist_p256);
 }
 
include/linux/oid_registry.h +1 -0
index 9060f19c80eb..e8071133d0e2 100644
@@ -21,6 +21,7 @@ enum OID {
 	OID_id_dsa,			/* 1.2.840.10040.4.1 */
 	OID_id_ecdsa_with_sha1,		/* 1.2.840.10045.4.1 */
 	OID_id_ecPublicKey,		/* 1.2.840.10045.2.1 */
+	OID_id_prime192v1,		/* 1.2.840.10045.3.1.1 */
 	OID_id_prime256v1,		/* 1.2.840.10045.3.1.7 */
 	OID_id_ecdsa_with_sha224,	/* 1.2.840.10045.4.3.1 */
 	OID_id_ecdsa_with_sha256,	/* 1.2.840.10045.4.3.2 */
Signature · 1 line
2.25.4
#3 lore raw

PATCH 3/3 x509: Detect sm2 keys by their parameters OID

In reply to Stefan Berger

From: Stefan Berger <[email protected]>

Detect whether a key is a sm2 type of key by its OID in the parameters array.

---
 crypto/asymmetric_keys/x509_cert_parser.c | 27 +++++++++++++----------
 1 file changed, 15 insertions(+), 12 deletions(-)
crypto/asymmetric_keys/x509_cert_parser.c +15 -12
index 720cc7977077..82e331da5041 100644
@@ -485,6 +485,7 @@ int x509_extract_key_data(void *context, size_t hdrlen,
 			  const void *value, size_t vlen)
 {
 	struct x509_parse_context *ctx = context;
+	enum OID oid;
 
 	ctx->key_algo = ctx->last_oid;
 	switch (ctx->last_oid) {
@@ -496,18 +497,20 @@ int x509_extract_key_data(void *context, size_t hdrlen,
 		ctx->cert->pub->pkey_algo = "ecrdsa";
 		break;
 	case OID_id_ecPublicKey:
-		ctx->cert->pub->pkey_algo = "sm2";
-		if (ctx->params_size > 2) {
-			enum OID oid = look_up_OID(ctx->params + 2,
-						   ctx->params_size - 2);
-			switch (oid) {
-			case OID_id_prime192v1:
-			case OID_id_prime256v1:
-				ctx->cert->pub->pkey_algo = "ecdsa";
-				break;
-			default:
-				break;
-			}
+		if (ctx->params_size < 2)
+			return -ENOPKG;
+
+		oid = look_up_OID(ctx->params + 2, ctx->params_size - 2);
+		switch (oid) {
+		case OID_id_prime192v1:
+		case OID_id_prime256v1:
+			ctx->cert->pub->pkey_algo = "ecdsa";
+			break;
+		case OID_sm2:
+			ctx->cert->pub->pkey_algo = "sm2";
+			break;
+		default:
+			return -ENOPKG;
 		}
 		break;
 	default:
Signature · 1 line
2.25.4