1 11 package org.eclipse.pde.internal.build.jarprocessor; 12 import java.io.*; 13 import java.util.Enumeration ; 14 import java.util.jar.JarFile ; 15 import java.util.jar.Manifest ; 16 import java.util.zip.*; 17 18 21 public class Unsigner { 22 private static final String META_INF = "META-INF"; private static final String DSA_EXT = ".DSA"; private static final String RSA_EXT = ".RSA"; private static final String SF_EXT = ".SF"; private static final String META_INF_PREFIX = META_INF + '/'; 27 28 private String [] signers; 29 private File jarFile; 30 private boolean keepManifestContent = false; 31 32 private boolean isSigned(File file) { 33 ZipFile jar = null; 34 try { 35 jar = new ZipFile(file); 36 37 if (signers != null) { 38 for (int i = 0; i < signers.length; i++) { 39 if (jar.getEntry((META_INF_PREFIX + signers[i] + SF_EXT).toUpperCase()) != null) 40 return true; 41 } 42 } else { 43 Enumeration entries = jar.entries(); 44 while (entries.hasMoreElements()) { 45 ZipEntry entry = (ZipEntry) entries.nextElement(); 46 String entryName = entry.getName(); 47 if(entryName.endsWith(SF_EXT) && entryName.startsWith(META_INF)) 48 return true; 49 } 50 } 51 return false; 52 } catch (ZipException e) { 53 return false; 54 } catch (IOException e) { 55 return false; 56 } finally { 57 if (jar != null) 58 try { 59 jar.close(); 60 } catch (IOException e) { 61 } 63 } 64 } 65 66 public void execute() { 67 processJar(jarFile); 68 } 69 70 private void processJar(File inputFile) { 71 if (!isSigned(inputFile)) 72 return; 73 74 try { 75 ZipInputStream input = new ZipInputStream(new BufferedInputStream(new FileInputStream(inputFile))); 76 File outputFile = File.createTempFile("removing.", ".signature", inputFile.getParentFile()); ZipOutputStream output = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(outputFile))); 78 79 ZipEntry inputZe = input.getNextEntry(); 80 byte[] b = new byte[8192]; 81 while (inputZe != null) { 82 byte remove = shouldRemove(inputZe); 83 if (remove == 2) { 84 inputZe = input.getNextEntry(); 85 continue; 86 } 87 88 if (remove == 1) { 90 output.putNextEntry(new ZipEntry(inputZe.getName())); 91 Manifest m = new Manifest (); 92 m.read(input); 93 m.getEntries().clear(); m.write(output); 95 } else { 96 output.putNextEntry(inputZe); 97 while (input.available() != 0) { 98 int read = input.read(b); 99 if (read != -1) 100 output.write(b, 0, read); 101 } 102 } 103 output.closeEntry(); 104 input.closeEntry(); 105 106 inputZe = input.getNextEntry(); 107 } 108 output.close(); 109 input.close(); 110 inputFile.delete(); 111 outputFile.renameTo(inputFile); 112 } catch (FileNotFoundException e) { 113 } catch (IOException e) { 115 e.printStackTrace(); 116 } 117 } 118 119 private byte shouldRemove(ZipEntry entry) { 120 String entryName = entry.getName(); 121 if(keepManifestContent == false && entryName.equalsIgnoreCase(JarFile.MANIFEST_NAME)) { 122 return 1; 123 } 124 if (signers != null) { 125 for (int i = 0; i < signers.length; i++) { 126 if (entryName.equalsIgnoreCase(META_INF_PREFIX + signers[i] + SF_EXT) || entryName.equalsIgnoreCase(META_INF_PREFIX + signers[i] + RSA_EXT) || entryName.equalsIgnoreCase(META_INF_PREFIX + signers[i] + DSA_EXT)) 127 return 2; 128 } 129 } else { 130 if (entryName.startsWith(META_INF) && (entryName.endsWith(SF_EXT) || entryName.endsWith(RSA_EXT) || entryName.endsWith(DSA_EXT))) 131 return 2; 132 } 133 return 0; 134 } 135 136 public void setRemoveSigners(String [] fileName) { 137 signers = fileName; 138 } 139 140 public void setJar(File file) { 141 jarFile = file; 142 } 143 144 public void setKeepManifestEntries(boolean keep) { 145 keepManifestContent = keep; 146 } 147 } 148 | Popular Tags |