blob: e09005b0f25c1660ab88e983cb12597cf1a69131 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
|
package org.spongycastle.crypto.tls;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import org.spongycastle.util.Arrays;
import org.spongycastle.util.io.Streams;
public class HeartbeatMessage
{
protected short type;
protected byte[] payload;
protected int paddingLength;
public HeartbeatMessage(short type, byte[] payload, int paddingLength)
{
if (!HeartbeatMessageType.isValid(type))
{
throw new IllegalArgumentException("'type' is not a valid HeartbeatMessageType value");
}
if (payload == null || payload.length >= (1 << 16))
{
throw new IllegalArgumentException("'payload' must have length < 2^16");
}
if (paddingLength < 16)
{
throw new IllegalArgumentException("'paddingLength' must be at least 16");
}
this.type = type;
this.payload = payload;
this.paddingLength = paddingLength;
}
/**
* Encode this {@link HeartbeatMessage} to an {@link OutputStream}.
*
* @param output
* the {@link OutputStream} to encode to.
* @throws IOException
*/
public void encode(TlsContext context, OutputStream output) throws IOException
{
TlsUtils.writeUint8(type, output);
TlsUtils.checkUint16(payload.length);
TlsUtils.writeUint16(payload.length, output);
output.write(payload);
byte[] padding = new byte[paddingLength];
context.getNonceRandomGenerator().nextBytes(padding);
output.write(padding);
}
/**
* Parse a {@link HeartbeatMessage} from an {@link InputStream}.
*
* @param input
* the {@link InputStream} to parse from.
* @return a {@link HeartbeatMessage} object.
* @throws IOException
*/
public static HeartbeatMessage parse(InputStream input) throws IOException
{
short type = TlsUtils.readUint8(input);
if (!HeartbeatMessageType.isValid(type))
{
throw new TlsFatalAlert(AlertDescription.illegal_parameter);
}
int payload_length = TlsUtils.readUint16(input);
PayloadBuffer buf = new PayloadBuffer();
Streams.pipeAll(input, buf);
byte[] payload = buf.toTruncatedByteArray(payload_length);
if (payload == null)
{
/*
* RFC 6520 4. If the payload_length of a received HeartbeatMessage is too large, the
* received HeartbeatMessage MUST be discarded silently.
*/
return null;
}
int padding_length = buf.size() - payload.length;
/*
* RFC 6520 4. The padding of a received HeartbeatMessage message MUST be ignored
*/
return new HeartbeatMessage(type, payload, padding_length);
}
static class PayloadBuffer extends ByteArrayOutputStream
{
byte[] toTruncatedByteArray(int payloadLength)
{
/*
* RFC 6520 4. The padding_length MUST be at least 16.
*/
int minimumCount = payloadLength + 16;
if (count < minimumCount)
{
return null;
}
return Arrays.copyOf(buf, payloadLength);
}
}
}
|