Skip to contentSkip to conversation

import x509/pkcs7 parsers from linux

PATCH Superseded AKASHI Takahiro 3 participants 23 messages Oct 25, 2019 – Oct 28, 2019

Subsystems (9)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

v2 cover import x509/pkcs7 parsers from linux

# This patch set should be merged first prior to my rsa extension patch

# due to some dependency. I plan to send out a new version of rsa
# extension next week.

Asn1 parsers of x509 certificates and pkcs7 messages are required to implement image authentication and variable authentication as part of UEFI secure boot feature.

As we discussed before in the thread[1], most people insisted that we should re-use corresponding source code from Linux repository for this purpose.

Here is my attempt to import all the necessary files from Linux; Those will eventually be part of UEFI secure boot implementation, but I'd like to get early feedback from other peoples before submitting the whole patchset so that they will be better formatted for merging.

My approach here is
* files from the latest Linux
* modify files as little as possible
* mark/protect unavoidable changes with "#if(n)def __UBOOT__" so that future fixes/differences in Linux repository will easily be applied to U-Boot.

Known issues:
* checkpatch.pl
Checkpatch.pl will complain with a bunch of warnings/errors but I intentionally left them unchanged for the sake of better maintainability I said above.

* unit test
I'm still waiting for a feedback from Tom[2] regarding how we should enable "unit test" for ASN1 compiler/decoder on sandbox and others in Travis CI.

* Travis CI
It is still running against this very version, but I'm sure I have fixed all the issues (as long as Heinrich's [3] is applied); I will let you know the result on Monday.

Any comments will be appreciated.
-Takahiro Akashi

[1] https://lists.denx.de/pipermail/u-boot/2019-April/366423.html
[2] https://lists.denx.de/pipermail/u-boot/2019-October/387734.html
[3] https://lists.denx.de/pipermail/u-boot/2019-October/385643.html

