create_self-signed.py 1.2 KB

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