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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
|
# Copyright VyOS maintainers and contributors <maintainers@vyos.io>
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this library. If not, see <http://www.gnu.org/licenses/>.
# Migrate Wireguard to store keys in CLI
# Migrate EAPoL to PKI configuration
import os
from vyos.configtree import ConfigTree
from vyos.pki import CERT_BEGIN
from vyos.pki import load_certificate
from vyos.pki import load_crl
from vyos.pki import load_dh_parameters
from vyos.pki import load_private_key
from vyos.pki import encode_certificate
from vyos.pki import encode_dh_parameters
from vyos.pki import encode_private_key
from vyos.pki import verify_crl
from vyos.utils.process import run
from vyos.utils.file import read_file
def wrapped_pem_to_config_value(pem):
out = []
for line in pem.strip().split("\n"):
if not line or line.startswith("-----") or line[0] == '#':
continue
out.append(line)
return "".join(out)
def read_auth_file(config_auth_path):
full_path = os.path.normpath(os.path.join(AUTH_DIR, config_auth_path))
# If the file is not found under `/config/auth`, it may be because the `/config`
# partition has not been bind-mounted yet during early boot migration execution.
# Fall back to the equivalent path under `/opt/vyatta/etc/config/auth` which
# is accessible at all boot stages.
if not os.path.isfile(full_path) and full_path.startswith(f'{AUTH_DIR}/'):
full_path = AUTH_DIR_FALLBACK + full_path[len(AUTH_DIR): ]
if os.path.isfile(full_path):
if not os.access(full_path, os.R_OK):
run(['sudo', 'chmod', '644', full_path])
return read_file(full_path)
return None
AUTH_DIR = '/config/auth'
AUTH_DIR_FALLBACK = '/opt/vyatta/etc/config/auth'
pki_base = ['pki']
def migrate(config: ConfigTree) -> None:
# OpenVPN
base = ['interfaces', 'openvpn']
if config.exists(base):
for interface in config.list_nodes(base):
x509_base = base + [interface, 'tls']
pki_name = f'openvpn_{interface}'
if config.exists(base + [interface, 'shared-secret-key-file']):
if not config.exists(pki_base + ['openvpn', 'shared-secret']):
config.set(pki_base + ['openvpn', 'shared-secret'])
config.set_tag(pki_base + ['openvpn', 'shared-secret'])
key_file = config.return_value(base + [interface, 'shared-secret-key-file'])
key = read_auth_file(key_file)
key_pki_name = f'{pki_name}_shared'
if key:
# Check if OpenVPN shared-secret already exists - no need to check node
# existence as it is always created above when entering this context
secret_exists = None
for secret_name in config.list_nodes(pki_base + ['openvpn', 'shared-secret']):
secret_path = pki_base + ['openvpn', 'shared-secret', secret_name, 'key']
if not config.exists(secret_path):
continue
secret = config.return_value(secret_path)
# Check for duplicate cert/key - and re-use if possible
if secret == wrapped_pem_to_config_value(key):
secret_exists = secret_name
break
if secret_exists:
config.set(base + [interface, 'shared-secret-key'], value=secret_exists)
else:
config.set(pki_base + ['openvpn', 'shared-secret', key_pki_name, 'key'], value=wrapped_pem_to_config_value(key))
config.set(pki_base + ['openvpn', 'shared-secret', key_pki_name, 'version'], value='1')
config.set(base + [interface, 'shared-secret-key'], value=key_pki_name)
else:
print(f'Failed to migrate shared-secret-key on openvpn interface {interface}')
config.delete(base + [interface, 'shared-secret-key-file'])
if not config.exists(base + [interface, 'tls']):
continue
if config.exists(base + [interface, 'tls', 'auth-file']):
if not config.exists(pki_base + ['openvpn', 'shared-secret']):
config.set(pki_base + ['openvpn', 'shared-secret'])
config.set_tag(pki_base + ['openvpn', 'shared-secret'])
key_file = config.return_value(base + [interface, 'tls', 'auth-file'])
key = read_auth_file(key_file)
key_pki_name = f'{pki_name}_auth'
if key:
# Check if OpenVPN auth key already exists - no need to check node
# existence as it is always created above when entering this context
secret_exists = None
for secret_name in config.list_nodes(pki_base + ['openvpn', 'shared-secret']):
secret_path = pki_base + ['openvpn', 'shared-secret', secret_name, 'key']
if not config.exists(secret_path):
continue
secret = config.return_value(secret_path)
# Check for duplicate cert/key - and re-use if possible
if secret == wrapped_pem_to_config_value(key):
secret_exists = secret_name
break
if secret_exists:
config.set(base + [interface, 'tls', 'auth-key'], value=secret_exists)
else:
config.set(pki_base + ['openvpn', 'shared-secret', key_pki_name, 'key'], value=wrapped_pem_to_config_value(key))
config.set(pki_base + ['openvpn', 'shared-secret', key_pki_name, 'version'], value='1')
config.set(base + [interface, 'tls', 'auth-key'], value=key_pki_name)
else:
print(f'Failed to migrate auth-key on openvpn interface {interface}')
config.delete(base + [interface, 'tls', 'auth-file'])
if config.exists(base + [interface, 'tls', 'crypt-file']):
if not config.exists(pki_base + ['openvpn', 'shared-secret']):
config.set(pki_base + ['openvpn', 'shared-secret'])
config.set_tag(pki_base + ['openvpn', 'shared-secret'])
key_file = config.return_value(base + [interface, 'tls', 'crypt-file'])
key = read_auth_file(key_file)
key_pki_name = f'{pki_name}_crypt'
if key:
# Check if OpenVPN auth key already exists - no need to check node
# existence as it is always created above when entering this context
secret_exists = None
for secret_name in config.list_nodes(pki_base + ['openvpn', 'shared-secret']):
secret_path = pki_base + ['openvpn', 'shared-secret', secret_name, 'key']
if not config.exists(secret_path):
continue
secret = config.return_value(secret_path)
# Check for duplicate cert/key - and re-use if possible
if secret == wrapped_pem_to_config_value(key):
secret_exists = secret_name
break
if secret_exists:
config.set(base + [interface, 'tls', 'crypt-key'], value=secret_exists)
else:
config.set(pki_base + ['openvpn', 'shared-secret', key_pki_name, 'key'], value=wrapped_pem_to_config_value(key))
config.set(pki_base + ['openvpn', 'shared-secret', key_pki_name, 'version'], value='1')
config.set(base + [interface, 'tls', 'crypt-key'], value=key_pki_name)
else:
print(f'Failed to migrate crypt-key on openvpn interface {interface}')
config.delete(base + [interface, 'tls', 'crypt-file'])
ca_certs = {}
if config.exists(x509_base + ['ca-cert-file']):
if not config.exists(pki_base + ['ca']):
config.set(pki_base + ['ca'])
config.set_tag(pki_base + ['ca'])
cert_file = config.return_value(x509_base + ['ca-cert-file'])
certs_str = read_auth_file(cert_file)
if certs_str:
certs_data = certs_str.split(CERT_BEGIN)
index = 1
for cert_data in certs_data[1:]:
cert = load_certificate(CERT_BEGIN + cert_data, wrap_tags=False)
if cert:
ca_certs[f'{pki_name}_{index}'] = cert
cert_pem = encode_certificate(cert)
# Check if CA already exists - no need to check node existence as
# it is always created above when entering this context
ca_exists = None
for ca_name in config.list_nodes(pki_base + ['ca']):
ca_cert_path = pki_base + ['ca', ca_name, 'certificate']
if not config.exists(ca_cert_path):
continue
ca_base64 = config.return_value(ca_cert_path)
# Check for duplicate cert/key - and re-use if possible
if ca_base64 == wrapped_pem_to_config_value(cert_pem):
ca_exists = ca_name
break
if ca_exists:
config.set(x509_base + ['ca-certificate'], value=ca_exists, replace=False)
else:
config.set(pki_base + ['ca', f'{pki_name}_{index}', 'certificate'], value=wrapped_pem_to_config_value(cert_pem))
config.set(x509_base + ['ca-certificate'], value=f'{pki_name}_{index}', replace=False)
else:
print(f'Failed to migrate CA certificate on openvpn interface {interface}')
index += 1
else:
print(f'Failed to migrate CA certificate on openvpn interface {interface}')
config.delete(x509_base + ['ca-cert-file'])
if config.exists(x509_base + ['crl-file']):
if not config.exists(pki_base + ['ca']):
config.set(pki_base + ['ca'])
config.set_tag(pki_base + ['ca'])
crl_file = config.return_value(x509_base + ['crl-file'])
crl_data = read_auth_file(crl_file)
crl = load_crl(crl_data, wrap_tags=False) if crl_data else None
crl_ca_name = None
if crl:
for ca_name, ca_cert in ca_certs.items():
if verify_crl(crl, ca_cert):
crl_ca_name = ca_name
break
if crl_ca_name:
crl_pem = encode_certificate(crl)
# Check if CRL already exists - no need to check node
# existence as it is always created above when entering this context
crl_exists = None
for ca_name in config.list_nodes(pki_base + ['ca']):
crl_path = pki_base + ['ca', ca_name, 'crl']
if not config.exists(crl_path):
continue
crl_base64 = config.return_value(crl_path)
# Check if CRL is a duplicate and we have already imported it
if crl_base64 == wrapped_pem_to_config_value(crl_pem):
crl_exists = ca_name
break
if not crl_exists:
config.set(pki_base + ['ca', crl_ca_name, 'crl'], value=wrapped_pem_to_config_value(crl_pem))
else:
print(f'Failed to migrate CRL on openvpn interface {interface}')
config.delete(x509_base + ['crl-file'])
if config.exists(x509_base + ['cert-file']):
if not config.exists(pki_base + ['certificate']):
config.set(pki_base + ['certificate'])
config.set_tag(pki_base + ['certificate'])
cert_file = config.return_value(x509_base + ['cert-file'])
cert_data = read_auth_file(cert_file)
cert = load_certificate(cert_data, wrap_tags=False) if cert_data else None
if cert:
cert_pem = encode_certificate(cert)
# Check if certificate public key already exists - no need to check node
# existence as it is always created above when entering this context
cert_exists = None
for cert_name in config.list_nodes(pki_base + ['certificate']):
cert_path = pki_base + ['certificate', cert_name, 'certificate']
if not config.exists(cert_path):
continue
cert_base64 = config.return_value(cert_path)
# Check for duplicate cert/key - and re-use if possible
if cert_base64 == wrapped_pem_to_config_value(cert_pem):
cert_exists = cert_name
break
if cert_exists:
config.set(x509_base + ['certificate'], value=cert_exists)
else:
config.set(pki_base + ['certificate', pki_name, 'certificate'], value=wrapped_pem_to_config_value(cert_pem))
config.set(x509_base + ['certificate'], value=pki_name)
else:
print(f'Failed to migrate certificate on openvpn interface {interface}')
config.delete(x509_base + ['cert-file'])
if config.exists(x509_base + ['key-file']):
key_file = config.return_value(x509_base + ['key-file'])
key_data = read_auth_file(key_file)
key = load_private_key(key_data, passphrase=None, wrap_tags=False) if key_data else None
if key:
key_pem = encode_private_key(key, passphrase=None)
# Check if certificate public key already exists - no need to check node
# existence as it is always created above when entering this context
key_exists = None
for key_name in config.list_nodes(pki_base + ['certificate']):
key_path = pki_base + ['certificate', key_name, 'private', 'key']
if not config.exists(key_path):
continue
key_base64 = config.return_value(key_path)
# Check for duplicate cert/key - and re-use if possible
if key_base64 == wrapped_pem_to_config_value(key_pem):
key_exists = key_name
break
if not key_exists:
config.set(pki_base + ['certificate', pki_name, 'private', 'key'], value=wrapped_pem_to_config_value(key_pem))
else:
print(f'Failed to migrate private key on openvpn interface {interface}')
config.delete(x509_base + ['key-file'])
if config.exists(x509_base + ['dh-file']):
if not config.exists(pki_base + ['dh']):
config.set(pki_base + ['dh'])
config.set_tag(pki_base + ['dh'])
dh_file = config.return_value(x509_base + ['dh-file'])
dh_data = read_auth_file(dh_file)
dh = load_dh_parameters(dh_data, wrap_tags=False) if dh_data else None
if dh:
dh_pem = encode_dh_parameters(dh)
# Check if DH parameters already exists - no need to check node existence
# as it is always created above when entering this context
dh_exists = None
for dh_name in config.list_nodes(pki_base + ['dh']):
dh_param_path = pki_base + ['dh', dh_name, 'parameters']
if not config.exists(dh_param_path):
continue
dh_base64 = config.return_value(dh_param_path)
# Check for duplicate cert/key - and re-use if possible
if dh_base64 == wrapped_pem_to_config_value(dh_pem):
dh_exists = dh_name
break
if dh_exists:
config.set(x509_base + ['dh-params'], value=dh_exists)
else:
config.set(pki_base + ['dh', pki_name, 'parameters'], value=wrapped_pem_to_config_value(dh_pem))
config.set(x509_base + ['dh-params'], value=pki_name)
else:
print(f'Failed to migrate DH parameters on openvpn interface {interface}')
config.delete(x509_base + ['dh-file'])
# Wireguard
base = ['interfaces', 'wireguard']
if config.exists(base):
for interface in config.list_nodes(base):
private_key_path = base + [interface, 'private-key']
key_file = 'default'
if config.exists(private_key_path):
key_file = config.return_value(private_key_path)
full_key_path = f'{AUTH_DIR}/wireguard/{key_file}/private.key'
key_data = read_auth_file(full_key_path)
if not key_data:
print(f'Could not find wireguard private key for migration on interface "{interface}"')
continue
key_data = key_data.strip()
config.set(private_key_path, value=key_data)
for peer in config.list_nodes(base + [interface, 'peer']):
config.rename(base + [interface, 'peer', peer, 'pubkey'], 'public-key')
# Ethernet EAPoL
base = ['interfaces', 'ethernet']
if config.exists(base):
for interface in config.list_nodes(base):
if not config.exists(base + [interface, 'eapol']):
continue
x509_base = base + [interface, 'eapol']
pki_name = f'eapol_{interface}'
if config.exists(x509_base + ['ca-cert-file']):
if not config.exists(pki_base + ['ca']):
config.set(pki_base + ['ca'])
config.set_tag(pki_base + ['ca'])
cert_file = config.return_value(x509_base + ['ca-cert-file'])
cert_data = read_auth_file(cert_file)
cert = load_certificate(cert_data, wrap_tags=False) if cert_data else None
if cert:
cert_pem = encode_certificate(cert)
config.set(pki_base + ['ca', pki_name, 'certificate'], value=wrapped_pem_to_config_value(cert_pem))
config.set(x509_base + ['ca-certificate'], value=pki_name)
else:
print(f'Failed to migrate CA certificate on eapol config for interface {interface}')
config.delete(x509_base + ['ca-cert-file'])
if config.exists(x509_base + ['cert-file']):
if not config.exists(pki_base + ['certificate']):
config.set(pki_base + ['certificate'])
config.set_tag(pki_base + ['certificate'])
cert_file = config.return_value(x509_base + ['cert-file'])
cert_data = read_auth_file(cert_file)
cert = load_certificate(cert_data, wrap_tags=False) if cert_data else None
if cert:
cert_pem = encode_certificate(cert)
config.set(pki_base + ['certificate', pki_name, 'certificate'], value=wrapped_pem_to_config_value(cert_pem))
config.set(x509_base + ['certificate'], value=pki_name)
else:
print(f'Failed to migrate certificate on eapol config for interface {interface}')
config.delete(x509_base + ['cert-file'])
if config.exists(x509_base + ['key-file']):
key_file = config.return_value(x509_base + ['key-file'])
key_data = read_auth_file(key_file)
key = load_private_key(key_data, passphrase=None, wrap_tags=False) if key_data else None
if key:
key_pem = encode_private_key(key, passphrase=None)
config.set(pki_base + ['certificate', pki_name, 'private', 'key'], value=wrapped_pem_to_config_value(key_pem))
else:
print(f'Failed to migrate private key on eapol config for interface {interface}')
config.delete(x509_base + ['key-file'])
|