Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/javax/xml/crypto/dsig/FileSocketPermissions.java
41152 views
1
/*
2
* Copyright (c) 2021, 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
/**
25
* @test
26
* @bug 8264277
27
* @library /test/lib
28
* @modules jdk.httpserver
29
* java.base/jdk.internal.misc
30
* @requires os.family != "windows"
31
* @summary check permissions for XML signature
32
*/
33
34
import com.sun.net.httpserver.HttpServer;
35
import jdk.test.lib.Asserts;
36
import jdk.test.lib.process.Proc;
37
import jdk.test.lib.security.XMLUtils;
38
39
import java.io.File;
40
import java.io.FilePermission;
41
import java.net.InetSocketAddress;
42
import java.net.SocketPermission;
43
import java.net.URI;
44
import java.nio.charset.StandardCharsets;
45
import java.nio.file.Files;
46
import java.nio.file.Path;
47
import java.security.KeyPair;
48
import java.security.KeyPairGenerator;
49
50
// Note: This test does not run fine on Windows because the format by
51
// Path.toUri.toString (file:///c:/path/to/file) is not supported by
52
// ResolverLocalFilesystem.translateUriToFilename.
53
public class FileSocketPermissions {
54
public static void main(String[] args) throws Exception {
55
if (args.length == 0) {
56
Path plain = Files.writeString(
57
Path.of(System.getProperty("user.dir"), "a.xml"), "<a>x</a>");
58
HttpServer server = HttpServer.create(new InetSocketAddress(0), 0);
59
server.createContext("/", ex -> {
60
ex.sendResponseHeaders(200, 0);
61
ex.getResponseBody().write("<a>x</a>".getBytes(StandardCharsets.UTF_8));
62
ex.close();
63
});
64
server.start();
65
try {
66
String httpDoc = "http://localhost:" + server.getAddress().getPort() + "/b.xml";
67
System.out.println(httpDoc);
68
69
// No permission granted.
70
Proc p0 = Proc.create("FileSocketPermissions")
71
.prop("java.security.manager", "")
72
.debug("S")
73
.args("sign", plain.toUri().toString(), httpDoc)
74
.start();
75
Asserts.assertEQ(p0.readData(), "Error");
76
Asserts.assertEQ(p0.readData(), "Error");
77
78
// Permission to file and socket granted.
79
Proc p = Proc.create("FileSocketPermissions")
80
.prop("java.security.manager", "")
81
.grant(new File(System.getProperty("test.classes")))
82
.perm(new FilePermission(plain.toString(), "read"))
83
.perm(new SocketPermission("localhost", "resolve,connect"))
84
.debug("S2")
85
.args("sign", plain.toUri().toString(), httpDoc)
86
.start();
87
88
Proc p2 = Proc.create("FileSocketPermissions")
89
.prop("java.security.manager", "")
90
.grant(new File(System.getProperty("test.classes")))
91
.perm(new FilePermission(plain.toString(), "read"))
92
.perm(new SocketPermission("localhost", "resolve,connect"))
93
.debug("V")
94
.args("validate")
95
.start();
96
97
while (true) {
98
String in = p.readData(); // read signed XML from signer
99
p2.println(in); // send signed XML to validator
100
if (in.equals("Over")) {
101
break;
102
}
103
if (!p2.readData().equals("true")) { // read validator result
104
throw new Exception("Validation error");
105
}
106
}
107
} finally {
108
server.stop(0);
109
}
110
} else if (args[0].equals("sign")) {
111
KeyPairGenerator g = KeyPairGenerator.getInstance("EC");
112
KeyPair p = g.generateKeyPair();
113
var signer = XMLUtils.signer(p.getPrivate(), p.getPublic());
114
for (int i = 1; i < args.length; i++) {
115
try {
116
// Multiple line XML. Send as raw bytes (in Base64)
117
Proc.binOut(XMLUtils.doc2string(signer.sign(new URI(args[i])))
118
.getBytes(StandardCharsets.UTF_8));
119
} catch (Exception se) {
120
se.printStackTrace();
121
Proc.textOut("Error");
122
}
123
}
124
Proc.textOut("Over");
125
} else if (args[0].equals("validate")) {
126
// Turn secureValidation off. Will read external data
127
var validator = XMLUtils.validator().secureValidation(false);
128
while (true) {
129
String in = new String(Proc.binIn());
130
if (in.equals("Over")) {
131
Proc.textOut("Over");
132
break;
133
}
134
Proc.textOut(Boolean.toString(validator.validate(XMLUtils.string2doc(in))));
135
}
136
}
137
}
138
}
139
140