Changes in v2 (Oct 25, 2019)
* revise commit messages, describing what files are modified or not.
* move kmemdump() in ubifs.c to linux_compat.c for general use (patch#1)
* add patch#2
* move date.c to lib/ for general use (patch#3)
* implement mktime64() with rtc_mktime() (patch#4)
* move asn1_compiler.c to tools/ (patch#7)
* change CONFIG_BUILD_ASN1 to CONFIG_ASN1_COMPILER (patch#7)
* add clean rule to asn1_compiler-generated files to clean targets (patch#8)
* change CONFIG_ASN1 to CONFIG_ASN1_DECODER (patch#9)
* add README for asn1 compiler/decoder (patch#10)
* move build_oid_registory to scripts/ (patch#11)
* shuffle an order of patches (patch#13,#14,#15)
* add a new config CONFIG_RSA_PUBLIC_KEY_PARSER so that it can be
* modify Kconfig dependency (patch#13,#14,#15)
compiled in independently (patch#13)
* add unit test (patch#16,#17)

Changes in v1 (Oct 11, 2019) from RFC
* change the kernel code base from v5.0 to v5.3
* add preparatory patches (#1, #2 and #3)
* comment off x509_check_for_self_signed() which is not useful for UEFI secure boot (patch#9)
* improve usages of "#if(n)def __UBOOT__* to minimize differences between U-Boot and linux kernel

AKASHI Takahiro (17):
linux_compat: move kmemdup() from ubifs.c to linux_compat.c rtc.h: add struct udevice declaration
rtc: move date.c from drivers/rtc/ to lib/
lib: add mktime64() for linux compatibility
include: kernel.h: include printk.h
linux/time.h: include vsprintf.h
cmd: add asn1_compiler
Makefile: add build script for asn1 parsers
lib: add asn1 decoder
doc: add README for asn1 compiler and decoder
lib: add oid registry utility
lib: crypto: add public key utility
lib: crypto: add rsa public key parser
lib: crypto: add x509 parser
lib: crypto: add pkcs7 message parser
test: add lib specific Kconfig
test: add asn1 unit test

 Makefile                          |    1 +
 cmd/Kconfig                       |    1 +
 doc/README.asn1                   |   40 +
 drivers/rtc/Kconfig               |    1 +
 drivers/rtc/Makefile              |    1 -
 fs/ubifs/ubifs.c                  |   19 +-
 include/crypto/internal/rsa.h     |   57 +
 include/crypto/pkcs7.h            |   47 +
 include/crypto/public_key.h       |   90 ++
 include/keys/asymmetric-type.h    |   88 ++
 include/linux/asn1.h              |   65 ++
 include/linux/asn1_ber_bytecode.h |   89 ++
 include/linux/asn1_decoder.h      |   20 +
 include/linux/kernel.h            |    2 +-
 include/linux/oid_registry.h      |  117 +++
 include/linux/time.h              |   11 +
 include/rtc.h                     |    2 +
 lib/Kconfig                       |   17 +
 lib/Makefile                      |   20 +
 lib/asn1_decoder.c                |  527 ++++++++++
 lib/crypto/Kconfig                |   52 +
 lib/crypto/Makefile               |   49 +
 lib/crypto/asymmetric_type.c      |  668 ++++++++++++
 lib/crypto/pkcs7.asn1             |  135 +++
 lib/crypto/pkcs7_parser.c         |  693 +++++++++++++
 lib/crypto/pkcs7_parser.h         |   65 ++
 lib/crypto/public_key.c           |  376 +++++++
 lib/crypto/rsa_helper.c           |  198 ++++
 lib/crypto/rsapubkey.asn1         |    4 +
 lib/crypto/x509.asn1              |   60 ++
 lib/crypto/x509_akid.asn1         |   35 +
 lib/crypto/x509_cert_parser.c     |  697 +++++++++++++
 lib/crypto/x509_parser.h          |   57 +
 lib/crypto/x509_public_key.c      |  292 ++++++
 {drivers/rtc => lib}/date.c       |   23 +-
 lib/linux_compat.c                |   17 +
 lib/oid_registry.c                |  179 ++++
 scripts/Makefile.build            |    4 +-
 scripts/build_OID_registry        |  203 ++++
 test/Kconfig                      |    8 +-
 test/lib/Kconfig                  |   23 +
 test/lib/Makefile                 |    2 +
 test/lib/asn1.c                   |  392 +++++++
 tools/Makefile                    |    3 +
 tools/asn1_compiler.c             | 1611 +++++++++++++++++++++++++++++
 45 files changed, 7030 insertions(+), 31 deletions(-)
 create mode 100644 doc/README.asn1
 create mode 100644 include/crypto/internal/rsa.h
 create mode 100644 include/crypto/pkcs7.h
 create mode 100644 include/crypto/public_key.h
 create mode 100644 include/keys/asymmetric-type.h
 create mode 100644 include/linux/asn1.h
 create mode 100644 include/linux/asn1_ber_bytecode.h
 create mode 100644 include/linux/asn1_decoder.h
 create mode 100644 include/linux/oid_registry.h
 create mode 100644 lib/asn1_decoder.c
 create mode 100644 lib/crypto/Kconfig
 create mode 100644 lib/crypto/Makefile
 create mode 100644 lib/crypto/asymmetric_type.c
 create mode 100644 lib/crypto/pkcs7.asn1
 create mode 100644 lib/crypto/pkcs7_parser.c
 create mode 100644 lib/crypto/pkcs7_parser.h
 create mode 100644 lib/crypto/public_key.c
 create mode 100644 lib/crypto/rsa_helper.c
 create mode 100644 lib/crypto/rsapubkey.asn1
 create mode 100644 lib/crypto/x509.asn1
 create mode 100644 lib/crypto/x509_akid.asn1
 create mode 100644 lib/crypto/x509_cert_parser.c
 create mode 100644 lib/crypto/x509_parser.h
 create mode 100644 lib/crypto/x509_public_key.c
 rename {drivers/rtc => lib}/date.c (81%)
 create mode 100644 lib/oid_registry.c
 create mode 100755 scripts/build_OID_registry
 create mode 100644 test/lib/Kconfig
 create mode 100644 test/lib/asn1.c
 create mode 100644 tools/asn1_compiler.c
Signature · 1 line
2.21.0
#1 lore raw

PATCH v2 1/17 linux_compat: move kmemdup() from ubifs.c to linux_compat.c

linux_compat.c is the best place for kmemdup(), which is currenly used only in ubifs.c, but will also be used when other kernel files (in my case, lib/crypto/x509_cert_parser.c and pkcs7_parser.c) will be imported. So just move it.

---
 fs/ubifs/ubifs.c   | 19 +------------------
 lib/linux_compat.c | 17 +++++++++++++++++
 2 files changed, 18 insertions(+), 18 deletions(-)
fs/ubifs/ubifs.c +1 -18
index 67a0e8caae70..1ffdfe0d9089 100644
@@ -18,6 +18,7 @@
 #include "ubifs.h"
 #include <u-boot/zlib.h>
 
+#include <linux/compat.h>
 #include <linux/err.h>
 #include <linux/lzo.h>
 
@@ -70,24 +71,6 @@ struct ubifs_compressor *ubifs_compressors[UBIFS_COMPR_TYPES_CNT];
 
 
 #ifdef __UBOOT__
-/* from mm/util.c */
-
-/**
- * kmemdup - duplicate region of memory
- *
- * @src: memory region to duplicate
- * @len: memory region length
- * @gfp: GFP mask to use
- */
-void *kmemdup(const void *src, size_t len, gfp_t gfp)
-{
-	void *p;
-
-	p = kmalloc(len, gfp);
-	if (p)
-		memcpy(p, src, len);
-	return p;
-}
 
 struct crypto_comp {
 	int compressor;
lib/linux_compat.c +17 -0
index 6373b4451eb3..1f8ef4266613 100644
@@ -40,3 +40,20 @@ void *kmem_cache_alloc(struct kmem_cache *obj, int flag)
 {
 	return malloc_cache_aligned(obj->sz);
 }
+
+/**
+ * kmemdup - duplicate region of memory
+ *
+ * @src: memory region to duplicate
+ * @len: memory region length
+ * @gfp: GFP mask to use
+ */
+void *kmemdup(const void *src, size_t len, gfp_t gfp)
+{
+	void *p;
+
+	p = kmalloc(len, gfp);
+	if (p)
+		memcpy(p, src, len);
+	return p;
+}
Signature · 1 line
2.21.0
#2 lore raw

PATCH v2 1/17 linux_compat: move kmemdup() from ubifs.c to linux_compat.c

Quoted 53 lines · AKASHI Takahiro

linux_compat.c is the best place for kmemdup(), which is currenly used only in ubifs.c, but will also be used when other kernel files (in my case, lib/crypto/x509_cert_parser.c and pkcs7_parser.c) will be imported. So just move it.

---
 fs/ubifs/ubifs.c   | 19 +------------------
 lib/linux_compat.c | 17 +++++++++++++++++
 2 files changed, 18 insertions(+), 18 deletions(-)
fs/ubifs/ubifs.c +1 -18
index 67a0e8caae70..1ffdfe0d9089 100644
@@ -18,6 +18,7 @@
 #include "ubifs.h"
 #include <u-boot/zlib.h>
 
+#include <linux/compat.h>
 #include <linux/err.h>
 #include <linux/lzo.h>
 
@@ -70,24 +71,6 @@ struct ubifs_compressor *ubifs_compressors[UBIFS_COMPR_TYPES_CNT];
 
 
 #ifdef __UBOOT__
-/* from mm/util.c */
-
-/**
- * kmemdup - duplicate region of memory
- *
- * @src: memory region to duplicate
- * @len: memory region length
- * @gfp: GFP mask to use
- */
-void *kmemdup(const void *src, size_t len, gfp_t gfp)
-{
-	void *p;
-
-	p = kmalloc(len, gfp);
-	if (p)
-		memcpy(p, src, len);
-	return p;
-}
 
 struct crypto_comp {
 	int compressor;
lib/linux_compat.c +0 -0
index 6373b4451eb3..1f8ef4266613 100644

lib/linux_compat.c void *kmem_cache_alloc(struct kmem_cache *obj, int flag) · line 49

Quoted 10 lines
lib/linux_compat.c +7 -0
@@ -40,3 +40,20 @@ void *kmem_cache_alloc(struct kmem_cache *obj, int flag)
 {
 	return malloc_cache_aligned(obj->sz);
 }
+
+/**
+ * kmemdup - duplicate region of memory
+ *
+ * @src: memory region to duplicate
+ * @len: memory region length
+ * @gfp: GFP mask to use

Please add a 'Return:' line here. Linux has:

* Return: newly allocated copy of @src or %NULL in case of error

Otherwise

Quoted 11 lines
lib/linux_compat.c +10 -0
+ */
+void *kmemdup(const void *src, size_t len, gfp_t gfp)
+{
+	void *p;
+
+	p = kmalloc(len, gfp);
+	if (p)
+		memcpy(p, src, len);
+	return p;
+}
#3 lore raw

PATCH v2 1/17 linux_compat: move kmemdup() from ubifs.c to linux_compat.c

Quoted 58 lines · Heinrich Schuchardt
Quoted 57 lines · AKASHI Takahiro

linux_compat.c is the best place for kmemdup(), which is currenly used only in ubifs.c, but will also be used when other kernel files (in my case, lib/crypto/x509_cert_parser.c and pkcs7_parser.c) will be imported. So just move it.

---
 fs/ubifs/ubifs.c   | 19 +------------------
 lib/linux_compat.c | 17 +++++++++++++++++
 2 files changed, 18 insertions(+), 18 deletions(-)
fs/ubifs/ubifs.c +1 -18
index 67a0e8caae70..1ffdfe0d9089 100644
@@ -18,6 +18,7 @@
 #include "ubifs.h"
 #include <u-boot/zlib.h>
 
+#include <linux/compat.h>
 #include <linux/err.h>
 #include <linux/lzo.h>
 
@@ -70,24 +71,6 @@ struct ubifs_compressor *ubifs_compressors[UBIFS_COMPR_TYPES_CNT];
 
 
 #ifdef __UBOOT__
-/* from mm/util.c */
-
-/**
- * kmemdup - duplicate region of memory
- *
- * @src: memory region to duplicate
- * @len: memory region length
- * @gfp: GFP mask to use
- */
-void *kmemdup(const void *src, size_t len, gfp_t gfp)
-{
-	void *p;
-
-	p = kmalloc(len, gfp);
-	if (p)
-		memcpy(p, src, len);
-	return p;
-}
 
 struct crypto_comp {
 	int compressor;
lib/linux_compat.c +1 -0
index 6373b4451eb3..1f8ef4266613 100644
@@ -40,3 +40,20 @@ void *kmem_cache_alloc(struct kmem_cache *obj, int flag)
 {
 	return malloc_cache_aligned(obj->sz);
 }
+
Quoted 8 lines

lib/linux_compat.c void *kmem_cache_alloc(struct kmem_cache *obj, int flag) · line 49

Quoted 6 lines
lib/linux_compat.c +6 -0
+/**
+ * kmemdup - duplicate region of memory
+ *
+ * @src: memory region to duplicate
+ * @len: memory region length
+ * @gfp: GFP mask to use

Please add a 'Return:' line here. Linux has:

Okay.

-Takahiro Akashi

Quoted 18 lines

* Return: newly allocated copy of @src or %NULL in case of error

Otherwise

Quoted 11 lines
lib/linux_compat.c +10 -0
+ */
+void *kmemdup(const void *src, size_t len, gfp_t gfp)
+{
+	void *p;
+
+	p = kmalloc(len, gfp);
+	if (p)
+		memcpy(p, src, len);
+	return p;
+}
#4 lore raw

PATCH v2 2/17 rtc.h: add struct udevice declaration

In reply to AKASHI Takahiro

Without this change, including rtc.h solely will cause a build error.

---
 include/rtc.h | 2 ++
 1 file changed, 2 insertions(+)
include/rtc.h +2 -0
index 7386d52db1d7..8aabfc1162a4 100644
@@ -18,6 +18,8 @@
 
 #ifdef CONFIG_DM_RTC
 
+struct udevice;
+
 struct rtc_ops {
 	/**
 	 * get() - get the current time
Signature · 1 line
2.21.0
#5 lore raw

PATCH v2 3/17 rtc: move date.c from drivers/rtc/ to lib/

In reply to AKASHI Takahiro

In the next commit, rtc_mktime(), for compatibility with linux, will be implemented using rtc_mktime(), which is no longer drivers/rtc specific. So move this file under lib/.

---
 cmd/Kconfig                 | 1 +
 drivers/rtc/Kconfig         | 1 +
 drivers/rtc/Makefile        | 1 -
 lib/Kconfig                 | 3 +++
 lib/Makefile                | 2 ++
 {drivers/rtc => lib}/date.c | 5 ++---
 6 files changed, 9 insertions(+), 4 deletions(-)
 rename {drivers/rtc => lib}/date.c (96%)
cmd/Kconfig +1 -0
index 8e55b34c961f..e7213e9fe31f 100644
@@ -1610,6 +1610,7 @@ config CMD_LED
 config CMD_DATE
 	bool "date"
 	default y if DM_RTC
+	select LIB_DATE
 	help
 	  Enable the 'date' command for getting/setting the time/date in RTC
 	  devices.
drivers/rtc/Kconfig +1 -0
index 8778cc7b264c..89e71cc7ebef 100644
@@ -7,6 +7,7 @@ menu "Real Time Clock"
 config DM_RTC
 	bool "Enable Driver Model for RTC drivers"
 	depends on DM
+	select LIB_DATE
 	help
 	  Enable drver model for real-time-clock drivers. The RTC uclass
 	  then provides the rtc_get()/rtc_set() interface, delegating to
drivers/rtc/Makefile +0 -1
index f97a6699820e..e8875ce10f5e 100644
@@ -7,7 +7,6 @@
 obj-$(CONFIG_$(SPL_TPL_)DM_RTC) += rtc-uclass.o
 
 obj-$(CONFIG_RTC_AT91SAM9_RTT) += at91sam9_rtt.o
-obj-y += date.o
 obj-y += rtc-lib.o
 obj-$(CONFIG_RTC_DAVINCI) += davinci.o
 obj-$(CONFIG_RTC_DS1302) += ds1302.o
lib/Kconfig +3 -0
index 3da45a5ec322..e093129a912c 100644
@@ -529,4 +529,7 @@ config TEST_FDTDEC
 	bool "enable fdtdec test"
 	depends on OF_LIBFDT
 
+config LIB_DATE
+	bool
+
 endmenu
lib/Makefile +2 -0
index 2fffd68f943c..a44f9cc0ae20 100644
@@ -117,4 +117,6 @@ else
 obj-y += vsprintf.o strto.o strmhz.o
 endif
 
+obj-y += date.o
+
 subdir-ccflags-$(CONFIG_CC_OPTIMIZE_LIBS_FOR_SPEED) += -O2
lib/date.c renamed from drivers/rtc/date.c +2 -3
similarity index 96%
rename from drivers/rtc/date.c
rename to lib/date.c
index c57317d2c267..63af4a142612 100644
@@ -9,8 +9,7 @@
 #include <errno.h>
 #include <rtc.h>
 
-#if defined(CONFIG_CMD_DATE) || defined(CONFIG_DM_RTC) || \
-				defined(CONFIG_TIMESTAMP)
+#if defined(CONFIG_LIB_DATE) || defined(CONFIG_TIMESTAMP)
 
 #define FEBRUARY		2
 #define	STARTOFTIME		1970
@@ -97,4 +96,4 @@ unsigned long rtc_mktime(const struct rtc_time *tm)
 	return (hours * 60 + tm->tm_min) * 60 + tm->tm_sec;
 }
 
-#endif
+#endif /* CONFIG_LIB_DATE || CONFIG_TIMESTAMP */
Signature · 1 line
2.21.0
#6 lore raw

PATCH v2 4/17 lib: add mktime64() for linux compatibility

In reply to AKASHI Takahiro

This function will be used in lib/crypto/x509_cert_parser.c, which will also be imported from linux code in a later commit.

---
 include/linux/time.h | 10 ++++++++++
 lib/date.c           | 20 ++++++++++++++++++++
 2 files changed, 30 insertions(+)
include/linux/time.h +10 -0
index b8d298eb4d68..dc9344a6d97b 100644
@@ -1,6 +1,7 @@
 #ifndef _LINUX_TIME_H
 #define _LINUX_TIME_H
 
+#include <rtc.h>
 #include <linux/types.h>
 
 #define _DEFUN(a,b,c) a(c)
@@ -150,4 +151,13 @@ _DEFUN (ctime_r, (tim_p, result),
     return asctime_r (localtime_r (tim_p, &tm), result);
 }
 
+/* for compatibility with linux code */
+typedef __s64 time64_t;
+
+#ifdef CONFIG_LIB_DATE
+time64_t mktime64(const unsigned int year, const unsigned int mon,
+		  const unsigned int day, const unsigned int hour,
+		  const unsigned int min, const unsigned int sec);
+#endif
+
 #endif
lib/date.c +20 -0
index 63af4a142612..0456de78ab14 100644
@@ -8,6 +8,7 @@
 #include <command.h>
 #include <errno.h>
 #include <rtc.h>
+#include <linux/time.h>
 
 #if defined(CONFIG_LIB_DATE) || defined(CONFIG_TIMESTAMP)
 
@@ -97,3 +98,22 @@ unsigned long rtc_mktime(const struct rtc_time *tm)
 }
 
 #endif /* CONFIG_LIB_DATE || CONFIG_TIMESTAMP */
+
+#ifdef CONFIG_LIB_DATE
+/* for compatibility with linux code */
+time64_t mktime64(const unsigned int year, const unsigned int mon,
+		  const unsigned int day, const unsigned int hour,
+		  const unsigned int min, const unsigned int sec)
+{
+	struct rtc_time time;
+
+	time.tm_year = year;
+	time.tm_mon = mon;
+	time.tm_mday = day;
+	time.tm_hour = hour;
+	time.tm_min = min;
+	time.tm_sec = sec;
+
+	return (time64_t)rtc_mktime((const struct rtc_time *)&time);
+}
+#endif
Signature · 1 line
2.21.0
#7 lore raw

PATCH v2 5/17 include: kernel.h: include printk.h

In reply to AKASHI Takahiro

Adding "printk.h" will help improve portability from linux kernel code (in my case, lib/asn1_decoder.c and others) where printf and pr_* variant functions are used.

---
 include/linux/kernel.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
include/linux/kernel.h +1 -1
index a85c15d8dc28..18846bef3997 100644
@@ -1,8 +1,8 @@
 #ifndef _LINUX_KERNEL_H
 #define _LINUX_KERNEL_H
 
-
 #include <linux/types.h>
+#include <linux/printk.h> /* for printf/pr_* utilities */
 
 #define USHRT_MAX	((u16)(~0U))
 #define SHRT_MAX	((s16)(USHRT_MAX>>1))
Signature · 1 line
2.21.0
#8 lore raw

PATCH v2 6/17 linux/time.h: include vsprintf.h

In reply to AKASHI Takahiro

Without this commit, time.h possibly causes a build error as asctime_r() uses sprintf().

---
 include/linux/time.h | 1 +
 1 file changed, 1 insertion(+)
include/linux/time.h +1 -0
index dc9344a6d97b..702dd276aea5 100644
@@ -2,6 +2,7 @@
 #define _LINUX_TIME_H
 
 #include <rtc.h>
+#include <vsprintf.h>
 #include <linux/types.h>
 
 #define _DEFUN(a,b,c) a(c)
Signature · 1 line
2.21.0
#9 lore raw

PATCH v2 7/17 cmd: add asn1_compiler

In reply to AKASHI Takahiro

Imported from linux kernel v5.3:
asn1.h without changes
asn1_ber_bytecode.h without changes
asn1_decoder.h without changes
asn1_compiler.c without changes

This host command will be used to create a ASN1 parser, for example, for pkcs7 messages or x509 certificates. More specifically, it will generate *byte code* which will be interpreted by asn1 decoder library.

---
 include/linux/asn1.h              |   65 ++
 include/linux/asn1_ber_bytecode.h |   89 ++
 include/linux/asn1_decoder.h      |   20 +
 lib/Kconfig                       |    3 +
 scripts/Makefile.build            |    4 +-
 tools/Makefile                    |    3 +
 tools/asn1_compiler.c             | 1611 +++++++++++++++++++++++++++++
 7 files changed, 1793 insertions(+), 2 deletions(-)
 create mode 100644 include/linux/asn1.h
 create mode 100644 include/linux/asn1_ber_bytecode.h
 create mode 100644 include/linux/asn1_decoder.h
 create mode 100644 tools/asn1_compiler.c
include/linux/asn1.h new file +65 -0
new file mode 100644
index 000000000000..a4d0bdd10711
@@ -0,0 +1,65 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+/* ASN.1 BER/DER/CER encoding definitions
+ *
+ * Copyright (C) 2012 Red Hat, Inc. All Rights Reserved.
+ * Written by David Howells (dhowells at redhat.com)
+ */
+
+#ifndef _LINUX_ASN1_H
+#define _LINUX_ASN1_H
+
+/* Class */
+enum asn1_class {
+	ASN1_UNIV	= 0,	/* Universal */
+	ASN1_APPL	= 1,	/* Application */
+	ASN1_CONT	= 2,	/* Context */
+	ASN1_PRIV	= 3	/* Private */
+};
+#define ASN1_CLASS_BITS	0xc0
+
+
+enum asn1_method {
+	ASN1_PRIM	= 0,	/* Primitive */
+	ASN1_CONS	= 1	/* Constructed */
+};
+#define ASN1_CONS_BIT	0x20
+
+/* Tag */
+enum asn1_tag {
+	ASN1_EOC	= 0,	/* End Of Contents or N/A */
+	ASN1_BOOL	= 1,	/* Boolean */
+	ASN1_INT	= 2,	/* Integer */
+	ASN1_BTS	= 3,	/* Bit String */
+	ASN1_OTS	= 4,	/* Octet String */
+	ASN1_NULL	= 5,	/* Null */
+	ASN1_OID	= 6,	/* Object Identifier  */
+	ASN1_ODE	= 7,	/* Object Description */
+	ASN1_EXT	= 8,	/* External */
+	ASN1_REAL	= 9,	/* Real float */
+	ASN1_ENUM	= 10,	/* Enumerated */
+	ASN1_EPDV	= 11,	/* Embedded PDV */
+	ASN1_UTF8STR	= 12,	/* UTF8 String */
+	ASN1_RELOID	= 13,	/* Relative OID */
+	/* 14 - Reserved */
+	/* 15 - Reserved */
+	ASN1_SEQ	= 16,	/* Sequence and Sequence of */
+	ASN1_SET	= 17,	/* Set and Set of */
+	ASN1_NUMSTR	= 18,	/* Numerical String */
+	ASN1_PRNSTR	= 19,	/* Printable String */
+	ASN1_TEXSTR	= 20,	/* T61 String / Teletext String */
+	ASN1_VIDSTR	= 21,	/* Videotex String */
+	ASN1_IA5STR	= 22,	/* IA5 String */
+	ASN1_UNITIM	= 23,	/* Universal Time */
+	ASN1_GENTIM	= 24,	/* General Time */
+	ASN1_GRASTR	= 25,	/* Graphic String */
+	ASN1_VISSTR	= 26,	/* Visible String */
+	ASN1_GENSTR	= 27,	/* General String */
+	ASN1_UNISTR	= 28,	/* Universal String */
+	ASN1_CHRSTR	= 29,	/* Character String */
+	ASN1_BMPSTR	= 30,	/* BMP String */
+	ASN1_LONG_TAG	= 31	/* Long form tag */
+};
+
+#define ASN1_INDEFINITE_LENGTH 0x80
+
+#endif /* _LINUX_ASN1_H */
include/linux/asn1_ber_bytecode.h new file +89 -0

89 lines not on this page · raw message

include/linux/asn1_decoder.h new file +20 -0
new file mode 100644
index 000000000000..83f9c6e1e5e9
@@ -0,0 +1,20 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+/* ASN.1 decoder
+ *
+ * Copyright (C) 2012 Red Hat, Inc. All Rights Reserved.
+ * Written by David Howells ([email protected])
+ */
+
+#ifndef _LINUX_ASN1_DECODER_H
+#define _LINUX_ASN1_DECODER_H
+
+#include <linux/asn1.h>
+
+struct asn1_decoder;
+
+extern int asn1_ber_decoder(const struct asn1_decoder *decoder,
+			    void *context,
+			    const unsigned char *data,
+			    size_t datalen);
+
+#endif /* _LINUX_ASN1_DECODER_H */
lib/Kconfig +3 -0
index e093129a912c..83c05645487f 100644
@@ -521,6 +521,9 @@ config SMBIOS_PRODUCT_NAME
 
 endmenu
 
+config ASN1_COMPILER
+	bool
+
 source lib/efi/Kconfig
 source lib/efi_loader/Kconfig
 source lib/optee/Kconfig
scripts/Makefile.build +2 -2
index f7a041296d3d..26eb701f8dea 100644
@@ -328,10 +328,10 @@ $(obj)/%.lds: $(src)/%.lds.S FORCE
 # ASN.1 grammar
 # ---------------------------------------------------------------------------
 quiet_cmd_asn1_compiler = ASN.1   $@
-      cmd_asn1_compiler = $(objtree)/scripts/asn1_compiler $< \
+      cmd_asn1_compiler = $(objtree)/tools/asn1_compiler $< \
 				$(subst .h,.c,$@) $(subst .c,.h,$@)
 
-$(obj)/%-asn1.c $(obj)/%-asn1.h: $(src)/%.asn1 $(objtree)/scripts/asn1_compiler
+$(obj)/%.asn1.c $(obj)/%.asn1.h: $(src)/%.asn1 $(objtree)/tools/asn1_compiler
 	$(call cmd,asn1_compiler)
 
 # Build the compiled-in targets
tools/Makefile +3 -0
index 24581adccd4b..345bc84e48db 100644
@@ -207,6 +207,9 @@ endif
 
 hostprogs-$(CONFIG_MIPS) += mips-relocs
 
+hostprogs-$(CONFIG_ASN1_COMPILER)	+= asn1_compiler
+HOSTCFLAGS_asn1_compiler.o = -idirafter $(srctree)/include
+
 # We build some files with extra pedantic flags to try to minimize things
 # that won't build on some weird host compiler -- though there are lots of
 # exceptions for files that aren't complaint.
tools/asn1_compiler.c new file +1611 -0

1,611 lines not on this page · raw message

Signature · 1 line
2.21.0
#10 lore raw

PATCH v2 8/17 Makefile: add build script for asn1 parsers

In reply to AKASHI Takahiro

This rule will be used to build x509 and pkcs7 parsers.

---
 Makefile               | 1 +
 scripts/Makefile.build | 4 +++-
 2 files changed, 4 insertions(+), 1 deletion(-)
Makefile +1 -0
index 54da5cd51cea..66ef4503e606 100644
@@ -1878,6 +1878,7 @@ clean: $(clean-dirs)
 		-o -name '*.ko.*' -o -name '*.su' -o -name '*.pyc' \
 		-o -name '.*.d' -o -name '.*.tmp' -o -name '*.mod.c' \
 		-o -name '*.lex.c' -o -name '*.tab.[ch]' \
+		-o -name '*.asn1.[ch]' \
 		-o -name '*.symtypes' -o -name 'modules.order' \
 		-o -name modules.builtin -o -name '.tmp_*.o.*' \
 		-o -name 'dsdt.aml' -o -name 'dsdt.asl.tmp' -o -name 'dsdt.c' \
scripts/Makefile.build +3 -1
index 26eb701f8dea..6d59ea91fac3 100644
@@ -419,9 +419,11 @@ targets += $(multi-used-y) $(multi-used-m)
 intermediate_targets = $(foreach sfx, $(2), \
 				$(patsubst %$(strip $(1)),%$(sfx), \
 					$(filter %$(strip $(1)), $(targets))))
+# %.asn1.o <- %.asn1.[ch] <- %.asn1
 # %.lex.o <- %.lex.c <- %.l
 # %.tab.o <- %.tab.[ch] <- %.y
-targets += $(call intermediate_targets, .lex.o, .lex.c) \
+targets += $(call intermediate_targets, .asn1.o, .asn1.c .asn1.h) \
+	   $(call intermediate_targets, .lex.o, .lex.c) \
 	   $(call intermediate_targets, .tab.o, .tab.c .tab.h)
 
 # Descending
Signature · 1 line
2.21.0
#11 lore raw

PATCH v2 9/17 lib: add asn1 decoder

In reply to AKASHI Takahiro

Imported from linux kernel v5.3:
lib/asn1_decoder.c with changes marked as __UBOOT__

---
 lib/Kconfig        |   5 +
 lib/Makefile       |   1 +
 lib/asn1_decoder.c | 527 +++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 533 insertions(+)
 create mode 100644 lib/asn1_decoder.c
lib/Kconfig +5 -0
index 83c05645487f..ddf7d1313b09 100644
@@ -524,6 +524,11 @@ endmenu
 config ASN1_COMPILER
 	bool
 
+config ASN1_DECODER
+	bool
+	help
+	  Enable asn1 decoder library.
+
 source lib/efi/Kconfig
 source lib/efi_loader/Kconfig
 source lib/optee/Kconfig
lib/Makefile +1 -0
index a44f9cc0ae20..4aa99fe779b1 100644
@@ -17,6 +17,7 @@ obj-$(CONFIG_OF_LIVE) += of_live.o
 obj-$(CONFIG_CMD_DHRYSTONE) += dhry/
 obj-$(CONFIG_ARCH_AT91) += at91/
 obj-$(CONFIG_OPTEE) += optee/
+obj-$(CONFIG_ASN1_DECODER) += asn1_decoder.o
 
 obj-$(CONFIG_AES) += aes.o
 
lib/asn1_decoder.c new file +527 -0

527 lines not on this page · raw message

Signature · 1 line
2.21.0
#12 lore raw

PATCH v2 10/17 doc: add README for asn1 compiler and decoder

In reply to AKASHI Takahiro

This document gives a brief description about ASN1 compiler as well as ASN1 decoder.

---
 doc/README.asn1 | 40 ++++++++++++++++++++++++++++++++++++++++
 1 file changed, 40 insertions(+)
 create mode 100644 doc/README.asn1
doc/README.asn1 new file +40 -0
new file mode 100644
index 000000000000..1359b93aef0b
@@ -0,0 +1,40 @@
+ASN1
+====
+
+Abstract Syntax Notation One (or ASN1) is a standard by ITU-T and ISO/IEC
+and used as a description language for defining data structure in
+an independent manner.
+Any data described in ASN1 notation can be serialized (or encoded) and
+de-serialized (or decoded) with well-defined encoding rules.
+
+A combination of ASN1 compiler and ASN1 decoder library function will
+provide a function interface for parsing encoded binary into specific
+data structure:
+1) define data structure in a text file (*.asn1)
+2) define "action" routines for specific "tags" defined in (1)
+3) generate bytecode as a C file (*.asn1.[ch]) from *.asn1 file
+   with ASN1 compiler (tools/asn1_compiler)
+4) call a ASN1 decoder (asn1_ber_decoder()) with bytecode and data
+
+Usage of ASN1 compiler
+----------------------
+  asn1_compiler [-v] [-d] <grammar-file> <c-file> <hdr-file>
+
+  <grammar-file>:	ASN1 input file
+  <c-file>:		generated C file
+  <hdr-file>:		generated include file
+
+Usage of ASN1 decoder
+---------------------
+  int asn1_ber_decoder(const struct asn1_decoder *decoder, void *context,
+		       const unsigned char *data, size_t datalen);
+
+  @decoder:		bytecode binary
+  @context:		context for decoder
+  @data:		data to be parsed
+  @datalen:		size of data
+
+
+As of writing this, ASN1 compiler and decoder are used to implement
+X509 certificate parser, pcks7 message parser and RSA public key parser
+for UEFI secure boot.
Signature · 1 line
2.21.0
#13 lore raw

PATCH v2 11/17 lib: add oid registry utility

In reply to AKASHI Takahiro

Imported from linux kernel v5.3:
build_OID_registry without changes
oid_registry.h without changes
oid_registry.c with changes marked as __UBOOT__

---
 include/linux/oid_registry.h | 117 ++++++++++++++++++++
 lib/Kconfig                  |   5 +
 lib/Makefile                 |  16 +++
 lib/oid_registry.c           | 179 ++++++++++++++++++++++++++++++
 scripts/build_OID_registry   | 203 +++++++++++++++++++++++++++++++++++
 5 files changed, 520 insertions(+)
 create mode 100644 include/linux/oid_registry.h
 create mode 100644 lib/oid_registry.c
 create mode 100755 scripts/build_OID_registry
include/linux/oid_registry.h new file +117 -0
new file mode 100644
index 000000000000..657d6bf2c064
@@ -0,0 +1,117 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+/* ASN.1 Object identifier (OID) registry
+ *
+ * Copyright (C) 2012 Red Hat, Inc. All Rights Reserved.
+ * Written by David Howells (dhowells at redhat.com)
+ */
+
+#ifndef _LINUX_OID_REGISTRY_H
+#define _LINUX_OID_REGISTRY_H
+
+#include <linux/types.h>
+
+/*
+ * OIDs are turned into these values if possible, or OID__NR if not held here.
+ *
+ * NOTE!  Do not mess with the format of each line as this is read by
+ *	  build_OID_registry.pl to generate the data for look_up_OID().
+ */
+enum OID {
+	OID_id_dsa_with_sha1,		/* 1.2.840.10030.4.3 */
+	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 */
+
+	/* 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 */
+	OID_md2WithRSAEncryption,	/* 1.2.840.113549.1.1.2 */
+	OID_md3WithRSAEncryption,	/* 1.2.840.113549.1.1.3 */
+	OID_md4WithRSAEncryption,	/* 1.2.840.113549.1.1.4 */
+	OID_sha1WithRSAEncryption,	/* 1.2.840.113549.1.1.5 */
+	OID_sha256WithRSAEncryption,	/* 1.2.840.113549.1.1.11 */
+	OID_sha384WithRSAEncryption,	/* 1.2.840.113549.1.1.12 */
+	OID_sha512WithRSAEncryption,	/* 1.2.840.113549.1.1.13 */
+	OID_sha224WithRSAEncryption,	/* 1.2.840.113549.1.1.14 */
+	/* PKCS#7 {iso(1) member-body(2) us(840) rsadsi(113549) pkcs(1) pkcs-7(7)} */
+	OID_data,			/* 1.2.840.113549.1.7.1 */
+	OID_signed_data,		/* 1.2.840.113549.1.7.2 */
+	/* PKCS#9 {iso(1) member-body(2) us(840) rsadsi(113549) pkcs(1) pkcs-9(9)} */
+	OID_email_address,		/* 1.2.840.113549.1.9.1 */
+	OID_contentType,		/* 1.2.840.113549.1.9.3 */
+	OID_messageDigest,		/* 1.2.840.113549.1.9.4 */
+	OID_signingTime,		/* 1.2.840.113549.1.9.5 */
+	OID_smimeCapabilites,		/* 1.2.840.113549.1.9.15 */
+	OID_smimeAuthenticatedAttrs,	/* 1.2.840.113549.1.9.16.2.11 */
+
+	/* {iso(1) member-body(2) us(840) rsadsi(113549) digestAlgorithm(2)} */
+	OID_md2,			/* 1.2.840.113549.2.2 */
+	OID_md4,			/* 1.2.840.113549.2.4 */
+	OID_md5,			/* 1.2.840.113549.2.5 */
+
+	/* Microsoft Authenticode & Software Publishing */
+	OID_msIndirectData,		/* 1.3.6.1.4.1.311.2.1.4 */
+	OID_msStatementType,		/* 1.3.6.1.4.1.311.2.1.11 */
+	OID_msSpOpusInfo,		/* 1.3.6.1.4.1.311.2.1.12 */
+	OID_msPeImageDataObjId,		/* 1.3.6.1.4.1.311.2.1.15 */
+	OID_msIndividualSPKeyPurpose,	/* 1.3.6.1.4.1.311.2.1.21 */
+	OID_msOutlookExpress,		/* 1.3.6.1.4.1.311.16.4 */
+
+	OID_certAuthInfoAccess,		/* 1.3.6.1.5.5.7.1.1 */
+	OID_sha1,			/* 1.3.14.3.2.26 */
+	OID_sha256,			/* 2.16.840.1.101.3.4.2.1 */
+	OID_sha384,			/* 2.16.840.1.101.3.4.2.2 */
+	OID_sha512,			/* 2.16.840.1.101.3.4.2.3 */
+	OID_sha224,			/* 2.16.840.1.101.3.4.2.4 */
+
+	/* Distinguished Name attribute IDs [RFC 2256] */
+	OID_commonName,			/* 2.5.4.3 */
+	OID_surname,			/* 2.5.4.4 */
+	OID_countryName,		/* 2.5.4.6 */
+	OID_locality,			/* 2.5.4.7 */
+	OID_stateOrProvinceName,	/* 2.5.4.8 */
+	OID_organizationName,		/* 2.5.4.10 */
+	OID_organizationUnitName,	/* 2.5.4.11 */
+	OID_title,			/* 2.5.4.12 */
+	OID_description,		/* 2.5.4.13 */
+	OID_name,			/* 2.5.4.41 */
+	OID_givenName,			/* 2.5.4.42 */
+	OID_initials,			/* 2.5.4.43 */
+	OID_generationalQualifier,	/* 2.5.4.44 */
+
+	/* Certificate extension IDs */
+	OID_subjectKeyIdentifier,	/* 2.5.29.14 */
+	OID_keyUsage,			/* 2.5.29.15 */
+	OID_subjectAltName,		/* 2.5.29.17 */
+	OID_issuerAltName,		/* 2.5.29.18 */
+	OID_basicConstraints,		/* 2.5.29.19 */
+	OID_crlDistributionPoints,	/* 2.5.29.31 */
+	OID_certPolicies,		/* 2.5.29.32 */
+	OID_authorityKeyIdentifier,	/* 2.5.29.35 */
+	OID_extKeyUsage,		/* 2.5.29.37 */
+
+	/* EC-RDSA */
+	OID_gostCPSignA,		/* 1.2.643.2.2.35.1 */
+	OID_gostCPSignB,		/* 1.2.643.2.2.35.2 */
+	OID_gostCPSignC,		/* 1.2.643.2.2.35.3 */
+	OID_gost2012PKey256,		/* 1.2.643.7.1.1.1.1 */
+	OID_gost2012PKey512,		/* 1.2.643.7.1.1.1.2 */
+	OID_gost2012Digest256,		/* 1.2.643.7.1.1.2.2 */
+	OID_gost2012Digest512,		/* 1.2.643.7.1.1.2.3 */
+	OID_gost2012Signature256,	/* 1.2.643.7.1.1.3.2 */
+	OID_gost2012Signature512,	/* 1.2.643.7.1.1.3.3 */
+	OID_gostTC26Sign256A,		/* 1.2.643.7.1.2.1.1.1 */
+	OID_gostTC26Sign256B,		/* 1.2.643.7.1.2.1.1.2 */
+	OID_gostTC26Sign256C,		/* 1.2.643.7.1.2.1.1.3 */
+	OID_gostTC26Sign256D,		/* 1.2.643.7.1.2.1.1.4 */
+	OID_gostTC26Sign512A,		/* 1.2.643.7.1.2.1.2.1 */
+	OID_gostTC26Sign512B,		/* 1.2.643.7.1.2.1.2.2 */
+	OID_gostTC26Sign512C,		/* 1.2.643.7.1.2.1.2.3 */
+
+	OID__NR
+};
+
+extern enum OID look_up_OID(const void *data, size_t datasize);
+extern int sprint_oid(const void *, size_t, char *, size_t);
+extern int sprint_OID(enum OID, char *, size_t);
+
+#endif /* _LINUX_OID_REGISTRY_H */
lib/Kconfig +5 -0
index ddf7d1313b09..1e373f9f8455 100644
@@ -529,6 +529,11 @@ config ASN1_DECODER
 	help
 	  Enable asn1 decoder library.
 
+config OID_REGISTRY
+	bool
+	help
+	  Enable fast lookup object identifier registry.
+
 source lib/efi/Kconfig
 source lib/efi_loader/Kconfig
 source lib/optee/Kconfig
lib/Makefile +16 -0
index 4aa99fe779b1..52bf33d17086 100644
@@ -120,4 +120,20 @@ endif
 
 obj-y += date.o
 
+#
+# Build a fast OID lookup registry from include/linux/oid_registry.h
+#
+obj-$(CONFIG_OID_REGISTRY) += oid_registry.o
+
+$(obj)/oid_registry.o: $(obj)/oid_registry_data.c
+
+$(obj)/oid_registry_data.c: $(srctree)/include/linux/oid_registry.h \
+			    $(srctree)/scripts/build_OID_registry
+	$(call cmd,build_OID_registry)
+
+quiet_cmd_build_OID_registry = GEN     $@
+      cmd_build_OID_registry = perl $(srctree)/scripts/build_OID_registry $< $@
+
+clean-files     += oid_registry_data.c
+
 subdir-ccflags-$(CONFIG_CC_OPTIMIZE_LIBS_FOR_SPEED) += -O2
lib/oid_registry.c new file +179 -0

179 lines not on this page · raw message

scripts/build_OID_registry new file +203 -0

203 lines not on this page · raw message

Signature · 1 line
2.21.0
#14 lore raw

PATCH v2 12/17 lib: crypto: add public key utility

In reply to AKASHI Takahiro

Imported from linux kernel v5.3:
asymmetric-type.h with changes marked as __UBOOT__ asymmetric_type.c with changes marked as __UBOOT__ public_key.h with changes marked as __UBOOT__ public_key.c with changes marked as __UBOOT__

---
 include/crypto/public_key.h    |  90 +++++
 include/keys/asymmetric-type.h |  88 +++++
 lib/crypto/Kconfig             |  19 +
 lib/crypto/Makefile            |  10 +
 lib/crypto/asymmetric_type.c   | 668 +++++++++++++++++++++++++++++++++
 lib/crypto/public_key.c        | 376 +++++++++++++++++++
 6 files changed, 1251 insertions(+)
 create mode 100644 include/crypto/public_key.h
 create mode 100644 include/keys/asymmetric-type.h
 create mode 100644 lib/crypto/Kconfig
 create mode 100644 lib/crypto/Makefile
 create mode 100644 lib/crypto/asymmetric_type.c
 create mode 100644 lib/crypto/public_key.c
include/crypto/public_key.h new file +90 -0
new file mode 100644
index 000000000000..436a1ee1ee64
@@ -0,0 +1,90 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+/* Asymmetric public-key algorithm definitions
+ *
+ * See Documentation/crypto/asymmetric-keys.txt
+ *
+ * Copyright (C) 2012 Red Hat, Inc. All Rights Reserved.
+ * Written by David Howells (dhowells at redhat.com)
+ */
+
+#ifndef _LINUX_PUBLIC_KEY_H
+#define _LINUX_PUBLIC_KEY_H
+
+#ifdef __UBOOT__
+#include <linux/types.h>
+#else
+#include <linux/keyctl.h>
+#endif
+#include <linux/oid_registry.h>
+
+/*
+ * Cryptographic data for the public-key subtype of the asymmetric key type.
+ *
+ * Note that this may include private part of the key as well as the public
+ * part.
+ */
+struct public_key {
+	void *key;
+	u32 keylen;
+	enum OID algo;
+	void *params;
+	u32 paramlen;
+	bool key_is_private;
+	const char *id_type;
+	const char *pkey_algo;
+};
+
+extern void public_key_free(struct public_key *key);
+
+/*
+ * Public key cryptography signature data
+ */
+struct public_key_signature {
+	struct asymmetric_key_id *auth_ids[2];
+	u8 *s;			/* Signature */
+	u32 s_size;		/* Number of bytes in signature */
+	u8 *digest;
+	u8 digest_size;		/* Number of bytes in digest */
+	const char *pkey_algo;
+	const char *hash_algo;
+	const char *encoding;
+};
+
+extern void public_key_signature_free(struct public_key_signature *sig);
+
+#ifndef __UBOOT__
+extern struct asymmetric_key_subtype public_key_subtype;
+
+struct key;
+struct key_type;
+union key_payload;
+
+extern int restrict_link_by_signature(struct key *dest_keyring,
+				      const struct key_type *type,
+				      const union key_payload *payload,
+				      struct key *trust_keyring);
+
+extern int restrict_link_by_key_or_keyring(struct key *dest_keyring,
+					   const struct key_type *type,
+					   const union key_payload *payload,
+					   struct key *trusted);
+
+extern int restrict_link_by_key_or_keyring_chain(struct key *trust_keyring,
+						 const struct key_type *type,
+						 const union key_payload *payload,
+						 struct key *trusted);
+
+extern int query_asymmetric_key(const struct kernel_pkey_params *,
+				struct kernel_pkey_query *);
+
+extern int encrypt_blob(struct kernel_pkey_params *, const void *, void *);
+extern int decrypt_blob(struct kernel_pkey_params *, const void *, void *);
+extern int create_signature(struct kernel_pkey_params *, const void *, void *);
+extern int verify_signature(const struct key *,
+			    const struct public_key_signature *);
+
+int public_key_verify_signature(const struct public_key *pkey,
+				const struct public_key_signature *sig);
+#endif /* !__UBOOT__ */
+
+#endif /* _LINUX_PUBLIC_KEY_H */
include/keys/asymmetric-type.h new file +88 -0

88 lines not on this page · raw message

lib/crypto/Kconfig new file +19 -0
new file mode 100644
index 000000000000..b8e8288d2f80
@@ -0,0 +1,19 @@
+menuconfig ASYMMETRIC_KEY_TYPE
+	bool "Asymmetric (public-key cryptographic) key Support"
+	help
+	  This option provides support for a key type that holds the data for
+	  the asymmetric keys used for public key cryptographic operations such
+	  as encryption, decryption, signature generation and signature
+	  verification.
+
+if ASYMMETRIC_KEY_TYPE
+
+config ASYMMETRIC_PUBLIC_KEY_SUBTYPE
+	bool "Asymmetric public-key crypto algorithm subtype"
+	help
+	  This option provides support for asymmetric public key type handling.
+	  If signature generation and/or verification are to be used,
+	  appropriate hash algorithms (such as SHA-1) must be available.
+	  ENOPKG will be reported if the requisite algorithm is unavailable.
+
+endif # ASYMMETRIC_KEY_TYPE
lib/crypto/Makefile new file +10 -0
new file mode 100644
index 000000000000..a284de9e0411
@@ -0,0 +1,10 @@
+# SPDX-License-Identifier: GPL-2.0+
+#
+# Makefile for asymmetric cryptographic keys
+#
+
+obj-$(CONFIG_ASYMMETRIC_KEY_TYPE) += asymmetric_keys.o
+
+asymmetric_keys-y := asymmetric_type.o
+
+obj-$(CONFIG_ASYMMETRIC_PUBLIC_KEY_SUBTYPE) += public_key.o
lib/crypto/asymmetric_type.c new file +668 -0

668 lines not on this page · raw message

lib/crypto/public_key.c new file +376 -0

376 lines not on this page · raw message

Signature · 1 line
2.21.0
#15 lore raw

PATCH v2 13/17 lib: crypto: add rsa public key parser

In reply to AKASHI Takahiro

Imported from linux kernel v5.3:
rsapubkey.asn1 without changes
rsa.h without changes
rsa_helper.c with changes marked as __UBOOT__

---
 include/crypto/internal/rsa.h |  57 ++++++++++
 lib/crypto/Kconfig            |  11 ++
 lib/crypto/Makefile           |  11 ++
 lib/crypto/rsa_helper.c       | 198 ++++++++++++++++++++++++++++++++++
 lib/crypto/rsapubkey.asn1     |   4 +
 5 files changed, 281 insertions(+)
 create mode 100644 include/crypto/internal/rsa.h
 create mode 100644 lib/crypto/rsa_helper.c
 create mode 100644 lib/crypto/rsapubkey.asn1
include/crypto/internal/rsa.h new file +57 -0
new file mode 100644
index 000000000000..e870133f4b77
@@ -0,0 +1,57 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+/*
+ * RSA internal helpers
+ *
+ * Copyright (c) 2015, Intel Corporation
+ * Authors: Tadeusz Struk <[email protected]>
+ */
+#ifndef _RSA_HELPER_
+#define _RSA_HELPER_
+#include <linux/types.h>
+
+/**
+ * rsa_key - RSA key structure
+ * @n           : RSA modulus raw byte stream
+ * @e           : RSA public exponent raw byte stream
+ * @d           : RSA private exponent raw byte stream
+ * @p           : RSA prime factor p of n raw byte stream
+ * @q           : RSA prime factor q of n raw byte stream
+ * @dp          : RSA exponent d mod (p - 1) raw byte stream
+ * @dq          : RSA exponent d mod (q - 1) raw byte stream
+ * @qinv        : RSA CRT coefficient q^(-1) mod p raw byte stream
+ * @n_sz        : length in bytes of RSA modulus n
+ * @e_sz        : length in bytes of RSA public exponent
+ * @d_sz        : length in bytes of RSA private exponent
+ * @p_sz        : length in bytes of p field
+ * @q_sz        : length in bytes of q field
+ * @dp_sz       : length in bytes of dp field
+ * @dq_sz       : length in bytes of dq field
+ * @qinv_sz     : length in bytes of qinv field
+ */
+struct rsa_key {
+	const u8 *n;
+	const u8 *e;
+	const u8 *d;
+	const u8 *p;
+	const u8 *q;
+	const u8 *dp;
+	const u8 *dq;
+	const u8 *qinv;
+	size_t n_sz;
+	size_t e_sz;
+	size_t d_sz;
+	size_t p_sz;
+	size_t q_sz;
+	size_t dp_sz;
+	size_t dq_sz;
+	size_t qinv_sz;
+};
+
+int rsa_parse_pub_key(struct rsa_key *rsa_key, const void *key,
+		      unsigned int key_len);
+
+int rsa_parse_priv_key(struct rsa_key *rsa_key, const void *key,
+		       unsigned int key_len);
+
+extern struct crypto_template rsa_pkcs1pad_tmpl;
+#endif
lib/crypto/Kconfig +11 -0
index b8e8288d2f80..9572ea8c87f3 100644
@@ -16,4 +16,15 @@ config ASYMMETRIC_PUBLIC_KEY_SUBTYPE
 	  appropriate hash algorithms (such as SHA-1) must be available.
 	  ENOPKG will be reported if the requisite algorithm is unavailable.
 
+config RSA_PUBLIC_KEY_PARSER
+	bool "RSA public key parser"
+	depends on ASYMMETRIC_PUBLIC_KEY_SUBTYPE
+	select ASN1_DECODER
+	select ASN1_COMPILER
+	select OID_REGISTRY
+	help
+	  This option provides support for parsing a blob containing RSA
+	  public key data and provides the ability to instantiate a public
+	  key.
+
 endif # ASYMMETRIC_KEY_TYPE
lib/crypto/Makefile +11 -0
index a284de9e0411..69330a9ebd27 100644
@@ -8,3 +8,14 @@ obj-$(CONFIG_ASYMMETRIC_KEY_TYPE) += asymmetric_keys.o
 asymmetric_keys-y := asymmetric_type.o
 
 obj-$(CONFIG_ASYMMETRIC_PUBLIC_KEY_SUBTYPE) += public_key.o
+
+#
+# RSA public key parser
+#
+obj-$(CONFIG_RSA_PUBLIC_KEY_PARSER) += rsa_public_key.o
+rsa_public_key-y := \
+	rsapubkey.asn1.o \
+	rsa_helper.o
+
+$(obj)/rsapubkey.asn1.o: $(obj)/rsapubkey.asn1.c $(obj)/rsapubkey.asn1.h
+$(obj)/rsa_helper.o: $(obj)/rsapubkey.asn1.h
lib/crypto/rsa_helper.c new file +198 -0

198 lines not on this page · raw message

lib/crypto/rsapubkey.asn1 new file +4 -0
new file mode 100644
index 000000000000..725498e461d2
@@ -0,0 +1,4 @@
+RsaPubKey ::= SEQUENCE {
+	n INTEGER ({ rsa_get_n }),
+	e INTEGER ({ rsa_get_e })
+}
Signature · 1 line
2.21.0
#16 lore raw

PATCH v2 14/17 lib: crypto: add x509 parser

In reply to AKASHI Takahiro

Imported from linux kernel v5.3:
x509.asn1 without changes
x509_akid.asn1 without changes x509_parser.h without changes x509_cert_parser.c with changes marked as __UBOOT__ x509_public_key.c with changes marked as __UBOOT__

---
 lib/Kconfig                   |   1 +
 lib/Makefile                  |   1 +
 lib/crypto/Kconfig            |  12 +
 lib/crypto/Makefile           |  17 +
 lib/crypto/x509.asn1          |  60 +++
 lib/crypto/x509_akid.asn1     |  35 ++
 lib/crypto/x509_cert_parser.c | 697 ++++++++++++++++++++++++++++++++++
 lib/crypto/x509_parser.h      |  57 +++
 lib/crypto/x509_public_key.c  | 292 ++++++++++++++
 9 files changed, 1172 insertions(+)
 create mode 100644 lib/crypto/x509.asn1
 create mode 100644 lib/crypto/x509_akid.asn1
 create mode 100644 lib/crypto/x509_cert_parser.c
 create mode 100644 lib/crypto/x509_parser.h
 create mode 100644 lib/crypto/x509_public_key.c
lib/Kconfig +1 -0
index 1e373f9f8455..d34a8da8ac60 100644
@@ -253,6 +253,7 @@ config AES
 	  present.
 
 source lib/rsa/Kconfig
+source lib/crypto/Kconfig
 
 config TPM
 	bool "Trusted Platform Module (TPM) Support"
lib/Makefile +1 -0
index 52bf33d17086..16ed26f31708 100644
@@ -18,6 +18,7 @@ obj-$(CONFIG_CMD_DHRYSTONE) += dhry/
 obj-$(CONFIG_ARCH_AT91) += at91/
 obj-$(CONFIG_OPTEE) += optee/
 obj-$(CONFIG_ASN1_DECODER) += asn1_decoder.o
+obj-y += crypto/
 
 obj-$(CONFIG_AES) += aes.o
 
lib/crypto/Kconfig +12 -0
index 9572ea8c87f3..aeb599c593ac 100644
@@ -27,4 +27,16 @@ config RSA_PUBLIC_KEY_PARSER
 	  public key data and provides the ability to instantiate a public
 	  key.
 
+config X509_CERTIFICATE_PARSER
+	bool "X.509 certificate parser"
+	depends on ASYMMETRIC_PUBLIC_KEY_SUBTYPE
+	select ASN1_DECODER
+	select ASN1_COMPILER
+	select OID_REGISTRY
+	select LIB_DATE
+	help
+	  This option provides support for parsing X.509 format blobs for key
+	  data and provides the ability to instantiate a crypto key from a
+	  public key packet found inside the certificate.
+
 endif # ASYMMETRIC_KEY_TYPE
lib/crypto/Makefile +17 -0
index 69330a9ebd27..d7e27be568a1 100644
@@ -19,3 +19,20 @@ rsa_public_key-y := \
 
 $(obj)/rsapubkey.asn1.o: $(obj)/rsapubkey.asn1.c $(obj)/rsapubkey.asn1.h
 $(obj)/rsa_helper.o: $(obj)/rsapubkey.asn1.h
+
+#
+# X.509 Certificate handling
+#
+obj-$(CONFIG_X509_CERTIFICATE_PARSER) += x509_key_parser.o
+x509_key_parser-y := \
+	x509.asn1.o \
+	x509_akid.asn1.o \
+	x509_cert_parser.o \
+	x509_public_key.o
+
+$(obj)/x509_cert_parser.o: \
+	$(obj)/x509.asn1.h \
+	$(obj)/x509_akid.asn1.h
+
+$(obj)/x509.asn1.o: $(obj)/x509.asn1.c $(obj)/x509.asn1.h
+$(obj)/x509_akid.asn1.o: $(obj)/x509_akid.asn1.c $(obj)/x509_akid.asn1.h
lib/crypto/x509.asn1 new file +60 -0

60 lines not on this page · raw message

lib/crypto/x509_akid.asn1 new file +35 -0
new file mode 100644
index 000000000000..1a33231a75a8
@@ -0,0 +1,35 @@
+-- X.509 AuthorityKeyIdentifier
+-- rfc5280 section 4.2.1.1
+
+AuthorityKeyIdentifier ::= SEQUENCE {
+	keyIdentifier			[0] IMPLICIT KeyIdentifier		OPTIONAL,
+	authorityCertIssuer		[1] IMPLICIT GeneralNames		OPTIONAL,
+	authorityCertSerialNumber	[2] IMPLICIT CertificateSerialNumber	OPTIONAL
+	}
+
+KeyIdentifier ::= OCTET STRING ({ x509_akid_note_kid })
+
+CertificateSerialNumber ::= INTEGER ({ x509_akid_note_serial })
+
+GeneralNames ::= SEQUENCE OF GeneralName
+
+GeneralName ::= CHOICE {
+	otherName			[0] ANY,
+	rfc822Name			[1] IA5String,
+	dNSName				[2] IA5String,
+	x400Address			[3] ANY,
+	directoryName			[4] Name ({ x509_akid_note_name }),
+	ediPartyName			[5] ANY,
+	uniformResourceIdentifier	[6] IA5String,
+	iPAddress			[7] OCTET STRING,
+	registeredID			[8] OBJECT IDENTIFIER
+	}
+
+Name ::= SEQUENCE OF RelativeDistinguishedName
+
+RelativeDistinguishedName ::= SET OF AttributeValueAssertion
+
+AttributeValueAssertion ::= SEQUENCE {
+	attributeType		OBJECT IDENTIFIER ({ x509_note_OID }),
+	attributeValue		ANY ({ x509_extract_name_segment })
+	}
lib/crypto/x509_cert_parser.c new file +697 -0

697 lines not on this page · raw message

lib/crypto/x509_parser.h new file +57 -0

57 lines not on this page · raw message

lib/crypto/x509_public_key.c new file +292 -0

292 lines not on this page · raw message

Signature · 1 line
2.21.0
#17 lore raw

PATCH v2 15/17 lib: crypto: add pkcs7 message parser

In reply to AKASHI Takahiro

Imported from linux kernel v5.3:
pkcs7.asn1 without changes
pkcs7.h with changes marked as __UBOOT__
pkcs7_parser.h without changes
pkcs7_parser.c with changes marked as __UBOOT__

---
 include/crypto/pkcs7.h    |  47 +++
 lib/crypto/Kconfig        |  10 +
 lib/crypto/Makefile       |  11 +
 lib/crypto/pkcs7.asn1     | 135 ++++++++
 lib/crypto/pkcs7_parser.c | 693 ++++++++++++++++++++++++++++++++++++++
 lib/crypto/pkcs7_parser.h |  65 ++++
 scripts/Makefile.build    |   4 +-
 7 files changed, 962 insertions(+), 3 deletions(-)
 create mode 100644 include/crypto/pkcs7.h
 create mode 100644 lib/crypto/pkcs7.asn1
 create mode 100644 lib/crypto/pkcs7_parser.c
 create mode 100644 lib/crypto/pkcs7_parser.h
include/crypto/pkcs7.h new file +47 -0
new file mode 100644
index 000000000000..8f5c8a7ee3b9
@@ -0,0 +1,47 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+/* PKCS#7 crypto data parser
+ *
+ * Copyright (C) 2012 Red Hat, Inc. All Rights Reserved.
+ * Written by David Howells (dhowells at redhat.com)
+ */
+
+#ifndef _CRYPTO_PKCS7_H
+#define _CRYPTO_PKCS7_H
+
+#ifndef __UBOOT__
+#include <linux/verification.h>
+#include <crypto/public_key.h>
+#endif
+
+struct key;
+struct pkcs7_message;
+
+/*
+ * pkcs7_parser.c
+ */
+extern struct pkcs7_message *pkcs7_parse_message(const void *data,
+						 size_t datalen);
+extern void pkcs7_free_message(struct pkcs7_message *pkcs7);
+
+extern int pkcs7_get_content_data(const struct pkcs7_message *pkcs7,
+				  const void **_data, size_t *_datalen,
+				  size_t *_headerlen);
+
+#ifndef __UBOOT__
+/*
+ * pkcs7_trust.c
+ */
+extern int pkcs7_validate_trust(struct pkcs7_message *pkcs7,
+				struct key *trust_keyring);
+
+/*
+ * pkcs7_verify.c
+ */
+extern int pkcs7_verify(struct pkcs7_message *pkcs7,
+			enum key_being_used_for usage);
+
+extern int pkcs7_supply_detached_data(struct pkcs7_message *pkcs7,
+				      const void *data, size_t datalen);
+#endif
+
+#endif /* _CRYPTO_PKCS7_H */
lib/crypto/Kconfig +10 -0
index aeb599c593ac..2b221b915aa6 100644
@@ -39,4 +39,14 @@ config X509_CERTIFICATE_PARSER
 	  data and provides the ability to instantiate a crypto key from a
 	  public key packet found inside the certificate.
 
+config PKCS7_MESSAGE_PARSER
+	bool "PKCS#7 message parser"
+	depends on X509_CERTIFICATE_PARSER
+	select ASN1_DECODER
+	select ASN1_COMPILER
+	select OID_REGISTRY
+	help
+	  This option provides support for parsing PKCS#7 format messages for
+	  signature data and provides the ability to verify the signature.
+
 endif # ASYMMETRIC_KEY_TYPE
lib/crypto/Makefile +11 -0
index d7e27be568a1..8267fee0a7b8 100644
@@ -36,3 +36,14 @@ $(obj)/x509_cert_parser.o: \
 
 $(obj)/x509.asn1.o: $(obj)/x509.asn1.c $(obj)/x509.asn1.h
 $(obj)/x509_akid.asn1.o: $(obj)/x509_akid.asn1.c $(obj)/x509_akid.asn1.h
+
+#
+# PKCS#7 message handling
+#
+obj-$(CONFIG_PKCS7_MESSAGE_PARSER) += pkcs7_message.o
+pkcs7_message-y := \
+	pkcs7.asn1.o \
+	pkcs7_parser.o
+
+$(obj)/pkcs7_parser.o: $(obj)/pkcs7.asn1.h
+$(obj)/pkcs7.asn1.o: $(obj)/pkcs7.asn1.c $(obj)/pkcs7.asn1.h
lib/crypto/pkcs7.asn1 new file +135 -0

135 lines not on this page · raw message

lib/crypto/pkcs7_parser.c new file +693 -0

693 lines not on this page · raw message

lib/crypto/pkcs7_parser.h new file +65 -0

65 lines not on this page · raw message

scripts/Makefile.build +1 -3
index 6d59ea91fac3..26eb701f8dea 100644
@@ -419,11 +419,9 @@ targets += $(multi-used-y) $(multi-used-m)
 intermediate_targets = $(foreach sfx, $(2), \
 				$(patsubst %$(strip $(1)),%$(sfx), \
 					$(filter %$(strip $(1)), $(targets))))
-# %.asn1.o <- %.asn1.[ch] <- %.asn1
 # %.lex.o <- %.lex.c <- %.l
 # %.tab.o <- %.tab.[ch] <- %.y
-targets += $(call intermediate_targets, .asn1.o, .asn1.c .asn1.h) \
-	   $(call intermediate_targets, .lex.o, .lex.c) \
+targets += $(call intermediate_targets, .lex.o, .lex.c) \
 	   $(call intermediate_targets, .tab.o, .tab.c .tab.h)
 
 # Descending
Signature · 1 line
2.21.0
#18 lore raw

PATCH v2 16/17 test: add lib specific Kconfig

In reply to AKASHI Takahiro

Adding new unit tests for library routines will make test/Kconfig messy. So just create a Kconfig file under lib.

---
 test/Kconfig     | 8 +-------
 test/lib/Kconfig | 7 +++++++
 2 files changed, 8 insertions(+), 7 deletions(-)
 create mode 100644 test/lib/Kconfig
test/Kconfig +1 -7
index 48a0e501f88f..3a5aabedd0ef 100644
@@ -6,13 +6,7 @@ menuconfig UNIT_TEST
 	  This does not require sandbox to be included, but it is most
 	  often used there.
 
-config UT_LIB
-	bool "Unit tests for library functions"
-	depends on UNIT_TEST
-	default y
-	help
-	  Enables the 'ut lib' command which tests library functions like
-	  memcat(), memcyp(), memmove().
+source "test/lib/Kconfig"
 
 config UT_TIME
 	bool "Unit tests for time functions"
test/lib/Kconfig new file +7 -0
new file mode 100644
index 000000000000..a983bdcaa3f1
@@ -0,0 +1,7 @@
+config UT_LIB
+	bool "Unit tests for library functions"
+	depends on UNIT_TEST
+	default y
+	help
+	  Enables the 'ut lib' command which tests library functions like
+	  memcat(), memcyp(), memmove().
Signature · 1 line
2.21.0
#19 lore raw

PATCH v2 16/17 test: add lib specific Kconfig

Quoted 35 lines · AKASHI Takahiro

Adding new unit tests for library routines will make test/Kconfig messy. So just create a Kconfig file under lib.

---
 test/Kconfig     | 8 +-------
 test/lib/Kconfig | 7 +++++++
 2 files changed, 8 insertions(+), 7 deletions(-)
 create mode 100644 test/lib/Kconfig
test/Kconfig +1 -7
index 48a0e501f88f..3a5aabedd0ef 100644
@@ -6,13 +6,7 @@ menuconfig UNIT_TEST
 	  This does not require sandbox to be included, but it is most
 	  often used there.
 
-config UT_LIB
-	bool "Unit tests for library functions"
-	depends on UNIT_TEST
-	default y
-	help
-	  Enables the 'ut lib' command which tests library functions like
-	  memcat(), memcyp(), memmove().
+source "test/lib/Kconfig"
 
 config UT_TIME
 	bool "Unit tests for time functions"
test/lib/Kconfig new file +0 -0
new file mode 100644
index 000000000000..a983bdcaa3f1

test/lib/Kconfig line 7

Quoted 7 lines
test/lib/Kconfig new file +7 -0
@@ -0,0 +1,7 @@
+config UT_LIB
+	bool "Unit tests for library functions"
+	depends on UNIT_TEST
+	default y
+	help
+	  Enables the 'ut lib' command which tests library functions like
+	  memcat(), memcyp(), memmove().

But we then only add one more entry in the next patch. How many library tests do you see adding when everything is done? If it's less than 5, we should just keep things in the test/Kconfig file I think. The rest of the series seems fine, thanks!

Signature · 8 lines
Tom
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 833 bytes
Desc: not available
URL: <http://lists.denx.de/pipermail/u-boot/attachments/20191025/6ee79e81/attachment.sig>
#20 lore raw

PATCH v2 16/17 test: add lib specific Kconfig

Tom,

Quoted 38 lines · Tom Rini
Quoted 37 lines · AKASHI Takahiro

Adding new unit tests for library routines will make test/Kconfig messy. So just create a Kconfig file under lib.

---
 test/Kconfig     | 8 +-------
 test/lib/Kconfig | 7 +++++++
 2 files changed, 8 insertions(+), 7 deletions(-)
 create mode 100644 test/lib/Kconfig
test/Kconfig +1 -7
index 48a0e501f88f..3a5aabedd0ef 100644
@@ -6,13 +6,7 @@ menuconfig UNIT_TEST
 	  This does not require sandbox to be included, but it is most
 	  often used there.
 
-config UT_LIB
-	bool "Unit tests for library functions"
-	depends on UNIT_TEST
-	default y
-	help
-	  Enables the 'ut lib' command which tests library functions like
-	  memcat(), memcyp(), memmove().
+source "test/lib/Kconfig"
 
 config UT_TIME
 	bool "Unit tests for time functions"
test/lib/Kconfig new file +2 -0
new file mode 100644
index 000000000000..a983bdcaa3f1
@@ -0,0 +1,7 @@
+config UT_LIB
+	bool "Unit tests for library functions"
Quoted 8 lines

test/lib/Kconfig line 7

Quoted 5 lines
test/lib/Kconfig new file +5 -0
+	depends on UNIT_TEST
+	default y
+	help
+	  Enables the 'ut lib' command which tests library functions like
+	  memcat(), memcyp(), memmove().

But we then only add one more entry in the next patch. How many library tests do you see adding when everything is done?

Only this one.

Quoted 3 lines

If it's less than 5,
we should just keep things in the test/Kconfig file I think. The rest of the series seems fine, thanks!

As you know, there are bunch of library functions which are NOT tested by "ut". I would expect that more and more tests should come as someone modify any of libraries functions.

But I don't have a strong opinion here and will follow your suggestion.

-Takahiro Akashi

Quoted 2 lines
Signature · 1 line
Tom
#21 lore raw

PATCH v2 17/17 test: add asn1 unit test

In reply to AKASHI Takahiro

This test will exercise asn1 compiler as well as asn1 decoder functions via various parsers.

---
 test/lib/Kconfig  |  18 ++-
 test/lib/Makefile |   2 +
 test/lib/asn1.c   | 392 ++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 411 insertions(+), 1 deletion(-)
 create mode 100644 test/lib/asn1.c
test/lib/Kconfig +17 -1
index a983bdcaa3f1..f8bf36bf65f1 100644
@@ -4,4 +4,20 @@ config UT_LIB
 	default y
 	help
 	  Enables the 'ut lib' command which tests library functions like
-	  memcat(), memcyp(), memmove().
+	  memcat(), memcyp(), memmove() and ASN1 compiler/decoder.
+
+if UT_LIB
+
+config UT_LIB_ASN1
+	bool "Unit test for asn1 compiler and decoder function"
+	default y
+	imply ASYMMETRIC_KEY_TYPE
+	imply ASYMMETRIC_PUBLIC_KEY_SUBTYPE
+	imply X509_CERTIFICATE_PARSER
+	imply PKCS7_MESSAGE_PARSER
+	imply RSA_PUBLIC_KEY_PARSER
+	help
+	  Enables a test which exercises asn1 compiler and decoder function
+	  via various parsers.
+
+endif
test/lib/Makefile +2 -0
index 308c61708ee3..50a07e717d27 100644
@@ -6,3 +6,5 @@ obj-y += cmd_ut_lib.o
 obj-y += hexdump.o
 obj-y += lmb.o
 obj-y += string.o
+
+obj-$(CONFIG_UT_LIB_ASN1) += asn1.o
test/lib/asn1.c new file +392 -0

392 lines not on this page · raw message

Signature · 1 line
2.21.0
#22 lore raw

v2 cover import x509/pkcs7 parsers from linux

In reply to AKASHI Takahiro
Quoted 35 lines · AKASHI Takahiro

# This patch set should be merged first prior to my rsa extension patch

# due to some dependency. I plan to send out a new version of rsa
# extension next week.

Asn1 parsers of x509 certificates and pkcs7 messages are required to implement image authentication and variable authentication as part of UEFI secure boot feature.

As we discussed before in the thread[1], most people insisted that we should re-use corresponding source code from Linux repository for this purpose.

Here is my attempt to import all the necessary files from Linux; Those will eventually be part of UEFI secure boot implementation, but I'd like to get early feedback from other peoples before submitting the whole patchset so that they will be better formatted for merging.

My approach here is
* files from the latest Linux
* modify files as little as possible
* mark/protect unavoidable changes with "#if(n)def __UBOOT__" so that future fixes/differences in Linux repository will easily be applied to U-Boot.

Known issues:
* checkpatch.pl
Checkpatch.pl will complain with a bunch of warnings/errors but I intentionally left them unchanged for the sake of better maintainability I said above.

* unit test
I'm still waiting for a feedback from Tom[2] regarding how we should enable "unit test" for ASN1 compiler/decoder on sandbox and others in Travis CI.

Quoted 4 lines

* Travis CI
It is still running against this very version, but I'm sure I have fixed all the issues (as long as Heinrich's [3] is applied); I will let you know the result on Monday.

All Travis CI tests have passed.

-Takahiro Akashi

Quoted 132 lines

Any comments will be appreciated.
-Takahiro Akashi

[1] https://lists.denx.de/pipermail/u-boot/2019-April/366423.html
[2] https://lists.denx.de/pipermail/u-boot/2019-October/387734.html
[3] https://lists.denx.de/pipermail/u-boot/2019-October/385643.html

Changes in v2 (Oct 25, 2019)
* revise commit messages, describing what files are modified or not.
* move kmemdump() in ubifs.c to linux_compat.c for general use (patch#1)
* add patch#2
* move date.c to lib/ for general use (patch#3)
* implement mktime64() with rtc_mktime() (patch#4)
* move asn1_compiler.c to tools/ (patch#7)
* change CONFIG_BUILD_ASN1 to CONFIG_ASN1_COMPILER (patch#7)
* add clean rule to asn1_compiler-generated files to clean targets (patch#8)
* change CONFIG_ASN1 to CONFIG_ASN1_DECODER (patch#9)
* add README for asn1 compiler/decoder (patch#10)
* move build_oid_registory to scripts/ (patch#11)
* shuffle an order of patches (patch#13,#14,#15)
* add a new config CONFIG_RSA_PUBLIC_KEY_PARSER so that it can be
* modify Kconfig dependency (patch#13,#14,#15)
compiled in independently (patch#13)
* add unit test (patch#16,#17)

Changes in v1 (Oct 11, 2019) from RFC
* change the kernel code base from v5.0 to v5.3
* add preparatory patches (#1, #2 and #3)
* comment off x509_check_for_self_signed() which is not useful for UEFI secure boot (patch#9)
* improve usages of "#if(n)def __UBOOT__* to minimize differences between U-Boot and linux kernel

AKASHI Takahiro (17):
linux_compat: move kmemdup() from ubifs.c to linux_compat.c rtc.h: add struct udevice declaration
rtc: move date.c from drivers/rtc/ to lib/
lib: add mktime64() for linux compatibility
include: kernel.h: include printk.h
linux/time.h: include vsprintf.h
cmd: add asn1_compiler
Makefile: add build script for asn1 parsers
lib: add asn1 decoder
doc: add README for asn1 compiler and decoder
lib: add oid registry utility
lib: crypto: add public key utility
lib: crypto: add rsa public key parser
lib: crypto: add x509 parser
lib: crypto: add pkcs7 message parser
test: add lib specific Kconfig
test: add asn1 unit test

 Makefile                          |    1 +
 cmd/Kconfig                       |    1 +
 doc/README.asn1                   |   40 +
 drivers/rtc/Kconfig               |    1 +
 drivers/rtc/Makefile              |    1 -
 fs/ubifs/ubifs.c                  |   19 +-
 include/crypto/internal/rsa.h     |   57 +
 include/crypto/pkcs7.h            |   47 +
 include/crypto/public_key.h       |   90 ++
 include/keys/asymmetric-type.h    |   88 ++
 include/linux/asn1.h              |   65 ++
 include/linux/asn1_ber_bytecode.h |   89 ++
 include/linux/asn1_decoder.h      |   20 +
 include/linux/kernel.h            |    2 +-
 include/linux/oid_registry.h      |  117 +++
 include/linux/time.h              |   11 +
 include/rtc.h                     |    2 +
 lib/Kconfig                       |   17 +
 lib/Makefile                      |   20 +
 lib/asn1_decoder.c                |  527 ++++++++++
 lib/crypto/Kconfig                |   52 +
 lib/crypto/Makefile               |   49 +
 lib/crypto/asymmetric_type.c      |  668 ++++++++++++
 lib/crypto/pkcs7.asn1             |  135 +++
 lib/crypto/pkcs7_parser.c         |  693 +++++++++++++
 lib/crypto/pkcs7_parser.h         |   65 ++
 lib/crypto/public_key.c           |  376 +++++++
 lib/crypto/rsa_helper.c           |  198 ++++
 lib/crypto/rsapubkey.asn1         |    4 +
 lib/crypto/x509.asn1              |   60 ++
 lib/crypto/x509_akid.asn1         |   35 +
 lib/crypto/x509_cert_parser.c     |  697 +++++++++++++
 lib/crypto/x509_parser.h          |   57 +
 lib/crypto/x509_public_key.c      |  292 ++++++
 {drivers/rtc => lib}/date.c       |   23 +-
 lib/linux_compat.c                |   17 +
 lib/oid_registry.c                |  179 ++++
 scripts/Makefile.build            |    4 +-
 scripts/build_OID_registry        |  203 ++++
 test/Kconfig                      |    8 +-
 test/lib/Kconfig                  |   23 +
 test/lib/Makefile                 |    2 +
 test/lib/asn1.c                   |  392 +++++++
 tools/Makefile                    |    3 +
 tools/asn1_compiler.c             | 1611 +++++++++++++++++++++++++++++
 45 files changed, 7030 insertions(+), 31 deletions(-)
 create mode 100644 doc/README.asn1
 create mode 100644 include/crypto/internal/rsa.h
 create mode 100644 include/crypto/pkcs7.h
 create mode 100644 include/crypto/public_key.h
 create mode 100644 include/keys/asymmetric-type.h
 create mode 100644 include/linux/asn1.h
 create mode 100644 include/linux/asn1_ber_bytecode.h
 create mode 100644 include/linux/asn1_decoder.h
 create mode 100644 include/linux/oid_registry.h
 create mode 100644 lib/asn1_decoder.c
 create mode 100644 lib/crypto/Kconfig
 create mode 100644 lib/crypto/Makefile
 create mode 100644 lib/crypto/asymmetric_type.c
 create mode 100644 lib/crypto/pkcs7.asn1
 create mode 100644 lib/crypto/pkcs7_parser.c
 create mode 100644 lib/crypto/pkcs7_parser.h
 create mode 100644 lib/crypto/public_key.c
 create mode 100644 lib/crypto/rsa_helper.c
 create mode 100644 lib/crypto/rsapubkey.asn1
 create mode 100644 lib/crypto/x509.asn1
 create mode 100644 lib/crypto/x509_akid.asn1
 create mode 100644 lib/crypto/x509_cert_parser.c
 create mode 100644 lib/crypto/x509_parser.h
 create mode 100644 lib/crypto/x509_public_key.c
 rename {drivers/rtc => lib}/date.c (81%)
 create mode 100644 lib/oid_registry.c
 create mode 100755 scripts/build_OID_registry
 create mode 100644 test/lib/Kconfig
 create mode 100644 test/lib/asn1.c
 create mode 100644 tools/asn1_compiler.c
Signature · 1 line
2.21.0