-
Notifications
You must be signed in to change notification settings - Fork 7
/
command_line.py
97 lines (89 loc) · 3.49 KB
/
command_line.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
###############################################################################
# Copyright (c) 2018, Lawrence Livermore National Security, LLC
# Produced at the Lawrence Livermore National Laboratory
# Written by Thomas Mendoza mendoza33@llnl.gov
# LLNL-CODE-754897
# All rights reserved
#
# This file is part of Certipy: https://github.com/LLNL/certipy
#
# SPDX-License-Identifier: BSD-3-Clause
###############################################################################
import argparse
import shutil
import sys
from OpenSSL import crypto
from certipy import (
Certipy, CertExistsError, CertNotFoundError, CertificateAuthorityInUseError
)
def main():
describe_certipy = """
Certipy: Create simple, self-signed certificate authorities and certs.
"""
parser = argparse.ArgumentParser(description=describe_certipy)
parser.add_argument(
'name', help="""Name of the cert to create,
defaults to creating a CA cert. If no signing
--ca-name specified.""")
parser.add_argument(
'--ca-name', help="The name of the CA to sign this cert." , default="")
parser.add_argument(
'--overwrite',
action="store_true",
help="If the cert already exists, bump the serial and overwrite it.")
parser.add_argument(
'--rm', action="store_true", help="Remove the cert specified by name.")
parser.add_argument(
'--cert-type', default="rsa", choices=['rsa', 'dsa'],
help="The type of cert to create.")
parser.add_argument(
'--bits', type=int, default=2048, help="The number of bits to use.")
parser.add_argument(
'--valid', type=int, default=5, help="Years the cert is valid for.")
parser.add_argument(
'--alt-names', default="",
help="Alt names for the certificate (comma delimited).")
parser.add_argument(
'--store-dir', default="out",
help="The location for the store and certs.")
args = parser.parse_args()
certipy = Certipy(store_dir=args.store_dir)
cert_type = crypto.TYPE_RSA if args.cert_type == "rsa" else crypto.TYPE_DSA
record = None
if args.rm:
try:
record = certipy.store.remove_files(args.name, delete_dir=True)
print("Deleted:")
for key, val in record.items():
print(key.upper(), val)
except CertificateAuthorityInUseError as e:
print("Unable to delete.", e)
sys.exit(0)
alt_names = None
if args.alt_names:
alt_names = [_.strip() for _ in args.alt_names.split(',')]
if args.ca_name:
ca_record = certipy.store.get_record(args.ca_name)
if ca_record:
try:
record = certipy.create_signed_pair(
args.name, args.ca_name, cert_type=cert_type,
bits=args.bits, years=args.valid,
alt_names=alt_names, overwrite=args.overwrite)
except CertExistsError as e:
print(e)
else:
print(
"CA {} not found. Must specify an exisiting authority to"
" sign this cert.".format(args.ca_name))
else:
try:
record = certipy.create_ca(
args.name, cert_type=cert_type, bits=args.bits,
years=args.valid, alt_names=alt_names,
overwrite=args.overwrite)
except CertExistsError as e:
print(e)
if record:
for key, val in record.items():
print(key.upper(), val)