Ejemplo n.º 1
0
  /**
   * Decodes an ASN.1-encoded {@code InitialContextToken}. See {@code encodeInitialContextToken} for
   * a description of the encoded token format.
   *
   * @param encodedToken the encoded token.
   * @param codec the {@code Codec} used to decode the token.
   * @return the decoded {@code InitialContextToken} instance.
   * @see #encodeInitialContextToken(org.omg.GSSUP.InitialContextToken, org.omg.IOP.Codec)
   */
  public static InitialContextToken decodeInitialContextToken(byte[] encodedToken, Codec codec) {
    if (encodedToken[0] != 0x60) return null;

    int encodedLength = 0;
    int n = 0;

    if (encodedToken[1] >= 0) encodedLength = encodedToken[1];
    else {
      n = encodedToken[1] & 0x7F;
      for (int i = 1; i <= n; i++) {
        encodedLength += (encodedToken[1 + i] & 0xFF) << (n - i) * 8;
      }
    }

    int length = encodedLength - gssUpMechOidArray.length;
    byte[] encodedInitialContextToken = new byte[length];

    System.arraycopy(
        encodedToken, 2 + n + gssUpMechOidArray.length, encodedInitialContextToken, 0, length);
    Any any;
    try {
      any = codec.decode_value(encodedInitialContextToken, InitialContextTokenHelper.type());
    } catch (Exception e) {
      return null;
    }

    return InitialContextTokenHelper.extract(any);
  }
Ejemplo n.º 2
0
  /**
   * ASN.1-encode an {@code InitialContextToken} as defined in RFC 2743, Section 3.1,
   * "Mechanism-Independent Token Format", pp. 81-82. The encoded token contains the ASN.1 tag 0x60,
   * followed by a token length (which is itself stored in a variable-length format and takes 1 to 5
   * bytes), the GSSUP mechanism identifier, and a mechanism-specific token, which in this case is a
   * CDR encapsulation of the GSSUP {@code InitialContextToken} in the {@code authToken} parameter.
   *
   * @param authToken the {@code InitialContextToken} to be encoded.
   * @param codec the {@code Codec} used to encode the token.
   * @return a {@code byte[]} representing the encoded token.
   */
  public static byte[] encodeInitialContextToken(InitialContextToken authToken, Codec codec) {
    byte[] out;
    Any any = ORB.init().create_any();
    InitialContextTokenHelper.insert(any, authToken);
    try {
      out = codec.encode_value(any);
    } catch (Exception e) {
      return new byte[0];
    }

    int length = out.length + gssUpMechOidArray.length;
    int n;

    if (length < (1 << 7)) {
      n = 0;
    } else if (length < (1 << 8)) {
      n = 1;
    } else if (length < (1 << 16)) {
      n = 2;
    } else if (length < (1 << 24)) {
      n = 3;
    } else { // if (length < (1 << 32))
      n = 4;
    }

    byte[] encodedToken = new byte[2 + n + length];
    encodedToken[0] = 0x60;

    if (n == 0) {
      encodedToken[1] = (byte) length;
    } else {
      encodedToken[1] = (byte) (n | 0x80);
      switch (n) {
        case 1:
          encodedToken[2] = (byte) length;
          break;
        case 2:
          encodedToken[2] = (byte) (length >> 8);
          encodedToken[3] = (byte) length;
          break;
        case 3:
          encodedToken[2] = (byte) (length >> 16);
          encodedToken[3] = (byte) (length >> 8);
          encodedToken[4] = (byte) length;
          break;
        default: // case 4:
          encodedToken[2] = (byte) (length >> 24);
          encodedToken[3] = (byte) (length >> 16);
          encodedToken[4] = (byte) (length >> 8);
          encodedToken[5] = (byte) length;
      }
    }
    System.arraycopy(gssUpMechOidArray, 0, encodedToken, 2 + n, gssUpMechOidArray.length);
    System.arraycopy(out, 0, encodedToken, 2 + n + gssUpMechOidArray.length, out.length);

    return encodedToken;
  }