Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/sun/security/util/Debug.java
38830 views
/*1* Copyright (c) 1998, 2014, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.security.util;2627import java.math.BigInteger;28import java.util.regex.Pattern;29import java.util.regex.Matcher;30import java.util.Locale;3132/**33* A utility class for debuging.34*35* @author Roland Schemers36*/37public class Debug {3839private String prefix;4041private static String args;4243static {44args = java.security.AccessController.doPrivileged45(new sun.security.action.GetPropertyAction46("java.security.debug"));4748String args2 = java.security.AccessController.doPrivileged49(new sun.security.action.GetPropertyAction50("java.security.auth.debug"));5152if (args == null) {53args = args2;54} else {55if (args2 != null)56args = args + "," + args2;57}5859if (args != null) {60args = marshal(args);61if (args.equals("help")) {62Help();63}64}65}6667public static void Help()68{69System.err.println();70System.err.println("all turn on all debugging");71System.err.println("access print all checkPermission results");72System.err.println("certpath PKIX CertPathBuilder and");73System.err.println(" CertPathValidator debugging");74System.err.println("combiner SubjectDomainCombiner debugging");75System.err.println("gssloginconfig");76System.err.println(" GSS LoginConfigImpl debugging");77System.err.println("configfile JAAS ConfigFile loading");78System.err.println("configparser JAAS ConfigFile parsing");79System.err.println("jar jar verification");80System.err.println("logincontext login context results");81System.err.println("jca JCA engine class debugging");82System.err.println("policy loading and granting");83System.err.println("provider security provider debugging");84System.err.println("pkcs11 PKCS11 session manager debugging");85System.err.println("pkcs11keystore");86System.err.println(" PKCS11 KeyStore debugging");87System.err.println("sunpkcs11 SunPKCS11 provider debugging");88System.err.println("scl permissions SecureClassLoader assigns");89System.err.println("ts timestamping");90System.err.println();91System.err.println("The following can be used with access:");92System.err.println();93System.err.println("stack include stack trace");94System.err.println("domain dump all domains in context");95System.err.println("failure before throwing exception, dump stack");96System.err.println(" and domain that didn't have permission");97System.err.println();98System.err.println("The following can be used with stack and domain:");99System.err.println();100System.err.println("permission=<classname>");101System.err.println(" only dump output if specified permission");102System.err.println(" is being checked");103System.err.println("codebase=<URL>");104System.err.println(" only dump output if specified codebase");105System.err.println(" is being checked");106System.err.println();107System.err.println("The following can be used with provider:");108System.err.println();109System.err.println("engine=<engines>");110System.err.println(" only dump output for the specified list");111System.err.println(" of JCA engines. Supported values:");112System.err.println(" Cipher, KeyAgreement, KeyGenerator,");113System.err.println(" KeyPairGenerator, KeyStore, Mac,");114System.err.println(" MessageDigest, SecureRandom, Signature.");115System.err.println();116System.err.println("Note: Separate multiple options with a comma");117System.exit(0);118}119120121/**122* Get a Debug object corresponding to whether or not the given123* option is set. Set the prefix to be the same as option.124*/125126public static Debug getInstance(String option)127{128return getInstance(option, option);129}130131/**132* Get a Debug object corresponding to whether or not the given133* option is set. Set the prefix to be prefix.134*/135public static Debug getInstance(String option, String prefix)136{137if (isOn(option)) {138Debug d = new Debug();139d.prefix = prefix;140return d;141} else {142return null;143}144}145146/**147* True if the system property "security.debug" contains the148* string "option".149*/150public static boolean isOn(String option)151{152if (args == null)153return false;154else {155if (args.indexOf("all") != -1)156return true;157else158return (args.indexOf(option) != -1);159}160}161162/**163* print a message to stderr that is prefixed with the prefix164* created from the call to getInstance.165*/166167public void println(String message)168{169System.err.println(prefix + ": "+message);170}171172/**173* print a blank line to stderr that is prefixed with the prefix.174*/175176public void println()177{178System.err.println(prefix + ":");179}180181/**182* print a message to stderr that is prefixed with the prefix.183*/184185public static void println(String prefix, String message)186{187System.err.println(prefix + ": "+message);188}189190/**191* return a hexadecimal printed representation of the specified192* BigInteger object. the value is formatted to fit on lines of193* at least 75 characters, with embedded newlines. Words are194* separated for readability, with eight words (32 bytes) per line.195*/196public static String toHexString(BigInteger b) {197String hexValue = b.toString(16);198StringBuffer buf = new StringBuffer(hexValue.length()*2);199200if (hexValue.startsWith("-")) {201buf.append(" -");202hexValue = hexValue.substring(1);203} else {204buf.append(" "); // four spaces205}206if ((hexValue.length()%2) != 0) {207// add back the leading 0208hexValue = "0" + hexValue;209}210int i=0;211while (i < hexValue.length()) {212// one byte at a time213buf.append(hexValue.substring(i, i+2));214i+=2;215if (i!= hexValue.length()) {216if ((i%64) == 0) {217buf.append("\n "); // line after eight words218} else if (i%8 == 0) {219buf.append(" "); // space between words220}221}222}223return buf.toString();224}225226/**227* change a string into lower case except permission classes and URLs.228*/229private static String marshal(String args) {230if (args != null) {231StringBuffer target = new StringBuffer();232StringBuffer source = new StringBuffer(args);233234// obtain the "permission=<classname>" options235// the syntax of classname: IDENTIFIER.IDENTIFIER236// the regular express to match a class name:237// "[a-zA-Z_$][a-zA-Z0-9_$]*([.][a-zA-Z_$][a-zA-Z0-9_$]*)*"238String keyReg = "[Pp][Ee][Rr][Mm][Ii][Ss][Ss][Ii][Oo][Nn]=";239String keyStr = "permission=";240String reg = keyReg +241"[a-zA-Z_$][a-zA-Z0-9_$]*([.][a-zA-Z_$][a-zA-Z0-9_$]*)*";242Pattern pattern = Pattern.compile(reg);243Matcher matcher = pattern.matcher(source);244StringBuffer left = new StringBuffer();245while (matcher.find()) {246String matched = matcher.group();247target.append(matched.replaceFirst(keyReg, keyStr));248target.append(" ");249250// delete the matched sequence251matcher.appendReplacement(left, "");252}253matcher.appendTail(left);254source = left;255256// obtain the "codebase=<URL>" options257// the syntax of URL is too flexible, and here assumes that the258// URL contains no space, comma(','), and semicolon(';'). That259// also means those characters also could be used as separator260// after codebase option.261// However, the assumption is incorrect in some special situation262// when the URL contains comma or semicolon263keyReg = "[Cc][Oo][Dd][Ee][Bb][Aa][Ss][Ee]=";264keyStr = "codebase=";265reg = keyReg + "[^, ;]*";266pattern = Pattern.compile(reg);267matcher = pattern.matcher(source);268left = new StringBuffer();269while (matcher.find()) {270String matched = matcher.group();271target.append(matched.replaceFirst(keyReg, keyStr));272target.append(" ");273274// delete the matched sequence275matcher.appendReplacement(left, "");276}277matcher.appendTail(left);278source = left;279280// convert the rest to lower-case characters281target.append(source.toString().toLowerCase(Locale.ENGLISH));282283return target.toString();284}285286return null;287}288289private final static char[] hexDigits = "0123456789abcdef".toCharArray();290291public static String toString(byte[] b) {292if (b == null) {293return "(null)";294}295StringBuilder sb = new StringBuilder(b.length * 3);296for (int i = 0; i < b.length; i++) {297int k = b[i] & 0xff;298if (i != 0) {299sb.append(':');300}301sb.append(hexDigits[k >>> 4]);302sb.append(hexDigits[k & 0xf]);303}304return sb.toString();305}306307}308309310