1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18 package java.security; 19 20 import java.io.Serializable; 21 import java.util.Arrays; 22 import java.util.Vector; 23 import libcore.util.Objects; 24 25 /** 26 * {@code Identity} represents an identity like a person or a company. 27 * 28 * @deprecated The functionality of this class has been replace by 29 * {@link Principal}, {@link KeyStore} and the {@code 30 * java.security.cert} package. 31 */ 32 @Deprecated 33 public abstract class Identity implements Principal, Serializable { 34 private static final long serialVersionUID = 3609922007826600659L; 35 36 private String name; 37 38 private PublicKey publicKey; 39 40 private String info = "no additional info"; 41 42 private IdentityScope scope; 43 44 private Vector<Certificate> certificates; 45 46 /** 47 * Constructs a new instance of {@code Identity}. 48 */ 49 protected Identity() { 50 } 51 52 /** 53 * Creates a new instance of {@code Identity} with the specified name. 54 * 55 * @param name 56 * the name of this {@code Identity}. 57 */ 58 public Identity(String name) { 59 this.name = name; 60 } 61 62 /** 63 * Creates a new instance of {@code Identity} with the specified name and 64 * the scope of this {@code Identity}. 65 * 66 * @param name 67 * the name of this {@code Identity}. 68 * @param scope 69 * the {@code IdentityScope} of this {@code Identity}. 70 * @throws KeyManagementException 71 * if an {@code Identity} with the same name is already present 72 * in the specified scope. 73 */ 74 public Identity(String name, IdentityScope scope) 75 throws KeyManagementException { 76 this(name); 77 if (scope != null) { 78 scope.addIdentity(this); 79 this.scope = scope; 80 } 81 } 82 83 /** 84 * Adds a {@code Certificate} to this {@code Identity}. 85 * 86 * @param certificate 87 * the {@code Certificate} to be added to this {@code Identity}. 88 * @throws KeyManagementException 89 * if the certificate is not valid. 90 */ 91 public void addCertificate(Certificate certificate) throws KeyManagementException { 92 PublicKey certPK = certificate.getPublicKey(); 93 if (publicKey != null) { 94 if (!checkKeysEqual(publicKey, certPK)) { 95 throw new KeyManagementException("Cert's public key does not match Identity's public key"); 96 } 97 } else { 98 publicKey = certPK; 99 } 100 if (certificates == null) { 101 certificates = new Vector<Certificate>(); 102 } 103 certificates.add(certificate); 104 } 105 106 107 108 109 private static boolean checkKeysEqual(PublicKey pk1, PublicKey pk2) { 110 // first, they should have the same format 111 // second, their encoded form must be the same 112 113 // assert(pk1 != null); 114 // assert(pk2 != null); 115 116 String format1 = pk1.getFormat(); 117 String format2; 118 if ((pk2 == null) 119 || (((format2 = pk2.getFormat()) != null) ^ (format1 != null)) 120 || ((format1 != null) && !format1.equals(format2))) { 121 return false; 122 } 123 124 return Arrays.equals(pk1.getEncoded(), pk2.getEncoded()); 125 } 126 127 128 129 130 /** 131 * Removes the specified {@code Certificate} from this {@code Identity}. 132 * 133 * @param certificate 134 * the {@code Certificate} to be removed. 135 * @throws KeyManagementException 136 * if the certificate is not found. 137 */ 138 public void removeCertificate(Certificate certificate) throws KeyManagementException { 139 if (certificates != null) { 140 if (!certificates.contains(certificate)) { 141 throw new KeyManagementException("Certificate not found"); 142 } 143 certificates.removeElement(certificate); 144 } 145 } 146 147 148 149 150 /** 151 * Returns the certificates for this {@code Identity}. External 152 * modifications of the returned array has no impact on this {@code 153 * Identity}. 154 * 155 * @return the {@code Certificates} for this {@code Identity} 156 */ 157 public Certificate[] certificates() { 158 if (certificates == null) { 159 return new Certificate[0]; 160 } 161 Certificate[] ret = new Certificate[certificates.size()]; 162 certificates.copyInto(ret); 163 return ret; 164 } 165 166 167 168 169 /** 170 * Compares the specified {@code Identity} with this {@code Identity} for 171 * equality and returns {@code true} if the specified object is equal, 172 * {@code false} otherwise. 173 * <p> 174 * To be equal, two {@code Identity} objects need to have the same name and 175 * the same public keys. 176 * 177 * @param identity 178 * the identity to check for equality. 179 * @return {@code true} if the {@code Identity} objects are equal, {@code 180 * false} otherwise. 181 */ 182 protected boolean identityEquals(Identity identity) { 183 if (!name.equals(identity.name)) { 184 return false; 185 } 186 187 if (publicKey == null) { 188 return (identity.publicKey == null); 189 } 190 191 return checkKeysEqual(publicKey, identity.publicKey); 192 } 193 194 195 196 197 /** 198 * Returns a string containing a concise, human-readable description of the 199 * this {@code Identity}. 200 * 201 * @param detailed 202 * whether or not this method should return detailed information. 203 * @return a printable representation for this {@code Permission}. 204 */ 205 public String toString(boolean detailed) { 206 String s = toString(); 207 if (detailed) { 208 s += " " + info; 209 } 210 return s; 211 } 212 213 214 215 216 /** 217 * Returns the {@code IdentityScope} of this {@code Identity}. 218 * 219 * @return the {@code IdentityScope} of this {@code Identity}. 220 */ 221 public final IdentityScope getScope() { 222 return scope; 223 } 224 225 226 227 228 /** 229 * Sets the specified {@code PublicKey} to this {@code Identity}. 230 * 231 * @param key 232 * the {@code PublicKey} to be set. 233 * @throws KeyManagementException 234 * if another {@code Identity} in the same scope as this {@code 235 * Identity} already has the same {@code PublicKey}. 236 */ 237 public void setPublicKey(PublicKey key) throws KeyManagementException { 238 // this check does not always work 239 if ((scope != null) && (key != null)) { 240 Identity i = scope.getIdentity(key); 241 //System.out.println("###DEBUG## Identity: "+i); 242 if ((i != null) && (i != this)) { 243 throw new KeyManagementException("key already used in scope"); 244 } 245 } 246 this.publicKey = key; 247 certificates = null; 248 } 249 250 251 252 253 /** 254 * Returns the {@code PublicKey} associated with this {@code Identity}. 255 * 256 * @return the {@code PublicKey} associated with this {@code Identity}. 257 */ 258 public PublicKey getPublicKey() { 259 return publicKey; 260 } 261 262 263 264 265 /** 266 * Sets an information string for this {@code Identity}. 267 * @param info 268 * the information to be set. 269 */ 270 public void setInfo(String info) { 271 this.info = info; 272 } 273 274 /** 275 * Returns the information string of this {@code Identity}. 276 * 277 * @return the information string of this {@code Identity}. 278 */ 279 public String getInfo() { 280 return info; 281 } 282 283 /** 284 * Compares the specified object with this {@code Identity} for equality and 285 * returns {@code true} if the specified object is equal, {@code false} 286 * otherwise. {@code Identity} objects are considered equal, if they have 287 * the same name and are in the same scope. 288 * 289 * @param obj 290 * object to be compared for equality with this {@code 291 * Identity}. 292 * @return {@code true} if the specified object is equal to this {@code 293 * Identity}, otherwise {@code false}. 294 */ 295 @Override 296 public final boolean equals(Object obj) { 297 if (this == obj) { 298 return true; 299 } 300 if (!(obj instanceof Identity)) { 301 return false; 302 } 303 Identity i = (Identity) obj; 304 if (Objects.equal(name, i.name) && (Objects.equal(scope, i.scope))) { 305 return true; 306 } 307 return identityEquals(i); 308 } 309 310 /** 311 * Returns the name of this {@code Identity}. 312 * 313 * @return the name of this {@code Identity}. 314 */ 315 public final String getName() { 316 return name; 317 } 318 319 /** 320 * Returns the hash code value for this {@code Identity}. Returns the same 321 * hash code for {@code Identity}s that are equal to each other as required 322 * by the general contract of {@link Object#hashCode}. 323 * 324 * @return the hash code value for this {@code Identity}. 325 * @see Object#equals(Object) 326 * @see Identity#equals(Object) 327 */ 328 @Override 329 public int hashCode() { 330 int hash = 0; 331 if (name != null) { 332 hash += name.hashCode(); 333 } 334 if (scope != null) { 335 hash += scope.hashCode(); 336 } 337 return hash; 338 } 339 340 /** 341 * Returns a string containing a concise, human-readable description of the 342 * this {@code Identity} including its name and its scope. 343 * 344 * @return a printable representation for this {@code Identity}. 345 */ 346 @Override 347 public String toString() { 348 String s = (this.name == null ? "" : this.name); 349 if (scope != null) { 350 s += " [" + scope.getName() + "]"; 351 } 352 return s; 353 } 354 } 355