📄 x500distinguishedname.java
字号:
/* X500DistinguishedName.java -- X.500 name. Copyright (C) 2003 Free Software Foundation, Inc.This file is part of GNU Classpath.GNU Classpath is free software; you can redistribute it and/or modifyit under the terms of the GNU General Public License as published bythe Free Software Foundation; either version 2, or (at your option)any later version. GNU Classpath is distributed in the hope that it will be useful, butWITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNUGeneral Public License for more details.You should have received a copy of the GNU General Public Licensealong with GNU Classpath; see the file COPYING. If not, write to theFree Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA02111-1307 USA.Linking this library statically or dynamically with other modules ismaking a combined work based on this library. Thus, the terms andconditions of the GNU General Public License cover the wholecombination.As a special exception, the copyright holders of this library give youpermission to link this library with independent modules to produce anexecutable, regardless of the license terms of these independentmodules, and to copy and distribute the resulting executable underterms of your choice, provided that you also meet, for each linkedindependent module, the terms and conditions of the license of thatmodule. An independent module is a module which is not derived fromor based on this library. If you modify this library, you may extendthis exception to your version of the library, but you are notobligated to do so. If you do not wish to do so, delete thisexception statement from your version. */package gnu.java.security.x509;import java.io.ByteArrayInputStream;import java.io.ByteArrayOutputStream;import java.io.InputStream;import java.io.IOException;import java.io.StreamTokenizer;import java.io.StringReader;import java.security.Principal;import java.util.HashSet;import java.util.LinkedList;import gnu.java.io.ASN1ParsingException;import gnu.java.security.OID;import gnu.java.security.der.DER;import gnu.java.security.der.DERReader;import gnu.java.security.der.DERValue;import gnu.java.security.der.DERWriter;/** * A X.500 distinguished name. Distinguished names are sequences of * ATTRIB=VALUE pairs, where ATTRIB is one of the following: * * <table cellpadding="0" cellspacing="0" border="0"> * <tr> * <th bgcolor="#CCCCFF" align="left">Name</th> * <th bgcolor="#CCCCFF" align="left">X.500 AttributeType</th> * <th bgcolor="#CCCCFF" align="left">ObjectIdentifier</th> * </tr> * <tr> * <td align="left">CN</td> * <td align="left">commonName</td> * <td align="left">2.5.4.3</td> * </tr> * <tr> * <td align="left">C</td> * <td align="left">countryName</td> * <td align="left">2.5.4.6</td> * </tr> * <tr> * <td align="left">L</td> * <td align="left">localityName</td> * <td align="left">2.5.4.7</td> * </tr> * <tr> * <td align="left">ST</td> * <td align="left">stateOrProvinceName</td> * <td align="left">2.5.4.8</td> * </tr> * <tr> * <td align="left">STREET</td> * <td align="left">streetAddress</td> * <td align="left">2.5.4.9</td> * </tr> * <tr> * <td align="left">O</td> * <td align="left">organizationName</td> * <td align="left">2.5.4.10</td> * </tr> * <tr> * <td align="left">OU</td> * <td align="left">organizationUnitName</td> * <td align="left">2.5.4.11</td> * </tr> * <tr> * <td align="left">DC</td> * <td align="left">domainComponent</td> * <td align="left">0.9.2342.19200300.100.1.25</td> * </tr> * <tr> * <td align="left">UID</td> * <td align="left">userid</td> * <td align="left"0.9.2342.19200300.100.1.1></td> * </tr> * <tr> * <td align="left">DNQ or DNQUALIFIER(*)</td> * <td align="left">domainNameQualifier</td> * <td align="left">2.5.4.46</td> * </tr> * <tr> * <td align="left">SURNAME(*)</td> * <td align="left">name</td> * <td align="left">2.5.4.41</td> * </tr> * <tr> * <td align="left">GIVENNAME(*)</td> * <td align="left">givenName</td> * <td align="left">2.5.4.42</td> * </tr> * <tr> * <td align="left">INITIALS(*)</td> * <td align="left">initials</td> * <td align="left">2.5.4.43</td> * </tr> * <tr> * <td align="left">EMAILADDRESS(*)</td> * <td align="left">emailAddress</td> * <td align="left">2.5.4.44</td> * </tr> * </table> * * <p><i>(*) = attributes not specified in RFC1779 or RFC2253, but * recognized anyway.</i> * * <p>Distinguished names of this form are used in the lightweight * directory access protocol (LDAP) and in the issuer and subject fields * of X.509 certificates. * * @author Casey Marshall (rsdio@metastatic.org) * @see javax.security.auth.x500.X500Principal * @status DER decoding/encoding works, RFC1779 and RFC2253 need to be * made more robust. */public class X500DistinguishedName{ // Constants and fields. // ------------------------------------------------------------------------ public static final OID CN = new OID("2.5.4.3"); public static final OID C = new OID("2.5.4.6"); public static final OID L = new OID("2.5.4.7"); public static final OID ST = new OID("2.5.4.8"); public static final OID STREET = new OID("2.5.4.9"); public static final OID O = new OID("2.5.4.10"); public static final OID OU = new OID("2.5.4.11"); public static final OID T = new OID("2.5.4.12"); public static final OID DNQ = new OID("2.5.4.46"); public static final OID NAME = new OID("2.5.4.41"); public static final OID GIVENNAME = new OID("2.5.4.42"); public static final OID INITIALS = new OID("2.5.4.43"); public static final OID GENERATION = new OID("2.5.4.44"); public static final OID EMAIL = new OID("1.2.840.113549.1.9.1"); public static final OID DC = new OID("0.9.2342.19200300.100.1.25"); public static final OID UID = new OID("0.9.2342.19200300.100.1.1"); private String commonName; private String country; private String locality; private String orgUnit; private String organization; private String street; private String state; private String title; private String dnQualifier; private String surname; private String givenName; private String initials; private String generation; private String email; private String domainComponent; private String userid; private String nameRFC1779; private String nameRFC2253; private String nameCanonical; private transient byte[] encoded; // Constructors. // ------------------------------------------------------------------------ /** * Create a new X500DistinguishedName from the RFC1779 or RFC2253 * encoded form. * * @param name The encoded name. * @throws IllegalArgumentException If the name cannot be parsed. */ public X500DistinguishedName(String name) { if (name == null) throw new NullPointerException(); try { parseDN(name, true); } catch (Exception e) { parseDN(name, false); } } /** * Create a new X500DistinguishedName from the DER encoded bytes. * * @param encoded The encoded form. * @throws IOException If the bytes are not a valid DER construct. */ public X500DistinguishedName(byte[] encoded) throws IOException { this(new ByteArrayInputStream(encoded)); } /** * Create a new X500DistinguishedName from the DER encoded bytes. * * @param encoded The encoded form. * @throws IOException If the bytes are not a valid DER construct. */ public X500DistinguishedName(InputStream encoded) throws IOException { parseDER(encoded); } // Instance methods. // ------------------------------------------------------------------------ public boolean equals(Object o) { return (commonName != null && commonName.equals(((X500DistinguishedName) o).commonName)) && (country != null && country.equals(((X500DistinguishedName) o).country)) && (locality != null && locality.equals(((X500DistinguishedName) o).locality)) && (orgUnit != null && orgUnit.equals(((X500DistinguishedName) o).orgUnit)) && (organization != null && organization.equals(((X500DistinguishedName) o).organization)) && (street != null && street.equals(((X500DistinguishedName) o).street)) && (state != null && state.equals(((X500DistinguishedName) o).state)) && (domainComponent != null && domainComponent.equals(((X500DistinguishedName) o).domainComponent)) && (title != null && title.equals(((X500DistinguishedName) o).title)) && (dnQualifier != null && dnQualifier.equals(((X500DistinguishedName) o).dnQualifier)) && (surname != null && surname.equals(((X500DistinguishedName) o).surname)) && (givenName != null && givenName.equals(((X500DistinguishedName) o).givenName)) && (initials != null && initials.equals(((X500DistinguishedName) o).initials)) && (generation != null && generation.equals(((X500DistinguishedName) o).generation)) && (email != null && email.equals(((X500DistinguishedName) o).email)) && (userid != null && userid.equals(((X500DistinguishedName) o).userid)); } public byte[] getEncoded() { if (encoded == null) encoded = encodeDER(); return (byte[]) encoded.clone(); } private static String quote(String str) { if (str.indexOf(" ") > 0 || str.indexOf("\f") > 0 || str.indexOf("\n") > 0 || str.indexOf("\r") > 0 || str.indexOf("\t") > 0) str = '"' + str + '"'; // XXX needs regex //return str.replaceAll("([,+\"\\<>;])", "\\\1"); return str; } public String toRFC1779() { if (nameRFC1779 != null) return nameRFC1779; StringBuffer buf = new StringBuffer(); if (commonName != null) buf.append("CN=").append(quote(commonName)).append(", "); if (country != null) buf.append("C=").append(quote(country)).append(", "); if (locality != null) buf.append("L=").append(quote(locality)).append(", "); if (orgUnit != null) buf.append("OU=").append(quote(orgUnit)).append(", "); if (organization != null) buf.append("O=").append(quote(organization)).append(", "); if (street != null) buf.append("STREET=").append(quote(street)).append(", "); if (state != null) buf.append("ST=").append(quote(state)).append(", "); if (title != null) buf.append(T).append("=").append(quote(title)).append(", "); if (dnQualifier != null) buf.append(DNQ).append("=").append(quote(dnQualifier)).append(", "); if (surname != null) buf.append(NAME).append("=").append(quote(surname)).append(", "); if (givenName != null) buf.append(GIVENNAME).append("=").append(quote(givenName)).append(", "); if (initials != null) buf.append(INITIALS).append("=").append(quote(initials)).append(", "); if (generation != null) buf.append(GENERATION).append("=").append(quote(generation)).append(", "); if (email != null) buf.append(EMAIL).append("=").append(quote(email)).append(", "); if (domainComponent != null) buf.append(DC).append("=").append(quote(domainComponent)).append(", "); if (userid != null) buf.append(UID).append("=").append(quote(userid)).append(", "); // XXX escapes return (nameRFC1779 = buf.substring(0, buf.length()-2)); } public String toRFC2253() { if (nameRFC2253 != null) return nameRFC2253; StringBuffer buf = new StringBuffer(); if (commonName != null) buf.append("CN=").append(quote(commonName)).append(","); if (country != null) buf.append("C=").append(quote(country)).append(","); if (locality != null) buf.append("L=").append(quote(locality)).append(","); if (orgUnit != null) buf.append("OU=").append(quote(orgUnit)).append(","); if (organization != null) buf.append("O=").append(quote(organization)).append(","); if (street != null) buf.append("STREET=").append(quote(street)).append(","); if (state != null) buf.append("ST=").append(quote(state)).append(","); if (title != null) buf.append(T).append("=").append(quote(title)).append(","); if (dnQualifier != null) buf.append(DNQ).append("=").append(quote(dnQualifier)).append(","); if (surname != null) buf.append(NAME).append("=").append(quote(surname)).append(","); if (givenName != null) buf.append(GIVENNAME).append("=").append(quote(givenName)).append(","); if (initials != null) buf.append(INITIALS).append("=").append(quote(initials)).append(","); if (generation != null) buf.append(GENERATION).append("=").append(quote(generation)).append(","); if (email != null) buf.append(EMAIL).append("=").append(quote(email)).append(","); if (domainComponent != null) buf.append(DC).append("=").append(quote(domainComponent)).append(","); if (userid != null) buf.append(UID).append("=").append(quote(userid)).append(","); // XXX escapes. return (nameRFC2253 = buf.substring(0, buf.length()-1)); } public String toCanonical() { if (nameCanonical != null) return nameCanonical; nameCanonical = toRFC2253(); return nameCanonical; // XXX canonicalize } public String getCommonName() { return commonName; } public String getCountry() { return country; } public String getLocality() { return locality; } public String getOrganizationalUnit() { return orgUnit; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -