/** * Check whether a base64-encoded {dest,privkey,signingprivkey} is valid * * @param dest The base64-encoded destination and keys to be checked (same format as PrivateKeyFile) * @return true if valid */ public static boolean checkPrivateDestination(String dest) { byte[] b = Base64.decode(dest); if (b == null || b.length < 663) return false; ByteArrayInputStream destKeyStream = new ByteArrayInputStream(b); try { Destination d = Destination.create(destKeyStream); new PrivateKey().readBytes(destKeyStream); SigningPrivateKey spk = new SigningPrivateKey(d.getSigningPublicKey().getType()); spk.readBytes(destKeyStream); } catch (DataFormatException e) { return false; } catch (IOException e) { return false; } return destKeyStream.available() == 0; }
@Test public void testNullEquals(){ SigningPrivateKey signingPrivateKey = new SigningPrivateKey(); byte data[] = new byte[SigningPrivateKey.KEYSIZE_BYTES]; for (int i = 0; i < data.length; i++) data[i] = (byte)(i%56); signingPrivateKey.setData(data); assertFalse(signingPrivateKey.equals(null)); }
/** * @throws IllegalArgumentException on mismatch of spubkey and spk types * @since 0.8.9 */ public PrivateKeyFile(File file, Destination dest, PrivateKey pk, SigningPrivateKey spk) { if (dest.getSigningPublicKey().getType() != spk.getType()) throw new IllegalArgumentException("Signing key type mismatch"); this.file = file; this.client = null; this.dest = dest; this.privKey = pk; this.signingPrivKey = spk; }
public DataStructure createDataStructure() throws DataFormatException { SigningPrivateKey signingPrivateKey = new SigningPrivateKey(); byte data[] = new byte[SigningPrivateKey.KEYSIZE_BYTES]; for (int i = 0; i < data.length; i++) data[i] = (byte)(i%16); signingPrivateKey.setData(data); return signingPrivateKey; } public DataStructure createStructureToRead() { return new SigningPrivateKey(); }
/** * @since 0.9.8 */ @Override public String toString() { StringBuilder buf = new StringBuilder(64); buf.append("[SigningPrivateKey ").append(_type).append(": "); int length = length(); if (_data == null) { buf.append("null"); } else if (length <= 32) { buf.append(toBase64()); } else { buf.append("size: ").append(length); } buf.append(']'); return buf.toString(); }
private static ECPrivateKey cvtToJavaECKey(SigningPrivateKey pk) throws GeneralSecurityException { SigType type = pk.getType(); byte[] b = pk.getData(); BigInteger s = new NativeBigInteger(1, b); // see ECConstants re: casting ECPrivateKeySpec ks = new ECPrivateKeySpec(s, (ECParameterSpec) type.getParams()); KeyFactory kf = KeyFactory.getInstance("EC"); return (ECPrivateKey) kf.generatePrivate(ks); }
/** * @since 0.9.15 */ public static SigningPrivateKey fromJavaKey(EdDSAPrivateKey pk, SigType type) throws GeneralSecurityException { return new SigningPrivateKey(type, pk.getSeed()); }
/** * Side effect - zeroes out the current signing private key * @since 0.9.38 */ public void setOfflineData(long expires, SigningPublicKey transientPub, Signature sig, SigningPrivateKey transientPriv) { if (!isOffline(signingPrivKey)) { SigType type = getSigningPrivKey().getType(); byte[] dbytes = new byte[type.getPrivkeyLen()]; signingPrivKey = new SigningPrivateKey(type, dbytes); } _offlineExpiration = expires; _transientSigningPubKey = transientPub; _offlineSignature = sig; _transientSigningPrivKey = transientPriv; }
@Test public void testShortData() throws Exception{ SigningPrivateKey signingPrivateKey = new SigningPrivateKey(); 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: " + SigningPrivateKey.KEYSIZE_BYTES); signingPrivateKey.setData(data); signingPrivateKey.writeBytes(new ByteArrayOutputStream()); }
@Test public void testBase64Constructor() throws Exception{ SigningPrivateKey signingPrivateKey = new SigningPrivateKey(); byte data[] = new byte[SigningPrivateKey.KEYSIZE_BYTES]; for (int i = 0; i < data.length; i++) data[i] = (byte)(i%56); signingPrivateKey.setData(data); SigningPrivateKey key2 = new SigningPrivateKey(signingPrivateKey.toBase64()); assertEquals(signingPrivateKey, key2); }
@Test public void testNullData() throws Exception{ SigningPrivateKey signingPrivateKey = new SigningPrivateKey(); signingPrivateKey.toString(); exception.expect(DataFormatException.class); exception.expectMessage("No data to write out"); signingPrivateKey.writeBytes(new ByteArrayOutputStream()); }
signingPrivKey = new SigningPrivateKey(type); signingPrivKey.fromBase64(sspk); } catch (DataFormatException dfe) { useOldKeys = false; if (!_context.isRouterContext() && spk.getType() != SigType.DSA_SHA1 && !(leaseSet instanceof LeaseSet2)) { byte[] dummy = new byte[SigningPrivateKey.KEYSIZE_BYTES]; _context.random().nextBytes(dummy); spk = new SigningPrivateKey(dummy);
signingPrivateKey.writeBytes(fileOutputStream);
@Override protected byte[] doWriteMessage() throws I2CPMessageException, IOException { if ((_sessionId == null) || (_signingPrivateKey == null) || (_privateKey == null) || (_leaseSet == null)) throw new I2CPMessageException("Unable to write out the message as there is not enough data"); int size = 4 // sessionId + _signingPrivateKey.length() + PrivateKey.KEYSIZE_BYTES + _leaseSet.size(); ByteArrayOutputStream os = new ByteArrayOutputStream(size); try { _sessionId.writeBytes(os); _signingPrivateKey.writeBytes(os); _privateKey.writeBytes(os); _leaseSet.writeBytes(os); } catch (DataFormatException dfe) { throw new I2CPMessageException("Error writing out the message data", dfe); } return os.toByteArray(); }
/** * Only for SigType EdDSA_SHA512_Ed25519. * * @param key must be SigType EdDSA_SHA512_Ed25519 * @param h hash of secret data, same length as this key * @throws UnsupportedOperationException unless supported */ public static SigningPrivateKey unblind(SigningPrivateKey key, SimpleDataStructure h) { if (key.getType() != TYPE) throw new UnsupportedOperationException(); if (h.length() != key.length()) throw new IllegalArgumentException(); try { EdDSAPrivateKey bjk = SigUtil.toJavaEdDSAKey(key); EdDSAPrivateKey jk = EdDSABlinding.unblind(bjk, h.getData()); return SigUtil.fromJavaKey(jk, TYPE); } catch (GeneralSecurityException gse) { throw new IllegalArgumentException(gse); } } /******
/** * @param primary must be a I2PSessionMuxedImpl */ public SubSession(I2PSession primary, InputStream destKeyStream, Properties options) throws I2PSessionException { super((I2PSessionMuxedImpl)primary, destKeyStream, options); _primary = (I2PSessionMuxedImpl) primary; if (!getDecryptionKey().equals(_primary.getDecryptionKey())) throw new I2PSessionException("encryption key mismatch"); if (getPrivateKey().equals(_primary.getPrivateKey())) throw new I2PSessionException("signing key must differ"); // state management }
SigType type = priv.getType(); if (type == null) throw new IllegalArgumentException("Unknown type"); switch (type.getBaseAlgorithm()) { case DSA: BigInteger x = new NativeBigInteger(1, priv.toByteArray()); BigInteger y = CryptoConstants.dsag.modPow(x, CryptoConstants.dsap); SigningPublicKey pub = new SigningPublicKey();
/** * @since 0.9.8 */ public SigningPrivateKey(SigType type, byte data[]) { super(); _type = type; setData(data); }
/** * @param type the SigType to expect on read-in, ignored on write */ private void syncSigningKey(File keyDir, SigType type) { DataStructure ds; File keyFile = new File(keyDir, KEYFILE_PRIVATE_SIGNING); boolean exists = (_signingPrivateKey != null); if (exists) ds = _signingPrivateKey; else ds = new SigningPrivateKey(type); DataStructure readin = syncKey(keyFile, ds, exists); if (readin != null && !exists) _signingPrivateKey = (SigningPrivateKey) readin; }
SigType type = pkf.getSigningPrivKey().getType(); byte[] dbytes = new byte[type.getPrivkeyLen()]; SigningPrivateKey dummy = new SigningPrivateKey(type, dbytes); PrivateKeyFile pkf2 = new PrivateKeyFile(f3, pkf.getDestination(), pkf.getPrivKey(), dummy);