0N/A/*
2362N/A * Copyright (c) 1997, 2009, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage sun.security.util;
0N/A
0N/Aimport java.security.*;
0N/Aimport java.util.HashMap;
0N/Aimport java.io.ByteArrayOutputStream;
0N/A
0N/A/**
0N/A * This class is used to compute digests on sections of the Manifest.
0N/A */
0N/Apublic class ManifestDigester {
0N/A
0N/A public static final String MF_MAIN_ATTRS = "Manifest-Main-Attributes";
0N/A
0N/A /** the raw bytes of the manifest */
0N/A private byte rawBytes[];
0N/A
0N/A /** the offset/length pair for a section */
0N/A private HashMap<String, Entry> entries; // key is a UTF-8 string
0N/A
0N/A /** state returned by findSection */
0N/A static class Position {
0N/A int endOfFirstLine; // not including newline character
0N/A
0N/A int endOfSection; // end of section, not including the blank line
0N/A // between sections
0N/A int startOfNext; // the start of the next section
0N/A }
0N/A
0N/A /**
0N/A * find a section in the manifest.
0N/A *
0N/A * @param offset should point to the starting offset with in the
0N/A * raw bytes of the next section.
0N/A *
0N/A * @pos set by
0N/A *
0N/A * @returns false if end of bytes has been reached, otherwise returns
0N/A * true
0N/A */
0N/A private boolean findSection(int offset, Position pos)
0N/A {
0N/A int i = offset, len = rawBytes.length;
0N/A int last = offset;
0N/A int next;
0N/A boolean allBlank = true;
0N/A
0N/A pos.endOfFirstLine = -1;
0N/A
0N/A while (i < len) {
0N/A byte b = rawBytes[i];
0N/A switch(b) {
0N/A case '\r':
0N/A if (pos.endOfFirstLine == -1)
0N/A pos.endOfFirstLine = i-1;
0N/A if ((i < len) && (rawBytes[i+1] == '\n'))
0N/A i++;
0N/A case '\n':
0N/A if (pos.endOfFirstLine == -1)
0N/A pos.endOfFirstLine = i-1;
0N/A if (allBlank || (i == len-1)) {
0N/A if (i == len-1)
0N/A pos.endOfSection = i;
0N/A else
0N/A pos.endOfSection = last;
0N/A pos.startOfNext = i+1;
0N/A return true;
0N/A }
0N/A else {
0N/A // start of a new line
0N/A last = i;
0N/A allBlank = true;
0N/A }
0N/A break;
0N/A default:
0N/A allBlank = false;
0N/A break;
0N/A }
0N/A i++;
0N/A }
0N/A return false;
0N/A }
0N/A
0N/A public ManifestDigester(byte bytes[])
0N/A {
0N/A rawBytes = bytes;
0N/A entries = new HashMap<String, Entry>();
0N/A
0N/A ByteArrayOutputStream baos = new ByteArrayOutputStream();
0N/A
0N/A Position pos = new Position();
0N/A
0N/A if (!findSection(0, pos))
0N/A return; // XXX: exception?
0N/A
0N/A // create an entry for main attributes
0N/A entries.put(MF_MAIN_ATTRS,
0N/A new Entry(0, pos.endOfSection + 1, pos.startOfNext, rawBytes));
0N/A
0N/A int start = pos.startOfNext;
0N/A while(findSection(start, pos)) {
0N/A int len = pos.endOfFirstLine-start+1;
0N/A int sectionLen = pos.endOfSection-start+1;
0N/A int sectionLenWithBlank = pos.startOfNext-start;
0N/A
0N/A if (len > 6) {
0N/A if (isNameAttr(bytes, start)) {
1302N/A StringBuilder nameBuf = new StringBuilder(sectionLen);
0N/A
0N/A try {
0N/A nameBuf.append(
0N/A new String(bytes, start+6, len-6, "UTF8"));
0N/A
0N/A int i = start + len;
0N/A if ((i-start) < sectionLen) {
0N/A if (bytes[i] == '\r') {
0N/A i += 2;
0N/A } else {
0N/A i += 1;
0N/A }
0N/A }
0N/A
0N/A while ((i-start) < sectionLen) {
0N/A if (bytes[i++] == ' ') {
0N/A // name is wrapped
0N/A int wrapStart = i;
0N/A while (((i-start) < sectionLen)
0N/A && (bytes[i++] != '\n'));
0N/A if (bytes[i-1] != '\n')
0N/A return; // XXX: exception?
0N/A int wrapLen;
0N/A if (bytes[i-2] == '\r')
0N/A wrapLen = i-wrapStart-2;
0N/A else
0N/A wrapLen = i-wrapStart-1;
0N/A
0N/A nameBuf.append(new String(bytes, wrapStart,
0N/A wrapLen, "UTF8"));
0N/A } else {
0N/A break;
0N/A }
0N/A }
0N/A
0N/A entries.put(nameBuf.toString(),
0N/A new Entry(start, sectionLen, sectionLenWithBlank,
0N/A rawBytes));
0N/A
0N/A } catch (java.io.UnsupportedEncodingException uee) {
0N/A throw new IllegalStateException(
0N/A "UTF8 not available on platform");
0N/A }
0N/A }
0N/A }
0N/A start = pos.startOfNext;
0N/A }
0N/A }
0N/A
0N/A private boolean isNameAttr(byte bytes[], int start)
0N/A {
0N/A return ((bytes[start] == 'N') || (bytes[start] == 'n')) &&
0N/A ((bytes[start+1] == 'a') || (bytes[start+1] == 'A')) &&
0N/A ((bytes[start+2] == 'm') || (bytes[start+2] == 'M')) &&
0N/A ((bytes[start+3] == 'e') || (bytes[start+3] == 'E')) &&
0N/A (bytes[start+4] == ':') &&
0N/A (bytes[start+5] == ' ');
0N/A }
0N/A
0N/A public static class Entry {
0N/A int offset;
0N/A int length;
0N/A int lengthWithBlankLine;
0N/A byte[] rawBytes;
0N/A boolean oldStyle;
0N/A
0N/A public Entry(int offset, int length,
0N/A int lengthWithBlankLine, byte[] rawBytes)
0N/A {
0N/A this.offset = offset;
0N/A this.length = length;
0N/A this.lengthWithBlankLine = lengthWithBlankLine;
0N/A this.rawBytes = rawBytes;
0N/A }
0N/A
0N/A public byte[] digest(MessageDigest md)
0N/A {
0N/A md.reset();
0N/A if (oldStyle) {
0N/A doOldStyle(md,rawBytes, offset, lengthWithBlankLine);
0N/A } else {
0N/A md.update(rawBytes, offset, lengthWithBlankLine);
0N/A }
0N/A return md.digest();
0N/A }
0N/A
0N/A private void doOldStyle(MessageDigest md,
0N/A byte[] bytes,
0N/A int offset,
0N/A int length)
0N/A {
0N/A // this is too gross to even document, but here goes
0N/A // the 1.1 jar verification code ignored spaces at the
0N/A // end of lines when calculating digests, so that is
0N/A // what this code does. It only gets called if we
0N/A // are parsing a 1.1 signed signature file
0N/A int i = offset;
0N/A int start = offset;
0N/A int max = offset + length;
0N/A int prev = -1;
0N/A while(i <max) {
0N/A if ((bytes[i] == '\r') && (prev == ' ')) {
0N/A md.update(bytes, start, i-start-1);
0N/A start = i;
0N/A }
0N/A prev = bytes[i];
0N/A i++;
0N/A }
0N/A md.update(bytes, start, i-start);
0N/A }
0N/A
0N/A
0N/A /** Netscape doesn't include the new line. Intel and JavaSoft do */
0N/A
0N/A public byte[] digestWorkaround(MessageDigest md)
0N/A {
0N/A md.reset();
0N/A md.update(rawBytes, offset, length);
0N/A return md.digest();
0N/A }
0N/A }
0N/A
0N/A public Entry get(String name, boolean oldStyle) {
0N/A Entry e = entries.get(name);
0N/A if (e != null)
0N/A e.oldStyle = oldStyle;
0N/A return e;
0N/A }
0N/A
0N/A public byte[] manifestDigest(MessageDigest md)
0N/A {
0N/A md.reset();
0N/A md.update(rawBytes, 0, rawBytes.length);
0N/A return md.digest();
0N/A }
0N/A
0N/A}