PageRenderTime 52ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/external/apache-harmony/crypto/src/test/api/java/org/apache/harmony/crypto/tests/javax/crypto/EncryptedPrivateKeyInfoTest.java

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