not-yet-commons-ssl-0.3.11

a Class Version: 1.2 (46.0)
 * JD-Core Version:    0.7.1
 */
package org.apache.commons.ssl.asn1;

import java.io.ByteArrayOutputStream;
import java.io.IOException;

public class DERBitString
  extends ASN1Object
  implements DERString
{
  private static final char[] table = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
  protected byte[] data;
  protected int padBits;
  
  protected static int getPadBits(int bitString)
  {
    int val = 0;
    for (int i = 3; i >= 0; i--) {
      if (i != 0)
      {
        if (bitString >> i * 8 != 0)
        {
          val = bitString >> i * 8 & 0xFF;
          break;
        }
      }
      else if (bitString != 0)
      {
        val = bitString & 0xFF;
        break;
      }
    }
    if (val == 0) {
      return 7;
    }
    int bits = 1;
    while ((val <<= 1 & 0xFF) != 0) {
      bits++;
    }
    return 8 - bits;
  }
  
  protected static byte[] getBytes(int bitString)
  {
    int bytes = 4;
    for (int i = 3; i >= 1; i--)
    {
      if ((bitString & 255 << i * 8) != 0) {
        break;
      }
      bytes--;
    }
    byte[] result = new byte[bytes];
    for (int i = 0; i < bytes; i++) {
      result[i] = ((byte)(bitString >> i * 8 & 0xFF));
    }
    return result;
  }
  
  public static DERBitString getInstance(Object obj)
  {
    if ((obj == null) || ((obj instanceof DERBitString))) {
      return (DERBitString)obj;
    }
    if ((obj instanceof ASN1OctetString))
    {
      byte[] bytes = ((ASN1OctetString)obj).getOctets();
      int padBits = bytes[0];
      byte[] data = new byte[bytes.length - 1];
      
      System.arraycopy(bytes, 1, data, 0, bytes.length - 1);
      
      return new DERBitString(data, padBits);
    }
    if ((obj instanceof ASN1TaggedObject)) {
      return getInstance(((ASN1TaggedObject)obj).getObject());
    }
    throw new IllegalArgumentException("illegal object in getInstance: " + obj.getClass().getName());
  }
  
  public static DERBitString getInstance(ASN1TaggedObject obj, boolean explicit)
  {
    return getInstance(obj.getObject());
  }
  
  protected DERBitString(byte data, int padBits)
  {
    this.data = new byte[1];
    this.data[0] = data;
    this.padBits = padBits;
  }
  
  public DERBitString(byte[] data, int padBits)
  {
    this.data = data;
    this.padBits = padBits;
  }
  
  public DERBitString(byte[] data)
  {
    this(data, 0);
  }
  
  public DERBitString(DEREncodable obj)
  {
    try
    {
      ByteArrayOutputStream bOut = new ByteArrayOutputStream();
      DEROutputStream dOut = new DEROutputStream(bOut);
      
      dOut.writeObject(obj);
      dOut.close();
      
      data = bOut.toByteArray();
      padBits = 0;
    }
    catch (IOException e)
    {
      throw new IllegalArgumentException("Error processing object : " + e.toString());
    }
  }
  
  public byte[] getBytes()
  {
    return data;
  }
  
  public int getPadBits()
  {
    return padBits;
  }
  
  public int intValue()
  {
    int value = 0;
    for (int i = 0; (i != data.length) && (i != 4); i++) {
      value |= (data[i] & 0xFF) << 8 * i;
    }
    return value;
  }
  
  void encode(DEROutputStream out)
    throws IOException
  {
    byte[] bytes = new byte[getBytes().length + 1];
    
    bytes[0] = ((byte)getPadBits());
    System.arraycopy(getBytes(), 0, bytes, 1, bytes.length - 1);
    
    out.writeEncoded(3, bytes);
  }
  
  public int hashCode()
  {
    int value = 0;
    for (int i = 0; i != data.length; i++) {
      value ^= (data[i] & 0xFF) << i % 4;
    }
    return value;
  }
  
  protected boolean asn1Equals(DERObject o)
  {
    if (!(o instanceof DERBitString)) {
      return false;
    }
    DERBitString other = (DERBitString)o;
    if (data.length != data.length) {
      return false;
    }
    for (int i = 0; i != data.length; i++) {
      if (data[i] != data[i]) {
        return false;
      }
    }
    return padBits == padBits;
  }
  
  public String getString()
  {
    StringBuffer buf = new StringBuffer("#");
    ByteArrayOutputStream bOut = new ByteArrayOutputStream();
    ASN1OutputStream aOut = new ASN1OutputStream(bOut);
    try
    {
      aOut.writeObject(this);
    }
    catch (IOException e)
    {
      throw new RuntimeException("internal error encoding BitString");
    }
    byte[] string = bOut.toByteArray();
    for (int i = 0; i != string.length; i++)
    {
      buf.append(table[(string[i] >>> 4 & 0xF)]);
      buf.append(table[(string[i] & 0xF)]);
    }
    return buf.toString();
  }
  
  public String toString()
  {
    return getString();
  }
}

/* Location:
 * Qualified Name:     org.apache.commons.ssl.asn1.DERBitString
 * Java Class Version: 1.2 (46.0)
 * JD-Core Version:    0.7.1
 */
package org.apache.commons.ssl.asn1;

import java.io.IOException;

