Path: blob/master/src/java.base/share/classes/jdk/internal/event/EventHelper.java
41159 views
/*1* Copyright (c) 2018, 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 jdk.internal.event;2627import jdk.internal.access.JavaUtilJarAccess;28import jdk.internal.access.SharedSecrets;2930import java.lang.invoke.MethodHandles;31import java.lang.invoke.VarHandle;32import java.time.Duration;33import java.time.Instant;34import java.util.Date;35import java.util.stream.Collectors;36import java.util.stream.IntStream;3738/**39* A helper class to have events logged to a JDK Event Logger.40*/4142public final class EventHelper {4344private static final JavaUtilJarAccess JUJA = SharedSecrets.javaUtilJarAccess();45private static volatile boolean loggingSecurity;46private static volatile System.Logger securityLogger;47private static final VarHandle LOGGER_HANDLE;48static {49try {50LOGGER_HANDLE =51MethodHandles.lookup().findStaticVarHandle(52EventHelper.class, "securityLogger", System.Logger.class);53} catch (ReflectiveOperationException e) {54throw new Error(e);55}56}57private static final System.Logger.Level LOG_LEVEL = System.Logger.Level.DEBUG;5859// helper class used for logging security related events for now60private static final String SECURITY_LOGGER_NAME = "jdk.event.security";616263public static void logTLSHandshakeEvent(Instant start,64String peerHost,65int peerPort,66String cipherSuite,67String protocolVersion,68long peerCertId) {69assert securityLogger != null;70String prepend = getDurationString(start);71securityLogger.log(LOG_LEVEL, prepend +72" TLSHandshake: {0}:{1,number,#}, {2}, {3}, {4,number,#}",73peerHost, peerPort, protocolVersion, cipherSuite, peerCertId);74}7576public static void logSecurityPropertyEvent(String key,77String value) {7879assert securityLogger != null;80securityLogger.log(LOG_LEVEL,81"SecurityPropertyModification: key:{0}, value:{1}", key, value);82}8384public static void logX509ValidationEvent(int anchorCertId,85int[] certIds) {86assert securityLogger != null;87String codes = IntStream.of(certIds)88.mapToObj(Integer::toString)89.collect(Collectors.joining(", "));90securityLogger.log(LOG_LEVEL,91"ValidationChain: {0,number,#}, {1}", anchorCertId, codes);92}9394public static void logX509CertificateEvent(String algId,95String serialNum,96String subject,97String issuer,98String keyType,99int length,100long certId,101long beginDate,102long endDate) {103assert securityLogger != null;104securityLogger.log(LOG_LEVEL, "X509Certificate: Alg:{0}, Serial:{1}" +105", Subject:{2}, Issuer:{3}, Key type:{4}, Length:{5,number,#}" +106", Cert Id:{6,number,#}, Valid from:{7}, Valid until:{8}",107algId, serialNum, subject, issuer, keyType, length,108certId, new Date(beginDate), new Date(endDate));109}110111/**112* Method to calculate a duration timestamp for events which measure113* the start and end times of certain operations.114* @param start Instant indicating when event started recording115* @return A string representing duraction from start time to116* time of this method call. Empty string is start is null.117*/118private static String getDurationString(Instant start) {119if (start != null) {120if (start.equals(Instant.MIN)) {121return "N/A";122}123Duration duration = Duration.between(start, Instant.now());124long micros = duration.toNanos() / 1_000;125if (micros < 1_000_000) {126return "duration = " + (micros / 1_000.0) + " ms:";127} else {128return "duration = " + ((micros / 1_000) / 1_000.0) + " s:";129}130} else {131return "";132}133}134135/**136* Helper to determine if security events are being logged137* at a preconfigured logging level. The configuration value138* is read once at class initialization.139*140* @return boolean indicating whether an event should be logged141*/142public static boolean isLoggingSecurity() {143// Avoid a bootstrap issue where the commitEvent attempts to144// trigger early loading of System Logger but where145// the verification process still has JarFiles locked146if (securityLogger == null && !JUJA.isInitializing()) {147LOGGER_HANDLE.compareAndSet( null, System.getLogger(SECURITY_LOGGER_NAME));148loggingSecurity = securityLogger.isLoggable(LOG_LEVEL);149}150return loggingSecurity;151}152153}154155156