create_self-signed.py 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. #
  6. # Copyright 2019 (c) Kalycito Infotech Private Limited
  7. #
  8. import netifaces
  9. import sys
  10. import os
  11. import socket
  12. if len(sys.argv) < 2:
  13. sys.exit('Usage: %s directory to output certificates' % sys.argv[0])
  14. if not os.path.exists(sys.argv[1]):
  15. sys.exit('ERROR: Directory %s was not found!' % sys.argv[1])
  16. keysize = 2048
  17. if len(sys.argv) == 3:
  18. keysize = int(sys.argv[2])
  19. certsdir = os.path.dirname(os.path.abspath(__file__))
  20. print(certsdir)
  21. # Function return TRUE (1) when an IP address is associated with the
  22. # given interface
  23. def is_interface_up(interface):
  24. addr = netifaces.ifaddresses(interface)
  25. return netifaces.AF_INET in addr
  26. # Initialize looping variables
  27. interfaceNum = 0
  28. iteratorValue = 0
  29. # Read the number of interfaces available
  30. numberOfInterfaces = int(format(len(netifaces.interfaces())))
  31. # Traverse through the available network interfaces and store the
  32. # corresponding IP addresses of the network interface in a variable
  33. for interfaceNum in range(0, numberOfInterfaces):
  34. # Function call which returns whether the given
  35. # interface is up or not
  36. check = is_interface_up(netifaces.interfaces()[interfaceNum])
  37. # Check if the interface is up and not the loopback one
  38. # If yes set the IP Address for the environmental variables
  39. if check != 0 and netifaces.interfaces()[interfaceNum] != 'lo':
  40. if iteratorValue == 0:
  41. os.environ['IPADDRESS1'] = netifaces.ifaddresses(netifaces.interfaces()[interfaceNum])[netifaces.AF_INET][0]['addr']
  42. if iteratorValue == 1:
  43. os.environ['IPADDRESS2'] = netifaces.ifaddresses(netifaces.interfaces()[interfaceNum])[netifaces.AF_INET][0]['addr']
  44. iteratorValue = iteratorValue + 1
  45. if iteratorValue == 2:
  46. break
  47. # If there is only one interface available then set the second
  48. # IP address as loopback IP
  49. if iteratorValue < 2:
  50. os.environ['IPADDRESS2'] = "127.0.0.1"
  51. os.environ['HOSTNAME'] = socket.gethostname()
  52. openssl_conf = os.path.join(certsdir, "localhost.cnf")
  53. os.chdir(os.path.abspath(sys.argv[1]))
  54. os.system("""openssl req \
  55. -config {} \
  56. -new \
  57. -nodes \
  58. -x509 -sha256 \
  59. -newkey rsa:{} \
  60. -keyout localhost.key -days 365 \
  61. -subj "/C=DE/O=open62541/CN=open62541Server@localhost"\
  62. -out localhost.crt""".format(openssl_conf, keysize))
  63. os.system("openssl x509 -in localhost.crt -outform der -out server_cert.der")
  64. os.system("openssl rsa -inform PEM -in localhost.key -outform DER -out server_key.der")
  65. os.remove("localhost.key")
  66. os.remove("localhost.crt")
  67. print("Certificates generated in " + sys.argv[1])