Easy CA management
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

141 lines
3.4 KiB

9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. import cmd
  4. import hashlib
  5. import json
  6. import os
  7. import os.path
  8. import shutil
  9. import sqlite3
  10. import tempfile
  11. from paths import *
  12. from lookup import CALookup, RequestLookup, CertificateLookup
  13. __doc__= """
  14. Define classes to interact with certificate requests and Certification Authority
  15. """
  16. class CAManager(object):
  17. """
  18. Middleware to interact with ssh-keygen
  19. """
  20. def __init__(self, path):
  21. self.path = path
  22. self.ca = CALookup()
  23. self.request = RequestLookup()
  24. self.certificate = CertificateLookup()
  25. def __enter__(self):
  26. """
  27. Enter a context block, connect to database
  28. """
  29. self.conn = sqlite3.connect(self.db_path)
  30. self.ca.conn = self.conn
  31. return self
  32. def __exit__(self, exc_type, exc_value, traceback):
  33. """
  34. Exit a context block, disconnect from database
  35. """
  36. if exc_type is not None:
  37. print(exc_type, exc_value)
  38. print(traceback)
  39. self.ca.conn = None
  40. self.conn.close()
  41. @property
  42. def db_path(self):
  43. return os.path.join(self.path, 'ca_manager.db')
  44. @property
  45. def ssh_ca_dir(self):
  46. return os.path.join(self.path, 'ssh_cas')
  47. @property
  48. def ssl_ca_dir(self):
  49. return os.path.join(self.path, 'ssl_cas')
  50. def init_manager(paths):
  51. """
  52. Initiate the manager by creating the
  53. directories to store CAs and requests.
  54. Create a database to store the information
  55. """
  56. db_path = os.path.join(paths[0], 'ca_manager.db')
  57. directories = ['ssh_cas', 'ssl_cas', 'pickled_cas',]
  58. # ensure the directories needed by CAManager
  59. # exists
  60. for dirpath in paths:
  61. if not os.path.exists(dirpath):
  62. os.makedirs(dirpath)
  63. # ensure ssh_cas ad ssl_cas directories
  64. # exists in MANAGER_PATH
  65. for dirname in directories:
  66. dirpath = os.path.join(paths[0], dirname)
  67. if not os.path.exists(dirpath):
  68. os.mkdir(dirpath)
  69. # ensure the database exists
  70. # in MANAGER_PATH
  71. if not os.path.exists(db_path):
  72. conn = sqlite3.connect(db_path)
  73. c = conn.cursor()
  74. c.execute("""CREATE TABLE cas (id text, name text, type text)""")
  75. conn.commit()
  76. conn.close()
  77. def sign_request(ca_manager, request_id, authority_id):
  78. authority, request = None, None
  79. try:
  80. authority = ca_manager.ca[authority_id]
  81. except IndexError:
  82. print("Could not find CA '%d'" % authority_id)
  83. return
  84. try:
  85. request = ca_manager.request[request_id]
  86. except IndexError:
  87. print("Could not find request '%d'" % request_id)
  88. h = hashlib.sha256()
  89. h.update(request.key_data.encode('utf-8'))
  90. print("Request hash: %s" % h.hexdigest())
  91. print("You are about to sign this request with the following CA:")
  92. confirm = input('Proceed? (type yes)> ')
  93. if confirm != 'yes':
  94. print ("user abort")
  95. return
  96. cert_path = authority.sign(request)
  97. del ca_manager.request[request_id]
  98. shutil.copy(cert_path, os.path.join(RESULTS_PATH, request.req_id))
  99. if __name__ == '__main__':
  100. from ca_shell import CAManagerShell
  101. init_manager([
  102. MANAGER_PATH,
  103. REQUESTS_PATH,
  104. OUTPUT_PATH,
  105. RESULTS_PATH,
  106. ])
  107. with CAManager(MANAGER_PATH) as ca_manager:
  108. CAManagerShell(ca_manager).cmdloop()