public static HandshakeMessage fromByteArray(byte[] byteArray, InetSocketAddress peerAddress) { DatagramReader reader = new DatagramReader(byteArray); int length = reader.read(LENGTH_BITS); byte[] pointEncoded = reader.readBytes(length); return new ECDHClientKeyExchange(pointEncoded, peerAddress); }
private static byte[] readHostName(final DatagramReader reader, final InetSocketAddress peerAddress) throws HandshakeException { if (reader.bitsLeft() >= LENGTH_BITS) { int length = reader.read(LENGTH_BITS); if (reader.bytesAvailable(length)) { return reader.readBytes(length); } } throw newDecodeError(peerAddress); }
private static ECDHServerKeyExchange readNamedCurve(final DatagramReader reader, final InetSocketAddress peerAddress) throws HandshakeException { 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(SignatureAndHashAlgorithm.HashAlgorithm.SHA256, SignatureAndHashAlgorithm.SignatureAlgorithm.ECDSA); byte[] signatureEncoded = null; if (bytesLeft.length > 0) { DatagramReader remainder = new DatagramReader(bytesLeft); int hashAlgorithm = remainder.read(HASH_ALGORITHM_BITS); int signatureAlgorithm = remainder.read(SIGNATURE_ALGORITHM_BITS); signAndHash = new SignatureAndHashAlgorithm(hashAlgorithm, signatureAlgorithm); length = remainder.read(SIGNATURE_LENGTH_BITS); signatureEncoded = remainder.readBytes(length); } return new ECDHServerKeyExchange(signAndHash, curveId, pointEncoded, signatureEncoded, peerAddress); }
public static HelloExtension fromExtensionData(byte[] extensionData) { DatagramReader reader = new DatagramReader(extensionData); int listLength = reader.read(LIST_LENGTH_BITS); List<Integer> groupIds = new ArrayList<Integer>(); while (listLength > 0) { int id = reader.read(CURVE_BITS); groupIds.add(id); listLength -= 2; } return new SupportedEllipticCurvesExtension(groupIds); }
DatagramReader reader = new DatagramReader(byteArray); while (reader.bytesAvailable()) { if (reader.bitsLeft() < RECORD_HEADER_BITS) { LOGGER.log(Level.FINE, "Received truncated DTLS record(s). Discarding ..."); return records; int type = reader.read(CONTENT_TYPE_BITS); int major = reader.read(VERSION_BITS); int minor = reader.read(VERSION_BITS); ProtocolVersion version = new ProtocolVersion(major, minor); int epoch = reader.read(EPOCH_BITS); long sequenceNumber = reader.readLong(SEQUENCE_NUMBER_BITS); int length = reader.read(LENGTH_BITS); if (reader.bitsLeft() < length) { LOGGER.log(Level.FINE, "Received truncated DTLS record(s). Discarding ..."); return records; byte[] fragmentBytes = reader.readBytes(length);
DatagramReader reader = new DatagramReader(byteArray); byte[] explicitNonceUsed = reader.readBytes(8); if (!Arrays.equals(explicitNonce, explicitNonceUsed) && LOGGER.isLoggable(Level.FINE)) { StringBuilder b = new StringBuilder("The explicit nonce used by the sender does not match the values provided in the DTLS record"); return CCMBlockCipher.decrypt(key, nonce, additionalData, reader.readBytesLeft(), 8);
byte nextByte = 0; while (reader.bytesAvailable()) { nextByte = reader.readNextByte(); if (nextByte != PAYLOAD_MARKER) { if (reader.bytesAvailable(optionLength)) { Option option = new Option(currentOptionNumber); option.setValue(reader.readBytes(optionLength)); if (!reader.bytesAvailable()) { throw new CoAPMessageFormatException( "Found payload marker (0xFF) but message contains no payload", } else { message.setPayload(reader.readBytesLeft());
int major = source.read(8); int minor = source.read(8); ProtocolVersion ver = new ProtocolVersion(major, minor); CipherSuite cipherSuite = CipherSuite.getTypeByCode(source.read(CipherSuite.CIPHER_SUITE_BITS)); if (cipherSuite == null) { return null; CompressionMethod compressionMethod = CompressionMethod.getMethodByCode(source.read(CompressionMethod.COMPRESSION_METHOD_BITS)); if (compressionMethod == null) { return null; byte[] masterSecret = source.readBytes(48); long timestamp = source.readLong(32);
private static ServerNameExtension readServerNameList( final DatagramReader reader, final InetSocketAddress peerAddress) throws HandshakeException { ServerNames serverNames = ServerNames.newInstance(); int listLengthBytes = reader.read(LIST_LENGTH_BITS); while (listLengthBytes > 0) { if (reader.bitsLeft() >= 8) { NameType nameType = NameType.fromCode(reader.readNextByte()); switch (nameType) { case HOST_NAME: byte[] hostname = readHostName(reader, peerAddress); serverNames.add(ServerName.from(nameType, hostname)); listLengthBytes -= (hostname.length + 3); break; default: throw new HandshakeException( "Server Name Indication extension contains unknown name_type", new AlertMessage(AlertLevel.FATAL, AlertDescription.ILLEGAL_PARAMETER, peerAddress)); } } else { throw newDecodeError(peerAddress); } } return new ServerNameExtension(serverNames); }
/** * Reads a sequence of bytes from the stream. * * @param count * The number of bytes to read. * * @return The sequence of bytes read from the stream. */ public byte[] readBytes(final int count) { int bytesToRead = count; // for negative count values, read all bytes left if (bytesToRead < 0) bytesToRead = byteStream.available(); // allocate byte array byte[] bytes = new byte[bytesToRead]; // are there bits left to read in buffer? if (currentBitIndex >= 0) { for (int i = 0; i < bytesToRead; i++) { bytes[i] = (byte) read(Byte.SIZE); } } else { // if bit buffer is empty, call can be delegated // to byte stream to increase performance byteStream.read(bytes, 0, bytes.length); } return bytes; }
@Test public void testBitsLeftWorksForByteWiseReading() { givenABuffer(new byte[]{0x01, 0x02, 0x03}); assertThat(reader.bitsLeft(), is(24)); reader.readBytes(1); assertThat(reader.bitsLeft(), is(16)); reader.readBytes(1); assertThat(reader.bitsLeft(), is(8)); reader.readBytes(1); assertThat(reader.bitsLeft(), is(0)); }
public static HandshakeMessage fromByteArray(byte[] byteArray, InetSocketAddress peerAddress) { DatagramReader reader = new DatagramReader(byteArray); byte[] verifyData = reader.readBytesLeft(); return new Finished(verifyData, peerAddress); } }
private void givenABuffer(byte[] buffer) { reader = new DatagramReader(buffer); } }
/** * Reads the next byte from the stream. * * @return The next byte. */ public byte readNextByte() { byte[] bytes = readBytes(1); return bytes[0]; }
public static AlertMessage fromByteArray(final byte[] byteArray, final InetSocketAddress peerAddress) throws HandshakeException { DatagramReader reader = new DatagramReader(byteArray); byte levelCode = reader.readNextByte(); byte descCode = reader.readNextByte(); AlertLevel level = AlertLevel.getLevelByCode(levelCode); AlertDescription description = AlertDescription.getDescriptionByCode(descCode); if (level == null) { throw new HandshakeException( String.format("Unknown alert level code [%d]", levelCode), new AlertMessage(AlertLevel.FATAL, AlertDescription.DECODE_ERROR, peerAddress)); } else if (description == null) { throw new HandshakeException( String.format("Unknown alert description code [%d]", descCode), new AlertMessage(AlertLevel.FATAL, AlertDescription.DECODE_ERROR, peerAddress)); } else { return new AlertMessage(level, description, peerAddress); } }
@Test public void testBitsLeftWorksForEmptyBuffer() { givenABuffer(new byte[]{}); assertThat(reader.bitsLeft(), is(0)); }
public static HelloExtension fromExtensionData(byte[] extensionData) { DatagramReader reader = new DatagramReader(extensionData); int listLength = reader.read(LIST_LENGTH_BITS); List<ECPointFormat> ecPointFormatList = new ArrayList<ECPointFormat>(); while (listLength > 0) { ECPointFormat format = ECPointFormat.getECPointFormatById(reader.read(POINT_FORMAT_BITS)); ecPointFormatList.add(format); // one point format uses 1 byte listLength -= 1; } return new SupportedPointFormatsExtension(ecPointFormatList); }
@Override public MessageHeader parseHeader(final DatagramReader reader) { int len = reader.read(LENGTH_NIBBLE_BITS); int tokenLength = reader.read(TOKEN_LENGTH_BITS); assertValidTokenLength(tokenLength); reader.readBytes(DatagramFramer.getLengthFieldSize(len)); int code = reader.read(CODE_BITS); byte token[] = reader.readBytes(tokenLength); // No MID/Type/VERSION in TCP message. Use defaults. return new MessageHeader(CoAP.VERSION, CoAP.Type.CON, token, code, Message.NONE, 0); } }