return writer.toByteArray();
}
public static HandshakeMessage fromByteArray(byte[] byteArray) throws HandshakeException {
DatagramReader reader = new DatagramReader(byteArray);
int curveType = reader.read(CURVE_TYPE_BITS);
switch (curveType) {
// TODO right now only named curve supported
case EXPLICIT_PRIME:
case EXPLICIT_CHAR2:
AlertMessage alert = new AlertMessage(AlertLevel.FATAL, AlertDescription.HANDSHAKE_FAILURE);
throw new HandshakeException("Not supported curve type in ServerKeyExchange message", alert);
case NAMED_CURVE:
int curveId = reader.read(NAMED_CURVE_BITS);
int length = reader.read(PUBLIC_LENGTH_BITS);
byte[] pointEncoded = reader.readBytes(length);
byte[] bytesLeft = reader.readBytesLeft();
// default is SHA256withECDSA
SignatureAndHashAlgorithm signAndHash = new SignatureAndHashAlgorithm(HashAlgorithm.SHA256, SignatureAlgorithm.ECDSA);
byte[] signatureEncoded = null;
if (bytesLeft.length > 0) {
reader = new DatagramReader(bytesLeft);
int hashAlgorithm = reader.read(HASH_ALGORITHM_BITS);
int signatureAlgorithm = reader.read(SIGNATURE_ALGORITHM_BITS);
signAndHash = new SignatureAndHashAlgorithm(hashAlgorithm, signatureAlgorithm);
length = reader.read(SIGNATURE_LENGTH_BITS);
signatureEncoded = reader.readBytes(length);
}
return new ECDHServerKeyExchange(signAndHash, curveId, pointEncoded, signatureEncoded);
default: