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.

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