use of net.i2p.data.DataFormatException in project i2p.i2p by i2p.
the class SessionConfig method getBytes.
private byte[] getBytes() {
if (_destination == null)
return null;
if (_options == null)
return null;
if (_creationDate == null)
return null;
ByteArrayOutputStream out = new ByteArrayOutputStream();
try {
// _log.debug("PubKey size for destination: " + _destination.getPublicKey().getData().length);
// _log.debug("SigningKey size for destination: " + _destination.getSigningPublicKey().getData().length);
_destination.writeBytes(out);
// UTF-8
DataHelper.writeProperties(out, _options, true);
DataHelper.writeDate(out, _creationDate);
} catch (IOException ioe) {
Log log = I2PAppContext.getGlobalContext().logManager().getLog(SessionConfig.class);
log.error("IOError signing", ioe);
return null;
} catch (DataFormatException dfe) {
Log log = I2PAppContext.getGlobalContext().logManager().getLog(SessionConfig.class);
log.error("Error writing out the bytes for signing/verification", dfe);
return null;
}
return out.toByteArray();
}
use of net.i2p.data.DataFormatException in project i2p.i2p by i2p.
the class SessionConfig method signSessionConfig.
/**
* Sign the structure using the supplied private key
*
* @param signingKey SigningPrivateKey to sign with
* @throws DataFormatException
*/
public void signSessionConfig(SigningPrivateKey signingKey) throws DataFormatException {
byte[] data = getBytes();
if (data == null)
throw new DataFormatException("Unable to retrieve bytes for signing");
if (signingKey == null)
throw new DataFormatException("No signing key");
_signature = DSAEngine.getInstance().sign(data, signingKey);
if (_signature == null)
throw new DataFormatException("Signature failed with " + signingKey.getType() + " key");
}
use of net.i2p.data.DataFormatException in project i2p.i2p by i2p.
the class RouterInfo method getBytes.
/**
* Write out the raw payload of the routerInfo, excluding the signature. This
* caches the data in memory if possible.
*
* @throws DataFormatException if the data is somehow b0rked (missing props, etc)
*/
protected byte[] getBytes() throws DataFormatException {
if (_byteified != null)
return _byteified;
ByteArrayOutputStream out = new ByteArrayOutputStream(2 * 1024);
try {
writeDataBytes(out);
} catch (IOException ioe) {
throw new DataFormatException("IO Error getting bytes", ioe);
}
byte[] data = out.toByteArray();
if (CACHE_ALL || _shouldCache)
_byteified = data;
return data;
}
use of net.i2p.data.DataFormatException in project i2p.i2p by i2p.
the class RouterPrivateKeyFile method getRouterIdentity.
/**
* Read it in from the file.
* Also sets the local privKey and signingPrivKey.
*/
public RouterIdentity getRouterIdentity() throws IOException, DataFormatException {
InputStream in = null;
try {
in = new BufferedInputStream(new FileInputStream(this.file));
RouterIdentity ri = new RouterIdentity();
ri.readBytes(in);
privKey = new PrivateKey();
privKey.readBytes(in);
SigType type = ri.getSigningPublicKey().getType();
if (type == null)
throw new DataFormatException("Unknown sig type");
signingPrivKey = new SigningPrivateKey(type);
signingPrivKey.readBytes(in);
// set it a Destination, so we may call validateKeyPairs()
// or other methods
dest = new Destination();
dest.setPublicKey(ri.getPublicKey());
dest.setSigningPublicKey(ri.getSigningPublicKey());
dest.setCertificate(ri.getCertificate());
dest.setPadding(ri.getPadding());
return ri;
} finally {
if (in != null) {
try {
in.close();
} catch (IOException ioe) {
}
}
}
}
use of net.i2p.data.DataFormatException in project i2p.i2p by i2p.
the class DatabaseStoreMessage method readMessage.
public void readMessage(byte[] data, int offset, int dataSize, int type) throws I2NPMessageException {
if (type != MESSAGE_TYPE)
throw new I2NPMessageException("Message type is incorrect for this message");
int curIndex = offset;
_key = Hash.create(data, curIndex);
// Fast-fail here to save resources.
if (_key.equals(Hash.FAKE_HASH)) {
// createRateStat in KNDF
_context.statManager().addRateData("netDb.DSMAllZeros", 1);
throw new I2NPMessageException("DSM all zeros");
}
curIndex += Hash.HASH_LENGTH;
// as of 0.9.18, ignore other 7 bits of the type byte, in preparation for future options
int dbType = data[curIndex] & 0x01;
curIndex++;
_replyToken = DataHelper.fromLong(data, curIndex, 4);
curIndex += 4;
if (_replyToken > 0) {
long tunnel = DataHelper.fromLong(data, curIndex, 4);
if (tunnel > 0)
_replyTunnel = new TunnelId(tunnel);
curIndex += 4;
_replyGateway = Hash.create(data, curIndex);
curIndex += Hash.HASH_LENGTH;
} else {
_replyTunnel = null;
_replyGateway = null;
}
if (dbType == DatabaseEntry.KEY_TYPE_LEASESET) {
_dbEntry = new LeaseSet();
try {
_dbEntry.readBytes(new ByteArrayInputStream(data, curIndex, data.length - curIndex));
} catch (DataFormatException dfe) {
throw new I2NPMessageException("Error reading the leaseSet", dfe);
} catch (IOException ioe) {
throw new I2NPMessageException("Error reading the leaseSet", ioe);
}
} else {
// dbType == DatabaseEntry.KEY_TYPE_ROUTERINFO
_dbEntry = new RouterInfo();
int compressedSize = (int) DataHelper.fromLong(data, curIndex, 2);
curIndex += 2;
if (compressedSize <= 0 || curIndex + compressedSize > data.length || curIndex + compressedSize > dataSize + offset)
throw new I2NPMessageException("Compressed RI length: " + compressedSize + " but remaining bytes: " + Math.min(data.length - curIndex, dataSize + offset - curIndex));
try {
// TODO we could delay decompression, just copy to a new byte array and store in _byteCache
// May not be necessary since the IBGW now uses UnknownI2NPMessage.
// DSMs at the OBEP are generally garlic wrapped, so the OBEP won't see it.
// If we do delay it, getEntry() will have to check if _dbEntry is null and _byteCache
// is non-null, and then decompress.
byte[] decompressed = DataHelper.decompress(data, curIndex, compressedSize);
_dbEntry.readBytes(new ByteArrayInputStream(decompressed));
} catch (DataFormatException dfe) {
throw new I2NPMessageException("Error reading the routerInfo", dfe);
} catch (IOException ioe) {
throw new I2NPMessageException("Corrupt compressed routerInfo size = " + compressedSize, ioe);
}
}
// if (!key.equals(_dbEntry.getHash()))
// throw new I2NPMessageException("Hash mismatch in DSM");
}
Aggregations