use of com.biglybt.core.security.CryptoManagerException in project BiglyBT by BiglySoftware.
the class SESTSConnectionImpl method connect.
@Override
public void connect() throws MessageException {
if (connection.isIncoming()) {
connection.connect();
} else {
try {
ByteBuffer buffer = ByteBuffer.allocate(32 * 1024);
sts_engine.getKeys(buffer);
buffer.flip();
sent_keys = true;
connection.connect(buffer);
} catch (CryptoManagerException e) {
throw (new MessageException("Failed to get initial keys", e));
}
}
}
use of com.biglybt.core.security.CryptoManagerException in project BiglyBT by BiglySoftware.
the class SESTSConnectionImpl method connect.
@Override
public void connect(GenericMessageConnection.GenericMessageConnectionPropertyHandler ph) throws MessageException {
if (connection.isIncoming()) {
connection.connect(ph);
} else {
try {
ByteBuffer buffer = ByteBuffer.allocate(32 * 1024);
sts_engine.getKeys(buffer);
buffer.flip();
sent_keys = true;
connection.connect(buffer, ph);
} catch (CryptoManagerException e) {
throw (new MessageException("Failed to get initial keys", e));
}
}
}
use of com.biglybt.core.security.CryptoManagerException in project BiglyBT by BiglySoftware.
the class CryptoSTSEngineImpl method getMessage.
public void getMessage(ByteBuffer buffer, boolean keys) throws CryptoManagerException {
try {
putInt(buffer, VERSION, 255);
SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
Signature sig = CryptoECCUtils.getSignature(myPrivateKey);
if (keys) {
final byte[] rawMyPubkey = CryptoECCUtils.keyToRawdata(myPublicKey);
final byte[] rawEphemeralPubkey = CryptoECCUtils.keyToRawdata(ephemeralKeyPair.getPublic());
sig.update(rawMyPubkey);
sig.update(rawEphemeralPubkey);
final byte[] rawSign = sig.sign();
final byte[] pad = new byte[random.nextInt(32)];
random.nextBytes(pad);
putBytes(buffer, rawMyPubkey, 65535);
putBytes(buffer, rawEphemeralPubkey, 65535);
putBytes(buffer, rawSign, 65535);
putBytes(buffer, pad, 65535);
} else {
if (sharedSecret == null) {
throw (new CryptoManagerException("phase error: keys not received"));
}
final byte[] IV = new byte[20 + random.nextInt(32)];
random.nextBytes(IV);
sig.update(IV);
sig.update(sharedSecret);
final byte[] rawSig = sig.sign();
putBytes(buffer, IV, 65535);
putBytes(buffer, rawSig, 65535);
}
} catch (CryptoManagerException e) {
throw (e);
} catch (Throwable e) {
throw (new CryptoManagerException("Failed to generate message"));
}
}
use of com.biglybt.core.security.CryptoManagerException in project BiglyBT by BiglySoftware.
the class CryptoSTSEngineImpl method getBytes.
protected byte[] getBytes(ByteBuffer buffer, int max_size) throws CryptoManagerException {
int len = getInt(buffer, max_size);
if (len > max_size) {
throw (new CryptoManagerException("Invalid length"));
}
try {
byte[] res = new byte[len];
buffer.get(res);
return (res);
} catch (Throwable e) {
throw (new CryptoManagerException("Failed to get byte[]", e));
}
}
use of com.biglybt.core.security.CryptoManagerException in project BiglyBT by BiglySoftware.
the class CryptoSTSEngineImpl method putMessage.
public void putMessage(ByteBuffer message, boolean keys) throws CryptoManagerException {
try {
int version = getInt(message, 255);
if (version != VERSION) {
throw (new CryptoManagerException("invalid version (" + version + ")"));
}
if (keys) {
if (sharedSecret != null) {
throw (new CryptoManagerException("phase error: keys already received"));
}
final byte[] rawRemoteOtherPubkey = getBytes(message, 65535);
final byte[] rawRemoteEphemeralPubkey = getBytes(message, 65535);
final byte[] remoteSig = getBytes(message, 65535);
final byte[] pad = getBytes(message, 65535);
remotePubKey = CryptoECCUtils.rawdataToPubkey(rawRemoteOtherPubkey);
Signature check = CryptoECCUtils.getSignature(remotePubKey);
check.update(rawRemoteOtherPubkey);
check.update(rawRemoteEphemeralPubkey);
if (check.verify(remoteSig)) {
ecDH.doPhase(CryptoECCUtils.rawdataToPubkey(rawRemoteEphemeralPubkey), true);
sharedSecret = ecDH.generateSecret();
} else {
throw (new CryptoManagerException("Signature check failed"));
}
} else {
if (sharedSecret == null) {
throw (new CryptoManagerException("phase error: keys not received"));
}
final byte[] IV = getBytes(message, 65535);
final byte[] remoteSig = getBytes(message, 65535);
Signature check = CryptoECCUtils.getSignature(remotePubKey);
check.update(IV);
check.update(sharedSecret);
if (!check.verify(remoteSig)) {
throw (new CryptoManagerException("Signature check failed"));
}
}
} catch (CryptoManagerException e) {
throw (e);
} catch (Throwable e) {
throw (new CryptoManagerException("Failed to generate message"));
}
}
Aggregations