8233884: Avoid looking up standard charsets in security libraries

Reviewed-by: coffeys
This commit is contained in:
Ivan Gerasimov 2019-11-12 01:36:17 -08:00
parent 301e068935
commit 8e859259bc
48 changed files with 317 additions and 391 deletions

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2000, 2019, 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
@ -32,11 +32,12 @@ import java.util.HashMap;
import java.util.HashSet;
import java.util.Arrays;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import sun.security.util.ObjectIdentifier;
import sun.security.util.DerInputStream;
import sun.security.util.DerOutputStream;
import static java.nio.charset.StandardCharsets.UTF_8;
/**
* This is the implementation class for GSSName. Conceptually the
* GSSName is a container with mechanism specific name elements. Each
@ -227,13 +228,10 @@ public class GSSNameImpl implements GSSName {
byte[] bytes = null;
if (appName instanceof String) {
try {
bytes = ((String) appName).getBytes("UTF-8");
} catch (UnsupportedEncodingException e) {
// Won't happen
}
} else
bytes = ((String) appName).getBytes(UTF_8);
} else {
bytes = (byte[]) appName;
}
if ((bytes[pos++] != 0x04) ||
(bytes[pos++] != 0x01))
@ -320,21 +318,14 @@ public class GSSNameImpl implements GSSName {
if (!this.appNameType.equals(that.appNameType)) {
return false;
}
byte[] myBytes = null;
byte[] bytes = null;
try {
myBytes =
byte[] myBytes =
(this.appNameStr != null ?
this.appNameStr.getBytes("UTF-8") :
this.appNameStr.getBytes(UTF_8) :
this.appNameBytes);
bytes =
byte[] bytes =
(that.appNameStr != null ?
that.appNameStr.getBytes("UTF-8") :
that.appNameStr.getBytes(UTF_8) :
that.appNameBytes);
} catch (UnsupportedEncodingException e) {
// Won't happen
}
return Arrays.equals(myBytes, bytes);
}

View file

@ -32,12 +32,13 @@ import sun.security.krb5.Realm;
import sun.security.krb5.KrbException;
import javax.security.auth.kerberos.ServicePermission;
import java.io.UnsupportedEncodingException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.security.Provider;
import java.util.Locale;
import static java.nio.charset.StandardCharsets.UTF_8;
/**
* Implements the GSSNameSpi for the krb5 mechanism.
*
@ -51,9 +52,6 @@ public class Krb5NameElement
private String gssNameStr = null;
private Oid gssNameType = null;
// XXX Move this concept into PrincipalName's asn1Encode() sometime
private static String CHAR_ENCODING = "UTF-8";
private Krb5NameElement(PrincipalName principalName,
String gssNameStr,
Oid gssNameType) {
@ -285,13 +283,7 @@ public class Krb5NameElement
*/
public byte[] export() throws GSSException {
// XXX Apply the above constraints.
byte[] retVal = null;
try {
retVal = krb5PrincipalName.getName().getBytes(CHAR_ENCODING);
} catch (UnsupportedEncodingException e) {
// Can't happen
}
return retVal;
return krb5PrincipalName.getName().getBytes(UTF_8);
}
/**

View file

@ -29,7 +29,6 @@ import org.ietf.jgss.*;
import java.security.Provider;
import java.security.Security;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import sun.security.krb5.Realm;
import sun.security.jgss.GSSUtil;
import sun.security.util.ObjectIdentifier;

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2005, 2011, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2005, 2019, 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
@ -25,7 +25,6 @@
package sun.security.jgss.wrapper;
import java.io.UnsupportedEncodingException;
import java.security.Provider;
import java.util.Vector;
import org.ietf.jgss.*;
@ -34,6 +33,8 @@ import sun.security.jgss.GSSCaller;
import sun.security.jgss.GSSExceptionImpl;
import sun.security.jgss.spi.*;
import static java.nio.charset.StandardCharsets.UTF_8;
/**
* JGSS plugin for generic mechanisms provided through native GSS framework.
*
@ -80,14 +81,9 @@ public final class NativeGSSFactory implements MechanismFactory {
public GSSNameSpi getNameElement(String nameStr, Oid nameType)
throws GSSException {
try {
byte[] nameBytes =
(nameStr == null ? null : nameStr.getBytes("UTF-8"));
return new GSSNameElement(nameBytes, nameType, cStub);
} catch (UnsupportedEncodingException uee) {
// Shouldn't happen
throw new GSSExceptionImpl(GSSException.FAILURE, uee);
}
byte[] nameBytes =
(nameStr == null ? null : nameStr.getBytes(UTF_8));
return new GSSNameElement(nameBytes, nameType, cStub);
}
public GSSNameSpi getNameElement(byte[] name, Oid nameType)

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2005, 2009, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2005, 2019, 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
@ -25,10 +25,13 @@
package sun.security.krb5.internal;
import sun.security.util.*;
import sun.security.krb5.Asn1Exception;
import java.io.IOException;
import static java.nio.charset.StandardCharsets.UTF_8;
import sun.security.krb5.Asn1Exception;
import sun.security.krb5.internal.util.KerberosString;
import sun.security.util.*;
/**
* Implements the ASN.1 ETYPE-INFO-ENTRY type.
@ -99,7 +102,7 @@ public class ETypeInfo {
// KerberosString in most implementations.
if (KerberosString.MSNAME) {
this.salt = new String(saltBytes, "UTF8");
this.salt = new String(saltBytes, UTF_8);
} else {
this.salt = new String(saltBytes);
}
@ -129,7 +132,7 @@ public class ETypeInfo {
if (salt != null) {
temp = new DerOutputStream();
if (KerberosString.MSNAME) {
temp.putOctetString(salt.getBytes("UTF8"));
temp.putOctetString(salt.getBytes(UTF_8));
} else {
temp.putOctetString(salt.getBytes());
}

View file

@ -31,13 +31,15 @@
package sun.security.krb5.internal;
import sun.security.krb5.internal.crypto.EType;
import sun.security.util.*;
import sun.security.krb5.Asn1Exception;
import java.io.IOException;
import java.util.Vector;
import static java.nio.charset.StandardCharsets.*;
import sun.security.krb5.Asn1Exception;
import sun.security.krb5.internal.util.KerberosString;
import sun.security.krb5.internal.crypto.EType;
import sun.security.util.*;
/**
* Implements the ASN.1 PA-DATA type.
@ -263,7 +265,7 @@ public class PAData {
switch (p.getType()) {
case Krb5.PA_PW_SALT:
paPwSalt = new String(p.getValue(),
KerberosString.MSNAME?"UTF8":"8859_1");
KerberosString.MSNAME ? UTF_8 : ISO_8859_1);
break;
case Krb5.PA_ETYPE_INFO:
d = new DerValue(p.getValue());

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2012, 2019, 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
@ -33,6 +33,8 @@ import sun.security.krb5.internal.util.KerberosString;
import sun.security.util.DerOutputStream;
import sun.security.util.DerValue;
import static java.nio.charset.StandardCharsets.UTF_8;
/**
* Implements the ASN.1 PA-FOR-USER type.
*
@ -163,25 +165,20 @@ public class PAForUserEnc {
* 4. the string value of auth-package field
*/
public byte[] getS4UByteArray() {
try {
ByteArrayOutputStream ba = new ByteArrayOutputStream();
ba.write(new byte[4]);
for (String s: name.getNameStrings()) {
ba.write(s.getBytes("UTF-8"));
}
ba.write(name.getRealm().toString().getBytes("UTF-8"));
ba.write(AUTH_PACKAGE.getBytes("UTF-8"));
byte[] output = ba.toByteArray();
int pnType = name.getNameType();
output[0] = (byte)(pnType & 0xff);
output[1] = (byte)((pnType>>8) & 0xff);
output[2] = (byte)((pnType>>16) & 0xff);
output[3] = (byte)((pnType>>24) & 0xff);
return output;
} catch (IOException ioe) {
// not possible
throw new AssertionError("Cannot write ByteArrayOutputStream", ioe);
ByteArrayOutputStream ba = new ByteArrayOutputStream();
ba.writeBytes(new byte[4]);
for (String s: name.getNameStrings()) {
ba.writeBytes(s.getBytes(UTF_8));
}
ba.writeBytes(name.getRealm().toString().getBytes(UTF_8));
ba.writeBytes(AUTH_PACKAGE.getBytes(UTF_8));
byte[] output = ba.toByteArray();
int pnType = name.getNameType();
output[0] = (byte)(pnType & 0xff);
output[1] = (byte)((pnType>>8) & 0xff);
output[2] = (byte)((pnType>>16) & 0xff);
output[3] = (byte)((pnType>>24) & 0xff);
return output;
}
public String toString() {

View file

@ -51,6 +51,8 @@ import java.io.FileOutputStream;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import static java.nio.charset.StandardCharsets.ISO_8859_1;
/**
* CredentialsCache stores credentials(tickets, session keys, etc) in a
* semi-permanent store
@ -594,7 +596,7 @@ public class FileCredentialsCache extends CredentialsCache
BufferedReader commandResult =
new BufferedReader
(new InputStreamReader(p.getInputStream(), "8859_1"));
(new InputStreamReader(p.getInputStream(), ISO_8859_1));
String s1 = null;
if ((command.length == 1) &&
(command[0].equals("/usr/bin/env"))) {

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2004, 2008, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2004, 2019, 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
@ -43,6 +43,8 @@ import sun.security.krb5.Confounder;
import sun.security.krb5.internal.crypto.KeyUsage;
import java.util.Arrays;
import static java.nio.charset.StandardCharsets.UTF_8;
/**
* This class provides the implementation of AES Encryption for Kerberos
* as defined RFC 3962.
@ -104,7 +106,7 @@ public class AesDkCrypto extends DkCrypto {
byte[] saltUtf8 = null;
try {
saltUtf8 = salt.getBytes("UTF-8");
saltUtf8 = salt.getBytes(UTF_8);
return stringToKey(password, saltUtf8, s2kparams);
} catch (Exception e) {
return null;

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2017, 2019, 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
@ -41,6 +41,8 @@ import sun.security.krb5.Confounder;
import sun.security.krb5.internal.crypto.KeyUsage;
import java.util.Arrays;
import static java.nio.charset.StandardCharsets.UTF_8;
/**
* This class provides the implementation of AES Encryption with
* HMAC-SHA2 for Kerberos 5
@ -107,7 +109,7 @@ public class AesSha2DkCrypto extends DkCrypto {
byte[] saltUtf8 = null;
try {
saltUtf8 = salt.getBytes("UTF-8");
saltUtf8 = salt.getBytes(UTF_8);
return stringToKey(password, saltUtf8, s2kparams);
} catch (Exception e) {
return null;

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2004, 2017, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2004, 2019, Oracle and/or its affiliates. All rights reserved.
*/
/*
@ -33,7 +33,6 @@ package sun.security.krb5.internal.crypto.dk;
import javax.crypto.Cipher;
import javax.crypto.Mac;
import java.security.GeneralSecurityException;
import java.io.UnsupportedEncodingException;
import java.util.Arrays;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
@ -45,6 +44,8 @@ import sun.security.krb5.Confounder;
import sun.security.krb5.internal.crypto.KeyUsage;
import sun.security.krb5.KrbCryptoException;
import static java.nio.charset.StandardCharsets.*;
/**
* Implements Derive Key cryptography functionality as defined in RFC 3961.
* http://www.ietf.org/rfc/rfc3961.txt
@ -672,13 +673,11 @@ public abstract class DkCrypto {
}
}
// String.getBytes("UTF-8");
// String.getBytes(UTF_8);
// Do this instead of using String to avoid making password immutable
static byte[] charToUtf8(char[] chars) {
Charset utf8 = Charset.forName("UTF-8");
CharBuffer cb = CharBuffer.wrap(chars);
ByteBuffer bb = utf8.encode(cb);
ByteBuffer bb = UTF_8.encode(cb);
int len = bb.limit();
byte[] answer = new byte[len];
bb.get(answer, 0, len);
@ -686,10 +685,8 @@ public abstract class DkCrypto {
}
static byte[] charToUtf16(char[] chars) {
Charset utf8 = Charset.forName("UTF-16LE");
CharBuffer cb = CharBuffer.wrap(chars);
ByteBuffer bb = utf8.encode(cb);
ByteBuffer bb = UTF_16LE.encode(cb);
int len = bb.limit();
byte[] answer = new byte[len];
bb.get(answer, 0, len);

View file

@ -1,4 +1,5 @@
/*
* Copyright (c) 2000, 2019, 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
@ -32,7 +33,8 @@ package sun.security.krb5.internal.ktab;
import sun.security.krb5.*;
import sun.security.krb5.internal.*;
import java.io.UnsupportedEncodingException;
import static java.nio.charset.StandardCharsets.ISO_8859_1;
/**
* This class represents a Key Table entry. Each entry contains the service principal of
@ -83,17 +85,10 @@ public class KeyTabEntry implements KeyTabConstants {
int totalPrincipalLength = 0;
String[] names = service.getNameStrings();
for (int i = 0; i < names.length; i++) {
try {
totalPrincipalLength += principalSize + names[i].getBytes("8859_1").length;
} catch (UnsupportedEncodingException exc) {
}
totalPrincipalLength += principalSize + names[i].getBytes(ISO_8859_1).length;
}
int realmLen = 0;
try {
realmLen = realm.toString().getBytes("8859_1").length;
} catch (UnsupportedEncodingException exc) {
}
int realmLen = realm.toString().getBytes(ISO_8859_1).length;
int size = principalComponentSize + realmSize + realmLen
+ totalPrincipalLength + principalTypeSize

View file

@ -1,4 +1,5 @@
/*
* Copyright (c) 2000, 2019, 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
@ -35,7 +36,8 @@ import sun.security.krb5.internal.util.KrbDataOutputStream;
import java.io.IOException;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import static java.nio.charset.StandardCharsets.ISO_8859_1;
/**
* This class implements a buffered input stream. It is used for parsing key table
@ -68,21 +70,16 @@ public class KeyTabOutputStream extends KrbDataOutputStream implements KeyTabCon
}
else write16(comp_num);
byte[] realm = null;
try {
realm = entry.service.getRealmString().getBytes("8859_1");
} catch (UnsupportedEncodingException exc) {
}
byte[] realm = entry.service.getRealmString().getBytes(ISO_8859_1);
write16(realm.length);
write(realm);
for (int i = 0; i < comp_num; i++) {
try {
write16(serviceNames[i].getBytes("8859_1").length);
write(serviceNames[i].getBytes("8859_1"));
} catch (UnsupportedEncodingException exc) {
}
byte[] serviceName = serviceNames[i].getBytes(ISO_8859_1);
write16(serviceName.length);
write(serviceName);
}
write32(entry.service.getNameType());
//time is long, but we only use 4 bytes to store the data.
write32((int)(entry.timestamp.getTime()/1000));

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2009, 2018, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2009, 2019, 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
@ -29,6 +29,8 @@ import java.io.IOException;
import sun.security.action.GetPropertyAction;
import sun.security.util.DerValue;
import static java.nio.charset.StandardCharsets.*;
/**
* Implements the ASN.1 KerberosString type.
*
@ -71,17 +73,17 @@ public final class KerberosString {
throw new IOException(
"KerberosString's tag is incorrect: " + der.tag);
}
s = new String(der.getDataBytes(), MSNAME?"UTF8":"ASCII");
s = new String(der.getDataBytes(), MSNAME ? UTF_8 : US_ASCII);
}
public String toString() {
return s;
}
public DerValue toDerValue() throws IOException {
public DerValue toDerValue() {
// No need to cache the result since this method is
// only called once.
return new DerValue(DerValue.tag_GeneralString,
s.getBytes(MSNAME?"UTF8":"ASCII"));
s.getBytes(MSNAME ? UTF_8 : US_ASCII));
}
}