|
33 | 33 | import cc.arduino.utils.MultiStepProgress;
|
34 | 34 | import cc.arduino.utils.Progress;
|
35 | 35 | import cc.arduino.utils.network.FileDownloader;
|
36 |
| -import org.slf4j.Logger; |
37 |
| -import org.slf4j.LoggerFactory; |
| 36 | +import org.apache.logging.log4j.LogManager; |
| 37 | +import org.apache.logging.log4j.Logger; |
38 | 38 | import processing.app.BaseNoGui;
|
| 39 | +import processing.app.PreferencesData; |
39 | 40 |
|
40 | 41 | import java.io.File;
|
41 | 42 | import java.net.URL;
|
42 | 43 | import java.nio.file.*;
|
| 44 | +import java.util.LinkedList; |
43 | 45 | import java.util.List;
|
44 | 46 |
|
45 | 47 | import static processing.app.I18n.format;
|
46 | 48 | import static processing.app.I18n.tr;
|
47 | 49 |
|
48 | 50 | public class DownloadableContributionsDownloader {
|
49 |
| - private static Logger log = LoggerFactory.getLogger(DownloadableContributionsDownloader.class); |
| 51 | + private static Logger log = LogManager.getLogger(DownloadableContributionsDownloader.class); |
50 | 52 |
|
51 | 53 | private final File stagingFolder;
|
52 | 54 |
|
@@ -147,55 +149,71 @@ public void downloadIndexAndSignature(MultiStepProgress progress, List<String> d
|
147 | 149 |
|
148 | 150 | // Extract the file name from the url
|
149 | 151 | URL packageIndexUrl = new URL(packageIndexUrlString);
|
150 |
| - URL packageIndexSignatureUrl = new URL(packageIndexUrlString + ".sig"); |
151 | 152 | String[] urlPathParts = packageIndexUrl.getFile().split("/");
|
152 | 153 | File packageIndex = BaseNoGui.indexer.getIndexFile(urlPathParts[urlPathParts.length - 1]);
|
153 |
| - // Signature file name |
154 |
| - File packageIndexSignature = BaseNoGui.indexer.getIndexFile(urlPathParts[urlPathParts.length - 1] + ".sig"); |
155 | 154 |
|
156 | 155 | final String statusText = tr("Downloading platforms index...");
|
157 | 156 | downloadedFilesAccumulator.add(packageIndex.getName());
|
158 | 157 |
|
159 | 158 | // Create temp files
|
160 | 159 | File packageIndexTemp = File.createTempFile(packageIndexUrl.getPath(), ".tmp");
|
161 |
| - File packageIndexSignatureTemp = File.createTempFile(packageIndexSignatureUrl.getPath(), ".tmp"); |
162 | 160 | try {
|
163 | 161 | // Download package index
|
164 | 162 | download(packageIndexUrl, packageIndexTemp, progress, statusText, progressListener, true);
|
165 |
| - try { |
166 |
| - // Download signature |
167 |
| - download(packageIndexSignatureUrl, packageIndexSignatureTemp, progress, statusText, progressListener, true); |
168 |
| - |
169 |
| - // Verify the signature before move the files |
170 |
| - boolean signatureVerified = signatureVerifier.isSigned(packageIndexTemp, packageIndexSignatureTemp); |
171 |
| - if (signatureVerified) { |
172 |
| - // Move if the signature is ok |
173 |
| - Files.move(packageIndexTemp.toPath(), packageIndex.toPath(), StandardCopyOption.REPLACE_EXISTING); |
174 |
| - Files.move(packageIndexSignatureTemp.toPath(), packageIndexSignature.toPath(), StandardCopyOption.REPLACE_EXISTING); |
175 |
| - downloadedFilesAccumulator.add(packageIndexSignature.getName()); |
176 |
| - } else { |
177 |
| - downloadedFilesAccumulator.remove(packageIndex.getName()); |
178 |
| - log.error("{} file signature verification failed. File ignored.", packageIndexSignatureUrl); |
179 |
| - System.err.println(format(tr("{0} file signature verification failed. File ignored."), packageIndexUrlString)); |
| 163 | + final List<String> domain = new LinkedList<>(PreferencesData.getCollection("http.signature_verify_domains")); |
| 164 | + // Default domain |
| 165 | + domain.add("downloads.arduino.cc"); |
180 | 166 |
|
181 |
| - } |
182 |
| - } catch (Exception e) { |
183 |
| - log.error("Cannot download the signature from {} the package will be install in any case", packageIndexSignatureUrl, e); |
184 |
| - if (packageIndexTemp.length() > 0) { |
| 167 | + if (domain.contains(packageIndexUrl.getHost())) { |
| 168 | + URL signatureUrl = new URL(packageIndexUrl.toString() + ".sig"); |
| 169 | + |
| 170 | + if (checkSignature(progress, downloadedFilesAccumulator, signatureUrl, progressListener, signatureVerifier, statusText, packageIndexTemp)) { |
185 | 171 | Files.move(packageIndexTemp.toPath(), packageIndex.toPath(), StandardCopyOption.REPLACE_EXISTING);
|
186 | 172 | } else {
|
187 |
| - log.error("The temporarily package index file is empty (path:{},url:{}), It cannot be move there {} ", |
188 |
| - packageIndexTemp.toPath(), packageIndexUrlString, packageIndex.toPath()); |
| 173 | + downloadedFilesAccumulator.remove(packageIndex.getName()); |
189 | 174 | }
|
| 175 | + } else { |
| 176 | + log.info("The domain is not selected to verify the signature. domain list: {}, packageIndex: {}", domain, packageIndexUrl); |
190 | 177 | }
|
191 |
| - |
192 | 178 | } catch (Exception e) {
|
193 | 179 | downloadedFilesAccumulator.remove(packageIndex.getName());
|
194 | 180 | throw e;
|
195 | 181 | } finally {
|
196 | 182 | // Delete useless temp file
|
197 | 183 | Files.deleteIfExists(packageIndexTemp.toPath());
|
| 184 | + } |
| 185 | + } |
| 186 | + |
| 187 | + public boolean checkSignature(MultiStepProgress progress, List<String> downloadedFilesAccumulator, URL signatureUrl, ProgressListener progressListener, SignatureVerifier signatureVerifier, String statusText, File fileToVerify) throws Exception { |
| 188 | + |
| 189 | + File packageIndexSignatureTemp = File.createTempFile(signatureUrl.getPath(), ".tmp"); |
| 190 | + // Signature file name |
| 191 | + String[] urlPathParts = signatureUrl.getFile().split("/"); |
| 192 | + File packageIndexSignature = BaseNoGui.indexer.getIndexFile(urlPathParts[urlPathParts.length - 1]); |
| 193 | + |
| 194 | + try { |
| 195 | + // Download signature |
| 196 | + download(signatureUrl, packageIndexSignatureTemp, progress, statusText, progressListener, true); |
| 197 | + |
| 198 | + // Verify the signature before move the files |
| 199 | + boolean signatureVerified = signatureVerifier.isSigned(fileToVerify, packageIndexSignatureTemp); |
| 200 | + if (signatureVerified) { |
| 201 | + log.info("Signature verified. url={}, signature url={}, file to verify={}, signature file={}", signatureUrl, signatureUrl, fileToVerify, packageIndexSignatureTemp); |
| 202 | + // Move if the signature is ok |
| 203 | + Files.move(packageIndexSignatureTemp.toPath(), packageIndexSignature.toPath(), StandardCopyOption.REPLACE_EXISTING); |
| 204 | + downloadedFilesAccumulator.add(packageIndexSignature.getName()); |
| 205 | + } else { |
| 206 | + log.error("{} file signature verification failed. File ignored.", signatureUrl); |
| 207 | + System.err.println(format(tr("{0} file signature verification failed. File ignored."), signatureUrl.toString())); |
| 208 | + } |
| 209 | + return signatureVerified; |
| 210 | + } catch (Exception e) { |
| 211 | + log.error("Cannot download the signature from {} the package will be discard", signatureUrl, e); |
| 212 | + throw e; |
| 213 | + } finally { |
198 | 214 | Files.deleteIfExists(packageIndexSignatureTemp.toPath());
|
199 | 215 | }
|
| 216 | + |
200 | 217 | }
|
| 218 | + |
201 | 219 | }
|
0 commit comments