home *** CD-ROM | disk | FTP | other *** search
/ PC Pro 1999 April / DPPCPRO0499.ISO / April / Notes / 50b2wic.exe / DATA1.CAB / NotesProgramFilesJavaSupport / rt.jar / sun / security / util / DerInputStream.class (.txt) < prev    next >
Encoding:
Java Class File  |  1998-04-23  |  5.2 KB  |  262 lines

  1. package sun.security.util;
  2.  
  3. import java.io.IOException;
  4. import java.io.InputStream;
  5. import java.util.Date;
  6. import java.util.Vector;
  7.  
  8. public class DerInputStream {
  9.    DerInputBuffer buffer;
  10.  
  11.    public DerInputStream(byte[] var1) {
  12.       this.buffer = new DerInputBuffer(var1);
  13.       this.buffer.mark(Integer.MAX_VALUE);
  14.    }
  15.  
  16.    public DerInputStream(byte[] var1, int var2, int var3) {
  17.       this.buffer = new DerInputBuffer(var1, var2, var3);
  18.       this.buffer.mark(Integer.MAX_VALUE);
  19.    }
  20.  
  21.    DerInputStream(DerInputBuffer var1) {
  22.       this.buffer = var1;
  23.       this.buffer.mark(Integer.MAX_VALUE);
  24.    }
  25.  
  26.    public DerInputStream subStream(int var1, boolean var2) throws IOException {
  27.       DerInputBuffer var3 = this.buffer.dup();
  28.       var3.truncate(var1);
  29.       if (var2) {
  30.          this.buffer.skip((long)var1);
  31.       }
  32.  
  33.       return new DerInputStream(var3);
  34.    }
  35.  
  36.    public BigInt getInteger() throws IOException {
  37.       if (this.buffer.read() != 2) {
  38.          throw new IOException("DER input, Integer tag error");
  39.       } else {
  40.          return this.buffer.getUnsigned(getLength(this.buffer));
  41.       }
  42.    }
  43.  
  44.    public byte[] getBitString() throws IOException {
  45.       if (this.buffer.read() != 3) {
  46.          throw new IOException("DER input not an bit string");
  47.       } else {
  48.          int var2 = getLength(this.buffer);
  49.          if (this.buffer.read() != 0) {
  50.             return null;
  51.          } else {
  52.             --var2;
  53.             byte[] var1 = new byte[var2];
  54.             if (this.buffer.read(var1) != var2) {
  55.                throw new IOException("short read of DER bit string");
  56.             } else {
  57.                return var1;
  58.             }
  59.          }
  60.       }
  61.    }
  62.  
  63.    public byte[] getOctetString() throws IOException {
  64.       if (this.buffer.read() != 4) {
  65.          throw new IOException("DER input not an octet string");
  66.       } else {
  67.          int var2 = getLength(this.buffer);
  68.          byte[] var1 = new byte[var2];
  69.          if (this.buffer.read(var1) != var2) {
  70.             throw new IOException("short read of DER octet string");
  71.          } else {
  72.             return var1;
  73.          }
  74.       }
  75.    }
  76.  
  77.    public void getNull() throws IOException {
  78.       if (this.buffer.read() != 5 || this.buffer.read() != 0) {
  79.          throw new IOException("getNull, bad data");
  80.       }
  81.    }
  82.  
  83.    public ObjectIdentifier getOID() throws IOException {
  84.       return new ObjectIdentifier(this);
  85.    }
  86.  
  87.    public DerValue[] getSequence(int var1) throws IOException {
  88.       if (this.buffer.read() != 48) {
  89.          throw new IOException("Sequence tag error");
  90.       } else {
  91.          return this.readVector(var1);
  92.       }
  93.    }
  94.  
  95.    public DerValue[] getSet(int var1) throws IOException {
  96.       if (this.buffer.read() != 49) {
  97.          throw new IOException("Set tag error");
  98.       } else {
  99.          return this.readVector(var1);
  100.       }
  101.    }
  102.  
  103.    protected DerValue[] readVector(int var1) throws IOException {
  104.       int var2 = getLength(this.buffer);
  105.       if (var2 == 0) {
  106.          return null;
  107.       } else {
  108.          DerInputStream var3;
  109.          if (this.buffer.available() == var2) {
  110.             var3 = this;
  111.          } else {
  112.             var3 = this.subStream(var2, true);
  113.          }
  114.  
  115.          Vector var4 = new Vector(var1, 5);
  116.  
  117.          do {
  118.             DerValue var5 = new DerValue(var3.buffer);
  119.             var4.addElement(var5);
  120.          } while(var3.available() > 0);
  121.  
  122.          if (var3.available() != 0) {
  123.             throw new IOException("extra data at end of vector");
  124.          } else {
  125.             int var7 = var4.size();
  126.             DerValue[] var8 = new DerValue[var7];
  127.  
  128.             for(int var6 = 0; var6 < var7; ++var6) {
  129.                var8[var6] = (DerValue)var4.elementAt(var6);
  130.             }
  131.  
  132.             return var8;
  133.          }
  134.       }
  135.    }
  136.  
  137.    public DerValue getDerValue() throws IOException {
  138.       return new DerValue(this.buffer);
  139.    }
  140.  
  141.    public Date getUTCTime() throws IOException {
  142.       if (this.buffer.read() != 23) {
  143.          throw new IOException("DER input, UTCtime tag invalid ");
  144.       } else if (this.buffer.available() < 12) {
  145.          throw new IOException("DER input, UTCtime short input");
  146.       } else {
  147.          int var1 = getLength(this.buffer);
  148.          if (var1 >= 11 && var1 <= 17) {
  149.             int var2 = 10 * Character.digit((char)this.buffer.read(), 10);
  150.             var2 += Character.digit((char)this.buffer.read(), 10);
  151.             if (var2 < 80) {
  152.                var2 += 100;
  153.             }
  154.  
  155.             int var3 = 10 * Character.digit((char)this.buffer.read(), 10);
  156.             var3 += Character.digit((char)this.buffer.read(), 10);
  157.             --var3;
  158.             int var4 = 10 * Character.digit((char)this.buffer.read(), 10);
  159.             var4 += Character.digit((char)this.buffer.read(), 10);
  160.             int var5 = 10 * Character.digit((char)this.buffer.read(), 10);
  161.             var5 += Character.digit((char)this.buffer.read(), 10);
  162.             int var6 = 10 * Character.digit((char)this.buffer.read(), 10);
  163.             var6 += Character.digit((char)this.buffer.read(), 10);
  164.             var1 -= 10;
  165.             int var19;
  166.             if (var1 != 3 && var1 != 7) {
  167.                var19 = 0;
  168.             } else {
  169.                var19 = 10 * Character.digit((char)this.buffer.read(), 10);
  170.                var19 += Character.digit((char)this.buffer.read(), 10);
  171.                var1 -= 2;
  172.             }
  173.  
  174.             if (var3 >= 0 && var4 > 0 && var3 <= 11 && var4 <= 31 && var5 < 24 && var6 < 60 && var19 < 60) {
  175.                long var8 = Date.UTC(var2, var3, var4, var5, var6, var19);
  176.                if (var1 != 1 && var1 != 5) {
  177.                   throw new IOException("Parse UTC time, invalid offset");
  178.                } else {
  179.                   switch (this.buffer.read()) {
  180.                      case 43:
  181.                         int var21 = 10 * Character.digit((char)this.buffer.read(), 10);
  182.                         var21 += Character.digit((char)this.buffer.read(), 10);
  183.                         int var24 = 10 * Character.digit((char)this.buffer.read(), 10);
  184.                         var24 += Character.digit((char)this.buffer.read(), 10);
  185.                         if (var21 >= 24 || var24 >= 60) {
  186.                            throw new IOException("Parse UTCtime, +hhmm");
  187.                         }
  188.  
  189.                         var8 += (long)((var21 * 60 + var24) * 60 * 1000);
  190.                         break;
  191.                      case 45:
  192.                         int var10 = 10 * Character.digit((char)this.buffer.read(), 10);
  193.                         var10 += Character.digit((char)this.buffer.read(), 10);
  194.                         int var11 = 10 * Character.digit((char)this.buffer.read(), 10);
  195.                         var11 += Character.digit((char)this.buffer.read(), 10);
  196.                         if (var10 >= 24 || var11 >= 60) {
  197.                            throw new IOException("Parse UTCtime, -hhmm");
  198.                         }
  199.  
  200.                         var8 -= (long)((var10 * 60 + var11) * 60 * 1000);
  201.                      case 90:
  202.                         break;
  203.                      default:
  204.                         throw new IOException("Parse UTCtime, garbage offset");
  205.                   }
  206.  
  207.                   return new Date(var8);
  208.                }
  209.             } else {
  210.                throw new IOException("Parse UTC time, invalid format");
  211.             }
  212.          } else {
  213.             throw new IOException("DER getUTCTime length error");
  214.          }
  215.       }
  216.    }
  217.  
  218.    int getByte() throws IOException {
  219.       return 255 & this.buffer.read();
  220.    }
  221.  
  222.    int peekByte() throws IOException {
  223.       return this.buffer.peek();
  224.    }
  225.  
  226.    int getLength() throws IOException {
  227.       return getLength(this.buffer);
  228.    }
  229.  
  230.    static int getLength(InputStream var0) throws IOException {
  231.       int var2 = var0.read();
  232.       int var1;
  233.       if ((var2 & 128) == 0) {
  234.          var1 = var2;
  235.       } else {
  236.          var2 &= 127;
  237.          if (var2 <= 0 || var2 > 4) {
  238.             throw new IOException("DerInput.getLength(): lengthTag=" + var2 + ", " + (var2 == 0 ? "Indefinite length encoding not supported or incorrect DER encoding." : "too big."));
  239.          }
  240.  
  241.          for(var1 = 0; var2 > 0; --var2) {
  242.             var1 <<= 8;
  243.             var1 += 255 & var0.read();
  244.          }
  245.       }
  246.  
  247.       return var1;
  248.    }
  249.  
  250.    public void mark(int var1) {
  251.       this.buffer.mark(var1);
  252.    }
  253.  
  254.    public void reset() {
  255.       this.buffer.reset();
  256.    }
  257.  
  258.    public int available() {
  259.       return this.buffer.available();
  260.    }
  261. }
  262.