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

   1: /* GeneralName.java -- a GeneralName.
   2:    Copyright (C) 2006  Free Software Foundation, Inc.
   3: 
   4: This file is a 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 of the License, or (at
   9: your option) 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; if not, write to the Free Software
  18: Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
  19: 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.der.DER;
  42: import gnu.java.security.der.DERReader;
  43: import gnu.java.security.der.DERValue;
  44: import gnu.java.security.x509.Util;
  45: 
  46: import java.io.IOException;
  47: import java.util.Arrays;
  48: 
  49: /**
  50:  * The GeneralName structure from X.509.
  51:  *
  52:  * <pre>
  53:   GeneralName ::= CHOICE {
  54:     otherName                       [0]     OtherName,
  55:     rfc822Name                      [1]     IA5String,
  56:     dNSName                         [2]     IA5String,
  57:     x400Address                     [3]     ORAddress,
  58:     directoryName                   [4]     Name,
  59:     ediPartyName                    [5]     EDIPartyName,
  60:     uniformResourceIdentifier       [6]     IA5String,
  61:     iPAddress                       [7]     OCTET STRING,
  62:     registeredID                    [8]     OBJECT IDENTIFIER }
  63: 
  64:   OtherName ::= SEQUENCE {
  65:     type-id    OBJECT IDENTIFIER,
  66:     value      [0] EXPLICIT ANY DEFINED BY type-id }
  67: 
  68:   EDIPartyName ::= SEQUENCE {
  69:     nameAssigner            [0]     DirectoryString OPTIONAL,
  70:     partyName               [1]     DirectoryString }
  71: </pre>
  72:  *
  73:  * @author Casey Marshall (csm@gnu.org)
  74:  */
  75: public class GeneralName
  76: {
  77:   public static enum Kind
  78:   {
  79:     otherName (0),
  80:     rfc822Name (1),
  81:     dNSName (2),
  82:     x400Address (3),
  83:     directoryName (4),
  84:     ediPartyName (5),
  85:     uniformResourceIdentifier (6),
  86:     iPAddress (7),
  87:     registeredId (8);
  88: 
  89:     private int tag;
  90: 
  91:     private Kind(int tag)
  92:     {
  93:       this.tag = tag;
  94:     }
  95: 
  96:     public static Kind forTag(final int tag)
  97:     {
  98:       switch (tag)
  99:       {
 100:         case 0: return otherName;
 101:         case 1: return rfc822Name;
 102:         case 2: return dNSName;
 103:         case 3: return x400Address;
 104:         case 4: return directoryName;
 105:         case 5: return ediPartyName;
 106:         case 6: return uniformResourceIdentifier;
 107:         case 7: return iPAddress;
 108:         case 8: return registeredId;
 109:       }
 110: 
 111:       throw new IllegalArgumentException("invalid tag: " + tag);
 112:     }
 113: 
 114:     public int tag()
 115:     {
 116:       return tag;
 117:     }
 118:   };
 119: 
 120:   private final Kind kind;
 121:   private final byte[] name;
 122:   private final byte[] encoded;
 123: 
 124:   public GeneralName(byte[] encoded) throws IOException
 125:   {
 126:     DERReader reader = new DERReader(encoded);
 127:     DERValue value = reader.read();
 128: 
 129:     if (value.getTagClass() != DER.CONTEXT)
 130:       throw new IOException("malformed GeneralName");
 131: 
 132:     this.encoded = value.getEncoded();
 133: 
 134:     kind = Kind.forTag(value.getTag());
 135:     switch (kind)
 136:     {
 137:       case otherName:
 138:         name = value.getEncoded();
 139:         name[0] = (byte) (DER.CONSTRUCTED | DER.SEQUENCE);
 140:         // Skip the two fields of the name.
 141:         reader.read();  // OID
 142:         reader.read();  // Octet string
 143:         break;
 144: 
 145:       case rfc822Name:
 146:         name = (byte[]) value.getValue();
 147:         break;
 148: 
 149:       case dNSName:
 150:         name = (byte[]) value.getValue();
 151:         break;
 152: 
 153:       case x400Address:
 154:         name = (byte[]) value.getValue();
 155:         break;
 156: 
 157:       case directoryName:
 158:         name = value.getEncoded();
 159:         name[0] = (byte) (DER.CONSTRUCTED | DER.SEQUENCE);
 160:         break;
 161: 
 162:       case ediPartyName:
 163:         name = value.getEncoded();
 164:         name[0] = (byte) (DER.CONSTRUCTED | DER.SEQUENCE);
 165:         break;
 166: 
 167:       case uniformResourceIdentifier:
 168:         name = (byte[]) value.getValue();
 169:         break;
 170: 
 171:       case iPAddress:
 172:         name = (byte[]) value.getValue();
 173:         break;
 174: 
 175:       case registeredId:
 176:         name = value.getEncoded();
 177:         name[0] = DER.OBJECT_IDENTIFIER;
 178:         break;
 179: 
 180:       default:
 181:         name = null; // Not reached.
 182:     }
 183:   }
 184: 
 185:   public GeneralName(Kind kind, byte[] name)
 186:   {
 187:     this.kind = kind;
 188:     this.name = (byte[]) name.clone();
 189:     this.encoded = null;
 190:   }
 191: 
 192:   public Kind kind()
 193:   {
 194:     return kind;
 195:   }
 196: 
 197:   public byte[] name()
 198:   {
 199:     return (byte[]) name.clone();
 200:   }
 201: 
 202:   public byte[] encoded()
 203:   {
 204:     try
 205:       {
 206:         return (byte[]) encoded.clone();
 207:       }
 208:     catch (NullPointerException npe)
 209:       {
 210:         return null;
 211:       }
 212:   }
 213: 
 214:   public boolean equals(Object o)
 215:   {
 216:     try
 217:       {
 218:         GeneralName that = (GeneralName) o;
 219:         return (that.kind() == kind() && Arrays.equals(name, that.name));
 220:       }
 221:     catch (ClassCastException cce)
 222:       {
 223:         return false;
 224:       }
 225:   }
 226: 
 227:   public String toString()
 228:   {
 229:     return (super.toString() + " [ kind=" + kind + "; name=" +
 230:             Util.hexDump(name, "") + " ]");
 231:   }
 232: }