Beispiel #1
0
  /**
   * Returns the encoded SPNEGO token Note: inserts the required CHOICE tags
   *
   * @return the encoded token
   * @exception GSSException
   */
  byte[] getEncoded() throws IOException, GSSException {

    // get the token encoded value
    DerOutputStream token = new DerOutputStream();
    token.write(encode());

    // now insert the CHOICE
    switch (tokenType) {
      case NEG_TOKEN_INIT_ID:
        // Insert CHOICE of Negotiation Token
        DerOutputStream initToken = new DerOutputStream();
        initToken.write(
            DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte) NEG_TOKEN_INIT_ID), token);
        return initToken.toByteArray();

      case NEG_TOKEN_TARG_ID:
        // Insert CHOICE of Negotiation Token
        DerOutputStream targToken = new DerOutputStream();
        targToken.write(
            DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte) NEG_TOKEN_TARG_ID), token);
        return targToken.toByteArray();
      default:
        return token.toByteArray();
    }
  }
  /**
   * Encodes an EncTicketPart object.
   *
   * @return byte array of encoded EncTicketPart object.
   * @exception Asn1Exception if an error occurs while decoding an ASN1 encoded data.
   * @exception IOException if an I/O error occurs while reading encoded data.
   */
  public byte[] asn1Encode() throws Asn1Exception, IOException {
    DerOutputStream bytes = new DerOutputStream();
    DerOutputStream temp = new DerOutputStream();
    bytes.write(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte) 0x00), flags.asn1Encode());
    bytes.write(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte) 0x01), key.asn1Encode());
    bytes.write(
        DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte) 0x02), cname.getRealm().asn1Encode());
    bytes.write(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte) 0x03), cname.asn1Encode());
    bytes.write(
        DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte) 0x04), transited.asn1Encode());
    bytes.write(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte) 0x05), authtime.asn1Encode());
    if (starttime != null) {
      bytes.write(
          DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte) 0x06), starttime.asn1Encode());
    }
    bytes.write(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte) 0x07), endtime.asn1Encode());

    if (renewTill != null) {
      bytes.write(
          DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte) 0x08), renewTill.asn1Encode());
    }

    if (caddr != null) {
      bytes.write(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte) 0x09), caddr.asn1Encode());
    }

    if (authorizationData != null) {
      bytes.write(
          DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte) 0x0A),
          authorizationData.asn1Encode());
    }
    temp.write(DerValue.tag_Sequence, bytes);
    bytes = new DerOutputStream();
    bytes.write(DerValue.createTag(DerValue.TAG_APPLICATION, true, (byte) 0x03), temp);
    return bytes.toByteArray();
  }
Beispiel #3
0
  /** Get the encoding of the key. */
  public synchronized byte[] getEncoded() {
    if (this.encodedKey == null) {
      try {
        DerOutputStream algid = new DerOutputStream();

        // store oid in algid
        algid.putOID(new ObjectIdentifier(DH_data));

        // encode parameters
        DerOutputStream params = new DerOutputStream();
        params.putInteger(this.p);
        params.putInteger(this.g);
        if (this.l != 0) params.putInteger(this.l);
        // wrap parameters into SEQUENCE
        DerValue paramSequence = new DerValue(DerValue.tag_Sequence, params.toByteArray());
        // store parameter SEQUENCE in algid
        algid.putDerValue(paramSequence);

        // wrap algid into SEQUENCE, and store it in key encoding
        DerOutputStream tmpDerKey = new DerOutputStream();
        tmpDerKey.write(DerValue.tag_Sequence, algid);

        // store key data
        tmpDerKey.putBitString(this.key);

        // wrap algid and key into SEQUENCE
        DerOutputStream derKey = new DerOutputStream();
        derKey.write(DerValue.tag_Sequence, tmpDerKey);
        this.encodedKey = derKey.toByteArray();
      } catch (IOException e) {
        return null;
      }
    }
    return (byte[]) this.encodedKey.clone();
  }
Beispiel #4
0
  public static void main(String[] args) throws Exception {

    s = new String("This is just a test!");

    byte[] asciiBytes = s.getBytes("ASCII");
    byte[] utf8Bytes = s.getBytes("UTF8");
    byte[] iso8859_1Bytes = s.getBytes("ISO-8859-1");

    byte[] unicodeBytes = s.getBytes("UnicodeBigUnmarked");
    byte[] unicodeBytes2 = getBytes(s);

    // test that unicode encoder is the correct one
    if (!equalBytes(unicodeBytes, unicodeBytes2))
      throw new Exception("Problem with unicode encoder being used.");

    FileOutputStream fout = new FileOutputStream(fileName);
    DerOutputStream derOut = new DerOutputStream();

    System.out.println("Writing Java string out as various DER" + " encoded Strings now...");
    derOut.putUTF8String(s);
    derOut.putPrintableString(s);
    derOut.putIA5String(s);
    derOut.putT61String(s);
    derOut.putBMPString(s);

    derOut.derEncode(fout);
    fout.close();

    FileInputStream fis = new FileInputStream(fileName);
    byte[] data = new byte[fis.available()];
    fis.read(data);
    DerInputStream derIn = new DerInputStream(data);
    fis.close();

    System.out.println("\nReading Strings back as DerValue's...\n");

    DerValue der;

    der = derIn.getDerValue();
    verifyDER("UTF8", der, DerValue.tag_UTF8String, utf8Bytes);

    der = derIn.getDerValue();
    verifyDER("Printable", der, DerValue.tag_PrintableString, asciiBytes);

    der = derIn.getDerValue();
    verifyDER("IA5", der, DerValue.tag_IA5String, asciiBytes);

    der = derIn.getDerValue();
    verifyDER("T61", der, DerValue.tag_T61String, iso8859_1Bytes);

    der = derIn.getDerValue();
    verifyDER("BMP", der, DerValue.tag_BMPString, unicodeBytes);

    if (derIn.available() > 0) throw new Exception("DerInputStream has extra data!");

    derIn.reset();

    System.out.println("Reading Strings back as Strings...\n");

    verifyString("UTF8", derIn.getUTF8String());
    verifyString("Printable", derIn.getPrintableString());
    verifyString("IA5", derIn.getIA5String());
    verifyString("T61", derIn.getT61String());
    verifyString("BMP", derIn.getBMPString());
  }