public class DERBoolean
  extends ASN1Object
{
  byte value;
  public static final DERBoolean FALSE = new DERBoolean(false);
  public static final DERBoolean TRUE = new DERBoolean(true);
  
  public static DERBoolean getInstance(Object obj)
  {
    if ((obj == null) || ((obj instanceof DERBoolean))) {
      return (DERBoolean)obj;
    }
    if ((obj instanceof ASN1OctetString)) {
      return new DERBoolean(((ASN1OctetString)obj).getOctets());
    }
    if ((obj instanceof ASN1TaggedObject)) {
      return getInstance(((ASN1TaggedObject)obj).getObject());
    }
    throw new IllegalArgumentException("illegal object in getInstance: " + obj.getClass().getName());
  }
  
  public static DERBoolean getInstance(boolean value)
  {
    return value ? TRUE : FALSE;
  }
  
  public static DERBoolean getInstance(ASN1TaggedObject obj, boolean explicit)
  {
    return getInstance(obj.getObject());
  }
  
  public DERBoolean(byte[] value)
  {
    this.value = value[0];
  }
  
  public DERBoolean(boolean value)
  {
    this.value = (value ? -1 : 0);
  }
  
  public boolean isTrue()
  {
    return value != 0;
  }
  
  void encode(DEROutputStream out)
    throws IOException
  {
    byte[] bytes = new byte[1];
    
    bytes[0] = value;
    
    out.writeEncoded(1, bytes);
  }
  
  protected boolean asn1Equals(DERObject o)
  {
    if ((o == null) || (!(o instanceof DERBoolean))) {
      return false;
    }
    return value == value;
  }
  
  public int hashCode()
  {
    return value;
  }
  
  public String toString()
  {
    return value != 0 ? "TRUE" : "FALSE";
  }
}

/* Location:
 * Qualified Name:     org.apache.commons.ssl.asn1.DERBoolean
 * Java Class Version: 1.2 (46.0)
 * JD-Core Version:    0.7.1
 */
package org.apache.commons.ssl.asn1;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Enumeration;

/**
 * @deprecated
 */
public class DERConstructedSequence
  extends ASN1Sequence
{
  public void addObject(DEREncodable obj)
  {
    super.addObject(obj);
  }
  
  public int getSize()
  {
    return size();
  }
  
  void encode(DEROutputStream out)
    throws IOException
  {
    ByteArrayOutputStream bOut = new ByteArrayOutputStream();
    DEROutputStream dOut = new DEROutputStream(bOut);
    Enumeration e = getObjects();
    while (e.hasMoreElements())
    {
      Object obj = e.nextElement();
      
      dOut.writeObject(obj);
    }
    dOut.close();
    
    byte[] bytes = bOut.toByteArray();
    
    out.writeEncoded(48, bytes);
  }
}

/* Location:
 * Qualified Name:     org.apache.commons.ssl.asn1.DERConstructedSequence
 * Java Class Version: 1.2 (46.0)
 * JD-Core Version:    0.7.1
 */
package org.apache.commons.ssl.asn1;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Enumeration;

/**
 * @deprecated
 */
public class DERConstructedSet
  extends ASN1Set
{
  public DERConstructedSet() {}
  
  public DERConstructedSet(DEREncodable obj)
  {
    addObject(obj);
  }
  
  public DERConstructedSet(DEREncodableVector v)
  {
    for (int i = 0; i != v.size(); i++) {
      addObject(v.get(i));
    }
  }
  
  public void addObject(DEREncodable obj)
  {
    super.addObject(obj);
  }
  
  public int getSize()
  {
    return size();
  }
  
  void encode(DEROutputStream out)
    throws IOException
  {
    ByteArrayOutputStream bOut = new ByteArrayOutputStream();
    DEROutputStream dOut = new DEROutputStream(bOut);
    Enumeration e = getObjects();
    while (e.hasMoreElements())
    {
      Object obj = e.nextElement();
      
      dOut.writeObject(obj);
    }
    dOut.close();
    
    byte[] bytes = bOut.toByteArray();
    
    out.writeEncoded(49, bytes);
  }
}

/* Location:
 * Qualified Name:     org.apache.commons.ssl.asn1.DERConstructedSet
 * Java Class Version: 1.2 (46.0)
 * JD-Core Version:    0.7.1
 */
package org.apache.commons.ssl.asn1;

public abstract interface DEREncodable
{
  public abstract DERObject getDERObject();
}

/* Location:
 * Qualified Name:     org.apache.commons.ssl.asn1.DEREncodable
 * Java Class Version: 1.2 (46.0)
 * JD-Core Version:    0.7.1
 */
package org.apache.commons.ssl.asn1;

import java.util.Vector;

public class DEREncodableVector
{
  private Vector v = new Vector();
  
  public void add(DEREncodable obj)
  {
    v.addElement(obj);
  }
  
  public DEREncodable get(int i)
  {
    return (DEREncodable)v.elementAt(i);
  }
  
  public int size()
  {
    return v.size();
  }
}

/* Location:
 * Qualified Name:     org.apache.commons.ssl.asn1.DEREncodableVector
 * Java Class Version: 1.2 (46.0)
 * JD-Core Version:    0.7.1
 */
package org.apache.commons.ssl.asn1;

import java.io.IOException;
import java.math.BigInteger;

