PageRenderTime 54ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/luni/src/test/java/org/apache/harmony/crypto/tests/javax/crypto/EncryptedPrivateKeyInfoTest.java

https://bitbucket.org/aways/android_libcore
Java | 1234 lines | 621 code | 131 blank | 482 comment | 40 complexity | 1c3f322e56679a3059a0ad62446a1d89 MD5 | raw file
  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. * @author Vladimir N. Molotkov
  19. * @version $Revision$
  20. */
  21. package org.apache.harmony.crypto.tests.javax.crypto;
  22. import java.io.IOException;
  23. import java.security.AlgorithmParameters;
  24. import java.security.AlgorithmParametersSpi;
  25. import java.security.InvalidAlgorithmParameterException;
  26. import java.security.InvalidKeyException;
  27. import java.security.Key;
  28. import java.security.KeyPair;
  29. import java.security.KeyPairGenerator;
  30. import java.security.NoSuchAlgorithmException;
  31. import java.security.NoSuchProviderException;
  32. import java.security.Provider;
  33. import java.security.Security;
  34. import java.security.spec.InvalidKeySpecException;
  35. import java.security.spec.InvalidParameterSpecException;
  36. import java.security.spec.PKCS8EncodedKeySpec;
  37. import java.util.Arrays;
  38. import javax.crypto.BadPaddingException;
  39. import javax.crypto.Cipher;
  40. import javax.crypto.EncryptedPrivateKeyInfo;
  41. import javax.crypto.IllegalBlockSizeException;
  42. import javax.crypto.KeyGenerator;
  43. import javax.crypto.NoSuchPaddingException;
  44. import javax.crypto.SecretKeyFactory;
  45. import javax.crypto.spec.PBEKeySpec;
  46. import javax.crypto.spec.PBEParameterSpec;
  47. import org.apache.harmony.crypto.tests.support.EncryptedPrivateKeyInfoData;
  48. import junit.framework.TestCase;
  49. /**
  50. * Test for EncryptedPrivateKeyInfo class.
  51. *
  52. * All binary data for this test were generated using BEA JRockit j2sdk1.4.2_04
  53. * (http://www.bea.com) with security providers list extended by Bouncy Castle's
  54. * one (http://www.bouncycastle.org)
  55. */
  56. public class EncryptedPrivateKeyInfoTest extends TestCase {
  57. private static final Provider[] provider = Security.getProviders();
  58. /**
  59. * Algorithm names/transformations used in roundtrip tests of
  60. * getKeySpec(...) methods
  61. */
  62. private static final String[][] algName = {
  63. // AES
  64. { "AES", null},
  65. // {"AES", "AES/ECB/PKCS5Padding"},
  66. // {"AES", "AES/CBC/PKCS5Padding"},
  67. // {"AES", "AES/OFB/PKCS5Padding"},
  68. // {"AES", "AES/CFB/PKCS5Padding"},
  69. // {"2.16.840.1.101.3.4.1.1", null},
  70. // {"2.16.840.1.101.3.4.1.2", null},
  71. // {"2.16.840.1.101.3.4.1.3", null},
  72. // {"2.16.840.1.101.3.4.1.4", null},
  73. // {"2.16.840.1.101.3.4.1.5", null},
  74. // {"2.16.840.1.101.3.4.1.21", null},
  75. // {"2.16.840.1.101.3.4.1.22", null},
  76. // {"2.16.840.1.101.3.4.1.23", null},
  77. // {"2.16.840.1.101.3.4.1.24", null},
  78. // {"2.16.840.1.101.3.4.1.25", null},
  79. // {"2.16.840.1.101.3.4.1.41", null},
  80. // {"2.16.840.1.101.3.4.1.42", null},
  81. // {"2.16.840.1.101.3.4.1.43", null},
  82. // {"2.16.840.1.101.3.4.1.44", null},
  83. // {"2.16.840.1.101.3.4.1.45", null},
  84. // Blowfish
  85. // NO OIDs for Blowfish defined (?)
  86. { "Blowfish", null },
  87. // {"Blowfish","Blowfish/CBC/PKCS5Padding"},
  88. // {"Blowfish","Blowfish/CFB/PKCS5Padding"},
  89. // {"Blowfish","Blowfish/OFB/PKCS5Padding"},
  90. // {"Blowfish","Blowfish/PCBC/PKCS5Padding"},
  91. // DES: OIW OIDs only
  92. // {iso(1) identified-organization(3) oiw(14) secsig(3)
  93. // algorithms(2) desECB(6)}
  94. // 1.3.14.3.2.6
  95. // 1.3.14.3.2.7
  96. // 1.3.14.3.2.8
  97. // 1.3.14.3.2.9
  98. { "DES", null },
  99. // {"DES", "DES/CBC/PKCS5Padding"},
  100. // {"DES","DES/CFB/PKCS5Padding"},
  101. // {"DES","DES/OFB/PKCS5Padding"},
  102. // {"DES","DES/PCBC/PKCS5Padding"},
  103. // DESede (=TripleDes)
  104. //{iso(1) identified-organization(3) oiw(14) secsig(3)
  105. // algorithms(2) desEDE(17)}
  106. // 1.3.14.3.2.17
  107. // {"DESede",null},
  108. // {"DESede","DESede/CBC/PKCS5Padding"},
  109. // {"DESede","DESede/CFB/PKCS5Padding"},
  110. // {"DESede","DESede/OFB/PKCS5Padding"},
  111. // {"DESede","DESede/PCBC/PKCS5Padding"},
  112. { "TripleDES", null },
  113. // {"TripleDES","TripleDES/CBC/PKCS5Padding"},
  114. // {"TripleDES","TripleDES/CFB/PKCS5Padding"},
  115. // {"TripleDES","TripleDES/OFB/PKCS5Padding"},
  116. // {"TripleDES","TripleDES/PCBC/PKCS5Padding"},
  117. // PBEWith<digest>And<encryption>
  118. { "PBEWithMD5AndTripleDES", null },
  119. // {iso(1) member-body(2) us(840) rsadsi(113549) pkcs(1) pkcs-5(5)
  120. // pbeWithMD5AndDES-CBC(3)}
  121. { "PBEWithMD5AndDES", "PBEWithMD5AndDES/CBC/PKCS5Padding", "PBEWithMD5AndDES"},
  122. { "PBEWithMD5AndDES", null, "PBEWithMD5AndDES"}, { "PBEWithHmacSHA1AndDESede", null },
  123. // more oids:
  124. // {iso(1) member-body(2) us(840) nortelnetworks(113533) entrust(7)
  125. // algorithms(66) pbeWithMD5AndCAST5-CBC(12)}
  126. //
  127. // also named pbeWithSHAAnd128BitRC4, pbeWithSHA1And128BitRC4:
  128. // {iso(1) member-body(2) us(840) rsadsi(113549) pkcs(1) pkcs-12(12)
  129. // pkcs-12-PbeIds(1) pkcs-12-OfflineTransportMode(1)}
  130. //
  131. // {iso(1) member-body(2) us(840) rsadsi(113549) pkcs(1) pkcs-12(12)
  132. // pkcs-12-PbeIds(1)} +
  133. // pbeWithSHAAnd40BitRC4(2) pbeWithSHAAnd3-KeyTripleDES-CBC(3)
  134. // pbeWithSHAAnd2-KeyTripleDES-CBC(4) pbeWithSHAAnd128BitRC2-CBC(5)
  135. // pbeWithSHAAnd40BitRC2-CBC(6)
  136. // DiffieHellman
  137. { "DiffieHellman", null }, // 1.2.840.10046.2.1
  138. // {"DH",null}, // 1.2.840.10046.2.1
  139. // {"1.2.840.113549.1.3.1", null},
  140. { "DSA", null }, // 1.2.840.10040.4.1
  141. { "RC2", null },
  142. { "RC4", null },
  143. { "RC5", null },
  144. // {"1.2.840.113549.1.12.1.1",null},
  145. // {"1.2.840.113549.1.12.1.2",null},
  146. //{ "1.2.840.113549.1.12.1.3", null, "PBEWithSHA1AndDESede"},
  147. //{ "PBEWithSHA1AndDESede", null, "PBEWithSHA1AndDESede"},
  148. // {"1.2.840.113549.1.12.1.4",null},
  149. // {"1.2.840.113549.1.12.1.5",null},
  150. // {"1.2.840.113549.1.12.1.6",null},
  151. // {"ELGAMAL/PKCS1", "ELGAMAL/ECB/PKCS1PADDING"},
  152. // {"ELGAMAL/PKCS1","ELGAMAL/NONE/PKCS1PADDING"},
  153. // {"PBEWITHSHAAND3-KEYTRIPLEDES-CBC", null},
  154. // {"PBEWITHSHA1ANDDESEDE", null},
  155. // {"PBEWithSHAAnd3KeyTripleDES",null},
  156. // {"PBEWITHSHAAND3-KEYTRIPLEDES-CBC",null},
  157. //
  158. // {"RC5-32",null},
  159. //
  160. // {"RSA/1", "RSA/1/PKCS1PADDING"},
  161. // {"RSA/2", "RSA/2/PKCS1PADDING"},
  162. // {"RSA/ISO9796-1", "RSA/ECB/ISO9796-1PADDING"},
  163. // {"RSA", "RSA/ECB/NOPADDING"},
  164. // {"RSA/OAEP", "RSA/ECB/OAEPPADDING"},
  165. // {"RSA/PKCS1", "RSA/ECB/PKCS1PADDING"},
  166. // {"RSA/ISO9796-1", "RSA/NONE/ISO9796-1PADDING"},
  167. // {"RSA", "RSA/NONE/NOPADDING"},
  168. // {"RSA/OAEP", "RSA/NONE/OAEPPADDING"},
  169. // {"RSA/PKCS1", "RSA/NONE/PKCS1PADDING"},
  170. // {"RSA",null}, // 1.2.840.113549.1.1.1
  171. // {"1.2.840.113549.1.1.1", null},
  172. };
  173. @Override protected void setUp() throws Exception {
  174. super.setUp();
  175. }
  176. public void test_getAlgName () {
  177. boolean performed = false;
  178. for (int i = 0; i < algName.length; i++) {
  179. try {
  180. // generate test data
  181. TestDataGenerator g = new TestDataGenerator(algName[i][0],
  182. algName[i][1], privateKeyInfoDamaged, null);
  183. // create test object
  184. EncryptedPrivateKeyInfo epki;
  185. if (g.ap() == null) {
  186. epki = new EncryptedPrivateKeyInfo(algName[i][0], g.ct());
  187. } else {
  188. epki = new EncryptedPrivateKeyInfo(g.ap(), g.ct());
  189. }
  190. // call methods under test
  191. if (algName[i].length == 3) {
  192. assertEquals(algName[i][2], epki.getAlgName());
  193. }
  194. performed = true;
  195. } catch (TestDataGenerator.AllowedFailure allowedFailure) {
  196. } catch (NoSuchAlgorithmException allowedFailure) {
  197. }
  198. }
  199. assertTrue("Test not performed", performed);
  200. }
  201. /**
  202. * Test #1 for <code>EncryptedPrivateKeyInfo(byte[])</code> constructor
  203. * <br>
  204. * Assertion: creates <code>EncryptedPrivateKeyInfo</code> instance <br>
  205. * Test preconditions: valid parameters passed <br>
  206. * Expected: must pass without any exceptions
  207. *
  208. * @throws IOException
  209. * @throws NoSuchAlgorithmException
  210. */
  211. public final void testEncryptedPrivateKeyInfobyteArray1() throws Exception {
  212. new EncryptedPrivateKeyInfo(EncryptedPrivateKeyInfoData
  213. .getValidEncryptedPrivateKeyInfoEncoding("DH"));
  214. }
  215. /**
  216. * Test #2 for <code>EncryptedPrivateKeyInfo(byte[])</code> constructor
  217. * <br>
  218. * Assertion: <code>NullPointerException</code> if encoding is
  219. * <code>null</code><br>
  220. * Test preconditions: <code>null</code> passed as a parameter <br>
  221. * Expected: <code>NullPointerException</code>
  222. *
  223. * @throws IOException
  224. */
  225. public final void testEncryptedPrivateKeyInfobyteArray2()
  226. throws IOException {
  227. try {
  228. new EncryptedPrivateKeyInfo(null);
  229. fail(getName() + ": NullPointerException has not been thrown");
  230. } catch (NullPointerException ok) {
  231. }
  232. }
  233. /**
  234. * Test #3 for <code>EncryptedPrivateKeyInfo(byte[])</code> constructor
  235. * <br>
  236. * Assertion: <code>IOException</code> if encoding is wrong <br>
  237. * Test preconditions: wrong encoding passed as a parameter <br>
  238. * Expected: <code>IOException</code>
  239. */
  240. public final void testEncryptedPrivateKeyInfobyteArray3() {
  241. try {
  242. new EncryptedPrivateKeyInfo(new byte[0]);
  243. fail(getName() + ": IOException has not been thrown");
  244. } catch (IOException ok) {
  245. }
  246. }
  247. /**
  248. * Test #4 for <code>EncryptedPrivateKeyInfo(byte[])</code> constructor
  249. * <br>
  250. * Assertion: <code>IOException</code> if encoding is wrong <br>
  251. * Test preconditions: wrong encoding passed as a parameter <br>
  252. * Expected: <code>IOException</code>
  253. */
  254. public final void testEncryptedPrivateKeyInfobyteArray4() {
  255. try {
  256. new EncryptedPrivateKeyInfo(new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9,
  257. 10 });
  258. fail(getName() + ": IOException has not been thrown");
  259. } catch (IOException ok) {
  260. }
  261. }
  262. /**
  263. * Test #5 for <code>EncryptedPrivateKeyInfo(byte[])</code> constructor
  264. * <br>
  265. * Assertion: <code>IOException</code> if encoding is wrong <br>
  266. * Test preconditions: wrong encoding passed as a parameter <br>
  267. * Expected: <code>IOException</code>
  268. */
  269. public final void testEncryptedPrivateKeyInfobyteArray5() throws Exception {
  270. byte[] enc = null;
  271. try {
  272. // 1: get valid encoding
  273. enc = EncryptedPrivateKeyInfoData
  274. .getValidEncryptedPrivateKeyInfoEncoding("DSA");
  275. // ... and corrupt it (set wrong alg OID length)
  276. enc[9] = (byte) 6;
  277. new EncryptedPrivateKeyInfo(enc);
  278. fail(getName() + "(1): IOException has not been thrown");
  279. } catch (IOException ok) {
  280. }
  281. try {
  282. // 2: get valid encoding
  283. enc = EncryptedPrivateKeyInfoData
  284. .getValidEncryptedPrivateKeyInfoEncoding("DSA");
  285. // ... and corrupt it (set wrong encrypted data tag)
  286. enc[307] = (byte) 6;
  287. new EncryptedPrivateKeyInfo(enc);
  288. fail(getName() + "(2): IOException has not been thrown");
  289. } catch (IOException ok) {
  290. }
  291. try {
  292. // 3: get valid encoding
  293. enc = EncryptedPrivateKeyInfoData
  294. .getValidEncryptedPrivateKeyInfoEncoding("DSA");
  295. // ... and corrupt it (set wrong encrypted data length)
  296. enc[310] = (byte) 1;
  297. new EncryptedPrivateKeyInfo(enc);
  298. fail(getName() + "(3): IOException has not been thrown");
  299. } catch (IOException ok) {
  300. }
  301. try {
  302. // 4: get valid encoding
  303. enc = EncryptedPrivateKeyInfoData
  304. .getValidEncryptedPrivateKeyInfoEncoding("DSA");
  305. // ... and corrupt it (set wrong tag for alg params sequence)
  306. enc[17] = (byte) 0x29;
  307. EncryptedPrivateKeyInfo epki = new EncryptedPrivateKeyInfo(enc);
  308. if (epki.getAlgParameters() == null) {
  309. // This kind of encoding corruption can
  310. // be only determined while AlgorithmParameters
  311. // initialization BUT No AlgorithmParameters instance
  312. // available for algName0[i][0].
  313. // So just skip this sub test
  314. } else {
  315. fail(getName() + "(4): IOException has not been thrown");
  316. }
  317. } catch (IOException ok) {
  318. }
  319. try {
  320. // 5: get valid encoding
  321. enc = EncryptedPrivateKeyInfoData
  322. .getValidEncryptedPrivateKeyInfoEncoding("DSA");
  323. // ... and corrupt it (set wrong length for alg params sequence)
  324. enc[20] = (byte) 0x1d;
  325. new EncryptedPrivateKeyInfo(enc);
  326. fail(getName() + "(5): IOException has not been thrown");
  327. } catch (IOException ok) {
  328. }
  329. try {
  330. // 6: get valid encoding
  331. enc = EncryptedPrivateKeyInfoData
  332. .getValidEncryptedPrivateKeyInfoEncoding("DSA");
  333. // ... and corrupt it (set wrong length for alg params sequence)
  334. enc[20] = (byte) 0x1f;
  335. new EncryptedPrivateKeyInfo(enc);
  336. fail(getName() + "(6): IOException has not been thrown");
  337. } catch (IOException ok) {
  338. }
  339. }
  340. /**
  341. * Test #6 for <code>EncryptedPrivateKeyInfo(byte[])</code> constructor
  342. * <br>
  343. * Assertion: byte array is copied to prevent subsequent modification <br>
  344. * Test preconditions: valid array passed then modified <br>
  345. * Expected: getEncoded(), invoked after above modification, must return
  346. * array as it was before the modification
  347. *
  348. * @throws IOException
  349. */
  350. public final void testEncryptedPrivateKeyInfobyteArray6() throws Exception {
  351. byte[] encoded = EncryptedPrivateKeyInfoData
  352. .getValidEncryptedPrivateKeyInfoEncoding("DSA");
  353. byte[] encodedCopy = encoded.clone();
  354. // pass valid array
  355. EncryptedPrivateKeyInfo epki = new EncryptedPrivateKeyInfo(encodedCopy);
  356. // modify array passed
  357. encodedCopy[9] = (byte) 6;
  358. // check that internal state has not been affected
  359. assertTrue(Arrays.equals(encoded, epki.getEncoded()));
  360. }
  361. /**
  362. * Test #1 for <code>EncryptedPrivateKeyInfo(String, byte[])</code>
  363. * constructor <br>
  364. * Assertion: creates <code>EncryptedPrivateKeyInfo</code> instance <br>
  365. * Test preconditions: valid parameters passed <br>
  366. * Expected: must pass without any exceptions
  367. */
  368. public final void testEncryptedPrivateKeyInfoStringbyteArray1() {
  369. boolean performed = false;
  370. for (int i = 0; i < EncryptedPrivateKeyInfoData.algName0.length; i++) {
  371. try {
  372. new EncryptedPrivateKeyInfo(
  373. EncryptedPrivateKeyInfoData.algName0[i][0],
  374. EncryptedPrivateKeyInfoData.encryptedData);
  375. performed = true;
  376. } catch (NoSuchAlgorithmException allowed) {
  377. }
  378. }
  379. assertTrue("Test not performed", performed);
  380. }
  381. /**
  382. * Test #2 for <code>EncryptedPrivateKeyInfo(String, byte[])</code>
  383. * constructor <br>
  384. * Assertion: <code>NoSuchAlgorithmException</code>- if the specified
  385. * algorithm is not supported <br>
  386. * Test preconditions: pass nonexistent algorithm name <br>
  387. * Expected: <code>NoSuchAlgorithmException</code>
  388. */
  389. public final void testEncryptedPrivateKeyInfoStringbyteArray2() {
  390. try {
  391. new EncryptedPrivateKeyInfo("bla-bla",
  392. EncryptedPrivateKeyInfoData.encryptedData);
  393. fail(getName() + ": NoSuchAlgorithmException has not been thrown");
  394. } catch (NoSuchAlgorithmException ok) {
  395. }
  396. try {
  397. new EncryptedPrivateKeyInfo("",
  398. EncryptedPrivateKeyInfoData.encryptedData);
  399. fail(getName() + ": NoSuchAlgorithmException has not been thrown");
  400. } catch (NoSuchAlgorithmException ok) {
  401. }
  402. }
  403. /**
  404. * Test #3 for <code>EncryptedPrivateKeyInfo(String, byte[])</code>
  405. * constructor <br>
  406. * Assertion: <code>NullPointerException</code>- if the specified
  407. * algorithm or encrypted data is <code>null</code><br>
  408. * Test preconditions: pass <code>null</code> as algorithm name then as
  409. * encrypted data <br>
  410. * Expected: <code>NullPointerException</code> in both cases
  411. *
  412. * @throws NoSuchAlgorithmException
  413. */
  414. public final void testEncryptedPrivateKeyInfoStringbyteArray3()
  415. throws NoSuchAlgorithmException {
  416. // pass null as name
  417. try {
  418. new EncryptedPrivateKeyInfo((String) null,
  419. EncryptedPrivateKeyInfoData.encryptedData);
  420. fail(getName() + ": NullPointerException has not been thrown");
  421. } catch (NullPointerException ok) {
  422. }
  423. // pass null as encrypted data
  424. try {
  425. new EncryptedPrivateKeyInfo("DSA", null);
  426. fail(getName() + ": NullPointerException has not been thrown");
  427. } catch (NullPointerException ok) {
  428. }
  429. }
  430. /**
  431. * Test #4 for <code>EncryptedPrivateKeyInfo(String, byte[])</code>
  432. * constructor <br>
  433. * Assertion: <code>IllegalArgumentException</code>- if encrypted data is
  434. * empty, i.e. 0-length <br>
  435. * Test preconditions: pass empty encrypted data <br>
  436. * Expected: <code>IllegalArgumentException</code>
  437. */
  438. public final void testEncryptedPrivateKeyInfoStringbyteArray4()
  439. throws Exception {
  440. try {
  441. new EncryptedPrivateKeyInfo("DSA", new byte[] {});
  442. fail(getName() + ": IllegalArgumentException has not been thrown");
  443. } catch (IllegalArgumentException ok) {
  444. }
  445. }
  446. /**
  447. * Test #5 for <code>EncryptedPrivateKeyInfo(String, byte[])</code>
  448. * constructor <br>
  449. * Assertion: byte array is copied to prevent subsequent modification <br>
  450. * Test preconditions: valid array passed then modified <br>
  451. * Expected: getEncryptedData(), invoked after above modification, must
  452. * return array as it was before the modification
  453. *
  454. * @throws IOException
  455. */
  456. public final void testEncryptedPrivateKeyInfoStringbyteArray5()
  457. throws Exception {
  458. byte[] encryptedDataCopy = EncryptedPrivateKeyInfoData.encryptedData
  459. .clone();
  460. // pass valid array
  461. EncryptedPrivateKeyInfo epki = new EncryptedPrivateKeyInfo("DSA",
  462. encryptedDataCopy);
  463. // modify array passed
  464. encryptedDataCopy[0] = (byte) 6;
  465. // check that internal state has not been affected
  466. assertTrue(Arrays.equals(EncryptedPrivateKeyInfoData.encryptedData,
  467. epki.getEncryptedData()));
  468. }
  469. /**
  470. * javax/crypto/EncryptedPrivateKeyInfo(String, byte[])
  471. * Checks exception order
  472. */
  473. public final void testEncryptedPrivateKeyInfoStringbyteArray6() {
  474. //Regression for HARMONY-768
  475. try {
  476. new EncryptedPrivateKeyInfo("0", new byte[] {});
  477. fail("NoSuchAlgorithmException expected");
  478. } catch (NoSuchAlgorithmException e) {
  479. //expected
  480. }
  481. }
  482. class Mock_AlgorithmParameters extends AlgorithmParameters {
  483. protected Mock_AlgorithmParameters(AlgorithmParametersSpi paramSpi, Provider provider, String algorithm) {
  484. super(paramSpi, provider, algorithm);
  485. }
  486. }
  487. /**
  488. * Test #1 for
  489. * <code>EncryptedPrivateKeyInfo(java.security.AlgorithmParameters, byte[])
  490. * </code>
  491. * constructor <br>
  492. * Assertion: creates <code>EncryptedPrivateKeyInfo</code> instance <br>
  493. * Test preconditions: valid parameters passed <br>
  494. * Expected: must pass without any exceptions
  495. *
  496. * @throws IOException
  497. * @throws NoSuchAlgorithmException
  498. */
  499. public final void testEncryptedPrivateKeyInfoAlgorithmParametersbyteArray1()
  500. throws IOException, NoSuchAlgorithmException {
  501. AlgorithmParameters ap = null;
  502. boolean performed = false;
  503. for (int i = 0; i < EncryptedPrivateKeyInfoData.algName0.length; i++) {
  504. try {
  505. ap = AlgorithmParameters
  506. .getInstance(EncryptedPrivateKeyInfoData.algName0[i][0]);
  507. // use pregenerated AlgorithmParameters encodings
  508. ap.init(EncryptedPrivateKeyInfoData.getParametersEncoding(
  509. EncryptedPrivateKeyInfoData.algName0[i][0]));
  510. new EncryptedPrivateKeyInfo(ap,
  511. EncryptedPrivateKeyInfoData.encryptedData);
  512. performed = true;
  513. } catch (NoSuchAlgorithmException allowedFailure) {
  514. }
  515. }
  516. assertTrue("Test not performed", performed);
  517. ap = new Mock_AlgorithmParameters(null, null, "Wrong alg name");
  518. try {
  519. new EncryptedPrivateKeyInfo(ap,
  520. EncryptedPrivateKeyInfoData.encryptedData);
  521. fail("NoSuchAlgorithmException expected");
  522. } catch (NoSuchAlgorithmException e) {
  523. //expected
  524. }
  525. }
  526. /**
  527. * Test #2 for
  528. * <code>EncryptedPrivateKeyInfo(java.security.AlgorithmParameters, byte[])
  529. * </code>
  530. * constructor <br>
  531. * Assertion: <code>NullPointerException</code>- if the specified
  532. * algorithm parameters or encrypted data is <code>null</code><br>
  533. * Test preconditions: pass <code>null</code> as algorithm parameters then
  534. * as encrypted data <br>
  535. * Expected: <code>NullPointerException</code> in both cases
  536. *
  537. * @throws NoSuchAlgorithmException
  538. * @throws IOException
  539. */
  540. public final void testEncryptedPrivateKeyInfoAlgorithmParametersbyteArray2()
  541. throws NoSuchAlgorithmException, IOException {
  542. // 1: pass null as AlgorithmParameters
  543. try {
  544. new EncryptedPrivateKeyInfo((AlgorithmParameters) null,
  545. EncryptedPrivateKeyInfoData.encryptedData);
  546. fail(getName() + ": NullPointerException has not been thrown");
  547. } catch (NullPointerException ok) {
  548. }
  549. // 2: pass null as encrypted data
  550. try {
  551. AlgorithmParameters ap = AlgorithmParameters.getInstance("DSA");
  552. // use pregenerated AlgorithmParameters encodings
  553. ap.init(EncryptedPrivateKeyInfoData.getParametersEncoding("DSA"));
  554. new EncryptedPrivateKeyInfo(ap, null);
  555. fail(getName() + ": NullPointerException has not been thrown");
  556. } catch (NullPointerException ok) {
  557. }
  558. }
  559. /**
  560. * Test #3 for
  561. * <code>EncryptedPrivateKeyInfo(java.security.AlgorithmParameters, byte[])
  562. * </code>
  563. * constructor <br>
  564. * Assertion: <code>IllegalArgumentException</code>- if encrypted data is
  565. * empty, i.e. 0-length <br>
  566. * Test preconditions: pass empty encrypted data <br>
  567. * Expected: <code>IllegalArgumentException</code>
  568. *
  569. * @throws NoSuchAlgorithmException
  570. * @throws IOException
  571. */
  572. public final void testEncryptedPrivateKeyInfoAlgorithmParametersbyteArray3()
  573. throws Exception {
  574. try {
  575. AlgorithmParameters ap = AlgorithmParameters.getInstance("DSA");
  576. // use pregenerated AlgorithmParameters encodings
  577. ap.init(EncryptedPrivateKeyInfoData.getParametersEncoding("DSA"));
  578. new EncryptedPrivateKeyInfo(ap, new byte[] {});
  579. fail(getName() + ": IllegalArgumentException has not been thrown");
  580. } catch (IllegalArgumentException ok) {
  581. }
  582. }
  583. /**
  584. * Test #4 for
  585. * <code>EncryptedPrivateKeyInfo(java.security.AlgorithmParameters, byte[])
  586. * </code>
  587. * constructor <br>
  588. * Assertion: byte array is copied to prevent subsequent modification <br>
  589. * Test preconditions: valid array passed then modified <br>
  590. * Expected: getEncryptedData(), invoked after above modification, must
  591. * return array as it was before the modification
  592. *
  593. * @throws IOException
  594. */
  595. public final void testEncryptedPrivateKeyInfoAlgorithmParametersbyteArray4()
  596. throws Exception {
  597. AlgorithmParameters ap = AlgorithmParameters.getInstance("DSA");
  598. // use pregenerated AlgorithmParameters encodings
  599. ap.init(EncryptedPrivateKeyInfoData.getParametersEncoding("DSA"));
  600. byte[] encryptedDataCopy = EncryptedPrivateKeyInfoData.encryptedData.clone();
  601. // pass valid array
  602. EncryptedPrivateKeyInfo epki = new EncryptedPrivateKeyInfo(ap,
  603. encryptedDataCopy);
  604. // modify array passed
  605. encryptedDataCopy[0] = (byte) 6;
  606. // check that internal state has not been affected
  607. assertTrue(Arrays.equals(EncryptedPrivateKeyInfoData.encryptedData,
  608. epki.getEncryptedData()));
  609. }
  610. /**
  611. * Test #1 for <code>getAlgParameters()</code> method <br>
  612. * Assertion: returns the algorithm parameters <br>
  613. * Test preconditions: test object created using ctor which takes encoded
  614. * form as the only parameter; encoded form passed contains algorithm
  615. * parameters encoding <br>
  616. * Expected: corresponding algorithm parameters must be returned
  617. *
  618. * @throws IOException
  619. */
  620. public final void testGetAlgParameters01() throws IOException {
  621. boolean performed = false;
  622. for (int i = 0; i < EncryptedPrivateKeyInfoData.algName0.length; i++) {
  623. try {
  624. EncryptedPrivateKeyInfo epki = new EncryptedPrivateKeyInfo(
  625. EncryptedPrivateKeyInfoData
  626. .getValidEncryptedPrivateKeyInfoEncoding(
  627. EncryptedPrivateKeyInfoData.algName0[i][0]));
  628. AlgorithmParameters apar = epki.getAlgParameters();
  629. if (apar == null) {
  630. continue;
  631. }
  632. // check that method under test returns
  633. // parameters with the same encoded form
  634. assertTrue(Arrays
  635. .equals(
  636. EncryptedPrivateKeyInfoData
  637. .getParametersEncoding(EncryptedPrivateKeyInfoData.algName0[i][0]),
  638. apar.getEncoded()));
  639. performed = true;
  640. } catch (NoSuchAlgorithmException allowedFailure) {
  641. }
  642. }
  643. assertTrue("Test not performed", performed);
  644. }
  645. public final void testGetAlgParameters01_01() throws Exception {
  646. byte[] validEncodingWithUnknownAlgOID = EncryptedPrivateKeyInfoData
  647. .getValidEncryptedPrivateKeyInfoEncoding("DH");
  648. // correct oid value
  649. validEncodingWithUnknownAlgOID[18] = 0;
  650. EncryptedPrivateKeyInfo epki = new EncryptedPrivateKeyInfo(
  651. validEncodingWithUnknownAlgOID);
  652. assertNull(epki.getAlgParameters());
  653. }
  654. /**
  655. * Test #2 for <code>getAlgParameters()</code> method <br>
  656. * Assertion: returns the algorithm parameters <br>
  657. * Test preconditions: test object created using ctor which takes encoded
  658. * form as the only parameter; encoded form passed does not contain
  659. * algorithm parameters encoding <br>
  660. * Expected: <code>null</code> must be returned
  661. *
  662. * @throws IOException
  663. */
  664. public final void testGetAlgParameters02() throws IOException {
  665. boolean performed = false;
  666. for (int i = 0; i < EncryptedPrivateKeyInfoData.algName0.length; i++) {
  667. try {
  668. EncryptedPrivateKeyInfo epki = new EncryptedPrivateKeyInfo(
  669. EncryptedPrivateKeyInfoData
  670. .getValidEncryptedPrivateKeyInfoEncoding(
  671. EncryptedPrivateKeyInfoData.algName0[i][0],
  672. false));
  673. // check that method under test returns null
  674. assertNull(epki.getAlgParameters());
  675. performed = true;
  676. } catch (NoSuchAlgorithmException allowedFailure) {
  677. }
  678. }
  679. assertTrue("Test not performed", performed);
  680. }
  681. /**
  682. * Test #3 for <code>getAlgParameters()</code> method <br>
  683. * Assertion: returns the algorithm parameters <br>
  684. * Test #6 for <code>EncryptedPrivateKeyInfo(String, byte[])</code>
  685. * constructor <br>
  686. * Assertion: ...This constructor will use null as the value of the
  687. * algorithm parameters. <br>
  688. * Test preconditions: test object created using ctor which takes algorithm
  689. * name and encrypted data as a parameters <br>
  690. * Expected: <code>null</code> must be returned
  691. *
  692. * @throws IOException
  693. */
  694. public final void testGetAlgParameters03() throws IOException {
  695. boolean performed = false;
  696. for (int i = 0; i < EncryptedPrivateKeyInfoData.algName0.length; i++) {
  697. try {
  698. EncryptedPrivateKeyInfo epki = new EncryptedPrivateKeyInfo(
  699. EncryptedPrivateKeyInfoData.algName0[i][0],
  700. EncryptedPrivateKeyInfoData.encryptedData);
  701. // check that method under test returns null
  702. // for object constructed in such a way
  703. assertNull(epki.getAlgParameters());
  704. performed = true;
  705. } catch (NoSuchAlgorithmException allowedFailure) {
  706. }
  707. }
  708. assertTrue("Test not performed", performed);
  709. }
  710. /**
  711. * Test #4 for <code>getAlgParameters()</code> method <br>
  712. * Assertion: returns the algorithm parameters <br>
  713. * Test preconditions: test object created using ctor which takes
  714. * AlgorithmParameters and encrypted data as a parameters; <br>
  715. * Expected: the same algorithm parameters as ones passed to the ctor must be
  716. * returned
  717. *
  718. * @throws IOException
  719. */
  720. public final void testGetAlgParameters04() throws IOException {
  721. boolean performed = false;
  722. for (int i = 0; i < EncryptedPrivateKeyInfoData.algName0.length; i++) {
  723. try {
  724. AlgorithmParameters ap = AlgorithmParameters
  725. .getInstance(EncryptedPrivateKeyInfoData.algName0[i][0]);
  726. // use pregenerated AlgorithmParameters encodings
  727. ap
  728. .init(EncryptedPrivateKeyInfoData
  729. .getParametersEncoding(
  730. EncryptedPrivateKeyInfoData.algName0[i][0]));
  731. EncryptedPrivateKeyInfo epki = new EncryptedPrivateKeyInfo(ap,
  732. EncryptedPrivateKeyInfoData.encryptedData);
  733. // check that method under test returns
  734. // the same parameters instance
  735. assertSame(ap, epki.getAlgParameters());
  736. performed = true;
  737. } catch (NoSuchAlgorithmException allowedFailure) {
  738. }
  739. }
  740. assertTrue("Test not performed", performed);
  741. }
  742. /**
  743. * Test #1 for <code>getEncryptedData()</code> method <br>
  744. * Assertion: returns the encrypted data <br>
  745. * Test preconditions: test object created using ctor which takes encoded
  746. * form as the only parameter; encoded form passed contains encrypted data
  747. * <br>
  748. * Expected: the equivalent encrypted data must be returned
  749. *
  750. * @throws IOException
  751. */
  752. public final void testGetEncryptedData01() throws IOException {
  753. boolean performed = false;
  754. for (int i = 0; i < EncryptedPrivateKeyInfoData.algName0.length; i++) {
  755. try {
  756. EncryptedPrivateKeyInfo epki = new EncryptedPrivateKeyInfo(
  757. EncryptedPrivateKeyInfoData
  758. .getValidEncryptedPrivateKeyInfoEncoding(
  759. EncryptedPrivateKeyInfoData.algName0[i][0]));
  760. // check that method under test returns
  761. // valid encrypted data
  762. assertTrue(Arrays.equals(
  763. EncryptedPrivateKeyInfoData.encryptedData, epki
  764. .getEncryptedData()));
  765. performed = true;
  766. } catch (NoSuchAlgorithmException allowedFailure) {
  767. }
  768. }
  769. assertTrue("Test not performed", performed);
  770. }
  771. /**
  772. * Test #2 for <code>getEncryptedData()</code> method <br>
  773. * Assertion: returns the encrypted data <br>
  774. * Test preconditions: test object created using ctor which takes algorithm
  775. * name and encrypted data as a parameters <br>
  776. * Expected: the equivalent encrypted data must be returned
  777. */
  778. public final void testGetEncryptedData02() {
  779. boolean performed = false;
  780. for (int i = 0; i < EncryptedPrivateKeyInfoData.algName0.length; i++) {
  781. try {
  782. EncryptedPrivateKeyInfo epki = new EncryptedPrivateKeyInfo(
  783. EncryptedPrivateKeyInfoData.algName0[i][0],
  784. EncryptedPrivateKeyInfoData.encryptedData);
  785. // check that method under test returns
  786. // valid encrypted data
  787. assertTrue(Arrays.equals(
  788. EncryptedPrivateKeyInfoData.encryptedData, epki
  789. .getEncryptedData()));
  790. performed = true;
  791. } catch (NoSuchAlgorithmException allowedFailure) {
  792. }
  793. }
  794. assertTrue("Test not performed", performed);
  795. }
  796. /**
  797. * Test #3 for <code>getEncryptedData()</code> method <br>
  798. * Assertion: returns the encrypted data <br>
  799. * Test preconditions: test object created using ctor which takes algorithm
  800. * parameters and encrypted data as a parameters <br>
  801. * Expected: the equivalent encrypted data must be returned
  802. *
  803. * @throws IOException
  804. */
  805. public final void testGetEncryptedData03() throws IOException {
  806. boolean performed = false;
  807. for (int i = 0; i < EncryptedPrivateKeyInfoData.algName0.length; i++) {
  808. try {
  809. AlgorithmParameters ap = AlgorithmParameters
  810. .getInstance(EncryptedPrivateKeyInfoData.algName0[i][0]);
  811. // use pregenerated AlgorithmParameters encodings
  812. ap.init(EncryptedPrivateKeyInfoData.getParametersEncoding(
  813. EncryptedPrivateKeyInfoData.algName0[i][0]));
  814. EncryptedPrivateKeyInfo epki = new EncryptedPrivateKeyInfo(ap,
  815. EncryptedPrivateKeyInfoData.encryptedData);
  816. // check that method under test returns
  817. // valid encrypted data
  818. assertTrue(Arrays.equals(
  819. EncryptedPrivateKeyInfoData.encryptedData, epki
  820. .getEncryptedData()));
  821. performed = true;
  822. } catch (NoSuchAlgorithmException allowedFailure) {
  823. }
  824. }
  825. assertTrue("Test not performed", performed);
  826. }
  827. /**
  828. * Test #4 for <code>getEncryptedData()</code> method <br>
  829. * Assertion: returns a new array each time this method is called <br>
  830. * Test preconditions: test object created using ctor which takes algorithm
  831. * name and encrypted data as a parameters <br>
  832. * Expected: refs to encrypted data byte array passed to the ctor and
  833. * returned by the method under test must be different
  834. */
  835. public final void testGetEncryptedData04() {
  836. boolean performed = false;
  837. for (int i = 0; i < EncryptedPrivateKeyInfoData.algName0.length; i++) {
  838. try {
  839. EncryptedPrivateKeyInfo epki = new EncryptedPrivateKeyInfo(
  840. EncryptedPrivateKeyInfoData.algName0[i][0],
  841. EncryptedPrivateKeyInfoData.encryptedData);
  842. // check that method under test returns
  843. // new array each time
  844. byte[] ecd1 = epki.getEncryptedData();
  845. byte[] ecd2 = epki.getEncryptedData();
  846. assertNotSame(EncryptedPrivateKeyInfoData.encryptedData, ecd1);
  847. assertNotSame(EncryptedPrivateKeyInfoData.encryptedData, ecd2);
  848. assertNotSame(ecd1, ecd2);
  849. performed = true;
  850. } catch (NoSuchAlgorithmException allowedFailure) {
  851. }
  852. }
  853. assertTrue("Test not performed", performed);
  854. }
  855. /**
  856. * Test #1 for <code>getEncoded()</code> method <br>
  857. * Assertion: returns the ASN.1 encoding of this object <br>
  858. * Test preconditions: test object created using ctor which takes encoded
  859. * form as the only parameter <br>
  860. * Expected: equivalent encoded form must be returned
  861. *
  862. * @throws IOException
  863. */
  864. public final void testGetEncoded01() throws IOException {
  865. boolean performed = false;
  866. for (int i = 0; i < EncryptedPrivateKeyInfoData.algName0.length; i++) {
  867. try {
  868. byte[] enc = EncryptedPrivateKeyInfoData
  869. .getValidEncryptedPrivateKeyInfoEncoding(
  870. EncryptedPrivateKeyInfoData.algName0[i][0]);
  871. EncryptedPrivateKeyInfo epki = new EncryptedPrivateKeyInfo(enc);
  872. // check that method under test returns
  873. // valid encoded form
  874. assertTrue(Arrays.equals(enc, epki.getEncoded()));
  875. performed = true;
  876. } catch (NoSuchAlgorithmException allowedFailure) {
  877. }
  878. }
  879. assertTrue("Test not performed", performed);
  880. }
  881. /**
  882. * Test #2 for <code>getEncoded()</code> method <br>
  883. * Assertion: returns the ASN.1 encoding of this object <br>
  884. * Test preconditions: test object created using ctor which takes algorithm
  885. * name and encrypted data as a parameters <br>
  886. * Expected: equivalent encoded form (without alg params) must be returned
  887. *
  888. * @throws IOException
  889. */
  890. public final void testGetEncoded02() throws IOException {
  891. boolean performed = false;
  892. for (int i = 0; i < EncryptedPrivateKeyInfoData.algName0.length; i++) {
  893. try {
  894. EncryptedPrivateKeyInfo epki = new EncryptedPrivateKeyInfo(
  895. EncryptedPrivateKeyInfoData.algName0[i][0],
  896. EncryptedPrivateKeyInfoData.encryptedData);
  897. // check that method under test returns
  898. // valid encoded form
  899. byte[] refEnc = EncryptedPrivateKeyInfoData
  900. .getValidEncryptedPrivateKeyInfoEncoding(
  901. EncryptedPrivateKeyInfoData.algName0[i][0],
  902. false);
  903. // System.out.println(Array.toString(refEnc, " "));
  904. byte[] actEnc = epki.getEncoded();
  905. // System.out.println(Array.toString(actEnc, " "));
  906. assertTrue(Arrays.equals(refEnc, actEnc));
  907. performed = true;
  908. } catch (NoSuchAlgorithmException allowedFailure) {
  909. }
  910. }
  911. assertTrue("Test not performed", performed);
  912. }
  913. /**
  914. * Test #3 for <code>getEncoded()</code> method <br>
  915. * Assertion: returns the ASN.1 encoding of this object <br>
  916. * Test preconditions: test object created using ctor which takes algorithm
  917. * name and encrypted data as a parameters <br>
  918. * Expected: equivalent encoded form (without alg params) must be returned
  919. *
  920. * @throws IOException
  921. */
  922. public final void testGetEncoded03() throws IOException {
  923. boolean performed = false;
  924. for (int i = 0; i < EncryptedPrivateKeyInfoData.algName0.length; i++) {
  925. try {
  926. AlgorithmParameters ap = AlgorithmParameters
  927. .getInstance(EncryptedPrivateKeyInfoData.algName0[i][0]);
  928. // use pregenerated AlgorithmParameters encodings
  929. ap.init(EncryptedPrivateKeyInfoData.getParametersEncoding(
  930. EncryptedPrivateKeyInfoData.algName0[i][0]));
  931. EncryptedPrivateKeyInfo epki = new EncryptedPrivateKeyInfo(ap,
  932. EncryptedPrivateKeyInfoData.encryptedData);
  933. // check that method under test returns
  934. // valid encoded form
  935. assertTrue(Arrays.equals(
  936. EncryptedPrivateKeyInfoData
  937. .getValidEncryptedPrivateKeyInfoEncoding(
  938. EncryptedPrivateKeyInfoData.algName0[i][0]),
  939. epki.getEncoded()));
  940. performed = true;
  941. } catch (NoSuchAlgorithmException allowedFailure) {
  942. }
  943. }
  944. assertTrue("Test not performed", performed);
  945. }
  946. /**
  947. * Test #4 for <code>getEncoded()</code> method <br>
  948. * Assertion: returns a new array each time this method is called <br>
  949. * Test preconditions: test object created using ctor which takes algorithm
  950. * name and encrypted data as a parameters <br>
  951. * Expected: several refs to byte array returned by the method under test
  952. * must be different
  953. *
  954. * @throws IOException
  955. */
  956. public final void testGetEncoded04() throws IOException {
  957. boolean performed = false;
  958. for (int i = 0; i < EncryptedPrivateKeyInfoData.algName0.length; i++) {
  959. try {
  960. EncryptedPrivateKeyInfo epki = new EncryptedPrivateKeyInfo(
  961. EncryptedPrivateKeyInfoData.algName0[i][0],
  962. EncryptedPrivateKeyInfoData.encryptedData);
  963. // check that method under test returns
  964. // new array each time
  965. byte[] ec1 = epki.getEncoded();
  966. byte[] ec2 = epki.getEncoded();
  967. byte[] ec3 = epki.getEncoded();
  968. assertNotSame(ec1, ec2);
  969. assertNotSame(ec2, ec3);
  970. assertNotSame(ec1, ec3);
  971. performed = true;
  972. } catch (NoSuchAlgorithmException allowedFailure) {
  973. }
  974. }
  975. assertTrue("Test not performed", performed);
  976. }
  977. public final void testGetKeySpecCipher01() {
  978. boolean performed = false;
  979. for (int i = 0; i < EncryptedPrivateKeyInfoData.algName0.length; i++) {
  980. try {
  981. EncryptedPrivateKeyInfo epki = new EncryptedPrivateKeyInfo(
  982. EncryptedPrivateKeyInfoData.algName0[i][0],
  983. EncryptedPrivateKeyInfoData.encryptedData);
  984. try {
  985. // check that method under test throws NPE
  986. epki.getKeySpec((Cipher) null);
  987. fail(getName() + "NullPointerException has not been thrown");
  988. } catch (NullPointerException ok) {
  989. } catch (InvalidKeySpecException e) {
  990. fail(getName() + "Unexpected exception: " + e);
  991. }
  992. performed = true;
  993. } catch (NoSuchAlgorithmException allowedFailure) {
  994. }
  995. }
  996. assertTrue("Test not performed", performed);
  997. }
  998. /**
  999. * Encrypted data contains valid PKCS8 key info encoding
  1000. */
  1001. public final void test_ROUNDTRIP_GetKeySpecCipher01() {
  1002. boolean performed = false;
  1003. for (int i = 0; i < algName.length; i++) {
  1004. try {
  1005. // generate test data
  1006. TestDataGenerator g = new TestDataGenerator(algName[i][0],
  1007. algName[i][1], privateKeyInfo, null);
  1008. // create test object
  1009. EncryptedPrivateKeyInfo epki;
  1010. if (g.ap() == null) {
  1011. epki = new EncryptedPrivateKeyInfo(algName[i][0], g.ct());
  1012. } else {
  1013. epki = new EncryptedPrivateKeyInfo(g.ap(), g.ct());
  1014. }
  1015. // call methods under test
  1016. try {
  1017. PKCS8EncodedKeySpec eks = epki.getKeySpec(g.c());
  1018. if (!Arrays.equals(privateKeyInfo, eks.getEncoded())) {
  1019. fail(algName[i][0] + " != " + algName[i][1]);
  1020. }
  1021. } catch (InvalidKeySpecException e) {
  1022. fail(algName[i][0] + ", " + algName[i][1] + e + "\n");
  1023. }
  1024. performed = true;
  1025. } catch (TestDataGenerator.AllowedFailure allowedFailure) {
  1026. } catch (NoSuchAlgorithmException allowed) {
  1027. }
  1028. }
  1029. assertTrue("Test not performed", performed);
  1030. }
  1031. /**
  1032. * Encrypted data contains invalid PKCS8 key info encoding
  1033. */
  1034. public final void test_ROUNDTRIP_GetKeySpecCipher02() {
  1035. boolean performed = false;
  1036. for (int i = 0; i < algName.length; i++) {
  1037. try {
  1038. // generate test data
  1039. TestDataGenerator g = new TestDataGenerator(algName[i][0],
  1040. algName[i][1], privateKeyInfoDamaged, null);
  1041. // create test object
  1042. EncryptedPrivateKeyInfo epki;
  1043. if (g.ap() == null) {
  1044. epki = new EncryptedPrivateKeyInfo(algName[i][0], g.ct());
  1045. } else {
  1046. epki = new EncryptedPrivateKeyInfo(g.ap(), g.ct());
  1047. }
  1048. // call methods under test
  1049. try {
  1050. epki.getKeySpec(g.c());
  1051. // must not get here because decrypted data does
  1052. // not represent valid PKCS8 encoding
  1053. fail(algName[i][0] + ", " + algName[i][1]);
  1054. } catch (InvalidKeySpecException ok) {
  1055. }
  1056. performed = true;
  1057. } catch (TestDataGenerator.AllowedFailure allowedFailure) {
  1058. } catch (NoSuchAlgorithmException allowedFailure) {
  1059. }
  1060. }
  1061. assertTrue("Test not performed", performed);
  1062. }
  1063. public final void testGetKeySpecKey01() {
  1064. boolean performed = false;
  1065. for (int i = 0; i < EncryptedPrivateKeyInfoData.algName0.length; i++) {
  1066. try {
  1067. EncryptedPrivateKeyInfo epki = new EncryptedPrivateKeyInfo(
  1068. EncryptedPrivateKeyInfoData.algName0[i][0],
  1069. EncryptedPrivateKeyInfoData.encryptedData);
  1070. try {
  1071. // check that method under test throws NPE
  1072. epki.getKeySpec((Key) null);
  1073. fail(getName() + "NullPointerException has not been thrown");
  1074. } catch (NullPointerException ok) {
  1075. } catch (InvalidKeyException e) {
  1076. fail(getName() + "Unexpected exception: " + e);
  1077. }
  1078. performed = true;
  1079. } catch (NoSuchAlgorithmException allowedFailure) {
  1080. }
  1081. }
  1082. assertTrue("Test not performed", performed);
  1083. }
  1084. /**
  1085. * Encrypted data contains valid PKCS8 key info encoding
  1086. */
  1087. public final void test_ROUNDTRIP_GetKeySpecKey01() {
  1088. boolean performed = false;
  1089. for (int i = 0; i < algName.length; i++) {
  1090. try {
  1091. // generate test data
  1092. TestDataGenerator g = new TestDataGenerator(algName[i][0],
  1093. algName[i][1], privateKeyInfo, null);
  1094. // create test object
  1095. EncryptedPrivateKeyInfo epki;
  1096. if (g.ap() == null) {
  1097. epki = new EncryptedPrivateKeyInfo(algName[i][0], g.ct());
  1098. } else {
  1099. epki = new EncryptedPrivateKeyInfo(g.ap(), g.ct());
  1100. }
  1101. try {
  1102. PKCS8EncodedKeySpec eks = epki
  1103. .