--- old/src/share/classes/sun/security/tools/JarSigner.java 2010-05-24 09:18:19.000000000 +0800 +++ new/src/share/classes/sun/security/tools/JarSigner.java 2010-05-24 09:18:19.000000000 +0800 @@ -1,5 +1,5 @@ /* - * Copyright 1997-2007 Sun Microsystems, Inc. All Rights Reserved. + * Copyright 1997-2010 Sun Microsystems, Inc. All Rights Reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -951,6 +951,8 @@ BASE64Encoder encoder = new JarBASE64Encoder(); Vector mfFiles = new Vector(); + boolean wasSigned = false; + for (Enumeration enum_=zipFile.entries(); enum_.hasMoreElements();) { ZipEntry ze = enum_.nextElement(); @@ -960,6 +962,11 @@ // out first mfFiles.addElement(ze); + if (SignatureFileVerifier.isBlockOrSF( + ze.getName().toUpperCase(Locale.ENGLISH))) { + wasSigned = true; + } + if (signatureRelated(ze.getName())) { // ignore signature-related and manifest files continue; @@ -987,37 +994,41 @@ if (mfModified) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); manifest.write(baos); - byte[] newBytes = baos.toByteArray(); - if (mfRawBytes != null - && oldAttr.equals(manifest.getMainAttributes())) { - - /* - * Note: - * - * The Attributes object is based on HashMap and can handle - * continuation columns. Therefore, even if the contents are - * not changed (in a Map view), the bytes that it write() - * may be different from the original bytes that it read() - * from. Since the signature on the main attributes is based - * on raw bytes, we must retain the exact bytes. - */ + if (wasSigned) { + byte[] newBytes = baos.toByteArray(); + if (mfRawBytes != null + && oldAttr.equals(manifest.getMainAttributes())) { + + /* + * Note: + * + * The Attributes object is based on HashMap and can handle + * continuation columns. Therefore, even if the contents are + * not changed (in a Map view), the bytes that it write() + * may be different from the original bytes that it read() + * from. Since the signature on the main attributes is based + * on raw bytes, we must retain the exact bytes. + */ - int newPos = findHeaderEnd(newBytes); - int oldPos = findHeaderEnd(mfRawBytes); + int newPos = findHeaderEnd(newBytes); + int oldPos = findHeaderEnd(mfRawBytes); - if (newPos == oldPos) { - System.arraycopy(mfRawBytes, 0, newBytes, 0, oldPos); - } else { - // cat oldHead newTail > newBytes - byte[] lastBytes = new byte[oldPos + - newBytes.length - newPos]; - System.arraycopy(mfRawBytes, 0, lastBytes, 0, oldPos); - System.arraycopy(newBytes, newPos, lastBytes, oldPos, - newBytes.length - newPos); - newBytes = lastBytes; + if (newPos == oldPos) { + System.arraycopy(mfRawBytes, 0, newBytes, 0, oldPos); + } else { + // cat oldHead newTail > newBytes + byte[] lastBytes = new byte[oldPos + + newBytes.length - newPos]; + System.arraycopy(mfRawBytes, 0, lastBytes, 0, oldPos); + System.arraycopy(newBytes, newPos, lastBytes, oldPos, + newBytes.length - newPos); + newBytes = lastBytes; + } } + mfRawBytes = newBytes; + } else { + mfRawBytes = baos.toByteArray(); } - mfRawBytes = newBytes; } // Write out the manifest @@ -1233,23 +1244,31 @@ } /** - * Find the position of an empty line inside bs + * Find the length of header inside bs. The header is a multiple (>=0) + * lines of attributes plus an empty line. The empty line is included + * in the header. */ private int findHeaderEnd(byte[] bs) { - // An empty line can be at the beginning... - if (bs.length > 1 && bs[0] == '\r' && bs[1] == '\n') { - return 0; - } - // ... or after another line - for (int i=0; i 1 +mkdir META-INF + +# Create a fake .RSA file so that jarsigner believes it's signed + +touch META-INF/x.RSA + +# A MANIFEST.MF using \n as newlines and no double newlines at the end + +cat > META-INF/MANIFEST.MF <