97ba4c229e20887e70b1356703aff580a378cba8
[sim/o1-interface.git] / ntsimulator / deploy / tls / enable_tls.sh
1 #! /bin/bash
2 ################################################################################
3 #
4 # Copyright 2020 highstreet technologies GmbH and others
5 #
6 # Licensed under the Apache License, Version 2.0 (the "License");
7 # you may not use this file except in compliance with the License.
8 # You may obtain a copy of the License at
9
10 #     http://www.apache.org/licenses/LICENSE-2.0
11
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS,
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 # See the License for the specific language governing permissions and
16 # limitations under the License.
17
18 ################################################################################
19
20 sleep 10
21
22 openssl genrsa -out melacon.server.key 2048
23
24 openssl req -new -sha256 -key melacon.server.key -subj "/C=US/ST=CA/O=MeLaCon, Inc./CN=melacon.com" -out melacon.server.csr
25 openssl x509 -req -in melacon.server.csr -CA ca.pem -CAkey ca.key -CAcreateserial -out melacon.server.crt -days 500 -sha256
26 rm melacon.server.csr
27 ssh-keygen -y -f melacon.server.key > melacon.server.key.pub
28
29 SSH_PUB_KEY="$(cat /home/netconf/.ssh/id_dsa.pub| awk '{print $2}')"
30 SSH_PUB_KEY_MELACON="$(cat melacon.server.key.pub | awk '{print $2}')"
31
32 echo '<system xmlns="urn:ietf:params:xml:ns:yang:ietf-system"><authentication><user><name>netconf</name>'  >> load_auth_pubkey.xml
33 echo '<authorized-key><name>ssh_key</name><algorithm>ssh-dss</algorithm>' >> load_auth_pubkey.xml
34 echo '<key-data>'"$SSH_PUB_KEY"'</key-data></authorized-key>' >> load_auth_pubkey.xml
35 echo '<authorized-key><name>melacon_server_key</name><algorithm>ssh-rsa</algorithm>' >> load_auth_pubkey.xml
36 echo '<key-data>'"$SSH_PUB_KEY_MELACON"'</key-data></authorized-key></user></authentication></system>' >> load_auth_pubkey.xml
37
38 sysrepocfg --merge=load_auth_pubkey.xml --format=xml ietf-system
39 rm load_auth_pubkey.xml
40
41 if [ $IPv6Enabled = "true" ]; then
42    ssh-keyscan -p 830 :: >> ~/.ssh/known_hosts
43 fi
44 ssh-keyscan -p 830 127.0.0.1 >> /root/.ssh/known_hosts
45
46 MELACON_SERVER_KEY="$(sed '1d;$d' melacon.server.key)"
47
48 echo '<action xmlns="urn:ietf:params:xml:ns:yang:1"><keystore xmlns="urn:ietf:params:xml:ns:yang:ietf-keystore"><private-keys><load-private-key><name>melacon_server_key</name>' >> load_private_key.xml
49 echo '<private-key>'"$MELACON_SERVER_KEY"'</private-key></load-private-key></private-keys></keystore></action>' >> load_private_key.xml
50
51 netopeer2-cli <<END
52 auth pref publickey 1000
53 auth keys add /home/netconf/.ssh/id_dsa.pub /home/netconf/.ssh/id_dsa
54 connect --host 127.0.0.1 --login netconf
55 user-rpc --content=load_private_key.xml
56 disconnect
57 END
58
59 rm load_private_key.xml
60
61 MELACON_CERT="$(sed '1d;$d' melacon.server.crt)"
62 CA_CERT="$(sed '1d;$d' ca.pem)"
63
64 echo '<keystore xmlns="urn:ietf:params:xml:ns:yang:ietf-keystore"><private-keys><private-key><name>melacon_server_key</name><certificate-chains><certificate-chain><name>melacon_server_cert</name>' >> load_server_certs.xml
65 echo '<certificate>'"$MELACON_CERT"'</certificate></certificate-chain></certificate-chains></private-key></private-keys><trusted-certificates><name>trusted_ca_list</name><trusted-certificate><name>ca</name>' >> load_server_certs.xml
66 echo '<certificate>'"$CA_CERT"'</certificate></trusted-certificate></trusted-certificates></keystore>' >> load_server_certs.xml
67
68 sysrepocfg --merge=load_server_certs.xml --format=xml ietf-keystore
69 rm load_server_certs.xml
70
71 # enable the SSH and TLS connections, according to the configuration file
72 ./enable_connections.sh
73
74 echo 'Done'
75 exit 0