Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/java.base/share/classes/jdk/internal/event/EventHelper.java
41159 views
1
/*
2
* Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation. Oracle designates this
8
* particular file as subject to the "Classpath" exception as provided
9
* by Oracle in the LICENSE file that accompanied this code.
10
*
11
* This code is distributed in the hope that it will be useful, but WITHOUT
12
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14
* version 2 for more details (a copy is included in the LICENSE file that
15
* accompanied this code).
16
*
17
* You should have received a copy of the GNU General Public License version
18
* 2 along with this work; if not, write to the Free Software Foundation,
19
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20
*
21
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22
* or visit www.oracle.com if you need additional information or have any
23
* questions.
24
*/
25
26
package jdk.internal.event;
27
28
import jdk.internal.access.JavaUtilJarAccess;
29
import jdk.internal.access.SharedSecrets;
30
31
import java.lang.invoke.MethodHandles;
32
import java.lang.invoke.VarHandle;
33
import java.time.Duration;
34
import java.time.Instant;
35
import java.util.Date;
36
import java.util.stream.Collectors;
37
import java.util.stream.IntStream;
38
39
/**
40
* A helper class to have events logged to a JDK Event Logger.
41
*/
42
43
public final class EventHelper {
44
45
private static final JavaUtilJarAccess JUJA = SharedSecrets.javaUtilJarAccess();
46
private static volatile boolean loggingSecurity;
47
private static volatile System.Logger securityLogger;
48
private static final VarHandle LOGGER_HANDLE;
49
static {
50
try {
51
LOGGER_HANDLE =
52
MethodHandles.lookup().findStaticVarHandle(
53
EventHelper.class, "securityLogger", System.Logger.class);
54
} catch (ReflectiveOperationException e) {
55
throw new Error(e);
56
}
57
}
58
private static final System.Logger.Level LOG_LEVEL = System.Logger.Level.DEBUG;
59
60
// helper class used for logging security related events for now
61
private static final String SECURITY_LOGGER_NAME = "jdk.event.security";
62
63
64
public static void logTLSHandshakeEvent(Instant start,
65
String peerHost,
66
int peerPort,
67
String cipherSuite,
68
String protocolVersion,
69
long peerCertId) {
70
assert securityLogger != null;
71
String prepend = getDurationString(start);
72
securityLogger.log(LOG_LEVEL, prepend +
73
" TLSHandshake: {0}:{1,number,#}, {2}, {3}, {4,number,#}",
74
peerHost, peerPort, protocolVersion, cipherSuite, peerCertId);
75
}
76
77
public static void logSecurityPropertyEvent(String key,
78
String value) {
79
80
assert securityLogger != null;
81
securityLogger.log(LOG_LEVEL,
82
"SecurityPropertyModification: key:{0}, value:{1}", key, value);
83
}
84
85
public static void logX509ValidationEvent(int anchorCertId,
86
int[] certIds) {
87
assert securityLogger != null;
88
String codes = IntStream.of(certIds)
89
.mapToObj(Integer::toString)
90
.collect(Collectors.joining(", "));
91
securityLogger.log(LOG_LEVEL,
92
"ValidationChain: {0,number,#}, {1}", anchorCertId, codes);
93
}
94
95
public static void logX509CertificateEvent(String algId,
96
String serialNum,
97
String subject,
98
String issuer,
99
String keyType,
100
int length,
101
long certId,
102
long beginDate,
103
long endDate) {
104
assert securityLogger != null;
105
securityLogger.log(LOG_LEVEL, "X509Certificate: Alg:{0}, Serial:{1}" +
106
", Subject:{2}, Issuer:{3}, Key type:{4}, Length:{5,number,#}" +
107
", Cert Id:{6,number,#}, Valid from:{7}, Valid until:{8}",
108
algId, serialNum, subject, issuer, keyType, length,
109
certId, new Date(beginDate), new Date(endDate));
110
}
111
112
/**
113
* Method to calculate a duration timestamp for events which measure
114
* the start and end times of certain operations.
115
* @param start Instant indicating when event started recording
116
* @return A string representing duraction from start time to
117
* time of this method call. Empty string is start is null.
118
*/
119
private static String getDurationString(Instant start) {
120
if (start != null) {
121
if (start.equals(Instant.MIN)) {
122
return "N/A";
123
}
124
Duration duration = Duration.between(start, Instant.now());
125
long micros = duration.toNanos() / 1_000;
126
if (micros < 1_000_000) {
127
return "duration = " + (micros / 1_000.0) + " ms:";
128
} else {
129
return "duration = " + ((micros / 1_000) / 1_000.0) + " s:";
130
}
131
} else {
132
return "";
133
}
134
}
135
136
/**
137
* Helper to determine if security events are being logged
138
* at a preconfigured logging level. The configuration value
139
* is read once at class initialization.
140
*
141
* @return boolean indicating whether an event should be logged
142
*/
143
public static boolean isLoggingSecurity() {
144
// Avoid a bootstrap issue where the commitEvent attempts to
145
// trigger early loading of System Logger but where
146
// the verification process still has JarFiles locked
147
if (securityLogger == null && !JUJA.isInitializing()) {
148
LOGGER_HANDLE.compareAndSet( null, System.getLogger(SECURITY_LOGGER_NAME));
149
loggingSecurity = securityLogger.isLoggable(LOG_LEVEL);
150
}
151
return loggingSecurity;
152
}
153
154
}
155
156