create_self-signed.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. import argparse
  13. parser = argparse.ArgumentParser()
  14. parser.add_argument('outdir',
  15. type=str,
  16. nargs='?',
  17. default=os.getcwd(),
  18. metavar='<OutputDirectory>')
  19. parser.add_argument('-u', '--uri',
  20. metavar="<ApplicationUri>",
  21. type=str,
  22. default="",
  23. dest="uri")
  24. parser.add_argument('-k', '--keysize',
  25. metavar="<KeySize>",
  26. type=int,
  27. dest="keysize")
  28. args = parser.parse_args()
  29. if not os.path.exists(args.outdir):
  30. sys.exit('ERROR: Directory %s was not found!' % args.outdir)
  31. keysize = 2048
  32. if args.keysize:
  33. keysize = args.keysize
  34. if args.uri == "":
  35. args.uri = "urn:open62541.server.application"
  36. print("No ApplicationUri given for the certificate. Setting to %s" % args.uri)
  37. os.environ['URI1'] = args.uri
  38. certsdir = os.path.dirname(os.path.abspath(__file__))
  39. # Function return TRUE (1) when an IP address is associated with the
  40. # given interface
  41. def is_interface_up(interface):
  42. addr = netifaces.ifaddresses(interface)
  43. return netifaces.AF_INET in addr
  44. # Initialize looping variables
  45. interfaceNum = 0
  46. iteratorValue = 0
  47. # Read the number of interfaces available
  48. numberOfInterfaces = int(format(len(netifaces.interfaces())))
  49. # Traverse through the available network interfaces and store the
  50. # corresponding IP addresses of the network interface in a variable
  51. for interfaceNum in range(0, numberOfInterfaces):
  52. # Function call which returns whether the given
  53. # interface is up or not
  54. check = is_interface_up(netifaces.interfaces()[interfaceNum])
  55. # Check if the interface is up and not the loopback one
  56. # If yes set the IP Address for the environmental variables
  57. if check != 0 and netifaces.interfaces()[interfaceNum] != 'lo':
  58. if iteratorValue == 0:
  59. os.environ['IPADDRESS1'] = netifaces.ifaddresses(netifaces.interfaces()[interfaceNum])[netifaces.AF_INET][0]['addr']
  60. if iteratorValue == 1:
  61. os.environ['IPADDRESS2'] = netifaces.ifaddresses(netifaces.interfaces()[interfaceNum])[netifaces.AF_INET][0]['addr']
  62. iteratorValue = iteratorValue + 1
  63. if iteratorValue == 2:
  64. break
  65. # If there is only one interface available then set the second
  66. # IP address as loopback IP
  67. if iteratorValue < 2:
  68. os.environ['IPADDRESS2'] = "127.0.0.1"
  69. os.environ['HOSTNAME'] = socket.gethostname()
  70. openssl_conf = os.path.join(certsdir, "localhost.cnf")
  71. os.chdir(os.path.abspath(args.outdir))
  72. os.system("""openssl req \
  73. -config {} \
  74. -new \
  75. -nodes \
  76. -x509 -sha256 \
  77. -newkey rsa:{} \
  78. -keyout localhost.key -days 365 \
  79. -subj "/C=DE/O=open62541/CN=open62541Server@localhost"\
  80. -out localhost.crt""".format(openssl_conf, keysize))
  81. os.system("openssl x509 -in localhost.crt -outform der -out server_cert.der")
  82. os.system("openssl rsa -inform PEM -in localhost.key -outform DER -out server_key.der")
  83. os.remove("localhost.key")
  84. os.remove("localhost.crt")
  85. print("Certificates generated in " + args.outdir)