diff mbox series

[RFC,v2,1/8] dtoc: openssl: Add GetHexOctet method

Message ID 20230926-binman-firewalling-v2-1-b1a084ec634d@ti.com
State RFC
Delegated to: Tom Rini
Headers show
Series ATF and OP-TEE Firewalling for K3 devices. | expand

Commit Message

Manorit Chawdhry Sept. 26, 2023, 7:58 a.m. UTC
HexOctet format is used by openssl for FORMAT:HEX,OCT property in x509
certificates. Add a helper function to extract the integer numbers in
HEX,OCT format to pass to openssl directly.

Signed-off-by: Manorit Chawdhry <m-chawdhry@ti.com>
---
 tools/dtoc/fdt_util.py | 20 ++++++++++++++++++++
 1 file changed, 20 insertions(+)

Comments

Simon Glass Oct. 2, 2023, 1:17 a.m. UTC | #1
Hi Manorit,

On Tue, 26 Sept 2023 at 01:58, Manorit Chawdhry <m-chawdhry@ti.com> wrote:
>
> HexOctet format is used by openssl for FORMAT:HEX,OCT property in x509
> certificates. Add a helper function to extract the integer numbers in
> HEX,OCT format to pass to openssl directly.
>
> Signed-off-by: Manorit Chawdhry <m-chawdhry@ti.com>
> ---
>  tools/dtoc/fdt_util.py | 20 ++++++++++++++++++++
>  1 file changed, 20 insertions(+)
>
> diff --git a/tools/dtoc/fdt_util.py b/tools/dtoc/fdt_util.py
> index f1f70568cfef..d51dbf5633d0 100644
> --- a/tools/dtoc/fdt_util.py
> +++ b/tools/dtoc/fdt_util.py
> @@ -100,6 +100,26 @@ def EnsureCompiled(fname, tmpdir=None, capture_stderr=False):
>      command.run(dtc, *args, capture_stderr=capture_stderr)
>      return dtb_output
>
> +def GetHexOctet(node, propname, default=None):

What is a hex octet?

> +    """Get an integer from a property in hex octet form required by openssl
> +

You should mention what size property is permitted.

> +    Args:
> +        node: Node object to read from
> +        propname: property name to read
> +        default: Default value to use if the node/property do not exist
> +
> +    Returns:
> +        Integer value read as a String in Hex Octet Form
> +    """
> +    prop = node.props.get(propname)
> +    if not isinstance(prop.value, list) or len(prop.value) != 2:
> +        value = GetInt(node, propname)
> +    elif isinstance(prop.value, list) and len(prop.value) == 2:
> +        value = GetInt64(node, propname)

What if it is neither of those?

> +
> +    hex_value = '%x' % (value)
> +    return ('0' * (len(hex_value) & 1)) + hex_value

Can you do:

return f'{value:02x}'

?


> +
>  def GetInt(node, propname, default=None):
>      """Get an integer from a property
>
>
> --
> 2.41.0
>
Manorit Chawdhry Oct. 3, 2023, 10:41 a.m. UTC | #2
Hi Simon,

On 19:17-20231001, Simon Glass wrote:
> Hi Manorit,
> 
> On Tue, 26 Sept 2023 at 01:58, Manorit Chawdhry <m-chawdhry@ti.com> wrote:
> >
> > HexOctet format is used by openssl for FORMAT:HEX,OCT property in x509
> > certificates. Add a helper function to extract the integer numbers in
> > HEX,OCT format to pass to openssl directly.
> >
> > Signed-off-by: Manorit Chawdhry <m-chawdhry@ti.com>
> > ---
> >  tools/dtoc/fdt_util.py | 20 ++++++++++++++++++++
> >  1 file changed, 20 insertions(+)
> >
> > diff --git a/tools/dtoc/fdt_util.py b/tools/dtoc/fdt_util.py
> > index f1f70568cfef..d51dbf5633d0 100644
> > --- a/tools/dtoc/fdt_util.py
> > +++ b/tools/dtoc/fdt_util.py
> > @@ -100,6 +100,26 @@ def EnsureCompiled(fname, tmpdir=None, capture_stderr=False):
> >      command.run(dtc, *args, capture_stderr=capture_stderr)
> >      return dtb_output
> >
> > +def GetHexOctet(node, propname, default=None):
> 
> What is a hex octet?
> 

It is actually a Hex number in Octet form, basically using 0x0a instead
of 0xa type of thing. 

OpenSSL has complained when we use FORMAT:HEX,OCT:0 type of string
instead of FORMAT:HEX,OCT:00. Tbvh I still haven't been able to find a
clear documentation for this but this is what I have been able to figure
out based on my tests.

> > +    """Get an integer from a property in hex octet form required by openssl
> > +
> 
> You should mention what size property is permitted.
> 
> > +    Args:
> > +        node: Node object to read from
> > +        propname: property name to read
> > +        default: Default value to use if the node/property do not exist
> > +
> > +    Returns:
> > +        Integer value read as a String in Hex Octet Form
> > +    """
> > +    prop = node.props.get(propname)
> > +    if not isinstance(prop.value, list) or len(prop.value) != 2:
> > +        value = GetInt(node, propname)
> > +    elif isinstance(prop.value, list) and len(prop.value) == 2:
> > +        value = GetInt64(node, propname)
> 
> What if it is neither of those?
> 
> > +
> > +    hex_value = '%x' % (value)
> > +    return ('0' * (len(hex_value) & 1)) + hex_value
> 
> Can you do:
> 
> return f'{value:02x}'
> 

With the following suggestion I don't think I need the patch at all
anymore given that no one else seems to be requiring this HEX,OCT just
yet and am still in the process of finding a clear documentation for it.

Will be dropping that patch after testing if not required to be generic.
Thanks!

Regards,
Manorit

> ?
> 
> 
> > +
> >  def GetInt(node, propname, default=None):
> >      """Get an integer from a property
> >
> >
> > --
> > 2.41.0
> >
diff mbox series

Patch

diff --git a/tools/dtoc/fdt_util.py b/tools/dtoc/fdt_util.py
index f1f70568cfef..d51dbf5633d0 100644
--- a/tools/dtoc/fdt_util.py
+++ b/tools/dtoc/fdt_util.py
@@ -100,6 +100,26 @@  def EnsureCompiled(fname, tmpdir=None, capture_stderr=False):
     command.run(dtc, *args, capture_stderr=capture_stderr)
     return dtb_output
 
+def GetHexOctet(node, propname, default=None):
+    """Get an integer from a property in hex octet form required by openssl
+
+    Args:
+        node: Node object to read from
+        propname: property name to read
+        default: Default value to use if the node/property do not exist
+
+    Returns:
+        Integer value read as a String in Hex Octet Form
+    """
+    prop = node.props.get(propname)
+    if not isinstance(prop.value, list) or len(prop.value) != 2:
+        value = GetInt(node, propname)
+    elif isinstance(prop.value, list) and len(prop.value) == 2:
+        value = GetInt64(node, propname)
+
+    hex_value = '%x' % (value)
+    return ('0' * (len(hex_value) & 1)) + hex_value
+
 def GetInt(node, propname, default=None):
     """Get an integer from a property