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

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


    return writer.toByteArray();
  }

  public static DTLSMessage fromByteArray(byte[] byteArray) throws HandshakeException {
    DatagramReader reader = new DatagramReader(byteArray);
    int code = reader.read(CCS_BITS);
    if (code == CCSType.CHANGE_CIPHER_SPEC.getCode()) {
      return new ChangeCipherSpecMessage();
    } else {
      String message = "Unknown Change Cipher Spec code received: " + code;
      AlertMessage alert = new AlertMessage(AlertLevel.FATAL, AlertDescription.HANDSHAKE_FAILURE);
View Full Code Here


    return writer.toByteArray();
  }

  public static HandshakeMessage fromByteArray(byte[] byteArray) throws HandshakeException {
    DatagramReader reader = new DatagramReader(byteArray);
    int curveType = reader.read(CURVE_TYPE_BITS);
    switch (curveType) {
    // TODO right now only named curve supported
    case EXPLICIT_PRIME:
    case EXPLICIT_CHAR2:
      AlertMessage alert = new AlertMessage(AlertLevel.FATAL, AlertDescription.HANDSHAKE_FAILURE);
      throw new HandshakeException("Not supported curve type in ServerKeyExchange message", alert);
     
    case NAMED_CURVE:
      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(HashAlgorithm.SHA256, SignatureAlgorithm.ECDSA);
     
      byte[] signatureEncoded = null;
      if (bytesLeft.length > 0) {
        reader = new DatagramReader(bytesLeft);
        int hashAlgorithm = reader.read(HASH_ALGORITHM_BITS);
        int signatureAlgorithm = reader.read(SIGNATURE_ALGORITHM_BITS);
        signAndHash = new SignatureAndHashAlgorithm(hashAlgorithm, signatureAlgorithm);
        length = reader.read(SIGNATURE_LENGTH_BITS);
        signatureEncoded = reader.readBytes(length);
      }

      return new ECDHServerKeyExchange(signAndHash, curveId, pointEncoded, signatureEncoded);

    default:
View Full Code Here

   
    return writer.toByteArray();
  }
 
  public static HandshakeMessage fromByteArray(byte[] byteArray) {
    DatagramReader reader = new DatagramReader(byteArray);
   
    int length = reader.read(IDENTITY_LENGTH_BITS);
    byte[] identityEncoded = reader.readBytes(length);
   
    return new PSKClientKeyExchange(identityEncoded);
  }
View Full Code Here

    return sb.toString();
  };
 
  public static HelloExtension fromByteArray(byte[] byteArray) {
    DatagramReader reader = new DatagramReader(byteArray);
   
    List<CertificateType> certificateTypes = new ArrayList<CertificateType>();
   
    // the client's extension needs at least 2 bytes, while the server's is exactly 1 byte long
    boolean isClientExtension = true;
    if (byteArray.length > 1) {
      int length = reader.read(LIST_FIELD_LENGTH_BITS);
      for (int i = 0; i < length; i++) {
        certificateTypes.add(CertificateType.getTypeFromCode(reader.read(EXTENSION_TYPE_BITS)));
      }
    } else {
      certificateTypes.add(CertificateType.getTypeFromCode(reader.read(EXTENSION_TYPE_BITS)));
      isClientExtension = false;
    }

    return new ServerCertificateTypeExtension(isClientExtension, certificateTypes);
  }
View Full Code Here

    return writer.toByteArray();
  }

  public static DTLSMessage fromByteArray(byte[] byteArray) {
    DatagramReader reader = new DatagramReader(byteArray);

    int level = reader.read(BITS);
    int description = reader.read(BITS);

    return new AlertMessage(AlertLevel.getLevelByCode(level), AlertDescription.getDescriptionByCode(description));
  }
