blob: 8b963deaf4cb8bbbe4d44bed8dbe208fc3a55e21 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
|
#!/usr/bin/env bash
# generate_certificate.sh — Create a self-signed X.509 certificate for
# open62541 OPC UA applications. Outputs DER-encoded certificate and
# private-key files suitable for the demo programs in this project.
#
# Arguments:
# $1 certs_dir — output directory for generated files (created if missing)
# $2 name — identity name (e.g. "ServerLDS", "Client")
# $3 uri — (optional) application URI; defaults to urn:localhost:bobink:<name>
#
# Options:
# -p <passphrase> — encrypt the private key with the given passphrase
#
# Produces:
# <certs_dir>/<name>_cert.der — DER-encoded X.509 certificate
# <certs_dir>/<name>_cert.pem — PEM-encoded X.509 certificate
# <certs_dir>/<name>_key.der — DER-encoded RSA private key
# <certs_dir>/<name>_key.pem — PEM-encoded RSA private key
# <certs_dir>/<name>.cnf — OpenSSL config (intermediate, kept for reference)
set -euo pipefail # Fail fast; no unset vars; catch pipe failures.
passphrase=""
while getopts "p:" opt; do
case "$opt" in
p) passphrase="$OPTARG" ;;
*)
echo "Usage: generate_certificate.sh [-p passphrase] <certs_dir> <name> [uri]" >&2
exit 1
;;
esac
done
shift $((OPTIND - 1))
if [ $# -lt 2 ] || [ $# -gt 3 ]; then
echo "Usage: generate_certificate.sh [-p passphrase] <certs_dir> <name> [uri]" >&2
exit 1
fi
certs_dir="$1"
name="$2"
cn="${name}@localhost"
uri="${3:-urn:localhost:bobink:${name}}"
mkdir -p "$certs_dir"
cnf="$certs_dir/${name}.cnf"
cat >"$cnf" <<EOF
[req]
distinguished_name = req_dn
x509_extensions = v3_ext
prompt = no
[req_dn]
C = FR
O = Bobink
CN = ${cn}
# OPC UA Part 6 §6.2.2: application-instance certificates must carry these
# key usages, both server and client auth, and a URI SAN matching the
# application URI.
[v3_ext]
basicConstraints = CA:FALSE
keyUsage = digitalSignature, keyEncipherment, nonRepudiation, dataEncipherment
extendedKeyUsage = serverAuth, clientAuth
subjectAltName = DNS:localhost, URI:${uri}
EOF
if [ -n "$passphrase" ]; then
pass_args=(-passout "pass:$passphrase")
else
pass_args=(-nodes)
fi
openssl req -x509 -newkey rsa:2048 "${pass_args[@]}" -sha256 \
-days 365 \
-config "$cnf" \
-keyout "$certs_dir/${name}_key.pem" \
-out "$certs_dir/${name}_cert.pem" \
2>/dev/null
openssl x509 -in "$certs_dir/${name}_cert.pem" -outform der \
-out "$certs_dir/${name}_cert.der"
if [ -n "$passphrase" ]; then
openssl pkcs8 -topk8 -in "$certs_dir/${name}_key.pem" -outform der \
-out "$certs_dir/${name}_key.der" \
-passin "pass:$passphrase" -passout "pass:$passphrase"
else
openssl rsa -in "$certs_dir/${name}_key.pem" -outform der \
-out "$certs_dir/${name}_key.der" 2>/dev/null
fi
echo "Generated certificate '$name' (CN=$cn, URI=$uri):"
echo " $certs_dir/${name}_cert.der"
echo " $certs_dir/${name}_cert.pem"
echo " $certs_dir/${name}_key.der"
echo " $certs_dir/${name}_key.pem"
|