1/*
2 *  Licensed to the Apache Software Foundation (ASF) under one or more
3 *  contributor license agreements.  See the NOTICE file distributed with
4 *  this work for additional information regarding copyright ownership.
5 *  The ASF licenses this file to You under the Apache License, Version 2.0
6 *  (the "License"); you may not use this file except in compliance with
7 *  the License.  You may obtain a copy of the License at
8 *
9 *     http://www.apache.org/licenses/LICENSE-2.0
10 *
11 *  Unless required by applicable law or agreed to in writing, software
12 *  distributed under the License is distributed on an "AS IS" BASIS,
13 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 *  See the License for the specific language governing permissions and
15 *  limitations under the License.
16 */
17
18/**
19* @author Vladimir N. Molotkov
20*/
21
22package org.apache.harmony.crypto.tests.javax.crypto;
23
24import java.io.IOException;
25import java.security.AlgorithmParameters;
26import java.security.InvalidAlgorithmParameterException;
27import java.security.InvalidKeyException;
28import java.security.Key;
29import java.security.KeyPair;
30import java.security.KeyPairGenerator;
31import java.security.NoSuchAlgorithmException;
32import java.security.Provider;
33import java.security.Security;
34import java.security.spec.InvalidKeySpecException;
35import java.security.spec.InvalidParameterSpecException;
36import java.security.spec.PKCS8EncodedKeySpec;
37import java.util.Arrays;
38
39import javax.crypto.BadPaddingException;
40import javax.crypto.Cipher;
41import javax.crypto.EncryptedPrivateKeyInfo;
42import javax.crypto.IllegalBlockSizeException;
43import javax.crypto.KeyGenerator;
44import javax.crypto.NoSuchPaddingException;
45import javax.crypto.SecretKeyFactory;
46import javax.crypto.spec.PBEKeySpec;
47import javax.crypto.spec.PBEParameterSpec;
48
49import org.apache.harmony.crypto.tests.support.EncryptedPrivateKeyInfoData;
50
51import junit.framework.TestCase;
52
53/**
54 * Test for EncryptedPrivateKeyInfo class.
55 *
56 * All binary data for this test were generated using BEA JRockit j2sdk1.4.2_04
57 * (http://www.bea.com) with security providers list extended by Bouncy Castle's
58 * one (http://www.bouncycastle.org)
59 */
60public class EncryptedPrivateKeyInfoTest extends TestCase {
61
62    private static final Provider[] provider = Security.getProviders();
63
64    /**
65     * Algorithm names/transformations used in roundtrip tests of
66     * getKeySpec(...) methods
67     */
68    private static final String[][] algName = {
69    // AES
70            { "AES", null },
71            //            {"AES", "AES/ECB/PKCS5Padding"},
72            //            {"AES", "AES/CBC/PKCS5Padding"},
73            //            {"AES", "AES/OFB/PKCS5Padding"},
74            //            {"AES", "AES/CFB/PKCS5Padding"},
75            //            {"2.16.840.1.101.3.4.1.1", null},
76            //            {"2.16.840.1.101.3.4.1.2", null},
77            //            {"2.16.840.1.101.3.4.1.3", null},
78            //            {"2.16.840.1.101.3.4.1.4", null},
79            //            {"2.16.840.1.101.3.4.1.5", null},
80            //            {"2.16.840.1.101.3.4.1.21", null},
81            //            {"2.16.840.1.101.3.4.1.22", null},
82            //            {"2.16.840.1.101.3.4.1.23", null},
83            //            {"2.16.840.1.101.3.4.1.24", null},
84            //            {"2.16.840.1.101.3.4.1.25", null},
85            //            {"2.16.840.1.101.3.4.1.41", null},
86            //            {"2.16.840.1.101.3.4.1.42", null},
87            //            {"2.16.840.1.101.3.4.1.43", null},
88            //            {"2.16.840.1.101.3.4.1.44", null},
89            //            {"2.16.840.1.101.3.4.1.45", null},
90
91            // Blowfish
92            // NO OIDs for Blowfish defined (?)
93            { "Blowfish", null },
94            //            {"Blowfish","Blowfish/CBC/PKCS5Padding"},
95            //            {"Blowfish","Blowfish/CFB/PKCS5Padding"},
96            //            {"Blowfish","Blowfish/OFB/PKCS5Padding"},
97            //            {"Blowfish","Blowfish/PCBC/PKCS5Padding"},
98
99            // DES: OIW OIDs only
100            // {iso(1) identified-organization(3) oiw(14) secsig(3)
101            // algorithms(2) desECB(6)}
102            // 1.3.14.3.2.6
103            // 1.3.14.3.2.7
104            // 1.3.14.3.2.8
105            // 1.3.14.3.2.9
106            { "DES", null },
107            //            {"DES", "DES/CBC/PKCS5Padding"},
108            //            {"DES","DES/CFB/PKCS5Padding"},
109            //            {"DES","DES/OFB/PKCS5Padding"},
110            //            {"DES","DES/PCBC/PKCS5Padding"},
111
112            // DESede (=TripleDes)
113            //{iso(1) identified-organization(3) oiw(14) secsig(3)
114            // algorithms(2) desEDE(17)}
115            // 1.3.14.3.2.17
116            //            {"DESede",null},
117            //            {"DESede","DESede/CBC/PKCS5Padding"},
118            //            {"DESede","DESede/CFB/PKCS5Padding"},
119            //            {"DESede","DESede/OFB/PKCS5Padding"},
120            //            {"DESede","DESede/PCBC/PKCS5Padding"},
121            { "TripleDES", null },
122            //            {"TripleDES","TripleDES/CBC/PKCS5Padding"},
123            //            {"TripleDES","TripleDES/CFB/PKCS5Padding"},
124            //            {"TripleDES","TripleDES/OFB/PKCS5Padding"},
125            //            {"TripleDES","TripleDES/PCBC/PKCS5Padding"},
126
127            // PBEWith<digest>And<encryption>
128            { "PBEWithMD5AndTripleDES", null },
129            // {iso(1) member-body(2) us(840) rsadsi(113549) pkcs(1) pkcs-5(5)
130            // pbeWithMD5AndDES-CBC(3)}
131            { "PBEWithMD5AndDES", "PBEWithMD5AndDES/CBC/PKCS5Padding" },
132            { "PBEWithMD5AndDES", null }, { "PBEWithHmacSHA1AndDESede", null },
133            // more oids:
134            // {iso(1) member-body(2) us(840) nortelnetworks(113533) entrust(7)
135            // algorithms(66) pbeWithMD5AndCAST5-CBC(12)}
136            //
137            // also named pbeWithSHAAnd128BitRC4, pbeWithSHA1And128BitRC4:
138            // {iso(1) member-body(2) us(840) rsadsi(113549) pkcs(1) pkcs-12(12)
139            // pkcs-12-PbeIds(1) pkcs-12-OfflineTransportMode(1)}
140            //
141            // {iso(1) member-body(2) us(840) rsadsi(113549) pkcs(1) pkcs-12(12)
142            // pkcs-12-PbeIds(1)} +
143            // pbeWithSHAAnd40BitRC4(2) pbeWithSHAAnd3-KeyTripleDES-CBC(3)
144            // pbeWithSHAAnd2-KeyTripleDES-CBC(4) pbeWithSHAAnd128BitRC2-CBC(5)
145            // pbeWithSHAAnd40BitRC2-CBC(6)
146
147            // DiffieHellman
148            { "DiffieHellman", null }, // 1.2.840.10046.2.1
149            //            {"DH",null}, // 1.2.840.10046.2.1
150            //            {"1.2.840.113549.1.3.1", null},
151
152            { "DSA", null }, // 1.2.840.10040.4.1
153
154            { "RC2", null },
155
156            { "RC4", null },
157
158            { "RC5", null },
159
160            //            {"1.2.840.113549.1.12.1.1",null},
161            //            {"1.2.840.113549.1.12.1.2",null},
162            { "1.2.840.113549.1.12.1.3", null },
163            { "PBEWithSHA1AndDESede", null },
164    //            {"1.2.840.113549.1.12.1.4",null},
165    //            {"1.2.840.113549.1.12.1.5",null},
166    //            {"1.2.840.113549.1.12.1.6",null},
167    //            {"ELGAMAL/PKCS1", "ELGAMAL/ECB/PKCS1PADDING"},
168    //            {"ELGAMAL/PKCS1","ELGAMAL/NONE/PKCS1PADDING"},
169    //            {"PBEWITHSHAAND3-KEYTRIPLEDES-CBC", null},
170    //            {"PBEWITHSHA1ANDDESEDE", null},
171    //            {"PBEWithSHAAnd3KeyTripleDES",null},
172    //            {"PBEWITHSHAAND3-KEYTRIPLEDES-CBC",null},
173    //
174    //            {"RC5-32",null},
175    //
176    //            {"RSA/1", "RSA/1/PKCS1PADDING"},
177    //            {"RSA/2", "RSA/2/PKCS1PADDING"},
178    //            {"RSA/ISO9796-1", "RSA/ECB/ISO9796-1PADDING"},
179    //            {"RSA", "RSA/ECB/NOPADDING"},
180    //            {"RSA/OAEP", "RSA/ECB/OAEPPADDING"},
181    //            {"RSA/PKCS1", "RSA/ECB/PKCS1PADDING"},
182    //            {"RSA/ISO9796-1", "RSA/NONE/ISO9796-1PADDING"},
183    //            {"RSA", "RSA/NONE/NOPADDING"},
184    //            {"RSA/OAEP", "RSA/NONE/OAEPPADDING"},
185    //            {"RSA/PKCS1", "RSA/NONE/PKCS1PADDING"},
186    //            {"RSA",null}, // 1.2.840.113549.1.1.1
187    //            {"1.2.840.113549.1.1.1", null},
188    };
189
190    /**
191     * Test #1 for <code>EncryptedPrivateKeyInfo(byte[])</code> constructor
192     * <br>
193     * Assertion: creates <code>EncryptedPrivateKeyInfo</code> instance <br>
194     * Test preconditions: valid parameters passed <br>
195     * Expected: must pass without any exceptions
196     *
197     * @throws IOException
198     * @throws NoSuchAlgorithmException
199     */
200    public final void testEncryptedPrivateKeyInfobyteArray1() throws Exception {
201        new EncryptedPrivateKeyInfo(EncryptedPrivateKeyInfoData
202                .getValidEncryptedPrivateKeyInfoEncoding("DH"));
203    }
204
205    /**
206     * Test #2 for <code>EncryptedPrivateKeyInfo(byte[])</code> constructor
207     * <br>
208     * Assertion: <code>NullPointerException</code> if encoding is
209     * <code>null</code><br>
210     * Test preconditions: <code>null</code> passed as a parameter <br>
211     * Expected: <code>NullPointerException</code>
212     *
213     * @throws IOException
214     */
215    public final void testEncryptedPrivateKeyInfobyteArray2()
216            throws IOException {
217        try {
218            new EncryptedPrivateKeyInfo(null);
219            fail(getName() + ": NullPointerException has not been thrown");
220        } catch (NullPointerException ok) {
221        }
222    }
223
224    /**
225     * Test #3 for <code>EncryptedPrivateKeyInfo(byte[])</code> constructor
226     * <br>
227     * Assertion: <code>IOException</code> if encoding is wrong <br>
228     * Test preconditions: wrong encoding passed as a parameter <br>
229     * Expected: <code>IOException</code>
230     */
231    public final void testEncryptedPrivateKeyInfobyteArray3() {
232        try {
233            new EncryptedPrivateKeyInfo(new byte[0]);
234            fail(getName() + ": IOException has not been thrown");
235        } catch (IOException ok) {
236        }
237    }
238
239    /**
240     * Test #4 for <code>EncryptedPrivateKeyInfo(byte[])</code> constructor
241     * <br>
242     * Assertion: <code>IOException</code> if encoding is wrong <br>
243     * Test preconditions: wrong encoding passed as a parameter <br>
244     * Expected: <code>IOException</code>
245     */
246    public final void testEncryptedPrivateKeyInfobyteArray4() {
247        try {
248            new EncryptedPrivateKeyInfo(new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9,
249                    10 });
250            fail(getName() + ": IOException has not been thrown");
251        } catch (IOException ok) {
252        }
253    }
254
255    /**
256     * Test #5 for <code>EncryptedPrivateKeyInfo(byte[])</code> constructor
257     * <br>
258     * Assertion: <code>IOException</code> if encoding is wrong <br>
259     * Test preconditions: wrong encoding passed as a parameter <br>
260     * Expected: <code>IOException</code>
261     */
262    public final void testEncryptedPrivateKeyInfobyteArray5() throws Exception {
263        byte[] enc = null;
264        try {
265            // 1: get valid encoding
266            enc = EncryptedPrivateKeyInfoData
267                    .getValidEncryptedPrivateKeyInfoEncoding("DSA");
268
269            // ... and corrupt it (set wrong alg OID length)
270            enc[9] = (byte) 6;
271
272            new EncryptedPrivateKeyInfo(enc);
273            fail(getName() + "(1): IOException has not been thrown");
274        } catch (IOException ok) {
275        }
276
277        try {
278            // 2: get valid encoding
279            enc = EncryptedPrivateKeyInfoData
280                    .getValidEncryptedPrivateKeyInfoEncoding("DSA");
281            // ... and corrupt it (set wrong encrypted data tag)
282            enc[307] = (byte) 6;
283            new EncryptedPrivateKeyInfo(enc);
284            fail(getName() + "(2): IOException has not been thrown");
285        } catch (IOException ok) {
286        }
287
288        try {
289            // 3: get valid encoding
290            enc = EncryptedPrivateKeyInfoData
291                    .getValidEncryptedPrivateKeyInfoEncoding("DSA");
292            // ... and corrupt it (set wrong encrypted data length)
293            enc[310] = (byte) 1;
294            new EncryptedPrivateKeyInfo(enc);
295            fail(getName() + "(3): IOException has not been thrown");
296        } catch (IOException ok) {
297        }
298
299        try {
300            // 4: get valid encoding
301            enc = EncryptedPrivateKeyInfoData
302                    .getValidEncryptedPrivateKeyInfoEncoding("DSA");
303            // ... and corrupt it (set wrong tag for alg params sequence)
304            enc[17] = (byte) 0x29;
305            EncryptedPrivateKeyInfo epki = new EncryptedPrivateKeyInfo(enc);
306
307            if (epki.getAlgParameters() == null) {
308                // This kind of encoding corruption can
309                // be only determined while AlgorithmParameters
310                // initialization BUT No AlgorithmParameters instance
311                // available for algName0[i][0].
312                // So just skip this sub test
313            } else {
314                fail(getName() + "(4): IOException has not been thrown");
315            }
316
317        } catch (IOException ok) {
318        }
319
320        try {
321            // 5: get valid encoding
322            enc = EncryptedPrivateKeyInfoData
323                    .getValidEncryptedPrivateKeyInfoEncoding("DSA");
324            // ... and corrupt it (set wrong length for alg params sequence)
325            enc[20] = (byte) 0x1d;
326            new EncryptedPrivateKeyInfo(enc);
327            fail(getName() + "(5): IOException has not been thrown");
328        } catch (IOException ok) {
329        }
330
331        try {
332            // 6: get valid encoding
333            enc = EncryptedPrivateKeyInfoData
334                    .getValidEncryptedPrivateKeyInfoEncoding("DSA");
335            // ... and corrupt it (set wrong length for alg params sequence)
336            enc[20] = (byte) 0x1f;
337            new EncryptedPrivateKeyInfo(enc);
338            fail(getName() + "(6): IOException has not been thrown");
339        } catch (IOException ok) {
340        }
341    }
342
343    /**
344     * Test #6 for <code>EncryptedPrivateKeyInfo(byte[])</code> constructor
345     * <br>
346     * Assertion: byte array is copied to prevent subsequent modification <br>
347     * Test preconditions: valid array passed then modified <br>
348     * Expected: getEncoded(), invoked after above modification, must return
349     * array as it was before the modification
350     *
351     * @throws IOException
352     */
353    public final void testEncryptedPrivateKeyInfobyteArray6() throws Exception {
354        byte[] encoded = EncryptedPrivateKeyInfoData
355                .getValidEncryptedPrivateKeyInfoEncoding("DSA");
356        byte[] encodedCopy = encoded.clone();
357        // pass valid array
358        EncryptedPrivateKeyInfo epki = new EncryptedPrivateKeyInfo(encodedCopy);
359        // modify array passed
360        encodedCopy[9] = (byte) 6;
361        // check that internal state has not been affected
362        assertTrue(Arrays.equals(encoded, epki.getEncoded()));
363    }
364
365    /**
366     * Test #1 for <code>EncryptedPrivateKeyInfo(String, byte[])</code>
367     * constructor <br>
368     * Assertion: creates <code>EncryptedPrivateKeyInfo</code> instance <br>
369     * Test preconditions: valid parameters passed <br>
370     * Expected: must pass without any exceptions
371     */
372    public final void testEncryptedPrivateKeyInfoStringbyteArray1() {
373        boolean performed = false;
374
375        for (int i = 0; i < EncryptedPrivateKeyInfoData.algName0.length; i++) {
376            try {
377                new EncryptedPrivateKeyInfo(
378                        EncryptedPrivateKeyInfoData.algName0[i][0],
379                        EncryptedPrivateKeyInfoData.encryptedData);
380                performed = true;
381            } catch (NoSuchAlgorithmException allowed) {
382            }
383        }
384
385        assertTrue("Test not performed", performed);
386    }
387
388    /**
389     * Test #2 for <code>EncryptedPrivateKeyInfo(String, byte[])</code>
390     * constructor <br>
391     * Assertion: <code>NoSuchAlgorithmException</code>- if the specified
392     * algorithm is not supported <br>
393     * Test preconditions: pass nonexistent algorithm name <br>
394     * Expected: <code>NoSuchAlgorithmException</code>
395     */
396    public final void testEncryptedPrivateKeyInfoStringbyteArray2() {
397        try {
398            new EncryptedPrivateKeyInfo("bla-bla",
399                    EncryptedPrivateKeyInfoData.encryptedData);
400            fail(getName() + ": NoSuchAlgorithmException has not been thrown");
401        } catch (NoSuchAlgorithmException ok) {
402        }
403
404        try {
405            new EncryptedPrivateKeyInfo("",
406                    EncryptedPrivateKeyInfoData.encryptedData);
407            fail(getName() + ": NoSuchAlgorithmException has not been thrown");
408        } catch (NoSuchAlgorithmException ok) {
409        }
410    }
411
412    /**
413     * Test #3 for <code>EncryptedPrivateKeyInfo(String, byte[])</code>
414     * constructor <br>
415     * Assertion: <code>NullPointerException</code>- if the specified
416     * algorithm or encrypted data is <code>null</code><br>
417     * Test preconditions: pass <code>null</code> as algorithm name then as
418     * encrypted data <br>
419     * Expected: <code>NullPointerException</code> in both cases
420     *
421     * @throws NoSuchAlgorithmException
422     */
423    public final void testEncryptedPrivateKeyInfoStringbyteArray3()
424            throws NoSuchAlgorithmException {
425        // pass null as name
426        try {
427            new EncryptedPrivateKeyInfo((String) null,
428                    EncryptedPrivateKeyInfoData.encryptedData);
429            fail(getName() + ": NullPointerException has not been thrown");
430        } catch (NullPointerException ok) {
431        }
432
433        // pass null as encrypted data
434        try {
435            new EncryptedPrivateKeyInfo("DSA", null);
436            fail(getName() + ": NullPointerException has not been thrown");
437        } catch (NullPointerException ok) {
438        }
439    }
440
441    /**
442     * Test #4 for <code>EncryptedPrivateKeyInfo(String, byte[])</code>
443     * constructor <br>
444     * Assertion: <code>IllegalArgumentException</code>- if encrypted data is
445     * empty, i.e. 0-length <br>
446     * Test preconditions: pass empty encrypted data <br>
447     * Expected: <code>IllegalArgumentException</code>
448     */
449    public final void testEncryptedPrivateKeyInfoStringbyteArray4()
450            throws Exception {
451        try {
452            new EncryptedPrivateKeyInfo("DSA", new byte[] {});
453            fail(getName() + ": IllegalArgumentException has not been thrown");
454        } catch (IllegalArgumentException ok) {
455        }
456    }
457
458    /**
459     * Test #5 for <code>EncryptedPrivateKeyInfo(String, byte[])</code>
460     * constructor <br>
461     * Assertion: byte array is copied to prevent subsequent modification <br>
462     * Test preconditions: valid array passed then modified <br>
463     * Expected: getEncryptedData(), invoked after above modification, must
464     * return array as it was before the modification
465     *
466     * @throws IOException
467     */
468    public final void testEncryptedPrivateKeyInfoStringbyteArray5()
469            throws Exception {
470        byte[] encryptedDataCopy = EncryptedPrivateKeyInfoData.encryptedData
471                .clone();
472        // pass valid array
473        EncryptedPrivateKeyInfo epki = new EncryptedPrivateKeyInfo("DSA",
474                encryptedDataCopy);
475        // modify array passed
476        encryptedDataCopy[0] = (byte) 6;
477        // check that internal state has not been affected
478        assertTrue(Arrays.equals(EncryptedPrivateKeyInfoData.encryptedData,
479                epki.getEncryptedData()));
480    }
481
482    /**
483     * @tests javax/crypto/EncryptedPrivateKeyInfo(String, byte[])
484     * Checks exception order
485     */
486    public final void testEncryptedPrivateKeyInfoStringbyteArray6() {
487        //Regression for HARMONY-768
488        try {
489            new EncryptedPrivateKeyInfo("0", new byte[] {});
490            fail("NoSuchAlgorithmException expected");
491        } catch (NoSuchAlgorithmException e) {
492            //expected
493        }
494    }
495
496    /**
497     * Test #1 for
498     * <code>EncryptedPrivateKeyInfo(java.security.AlgorithmParameters, byte[])
499     * </code>
500     * constructor <br>
501     * Assertion: creates <code>EncryptedPrivateKeyInfo</code> instance <br>
502     * Test preconditions: valid parameters passed <br>
503     * Expected: must pass without any exceptions
504     *
505     * @throws IOException
506     */
507    public final void testEncryptedPrivateKeyInfoAlgorithmParametersbyteArray1()
508            throws IOException {
509
510        boolean performed = false;
511        for (int i = 0; i < EncryptedPrivateKeyInfoData.algName0.length; i++) {
512            try {
513                AlgorithmParameters ap = AlgorithmParameters
514                        .getInstance(EncryptedPrivateKeyInfoData.algName0[i][0]);
515                // use pregenerated AlgorithmParameters encodings
516                ap.init(EncryptedPrivateKeyInfoData.getParametersEncoding(
517                        EncryptedPrivateKeyInfoData.algName0[i][0]));
518
519                new EncryptedPrivateKeyInfo(ap,
520                        EncryptedPrivateKeyInfoData.encryptedData);
521
522                performed = true;
523            } catch (NoSuchAlgorithmException allowedFailure) {
524            }
525        }
526        assertTrue("Test not performed", performed);
527    }
528
529    /**
530     * Test #2 for
531     * <code>EncryptedPrivateKeyInfo(java.security.AlgorithmParameters, byte[])
532     * </code>
533     * constructor <br>
534     * Assertion: <code>NullPointerException</code>- if the specified
535     * algorithm parameters or encrypted data is <code>null</code><br>
536     * Test preconditions: pass <code>null</code> as algorithm parameters then
537     * as encrypted data <br>
538     * Expected: <code>NullPointerException</code> in both cases
539     *
540     * @throws NoSuchAlgorithmException
541     * @throws IOException
542     */
543    public final void testEncryptedPrivateKeyInfoAlgorithmParametersbyteArray2()
544            throws NoSuchAlgorithmException, IOException {
545        // 1: pass null as AlgorithmParameters
546        try {
547            new EncryptedPrivateKeyInfo((AlgorithmParameters) null,
548                    EncryptedPrivateKeyInfoData.encryptedData);
549            fail(getName() + ": NullPointerException has not been thrown");
550        } catch (NullPointerException ok) {
551        }
552
553        // 2: pass null as encrypted data
554        try {
555            AlgorithmParameters ap = AlgorithmParameters.getInstance("DSA");
556            // use pregenerated AlgorithmParameters encodings
557            ap.init(EncryptedPrivateKeyInfoData.getParametersEncoding("DSA"));
558            new EncryptedPrivateKeyInfo(ap, null);
559            fail(getName() + ": NullPointerException has not been thrown");
560        } catch (NullPointerException ok) {
561        }
562    }
563
564    /**
565     * Test #3 for
566     * <code>EncryptedPrivateKeyInfo(java.security.AlgorithmParameters, byte[])
567     * </code>
568     * constructor <br>
569     * Assertion: <code>IllegalArgumentException</code>- if encrypted data is
570     * empty, i.e. 0-length <br>
571     * Test preconditions: pass empty encrypted data <br>
572     * Expected: <code>IllegalArgumentException</code>
573     *
574     * @throws NoSuchAlgorithmException
575     * @throws IOException
576     */
577    public final void testEncryptedPrivateKeyInfoAlgorithmParametersbyteArray3()
578            throws Exception {
579        try {
580            AlgorithmParameters ap = AlgorithmParameters.getInstance("DSA");
581            // use pregenerated AlgorithmParameters encodings
582            ap.init(EncryptedPrivateKeyInfoData.getParametersEncoding("DSA"));
583
584            new EncryptedPrivateKeyInfo(ap, new byte[] {});
585            fail(getName() + ": IllegalArgumentException has not been thrown");
586
587        } catch (IllegalArgumentException ok) {
588        }
589    }
590
591    /**
592     * Test #4 for
593     * <code>EncryptedPrivateKeyInfo(java.security.AlgorithmParameters, byte[])
594     * </code>
595     * constructor <br>
596     * Assertion: byte array is copied to prevent subsequent modification <br>
597     * Test preconditions: valid array passed then modified <br>
598     * Expected: getEncryptedData(), invoked after above modification, must
599     * return array as it was before the modification
600     *
601     * @throws IOException
602     */
603    public final void testEncryptedPrivateKeyInfoAlgorithmParametersbyteArray4()
604            throws Exception {
605        AlgorithmParameters ap = AlgorithmParameters.getInstance("DSA");
606        // use pregenerated AlgorithmParameters encodings
607        ap.init(EncryptedPrivateKeyInfoData.getParametersEncoding("DSA"));
608
609        byte[] encryptedDataCopy = EncryptedPrivateKeyInfoData.encryptedData.clone();
610        // pass valid array
611        EncryptedPrivateKeyInfo epki = new EncryptedPrivateKeyInfo(ap,
612                encryptedDataCopy);
613
614        // modify array passed
615        encryptedDataCopy[0] = (byte) 6;
616
617        // check that internal state has not been affected
618        assertTrue(Arrays.equals(EncryptedPrivateKeyInfoData.encryptedData,
619                epki.getEncryptedData()));
620    }
621
622    /**
623     * Test #1 for <code>getAlgParameters()</code> method <br>
624     * Assertion: returns the algorithm parameters <br>
625     * Test preconditions: test object created using ctor which takes encoded
626     * form as the only parameter; encoded form passed contains algorithm
627     * parameters encoding <br>
628     * Expected: corresponding algorithm parameters must be returned
629     *
630     * @throws IOException
631     */
632    public final void testGetAlgParameters01() throws IOException {
633        boolean performed = false;
634        for (int i = 0; i < EncryptedPrivateKeyInfoData.algName0.length; i++) {
635            try {
636                EncryptedPrivateKeyInfo epki = new EncryptedPrivateKeyInfo(
637                        EncryptedPrivateKeyInfoData
638                                .getValidEncryptedPrivateKeyInfoEncoding(
639                                        EncryptedPrivateKeyInfoData.algName0[i][0]));
640
641                AlgorithmParameters apar = epki.getAlgParameters();
642                if (apar == null) {
643                    continue;
644                }
645
646                // check that method under test returns
647                // parameters with the same encoded form
648                assertTrue(Arrays
649                        .equals(
650                                EncryptedPrivateKeyInfoData
651                                        .getParametersEncoding(EncryptedPrivateKeyInfoData.algName0[i][0]),
652                                apar.getEncoded()));
653                performed = true;
654            } catch (NoSuchAlgorithmException allowedFailure) {
655            }
656        }
657        assertTrue("Test not performed", performed);
658    }
659
660    public final void testGetAlgParameters01_01() throws Exception {
661        byte[] validEncodingWithUnknownAlgOID = EncryptedPrivateKeyInfoData
662                .getValidEncryptedPrivateKeyInfoEncoding("DH");
663        // correct oid value
664        validEncodingWithUnknownAlgOID[18] = 0;
665        EncryptedPrivateKeyInfo epki = new EncryptedPrivateKeyInfo(
666                validEncodingWithUnknownAlgOID);
667
668        assertNull(epki.getAlgParameters());
669    }
670
671    /**
672     * Test #2 for <code>getAlgParameters()</code> method <br>
673     * Assertion: returns the algorithm parameters <br>
674     * Test preconditions: test object created using ctor which takes encoded
675     * form as the only parameter; encoded form passed does not contain
676     * algorithm parameters encoding <br>
677     * Expected: <code>null</code> must be returned
678     *
679     * @throws IOException
680     */
681    public final void testGetAlgParameters02() throws IOException {
682        boolean performed = false;
683        for (int i = 0; i < EncryptedPrivateKeyInfoData.algName0.length; i++) {
684            try {
685                EncryptedPrivateKeyInfo epki = new EncryptedPrivateKeyInfo(
686                        EncryptedPrivateKeyInfoData
687                                .getValidEncryptedPrivateKeyInfoEncoding(
688                                        EncryptedPrivateKeyInfoData.algName0[i][0],
689                                        false));
690
691                // check that method under test returns null
692                assertNull(epki.getAlgParameters());
693
694                performed = true;
695            } catch (NoSuchAlgorithmException allowedFailure) {
696            }
697        }
698        assertTrue("Test not performed", performed);
699    }
700
701    /**
702     * Test #3 for <code>getAlgParameters()</code> method <br>
703     * Assertion: returns the algorithm parameters <br>
704     * Test #6 for <code>EncryptedPrivateKeyInfo(String, byte[])</code>
705     * constructor <br>
706     * Assertion: ...This constructor will use null as the value of the
707     * algorithm parameters. <br>
708     * Test preconditions: test object created using ctor which takes algorithm
709     * name and encrypted data as a parameters <br>
710     * Expected: <code>null</code> must be returned
711     *
712     * @throws IOException
713     */
714    public final void testGetAlgParameters03() throws IOException {
715        boolean performed = false;
716        for (int i = 0; i < EncryptedPrivateKeyInfoData.algName0.length; i++) {
717            try {
718                EncryptedPrivateKeyInfo epki = new EncryptedPrivateKeyInfo(
719                        EncryptedPrivateKeyInfoData.algName0[i][0],
720                        EncryptedPrivateKeyInfoData.encryptedData);
721
722                // check that method under test returns null
723                // for object constructed in such a way
724                assertNull(epki.getAlgParameters());
725
726                performed = true;
727            } catch (NoSuchAlgorithmException allowedFailure) {
728            }
729        }
730        assertTrue("Test not performed", performed);
731    }
732
733    /**
734     * Test #4 for <code>getAlgParameters()</code> method <br>
735     * Assertion: returns the algorithm parameters <br>
736     * Test preconditions: test object created using ctor which takes
737     * AlgorithmParameters and encrypted data as a parameters; <br>
738     * Expected: the same algorithm parameters as ones passed to the ctor must be
739     * returned
740     *
741     * @throws IOException
742     */
743    public final void testGetAlgParameters04() throws IOException {
744        boolean performed = false;
745        for (int i = 0; i < EncryptedPrivateKeyInfoData.algName0.length; i++) {
746            try {
747                AlgorithmParameters ap = AlgorithmParameters
748                        .getInstance(EncryptedPrivateKeyInfoData.algName0[i][0]);
749                // use pregenerated AlgorithmParameters encodings
750                ap
751                        .init(EncryptedPrivateKeyInfoData
752                                .getParametersEncoding(
753                                        EncryptedPrivateKeyInfoData.algName0[i][0]));
754
755                EncryptedPrivateKeyInfo epki = new EncryptedPrivateKeyInfo(ap,
756                        EncryptedPrivateKeyInfoData.encryptedData);
757
758                // check that method under test returns
759                // the same parameters instance
760                assertSame(ap, epki.getAlgParameters());
761
762                performed = true;
763            } catch (NoSuchAlgorithmException allowedFailure) {
764            }
765        }
766        assertTrue("Test not performed", performed);
767    }
768
769    /**
770     * Test #1 for <code>getEncryptedData()</code> method <br>
771     * Assertion: returns the encrypted data <br>
772     * Test preconditions: test object created using ctor which takes encoded
773     * form as the only parameter; encoded form passed contains encrypted data
774     * <br>
775     * Expected: the equivalent encrypted data must be returned
776     *
777     * @throws IOException
778     */
779    public final void testGetEncryptedData01() throws IOException {
780        boolean performed = false;
781        for (int i = 0; i < EncryptedPrivateKeyInfoData.algName0.length; i++) {
782            try {
783                EncryptedPrivateKeyInfo epki = new EncryptedPrivateKeyInfo(
784                        EncryptedPrivateKeyInfoData
785                                .getValidEncryptedPrivateKeyInfoEncoding(
786                                        EncryptedPrivateKeyInfoData.algName0[i][0]));
787
788                // check that method under test returns
789                // valid encrypted data
790                assertTrue(Arrays.equals(
791                        EncryptedPrivateKeyInfoData.encryptedData, epki
792                                .getEncryptedData()));
793
794                performed = true;
795            } catch (NoSuchAlgorithmException allowedFailure) {
796            }
797        }
798        assertTrue("Test not performed", performed);
799    }
800
801    /**
802     * Test #2 for <code>getEncryptedData()</code> method <br>
803     * Assertion: returns the encrypted data <br>
804     * Test preconditions: test object created using ctor which takes algorithm
805     * name and encrypted data as a parameters <br>
806     * Expected: the equivalent encrypted data must be returned
807     */
808    public final void testGetEncryptedData02() {
809        boolean performed = false;
810        for (int i = 0; i < EncryptedPrivateKeyInfoData.algName0.length; i++) {
811            try {
812                EncryptedPrivateKeyInfo epki = new EncryptedPrivateKeyInfo(
813                        EncryptedPrivateKeyInfoData.algName0[i][0],
814                        EncryptedPrivateKeyInfoData.encryptedData);
815
816                // check that method under test returns
817                // valid encrypted data
818                assertTrue(Arrays.equals(
819                        EncryptedPrivateKeyInfoData.encryptedData, epki
820                                .getEncryptedData()));
821
822                performed = true;
823            } catch (NoSuchAlgorithmException allowedFailure) {
824            }
825        }
826        assertTrue("Test not performed", performed);
827    }
828
829    /**
830     * Test #3 for <code>getEncryptedData()</code> method <br>
831     * Assertion: returns the encrypted data <br>
832     * Test preconditions: test object created using ctor which takes algorithm
833     * parameters and encrypted data as a parameters <br>
834     * Expected: the equivalent encrypted data must be returned
835     *
836     * @throws IOException
837     */
838    public final void testGetEncryptedData03() throws IOException {
839        boolean performed = false;
840        for (int i = 0; i < EncryptedPrivateKeyInfoData.algName0.length; i++) {
841            try {
842                AlgorithmParameters ap = AlgorithmParameters
843                        .getInstance(EncryptedPrivateKeyInfoData.algName0[i][0]);
844                // use pregenerated AlgorithmParameters encodings
845                ap.init(EncryptedPrivateKeyInfoData.getParametersEncoding(
846                        EncryptedPrivateKeyInfoData.algName0[i][0]));
847
848                EncryptedPrivateKeyInfo epki = new EncryptedPrivateKeyInfo(ap,
849                        EncryptedPrivateKeyInfoData.encryptedData);
850
851                // check that method under test returns
852                // valid encrypted data
853                assertTrue(Arrays.equals(
854                        EncryptedPrivateKeyInfoData.encryptedData, epki
855                                .getEncryptedData()));
856
857                performed = true;
858            } catch (NoSuchAlgorithmException allowedFailure) {
859            }
860        }
861        assertTrue("Test not performed", performed);
862    }
863
864    /**
865     * Test #4 for <code>getEncryptedData()</code> method <br>
866     * Assertion: returns a new array each time this method is called <br>
867     * Test preconditions: test object created using ctor which takes algorithm
868     * name and encrypted data as a parameters <br>
869     * Expected: refs to encrypted data byte array passed to the ctor and
870     * returned by the method under test must be different
871     */
872    public final void testGetEncryptedData04() {
873        boolean performed = false;
874        for (int i = 0; i < EncryptedPrivateKeyInfoData.algName0.length; i++) {
875            try {
876                EncryptedPrivateKeyInfo epki = new EncryptedPrivateKeyInfo(
877                        EncryptedPrivateKeyInfoData.algName0[i][0],
878                        EncryptedPrivateKeyInfoData.encryptedData);
879
880                // check that method under test returns
881                // new array each time
882                byte[] ecd1 = epki.getEncryptedData();
883                byte[] ecd2 = epki.getEncryptedData();
884                assertNotSame(EncryptedPrivateKeyInfoData.encryptedData, ecd1);
885                assertNotSame(EncryptedPrivateKeyInfoData.encryptedData, ecd2);
886                assertNotSame(ecd1, ecd2);
887
888                performed = true;
889            } catch (NoSuchAlgorithmException allowedFailure) {
890            }
891        }
892        assertTrue("Test not performed", performed);
893    }
894
895    /**
896     * Test #1 for <code>getEncoded()</code> method <br>
897     * Assertion: returns the ASN.1 encoding of this object <br>
898     * Test preconditions: test object created using ctor which takes encoded
899     * form as the only parameter <br>
900     * Expected: equivalent encoded form must be returned
901     *
902     * @throws IOException
903     */
904    public final void testGetEncoded01() throws IOException {
905        boolean performed = false;
906        for (int i = 0; i < EncryptedPrivateKeyInfoData.algName0.length; i++) {
907            try {
908                byte[] enc = EncryptedPrivateKeyInfoData
909                        .getValidEncryptedPrivateKeyInfoEncoding(
910                                EncryptedPrivateKeyInfoData.algName0[i][0]);
911                EncryptedPrivateKeyInfo epki = new EncryptedPrivateKeyInfo(enc);
912
913                // check that method under test returns
914                // valid encoded form
915                assertTrue(Arrays.equals(enc, epki.getEncoded()));
916
917                performed = true;
918            } catch (NoSuchAlgorithmException allowedFailure) {
919            }
920        }
921        assertTrue("Test not performed", performed);
922    }
923
924    /**
925     * Test #2 for <code>getEncoded()</code> method <br>
926     * Assertion: returns the ASN.1 encoding of this object <br>
927     * Test preconditions: test object created using ctor which takes algorithm
928     * name and encrypted data as a parameters <br>
929     * Expected: equivalent encoded form (without alg params) must be returned
930     *
931     * @throws IOException
932     */
933    public final void testGetEncoded02() throws IOException {
934        boolean performed = false;
935        for (int i = 0; i < EncryptedPrivateKeyInfoData.algName0.length; i++) {
936            try {
937                EncryptedPrivateKeyInfo epki = new EncryptedPrivateKeyInfo(
938                        EncryptedPrivateKeyInfoData.algName0[i][0],
939                        EncryptedPrivateKeyInfoData.encryptedData);
940
941                // check that method under test returns
942                // valid encoded form
943                byte[] refEnc = EncryptedPrivateKeyInfoData
944                        .getValidEncryptedPrivateKeyInfoEncoding(
945                                EncryptedPrivateKeyInfoData.algName0[i][0],
946                                false);
947                //                System.out.println(Array.toString(refEnc, " "));
948                byte[] actEnc = epki.getEncoded();
949                //                System.out.println(Array.toString(actEnc, " "));
950                assertTrue(Arrays.equals(refEnc, actEnc));
951
952                performed = true;
953            } catch (NoSuchAlgorithmException allowedFailure) {
954            }
955        }
956        assertTrue("Test not performed", performed);
957    }
958
959    /**
960     * Test #3 for <code>getEncoded()</code> method <br>
961     * Assertion: returns the ASN.1 encoding of this object <br>
962     * Test preconditions: test object created using ctor which takes algorithm
963     * name and encrypted data as a parameters <br>
964     * Expected: equivalent encoded form (without alg params) must be returned
965     *
966     * @throws IOException
967     */
968    public final void testGetEncoded03() throws IOException {
969        boolean performed = false;
970        for (int i = 0; i < EncryptedPrivateKeyInfoData.algName0.length; i++) {
971            try {
972                AlgorithmParameters ap = AlgorithmParameters
973                        .getInstance(EncryptedPrivateKeyInfoData.algName0[i][0]);
974                // use pregenerated AlgorithmParameters encodings
975                ap.init(EncryptedPrivateKeyInfoData.getParametersEncoding(
976                        EncryptedPrivateKeyInfoData.algName0[i][0]));
977
978                EncryptedPrivateKeyInfo epki = new EncryptedPrivateKeyInfo(ap,
979                        EncryptedPrivateKeyInfoData.encryptedData);
980
981                // check that method under test returns
982                // valid encoded form
983                assertTrue(Arrays.equals(
984                        EncryptedPrivateKeyInfoData
985                            .getValidEncryptedPrivateKeyInfoEncoding(
986                                EncryptedPrivateKeyInfoData.algName0[i][0]),
987                                epki.getEncoded()));
988
989                performed = true;
990            } catch (NoSuchAlgorithmException allowedFailure) {
991            }
992        }
993        assertTrue("Test not performed", performed);
994    }
995
996    /**
997     * Test #4 for <code>getEncoded()</code> method <br>
998     * Assertion: returns a new array each time this method is called <br>
999     * Test preconditions: test object created using ctor which takes algorithm
1000     * name and encrypted data as a parameters <br>
1001     * Expected: several refs to byte array returned by the method under test
1002     * must be different
1003     *
1004     * @throws IOException
1005     */
1006    public final void testGetEncoded04() throws IOException {
1007        boolean performed = false;
1008        for (int i = 0; i < EncryptedPrivateKeyInfoData.algName0.length; i++) {
1009            try {
1010                EncryptedPrivateKeyInfo epki = new EncryptedPrivateKeyInfo(
1011                        EncryptedPrivateKeyInfoData.algName0[i][0],
1012                        EncryptedPrivateKeyInfoData.encryptedData);
1013
1014                // check that method under test returns
1015                // new array each time
1016                byte[] ec1 = epki.getEncoded();
1017                byte[] ec2 = epki.getEncoded();
1018                byte[] ec3 = epki.getEncoded();
1019                assertNotSame(ec1, ec2);
1020                assertNotSame(ec2, ec3);
1021                assertNotSame(ec1, ec3);
1022
1023                performed = true;
1024            } catch (NoSuchAlgorithmException allowedFailure) {
1025            }
1026        }
1027        assertTrue("Test not performed", performed);
1028    }
1029
1030    public final void testGetKeySpecCipher01() {
1031        boolean performed = false;
1032        for (int i = 0; i < EncryptedPrivateKeyInfoData.algName0.length; i++) {
1033            try {
1034                EncryptedPrivateKeyInfo epki = new EncryptedPrivateKeyInfo(
1035                        EncryptedPrivateKeyInfoData.algName0[i][0],
1036                        EncryptedPrivateKeyInfoData.encryptedData);
1037
1038                try {
1039
1040                    // check that method under test throws NPE
1041                    epki.getKeySpec((Cipher) null);
1042                    fail(getName() + "NullPointerException has not been thrown");
1043
1044                } catch (NullPointerException ok) {
1045                } catch (InvalidKeySpecException e) {
1046                    fail(getName() + "Unexpected exception: " + e);
1047                }
1048
1049                performed = true;
1050            } catch (NoSuchAlgorithmException allowedFailure) {
1051            }
1052        }
1053        assertTrue("Test not performed", performed);
1054    }
1055
1056    /**
1057     * Encrypted data contains valid PKCS8 key info encoding
1058     */
1059    public final void test_ROUNDTRIP_GetKeySpecCipher01() {
1060        boolean performed = false;
1061
1062        for (int i = 0; i < algName.length; i++) {
1063            try {
1064                // generate test data
1065                TestDataGenerator g = new TestDataGenerator(algName[i][0],
1066                        algName[i][1], privateKeyInfo, null);
1067
1068                // create test object
1069                EncryptedPrivateKeyInfo epki;
1070                if (g.ap() == null) {
1071                    epki = new EncryptedPrivateKeyInfo(algName[i][0], g.ct());
1072                } else {
1073                    epki = new EncryptedPrivateKeyInfo(g.ap(), g.ct());
1074                }
1075
1076                // call methods under test
1077                try {
1078
1079                    PKCS8EncodedKeySpec eks = epki.getKeySpec(g.c());
1080
1081                    if (!Arrays.equals(privateKeyInfo, eks.getEncoded())) {
1082                        fail(algName[i][0] + " != " + algName[i][1]);
1083                    }
1084                } catch (InvalidKeySpecException e) {
1085                    fail(algName[i][0] + ", " + algName[i][1] + e + "\n");
1086                }
1087                performed = true;
1088
1089            } catch (TestDataGenerator.AllowedFailure allowedFailure) {
1090            } catch (NoSuchAlgorithmException allowed) {
1091            }
1092        }
1093        assertTrue("Test not performed", performed);
1094    }
1095
1096    /**
1097     * Encrypted data contains invalid PKCS8 key info encoding
1098     */
1099    public final void test_ROUNDTRIP_GetKeySpecCipher02() {
1100        boolean performed = false;
1101        for (int i = 0; i < algName.length; i++) {
1102            try {
1103                // generate test data
1104                TestDataGenerator g = new TestDataGenerator(algName[i][0],
1105                        algName[i][1], privateKeyInfoDamaged, null);
1106
1107                // create test object
1108                EncryptedPrivateKeyInfo epki;
1109                if (g.ap() == null) {
1110                    epki = new EncryptedPrivateKeyInfo(algName[i][0], g.ct());
1111                } else {
1112                    epki = new EncryptedPrivateKeyInfo(g.ap(), g.ct());
1113                }
1114
1115                // call methods under test
1116                try {
1117                    epki.getKeySpec(g.c());
1118
1119                    // must not get here because decrypted data does
1120                    // not represent valid PKCS8 encoding
1121                    fail(algName[i][0] + ", " + algName[i][1]);
1122                } catch (InvalidKeySpecException ok) {
1123                }
1124
1125                performed = true;
1126            } catch (TestDataGenerator.AllowedFailure allowedFailure) {
1127            } catch (NoSuchAlgorithmException allowedFailure) {
1128            }
1129        }
1130        assertTrue("Test not performed", performed);
1131    }
1132
1133    public final void testGetKeySpecKey01() {
1134        boolean performed = false;
1135        for (int i = 0; i < EncryptedPrivateKeyInfoData.algName0.length; i++) {
1136            try {
1137                EncryptedPrivateKeyInfo epki = new EncryptedPrivateKeyInfo(
1138                        EncryptedPrivateKeyInfoData.algName0[i][0],
1139                        EncryptedPrivateKeyInfoData.encryptedData);
1140
1141                try {
1142
1143                    // check that method under test throws NPE
1144                    epki.getKeySpec((Key) null);
1145                    fail(getName() + "NullPointerException has not been thrown");
1146
1147                } catch (NullPointerException ok) {
1148                } catch (InvalidKeyException e) {
1149                    fail(getName() + "Unexpected exception: " + e);
1150                }
1151
1152                performed = true;
1153            } catch (NoSuchAlgorithmException allowedFailure) {
1154            }
1155        }
1156        assertTrue("Test not performed", performed);
1157    }
1158
1159    /**
1160     * Encrypted data contains valid PKCS8 key info encoding
1161     */
1162    public final void test_ROUNDTRIP_GetKeySpecKey01() {
1163        boolean performed = false;
1164        for (int i = 0; i < algName.length; i++) {
1165            try {
1166                // generate test data
1167                TestDataGenerator g = new TestDataGenerator(algName[i][0],
1168                        algName[i][1], privateKeyInfo, null);
1169
1170                // create test object
1171                EncryptedPrivateKeyInfo epki;
1172                if (g.ap() == null) {
1173                    epki = new EncryptedPrivateKeyInfo(algName[i][0], g.ct());
1174                } else {
1175                    epki = new EncryptedPrivateKeyInfo(g.ap(), g.ct());
1176                }
1177
1178                try {
1179                    PKCS8EncodedKeySpec eks = epki
1180                            .getKeySpec(g.pubK() == null ? g.k() : g.pubK());
1181
1182                    if (!Arrays.equals(privateKeyInfo, eks.getEncoded())) {
1183                        fail(algName[i][0] + " != " + algName[i][1]);
1184                    }
1185                } catch (InvalidKeyException e) {
1186                    fail(algName[i][0] + ", " + algName[i][1] + ": " + e);
1187                }
1188
1189                performed = true;
1190            } catch (TestDataGenerator.AllowedFailure allowedFailure) {
1191            } catch (NoSuchAlgorithmException allowedFailure) {
1192            }
1193        }
1194        assertTrue("Test not performed", performed);
1195    }
1196
1197    /**
1198     * Encrypted data contains invalid PKCS8 key info encoding
1199     */
1200    public final void test_ROUNDTRIP_GetKeySpecKey02() {
1201        boolean performed = false;
1202        for (int i = 0; i < algName.length; i++) {
1203            try {
1204                // generate test data
1205                TestDataGenerator g = new TestDataGenerator(algName[i][0],
1206                        algName[i][1], privateKeyInfoDamaged, null);
1207
1208                // create test object
1209                EncryptedPrivateKeyInfo epki;
1210                if (g.ap() == null) {
1211                    epki = new EncryptedPrivateKeyInfo(algName[i][0], g.ct());
1212                } else {
1213                    epki = new EncryptedPrivateKeyInfo(g.ap(), g.ct());
1214                }
1215
1216                try {
1217                    epki.getKeySpec(g.pubK() == null ? g.k() : g.pubK());
1218                    fail(algName[i][0] + ", " + algName[i][1]);
1219                } catch (InvalidKeyException e) {
1220                }
1221
1222                performed = true;
1223            } catch (TestDataGenerator.AllowedFailure allowedFailure) {
1224            } catch (NoSuchAlgorithmException allowedFailure) {
1225            }
1226        }
1227        assertTrue("Test not performed", performed);
1228    }
1229
1230    public final void testGetKeySpecKeyString01() throws Exception {
1231        boolean performed = false;
1232        for (int i = 0; i < EncryptedPrivateKeyInfoData.algName0.length; i++) {
1233            try {
1234                EncryptedPrivateKeyInfo epki = new EncryptedPrivateKeyInfo(
1235                        EncryptedPrivateKeyInfoData.algName0[i][0],
1236                        EncryptedPrivateKeyInfoData.encryptedData);
1237
1238                try {
1239
1240                    // check that method under test throws NPE
1241                    epki.getKeySpec((Key) null, "SomeProviderName");
1242                    fail(getName() + "NullPointerException has not been thrown");
1243
1244                } catch (NullPointerException ok) {
1245                }
1246
1247                try {
1248
1249                    // check that method under test throws NPE
1250                    epki.getKeySpec(new Key() {
1251                        public String getAlgorithm() {
1252                            return "alg";
1253                        }
1254
1255                        public String getFormat() {
1256                            return "fmt";
1257                        }
1258
1259                        public byte[] getEncoded() {
1260                            return new byte[] {};
1261                        }
1262                    }, (String) null);
1263
1264                    fail(getName() + "NullPointerException has not been thrown");
1265
1266                } catch (NullPointerException ok) {
1267                }
1268
1269                performed = true;
1270            } catch (NoSuchAlgorithmException allowedFailure) {
1271            }
1272        }
1273        assertTrue("Test not performed", performed);
1274    }
1275
1276    /**
1277     * Encrypted data contains valid PKCS8 key info encoding
1278     */
1279    public final void test_ROUNDTRIP_GetKeySpecKeyString01() throws Exception {
1280        boolean performed = false;
1281        for (int i = 0; i < algName.length; i++) {
1282            for (int l = 0; l < provider.length; l++) {
1283                if (provider[l] == null) {
1284                    continue;
1285                }
1286                TestDataGenerator g;
1287                try {
1288                    // generate test data
1289                    g = new TestDataGenerator(algName[i][0], algName[i][1],
1290                            privateKeyInfo, provider[l]);
1291                } catch (TestDataGenerator.AllowedFailure allowedFailure) {
1292                    continue;
1293                }
1294
1295                try {
1296                    // create test object
1297                    EncryptedPrivateKeyInfo epki;
1298                    if (g.ap() == null) {
1299                        epki = new EncryptedPrivateKeyInfo(algName[i][0], g
1300                                .ct());
1301                    } else {
1302                        epki = new EncryptedPrivateKeyInfo(g.ap(), g.ct());
1303                    }
1304                    try {
1305
1306                        PKCS8EncodedKeySpec eks = epki.getKeySpec(
1307                                g.pubK() == null ? g.k() : g.pubK(),
1308                                provider[l].getName());
1309
1310                        if (!Arrays.equals(privateKeyInfo, eks.getEncoded())) {
1311                            fail(algName[i][0] + " != " + algName[i][1]);
1312                        }
1313                    } catch (InvalidKeyException e) {
1314                        fail(algName[i][0] + ", " + algName[i][1] + ": " + e);
1315                    }
1316
1317                    performed = true;
1318                } catch (NoSuchAlgorithmException allowedFailure) {
1319                }
1320            }
1321        }
1322        assertTrue("Test not performed", performed);
1323    }
1324
1325    /**
1326     * Encrypted data contains invalid PKCS8 key info encoding
1327     */
1328    public final void test_ROUNDTRIP_GetKeySpecKeyString02() throws Exception {
1329        boolean performed = false;
1330        for (int i = 0; i < algName.length; i++) {
1331            for (int l = 0; l < provider.length; l++) {
1332                if (provider[l] == null) {
1333                    continue;
1334                }
1335                TestDataGenerator g;
1336                try {
1337                    // generate test data
1338                    g = new TestDataGenerator(algName[i][0], algName[i][1],
1339                            privateKeyInfoDamaged, provider[l]);
1340                } catch (TestDataGenerator.AllowedFailure allowedFailure) {
1341                    continue;
1342                }
1343
1344                try {
1345                    // create test object
1346                    EncryptedPrivateKeyInfo epki;
1347                    if (g.ap() == null) {
1348                        epki = new EncryptedPrivateKeyInfo(algName[i][0], g
1349                                .ct());
1350                    } else {
1351                        epki = new EncryptedPrivateKeyInfo(g.ap(), g.ct());
1352                    }
1353
1354                    try {
1355
1356                        epki.getKeySpec(g.pubK() == null ? g.k() : g.pubK(),
1357                                provider[l].getName());
1358
1359                        fail(algName[i][0] + ", " + algName[i][1]);
1360
1361                    } catch (InvalidKeyException e) {
1362                    }
1363
1364                    performed = true;
1365                } catch (NoSuchAlgorithmException allowedFailure) {
1366                }
1367            }
1368        }
1369        assertTrue("Test not performed", performed);
1370    }
1371
1372    public final void testGetKeySpecKeyProvider01() throws Exception {
1373        boolean performed = false;
1374
1375        for (int i = 0; i < EncryptedPrivateKeyInfoData.algName0.length; i++) {
1376            try {
1377                EncryptedPrivateKeyInfo epki = new EncryptedPrivateKeyInfo(
1378                        EncryptedPrivateKeyInfoData.algName0[i][0],
1379                        EncryptedPrivateKeyInfoData.encryptedData);
1380
1381                try {
1382
1383                    // check that method under test throws NPE
1384                    epki.getKeySpec((Key) null, (Provider) null);
1385                    fail(getName() + "NullPointerException has not been thrown");
1386                } catch (NullPointerException ok) {
1387                }
1388
1389                try {
1390
1391                    // check that method under test throws NPE
1392                    epki.getKeySpec(new Key() {
1393                        public String getAlgorithm() {
1394                            return "alg";
1395                        }
1396
1397                        public String getFormat() {
1398                            return "fmt";
1399                        }
1400
1401                        public byte[] getEncoded() {
1402                            return new byte[] {};
1403                        }
1404                    }, (Provider) null);
1405
1406                    fail(getName() + "NullPointerException has not been thrown");
1407                } catch (NullPointerException ok) {
1408                }
1409
1410                performed = true;
1411            } catch (NoSuchAlgorithmException allowedFailure) {
1412            }
1413        }
1414        assertTrue("Test not performed", performed);
1415    }
1416
1417    /**
1418     * Encrypted data contains valid PKCS8 key info encoding
1419     */
1420    public final void test_ROUNDTRIP_GetKeySpecKeyProvider01() {
1421        boolean performed = false;
1422
1423        for (int i = 0; i < algName.length; i++) {
1424            for (int l = 0; l < provider.length; l++) {
1425                if (provider[l] == null) {
1426                    continue;
1427                }
1428                TestDataGenerator g;
1429                try {
1430                    // generate test data
1431                    g = new TestDataGenerator(algName[i][0], algName[i][1],
1432                            privateKeyInfo, provider[l]);
1433                } catch (TestDataGenerator.AllowedFailure allowedFailure) {
1434                    continue;
1435                }
1436                try {
1437                    // create test object
1438                    EncryptedPrivateKeyInfo epki;
1439                    if (g.ap() == null) {
1440                        epki = new EncryptedPrivateKeyInfo(algName[i][0], g
1441                                .ct());
1442                    } else {
1443                        epki = new EncryptedPrivateKeyInfo(g.ap(), g.ct());
1444                    }
1445                    try {
1446
1447                        PKCS8EncodedKeySpec eks = epki.getKeySpec(
1448                                g.pubK() == null ? g.k() : g.pubK(),
1449                                provider[l]);
1450
1451                        if (!Arrays.equals(privateKeyInfo, eks.getEncoded())) {
1452                            fail(algName[i][0] + " != " + algName[i][1]);
1453                        }
1454                    } catch (InvalidKeyException e) {
1455                        fail(algName[i][0] + ", " + algName[i][1] + ": " + e);
1456                    }
1457                    performed = true;
1458
1459                } catch (NoSuchAlgorithmException allowedFailure) {
1460                }
1461            }
1462        }
1463        assertTrue("Test not performed", performed);
1464    }
1465
1466    /**
1467     * Encrypted data contains invalid PKCS8 key info encoding
1468     */
1469    public final void test_ROUNDTRIP_GetKeySpecKeyProvider02() {
1470        boolean performed = false;
1471
1472        for (int i = 0; i < algName.length; i++) {
1473            for (int l = 0; l < provider.length; l++) {
1474                if (provider[l] == null) {
1475                    continue;
1476                }
1477                TestDataGenerator g;
1478                try {
1479                    // generate test data
1480                    g = new TestDataGenerator(algName[i][0], algName[i][1],
1481                            privateKeyInfoDamaged, provider[l]);
1482                } catch (TestDataGenerator.AllowedFailure allowedFailure) {
1483                    continue;
1484                }
1485
1486                try {
1487                    // create test object
1488                    EncryptedPrivateKeyInfo epki;
1489                    if (g.ap() == null) {
1490                        epki = new EncryptedPrivateKeyInfo(algName[i][0], g
1491                                .ct());
1492                    } else {
1493                        epki = new EncryptedPrivateKeyInfo(g.ap(), g.ct());
1494                    }
1495                    try {
1496
1497                        epki.getKeySpec(g.pubK() == null ? g.k() : g.pubK(),
1498                                provider[l]);
1499
1500                        fail(algName[i][0] + ", " + algName[i][1]);
1501
1502                    } catch (InvalidKeyException e) {
1503                    }
1504                    performed = true;
1505                } catch (NoSuchAlgorithmException allowedFailure) {
1506                }
1507            }
1508        }
1509        assertTrue("Test not performed", performed);
1510    }
1511
1512    public static class TestDataGenerator {
1513
1514        public static class AllowedFailure extends Exception {
1515            AllowedFailure(String msg) {
1516                super(msg);
1517            }
1518        }
1519
1520        private Cipher c = null;
1521
1522        private Key k = null, pubK = null;
1523
1524        private AlgorithmParameters ap = null;
1525
1526        byte[] ct;
1527
1528        public TestDataGenerator(String algName, String transformation,
1529                byte[] privateKeyInfo, Provider provider) throws AllowedFailure {
1530            try {
1531                c = (provider == null) ? Cipher
1532                        .getInstance(transformation != null ? transformation
1533                                : algName) : Cipher.getInstance(
1534                        transformation != null ? transformation : algName,
1535                        provider);
1536            } catch (NoSuchAlgorithmException e) {
1537                throw new AllowedFailure(e.getMessage());
1538            } catch (NoSuchPaddingException e) {
1539                throw new AllowedFailure(e.getMessage());
1540            }
1541
1542            try {
1543                KeyGenerator kg = (provider == null) ? KeyGenerator
1544                        .getInstance(algName) : KeyGenerator.getInstance(
1545                        algName, provider);
1546                k = kg.generateKey();
1547            } catch (NoSuchAlgorithmException e) {
1548            }
1549
1550            if (k == null) {
1551                try {
1552                    KeyPairGenerator kpg = (provider == null) ? KeyPairGenerator
1553                            .getInstance(algName)
1554                            : KeyPairGenerator.getInstance(algName, provider);
1555                    KeyPair kp = kpg.genKeyPair();
1556                    k = kp.getPrivate();
1557                    pubK = kp.getPublic();
1558                } catch (NoSuchAlgorithmException e) {
1559                }
1560            }
1561
1562            PBEParameterSpec pbeParamSpec = null;
1563            if (k == null) {
1564                try {
1565                    pbeParamSpec = new PBEParameterSpec(new byte[] { 1, 2, 3,
1566                            4, 5, 6, 7, 8 }, 10);
1567                    SecretKeyFactory skf = (provider == null) ? SecretKeyFactory
1568                            .getInstance(algName)
1569                            : SecretKeyFactory.getInstance(algName, provider);
1570                    PBEKeySpec ks = new PBEKeySpec("12345678".toCharArray());
1571                    try {
1572                        k = skf.generateSecret(ks);
1573                    } catch (InvalidKeySpecException e) {
1574                        throw new AllowedFailure(e.getMessage());
1575                    }
1576
1577                } catch (NoSuchAlgorithmException e) {
1578                    throw new AllowedFailure(e.getMessage());
1579                }
1580            }
1581
1582            try {
1583                if (pbeParamSpec == null) {
1584                    c.init(Cipher.ENCRYPT_MODE, k);
1585                } else {
1586                    c.init(Cipher.ENCRYPT_MODE, k, pbeParamSpec);
1587                }
1588            } catch (InvalidKeyException e) {
1589                throw new AllowedFailure(e.getMessage());
1590            } catch (SecurityException e) {
1591                throw new AllowedFailure(e.getMessage());
1592            } catch (InvalidAlgorithmParameterException e) {
1593                throw new AllowedFailure(e.getMessage());
1594            }
1595
1596            ap = c.getParameters();
1597
1598            try {
1599                ct = c.doFinal(privateKeyInfo);
1600            } catch (IllegalStateException e) {
1601                throw new AllowedFailure(e.getMessage());
1602            } catch (IllegalBlockSizeException e) {
1603                throw new AllowedFailure(e.getMessage());
1604            } catch (BadPaddingException e) {
1605                throw new AllowedFailure(e.getMessage());
1606            } catch (RuntimeException e) {
1607                throw new AllowedFailure(e.getMessage());
1608            }
1609
1610            try {
1611                // try to convert pbeParamSpec->ap
1612                if (pbeParamSpec != null) {
1613                    try {
1614                        ap = (provider == null) ? AlgorithmParameters
1615                                .getInstance(algName) : AlgorithmParameters
1616                                .getInstance(algName, provider);
1617                        ap.init(pbeParamSpec);
1618                        pbeParamSpec = null;
1619                    } catch (NoSuchAlgorithmException e) {
1620                        // couldn't convert
1621                        throw new AllowedFailure(e.getMessage());
1622                    } catch (InvalidParameterSpecException e) {
1623                        // couldn't convert
1624                        throw new AllowedFailure(e.getMessage());
1625                    }
1626                }
1627
1628                if (ap == null) {
1629                    c.init(Cipher.DECRYPT_MODE, pubK == null ? k : pubK);
1630                } else {
1631                    c.init(Cipher.DECRYPT_MODE, pubK == null ? k : pubK, ap);
1632                }
1633
1634            } catch (InvalidKeyException e) {
1635                throw new AllowedFailure(e.getMessage());
1636            } catch (SecurityException e) {
1637                throw new AllowedFailure(e.getMessage());
1638            } catch (InvalidAlgorithmParameterException e) {
1639                throw new AllowedFailure(e.getMessage());
1640            }
1641        }
1642
1643        public Key k() {
1644            return k;
1645        }
1646
1647        public Key pubK() {
1648            return pubK;
1649        }
1650
1651        public Cipher c() {
1652            return c;
1653        }
1654
1655        public byte[] ct() {
1656            return ct;
1657        }
1658
1659        public AlgorithmParameters ap() {
1660            return ap;
1661        }
1662    }
1663
1664    // valid PrivateKeyInfo encoding
1665    private static final byte[] privateKeyInfo = { (byte) 0x30, (byte) 0x82,
1666            (byte) 0x02, (byte) 0x77, (byte) 0x02, (byte) 0x01, (byte) 0x00,
1667            (byte) 0x30, (byte) 0x0d, (byte) 0x06, (byte) 0x09, (byte) 0x2a,
1668            (byte) 0x86, (byte) 0x48, (byte) 0x86, (byte) 0xf7, (byte) 0x0d,
1669            (byte) 0x01, (byte) 0x01, (byte) 0x01, (byte) 0x05, (byte) 0x00,
1670            (byte) 0x04, (byte) 0x82, (byte) 0x02, (byte) 0x61, (byte) 0x30,
1671            (byte) 0x82, (byte) 0x02, (byte) 0x5d, (byte) 0x02, (byte) 0x01,
1672            (byte) 0x00, (byte) 0x02, (byte) 0x81, (byte) 0x81, (byte) 0x00,
1673            (byte) 0xb2, (byte) 0x4a, (byte) 0x9b, (byte) 0x5b, (byte) 0xba,
1674            (byte) 0x01, (byte) 0xc0, (byte) 0xcd, (byte) 0x65, (byte) 0x09,
1675            (byte) 0x63, (byte) 0x70, (byte) 0x0b, (byte) 0x5a, (byte) 0x1b,
1676            (byte) 0x92, (byte) 0x08, (byte) 0xf8, (byte) 0x55, (byte) 0x5e,
1677            (byte) 0x7c, (byte) 0x1b, (byte) 0x50, (byte) 0x17, (byte) 0xec,
1678            (byte) 0x44, (byte) 0x4c, (byte) 0x58, (byte) 0x42, (byte) 0x2b,
1679            (byte) 0x41, (byte) 0x09, (byte) 0x59, (byte) 0xf2, (byte) 0xe1,
1680            (byte) 0x5d, (byte) 0x43, (byte) 0x71, (byte) 0x4d, (byte) 0x92,
1681            (byte) 0x03, (byte) 0x1d, (byte) 0xb6, (byte) 0x6c, (byte) 0x7f,
1682            (byte) 0x5d, (byte) 0x48, (byte) 0xcd, (byte) 0x17, (byte) 0xec,
1683            (byte) 0xd7, (byte) 0x4c, (byte) 0x39, (byte) 0xb1, (byte) 0x7b,
1684            (byte) 0xe2, (byte) 0xbf, (byte) 0x96, (byte) 0x77, (byte) 0xbe,
1685            (byte) 0xd0, (byte) 0xa0, (byte) 0xf0, (byte) 0x2d, (byte) 0x6b,
1686            (byte) 0x24, (byte) 0xaa, (byte) 0x14, (byte) 0xba, (byte) 0x82,
1687            (byte) 0x79, (byte) 0x10, (byte) 0x9b, (byte) 0x16, (byte) 0x68,
1688            (byte) 0x47, (byte) 0x81, (byte) 0x54, (byte) 0xa2, (byte) 0xfa,
1689            (byte) 0x91, (byte) 0x9e, (byte) 0x0a, (byte) 0x2a, (byte) 0x53,
1690            (byte) 0xa6, (byte) 0xe7, (byte) 0x9e, (byte) 0x7d, (byte) 0x29,
1691            (byte) 0x33, (byte) 0xd8, (byte) 0x05, (byte) 0xfc, (byte) 0x02,
1692            (byte) 0x3f, (byte) 0xbd, (byte) 0xc7, (byte) 0x6e, (byte) 0xed,
1693            (byte) 0xaa, (byte) 0x30, (byte) 0x6c, (byte) 0x5f, (byte) 0x52,
1694            (byte) 0xed, (byte) 0x35, (byte) 0x65, (byte) 0x4b, (byte) 0x0e,
1695            (byte) 0xc8, (byte) 0xa7, (byte) 0x12, (byte) 0x10, (byte) 0x56,
1696            (byte) 0x37, (byte) 0xaf, (byte) 0x11, (byte) 0xfa, (byte) 0x21,
1697            (byte) 0x0e, (byte) 0x99, (byte) 0xff, (byte) 0xfa, (byte) 0x8c,
1698            (byte) 0x65, (byte) 0x8e, (byte) 0x6d, (byte) 0x02, (byte) 0x03,
1699            (byte) 0x01, (byte) 0x00, (byte) 0x01, (byte) 0x02, (byte) 0x81,
1700            (byte) 0x80, (byte) 0x78, (byte) 0x41, (byte) 0x72, (byte) 0x40,
1701            (byte) 0x90, (byte) 0x59, (byte) 0x96, (byte) 0x5d, (byte) 0xf3,
1702            (byte) 0x84, (byte) 0x3d, (byte) 0x99, (byte) 0xd9, (byte) 0x4e,
1703            (byte) 0x51, (byte) 0xc2, (byte) 0x52, (byte) 0x62, (byte) 0x8d,
1704            (byte) 0xd2, (byte) 0x49, (byte) 0x0b, (byte) 0x73, (byte) 0x1e,
1705            (byte) 0x6f, (byte) 0xb2, (byte) 0x31, (byte) 0x7c, (byte) 0x66,
1706            (byte) 0x45, (byte) 0x1e, (byte) 0x7c, (byte) 0xdc, (byte) 0x3a,
1707            (byte) 0xc2, (byte) 0x5f, (byte) 0x51, (byte) 0x9a, (byte) 0x1e,
1708            (byte) 0xa4, (byte) 0x19, (byte) 0x8d, (byte) 0xf4, (byte) 0xf9,
1709            (byte) 0x81, (byte) 0x7e, (byte) 0xbe, (byte) 0x17, (byte) 0xf7,
1710            (byte) 0xc7, (byte) 0x3c, (byte) 0x00, (byte) 0xa1, (byte) 0xf9,
1711            (byte) 0x60, (byte) 0x82, (byte) 0x34, (byte) 0x8f, (byte) 0x9c,
1712            (byte) 0xfd, (byte) 0x0b, (byte) 0x63, (byte) 0x42, (byte) 0x1b,
1713            (byte) 0x7f, (byte) 0x45, (byte) 0xf1, (byte) 0x31, (byte) 0xc3,
1714            (byte) 0x63, (byte) 0x47, (byte) 0x5c, (byte) 0xc1, (byte) 0xb2,
1715            (byte) 0x5f, (byte) 0x57, (byte) 0xee, (byte) 0x02, (byte) 0x9f,
1716            (byte) 0x5e, (byte) 0x08, (byte) 0x48, (byte) 0xba, (byte) 0x74,
1717            (byte) 0xba, (byte) 0x81, (byte) 0xb7, (byte) 0x30, (byte) 0xac,
1718            (byte) 0x4c, (byte) 0x01, (byte) 0x35, (byte) 0xce, (byte) 0x46,
1719            (byte) 0x47, (byte) 0x8c, (byte) 0xe4, (byte) 0x62, (byte) 0x36,
1720            (byte) 0x1a, (byte) 0x65, (byte) 0x0e, (byte) 0x33, (byte) 0x56,
1721            (byte) 0xf9, (byte) 0xb7, (byte) 0xa0, (byte) 0xc4, (byte) 0xb6,
1722            (byte) 0x82, (byte) 0x55, (byte) 0x7d, (byte) 0x36, (byte) 0x55,
1723            (byte) 0xc0, (byte) 0x52, (byte) 0x5e, (byte) 0x35, (byte) 0x54,
1724            (byte) 0xbd, (byte) 0x97, (byte) 0x01, (byte) 0x00, (byte) 0xbf,
1725            (byte) 0x10, (byte) 0xdc, (byte) 0x1b, (byte) 0x51, (byte) 0x02,
1726            (byte) 0x41, (byte) 0x00, (byte) 0xe7, (byte) 0x68, (byte) 0x03,
1727            (byte) 0x3e, (byte) 0x21, (byte) 0x64, (byte) 0x68, (byte) 0x24,
1728            (byte) 0x7b, (byte) 0xd0, (byte) 0x31, (byte) 0xa0, (byte) 0xa2,
1729            (byte) 0xd9, (byte) 0x87, (byte) 0x6d, (byte) 0x79, (byte) 0x81,
1730            (byte) 0x8f, (byte) 0x8f, (byte) 0x2d, (byte) 0x7a, (byte) 0x95,
1731            (byte) 0x2e, (byte) 0x55, (byte) 0x9f, (byte) 0xd7, (byte) 0x86,
1732            (byte) 0x29, (byte) 0x93, (byte) 0xbd, (byte) 0x04, (byte) 0x7e,
1733            (byte) 0x4f, (byte) 0xdb, (byte) 0x56, (byte) 0xf1, (byte) 0x75,
1734            (byte) 0xd0, (byte) 0x4b, (byte) 0x00, (byte) 0x3a, (byte) 0xe0,
1735            (byte) 0x26, (byte) 0xf6, (byte) 0xab, (byte) 0x9e, (byte) 0x0b,
1736            (byte) 0x2a, (byte) 0xf4, (byte) 0xa8, (byte) 0xd7, (byte) 0xff,
1737            (byte) 0xbe, (byte) 0x01, (byte) 0xeb, (byte) 0x9b, (byte) 0x81,
1738            (byte) 0xc7, (byte) 0x5f, (byte) 0x02, (byte) 0x73, (byte) 0xe1,
1739            (byte) 0x2b, (byte) 0x02, (byte) 0x41, (byte) 0x00, (byte) 0xc5,
1740            (byte) 0x3d, (byte) 0x78, (byte) 0xab, (byte) 0xe6, (byte) 0xab,
1741            (byte) 0x3e, (byte) 0x29, (byte) 0xfd, (byte) 0x98, (byte) 0xd0,
1742            (byte) 0xa4, (byte) 0x3e, (byte) 0x58, (byte) 0xee, (byte) 0x48,
1743            (byte) 0x45, (byte) 0xa3, (byte) 0x66, (byte) 0xac, (byte) 0xe9,
1744            (byte) 0x4d, (byte) 0xbd, (byte) 0x60, (byte) 0xea, (byte) 0x24,
1745            (byte) 0xff, (byte) 0xed, (byte) 0x0c, (byte) 0x67, (byte) 0xc5,
1746            (byte) 0xfd, (byte) 0x36, (byte) 0x28, (byte) 0xea, (byte) 0x74,
1747            (byte) 0x88, (byte) 0xd1, (byte) 0xd1, (byte) 0xad, (byte) 0x58,
1748            (byte) 0xd7, (byte) 0xf0, (byte) 0x67, (byte) 0x20, (byte) 0xc1,
1749            (byte) 0xe3, (byte) 0xb3, (byte) 0xdb, (byte) 0x52, (byte) 0xad,
1750            (byte) 0xf3, (byte) 0xc4, (byte) 0x21, (byte) 0xd8, (byte) 0x8c,
1751            (byte) 0x4c, (byte) 0x41, (byte) 0x27, (byte) 0xdb, (byte) 0xd0,
1752            (byte) 0x35, (byte) 0x92, (byte) 0xc7, (byte) 0x02, (byte) 0x41,
1753            (byte) 0x00, (byte) 0xe0, (byte) 0x99, (byte) 0x42, (byte) 0xb4,
1754            (byte) 0x76, (byte) 0x02, (byte) 0x97, (byte) 0x55, (byte) 0xf9,
1755            (byte) 0xda, (byte) 0x3b, (byte) 0xa0, (byte) 0xd7, (byte) 0x0e,
1756            (byte) 0xdc, (byte) 0xf4, (byte) 0x33, (byte) 0x7f, (byte) 0xbd,
1757            (byte) 0xcf, (byte) 0xd0, (byte) 0xeb, (byte) 0x6e, (byte) 0x89,
1758            (byte) 0xf7, (byte) 0x4f, (byte) 0x5a, (byte) 0x07, (byte) 0x7c,
1759            (byte) 0xa9, (byte) 0x49, (byte) 0x47, (byte) 0x68, (byte) 0x35,
1760            (byte) 0xa8, (byte) 0x05, (byte) 0x3d, (byte) 0xfd, (byte) 0x04,
1761            (byte) 0x7b, (byte) 0x17, (byte) 0x31, (byte) 0x0d, (byte) 0xc8,
1762            (byte) 0xa3, (byte) 0x98, (byte) 0x34, (byte) 0xa0, (byte) 0x50,
1763            (byte) 0x44, (byte) 0x00, (byte) 0xf1, (byte) 0x0c, (byte) 0xe6,
1764            (byte) 0xe5, (byte) 0xc4, (byte) 0x41, (byte) 0x3d, (byte) 0xf8,
1765            (byte) 0x3d, (byte) 0x4e, (byte) 0x0b, (byte) 0x1c, (byte) 0xdb,
1766            (byte) 0x02, (byte) 0x41, (byte) 0x00, (byte) 0x82, (byte) 0x9b,
1767            (byte) 0x8a, (byte) 0xfd, (byte) 0xa1, (byte) 0x98, (byte) 0x41,
1768            (byte) 0x68, (byte) 0xc2, (byte) 0xd1, (byte) 0xdf, (byte) 0x4e,
1769            (byte) 0xf3, (byte) 0x2e, (byte) 0x26, (byte) 0x53, (byte) 0x5b,
1770            (byte) 0x31, (byte) 0xb1, (byte) 0x7a, (byte) 0xcc, (byte) 0x5e,
1771            (byte) 0xbb, (byte) 0x09, (byte) 0xa2, (byte) 0xe2, (byte) 0x6f,
1772            (byte) 0x4a, (byte) 0x04, (byte) 0x0d, (byte) 0xef, (byte) 0x90,
1773            (byte) 0x15, (byte) 0xbe, (byte) 0x10, (byte) 0x4a, (byte) 0xac,
1774            (byte) 0x92, (byte) 0xeb, (byte) 0xda, (byte) 0x72, (byte) 0xdb,
1775            (byte) 0x43, (byte) 0x08, (byte) 0xb7, (byte) 0x2b, (byte) 0x4c,
1776            (byte) 0xe1, (byte) 0xbb, (byte) 0x58, (byte) 0xcb, (byte) 0x71,
1777            (byte) 0x80, (byte) 0xad, (byte) 0xbc, (byte) 0xdc, (byte) 0x62,
1778            (byte) 0x5e, (byte) 0x3e, (byte) 0xcb, (byte) 0x92, (byte) 0xda,
1779            (byte) 0xf6, (byte) 0xdf, (byte) 0x02, (byte) 0x40, (byte) 0x4d,
1780            (byte) 0x81, (byte) 0x90, (byte) 0xc5, (byte) 0x77, (byte) 0x30,
1781            (byte) 0xb7, (byte) 0x29, (byte) 0x00, (byte) 0xa8, (byte) 0xf1,
1782            (byte) 0xb4, (byte) 0xae, (byte) 0x52, (byte) 0x63, (byte) 0x00,
1783            (byte) 0xb2, (byte) 0x2d, (byte) 0x3e, (byte) 0x7d, (byte) 0xd6,
1784            (byte) 0x4d, (byte) 0xf9, (byte) 0x8a, (byte) 0xc1, (byte) 0xb1,
1785            (byte) 0x98, (byte) 0x89, (byte) 0x52, (byte) 0x40, (byte) 0x14,
1786            (byte) 0x1b, (byte) 0x0e, (byte) 0x61, (byte) 0x8f, (byte) 0xf4,
1787            (byte) 0xbe, (byte) 0x59, (byte) 0x79, (byte) 0x79, (byte) 0x95,
1788            (byte) 0x19, (byte) 0x5c, (byte) 0x51, (byte) 0x08, (byte) 0x66,
1789            (byte) 0xc1, (byte) 0x42, (byte) 0x30, (byte) 0xb3, (byte) 0x7a,
1790            (byte) 0x86, (byte) 0x9f, (byte) 0x3e, (byte) 0xf5, (byte) 0x19,
1791            (byte) 0xa3, (byte) 0xae, (byte) 0x64, (byte) 0x69, (byte) 0x14,
1792            (byte) 0x07, (byte) 0x50, (byte) 0x97, };
1793
1794    // valid PrivateKeyInfo encoding (Damaged)
1795    private static final byte[] privateKeyInfoDamaged = { (byte) 0x30,
1796            (byte) 0x82, (byte) 0x02, (byte) 0x77, (byte) 0x02, (byte) 0x01,
1797            (byte) 0x00, (byte) 0x30, (byte) 0x0d, (byte) 0x06, (byte) 0x09,
1798            (byte) 0x2a, (byte) 0x86, (byte) 0x48, (byte) 0x86, (byte) 0xf7,
1799            (byte) 0x0d, (byte) 0x01, (byte) 0x01, (byte) 0x01, (byte) 0x05,
1800            (byte) 0x00, (byte) 0x04, // private key octet str
1801            (byte) 0x82, (byte) 0x02, (byte) 0x62, // Damage: l=460->461
1802                                                   // (0x61->0x62)
1803            (byte) 0x30, (byte) 0x82, (byte) 0x02, (byte) 0x5d, (byte) 0x02,
1804            (byte) 0x01, (byte) 0x00, (byte) 0x02, (byte) 0x81, (byte) 0x81,
1805            (byte) 0x00, (byte) 0xb2, (byte) 0x4a, (byte) 0x9b, (byte) 0x5b,
1806            (byte) 0xba, (byte) 0x01, (byte) 0xc0, (byte) 0xcd, (byte) 0x65,
1807            (byte) 0x09, (byte) 0x63, (byte) 0x70, (byte) 0x0b, (byte) 0x5a,
1808            (byte) 0x1b, (byte) 0x92, (byte) 0x08, (byte) 0xf8, (byte) 0x55,
1809            (byte) 0x5e, (byte) 0x7c, (byte) 0x1b, (byte) 0x50, (byte) 0x17,
1810            (byte) 0xec, (byte) 0x44, (byte) 0x4c, (byte) 0x58, (byte) 0x42,
1811            (byte) 0x2b, (byte) 0x41, (byte) 0x09, (byte) 0x59, (byte) 0xf2,
1812            (byte) 0xe1, (byte) 0x5d, (byte) 0x43, (byte) 0x71, (byte) 0x4d,
1813            (byte) 0x92, (byte) 0x03, (byte) 0x1d, (byte) 0xb6, (byte) 0x6c,
1814            (byte) 0x7f, (byte) 0x5d, (byte) 0x48, (byte) 0xcd, (byte) 0x17,
1815            (byte) 0xec, (byte) 0xd7, (byte) 0x4c, (byte) 0x39, (byte) 0xb1,
1816            (byte) 0x7b, (byte) 0xe2, (byte) 0xbf, (byte) 0x96, (byte) 0x77,
1817            (byte) 0xbe, (byte) 0xd0, (byte) 0xa0, (byte) 0xf0, (byte) 0x2d,
1818            (byte) 0x6b, (byte) 0x24, (byte) 0xaa, (byte) 0x14, (byte) 0xba,
1819            (byte) 0x82, (byte) 0x79, (byte) 0x10, (byte) 0x9b, (byte) 0x16,
1820            (byte) 0x68, (byte) 0x47, (byte) 0x81, (byte) 0x54, (byte) 0xa2,
1821            (byte) 0xfa, (byte) 0x91, (byte) 0x9e, (byte) 0x0a, (byte) 0x2a,
1822            (byte) 0x53, (byte) 0xa6, (byte) 0xe7, (byte) 0x9e, (byte) 0x7d,
1823            (byte) 0x29, (byte) 0x33, (byte) 0xd8, (byte) 0x05, (byte) 0xfc,
1824            (byte) 0x02, (byte) 0x3f, (byte) 0xbd, (byte) 0xc7, (byte) 0x6e,
1825            (byte) 0xed, (byte) 0xaa, (byte) 0x30, (byte) 0x6c, (byte) 0x5f,
1826            (byte) 0x52, (byte) 0xed, (byte) 0x35, (byte) 0x65, (byte) 0x4b,
1827            (byte) 0x0e, (byte) 0xc8, (byte) 0xa7, (byte) 0x12, (byte) 0x10,
1828            (byte) 0x56, (byte) 0x37, (byte) 0xaf, (byte) 0x11, (byte) 0xfa,
1829            (byte) 0x21, (byte) 0x0e, (byte) 0x99, (byte) 0xff, (byte) 0xfa,
1830            (byte) 0x8c, (byte) 0x65, (byte) 0x8e, (byte) 0x6d, (byte) 0x02,
1831            (byte) 0x03, (byte) 0x01, (byte) 0x00, (byte) 0x01, (byte) 0x02,
1832            (byte) 0x81, (byte) 0x80, (byte) 0x78, (byte) 0x41, (byte) 0x72,
1833            (byte) 0x40, (byte) 0x90, (byte) 0x59, (byte) 0x96, (byte) 0x5d,
1834            (byte) 0xf3, (byte) 0x84, (byte) 0x3d, (byte) 0x99, (byte) 0xd9,
1835            (byte) 0x4e, (byte) 0x51, (byte) 0xc2, (byte) 0x52, (byte) 0x62,
1836            (byte) 0x8d, (byte) 0xd2, (byte) 0x49, (byte) 0x0b, (byte) 0x73,
1837            (byte) 0x1e, (byte) 0x6f, (byte) 0xb2, (byte) 0x31, (byte) 0x7c,
1838            (byte) 0x66, (byte) 0x45, (byte) 0x1e, (byte) 0x7c, (byte) 0xdc,
1839            (byte) 0x3a, (byte) 0xc2, (byte) 0x5f, (byte) 0x51, (byte) 0x9a,
1840            (byte) 0x1e, (byte) 0xa4, (byte) 0x19, (byte) 0x8d, (byte) 0xf4,
1841            (byte) 0xf9, (byte) 0x81, (byte) 0x7e, (byte) 0xbe, (byte) 0x17,
1842            (byte) 0xf7, (byte) 0xc7, (byte) 0x3c, (byte) 0x00, (byte) 0xa1,
1843            (byte) 0xf9, (byte) 0x60, (byte) 0x82, (byte) 0x34, (byte) 0x8f,
1844            (byte) 0x9c, (byte) 0xfd, (byte) 0x0b, (byte) 0x63, (byte) 0x42,
1845            (byte) 0x1b, (byte) 0x7f, (byte) 0x45, (byte) 0xf1, (byte) 0x31,
1846            (byte) 0xc3, (byte) 0x63, (byte) 0x47, (byte) 0x5c, (byte) 0xc1,
1847            (byte) 0xb2, (byte) 0x5f, (byte) 0x57, (byte) 0xee, (byte) 0x02,
1848            (byte) 0x9f, (byte) 0x5e, (byte) 0x08, (byte) 0x48, (byte) 0xba,
1849            (byte) 0x74, (byte) 0xba, (byte) 0x81, (byte) 0xb7, (byte) 0x30,
1850            (byte) 0xac, (byte) 0x4c, (byte) 0x01, (byte) 0x35, (byte) 0xce,
1851            (byte) 0x46, (byte) 0x47, (byte) 0x8c, (byte) 0xe4, (byte) 0x62,
1852            (byte) 0x36, (byte) 0x1a, (byte) 0x65, (byte) 0x0e, (byte) 0x33,
1853            (byte) 0x56, (byte) 0xf9, (byte) 0xb7, (byte) 0xa0, (byte) 0xc4,
1854            (byte) 0xb6, (byte) 0x82, (byte) 0x55, (byte) 0x7d, (byte) 0x36,
1855            (byte) 0x55, (byte) 0xc0, (byte) 0x52, (byte) 0x5e, (byte) 0x35,
1856            (byte) 0x54, (byte) 0xbd, (byte) 0x97, (byte) 0x01, (byte) 0x00,
1857            (byte) 0xbf, (byte) 0x10, (byte) 0xdc, (byte) 0x1b, (byte) 0x51,
1858            (byte) 0x02, (byte) 0x41, (byte) 0x00, (byte) 0xe7, (byte) 0x68,
1859            (byte) 0x03, (byte) 0x3e, (byte) 0x21, (byte) 0x64, (byte) 0x68,
1860            (byte) 0x24, (byte) 0x7b, (byte) 0xd0, (byte) 0x31, (byte) 0xa0,
1861            (byte) 0xa2, (byte) 0xd9, (byte) 0x87, (byte) 0x6d, (byte) 0x79,
1862            (byte) 0x81, (byte) 0x8f, (byte) 0x8f, (byte) 0x2d, (byte) 0x7a,
1863            (byte) 0x95, (byte) 0x2e, (byte) 0x55, (byte) 0x9f, (byte) 0xd7,
1864            (byte) 0x86, (byte) 0x29, (byte) 0x93, (byte) 0xbd, (byte) 0x04,
1865            (byte) 0x7e, (byte) 0x4f, (byte) 0xdb, (byte) 0x56, (byte) 0xf1,
1866            (byte) 0x75, (byte) 0xd0, (byte) 0x4b, (byte) 0x00, (byte) 0x3a,
1867            (byte) 0xe0, (byte) 0x26, (byte) 0xf6, (byte) 0xab, (byte) 0x9e,
1868            (byte) 0x0b, (byte) 0x2a, (byte) 0xf4, (byte) 0xa8, (byte) 0xd7,
1869            (byte) 0xff, (byte) 0xbe, (byte) 0x01, (byte) 0xeb, (byte) 0x9b,
1870            (byte) 0x81, (byte) 0xc7, (byte) 0x5f, (byte) 0x02, (byte) 0x73,
1871            (byte) 0xe1, (byte) 0x2b, (byte) 0x02, (byte) 0x41, (byte) 0x00,
1872            (byte) 0xc5, (byte) 0x3d, (byte) 0x78, (byte) 0xab, (byte) 0xe6,
1873            (byte) 0xab, (byte) 0x3e, (byte) 0x29, (byte) 0xfd, // 88
1874            (byte) 0x98, (byte) 0xd0, (byte) 0xa4, (byte) 0x3e, (byte) 0x58,
1875            (byte) 0xee, (byte) 0x48, (byte) 0x45, (byte) 0xa3, (byte) 0x66,
1876            (byte) 0xac, (byte) 0xe9, (byte) 0x4d, (byte) 0xbd, (byte) 0x60,
1877            (byte) 0xea, (byte) 0x24, (byte) 0xff, (byte) 0xed, (byte) 0x0c,
1878            (byte) 0x67, (byte) 0xc5, (byte) 0xfd, (byte) 0x36, (byte) 0x28,
1879            (byte) 0xea, (byte) 0x74, (byte) 0x88, (byte) 0xd1, (byte) 0xd1,
1880            (byte) 0xad, (byte) 0x58, (byte) 0xd7, (byte) 0xf0, (byte) 0x67,
1881            (byte) 0x20, (byte) 0xc1, (byte) 0xe3, (byte) 0xb3, (byte) 0xdb,
1882            (byte) 0x52, (byte) 0xad, (byte) 0xf3, (byte) 0xc4, (byte) 0x21,
1883            (byte) 0xd8, (byte) 0x8c, (byte) 0x4c, (byte) 0x41, (byte) 0x27,
1884            (byte) 0xdb, (byte) 0xd0, (byte) 0x35, (byte) 0x92, (byte) 0xc7,
1885            (byte) 0x02, (byte) 0x41, (byte) 0x00, (byte) 0xe0, (byte) 0x99,
1886            (byte) 0x42, (byte) 0xb4, (byte) 0x76, (byte) 0x02, (byte) 0x97,
1887            (byte) 0x55, (byte) 0xf9, (byte) 0xda, (byte) 0x3b, (byte) 0xa0,
1888            (byte) 0xd7, (byte) 0x0e, (byte) 0xdc, (byte) 0xf4, (byte) 0x33,
1889            (byte) 0x7f, (byte) 0xbd, (byte) 0xcf, (byte) 0xd0, (byte) 0xeb,
1890            (byte) 0x6e, (byte) 0x89, (byte) 0xf7, (byte) 0x4f, (byte) 0x5a,
1891            (byte) 0x07, (byte) 0x7c, (byte) 0xa9, (byte) 0x49, (byte) 0x47,
1892            (byte) 0x68, (byte) 0x35, (byte) 0xa8, (byte) 0x05, (byte) 0x3d,
1893            (byte) 0xfd, (byte) 0x04, (byte) 0x7b, (byte) 0x17, (byte) 0x31,
1894            (byte) 0x0d, (byte) 0xc8, (byte) 0xa3, (byte) 0x98, (byte) 0x34,
1895            (byte) 0xa0, (byte) 0x50, (byte) 0x44, (byte) 0x00, (byte) 0xf1,
1896            (byte) 0x0c, (byte) 0xe6, (byte) 0xe5, (byte) 0xc4, (byte) 0x41,
1897            (byte) 0x3d, (byte) 0xf8, (byte) 0x3d, (byte) 0x4e, (byte) 0x0b, // 118
1898            (byte) 0x1c, (byte) 0xdb, (byte) 0x02, (byte) 0x41, (byte) 0x00,
1899            (byte) 0x82, (byte) 0x9b, (byte) 0x8a, (byte) 0xfd, (byte) 0xa1,
1900            (byte) 0x98, (byte) 0x41, (byte) 0x68, (byte) 0xc2, (byte) 0xd1,
1901            (byte) 0xdf, (byte) 0x4e, (byte) 0xf3, (byte) 0x2e, (byte) 0x26,
1902            (byte) 0x53, (byte) 0x5b, (byte) 0x31, (byte) 0xb1, (byte) 0x7a,
1903            (byte) 0xcc, (byte) 0x5e, (byte) 0xbb, (byte) 0x09, (byte) 0xa2,
1904            (byte) 0xe2, (byte) 0x6f, (byte) 0x4a, (byte) 0x04, (byte) 0x0d,
1905            (byte) 0xef, (byte) 0x90, (byte) 0x15, (byte) 0xbe, (byte) 0x10,
1906            (byte) 0x4a, (byte) 0xac, (byte) 0x92, (byte) 0xeb, (byte) 0xda,
1907            (byte) 0x72, (byte) 0xdb, (byte) 0x43, (byte) 0x08, (byte) 0xb7,
1908            (byte) 0x2b, (byte) 0x4c, (byte) 0xe1, (byte) 0xbb, (byte) 0x58,
1909            (byte) 0xcb, (byte) 0x71, (byte) 0x80, (byte) 0xad, (byte) 0xbc,
1910            (byte) 0xdc, (byte) 0x62, (byte) 0x5e, (byte) 0x3e, (byte) 0xcb,
1911            (byte) 0x92, (byte) 0xda, (byte) 0xf6, (byte) 0xdf, (byte) 0x02,
1912            (byte) 0x40, (byte) 0x4d, (byte) 0x81, (byte) 0x90, (byte) 0xc5,
1913            (byte) 0x77, (byte) 0x30, (byte) 0xb7, (byte) 0x29, (byte) 0x00,
1914            (byte) 0xa8, (byte) 0xf1, (byte) 0xb4, (byte) 0xae, (byte) 0x52,
1915            (byte) 0x63, (byte) 0x00, (byte) 0xb2, // 140
1916            (byte) 0x2d, (byte) 0x3e, (byte) 0x7d, (byte) 0xd6, (byte) 0x4d,
1917            (byte) 0xf9, (byte) 0x8a, (byte) 0xc1, (byte) 0xb1, (byte) 0x98,
1918            (byte) 0x89, (byte) 0x52, (byte) 0x40, (byte) 0x14, (byte) 0x1b,
1919            (byte) 0x0e, (byte) 0x61, (byte) 0x8f, (byte) 0xf4, (byte) 0xbe,
1920            (byte) 0x59, (byte) 0x79, (byte) 0x79, (byte) 0x95, (byte) 0x19,
1921            (byte) 0x5c, (byte) 0x51, (byte) 0x08, (byte) 0x66, (byte) 0xc1,
1922            (byte) 0x42, (byte) 0x30, (byte) 0xb3, (byte) 0x7a, (byte) 0x86,
1923            (byte) 0x9f, (byte) 0x3e, (byte) 0xf5, (byte) 0x19, (byte) 0xa3, // 150
1924            (byte) 0xae, (byte) 0x64, (byte) 0x69, (byte) 0x14, (byte) 0x07,
1925            (byte) 0x50, (byte) 0x97, };
1926}
1927