Skip to contentSkip to conversation

Add support for x509 certs with NIST p256 and p192 keys

PATCH Superseded Stefan Berger 6 participants 17 messages Feb 1, 2021 – Feb 11, 2021

Subsystems (10)

v7 of 8 Superseded Latest is v8 →

lore
Share & tools
lore

Stefan Berger

author reviewer
#0 lore raw

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

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

v6->v7:
- Moved some OID defintions to patch 1 for bisectability
- Applied R-b's

v5->v6:
- moved ecdsa code into its own module ecdsa_generic built from ecdsa.c
- added script-generated test vectors for NIST P256 & P192 and all hashes
- parsing of OID that contain header with new parse_oid()

v4->v5:
- registering crypto support under names ecdsa-nist-p256/p192 following Hubert Xu's suggestion in other thread
- appended IMA ECDSA support patch

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 (4):
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
ima: Support EC keys for signature verification

 crypto/Kconfig                            |  10 +
 crypto/Makefile                           |   6 +
 crypto/asymmetric_keys/public_key.c       |  19 ++
 crypto/asymmetric_keys/x509_cert_parser.c |  44 ++-
 crypto/ecc.c                              |  13 +-
 crypto/ecc.h                              |  28 ++
 crypto/ecdsa.c                            | 361 ++++++++++++++++++++++
 crypto/ecdsasignature.asn1                |   4 +
 crypto/testmgr.c                          |  12 +
 crypto/testmgr.h                          | 267 ++++++++++++++++
 include/keys/asymmetric-type.h            |   6 +
 include/linux/oid_registry.h              |   7 +
 lib/oid_registry.c                        |  13 +
 security/integrity/digsig_asymmetric.c    |  29 +-
 14 files changed, 790 insertions(+), 29 deletions(-)
 create mode 100644 crypto/ecdsa.c
 create mode 100644 crypto/ecdsasignature.asn1
Signature · 1 line
2.29.2
author reviewer
#1 lore raw

PATCH v7 1/4 crypto: Add support for ECDSA signature verification

Add support for parsing the parameters of a NIST P256 or NIST P192 key. Enable signature verification using these keys. The new module is enabled with CONFIG_ECDSA:
Elliptic Curve Digital Signature Algorithm (NIST P192, P256 etc.) is A NIST cryptographic standard algorithm. Only signature verification is implemented.

---
 crypto/Kconfig               |  10 +
 crypto/Makefile              |   6 +
 crypto/ecc.c                 |  13 +-
 crypto/ecc.h                 |  28 +++
 crypto/ecdsa.c               | 361 +++++++++++++++++++++++++++++++++++
 crypto/ecdsasignature.asn1   |   4 +
 crypto/testmgr.c             |  12 ++
 crypto/testmgr.h             | 267 ++++++++++++++++++++++++++
 include/linux/oid_registry.h |   4 +
 9 files changed, 694 insertions(+), 11 deletions(-)
 create mode 100644 crypto/ecdsa.c
 create mode 100644 crypto/ecdsasignature.asn1
crypto/Kconfig +10 -0
index 094ef56ab7b4..152a4ee54fc6 100644
@@ -247,6 +247,16 @@ config CRYPTO_ECDH
 	help
 	  Generic implementation of the ECDH algorithm
 
+config CRYPTO_ECDSA
+	tristate "ECDSA (NIST P192, P256 etc.) algorithm"
+	select CRYPTO_ECC
+	select CRYPTO_AKCIPHER
+	select ASN1
+	help
+	  Elliptic Curve Digital Signature Algorithm (NIST P192, P256 etc.)
+	  is A NIST cryptographic standard algorithm. Only signature verification
+	  is implemented.
+
 config CRYPTO_ECRDSA
 	tristate "EC-RDSA (GOST 34.10) algorithm"
 	select CRYPTO_ECC
crypto/Makefile +6 -0
index b279483fba50..982066c6bdfb 100644
@@ -50,6 +50,12 @@ 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)/ecdsa.o: $(obj)/ecdsasignature.asn1.h
+ecdsa_generic-y += ecdsa.o
+ecdsa_generic-y += ecdsasignature.asn1.o
+obj-$(CONFIG_CRYPTO_ECDSA) += ecdsa_generic.o
+
 crypto_acompress-y := acompress.o
 crypto_acompress-y += scompress.o
 obj-$(CONFIG_CRYPTO_ACOMP2) += crypto_acompress.o
