/** * Write out the destination to the stream * Deprecated - only used by CLI * * @param d Destination to write * @param o stream to write the destination to, or null for noop * @param l logger to send messages to */ private static void writePubKey(Destination d, OutputStream o, Logging l) throws I2PException, IOException { if (o == null) return; d.writeBytes(o); l.log("Public key saved."); }
/** * A format error on the properties is non-fatal (only the properties are lost) * A format error on the destination is fatal */ public byte[] getBytes(DestEntry de) { ByteArrayOutputStream baos = new ByteArrayOutputStream(1024); try { try { DataHelper.writeProperties(baos, de.props, true, false); // UTF-8, unsorted } catch (DataFormatException dfe) { logError("DB Write Fail - properties too big?", dfe); // null properties is a two-byte length of 0. baos.write(new byte[2]); } de.dest.writeBytes(baos); } catch (IOException ioe) { logError("DB Write Fail", ioe); } catch (DataFormatException dfe) { logError("DB Write Fail", dfe); } return baos.toByteArray(); }
public byte[] getBytes(DestEntry de) { ByteArrayOutputStream baos = new ByteArrayOutputStream(1024); int sz = de.destList != null ? de.destList.size() : 1; try { baos.write((byte) sz); for (int i = 0; i < sz; i++) { Properties p; Destination d; if (i == 0) { p = de.props; d = de.dest; } else { p = de.propsList.get(i); d = de.destList.get(i); } try { writeProperties(baos, p); } catch (DataFormatException dfe) { logError("DB Write Fail - properties too big?", dfe); baos.write(new byte[2]); } d.writeBytes(baos); } } catch (IOException ioe) { logError("DB Write Fail", ioe); } catch (DataFormatException dfe) { logError("DB Write Fail", dfe); } return baos.toByteArray(); }
protected byte[] doWriteMessage() throws I2CPMessageException, IOException { if (_dest == null) { if (_hash == null) return new byte[0]; // null response allowed return _hash.getData(); } ByteArrayOutputStream os = new ByteArrayOutputStream(_dest.size()); try { _dest.writeBytes(os); } catch (DataFormatException dfe) { throw new I2CPMessageException("Error writing out the dest", dfe); } return os.toByteArray(); }
/** * Generate a random destination key. * Caller must close streams. Fails silently. * * @param priv Stream used to write the destination and private keys * @param pub Stream used to write the destination (may be null) * @param sigType what signature type * @since 0.9.14 */ public static void genRandomKey(OutputStream priv, OutputStream pub, SigType sigType) { //_log.debug("Generating random keys..."); try { I2PClient c = I2PClientFactory.createClient(); Destination d = c.createDestination(priv, sigType); priv.flush(); if (pub != null) { d.writeBytes(pub); pub.flush(); } } catch (I2PException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }
/** without sig! */ protected byte[] getBytes() { if (_byteified != null) return _byteified; if ((_destination == null) || (_encryptionKey == null) || (_signingKey == null)) return null; int len = size(); ByteArrayOutputStream out = new ByteArrayOutputStream(len); try { _destination.writeBytes(out); _encryptionKey.writeBytes(out); _signingKey.writeBytes(out); out.write((byte) _leases.size()); for (Lease lease : _leases) { lease.writeBytes(out); } } catch (IOException ioe) { return null; } catch (DataFormatException dfe) { return null; } byte rv[] = out.toByteArray(); // if we are floodfill and this was published to us if (_receivedAsPublished) _byteified = rv; return rv; }
/** * This does NOT validate the signature */ public void writeBytes(OutputStream out) throws DataFormatException, IOException { if ((_destination == null) || (_encryptionKey == null) || (_signingKey == null) || (_signature == null)) throw new DataFormatException("Not enough data to write out a LeaseSet"); _destination.writeBytes(out); _encryptionKey.writeBytes(out); _signingKey.writeBytes(out); out.write((byte) _leases.size()); for (Lease lease : _leases) { lease.writeBytes(out); } _signature.writeBytes(out); }
public void writeBytes(OutputStream out) throws DataFormatException, IOException { if ((_destination == null) || (_options == null) || (_signature == null) || (_creationDate == null)) throw new DataFormatException("Not enough data to create the session config"); _destination.writeBytes(out); DataHelper.writeProperties(out, _options, true); // UTF-8 DataHelper.writeDate(out, _creationDate); _signature.writeBytes(out); }
protected byte[] doWriteMessage() throws I2CPMessageException, IOException { int len = 7; if (_code == RESULT_SUCCESS) { if (_dest == null) throw new I2CPMessageException("Unable to write out the message as there is not enough data"); len += _dest.size(); } ByteArrayOutputStream os = new ByteArrayOutputStream(len); try { _sessionId.writeBytes(os); DataHelper.writeLong(os, 4, _reqID); DataHelper.writeLong(os, 1, _code); if (_code == RESULT_SUCCESS) _dest.writeBytes(os); } catch (DataFormatException dfe) { throw new I2CPMessageException("bad data", dfe); } return os.toByteArray(); }
protected void writeHeader(OutputStream out) throws DataFormatException, IOException { _destination.writeBytes(out); if (_published <= 0) _published = Clock.getInstance().now(); long pub1k = _published / 1000; DataHelper.writeLong(out, 4, pub1k); // Divide separately to prevent rounding errors DataHelper.writeLong(out, 2, ((_expires / 1000) - pub1k)); DataHelper.writeLong(out, 2, _flags); if (isOffline()) writeOfflineBytes(out); }
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); DataHelper.writeProperties(out, _options, true); // UTF-8 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(); }
/** * Copied from I2PClientImpl.createDestination() */ public void write() throws IOException, DataFormatException { OutputStream out = null; try { out = new SecureFileOutputStream(this.file); this.dest.writeBytes(out); this.privKey.writeBytes(out); this.signingPrivKey.writeBytes(out); if (isOffline()) { DataHelper.writeLong(out, 4, _offlineExpiration / 1000); DataHelper.writeLong(out, 2, _transientSigningPubKey.getType().getCode()); _transientSigningPubKey.writeBytes(out); _offlineSignature.writeBytes(out); _transientSigningPrivKey.writeBytes(out); } } finally { if (out != null) { try { out.close(); } catch (IOException ioe) {} } } }
/** * Write out the full message to the stream, including the 4 byte size and 1 * byte type header. Override the parent so we can be more mem efficient * * @throws IOException */ @Override public void writeMessage(OutputStream out) throws I2CPMessageException, IOException { if (_sessionId == null) throw new I2CPMessageException("No session ID"); if (_destination == null) throw new I2CPMessageException("No dest"); if (_payload == null) throw new I2CPMessageException("No payload"); if (_nonce < 0) throw new I2CPMessageException("No nonce"); int len = 2 + _destination.size() + _payload.getSize() + 4 + 4; try { DataHelper.writeLong(out, 4, len); out.write((byte) getType()); _sessionId.writeBytes(out); _destination.writeBytes(out); _payload.writeBytes(out); DataHelper.writeLong(out, 4, _nonce); } catch (DataFormatException dfe) { throw new I2CPMessageException("Error writing the msg", dfe); } }
out.write((byte) MESSAGE_TYPE); _sessionId.writeBytes(out); _destination.writeBytes(out); _payload.writeBytes(out); DataHelper.writeLong(out, 4, _nonce);
d.writeBytes(dout); dsaEng.sign(Hash.FAKE_HASH.toByteArray(), session.getPrivateKey()).writeBytes(dout); dout.write(DataHelper.getASCII("blah"));
d.writeBytes(out); priv.writeBytes(out); signingPrivKey.writeBytes(out);
cur += _optionFrom.writeBytes(buffer, cur);
d.writeBytes(destKeyStream); privateKey.writeBytes(destKeyStream); signingPrivKey.writeBytes(destKeyStream);
cur += _optionFrom.writeBytes(buffer, cur);