diff options
author | Jack Lloyd <[email protected]> | 2016-10-22 00:51:13 -0400 |
---|---|---|
committer | Jack Lloyd <[email protected]> | 2016-11-03 11:56:50 -0400 |
commit | 62e55f484a7a03e2532875696eb2479a577878e9 (patch) | |
tree | 71c86436d9c3dd2059f7d0024f204e66e535b2b2 /src/scripts/oids.py | |
parent | b1021ca76bb3c47b1b520421ccece38d772e5907 (diff) |
Remove ability to add OIDS at runtime. Remove global OID lock.
OID map is now generated from an input file on an as needed basis.
Just uses a sequence of ifs - simple, fast, and small code size.
Merges oid_lookup sub-module which was already required by asn1 anyway,
so completely non-optional.
Removes @neusdan's nice OID tests since without any runtime adds the
tests are moot.
Diffstat (limited to 'src/scripts/oids.py')
-rwxr-xr-x | src/scripts/oids.py | 162 |
1 files changed, 162 insertions, 0 deletions
diff --git a/src/scripts/oids.py b/src/scripts/oids.py new file mode 100755 index 000000000..e3c232211 --- /dev/null +++ b/src/scripts/oids.py @@ -0,0 +1,162 @@ +#!/usr/bin/python2 + +""" +(C) 2016 Jack Lloyd + +Botan is released under the Simplified BSD License (see license.txt) +""" + +import sys +import datetime +import re + +def format_map(m, for_oid = False): + s = '' + for k in sorted(m.keys()): + v = m[k] + + if len(s) > 0: + s += ' ' + + if for_oid: + s += '{ "%s", OID("%s") },\n' % (k,v) + else: + s += '{ "%s", "%s" },\n' % (k,v) + + s = s[:-2] # chomp last two chars + + return s + + +def format_as_map(oid2str, str2oid): + return """/* +* OID maps +* +* This file was automatically generated by %s on %s +* +* Botan is released under the Simplified BSD License (see license.txt) +*/ + +#include <botan/oids.h> +#include <unordered_map> + +namespace Botan { + +std::unordered_map<std::string, std::string> OIDS::load_oid2str_map() + { + return std::unordered_map<std::string,std::string>{ + %s + }; + } + +std::unordered_map<std::string, OID> OIDS::load_str2oid_map() + { + return std::unordered_map<std::string,OID>{ + %s + }; + } + +} +""" % (sys.argv[0], datetime.date.today().strftime("%Y-%m-%d"), + format_map(oid2str), format_map(str2oid, True)) + + +def format_if(m, nm,t=False): + s = '' + for k in sorted(m.keys()): + v = m[k] + + if t: + s += ' if(%s == "%s") return OID("%s");\n' % (nm,k, v) + else: + s += ' if(%s == "%s") return "%s";\n' % (nm,k, v) + + s = s[:-1] + + return s + +def format_as_ifs(oid2str, str2oid): + return """/* +* OID maps +* +* This file was automatically generated by %s on %s +* +* Botan is released under the Simplified BSD License (see license.txt) +*/ + +#include <botan/oids.h> + +namespace Botan { + +namespace OIDS { + +std::string lookup(const OID& oid) + { + const std::string oid_str = oid.as_string(); +%s + return std::string(); + } + +OID lookup(const std::string& name) + { +%s + return OID(); + } + +} + +} +""" % (sys.argv[0], datetime.date.today().strftime("%Y-%m-%d"), + format_if(oid2str,"oid_str"), format_if(str2oid, "name", True)) + +def main(args = None): + if args is None: + args = sys.argv + + oid_lines = open('src/build-data/oids.txt').readlines() + + oid_re = re.compile("^([1-9][0-9.]+) = ([A-Za-z0-9_\./\(\), -]+)$") + hdr_re = re.compile("^\[([a-z0-9_]+)\]$") + + oid2str = {} + str2oid = {} + cur_hdr = None + + for line in oid_lines: + line = line.strip() + if len(line) == 0: + continue + + if line[0] == '#': + continue + + match = hdr_re.match(line) + if match is not None: + cur_hdr = match.group(1) + continue + + match = oid_re.match(line) + if match is None: + raise Exception(line) + + oid = match.group(1) + nam = match.group(2) + + if oid in str2oid: + print "Duplicated OID", oid, name, oid2str[oid] + sys.exit() # hard error + else: + oid2str[oid] = nam + + if nam in str2oid: + #print "Duplicated name", nam, oid, str2oid[nam] + #str2oid[nam] = oid + pass + else: + str2oid[nam] = oid + + print format_as_ifs(oid2str, str2oid) + + +if __name__ == '__main__': + sys.exit(main()) |