Exemplo n.º 1
0
  /**
   * Produce an object suitable for an ASN1OutputStream.
   *
   * <pre>
   * IDEA-CBCPar ::= SEQUENCE {
   *                      iv    OCTET STRING OPTIONAL -- exactly 8 octets
   *                  }
   * </pre>
   */
  public DERObject getDERObject() {
    ASN1EncodableVector v = new ASN1EncodableVector();

    if (iv != null) {
      v.add(iv);
    }

    return new DERSequence(v);
  }
  /**
   * Produce an object suitable for an ASN1OutputStream.
   *
   * <pre>
   * ContentInfo ::= SEQUENCE {
   *          contentType ContentType,
   *          content
   *          [0] EXPLICIT ANY DEFINED BY contentType OPTIONAL }
   * </pre>
   */
  public DERObject toASN1Object() {
    ASN1EncodableVector v = new ASN1EncodableVector();

    v.add(contentType);

    if (content != null) {
      v.add(new BERTaggedObject(0, content));
    }

    return new BERSequence(v);
  }
  public DERApplicationSpecific(int tagNo, ASN1EncodableVector vec) {
    this.tag = tagNo;
    this.isConstructed = true;
    ByteArrayOutputStream bOut = new ByteArrayOutputStream();

    for (int i = 0; i != vec.size(); i++) {
      try {
        bOut.write(((ASN1Encodable) vec.get(i)).getEncoded());
      } catch (IOException e) {
        throw new ASN1ParsingException("malformed object: " + e, e);
      }
    }
    this.octets = bOut.toByteArray();
  }
Exemplo n.º 4
0
  /**
   * Produce an object suitable for an ASN1OutputStream.
   *
   * <pre>
   * CrlID ::= SEQUENCE {
   *     crlUrl               [0]     EXPLICIT IA5String OPTIONAL,
   *     crlNum               [1]     EXPLICIT INTEGER OPTIONAL,
   *     crlTime              [2]     EXPLICIT GeneralizedTime OPTIONAL }
   * </pre>
   */
  public DERObject toASN1Object() {
    ASN1EncodableVector v = new ASN1EncodableVector();

    if (crlUrl != null) {
      v.add(new DERTaggedObject(true, 0, crlUrl));
    }

    if (crlNum != null) {
      v.add(new DERTaggedObject(true, 1, crlNum));
    }

    if (crlTime != null) {
      v.add(new DERTaggedObject(true, 2, crlTime));
    }

    return new DERSequence(v);
  }
  /**
   * Return an ASN1 set from a tagged object. There is a special case here, if an object appears to
   * have been explicitly tagged on reading but we were expecting it to be implicitly tagged in the
   * normal course of events it indicates that we lost the surrounding set - so we need to add it
   * back (this will happen if the tagged object is a sequence that contains other sequences). If
   * you are dealing with implicitly tagged sets you really <b>should</b> be using this method.
   *
   * @param obj the tagged object.
   * @param explicit true if the object is meant to be explicitly tagged false otherwise.
   * @exception IllegalArgumentException if the tagged object cannot be converted.
   */
  public static ASN1Set getInstance(ASN1TaggedObject obj, boolean explicit) {
    if (explicit) {
      if (!obj.isExplicit()) {
        throw new IllegalArgumentException("object implicit - explicit expected.");
      }

      return (ASN1Set) obj.getObject();
    } else {
      //
      // constructed object which appears to be explicitly tagged
      // and it's really implicit means we have to add the
      // surrounding sequence.
      //
      if (obj.isExplicit()) {
        ASN1Set set = new DERSet(obj.getObject());

        return set;
      } else {
        if (obj.getObject() instanceof ASN1Set) {
          return (ASN1Set) obj.getObject();
        }

        //
        // in this case the parser returns a sequence, convert it
        // into a set.
        //
        ASN1EncodableVector v = new ASN1EncodableVector();

        if (obj.getObject() instanceof ASN1Sequence) {
          ASN1Sequence s = (ASN1Sequence) obj.getObject();
          Enumeration e = s.getObjects();

          while (e.hasMoreElements()) {
            v.add((DEREncodable) e.nextElement());
          }

          return new DERSet(v, false);
        }
      }
    }

    throw new IllegalArgumentException(
        "unknown object in getInstance: " + obj.getClass().getName());
  }
Exemplo n.º 6
0
  /** build an object given its tag and a byte stream to construct it from. */
  protected DERObject buildObject(int tag, byte[] bytes) throws IOException {
    switch (tag) {
      case NULL:
        return null;
      case SEQUENCE | CONSTRUCTED:
        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 SET | CONSTRUCTED:
        bIn = new ByteArrayInputStream(bytes);
        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 BOOLEAN:
        return new DERBoolean(bytes);
      case INTEGER:
        return new DERInteger(bytes);
      case ENUMERATED:
        return new DEREnumerated(bytes);
      case OBJECT_IDENTIFIER:
        return new DERObjectIdentifier(bytes);
      case BIT_STRING:
        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 UTF8_STRING:
        return new DERUTF8String(bytes);
      case PRINTABLE_STRING:
        return new DERPrintableString(bytes);
      case IA5_STRING:
        return new DERIA5String(bytes);
      case T61_STRING:
        return new DERT61String(bytes);
      case VISIBLE_STRING:
        return new DERVisibleString(bytes);
      case UNIVERSAL_STRING:
        return new DERUniversalString(bytes);
      case GENERAL_STRING:
        return new DERGeneralString(bytes);
      case BMP_STRING:
        return new DERBMPString(bytes);
      case OCTET_STRING:
        return new DEROctetString(bytes);
      case UTC_TIME:
        return new DERUTCTime(bytes);
      case GENERALIZED_TIME:
        return new DERGeneralizedTime(bytes);
      default:
        //
        // with tagged object tag number is bottom 5 bits
        //
        if ((tag & TAGGED) != 0) {
          if ((tag & 0x1f) == 0x1f) {
            throw new IOException("unsupported high tag encountered");
          }

          if (bytes.length == 0) // empty tag!
          {
            if ((tag & CONSTRUCTED) == 0) {
              return new DERTaggedObject(false, tag & 0x1f, new DERNull());
            } else {
              return new DERTaggedObject(false, tag & 0x1f, new DERConstructedSequence());
            }
          }

          //
          // simple type - implicit... return an octet string
          //
          if ((tag & CONSTRUCTED) == 0) {
            return new DERTaggedObject(false, tag & 0x1f, new DEROctetString(bytes));
          }

          bIn = new ByteArrayInputStream(bytes);
          dIn = new BERInputStream(bIn);

          DEREncodable dObj = dIn.readObject();

          //
          // explicitly tagged (probably!) - if it isn't we'd have to
          // tell from the context
          //
          if (dIn.available() == 0) {
            return new DERTaggedObject(tag & 0x1f, dObj);
          }

          //
          // another implicit object, we'll create a sequence...
          //
          seq = new DERConstructedSequence();

          seq.addObject(dObj);

          try {
            for (; ; ) {
              dObj = dIn.readObject();

              seq.addObject(dObj);
            }
          } catch (EOFException ex) {
            // ignore --
          }

          return new DERTaggedObject(false, tag & 0x1f, seq);
        }

        return new DERUnknownTag(tag, bytes);
    }
  }
Exemplo n.º 7
0
 /** create a sequence containing a vector of objects. */
 public DERSequence(ASN1EncodableVector v) {
   for (int i = 0; i != v.size(); i++) {
     this.addObject(v.get(i));
   }
 }