private static void writeBytes(final byte[] bytesToWrite, final DatagramWriter writer) { writer.write(bytesToWrite.length, 16); writer.writeBytes(bytesToWrite); }
/** * Encodes this record into its corresponding <em>DTLSCiphertext</em> structure. * * @return a byte array containing the <em>DTLSCiphertext</em> structure */ public synchronized byte[] toByteArray() { DatagramWriter writer = new DatagramWriter(); writer.write(type.getCode(), CONTENT_TYPE_BITS); writer.write(version.getMajor(), VERSION_BITS); writer.write(version.getMinor(), VERSION_BITS); writer.write(epoch, EPOCH_BITS); writer.writeLong(sequenceNumber, SEQUENCE_NUMBER_BITS); length = fragmentBytes.length; writer.write(length, LENGTH_BITS); writer.writeBytes(fragmentBytes); return writer.toByteArray(); }
@Override protected void addExtensionData(final DatagramWriter writer) { if (serverNames == null) { writer.write(0, LENGTH_BITS); } else { writer.write(serverNames.getEncodedLength() + 2, LENGTH_BITS); //extension_length writer.write(serverNames.getEncodedLength(), LIST_LENGTH_BITS); //server_names_list_length for (ServerName serverName : serverNames) { writer.writeByte(serverName.getType().getCode()); // name type writer.write(serverName.getName().length, LENGTH_BITS); // name length writer.writeBytes(serverName.getName()); // name } } }
private void writeNamedCurve(DatagramWriter writer) { // http://tools.ietf.org/html/rfc4492#section-5.4 writer.write(NAMED_CURVE, CURVE_TYPE_BITS); writer.write(curveId, NAMED_CURVE_BITS); writer.write(pointEncoded.length, PUBLIC_LENGTH_BITS); writer.writeBytes(pointEncoded); // signature if (signatureEncoded != null) { // according to http://tools.ietf.org/html/rfc5246#section-A.7 the // signature algorithm must also be included writer.write(signatureAndHashAlgorithm.getHash().getCode(), HASH_ALGORITHM_BITS); writer.write(signatureAndHashAlgorithm.getSignature().getCode(), SIGNATURE_ALGORITHM_BITS); writer.write(signatureEncoded.length, SIGNATURE_LENGTH_BITS); writer.writeBytes(signatureEncoded); } }
@Override protected void serializeHeader(final DatagramWriter writer, final MessageHeader header) { writer.write(VERSION, VERSION_BITS); writer.write(header.getType().value, TYPE_BITS); writer.write(header.getToken().length, TOKEN_LENGTH_BITS); writer.write(header.getCode(), CODE_BITS); writer.write(header.getMID(), MESSAGE_ID_BITS); writer.writeBytes(header.getToken()); } }
@Override protected void addExtensionData(DatagramWriter writer) { int listLength = ecPointFormatList.size(); // list length + list length field (1 byte) writer.write(listLength + 1, LENGTH_BITS); writer.write(listLength, LIST_LENGTH_BITS); for (ECPointFormat format : ecPointFormatList) { writer.write(format.getId(), POINT_FORMAT_BITS); } }
@Override public byte[] fragmentToByteArray() { DatagramWriter writer = new DatagramWriter(); // TODO only true, if the public value encoding is explicit (not in the // client's certificate), see // http://tools.ietf.org/html/rfc4492#section-5.7 int length = pointEncoded.length; writer.write(length, LENGTH_BITS); writer.writeBytes(pointEncoded); return writer.toByteArray(); }
@Override public byte[] fragmentToByteArray() { DatagramWriter writer = new DatagramWriter(); writer.write(identityEncoded.length, IDENTITY_LENGTH_BITS); writer.writeBytes(identityEncoded); return writer.toByteArray(); }
@Override public byte[] toByteArray() { DatagramWriter writer = new DatagramWriter(); writer.write(CCSProtocolType.getCode(), CCS_BITS); return writer.toByteArray(); }
@Override public byte[] fragmentToByteArray() { DatagramWriter writer = new DatagramWriter(); writer.write(serverVersion.getMajor(), VERSION_BITS); writer.write(serverVersion.getMinor(), VERSION_BITS); writer.write(cookie.length, COOKIE_LENGTH_BITS); writer.writeBytes(cookie); return writer.toByteArray(); }
/** * Transform a list of cipher suites into the appropriate bit-format. * * @param cipherSuites * the cipher suites * @return the byte[] */ public static byte[] listToByteArray(List<CipherSuite> cipherSuites) { DatagramWriter writer = new DatagramWriter(); for (CipherSuite cipherSuite : cipherSuites) { writer.write(cipherSuite.getCode(), CIPHER_SUITE_BITS); } return writer.toByteArray(); }
@Override public byte[] fragmentToByteArray() { DatagramWriter writer = new DatagramWriter(); writer.write(hintEncoded.length, IDENTITY_HINT_LENGTH_BITS); writer.writeBytes(hintEncoded); return writer.toByteArray(); }
/** * Takes a list of compression methods and creates the representing byte * stream. * * @param compressionMethods * the list of the compression methods * @return the corresponding byte array */ public static byte[] listToByteArray(List<CompressionMethod> compressionMethods) { DatagramWriter writer = new DatagramWriter(); for (CompressionMethod compressionMethod : compressionMethods) { writer.write(compressionMethod.getCode(), COMPRESSION_METHOD_BITS); } return writer.toByteArray(); }
@Override public byte[] fragmentToByteArray() { DatagramWriter writer = new DatagramWriter(); // according to http://tools.ietf.org/html/rfc5246#section-4.7 the // signature algorithm must also be included writer.write(signatureAndHashAlgorithm.getHash().getCode(), HASH_ALGORITHM_BITS); writer.write(signatureAndHashAlgorithm.getSignature().getCode(), SIGNATURE_ALGORITHM_BITS); writer.write(signatureBytes.length, SIGNATURE_LENGTH_BITS); writer.writeBytes(signatureBytes); return writer.toByteArray(); }
/** * Serializes this extension to its byte representation as specified by its * respective RFC. * <p> * This method writes this extension's 2-byte code to the result array * and then hands the array over to the {@link #addExtensionData(DatagramWriter)} * method in order to add the encoded extension data. * * @return The byte representation. */ public final byte[] toByteArray() { DatagramWriter writer = new DatagramWriter(); writer.write(type.getId(), TYPE_BITS); addExtensionData(writer); return writer.toByteArray(); }
@Override public byte[] fragmentToByteArray() { DatagramWriter writer = new DatagramWriter(); writer.write(serverVersion.getMajor(), VERSION_BITS); writer.write(serverVersion.getMinor(), VERSION_BITS); writer.writeBytes(random.getRandomBytes()); writer.write(sessionId.length(), SESSION_ID_LENGTH_BITS); writer.writeBytes(sessionId.getId()); writer.write(cipherSuite.getCode(), CIPHER_SUITE_BITS); writer.write(compressionMethod.getCode(), COMPRESSION_METHOD_BITS); if (extensions != null) { writer.writeBytes(extensions.toByteArray()); } return writer.toByteArray(); }
public byte[] toByteArray() { if (extensions.isEmpty()) { return new byte[]{}; } else { DatagramWriter writer = new DatagramWriter(); writer.write(getLength(), LENGTH_BITS); for (HelloExtension extension : extensions) { writer.writeBytes(extension.toByteArray()); } return writer.toByteArray(); } }