8080522: Optimize string operations in java.base/share/classes/sun/security/x509/

Reviewed-by: mullan
This commit is contained in:
Ivan Gerasimov 2015-05-18 17:38:38 +03:00
parent e8064300cb
commit 7e89934d9e
30 changed files with 305 additions and 254 deletions

View File

@ -454,7 +454,7 @@ public class AVA implements DerEncoder {
if (embeddedHex.size() > 0) { if (embeddedHex.size() > 0) {
// add space(s) before embedded hex bytes // add space(s) before embedded hex bytes
for (int i = 0; i < spaceCount; i++) { for (int i = 0; i < spaceCount; i++) {
temp.append(" "); temp.append(' ');
} }
spaceCount = 0; spaceCount = 0;
@ -472,7 +472,7 @@ public class AVA implements DerEncoder {
} else { } else {
// add space(s) // add space(s)
for (int i = 0; i < spaceCount; i++) { for (int i = 0; i < spaceCount; i++) {
temp.append(" "); temp.append(' ');
} }
spaceCount = 0; spaceCount = 0;
temp.append((char)c); temp.append((char)c);
@ -853,7 +853,7 @@ public class AVA implements DerEncoder {
} }
sbuffer.append(c); sbuffer.append(c);
} }
typeAndValue.append(sbuffer.toString()); typeAndValue.append(sbuffer);
} }
return typeAndValue.toString(); return typeAndValue.toString();
} }
@ -1039,7 +1039,7 @@ public class AVA implements DerEncoder {
StringBuilder retval = new StringBuilder(40); StringBuilder retval = new StringBuilder(40);
retval.append(keyword); retval.append(keyword);
retval.append("="); retval.append('=');
try { try {
String valStr = value.getAsString(); String valStr = value.getAsString();
@ -1147,9 +1147,11 @@ public class AVA implements DerEncoder {
// Emit the string ... quote it if needed // Emit the string ... quote it if needed
// if string is already quoted, don't re-quote // if string is already quoted, don't re-quote
if (!alreadyQuoted && quoteNeeded) { if (!alreadyQuoted && quoteNeeded) {
retval.append("\"" + sbuffer.toString() + "\""); retval.append('\"')
.append(sbuffer)
.append('\"');
} else { } else {
retval.append(sbuffer.toString()); retval.append(sbuffer);
} }
} }
} catch (IOException e) { } catch (IOException e) {

View File

@ -196,17 +196,20 @@ implements CertAttrSet<String> {
* Return the object as a string. * Return the object as a string.
*/ */
public String toString() { public String toString() {
String s = super.toString() + "AuthorityKeyIdentifier [\n"; StringBuilder sb = new StringBuilder();
sb.append(super.toString())
.append("AuthorityKeyIdentifier [\n");
if (id != null) { if (id != null) {
s += id.toString(); // id already has a newline sb.append(id); // id already has a newline
} }
if (names != null) { if (names != null) {
s += names.toString() + "\n"; sb.append(names).append('\n');
} }
if (serialNum != null) { if (serialNum != null) {
s += serialNum.toString() + "\n"; sb.append(serialNum).append('\n');
} }
return (s + "]\n"); sb.append("]\n");
return sb.toString();
} }
/** /**

View File

@ -171,15 +171,11 @@ implements CertAttrSet<String> {
* Return user readable form of extension. * Return user readable form of extension.
*/ */
public String toString() { public String toString() {
String s = super.toString() + "BasicConstraints:[\n"; return super.toString() +
"BasicConstraints:[\n CA:" + ca +
s += ((ca) ? (" CA:true") : (" CA:false")) + "\n"; "\n PathLen:" +
if (pathLen >= 0) { ((pathLen >= 0) ? String.valueOf(pathLen) : " undefined") +
s += " PathLen:" + pathLen + "\n"; "\n]\n";
} else {
s += " PathLen: undefined\n";
}
return (s + "]\n");
} }
/** /**

View File

@ -231,8 +231,8 @@ public class CRLDistributionPointsExtension extends Extension
distributionPoints = (List<DistributionPoint>)obj; distributionPoints = (List<DistributionPoint>)obj;
} else { } else {
throw new IOException("Attribute name [" + name + throw new IOException("Attribute name [" + name +
"] not recognized by " + "] not recognized by " +
"CertAttrSet:" + extensionName + "."); "CertAttrSet:" + extensionName + '.');
} }
encodeThis(); encodeThis();
} }
@ -245,8 +245,8 @@ public class CRLDistributionPointsExtension extends Extension
return distributionPoints; return distributionPoints;
} else { } else {
throw new IOException("Attribute name [" + name + throw new IOException("Attribute name [" + name +
"] not recognized by " + "] not recognized by " +
"CertAttrSet:" + extensionName + "."); "CertAttrSet:" + extensionName + '.');
} }
} }

View File

@ -146,8 +146,8 @@ implements CertAttrSet<String> {
} }
crlNumber = (BigInteger)obj; crlNumber = (BigInteger)obj;
} else { } else {
throw new IOException("Attribute name not recognized by" throw new IOException("Attribute name not recognized by" +
+ " CertAttrSet:" + extensionName + "."); " CertAttrSet:" + extensionName + '.');
} }
encodeThis(); encodeThis();
} }
@ -172,8 +172,8 @@ implements CertAttrSet<String> {
if (name.equalsIgnoreCase(NUMBER)) { if (name.equalsIgnoreCase(NUMBER)) {
crlNumber = null; crlNumber = null;
} else { } else {
throw new IOException("Attribute name not recognized by" throw new IOException("Attribute name not recognized by" +
+ " CertAttrSet:" + extensionName + "."); " CertAttrSet:" + extensionName + '.');
} }
encodeThis(); encodeThis();
} }
@ -182,10 +182,15 @@ implements CertAttrSet<String> {
* Returns a printable representation of the CRLNumberExtension. * Returns a printable representation of the CRLNumberExtension.
*/ */
public String toString() { public String toString() {
String s = super.toString() + extensionLabel + ": " + StringBuilder sb = new StringBuilder();
((crlNumber == null) ? "" : Debug.toHexString(crlNumber)) sb.append(super.toString())
+ "\n"; .append(extensionLabel)
return (s); .append(": ");
if (crlNumber != null) {
sb.append(Debug.toHexString(crlNumber));
}
sb.append('\n');
return sb.toString();
} }
/** /**
@ -195,7 +200,7 @@ implements CertAttrSet<String> {
* @exception IOException on encoding errors. * @exception IOException on encoding errors.
*/ */
public void encode(OutputStream out) throws IOException { public void encode(OutputStream out) throws IOException {
DerOutputStream tmp = new DerOutputStream(); DerOutputStream tmp = new DerOutputStream();
encode(out, PKIXExtensions.CRLNumber_Id, true); encode(out, PKIXExtensions.CRLNumber_Id, true);
} }

View File

@ -158,7 +158,7 @@ public class CertException extends SecurityException {
*/ */
public String toString() public String toString()
{ {
return "[Certificate Exception: " + getMessage() + "]"; return "[Certificate Exception: " + getMessage() + ']';
} }
/** /**
@ -168,6 +168,6 @@ public class CertException extends SecurityException {
{ {
return getVerfDescription() return getVerfDescription()
+ ( (moreData != null) + ( (moreData != null)
? ( "\n (" + moreData + ")" ) : "" ); ? ( "\n (" + moreData + ')' ) : "" );
} }
} }

View File

@ -160,10 +160,12 @@ implements CertAttrSet<String> {
if (certPolicies == null) { if (certPolicies == null) {
return ""; return "";
} }
StringBuilder sb = new StringBuilder(super.toString());
sb.append("CertificatePolicies [\n"); StringBuilder sb = new StringBuilder();
sb.append(super.toString())
.append("CertificatePolicies [\n");
for (PolicyInformation info : certPolicies) { for (PolicyInformation info : certPolicies) {
sb.append(info.toString()); sb.append(info);
} }
sb.append("]\n"); sb.append("]\n");
return sb.toString(); return sb.toString();

View File

@ -134,8 +134,8 @@ public class CertificateValidity implements CertAttrSet<String> {
public String toString() { public String toString() {
if (notBefore == null || notAfter == null) if (notBefore == null || notAfter == null)
return ""; return "";
return ("Validity: [From: " + notBefore.toString() + return "Validity: [From: " + notBefore +
",\n To: " + notAfter.toString() + "]"); ",\n To: " + notAfter + ']';
} }
/** /**

View File

@ -380,23 +380,29 @@ public class DistributionPoint {
*/ */
public String toString() { public String toString() {
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
sb.append("DistributionPoint:\n ");
if (fullName != null) { if (fullName != null) {
sb.append("DistributionPoint:\n " + fullName + "\n"); sb.append(fullName);
} }
if (relativeName != null) { if (relativeName != null) {
sb.append("DistributionPoint:\n " + relativeName + "\n"); sb.append(relativeName);
} }
sb.append('\n');
if (reasonFlags != null) { if (reasonFlags != null) {
sb.append(" ReasonFlags:\n"); sb.append(" ReasonFlags:\n");
for (int i = 0; i < reasonFlags.length; i++) { for (int i = 0; i < reasonFlags.length; i++) {
if (reasonFlags[i]) { if (reasonFlags[i]) {
sb.append(" " + reasonToString(i) + "\n"); sb.append(" ")
.append(reasonToString(i))
.append('\n');
} }
} }
} }
if (crlIssuer != null) { if (crlIssuer != null) {
sb.append(" CRLIssuer:" + crlIssuer + "\n"); sb.append(" CRLIssuer:")
.append(crlIssuer)
.append('\n');
} }
return sb.toString(); return sb.toString();
} }

View File

@ -230,13 +230,13 @@ public class DistributionPointName {
*/ */
public String toString() { public String toString() {
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
sb.append("DistributionPointName:\n ");
if (fullName != null) { if (fullName != null) {
sb.append("DistributionPointName:\n " + fullName + "\n"); sb.append(fullName);
} else { } else {
sb.append("DistributionPointName:\n " + relativeName + "\n"); sb.append(relativeName);
} }
sb.append('\n');
return sb.toString(); return sb.toString();
} }
} }

View File

@ -209,10 +209,15 @@ public class EDIPartyName implements GeneralNameInterface {
* Return the printable string. * Return the printable string.
*/ */
public String toString() { public String toString() {
return ("EDIPartyName: " + StringBuilder sb = new StringBuilder("EDIPartyName: ");
((assigner == null) ? "" : if (assigner != null) {
(" nameAssigner = " + assigner + ",")) sb.append(" nameAssigner = ")
+ " partyName = " + party); .append(assigner)
.append(',');
}
sb.append(" partyName = ")
.append(party);
return sb.toString();
} }
/** /**

View File

@ -219,13 +219,8 @@ public class Extension implements java.security.cert.Extension {
* Returns the Extension in user readable form. * Returns the Extension in user readable form.
*/ */
public String toString() { public String toString() {
String s = "ObjectId: " + extensionId.toString(); return "ObjectId: " + extensionId +
if (critical) { " Criticality=" + critical + '\n';
s += " Criticality=true\n";
} else {
s += " Criticality=false\n";
}
return (s);
} }
// Value to mix up the hash // Value to mix up the hash

View File

@ -127,15 +127,22 @@ public class GeneralSubtree {
* Return a printable string of the GeneralSubtree. * Return a printable string of the GeneralSubtree.
*/ */
public String toString() { public String toString() {
String s = "\n GeneralSubtree: [\n" + StringBuilder sb = new StringBuilder();
" GeneralName: " + ((name == null) ? "" : name.toString()) + sb.append("\n GeneralSubtree: [")
"\n Minimum: " + minimum; .append("\n GeneralName: ");
if (maximum == -1) { if (name != null) {
s += "\t Maximum: undefined"; sb.append(name);
} else }
s += "\t Maximum: " + maximum; sb.append("\n Minimum: ")
s += " ]\n"; .append(minimum)
return (s); .append("\n Maximum: ");
if (maximum == -1) {
sb.append("undefined");
} else {
sb.append(maximum);
}
sb.append(" ]\n");
return sb.toString();
} }
/** /**

View File

@ -124,8 +124,7 @@ public class GeneralSubtrees implements Cloneable {
* Return a printable string of the GeneralSubtree. * Return a printable string of the GeneralSubtree.
*/ */
public String toString() { public String toString() {
String s = " GeneralSubtrees:\n" + trees.toString() + "\n"; return " GeneralSubtrees:\n" + trees + '\n';
return s;
} }
/** /**

View File

@ -263,7 +263,7 @@ public class IPAddressName implements GeneralNameInterface {
if (address.length == 8) { if (address.length == 8) {
byte[] mask = new byte[4]; byte[] mask = new byte[4];
System.arraycopy(address, 4, mask, 0, 4); System.arraycopy(address, 4, mask, 0, 4);
name = name + "/" + name = name + '/' +
InetAddress.getByAddress(mask).getHostAddress(); InetAddress.getByAddress(mask).getHostAddress();
} }
} else { } else {
@ -285,7 +285,7 @@ public class IPAddressName implements GeneralNameInterface {
if (!ba.get(i)) if (!ba.get(i))
break; break;
} }
name = name + "/" + i; name = name + '/' + i;
// Verify remaining bits 0 // Verify remaining bits 0
for (; i < 16*8; i++) { for (; i < 16*8; i++) {
if (ba.get(i)) { if (ba.get(i)) {

View File

@ -140,17 +140,20 @@ extends Extension implements CertAttrSet<String> {
* Returns a printable representation of the IssuerAlternativeName. * Returns a printable representation of the IssuerAlternativeName.
*/ */
public String toString() { public String toString() {
StringBuilder sb = new StringBuilder();
String result = super.toString() + "IssuerAlternativeName [\n"; sb.append(super.toString())
if(names == null) { .append("IssuerAlternativeName [\n");
result += " null\n"; if (names == null) {
sb.append(" null\n");
} else { } else {
for(GeneralName name: names.names()) { for (GeneralName name : names.names()) {
result += " "+name+"\n"; sb.append(" ")
.append(name)
.append('\n');
} }
} }
result += "]\n"; sb.append("]\n");
return result; return sb.toString();
} }
/** /**

View File

@ -441,9 +441,9 @@ public class IssuingDistributionPointExtension extends Extension
* Returns the extension as user readable string. * Returns the extension as user readable string.
*/ */
public String toString() { public String toString() {
StringBuilder sb = new StringBuilder();
StringBuilder sb = new StringBuilder(super.toString()); sb.append(super.toString())
sb.append("IssuingDistributionPoint [\n "); .append("IssuingDistributionPoint [\n ");
if (distributionPoint != null) { if (distributionPoint != null) {
sb.append(distributionPoint); sb.append(distributionPoint);
@ -453,23 +453,18 @@ public class IssuingDistributionPointExtension extends Extension
sb.append(revocationReasons); sb.append(revocationReasons);
} }
sb.append((hasOnlyUserCerts) sb.append(" Only contains user certs: ")
? (" Only contains user certs: true") .append(hasOnlyUserCerts)
: (" Only contains user certs: false")).append("\n"); .append('\n')
.append(" Only contains CA certs: ")
sb.append((hasOnlyCACerts) .append(hasOnlyCACerts)
? (" Only contains CA certs: true") .append('\n')
: (" Only contains CA certs: false")).append("\n"); .append(" Only contains attribute certs: ")
.append(hasOnlyAttributeCerts)
sb.append((hasOnlyAttributeCerts) .append('\n')
? (" Only contains attribute certs: true") .append(" Indirect CRL: ")
: (" Only contains attribute certs: false")).append("\n"); .append(isIndirectCRL)
.append("\n]\n");
sb.append((isIndirectCRL)
? (" Indirect CRL: true")
: (" Indirect CRL: false")).append("\n");
sb.append("]\n");
return sb.toString(); return sb.toString();
} }

View File

@ -214,12 +214,19 @@ implements CertAttrSet<String>, Cloneable {
* Return the printable string. * Return the printable string.
*/ */
public String toString() { public String toString() {
return (super.toString() + "NameConstraints: [" + StringBuilder sb = new StringBuilder();
((permitted == null) ? "" : sb.append(super.toString())
("\n Permitted:" + permitted.toString())) + .append("NameConstraints: [");
((excluded == null) ? "" : if (permitted != null) {
("\n Excluded:" + excluded.toString())) sb.append("\n Permitted:")
+ " ]\n"); .append(permitted);
}
if (excluded != null) {
sb.append("\n Excluded:")
.append(excluded);
}
sb.append(" ]\n");
return sb.toString();
} }
/** /**

View File

@ -175,19 +175,24 @@ implements CertAttrSet<String> {
* Return the extension as user readable string. * Return the extension as user readable string.
*/ */
public String toString() { public String toString() {
String s; StringBuilder sb = new StringBuilder();
s = super.toString() + "PolicyConstraints: [" + " Require: "; sb.append(super.toString())
if (require == -1) .append("PolicyConstraints: [")
s += "unspecified;"; .append(" Require: ");
else if (require == -1) {
s += require + ";"; sb.append("unspecified;");
s += "\tInhibit: "; } else {
if (inhibit == -1) sb.append(require)
s += "unspecified"; .append(';');
else }
s += inhibit; sb.append("\tInhibit: ");
s += " ]\n"; if (inhibit == -1) {
return s; sb.append("unspecified");
} else {
sb.append(inhibit);
}
sb.append(" ]\n");
return sb.toString();
} }
/** /**

View File

@ -258,9 +258,7 @@ public class PolicyInformation {
* Return a printable representation of the PolicyInformation. * Return a printable representation of the PolicyInformation.
*/ */
public String toString() { public String toString() {
StringBuilder s = new StringBuilder(" [" + policyIdentifier.toString()); return " [" + policyIdentifier + policyQualifiers + " ]\n";
s.append(policyQualifiers + " ]\n");
return s.toString();
} }
/** /**

View File

@ -175,11 +175,22 @@ implements CertAttrSet<String> {
* Return the printable string. * Return the printable string.
*/ */
public String toString() { public String toString() {
return(super.toString() + StringBuilder sb = new StringBuilder();
"PrivateKeyUsage: [\n" + sb.append(super.toString())
((notBefore == null) ? "" : "From: " + notBefore.toString() + ", ") .append("PrivateKeyUsage: [\n");
+ ((notAfter == null) ? "" : "To: " + notAfter.toString()) if (notBefore != null) {
+ "]\n"); sb.append("From: ")
.append(notBefore);
if (notAfter != null) {
sb.append(", ");
}
}
if (notAfter != null) {
sb.append("To: ")
.append(notAfter);
}
sb.append("]\n");
return sb.toString();
} }
/** /**

View File

@ -348,14 +348,11 @@ public class RDN {
return assertion[0].toString(); return assertion[0].toString();
} }
StringBuilder sb = new StringBuilder(); StringJoiner sj = new StringJoiner(" + ");
for (int i = 0; i < assertion.length; i++) { for (int i = 0; i < assertion.length; i++) {
if (i != 0) { sj.add(assertion[i].toString());
sb.append(" + ");
}
sb.append(assertion[i].toString());
} }
return sb.toString(); return sj.toString();
} }
/* /*
@ -376,14 +373,11 @@ public class RDN {
return assertion[0].toRFC1779String(oidMap); return assertion[0].toRFC1779String(oidMap);
} }
StringBuilder sb = new StringBuilder(); StringJoiner sj = new StringJoiner(" + ");
for (int i = 0; i < assertion.length; i++) { for (int i = 0; i < assertion.length; i++) {
if (i != 0) { sj.add(assertion[i].toRFC1779String(oidMap));
sb.append(" + ");
}
sb.append(assertion[i].toRFC1779String(oidMap));
} }
return sb.toString(); return sj.toString();
} }
/* /*

View File

@ -101,7 +101,7 @@ public class SerialNumber {
* Return the SerialNumber as user readable string. * Return the SerialNumber as user readable string.
*/ */
public String toString() { public String toString() {
return ("SerialNumber: [" + Debug.toHexString(serialNum) + "]"); return "SerialNumber: [" + Debug.toHexString(serialNum) + ']';
} }
/** /**

View File

@ -238,8 +238,8 @@ public class SubjectInfoAccessExtension extends Extension
* Return the extension as user readable string. * Return the extension as user readable string.
*/ */
public String toString() { public String toString() {
return super.toString() + "SubjectInfoAccess [\n " return super.toString() +
+ accessDescriptions + "\n]\n"; "SubjectInfoAccess [\n " + accessDescriptions + "\n]\n";
} }
} }

View File

@ -115,8 +115,8 @@ implements CertAttrSet<String> {
* Returns a printable representation. * Returns a printable representation.
*/ */
public String toString() { public String toString() {
return super.toString() + "SubjectKeyIdentifier [\n" return super.toString() +
+ String.valueOf(id) + "]\n"; "SubjectKeyIdentifier [\n" + id + "]\n";
} }
/** /**

View File

@ -31,6 +31,7 @@ import java.security.PrivilegedExceptionAction;
import java.security.AccessController; import java.security.AccessController;
import java.security.Principal; import java.security.Principal;
import java.util.*; import java.util.*;
import java.util.StringJoiner;
import sun.security.util.*; import sun.security.util.*;
import javax.security.auth.x500.X500Principal; import javax.security.auth.x500.X500Principal;
@ -689,14 +690,11 @@ public class X500Name implements GeneralNameInterface, Principal {
* The encodings of adjoining RelativeDistinguishedNames are separated * The encodings of adjoining RelativeDistinguishedNames are separated
* by a comma character (',' ASCII 44). * by a comma character (',' ASCII 44).
*/ */
StringBuilder fullname = new StringBuilder(48); StringJoiner sj = new StringJoiner(",");
for (int i = names.length - 1; i >= 0; i--) { for (int i = names.length - 1; i >= 0; i--) {
if (i < names.length - 1) { sj.add(names[i].toRFC2253String(oidMap));
fullname.append(',');
}
fullname.append(names[i].toRFC2253String(oidMap));
} }
return fullname.toString(); return sj.toString();
} }
public String getRFC2253CanonicalName() { public String getRFC2253CanonicalName() {
@ -722,14 +720,11 @@ public class X500Name implements GeneralNameInterface, Principal {
* The encodings of adjoining RelativeDistinguishedNames are separated * The encodings of adjoining RelativeDistinguishedNames are separated
* by a comma character (',' ASCII 44). * by a comma character (',' ASCII 44).
*/ */
StringBuilder fullname = new StringBuilder(48); StringJoiner sj = new StringJoiner(",");
for (int i = names.length - 1; i >= 0; i--) { for (int i = names.length - 1; i >= 0; i--) {
if (i < names.length - 1) { sj.add(names[i].toRFC2253String(true));
fullname.append(',');
}
fullname.append(names[i].toRFC2253String(true));
} }
canonicalDn = fullname.toString(); canonicalDn = sj.toString();
return canonicalDn; return canonicalDn;
} }
@ -1064,16 +1059,16 @@ public class X500Name implements GeneralNameInterface, Principal {
return; return;
} }
StringBuilder sb = new StringBuilder(48); if (names == null) {
if (names != null) { dn = "";
for (int i = names.length - 1; i >= 0; i--) { return;
if (i != names.length - 1) {
sb.append(", ");
}
sb.append(names[i].toString());
}
} }
dn = sb.toString();
StringJoiner sj = new StringJoiner(", ");
for (int i = names.length - 1; i >= 0; i--) {
sj.add(names[i].toString());
}
dn = sj.toString();
} }
/* /*
@ -1090,16 +1085,15 @@ public class X500Name implements GeneralNameInterface, Principal {
return names[0].toRFC1779String(oidMap); return names[0].toRFC1779String(oidMap);
} }
StringBuilder sb = new StringBuilder(48); if (names == null) {
if (names != null) { return "";
for (int i = names.length - 1; i >= 0; i--) {
if (i != names.length - 1) {
sb.append(", ");
}
sb.append(names[i].toRFC1779String(oidMap));
}
} }
return sb.toString();
StringJoiner sj = new StringJoiner(", ");
for (int i = names.length - 1; i >= 0; i--) {
sj.add(names[i].toRFC1779String(oidMap));
}
return sj.toString();
} }
/****************************************************************/ /****************************************************************/

View File

@ -291,40 +291,47 @@ public class X509CRLEntryImpl extends X509CRLEntry
public String toString() { public String toString() {
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
sb.append(serialNumber.toString()); sb.append(serialNumber)
sb.append(" On: " + revocationDate.toString()); .append(" On: ")
.append(revocationDate);
if (certIssuer != null) { if (certIssuer != null) {
sb.append("\n Certificate issuer: " + certIssuer); sb.append("\n Certificate issuer: ")
.append(certIssuer);
} }
if (extensions != null) { if (extensions != null) {
Collection<Extension> allEntryExts = extensions.getAllExtensions(); Collection<Extension> allEntryExts = extensions.getAllExtensions();
Extension[] exts = allEntryExts.toArray(new Extension[0]); Extension[] exts = allEntryExts.toArray(new Extension[0]);
sb.append("\n CRL Entry Extensions: " + exts.length); sb.append("\n CRL Entry Extensions: ")
.append(exts.length);
for (int i = 0; i < exts.length; i++) { for (int i = 0; i < exts.length; i++) {
sb.append("\n [" + (i+1) + "]: "); sb.append("\n [")
.append(i+1)
.append("]: ");
Extension ext = exts[i]; Extension ext = exts[i];
try { try {
if (OIDMap.getClass(ext.getExtensionId()) == null) { if (OIDMap.getClass(ext.getExtensionId()) == null) {
sb.append(ext.toString()); sb.append(ext);
byte[] extValue = ext.getExtensionValue(); byte[] extValue = ext.getExtensionValue();
if (extValue != null) { if (extValue != null) {
DerOutputStream out = new DerOutputStream(); DerOutputStream out = new DerOutputStream();
out.putOctetString(extValue); out.putOctetString(extValue);
extValue = out.toByteArray(); extValue = out.toByteArray();
HexDumpEncoder enc = new HexDumpEncoder(); HexDumpEncoder enc = new HexDumpEncoder();
sb.append("Extension unknown: " sb.append("Extension unknown: ")
+ "DER encoded OCTET string =\n" .append("DER encoded OCTET string =\n")
+ enc.encodeBuffer(extValue) + "\n"); .append(enc.encodeBuffer(extValue))
.append('\n');
} }
} else } else {
sb.append(ext.toString()); //sub-class exists sb.append(ext); //sub-class exists
}
} catch (Exception e) { } catch (Exception e) {
sb.append(", Error parsing this extension"); sb.append(", Error parsing this extension");
} }
} }
} }
sb.append("\n"); sb.append('\n');
return sb.toString(); return sb.toString();
} }

View File

@ -537,47 +537,65 @@ public class X509CRLImpl extends X509CRL implements DerEncoder {
*/ */
public String toString() { public String toString() {
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
sb.append("X.509 CRL v" + (version+1) + "\n"); sb.append("X.509 CRL v")
.append(version+1)
.append('\n');
if (sigAlgId != null) if (sigAlgId != null)
sb.append("Signature Algorithm: " + sigAlgId.toString() + sb.append("Signature Algorithm: ")
", OID=" + (sigAlgId.getOID()).toString() + "\n"); .append(sigAlgId)
.append(", OID=")
.append(sigAlgId.getOID())
.append('\n');
if (issuer != null) if (issuer != null)
sb.append("Issuer: " + issuer.toString() + "\n"); sb.append("Issuer: ")
.append(issuer)
.append('\n');
if (thisUpdate != null) if (thisUpdate != null)
sb.append("\nThis Update: " + thisUpdate.toString() + "\n"); sb.append("\nThis Update: ")
.append(thisUpdate)
.append('\n');
if (nextUpdate != null) if (nextUpdate != null)
sb.append("Next Update: " + nextUpdate.toString() + "\n"); sb.append("Next Update: ")
.append(nextUpdate)
.append('\n');
if (revokedList.isEmpty()) if (revokedList.isEmpty())
sb.append("\nNO certificates have been revoked\n"); sb.append("\nNO certificates have been revoked\n");
else { else {
sb.append("\nRevoked Certificates: " + revokedList.size()); sb.append("\nRevoked Certificates: ")
.append(revokedList.size());
int i = 1; int i = 1;
for (X509CRLEntry entry: revokedList) { for (X509CRLEntry entry: revokedList) {
sb.append("\n[" + i++ + "] " + entry.toString()); sb.append("\n[")
.append(i++)
.append("] ")
.append(entry);
} }
} }
if (extensions != null) { if (extensions != null) {
Collection<Extension> allExts = extensions.getAllExtensions(); Collection<Extension> allExts = extensions.getAllExtensions();
Object[] objs = allExts.toArray(); Object[] objs = allExts.toArray();
sb.append("\nCRL Extensions: " + objs.length); sb.append("\nCRL Extensions: ")
.append(objs.length);
for (int i = 0; i < objs.length; i++) { for (int i = 0; i < objs.length; i++) {
sb.append("\n[" + (i+1) + "]: "); sb.append("\n[").append(i+1).append("]: ");
Extension ext = (Extension)objs[i]; Extension ext = (Extension)objs[i];
try { try {
if (OIDMap.getClass(ext.getExtensionId()) == null) { if (OIDMap.getClass(ext.getExtensionId()) == null) {
sb.append(ext.toString()); sb.append(ext);
byte[] extValue = ext.getExtensionValue(); byte[] extValue = ext.getExtensionValue();
if (extValue != null) { if (extValue != null) {
DerOutputStream out = new DerOutputStream(); DerOutputStream out = new DerOutputStream();
out.putOctetString(extValue); out.putOctetString(extValue);
extValue = out.toByteArray(); extValue = out.toByteArray();
HexDumpEncoder enc = new HexDumpEncoder(); HexDumpEncoder enc = new HexDumpEncoder();
sb.append("Extension unknown: " sb.append("Extension unknown: ")
+ "DER encoded OCTET string =\n" .append("DER encoded OCTET string =\n")
+ enc.encodeBuffer(extValue) + "\n"); .append(enc.encodeBuffer(extValue))
} .append('\n');
} else }
sb.append(ext.toString()); // sub-class exists } else {
sb.append(ext); // sub-class exists
}
} catch (Exception e) { } catch (Exception e) {
sb.append(", Error parsing this extension"); sb.append(", Error parsing this extension");
} }
@ -585,10 +603,12 @@ public class X509CRLImpl extends X509CRL implements DerEncoder {
} }
if (signature != null) { if (signature != null) {
HexDumpEncoder encoder = new HexDumpEncoder(); HexDumpEncoder encoder = new HexDumpEncoder();
sb.append("\nSignature:\n" + encoder.encodeBuffer(signature) sb.append("\nSignature:\n")
+ "\n"); .append(encoder.encodeBuffer(signature))
} else .append('\n');
} else {
sb.append("NOT signed yet\n"); sb.append("NOT signed yet\n");
}
return sb.toString(); return sb.toString();
} }

View File

@ -81,7 +81,7 @@ public class X509CertImpl extends X509Certificate implements DerEncoder {
private static final long serialVersionUID = -3457612960190864406L; private static final long serialVersionUID = -3457612960190864406L;
private static final String DOT = "."; private static final char DOT = '.';
/** /**
* Public attribute names. * Public attribute names.
*/ */
@ -799,17 +799,10 @@ public class X509CertImpl extends X509Certificate implements DerEncoder {
if (info == null || algId == null || signature == null) if (info == null || algId == null || signature == null)
return ""; return "";
StringBuilder sb = new StringBuilder();
sb.append("[\n");
sb.append(info.toString() + "\n");
sb.append(" Algorithm: [" + algId.toString() + "]\n");
HexDumpEncoder encoder = new HexDumpEncoder(); HexDumpEncoder encoder = new HexDumpEncoder();
sb.append(" Signature:\n" + encoder.encodeBuffer(signature)); return "[\n" + info + '\n' +
sb.append("\n]"); " Algorithm: [" + algId + "]\n" +
" Signature:\n" + encoder.encodeBuffer(signature) + "\n]";
return sb.toString();
} }
// the strongly typed gets, as per java.security.cert.X509Certificate // the strongly typed gets, as per java.security.cert.X509Certificate
@ -1941,31 +1934,30 @@ public class X509CertImpl extends X509Certificate implements DerEncoder {
* only contains 0-9 and A-F. No small case, no colon. * only contains 0-9 and A-F. No small case, no colon.
*/ */
private String getCertificateFingerPrint(String mdAlg) { private String getCertificateFingerPrint(String mdAlg) {
String fingerPrint = "";
try { try {
byte[] encCertInfo = getEncoded(); byte[] encCertInfo = getEncoded();
MessageDigest md = MessageDigest.getInstance(mdAlg); MessageDigest md = MessageDigest.getInstance(mdAlg);
byte[] digest = md.digest(encCertInfo); byte[] digest = md.digest(encCertInfo);
StringBuffer buf = new StringBuffer(); StringBuilder sb = new StringBuilder(digest.length * 2);
for (int i = 0; i < digest.length; i++) { for (int i = 0; i < digest.length; i++) {
byte2hex(digest[i], buf); byte2hex(digest[i], sb);
} }
fingerPrint = buf.toString(); return sb.toString();
} catch (NoSuchAlgorithmException | CertificateEncodingException e) { } catch (NoSuchAlgorithmException | CertificateEncodingException e) {
// ignored // ignored
} }
return fingerPrint; return "";
} }
/** /**
* Converts a byte to hex digit and writes to the supplied buffer * Converts a byte to hex digit and writes to the supplied builder
*/ */
private static void byte2hex(byte b, StringBuffer buf) { private static void byte2hex(byte b, StringBuilder buf) {
char[] hexChars = { '0', '1', '2', '3', '4', '5', '6', '7', '8', char[] hexChars = { '0', '1', '2', '3', '4', '5', '6', '7', '8',
'9', 'A', 'B', 'C', 'D', 'E', 'F' }; '9', 'A', 'B', 'C', 'D', 'E', 'F' };
int high = ((b & 0xf0) >> 4); int high = ((b & 0xf0) >> 4);
int low = (b & 0x0f); int low = (b & 0x0f);
buf.append(hexChars[high]); buf.append(hexChars[high])
buf.append(hexChars[low]); .append(hexChars[low]);
} }
} }

View File

@ -299,55 +299,60 @@ public class X509CertInfo implements CertAttrSet<String> {
} }
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
sb.append("[\n"); sb.append("[\n")
sb.append(" " + version.toString() + "\n"); .append(" ").append(version).append('\n')
sb.append(" Subject: " + subject.toString() + "\n"); .append(" Subject: ").append(subject).append('\n')
sb.append(" Signature Algorithm: " + algId.toString() + "\n"); .append(" Signature Algorithm: ").append(algId).append('\n')
sb.append(" Key: " + pubKey.toString() + "\n"); .append(" Key: ").append(pubKey).append('\n')
sb.append(" " + interval.toString() + "\n"); .append(" ").append(interval).append('\n')
sb.append(" Issuer: " + issuer.toString() + "\n"); .append(" Issuer: ").append(issuer).append('\n')
sb.append(" " + serialNum.toString() + "\n"); .append(" ").append(serialNum).append('\n');
// optional v2, v3 extras // optional v2, v3 extras
if (issuerUniqueId != null) { if (issuerUniqueId != null) {
sb.append(" Issuer Id:\n" + issuerUniqueId.toString() + "\n"); sb.append(" Issuer Id:\n").append(issuerUniqueId).append('\n');
} }
if (subjectUniqueId != null) { if (subjectUniqueId != null) {
sb.append(" Subject Id:\n" + subjectUniqueId.toString() + "\n"); sb.append(" Subject Id:\n").append(subjectUniqueId).append('\n');
} }
if (extensions != null) { if (extensions != null) {
Collection<Extension> allExts = extensions.getAllExtensions(); Collection<Extension> allExts = extensions.getAllExtensions();
Extension[] exts = allExts.toArray(new Extension[0]); Extension[] exts = allExts.toArray(new Extension[0]);
sb.append("\nCertificate Extensions: " + exts.length); sb.append("\nCertificate Extensions: ").append(exts.length);
for (int i = 0; i < exts.length; i++) { for (int i = 0; i < exts.length; i++) {
sb.append("\n[" + (i+1) + "]: "); sb.append("\n[").append(i+1).append("]: ");
Extension ext = exts[i]; Extension ext = exts[i];
try { try {
if (OIDMap.getClass(ext.getExtensionId()) == null) { if (OIDMap.getClass(ext.getExtensionId()) == null) {
sb.append(ext.toString()); sb.append(ext);
byte[] extValue = ext.getExtensionValue(); byte[] extValue = ext.getExtensionValue();
if (extValue != null) { if (extValue != null) {
DerOutputStream out = new DerOutputStream(); DerOutputStream out = new DerOutputStream();
out.putOctetString(extValue); out.putOctetString(extValue);
extValue = out.toByteArray(); extValue = out.toByteArray();
HexDumpEncoder enc = new HexDumpEncoder(); HexDumpEncoder enc = new HexDumpEncoder();
sb.append("Extension unknown: " sb.append("Extension unknown: ")
+ "DER encoded OCTET string =\n" .append("DER encoded OCTET string =\n")
+ enc.encodeBuffer(extValue) + "\n"); .append(enc.encodeBuffer(extValue))
.append('\n');
} }
} else } else {
sb.append(ext.toString()); //sub-class exists sb.append(ext); //sub-class exists
}
} catch (Exception e) { } catch (Exception e) {
sb.append(", Error parsing this extension"); sb.append(", Error parsing this extension");
} }
} }
Map<String,Extension> invalid = extensions.getUnparseableExtensions(); Map<String,Extension> invalid = extensions.getUnparseableExtensions();
if (invalid.isEmpty() == false) { if (invalid.isEmpty() == false) {
sb.append("\nUnparseable certificate extensions: " + invalid.size()); sb.append("\nUnparseable certificate extensions: ")
.append(invalid.size());
int i = 1; int i = 1;
for (Extension ext : invalid.values()) { for (Extension ext : invalid.values()) {
sb.append("\n[" + (i++) + "]: "); sb.append("\n[")
sb.append(ext); .append(i++)
.append("]: ")
.append(ext);
} }
} }
} }