1 /*
2  * Copyright (c) 1998, 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 package test.java.security.cert;
24 
25 import static org.testng.Assert.assertEquals;
26 
27 import java.io.ByteArrayInputStream;
28 import java.security.cert.CertPath;
29 import java.security.cert.Certificate;
30 import java.security.cert.CertificateFactory;
31 import java.util.ArrayList;
32 import java.util.Arrays;
33 import java.util.Base64;
34 import java.util.List;
35 import org.testng.annotations.Test;
36 
37 /*
38  * @test
39  * @bug 8074931
40  * @summary CertPathEncodingTest tests the ability of the CertPath and
41  *          CertificateFactory to encode and decode CertPaths.
42  */
43 public final class CertPathEncodingTest {
44     /*
45             Certificate:
46                 Data:
47                     Version: 3 (0x2)
48                     Serial Number: 935438132 (0x37c1a734)
49                 Signature Algorithm: dsaWithSHA1
50                     Issuer: C=us, O=sun, OU=east, OU=bcn, CN=yassir
51                     Validity
52                         Not Before: Aug 23 19:55:32 1999 GMT
53                         Not After : Aug 22 19:55:32 2000 GMT
54                     Subject: C=us, O=sun, OU=east, OU=bcn
55                     Subject Public Key Info:
56                         Public Key Algorithm: dsaEncryption
57                             pub:
58                                 63:47:4f:f6:29:e5:98:a2:21:fd:da:97:9e:3f:ca:
59                                 b0:17:49:8d:8a:a7:06:0d:a6:78:97:39:59:33:72:
60                                 a2:a5:74:d5:3a:ef:e6:7c:07:d7:8e:8e:d1:66:73:
61                                 99:14:04:96:f5:31:d6:72:ee:d2:53:f8:90:b5:f3:
62                                 c3:f1:64:ba:1a:9e:c0:0a:da:92:48:c5:d3:84:7e:
63                                 48:09:66:d9:51:ba:74:56:5a:77:8a:8c:9a:9c:f6:
64                                 84:12:61:12:51:dc:c6:4f:84:94:ec:cb:78:51:83:
65                                 8c:20:8a:53:7b:d2:b6:36:df:50:35:95:1f:cb:50:
66                                 55:8b:3f:fb:e2:77:cb
67                             P:
68                                 00:fd:7f:53:81:1d:75:12:29:52:df:4a:9c:2e:ec:
69                                 e4:e7:f6:11:b7:52:3c:ef:44:00:c3:1e:3f:80:b6:
70                                 51:26:69:45:5d:40:22:51:fb:59:3d:8d:58:fa:bf:
71                                 c5:f5:ba:30:f6:cb:9b:55:6c:d7:81:3b:80:1d:34:
72                                 6f:f2:66:60:b7:6b:99:50:a5:a4:9f:9f:e8:04:7b:
73                                 10:22:c2:4f:bb:a9:d7:fe:b7:c6:1b:f8:3b:57:e7:
74                                 c6:a8:a6:15:0f:04:fb:83:f6:d3:c5:1e:c3:02:35:
75                                 54:13:5a:16:91:32:f6:75:f3:ae:2b:61:d7:2a:ef:
76                                 f2:22:03:19:9d:d1:48:01:c7
77                             Q:
78                                 00:97:60:50:8f:15:23:0b:cc:b2:92:b9:82:a2:eb:
79                                 84:0b:f0:58:1c:f5
80                             G:
81                                 00:f7:e1:a0:85:d6:9b:3d:de:cb:bc:ab:5c:36:b8:
82                                 57:b9:79:94:af:bb:fa:3a:ea:82:f9:57:4c:0b:3d:
83                                 07:82:67:51:59:57:8e:ba:d4:59:4f:e6:71:07:10:
84                                 81:80:b4:49:16:71:23:e8:4c:28:16:13:b7:cf:09:
85                                 32:8c:c8:a6:e1:3c:16:7a:8b:54:7c:8d:28:e0:a3:
86                                 ae:1e:2b:b3:a6:75:91:6e:a3:7f:0b:fa:21:35:62:
87                                 f1:fb:62:7a:01:24:3b:cc:a4:f1:be:a8:51:90:89:
88                                 a8:83:df:e1:5a:e5:9f:06:92:8b:66:5e:80:7b:55:
89                                 25:64:01:4c:3b:fe:cf:49:2a
90                     X509v3 extensions:
91                         X509v3 Key Usage: critical
92                             Digital Signature, Key Encipherment, Certificate Sign
93                 Signature Algorithm: dsaWithSHA1
94                      r:
95                          52:80:52:2b:2c:3d:02:66:58:b4:dc:ef:52:26:70:
96                          1b:53:ca:b3:7d
97                      s:
98                          62:03:b2:ab:3e:18:2a:66:09:b6:ce:d4:05:a5:8e:
99                          a5:7a:0d:55:67
100     */
101     private static final String cert1 =
102             "-----BEGIN CERTIFICATE-----\n" +
103             "MIICzTCCAougAwIBAgIEN8GnNDALBgcqhkjOOAQDBQAwSTELMAkGA1UEBhMCdXMx\n" +
104             "DDAKBgNVBAoTA3N1bjENMAsGA1UECxMEZWFzdDEMMAoGA1UECxMDYmNuMQ8wDQYD\n" +
105             "VQQDEwZ5YXNzaXIwHhcNOTkwODIzMTk1NTMyWhcNMDAwODIyMTk1NTMyWjA4MQsw\n" +
106             "CQYDVQQGEwJ1czEMMAoGA1UEChMDc3VuMQ0wCwYDVQQLEwRlYXN0MQwwCgYDVQQL\n" +
107             "EwNiY24wggG1MIIBLAYHKoZIzjgEATCCAR8CgYEA/X9TgR11EilS30qcLuzk5/YR\n" +
108             "t1I870QAwx4/gLZRJmlFXUAiUftZPY1Y+r/F9bow9subVWzXgTuAHTRv8mZgt2uZ\n" +
109             "UKWkn5/oBHsQIsJPu6nX/rfGG/g7V+fGqKYVDwT7g/bTxR7DAjVUE1oWkTL2dfOu\n" +
110             "K2HXKu/yIgMZndFIAccCFQCXYFCPFSMLzLKSuYKi64QL8Fgc9QKBgQD34aCF1ps9\n" +
111             "3su8q1w2uFe5eZSvu/o66oL5V0wLPQeCZ1FZV4661FlP5nEHEIGAtEkWcSPoTCgW\n" +
112             "E7fPCTKMyKbhPBZ6i1R8jSjgo64eK7OmdZFuo38L+iE1YvH7YnoBJDvMpPG+qFGQ\n" +
113             "iaiD3+Fa5Z8GkotmXoB7VSVkAUw7/s9JKgOBggACf2NHT/Yp5ZiiIf3al54/yrAX\n" +
114             "SY2KpwYNpniXOVkzcqKldNU67+Z8B9eOjtFmc5kUBJb1MdZy7tJT+JC188PxZLoa\n" +
115             "nsAK2pJIxdOEfkgJZtlRunRWWneKjJqc9oQSYRJR3MZPhJTsy3hRg4wgilN70rY2\n" +
116             "31A1lR/LUFWLP/vid8ujEzARMA8GA1UdDwEB/wQFAwMHpAAwCwYHKoZIzjgEAwUA\n" +
117             "Ay8AMCwCFFKAUissPQJmWLTc71ImcBtTyrN9AhRiA7KrPhgqZgm2ztQFpY6leg1V\n" +
118             "Zw==\n" +
119             "-----END CERTIFICATE-----\n" +
120             "";
121 
122     /*
123             Certificate:
124                 Data:
125                     Version: 3 (0x2)
126                     Serial Number: 935095671 (0x37bc6d77)
127                 Signature Algorithm: dsaWithSHA1
128                     Issuer: C=us, O=sun, OU=east, OU=bcn, CN=yassir
129                     Validity
130                         Not Before: Aug 19 20:47:51 1999 GMT
131                         Not After : Aug 18 20:47:51 2000 GMT
132                     Subject: C=us, O=sun, OU=east, OU=bcn, CN=yassir
133                     Subject Public Key Info:
134                         Public Key Algorithm: dsaEncryption
135                             pub:
136                                 0a:cc:a4:ec:d6:88:45:c2:24:6b:0d:78:f1:82:f3:
137                                 5e:3e:31:5d:fb:64:d5:06:5e:39:16:f1:0a:85:d1:
138                                 ff:d1:a4:74:c5:e6:b0:ba:93:1c:ee:69:51:be:3b:
139                                 a6:66:44:50:b4:f0:5e:0e:dd:9f:08:71:fe:a1:91:
140                                 2e:d4:9e:6b:b2:c0:82:3c:91:6c:18:b0:d9:bc:a3:
141                                 48:91:3f:8b:59:01:61:00:02:ab:22:31:bc:7c:6c:
142                                 0d:9f:ed:be:33:e6:5c:44:9e:62:30:95:f8:6d:22:
143                                 d7:e5:85:4c:b0:98:6e:ad:cc:ca:3b:ad:cb:fa:f7:
144                                 9f:37:13:f7:ca:e2:22:ba
145                             P:
146                                 00:fd:7f:53:81:1d:75:12:29:52:df:4a:9c:2e:ec:
147                                 e4:e7:f6:11:b7:52:3c:ef:44:00:c3:1e:3f:80:b6:
148                                 51:26:69:45:5d:40:22:51:fb:59:3d:8d:58:fa:bf:
149                                 c5:f5:ba:30:f6:cb:9b:55:6c:d7:81:3b:80:1d:34:
150                                 6f:f2:66:60:b7:6b:99:50:a5:a4:9f:9f:e8:04:7b:
151                                 10:22:c2:4f:bb:a9:d7:fe:b7:c6:1b:f8:3b:57:e7:
152                                 c6:a8:a6:15:0f:04:fb:83:f6:d3:c5:1e:c3:02:35:
153                                 54:13:5a:16:91:32:f6:75:f3:ae:2b:61:d7:2a:ef:
154                                 f2:22:03:19:9d:d1:48:01:c7
155                             Q:
156                                 00:97:60:50:8f:15:23:0b:cc:b2:92:b9:82:a2:eb:
157                                 84:0b:f0:58:1c:f5
158                             G:
159                                 00:f7:e1:a0:85:d6:9b:3d:de:cb:bc:ab:5c:36:b8:
160                                 57:b9:79:94:af:bb:fa:3a:ea:82:f9:57:4c:0b:3d:
161                                 07:82:67:51:59:57:8e:ba:d4:59:4f:e6:71:07:10:
162                                 81:80:b4:49:16:71:23:e8:4c:28:16:13:b7:cf:09:
163                                 32:8c:c8:a6:e1:3c:16:7a:8b:54:7c:8d:28:e0:a3:
164                                 ae:1e:2b:b3:a6:75:91:6e:a3:7f:0b:fa:21:35:62:
165                                 f1:fb:62:7a:01:24:3b:cc:a4:f1:be:a8:51:90:89:
166                                 a8:83:df:e1:5a:e5:9f:06:92:8b:66:5e:80:7b:55:
167                                 25:64:01:4c:3b:fe:cf:49:2a
168                     X509v3 extensions:
169                         X509v3 Key Usage: critical
170                             Digital Signature, Key Encipherment, Certificate Sign
171                         X509v3 Basic Constraints: critical
172                             CA:TRUE, pathlen:5
173                 Signature Algorithm: dsaWithSHA1
174                      r:
175                          2f:88:46:37:94:92:b2:02:07:5b:8d:76:e5:81:23:
176                          85:7f:bc:8d:b9
177                      s:
178                          00:8b:d7:41:fa:11:c7:ab:27:92:5d:0a:03:98:56:
179                          36:42:5f:f5:1f:9d
180     */
181     private static final String cert2 =
182             "-----BEGIN CERTIFICATE-----\n" +
183             "MIIC9TCCArKgAwIBAgIEN7xtdzALBgcqhkjOOAQDBQAwSTELMAkGA1UEBhMCdXMx\n" +
184             "DDAKBgNVBAoTA3N1bjENMAsGA1UECxMEZWFzdDEMMAoGA1UECxMDYmNuMQ8wDQYD\n" +
185             "VQQDEwZ5YXNzaXIwHhcNOTkwODE5MjA0NzUxWhcNMDAwODE4MjA0NzUxWjBJMQsw\n" +
186             "CQYDVQQGEwJ1czEMMAoGA1UEChMDc3VuMQ0wCwYDVQQLEwRlYXN0MQwwCgYDVQQL\n" +
187             "EwNiY24xDzANBgNVBAMTBnlhc3NpcjCCAbcwggEsBgcqhkjOOAQBMIIBHwKBgQD9\n" +
188             "f1OBHXUSKVLfSpwu7OTn9hG3UjzvRADDHj+AtlEmaUVdQCJR+1k9jVj6v8X1ujD2\n" +
189             "y5tVbNeBO4AdNG/yZmC3a5lQpaSfn+gEexAiwk+7qdf+t8Yb+DtX58aophUPBPuD\n" +
190             "9tPFHsMCNVQTWhaRMvZ1864rYdcq7/IiAxmd0UgBxwIVAJdgUI8VIwvMspK5gqLr\n" +
191             "hAvwWBz1AoGBAPfhoIXWmz3ey7yrXDa4V7l5lK+7+jrqgvlXTAs9B4JnUVlXjrrU\n" +
192             "WU/mcQcQgYC0SRZxI+hMKBYTt88JMozIpuE8FnqLVHyNKOCjrh4rs6Z1kW6jfwv6\n" +
193             "ITVi8ftiegEkO8yk8b6oUZCJqIPf4VrlnwaSi2ZegHtVJWQBTDv+z0kqA4GEAAKB\n" +
194             "gArMpOzWiEXCJGsNePGC814+MV37ZNUGXjkW8QqF0f/RpHTF5rC6kxzuaVG+O6Zm\n" +
195             "RFC08F4O3Z8Icf6hkS7UnmuywII8kWwYsNm8o0iRP4tZAWEAAqsiMbx8bA2f7b4z\n" +
196             "5lxEnmIwlfhtItflhUywmG6tzMo7rcv69583E/fK4iK6oycwJTAPBgNVHQ8BAf8E\n" +
197             "BQMDB6QAMBIGA1UdEwEB/wQIMAYBAf8CAQUwCwYHKoZIzjgEAwUAAzAAMC0CFC+I\n" +
198             "RjeUkrICB1uNduWBI4V/vI25AhUAi9dB+hHHqyeSXQoDmFY2Ql/1H50=\n" +
199             "-----END CERTIFICATE-----\n" +
200             "";
201 
202     private static final String pkcs7path =
203             "MIIF9QYJKoZIhvcNAQcCoIIF5jCCBeICAQExADALBgkqhkiG9w0BBwGgggXKMIICzTCCAougAwIB\n" +
204             "AgIEN8GnNDALBgcqhkjOOAQDBQAwSTELMAkGA1UEBhMCdXMxDDAKBgNVBAoTA3N1bjENMAsGA1UE\n" +
205             "CxMEZWFzdDEMMAoGA1UECxMDYmNuMQ8wDQYDVQQDEwZ5YXNzaXIwHhcNOTkwODIzMTk1NTMyWhcN\n" +
206             "MDAwODIyMTk1NTMyWjA4MQswCQYDVQQGEwJ1czEMMAoGA1UEChMDc3VuMQ0wCwYDVQQLEwRlYXN0\n" +
207             "MQwwCgYDVQQLEwNiY24wggG1MIIBLAYHKoZIzjgEATCCAR8CgYEA/X9TgR11EilS30qcLuzk5/YR\n" +
208             "t1I870QAwx4/gLZRJmlFXUAiUftZPY1Y+r/F9bow9subVWzXgTuAHTRv8mZgt2uZUKWkn5/oBHsQ\n" +
209             "IsJPu6nX/rfGG/g7V+fGqKYVDwT7g/bTxR7DAjVUE1oWkTL2dfOuK2HXKu/yIgMZndFIAccCFQCX\n" +
210             "YFCPFSMLzLKSuYKi64QL8Fgc9QKBgQD34aCF1ps93su8q1w2uFe5eZSvu/o66oL5V0wLPQeCZ1FZ\n" +
211             "V4661FlP5nEHEIGAtEkWcSPoTCgWE7fPCTKMyKbhPBZ6i1R8jSjgo64eK7OmdZFuo38L+iE1YvH7\n" +
212             "YnoBJDvMpPG+qFGQiaiD3+Fa5Z8GkotmXoB7VSVkAUw7/s9JKgOBggACf2NHT/Yp5ZiiIf3al54/\n" +
213             "yrAXSY2KpwYNpniXOVkzcqKldNU67+Z8B9eOjtFmc5kUBJb1MdZy7tJT+JC188PxZLoansAK2pJI\n" +
214             "xdOEfkgJZtlRunRWWneKjJqc9oQSYRJR3MZPhJTsy3hRg4wgilN70rY231A1lR/LUFWLP/vid8uj\n" +
215             "EzARMA8GA1UdDwEB/wQFAwMHpAAwCwYHKoZIzjgEAwUAAy8AMCwCFFKAUissPQJmWLTc71ImcBtT\n" +
216             "yrN9AhRiA7KrPhgqZgm2ztQFpY6leg1VZzCCAvUwggKyoAMCAQICBDe8bXcwCwYHKoZIzjgEAwUA\n" +
217             "MEkxCzAJBgNVBAYTAnVzMQwwCgYDVQQKEwNzdW4xDTALBgNVBAsTBGVhc3QxDDAKBgNVBAsTA2Jj\n" +
218             "bjEPMA0GA1UEAxMGeWFzc2lyMB4XDTk5MDgxOTIwNDc1MVoXDTAwMDgxODIwNDc1MVowSTELMAkG\n" +
219             "A1UEBhMCdXMxDDAKBgNVBAoTA3N1bjENMAsGA1UECxMEZWFzdDEMMAoGA1UECxMDYmNuMQ8wDQYD\n" +
220             "VQQDEwZ5YXNzaXIwggG3MIIBLAYHKoZIzjgEATCCAR8CgYEA/X9TgR11EilS30qcLuzk5/YRt1I8\n" +
221             "70QAwx4/gLZRJmlFXUAiUftZPY1Y+r/F9bow9subVWzXgTuAHTRv8mZgt2uZUKWkn5/oBHsQIsJP\n" +
222             "u6nX/rfGG/g7V+fGqKYVDwT7g/bTxR7DAjVUE1oWkTL2dfOuK2HXKu/yIgMZndFIAccCFQCXYFCP\n" +
223             "FSMLzLKSuYKi64QL8Fgc9QKBgQD34aCF1ps93su8q1w2uFe5eZSvu/o66oL5V0wLPQeCZ1FZV466\n" +
224             "1FlP5nEHEIGAtEkWcSPoTCgWE7fPCTKMyKbhPBZ6i1R8jSjgo64eK7OmdZFuo38L+iE1YvH7YnoB\n" +
225             "JDvMpPG+qFGQiaiD3+Fa5Z8GkotmXoB7VSVkAUw7/s9JKgOBhAACgYAKzKTs1ohFwiRrDXjxgvNe\n" +
226             "PjFd+2TVBl45FvEKhdH/0aR0xeawupMc7mlRvjumZkRQtPBeDt2fCHH+oZEu1J5rssCCPJFsGLDZ\n" +
227             "vKNIkT+LWQFhAAKrIjG8fGwNn+2+M+ZcRJ5iMJX4bSLX5YVMsJhurczKO63L+vefNxP3yuIiuqMn\n" +
228             "MCUwDwYDVR0PAQH/BAUDAwekADASBgNVHRMBAf8ECDAGAQH/AgEFMAsGByqGSM44BAMFAAMwADAt\n" +
229             "AhQviEY3lJKyAgdbjXblgSOFf7yNuQIVAIvXQfoRx6snkl0KA5hWNkJf9R+dMQA=\n" +
230             "";
231 
232     // Runs test of CertPath encoding and decoding.
233     @Test
testCertPathEncoding(String[] args)234     public static void testCertPathEncoding(String[] args) throws Exception {
235         // Make the CertPath whose encoded form has already been stored
236         CertificateFactory certFac = CertificateFactory.getInstance("X509");
237 
238         final List<Certificate> certs = new ArrayList<>();
239         certs.add(certFac.generateCertificate(new ByteArrayInputStream(cert1.getBytes())));
240         certs.add(certFac.generateCertificate(new ByteArrayInputStream(cert2.getBytes())));
241 
242         CertPath cp = certFac.generateCertPath(certs);
243 
244         // Get the encoded form of the CertPath we made
245         byte[] encoded = cp.getEncoded("PKCS7");
246         // check if it matches the encoded value
247         assertEquals(Base64.getMimeDecoder().decode(pkcs7path.getBytes()), encoded,
248             "PKCS#7 encoding doesn't match stored value");
249 
250         // Generate a CertPath from the encoded value and check if it equals
251         // the CertPath generated from the certificates
252         CertPath decodedCP = certFac.generateCertPath(new ByteArrayInputStream(encoded), "PKCS7");
253         assertEquals(decodedCP, cp, "CertPath decoded from PKCS#7 isn't equal to original");
254     }
255 }
256