Package ch.ethz.inf.vs.scandium.util

Examples of ch.ethz.inf.vs.scandium.util.DatagramWriter


  // Serialization //////////////////////////////////////////////////

  // TODO this is called 4 times for Flight 4
  @Override
  public byte[] fragmentToByteArray() {
    DatagramWriter writer = new DatagramWriter();

    switch (curveType) {
    // TODO add support for other curve types
    case EXPLICIT_PRIME:
    case EXPLICIT_CHAR2:
      break;

    case NAMED_CURVE:
      // 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);
      }
      break;

    default:
      LOGGER.severe("Unknown curve type: " + curveId);
      break;
    }

    return writer.toByteArray();
  }
View Full Code Here


  // Serialization //////////////////////////////////////////////////

  @Override
  public byte[] fragmentToByteArray() {
    DatagramWriter writer = new DatagramWriter();
   
    writer.write(identityEncoded.length, IDENTITY_LENGTH_BITS);
    writer.writeBytes(identityEncoded);
   
    return writer.toByteArray();
  }
View Full Code Here

  // Serialization //////////////////////////////////////////////////

  // @Override
  public byte[] toByteArray() {
    DatagramWriter writer = new DatagramWriter();

    writer.write(level.getCode(), BITS);
    writer.write(description.getCode(), BITS);

    return writer.toByteArray();
  }
View Full Code Here

  public abstract int getLength();

  // Serialization //////////////////////////////////////////////////

  public byte[] toByteArray() {
    DatagramWriter writer = new DatagramWriter();

    writer.write(type.getId(), TYPE_BITS);

    return writer.toByteArray();
  }
View Full Code Here

  // Serialization //////////////////////////////////////////////////

  public byte[] toByteArray() {

    DatagramWriter writer = new DatagramWriter();

    // AlgorithmIdentifier ::= SEQUENCE {
    // algorithm OBJECT IDENTIFIER,
    // parameters ANY DEFINED BY algorithm OPTIONAL
    // }
    byte[] algorithmBytes = writeTLV(OBJECT_IDENTIFIER_TAG, encodeOID(algorithmOID));
    byte[] parametersBytes;
    if (parametersOID != null) {
      parametersBytes = writeTLV(OBJECT_IDENTIFIER_TAG, encodeOID(parametersOID));
    } else {
      parametersBytes = writeTLV(NULL_TAG, new byte[0]);
    }
    byte[] algorithmIdentifierBytes = writeTLV(SEQUENCE_TAG, ByteArrayUtils.concatenate(algorithmBytes, parametersBytes));

    // SubjectPublicKeyInfo ::= SEQUENCE {
    // algorithm AlgorithmIdentifier,
    // subjectPublicKey BIT STRING
    // }
    byte[] subjectPublicKeyBytes = writeTLV(BIT_STRING_TAG, subjectPublicKey);
    byte[] subjectPublicKeyInfo = writeTLV(SEQUENCE_TAG, ByteArrayUtils.concatenate(algorithmIdentifierBytes, subjectPublicKeyBytes));

    writer.writeBytes(subjectPublicKeyInfo);

    return writer.toByteArray();
  }
View Full Code Here

   * @param value
   *            the value to be written.
   * @return the corresponding byte array representation.
   */
  private byte[] writeTLV(int tag, byte[] value) {
    DatagramWriter writer = new DatagramWriter();

    // write the tag
    writer.write(tag, OCTET_BITS);

    switch (tag) {
    case BIT_STRING_TAG:
      // add the unused field to the value, as described here:
      // http://msdn.microsoft.com/en-us/library/windows/desktop/bb540792(v=vs.85).aspx
      byte[] unusedByte = new byte[1];

      // in our cases, there are never unused bits in the last byte
      unusedByte[0] = 0x00;
      value = ByteArrayUtils.concatenate(unusedByte, value);
      break;

    default:
      break;
    }

    int length = value.length;
    if (length > 127) {
      /*
       * If it is more than 127 bytes, bit 7 of the Length field is set to
       * 1 and bits 6 through 0 specify the number of additional bytes
       * used to identify the content length.
       */
      int additionalBytes = 0;
      if (length >= 16777216) { // 2^24
        additionalBytes = 4;
      } else if (length >= 65536) { // 2^16
        additionalBytes = 3;
      } else if (length >= 256) { // 2^8
        additionalBytes = 2;
      } else {
        additionalBytes = 1;
      }
      int lengthField = 0x80;
      lengthField += additionalBytes;
      writer.write(lengthField, OCTET_BITS);
      writer.write(length, additionalBytes * OCTET_BITS);

    } else {
      /*
       * If the SEQUENCE contains fewer than 128 bytes, the Length field
       * of the TLV triplet requires only one byte to specify the content
       * length.
       */
      writer.write(length, OCTET_BITS);
    }
    writer.writeBytes(value);

    return writer.toByteArray();
  }
View Full Code Here

  // Serialization //////////////////////////////////////////////////

  @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.getSessionId());

    writer.write(cipherSuite.getCode(), CIPHER_SUITE_BITS);
    writer.write(compressionMethod.getCode(), COMPRESSION_METHOD_BITS);

    if (extensions != null) {
      writer.writeBytes(extensions.toByteArray());
    }

    return writer.toByteArray();
  }
View Full Code Here

  // Serialization //////////////////////////////////////////////////

  @Override
  public byte[] fragmentToByteArray() {
    DatagramWriter writer = new DatagramWriter();
   
    writer.write(hintEncoded.length, IDENTITY_HINT_LENGTH_BITS);
    writer.writeBytes(hintEncoded);
   
    return writer.toByteArray();
  }
View Full Code Here

  // Serialization //////////////////////////////////////////////////

  @Override
  public byte[] toByteArray() {
    DatagramWriter writer = new DatagramWriter();
    writer.writeBytes(super.toByteArray());
   
    if (isClientExtension) {
      int listLength = certificateTypes.size();     
      writer.write(listLength + 1, LENGTH_BITS);
      writer.write(listLength, LIST_FIELD_LENGTH_BITS);
      for (CertificateType type : certificateTypes) {
        writer.write(type.getCode(), EXTENSION_TYPE_BITS);
      }
    } else {
      // we assume the list contains exactly one element
      writer.write(1, LENGTH_BITS);
      writer.write(certificateTypes.get(0).getCode(), EXTENSION_TYPE_BITS);
    }
   
    return writer.toByteArray();
  }
View Full Code Here

  // Serialization //////////////////////////////////////////////////

  @Override
  public byte[] fragmentToByteArray() {

    DatagramWriter writer = new DatagramWriter();

    writer.write(clientVersion.getMajor(), VERSION_BITS);
    writer.write(clientVersion.getMinor(), VERSION_BITS);

    writer.writeBytes(random.getRandomBytes());

    writer.write(sessionId.length(), SESSION_ID_LENGTH_BITS);
    writer.writeBytes(sessionId.getSessionId());

    writer.write(cookie.length(), COOKIE_LENGTH);
    writer.writeBytes(cookie.getCookie());

    writer.write(cipherSuites.size() * 2, CIPHER_SUITS_LENGTH_BITS);
    writer.writeBytes(CipherSuite.listToByteArray(cipherSuites));

    writer.write(compressionMethods.size(), COMPRESSION_METHODS_LENGTH_BITS);
    writer.writeBytes(CompressionMethod.listToByteArray(compressionMethods));

    if (extensions != null) {
      writer.writeBytes(extensions.toByteArray());
    }

    return writer.toByteArray();
  }
View Full Code Here

TOP

Related Classes of ch.ethz.inf.vs.scandium.util.DatagramWriter

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.