DSAKeyFactoryImplTest.java revision 561ee011997c6c2f1befbfaa9d5f0a99771c1d63
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 19package org.apache.harmony.security.tests.provider.crypto; 20 21 22import java.math.BigInteger; 23 24import java.security.InvalidKeyException; 25import java.security.Key; 26import java.security.KeyFactory; 27import java.security.KeyPairGenerator; 28import java.security.KeyPair; 29 30import java.security.interfaces.DSAParams; 31import java.security.interfaces.DSAPrivateKey; 32import java.security.interfaces.DSAPublicKey; 33import java.security.interfaces.RSAPrivateKey; 34import java.security.interfaces.RSAPublicKey; 35 36import java.security.spec.DSAParameterSpec; 37import java.security.spec.KeySpec; 38import java.security.spec.DSAPublicKeySpec; 39import java.security.spec.DSAPrivateKeySpec; 40import java.security.spec.X509EncodedKeySpec; 41import java.security.spec.PKCS8EncodedKeySpec; 42import java.security.spec.InvalidKeySpecException; 43 44import java.util.Arrays; 45 46import junit.framework.Test; 47import junit.framework.TestCase; 48import junit.framework.TestSuite; 49 50 51public class DSAKeyFactoryImplTest extends TestCase { 52 53 54 static KeyFactory kf; 55 56 static String algorithm = "DSA"; 57 58 static KeyPairGenerator kpGen; 59 60 static KeyPair keyPair; 61 62 static DSAPublicKey publicKey; 63 static DSAPrivateKey privateKey; 64 65 static DSAParams publicParams; 66 67 static BigInteger publicP; 68 static BigInteger publicQ; 69 static BigInteger publicG; 70 static BigInteger publicY; 71 72 static String publicAlgorithm; 73 static String publicFormat; 74 75 static byte[] publicEncoding; 76 77 static KeySpec x509KeySpec; 78 79 80 static DSAParams privateParams; 81 82 static BigInteger privateP; 83 static BigInteger privateQ; 84 static BigInteger privateG; 85 static BigInteger privateX; 86 87 static String privateAlgorithm; 88 static String privateFormat; 89 90 static byte[] privateEncoding; 91 92 static KeySpec pkcs8KeySpec; 93 94 95 static RSAPrivateKey privateRSAKey; 96 static RSAPublicKey publicRSAKey; 97 98 static Key keyPublic; 99 static Key keyPrivate; 100 101 private static boolean flag; 102 private static Exception reason; 103 104 static { 105 try { 106 kpGen = KeyPairGenerator.getInstance(algorithm); 107 108 keyPair = kpGen.generateKeyPair(); 109 110 publicKey = (DSAPublicKey) keyPair.getPublic(); 111 privateKey = (DSAPrivateKey) keyPair.getPrivate(); 112 113 publicParams = publicKey.getParams(); 114 115 publicP = publicParams.getP(); 116 publicQ = publicParams.getQ(); 117 publicG = publicParams.getG(); 118 publicY = publicKey.getY(); 119 120 publicAlgorithm = publicKey.getAlgorithm(); 121 publicFormat = publicKey.getFormat(); 122 123 publicEncoding = publicKey.getEncoded(); 124 125 x509KeySpec = (KeySpec) new X509EncodedKeySpec(publicEncoding); 126 127 128 privateParams = privateKey.getParams(); 129 130 privateP = privateParams.getP(); 131 privateQ = privateParams.getQ(); 132 privateG = privateParams.getG(); 133 privateX = privateKey.getX(); 134 135 privateAlgorithm = privateKey.getAlgorithm(); 136 privateFormat = privateKey.getFormat(); 137 138 privateEncoding = privateKey.getEncoded(); 139 140 pkcs8KeySpec = (KeySpec) new PKCS8EncodedKeySpec(privateEncoding); 141 142 143 privateRSAKey = new RSAPrivateKey () { 144 public BigInteger getPrivateExponent() { return BigInteger.ZERO; } 145 public BigInteger getModulus() { return BigInteger.ZERO; } 146 public String getAlgorithm() { return "RSA"; } 147 public byte[] getEncoded() { return new byte[] {0}; } 148 public String getFormat() { return "fff"; } 149 }; 150 151 publicRSAKey = new RSAPublicKey () { 152 public BigInteger getPublicExponent() { return BigInteger.ZERO; } 153 public BigInteger getModulus() { return BigInteger.ZERO; } 154 public String getAlgorithm() { return "RSA"; } 155 public byte[] getEncoded() { return new byte[] {0}; } 156 public String getFormat() { return "fff"; } 157 }; 158 159 keyPublic = new Key() { public String getAlgorithm() { return "DSA"; } 160 public byte[] getEncoded() { return publicEncoding; } 161 public String getFormat() { return "X.509"; } 162 }; 163 keyPrivate = new Key() { public String getAlgorithm() { return "DSA"; } 164 public byte[] getEncoded() { return privateEncoding; } 165 public String getFormat() { return "PKCS#8"; } 166 }; 167 168 flag = true; 169 } catch (Exception e) { 170 flag = false; 171 reason = e; 172 } 173 } 174 175 176 /* 177 * @see TestCase#setUp() 178 */ 179 protected void setUp() throws Exception { 180 181 if (!flag) { 182 throw new Exception("some problem in static initializer : " + reason); 183 } 184 super.setUp(); 185 kf = KeyFactory.getInstance(algorithm, "Crypto"); 186 } 187 188 189 190 private void checkPublicIntegers(DSAPublicKey pk) { 191 192 DSAParams params = pk.getParams(); 193 194 assertEquals("failure for 'pk.getY().compareTo(publicY)'", 0, pk.getY() 195 .compareTo(publicY)); 196 197 assertEquals("failure for 'params.getP().compareTo(publicP)'", 0, 198 params.getP().compareTo(publicP)); 199 assertEquals("failure for 'params.getQ().compareTo(publicQ)'", 0, 200 params.getQ().compareTo(publicQ)); 201 assertEquals("failure for 'params.getG().compareTo(publicG)'", 0, 202 params.getG().compareTo(publicG)); 203 } 204 205 206 private void checkPublicKeys(DSAPublicKey pk) { 207 208 checkPublicIntegers(pk); 209 210 assertEquals( 211 "failure for 'pk.getAlgorithm().compareTo(publicAlgorithm)'", 212 0, pk.getAlgorithm().compareTo(publicAlgorithm)); 213 assertEquals("failure for 'pk.getFormat().compareTo(publicFormat)'", 0, 214 pk.getFormat().compareTo(publicFormat)); 215 216 if ( publicEncoding != null) { 217 assertTrue("failure: encodings are not equal", 218 Arrays.equals(pk.getEncoded(), publicEncoding) ); 219 } 220 } 221 222 223 private void checkPrivateIntegers(DSAPrivateKey pk) { 224 225 DSAParams params = pk.getParams(); 226 227 assertEquals("failure for 'pk.getX().compareTo(privateX)'", 0, pk 228 .getX().compareTo(privateX)); 229 230 assertEquals("failure for 'params.getP().compareTo(privateP)'", 0, 231 params.getP().compareTo(privateP)); 232 assertEquals("failure for 'params.getQ().compareTo(privateQ)'", 0, 233 params.getQ().compareTo(privateQ)); 234 assertEquals("failure for 'params.getG().compareTo(privateG)'", 0, 235 params.getG().compareTo(privateG)); 236 } 237 238 239 private void checkPrivateKeys(DSAPrivateKey pk) { 240 241 checkPrivateIntegers(pk); 242 243 assertEquals( 244 "failure for 'pk.getAlgorithm().compareTo(privateAlgorithm)'", 245 0, pk.getAlgorithm().compareTo(privateAlgorithm)); 246 assertEquals("failure for 'pk.getFormat().compareTo(privateFormat)", 0, 247 pk.getFormat().compareTo(privateFormat)); 248 249 if ( privateEncoding != null) { 250 assertTrue("failure: encodings are not equal", 251 Arrays.equals(pk.getEncoded(), privateEncoding) ); 252 } 253 } 254 255 256 /** 257 * A test against the "generatePublic(KeySpec)" method. 258 * The test checks out that the method throws up InvalidKeySpecException 259 * if argument is neither "X509EncodedKeySpec" nor "DSAPublicKeySpec" 260 */ 261 public final void testGeneratePublicKeySpec01() throws Exception { 262 try { 263 kf.generatePublic(pkcs8KeySpec); 264 fail("testcase1: no InvalidKeySpecException"); 265 } catch (InvalidKeySpecException e) { 266 } 267 try { 268 kf.generatePublic(null); 269 fail("testcase2: no InvalidKeySpecException"); 270 } catch (InvalidKeySpecException e) { 271 } 272 } 273 274 275 /** 276 * A test against the "generatePublic(KeySpec)" method. 277 * The test checks out that the method returns DSAPublicKey 278 * if argument is "X509EncodedKeySpec" 279 */ 280 public final void testGeneratePublicKeySpec02() throws Exception { 281 282 checkPublicKeys( (DSAPublicKey) kf.generatePublic(x509KeySpec) ); 283 } 284 285 286 /** 287 * A test against the "generatePublic(KeySpec)" method. 288 * The test checks out that the method returns DSAPublicKey 289 * if argument is "DSAPublicKeySpec" 290 */ 291 public final void testGeneratePublicKeySpec03() throws Exception { 292 293 KeySpec keySpec = (KeySpec) new DSAPublicKeySpec(publicY, publicP, publicQ, publicG); 294 295 checkPublicKeys( (DSAPublicKey) kf.generatePublic(keySpec) ); 296 } 297 298 299 /** 300 * A test against the "generatePublic(KeySpec)" method. 301 * The test checks out that the method throws up InvalidKeySpecException 302 * if KeySpec argument contains incorrect ASN.1 syntax 303 */ 304 public final void testGeneratePublicKeySpec04() throws Exception { 305 306 X509EncodedKeySpec ks; 307 DSAPublicKey pubKey; 308 309 final BigInteger y = publicY; 310 final BigInteger p = publicP; 311 final BigInteger q = publicQ; 312 final BigInteger g = publicG; 313 314 final byte enc1[] = new byte[20]; 315 System.arraycopy(publicEncoding, 0, enc1, 0, 20); 316 final byte[] enc2 = enc1; 317 318 pubKey = new DSAPublicKey () { 319 320 public BigInteger getY() { return y; } 321 public DSAParams getParams() { 322 return (DSAParams)(new DSAParameterSpec(p, q, g)); 323 } 324 public String getAlgorithm() { return "DSA"; } 325 public byte[] getEncoded() { return enc2; } 326 public String getFormat() { return "X.509"; } 327 }; 328 329 ks = kf.getKeySpec(pubKey, X509EncodedKeySpec.class); 330 try { 331 pubKey = (DSAPublicKey) kf.generatePublic((KeySpec)ks); 332 fail("no InvalidKeySpecException"); 333 } catch (InvalidKeySpecException e) { 334 } 335 } 336 337 338 /** 339 * A compatibility with RI test. 340 * It checks out that if key encoding in KeySpec has correct ASN1 structure 341 * but AlgorithmIdentifier contains value that connot be translated to "DSA" 342 * the "generatePublic" method returns DSA public key 343 * whose algorithm is neither null nor "DSA". 344 */ 345 public final void testGeneratePublicKeySpec05() throws Exception { 346 347 X509EncodedKeySpec ks; 348 DSAPublicKey pubKey; 349 350 final BigInteger y = publicY; 351 final BigInteger p = publicP; 352 final BigInteger q = publicQ; 353 final BigInteger g = publicG; 354 355 final byte enc1[] = new byte[publicEncoding.length]; 356 System.arraycopy(publicEncoding, 0, enc1, 0, publicEncoding.length); 357 enc1[13] = 0; 358 final byte[] enc2 = enc1; 359 360 pubKey = new DSAPublicKey () { 361 362 public BigInteger getY() { return y; } 363 public DSAParams getParams() { 364 return (DSAParams)(new DSAParameterSpec(p, q, g)); 365 } 366 public String getAlgorithm() { return "DSA"; } 367 public byte[] getEncoded() { return enc2; } 368 public String getFormat() { return "X.509"; } 369 }; 370 371 ks = kf.getKeySpec(pubKey, X509EncodedKeySpec.class); 372 pubKey = (DSAPublicKey) kf.generatePublic((KeySpec)ks); 373 374 String alg = pubKey.getAlgorithm(); 375 assertNotNull(alg); 376 assertFalse(alg.equals("DSA")); 377 } 378 379 380 /** 381 * A test against the "generatePrivate(KeySpec)" method. 382 * The test checks out that the method throws up InvalidKeySpecException 383 * if argument is neither "PKCS8EncodedKeySpec" nor "DSAPrivateKeySpec" 384 */ 385 public final void testGeneratePrivateKeySpec01() throws Exception { 386 try { 387 kf.generatePrivate(x509KeySpec); 388 fail("testcase1: no InvalidKeySpecException"); 389 } catch (InvalidKeySpecException e) { 390 } 391 try { 392 kf.generatePrivate(null); 393 fail("testcase2: no InvalidKeySpecException"); 394 } catch (InvalidKeySpecException e) { 395 } 396 } 397 398 399 /** 400 * A test against the "generatePrivate(KeySpec)" method. 401 * The test checks out that the method returns DSAPrivateKey 402 * if argument is "PKCS8EncodedKeySpec" 403 */ 404 public final void testGeneratePrivateKeySpec02() throws Exception { 405 406 checkPrivateKeys( (DSAPrivateKey) kf.generatePrivate(pkcs8KeySpec) ); 407 } 408 409 410 /** 411 * A test against the "generatePrivate(KeySpec)" method. 412 * The test checks out that the method returns DSAPrivateKey 413 * if argument is "DSAPrivateKeySpec" 414 */ 415 public final void testGeneratePrivateKeySpec03() throws Exception { 416 417 KeySpec keySpec = (KeySpec) new DSAPrivateKeySpec(privateX, 418 privateP, privateQ, privateG); 419 420 checkPrivateKeys( (DSAPrivateKey) kf.generatePrivate(keySpec) ); 421 } 422 423 424 /** 425 * A test against the "generatePrivate(KeySpec)" method. 426 * The test checks out that the method throws up InvalidKeySpecException 427 * if KeySpec argument contains incorrect ASN.1 syntax 428 */ 429 public final void testGeneratePrivateKeySpec04() throws Exception { 430 431 PKCS8EncodedKeySpec ks; 432 DSAPrivateKey prKey; 433 434 final BigInteger x = privateX; 435 final BigInteger p = privateP; 436 final BigInteger q = privateQ; 437 final BigInteger g = privateG; 438 439 final byte enc1[] = new byte[20]; 440 System.arraycopy(privateEncoding, 0, enc1, 0, 20); 441 final byte[] enc2 = enc1; 442 443 prKey = new DSAPrivateKey () { 444 445 public BigInteger getX() { return x; } 446 public DSAParams getParams() { 447 return (DSAParams)(new DSAParameterSpec(p, q, g)); 448 } 449 public String getAlgorithm() { return "DSA"; } 450 public byte[] getEncoded() { return enc2; } 451 public String getFormat() { return "PKCS8"; } 452 }; 453 454 ks = kf.getKeySpec(prKey, PKCS8EncodedKeySpec.class); 455 try { 456 prKey = (DSAPrivateKey) kf.generatePrivate((KeySpec)ks); 457 fail("no InvalidKeySpecException"); 458 } catch (InvalidKeySpecException e) { 459 } 460 } 461 462 463 /** 464 * A compatibility with RI test. 465 * It checks out that if key encoding in KeySpec has correct ASN1 structure 466 * but AlgorithmIdentifier contains value that connot be translated to "DSA" 467 * the "generatePrivate" method returns DSA private key 468 * whose algorithm is neither null nor "DSA". 469 */ 470 public final void testGeneratePrivateKeySpec05() throws Exception { 471 472 PKCS8EncodedKeySpec ks; 473 DSAPrivateKey prKey; 474 475 final BigInteger x = privateX; 476 final BigInteger p = privateP; 477 final BigInteger q = privateQ; 478 final BigInteger g = privateG; 479 480 final byte enc1[] = new byte[privateEncoding.length]; 481 System.arraycopy(privateEncoding, 0, enc1, 0, privateEncoding.length); 482 enc1[13] = 0; 483 final byte[] enc2 = enc1; 484 485 prKey = new DSAPrivateKey () { 486 487 public BigInteger getX() { return x; } 488 public DSAParams getParams() { 489 return (DSAParams)(new DSAParameterSpec(p, q, g)); 490 } 491 public String getAlgorithm() { return "DSA"; } 492 public byte[] getEncoded() { return enc2; } 493 public String getFormat() { return "PKCS8"; } 494 }; 495 496 ks = kf.getKeySpec(prKey, PKCS8EncodedKeySpec.class); 497 prKey = (DSAPrivateKey) kf.generatePrivate((KeySpec)ks); 498 499 String alg = prKey.getAlgorithm(); 500 assertNotNull(alg); 501 assertFalse("DSA".equals(alg)); 502 } 503 504 505 /** 506 * A test against the "getKeySpec(Key, Class)" method. 507 * The test checks out that the method throws up InvalidKeySpecException if 508 * a "Class" argument is not appropriate for a "Key" argument 509 * regardless of whether a correct value or null is passed to a Key argument. 510 */ 511 public final void testGetKeySpec01() throws Exception { 512 513 try { 514 kf.getKeySpec( privateKey, DSAPublicKeySpec.class); 515 fail("testcase1: No InvalidKeySpecException"); 516 } catch (InvalidKeySpecException e) { 517 } 518 try { 519 kf.getKeySpec( privateKey, X509EncodedKeySpec.class); 520 fail("testcase2: No InvalidKeySpecException"); 521 } catch (InvalidKeySpecException e) { 522 } 523 try { 524 kf.getKeySpec( null, DSAPublicKeySpec.class); 525 fail("testcase3: No InvalidKeySpecException"); 526 } catch (InvalidKeySpecException e) { 527 } 528 try { 529 kf.getKeySpec( null, X509EncodedKeySpec.class); 530 fail("testcase4: No InvalidKeySpecException"); 531 } catch (InvalidKeySpecException e) { 532 } 533 try { 534 kf.getKeySpec( publicKey, DSAPrivateKeySpec.class); 535 fail("testcase5: No InvalidKeySpecException"); 536 } catch (InvalidKeySpecException e) { 537 } 538 try { 539 kf.getKeySpec( publicKey, PKCS8EncodedKeySpec.class); 540 fail("testcase6: No InvalidKeySpecException"); 541 } catch (InvalidKeySpecException e) { 542 } 543 try { 544 kf.getKeySpec( null, DSAPrivateKeySpec.class); 545 fail("testcase7: No InvalidKeySpecException"); 546 } catch (InvalidKeySpecException e) { 547 } 548 try { 549 kf.getKeySpec( null, PKCS8EncodedKeySpec.class); 550 fail("testcase8: No InvalidKeySpecException"); 551 } catch (InvalidKeySpecException e) { 552 } 553 } 554 555 556 /** 557 * A test against the "getKeySpec(Key, Class)" method. 558 * The test checks out that the method throws up NullPointerException 559 * if null is passed to a "Class" argument. 560 */ 561 public final void testGetKeySpec02() throws Exception { 562 try { 563 kf.getKeySpec(privateKey, null); 564 fail("testcase1: No NullPointerException"); 565 } catch (NullPointerException e) { 566 } 567 try { 568 kf.getKeySpec(publicKey, null); 569 fail("testcase2: No NullPointerException"); 570 } catch (NullPointerException e) { 571 } 572 } 573 574 575 /** 576 * A test against the "getKeySpec(Key, Class)" method. 577 * The test checks out that 578 * 1) a KeySpec returned by the method is being casted to 579 * expected "DSAPublicKeySpec" or "X509EncodedKeySpec", and 580 * 2) DSAPublickey object generated from KeySpec is equal a "publicKey" argument. 581 */ 582 public final void testGetKeySpec03() throws Exception { 583 584 KeySpec ks; 585 586 ks = kf.getKeySpec( publicKey, DSAPublicKeySpec.class); 587 checkPublicKeys( (DSAPublicKey) kf.generatePublic((DSAPublicKeySpec) ks) ); 588 589 ks = kf.getKeySpec( publicKey, X509EncodedKeySpec.class); 590 checkPublicKeys( (DSAPublicKey) kf.generatePublic((X509EncodedKeySpec) ks) ); 591 } 592 593 594 /** 595 * A test against the "getKeySpec(Key, Class)" method. 596 * The test checks out that 597 * 1) a KeySpec returned by the method is being casted to 598 expected "DSAPrivateKeySpec" or "PKCS8EncodedKeySpec", and 599 * 2) DSAPublickey object generated from KeySpec is equal a "privateKey" argument. 600 */ 601 public final void testGetKeySpec04() throws Exception { 602 603 KeySpec ks; 604 605 ks = kf.getKeySpec( privateKey, DSAPrivateKeySpec.class); 606 checkPrivateKeys( (DSAPrivateKey) kf.generatePrivate((DSAPrivateKeySpec) ks) ); 607 608 ks = kf.getKeySpec( privateKey, PKCS8EncodedKeySpec.class); 609 checkPrivateKeys( (DSAPrivateKey) kf.generatePrivate((PKCS8EncodedKeySpec) ks) ); 610 } 611 612 613 /** 614 * Compatibility with RI test. 615 * A test against the "getKeySpec(Key, Class)" method. 616 * It checks out that if Key is DSAPrivateKey having incorrect encoding 617 * the method doesn't throw up InvalidKeySpecException. 618 */ 619 public final void testGetKeySpec05() throws Exception { 620 621 int lng = 20; 622 623 final BigInteger x = privateX; 624 final BigInteger p = privateP; 625 final BigInteger q = privateQ; 626 final BigInteger g = privateG; 627 628 final byte enc1[] = new byte[lng]; 629 System.arraycopy(privateEncoding, 0, enc1, 0, lng); // enc1 contains incorrect encoding 630 631 DSAPrivateKey prKey = new DSAPrivateKey () { 632 633 public BigInteger getX() { return x; } 634 public DSAParams getParams() { 635 return (DSAParams)(new DSAParameterSpec(p, q, g)); 636 } 637 public String getAlgorithm() { return "DSA"; } 638 public byte[] getEncoded() { return enc1; } 639 public String getFormat() { return "PKCS8"; } 640 }; 641 try { 642 kf.getKeySpec(prKey, PKCS8EncodedKeySpec.class); 643 } catch (InvalidKeySpecException e) { 644 fail("InvalidKeySpecException : " + e); 645 } 646 } 647 648 649 /** 650 * Compatibility with RI test. 651 * A test against the "getKeySpec(Key, Class)" method. 652 * It checks out that if Key is DSAPublicKey having incorrect encoding 653 * the method doesn't throw up InvalidKeySpecException 654 */ 655 public final void testGetKeySpec06() throws Exception { 656 657 int lng = 20; 658 659 final BigInteger y = publicY; 660 final BigInteger p = publicP; 661 final BigInteger q = publicQ; 662 final BigInteger g = publicG; 663 664 final byte enc2[] = new byte[lng]; 665 System.arraycopy(publicEncoding, 0, enc2, 0, lng);; // enc2 contains incorrect encoding 666 667 DSAPublicKey pubKey = new DSAPublicKey () { 668 669 public BigInteger getY() { return y; } 670 public DSAParams getParams() { 671 return (DSAParams)(new DSAParameterSpec(p, q, g)); 672 } 673 public String getAlgorithm() { return "DSA"; } 674 public byte[] getEncoded() { return enc2; } 675 public String getFormat() { return "X.509"; } 676 }; 677 678 try { 679 kf.getKeySpec(pubKey, X509EncodedKeySpec.class); 680 } catch (InvalidKeySpecException e) { 681 fail("InvalidKeySpecException : " + e); 682 } 683 684 } 685 686 687 /** 688 * A test against the "translateKey(Key)" method. 689 * The test checks out that the method throws up InvalidKeyException 690 * if argument is not a DSAPublicKey or a DSAPrivateKey 691 */ 692 public final void testTranslateKey01() throws Exception { 693 try { 694 kf.translateKey( (Key) privateRSAKey ); 695 fail("testcase1: No InvalidKeyException"); 696 } catch (InvalidKeyException e) { 697 } 698 try { 699 kf.translateKey( (Key) publicRSAKey ); 700 fail("testcase2: No InvalidKeyException"); 701 } catch (InvalidKeyException e) { 702 } 703 try { 704 kf.translateKey(null); 705 fail("testcase3: No InvalidKeyException"); 706 } catch (InvalidKeyException e) { 707 } 708 try { 709 kf.translateKey(keyPublic); 710 fail("testcase4: No InvalidKeyException"); 711 } catch (InvalidKeyException e) { 712 } 713 try { 714 kf.translateKey(keyPrivate); 715 fail("testcase5: No InvalidKeyException"); 716 } catch (InvalidKeyException e) { 717 } 718 } 719 720 721 /** 722 * A test against the "translateKey(Key)" method. 723 * The test checks out that for a DSAPublicKey argument 724 * the new key has the same values of p, q, g, and y that original key has. 725 */ 726 public final void testTranslateKey02() throws Exception { 727 728 checkPublicIntegers( (DSAPublicKey) kf.translateKey(publicKey) ); 729 } 730 731 732 /** 733 * A test against the "translateKey(Key)" method. 734 * It checks out that for a DSAPrivateKey argument 735 * the new key has the same values of p, q, g, and x that original key has. 736 */ 737 public final void testTranslateKey03() throws Exception { 738 739 checkPrivateIntegers( (DSAPrivateKey) kf.translateKey(privateKey) ); 740 } 741 742 743 /** 744 * A compatibility with RI test. 745 * The test against the "translateKey(Key)" method. 746 * It checks out that 747 * if a key encoding in a DSAPrivateKey argument has correct ASN1 structure 748 * but AlgorithmIdentifier contains value that connot be translated to "DSA" 749 * the method returns DSAPrivateKey whose algorithm is neither null nor "DSA". 750 */ 751 public final void testTranslateKey04() throws Exception { 752 753 final BigInteger y = publicY; 754 final BigInteger p = publicP; 755 final BigInteger q = publicQ; 756 final BigInteger g = publicG; 757 758 byte[] publicEncoding = publicKey.getEncoded(); 759 760 final byte enc1[] = new byte[publicEncoding.length]; 761 System.arraycopy(publicEncoding, 0, enc1, 0, publicEncoding.length); 762 enc1[13] = 0; 763 final byte[] enc2 = enc1; 764 765 DSAPublicKey pubKey = new DSAPublicKey () { 766 767 public BigInteger getY() { return y; } 768 public DSAParams getParams() { 769 return (DSAParams)(new DSAParameterSpec(p, q, g)); 770 } 771 public String getAlgorithm() { return "DSA"; } 772 public byte[] getEncoded() { return enc2; } 773 public String getFormat() { return "X.509"; } 774 }; 775 776 X509EncodedKeySpec ks = kf.getKeySpec(pubKey, X509EncodedKeySpec.class); 777 778 pubKey = (DSAPublicKey) kf.generatePublic((KeySpec)ks); 779 pubKey = (DSAPublicKey) kf.translateKey( (Key)pubKey ); 780 781 String alg = pubKey.getAlgorithm(); 782 assertNotNull(alg); 783 assertFalse("X.509".equals(alg)); 784 } 785 786 787 /** 788 * A compatibility with RI test. 789 * The test against the "translateKey(Key)" method. 790 * It checks out that 791 * if a key encoding in a DSAPrivateKey argument has correct ASN1 structure 792 * but AlgorithmIdentifier contains value that connot be translated to "DSA" 793 * the method returns DSAPrivateKey whose algorithm is neither null nor "DSA". 794 */ 795 public final void testTranslateKey05() throws Exception { 796 797 final BigInteger x = privateKey.getX(); 798 final BigInteger p = privateParams.getP(); 799 final BigInteger q = privateParams.getP(); 800 final BigInteger g = privateParams.getP(); 801 802 byte[] privateEncoding = privateKey.getEncoded(); 803 804 final byte enc1[] = new byte[privateEncoding.length]; 805 System.arraycopy(privateEncoding, 0, enc1, 0, privateEncoding.length); 806 enc1[13] = 0; 807 final byte[] enc2 = enc1; 808 809 DSAPrivateKey prKey = new DSAPrivateKey () { 810 811 public BigInteger getX() { return x; } 812 public DSAParams getParams() { 813 return (DSAParams)(new DSAParameterSpec(p, q, g)); 814 } 815 public String getAlgorithm() { return "DSA"; } 816 public byte[] getEncoded() { return enc2; } 817 public String getFormat() { return "PKCS8"; } 818 }; 819 820 PKCS8EncodedKeySpec ks = kf.getKeySpec(prKey, PKCS8EncodedKeySpec.class); 821 822 prKey = (DSAPrivateKey) kf.generatePrivate((KeySpec)ks); 823 prKey = (DSAPrivateKey) kf.translateKey( (Key)prKey ); 824 825 String alg = prKey.getAlgorithm(); 826 assertNotNull(alg); 827 assertFalse("PKCS8".equals(alg)); 828 } 829 830 831 public static Test suite() { 832 return new TestSuite(DSAKeyFactoryImplTest.class); 833 } 834 835 836 public static void main(String[] args) { 837 junit.textui.TestRunner.run(suite()); 838 } 839} 840