create_self-signed.py 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. #!/usr/bin/env python
  2. # This Source Code Form is subject to the terms of the Mozilla Public
  3. # License, v. 2.0. If a copy of the MPL was not distributed with this
  4. # file, You can obtain one at http://mozilla.org/MPL/2.0/.
  5. import sys
  6. import os
  7. import socket
  8. if len(sys.argv) < 2:
  9. sys.exit('Usage: %s directory to output certificates' % sys.argv[0])
  10. if not os.path.exists(sys.argv[1]):
  11. sys.exit('ERROR: Directory %s was not found!' % sys.argv[1])
  12. keysize = 2048
  13. if len(sys.argv) == 3:
  14. keysize = int(sys.argv[2])
  15. certsdir = os.path.dirname(os.path.abspath(__file__))
  16. print(certsdir)
  17. os.environ['HOSTNAME'] = socket.gethostname()
  18. openssl_conf = os.path.join(certsdir, "localhost.cnf")
  19. os.chdir(os.path.abspath(sys.argv[1]))
  20. os.system("""openssl req \
  21. -config {} \
  22. -new \
  23. -nodes \
  24. -x509 -sha256 \
  25. -newkey rsa:{} \
  26. -keyout localhost.key -days 365 \
  27. -subj "/C=DE/O=open62541/CN=open62541Server@localhost"\
  28. -out localhost.crt""".format(openssl_conf, keysize))
  29. os.system("openssl x509 -in localhost.crt -outform der -out server_cert.der")
  30. os.system("openssl rsa -inform PEM -in localhost.key -outform DER -out server_key.der")
  31. os.remove("localhost.key")
  32. os.remove("localhost.crt")
  33. print("Certificates generated in " + sys.argv[1])