Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/javax/xml/crypto/dsig/SecurityManager/XMLDSigWithSecMgr.java
41154 views
1
/*
2
* Copyright (c) 2006, 2015, 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 6436919 6460930
27
* @summary check that XML Signatures can be generated and validated with
28
* SecurityManager enabled and default policy
29
* @run main/othervm -Djava.security.manager=allow XMLDSigWithSecMgr
30
* @author Sean Mullan
31
*/
32
import java.io.*;
33
import java.net.*;
34
import java.security.KeyPair;
35
import java.security.KeyPairGenerator;
36
import java.util.ArrayList;
37
import java.util.Collections;
38
import javax.xml.crypto.dsig.*;
39
import javax.xml.crypto.dsig.dom.DOMSignContext;
40
import javax.xml.crypto.dsig.dom.DOMValidateContext;
41
import javax.xml.crypto.dsig.spec.C14NMethodParameterSpec;
42
import javax.xml.crypto.dsig.spec.TransformParameterSpec;
43
import javax.xml.parsers.DocumentBuilder;
44
import javax.xml.parsers.DocumentBuilderFactory;
45
import org.w3c.dom.Document;
46
import org.w3c.dom.Element;
47
48
public class XMLDSigWithSecMgr implements Runnable {
49
50
private XMLSignatureFactory fac;
51
private DigestMethod sha1;
52
private CanonicalizationMethod withoutComments;
53
private DocumentBuilder db;
54
55
private ServerSocket ss;
56
57
private void setup() throws Exception {
58
ss = new ServerSocket(0);
59
Thread thr = new Thread(this);
60
thr.start();
61
62
fac = XMLSignatureFactory.getInstance();
63
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
64
dbf.setNamespaceAware(true);
65
db = dbf.newDocumentBuilder();
66
sha1 = fac.newDigestMethod(DigestMethod.SHA1, null);
67
withoutComments = fac.newCanonicalizationMethod
68
(CanonicalizationMethod.INCLUSIVE, (C14NMethodParameterSpec)null);
69
}
70
71
public void run() {
72
try {
73
74
for (int i=0; i<2; i++) {
75
Socket s = ss.accept();
76
s.setTcpNoDelay(true);
77
78
PrintStream out = new PrintStream(
79
new BufferedOutputStream(
80
s.getOutputStream() ));
81
82
out.print("HTTP/1.1 200 OK\r\n");
83
out.print("Content-Length: 11\r\n");
84
out.print("Content-Type: text/plain\r\n");
85
out.print("\r\n");
86
out.print("l;ajfdjafd\n");
87
out.flush();
88
89
// don't close the connection immediately as otherwise
90
// the http headers may not have been received and the
91
// http client will re-connect.
92
Thread.currentThread().sleep(2000);
93
94
s.close();
95
}
96
97
} catch (Exception e) {
98
e.printStackTrace();
99
}
100
}
101
102
XMLDSigWithSecMgr() throws Exception {
103
setup();
104
Document doc = db.newDocument();
105
Element envelope = doc.createElementNS
106
("http://example.org/envelope", "Envelope");
107
envelope.setAttributeNS("http://www.w3.org/2000/xmlns/",
108
"xmlns", "http://example.org/envelope");
109
doc.appendChild(envelope);
110
111
KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA");
112
KeyPair kp = kpg.genKeyPair();
113
114
// the policy only grants this test SocketPermission to accept, resolve
115
// and connect to localhost so that it can dereference 2nd reference
116
System.setProperty("java.security.policy",
117
System.getProperty("test.src", ".") + File.separator + "policy");
118
System.setSecurityManager(new SecurityManager());
119
120
try {
121
// generate a signature with SecurityManager enabled
122
ArrayList refs = new ArrayList();
123
refs.add(fac.newReference
124
("", sha1,
125
Collections.singletonList
126
(fac.newTransform(Transform.ENVELOPED,
127
(TransformParameterSpec) null)), null, null));
128
refs.add(fac.newReference("http://localhost:" + ss.getLocalPort()
129
+ "/anything.txt", sha1));
130
SignedInfo si = fac.newSignedInfo(withoutComments,
131
fac.newSignatureMethod(SignatureMethod.RSA_SHA1, null), refs);
132
XMLSignature sig = fac.newXMLSignature(si, null);
133
DOMSignContext dsc = new DOMSignContext(kp.getPrivate(), envelope);
134
sig.sign(dsc);
135
136
// validate a signature with SecurityManager enabled
137
DOMValidateContext dvc = new DOMValidateContext
138
(kp.getPublic(), envelope.getFirstChild());
139
140
// disable secure validation mode so that http reference will work
141
dvc.setProperty("org.jcp.xml.dsig.secureValidation", Boolean.FALSE);
142
143
sig = fac.unmarshalXMLSignature(dvc);
144
if (!sig.validate(dvc)) {
145
throw new Exception
146
("XMLDSigWithSecMgr signature validation FAILED");
147
}
148
} catch (SecurityException se) {
149
throw new Exception("XMLDSigWithSecMgr FAILED", se);
150
}
151
ss.close();
152
}
153
154
public static void main(String[] args) throws Exception {
155
new XMLDSigWithSecMgr();
156
}
157
}
158
159