| 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 */  | 
 | 
 | 
 | 
package sun.security.x509;  | 
 | 
 | 
 | 
import java.io.IOException;  | 
 | 
 | 
 | 
import sun.security.util.*;  | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 */  | 
 | 
public class GeneralName { | 
 | 
 | 
 | 
      | 
 | 
    private GeneralNameInterface name = null;  | 
 | 
 | 
 | 
      | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
     */  | 
 | 
    public GeneralName(GeneralNameInterface name) { | 
 | 
        if (name == null) { | 
 | 
            throw new NullPointerException("GeneralName must not be null"); | 
 | 
        }  | 
 | 
        this.name = name;  | 
 | 
    }  | 
 | 
 | 
 | 
      | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
     */  | 
 | 
    public GeneralName(DerValue encName) throws IOException { | 
 | 
        this(encName, false);  | 
 | 
    }  | 
 | 
 | 
 | 
      | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
     */  | 
 | 
    public GeneralName(DerValue encName, boolean nameConstraint)  | 
 | 
        throws IOException { | 
 | 
        short tag = (byte)(encName.tag & 0x1f);  | 
 | 
 | 
 | 
        // All names except for NAME_DIRECTORY should be encoded with the  | 
 | 
          | 
 | 
        switch (tag) { | 
 | 
        case GeneralNameInterface.NAME_ANY:  | 
 | 
            if (encName.isContextSpecific() && encName.isConstructed()) { | 
 | 
                encName.resetTag(DerValue.tag_Sequence);  | 
 | 
                name = new OtherName(encName);  | 
 | 
            } else { | 
 | 
                throw new IOException("Invalid encoding of Other-Name"); | 
 | 
            }  | 
 | 
            break;  | 
 | 
 | 
 | 
        case GeneralNameInterface.NAME_RFC822:  | 
 | 
            if (encName.isContextSpecific() && !encName.isConstructed()) { | 
 | 
                encName.resetTag(DerValue.tag_IA5String);  | 
 | 
                name = new RFC822Name(encName);  | 
 | 
            } else { | 
 | 
                throw new IOException("Invalid encoding of RFC822 name"); | 
 | 
            }  | 
 | 
            break;  | 
 | 
 | 
 | 
        case GeneralNameInterface.NAME_DNS:  | 
 | 
            if (encName.isContextSpecific() && !encName.isConstructed()) { | 
 | 
                encName.resetTag(DerValue.tag_IA5String);  | 
 | 
                name = new DNSName(encName);  | 
 | 
            } else { | 
 | 
                throw new IOException("Invalid encoding of DNSName"); | 
 | 
            }  | 
 | 
            break;  | 
 | 
 | 
 | 
        case GeneralNameInterface.NAME_URI:  | 
 | 
            if (encName.isContextSpecific() && !encName.isConstructed()) { | 
 | 
                encName.resetTag(DerValue.tag_IA5String);  | 
 | 
                name = (nameConstraint ? URIName.nameConstraint(encName) :  | 
 | 
                        new URIName(encName));  | 
 | 
            } else { | 
 | 
                throw new IOException("Invalid encoding of URI"); | 
 | 
            }  | 
 | 
            break;  | 
 | 
 | 
 | 
        case GeneralNameInterface.NAME_IP:  | 
 | 
            if (encName.isContextSpecific() && !encName.isConstructed()) { | 
 | 
                encName.resetTag(DerValue.tag_OctetString);  | 
 | 
                name = new IPAddressName(encName);  | 
 | 
            } else { | 
 | 
                throw new IOException("Invalid encoding of IP address"); | 
 | 
            }  | 
 | 
            break;  | 
 | 
 | 
 | 
        case GeneralNameInterface.NAME_OID:  | 
 | 
            if (encName.isContextSpecific() && !encName.isConstructed()) { | 
 | 
                encName.resetTag(DerValue.tag_ObjectId);  | 
 | 
                name = new OIDName(encName);  | 
 | 
            } else { | 
 | 
                throw new IOException("Invalid encoding of OID name"); | 
 | 
            }  | 
 | 
            break;  | 
 | 
 | 
 | 
        case GeneralNameInterface.NAME_DIRECTORY:  | 
 | 
            if (encName.isContextSpecific() && encName.isConstructed()) { | 
 | 
                name = new X500Name(encName.getData());  | 
 | 
            } else { | 
 | 
                throw new IOException("Invalid encoding of Directory name"); | 
 | 
            }  | 
 | 
            break;  | 
 | 
 | 
 | 
        case GeneralNameInterface.NAME_EDI:  | 
 | 
            if (encName.isContextSpecific() && encName.isConstructed()) { | 
 | 
                encName.resetTag(DerValue.tag_Sequence);  | 
 | 
                name = new EDIPartyName(encName);  | 
 | 
            } else { | 
 | 
                throw new IOException("Invalid encoding of EDI name"); | 
 | 
            }  | 
 | 
            break;  | 
 | 
 | 
 | 
        default:  | 
 | 
            throw new IOException("Unrecognized GeneralName tag, (" | 
 | 
                                  + tag +")");  | 
 | 
        }  | 
 | 
    }  | 
 | 
 | 
 | 
      | 
 | 
 | 
 | 
     */  | 
 | 
    public int getType() { | 
 | 
        return name.getType();  | 
 | 
    }  | 
 | 
 | 
 | 
      | 
 | 
 | 
 | 
     */  | 
 | 
    public GeneralNameInterface getName() { | 
 | 
          | 
 | 
        return name;  | 
 | 
    }  | 
 | 
 | 
 | 
      | 
 | 
 | 
 | 
     */  | 
 | 
    public String toString() { | 
 | 
        return name.toString();  | 
 | 
    }  | 
 | 
 | 
 | 
      | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
     */  | 
 | 
    public boolean equals(Object other) { | 
 | 
        if (this == other) { | 
 | 
            return true;  | 
 | 
        }  | 
 | 
        if (!(other instanceof GeneralName))  | 
 | 
            return false;  | 
 | 
        GeneralNameInterface otherGNI = ((GeneralName)other).name;  | 
 | 
        try { | 
 | 
            return name.constrains(otherGNI) == GeneralNameInterface.NAME_MATCH;  | 
 | 
        } catch (UnsupportedOperationException ioe) { | 
 | 
            return false;  | 
 | 
        }  | 
 | 
    }  | 
 | 
 | 
 | 
      | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
     */  | 
 | 
    public int hashCode() { | 
 | 
        return name.hashCode();  | 
 | 
    }  | 
 | 
 | 
 | 
      | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
     */  | 
 | 
    public void encode(DerOutputStream out) throws IOException { | 
 | 
        DerOutputStream tmp = new DerOutputStream();  | 
 | 
        name.encode(tmp);  | 
 | 
        int nameType = name.getType();  | 
 | 
        if (nameType == GeneralNameInterface.NAME_ANY ||  | 
 | 
            nameType == GeneralNameInterface.NAME_X400 ||  | 
 | 
            nameType == GeneralNameInterface.NAME_EDI) { | 
 | 
 | 
 | 
              | 
 | 
            out.writeImplicit(DerValue.createTag(DerValue.TAG_CONTEXT,  | 
 | 
                              true, (byte)nameType), tmp);  | 
 | 
        } else if (nameType == GeneralNameInterface.NAME_DIRECTORY) { | 
 | 
            // explicit, constructed form since underlying tag is CHOICE  | 
 | 
              | 
 | 
            out.write(DerValue.createTag(DerValue.TAG_CONTEXT,  | 
 | 
                                         true, (byte)nameType), tmp);  | 
 | 
        } else { | 
 | 
              | 
 | 
            out.writeImplicit(DerValue.createTag(DerValue.TAG_CONTEXT,  | 
 | 
                              false, (byte)nameType), tmp);  | 
 | 
        }  | 
 | 
    }  | 
 | 
}  |