create_self-signed.py 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. import sys
  2. import os
  3. import shutil
  4. import socket
  5. if len(sys.argv) < 2:
  6. sys.exit('Usage: %s directory to output certificates' % sys.argv[0])
  7. if not os.path.exists(sys.argv[1]):
  8. sys.exit('ERROR: Directory %s was not found!' % sys.argv[1])
  9. os.chdir(os.path.dirname(os.path.abspath(__file__)))
  10. os.environ['HOSTNAME'] = socket.gethostname()
  11. os.system("""openssl genrsa -out ca.key 2048""")
  12. os.system("""openssl req \
  13. -x509 \
  14. -new \
  15. -nodes \
  16. -key ca.key \
  17. -days 3650 \
  18. -subj "/C=DE/O=open62541/CN=open62541.org" \
  19. -out ca.crt""")
  20. os.system("""openssl req \
  21. -new \
  22. -newkey rsa:2048 \
  23. -nodes \
  24. -subj "/C=DE/O=open62541/CN=open62541Server@localhost" \
  25. -config localhost.cnf \
  26. -keyout localhost.key \
  27. -out localhost.csr""")
  28. os.system("""openssl x509 -req \
  29. -days 3650 \
  30. -in localhost.csr \
  31. -CA ca.crt \
  32. -CAkey ca.key \
  33. -CAcreateserial \
  34. -out localhost.crt \
  35. -extensions v3_ca \
  36. -extfile localhost.cnf""")
  37. os.system("""openssl x509 -in localhost.crt -outform der -out localhost.der""")
  38. #we will need these files later
  39. os.remove("localhost.key") #we will need it later
  40. os.remove("localhost.crt")
  41. os.remove("localhost.csr")
  42. os.remove("ca.key")
  43. os.remove("ca.srl")
  44. shutil.move("localhost.der", sys.argv[1])
  45. shutil.move("ca.crt", sys.argv[1])