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

   1: /* GeneralNames.java -- the GeneralNames object
   2:    Copyright (C) 2004, 2006  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: 
  46: import java.io.IOException;
  47: import java.net.InetAddress;
  48: import java.util.ArrayList;
  49: import java.util.Collections;
  50: import java.util.Iterator;
  51: import java.util.LinkedList;
  52: import java.util.List;
  53: 
  54: import javax.security.auth.x500.X500Principal;
  55: 
  56: public class GeneralNames
  57: {
  58: 
  59:   // Instance methods.
  60:   // -------------------------------------------------------------------------
  61: 
  62:   public static final int OTHER_NAME     = 0;
  63:   public static final int RFC822_NAME    = 1;
  64:   public static final int DNS_NAME       = 2;
  65:   public static final int X400_ADDRESS   = 3;
  66:   public static final int DIRECTORY_NAME = 4;
  67:   public static final int EDI_PARTY_NAME = 5;
  68:   public static final int URI            = 6;
  69:   public static final int IP_ADDRESS     = 7;
  70:   public static final int REGISTERED_ID  = 8;
  71: 
  72:   private List names;
  73: 
  74:   // Constructor.
  75:   // -------------------------------------------------------------------------
  76: 
  77:   public GeneralNames(final byte[] encoded) throws IOException
  78:   {
  79:     names = new LinkedList();
  80:     DERReader der = new DERReader(encoded);
  81:     DERValue nameList = der.read();
  82:     if (!nameList.isConstructed())
  83:       throw new IOException("malformed GeneralNames");
  84:     int len = 0;
  85:     int i = 0;
  86:     while (len < nameList.getLength())
  87:       {
  88:         DERValue name = der.read();
  89:         List namePair = new ArrayList(2);
  90:         int tagClass = name.getTagClass();
  91:         if (tagClass != DER.CONTEXT)
  92:           throw new IOException("malformed GeneralName: Tag class is " + tagClass);
  93:         namePair.add(Integer.valueOf(name.getTag()));
  94:         DERValue val = null;
  95:         switch (name.getTag())
  96:           {
  97:           case RFC822_NAME:
  98:           case DNS_NAME:
  99:           case X400_ADDRESS:
 100:           case URI:
 101:             namePair.add(new String((byte[]) name.getValue()));
 102:             break;
 103: 
 104:           case OTHER_NAME:
 105:             // MUST return the encoded bytes of the OID/OctetString sequence
 106:             byte[] anotherName = name.getEncoded();
 107:             anotherName[0] = (byte) (DER.CONSTRUCTED|DER.SEQUENCE);
 108:             namePair.add(anotherName);
 109:             // DERReader goes back on Constructed things so we need to skip over them
 110:             DERValue skip = der.read(); // skip OID
 111:             skip = der.read(); // skip Octet String
 112:             break;
 113:             
 114:           case EDI_PARTY_NAME:
 115:             namePair.add(name.getValue());
 116:             break;
 117: 
 118:           case DIRECTORY_NAME:
 119:             byte[] b = name.getEncoded();
 120:             b[0] = (byte) (DER.CONSTRUCTED|DER.SEQUENCE);
 121:             DERReader r = new DERReader (b);
 122:             r.read ();
 123:             namePair.add(new X500Principal(r.read ().getEncoded ()).toString());
 124:             break;
 125: 
 126:           case IP_ADDRESS:
 127:             namePair.add(InetAddress.getByAddress((byte[]) name.getValue())
 128:                          .getHostAddress());
 129:             break;
 130: 
 131:           case REGISTERED_ID:
 132:             byte[] bb = name.getEncoded();
 133:             bb[0] = (byte) DER.OBJECT_IDENTIFIER;
 134:             namePair.add(new OID(bb).toString());
 135:             break;
 136: 
 137:           default:
 138:             throw new IOException("unknown tag " + name.getTag());
 139:           }
 140:         names.add(namePair);
 141:         len += name.getEncodedLength();
 142:       }
 143:   }
 144: 
 145:   // Instance methods.
 146:   // -------------------------------------------------------------------------
 147: 
 148:   public List getNames()
 149:   {
 150:     List l = new ArrayList(names.size());
 151:     for (Iterator it = names.iterator(); it.hasNext(); )
 152:       {
 153:         List ll = (List) it.next();
 154:         List pair = new ArrayList(2);
 155:         pair.add(ll.get(0));
 156:         if (ll.get(1) instanceof byte[])
 157:           pair.add(((byte[]) ll.get(1)).clone());
 158:         else
 159:           pair.add(ll.get(1));
 160:         l.add(Collections.unmodifiableList(pair));
 161:       }
 162:     return Collections.unmodifiableList(l);
 163:   }
 164: 
 165:   public String toString()
 166:   {
 167:     return GeneralNames.class.getName() + " [ " + names + " ]";
 168:   }
 169: }