Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/security/cert/CertPathValidator/nameConstraintsRFC822/ValidateCertPath.java
41161 views
1
/*
2
* Copyright (c) 2002, 2010, 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 4684810 6994717
27
* @summary Verify that RFC822 name constraints are checked correctly
28
*/
29
30
import java.io.ByteArrayOutputStream;
31
import java.io.File;
32
import java.io.FileInputStream;
33
import java.io.InputStream;
34
import java.io.IOException;
35
36
import java.security.cert.*;
37
import java.security.cert.PKIXReason;
38
39
import java.util.ArrayList;
40
import java.util.Collections;
41
import java.util.Date;
42
import java.util.List;
43
import java.util.Set;
44
45
/**
46
* ValidateCertPath performs a simple validation of a certification path.
47
* On success, it prints the CertPathValidatorResult. On failure, it
48
* prints the error.
49
*
50
* Synopsis:
51
* <pre>
52
* ValidateCertPath trustAnchor [certFile ...]
53
* where each argument is the path to a file that contains a
54
* certificate. Each certificate should have an issuer equal to
55
* the subject of the preceding certificate.
56
*</pre>
57
*
58
* @author Steve Hanna
59
*/
60
public final class ValidateCertPath {
61
62
private final static String BASE = System.getProperty("test.src", "./");
63
64
private static CertPath path;
65
private static PKIXParameters params;
66
67
public static void main(String[] args) throws Exception {
68
69
try {
70
parseArgs(args);
71
validate(path, params);
72
throw new Exception("Successfully validated invalid path.");
73
} catch (CertPathValidatorException e) {
74
if (e.getReason() != PKIXReason.INVALID_NAME) {
75
throw new Exception("unexpected reason: " + e.getReason());
76
}
77
System.out.println("Path rejected as expected: " + e);
78
}
79
}
80
81
/**
82
* Parse the command line arguments. Populate the static
83
* class fields based on the values of the arugments. In
84
* case of bad arguments, print usage and exit. In case of
85
* other error, throw an exception.
86
*
87
* @param args command line arguments
88
* @throws Exception on error
89
*/
90
public static void parseArgs(String[] args) throws Exception {
91
args = new String[] {"jane2jane.cer", "jane2steve.cer", "steve2tom.cer"};
92
93
TrustAnchor anchor = new TrustAnchor(getCertFromFile(args[0]), null);
94
List<X509Certificate> list = new ArrayList<X509Certificate>();
95
for (int i = 1; i < args.length; i++) {
96
list.add(0, getCertFromFile(args[i]));
97
}
98
CertificateFactory cf = CertificateFactory.getInstance("X509");
99
path = cf.generateCertPath(list);
100
101
Set<TrustAnchor> anchors = Collections.singleton(anchor);
102
params = new PKIXParameters(anchors);
103
params.setRevocationEnabled(false);
104
// The certificates expired on 10/22/10, so set the validity date to
105
// 05/01/2009 to avoid expiration failures
106
params.setDate(new Date(1243828800000l));
107
}
108
109
/*
110
* Reads the entire input stream into a byte array.
111
*/
112
private static byte[] getTotalBytes(InputStream is) throws IOException {
113
byte[] buffer = new byte[8192];
114
ByteArrayOutputStream baos = new ByteArrayOutputStream(2048);
115
int n;
116
baos.reset();
117
while ((n = is.read(buffer, 0, buffer.length)) != -1) {
118
baos.write(buffer, 0, n);
119
}
120
return baos.toByteArray();
121
}
122
123
/**
124
* Get a DER-encoded X.509 certificate from a file.
125
*
126
* @param certFilePath path to file containing DER-encoded certificate
127
* @return X509Certificate
128
* @throws IOException on error
129
*/
130
public static X509Certificate getCertFromFile(String certFilePath)
131
throws IOException {
132
X509Certificate cert = null;
133
try {
134
File certFile = new File(BASE, certFilePath);
135
if (!certFile.canRead())
136
throw new IOException("File " +
137
certFile.toString() +
138
" is not a readable file.");
139
FileInputStream certFileInputStream =
140
new FileInputStream(certFile);
141
CertificateFactory cf = CertificateFactory.getInstance("X509");
142
cert = (X509Certificate)
143
cf.generateCertificate(certFileInputStream);
144
} catch (Exception e) {
145
e.printStackTrace();
146
throw new IOException("Can't construct X509Certificate: " +
147
e.getMessage());
148
}
149
return cert;
150
}
151
152
/**
153
* Perform a PKIX validation. On success, print the
154
* CertPathValidatorResult on System.out. On failure,
155
* throw an exception.
156
*
157
* @param path CertPath to validate
158
* @param params PKIXParameters to use in validation
159
* @throws Exception on error
160
*/
161
public static void validate(CertPath path, PKIXParameters params)
162
throws Exception {
163
CertPathValidator validator =
164
CertPathValidator.getInstance("PKIX");
165
CertPathValidatorResult cpvr = validator.validate(path, params);
166
System.out.println("ValidateCertPath successful.");
167
}
168
}
169
170