/** 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); }
signingPublicKey.writeBytes(fileOutputStream); fileOutputStream.close(); fileOutputStream = null;
/** * Generate a Signature to pass to setOfflineSignature() * * @param expires absolute ms * @param transientSPK the key that will sign the leaseset * @param priv the private signing key for the destination * @return null on error */ public static Signature offlineSign(long expires, SigningPublicKey transientSPK, SigningPrivateKey priv) { ByteArrayOutputStream baos = new ByteArrayOutputStream(128); try { DataHelper.writeLong(baos, 4, expires / 1000); DataHelper.writeLong(baos, 2, transientSPK.getType().getCode()); transientSPK.writeBytes(baos); } catch (IOException ioe) { return null; } catch (DataFormatException dfe) { return null; } byte[] data = baos.toByteArray(); I2PAppContext ctx = I2PAppContext.getGlobalContext(); return ctx.dsa().sign(data, priv); }
protected void writeOfflineBytes(OutputStream out) throws DataFormatException, IOException { if (_transientSigningPublicKey == null || _offlineSignature == null) throw new DataFormatException("No offline key/sig"); DataHelper.writeLong(out, 4, _transientExpires / 1000); DataHelper.writeLong(out, 2, _transientSigningPublicKey.getType().getCode()); _transientSigningPublicKey.writeBytes(out); _offlineSignature.writeBytes(out); }
/** * Overridden because we have a blinded key, not a dest */ @Override protected void writeOfflineBytes(OutputStream out) throws DataFormatException, IOException { if (_transientSigningPublicKey == null || _offlineSignature == null) throw new DataFormatException("No offline key/sig"); DataHelper.writeLong(out, 4, _transientExpires / 1000); DataHelper.writeLong(out, 2, _signingKey.getType().getCode()); _transientSigningPublicKey.writeBytes(out); _offlineSignature.writeBytes(out); }
protected boolean verifyOfflineSignature(SigningPublicKey spk) { if (!isOffline()) return false; I2PAppContext ctx = I2PAppContext.getGlobalContext(); if (_transientExpires < ctx.clock().now()) return false; ByteArrayOutputStream baos = new ByteArrayOutputStream(128); try { DataHelper.writeLong(baos, 4, _transientExpires / 1000); DataHelper.writeLong(baos, 2, _transientSigningPublicKey.getType().getCode()); _transientSigningPublicKey.writeBytes(baos); } catch (IOException ioe) { return false; } catch (DataFormatException dfe) { return false; } byte[] data = baos.toByteArray(); return ctx.dsa().verifySignature(_offlineSignature, data, 0, data.length, getSigningPublicKey()); }
@Test public void testNullData() throws Exception{ SigningPublicKey publicKey = new SigningPublicKey(); publicKey.toString(); exception.expect(DataFormatException.class); exception.expectMessage("No data to write out"); publicKey.writeBytes(new ByteArrayOutputStream()); }
@Test public void testShortData() throws Exception{ SigningPublicKey publicKey = new SigningPublicKey(); byte data[] = new byte[56]; for (int i = 0; i < data.length; i++) data[i] = (byte)(i); exception.expect(IllegalArgumentException.class); exception.expectMessage("Bad data length: 56; required: " + SigningPublicKey.KEYSIZE_BYTES); publicKey.setData(data); publicKey.writeBytes(new ByteArrayOutputStream()); }
/** * 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) {} } } }
/** * Overridden because we have a blinded key, not a dest */ @Override protected void writeHeader(OutputStream out) throws DataFormatException, IOException { DataHelper.writeLong(out, 2, _signingKey.getType().getCode()); _signingKey.writeBytes(out); if (_published <= 0) _published = Clock.getInstance().now(); DataHelper.writeLong(out, 4, _published / 1000); DataHelper.writeLong(out, 2, (_expires - _published) / 1000); DataHelper.writeLong(out, 2, _flags); if (isOffline()) writeOfflineBytes(out); }
DataHelper.writeLong(baos, 4, expires / 1000); DataHelper.writeLong(baos, 2, spk.getType().getCode()); spk.writeBytes(baos); } catch (IOException ioe) { return false;