From 68301733ca0d4eac01b2f6a5e1304241347db270 Mon Sep 17 00:00:00 2001 From: xuelei Date: Mon, 29 Aug 2011 05:55:26 -0700 Subject: [PATCH] 7059542: JNDI name operations should be locale independent Reviewed-by: weijun --- .../classes/com/sun/jndi/ldap/ClientId.java | 13 +-- .../classes/com/sun/jndi/ldap/LdapClient.java | 10 ++- .../classes/com/sun/jndi/ldap/LdapCtx.java | 3 +- .../classes/com/sun/jndi/ldap/LdapName.java | 12 +-- .../com/sun/jndi/ldap/LdapPoolManager.java | 3 +- .../sun/jndi/toolkit/dir/HierMemDirCtx.java | 2 +- .../sun/jndi/toolkit/dir/SearchFilter.java | 15 ++-- .../classes/com/sun/security/ntlm/NTLM.java | 4 +- src/share/classes/java/security/KeyRep.java | 3 +- src/share/classes/java/security/Security.java | 9 +- src/share/classes/javax/naming/NameImpl.java | 15 ++-- .../naming/directory/BasicAttributes.java | 7 +- src/share/classes/javax/naming/ldap/Rdn.java | 9 +- .../security/jgss/krb5/Krb5NameElement.java | 3 +- .../sun/security/krb5/PrincipalName.java | 7 +- .../sun/security/pkcs12/PKCS12KeyStore.java | 29 ++++--- .../sun/security/provider/JavaKeyStore.java | 2 +- .../provider/certpath/ldap/LDAPCertStore.java | 3 +- .../security/ssl/SSLSessionContextImpl.java | 4 +- .../sun/security/tools/KeyStoreUtil.java | 4 +- .../sun/security/util/HostnameChecker.java | 8 +- .../classes/sun/security/x509/DNSName.java | 2 +- .../classes/sun/security/x509/RFC822Name.java | 2 +- .../ldap/LdapName/CompareToEqualsTests.java | 87 +++++++++++-------- 24 files changed, 149 insertions(+), 107 deletions(-) diff --git a/src/share/classes/com/sun/jndi/ldap/ClientId.java b/src/share/classes/com/sun/jndi/ldap/ClientId.java index 6540380a4..9f75e8373 100644 --- a/src/share/classes/com/sun/jndi/ldap/ClientId.java +++ b/src/share/classes/com/sun/jndi/ldap/ClientId.java @@ -25,6 +25,7 @@ package com.sun.jndi.ldap; +import java.util.Locale; import java.util.Arrays; // JDK 1.2 import java.io.OutputStream; import javax.naming.ldap.Control; @@ -71,7 +72,7 @@ class ClientId { ClientId(int version, String hostname, int port, String protocol, Control[] bindCtls, OutputStream trace, String socketFactory) { this.version = version; - this.hostname = hostname.toLowerCase(); // ignore case + this.hostname = hostname.toLowerCase(Locale.ENGLISH); // ignore case this.port = port; this.protocol = protocol; this.bindCtls = (bindCtls != null ? bindCtls.clone() : null); @@ -83,13 +84,15 @@ class ClientId { if ((socketFactory != null) && !socketFactory.equals(LdapCtx.DEFAULT_SSL_FACTORY)) { try { - Class socketFactoryClass = Obj.helper.loadClass(socketFactory); + Class socketFactoryClass = + Obj.helper.loadClass(socketFactory); Class objClass = Class.forName("java.lang.Object"); this.sockComparator = socketFactoryClass.getMethod( "compare", new Class[]{objClass, objClass}); - Method getDefault = - socketFactoryClass.getMethod("getDefault", new Class[]{}); - this.factory = (SocketFactory) getDefault.invoke(null, new Object[]{}); + Method getDefault = socketFactoryClass.getMethod( + "getDefault", new Class[]{}); + this.factory = + (SocketFactory)getDefault.invoke(null, new Object[]{}); } catch (Exception e) { // Ignore it here, the same exceptions are/will be handled by // LdapPoolManager and Connection classes. diff --git a/src/share/classes/com/sun/jndi/ldap/LdapClient.java b/src/share/classes/com/sun/jndi/ldap/LdapClient.java index ebfc4a45c..0053758c2 100644 --- a/src/share/classes/com/sun/jndi/ldap/LdapClient.java +++ b/src/share/classes/com/sun/jndi/ldap/LdapClient.java @@ -26,6 +26,7 @@ package com.sun.jndi.ldap; import java.io.*; +import java.util.Locale; import java.util.Vector; import java.util.Hashtable; @@ -738,14 +739,15 @@ public final class LdapClient implements PooledConnection { if (hasBinaryValues) { la.add(ber.parseOctetString(ber.peekByte(), len)); } else { - la.add(ber.parseStringWithTag(Ber.ASN_SIMPLE_STRING, isLdapv3, len)); + la.add(ber.parseStringWithTag( + Ber.ASN_SIMPLE_STRING, isLdapv3, len)); } return len[0]; } private boolean isBinaryValued(String attrid, Hashtable binaryAttrs) { - String id = attrid.toLowerCase(); + String id = attrid.toLowerCase(Locale.ENGLISH); return ((id.indexOf(";binary") != -1) || defaultBinaryAttrs.containsKey(id) || @@ -753,8 +755,8 @@ public final class LdapClient implements PooledConnection { } // package entry point; used by Connection - static void parseResult(BerDecoder replyBer, LdapResult res, boolean isLdapv3) - throws IOException { + static void parseResult(BerDecoder replyBer, LdapResult res, + boolean isLdapv3) throws IOException { res.status = replyBer.parseEnumeration(); res.matchedDN = replyBer.parseString(isLdapv3); diff --git a/src/share/classes/com/sun/jndi/ldap/LdapCtx.java b/src/share/classes/com/sun/jndi/ldap/LdapCtx.java index d0d38308d..b861ea030 100644 --- a/src/share/classes/com/sun/jndi/ldap/LdapCtx.java +++ b/src/share/classes/com/sun/jndi/ldap/LdapCtx.java @@ -33,6 +33,7 @@ import javax.naming.ldap.*; import javax.naming.ldap.LdapName; import javax.naming.ldap.Rdn; +import java.util.Locale; import java.util.Vector; import java.util.Hashtable; import java.util.List; @@ -2597,7 +2598,7 @@ final public class LdapCtx extends ComponentDirContext } else { binaryAttrs = new Hashtable<>(11, 0.75f); StringTokenizer tokens = - new StringTokenizer(attrIds.toLowerCase(), " "); + new StringTokenizer(attrIds.toLowerCase(Locale.ENGLISH), " "); while (tokens.hasMoreTokens()) { binaryAttrs.put(tokens.nextToken(), Boolean.TRUE); diff --git a/src/share/classes/com/sun/jndi/ldap/LdapName.java b/src/share/classes/com/sun/jndi/ldap/LdapName.java index 18cddb02e..5e2ec66a0 100644 --- a/src/share/classes/com/sun/jndi/ldap/LdapName.java +++ b/src/share/classes/com/sun/jndi/ldap/LdapName.java @@ -28,6 +28,7 @@ package com.sun.jndi.ldap; import java.util.Enumeration; import java.util.Vector; +import java.util.Locale; import javax.naming.*; import javax.naming.directory.Attributes; @@ -707,7 +708,7 @@ public final class LdapName implements Name { TypeAndValue that = (TypeAndValue)obj; - int diff = type.toUpperCase().compareTo(that.type.toUpperCase()); + int diff = type.compareToIgnoreCase(that.type); if (diff != 0) { return diff; } @@ -730,7 +731,7 @@ public final class LdapName implements Name { public int hashCode() { // If two objects are equal, their hash codes must match. - return (type.toUpperCase().hashCode() + + return (type.toUpperCase(Locale.ENGLISH).hashCode() + getValueComparable().hashCode()); } @@ -764,11 +765,12 @@ public final class LdapName implements Name { // cache result if (binary) { - comparable = value.toUpperCase(); + comparable = value.toUpperCase(Locale.ENGLISH); } else { comparable = (String)unescapeValue(value); if (!valueCaseSensitive) { - comparable = comparable.toUpperCase(); // ignore case + // ignore case + comparable = comparable.toUpperCase(Locale.ENGLISH); } } return comparable; @@ -836,7 +838,7 @@ public final class LdapName implements Name { buf.append(Character.forDigit(0xF & b, 16)); } - return (new String(buf)).toUpperCase(); + return (new String(buf)).toUpperCase(Locale.ENGLISH); } /* diff --git a/src/share/classes/com/sun/jndi/ldap/LdapPoolManager.java b/src/share/classes/com/sun/jndi/ldap/LdapPoolManager.java index acd6f6794..b401c0289 100644 --- a/src/share/classes/com/sun/jndi/ldap/LdapPoolManager.java +++ b/src/share/classes/com/sun/jndi/ldap/LdapPoolManager.java @@ -28,6 +28,7 @@ package com.sun.jndi.ldap; import java.io.PrintStream; import java.io.OutputStream; import java.util.Hashtable; +import java.util.Locale; import java.util.StringTokenizer; import javax.naming.ldap.Control; @@ -133,7 +134,7 @@ public final class LdapPoolManager { String mech; int p; for (int i = 0; i < count; i++) { - mech = parser.nextToken().toLowerCase(); + mech = parser.nextToken().toLowerCase(Locale.ENGLISH); if (mech.equals("anonymous")) { mech = "none"; } diff --git a/src/share/classes/com/sun/jndi/toolkit/dir/HierMemDirCtx.java b/src/share/classes/com/sun/jndi/toolkit/dir/HierMemDirCtx.java index de7ac9279..e10ee2aec 100644 --- a/src/share/classes/com/sun/jndi/toolkit/dir/HierMemDirCtx.java +++ b/src/share/classes/com/sun/jndi/toolkit/dir/HierMemDirCtx.java @@ -910,7 +910,7 @@ final class HierarchicalName extends CompoundName { public int hashCode() { if (hashValue == -1) { - String name = toString().toUpperCase(); + String name = toString().toUpperCase(Locale.ENGLISH); int len = name.length(); int off = 0; char val[] = new char[len]; diff --git a/src/share/classes/com/sun/jndi/toolkit/dir/SearchFilter.java b/src/share/classes/com/sun/jndi/toolkit/dir/SearchFilter.java index 93156f6c5..351e5f26c 100644 --- a/src/share/classes/com/sun/jndi/toolkit/dir/SearchFilter.java +++ b/src/share/classes/com/sun/jndi/toolkit/dir/SearchFilter.java @@ -29,6 +29,7 @@ import javax.naming.directory.*; import java.util.Enumeration; import java.util.StringTokenizer; import java.util.Vector; +import java.util.Locale; /** * A class for parsing LDAP search filters (defined in RFC 1960, 2254) @@ -395,19 +396,21 @@ public class SearchFilter implements AttrFilter { // do we need to begin with the first token? if(proto.charAt(0) != WILDCARD_TOKEN && - !value.toString().toLowerCase().startsWith( - subStrs.nextToken().toLowerCase())) { - if(debug) {System.out.println("faild initial test");} + !value.toString().toLowerCase(Locale.ENGLISH).startsWith( + subStrs.nextToken().toLowerCase(Locale.ENGLISH))) { + if(debug) { + System.out.println("faild initial test"); + } return false; } - while(subStrs.hasMoreTokens()) { String currentStr = subStrs.nextToken(); if (debug) {System.out.println("looking for \"" + currentStr +"\"");} - currentPos = value.toLowerCase().indexOf( - currentStr.toLowerCase(), currentPos); + currentPos = value.toLowerCase(Locale.ENGLISH).indexOf( + currentStr.toLowerCase(Locale.ENGLISH), currentPos); + if(currentPos == -1) { return false; } diff --git a/src/share/classes/com/sun/security/ntlm/NTLM.java b/src/share/classes/com/sun/security/ntlm/NTLM.java index 226405e97..b8e6092ef 100644 --- a/src/share/classes/com/sun/security/ntlm/NTLM.java +++ b/src/share/classes/com/sun/security/ntlm/NTLM.java @@ -33,6 +33,7 @@ import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.security.spec.InvalidKeySpecException; import java.util.Arrays; +import java.util.Locale; import javax.crypto.BadPaddingException; import javax.crypto.Cipher; import javax.crypto.IllegalBlockSizeException; @@ -410,7 +411,8 @@ class NTLM { static byte[] getP1(char[] password) { try { - return new String(password).toUpperCase().getBytes("ISO8859_1"); + return new String(password).toUpperCase( + Locale.ENGLISH).getBytes("ISO8859_1"); } catch (UnsupportedEncodingException ex) { return null; } diff --git a/src/share/classes/java/security/KeyRep.java b/src/share/classes/java/security/KeyRep.java index b7d94f1a7..11f3e634b 100644 --- a/src/share/classes/java/security/KeyRep.java +++ b/src/share/classes/java/security/KeyRep.java @@ -26,6 +26,7 @@ package java.security; import java.io.*; +import java.util.Locale; import java.security.spec.PKCS8EncodedKeySpec; import java.security.spec.X509EncodedKeySpec; @@ -137,7 +138,7 @@ public class KeyRep implements Serializable { this.type = type; this.algorithm = algorithm; - this.format = format.toUpperCase(); + this.format = format.toUpperCase(Locale.ENGLISH); this.encoded = encoded.clone(); } diff --git a/src/share/classes/java/security/Security.java b/src/share/classes/java/security/Security.java index 6048c4394..b9dd1441d 100644 --- a/src/share/classes/java/security/Security.java +++ b/src/share/classes/java/security/Security.java @@ -1087,8 +1087,10 @@ public final class Security { // Check the keys for each provider. for (Enumeration e = providers[i].keys(); e.hasMoreElements(); ) { - String currentKey = ((String)e.nextElement()).toUpperCase(); - if (currentKey.startsWith(serviceName.toUpperCase())) { + String currentKey = + ((String)e.nextElement()).toUpperCase(Locale.ENGLISH); + if (currentKey.startsWith( + serviceName.toUpperCase(Locale.ENGLISH))) { // We should skip the currentKey if it contains a // whitespace. The reason is: such an entry in the // provider property contains attributes for the @@ -1096,7 +1098,8 @@ public final class Security { // in entries which lead to the implementation // classes. if (currentKey.indexOf(" ") < 0) { - result.add(currentKey.substring(serviceName.length() + 1)); + result.add(currentKey.substring( + serviceName.length() + 1)); } } } diff --git a/src/share/classes/javax/naming/NameImpl.java b/src/share/classes/javax/naming/NameImpl.java index 8670bfded..f9643054d 100644 --- a/src/share/classes/javax/naming/NameImpl.java +++ b/src/share/classes/javax/naming/NameImpl.java @@ -25,6 +25,7 @@ package javax.naming; +import java.util.Locale; import java.util.Vector; import java.util.Enumeration; import java.util.Properties; @@ -216,7 +217,8 @@ class NameImpl { } private static boolean toBoolean(String name) { - return ((name != null) && name.toLowerCase().equals("true")); + return ((name != null) && + name.toLowerCase(Locale.ENGLISH).equals("true")); } private final void recordNamingConvention(Properties p) { @@ -526,11 +528,14 @@ class NameImpl { comp1 = comp1.trim(); comp2 = comp2.trim(); } + + int local; if (syntaxCaseInsensitive) { - comp1 = comp1.toLowerCase(); - comp2 = comp2.toLowerCase(); + local = comp1.compareToIgnoreCase(comp2); + } else { + local = comp1.compareTo(comp2); } - int local = comp1.compareTo(comp2); + if (local != 0) { return local; } @@ -696,7 +701,7 @@ class NameImpl { comp = comp.trim(); } if (syntaxCaseInsensitive) { - comp = comp.toLowerCase(); + comp = comp.toLowerCase(Locale.ENGLISH); } hash += comp.hashCode(); diff --git a/src/share/classes/javax/naming/directory/BasicAttributes.java b/src/share/classes/javax/naming/directory/BasicAttributes.java index cea77e6dd..70ed5f465 100644 --- a/src/share/classes/javax/naming/directory/BasicAttributes.java +++ b/src/share/classes/javax/naming/directory/BasicAttributes.java @@ -28,6 +28,7 @@ package javax.naming.directory; import java.util.Hashtable; import java.util.Enumeration; +import java.util.Locale; import javax.naming.NamingException; import javax.naming.NamingEnumeration; @@ -160,7 +161,7 @@ public class BasicAttributes implements Attributes { public Attribute get(String attrID) { Attribute attr = attrs.get( - ignoreCase ? attrID.toLowerCase() : attrID); + ignoreCase ? attrID.toLowerCase(Locale.ENGLISH) : attrID); return (attr); } @@ -179,13 +180,13 @@ public class BasicAttributes implements Attributes { public Attribute put(Attribute attr) { String id = attr.getID(); if (ignoreCase) { - id = id.toLowerCase(); + id = id.toLowerCase(Locale.ENGLISH); } return attrs.put(id, attr); } public Attribute remove(String attrID) { - String id = (ignoreCase ? attrID.toLowerCase() : attrID); + String id = (ignoreCase ? attrID.toLowerCase(Locale.ENGLISH) : attrID); return attrs.remove(id); } diff --git a/src/share/classes/javax/naming/ldap/Rdn.java b/src/share/classes/javax/naming/ldap/Rdn.java index ec8277c46..b9f305ee8 100644 --- a/src/share/classes/javax/naming/ldap/Rdn.java +++ b/src/share/classes/javax/naming/ldap/Rdn.java @@ -28,6 +28,7 @@ package javax.naming.ldap; import java.util.Iterator; import java.util.NoSuchElementException; import java.util.ArrayList; +import java.util.Locale; import java.util.Collections; import javax.naming.InvalidNameException; @@ -434,8 +435,7 @@ public class Rdn implements Serializable, Comparable { } public int compareTo(RdnEntry that) { - int diff = type.toUpperCase().compareTo( - that.type.toUpperCase()); + int diff = type.compareToIgnoreCase(that.type); if (diff != 0) { return diff; } @@ -462,7 +462,7 @@ public class Rdn implements Serializable, Comparable { } public int hashCode() { - return (type.toUpperCase().hashCode() + + return (type.toUpperCase(Locale.ENGLISH).hashCode() + getValueComparable().hashCode()); } @@ -479,7 +479,7 @@ public class Rdn implements Serializable, Comparable { if (value instanceof byte[]) { comparable = escapeBinaryValue((byte[]) value); } else { - comparable = ((String) value).toUpperCase(); + comparable = ((String) value).toUpperCase(Locale.ENGLISH); } return comparable; } @@ -569,7 +569,6 @@ public class Rdn implements Serializable, Comparable { builder.append(Character.forDigit(0xF & b, 16)); } return builder.toString(); - // return builder.toString().toUpperCase(); } /** diff --git a/src/share/classes/sun/security/jgss/krb5/Krb5NameElement.java b/src/share/classes/sun/security/jgss/krb5/Krb5NameElement.java index 876cd6c2b..1fb1bb0e6 100644 --- a/src/share/classes/sun/security/jgss/krb5/Krb5NameElement.java +++ b/src/share/classes/sun/security/jgss/krb5/Krb5NameElement.java @@ -35,6 +35,7 @@ import java.io.UnsupportedEncodingException; import java.net.InetAddress; import java.net.UnknownHostException; import java.security.Provider; +import java.util.Locale; /** * Implements the GSSNameSpi for the krb5 mechanism. @@ -184,7 +185,7 @@ public class Krb5NameElement } catch (UnknownHostException e) { // use hostname as it is } - hostName = hostName.toLowerCase(); + hostName = hostName.toLowerCase(Locale.ENGLISH); temp = temp.append('/').append(hostName); return temp.toString(); diff --git a/src/share/classes/sun/security/krb5/PrincipalName.java b/src/share/classes/sun/security/krb5/PrincipalName.java index 86094d629..d03e2fe30 100644 --- a/src/share/classes/sun/security/krb5/PrincipalName.java +++ b/src/share/classes/sun/security/krb5/PrincipalName.java @@ -35,6 +35,7 @@ import sun.security.krb5.internal.*; import sun.security.util.*; import java.net.*; import java.util.Vector; +import java.util.Locale; import java.io.IOException; import java.math.BigInteger; import sun.security.krb5.internal.ccache.CCacheOutputStream; @@ -389,14 +390,14 @@ public class PrincipalName // Looks if canonicalized is a longer format of hostName, // we accept cases like // bunny -> bunny.rabbit.hole - if (canonicalized.toLowerCase() - .startsWith(hostName.toLowerCase()+".")) { + if (canonicalized.toLowerCase(Locale.ENGLISH).startsWith( + hostName.toLowerCase(Locale.ENGLISH)+".")) { hostName = canonicalized; } } catch (UnknownHostException e) { // no canonicalization, use old } - nameParts[1] = hostName.toLowerCase(); + nameParts[1] = hostName.toLowerCase(Locale.ENGLISH); } nameStrings = nameParts; nameType = type; diff --git a/src/share/classes/sun/security/pkcs12/PKCS12KeyStore.java b/src/share/classes/sun/security/pkcs12/PKCS12KeyStore.java index e7fce4ea6..f6bada939 100644 --- a/src/share/classes/sun/security/pkcs12/PKCS12KeyStore.java +++ b/src/share/classes/sun/security/pkcs12/PKCS12KeyStore.java @@ -219,7 +219,7 @@ public final class PKCS12KeyStore extends KeyStoreSpi { public Key engineGetKey(String alias, char[] password) throws NoSuchAlgorithmException, UnrecoverableKeyException { - KeyEntry entry = entries.get(alias.toLowerCase()); + KeyEntry entry = entries.get(alias.toLowerCase(Locale.ENGLISH)); Key key = null; if (entry == null) { @@ -296,7 +296,7 @@ public final class PKCS12KeyStore extends KeyStoreSpi { * key entry without a certificate chain). */ public Certificate[] engineGetCertificateChain(String alias) { - KeyEntry entry = entries.get(alias.toLowerCase()); + KeyEntry entry = entries.get(alias.toLowerCase(Locale.ENGLISH)); if (entry != null) { if (entry.chain == null) { return null; @@ -324,7 +324,7 @@ public final class PKCS12KeyStore extends KeyStoreSpi { * does not contain a certificate. */ public Certificate engineGetCertificate(String alias) { - KeyEntry entry = entries.get(alias.toLowerCase()); + KeyEntry entry = entries.get(alias.toLowerCase(Locale.ENGLISH)); if (entry != null) { if (entry.chain == null) { return null; @@ -345,7 +345,7 @@ public final class PKCS12KeyStore extends KeyStoreSpi { * not exist */ public Date engineGetCreationDate(String alias) { - KeyEntry entry = entries.get(alias.toLowerCase()); + KeyEntry entry = entries.get(alias.toLowerCase(Locale.ENGLISH)); if (entry != null) { return new Date(entry.date.getTime()); } else { @@ -409,10 +409,10 @@ public final class PKCS12KeyStore extends KeyStoreSpi { // set the keyId to current date entry.keyId = ("Time " + (entry.date).getTime()).getBytes("UTF8"); // set the alias - entry.alias = alias.toLowerCase(); + entry.alias = alias.toLowerCase(Locale.ENGLISH); // add the entry - entries.put(alias.toLowerCase(), entry); + entries.put(alias.toLowerCase(Locale.ENGLISH), entry); } catch (Exception nsae) { throw new KeyStoreException("Key protection " + " algorithm not found: " + nsae, nsae); @@ -465,7 +465,7 @@ public final class PKCS12KeyStore extends KeyStoreSpi { // Won't happen } // set the alias - entry.alias = alias.toLowerCase(); + entry.alias = alias.toLowerCase(Locale.ENGLISH); entry.protectedPrivKey = key.clone(); if (chain != null) { @@ -473,7 +473,7 @@ public final class PKCS12KeyStore extends KeyStoreSpi { } // add the entry - entries.put(alias.toLowerCase(), entry); + entries.put(alias.toLowerCase(Locale.ENGLISH), entry); } @@ -618,7 +618,7 @@ public final class PKCS12KeyStore extends KeyStoreSpi { public synchronized void engineSetCertificateEntry(String alias, Certificate cert) throws KeyStoreException { - KeyEntry entry = entries.get(alias.toLowerCase()); + KeyEntry entry = entries.get(alias.toLowerCase(Locale.ENGLISH)); if (entry != null) { throw new KeyStoreException("Cannot overwrite own certificate"); } else @@ -635,7 +635,7 @@ public final class PKCS12KeyStore extends KeyStoreSpi { public synchronized void engineDeleteEntry(String alias) throws KeyStoreException { - entries.remove(alias.toLowerCase()); + entries.remove(alias.toLowerCase(Locale.ENGLISH)); } /** @@ -655,7 +655,7 @@ public final class PKCS12KeyStore extends KeyStoreSpi { * @return true if the alias exists, false otherwise */ public boolean engineContainsAlias(String alias) { - return entries.containsKey(alias.toLowerCase()); + return entries.containsKey(alias.toLowerCase(Locale.ENGLISH)); } /** @@ -675,7 +675,7 @@ public final class PKCS12KeyStore extends KeyStoreSpi { * key entry, false otherwise. */ public boolean engineIsKeyEntry(String alias) { - KeyEntry entry = entries.get(alias.toLowerCase()); + KeyEntry entry = entries.get(alias.toLowerCase(Locale.ENGLISH)); if (entry != null) { return true; } else { @@ -1274,7 +1274,8 @@ public final class PKCS12KeyStore extends KeyStoreSpi { if (password != null && s.available() > 0) { MacData macData = new MacData(s); try { - String algName = macData.getDigestAlgName().toUpperCase(); + String algName = + macData.getDigestAlgName().toUpperCase(Locale.ENGLISH); if (algName.equals("SHA") || algName.equals("SHA1") || algName.equals("SHA-1")) { @@ -1479,7 +1480,7 @@ public final class PKCS12KeyStore extends KeyStoreSpi { if (alias == null) alias = getUnfriendlyName(); entry.alias = alias; - entries.put(alias.toLowerCase(), entry); + entries.put(alias.toLowerCase(Locale.ENGLISH), entry); } else if (bagItem instanceof X509Certificate) { X509Certificate cert = (X509Certificate)bagItem; // Insert a localKeyID for the corresponding cert diff --git a/src/share/classes/sun/security/provider/JavaKeyStore.java b/src/share/classes/sun/security/provider/JavaKeyStore.java index 1a899868d..2335a17c8 100644 --- a/src/share/classes/sun/security/provider/JavaKeyStore.java +++ b/src/share/classes/sun/security/provider/JavaKeyStore.java @@ -54,7 +54,7 @@ abstract class JavaKeyStore extends KeyStoreSpi { // regular JKS public static final class JKS extends JavaKeyStore { String convertAlias(String alias) { - return alias.toLowerCase(); + return alias.toLowerCase(Locale.ENGLISH); } } diff --git a/src/share/classes/sun/security/provider/certpath/ldap/LDAPCertStore.java b/src/share/classes/sun/security/provider/certpath/ldap/LDAPCertStore.java index 0c037a50a..d86404d08 100644 --- a/src/share/classes/sun/security/provider/certpath/ldap/LDAPCertStore.java +++ b/src/share/classes/sun/security/provider/certpath/ldap/LDAPCertStore.java @@ -879,7 +879,8 @@ public class LDAPCertStore extends CertStoreSpi { if (hashCode == 0) { int result = 17; result = 37*result + getPort(); - result = 37*result + getServerName().toLowerCase().hashCode(); + result = 37*result + + getServerName().toLowerCase(Locale.ENGLISH).hashCode(); hashCode = result; } return hashCode; diff --git a/src/share/classes/sun/security/ssl/SSLSessionContextImpl.java b/src/share/classes/sun/security/ssl/SSLSessionContextImpl.java index 15ac88467..ce1336bf1 100644 --- a/src/share/classes/sun/security/ssl/SSLSessionContextImpl.java +++ b/src/share/classes/sun/security/ssl/SSLSessionContextImpl.java @@ -33,6 +33,7 @@ import java.util.Enumeration; import java.util.Hashtable; import java.util.NoSuchElementException; import java.util.Vector; +import java.util.Locale; import javax.net.ssl.SSLSession; import javax.net.ssl.SSLSessionContext; @@ -166,7 +167,8 @@ final class SSLSessionContextImpl implements SSLSessionContext { } private String getKey(String hostname, int port) { - return (hostname + ":" + String.valueOf(port)).toLowerCase(); + return (hostname + ":" + + String.valueOf(port)).toLowerCase(Locale.ENGLISH); } // cache a SSLSession diff --git a/src/share/classes/sun/security/tools/KeyStoreUtil.java b/src/share/classes/sun/security/tools/KeyStoreUtil.java index 8e033554a..679a24739 100644 --- a/src/share/classes/sun/security/tools/KeyStoreUtil.java +++ b/src/share/classes/sun/security/tools/KeyStoreUtil.java @@ -25,6 +25,8 @@ package sun.security.tools; +import java.util.Locale; + /** *

