ChimeraTK-ControlSystemAdapter-OPCUAAdapter  04.00.01
create_self-signed.py
Go to the documentation of this file.
1 #!/usr/bin/env python3
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 
9 import netifaces
10 import sys
11 import os
12 import socket
13 import argparse
14 
15 parser = argparse.ArgumentParser()
16 
17 parser.add_argument('outdir',
18  type=str,
19  nargs='?',
20  default=os.getcwd(),
21  metavar='<OutputDirectory>')
22 
23 parser.add_argument('-u', '--uri',
24  metavar="<ApplicationUri>",
25  type=str,
26  default="",
27  dest="uri")
28 
29 parser.add_argument('-k', '--keysize',
30  metavar="<KeySize>",
31  type=int,
32  dest="keysize")
33 
34 parser.add_argument('-c', '--certificatename',
35  metavar="<CertificateName>",
36  type=str,
37  default="",
38  dest="certificatename")
39 
40 args = parser.parse_args()
41 
42 if not os.path.exists(args.outdir):
43  sys.exit('ERROR: Directory %s was not found!' % args.outdir)
44 
45 keysize = 2048
46 
47 if args.keysize:
48  keysize = args.keysize
49 
50 if args.uri == "":
51  args.uri = "urn:open62541.server.application"
52  print("No ApplicationUri given for the certificate. Setting to %s" % args.uri)
53 os.environ['URI1'] = args.uri
54 
55 if args.certificatename == "":
56  certificatename = "server"
57  print("No Certificate name provided. Setting to %s" % certificatename)
58 
59 if args.certificatename:
60  certificatename = args.certificatename
61 
62 certsdir = os.path.dirname(os.path.abspath(__file__))
63 
64 # Function return TRUE (1) when an IP address is associated with the
65 # given interface
66 def is_interface_up(interface):
67  addr = netifaces.ifaddresses(interface)
68  return netifaces.AF_INET in addr
69 
70 # Initialize looping variables
71 interfaceNum = 0
72 iteratorValue = 0
73 
74 # Read the number of interfaces available
75 numberOfInterfaces = int(format(len(netifaces.interfaces())))
76 
77 # Traverse through the available network interfaces and store the
78 # corresponding IP addresses of the network interface in a variable
79 for interfaceNum in range(0, numberOfInterfaces):
80  # Function call which returns whether the given
81  # interface is up or not
82  check = is_interface_up(netifaces.interfaces()[interfaceNum])
83 
84  # Check if the interface is up and not the loopback one
85  # If yes set the IP Address for the environmental variables
86  if check != 0 and netifaces.interfaces()[interfaceNum] != 'lo':
87  if iteratorValue == 0:
88  os.environ['IPADDRESS1'] = netifaces.ifaddresses(netifaces.interfaces()[interfaceNum])[netifaces.AF_INET][0]['addr']
89  if iteratorValue == 1:
90  os.environ['IPADDRESS2'] = netifaces.ifaddresses(netifaces.interfaces()[interfaceNum])[netifaces.AF_INET][0]['addr']
91  iteratorValue = iteratorValue + 1
92  if iteratorValue == 2:
93  break
94 
95 # If there is only one interface available then set the second
96 # IP address as loopback IP
97 if iteratorValue < 2:
98  os.environ['IPADDRESS2'] = "127.0.0.1"
99 
100 os.environ['HOSTNAME'] = socket.gethostname()
101 openssl_conf = os.path.join(certsdir, "localhost.cnf")
102 
103 os.chdir(os.path.abspath(args.outdir))
104 
105 os.system("""openssl req \
106  -config {} \
107  -new \
108  -nodes \
109  -x509 -sha256 \
110  -newkey rsa:{} \
111  -keyout localhost.key -days 365 \
112  -subj "/C=DE/O=open62541/CN=open62541Server@localhost"\
113  -out localhost.crt""".format(openssl_conf, keysize))
114 os.system("openssl x509 -in localhost.crt -outform der -out %s_cert.der" % (certificatename))
115 os.system("openssl rsa -inform PEM -in localhost.key -outform DER -out %s_key.der"% (certificatename))
116 
117 os.remove("localhost.key")
118 os.remove("localhost.crt")
119 
120 print("Certificates generated in " + args.outdir)
testMapGenerator.format
format
Definition: testMapGenerator.py:78
create_self-signed.is_interface_up
def is_interface_up(interface)
Definition: create_self-signed.py:66