|
| 1 | +/* |
| 2 | + * Copyright (c) 2020, 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 8239950 |
| 27 | + * @summary Update PKCS9 Attributes to PKCS#9 v2.0 Encodings |
| 28 | + * @library /test/lib |
| 29 | + * @modules java.base/sun.security.pkcs |
| 30 | + java.base/sun.security.util |
| 31 | + */ |
| 32 | + |
| 33 | +import java.io.IOException; |
| 34 | +import java.util.*; |
| 35 | +import sun.security.pkcs.PKCS9Attribute; |
| 36 | +import sun.security.util.DerValue; |
| 37 | +import jdk.test.lib.Utils; |
| 38 | + |
| 39 | +public class PKCS9AttrTypeTests { |
| 40 | + |
| 41 | + static final Map<String, String> TEST_INPUT_GOOD = |
| 42 | + new LinkedHashMap<String, String>() {{ |
| 43 | + |
| 44 | + // Challenge Password |
| 45 | + put("challengePasssword as TeletexString", |
| 46 | + "301e06092a864886f70d010907311114" + |
| 47 | + "0f5468697349734150617373776f7264"); |
| 48 | + put("challengePasssword as PrintableString", |
| 49 | + "301e06092a864886f70d010907311113" + |
| 50 | + "0f5468697349734150617373776f7264"); |
| 51 | + put("challengePasssword as UniversalString", |
| 52 | + "304b06092a864886f70d010907313e1c" + |
| 53 | + "3c000000540000006800000069000000" + |
| 54 | + "73000000490000007300000041000000" + |
| 55 | + "50000000610000007300000073000000" + |
| 56 | + "770000006f0000007200000064"); |
| 57 | + put("challengePasssword as UTF8String", |
| 58 | + "301e06092a864886f70d01090731110c" + |
| 59 | + "0f5468697349734150617373776f7264"); |
| 60 | + put("challengePasssword as BMPString", |
| 61 | + "302d06092a864886f70d01090731201e" + |
| 62 | + "1e005400680069007300490073004100" + |
| 63 | + "500061007300730077006f00720064"); |
| 64 | + |
| 65 | + // Unstructured Name |
| 66 | + put("unstructuredName as IA5String", |
| 67 | + "301b06092a864886f70d010902310e16" + |
| 68 | + "0c5468697349734d794e616d65"); |
| 69 | + put("unstructuredName as TeletexString", |
| 70 | + "301b06092a864886f70d010902310e14" + |
| 71 | + "0c5468697349734d794e616d65"); |
| 72 | + put("unstructuredName as PrintableString", |
| 73 | + "301b06092a864886f70d010902310e13" + |
| 74 | + "0c5468697349734d794e616d65"); |
| 75 | + put("unstructuredName as UniversalString", |
| 76 | + "303f06092a864886f70d01090231321c" + |
| 77 | + "30000000540000006800000069000000" + |
| 78 | + "7300000049000000730000004d000000" + |
| 79 | + "790000004e000000610000006d000000" + |
| 80 | + "65"); |
| 81 | + put("unstructuredName as UTF8String", |
| 82 | + "301b06092a864886f70d010902310e0c" + |
| 83 | + "0c5468697349734d794e616d65"); |
| 84 | + put("unstructuredName as BMPString", |
| 85 | + "302706092a864886f70d010902311a1e" + |
| 86 | + "18005400680069007300490073004d00" + |
| 87 | + "79004e0061006d0065"); |
| 88 | + put("unstructuredName as Multi-Valued", |
| 89 | + "302d06092a864886f70d010902312013" + |
| 90 | + "10546869734973416c736f4d794e616d" + |
| 91 | + "65160c5468697349734d794e616d65"); |
| 92 | + |
| 93 | + // Unstructured Address |
| 94 | + put("unstructuredAddress as TeletexString", |
| 95 | + "301e06092a864886f70d010908311114" + |
| 96 | + "0f5468697349734d7941646472657373"); |
| 97 | + put("unstructuredAddress as PrintableString", |
| 98 | + "301e06092a864886f70d010908311113" + |
| 99 | + "0f5468697349734d7941646472657373"); |
| 100 | + put("unstructuredAddress as UniversalString", |
| 101 | + "304b06092a864886f70d010908313e1c" + |
| 102 | + "3c000000540000006800000069000000" + |
| 103 | + "7300000049000000730000004d000000" + |
| 104 | + "79000000410000006400000064000000" + |
| 105 | + "72000000650000007300000073"); |
| 106 | + put("unstructuredAddress as UTF8String", |
| 107 | + "301e06092a864886f70d01090831110c" + |
| 108 | + "0f5468697349734d7941646472657373"); |
| 109 | + put("unstructuredAddress as BMPString", |
| 110 | + "302d06092a864886f70d01090831201e" + |
| 111 | + "1e005400680069007300490073004d00" + |
| 112 | + "790041006400640072006500730073"); |
| 113 | + put("unstructuredAddress as Multi-Valued", |
| 114 | + "303306092a864886f70d01090831260c" + |
| 115 | + "13546869734973416c736f4d79416464" + |
| 116 | + "72657373130f5468697349734d794164" + |
| 117 | + "6472657373"); |
| 118 | + |
| 119 | + // Signing Time |
| 120 | + put("signingTime as UTCTime", |
| 121 | + "301c06092a864886f70d010905310f17" + |
| 122 | + "0d3635303533313132303030305a"); |
| 123 | + put("signingTime as GeneralizedTime", |
| 124 | + "301e06092a864886f70d010905311118" + |
| 125 | + "0f32303530303533313132303030305a"); |
| 126 | + }}; |
| 127 | + |
| 128 | + static final Map<String, String> TEST_INPUT_BAD = |
| 129 | + new LinkedHashMap<String, String>() {{ |
| 130 | + |
| 131 | + // Challenge Password |
| 132 | + put("challengePasssword as IA5String", |
| 133 | + "301e06092a864886f70d010907311116" + |
| 134 | + "0f5468697349734150617373776f7264"); |
| 135 | + put("challengePassword as Multi-Valued", |
| 136 | + "303306092a864886f70d01090731260c" + |
| 137 | + "13546869734973416c736f4150617373" + |
| 138 | + "776f7264130f54686973497341506173" + |
| 139 | + "73776f7264"); |
| 140 | + |
| 141 | + // Unstructured Name |
| 142 | + put("unstructuredName as UTCTime", |
| 143 | + "301c06092a864886f70d010902310f17" + |
| 144 | + "0d3635303533313132303030305a"); |
| 145 | + |
| 146 | + // Unstructured Address |
| 147 | + put("unstructuredAddress as IA5String", |
| 148 | + "301e06092a864886f70d010908311116" + |
| 149 | + "0f5468697349734d7941646472657373"); |
| 150 | + |
| 151 | + // Signing Time |
| 152 | + put("signingTime as Multi-Valued", |
| 153 | + "302b06092a864886f70d010905311e17" + |
| 154 | + "0d3230303432383035303030305a170d" + |
| 155 | + "3635303533313132303030305a"); // DKN |
| 156 | + }}; |
| 157 | + |
| 158 | + public static void main(String[] args) throws Exception { |
| 159 | + int failedTests = 0; |
| 160 | + |
| 161 | + for (Map.Entry<String, String> entry : TEST_INPUT_GOOD.entrySet()) { |
| 162 | + try { |
| 163 | + System.out.print("Test - " + entry.getKey() + ": "); |
| 164 | + |
| 165 | + // Decode each Base64 test vector into DER and place into |
| 166 | + // a DerValue object to be consumed by PKCS9Attribute. |
| 167 | + PKCS9Attribute p9Attr = new PKCS9Attribute( |
| 168 | + new DerValue(Utils.toByteArray(entry.getValue()))); |
| 169 | + System.out.println("PASS"); |
| 170 | + System.out.println("---------------"); |
| 171 | + System.out.println(p9Attr); |
| 172 | + System.out.println("---------------"); |
| 173 | + } catch (IOException ioe) { |
| 174 | + failedTests++; |
| 175 | + System.out.println("FAIL:"); |
| 176 | + ioe.printStackTrace(System.out); |
| 177 | + System.out.println(); |
| 178 | + } |
| 179 | + } |
| 180 | + |
| 181 | + for (Map.Entry<String, String> entry : TEST_INPUT_BAD.entrySet()) { |
| 182 | + try { |
| 183 | + System.out.print("Test - " + entry.getKey() + ": "); |
| 184 | + |
| 185 | + // Decode each Base64 test vector into DER and place into |
| 186 | + // a DerValue object to be consumed by PKCS9Attribute. |
| 187 | + PKCS9Attribute p9Attr = new PKCS9Attribute( |
| 188 | + new DerValue( |
| 189 | + Base64.getMimeDecoder().decode(entry.getValue()))); |
| 190 | + failedTests++; |
| 191 | + System.out.println("FAIL: Expected IOException did not occur"); |
| 192 | + } catch (IOException ioe) { |
| 193 | + System.out.println("PASS\n" + ioe); |
| 194 | + } |
| 195 | + |
| 196 | + } |
| 197 | + |
| 198 | + if (failedTests > 0) { |
| 199 | + throw new RuntimeException( |
| 200 | + "One or more test cases failed, see output"); |
| 201 | + } |
| 202 | + } |
| 203 | +} |
0 commit comments