public class DEREnumerated
  extends ASN1Object
{
  byte[] bytes;
  
  public static DEREnumerated getInstance(Object obj)
  {
    if ((obj == null) || ((obj instanceof DEREnumerated))) {
      return (DEREnumerated)obj;
    }
    if ((obj instanceof ASN1OctetString)) {
      return new DEREnumerated(((ASN1OctetString)obj).getOctets());
    }
    if ((obj instanceof ASN1TaggedObject)) {
      return getInstance(((ASN1TaggedObject)obj).getObject());
    }
    throw new IllegalArgumentException("illegal object in getInstance: " + obj.getClass().getName());
  }
  
  public static DEREnumerated getInstance(ASN1TaggedObject obj, boolean explicit)
  {
    return getInstance(obj.getObject());
  }
  
  public DEREnumerated(int value)
  {
    bytes = BigInteger.valueOf(value).toByteArray();
  }
  
  public DEREnumerated(BigInteger value)
  {
    bytes = value.toByteArray();
  }
  
  public DEREnumerated(byte[] bytes)
  {
    this.bytes = bytes;
  }
  
  public BigInteger getValue()
  {
    return new BigInteger(bytes);
  }
  
  void encode(DEROutputStream out)
    throws IOException
  {
    out.writeEncoded(10, bytes);
  }
  
  boolean asn1Equals(DERObject o)
  {
    if (!(o instanceof DEREnumerated)) {
      return false;
    }
    DEREnumerated other = (DEREnumerated)o;
    if (bytes.length != bytes.length) {
      return false;
    }
    for (int i = 0; i != bytes.length; i++) {
      if (bytes[i] != bytes[i]) {
        return false;
      }
    }
    return true;
  }
  
  public int hashCode()
  {
    return getValue().hashCode();
  }
}

/* Location:
 * Qualified Name:     org.apache.commons.ssl.asn1.DEREnumerated
 * Java Class Version: 1.2 (46.0)
 * JD-Core Version:    0.7.1
 */
package org.apache.commons.ssl.asn1;

import java.io.IOException;

public class DERGeneralString
  extends ASN1Object
  implements DERString
{
  private String string;
  
  public static DERGeneralString getInstance(Object obj)
  {
    if ((obj == null) || ((obj instanceof DERGeneralString))) {
      return (DERGeneralString)obj;
    }
    if ((obj instanceof ASN1OctetString)) {
      return new DERGeneralString(((ASN1OctetString)obj).getOctets());
    }
    if ((obj instanceof ASN1TaggedObject)) {
      return getInstance(((ASN1TaggedObject)obj).getObject());
    }
    throw new IllegalArgumentException("illegal object in getInstance: " + obj.getClass().getName());
  }
  
  public static DERGeneralString getInstance(ASN1TaggedObject obj, boolean explicit)
  {
    return getInstance(obj.getObject());
  }
  
  public DERGeneralString(byte[] string)
  {
    char[] cs = new char[string.length];
    for (int i = 0; i != cs.length; i++) {
      cs[i] = ((char)(string[i] & 0xFF));
    }
    this.string = new String(cs);
  }
  
  public DERGeneralString(String string)
  {
    this.string = string;
  }
  
  public String getString()
  {
    return string;
  }
  
  public String toString()
  {
    return string;
  }
  
  public byte[] getOctets()
  {
    char[] cs = string.toCharArray();
    byte[] bs = new byte[cs.length];
    for (int i = 0; i != cs.length; i++) {
      bs[i] = ((byte)cs[i]);
    }
    return bs;
  }
  
  void encode(DEROutputStream out)
    throws IOException
  {
    out.writeEncoded(27, getOctets());
  }
  
  public int hashCode()
  {
    return getString().hashCode();
  }
  
  boolean asn1Equals(DERObject o)
  {
    if (!(o instanceof DERGeneralString)) {
      return false;
    }
    DERGeneralString s = (DERGeneralString)o;
    return getString().equals(s.getString());
  }
}

/* Location:
 * Qualified Name:     org.apache.commons.ssl.asn1.DERGeneralString
 * Java Class Version: 1.2 (46.0)
 * JD-Core Version:    0.7.1
 */
package org.apache.commons.ssl.asn1;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.SimpleTimeZone;
import java.util.TimeZone;

