Bug 1059926: Give DottedOIDToCode.py the ability to generate more encodings, r=keeler
authorBrian Smith <brian@briansmith.org>
Thu, 28 Aug 2014 09:58:13 -0700
changeset 14665 e8409018d8ecc61d47068a7141a5e5608529cc48
parent 14664 5dd940a541d303ac12f82e4f651b7fe3e6d77228
child 14666 0d9e1adba9c0fae4739996826987c08ce26b1ebf
push id3202
push userfranziskuskiefer@gmail.com
push dateMon, 01 Oct 2018 08:30:12 +0000
reviewerskeeler
bugs1059926
Bug 1059926: Give DottedOIDToCode.py the ability to generate more encodings, r=keeler
lib/mozpkix/tools/DottedOIDToCode.py
--- a/lib/mozpkix/tools/DottedOIDToCode.py
+++ b/lib/mozpkix/tools/DottedOIDToCode.py
@@ -15,99 +15,170 @@
 #
 # Unless required by applicable law or agreed to in writing, software
 # distributed under the License is distributed on an "AS IS" BASIS,
 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
 from __future__ import print_function
+import argparse
+import itertools
 import sys
 
-def base128Stringified(value):
+def base128(value):
     """
-    Encodes the given integral value into a string that is an encoded comma-
-    separated series of bytes, base-128, with all but the last byte having
-    the high bit set, in C++ hex notation, as required by the DER rules for the
-    nodes of an OID after the first two.
+    Given an integral value, returns an array of the base-128 representation
+    of that value, with all but the last byte having the high bit set as
+    required by the DER rules for the nodes of an OID after the first two
+    bytes.
 
-    >>> base128Stringified(1)
-    '0x01'
-    >>> base128Stringified(10045)
-    '0xce, 0x3d'
+    >>> base128(1)
+    [1]
+    >>> base128(10045)
+    [206, 61]
     """
+
     if value < 0:
         raise ValueError("An OID must have only positive-value nodes.")
 
-    format = "0x%.2x"
-
     # least significant byte has highest bit unset
-    result = format % (value % 0x80)
+    result = [value % 0x80]
     value /= 0x80
 
     while value != 0:
-        # other bytes have highest bit set
-        result = (format % (0x80 | (value % 0x80))) + ", " + result
+        result = [0x80 | (value % 0x80)] + result
         value /= 0x80
 
     return result
-    
-def dottedOIDToCEncoding(dottedOID):
+
+def dottedOIDToEncodedArray(dottedOID):
     """
     Takes a dotted OID string (e.g. '1.2.840.10045.4.3.4') as input, and
-    returns a string that contains the hex encoding of the OID in C++ literal
-    notation, e.g. '0x2a, 0x86, 0x48, 0xce, 0x3d, 0x04, 0x03, 0x04'. Note that
-    the ASN.1 tag and length are *not* included in the result.
+    returns an array that contains the DER encoding of its value, without
+    the tag and length (e.g. [0x2a, 0x86, 0x48, 0xce, 0x3d, 0x04, 0x03, 0x04]).
     """
     nodes = [int(x) for x in dottedOID.strip().split(".")]
     if len(nodes) < 2:
         raise ValueError("An OID must have at least two nodes.")
     if not (0 <= nodes[0] <= 2):
         raise ValueError("The first node of an OID must be 0, 1, or 2.")
     if not (0 <= nodes[1] <= 39):
         # XXX: Does this restriction apply when the first part is 2?
         raise ValueError("The second node of an OID must be 0-39.")
     firstByte = (40 * nodes[0]) + nodes[1]
-    allStringified = [base128Stringified(x) for x in [firstByte] + nodes[2:]]
-    return ", ".join(allStringified)
+    restBase128 = [base128(x) for x in nodes[2:]]
+    return [firstByte] + list(itertools.chain.from_iterable(restBase128))
+
+def dottedOIDToCArray(dottedOID, mode):
+    """
+    Takes a dotted OID string (e.g. '1.2.840.10045.4.3.4') as input, and
+    returns a string that contains the hex encoding of the OID in C++ literal
+    notation, e.g. '0x2a, 0x86, 0x48, 0xce, 0x3d, 0x04, 0x03, 0x04'.
+    """
+    bytes = dottedOIDToEncodedArray(dottedOID)
+
+    if mode != "value":
+        bytes = [0x06, len(bytes)] + bytes
+
+    if mode == "alg":
+        # Wrap the DER-encoded OID in a SEQUENCE to create an
+        # AlgorithmIdentifier with no parameters.
+        bytes = [0x30, len(bytes)] + bytes
+
+    return ", ".join(["0x%.2x" % b for b in bytes])
 
 def specNameToCName(specName):
     """
     Given an string containing an ASN.1 name, returns a string that is a valid
     C++ identifier that is as similar to that name as possible. Since most
     ASN.1 identifiers used in PKIX specifications are legal C++ names except
     for containing hyphens, this function just converts the hyphens to
     underscores. This may need to be improved in the future if we encounter
     names with other funny characters.
     """
     return specName.replace("-", "_")
 
