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.

94 lines
2.4 KiB

  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. from playhouse.gfk import *
  4. import os
  5. import os.path
  6. import subprocess
  7. from models.authority import Authority
  8. from models.certificate import Certificate
  9. from models.request import SignRequest
  10. from paths import *
  11. class HostSSLRequest(SignRequest):
  12. def __init__(self, req_id, host_name, key_data):
  13. super(HostSSLRequest, self).__init__(req_id)
  14. self.host_name = host_name
  15. self.key_data = key_data
  16. @property
  17. def name(self):
  18. return "Hostname: %s" % self.host_name
  19. @property
  20. def fields(self):
  21. return [
  22. ("Hostname", self.host_name)
  23. ]
  24. @property
  25. def receiver(self):
  26. return self.host_name
  27. class SSLAuthority(Authority):
  28. request_allowed = [ HostSSLRequest, ]
  29. ca_key_algorithm = 'des3'
  30. key_length = '4096'
  31. key_algorithm = 'sha256'
  32. ca_validity = '365'
  33. cert_validity = '365'
  34. def generate(self):
  35. if os.path.exists(self.path):
  36. raise ValueError("A CA with the same id and type already exists")
  37. subprocess.check_output(['openssl',
  38. 'genrsa',
  39. '-%s'%self.ca_key_algorithm,
  40. '-out', '%s'%(self.path),
  41. self.key_length])
  42. subprocess.check_output(['openssl',
  43. 'req',
  44. '-new',
  45. '-x509',
  46. '-days', self.ca_validity,
  47. '-key', self.path,
  48. # '-extensions', 'v3_ca'
  49. '-out', "%s.pub"%self.path,
  50. # '-config', "%s.conf"%self.path
  51. ])
  52. with open(self.path + '.serial', 'w') as stream:
  53. stream.write(str(0))
  54. def generate_certificate(self, request):
  55. """
  56. Sign a *SSLRequest with this certification authority
  57. """
  58. pub_key_path = request.destination + '.pub'
  59. cert_path = request.destination + '-cert.pub'
  60. with open(pub_key_path, 'w') as stream:
  61. stream.write(request.key_data)
  62. subprocess.check_output(['openssl',
  63. 'x509',
  64. '-req',
  65. '-days', self.ca_validity,
  66. '-in', pub_key_path,
  67. '-CA', "%s.pub"%self.path,
  68. '-CAkey', self.path,
  69. '-CAcreateserial',
  70. '-out', cert_path,
  71. '-%s'%self.key_algorithm])
  72. return self.ca_validity