create_self-signed.py 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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 shutil
  8. import socket
  9. if len(sys.argv) < 2:
  10. sys.exit('Usage: %s directory to output certificates' % sys.argv[0])
  11. if not os.path.exists(sys.argv[1]):
  12. sys.exit('ERROR: Directory %s was not found!' % sys.argv[1])
  13. keysize = 2048
  14. if len(sys.argv) == 3:
  15. keysize = int(sys.argv[2])
  16. certsdir = os.path.dirname(os.path.abspath(__file__))
  17. print(certsdir)
  18. os.environ['HOSTNAME'] = socket.gethostname()
  19. openssl_conf = os.path.join(certsdir, "localhost.cnf")
  20. os.chdir(os.path.abspath(sys.argv[1]))
  21. os.system("""openssl req \
  22. -config {} \
  23. -new \
  24. -nodes \
  25. -x509 -sha256 \
  26. -newkey rsa:{} \
  27. -keyout localhost.key -days 365 \
  28. -subj "/C=DE/O=open62541/CN=open62541Server@localhost"\
  29. -out localhost.crt""".format(openssl_conf, keysize))
  30. os.system("openssl x509 -in localhost.crt -outform der -out server_cert.der")
  31. os.system("openssl rsa -inform PEM -in localhost.key -outform DER -out server_key.der")
  32. os.remove("localhost.key")
  33. os.remove("localhost.crt")
  34. print("Certificates generated in " + sys.argv[1])