/** * Reads the next byte from the stream. * * @return The next byte. */ public byte readNextByte() { byte[] bytes = readBytes(1); return bytes[0]; }
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");
/** * Reads the complete sequence of bytes left in the stream. * * @return The sequence of bytes left in the stream. */ public byte[] readBytesLeft() { return readBytes(-1); }
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); }
private static byte[] readBytes(final DatagramReader reader, final int lengthBits) { int length = reader.read(lengthBits); return reader.readBytes(length); }
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); }
public static HandshakeMessage fromByteArray(byte[] byteArray, InetSocketAddress peerAddress) { DatagramReader reader = new DatagramReader(byteArray); int length = reader.read(IDENTITY_LENGTH_BITS); byte[] identityEncoded = reader.readBytes(length); return new PSKClientKeyExchange(identityEncoded, peerAddress); }
@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); } }
public static HandshakeMessage fromByteArray(byte[] byteArray, InetSocketAddress peerAddress) { DatagramReader reader = new DatagramReader(byteArray); int length = reader.read(IDENTITY_HINT_LENGTH_BITS); byte[] hintEncoded = reader.readBytes(length); return new PSKServerKeyExchange(hintEncoded, peerAddress); }
private static CertificateMessage readX509CertificateMessage(final DatagramReader reader, final InetSocketAddress peerAddress) throws HandshakeException { LOGGER.log(Level.FINER, "Parsing X.509 CERTIFICATE message"); int certificateChainLength = reader.read(CERTIFICATE_LIST_LENGTH); List<Certificate> certs = new ArrayList<>(); try { CertificateFactory factory = CertificateFactory.getInstance(CERTIFICATE_TYPE_X509); while (certificateChainLength > 0) { int certificateLength = reader.read(CERTIFICATE_LENGTH_BITS); byte[] certificate = reader.readBytes(certificateLength); // the size of the length and the actual length of the encoded certificate certificateChainLength -= (CERTIFICATE_LENGTH_BITS/8) + certificateLength; certs.add(factory.generateCertificate(new ByteArrayInputStream(certificate))); } return new CertificateMessage(factory.generateCertPath(certs), peerAddress); } catch (CertificateException e) { throw new HandshakeException( "Cannot parse X.509 certificate chain provided by peer", new AlertMessage(AlertLevel.FATAL, AlertDescription.BAD_CERTIFICATE, peerAddress), e); } }
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); }
/** * Creates a certificate message from its binary encoding. * * @param byteArray The binary encoding of the message. * @param useRawPublicKey {@code true} if the certificate message contains a RawPublicKey instead * of an X.509 certificate chain. * @param peerAddress The IP address and port of the peer that sent the message. * @return The certificate message. * @throws HandshakeException if the binary encoding could not be parsed. */ public static CertificateMessage fromByteArray( final byte[] byteArray, boolean useRawPublicKey, InetSocketAddress peerAddress) throws HandshakeException { DatagramReader reader = new DatagramReader(byteArray); if (useRawPublicKey) { LOGGER.log(Level.FINER, "Parsing RawPublicKey CERTIFICATE message"); int certificateLength = reader.read(CERTIFICATE_LENGTH_BITS); byte[] rawPublicKey = reader.readBytes(certificateLength); return new CertificateMessage(rawPublicKey, peerAddress); } else { return readX509CertificateMessage(reader, peerAddress); } }
while (length > 0) { int nameLength = reader.read(CERTIFICATE_AUTHORITY_LENGTH_BITS); byte[] name = reader.readBytes(nameLength); certificateAuthorities.add(new X500Principal(name));
@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); // according to http://tools.ietf.org/html/rfc5246#section-4.7 the // signature algorithm must also be included int hashAlgorithm = reader.read(HASH_ALGORITHM_BITS); int signatureAlgorithm = reader.read(SIGNATURE_ALGORITHM_BITS); SignatureAndHashAlgorithm signAndHash = new SignatureAndHashAlgorithm(hashAlgorithm, signatureAlgorithm); int length = reader.read(SIGNATURE_LENGTH_BITS); byte[] signature = reader.readBytes(length); return new CertificateVerify(signAndHash, signature, peerAddress); }
public static HandshakeMessage fromByteArray(byte[] byteArray, InetSocketAddress peerAddress) { DatagramReader reader = new DatagramReader(byteArray); int major = reader.read(VERSION_BITS); int minor = reader.read(VERSION_BITS); ProtocolVersion version = new ProtocolVersion(major, minor); int cookieLength = reader.read(COOKIE_LENGTH_BITS); byte[] cookie = reader.readBytes(cookieLength); return new HelloVerifyRequest(version, cookie, peerAddress); }
int typeId = reader.read(HelloExtension.TYPE_BITS); int extensionLength = reader.read(HelloExtension.LENGTH_BITS); byte[] extensionBytes = reader.readBytes(extensionLength); HelloExtension extension = HelloExtension.fromByteArray(typeId, extensionBytes, peerAddress);
@Override protected MessageHeader parseHeader(final DatagramReader reader) { int version = reader.read(VERSION_BITS); assertCorrectVersion(version); int type = reader.read(TYPE_BITS); int tokenLength = reader.read(TOKEN_LENGTH_BITS); assertValidTokenLength(tokenLength); int code = reader.read(CODE_BITS); int mid = reader.read(MESSAGE_ID_BITS); byte token[] = reader.readBytes(tokenLength); return new MessageHeader(version, CoAP.Type.valueOf(type), token, code, mid, 0); }
@Test public void testBitsLeftWorksForBitWiseReading() { givenABuffer(new byte[]{0x01, 0x02, 0x03}); reader.read(6); assertThat(reader.bitsLeft(), is(18)); reader.readBytes(1); assertThat(reader.bitsLeft(), is(10)); reader.read(10); assertThat(reader.bitsLeft(), is(0)); }