public class DERGeneralizedTime
  extends ASN1Object
{
  String time;
  
  public static DERGeneralizedTime getInstance(Object obj)
  {
    if ((obj == null) || ((obj instanceof DERGeneralizedTime))) {
      return (DERGeneralizedTime)obj;
    }
    if ((obj instanceof ASN1OctetString)) {
      return new DERGeneralizedTime(((ASN1OctetString)obj).getOctets());
    }
    throw new IllegalArgumentException("illegal object in getInstance: " + obj.getClass().getName());
  }
  
  public static DERGeneralizedTime getInstance(ASN1TaggedObject obj, boolean explicit)
  {
    return getInstance(obj.getObject());
  }
  
  public DERGeneralizedTime(String time)
  {
    this.time = time;
    try
    {
      getDate();
    }
    catch (ParseException e)
    {
      throw new IllegalArgumentException("invalid date string: " + e.getMessage());
    }
  }
  
  public DERGeneralizedTime(Date time)
  {
    SimpleDateFormat dateF = new SimpleDateFormat("yyyyMMddHHmmss'Z'");
    
    dateF.setTimeZone(new SimpleTimeZone(0, "Z"));
    
    this.time = dateF.format(time);
  }
  
  DERGeneralizedTime(byte[] bytes)
  {
    char[] dateC = new char[bytes.length];
    for (int i = 0; i != dateC.length; i++) {
      dateC[i] = ((char)(bytes[i] & 0xFF));
    }
    time = new String(dateC);
  }
  
  public String getTimeString()
  {
    return time;
  }
  
  public String getTime()
  {
    if (time.charAt(time.length() - 1) == 'Z') {
      return time.substring(0, time.length() - 1) + "GMT+00:00";
    }
    int signPos = time.length() - 5;
    char sign = time.charAt(signPos);
    if ((sign == '-') || (sign == '+')) {
      return time.substring(0, signPos) + "GMT" + time.substring(signPos, signPos + 3) + ":" + time.substring(signPos + 3);
    }
    signPos = time.length() - 3;
    sign = time.charAt(signPos);
    if ((sign == '-') || (sign == '+')) {
      return time.substring(0, signPos) + "GMT" + time.substring(signPos) + ":00";
    }
    return time + calculateGMTOffset();
  }
  
  private String calculateGMTOffset()
  {
    String sign = "+";
    TimeZone timeZone = TimeZone.getDefault();
    int offset = timeZone.getRawOffset();
    if (offset < 0)
    {
      sign = "-";
      offset = -offset;
    }
    int hours = offset / 3600000;
    int minutes = (offset - hours * 60 * 60 * 1000) / 60000;
    try
    {
      if ((timeZone.useDaylightTime()) && (timeZone.inDaylightTime(getDate()))) {
        hours += (sign.equals("+") ? 1 : -1);
      }
    }
    catch (ParseException e) {}
    return "GMT" + sign + convert(hours) + ":" + convert(minutes);
  }
  
  private String convert(int time)
  {
    if (time < 10) {
      return "0" + time;
    }
    return Integer.toString(time);
  }
  
  public Date getDate()
    throws ParseException
  {
    String d = time;
    SimpleDateFormat dateF;
    if (time.endsWith("Z"))
    {
      SimpleDateFormat dateF;
      SimpleDateFormat dateF;
      if (hasFractionalSeconds()) {
        dateF = new SimpleDateFormat("yyyyMMddHHmmss.SSSS'Z'");
      } else {
        dateF = new SimpleDateFormat("yyyyMMddHHmmss'Z'");
      }
      dateF.setTimeZone(new SimpleTimeZone(0, "Z"));
    }
    else if ((time.indexOf('-') > 0) || (time.indexOf('+') > 0))
    {
      d = getTime();
      SimpleDateFormat dateF;
      SimpleDateFormat dateF;
      if (hasFractionalSeconds()) {
        dateF = new SimpleDateFormat("yyyyMMddHHmmss.SSSSz");
      } else {
        dateF = new SimpleDateFormat("yyyyMMddHHmmssz");
      }
      dateF.setTimeZone(new SimpleTimeZone(0, "Z"));
    }
    else
    {
      SimpleDateFormat dateF;
      if (hasFractionalSeconds()) {
        dateF = new SimpleDateFormat("yyyyMMddHHmmss.SSSS");
      } else {
        dateF = new SimpleDateFormat("yyyyMMddHHmmss");
      }
      dateF.setTimeZone(new SimpleTimeZone(0, TimeZone.getDefault().getID()));
    }
    return dateF.parse(d);
  }
  
  private boolean hasFractionalSeconds()
  {
    return time.indexOf('.') == 14;
  }
  
  private byte[] getOctets()
  {
    char[] cs = time.toCharArray();
    byte[] bs = new byte[cs.length];
    for (int i = 0; i != cs.length; i++) {
      bs[i] = ((byte)cs[i]);
    }
    return bs;
  }
  
  void encode(DEROutputStream out)
    throws IOException
  {
    out.writeEncoded(24, getOctets());
  }
  
  boolean asn1Equals(DERObject o)
  {
    if (!(o instanceof DERGeneralizedTime)) {
      return false;
    }
    return time.equals(time);
  }
  
  public int hashCode()
  {
    return time.hashCode();
  }
}

/* Location:
 * Qualified Name:     org.apache.commons.ssl.asn1.DERGeneralizedTime
 * Java Class Version: 1.2 (46.0)
 * JD-Core Version:    0.7.1
 */
package org.apache.commons.ssl.asn1;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public abstract class DERGenerator
  extends ASN1Generator
{
  private boolean _tagged = false;
  private boolean _isExplicit;
  private int _tagNo;
  
  protected DERGenerator(OutputStream out)
  {
    super(out);
  }
  
  public DERGenerator(OutputStream out, int tagNo, boolean isExplicit)
  {
    super(out);
    
    _tagged = true;
    _isExplicit = isExplicit;
    _tagNo = tagNo;
  }
  
  private void writeLength(OutputStream out, int length)
    throws IOException
  {
    if (length > 127)
    {
      int size = 1;
      int val = length;
      while (val >>>= 8 != 0) {
        size++;
      }
      out.write((byte)(size | 0x80));
      for (int i = (size - 1) * 8; i >= 0; i -= 8) {
        out.write((byte)(length >> i));
      }
    }
    else
    {
      out.write((byte)length);
    }
  }
  
  void writeDEREncoded(OutputStream out, int tag, byte[] bytes)
    throws IOException
  {
    out.write(tag);
    writeLength(out, bytes.length);
    out.write(bytes);
  }
  
  void writeDEREncoded(int tag, byte[] bytes)
    throws IOException
  {
    if (_tagged)
    {
      int tagNum = _tagNo | 0x80;
      if (_isExplicit)
      {
        int newTag = _tagNo | 0x20 | 0x80;
        
        ByteArrayOutputStream bOut = new ByteArrayOutputStream();
        
        writeDEREncoded(bOut, tag, bytes);
        
        writeDEREncoded(_out, newTag, bOut.toByteArray());
      }
      else if ((tag & 0x20) != 0)
      {
        writeDEREncoded(_out, tagNum | 0x20, bytes);
      }
      else
      {
        writeDEREncoded(_out, tagNum, bytes);
      }
    }
    else
    {
      writeDEREncoded(_out, tag, bytes);
    }
  }
  
  void writeDEREncoded(OutputStream out, int tag, InputStream in)
    throws IOException
  {
    out.write(tag);
    
    ByteArrayOutputStream bOut = new ByteArrayOutputStream();
    
    int b = 0;
    while ((b = in.read()) >= 0) {
      bOut.write(b);
    }
    byte[] bytes = bOut.toByteArray();
    
    writeLength(out, bytes.length);
    out.write(bytes);
  }
}

