private void givenABuffer(byte[] buffer) { reader = new DatagramReader(buffer); } }
public static List<CipherSuite> listFromByteArray(byte[] byteArray, int numElements) { List<CipherSuite> cipherSuites = new ArrayList<CipherSuite>(); DatagramReader reader = new DatagramReader(byteArray); for (int i = 0; i < numElements; i++) { int code = reader.read(CIPHER_SUITE_BITS); CipherSuite cipher = CipherSuite.getTypeByCode(code); // simply ignore unknown cipher suites as mandated by // RFC 5246, Section 7.4.1.2 Client Hello if (cipher != null) { cipherSuites.add(cipher); } } return cipherSuites; }
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); }
/** * Parses a byte array into a CoAP message header. * * @param raw contains the byte array to parse. * @return the message header the array has been parsed into. * @throws MessageFormatException if the array cannot be parsed into a message header. */ public final MessageHeader parseHeader(RawData raw) { DatagramReader reader = new DatagramReader(raw.getBytes()); return parseHeader(reader); }
public static HandshakeMessage fromByteArray(byte[] byteArray, InetSocketAddress peerAddress) { DatagramReader reader = new DatagramReader(byteArray); byte[] verifyData = reader.readBytesLeft(); return new Finished(verifyData, peerAddress); } }
/** * Takes a byte array and creates the representing list of compression * methods. * * @param byteArray * the encoded compression methods as byte array * @param numElements * the number of compression methods represented in the byte * array * @return corresponding list of compression methods */ public static List<CompressionMethod> listFromByteArray(byte[] byteArray, int numElements) { List<CompressionMethod> compressionMethods = new ArrayList<CompressionMethod>(); DatagramReader reader = new DatagramReader(byteArray); for (int i = 0; i < numElements; i++) { int code = reader.read(COMPRESSION_METHOD_BITS); CompressionMethod method = CompressionMethod.getMethodByCode(code); if (method != null) { compressionMethods.add(method); } } return compressionMethods; } }
/** * Creates a new instance from its byte representation. * * @param extensionData The byte representation. * @param peerAddress The IP address and port that the extension has been received from. * @return The instance. * @throws HandshakeException if the byte representation could not be parsed. */ public static ServerNameExtension fromExtensionData(final byte[] extensionData, final InetSocketAddress peerAddress) throws HandshakeException { if (extensionData == null || extensionData.length == 0) { // this is an "empty" Server Name Indication received in a SERVER_HELLO return ServerNameExtension.emptyServerNameIndication(); } else { DatagramReader reader = new DatagramReader(extensionData); return readServerNameList(reader, peerAddress); } }
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); }
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); }
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_HINT_LENGTH_BITS); byte[] hintEncoded = reader.readBytes(length); return new PSKServerKeyExchange(hintEncoded, peerAddress); }
public static HandshakeMessage fromByteArray(byte[] byteArray, InetSocketAddress peerAddress) throws HandshakeException { DatagramReader reader = new DatagramReader(byteArray); int curveType = reader.read(CURVE_TYPE_BITS); switch (curveType) { // TODO right now only named curve supported case NAMED_CURVE: return readNamedCurve(reader, peerAddress); default: throw new HandshakeException( String.format( "Curve type [%s] received in ServerKeyExchange message from peer [%s] is unsupported", curveType, peerAddress), new AlertMessage(AlertLevel.FATAL, AlertDescription.HANDSHAKE_FAILURE, 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); } }
public static DTLSMessage fromByteArray(byte[] byteArray, InetSocketAddress peerAddress) throws HandshakeException { DatagramReader reader = new DatagramReader(byteArray); int code = reader.read(CCS_BITS); if (code == CCSType.CHANGE_CIPHER_SPEC.getCode()) { return new ChangeCipherSpecMessage(peerAddress); } else { String message = "Unknown Change Cipher Spec code received: " + code; AlertMessage alert = new AlertMessage(AlertLevel.FATAL, AlertDescription.HANDSHAKE_FAILURE, peerAddress); throw new HandshakeException(message, alert); } }
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); }
/** * Creates an instance from a <em>MaxFragmentLength</em> structure as defined * in <a href="http://tools.ietf.org/html/rfc6066#section-4">RFC 6066, Section 4</a>. * * @param extensionData the extension data struct containing the length code * @param peerAddress the IP address and port of the peer that sent the extension * @return the extension object * @throws HandshakeException if the extension data contains an unknown code */ static final MaxFragmentLengthExtension fromExtensionData(byte[] extensionData, InetSocketAddress peerAddress) throws HandshakeException { DatagramReader reader = new DatagramReader(extensionData); int code = reader.read(CODE_BITS); Length length = Length.fromCode(code); if (length != null) { return new MaxFragmentLengthExtension(length); } else { throw new HandshakeException( String.format( "Peer uses unknown code [%d] in %s extension", code, ExtensionType.MAX_FRAGMENT_LENGTH.name()), new AlertMessage( AlertLevel.FATAL, AlertDescription.ILLEGAL_PARAMETER, peerAddress)); } }
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); } }
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); }
/** * Parses a byte array into a CoAP Message. * * @param msg the byte array to parse. * @return the message. * @throws MessageFormatException if the array cannot be parsed into a message. */ public final Message parseMessage(final byte[] msg) { String message = "illegal message code"; DatagramReader reader = new DatagramReader(msg); MessageHeader header = parseHeader(reader); try { if (CoAP.isRequest(header.getCode())) { return parseMessage(reader, header, new Request(CoAP.Code.valueOf(header.getCode()))); } else if (CoAP.isResponse(header.getCode())) { return parseMessage(reader, header, new Response(CoAP.ResponseCode.valueOf(header.getCode()))); } else if (CoAP.isEmptyMessage(header.getCode())) { return parseMessage(reader, header, new EmptyMessage(header.getType())); } } catch (MessageFormatException e) { /** use message to add CoAP message specific information */ message = e.getMessage(); } throw new CoAPMessageFormatException(message, header.getMID(), header.getCode(), CoAP.Type.CON == header.getType()); }