diff --git a/csrgen.py b/csrgen.py new file mode 100755 index 0000000..1b3e1f7 --- /dev/null +++ b/csrgen.py @@ -0,0 +1,190 @@ +#!/usr/bin/env python3 +import argparse +import fnmatch +import re +import os +import subprocess +import readline +from jinja2 import Template, Environment, FileSystemLoader + +file_loader = FileSystemLoader('.') +env = Environment(loader=file_loader) + + +description = """ +Simple script to handle generating Certificate Signing Requests (CSR) +This has the ability to generate CSRs with and without a provided key. +Also can handle multiple domains as Subject Alternative Name (SAN) records. +""" +config_template = """[ req ] + default_bits = 2048 + distinguished_name = req_distinguished_name + prompt = no + req_extensions = v3_req +[ req_distinguished_name ] + {% if c is not none -%} + C = {{s}} + {%- endif %} + {%- if l is not none -%} + L = {{l}} + {%- endif %} + {%- if s is not none -%} + ST = {{s}} + {%- endif %} + {%- if o is not none -%} + O = {{o}} + {%- endif %} +{{' CN = {{ cn }} +[ v3_req ] + subjectAltName = @alt_names + +[alt_names] + DNS.1 = {{ cn }} + {%- for domain in sans %} + {%- if domain %} + DNS.{{ loop.index +1 }} = {{ domain }} + {%- endif %} + {%- endfor %} +'}} +""" + + +def check_file(file): + try: + os.stat(file).st_size > 1 + except: + return False + else: + return file + + +def check_domain(input): + if check_file(input): + return input, True + else: + domain_template = re.compile( + "^(?=.{1,255}$)(?!-)[A-Za-z0-9\-]{1,63}(\.[A-Za-z0-9\-]{1,63})*\.?(? 1: # Check if there is more then one config file + if args.org: + # Check if any of the config files match the specified Org + if check_file({args.org + "_gen_config"}): + csr_config_out = gen_csr_config( + args, {args.org + "_gen_config"}) + else: + print("Please specify Organization name with -o ") + exit() + else: # There is only one found so assume its the right one\ + csr_config_out = gen_csr_config(args, config_file_search[0]) + + # Validate key argument and file + if args.key: + if check_file(args.key): + try: + subprocess.call(['openssl', 'rsa', '-in', + args.key, '-check']) + except: + print("Key specified is not a valid rsa key.") + else: + print("Key file specified doesn't exist.") + else: + print("No key specified. Creating a new one.") + subprocess.call(['openssl', 'genrsa', '-out', + args.domain + '.key', '2048']) + print("Key Generated") + + if args.key: + print("Generating CSR") + subprocess.call(['openssl', 'req', '-new', '-config', csr_config_out, + '-key', + args.key, '-out', args.domain + '.csr']) + else: + print("Generating CSR") + subprocess.call(['openssl', 'req', '-new', '-config', csr_config_out, + '-key', args.domain + '.key', '-out', args.domain + '.csr']) + + if args.command == 'config': + tm = Template(config_template) + config_file_name = args.org + "_gen_config" + print("Creating config template file: {filename}".format( + filename=config_file_name)) + config_file = open(config_file_name, "w") + config_file.write(tm.render(c=args.country, l=args.locality, + s=args.state, o=args.org)) + config_file.close() + + +if __name__ == '__main__': + main(parse_arguments())