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

CA认证

开发平台:

Java

  1. package org.bouncycastle.asn1;
  2. import java.math.BigInteger;
  3. import java.io.*;
  4. import java.util.*;
  5. public class BERInputStream
  6.     extends DERInputStream
  7. {
  8. private DERObject END_OF_STREAM = new DERObject() {
  9. void encode(
  10. DEROutputStream out)
  11. throws IOException
  12. {
  13. throw new IOException("Eeek!");
  14. }
  15. };
  16.     public BERInputStream(
  17.         InputStream is)
  18.     {
  19.         super(is);
  20.     }
  21.     /**
  22.      * read a string of bytes representing an indefinite length object.
  23.      */
  24.     private byte[] readIndefiniteLengthFully()
  25.         throws IOException
  26.     {
  27.         ByteArrayOutputStream   bOut = new ByteArrayOutputStream();
  28.         int                     b, b1;
  29.         b1 = read();
  30.         while ((b = read()) >= 0)
  31.         {
  32. if (b1 == 0 && b == 0)
  33. {
  34. break;
  35. }
  36.             bOut.write(b1);
  37.             b1 = b;
  38.         }
  39.         return bOut.toByteArray();
  40.     }
  41. private BERConstructedOctetString buildConstructedOctetString()
  42. throws IOException
  43. {
  44.         Vector                  octs = new Vector();
  45. for (;;)
  46. {
  47. DERObject o = readObject();
  48. if (o == END_OF_STREAM)
  49. {
  50. break;
  51. }
  52.             octs.addElement(o);
  53. }
  54. return new BERConstructedOctetString(octs);
  55. }
  56.     public DERObject readObject()
  57.         throws IOException
  58.     {
  59.         int tag = read();
  60.         if (tag == -1)
  61.         {
  62.             throw new EOFException();
  63.         }
  64.     
  65.         int     length = readLength();
  66.         if (length < 0)    // indefinite length method
  67.         {
  68.             switch (tag)
  69.             {
  70.             case NULL:
  71.                 return null;
  72.             case SEQUENCE | CONSTRUCTED:
  73.                 BERConstructedSequence  seq = new BERConstructedSequence();
  74.     
  75. for (;;)
  76. {
  77. DERObject   obj = readObject();
  78. if (obj == END_OF_STREAM)
  79. {
  80. break;
  81. }
  82. seq.addObject(obj);
  83. }
  84. return seq;
  85.             case OCTET_STRING | CONSTRUCTED:
  86. return buildConstructedOctetString();
  87.             case SET | CONSTRUCTED:
  88.                 ASN1EncodableVector  v = new ASN1EncodableVector();
  89.     
  90. for (;;)
  91. {
  92. DERObject   obj = readObject();
  93. if (obj == END_OF_STREAM)
  94. {
  95. break;
  96. }
  97. v.add(obj);
  98. }
  99. return new BERSet(v);
  100.             default:
  101.                 //
  102.                 // with tagged object tag number is bottom 5 bits
  103.                 //
  104.                 if ((tag & TAGGED) != 0)  
  105.                 {
  106.                     if ((tag & 0x1f) == 0x1f)
  107.                     {
  108.                         throw new IOException("unsupported high tag encountered");
  109.                     }
  110.                     //
  111.                     // simple type - implicit... return an octet string
  112.                     //
  113.                     if ((tag & CONSTRUCTED) == 0)
  114.                     {
  115.                         byte[]  bytes = readIndefiniteLengthFully();
  116.                         return new BERTaggedObject(false, tag & 0x1f, new DEROctetString(bytes));
  117.                     }
  118.                     //
  119.                     // either constructed or explicitly tagged
  120.                     //
  121. DERObject dObj = readObject();
  122. if (dObj == END_OF_STREAM)     // empty tag!
  123.                     {
  124.                         return new DERTaggedObject(tag & 0x1f);
  125.                     }
  126.                     DERObject       next = readObject();
  127.                     //
  128.                     // explicitly tagged (probably!) - if it isn't we'd have to
  129.                     // tell from the context
  130.                     //
  131.                     if (next == END_OF_STREAM)
  132.                     {
  133.                         return new BERTaggedObject(tag & 0x1f, dObj);
  134.                     }
  135.                     //
  136.                     // another implicit object, we'll create a sequence...
  137.                     //
  138.                     seq = new BERConstructedSequence();
  139.                     seq.addObject(dObj);
  140.                     do
  141.                     {
  142.                         seq.addObject(next);
  143.                         next = readObject();
  144.                     }
  145.                     while (next != END_OF_STREAM);
  146.                     return new BERTaggedObject(false, tag & 0x1f, seq);
  147.                 }
  148.                 throw new IOException("unknown BER object encountered");
  149.             }
  150.         }
  151.         else
  152.         {
  153.             if (tag == 0 && length == 0)    // end of contents marker.
  154.             {
  155.                 return END_OF_STREAM;
  156.             }
  157.             byte[]  bytes = new byte[length];
  158.     
  159.             readFully(bytes);
  160.     
  161. return buildObject(tag, bytes);
  162.         }
  163.     }
  164. }