Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/security/Provider/SupportsParameter.java
41149 views
1
/*
2
* Copyright (c) 2003, 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
/**
25
* @test
26
* @bug 4911081 8130181
27
* @summary verify that Provider.Service.supportsParameter() works
28
* @author Andreas Sterbenz
29
*/
30
31
import java.security.*;
32
import java.security.Provider.Service;
33
34
import javax.crypto.*;
35
import javax.crypto.spec.SecretKeySpec;
36
37
public class SupportsParameter {
38
39
public static void main(String[] args) throws Exception {
40
KeyPairGenerator kpg = KeyPairGenerator.getInstance("DSA");
41
kpg.initialize(512);
42
KeyPair kp = kpg.generateKeyPair();
43
PublicKey dsaPublicKey = kp.getPublic();
44
PrivateKey dsaPrivateKey = kp.getPrivate();
45
46
PublicKey myPublicKey = new MyPublicKey();
47
PrivateKey myPrivateKey = new MyPrivateKey();
48
SecretKey mySecretKey = new MySecretKey();
49
50
Provider p = new MyProvider();
51
Service s;
52
53
// none specified, always true
54
s = p.getService("Signature", "DSA0");
55
checkSupports(s, null, true);
56
checkSupports(s, dsaPublicKey, true);
57
checkSupports(s, dsaPrivateKey, true);
58
checkSupports(s, myPublicKey, true);
59
checkSupports(s, myPrivateKey, true);
60
checkSupports(s, mySecretKey, true);
61
62
// DSAPublic/PrivateKey specified as classes
63
s = p.getService("Signature", "DSA");
64
checkSupports(s, dsaPublicKey, true);
65
checkSupports(s, dsaPrivateKey, true);
66
checkSupports(s, myPublicKey, false);
67
checkSupports(s, myPrivateKey, false);
68
checkSupports(s, mySecretKey, false);
69
70
// X.509/PKCS#8 specified as formats
71
s = p.getService("Signature", "DSA2");
72
checkSupports(s, dsaPublicKey, true);
73
checkSupports(s, dsaPrivateKey, true);
74
checkSupports(s, myPublicKey, false);
75
checkSupports(s, myPrivateKey, false);
76
checkSupports(s, mySecretKey, false);
77
78
// MyPublic/PrivateKey + DSAPublicKey specified as classes
79
s = p.getService("Signature", "DSA3");
80
checkSupports(s, dsaPublicKey, true);
81
checkSupports(s, dsaPrivateKey, false);
82
checkSupports(s, myPublicKey, true);
83
checkSupports(s, myPrivateKey, true);
84
checkSupports(s, mySecretKey, false);
85
86
// MyPublic/PrivateKey + DSAPublicKey specified as classes
87
s = p.getService("Cipher", "DES");
88
checkSupports(s, dsaPublicKey, false);
89
checkSupports(s, dsaPrivateKey, false);
90
checkSupports(s, myPublicKey, false);
91
checkSupports(s, myPrivateKey, false);
92
checkSupports(s, mySecretKey, true);
93
Key secretKeySpec = new SecretKeySpec(new byte[8], "DES");
94
checkSupports(s, secretKeySpec, true);
95
96
}
97
98
private static void checkSupports(Service s, Key key, boolean r) throws Exception {
99
if (s.supportsParameter(key) != r) {
100
throw new Exception("Result mismatch");
101
}
102
System.out.println("Passed");
103
}
104
105
private static class MyProvider extends Provider {
106
MyProvider() {
107
super("MyProvider", "1.0", "MyProvider");
108
109
put("Signature.DSA0", "foo.DSA0");
110
111
put("Signature.DSA", "foo.DSA");
112
put("Signature.DSA SupportedKeyClasses",
113
"java.security.interfaces.DSAPublicKey" +
114
"|java.security.interfaces.DSAPrivateKey");
115
116
put("Signature.DSA2", "foo.DSA2");
117
put("Signature.DSA2 SupportedKeyFormats", "X.509|PKCS#8");
118
119
put("Signature.DSA3", "foo.DSA3");
120
put("Signature.DSA3 SupportedKeyClasses",
121
"SupportsParameter$MyPrivateKey" +
122
"|SupportsParameter$MyPublicKey" +
123
"|java.security.interfaces.DSAPublicKey");
124
125
put("Cipher.DES", "foo.DES");
126
put("Cipher.DES SupportedKeyFormats", "RAW");
127
put("Cipher.DES SupportedKeyClasses", "SupportsParameter$MySecretKey");
128
}
129
}
130
131
private static class MyKey implements Key {
132
public String getAlgorithm() { return "FOO"; }
133
134
public String getFormat() { return null; }
135
136
public byte[] getEncoded() { return null; }
137
}
138
139
private static class MyPrivateKey extends MyKey implements PrivateKey { }
140
141
private static class MyPublicKey extends MyKey implements PublicKey {}
142
143
private static class MySecretKey extends MyKey implements SecretKey {
144
public String getAlgorithm() { return "DES"; }
145
}
146
147
}
148
149