diff --git a/gimmecert/cli.py b/gimmecert/cli.py index 413edf5f08fbbfd2ea90c3487d78529ff76cb1c0..483d11dced41929312891326902c2727044331ea 100644 --- a/gimmecert/cli.py +++ b/gimmecert/cli.py @@ -25,7 +25,6 @@ import sys from .decorators import subcommand_parser, get_subcommand_parser_setup_functions from .commands import client, help_, init, renew, server, status, usage, ExitCode -from .crypto import KeyGenerator ERROR_ARGUMENTS = 2 @@ -78,12 +77,40 @@ Examples: """ +def key_specification(specification): + """ + Verifies and parses the passed-in key specification. This is a + small utility function for use with the Python argument parser. + + :param specification: Key specification. Currently supported formats are: "rsa:KEY_SIZE". + :type specification: str + + :returns: Parsed key algorithm and parameter(s) for the algorithm. For RSA, parameter is the RSA key size. + :rtype: tuple(str, int) + + :raises ValueError: If passed-in specification is invalid. + """ + + try: + algorithm, parameters = specification.split(":", 2) + + if algorithm == "rsa": + parameters = int(parameters) + else: + raise ValueError() + + except ValueError: + raise ValueError("Invalid key specification: '%s'" % specification) + + return algorithm, parameters + + @subcommand_parser def setup_init_subcommand_parser(parser, subparsers): subparser = subparsers.add_parser('init', description='Initialise CA hierarchy.') subparser.add_argument('--ca-base-name', '-b', help="Base name to use for CA naming. Default is to use the working directory base name.") subparser.add_argument('--ca-hierarchy-depth', '-d', type=int, help="Depth of CA hierarchy to generate. Default is 1", default=1) - subparser.add_argument('--key-specification', '-k', type=KeyGenerator, + subparser.add_argument('--key-specification', '-k', type=key_specification, help='''Default specification/parameters to use for private key generation. \ For RSA keys, use format rsa:BIT_LENGTH. Default is rsa:2048.''', default="rsa:2048")