-def toCode(programName, specName, dottedOID):
+def toCode(programName, specName, dottedOID, mode):
     """
     Given an ASN.1 name and a string containing the dotted representation of an
     OID, returns a string that contains a C++ declaration for a named constant
-    that contains that OID value. Note that the ASN.1 tag and length are *not*
-    included in the result.
+    that contains that OID value. If mode is "value" then only the value of
+    the OID (without the tag or length) will be included in the output. If mode
+    is "tlv" then the value will be prefixed with the tag and length. If mode
+    is "alg" then the value will be a complete der-encoded AlgorithmIdentifier
+    with no parameters.
+
+    This:
+
+        toCode("DottedOIDToCode.py", "ecdsa-with-SHA512", "1.2.840.10045.4.3.4",
+               "value")
+
+    would result in a string like:
+
+        // python DottedOIDToCode.py ecdsa-with-SHA512 1.2.840.10045.4.3.4
+        static const uint8_t ecdsa_with_SHA512[] = {
+          0x2a, 0x86, 0x48, 0xce, 0x3d, 0x04, 0x03, 0x04
+        };
 
     This:
-        toCode("DottedOIDToCode.py", "ecdsa-with-SHA512", "1.2.840.10045.4.3.4")
+
+        toCode("DottedOIDToCode.py", "ecdsa-with-SHA512", "1.2.840.10045.4.3.4",
+               "tlv")
+
     would result in a string like:
-      // python DottedOIDToCode.py ecdsa-with-SHA512 1.2.840.10045.4.3.4
-      static const uint8_t ecdsa_with_SHA512[] = {
-        0x2a, 0x86,0x48, 0xce,0x3d, 0x04, 0x03, 0x04
-      };
+
+        // python DottedOIDToCode.py --tlv ecdsa-with-SHA512 1.2.840.10045.4.3.4
+        static const uint8_t tlv_ecdsa_with_SHA512[] = {
+          0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x04, 0x03, 0x04
+        };
+
+    This:
+
+        toCode("DottedOIDToCode.py", "ecdsa-with-SHA512", "1.2.840.10045.4.3.4",
+               "alg")
+
+    would result in a string like:
+
+        // python DottedOIDToCode.py --alg ecdsa-with-SHA512 1.2.840.10045.4.3.4
+        static const uint8_t alg_ecdsa_with_SHA512[] = {
+          0x30, 0x0a, 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x04, 0x03, 0x04
+        };
     """
+    programNameWithOptions = programName
+    varName = specNameToCName(specName)
+    if mode == "tlv":
+        programNameWithOptions += " --tlv"
+        varName = "tlv_" + varName
+    elif mode == "alg":
+        programNameWithOptions += " --alg"
+        varName = "alg_" + varName
+
     return ("  // python %s %s %s\n" +
             "  static const uint8_t %s[] = {\n" +
             "    %s\n" +
-            "  };\n") % (programName, specName, dottedOID,
-                         specNameToCName(specName),
-                         dottedOIDToCEncoding(dottedOID))
+            "  };\n") % (programNameWithOptions, specName, dottedOID, varName,
+                         dottedOIDToCArray(dottedOID, mode))
 
 if __name__ == "__main__":
-    if len(sys.argv) != 3:
-        print("usage:   python %s <name> <dotted-oid>" % sys.argv[0],
-              file=sys.stderr)
-        print("example: python %s ecdsa-with-SHA1 1.2.840.10045.4.1" %
-                  sys.argv[0], file=sys.stderr)
-        sys.exit(1)
+    parser = argparse.ArgumentParser(
+                 description="Generate code snippets to handle OIDs in C++",
+                 epilog="example: python %s ecdsa-with-SHA1 1.2.840.10045.4.1"
+                            % sys.argv[0])
+    group = parser.add_mutually_exclusive_group()
+    group.add_argument("--tlv", action='store_true',
+                       help="Wrap the encoded OID value with the tag and length")
+    group.add_argument("--alg", action='store_true',
+                       help="Wrap the encoded OID value in an encoded SignatureAlgorithm")
+    parser.add_argument("name",
+                        help="The name given to the OID in the specification")
+    parser.add_argument("dottedOID", metavar="dotted-oid",
+                        help="The OID value, in dotted notation")
 
-    print(toCode(sys.argv[0], sys.argv[1], sys.argv[2]))
+    args = parser.parse_args()
+    if args.alg:
+        mode = 'alg'
+    elif args.tlv:
+        mode = 'tlv'
+    else:
+        mode = 'value'
+
+    print(toCode(sys.argv[0], args.name, args.dottedOID, mode))