001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: /**
019: * @author Alexander V. Astapchuk
020: * @version $Revision$
021: */package java.security;
022:
023: import java.io.Serializable;
024: import java.util.Vector;
025: import java.util.Arrays;
026:
027: import org.apache.harmony.security.internal.nls.Messages;
028:
029: /**
030: *
031: * @deprecated
032: */
033: @Deprecated
034: public abstract class Identity implements Principal, Serializable {
035: private static final long serialVersionUID = 3609922007826600659L;
036:
037: private String name;
038:
039: private PublicKey publicKey;
040:
041: private String info = "no additional info"; //$NON-NLS-1$
042:
043: private IdentityScope scope;
044:
045: private Vector<Certificate> certificates;
046:
047: protected Identity() {
048: }
049:
050: public Identity(String name) {
051: this .name = name;
052: }
053:
054: public Identity(String name, IdentityScope scope)
055: throws KeyManagementException {
056: this (name);
057: if (scope != null) {
058: scope.addIdentity(this );
059: this .scope = scope;
060: }
061: }
062:
063: public void addCertificate(Certificate certificate)
064: throws KeyManagementException {
065: SecurityManager sm = System.getSecurityManager();
066: if (sm != null) {
067: sm.checkSecurityAccess("addIdentityCertificate"); //$NON-NLS-1$
068: }
069: PublicKey certPK = certificate.getPublicKey();
070: if (publicKey != null) {
071: if (!checkKeysEqual(publicKey, certPK)) {
072: throw new KeyManagementException(Messages
073: .getString("security.13")); //$NON-NLS-1$
074: }
075: } else {
076: publicKey = certPK;
077: }
078: if (certificates == null) {
079: certificates = new Vector<Certificate>();
080: }
081: certificates.add(certificate);
082: }
083:
084: private static boolean checkKeysEqual(PublicKey pk1, PublicKey pk2) {
085: // first, they should have the same format
086: // second, their encoded form must be the same
087:
088: // assert(pk1 != null);
089: // assert(pk2 != null);
090:
091: String format1 = pk1.getFormat();
092: String format2;
093: if ((pk2 == null)
094: || (((format2 = pk2.getFormat()) != null) ^ (format1 != null))
095: || ((format1 != null) && !format1.equals(format2))) {
096: return false;
097: }
098:
099: return Arrays.equals(pk1.getEncoded(), pk2.getEncoded());
100: }
101:
102: public void removeCertificate(Certificate certificate)
103: throws KeyManagementException {
104: SecurityManager sm = System.getSecurityManager();
105: if (sm != null) {
106: sm.checkSecurityAccess("removeIdentityCertificate"); //$NON-NLS-1$
107: }
108: if (certificates != null) {
109: certificates.removeElement(certificate);
110: }
111: }
112:
113: public Certificate[] certificates() {
114: if (certificates == null) {
115: return new Certificate[0];
116: }
117: Certificate[] ret = new Certificate[certificates.size()];
118: certificates.copyInto(ret);
119: return ret;
120: }
121:
122: protected boolean identityEquals(Identity identity) {
123: if (!name.equals(identity.name)) {
124: return false;
125: }
126:
127: if (publicKey == null) {
128: return (identity.publicKey == null);
129: }
130:
131: return checkKeysEqual(publicKey, identity.publicKey);
132: }
133:
134: public String toString(boolean detailed) {
135: String s = toString();
136: if (detailed) {
137: s += " " + info; //$NON-NLS-1$
138: }
139: return s;
140: }
141:
142: public final IdentityScope getScope() {
143: return scope;
144: }
145:
146: public void setPublicKey(PublicKey key)
147: throws KeyManagementException {
148: SecurityManager sm = System.getSecurityManager();
149: if (sm != null) {
150: sm.checkSecurityAccess("setIdentityPublicKey"); //$NON-NLS-1$
151: }
152: // this check does not always work
153: if ((scope != null) && (key != null)) {
154: Identity i = scope.getIdentity(key);
155: //System.out.println("###DEBUG## Identity: "+i);
156: if ((i != null) && (i != this )) {
157: throw new KeyManagementException(Messages
158: .getString("security.14")); //$NON-NLS-1$
159: }
160: }
161: this .publicKey = key;
162: certificates = null;
163: }
164:
165: public PublicKey getPublicKey() {
166: return publicKey;
167: }
168:
169: public void setInfo(String info) {
170: SecurityManager sm = System.getSecurityManager();
171: if (sm != null) {
172: sm.checkSecurityAccess("setIdentityInfo"); //$NON-NLS-1$
173: }
174: this .info = info;
175: }
176:
177: public String getInfo() {
178: return info;
179: }
180:
181: public final boolean equals(Object obj) {
182: if (this == obj) {
183: return true;
184: }
185: if (!(obj instanceof Identity)) {
186: return false;
187: }
188: Identity i = (Identity) obj;
189: if ((name == i.name || (name != null && name.equals(i.name)))
190: && (scope == i.scope || (scope != null && scope
191: .equals(i.scope)))) {
192: return true;
193: }
194: return identityEquals(i);
195: }
196:
197: public final String getName() {
198: return name;
199: }
200:
201: public int hashCode() {
202: int hash = 0;
203: if (name != null) {
204: hash += name.hashCode();
205: }
206: if (scope != null) {
207: hash += scope.hashCode();
208: }
209: return hash;
210: }
211:
212: public String toString() {
213: SecurityManager sm = System.getSecurityManager();
214: if (sm != null) {
215: sm.checkSecurityAccess("printIdentity"); //$NON-NLS-1$
216: }
217: String s = (this .name == null ? "" : this .name);
218: if (scope != null) {
219: s += " [" + scope.getName() + "]"; //$NON-NLS-1$ //$NON-NLS-2$
220: }
221: return s;
222: }
223: }
|