| 1 |
#!/usr/bin/env python |
| 2 |
|
| 3 |
# Copyright New York University and the in-toto contributors |
| 4 |
# SPDX-License-Identifier: Apache-2.0 |
| 5 |
|
| 6 |
""" |
| 7 |
<Program Name> |
| 8 |
in_toto_keygen.py |
| 9 |
|
| 10 |
<Author> |
| 11 |
Lukas Puehringer <[email protected]> |
| 12 |
Sachit Malik <[email protected]> |
| 13 |
|
| 14 |
<Started> |
| 15 |
June 28, 2017 |
| 16 |
|
| 17 |
<Copyright> |
| 18 |
See LICENSE for licensing information. |
| 19 |
|
| 20 |
<Purpose> |
| 21 |
A CLI tool for creating key files, and dumping them with <filename>, |
| 22 |
<filename>.pub for private and public keys respectively. It also takes an |
| 23 |
integer as an input, which specifies the length of the RSA key to be |
| 24 |
generated. By default it is set as 3072. |
| 25 |
|
| 26 |
<Return Codes> |
| 27 |
2 if an exception occurred during argument parsing |
| 28 |
1 if an exception occurred |
| 29 |
0 if no exception occurred |
| 30 |
|
| 31 |
""" |
| 32 |
import argparse |
| 33 |
import logging |
| 34 |
import sys |
| 35 |
|
| 36 |
from securesystemslib import interface |
| 37 |
|
| 38 |
from in_toto import ( |
| 39 |
KEY_TYPE_ECDSA, |
| 40 |
KEY_TYPE_ED25519, |
| 41 |
KEY_TYPE_RSA, |
| 42 |
SUPPORTED_KEY_TYPES, |
| 43 |
__version__, |
| 44 |
) |
| 45 |
from in_toto.common_args import title_case_action_groups |
| 46 |
|
| 47 |
# Command line interfaces should use in_toto base logger (c.f. in_toto.log) |
| 48 |
LOG = logging.getLogger("in_toto") |
| 49 |
|
| 50 |
|
| 51 |
def create_parser(): |
| 52 |
""" |
| 53 |
<Purpose> |
| 54 |
A function which parses the user supplied arguments. |
| 55 |
|
| 56 |
<Arguments> |
| 57 |
None |
| 58 |
|
| 59 |
<Exceptions> |
| 60 |
None |
| 61 |
|
| 62 |
<Returns> |
| 63 |
Parsed arguments (args object) |
| 64 |
""" |
| 65 |
parser = argparse.ArgumentParser( |
| 66 |
formatter_class=argparse.RawDescriptionHelpFormatter, |
| 67 |
description="in-toto-keygen is a tool to generate, optionally encrypt, and" |
| 68 |
" write cryptographic keys to disk. These keys may be used" |
| 69 |
" with other in-toto tooling to e.g. sign or verify link or" |
| 70 |
" layout metadata.", |
| 71 |
) |
| 72 |
|
| 73 |
parser.epilog = """EXAMPLE USAGE |
| 74 |
|
| 75 |
Generate RSA key pair of size 2048 bits, prompt for a password to encrypt |
| 76 |
the private key, and write 'alice' (private encrypted) and 'alice.pub' (public) |
| 77 |
as PEM-formatted key files to the current working directory. |
| 78 |
|
| 79 |
in-toto-keygen -p -t rsa -b 2048 alice |
| 80 |
|
| 81 |
|
| 82 |
Generate unencrypted ed25519 key pair and write 'bob' (private) and 'bob.pub' |
| 83 |
(public) as securesystemslib/json-formatted key files to the current working |
| 84 |
directory. |
| 85 |
|
| 86 |
in-toto-keygen -t ed25519 bob |
| 87 |
|
| 88 |
|
| 89 |
""" |
| 90 |
|
| 91 |
parser.add_argument( |
| 92 |
"-p", |
| 93 |
"--prompt", |
| 94 |
action="store_true", |
| 95 |
help="prompts for a password used to encrypt the" |
| 96 |
" private key before storing it", |
| 97 |
) |
| 98 |
|
| 99 |
parser.add_argument( |
| 100 |
"-t", |
| 101 |
"--type", |
| 102 |
type=str, |
| 103 |
choices=SUPPORTED_KEY_TYPES, |
| 104 |
default=KEY_TYPE_RSA, |
| 105 |
help="type of the key to be generated. '{rsa}'" |
| 106 |
" keys are written in a 'PEM' format. '{ed25519}'" |
| 107 |
" and '{ecdsa}' keys are written in a custom" |
| 108 |
" 'securesystemslib/json' format. Default is" |
| 109 |
" '{rsa}'.".format( |
| 110 |
rsa=KEY_TYPE_RSA, ed25519=KEY_TYPE_ED25519, ecdsa=KEY_TYPE_ECDSA |
| 111 |
), |
| 112 |
) |
| 113 |
|
| 114 |
parser.add_argument( |
| 115 |
"name", |
| 116 |
type=str, |
| 117 |
metavar="<filename>", |
| 118 |
help="filename for the resulting key files, which" |
| 119 |
" are written to '<filename>' (private key) and" |
| 120 |
" '<filename>.pub' (public key).", |
| 121 |
) |
| 122 |
|
| 123 |
parser.add_argument( |
| 124 |
"-b", |
| 125 |
"--bits", |
| 126 |
default=3072, |
| 127 |
type=int, |
| 128 |
metavar="<bits>", |
| 129 |
help="key size, or key length, of the RSA key", |
| 130 |
) |
| 131 |
|
| 132 |
parser.add_argument( |
| 133 |
"--version", |
| 134 |
action="version", |
| 135 |
version="{} {}".format(parser.prog, __version__), |
| 136 |
) |
| 137 |
|
| 138 |
title_case_action_groups(parser) |
| 139 |
|
| 140 |
return parser |
| 141 |
|
| 142 |
|
| 143 |
def main(): |
| 144 |
""" |
| 145 |
First calls parse_args to parse the arguments, and then calls either |
| 146 |
_generate_and_write_rsa_keypair or _generate_and_write_ed25519_keypair |
| 147 |
depending upon the arguments. It then dumps the corresponding key files as: |
| 148 |
<filename> and <filename>.pub (Private key and Public key respectively) |
| 149 |
""" |
| 150 |
parser = create_parser() |
| 151 |
args = parser.parse_args() |
| 152 |
|
| 153 |
if args.type != KEY_TYPE_RSA: |
| 154 |
if args.bits is not None: |
| 155 |
parser.print_help() |
| 156 |
parser.error( |
| 157 |
"wrong arguments: bits specified," |
| 158 |
" only Type RSA supports Bits as a parameter" |
| 159 |
) |
| 160 |
|
| 161 |
try: |
| 162 |
if args.type == KEY_TYPE_RSA: |
| 163 |
interface._generate_and_write_rsa_keypair( # pylint: disable=protected-access |
| 164 |
filepath=args.name, bits=args.bits, prompt=args.prompt |
| 165 |
) |
| 166 |
elif args.type == KEY_TYPE_ED25519: |
| 167 |
interface._generate_and_write_ed25519_keypair( # pylint: disable=protected-access |
| 168 |
filepath=args.name, prompt=args.prompt |
| 169 |
) |
| 170 |
elif args.type == KEY_TYPE_ECDSA: |
| 171 |
interface._generate_and_write_ecdsa_keypair( # pylint: disable=protected-access |
| 172 |
filepath=args.name, prompt=args.prompt |
| 173 |
) |
| 174 |
else: # pragma: no cover |
| 175 |
LOG.error( |
| 176 |
"(in-toto-keygen) Unsupported keytype: {0}".format( |
| 177 |
str(args.type) |
| 178 |
) |
| 179 |
) |
| 180 |
sys.exit(1) |
| 181 |
sys.exit(0) |
| 182 |
|
| 183 |
except Exception as e: |
| 184 |
LOG.error("(in-toto-keygen) {0}: {1}".format(type(e).__name__, e)) |
| 185 |
sys.exit(1) |
| 186 |
|
| 187 |
|
| 188 |
if __name__ == "__main__": |
| 189 |
main() |