1 7 package org.jgroups.protocols; 8 9 import junit.framework.TestCase; 10 import org.jgroups.*; 11 import org.jgroups.protocols.ENCRYPT.EncryptHeader; 12 import org.jgroups.stack.Protocol; 13 14 import javax.crypto.Cipher; 15 import java.io.*; 16 import java.security.MessageDigest ; 17 import java.security.Security ; 18 import java.util.HashMap ; 19 import java.util.Map ; 20 import java.util.Properties ; 21 import java.util.Vector ; 22 28 public class ENCRYPTAsymmetricTest extends TestCase { 29 30 31 { 32 Security.addProvider( new org.bouncycastle.jce.provider.BouncyCastleProvider()); 33 34 } 35 36 public void testInitNoProperties() 37 { 38 39 ENCRYPT encrypt = new ENCRYPT(); 40 try { 41 encrypt.init(); 42 } catch (Exception e){ 43 fail(e.getMessage()); 44 } 45 46 assertEquals("RSA",encrypt.getAsymAlgorithm()); 48 assertEquals(512,encrypt.getAsymInit()); 49 assertEquals("RSA",encrypt.getKpair().getPublic().getAlgorithm()); 50 assertEquals("X.509",encrypt.getKpair().getPublic().getFormat()); 51 52 assertNotNull(encrypt.getKpair().getPublic().getEncoded()); 53 54 assertEquals("Blowfish",encrypt.getSymAlgorithm()); 56 assertEquals(56,encrypt.getSymInit()); 57 assertEquals("Blowfish",encrypt.getDesKey().getAlgorithm()); 58 assertEquals("RAW",encrypt.getDesKey().getFormat()); 59 assertNotNull(encrypt.getDesKey().getEncoded()); 60 61 62 System.out.println("Provider:"+encrypt.getAsymCipher().getProvider()); 64 assertNotNull(encrypt.getAsymCipher()); 65 assertNotNull(encrypt.getSymDecodingCipher()); 66 assertNotNull(encrypt.getSymEncodingCipher()); 67 } 68 69 public void testInitBCAsymProperties() 70 { 71 72 Properties props = new Properties (); 73 props.put("asym_provider","BC"); 74 props.put("asym_algorithm","RSA"); 75 ENCRYPT encrypt = new ENCRYPT(); 77 encrypt.setProperties(props); 78 try { 79 encrypt.init(); 80 } catch (Exception e){ 81 fail(e.getMessage()); 82 } 83 84 assertEquals("RSA",encrypt.getAsymAlgorithm()); 86 assertEquals(512,encrypt.getAsymInit()); 87 assertEquals("RSA",encrypt.getKpair().getPublic().getAlgorithm()); 88 assertEquals("X.509",encrypt.getKpair().getPublic().getFormat()); 90 assertNotNull(encrypt.getKpair().getPublic().getEncoded()); 91 92 93 94 assertNotNull(encrypt.getAsymCipher()); 96 97 } 98 99 public void XtestInitRSABlockAsymProperties() 100 { 101 102 Properties props = new Properties (); 103 props.put("asym_algorithm","RSA/ECB/OAEPPadding"); 104 ENCRYPT encrypt = new ENCRYPT(); 106 encrypt.setProperties(props); 107 try { 108 encrypt.init(); 109 } catch (Exception e){ 110 fail(e.getMessage()); 111 } 112 113 assertEquals("RSA/ECB/OAEPPadding",encrypt.getAsymAlgorithm()); 115 assertEquals(512,encrypt.getAsymInit()); 116 assertEquals("RSA",encrypt.getKpair().getPublic().getAlgorithm()); 117 assertEquals("X509",encrypt.getKpair().getPublic().getFormat()); 119 assertNotNull(encrypt.getKpair().getPublic().getEncoded()); 120 121 122 123 assertNotNull(encrypt.getAsymCipher()); 125 126 } 127 128 129 130 public void testInitIDEAProperties() 131 { 132 Properties props = new Properties (); 133 props.put("sym_algorithm","IDEA"); 134 props.put("sym_init","128"); 135 136 ENCRYPT encrypt = new ENCRYPT(); 137 encrypt.setProperties(props); 138 try { 139 encrypt.init(); 140 } catch (Exception e){ 141 fail(e.getMessage()); 142 } 143 144 145 assertEquals("IDEA",encrypt.getSymAlgorithm()); 147 assertEquals(128,encrypt.getSymInit()); 148 assertEquals("IDEA",encrypt.getDesKey().getAlgorithm()); 149 assertEquals("RAW",encrypt.getDesKey().getFormat()); 150 assertNotNull(encrypt.getDesKey().getEncoded()); 151 152 153 155 assertNotNull(encrypt.getSymDecodingCipher()); 156 assertNotNull(encrypt.getSymEncodingCipher()); 157 } 158 159 160 public void testInitAESProperties() 161 { 162 Properties props = new Properties (); 163 props.put("sym_algorithm","AES"); 164 props.put("sym_init","128"); 165 ENCRYPT encrypt = new ENCRYPT(); 166 encrypt.setProperties(props); 167 try { 168 encrypt.init(); 169 } catch (Exception e){ 170 fail(e.getMessage()); 171 } 172 173 174 assertEquals("AES",encrypt.getSymAlgorithm()); 176 assertEquals(128,encrypt.getSymInit()); 177 assertEquals("AES",encrypt.getDesKey().getAlgorithm()); 178 assertEquals("RAW",encrypt.getDesKey().getFormat()); 179 assertNotNull(encrypt.getDesKey().getEncoded()); 180 181 182 184 assertNotNull(encrypt.getSymDecodingCipher()); 185 assertNotNull(encrypt.getSymEncodingCipher()); 186 } 187 188 public void testViewChangeBecomeKeyserver() throws Exception { 189 ENCRYPT encrypt = new ENCRYPT(); 191 try { 192 encrypt.init(); 193 } catch (Exception e){ 194 fail(e.getMessage()); 195 } 196 197 MockAddress tempAddress = new MockAddress("encrypt"); 199 encrypt.setLocal_addr(tempAddress); 200 MockObserver observer = new MockObserver(); 201 encrypt.setObserver(observer); 202 203 Cipher cipher = encrypt.getSymEncodingCipher(); 205 206 MessageDigest digest = MessageDigest.getInstance("MD5"); 207 digest.reset(); 208 digest.update(encrypt.getDesKey().getEncoded()); 209 210 String symVersion = new String (digest.digest(), "UTF-8"); 211 212 encrypt.keyServer = false; 213 Message msg = new Message(); 214 msg.setBuffer(cipher.doFinal("hello".getBytes())); 215 msg.putHeader(EncryptHeader.KEY, new EncryptHeader( 216 EncryptHeader.ENCRYPT, symVersion)); 217 218 Event evt = new Event(Event.MSG,msg); 219 220 222 encrypt.up(evt); 223 224 assertTrue(observer.getUpMessages().isEmpty()); 226 227 Vector tempVector = new Vector (); 230 tempVector.add(tempAddress); 231 View tempView = new View(new ViewId(tempAddress,1),tempVector); 232 Event event = new Event(Event.VIEW_CHANGE,tempView); 233 encrypt.up(event); 235 236 Message msg2 = new Message(); 238 msg2.setBuffer(cipher.doFinal("hello2".getBytes())); 239 msg2.putHeader(EncryptHeader.KEY, new EncryptHeader( 240 EncryptHeader.ENCRYPT, symVersion)); 241 242 245 Event evt2 = new Event(Event.MSG,msg2); 246 247 encrypt.up(evt2); 248 assertEquals(3,observer.getUpMessages().size()); 249 250 251 Event sent = (Event)observer.getUpMessages().get("message1"); 252 253 254 assertEquals("hello", new String (((Message)sent.getArg()).getBuffer())); 255 256 sent = (Event)observer.getUpMessages().get("message2"); 257 258 assertEquals("hello2", new String (((Message)sent.getArg()).getBuffer())); 259 260 261 } 262 263 264 public void testViewChangeNewKeyServer() throws Exception { 265 ENCRYPT peer = new ENCRYPT(); 267 try { 268 peer.init(); 269 } catch (Exception e){ 270 fail(e.getMessage()); 271 } 272 273 ENCRYPT server = new ENCRYPT(); 274 try { 275 server.init(); 276 } catch (Exception e){ 277 fail(e.getMessage()); 278 } 279 280 server.keyServer = true; 282 MockObserver serverObserver = new MockObserver(); 283 server.setObserver(serverObserver); 284 Address serverAddress =new MockAddress("server"); 285 286 server.setLocal_addr(serverAddress); 287 Vector serverVector = new Vector (); 289 serverVector.add(serverAddress); 290 View tempView = new View(new ViewId(serverAddress,1),serverVector); 291 Event serverEvent = new Event(Event.VIEW_CHANGE,tempView); 292 server.up(serverEvent); 293 294 Address peerAddress =new MockAddress("peer"); 296 peer.setLocal_addr(peerAddress); 297 MockObserver peerObserver = new MockObserver(); 298 peer.setObserver(peerObserver); 299 peer.keyServer = false; 300 301 302 MessageDigest digest = MessageDigest.getInstance("MD5"); 303 digest.reset(); 304 digest.update(server.getDesKey().getEncoded()); 305 306 String symVersion = new String (digest.digest(), "UTF-8"); 307 308 Cipher cipher = server.getSymEncodingCipher(); 310 Message msg = new Message(); 311 msg.setBuffer(cipher.doFinal("hello".getBytes())); 312 msg.putHeader(EncryptHeader.KEY, new EncryptHeader( 313 EncryptHeader.ENCRYPT, symVersion)); 314 315 Event evt = new Event(Event.MSG,msg); 316 317 peer.up(evt); 318 assertTrue(peerObserver.getUpMessages().isEmpty()); 320 321 323 324 peer.up(serverEvent); 326 assertEquals(serverAddress, peer.getKeyServerAddr()); 328 329 331 Event sent = (Event)peerObserver.getDownMessages().get("message0"); 332 333 assertEquals(((EncryptHeader)((Message)sent.getArg()).getHeader(EncryptHeader.KEY)).getType(), EncryptHeader.KEY_REQUEST); 334 assertEquals(new String (((Message)sent.getArg()).getBuffer()),new String (peer.getKpair().getPublic().getEncoded())); 335 336 server.up(sent); 338 339 Event reply = (Event)serverObserver.getDownMessages().get("message1"); 340 341 342 343 assertEquals(((EncryptHeader)((Message)reply.getArg()).getHeader(EncryptHeader.KEY)).getType(), EncryptHeader.SECRETKEY); 345 346 347 assertNotSame(peer.getDesKey(),server.getDesKey()); 348 peer.up(reply); 350 351 assertEquals(peer.getDesKey(),server.getDesKey()); 353 354 Message msg2 = new Message(); 356 msg2.setBuffer(cipher.doFinal("hello2".getBytes())); 357 msg2.putHeader(EncryptHeader.KEY, new EncryptHeader( 358 EncryptHeader.ENCRYPT, symVersion)); 359 360 Event evt2 = new Event(Event.MSG,msg2); 361 362 peer.up(evt2); 363 364 assertEquals(3,peerObserver.getUpMessages().size()); 366 367 Event tempEvt = (Event)peerObserver.getUpMessages().get("message2"); 368 369 370 assertEquals("hello", new String (((Message)tempEvt.getArg()).getBuffer())); 371 372 tempEvt = (Event)peerObserver.getUpMessages().get("message3"); 373 374 assertEquals("hello2", new String (((Message)tempEvt.getArg()).getBuffer())); 375 376 377 378 } 379 380 381 public void testViewChangeNewKeyServerNewKey() throws Exception { 382 ENCRYPT peer = new ENCRYPT(); 384 try { 385 peer.init(); 386 } catch (Exception e){ 387 fail(e.getMessage()); 388 } 389 390 ENCRYPT server = new ENCRYPT(); 391 try { 392 server.init(); 393 } catch (Exception e){ 394 fail(e.getMessage()); 395 } 396 397 398 ENCRYPT peer2 = new ENCRYPT(); 399 try { 400 peer2.init(); 401 } catch (Exception e){ 402 fail(e.getMessage()); 403 } 404 405 server.keyServer = true; 407 MockObserver serverObserver = new MockObserver(); 408 server.setObserver(serverObserver); 409 410 Address serverAddress = new MockAddress("server"); 412 server.setLocal_addr(serverAddress); 413 414 Vector serverVector = new Vector (); 416 serverVector.add(serverAddress); 417 View tempView = new View(new ViewId(serverAddress,1),serverVector); 418 Event serverEvent = new Event(Event.VIEW_CHANGE,tempView); 419 server.up(serverEvent); 420 421 422 Address peerAddress = new MockAddress("peer"); 424 peer.setLocal_addr(peerAddress); 425 MockObserver peerObserver = new MockObserver(); 426 peer.setObserver(peerObserver); 427 peer.keyServer = false; 428 429 Address peer2Address = new MockAddress("peer2"); 431 peer2.setLocal_addr(peer2Address); 432 MockObserver peer2Observer = new MockObserver(); 433 peer2.setObserver(peer2Observer); 434 peer2.keyServer = false; 435 peer2.setKeyServerAddr(serverAddress); 436 437 Message msg = new Message(); 439 msg.setBuffer("hello".getBytes()); 440 441 442 Event evt = new Event(Event.MSG,msg); 443 444 server.down(evt); 445 446 Event encEvt = (Event)serverObserver.getDownMessages().get("message1"); 448 449 peer.up(encEvt); 451 452 assertTrue(peerObserver.getUpMessages().isEmpty()); 454 455 Vector peerVector = new Vector (); 457 peerVector.add(peer2Address); 458 View tempPeerView = new View(new ViewId(peer2Address,1),peerVector); 459 Event event = new Event(Event.VIEW_CHANGE,tempPeerView); 460 461 peer.up(event); 463 464 assertEquals(peer2Address, peer.getKeyServerAddr()); 466 467 Event sent = (Event)peerObserver.getDownMessages().get("message0"); 469 470 assertEquals(((EncryptHeader)((Message)sent.getArg()).getHeader(EncryptHeader.KEY)).getType(), EncryptHeader.KEY_REQUEST); 472 assertEquals(new String (((Message)sent.getArg()).getBuffer()),new String (peer.getKpair().getPublic().getEncoded())); 473 474 Vector peer2Vector = new Vector (); 481 peer2Vector.add(peer2Address); 482 View tempPeer2View = new View(new ViewId(peer2Address,1),peer2Vector); 483 Event event2 = new Event(Event.VIEW_CHANGE,tempPeer2View); 484 peer2.up(event2); 486 487 peer2.up(sent); 488 489 Event reply = (Event)peer2Observer.getDownMessages().get("message1"); 490 491 492 493 assertEquals(((EncryptHeader)((Message)reply.getArg()).getHeader(EncryptHeader.KEY)).getType(), EncryptHeader.SECRETKEY); 495 496 497 assertNotSame(peer.getDesKey(),peer2.getDesKey()); 498 assertNotSame(server.getDesKey(),peer2.getDesKey()); 499 500 peer.up(reply); 502 503 assertEquals(peer.getDesKey(),peer2.getDesKey()); 505 assertNotSame(server.getDesKey(),peer.getDesKey()); 506 507 508 Message msg2 = new Message(); 510 msg2.setBuffer("hello2".getBytes()); 511 512 513 Event evt2 = new Event(Event.MSG,msg2); 514 515 peer2.down(evt2); 516 517 Event Evt2 = (Event)peer2Observer.getDownMessages().get("message2"); 518 519 peer.up(Evt2); 520 assertEquals(2,peerObserver.getUpMessages().size()); 522 523 Event tempEvt = (Event)peerObserver.getUpMessages().get("message2"); 524 525 526 assertEquals("hello2", new String (((Message)tempEvt.getArg()).getBuffer())); 527 528 529 530 531 } 532 533 534 535 static class MockObserver implements ENCRYPT.Observer { 536 537 private Map upMessages = new HashMap (); 538 private Map downMessages = new HashMap (); 539 private int counter =0; 540 543 544 private void storeUp(Event evt){ 545 upMessages.put("message"+counter++,evt); 546 } 547 548 private void storeDown(Event evt){ 549 downMessages.put("message"+counter++,evt); 550 } 551 public void up(Event evt) 552 { 553 storeUp(evt); 554 System.out.println("Up:"+evt.toString()); 555 } 556 557 560 public void setProtocol(Protocol prot) 561 { 562 } 563 564 565 566 569 public void passUp(Event evt) 570 { 571 storeUp(evt); 572 System.out.println("PassUp:"+evt.toString()); 573 } 574 575 578 public void down(Event evt) 579 { 580 System.out.println("down:"+evt.toString()); 581 } 582 583 586 public void passDown(Event evt) 587 { 588 storeDown(evt); 589 System.out.println("passDown:"+evt.toString()); 590 } 591 592 595 protected Map getUpMessages() 596 { 597 return upMessages; 598 } 599 602 protected void setUpMessages(Map upMessages) 603 { 604 this.upMessages = upMessages; 605 } 606 609 protected Map getDownMessages() 610 { 611 return downMessages; 612 } 613 616 protected void setDownMessages(Map downMessages) 617 { 618 this.downMessages = downMessages; 619 } 620 } 621 622 class MockAddress implements Address{ 623 624 private static final long serialVersionUID = -479331506050129599L; 625 626 629 String name; 630 public MockAddress(String name){ 631 this.name = name; 632 } 633 634 public MockAddress() { 635 } 636 637 public boolean isMulticastAddress() 638 { 639 return false; 640 } 641 642 public int size() { 643 return 0; 644 } 645 646 649 public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException 650 { 651 } 652 653 656 public void writeExternal(ObjectOutput out) throws IOException 657 { 658 } 659 660 661 662 665 public int compareTo(Object o) 666 { 667 return -1; 668 } 669 670 public boolean equals(Object obj){ 671 MockAddress address = (MockAddress)obj; 672 return address.name.equals(this.name); 673 } 674 675 public void writeTo(DataOutputStream out) throws IOException { 676 } 677 678 public void readFrom(DataInputStream in) throws IOException, IllegalAccessException , InstantiationException { 679 } 680 } 681 682 683 } 684 | Popular Tags |