Skip to contentSkip to conversation

Add support for x509 certs with NIST p256 and p192 keys

PATCH Superseded Stefan Berger 4 participants 9 messages Jan 28, 2021 – Jan 29, 2021

Subsystems (4)

v4 of 11 Superseded Latest is v11 →

lore
Share & tools
lore
#0 lore raw

v4 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:

k=$(keyctl 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 $k < 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. Scripts for testing are here:

https://github.com/stefanberger/eckey-testing

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

v3->v4:
- split off of ecdsa crypto part; registering akcipher as "ecdsa" and deriving used curve from digits in parsed key

v2->v3:
- patch 2 now includes linux/scatterlist.h

v1->v2:
- using faster vli_sub rather than newly added vli_mod_fast to 'reduce' result
- rearranged switch statements to follow after RSA
- 3rd patch from 1st posting is now 1st patch

Stefan Berger (3):
crypto: Add support for ECDSA signature verification
x509: Detect sm2 keys by their parameters OID
x509: Add support for parsing x509 certs with ECDSA keys

 crypto/Makefile                           |   9 +-
 crypto/asymmetric_keys/public_key.c       |  17 ++
 crypto/asymmetric_keys/x509_cert_parser.c |  45 +++-
 crypto/ecc.c                              | 279 ++++++++++++++++++++++
 crypto/ecc.h                              |   2 +
 crypto/ecdsasignature.asn1                |   4 +
 include/linux/oid_registry.h              |   6 +
 7 files changed, 359 insertions(+), 3 deletions(-)
 create mode 100644 crypto/ecdsasignature.asn1
Signature · 1 line
2.26.2
#1 lore raw

PATCH v4 1/3 crypto: Add support for ECDSA signature verification

From: Stefan Berger <[email protected]>

Add support for parsing the parameters of a NIST P256 or NIST P192 key. Enable signature verification using these keys.

---
 crypto/Makefile            |   9 +-
 crypto/ecc.c               | 279 +++++++++++++++++++++++++++++++++++++
 crypto/ecc.h               |   2 +
 crypto/ecdsasignature.asn1 |   4 +
 4 files changed, 293 insertions(+), 1 deletion(-)
 create mode 100644 crypto/ecdsasignature.asn1
crypto/Makefile +8 -1
index b279483fba50..767f6b71aa20 100644
@@ -50,6 +50,14 @@ sm2_generic-y += sm2.o
 
 obj-$(CONFIG_CRYPTO_SM2) += sm2_generic.o
 
+$(obj)/ecdsasignature.asn1.o: $(obj)/ecdsasignature.asn1.c $(obj)/ecdsasignature.asn1.h
+$(obj)/ecc.o: $(obj)/ecdsasignature.asn1.h
+
+ecc_generic-y += ecdsasignature.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/ecc.c +279 -0

296 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/ecdsasignature.asn1 new file +4 -0
new file mode 100644
index 000000000000..621ab754fb9f
@@ -0,0 +1,4 @@
+ECDSASignature ::= SEQUENCE {
+	r	INTEGER ({ ecdsa_get_signature_r }),
+	s	INTEGER ({ ecdsa_get_signature_s })
+}
Signature · 1 line
2.26.2
maintainer
#2 lore raw

Re: [PATCH v4 1/3] crypto: Add support for ECDSA signature verification

Quoted 9 lines · Stefan Berger

From: Stefan Berger <[email protected]>

Add support for parsing the parameters of a NIST P256 or NIST P192 key. Enable signature verification using these keys.

Quoted 7 lines
---
 crypto/Makefile            |   9 +-
 crypto/ecc.c               | 279 +++++++++++++++++++++++++++++++++++++
 crypto/ecc.h               |   2 +
 crypto/ecdsasignature.asn1 |   4 +
 4 files changed, 293 insertions(+), 1 deletion(-)
 create mode 100644 crypto/ecdsasignature.asn1

Hi Stefan:

I forgot to cc you on the other thread at

https://lore.kernel.org/linux-crypto/[email protected]/T/#u

Whatever we decide there would be applicable to your scenario too. Please join the discussion on that thread.

Thanks,

Signature · 3 lines
Email: Herbert Xu <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
#3 lore raw

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

In reply to Stefan Berger

From: Stefan Berger <[email protected]>

Detect whether a key is an sm2 type of key by its OID in the parameters array rather than assuming that everything under OID_id_ecPublicKey is sm2, which is not the case.

---
 crypto/asymmetric_keys/x509_cert_parser.c | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)
