Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/sun/security/provider/X509Factory/BadPem.java
41154 views
1
/*
2
* Copyright (c) 2015, 2018, 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 8074935 8208602
27
* @summary X.509 cert PEM format read
28
* @modules java.base/sun.security.provider
29
*/
30
31
import java.io.ByteArrayOutputStream;
32
import java.io.FileInputStream;
33
import java.io.PrintStream;
34
import java.security.KeyStore;
35
import java.security.cert.CertificateException;
36
import java.util.Arrays;
37
import java.util.Base64;
38
39
import sun.security.provider.X509Factory;
40
import java.security.cert.CertificateFactory;
41
import java.io.ByteArrayInputStream;
42
43
public class BadPem {
44
45
public static void main(String[] args) throws Exception {
46
String ks = System.getProperty("test.src", ".")
47
+ "/../../../../javax/net/ssl/etc/keystore";
48
String pass = "passphrase";
49
String alias = "dummy";
50
51
CertificateFactory cf = CertificateFactory.getInstance("X.509");
52
KeyStore keyStore = KeyStore.getInstance("JKS");
53
keyStore.load(new FileInputStream(ks), pass.toCharArray());
54
byte[] cert = keyStore.getCertificate(alias).getEncoded();
55
56
// 8074935
57
ByteArrayOutputStream bout = new ByteArrayOutputStream();
58
PrintStream pout = new PrintStream(bout);
59
byte[] CRLF = new byte[] {'\r', '\n'};
60
pout.println(X509Factory.BEGIN_CERT);
61
for (int i=0; i<cert.length; i += 48) {
62
int blockLen = (cert.length > i + 48) ? 48 : (cert.length - i);
63
pout.println("!" + Base64.getEncoder()
64
.encodeToString(Arrays.copyOfRange(cert, i, i + blockLen)));
65
}
66
pout.println(X509Factory.END_CERT);
67
68
try {
69
cf.generateCertificate(new ByteArrayInputStream(bout.toByteArray()));
70
throw new Exception("Should fail");
71
} catch (CertificateException e) {
72
// Good
73
}
74
75
// 8208602
76
bout.reset();
77
pout.println(X509Factory.BEGIN_CERT + " ");
78
pout.println(Base64.getMimeEncoder().encodeToString(cert));
79
pout.println(X509Factory.END_CERT + " ");
80
81
cf.generateCertificate(new ByteArrayInputStream(bout.toByteArray()));
82
}
83
}
84
85
86