ChimeraTK-ControlSystemAdapter-OPCUAAdapter 04.00.05
Loading...
Searching...
No Matches
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
9import netifaces
10import sys
11import os
12import socket
13import argparse
14
15parser = argparse.ArgumentParser()
16
17parser.add_argument('outdir',
18 type=str,
19 nargs='?',
20 default=os.getcwd(),
21 metavar='<OutputDirectory>')
22
23parser.add_argument('-u', '--uri',
24 metavar="<ApplicationUri>",
25 type=str,
26 default="",
27 dest="uri")
28
29parser.add_argument('-k', '--keysize',
30 metavar="<KeySize>",
31 type=int,
32 dest="keysize")
33
34parser.add_argument('-c', '--certificatename',
35 metavar="<CertificateName>",
36 type=str,
37 default="",
38 dest="certificatename")
39
40args = parser.parse_args()
41
42if not os.path.exists(args.outdir):
43 sys.exit('ERROR: Directory %s was not found!' % args.outdir)
44
45keysize = 2048
46
47if args.keysize:
48 keysize = args.keysize
49
50if args.uri == "":
51 args.uri = "urn:open62541.server.application"
52 print("No ApplicationUri given for the certificate. Setting to %s" % args.uri)
53os.environ['URI1'] = args.uri
54
55if args.certificatename == "":
56 certificatename = "server"
57 print("No Certificate name provided. Setting to %s" % certificatename)
58
59if args.certificatename:
60 certificatename = args.certificatename
61
62certsdir = os.path.dirname(os.path.abspath(__file__))
63
64# Function return TRUE (1) when an IP address is associated with the
65# given interface
66def is_interface_up(interface):
67 addr = netifaces.ifaddresses(interface)
68 return netifaces.AF_INET in addr
69
70# Initialize looping variables
71interfaceNum = 0
72iteratorValue = 0
73
74# Read the number of interfaces available
75numberOfInterfaces = 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
79for 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
97if iteratorValue < 2:
98 os.environ['IPADDRESS2'] = "127.0.0.1"
99
100os.environ['HOSTNAME'] = socket.gethostname()
101openssl_conf = os.path.join(certsdir, "localhost.cnf")
102
103os.chdir(os.path.abspath(args.outdir))
104
105os.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))
114os.system("openssl x509 -in localhost.crt -outform der -out %s_cert.der" % (certificatename))
115os.system("openssl rsa -inform PEM -in localhost.key -outform DER -out %s_key.der"% (certificatename))
116
117os.remove("localhost.key")
118os.remove("localhost.crt")
119
120print("Certificates generated in " + args.outdir)