001: /*
002: * @(#)RFC822Name.java 1.15 06/10/10
003: *
004: * Copyright 1990-2006 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License version
009: * 2 only, as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful, but
012: * WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * General Public License version 2 for more details (a copy is
015: * included at /legal/license.txt).
016: *
017: * You should have received a copy of the GNU General Public License
018: * version 2 along with this work; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA
021: *
022: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
023: * Clara, CA 95054 or visit www.sun.com if you need additional
024: * information or have any questions.
025: *
026: */
027:
028: package sun.security.x509;
029:
030: import java.io.IOException;
031:
032: import sun.security.util.*;
033:
034: /**
035: * This class implements the RFC822Name as required by the GeneralNames
036: * ASN.1 object.
037: *
038: * @author Amit Kapoor
039: * @author Hemma Prafullchandra
040: * @version 1.8
041: * @see GeneralName
042: * @see GeneralNames
043: * @see GeneralNameInterface
044: */
045: public class RFC822Name implements GeneralNameInterface {
046: private String name;
047:
048: /**
049: * Create the RFC822Name object from the passed encoded Der value.
050: *
051: * @param derValue the encoded DER RFC822Name.
052: * @exception IOException on error.
053: */
054: public RFC822Name(DerValue derValue) throws IOException {
055: name = derValue.getIA5String();
056: parseName(name);
057: }
058:
059: /**
060: * Create the RFC822Name object with the specified name.
061: *
062: * @param name the RFC822Name.
063: * @throws IOException on invalid input name
064: */
065: public RFC822Name(String name) throws IOException {
066: parseName(name);
067: this .name = name;
068: }
069:
070: /**
071: * Parse an RFC822Name string to see if it is a valid
072: * addr-spec according to IETF RFC822 and RFC2459:
073: * [local-part@]domain
074: * <p>
075: * local-part@ could be empty for an RFC822Name NameConstraint,
076: * but the domain at least must be non-empty. Case is not
077: * significant.
078: *
079: * @param name the RFC822Name string
080: * @throws IOException if name is not valid
081: */
082: public void parseName(String name) throws IOException {
083: if (name == null || name.length() == 0) {
084: throw new IOException("RFC822Name may not be null or empty");
085: }
086: // See if domain is a valid domain name
087: String domain = name.substring(name.indexOf('@') + 1);
088: if (domain.length() == 0) {
089: throw new IOException("RFC822Name may not end with @");
090: } else {
091: //An RFC822 NameConstraint could start with a ., although
092: //a DNSName may not
093: if (domain.startsWith(".")) {
094: if (domain.length() == 1)
095: throw new IOException(
096: "RFC822Name domain may not be just .");
097: }
098: }
099: }
100:
101: /**
102: * Return the type of the GeneralName.
103: */
104: public int getType() {
105: return (GeneralNameInterface.NAME_RFC822);
106: }
107:
108: /**
109: * Return the actual name value of the GeneralName.
110: */
111: public String getName() {
112: return name;
113: }
114:
115: /**
116: * Encode the RFC822 name into the DerOutputStream.
117: *
118: * @param out the DER stream to encode the RFC822Name to.
119: * @exception IOException on encoding errors.
120: */
121: public void encode(DerOutputStream out) throws IOException {
122: out.putIA5String(name);
123: }
124:
125: /**
126: * Convert the name into user readable string.
127: */
128: public String toString() {
129: return ("RFC822Name: " + name);
130: }
131:
132: /**
133: * Compares this name with another, for equality.
134: *
135: * @return true iff the names are equivalent
136: * according to RFC2459.
137: */
138: public boolean equals(Object obj) {
139: if (this == obj)
140: return true;
141:
142: if (!(obj instanceof RFC822Name))
143: return false;
144:
145: RFC822Name other = (RFC822Name) obj;
146:
147: // RFC2459 mandates that these names are
148: // not case-sensitive
149: return name.equalsIgnoreCase(other.name);
150: }
151:
152: /**
153: * Returns the hash code value for this object.
154: *
155: * @return a hash code value for this object.
156: */
157: public int hashCode() {
158: return name.toUpperCase().hashCode();
159: }
160:
161: /**
162: * Return constraint type:<ul>
163: * <li>NAME_DIFF_TYPE = -1: input name is different type from name (i.e. does not constrain)
164: * <li>NAME_MATCH = 0: input name matches name
165: * <li>NAME_NARROWS = 1: input name narrows name
166: * <li>NAME_WIDENS = 2: input name widens name
167: * <li>NAME_SAME_TYPE = 3: input name does not match or narrow name, but is same type
168: * </ul>. These results are used in checking NameConstraints during
169: * certification path verification.
170: * <p>
171: * [RFC2459] When the subjectAltName extension contains an Internet mail address,
172: * the address MUST be included as an rfc822Name. The format of an
173: * rfc822Name is an "addr-spec" as defined in RFC 822 [RFC 822]. An
174: * addr-spec has the form "local-part@domain". Note that an addr-spec
175: * has no phrase (such as a common name) before it, has no comment (text
176: * surrounded in parentheses) after it, and is not surrounded by "<" and
177: * ">". Note that while upper and lower case letters are allowed in an
178: * RFC 822 addr-spec, no significance is attached to the case.
179: * <p>
180: * @param inputName to be checked for being constrained
181: * @returns constraint type above
182: * @throws UnsupportedOperationException if name is not exact match, but narrowing and widening are
183: * not supported for this name type.
184: */
185: public int constrains(GeneralNameInterface inputName)
186: throws UnsupportedOperationException {
187: int constraintType;
188: if (inputName == null)
189: constraintType = NAME_DIFF_TYPE;
190: else if (inputName.getType() != (GeneralNameInterface.NAME_RFC822)) {
191: constraintType = NAME_DIFF_TYPE;
192: } else {
193: //RFC2459 specifies that case is not significant in RFC822Names
194: String inName = (((RFC822Name) inputName).getName())
195: .toLowerCase();
196: String this Name = name.toLowerCase();
197: if (inName.equals(this Name)) {
198: constraintType = NAME_MATCH;
199: } else if (this Name.endsWith(inName)) {
200: /* if both names contain @, then they had to match exactly */
201: if (inName.indexOf('@') != -1) {
202: constraintType = NAME_SAME_TYPE;
203: } else if (inName.startsWith(".")) {
204: constraintType = NAME_WIDENS;
205: } else {
206: int inNdx = this Name.lastIndexOf(inName);
207: if (this Name.charAt(inNdx - 1) == '@') {
208: constraintType = NAME_WIDENS;
209: } else {
210: constraintType = NAME_SAME_TYPE;
211: }
212: }
213: } else if (inName.endsWith(this Name)) {
214: /* if thisName contains @, then they had to match exactly */
215: if (this Name.indexOf('@') != -1) {
216: constraintType = NAME_SAME_TYPE;
217: } else if (this Name.startsWith(".")) {
218: constraintType = NAME_NARROWS;
219: } else {
220: int ndx = inName.lastIndexOf(this Name);
221: if (inName.charAt(ndx - 1) == '@') {
222: constraintType = NAME_NARROWS;
223: } else {
224: constraintType = NAME_SAME_TYPE;
225: }
226: }
227: } else {
228: constraintType = NAME_SAME_TYPE;
229: }
230: }
231: return constraintType;
232: }
233:
234: /**
235: * Return subtree depth of this name for purposes of determining
236: * NameConstraints minimum and maximum bounds.
237: *
238: * @returns distance of name from root
239: * @throws UnsupportedOperationException if not supported for this name type
240: */
241: public int subtreeDepth() throws UnsupportedOperationException {
242: String subtree = name;
243: int i = 1;
244:
245: /* strip off name@ portion */
246: int atNdx = subtree.lastIndexOf('@');
247: if (atNdx >= 0) {
248: i++;
249: subtree = subtree.substring(atNdx + 1);
250: }
251:
252: /* count dots in dnsname, adding one if dnsname preceded by @ */
253: for (; subtree.lastIndexOf('.') >= 0; i++) {
254: subtree = subtree.substring(0, subtree.lastIndexOf('.'));
255: }
256:
257: return i;
258: }
259: }
|