java - Convert modified manifest files back to JarEntry -
at work use lot of third-party libraries come signed. of them no longer maintained, still need use them. due recent java security update manifest files in jars need updated include security attributes. given how jar files work need perform unpack-edit-repack procedure on each jar.
this involves unpacking jar, adding attributes manifest while removing present sha1 signature strings, repacking , resigning jar our own signature. here's code have far unpack jar:
public static void unpackjarfile(file srcjarfile) throws ioexception{ file tmpjarfile = file.createtempfile("tempjar", ".tmp"); jarfile jarfile = new jarfile(srcjarfile); jaroutputstream tempjaroutputstream = new jaroutputstream(new fileoutputstream(tmpjarfile)); //copy original jar file temporary one. enumeration<jarentry> jarentries = jarfile.entries(); while(jarentries.hasmoreelements()) { jarentry temp = jarentries.nextelement(); //if file manifest unsign else carry on jarentry entry = ismanifestfile(temp) ? unsignmanifest(temp, jarfile) : temp; // ingore files ending in .sf , .rsa if (fileisnotsignature(entry)){ inputstream entryinputstream = jarfile.getinputstream(entry); tempjaroutputstream.putnextentry(entry); byte[] buffer = new byte[1024]; int bytesread = 0; while ((bytesread = entryinputstream.read(buffer)) != -1) { tempjaroutputstream.write(buffer, 0, bytesread); } } } tempjaroutputstream.close(); jarfile.close(); srcjarfile.delete(); tmpjarfile.renameto(srcjarfile); } private static jarentry unsignmanifest(jarentry signed, jarfile jarfile) throws ioexception { //extract signed manifest contents temp text file file tmpmanifestfile = file.createtempfile("tempmanifest", ".tmp"); tmpmanifestfile.deleteonexit(); manifest manifest = jarfile.getmanifest(); outputstream fos = new fileoutputstream(tmpmanifestfile); //write out manifest contents actual file on disk later editing manifest.write(fos); fos.close(); //read physical file in line line sha1 strings //can ignored when creating new manifest file. path path = paths.get(tmpmanifestfile.getpath()); list<string> lines = files.readalllines(path, charset.defaultcharset()); // logic here. // each line : lines // if !line.contains("sha1-digest: swda1ic/t+le1n+uvrayf89ly4e=") // write out new manifest file // append security attributes //convert new manifest file new jarentry unsigned // return jarentry return unsigned; }
given one-way nature of java's jarentry , manifest classes looks have heavy lifting. i'm okay getting values out, selectively putting them in painful in opinion. far, can unpack , repack jars fine. can see @ end of code can't figure out how selectively put contents manifest file , convert jarentry unpackjarfile() method thinks newly-created manifest file came original jar.
thanks in advance.
Comments
Post a Comment