This class provides several utilities to KeyStore. * @@ -63,7 +65,7 @@ public class KeyStoreUtil { } else if(storetype.equalsIgnoreCase("Windows-ROOT")) { return "Windows-ROOT"; } else { - return storetype.toUpperCase(); + return storetype.toUpperCase(Locale.ENGLISH); } } } diff --git a/src/share/classes/sun/security/util/HostnameChecker.java b/src/share/classes/sun/security/util/HostnameChecker.java index b3e897184..313ddb110 100644 --- a/src/share/classes/sun/security/util/HostnameChecker.java +++ b/src/share/classes/sun/security/util/HostnameChecker.java @@ -266,8 +266,8 @@ public class HostnameChecker { */ private static boolean matchAllWildcards(String name, String template) { - name = name.toLowerCase(); - template = template.toLowerCase(); + name = name.toLowerCase(Locale.ENGLISH); + template = template.toLowerCase(Locale.ENGLISH); StringTokenizer nameSt = new StringTokenizer(name, "."); StringTokenizer templateSt = new StringTokenizer(template, "."); @@ -296,8 +296,8 @@ public class HostnameChecker { */ private static boolean matchLeftmostWildcard(String name, String template) { - name = name.toLowerCase(); - template = template.toLowerCase(); + name = name.toLowerCase(Locale.ENGLISH); + template = template.toLowerCase(Locale.ENGLISH); // Retreive leftmost component int templateIdx = template.indexOf("."); diff --git a/src/share/classes/sun/security/x509/DNSName.java b/src/share/classes/sun/security/x509/DNSName.java index 398e93fe0..b571e0e5d 100644 --- a/src/share/classes/sun/security/x509/DNSName.java +++ b/src/share/classes/sun/security/x509/DNSName.java @@ -159,7 +159,7 @@ public class DNSName implements GeneralNameInterface { * @return a hash code value for this object. */ public int hashCode() { - return name.toUpperCase().hashCode(); + return name.toUpperCase(Locale.ENGLISH).hashCode(); } /** diff --git a/src/share/classes/sun/security/x509/RFC822Name.java b/src/share/classes/sun/security/x509/RFC822Name.java index 8f5690346..a477174a4 100644 --- a/src/share/classes/sun/security/x509/RFC822Name.java +++ b/src/share/classes/sun/security/x509/RFC822Name.java @@ -153,7 +153,7 @@ public class RFC822Name implements GeneralNameInterface * @return a hash code value for this object. */ public int hashCode() { - return name.toUpperCase().hashCode(); + return name.toUpperCase(Locale.ENGLISH).hashCode(); } /** diff --git a/test/javax/naming/ldap/LdapName/CompareToEqualsTests.java b/test/javax/naming/ldap/LdapName/CompareToEqualsTests.java index 7c9146db8..af6c9fcae 100644 --- a/test/javax/naming/ldap/LdapName/CompareToEqualsTests.java +++ b/test/javax/naming/ldap/LdapName/CompareToEqualsTests.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2003, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2003, 2011, 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 @@ -23,12 +23,14 @@ /* * @test - * @bug 4635618 + * @bug 4635618 7059542 * @summary Support for manipulating LDAP Names + * JNDI name operations should be locale independent */ import javax.naming.ldap.*; import java.util.ArrayList; +import java.util.Locale; import java.util.List; import javax.naming.InvalidNameException; @@ -39,52 +41,61 @@ public class CompareToEqualsTests { public static void main(String args[]) throws Exception { - - /** - * Test cases: - * 1) Same RDNs. - * 2) same RDN sequence with an AVA ordered differently. - * 3) RDN sequences of a differing AVA. - * 4) RDN sequence of different length. - * 5) RDN sequence of different Case. - * 6) Matching binary return values. - * 7) Binary values that don't match. - */ - String names1[] = new String [] { + Locale reservedLocale = Locale.getDefault(); + try { + /** + * Test cases: + * 1) Same RDNs. + * 2) same RDN sequence with an AVA ordered differently. + * 3) RDN sequences of a differing AVA. + * 4) RDN sequence of different length. + * 5) RDN sequence of different Case. + * 6) Matching binary return values. + * 7) Binary values that don't match. + */ + String names1[] = new String [] { "ou=Sales+cn=Bob", "ou=Sales+cn=Bob", "ou=Sales+cn=Bob", "ou=Sales+cn=Scott+c=US", "cn=config"}; - String names2[] = new String [] { + String names2[] = new String [] { "ou=Sales+cn=Bob", "cn=Bob+ou=Sales", "ou=Sales+cn=Scott", "ou=Sales+cn=Scott", "Cn=COnFIG"}; - int expectedResults[] = {0, 0, -1, -1, 0}; + int expectedResults[] = {0, 0, -1, -1, 0}; + for (Locale locale : Locale.getAvailableLocales()) { + // reset the default locale + Locale.setDefault(locale); - for (int i = 0; i < names1.length; i++) { - checkResults(new LdapName(names1[i]), + for (int i = 0; i < names1.length; i++) { + checkResults(new LdapName(names1[i]), new LdapName(names2[i]), expectedResults[i]); + } + + byte[] value = "abcxyz".getBytes(); + Rdn rdn1 = new Rdn("binary", value); + ArrayList rdns1 = new ArrayList(); + rdns1.add(rdn1); + LdapName l1 = new LdapName(rdns1); + + Rdn rdn2 = new Rdn("binary", value); + ArrayList rdns2 = new ArrayList(); + rdns2.add(rdn2); + LdapName l2 = new LdapName(rdns2); + checkResults(l1, l2, 0); + + l2 = new LdapName("binary=#61626378797A"); + checkResults(l1, l2, 0); + + l2 = new LdapName("binary=#61626378797B"); + checkResults(l1, l2, -1); + + System.out.println("Tests passed"); + } + } finally { + // restore the reserved locale + Locale.setDefault(reservedLocale); } - - byte[] value = "abcxyz".getBytes(); - Rdn rdn1 = new Rdn("binary", value); - ArrayList rdns1 = new ArrayList(); - rdns1.add(rdn1); - LdapName l1 = new LdapName(rdns1); - - Rdn rdn2 = new Rdn("binary", value); - ArrayList rdns2 = new ArrayList(); - rdns2.add(rdn2); - LdapName l2 = new LdapName(rdns2); - checkResults(l1, l2, 0); - - l2 = new LdapName("binary=#61626378797A"); - checkResults(l1, l2, 0); - - l2 = new LdapName("binary=#61626378797B"); - checkResults(l1, l2, -1); - - System.out.println("Tests passed"); } -- GitLab