1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243
|
/*******************************************************************************
* Copyright (c) 2006, 2010 IBM Corporation and others. All rights reserved.
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v1.0 which accompanies this distribution,
* and is available at http://www.eclipse.org/legal/epl-v10.html
*
* Contributors: IBM Corporation - initial API and implementation
******************************************************************************/
package org.eclipse.osgi.internal.signedcontent;
import java.io.*;
import java.security.cert.*;
import java.util.*;
import org.eclipse.osgi.baseadaptor.BaseData;
import org.eclipse.osgi.baseadaptor.hooks.StorageHook;
import org.eclipse.osgi.framework.util.KeyedElement;
import org.eclipse.osgi.signedcontent.SignedContent;
import org.eclipse.osgi.signedcontent.SignerInfo;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleException;
public class SignedStorageHook implements StorageHook {
public static final String KEY = SignedStorageHook.class.getName();
public static final int HASHCODE = KEY.hashCode();
private static final int STORAGE_VERSION = 3;
private static List<SignerInfo> savedSignerInfo = new ArrayList<SignerInfo>(5);
private static long firstIDSaved = -1;
private static long lastIDSaved = -1;
private static List<SignerInfo> loadedSignerInfo = new ArrayList<SignerInfo>(5);
private static long lastIDLoaded;
private BaseData bundledata;
SignedContentImpl signedContent;
public int getStorageVersion() {
return STORAGE_VERSION;
}
/**
* @throws BundleException
*/
public StorageHook create(BaseData data) throws BundleException {
SignedStorageHook hook = new SignedStorageHook();
hook.bundledata = data;
return hook;
}
/**
* @throws BundleException
*/
public void initialize(Dictionary<String, String> manifest) throws BundleException {
// do nothing
}
public StorageHook load(BaseData target, DataInputStream is) throws IOException {
if (lastIDLoaded > target.getBundleID())
loadedSignerInfo.clear();
lastIDLoaded = target.getBundleID();
SignedStorageHook hook = new SignedStorageHook();
hook.bundledata = target;
boolean signed = is.readBoolean();
if (!signed)
return hook;
int numSigners = is.readInt();
SignerInfo[] signerInfos = new SignerInfo[numSigners];
for (int i = 0; i < numSigners; i++)
signerInfos[i] = readSignerInfo(is);
int resultsSize = is.readInt();
Map<String, Object> contentMDResults = null;
if (resultsSize > 0) {
contentMDResults = new HashMap<String, Object>(resultsSize);
for (int i = 0; i < resultsSize; i++) {
String path = is.readUTF();
int numEntrySigners = is.readInt();
SignerInfo[] entrySigners = new SignerInfo[numEntrySigners];
byte[][] entryResults = new byte[numEntrySigners][];
for (int j = 0; j < numEntrySigners; j++) {
entrySigners[j] = readSignerInfo(is);
int resultSize = is.readInt();
entryResults[j] = new byte[resultSize];
is.readFully(entryResults[j]);
}
contentMDResults.put(path, new Object[] {entrySigners, entryResults});
}
}
SignedContentImpl result = new SignedContentImpl(signerInfos, contentMDResults);
for (int i = 0; i < numSigners; i++) {
boolean hasTSA = is.readBoolean();
if (!hasTSA)
continue;
SignerInfo tsaSigner = readSignerInfo(is);
Date signingDate = new Date(is.readLong());
result.addTSASignerInfo(signerInfos[i], tsaSigner, signingDate);
}
hook.signedContent = result;
return hook;
}
public void save(DataOutputStream os) throws IOException {
getFirstLastID();
if (firstIDSaved == bundledata.getBundleID())
savedSignerInfo.clear();
if (lastIDSaved == bundledata.getBundleID())
firstIDSaved = lastIDSaved = -1;
os.writeBoolean(signedContent != null);
if (signedContent == null)
return;
SignerInfo[] signerInfos = signedContent.getSignerInfos();
os.writeInt(signerInfos.length);
for (int i = 0; i < signerInfos.length; i++)
saveSignerInfo(signerInfos[i], os);
// keyed by entry path -> {SignerInfo[] infos, byte[][] results)}
Map<String, Object> contentMDResults = signedContent.getContentMDResults();
os.writeInt(contentMDResults == null ? -1 : contentMDResults.size());
if (contentMDResults != null)
for (Map.Entry<String, Object> entry : contentMDResults.entrySet()) {
String path = entry.getKey();
os.writeUTF(path);
Object[] signerResults = (Object[]) entry.getValue();
SignerInfo[] entrySigners = (SignerInfo[]) signerResults[0];
byte[][] entryResults = (byte[][]) signerResults[1];
os.writeInt(entrySigners.length);
for (int i = 0; i < entrySigners.length; i++) {
saveSignerInfo(entrySigners[i], os);
os.writeInt(entryResults[i].length);
os.write(entryResults[i]);
}
}
for (int i = 0; i < signerInfos.length; i++) {
SignerInfo tsaInfo = signedContent.getTSASignerInfo(signerInfos[i]);
os.writeBoolean(tsaInfo != null);
if (tsaInfo == null)
continue;
saveSignerInfo(tsaInfo, os);
Date signingTime = signedContent.getSigningTime(signerInfos[i]);
os.writeLong(signingTime != null ? signingTime.getTime() : Long.MIN_VALUE);
}
}
private void saveSignerInfo(SignerInfo signerInfo, DataOutputStream os) throws IOException {
int cacheIdx = savedSignerInfo.indexOf(signerInfo);
os.writeInt(cacheIdx);
if (cacheIdx >= 0)
return;
Certificate[] certs = signerInfo.getCertificateChain();
int anchorIndex = -1;
os.writeInt(certs == null ? 0 : certs.length);
if (certs != null)
for (int i = 0; i < certs.length; i++) {
if (certs[i].equals(signerInfo.getTrustAnchor()))
anchorIndex = i;
byte[] certBytes;
try {
certBytes = certs[i].getEncoded();
} catch (CertificateEncodingException e) {
throw (IOException) new IOException(e.getMessage()).initCause(e);
}
os.writeInt(certBytes.length);
os.write(certBytes);
}
os.writeInt(anchorIndex);
os.writeUTF(signerInfo.getMessageDigestAlgorithm());
savedSignerInfo.add(signerInfo);
}
private SignerInfo readSignerInfo(DataInputStream is) throws IOException {
int index = is.readInt();
if (index >= 0)
return loadedSignerInfo.get(index);
int numCerts = is.readInt();
Certificate[] certs = new Certificate[numCerts];
for (int i = 0; i < numCerts; i++) {
int certSize = is.readInt();
byte[] certBytes = new byte[certSize];
is.readFully(certBytes);
try {
certs[i] = PKCS7Processor.certFact.generateCertificate(new ByteArrayInputStream(certBytes));
} catch (CertificateException e) {
throw (IOException) new IOException(e.getMessage()).initCause(e);
}
}
int anchorIdx = is.readInt();
SignerInfoImpl result = new SignerInfoImpl(certs, anchorIdx >= 0 ? certs[anchorIdx] : null, is.readUTF());
loadedSignerInfo.add(result);
return result;
}
private void getFirstLastID() {
if (firstIDSaved >= 0)
return;
Bundle[] bundles = bundledata.getAdaptor().getContext().getBundles();
if (bundles.length > 1) {
firstIDSaved = bundles[1].getBundleId();
lastIDSaved = bundles[bundles.length - 1].getBundleId();
}
}
public void copy(StorageHook storageHook) {
// do nothing
}
public void validate() throws IllegalArgumentException {
// do nothing
}
/**
* @throws BundleException
*/
public Dictionary<String, String> getManifest(boolean firstLoad) throws BundleException {
// do nothing
return null;
}
public boolean forgetStatusChange(int status) {
// do nothing
return false;
}
public boolean forgetStartLevelChange(int startlevel) {
// do nothing
return false;
}
public int getKeyHashCode() {
return HASHCODE;
}
public boolean compare(KeyedElement other) {
return other.getKey() == KEY;
}
public Object getKey() {
return KEY;
}
public SignedContent getSignedContent() {
return signedContent;
}
}
|