15 parser = argparse.ArgumentParser()
17 parser.add_argument(
'outdir',
21 metavar=
'<OutputDirectory>')
23 parser.add_argument(
'-u',
'--uri',
24 metavar=
"<ApplicationUri>",
29 parser.add_argument(
'-k',
'--keysize',
34 parser.add_argument(
'-c',
'--certificatename',
35 metavar=
"<CertificateName>",
38 dest=
"certificatename")
40 args = parser.parse_args()
42 if not os.path.exists(args.outdir):
43 sys.exit(
'ERROR: Directory %s was not found!' % args.outdir)
48 keysize = args.keysize
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
55 if args.certificatename ==
"":
56 certificatename =
"server"
57 print(
"No Certificate name provided. Setting to %s" % certificatename)
59 if args.certificatename:
60 certificatename = args.certificatename
62 certsdir = os.path.dirname(os.path.abspath(__file__))
67 addr = netifaces.ifaddresses(interface)
68 return netifaces.AF_INET
in addr
75 numberOfInterfaces = int(
format(len(netifaces.interfaces())))
79 for interfaceNum
in range(0, numberOfInterfaces):
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:
98 os.environ[
'IPADDRESS2'] =
"127.0.0.1"
100 os.environ[
'HOSTNAME'] = socket.gethostname()
101 openssl_conf = os.path.join(certsdir,
"localhost.cnf")
103 os.chdir(os.path.abspath(args.outdir))
105 os.system(
"""openssl req \
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))
117 os.remove(
"localhost.key")
118 os.remove(
"localhost.crt")
120 print(
"Certificates generated in " + args.outdir)