/* Location:
 * Qualified Name:     org.apache.commons.ssl.asn1.DERGenerator
 * Java Class Version: 1.2 (46.0)
 * JD-Core Version:    0.7.1
 */
package org.apache.commons.ssl.asn1;

import java.io.IOException;

public class DERIA5String
  extends ASN1Object
  implements DERString
{
  String string;
  
  public static DERIA5String getInstance(Object obj)
  {
    if ((obj == null) || ((obj instanceof DERIA5String))) {
      return (DERIA5String)obj;
    }
    if ((obj instanceof ASN1OctetString)) {
      return new DERIA5String(((ASN1OctetString)obj).getOctets());
    }
    if ((obj instanceof ASN1TaggedObject)) {
      return getInstance(((ASN1TaggedObject)obj).getObject());
    }
    throw new IllegalArgumentException("illegal object in getInstance: " + obj.getClass().getName());
  }
  
  public static DERIA5String getInstance(ASN1TaggedObject obj, boolean explicit)
  {
    return getInstance(obj.getObject());
  }
  
  public DERIA5String(byte[] string)
  {
    char[] cs = new char[string.length];
    for (int i = 0; i != cs.length; i++) {
      cs[i] = ((char)(string[i] & 0xFF));
    }
    this.string = new String(cs);
  }
  
  public DERIA5String(String string)
  {
    this(string, false);
  }
  
  public DERIA5String(String string, boolean validate)
  {
    if ((validate) && (!isIA5String(string))) {
      throw new IllegalArgumentException("string contains illegal characters");
    }
    this.string = string;
  }
  
  public String getString()
  {
    return string;
  }
  
  public String toString()
  {
    return string;
  }
  
  public byte[] getOctets()
  {
    char[] cs = string.toCharArray();
    byte[] bs = new byte[cs.length];
    for (int i = 0; i != cs.length; i++) {
      bs[i] = ((byte)cs[i]);
    }
    return bs;
  }
  
  void encode(DEROutputStream out)
    throws IOException
  {
    out.writeEncoded(22, getOctets());
  }
  
  public int hashCode()
  {
    return getString().hashCode();
  }
  
  boolean asn1Equals(DERObject o)
  {
    if (!(o instanceof DERIA5String)) {
      return false;
    }
    DERIA5String s = (DERIA5String)o;
    
    return getString().equals(s.getString());
  }
  
  public static boolean isIA5String(String str)
  {
    for (int i = str.length() - 1; i >= 0; i--)
    {
      char ch = str.charAt(i);
      if (ch > '') {
        return false;
      }
    }
    return true;
  }
}

/* Location:
 * Qualified Name:     org.apache.commons.ssl.asn1.DERIA5String
 * Java Class Version: 1.2 (46.0)
 * JD-Core Version:    0.7.1
 */
package org.apache.commons.ssl.asn1;

import java.io.ByteArrayInputStream;
import java.io.EOFException;
import java.io.FilterInputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * @deprecated
 */
