DERInputStream.java
上传用户:lior1029
上传日期:2013-05-07
资源大小:209k
文件大小:6k
源码类别:

CA认证

开发平台:

Java

  1. package org.bouncycastle.asn1;
  2. import java.math.BigInteger;
  3. import java.io.FilterInputStream;
  4. import java.io.InputStream;
  5. import java.io.ByteArrayInputStream;
  6. import java.io.IOException;
  7. import java.io.EOFException;
  8. public class DERInputStream
  9.     extends FilterInputStream implements DERTags
  10. {
  11.     public DERInputStream(
  12.         InputStream is)
  13.     {
  14.         super(is);
  15.     }
  16.     protected int readLength()
  17.         throws IOException
  18.     {
  19.         int length = read();
  20.         if (length < 0)
  21.         {
  22.             throw new IOException("EOF found when length expected");
  23.         }
  24.         if (length == 0x80)
  25.         {
  26.             return -1;      // indefinite-length encoding
  27.         }
  28.         if (length > 127)
  29.         {
  30.             int size = length & 0x7f;
  31.             length = 0;
  32.             for (int i = 0; i < size; i++)
  33.             {
  34.                 int next = read();
  35.                 if (next < 0)
  36.                 {
  37.                     throw new IOException("EOF found reading length");
  38.                 }
  39.                 length = (length << 8) + next;
  40.             }
  41.         }
  42.         return length;
  43.     }
  44.     protected void readFully(
  45.         byte[]  bytes)
  46.         throws IOException
  47.     {
  48.         int     left = bytes.length;
  49.         if (left == 0)
  50.         {
  51.             return;
  52.         }
  53.         while ((left -= read(bytes, bytes.length - left, left)) != 0)
  54.         {
  55.             ;
  56.         }
  57.     }
  58. /**
  59.  * build an object given its tag and a byte stream to construct it
  60.  * from.
  61.  */
  62.     protected DERObject buildObject(
  63. int     tag,
  64. byte[] bytes)
  65. throws IOException
  66. {
  67. switch (tag)
  68.         {
  69.         case NULL:
  70.             return null;   
  71.         case SEQUENCE | CONSTRUCTED:
  72.             ByteArrayInputStream    bIn = new ByteArrayInputStream(bytes);
  73.             BERInputStream          dIn = new BERInputStream(bIn);
  74.             DERConstructedSequence  seq = new DERConstructedSequence();
  75.             try
  76.             {
  77.                 for (;;)
  78.                 {
  79.                     DERObject   obj = dIn.readObject();
  80.                     seq.addObject(obj);
  81.                 }
  82.             }
  83.             catch (EOFException ex)
  84.             {
  85.                 return seq;
  86.             }
  87.         case SET | CONSTRUCTED:
  88.             bIn = new ByteArrayInputStream(bytes);
  89.             dIn = new BERInputStream(bIn);
  90.             ASN1EncodableVector    v = new ASN1EncodableVector();
  91.             try
  92.             {
  93.                 for (;;)
  94.                 {
  95.                     DERObject   obj = dIn.readObject();
  96.                     v.add(obj);
  97.                 }
  98.             }
  99.             catch (EOFException ex)
  100.             {
  101.                 return new DERConstructedSet(v);
  102.             }
  103.         case BOOLEAN:
  104.             return new DERBoolean(bytes);
  105.         case INTEGER:
  106.             return new DERInteger(bytes);
  107.         case ENUMERATED:
  108.             return new DEREnumerated(bytes);
  109.         case OBJECT_IDENTIFIER:
  110.             return new DERObjectIdentifier(bytes);
  111.         case BIT_STRING:
  112.             int     padBits = bytes[0];
  113.             byte[]  data = new byte[bytes.length - 1];
  114.             System.arraycopy(bytes, 1, data, 0, bytes.length - 1);
  115.             return new DERBitString(data, padBits);
  116.         case UTF8_STRING:
  117.             return new DERUTF8String(bytes);
  118.         case PRINTABLE_STRING:
  119.             return new DERPrintableString(bytes);
  120.         case IA5_STRING:
  121.             return new DERIA5String(bytes);
  122.         case T61_STRING:
  123.             return new DERT61String(bytes);
  124.         case VISIBLE_STRING:
  125.             return new DERVisibleString(bytes);
  126.         case UNIVERSAL_STRING:
  127.             return new DERUniversalString(bytes);
  128.         case BMP_STRING:
  129.             return new DERBMPString(bytes);
  130.         case OCTET_STRING:
  131.             return new DEROctetString(bytes);
  132.         case UTC_TIME:
  133.             return new DERUTCTime(bytes);
  134.         case GENERALIZED_TIME:
  135.             return new DERGeneralizedTime(bytes);
  136.         default:
  137.             //
  138.             // with tagged object tag number is bottom 5 bits
  139.             //
  140.             if ((tag & TAGGED) != 0)  
  141.             {
  142.                 if ((tag & 0x1f) == 0x1f)
  143.                 {
  144.                     throw new IOException("unsupported high tag encountered");
  145.                 }
  146.                 if (bytes.length == 0)        // empty tag!
  147.                 {
  148.                     return new DERTaggedObject(false, tag & 0x1f, new DERConstructedSequence());
  149.                 }
  150.                 //
  151.                 // simple type - implicit... return an octet string
  152.                 //
  153.                 if ((tag & CONSTRUCTED) == 0)
  154.                 {
  155.                     return new DERTaggedObject(false, tag & 0x1f, new DEROctetString(bytes));
  156.                 }
  157.                 bIn = new ByteArrayInputStream(bytes);
  158.                 dIn = new BERInputStream(bIn);
  159.                 DEREncodable dObj = dIn.readObject();
  160.                 //
  161.                 // explicitly tagged (probably!) - if it isn't we'd have to
  162.                 // tell from the context
  163.                 //
  164.                 if (dIn.available() == 0)
  165.                 {
  166.                     return new DERTaggedObject(tag & 0x1f, dObj);
  167.                 }
  168.                 //
  169.                 // another implicit object, we'll create a sequence...
  170.                 //
  171.                 seq = new DERConstructedSequence();
  172.                 seq.addObject(dObj);
  173.                 try
  174.                 {
  175.                     for (;;)
  176.                     {
  177.                         dObj = dIn.readObject();
  178.                         seq.addObject(dObj);
  179.                     }
  180.                 }
  181.                 catch (EOFException ex)
  182.                 {
  183.                     // ignore --
  184.                 }
  185.                 return new DERTaggedObject(false, tag & 0x1f, seq);
  186.             }
  187.             return new DERUnknownTag(tag, bytes);
  188.         }
  189. }
  190.     public DERObject readObject()
  191.         throws IOException
  192.     {
  193.         int tag = read();
  194.         if (tag == -1)
  195.         {
  196.             throw new EOFException();
  197.         }
  198.         int     length = readLength();
  199.         byte[]  bytes = new byte[length];
  200.         readFully(bytes);
  201. return buildObject(tag, bytes);
  202. }
  203. }