8289274: Cleanup unnecessary null comparison before instanceof check in security modules

Reviewed-by: mullan
This commit is contained in:
Andrey Turbanov 2022-07-09 17:59:43 +00:00
parent 81ee7d28f8
commit 87aa3ce03e
14 changed files with 54 additions and 94 deletions

View File

@ -183,12 +183,12 @@ public final class KeychainStore extends KeyStoreSpi {
Object entry = entries.get(alias.toLowerCase());
if (entry == null || !(entry instanceof KeyEntry)) {
if (!(entry instanceof KeyEntry keyEntry)) {
return null;
}
// This call gives us a PKCS12 bag, with the key inside it.
byte[] exportedKeyInfo = _getEncodedKeyData(((KeyEntry)entry).keyRef, password);
byte[] exportedKeyInfo = _getEncodedKeyData(keyEntry.keyRef, password);
if (exportedKeyInfo == null) {
return null;
}
@ -273,11 +273,11 @@ public final class KeychainStore extends KeyStoreSpi {
Object entry = entries.get(alias.toLowerCase());
if (entry != null && entry instanceof KeyEntry) {
if (((KeyEntry)entry).chain == null) {
if (entry instanceof KeyEntry keyEntry) {
if (keyEntry.chain == null) {
return null;
} else {
return ((KeyEntry)entry).chain.clone();
return keyEntry.chain.clone();
}
} else {
return null;
@ -569,11 +569,7 @@ public final class KeychainStore extends KeyStoreSpi {
public boolean engineIsKeyEntry(String alias) {
permissionCheck();
Object entry = entries.get(alias.toLowerCase());
if ((entry != null) && (entry instanceof KeyEntry)) {
return true;
} else {
return false;
}
return entry instanceof KeyEntry;
}
/**
@ -586,11 +582,7 @@ public final class KeychainStore extends KeyStoreSpi {
public boolean engineIsCertificateEntry(String alias) {
permissionCheck();
Object entry = entries.get(alias.toLowerCase());
if ((entry != null) && (entry instanceof TrustedCertEntry)) {
return true;
} else {
return false;
}
return entry instanceof TrustedCertEntry;
}
/**

View File

@ -86,9 +86,8 @@ public sealed class RC2Cipher extends CipherSpi
protected void engineInit(int opmode, Key key,
AlgorithmParameterSpec params, SecureRandom random)
throws InvalidKeyException, InvalidAlgorithmParameterException {
if (params != null && params instanceof RC2ParameterSpec) {
embeddedCipher.initEffectiveKeyBits
(((RC2ParameterSpec)params).getEffectiveKeyBits());
if (params instanceof RC2ParameterSpec rc2Spec) {
embeddedCipher.initEffectiveKeyBits(rc2Spec.getEffectiveKeyBits());
} else {
embeddedCipher.initEffectiveKeyBits(0);
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 1999, 2021, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1999, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -241,12 +241,9 @@ public final class PrivateCredentialPermission extends Permission {
* the specified {@code Permission}, false if not.
*/
public boolean implies(Permission p) {
if (p == null || !(p instanceof PrivateCredentialPermission))
if (!(p instanceof PrivateCredentialPermission that))
return false;
PrivateCredentialPermission that = (PrivateCredentialPermission)p;
if (!impliesCredentialClass(credentialClass, that.credentialClass))
return false;
@ -524,11 +521,9 @@ public final class PrivateCredentialPermission extends Permission {
}
public boolean implies(Object obj) {
if (obj == null || !(obj instanceof CredOwner))
if (!(obj instanceof CredOwner that))
return false;
CredOwner that = (CredOwner)obj;
if (principalClass.equals("*") ||
principalClass.equals(that.principalClass)) {

View File

@ -300,7 +300,7 @@ public final class PKCS12KeyStore extends KeyStoreSpi {
Entry entry = entries.get(alias.toLowerCase(Locale.ENGLISH));
Key key = null;
if (entry == null || (!(entry instanceof KeyEntry))) {
if (!(entry instanceof KeyEntry)) {
return null;
}
@ -471,18 +471,18 @@ public final class PKCS12KeyStore extends KeyStoreSpi {
*/
public Certificate[] engineGetCertificateChain(String alias) {
Entry entry = entries.get(alias.toLowerCase(Locale.ENGLISH));
if (entry != null && entry instanceof PrivateKeyEntry) {
if (((PrivateKeyEntry) entry).chain == null) {
if (entry instanceof PrivateKeyEntry privateKeyEntry) {
if (privateKeyEntry.chain == null) {
return null;
} else {
if (debug != null) {
debug.println("Retrieved a " +
((PrivateKeyEntry) entry).chain.length +
privateKeyEntry.chain.length +
"-certificate chain at alias '" + alias + "'");
}
return ((PrivateKeyEntry) entry).chain.clone();
return privateKeyEntry.chain.clone();
}
} else {
return null;
@ -1012,7 +1012,7 @@ public final class PKCS12KeyStore extends KeyStoreSpi {
}
Entry entry = entries.get(alias.toLowerCase(Locale.ENGLISH));
if (entry != null && entry instanceof KeyEntry) {
if (entry instanceof KeyEntry) {
throw new KeyStoreException("Cannot overwrite own certificate");
}
@ -1095,11 +1095,7 @@ public final class PKCS12KeyStore extends KeyStoreSpi {
*/
public boolean engineIsKeyEntry(String alias) {
Entry entry = entries.get(alias.toLowerCase(Locale.ENGLISH));
if (entry != null && entry instanceof KeyEntry) {
return true;
} else {
return false;
}
return entry instanceof KeyEntry;
}
/**
@ -1111,8 +1107,8 @@ public final class PKCS12KeyStore extends KeyStoreSpi {
*/
public boolean engineIsCertificateEntry(String alias) {
Entry entry = entries.get(alias.toLowerCase(Locale.ENGLISH));
if (entry != null && entry instanceof CertEntry &&
((CertEntry) entry).trustedKeyUsage != null) {
if (entry instanceof CertEntry certEntry &&
certEntry.trustedKeyUsage != null) {
return true;
} else {
return false;
@ -1144,10 +1140,10 @@ public final class PKCS12KeyStore extends KeyStoreSpi {
Entry entry = entries.get(alias.toLowerCase(Locale.ENGLISH));
if (entryClass == KeyStore.PrivateKeyEntry.class) {
return (entry != null && entry instanceof PrivateKeyEntry);
return (entry instanceof PrivateKeyEntry);
}
if (entryClass == KeyStore.SecretKeyEntry.class) {
return (entry != null && entry instanceof SecretKeyEntry);
return (entry instanceof SecretKeyEntry);
}
return false;
}
@ -1827,11 +1823,10 @@ public final class PKCS12KeyStore extends KeyStoreSpi {
String alias = e.nextElement();
Entry entry = entries.get(alias);
if (entry == null || (!(entry instanceof KeyEntry))) {
if (!(entry instanceof KeyEntry keyEntry)) {
continue;
}
DerOutputStream safeBag = new DerOutputStream();
KeyEntry keyEntry = (KeyEntry) entry;
// DER encode the private key
if (keyEntry instanceof PrivateKeyEntry) {

View File

@ -146,7 +146,7 @@ public abstract sealed class JavaKeyStore extends KeyStoreSpi {
{
Object entry = entries.get(convertAlias(alias));
if (entry == null || !(entry instanceof KeyEntry)) {
if (!(entry instanceof KeyEntry keyEntry)) {
return null;
}
if (password == null) {
@ -155,7 +155,7 @@ public abstract sealed class JavaKeyStore extends KeyStoreSpi {
byte[] passwordBytes = convertToBytes(password);
KeyProtector keyProtector = new KeyProtector(passwordBytes);
byte[] encrBytes = ((KeyEntry)entry).protectedPrivKey;
byte[] encrBytes = keyEntry.protectedPrivKey;
EncryptedPrivateKeyInfo encrInfo;
try {
encrInfo = new EncryptedPrivateKeyInfo(encrBytes);
@ -183,11 +183,11 @@ public abstract sealed class JavaKeyStore extends KeyStoreSpi {
public Certificate[] engineGetCertificateChain(String alias) {
Object entry = entries.get(convertAlias(alias));
if (entry != null && entry instanceof KeyEntry) {
if (((KeyEntry)entry).chain == null) {
if (entry instanceof KeyEntry keyEntry) {
if (keyEntry.chain == null) {
return null;
} else {
return ((KeyEntry)entry).chain.clone();
return keyEntry.chain.clone();
}
} else {
return null;
@ -384,7 +384,7 @@ public abstract sealed class JavaKeyStore extends KeyStoreSpi {
synchronized(entries) {
Object entry = entries.get(convertAlias(alias));
if ((entry != null) && (entry instanceof KeyEntry)) {
if (entry instanceof KeyEntry) {
throw new KeyStoreException
("Cannot overwrite own certificate");
}
@ -449,11 +449,7 @@ public abstract sealed class JavaKeyStore extends KeyStoreSpi {
*/
public boolean engineIsKeyEntry(String alias) {
Object entry = entries.get(convertAlias(alias));
if ((entry != null) && (entry instanceof KeyEntry)) {
return true;
} else {
return false;
}
return entry instanceof KeyEntry;
}
/**
@ -465,11 +461,7 @@ public abstract sealed class JavaKeyStore extends KeyStoreSpi {
*/
public boolean engineIsCertificateEntry(String alias) {
Object entry = entries.get(convertAlias(alias));
if ((entry != null) && (entry instanceof TrustedCertEntry)) {
return true;
} else {
return false;
}
return entry instanceof TrustedCertEntry;
}
/**

View File

@ -1819,7 +1819,7 @@ public class PolicyFile extends java.security.Policy {
return null;
}
if (cert == null || !(cert instanceof X509Certificate)) {
if (!(cert instanceof X509Certificate x509Cert)) {
if (debug != null) {
debug.println(" -- No certificate for '" +
alias +
@ -1827,8 +1827,6 @@ public class PolicyFile extends java.security.Policy {
}
return null;
} else {
X509Certificate x509Cert = (X509Certificate)cert;
// 4702543: X500 names with an EmailAddress
// were encoded incorrectly. create new
// X500Principal name with correct encoding

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 1999, 2021, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1999, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -164,17 +164,14 @@ class SubjectCodeSource extends CodeSource implements java.io.Serializable {
LinkedList<PrincipalEntry> subjectList = null;
if (codesource == null ||
!(codesource instanceof SubjectCodeSource) ||
!(super.implies(codesource))) {
if (!(codesource instanceof SubjectCodeSource that) ||
!super.implies(codesource)) {
if (debug != null)
debug.println("\tSubjectCodeSource.implies: FAILURE 1");
return false;
}
SubjectCodeSource that = (SubjectCodeSource)codesource;
// if the principal list in the policy "implies"
// the Subject associated with the current AccessControlContext,
// then return true

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2003, 2020, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2003, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -202,11 +202,10 @@ public class CertId {
if (this == other) {
return true;
}
if (other == null || (!(other instanceof CertId))) {
if (!(other instanceof CertId that)) {
return false;
}
CertId that = (CertId) other;
if (hashAlgId.equals(that.getHashAlgorithm()) &&
Arrays.equals(issuerNameHash, that.getIssuerNameHash()) &&
Arrays.equals(issuerKeyHash, that.getIssuerKeyHash()) &&

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2012, 2021, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2012, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -488,10 +488,10 @@ class RevocationChecker extends PKIXRevocationChecker {
}
break;
case "SSLServer":
result = (t != null && t instanceof IOException);
result = (t instanceof IOException);
break;
case "URI":
result = (t != null && t instanceof IOException);
result = (t instanceof IOException);
break;
default:
// we don't know about any other remote CertStore types

View File

@ -170,9 +170,7 @@ public class BitArray {
public boolean equals(Object obj) {
if (obj == this) return true;
if (obj == null || !(obj instanceof BitArray)) return false;
BitArray ba = (BitArray) obj;
if (!(obj instanceof BitArray ba)) return false;
if (ba.length != length) return false;

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2003, 2020, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2003, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -87,10 +87,9 @@ public final class AccessDescription {
}
public boolean equals(Object obj) {
if (obj == null || (!(obj instanceof AccessDescription))) {
if (!(obj instanceof AccessDescription that)) {
return false;
}
AccessDescription that = (AccessDescription)obj;
if (this == that) {
return true;

View File

@ -915,9 +915,8 @@ final class P11KeyStore extends KeyStoreSpi {
token.ensureValid();
if (protParam != null &&
protParam instanceof KeyStore.PasswordProtection &&
((KeyStore.PasswordProtection)protParam).getPassword() != null &&
if (protParam instanceof PasswordProtection pp &&
pp.getPassword() != null &&
!token.config.getKeyStoreCompatibilityMode()) {
throw new KeyStoreException("ProtectionParameter must be null");
}
@ -1018,9 +1017,8 @@ final class P11KeyStore extends KeyStoreSpi {
token.ensureValid();
checkWrite();
if (protParam != null &&
protParam instanceof KeyStore.PasswordProtection &&
((KeyStore.PasswordProtection)protParam).getPassword() != null &&
if (protParam instanceof PasswordProtection pp &&
pp.getPassword() != null &&
!token.config.getKeyStoreCompatibilityMode()) {
throw new KeyStoreException(new UnsupportedOperationException
("ProtectionParameter must be null"));

View File

@ -662,16 +662,14 @@ public class KeyStoreLoginModule implements LoginModule {
principal = certificate.getSubjectX500Principal();
// if token, privateKeyPassword will be null
Key privateKey = keyStore.getKey(keyStoreAlias, privateKeyPassword);
if (privateKey == null
|| !(privateKey instanceof PrivateKey))
{
Key key = keyStore.getKey(keyStoreAlias, privateKeyPassword);
if (!(key instanceof PrivateKey privateKey)) {
throw new FailedLoginException(
"Unable to recover key from keystore");
}
privateCredential = new X500PrivateCredential(
certificate, (PrivateKey) privateKey, keyStoreAlias);
certificate, privateKey, keyStoreAlias);
} catch (KeyStoreException | NoSuchAlgorithmException e) {
LoginException le = new LoginException("Error using keystore");
le.initCause(e);

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2000, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -112,8 +112,8 @@ final class GssKrb5Client extends GssKrb5Base implements SaslClient {
GSSCredential credentials = null;
if (props != null) {
Object prop = props.get(Sasl.CREDENTIALS);
if (prop != null && prop instanceof GSSCredential) {
credentials = (GSSCredential) prop;
if (prop instanceof GSSCredential c) {
credentials = c;
logger.log(Level.FINE,
"KRB5CLNT01:Using the credentials supplied in " +
"javax.security.sasl.credentials");