public class DERInputStream
  extends FilterInputStream
  implements DERTags
{
  /**
   * @deprecated
   */
  public DERInputStream(InputStream is)
  {
    super(is);
  }
  
  protected int readLength()
    throws IOException
  {
    int length = read();
    if (length < 0) {
      throw new IOException("EOF found when length expected");
    }
    if (length == 128) {
      return -1;
    }
    if (length > 127)
    {
      int size = length & 0x7F;
      if (size > 4) {
        throw new IOException("DER length more than 4 bytes");
      }
      length = 0;
      for (int i = 0; i < size; i++)
      {
        int next = read();
        if (next < 0) {
          throw new IOException("EOF found reading length");
        }
        length = (length << 8) + next;
      }
      if (length < 0) {
        throw new IOException("corrupted stream - negative length found");
      }
    }
    return length;
  }
  
  protected void readFully(byte[] bytes)
    throws IOException
  {
    int left = bytes.length;
    if (left == 0) {
      return;
    }
    while (left > 0)
    {
      int l = read(bytes, bytes.length - left, left);
      if (l < 0) {
        throw new EOFException("unexpected end of stream");
      }
      left -= l;
    }
  }
  
  protected DERObject buildObject(int tag, byte[] bytes)
    throws IOException
  {
    switch (tag)
    {
    case 5: 
      return null;
    case 48: 
      ByteArrayInputStream bIn = new ByteArrayInputStream(bytes);
      BERInputStream dIn = new BERInputStream(bIn);
      DERConstructedSequence seq = new DERConstructedSequence();
      try
      {
        for (;;)
        {
          DERObject obj = dIn.readObject();
          
          seq.addObject(obj);
        }
      }
      catch (EOFException ex)
      {
        return seq;
      }
    case 49: 
      ByteArrayInputStream bIn = new ByteArrayInputStream(bytes);
      BERInputStream dIn = new BERInputStream(bIn);
      
      ASN1EncodableVector v = new ASN1EncodableVector();
      try
      {
        for (;;)
        {
          DERObject obj = dIn.readObject();
          
          v.add(obj);
        }
      }
      catch (EOFException ex)
      {
        return new DERConstructedSet(v);
      }
    case 1: 
      return new DERBoolean(bytes);
    case 2: 
      return new DERInteger(bytes);
    case 10: 
      return new DEREnumerated(bytes);
    case 6: 
      return new DERObjectIdentifier(bytes);
    case 3: 
      int padBits = bytes[0];
      byte[] data = new byte[bytes.length - 1];
      
      System.arraycopy(bytes, 1, data, 0, bytes.length - 1);
      
      return new DERBitString(data, padBits);
    case 12: 
      return new DERUTF8String(bytes);
    case 19: 
      return new DERPrintableString(bytes);
    case 22: 
      return new DERIA5String(bytes);
    case 20: 
      return new DERT61String(bytes);
    case 26: 
      return new DERVisibleString(bytes);
    case 28: 
      return new DERUniversalString(bytes);
    case 27: 
      return new DERGeneralString(bytes);
    case 30: 
      return new DERBMPString(bytes);
    case 4: 
      return new DEROctetString(bytes);
    case 23: 
      return new DERUTCTime(bytes);
    case 24: 
      return new DERGeneralizedTime(bytes);
    }
    if ((tag & 0x80) != 0)
    {
      if ((tag & 0x1F) == 31) {
        throw new IOException("unsupported high tag encountered");
      }
      if (bytes.length == 0)
      {
        if ((tag & 0x20) == 0) {
          return new DERTaggedObject(false, tag & 0x1F, new DERNull());
        }
        return new DERTaggedObject(false, tag & 0x1F, new DERConstructedSequence());
      }
      if ((tag & 0x20) == 0) {
        return new DERTaggedObject(false, tag & 0x1F, new DEROctetString(bytes));
      }
      ByteArrayInputStream bIn = new ByteArrayInputStream(bytes);
      BERInputStream dIn = new BERInputStream(bIn);
      
      DEREncodable dObj = dIn.readObject();
      if (dIn.available() == 0) {
        return new DERTaggedObject(tag & 0x1F, dObj);
      }
      DERConstructedSequence seq = new DERConstructedSequence();
      
      seq.addObject(dObj);
      try
      {
        for (;;)
        {
          dObj = dIn.readObject();
          
          seq.addObject(dObj);
        }
      }
      catch (EOFException ex)
      {
        return new DERTaggedObject(false, tag & 0x1F, seq);
      }
    }
    return new DERUnknownTag(tag, bytes);
  }
  
  public DERObject readObject()
    throws IOException
  {
    int tag = read();
    if (tag == -1) {
      throw new EOFException();
    }
    int length = readLength();
    byte[] bytes = new byte[length];
    
    readFully(bytes);
    
    return buildObject(tag, bytes);
  }
}

/* Location:
 * Qualified Name:     org.apache.commons.ssl.asn1.DERInputStream
 * Java Class Version: 1.2 (46.0)
 * JD-Core Version:    0.7.1
 */
package org.apache.commons.ssl.asn1;

import java.io.IOException;
import java.math.BigInteger;

public class DERInteger
  extends ASN1Object
{
  byte[] bytes;
  
  public static DERInteger getInstance(Object obj)
  {
    if ((obj == null) || ((obj instanceof DERInteger))) {
      return (DERInteger)obj;
    }
    if ((obj instanceof ASN1OctetString)) {
      return new DERInteger(((ASN1OctetString)obj).getOctets());
    }
    if ((obj instanceof ASN1TaggedObject)) {
      return getInstance(((ASN1TaggedObject)obj).getObject());
    }
    throw new IllegalArgumentException("illegal object in getInstance: " + obj.getClass().getName());
  }
  
  public static DERInteger getInstance(ASN1TaggedObject obj, boolean explicit)
  {
    return getInstance(obj.getObject());
  }
  
  public DERInteger(int value)
  {
    bytes = BigInteger.valueOf(value).toByteArray();
  }
  
  public DERInteger(BigInteger value)
  {
    bytes = value.toByteArray();
  }
  
  public DERInteger(byte[] bytes)
  {
    this.bytes = bytes;
  }
  
  public BigInteger getValue()
  {
    return new BigInteger(bytes);
  }
  
  public BigInteger getPositiveValue()
  {
    return new BigInteger(1, bytes);
  }
  
  void encode(DEROutputStream out)
    throws IOException
  {
    out.writeEncoded(2, bytes);
  }
  
  public int hashCode()
  {
    int value = 0;
    for (int i = 0; i != bytes.length; i++) {
      value ^= (bytes[i] & 0xFF) << i % 4;
    }
    return value;
  }
  
  boolean asn1Equals(DERObject o)
  {
    if (!(o instanceof DERInteger)) {
      return false;
    }
    DERInteger other = (DERInteger)o;
    if (bytes.length != bytes.length) {
      return false;
    }
    for (int i = 0; i != bytes.length; i++) {
      if (bytes[i] != bytes[i]) {
        return false;
      }
    }
    return true;
  }
  
  public String toString()
  {
    return getValue().toString();
  }
}

/* Location:
 * Qualified Name:     org.apache.commons.ssl.asn1.DERInteger
 * Java Class Version: 1.2 (46.0)
 * JD-Core Version:    0.7.1
 */
package org.apache.commons.ssl.asn1;

import java.io.IOException;

