Port sun.security.x509* changes from openJdk8u121-b13

changeset:   11301:b718a9c701f2
user:        igerasim
date:        Tue Sep 08 12:56:41 2015 +0300
summary:     8074068: Cleanup in java.base/share/classes/sun/security/x509/

Test: CtsLibcoreTestCases
Bug: 36461944
Change-Id: Ic9b6eab775981d7065f1e887d08d562f30e62df9
diff --git a/ojluni/src/main/java/sun/security/x509/CRLDistributionPointsExtension.java b/ojluni/src/main/java/sun/security/x509/CRLDistributionPointsExtension.java
index c381430..9062a53 100644
--- a/ojluni/src/main/java/sun/security/x509/CRLDistributionPointsExtension.java
+++ b/ojluni/src/main/java/sun/security/x509/CRLDistributionPointsExtension.java
@@ -29,6 +29,7 @@
 import java.io.OutputStream;
 
 import java.util.*;
+import java.util.Collections;
 
 import sun.security.util.DerOutputStream;
 import sun.security.util.DerValue;
@@ -255,11 +256,12 @@
      */
     public void delete(String name) throws IOException {
         if (name.equalsIgnoreCase(POINTS)) {
-            distributionPoints = new ArrayList<DistributionPoint>();
+            distributionPoints =
+                    Collections.<DistributionPoint>emptyList();
         } else {
             throw new IOException("Attribute name [" + name +
-                                "] not recognized by " +
-                                "CertAttrSet:" + extensionName + ".");
+                                  "] not recognized by " +
+                                  "CertAttrSet:" + extensionName + '.');
         }
         encodeThis();
     }
diff --git a/ojluni/src/main/java/sun/security/x509/CRLNumberExtension.java b/ojluni/src/main/java/sun/security/x509/CRLNumberExtension.java
index 9649df6..3abeb01 100644
--- a/ojluni/src/main/java/sun/security/x509/CRLNumberExtension.java
+++ b/ojluni/src/main/java/sun/security/x509/CRLNumberExtension.java
@@ -157,11 +157,10 @@
      */
     public BigInteger get(String name) throws IOException {
         if (name.equalsIgnoreCase(NUMBER)) {
-            if (crlNumber == null) return null;
-            else return crlNumber;
+            return crlNumber;
         } else {
-          throw new IOException("Attribute name not recognized by"
-                                + " CertAttrSet:" + extensionName + ".");
+            throw new IOException("Attribute name not recognized by" +
+                                  " CertAttrSet:" + extensionName + '.');
         }
     }
 
diff --git a/ojluni/src/main/java/sun/security/x509/DNSName.java b/ojluni/src/main/java/sun/security/x509/DNSName.java
index 2a35b86..0946dd1b 100644
--- a/ojluni/src/main/java/sun/security/x509/DNSName.java
+++ b/ojluni/src/main/java/sun/security/x509/DNSName.java
@@ -232,15 +232,15 @@
      * @throws UnsupportedOperationException if not supported for this name type
      */
     public int subtreeDepth() throws UnsupportedOperationException {
-        String subtree=name;
-        int i=1;
+        // subtree depth is always at least 1
+        int sum = 1;
 
-        /* count dots */
-        for (; subtree.lastIndexOf('.') >= 0; i++) {
-            subtree=subtree.substring(0,subtree.lastIndexOf('.'));
+        // count dots
+        for (int i = name.indexOf('.'); i >= 0; i = name.indexOf('.', i + 1)) {
+            ++sum;
         }
 
-        return i;
+        return sum;
     }
 
 }
