-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathKeyringViewer.java
115 lines (98 loc) · 3.8 KB
/
KeyringViewer.java
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
/*
Calimero 2 - A library for KNX network access
Copyright (c) 2019, 2023 B. Malinowsky
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
import java.util.HexFormat;
import java.util.Optional;
import io.calimero.secure.Keyring;
/**
* Prints the (decrypted) content of a keyring (*.knxkeys) resource. Minimum requirements are Calimero version ≥
* 3.0-SNAPSHOT and Java SE 17 (module java.base).
* <p>
* You can safely run this example, the keyring is not modified, and any content is output to {@code System.out}.
*/
public class KeyringViewer {
private final Keyring keyring;
private char[] keyringPwd = new char[0];
public static void main(final String... args) {
if (args.length == 0) {
System.out.println("Usage: [--pwd keyringPassword] <keyring resource (*.knxkeys)>");
return;
}
new KeyringViewer(args).view();
}
public KeyringViewer(final String... args) {
int argIdx = 0;
if ("--pwd".equals(args[argIdx])) {
keyringPwd = args[++argIdx].toCharArray();
++argIdx;
}
keyring = Keyring.load(args[argIdx]);
}
private void view() {
// check keyring signature if we got a password
if (keyringPwd.length > 0) {
final var valid = keyring.verifySignature(keyringPwd);
final var result = valid ? "OK" : "FAILED!";
System.out.println("Signature verification " + result);
}
keyring.backbone().ifPresent(bb -> {
header("Backbone");
final var keyInfo = bb.groupKey().filter(__ -> keyringPwd.length > 0).map(key -> ", key " + decryptKey(key))
.orElse("");
System.out.println(bb + keyInfo);
});
header("Devices");
for (final var device : keyring.devices().values()) {
System.out.print(device);
if (keyringPwd.length > 0) {
System.out.print(", management password " + decryptPwd(device.password()));
System.out.print(", authentication " + decryptPwd(device.authentication()));
System.out.print(", toolkey " + device.toolKey().map(this::decryptKey).orElse("n/a"));
}
System.out.println();
}
for (final var entry : keyring.interfaces().entrySet()) {
header("Interfaces of device " + entry.getKey());
final var interfaces = entry.getValue();
for (final var iface : interfaces) {
System.out.print(iface);
if (keyringPwd.length > 0) {
System.out.print(", management password " + decryptPwd(iface.password()));
System.out.print(", authentication " + decryptPwd(iface.authentication()));
}
System.out.println();
}
}
header("Groups");
for (final var group : keyring.groups().entrySet()) {
if (keyringPwd.length > 0)
System.out.println(group.getKey() + " key " + decryptKey(group.getValue()));
else
System.out.println(group.getKey());
}
}
private String decryptPwd(final Optional<byte[]> input) {
return input.map(this::decryptPwd).orElse("n/a");
}
private String decryptPwd(final byte[] input) {
return "'" + new String(keyring.decryptPassword(input, keyringPwd)) + "'";
}
private String decryptKey(final byte[] input) {
return "'" + HexFormat.of().formatHex(keyring.decryptKey(input, keyringPwd)) + "'";
}
private static void header(final String header) {
System.out.format("%n%s%n%s%n", header, "-".repeat(header.length()));
}
}