public class DERNull
  extends ASN1Null
{
  public static final DERNull INSTANCE = new DERNull();
  byte[] zeroBytes = new byte[0];
  
  void encode(DEROutputStream out)
    throws IOException
  {
    out.writeEncoded(5, zeroBytes);
  }
}

/* Location:
 * Qualified Name:     org.apache.commons.ssl.asn1.DERNull
 * Java Class Version: 1.2 (46.0)
 * JD-Core Version:    0.7.1
 */
package org.apache.commons.ssl.asn1;

import java.io.IOException;

public class DERNumericString
  extends ASN1Object
  implements DERString
{
  String string;
  
  public static DERNumericString getInstance(Object obj)
  {
    if ((obj == null) || ((obj instanceof DERNumericString))) {
      return (DERNumericString)obj;
    }
    if ((obj instanceof ASN1OctetString)) {
      return new DERNumericString(((ASN1OctetString)obj).getOctets());
    }
    if ((obj instanceof ASN1TaggedObject)) {
      return getInstance(((ASN1TaggedObject)obj).getObject());
    }
    throw new IllegalArgumentException("illegal object in getInstance: " + obj.getClass().getName());
  }
  
  public static DERNumericString getInstance(ASN1TaggedObject obj, boolean explicit)
  {
    return getInstance(obj.getObject());
  }
  
  public DERNumericString(byte[] string)
  {
    char[] cs = new char[string.length];
    for (int i = 0; i != cs.length; i++) {
      cs[i] = ((char)(string[i] & 0xFF));
    }
    this.string = new String(cs);
  }
  
  public DERNumericString(String string)
  {
    this(string, false);
  }
  
  public DERNumericString(String string, boolean validate)
  {
    if ((validate) && (!isNumericString(string))) {
      throw new IllegalArgumentException("string contains illegal characters");
    }
    this.string = string;
  }
  
  public String getString()
  {
    return string;
  }
  
  public String toString()
  {
    return string;
  }
  
  public byte[] getOctets()
  {
    char[] cs = string.toCharArray();
    byte[] bs = new byte[cs.length];
    for (int i = 0; i != cs.length; i++) {
      bs[i] = ((byte)cs[i]);
    }
    return bs;
  }
  
  void encode(DEROutputStream out)
    throws IOException
  {
    out.writeEncoded(18, getOctets());
  }
  
  public int hashCode()
  {
    return getString().hashCode();
  }
  
  boolean asn1Equals(DERObject o)
  {
    if (!(o instanceof DERNumericString)) {
      return false;
    }
    DERNumericString s = (DERNumericString)o;
    
    return getString().equals(s.getString());
  }
  
  public static boolean isNumericString(String str)
  {
    for (int i = str.length() - 1; i >= 0; i--)
    {
      char ch = str.charAt(i);
      if (ch > '') {
        return false;
      }
      if ((('0' > ch) || (ch > '9')) && (ch != ' ')) {
        return false;
      }
    }
    return true;
  }
}

/* Location:
 * Qualified Name:     org.apache.commons.ssl.asn1.DERNumericString
 * Java Class Version: 1.2 (46.0)
 * JD-Core Version:    0.7.1
 */
package org.apache.commons.ssl.asn1;

import java.io.IOException;

public abstract class DERObject
  extends ASN1Encodable
  implements DERTags
{
  public DERObject toASN1Object()
  {
    return this;
  }
  
  public abstract int hashCode();
  
  public abstract boolean equals(Object paramObject);
  
  abstract void encode(DEROutputStream paramDEROutputStream)
    throws IOException;
}

/* Location:
 * Qualified Name:     org.apache.commons.ssl.asn1.DERObject
 * Java Class Version: 1.2 (46.0)
 * JD-Core Version:    0.7.1
 */
package org.apache.commons.ssl.asn1;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.math.BigInteger;

