Source for java.security.cert.PolicyQualifierInfo

   1: /* PolicyQualifierInfo.java -- policy qualifier info object.
   2:    Copyright (C) 2003, 2004  Free Software Foundation, Inc.
   3: 
   4: This file is part of GNU Classpath.
   5: 
   6: GNU Classpath is free software; you can redistribute it and/or modify
   7: it under the terms of the GNU General Public License as published by
   8: the Free Software Foundation; either version 2, or (at your option)
   9: any later version.
  10:  
  11: GNU Classpath is distributed in the hope that it will be useful, but
  12: WITHOUT ANY WARRANTY; without even the implied warranty of
  13: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14: General Public License for more details.
  15: 
  16: You should have received a copy of the GNU General Public License
  17: along with GNU Classpath; see the file COPYING.  If not, write to the
  18: Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  19: 02110-1301 USA.
  20: 
  21: Linking this library statically or dynamically with other modules is
  22: making a combined work based on this library.  Thus, the terms and
  23: conditions of the GNU General Public License cover the whole
  24: combination.
  25: 
  26: As a special exception, the copyright holders of this library give you
  27: permission to link this library with independent modules to produce an
  28: executable, regardless of the license terms of these independent
  29: modules, and to copy and distribute the resulting executable under
  30: terms of your choice, provided that you also meet, for each linked
  31: independent module, the terms and conditions of the license of that
  32: module.  An independent module is a module which is not derived from
  33: or based on this library.  If you modify this library, you may extend
  34: this exception to your version of the library, but you are not
  35: obligated to do so.  If you do not wish to do so, delete this
  36: exception statement from your version. */
  37: 
  38: 
  39: package java.security.cert;
  40: 
  41: import gnu.java.io.ASN1ParsingException;
  42: import gnu.java.security.OID;
  43: import gnu.java.security.der.DERReader;
  44: import gnu.java.security.der.DERValue;
  45: 
  46: import java.io.ByteArrayInputStream;
  47: import java.io.IOException;
  48: 
  49: /**
  50:  * The PolicyQualifierInfo X.509 certificate extension.
  51:  * PolicyQualifierInfo objects are represented by the ASN.1 structure:
  52:  *
  53:  * <pre>
  54:  * PolicyQualifierInfo ::= SEQUENCE {
  55:  *    policyQualifierId   PolicyQualifierId,
  56:  *    qualifier           ANY DEFINED BY policyQualifierId
  57:  * }
  58:  *
  59:  * PolicyQualifierId ::= OBJECT IDENTIFIER
  60:  * </pre>
  61:  *
  62:  * @since JDK 1.4
  63:  */
  64: public final class PolicyQualifierInfo
  65: {
  66: 
  67:   // Fields.
  68:   // ------------------------------------------------------------------------
  69: 
  70:   /** The <code>policyQualifierId</code> field. */
  71:   private OID oid;
  72: 
  73:   /** The DER encoded form of this object. */
  74:   private byte[] encoded;
  75: 
  76:   /** The DER encoded form of the <code>qualifier</code> field. */
  77:   private DERValue qualifier;
  78: 
  79:   // Constructor.
  80:   // ------------------------------------------------------------------------
  81: 
  82:   /**
  83:    * Create a new PolicyQualifierInfo object from the DER encoded form
  84:    * passed in the byte array. The argument is copied.
  85:    *
  86:    * <p>The ASN.1 form of PolicyQualifierInfo is:
  87: <pre>
  88: PolicyQualifierInfo ::= SEQUENCE {
  89:    policyQualifierId     PolicyQualifierId,
  90:    qualifier             ANY DEFINED BY policyQualifierId
  91: }
  92: 
  93: PolicyQualifierId ::= OBJECT IDENTIFIER
  94: </pre>
  95:    *
  96:    * @param encoded The DER encoded form.
  97:    * @throws IOException If the structure cannot be parsed from the
  98:    *         encoded bytes.
  99:    */
 100:   public PolicyQualifierInfo(byte[] encoded) throws IOException
 101:   {
 102:     if (encoded == null)
 103:       throw new IOException("null bytes");
 104:     this.encoded = (byte[]) encoded.clone();
 105:     DERReader in = new DERReader(new ByteArrayInputStream(this.encoded));
 106:     DERValue qualInfo = in.read();
 107:     if (!qualInfo.isConstructed())
 108:       throw new ASN1ParsingException("malformed PolicyQualifierInfo");
 109:     DERValue val = in.read();
 110:     if (!(val.getValue() instanceof OID))
 111:       throw new ASN1ParsingException("value read not an OBJECT IDENTIFIER");
 112:     oid = (OID) val.getValue();
 113:     if (val.getEncodedLength() < val.getLength())
 114:       qualifier = in.read();
 115:   }
 116: 
 117:   // Instance methods.
 118:   // ------------------------------------------------------------------------
 119: 
 120:   /**
 121:    * Returns the <code>policyQualifierId</code> field of this structure,
 122:    * as a dotted-decimal representation of the object identifier.
 123:    *
 124:    * @return This structure's OID field.
 125:    */
 126:   public String getPolicyQualifierId()
 127:   {
 128:     return oid.toString();
 129:   }
 130: 
 131:   /**
 132:    * Returns the DER encoded form of this object; the contents of the
 133:    * returned byte array are equivalent to those that were passed to the
 134:    * constructor. The byte array is cloned every time this method is
 135:    * called.
 136:    *
 137:    * @return The encoded form.
 138:    */
 139:   public byte[] getEncoded()
 140:   {
 141:     return (byte[]) encoded.clone();
 142:   }
 143: 
 144:   /**
 145:    * Get the <code>qualifier</code> field of this object, as a DER
 146:    * encoded byte array. The byte array returned is cloned every time
 147:    * this method is called.
 148:    *
 149:    * @return The encoded qualifier.
 150:    */
 151:   public byte[] getPolicyQualifier()
 152:   {
 153:     if (qualifier == null)
 154:       return new byte[0];
 155:     return qualifier.getEncoded();
 156:   }
 157: 
 158:   /**
 159:    * Returns a printable string representation of this object.
 160:    *
 161:    * @return The string representation.
 162:    */
 163:   public String toString()
 164:   {
 165:     return "PolicyQualifierInfo { policyQualifierId ::= " + oid
 166:       + ", qualifier ::= " + qualifier + " }";
 167:   }
 168: }