diff --git a/ojluni/src/main/java/sun/security/x509/EDIPartyName.java b/ojluni/src/main/java/sun/security/x509/EDIPartyName.java
index feaf30b..513e337 100644
--- a/ojluni/src/main/java/sun/security/x509/EDIPartyName.java
+++ b/ojluni/src/main/java/sun/security/x509/EDIPartyName.java
@@ -197,7 +197,7 @@
      */
     public int hashCode() {
         if (myhash == -1) {
-            myhash = 37 + party.hashCode();
+            myhash = 37 + (party == null ? 1 : party.hashCode());
             if (assigner != null) {
                 myhash = 37 * myhash + assigner.hashCode();
             }
diff --git a/ojluni/src/main/java/sun/security/x509/GeneralSubtrees.java b/ojluni/src/main/java/sun/security/x509/GeneralSubtrees.java
index 9c5e621..e5c9e73 100644
--- a/ojluni/src/main/java/sun/security/x509/GeneralSubtrees.java
+++ b/ojluni/src/main/java/sun/security/x509/GeneralSubtrees.java
@@ -191,7 +191,7 @@
         // the list: if any subsequent entry matches or widens entry n,
         // remove entry n. If any subsequent entries narrow entry n, remove
         // the subsequent entries.
-        for (int i = 0; i < size(); i++) {
+        for (int i = 0; i < (size() - 1); i++) {
             GeneralNameInterface current = getGeneralNameInterface(i);
             boolean remove1 = false;
 
diff --git a/ojluni/src/main/java/sun/security/x509/IPAddressName.java b/ojluni/src/main/java/sun/security/x509/IPAddressName.java
index 084a6f3..e558c9e 100644
--- a/ojluni/src/main/java/sun/security/x509/IPAddressName.java
+++ b/ojluni/src/main/java/sun/security/x509/IPAddressName.java
@@ -197,8 +197,10 @@
 
             // append a mask corresponding to the num of prefix bits specified
             int prefixLen = Integer.parseInt(name.substring(slashNdx+1));
-            if (prefixLen > 128)
-                throw new IOException("IPv6Address prefix is longer than 128");
+            if (prefixLen < 0 || prefixLen > 128) {
+                throw new IOException("IPv6Address prefix length (" +
+                        prefixLen + ") in out of valid range [0,128]");
+            }
 
             // create new bit array initialized to zeros
             BitArray bitArray = new BitArray(MASKSIZE * 8);
@@ -317,7 +319,8 @@
         if (!(obj instanceof IPAddressName))
             return false;
 
-        byte[] other = ((IPAddressName)obj).getBytes();
+        IPAddressName otherName = (IPAddressName)obj;
+        byte[] other = otherName.address;
 
         if (other.length != address.length)
             return false;
@@ -326,12 +329,10 @@
             // Two subnet addresses
             // Mask each and compare masked values
             int maskLen = address.length/2;
-            byte[] maskedThis = new byte[maskLen];
-            byte[] maskedOther = new byte[maskLen];
             for (int i=0; i < maskLen; i++) {
-                maskedThis[i] = (byte)(address[i] & address[i+maskLen]);
-                maskedOther[i] = (byte)(other[i] & other[i+maskLen]);
-                if (maskedThis[i] != maskedOther[i]) {
+                byte maskedThis = (byte)(address[i] & address[i+maskLen]);
+                byte maskedOther = (byte)(other[i] & other[i+maskLen]);
+                if (maskedThis != maskedOther) {
                     return false;
                 }
             }
@@ -400,7 +401,8 @@
         else if (((IPAddressName)inputName).equals(this))
             constraintType = NAME_MATCH;
         else {
-            byte[] otherAddress = ((IPAddressName)inputName).getBytes();
+            IPAddressName otherName = (IPAddressName)inputName;
+            byte[] otherAddress = otherName.address;
             if (otherAddress.length == 4 && address.length == 4)
                 // Two host addresses
                 constraintType = NAME_SAME_TYPE;
diff --git a/ojluni/src/main/java/sun/security/x509/IssuingDistributionPointExtension.java b/ojluni/src/main/java/sun/security/x509/IssuingDistributionPointExtension.java
index 6a93258..6fe8eb3 100644
--- a/ojluni/src/main/java/sun/security/x509/IssuingDistributionPointExtension.java
+++ b/ojluni/src/main/java/sun/security/x509/IssuingDistributionPointExtension.java
@@ -261,6 +261,7 @@
                 throw new IOException(
                     "Attribute value should be of type ReasonFlags.");
             }
+            revocationReasons = (ReasonFlags)obj;
 
         } else if (name.equalsIgnoreCase(INDIRECT_CRL)) {
             if (!(obj instanceof Boolean)) {
@@ -290,7 +291,6 @@
             }
             hasOnlyAttributeCerts = ((Boolean)obj).booleanValue();
 
-
         } else {
             throw new IOException("Attribute name [" + name +
                 "] not recognized by " +
diff --git a/ojluni/src/main/java/sun/security/x509/KeyIdentifier.java b/ojluni/src/main/java/sun/security/x509/KeyIdentifier.java
index 01d4803..016fd4e 100644
--- a/ojluni/src/main/java/sun/security/x509/KeyIdentifier.java
+++ b/ojluni/src/main/java/sun/security/x509/KeyIdentifier.java
@@ -148,7 +148,7 @@
             return true;
         if (!(other instanceof KeyIdentifier))
             return false;
-        return java.util.Arrays.equals(octetString,
-                                       ((KeyIdentifier)other).getIdentifier());
+        byte[] otherString = ((KeyIdentifier)other).octetString;
+        return java.util.Arrays.equals(octetString, otherString);
     }
 }
diff --git a/ojluni/src/main/java/sun/security/x509/PolicyMappingsExtension.java b/ojluni/src/main/java/sun/security/x509/PolicyMappingsExtension.java
index fba5104..e259826 100644
--- a/ojluni/src/main/java/sun/security/x509/PolicyMappingsExtension.java
+++ b/ojluni/src/main/java/sun/security/x509/PolicyMappingsExtension.java
@@ -102,7 +102,7 @@
     public PolicyMappingsExtension() {
         extensionId = PKIXExtensions.KeyUsage_Id;
         critical = false;
-        maps = new ArrayList<CertificatePolicyMap>();
+        maps = Collections.<CertificatePolicyMap>emptyList();
     }
 
     /**
diff --git a/ojluni/src/main/java/sun/security/x509/PrivateKeyUsageExtension.java b/ojluni/src/main/java/sun/security/x509/PrivateKeyUsageExtension.java
index c20d855..512880b 100644
--- a/ojluni/src/main/java/sun/security/x509/PrivateKeyUsageExtension.java
+++ b/ojluni/src/main/java/sun/security/x509/PrivateKeyUsageExtension.java
@@ -33,6 +33,7 @@
 import java.security.cert.CertificateNotYetValidException;
 import java.util.Date;
 import java.util.Enumeration;
+import java.util.Objects;
 
 import sun.security.util.*;
 
@@ -206,16 +207,17 @@
      */
     public void valid(Date now)
     throws CertificateNotYetValidException, CertificateExpiredException {
+        Objects.requireNonNull(now);
         /*
          * we use the internal Dates rather than the passed in Date
          * because someone could override the Date methods after()
          * and before() to do something entirely different.
          */
-        if (notBefore.after(now)) {
+        if (notBefore != null && notBefore.after(now)) {
             throw new CertificateNotYetValidException("NotBefore: " +
                                                       notBefore.toString());
         }
-        if (notAfter.before(now)) {
+        if (notAfter != null && notAfter.before(now)) {
             throw new CertificateExpiredException("NotAfter: " +
                                                   notAfter.toString());
         }
diff --git a/ojluni/src/main/java/sun/security/x509/RDN.java b/ojluni/src/main/java/sun/security/x509/RDN.java
index ed46abe..5a4c243 100644
--- a/ojluni/src/main/java/sun/security/x509/RDN.java
+++ b/ojluni/src/main/java/sun/security/x509/RDN.java
@@ -28,6 +28,8 @@
 
 import java.io.IOException;
 import java.io.StringReader;
+import java.util.Arrays;
+import java.util.StringJoiner;
 import java.util.*;
 
 import sun.security.util.*;
@@ -443,31 +445,19 @@
                                assertion[0].toRFC2253String(oidMap);
         }
 
-        StringBuilder relname = new StringBuilder();
-        if (!canonical) {
-            for (int i = 0; i < assertion.length; i++) {
-                if (i > 0) {
-                    relname.append('+');
-                }
-                relname.append(assertion[i].toRFC2253String(oidMap));
-            }
-        } else {
+        AVA[] toOutput = assertion;
+        if (canonical) {
             // order the string type AVA's alphabetically,
             // followed by the oid type AVA's numerically
-            List<AVA> avaList = new ArrayList<AVA>(assertion.length);
-            for (int i = 0; i < assertion.length; i++) {
-                avaList.add(assertion[i]);
-            }
-            java.util.Collections.sort(avaList, AVAComparator.getInstance());
-
-            for (int i = 0; i < avaList.size(); i++) {
-                if (i > 0) {
-                    relname.append('+');
-                }
-                relname.append(avaList.get(i).toRFC2253CanonicalString());
-            }
+            toOutput = assertion.clone();
+            Arrays.sort(toOutput, AVAComparator.getInstance());
         }
-        return relname.toString();
+        StringJoiner sj = new StringJoiner("+");
+        for (AVA ava : toOutput) {
+            sj.add(canonical ? ava.toRFC2253CanonicalString()
+                             : ava.toRFC2253String(oidMap));
+        }
+        return sj.toString();
     }
 
 }
diff --git a/ojluni/src/main/java/sun/security/x509/SubjectInfoAccessExtension.java b/ojluni/src/main/java/sun/security/x509/SubjectInfoAccessExtension.java
index 2f851f7..e1a0bc2 100644
--- a/ojluni/src/main/java/sun/security/x509/SubjectInfoAccessExtension.java
+++ b/ojluni/src/main/java/sun/security/x509/SubjectInfoAccessExtension.java
@@ -28,6 +28,7 @@
 import java.io.IOException;
 import java.io.OutputStream;
 
+import java.util.Collections;
 import java.util.*;
 
 import sun.security.util.DerOutputStream;
@@ -200,7 +201,8 @@
      */
     public void delete(String name) throws IOException {
         if (name.equalsIgnoreCase(DESCRIPTIONS)) {
-            accessDescriptions = new ArrayList<AccessDescription>();
+            accessDescriptions =
+                Collections.<AccessDescription>emptyList();
         } else {
             throw new IOException("Attribute name [" + name +
                                 "] not recognized by " +
diff --git a/ojluni/src/main/java/sun/security/x509/URIName.java b/ojluni/src/main/java/sun/security/x509/URIName.java
index 034117e..5f4021e 100644
--- a/ojluni/src/main/java/sun/security/x509/URIName.java
+++ b/ojluni/src/main/java/sun/security/x509/URIName.java
@@ -165,7 +165,7 @@
             String host = uri.getSchemeSpecificPart();
             try {
                 DNSName hostDNS;
-                if (host.charAt(0) == '.') {
+                if (host.startsWith(".")) {
                     hostDNS = new DNSName(host.substring(1));
                 } else {
                     hostDNS = new DNSName(host);
diff --git a/ojluni/src/main/java/sun/security/x509/X500Name.java b/ojluni/src/main/java/sun/security/x509/X500Name.java
index 6090554..166ce3c 100644
--- a/ojluni/src/main/java/sun/security/x509/X500Name.java
+++ b/ojluni/src/main/java/sun/security/x509/X500Name.java
@@ -347,6 +347,8 @@
             for (int i = 0; i < names.length; i++) {
                 list.addAll(names[i].avas());
             }
+            list = Collections.unmodifiableList(list);
+            allAvaList = list;
         }
         return list;
     }
@@ -365,9 +367,6 @@
      */
     public boolean isEmpty() {
         int n = names.length;
-        if (n == 0) {
-            return true;
-        }
         for (int i = 0; i < n; i++) {
             if (names[i].assertion.length != 0) {
                 return false;
@@ -1130,12 +1129,8 @@
      * and speed recognition of common X.500 attributes.
      */
     static ObjectIdentifier intern(ObjectIdentifier oid) {
-        ObjectIdentifier interned = internedOIDs.get(oid);
-        if (interned != null) {
-            return interned;
-        }
-        internedOIDs.put(oid, oid);
-        return oid;
+        ObjectIdentifier interned = internedOIDs.putIfAbsent(oid, oid);
+        return (interned == null) ? oid : interned;
     }
 
     private static final Map<ObjectIdentifier,ObjectIdentifier> internedOIDs
diff --git a/ojluni/src/main/java/sun/security/x509/X509AttributeName.java b/ojluni/src/main/java/sun/security/x509/X509AttributeName.java
index 090792a..c60b6f4 100644
--- a/ojluni/src/main/java/sun/security/x509/X509AttributeName.java
+++ b/ojluni/src/main/java/sun/security/x509/X509AttributeName.java
@@ -47,7 +47,7 @@
      */
     public X509AttributeName(String name) {
         int i = name.indexOf(SEPARATOR);
-        if (i == (-1)) {
+        if (i < 0) {
             prefix = name;
         } else {
             prefix = name.substring(0, i);
diff --git a/ojluni/src/main/java/sun/security/x509/X509CRLImpl.java b/ojluni/src/main/java/sun/security/x509/X509CRLImpl.java
index 52e9818..e0021c8 100644
--- a/ojluni/src/main/java/sun/security/x509/X509CRLImpl.java
+++ b/ojluni/src/main/java/sun/security/x509/X509CRLImpl.java
@@ -748,9 +748,7 @@
     public byte[] getTBSCertList() throws CRLException {
         if (tbsCertList == null)
             throw new CRLException("Uninitialized CRL");
-        byte[] dup = new byte[tbsCertList.length];
-        System.arraycopy(tbsCertList, 0, dup, 0, dup.length);
-        return dup;
+        return tbsCertList.clone();
     }
 
     /**
@@ -761,9 +759,7 @@
     public byte[] getSignature() {
         if (signature == null)
             return null;
-        byte[] dup = new byte[signature.length];
-        System.arraycopy(signature, 0, dup, 0, dup.length);
-        return dup;
+        return signature.clone();
     }
 
     /**
diff --git a/ojluni/src/main/java/sun/security/x509/X509CertImpl.java b/ojluni/src/main/java/sun/security/x509/X509CertImpl.java
index de4d313..6060b56 100644
--- a/ojluni/src/main/java/sun/security/x509/X509CertImpl.java
+++ b/ojluni/src/main/java/sun/security/x509/X509CertImpl.java
@@ -1023,9 +1023,7 @@
     public byte[] getSignature() {
         if (signature == null)
             return null;
-        byte[] dup = new byte[signature.length];
-        System.arraycopy(signature, 0, dup, 0, dup.length);
-        return dup;
+        return signature.clone();
     }
 
     /**