Source for gnu.java.security.x509.ext.Extension

   1: /* Extension.java -- an X.509 certificate or CRL extension.
   2:    Copyright (C) 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 gnu.java.security.x509.ext;
  40: 
  41: import gnu.java.security.OID;
  42: import gnu.java.security.der.DER;
  43: import gnu.java.security.der.DERReader;
  44: import gnu.java.security.der.DERValue;
  45: import gnu.java.security.x509.Util;
  46: 
  47: import java.io.IOException;
  48: import java.util.ArrayList;
  49: import java.util.Arrays;
  50: import java.util.List;
  51: 
  52: public class Extension
  53: {
  54: 
  55:   // Fields.
  56:   // -------------------------------------------------------------------------
  57: 
  58:   private static final boolean DEBUG = false;
  59:   private static void debug(String msg)
  60:   {
  61:     System.err.print(">> Extension: ");
  62:     System.err.println(msg);
  63:   }
  64: 
  65:   /**
  66:    * This extension's object identifier.
  67:    */
  68:   protected final OID oid;
  69: 
  70:   /**
  71:    * The criticality flag.
  72:    */
  73:   protected final boolean critical;
  74: 
  75:   /**
  76:    * Whether or not this extension is locally supported.
  77:    */
  78:   protected boolean isSupported;
  79: 
  80:   /**
  81:    * The extension value.
  82:    */
  83:   protected final Value value;
  84: 
  85:   /**
  86:    * The DER encoded form.
  87:    */
  88:   protected byte[] encoded;
  89: 
  90:   // Constructors.
  91:   // -------------------------------------------------------------------------
  92: 
  93:   public Extension(byte[] encoded) throws IOException
  94:   {
  95:     this.encoded = (byte[]) encoded.clone();
  96:     DERReader der = new DERReader(encoded);
  97: 
  98:     // Extension ::= SEQUENCE {
  99:     DERValue val = der.read();
 100:     if (DEBUG) debug("read val  tag == " + val.getTag() + " len == " + val.getLength());
 101:     if (!val.isConstructed())
 102:       throw new IOException("malformed Extension");
 103: 
 104:     //   extnID    OBJECT IDENTIFIER,
 105:     val = der.read();
 106:     if (val.getTag() != DER.OBJECT_IDENTIFIER)
 107:       throw new IOException("expecting OBJECT IDENTIFIER");
 108:     oid = (OID) val.getValue();
 109:     if (DEBUG) debug("read oid == " + oid);
 110: 
 111:     //   critical  BOOLEAN DEFAULT FALSE,
 112:     val = der.read();
 113:     if (val.getTag() == DER.BOOLEAN)
 114:       {
 115:         critical = ((Boolean) val.getValue()).booleanValue();
 116:         val = der.read();
 117:       }
 118:     else
 119:       critical = false;
 120:     if (DEBUG) debug("is critical == " + critical);
 121: 
 122:     //   extnValue OCTET STRING }
 123:     if (val.getTag() != DER.OCTET_STRING)
 124:       throw new IOException("expecting OCTET STRING");
 125:     byte[] encval = (byte[]) val.getValue();
 126:     isSupported = true;
 127:     if (oid.equals(AuthorityKeyIdentifier.ID))
 128:       {
 129:         value = new AuthorityKeyIdentifier(encval);
 130:       }
 131:     else if (oid.equals(SubjectKeyIdentifier.ID))
 132:       {
 133:         value = new SubjectKeyIdentifier(encval);
 134:       }
 135:     else if (oid.equals(KeyUsage.ID))
 136:       {
 137:         value = new KeyUsage(encval);
 138:       }
 139:     else if (oid.equals(PrivateKeyUsagePeriod.ID))
 140:       {
 141:         value = new PrivateKeyUsagePeriod(encval);
 142:       }
 143:     else if (oid.equals(CertificatePolicies.ID))
 144:       {
 145:         value = new CertificatePolicies(encval);
 146:       }
 147:     else if (oid.equals (PolicyConstraint.ID))
 148:       {
 149:         value = new PolicyConstraint (encval);
 150:       }
 151:     else if (oid.equals(PolicyMappings.ID))
 152:       {
 153:         value = new PolicyMappings(encval);
 154:       }
 155:     else if (oid.equals(SubjectAlternativeNames.ID))
 156:       {
 157:         value = new SubjectAlternativeNames(encval);
 158:       }
 159:     else if (oid.equals(IssuerAlternativeNames.ID))
 160:       {
 161:         value = new IssuerAlternativeNames(encval);
 162:       }
 163:     else if (oid.equals(BasicConstraints.ID))
 164:       {
 165:         value = new BasicConstraints(encval);
 166:       }
 167:     else if (oid.equals(ExtendedKeyUsage.ID))
 168:       {
 169:         value = new ExtendedKeyUsage(encval);
 170:       }
 171:     else if (oid.equals(CRLNumber.ID))
 172:       {
 173:         value = new CRLNumber(encval);
 174:       }
 175:     else if (oid.equals(ReasonCode.ID))
 176:       {
 177:         value = new ReasonCode(encval);
 178:       }
 179:     else
 180:       {
 181:         value = new Value(encval);
 182:         isSupported = false;
 183:       }
 184:     if (DEBUG) debug("read value == " + value);
 185:   }
 186: 
 187:   public Extension (final OID oid, final Value value, final boolean critical)
 188:   {
 189:     this.oid = oid;
 190:     this.value = value;
 191:     this.critical = critical;
 192:     isSupported = true;
 193:   }
 194: 
 195:   // Instance methods.
 196:   // -------------------------------------------------------------------------
 197: 
 198:   public OID getOid()
 199:   {
 200:     return oid;
 201:   }
 202: 
 203:   public boolean isCritical()
 204:   {
 205:     return critical;
 206:   }
 207: 
 208:   public boolean isSupported()
 209:   {
 210:     return isSupported;
 211:   }
 212: 
 213:   public Value getValue()
 214:   {
 215:     return value;
 216:   }
 217: 
 218:   public byte[] getEncoded()
 219:   {
 220:     if (encoded == null)
 221:       encode();
 222:     return (byte[]) encoded.clone();
 223:   }
 224: 
 225:   public String toString()
 226:   {
 227:     return Extension.class.getName() + " [ id=" + oid + " critical=" +
 228:       critical + " value=" + value + " ]";
 229:   }
 230: 
 231:   public DERValue getDerValue()
 232:   {
 233:     List ext = new ArrayList (3);
 234:     ext.add (new DERValue (DER.OBJECT_IDENTIFIER, oid));
 235:     ext.add (new DERValue (DER.BOOLEAN, Boolean.valueOf (critical)));
 236:     ext.add (new DERValue (DER.OCTET_STRING, value.getEncoded()));
 237:     return new DERValue (DER.CONSTRUCTED|DER.SEQUENCE, ext);
 238:   }
 239: 
 240:   // Own methods.
 241:   // -------------------------------------------------------------------------
 242: 
 243:   private void encode()
 244:   {
 245:     encoded = getDerValue().getEncoded();
 246:   }
 247: 
 248:   // Inner class.
 249:   // -------------------------------------------------------------------------
 250: 
 251:   public static class Value
 252:   {
 253: 
 254:     // Fields.
 255:     // -----------------------------------------------------------------------
 256: 
 257:     protected byte[] encoded;
 258: 
 259:     // Constructor.
 260:     // -----------------------------------------------------------------------
 261: 
 262:     public Value(byte[] encoded)
 263:     {
 264:       this.encoded = (byte[]) encoded.clone();
 265:     }
 266: 
 267:     protected Value() { }
 268: 
 269:     // Instance methods.
 270:     // -----------------------------------------------------------------------
 271: 
 272:     public byte[] getEncoded()
 273:     {
 274:       return (byte[]) encoded;
 275:     }
 276: 
 277:     public int hashCode()
 278:     {
 279:       int result = 0;
 280:       for (int i = 0; i < encoded.length; ++i)
 281:         result = result * 31 + encoded[i];
 282:       return result;
 283:     }
 284: 
 285:     public boolean equals(Object o)
 286:     {
 287:       if (!(o instanceof Value))
 288:         return false;
 289:       return Arrays.equals(encoded, ((Value) o).encoded);
 290:     }
 291: 
 292:     public String toString()
 293:     {
 294:       return Util.toHexString(encoded, ':');
 295:     }
 296:   }
 297: }