what-the-bank/sources/org/bouncycastle/crypto/tls/HeartbeatMessage.java

66 lines
2.4 KiB
Java

package org.bouncycastle.crypto.tls;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import org.bouncycastle.util.Arrays;
import org.bouncycastle.util.io.Streams;
/* loaded from: classes6.dex */
public class HeartbeatMessage {
protected int paddingLength;
protected byte[] payload;
protected short type;
/* loaded from: classes6.dex */
static class PayloadBuffer extends ByteArrayOutputStream {
byte[] toTruncatedByteArray(int i) {
if (((ByteArrayOutputStream) this).count < i + 16) {
return null;
}
return Arrays.copyOf(((ByteArrayOutputStream) this).buf, i);
}
}
public void encode(TlsContext tlsContext, OutputStream outputStream) throws IOException {
TlsUtils.writeUint8(this.type, outputStream);
TlsUtils.checkUint16(this.payload.length);
TlsUtils.writeUint16(this.payload.length, outputStream);
outputStream.write(this.payload);
byte[] bArr = new byte[this.paddingLength];
tlsContext.getNonceRandomGenerator().nextBytes(bArr);
outputStream.write(bArr);
}
public static HeartbeatMessage parse(InputStream inputStream) throws IOException {
short readUint8 = TlsUtils.readUint8(inputStream);
if (!HeartbeatMessageType.isValid(readUint8)) {
throw new TlsFatalAlert((short) 47);
}
int readUint16 = TlsUtils.readUint16(inputStream);
PayloadBuffer payloadBuffer = new PayloadBuffer();
Streams.pipeAll(inputStream, payloadBuffer);
byte[] truncatedByteArray = payloadBuffer.toTruncatedByteArray(readUint16);
if (truncatedByteArray == null) {
return null;
}
return new HeartbeatMessage(readUint8, truncatedByteArray, payloadBuffer.size() - truncatedByteArray.length);
}
public HeartbeatMessage(short s, byte[] bArr, int i) {
if (!HeartbeatMessageType.isValid(s)) {
throw new IllegalArgumentException("'type' is not a valid HeartbeatMessageType value");
}
if (bArr == null || bArr.length >= 65536) {
throw new IllegalArgumentException("'payload' must have length < 2^16");
}
if (i < 16) {
throw new IllegalArgumentException("'paddingLength' must be at least 16");
}
this.type = s;
this.payload = bArr;
this.paddingLength = i;
}
}