-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy pathAutoUpdater.java
More file actions
205 lines (184 loc) · 8.45 KB
/
AutoUpdater.java
File metadata and controls
205 lines (184 loc) · 8.45 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
package com.itextpdf.research.autoupdate;
import com.itextpdf.kernel.pdf.PdfArray;
import com.itextpdf.kernel.pdf.PdfDictionary;
import com.itextpdf.kernel.pdf.PdfName;
import com.itextpdf.kernel.pdf.PdfString;
import com.itextpdf.rups.model.LoggerHelper;
import com.itextpdf.rups.model.PdfFile;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import java.security.GeneralSecurityException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.SignatureException;
import java.util.Base64;
import java.util.Base64.Encoder;
public class AutoUpdater {
private final PdfFile input;
private final OutputStream output;
private final PdfDictionary autoUpdateDict;
private byte[] fileContent;
public AutoUpdater(PdfFile input, OutputStream output) {
this.input = input;
this.output = output;
this.autoUpdateDict = input.getPdfDocument().getCatalog().getPdfObject()
.getAsDictionary(new PdfName("AutoUpdate"));
}
public boolean hasAutoUpdate() {
return autoUpdateDict != null;
}
private byte[] getFileContent() {
if (fileContent == null) {
fileContent = input.getBytes();
}
return fileContent;
}
private String getResourceIdentifier() {
PdfName addressMode = autoUpdateDict.getAsName(new PdfName("AddressMode"));
Encoder enc = Base64.getUrlEncoder();
if ("ContentDigest".equals(addressMode.getValue())) {
byte[] hash;
try {
MessageDigest md = MessageDigest.getInstance("SHA384");
md.update(getFileContent());
hash = md.digest();
} catch (NoSuchAlgorithmException e) {
throw new IllegalStateException(e);
}
return new String(enc.encode(hash), StandardCharsets.UTF_8);
} else if ("DocumentID".equals(addressMode.getValue())) {
PdfArray arr = input.getPdfDocument().getTrailer().getAsArray(PdfName.ID);
byte[] id1 = arr.getAsString(0).getValueBytes();
byte[] id2 = arr.getAsString(1).getValueBytes();
return String.format(
"%s/%s",
new String(enc.encode(id1), StandardCharsets.UTF_8),
new String(enc.encode(id2), StandardCharsets.UTF_8)
);
}
throw new IllegalStateException();
}
private URL getUpdateURL() throws MalformedURLException {
PdfString repo = autoUpdateDict.getAsString(new PdfName("Repository"));
PdfName addressMode = autoUpdateDict.getAsName(new PdfName("AddressMode"));
StringBuilder urlStr = new StringBuilder(repo.toUnicodeString());
if ("ContentDigest".equals(addressMode.getValue())) {
urlStr.append("/hash/");
} else if ("DocumentID".equals(addressMode.getValue())) {
urlStr.append("/docId/");
}
String resourceId = getResourceIdentifier();
urlStr.append(resourceId);
URL result = new URL(urlStr.toString());
LoggerHelper.info("Fetching update with ID " + resourceId + "; URL is " + result, AutoUpdater.class);
return result;
}
private static boolean nameValueIs(PdfDictionary dict, String key, String expectedValue) {
PdfName valueFound = dict.getAsName(new PdfName(key));
return valueFound != null && expectedValue.equals(valueFound.getValue());
}
public void downloadAndApplyUpdate() throws IOException, UpdateVerificationException {
// this method only implements a small part of the draft spec, but was written to
// somewhat realistically represent how one would ingest an update "for real", e.g.
// with potentially large update payloads, only exposing update content to the
// caller after verification passes, etc.
// TODO refactor
if (!nameValueIs(autoUpdateDict, "UpdateType", "Incremental")) {
throw new IOException("Only Incremental is supported in this PoC");
}
PdfDictionary integrity = autoUpdateDict.getAsDictionary(new PdfName("Integrity"));
PasetoV4PublicVerifier verifier;
if (integrity != null) {
boolean isPasetoV4Pub =
nameValueIs(integrity, "CertDataType", "PASETOV4Public");
PdfString pskStr = integrity.getAsString(new PdfName("PreSharedKey"));
if (!isPasetoV4Pub || pskStr == null) {
throw new UpdateVerificationException("Only PASETOV4Public with pre-shared keys is supported");
}
try {
verifier = new PasetoV4PublicVerifier(pskStr.getValueBytes());
} catch (GeneralSecurityException e) {
LoggerHelper.error(e.getMessage(), e, AutoUpdater.class);
throw new UpdateVerificationException("Key deser error", e);
}
} else {
verifier = null;
}
// stream the update content to disk (out of sight) while also feeding it to the verifier
Path tempFile = Files.createTempFile("pdfupdate", ".bin");
long contentLength = -1;
try (OutputStream tempOut = Files.newOutputStream(tempFile)) {
byte[] buf = new byte[2048];
HttpURLConnection conn = (HttpURLConnection) getUpdateURL().openConnection();
if (conn.getResponseCode() != 200) {
throw new IOException("Fetch failed; error " + conn.getResponseCode());
}
if (verifier != null) {
try {
contentLength = conn.getContentLengthLong();
if (contentLength == -1) {
throw new IOException("Content-Length must be present for this PoC");
}
verifier.init(conn.getHeaderField("X-PDF-Update-Token").getBytes(StandardCharsets.UTF_8), contentLength);
} catch (GeneralSecurityException e) {
LoggerHelper.error(e.getMessage(), e, AutoUpdater.class);
throw new UpdateVerificationException("Cryptographic failure", e);
}
}
InputStream is = conn.getInputStream();
int bytesRead;
while ((bytesRead = is.read(buf)) > 0) {
tempOut.write(buf, 0, bytesRead);
if (verifier != null) {
try {
verifier.updateImplicit(buf, 0, bytesRead);
} catch (GeneralSecurityException e) {
LoggerHelper.error(e.getMessage(), e, AutoUpdater.class);
throw new UpdateVerificationException("Cryptographic failure", e);
}
}
}
}
if (verifier != null) {
byte[] payload;
try {
payload = verifier.verifyAndGetPayload();
} catch (SignatureException e) {
LoggerHelper.error(e.getMessage(), e, AutoUpdater.class);
throw new UpdateVerificationException("Cryptographic failure", e);
}
// verify the token contents
// TODO do this with a schema and null-safe queries
JsonObject el = JsonParser
.parseString(new String(payload, StandardCharsets.UTF_8))
.getAsJsonObject();
// TODO check updateType, protocol version
if (!getResourceIdentifier().equals(el.getAsJsonPrimitive("resourceId").getAsString())) {
throw new UpdateVerificationException("Resource ID mismatch");
}
if (el.getAsJsonPrimitive("updateLength").getAsNumber().longValue()
!= contentLength) {
throw new UpdateVerificationException("Length mismatch");
}
}
// all clear -> proceed to output
output.write(getFileContent());
try (InputStream tempIn = Files.newInputStream(tempFile, StandardOpenOption.DELETE_ON_CLOSE)) {
byte[] buf = new byte[2048];
int bytesRead;
while ((bytesRead = tempIn.read(buf)) > 0) {
output.write(buf, 0, bytesRead);
}
}
}
}