Re: [U-Boot] [PATCH v1 04/11] cmd: add asn1_compiler

2019-10-17 Thread AKASHI Takahiro
On Sat, Oct 12, 2019 at 02:22:14PM +0200, Heinrich Schuchardt wrote:
> On 10/11/19 9:41 AM, AKASHI Takahiro wrote:
> >Imported from linux kernel v5.3.
> >
> >This host command will be used to create a ASN1 parser, for example,
> >for pkcs7 or x509.
> >
> >Signed-off-by: AKASHI Takahiro 
> >---
> >  cmd/Kconfig   |3 +
> >  include/linux/asn1.h  |   65 ++
> >  include/linux/asn1_ber_bytecode.h |   89 ++
> >  include/linux/asn1_decoder.h  |   20 +
> >  scripts/Makefile  |3 +
> >  scripts/asn1_compiler.c   | 1611 +
> 
> Except for Makefile and Kconfig these are verbatime copies of the Linux
> files. OK.
> 
> >  6 files changed, 1791 insertions(+)
> >  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 scripts/asn1_compiler.c
> >
> >diff --git a/cmd/Kconfig b/cmd/Kconfig
> >index 4e61565aab16..605dcfdf4282 100644
> >--- a/cmd/Kconfig
> >+++ b/cmd/Kconfig
> >@@ -184,6 +184,9 @@ endmenu
> >  config BUILD_BIN2C
> > bool
> >
> >+config BUILD_ASN1
> >+bool
> >+
> >  comment "Commands"
> >
> >  menu "Info commands"
> >diff --git a/include/linux/asn1.h b/include/linux/asn1.h
> >new file mode 100644
> >index ..a4d0bdd10711
> >--- /dev/null
> >+++ b/include/linux/asn1.h
> >@@ -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 (dhowe...@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 */
> >diff --git a/include/linux/asn1_ber_bytecode.h 
> >b/include/linux/asn1_ber_bytecode.h
> >new file mode 100644
> >index ..b38361953a48
> >--- /dev/null
> >+++ b/include/linux/asn1_ber_bytecode.h
> >@@ -0,0 +1,89 @@
> >+/* SPDX-License-Identifier: GPL-2.0-or-later */
> >+/* ASN.1 BER/DER/CER parsing state machine internal definitions
> >+ *
> >+ * Copyright (C) 2012 Red Hat, Inc. All Rights Reserved.
> >+ * Written by David Howells (dhowe...@redhat.com)
> >+ */
> >+
> >+#ifndef _LINUX_ASN1_BER_BYTECODE_H
> >+#define _LINUX_ASN1_BER_BYTECODE_H
> >+
> >+#ifdef __KERNEL__
> >+#include 
> >+#endif
> >+#include 
> >+
> >+typedef int (*asn1_action_t)(void *context,
> >+ size_t hdrlen, /* In case of ANY type */
> >+ unsigned char tag, /* In case of ANY type */
> >+ const void *value, size_t vlen);
> >+
> >+struct asn1_decoder {
> >+const unsigned char *machine;
> >+size_t machlen;
> >+const asn1_action_t *actions;
> >+};
> >+
> >+enum asn1_opcode {
> >+/* 

Re: [U-Boot] [PATCH v1 04/11] cmd: add asn1_compiler

2019-10-12 Thread Heinrich Schuchardt

On 10/11/19 9:41 AM, AKASHI Takahiro wrote:

Imported from linux kernel v5.3.

This host command will be used to create a ASN1 parser, for example,
for pkcs7 or x509.

Signed-off-by: AKASHI Takahiro 
---
  cmd/Kconfig   |3 +
  include/linux/asn1.h  |   65 ++
  include/linux/asn1_ber_bytecode.h |   89 ++
  include/linux/asn1_decoder.h  |   20 +
  scripts/Makefile  |3 +
  scripts/asn1_compiler.c   | 1611 +


Except for Makefile and Kconfig these are verbatime copies of the Linux
files. OK.


  6 files changed, 1791 insertions(+)
  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 scripts/asn1_compiler.c

diff --git a/cmd/Kconfig b/cmd/Kconfig
index 4e61565aab16..605dcfdf4282 100644
--- a/cmd/Kconfig
+++ b/cmd/Kconfig
@@ -184,6 +184,9 @@ endmenu
  config BUILD_BIN2C
bool

+config BUILD_ASN1
+   bool
+
  comment "Commands"

  menu "Info commands"
diff --git a/include/linux/asn1.h b/include/linux/asn1.h
new file mode 100644
index ..a4d0bdd10711
--- /dev/null
+++ b/include/linux/asn1.h
@@ -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 (dhowe...@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_BITS0xc0
+
+
+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 */
diff --git a/include/linux/asn1_ber_bytecode.h 
b/include/linux/asn1_ber_bytecode.h
new file mode 100644
index ..b38361953a48
--- /dev/null
+++ b/include/linux/asn1_ber_bytecode.h
@@ -0,0 +1,89 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+/* ASN.1 BER/DER/CER parsing state machine internal definitions
+ *
+ * Copyright (C) 2012 Red Hat, Inc. All Rights Reserved.
+ * Written by David Howells (dhowe...@redhat.com)
+ */
+
+#ifndef _LINUX_ASN1_BER_BYTECODE_H
+#define _LINUX_ASN1_BER_BYTECODE_H
+
+#ifdef __KERNEL__
+#include 
+#endif
+#include 
+
+typedef int (*asn1_action_t)(void *context,
+size_t hdrlen, /* In case of ANY type */
+unsigned char tag, /* In case of ANY type */
+const void *value, size_t vlen);
+
+struct asn1_decoder {
+   const unsigned char *machine;
+   size_t machlen;
+   const asn1_action_t *actions;
+};
+
+enum asn1_opcode {
+   /* The tag-matching ops come first and the odd-numbered slots
+* are for OR_SKIP ops.
+*/
+#define ASN1_OP_MATCH__SKIP  0x01
+#define ASN1_OP_MATCH__ACT   0x02
+#define ASN1_OP_MATCH__JUMP  0x04
+#define ASN1_OP_MATCH__ANY   0x08
+#define ASN1_OP_MATCH__COND  0x10
+
+