Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/javax/net/ssl/ALPN/MyX509ExtendedKeyManager.java
41152 views
1
/*
2
* Copyright (c) 2016, 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.
8
*
9
* This code is distributed in the hope that it will be useful, but WITHOUT
10
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12
* version 2 for more details (a copy is included in the LICENSE file that
13
* accompanied this code).
14
*
15
* You should have received a copy of the GNU General Public License version
16
* 2 along with this work; if not, write to the Free Software Foundation,
17
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18
*
19
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20
* or visit www.oracle.com if you need additional information or have any
21
* questions.
22
*/
23
24
import java.net.Socket;
25
import java.security.Principal;
26
import java.security.PrivateKey;
27
import java.security.cert.X509Certificate;
28
import javax.net.ssl.SSLEngine;
29
import javax.net.ssl.SSLSocket;
30
import javax.net.ssl.X509ExtendedKeyManager;
31
32
public class MyX509ExtendedKeyManager extends X509ExtendedKeyManager {
33
34
static final String ERROR = "ERROR";
35
X509ExtendedKeyManager akm;
36
String expectedAP;
37
boolean doCheck = true;
38
39
MyX509ExtendedKeyManager(X509ExtendedKeyManager akm) {
40
this.akm = akm;
41
}
42
43
public MyX509ExtendedKeyManager(
44
X509ExtendedKeyManager akm, String expectedAP, boolean doCheck) {
45
this.akm = akm;
46
this.expectedAP = expectedAP;
47
this.doCheck = doCheck;
48
49
}
50
51
@Override
52
public String[] getClientAliases(String keyType, Principal[] issuers) {
53
return akm.getClientAliases(keyType, issuers);
54
}
55
56
@Override
57
public String chooseClientAlias(String[] keyType, Principal[] issuers,
58
Socket socket) {
59
String nap = ((SSLSocket) socket).getHandshakeApplicationProtocol();
60
checkALPN(nap);
61
62
return akm.chooseClientAlias(keyType, issuers, socket);
63
}
64
65
@Override
66
public String[] getServerAliases(String keyType, Principal[] issuers) {
67
return akm.getServerAliases(keyType, issuers);
68
}
69
70
@Override
71
public String chooseServerAlias(String keyType, Principal[] issuers,
72
Socket socket) {
73
String nap = ((SSLSocket) socket).getHandshakeApplicationProtocol();
74
checkALPN(nap);
75
76
return akm.chooseServerAlias(keyType, issuers, socket);
77
}
78
79
@Override
80
public X509Certificate[] getCertificateChain(String alias) {
81
return akm.getCertificateChain(alias);
82
}
83
84
@Override
85
public PrivateKey getPrivateKey(String alias) {
86
return akm.getPrivateKey(alias);
87
}
88
89
@Override
90
public String chooseEngineClientAlias(String[] keyType, Principal[] issuers,
91
SSLEngine engine) {
92
String nap = engine.getHandshakeApplicationProtocol();
93
checkALPN(nap);
94
95
return akm.chooseEngineClientAlias(keyType, issuers, engine);
96
}
97
98
@Override
99
public String chooseEngineServerAlias(String keyType, Principal[] issuers,
100
SSLEngine engine) {
101
String nap = engine.getHandshakeApplicationProtocol();
102
checkALPN(nap);
103
104
return akm.chooseEngineServerAlias(keyType, issuers, engine);
105
}
106
107
private void checkALPN(String ap) {
108
109
if (!doCheck) {
110
System.out.println("Skipping KeyManager checks " +
111
"because a callback has been registered");
112
return;
113
}
114
115
if (ERROR.equals(expectedAP)) {
116
throw new RuntimeException("Should not reach here");
117
}
118
119
System.out.println("Expected ALPN value: " + expectedAP
120
+ " Got: " + ap);
121
122
if (ap == null) {
123
throw new RuntimeException(
124
"ALPN should be negotiated, but null was received");
125
}
126
if (expectedAP.equals("NONE")) {
127
if (!ap.isEmpty()) {
128
throw new RuntimeException("Expected no ALPN value");
129
} else {
130
System.out.println("No ALPN value negotiated, as expected");
131
}
132
} else if (!expectedAP.equals(ap)) {
133
throw new RuntimeException(expectedAP
134
+ " ALPN value not available on negotiated connection");
135
}
136
137
}
138
}
139
140