View Full Code Here

   *            the encoded TLV triplet.
   * @param rawPublicKey
   *            the {@link RawPublicKey} which needs to be initialized.
   */
  private static void readTLV(byte[] byteArray, RawPublicKey rawPublicKey) {
    DatagramReader reader = new DatagramReader(byteArray);

    while (reader.bytesAvailable()) {
      int tag = reader.read(OCTET_BITS);
      int length = reader.read(OCTET_BITS);

      // decode the length of the value, if encoded into multiply bytes
      if (length > 127) {
        int additionalBytes = length & 0x7F;
        length = reader.read(additionalBytes * OCTET_BITS);
      }
      byte[] fragment = reader.readBytes(length);

      switch (tag) {
      case SEQUENCE_TAG:
        readTLV(fragment, rawPublicKey);
        break;
View Full Code Here

    return writer.toByteArray();
  }

  public static HandshakeMessage fromByteArray(byte[] byteArray) throws HandshakeException {
    DatagramReader reader = new DatagramReader(byteArray);

    int major = reader.read(VERSION_BITS);
    int minor = reader.read(VERSION_BITS);
    ProtocolVersion version = new ProtocolVersion(major, minor);

    Random random = new Random(reader.readBytes(RANDOM_BYTES));

    int sessionIdLength = reader.read(SESSION_ID_LENGTH_BITS);
    SessionId sessionId = new SessionId(reader.readBytes(sessionIdLength));

    CipherSuite cipherSuite = CipherSuite.getTypeByCode(reader.read(CIPHER_SUITE_BITS));
    CompressionMethod compressionMethod = CompressionMethod.getMethodByCode(reader.read(COMPRESSION_METHOD_BITS));

    byte[] bytesLeft = reader.readBytesLeft();
    HelloExtensions extensions = null;
    if (bytesLeft.length > 0) {
      extensions = HelloExtensions.fromByteArray(bytesLeft);
    }
View Full Code Here

   
    return writer.toByteArray();
  }
 
  public static HandshakeMessage fromByteArray(byte[] byteArray) {
    DatagramReader reader = new DatagramReader(byteArray);
   
    int length = reader.read(IDENTITY_HINT_LENGTH_BITS);
    byte[] hintEncoded = reader.readBytes(length);
   
    return new PSKServerKeyExchange(hintEncoded);
  }
View Full Code Here

    return writer.toByteArray();
  }

  public static HandshakeMessage fromByteArray(byte[] byteArray) throws HandshakeException {
    DatagramReader reader = new DatagramReader(byteArray);

    int major = reader.read(VERSION_BITS);
    int minor = reader.read(VERSION_BITS);
    ProtocolVersion clientVersion = new ProtocolVersion(major, minor);

    Random random = new Random(reader.readBytes(RANDOM_BYTES));

    int sessionIdLength = reader.read(SESSION_ID_LENGTH_BITS);
    SessionId sessionId = new SessionId(reader.readBytes(sessionIdLength));

    int cookieLength = reader.read(COOKIE_LENGTH);
    Cookie cookie = new Cookie(reader.readBytes(cookieLength));

    int cipherSuitesLength = reader.read(CIPHER_SUITS_LENGTH_BITS);
    List<CipherSuite> cipherSuites = CipherSuite.listFromByteArray(reader.readBytes(cipherSuitesLength), cipherSuitesLength / 2); // 2

    int compressionMethodsLength = reader.read(COMPRESSION_METHODS_LENGTH_BITS);
    List<CompressionMethod> compressionMethods = CompressionMethod.listFromByteArray(reader.readBytes(compressionMethodsLength), compressionMethodsLength);

    byte[] bytesLeft = reader.readBytesLeft();
    HelloExtensions extensions = null;
    if (bytesLeft.length > 0) {
      extensions = HelloExtensions.fromByteArray(bytesLeft);
    }
View Full Code Here

    return writer.toByteArray();
  }

  public static HandshakeMessage fromByteArray(byte[] byteArray) {
    DatagramReader reader = new DatagramReader(byteArray);
    int length = reader.read(LENGTH_BITS);
    byte[] pointEncoded = reader.readBytes(length);

    return new ECDHClientKeyExchange(pointEncoded);
  }
View Full Code Here

TOP

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

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.