public class DERObjectIdentifier
  extends ASN1Object
{
  String identifier;
  
  public static DERObjectIdentifier getInstance(Object obj)
  {
    if ((obj == null) || ((obj instanceof DERObjectIdentifier))) {
      return (DERObjectIdentifier)obj;
    }
    if ((obj instanceof ASN1OctetString)) {
      return new DERObjectIdentifier(((ASN1OctetString)obj).getOctets());
    }
    if ((obj instanceof ASN1TaggedObject)) {
      return getInstance(((ASN1TaggedObject)obj).getObject());
    }
    throw new IllegalArgumentException("illegal object in getInstance: " + obj.getClass().getName());
  }
  
  public static DERObjectIdentifier getInstance(ASN1TaggedObject obj, boolean explicit)
  {
    return getInstance(obj.getObject());
  }
  
  DERObjectIdentifier(byte[] bytes)
  {
    StringBuffer objId = new StringBuffer();
    long value = 0L;
    BigInteger bigValue = null;
    boolean first = true;
    for (int i = 0; i != bytes.length; i++)
    {
      int b = bytes[i] & 0xFF;
      if (value < 36028797018963968L)
      {
        value = value * 128L + (b & 0x7F);
        if ((b & 0x80) == 0)
        {
          if (first)
          {
            switch ((int)value / 40)
            {
            case 0: 
              objId.append('0');
              break;
            case 1: 
              objId.append('1');
              value -= 40L;
              break;
            default: 
              objId.append('2');
              value -= 80L;
            }
            first = false;
          }
          objId.append('.');
          objId.append(value);
          value = 0L;
        }
      }
      else
      {
        if (bigValue == null) {
          bigValue = BigInteger.valueOf(value);
        }
        bigValue = bigValue.shiftLeft(7);
        bigValue = bigValue.or(BigInteger.valueOf(b & 0x7F));
        if ((b & 0x80) == 0)
        {
          objId.append('.');
          objId.append(bigValue);
          bigValue = null;
          value = 0L;
        }
      }
    }
    identifier = objId.toString();
  }
  
  public DERObjectIdentifier(String identifier)
  {
    if (!isValidIdentifier(identifier)) {
      throw new IllegalArgumentException("string " + identifier + " not an OID");
    }
    this.identifier = identifier;
  }
  
  public String getId()
  {
    return identifier;
  }
  
  private void writeField(OutputStream out, long fieldValue)
    throws IOException
  {
    if (fieldValue >= 128L)
    {
      if (fieldValue >= 16384L)
      {
        if (fieldValue >= 2097152L)
        {
          if (fieldValue >= 268435456L)
          {
            if (fieldValue >= 34359738368L)
            {
              if (fieldValue >= 4398046511104L)
              {
                if (fieldValue >= 562949953421312L)
                {
                  if (fieldValue >= 72057594037927936L) {
                    out.write((int)(fieldValue >> 56) | 0x80);
                  }
                  out.write((int)(fieldValue >> 49) | 0x80);
                }
                out.write((int)(fieldValue >> 42) | 0x80);
              }
              out.write((int)(fieldValue >> 35) | 0x80);
            }
            out.write((int)(fieldValue >> 28) | 0x80);
          }
          out.write((int)(fieldValue >> 21) | 0x80);
        }
        out.write((int)(fieldValue >> 14) | 0x80);
      }
      out.write((int)(fieldValue >> 7) | 0x80);
    }
    out.write((int)fieldValue & 0x7F);
  }
  
  private void writeField(OutputStream out, BigInteger fieldValue)
    throws IOException
  {
    int byteCount = (fieldValue.bitLength() + 6) / 7;
    if (byteCount == 0)
    {
      out.write(0);
    }
    else
    {
      BigInteger tmpValue = fieldValue;
      byte[] tmp = new byte[byteCount];
      for (int i = byteCount - 1; i >= 0; i--)
      {
        tmp[i] = ((byte)(tmpValue.intValue() & 0x7F | 0x80));
        tmpValue = tmpValue.shiftRight(7);
      }
      int tmp79_78 = (byteCount - 1); byte[] tmp79_74 = tmp;tmp79_74[tmp79_78] = ((byte)(tmp79_74[tmp79_78] & 0x7F));
      out.write(tmp);
    }
  }
  
  void encode(DEROutputStream out)
    throws IOException
  {
    OIDTokenizer tok = new OIDTokenizer(identifier);
    ByteArrayOutputStream bOut = new ByteArrayOutputStream();
    DEROutputStream dOut = new DEROutputStream(bOut);
    
    writeField(bOut, Integer.parseInt(tok.nextToken()) * 40 + Integer.parseInt(tok.nextToken()));
    while (tok.hasMoreTokens())
    {
      String token = tok.nextToken();
      if (token.length() < 18) {
        writeField(bOut, Long.parseLong(token));
      } else {
        writeField(bOut, new BigInteger(token));
      }
    }
    dOut.close();
    
    byte[] bytes = bOut.toByteArray();
    
    out.writeEncoded(6, bytes);
  }
  
  public int hashCode()
  {
    return identifier.hashCode();
  }
  
  boolean asn1Equals(DERObject o)
  {
    if (!(o instanceof DERObjectIdentifier)) {
      return false;
    }
    return identifier.equals(identifier);
  }
  
  public String toString()
  {
    return getId();
  }
  
  private static boolean isValidIdentifier(String identifier)
  {
    if ((identifier.length() < 3) || (identifier.charAt(1) != '.')) {
      return false;
    }
    char first = identifier.charAt(0);
    if ((first < '0') || (first > '2')) {
      return false;
    }
    boolean periodAllowed = false;
    for (int i = identifier.length() - 1; i >= 2; i--)
    {
      char ch = identifier.charAt(i);
      if (('0' <= ch) && (ch <= '9'))
      {
        periodAllowed = true;
      }
      else if (ch == '.')
      {
        if (!periodAllowed) {
          return false;
        }
        periodAllowed = false;
      }
      else
      {
        return false;
      }
    }
    return periodAllowed;
  }
}

/* Location:
 * Qualified Name:     org.apache.commons.ssl.asn1.DERObjectIdentifier
 * Java Class Version: 1.2 (46.0)
 * JD-Core Version:    0.7.1
 */
package org.apache.commons.ssl.asn1;

import java.io.IOException;

public class DEROctetString
  extends ASN1OctetString
{
  public DEROctetString(byte[] string)
  {
    super(string);
  }
  
  public DEROctetString(DEREncodable obj)
  {
    super(obj);
  }
  
  void encode(DEROutputStream out)
    throws IOException
  {
    out.writeEncoded(4, string);
  }
}

/* Location:
 * Qualified Name:     org.apache.commons.ssl.asn1.DEROctetString
 * Java Class Version: 1.2 (46.0)
 * JD-Core Version:    0.7.1
 */
package org.apache.commons.ssl.asn1;

import java.io.FilterOutputStream;
import java.io.IOException;
import java.io.OutputStream;

public class DEROutputStream
  ex
1 2 3 4 5 6 7 8 9 10 11 12 13

Further reading...

For more information on Java 1.5 Tiger, you may find Java 1.5 Tiger, A developer's Notebook by D. Flanagan and B. McLaughlin from O'Reilly of interest.

New!JAR listings


Copyright 2006-2017. Infinite Loop Ltd