This repository was archived by the owner on Mar 17, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathPGPKeyStore.java
More file actions
242 lines (223 loc) · 10.4 KB
/
PGPKeyStore.java
File metadata and controls
242 lines (223 loc) · 10.4 KB
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
/*
* Copyright (C) 2021 3TUSK
*
* 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, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
// SPDX-Identifier: LGPL-2.1-or-later
package org.teacon.sync;
import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.Marker;
import org.apache.logging.log4j.MarkerManager;
import org.bouncycastle.openpgp.PGPException;
import org.bouncycastle.openpgp.PGPObjectFactory;
import org.bouncycastle.openpgp.PGPPublicKey;
import org.bouncycastle.openpgp.PGPPublicKeyRing;
import org.bouncycastle.openpgp.PGPPublicKeyRingCollection;
import org.bouncycastle.openpgp.PGPSignature;
import org.bouncycastle.openpgp.PGPSignatureList;
import org.bouncycastle.openpgp.PGPUtil;
import org.bouncycastle.openpgp.bc.BcPGPObjectFactory;
import org.bouncycastle.openpgp.operator.bc.BcPGPContentVerifierBuilderProvider;
import org.bouncycastle.util.encoders.Hex;
import javax.naming.Context;
import javax.naming.directory.Attribute;
import javax.naming.directory.Attributes;
import javax.naming.directory.DirContext;
import javax.naming.directory.InitialDirContext;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.file.Files;
import java.nio.file.Path;
import java.time.Instant;
import java.time.temporal.ChronoUnit;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;
public final class PGPKeyStore {
/*
* Implementation overview
*
* This class implements a partial client that supports retrieving
* public PGP keys via HTTP Keyserver Protocol (HKP).
*
* More info about HKP may be found at this IETF draft:
* https://tools.ietf.org/html/draft-shaw-openpgp-hkp-00
*/
private static final Logger LOGGER = LogManager.getLogger("RemoteSync");
private static final Marker MARKER = MarkerManager.getMarker("KeyStore");
private static final boolean RESOLVE_SRV_RECORD;
static {
boolean resolveSrv = false;
try {
Class.forName("com.sun.jndi.dns.DnsContextFactory", false, null);
resolveSrv = true;
} catch (Exception ignored) {
}
RESOLVE_SRV_RECORD = resolveSrv;
}
private final PGPPublicKeyRingCollection keyRings;
public PGPKeyStore(Path localKeyStorePath, List<String> keyServers, List<String> keyIds) throws Exception {
final Map<Long, PGPPublicKeyRing> keyRings = new HashMap<>();
if (Files.exists(localKeyStorePath)) {
LOGGER.debug(MARKER, "Try reading keys from local key ring at {}", localKeyStorePath);
readKeys(Files.newInputStream(localKeyStorePath), keyRings);
}
for (String keyId : keyIds) {
if (keyRings.containsKey(keyId.startsWith("0x") ? Long.parseUnsignedLong(keyId.substring(2), 16) : Long.parseUnsignedLong(keyId, 16))) {
LOGGER.debug(MARKER,"Not trying to load key {} because it exists locally", keyId);
continue;
}
final String queryParams = "/pks/lookup?op=get&search=".concat(keyId);
for (String keyServer : keyServers) {
final URL resolved = resolveSrv(new URL(keyServer));
final URL keyQuery = new URL(resolved.getProtocol(), resolved.getHost(), resolved.getPort(), queryParams);
try (InputStream input = keyQuery.openStream()) {
LOGGER.debug(MARKER, "Receiving key {} from {}", keyId, keyServer);
readKeys(input, keyRings);
break; // Stop on first available key server
} catch (Exception ignored) {
// No-op
}
}
}
this.keyRings = new PGPPublicKeyRingCollection(keyRings.values());
this.saveTo(localKeyStorePath);
}
public void debugDump() {
for (PGPPublicKeyRing ring : this.keyRings) {
for (PGPPublicKey pubKey : ring) {
LOGGER.printf(Level.DEBUG, MARKER, "Public Key ID = %1$016X, Algo = %2$s, Fingerprint = %3$s",
pubKey.getKeyID(), Utils.getKeyAlgorithm(pubKey.getAlgorithm()), Hex.toHexString(pubKey.getFingerprint()));
}
}
}
public boolean verify(FileChannel src, PGPSignatureList sigList) {
for (PGPSignature sig : sigList) {
try {
PGPPublicKey pubKey = this.keyRings.getPublicKey(sig.getKeyID());
if (pubKey == null) {
LOGGER.printf(Level.WARN, MARKER, "Cannot find key %1$016X in current key ring, signature cannot be verified",
sig.getKeyID());
return false;
}
sig.init(new BcPGPContentVerifierBuilderProvider(), pubKey);
// Has to be a heap buffer, BouncyCastle only supports passing in byte[]
ByteBuffer buf = ByteBuffer.allocate(1 << 12);
src.position(0);
int limit;
while ((limit = src.read(buf)) != -1) {
buf.flip(); // limit = pos, pos = 0
sig.update(buf.array(), 0, limit);
buf.clear(); // limit = cap, pos = 0
}
if (!sig.verify()) {
LOGGER.printf(Level.WARN, MARKER, "Signature verification failed (%1$s key %2$016X, made on %3$tc)",
Utils.getKeyAlgorithm(sig.getKeyAlgorithm()), sig.getKeyID(), sig.getCreationTime());
return false;
} else if (pubKey.hasRevocation()) {
LOGGER.printf(Level.WARN, MARKER, "Signature verified (%1$s key %2$016X, made on %3$tc) but the key-pair has been revoked",
Utils.getKeyAlgorithm(sig.getKeyAlgorithm()), sig.getKeyID(), sig.getCreationTime());
return false;
} else if (hasExpired(pubKey)) {
LOGGER.printf(Level.WARN, MARKER, "Signature verified (%1$s key %2$016X, made on %3$tc) but the key-pair has expired",
Utils.getKeyAlgorithm(sig.getKeyAlgorithm()), sig.getKeyID(), sig.getCreationTime());
return false;
} else {
LOGGER.printf(Level.DEBUG, MARKER, "Signature verified: %1$s key %2$016X, made on %3$tc",
Utils.getKeyAlgorithm(sig.getKeyAlgorithm()), sig.getKeyID(), sig.getCreationTime());
}
} catch (PGPException e) {
LOGGER.warn(MARKER, "Unknown error occurred, signature verification automatically fails");
LOGGER.debug(MARKER, "Error details: ", e);
return false;
} catch (IOException e) {
LOGGER.warn(MARKER,"Failed to read file while checking signature", e);
return false;
}
}
return true;
}
public void saveTo(Path path) {
try (OutputStream output = Files.newOutputStream(path)) {
this.keyRings.encode(output);
} catch (IOException e) {
LOGGER.warn(MARKER, "Failed to save key store", e);
}
}
private static void readKeys(InputStream input, Map<Long, PGPPublicKeyRing> keyRings) throws Exception {
try (InputStream wrapped = PGPUtil.getDecoderStream(input)) {
PGPObjectFactory factory = new BcPGPObjectFactory(wrapped);
for (Object o : factory) {
if (o instanceof PGPPublicKeyRing keyRing) {
keyRings.put(keyRing.getPublicKey().getKeyID(), keyRing);
} else {
LOGGER.warn(MARKER, "Invalid PGP object {} (type {}) found and ignored", o, o.getClass());
}
}
}
}
private static URL resolveSrv(URL original) {
if (!RESOLVE_SRV_RECORD) {
return original;
}
/*
* Perform DNS query via JNDI.
*
* Documentation:
* https://docs.oracle.com/javase/8/docs/technotes/guides/jndi/jndi-dns.html
*/
try {
Hashtable<String, String> params = new Hashtable<>();
params.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.dns.DnsContextFactory");
params.put(Context.PROVIDER_URL, "dns:");
params.put("com.sun.jndi.dns.timeout.retries", "1");
DirContext context = new InitialDirContext(params);
// https://tools.ietf.org/html/draft-shaw-openpgp-hkp-00 Chapter 7
Attributes attrs = context.getAttributes("_hkp._tcp." + original.getHost(), new String[]{"SRV"});
Attribute attr = attrs.get("srv");
if (attr != null) {
String[] results = attr.get().toString().split(" ", 4);
int port = 80;
try {
port = Integer.parseInt(results[2]);
} catch (Exception ignored) {
}
return new URL(original.getProtocol(), results[3], port, "/");
}
} catch (Exception e) {
LOGGER.debug(MARKER, "Failed to resolve SRV record for '{}'", original, e);
}
return original;
}
private static boolean hasExpired(PGPPublicKey pubKey) {
long duration = pubKey.getValidSeconds();
if (duration > 0) {
// Only check expiration if getValidSeconds() returns a positive value
Instant creationTime = pubKey.getCreationTime().toInstant();
Instant expirationTime = creationTime.plus(duration, ChronoUnit.SECONDS);
return Instant.now().isAfter(expirationTime);
} else {
// If getValidSeconds() returns 0 or less it means no expiration.
return false;
}
}
}