Path: blob/master/test/jdk/javax/xml/crypto/dsig/FileSocketPermissions.java
41152 views
/*1* Copyright (c) 2021, 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.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/**24* @test25* @bug 826427726* @library /test/lib27* @modules jdk.httpserver28* java.base/jdk.internal.misc29* @requires os.family != "windows"30* @summary check permissions for XML signature31*/3233import com.sun.net.httpserver.HttpServer;34import jdk.test.lib.Asserts;35import jdk.test.lib.process.Proc;36import jdk.test.lib.security.XMLUtils;3738import java.io.File;39import java.io.FilePermission;40import java.net.InetSocketAddress;41import java.net.SocketPermission;42import java.net.URI;43import java.nio.charset.StandardCharsets;44import java.nio.file.Files;45import java.nio.file.Path;46import java.security.KeyPair;47import java.security.KeyPairGenerator;4849// Note: This test does not run fine on Windows because the format by50// Path.toUri.toString (file:///c:/path/to/file) is not supported by51// ResolverLocalFilesystem.translateUriToFilename.52public class FileSocketPermissions {53public static void main(String[] args) throws Exception {54if (args.length == 0) {55Path plain = Files.writeString(56Path.of(System.getProperty("user.dir"), "a.xml"), "<a>x</a>");57HttpServer server = HttpServer.create(new InetSocketAddress(0), 0);58server.createContext("/", ex -> {59ex.sendResponseHeaders(200, 0);60ex.getResponseBody().write("<a>x</a>".getBytes(StandardCharsets.UTF_8));61ex.close();62});63server.start();64try {65String httpDoc = "http://localhost:" + server.getAddress().getPort() + "/b.xml";66System.out.println(httpDoc);6768// No permission granted.69Proc p0 = Proc.create("FileSocketPermissions")70.prop("java.security.manager", "")71.debug("S")72.args("sign", plain.toUri().toString(), httpDoc)73.start();74Asserts.assertEQ(p0.readData(), "Error");75Asserts.assertEQ(p0.readData(), "Error");7677// Permission to file and socket granted.78Proc p = Proc.create("FileSocketPermissions")79.prop("java.security.manager", "")80.grant(new File(System.getProperty("test.classes")))81.perm(new FilePermission(plain.toString(), "read"))82.perm(new SocketPermission("localhost", "resolve,connect"))83.debug("S2")84.args("sign", plain.toUri().toString(), httpDoc)85.start();8687Proc p2 = Proc.create("FileSocketPermissions")88.prop("java.security.manager", "")89.grant(new File(System.getProperty("test.classes")))90.perm(new FilePermission(plain.toString(), "read"))91.perm(new SocketPermission("localhost", "resolve,connect"))92.debug("V")93.args("validate")94.start();9596while (true) {97String in = p.readData(); // read signed XML from signer98p2.println(in); // send signed XML to validator99if (in.equals("Over")) {100break;101}102if (!p2.readData().equals("true")) { // read validator result103throw new Exception("Validation error");104}105}106} finally {107server.stop(0);108}109} else if (args[0].equals("sign")) {110KeyPairGenerator g = KeyPairGenerator.getInstance("EC");111KeyPair p = g.generateKeyPair();112var signer = XMLUtils.signer(p.getPrivate(), p.getPublic());113for (int i = 1; i < args.length; i++) {114try {115// Multiple line XML. Send as raw bytes (in Base64)116Proc.binOut(XMLUtils.doc2string(signer.sign(new URI(args[i])))117.getBytes(StandardCharsets.UTF_8));118} catch (Exception se) {119se.printStackTrace();120Proc.textOut("Error");121}122}123Proc.textOut("Over");124} else if (args[0].equals("validate")) {125// Turn secureValidation off. Will read external data126var validator = XMLUtils.validator().secureValidation(false);127while (true) {128String in = new String(Proc.binIn());129if (in.equals("Over")) {130Proc.textOut("Over");131break;132}133Proc.textOut(Boolean.toString(validator.validate(XMLUtils.string2doc(in))));134}135}136}137}138139140