crypto/asymmetric_keys/x509_cert_parser.c +12 -1
index 52c9b455fc7d..4643fe5ed69a 100644
@@ -459,6 +459,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) {
@@ -470,7 +471,17 @@ 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)
+			return -ENOPKG;
+
+		oid = look_up_OID(ctx->params + 2, ctx->params_size - 2);
+		switch (oid) {
+		case OID_sm2:
+			ctx->cert->pub->pkey_algo = "sm2";
+			break;
+		default:
+			return -ENOPKG;
+		}
 		break;
 	default:
 		return -ENOPKG;
Signature · 1 line
2.26.2
#4 lore raw

PATCH v4 3/3 x509: Add support for parsing x509 certs with ECDSA keys

In reply to Stefan Berger

From: Stefan Berger <[email protected]>

This patch adds support for parsing of x509 certificates that contain ECDSA keys, such as NIST P256, that have been signed by a CA using any of the current SHA hash algorithms.

---
 crypto/asymmetric_keys/public_key.c       | 17 ++++++++++++
 crypto/asymmetric_keys/x509_cert_parser.c | 32 ++++++++++++++++++++++-
 include/linux/oid_registry.h              |  6 +++++
 3 files changed, 54 insertions(+), 1 deletion(-)
crypto/asymmetric_keys/public_key.c +17 -0
index 8892908ad58c..cc5d59d2b35d 100644
@@ -90,6 +90,23 @@ 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_prime192v1:
+		case OID_id_prime256v1:
+			strcpy(alg_name, "ecdsa");
+			return 0;
+		default:
+			return -EINVAL;
+		}
+	}
+
 	return -ENOPKG;
 }
 
crypto/asymmetric_keys/x509_cert_parser.c +31 -1
index 4643fe5ed69a..5ff891f8235d 100644
@@ -227,6 +227,26 @@ int x509_note_pkey_algo(void *context, size_t hdrlen,
 		ctx->cert->sig->hash_algo = "sha224";
 		goto rsa_pkcs1;
 
+	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_gost2012Signature256:
 		ctx->cert->sig->hash_algo = "streebog256";
 		goto ecrdsa;
@@ -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;
@@ -479,6 +505,10 @@ int x509_extract_key_data(void *context, size_t hdrlen,
 		case OID_sm2:
 			ctx->cert->pub->pkey_algo = "sm2";
 			break;
+		case OID_id_prime192v1:
+		case OID_id_prime256v1:
+			ctx->cert->pub->pkey_algo = "ecdsa";
+			break;
 		default:
 			return -ENOPKG;
 		}
include/linux/oid_registry.h +6 -0
index 4462ed2c18cd..e8071133d0e2 100644
@@ -21,6 +21,12 @@ 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 */
+	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.26.2
reviewer
#6 lore raw
Quoted 2 lines · David Howells

This (sub)set is intended to go through the keyrings tree or is it all going through the crypto tree now?

Patch 1/3 should go through 'crypto', the other ones through 'keyrings'.

   Stefan

Quoted 3 lines

David

maintainer
#7 lore raw
Quoted 5 lines · Stefan Berger
Quoted 2 lines

This (sub)set is intended to go through the keyrings tree or is it all going through the crypto tree now?

Patch 1/3 should go through 'crypto', the other ones through 'keyrings'.

Do 2 & 3 depend on 1?

David

reviewer
#8 lore raw
Quoted 7 lines · David Howells
Quoted 4 lines · Stefan Berger
Quoted 2 lines

This (sub)set is intended to go through the keyrings tree or is it all going through the crypto tree now?

Patch 1/3 should go through 'crypto', the other ones through 'keyrings'.

Do 2 & 3 depend on 1?

Functionality-wise, yes, compilation-wise, no.

   Stefan