crypto/ecc.c +2 -11
index c80aa25994a0..25e79fd70566 100644
@@ -42,7 +42,7 @@ typedef struct {
 	u64 m_high;
 } uint128_t;
 
-static inline const struct ecc_curve *ecc_get_curve(unsigned int curve_id)
+const struct ecc_curve *ecc_get_curve(unsigned int curve_id)
 {
 	switch (curve_id) {
 	/* In FIPS mode only allow P256 and higher */
@@ -54,6 +54,7 @@ static inline const struct ecc_curve *ecc_get_curve(unsigned int curve_id)
 		return NULL;
 	}
 }
+EXPORT_SYMBOL(ecc_get_curve);
 
 static u64 *ecc_alloc_digits_space(unsigned int ndigits)
 {
@@ -1281,16 +1282,6 @@ void ecc_point_mult_shamir(const struct ecc_point *result,
 }
 EXPORT_SYMBOL(ecc_point_mult_shamir);
 
-static inline void ecc_swap_digits(const u64 *in, u64 *out,
-				   unsigned int ndigits)
-{
-	const __be64 *src = (__force __be64 *)in;
-	int i;
-
-	for (i = 0; i < ndigits; i++)
-		out[i] = be64_to_cpu(src[ndigits - 1 - i]);
-}
-
 static int __ecc_is_key_valid(const struct ecc_curve *curve,
 			      const u64 *private_key, unsigned int ndigits)
 {
crypto/ecc.h +28 -0
index d4e546b9ad79..2ea86dfb5cf7 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
  *
@@ -70,6 +72,32 @@ struct ecc_curve {
 	u64 *b;
 };
 
+/**
+ * ecc_swap_digits() - Copy ndigits from big endian array to native array
+ *
+ * @in:       input array
+ * @out:      output array
+ * @ndigits:  number of digits to copy
+ */
+static inline void ecc_swap_digits(const u64 *in, u64 *out,
+				   unsigned int ndigits)
+{
+	const __be64 *src = (__force __be64 *)in;
+	int i;
+
+	for (i = 0; i < ndigits; i++)
+		out[i] = be64_to_cpu(src[ndigits - 1 - i]);
+}
+
+/**
+ * ecc_get_curve()  - Get a curve given its curve_id
+ *
+ * @curve_id:  Id of the curve
+ *
+ * Returns pointer to the curve data, NULL if curve is not available
+ */
+const struct ecc_curve *ecc_get_curve(unsigned int curve_id);
+
 /**
  * ecc_is_key_valid() - Validate a given ECDH private key
  *
crypto/ecdsa.c new file +361 -0

361 lines not on this page · raw message

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 })
+}
crypto/testmgr.c +12 -0
index a64a639eddfa..7303d82efac6 100644
@@ -4926,6 +4926,18 @@ static const struct alg_test_desc alg_test_descs[] = {
 		.suite = {
 			.kpp = __VECS(ecdh_tv_template)
 		}
+	}, {
+		.alg = "ecdsa-nist-p192",
+		.test = alg_test_akcipher,
+		.suite = {
+			.akcipher = __VECS(ecdsa_nist_p192_tv_template)
+		}
+	}, {
+		.alg = "ecdsa-nist-p256",
+		.test = alg_test_akcipher,
+		.suite = {
+			.akcipher = __VECS(ecdsa_nist_p256_tv_template)
+		}
 	}, {
 		.alg = "ecrdsa",
 		.test = alg_test_akcipher,
crypto/testmgr.h +267 -0

273 lines not on this page · raw message

include/linux/oid_registry.h +4 -0
index 4462ed2c18cd..59ad6109f918 100644
@@ -21,6 +21,10 @@ 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_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.29.2
maintainer
#2 lore raw

Re: [PATCH v7 1/4] crypto: Add support for ECDSA signature verification

Quoted 11 lines · Stefan Berger

Add support for parsing the parameters of a NIST P256 or NIST P192 key. Enable signature verification using these keys. The new module is enabled with CONFIG_ECDSA:
Elliptic Curve Digital Signature Algorithm (NIST P192, P256 etc.) is A NIST cryptographic standard algorithm. Only signature verification is implemented.

Quoted 13 lines
---
 crypto/Kconfig               |  10 +
 crypto/Makefile              |   6 +
 crypto/ecc.c                 |  13 +-
 crypto/ecc.h                 |  28 +++
 crypto/ecdsa.c               | 361 +++++++++++++++++++++++++++++++++++
 crypto/ecdsasignature.asn1   |   4 +
 crypto/testmgr.c             |  12 ++
 crypto/testmgr.h             | 267 ++++++++++++++++++++++++++
 include/linux/oid_registry.h |   4 +
 9 files changed, 694 insertions(+), 11 deletions(-)
 create mode 100644 crypto/ecdsa.c
 create mode 100644 crypto/ecdsasignature.asn1

Saulo Alessandre is implementing ecdsa with signing so you two should coordinate on this.

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
author reviewer
#3 lore raw

Re: [PATCH v7 1/4] crypto: Add support for ECDSA signature verification

Quoted 25 lines · Herbert Xu
Quoted 22 lines · Stefan Berger

Add support for parsing the parameters of a NIST P256 or NIST P192 key. Enable signature verification using these keys. The new module is enabled with CONFIG_ECDSA:
Elliptic Curve Digital Signature Algorithm (NIST P192, P256 etc.) is A NIST cryptographic standard algorithm. Only signature verification is implemented.

---
  crypto/Kconfig               |  10 +
  crypto/Makefile              |   6 +
  crypto/ecc.c                 |  13 +-
  crypto/ecc.h                 |  28 +++
  crypto/ecdsa.c               | 361 +++++++++++++++++++++++++++++++++++
  crypto/ecdsasignature.asn1   |   4 +
  crypto/testmgr.c             |  12 ++
  crypto/testmgr.h             | 267 ++++++++++++++++++++++++++
  include/linux/oid_registry.h |   4 +
  9 files changed, 694 insertions(+), 11 deletions(-)
Quoted 2 lines

create mode 100644 crypto/ecdsa.c
create mode 100644 crypto/ecdsasignature.asn1

Quoted 2 lines

Saulo Alessandre is implementing ecdsa with signing so you two should coordinate on this.

Hello Saulo,

 so this series here supports NIST P256 and NIST P192 for usage by IMA for example. It looks like you want to support more elliptic curves than these: p384 and even p521. Do you have any suggestion on how to proceed? Would you want to add patches with your additional curves on top of this series?

I have a project here with some test scripts that may also be relevant for your case: https://github.com/stefanberger/eckey-testing

    Stefan

Quoted 2 lines

Thanks,

maintainer Acked-by
#4 lore raw

Re: [PATCH v7 1/4] crypto: Add support for ECDSA signature verification

In reply to Herbert Xu
Quoted 17 lines · Herbert Xu
Quoted 16 lines · Stefan Berger

Add support for parsing the parameters of a NIST P256 or NIST P192 key. Enable signature verification using these keys. The new module is enabled with CONFIG_ECDSA:
Elliptic Curve Digital Signature Algorithm (NIST P192, P256 etc.) is A NIST cryptographic standard algorithm. Only signature verification is implemented.

---
 crypto/Kconfig               |  10 +
 crypto/Makefile              |   6 +
 crypto/ecc.c                 |  13 +-
 crypto/ecc.h                 |  28 +++
Quoted 8 lines
Quoted 8 lines
 crypto/ecdsa.c               | 361 +++++++++++++++++++++++++++++++++++
 crypto/ecdsasignature.asn1   |   4 +
 crypto/testmgr.c             |  12 ++
 crypto/testmgr.h             | 267 ++++++++++++++++++++++++++
 include/linux/oid_registry.h |   4 +
 9 files changed, 694 insertions(+), 11 deletions(-)
 create mode 100644 crypto/ecdsa.c
 create mode 100644 crypto/ecdsasignature.asn1

Great, ECDSA has been lacking for a way too long. Just wanted to acknowledge support for this, I just now also skimmed the change from patchwrok (way too quickly for reviewed-by but well enough for ack).

/Jarkko

author reviewer
#5 lore raw

PATCH v7 2/4 x509: Detect sm2 keys by their parameters OID

In reply to Stefan Berger

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 | 12 +++++++++++-
 include/linux/oid_registry.h              |  1 +
 lib/oid_registry.c                        | 13 +++++++++++++
 3 files changed, 25 insertions(+), 1 deletion(-)
crypto/asymmetric_keys/x509_cert_parser.c +11 -1
index 52c9b455fc7d..1621ceaf5c95 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,16 @@ 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 (parse_OID(ctx->params, ctx->params_size, &oid) != 0)
+			return -EBADMSG;
+
+		switch (oid) {
+		case OID_sm2:
+			ctx->cert->pub->pkey_algo = "sm2";
+			break;
+		default:
+			return -ENOPKG;
+		}
 		break;
 	default:
 		return -ENOPKG;
include/linux/oid_registry.h +1 -0
index 59ad6109f918..f3b2c097c886 100644
@@ -121,6 +121,7 @@ enum OID {
 };
 
 extern enum OID look_up_OID(const void *data, size_t datasize);
+extern int parse_OID(const void *data, size_t datasize, enum OID *oid);
 extern int sprint_oid(const void *, size_t, char *, size_t);
 extern int sprint_OID(enum OID, char *, size_t);
 
lib/oid_registry.c +13 -0
index f7ad43f28579..508e0b34b5f0 100644
@@ -11,6 +11,7 @@
 #include <linux/kernel.h>
 #include <linux/errno.h>
 #include <linux/bug.h>
+#include <linux/asn1.h>
 #include "oid_registry_data.c"
 
 MODULE_DESCRIPTION("OID Registry");
@@ -92,6 +93,18 @@ enum OID look_up_OID(const void *data, size_t datasize)
 }
 EXPORT_SYMBOL_GPL(look_up_OID);
 
+int parse_OID(const void *data, size_t datasize, enum OID *oid)
+{
+	const unsigned char *v = data;
+
+	if (datasize < 2 || v[0] != ASN1_OID || v[1] != datasize - 2)
+		return -EBADMSG;
+
+	*oid = look_up_OID(data + 2, datasize - 2);
+	return 0;
+}
+EXPORT_SYMBOL_GPL(parse_OID);
+
 /*
  * sprint_OID - Print an Object Identifier into a buffer
  * @data: The encoded OID to print
Signature · 1 line
2.29.2
author reviewer
#6 lore raw

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

In reply to Stefan Berger

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       | 19 ++++++++++++++
 crypto/asymmetric_keys/x509_cert_parser.c | 32 ++++++++++++++++++++++-
 include/linux/oid_registry.h              |  2 ++
 3 files changed, 52 insertions(+), 1 deletion(-)
crypto/asymmetric_keys/public_key.c +19 -0
index 8892908ad58c..7dae61b79d5a 100644
@@ -14,6 +14,7 @@
 #include <linux/slab.h>
 #include <linux/seq_file.h>
 #include <linux/scatterlist.h>
+#include <linux/asn1.h>
 #include <keys/asymmetric-subtype.h>
 #include <crypto/public_key.h>
 #include <crypto/akcipher.h>
@@ -90,6 +91,24 @@ int software_key_determine_akcipher(const char *encoding,
 		return 0;
 	}
 
+	if (strcmp(encoding, "x962") == 0) {
+		enum OID oid;
+
+		if (parse_OID(pkey->params, pkey->paramlen, &oid) != 0)
+			return -EBADMSG;
+
+		switch (oid) {
+		case OID_id_prime192v1:
+			strcpy(alg_name, "ecdsa-nist-p192");
+			return 0;
+		case OID_id_prime256v1:
+			strcpy(alg_name, "ecdsa-nist-p256");
+			return 0;
+		default:
+			return -EINVAL;
+		}
+	}
+
 	return -ENOPKG;
 }
 
crypto/asymmetric_keys/x509_cert_parser.c +31 -1
index 1621ceaf5c95..0aff4e584b11 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;
@@ -478,6 +504,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 +2 -0
index f3b2c097c886..ff3cad9f8c1f 100644
@@ -21,6 +21,8 @@ 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 */
Signature · 1 line
2.29.2
#7 lore raw

Re: [PATCH v7 3/4] x509: Add support for parsing x509 certs with ECDSA keys

Thank you for the patch! Yet something to improve:

Hi Stefan,

Thank you for the patch! Yet something to improve:

[auto build test ERROR on cryptodev/master]
[also build test ERROR on crypto/master security/next-testing v5.11-rc7 next-20210125] [If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch]

url:    https://github.com/0day-ci/linux/commits/Stefan-Berger/Add-support-for-x509-certs-with-NIST-p256-and-p192-keys/20210201-232803
base:   https://git.kernel.org/pub/scm/linux/kernel/git/herbert/cryptodev-2.6.git master
config: x86_64-randconfig-a011-20200911 (attached as .config)
compiler: gcc-9 (Debian 9.3.0-15) 9.3.0
reproduce (this is a W=1 build):
        # https://github.com/0day-ci/linux/commit/6e1523b0e77785c263bcb639b87a862ae59731a4
        git remote add linux-review https://github.com/0day-ci/linux
        git fetch --no-tags linux-review Stefan-Berger/Add-support-for-x509-certs-with-NIST-p256-and-p192-keys/20210201-232803
        git checkout 6e1523b0e77785c263bcb639b87a862ae59731a4
        # save the attached .config to linux build tree
        make W=1 ARCH=x86_64 

If you fix the issue, kindly add following tag as appropriate

All errors (new ones prefixed by >>):

ld: crypto/asymmetric_keys/public_key.o: in function `software_key_determine_akcipher':

>> crypto/asymmetric_keys/public_key.c:97: undefined reference to `parse_OID'

vim +97 crypto/asymmetric_keys/public_key.c

    61	
    62	/*
    63	 * Determine the crypto algorithm name.
    64	 */
    65	static
    66	int software_key_determine_akcipher(const char *encoding,
    67					    const char *hash_algo,
    68					    const struct public_key *pkey,
    69					    char alg_name[CRYPTO_MAX_ALG_NAME])
    70	{
    71		int n;
    72	
    73		if (strcmp(encoding, "pkcs1") == 0) {
    74			/* The data wangled by the RSA algorithm is typically padded
    75			 * and encoded in some manner, such as EMSA-PKCS1-1_5 [RFC3447
    76			 * sec 8.2].
    77			 */
    78			if (!hash_algo)
    79				n = snprintf(alg_name, CRYPTO_MAX_ALG_NAME,
    80					     "pkcs1pad(%s)",
    81					     pkey->pkey_algo);
    82			else
    83				n = snprintf(alg_name, CRYPTO_MAX_ALG_NAME,
    84					     "pkcs1pad(%s,%s)",
    85					     pkey->pkey_algo, hash_algo);
    86			return n >= CRYPTO_MAX_ALG_NAME ? -EINVAL : 0;
    87		}
    88	
    89		if (strcmp(encoding, "raw") == 0) {
    90			strcpy(alg_name, pkey->pkey_algo);
    91			return 0;
    92		}
    93	
    94		if (strcmp(encoding, "x962") == 0) {
    95			enum OID oid;
    96	
  > 97			if (parse_OID(pkey->params, pkey->paramlen, &oid) != 0)
    98				return -EBADMSG;
    99	

100 switch (oid) {
101 case OID_id_prime192v1:
102 strcpy(alg_name, "ecdsa-nist-p192");
103 return 0;
104 case OID_id_prime256v1:
105 strcpy(alg_name, "ecdsa-nist-p256");
106 return 0;
107 default:
108 return -EINVAL;
109 }
110 }
111
112 return -ENOPKG;
113 }
114

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org

author reviewer
#8 lore raw

Re: [PATCH v7 3/4] x509: Add support for parsing x509 certs with ECDSA keys

Quoted 5 lines · kernel test robot

Hi Stefan,

Thank you for the patch! Yet something to improve:

Quoted 1 line
Quoted 1 line

crypto/asymmetric_keys/public_key.c:97: undefined reference to `parse_OID'

So the issue is that  only ASYMMETRIC_PUBLIC_KEY_SUBTYPE is selected in this config and the selection of OID_REGISTRY is missing. I am not sure whether ASYMMETRIC_PUBLIC_KEY_SUBTYPE should/could select OID_REGISTRY or whether that would be wrong... ?

    Stefan

author reviewer
#9 lore raw

Re: [PATCH v7 3/4] x509: Add support for parsing x509 certs with ECDSA keys

Quoted 9 lines · Stefan Berger
Quoted 5 lines · kernel test robot

Hi Stefan,

Thank you for the patch! Yet something to improve:

Quoted 1 line
Quoted 1 line

crypto/asymmetric_keys/public_key.c:97: undefined reference to `parse_OID'

So the issue is that  only ASYMMETRIC_PUBLIC_KEY_SUBTYPE is selected in this config and the selection of OID_REGISTRY is missing. I am not sure whether ASYMMETRIC_PUBLIC_KEY_SUBTYPE should/could select OID_REGISTRY or whether that would be wrong... ?

David,

  if the above is not desired then the following change would let us get rid of the offending parse_OID(). The below change is only for NIST p192 in this experiment but shows that we need to add additional strcmp() cases in x509_check_for_self_signed() since cert->sig->pkey_algo is set to "ecdsa". I am not sure whether we should derive from the signature which curve was used to create the signature so that cert->sig->pkey_algo could be more specific and the simple existing strcmp() would pass. So two possible ways to go forward. Which way should we go?

   Stefan

crypto/asymmetric_keys/x509_cert_parser.c +0 -0
index 0aff4e584b11..71d83bb345c4 100644
@@ -505,6 +505,8 @@ int x509_extract_key_data(void *context, size_t hdrlen,

                        ctx->cert->pub->pkey_algo = "sm2"; break;
                case OID_id_prime192v1:
+                       ctx->cert->pub->pkey_algo = "ecdsa-nist-p192"; +                       break;
                case OID_id_prime256v1:
                        ctx->cert->pub->pkey_algo = "ecdsa"; break;

crypto/asymmetric_keys/x509_public_key.c +0 -0
index ae450eb8be14..3ebeed195b61 100644
@@ -129,7 +129,10 @@ int x509_check_for_self_signed(struct x509_certificate *cert)

        }

        ret = -EKEYREJECTED;
-       if (strcmp(cert->pub->pkey_algo, cert->sig->pkey_algo) != 0) +printk(KERN_INFO "%s: %s ==? %s\n", __func__, cert->pub->pkey_algo, cert->sig->pkey_algo); +       if (strcmp(cert->pub->pkey_algo, cert->sig->pkey_algo) != 0 &&
+           strncmp(cert->pub->pkey_algo, "ecdsa-nist-p", 12) != 0 &&
+           strcmp(cert->sig->pkey_algo, "ecdsa") != 0)
                goto out;

        ret = public_key_verify_signature(cert->pub, cert->sig);

Quoted 4 lines

    Stefan

author reviewer
#10 lore raw

PATCH v7 4/4 ima: Support EC keys for signature verification

In reply to Stefan Berger

Add support for IMA signature verification for EC keys. Since SHA type of hashes can be used by RSA and ECDSA signature schemes we need to look at the key and derive from the key which signature scheme to use. Since this can be applied to all types of keys, we change the selection of the encoding type to be driven by the key's signature scheme rather than by the hash type.

---
 include/keys/asymmetric-type.h         |  6 ++++++
 security/integrity/digsig_asymmetric.c | 29 ++++++++++++--------------
 2 files changed, 19 insertions(+), 16 deletions(-)
include/keys/asymmetric-type.h +6 -0
index a29d3ff2e7e8..c432fdb8547f 100644
@@ -72,6 +72,12 @@ const struct asymmetric_key_ids *asymmetric_key_ids(const struct key *key)
 	return key->payload.data[asym_key_ids];
 }
 
+static inline
+const struct public_key *asymmetric_key_public_key(const struct key *key)
+{
+	return key->payload.data[asym_crypto];
+}
+
 extern struct key *find_asymmetric_key(struct key *keyring,
 				       const struct asymmetric_key_id *id_0,
 				       const struct asymmetric_key_id *id_1,
security/integrity/digsig_asymmetric.c +13 -16
index a662024b4c70..29002d016607 100644
@@ -84,6 +84,7 @@ int asymmetric_verify(struct key *keyring, const char *sig,
 {
 	struct public_key_signature pks;
 	struct signature_v2_hdr *hdr = (struct signature_v2_hdr *)sig;
+	const struct public_key *pk;
 	struct key *key;
 	int ret;
 
@@ -105,23 +106,19 @@ int asymmetric_verify(struct key *keyring, const char *sig,
 	memset(&pks, 0, sizeof(pks));
 
 	pks.hash_algo = hash_algo_name[hdr->hash_algo];
-	switch (hdr->hash_algo) {
-	case HASH_ALGO_STREEBOG_256:
-	case HASH_ALGO_STREEBOG_512:
-		/* EC-RDSA and Streebog should go together. */
-		pks.pkey_algo = "ecrdsa";
-		pks.encoding = "raw";
-		break;
-	case HASH_ALGO_SM3_256:
-		/* SM2 and SM3 should go together. */
-		pks.pkey_algo = "sm2";
-		pks.encoding = "raw";
-		break;
-	default:
-		pks.pkey_algo = "rsa";
+
+	pk = asymmetric_key_public_key(key);
+	pks.pkey_algo = pk->pkey_algo;
+	if (!strcmp(pk->pkey_algo, "rsa"))
 		pks.encoding = "pkcs1";
-		break;
-	}
+	else if (!strcmp(pk->pkey_algo, "ecdsa"))
+		pks.encoding = "x962";
+	else if (!strcmp(pk->pkey_algo, "ecrdsa") ||
+		   !strcmp(pk->pkey_algo, "sm2"))
+		pks.encoding = "raw";
+	else
+		return -ENOPKG;
+
 	pks.digest = (u8 *)data;
 	pks.digest_size = datalen;
 	pks.s = hdr->sig;
Signature · 1 line
2.29.2
maintainer Acked-by on v7 4/4
#11 lore raw

Re: [PATCH v7 4/4] ima: Support EC keys for signature verification

Quoted 10 lines · Stefan Berger

Add support for IMA signature verification for EC keys. Since SHA type of hashes can be used by RSA and ECDSA signature schemes we need to look at the key and derive from the key which signature scheme to use. Since this can be applied to all types of keys, we change the selection of the encoding type to be driven by the key's signature scheme rather than by the hash type.

Thanks, Stefan!

maintainer
#12 lore raw
In reply to Stefan Berger
Quoted 3 lines · Stefan Berger

v6->v7:
- Moved some OID defintions to patch 1 for bisectability
- Applied R-b's

But I can't now apply 2-4 without patch 1.

David

author reviewer
#13 lore raw
Quoted 6 lines · David Howells
Quoted 3 lines · Stefan Berger

v6->v7:
- Moved some OID defintions to patch 1 for bisectability
- Applied R-b's

But I can't now apply 2-4 without patch 1.

Two possible solutions:

1) the whole series goes through the crypto tree

2) I make the OIDs addition patch 1 that both keyrings and crypto take separately?

  Stefan

maintainer
#14 lore raw
Quoted 4 lines · Stefan Berger

1) the whole series goes through the crypto tree

2) I make the OIDs addition patch 1 that both keyrings and crypto take separately?

The first might be easiest, but 2 is okay also. You'll just need to give myself and Herbert separate branches to pull, rooted on the same commit.

Btw, what do patches 2-4 do if patch 1 isn't applied?

David

author reviewer
#15 lore raw
Quoted 10 lines · David Howells
Quoted 4 lines · Stefan Berger

1) the whole series goes through the crypto tree

2) I make the OIDs addition patch 1 that both keyrings and crypto take separately?

The first might be easiest, but 2 is okay also. You'll just need to give myself and Herbert separate branches to pull, rooted on the same commit.

Btw, what do patches 2-4 do if patch 1 isn't applied?

With the crypto module missing in the kernel you will get an error trying to load an x509 certificate that needs the algorithm to verify the self-signed signature.

Before I post yet another series I hope that Herbert can say whether option 1) would work for him.

   Stefan

Quoted 4 lines

David

maintainer
#16 lore raw
Quoted 7 lines · Stefan Berger

With the crypto module missing in the kernel you will get an error trying to load an x509 certificate that needs the algorithm to verify the self-signed signature.

Before I post yet another series I hope that Herbert can say whether option
1) would work for him.

Please be patient. We need to make sure that whatever scheme you use for your algorithm also works for the driver authors who are working in the same area. Because if we end up having to change the scheme then that'll just create more churn for you and David.

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