001/* PolicyQualifierInfo.java -- policy qualifier info object.
002   Copyright (C) 2003, 2004  Free Software Foundation, Inc.
003
004This file is part of GNU Classpath.
005
006GNU Classpath is free software; you can redistribute it and/or modify
007it under the terms of the GNU General Public License as published by
008the Free Software Foundation; either version 2, or (at your option)
009any later version.
010 
011GNU Classpath is distributed in the hope that it will be useful, but
012WITHOUT ANY WARRANTY; without even the implied warranty of
013MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014General Public License for more details.
015
016You should have received a copy of the GNU General Public License
017along with GNU Classpath; see the file COPYING.  If not, write to the
018Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
01902110-1301 USA.
020
021Linking this library statically or dynamically with other modules is
022making a combined work based on this library.  Thus, the terms and
023conditions of the GNU General Public License cover the whole
024combination.
025
026As a special exception, the copyright holders of this library give you
027permission to link this library with independent modules to produce an
028executable, regardless of the license terms of these independent
029modules, and to copy and distribute the resulting executable under
030terms of your choice, provided that you also meet, for each linked
031independent module, the terms and conditions of the license of that
032module.  An independent module is a module which is not derived from
033or based on this library.  If you modify this library, you may extend
034this exception to your version of the library, but you are not
035obligated to do so.  If you do not wish to do so, delete this
036exception statement from your version. */
037
038
039package java.security.cert;
040
041import gnu.java.io.ASN1ParsingException;
042import gnu.java.security.OID;
043import gnu.java.security.der.DERReader;
044import gnu.java.security.der.DERValue;
045
046import java.io.ByteArrayInputStream;
047import java.io.IOException;
048
049/**
050 * The PolicyQualifierInfo X.509 certificate extension.
051 * PolicyQualifierInfo objects are represented by the ASN.1 structure:
052 *
053 * <pre>
054 * PolicyQualifierInfo ::= SEQUENCE {
055 *    policyQualifierId   PolicyQualifierId,
056 *    qualifier           ANY DEFINED BY policyQualifierId
057 * }
058 *
059 * PolicyQualifierId ::= OBJECT IDENTIFIER
060 * </pre>
061 *
062 * @since 1.4
063 * @specnote this class was final in 1.4, but beginning with 1.5 is not
064 */
065public class PolicyQualifierInfo
066{
067
068  // Fields.
069  // ------------------------------------------------------------------------
070
071  /** The <code>policyQualifierId</code> field. */
072  private OID oid;
073
074  /** The DER encoded form of this object. */
075  private byte[] encoded;
076
077  /** The DER encoded form of the <code>qualifier</code> field. */
078  private DERValue qualifier;
079
080  // Constructor.
081  // ------------------------------------------------------------------------
082
083  /**
084   * Create a new PolicyQualifierInfo object from the DER encoded form
085   * passed in the byte array. The argument is copied.
086   *
087   * <p>The ASN.1 form of PolicyQualifierInfo is:
088<pre>
089PolicyQualifierInfo ::= SEQUENCE {
090   policyQualifierId     PolicyQualifierId,
091   qualifier             ANY DEFINED BY policyQualifierId
092}
093
094PolicyQualifierId ::= OBJECT IDENTIFIER
095</pre>
096   *
097   * @param encoded The DER encoded form.
098   * @throws IOException If the structure cannot be parsed from the
099   *         encoded bytes.
100   */
101  public PolicyQualifierInfo(byte[] encoded) throws IOException
102  {
103    if (encoded == null)
104      throw new IOException("null bytes");
105    this.encoded = (byte[]) encoded.clone();
106    DERReader in = new DERReader(new ByteArrayInputStream(this.encoded));
107    DERValue qualInfo = in.read();
108    if (!qualInfo.isConstructed())
109      throw new ASN1ParsingException("malformed PolicyQualifierInfo");
110    DERValue val = in.read();
111    if (!(val.getValue() instanceof OID))
112      throw new ASN1ParsingException("value read not an OBJECT IDENTIFIER");
113    oid = (OID) val.getValue();
114    if (val.getEncodedLength() < val.getLength())
115      qualifier = in.read();
116  }
117
118  // Instance methods.
119  // ------------------------------------------------------------------------
120
121  /**
122   * Returns the <code>policyQualifierId</code> field of this structure,
123   * as a dotted-decimal representation of the object identifier.
124   *
125   * @return This structure's OID field.
126   */
127  public final String getPolicyQualifierId()
128  {
129    return oid.toString();
130  }
131
132  /**
133   * Returns the DER encoded form of this object; the contents of the
134   * returned byte array are equivalent to those that were passed to the
135   * constructor. The byte array is cloned every time this method is
136   * called.
137   *
138   * @return The encoded form.
139   */
140  public final byte[] getEncoded()
141  {
142    return (byte[]) encoded.clone();
143  }
144
145  /**
146   * Get the <code>qualifier</code> field of this object, as a DER
147   * encoded byte array. The byte array returned is cloned every time
148   * this method is called.
149   *
150   * @return The encoded qualifier.
151   */
152  public final byte[] getPolicyQualifier()
153  {
154    if (qualifier == null)
155      return new byte[0];
156    return qualifier.getEncoded();
157  }
158
159  /**
160   * Returns a printable string representation of this object.
161   *
162   * @return The string representation.
163   */
164  public String toString()
165  {
166    return "PolicyQualifierInfo { policyQualifierId ::= " + oid
167      + ", qualifier ::= " + qualifier + " }";
168  }
169}