1 2 3 5 package org.jgroups.protocols; 6 7 import java.io.IOException ; 8 import java.io.InputStream ; 9 import java.security.InvalidKeyException ; 10 import java.security.KeyFactory ; 11 import java.security.KeyPair ; 12 import java.security.KeyPairGenerator ; 13 import java.security.KeyStore ; 14 import java.security.KeyStoreException ; 15 import java.security.MessageDigest ; 16 import java.security.NoSuchAlgorithmException ; 17 import java.security.PublicKey ; 18 import java.security.SecureRandom ; 19 import java.security.UnrecoverableKeyException ; 20 import java.security.cert.CertificateException ; 21 import java.security.spec.X509EncodedKeySpec ; 22 import java.util.Map ; 23 import java.util.Properties ; 24 import java.util.WeakHashMap ; 25 26 import javax.crypto.BadPaddingException; 27 import javax.crypto.Cipher; 28 import javax.crypto.IllegalBlockSizeException; 29 import javax.crypto.KeyGenerator; 30 import javax.crypto.NoSuchPaddingException; 31 import javax.crypto.SecretKey; 32 import javax.crypto.spec.SecretKeySpec; 33 34 import org.jgroups.Address; 35 import org.jgroups.Event; 36 import org.jgroups.Message; 37 import org.jgroups.View; 38 import org.jgroups.stack.Protocol; 39 import org.jgroups.util.QueueClosedException; 40 41 import EDU.oswego.cs.dl.util.concurrent.LinkedQueue; 42 43 44 103 104 105 public class ENCRYPT extends Protocol { 106 107 public static class EncryptHeader extends org.jgroups.Header { 108 int type; 109 public static final int ENCRYPT = 0; 110 public static final int KEY_REQUEST = 1; 111 public static final int SERVER_PUBKEY = 2; 112 public static final int SECRETKEY = 3; 113 public static final int SECRETKEY_READY = 4; 114 115 static final String KEY = "encrypt"; 117 118 String version; 119 120 121 public EncryptHeader() 122 { 123 } 125 126 127 public EncryptHeader(int type) 128 { 129 this.type = type; 131 this.version = ""; 132 } 133 134 135 public EncryptHeader(int type, String version) 136 { 137 this.type = type; 138 this.version = version; 139 } 140 141 142 public void writeExternal(java.io.ObjectOutput out) throws IOException 143 { 144 out.writeInt(type); 145 out.writeObject(version); 146 } 147 148 149 public void readExternal(java.io.ObjectInput in) throws IOException , 150 ClassNotFoundException 151 { 152 type = in.readInt(); 154 version = (String )in.readObject(); 156 } 157 158 159 public String toString() 160 { 161 return "{ENCTYPT:[Type:" + type + " Version:" + version + "]}"; 162 } 164 165 166 171 public boolean equals(Object obj) 172 { 173 if (obj instanceof EncryptHeader) 175 { 176 boolean res = ((((EncryptHeader) obj).getType() == type) && ((((EncryptHeader) obj) 177 .getVersion() == version))); 178 return res; 179 } 180 return false; 181 } 182 183 184 187 protected int getType() 188 { 189 return type; 190 } 191 192 193 196 protected String getVersion() 197 { 198 return version; 199 } 200 201 } 202 203 Address local_addr = null; 205 Address keyServerAddr = null; 207 208 boolean keyServer = false; 210 211 String asymProvider = null; 213 final String symProvider = null; 214 String asymAlgorithm = "RSA"; 215 String symAlgorithm = "Blowfish"; 216 int asymInit = 512; int symInit = 56; 219 private boolean suppliedKey = false; 221 private String keyStoreName; 222 private String storePassword ="changeit"; private String keyPassword="changeit"; private String alias="mykey"; 226 227 KeyPair Kpair; 230 PublicKey serverPubKey = null; 232 233 Cipher symEncodingCipher; 236 Cipher symDecodingCipher; 237 238 private String symVersion = null; 240 SecretKey secretKey = null; 242 243 final Map keyMap = new WeakHashMap (); 245 final Object downLock = new Object (); 247 final Object upLock = new Object (); 248 249 252 private boolean queue_up = true; 253 254 private boolean queue_down = true; 255 256 private LinkedQueue upMessageQueue = new LinkedQueue(); 258 259 private LinkedQueue downMessageQueue = new LinkedQueue(); 261 private Cipher asymCipher; 263 264 public ENCRYPT() 265 { 266 } 267 268 269 public String getName() 270 { 271 return "ENCRYPT"; 272 } 273 274 275 279 private String getAlgorithm(String s) 280 { 281 int index = s.indexOf("/"); 282 if (index == -1) 283 return s; 284 285 return s.substring(0, index); 286 } 287 288 289 public boolean setProperties(Properties props) 290 { 291 String str; 292 293 super.setProperties(props); 294 str = props.getProperty("asym_init"); 296 if (str != null) 297 { 298 asymInit = Integer.parseInt(str); 299 props.remove("asym_init"); 300 301 if (log.isInfoEnabled()) 302 log.info("Asym algo bits used is " + asymInit); 303 } 304 305 str = props.getProperty("sym_init"); 307 if (str != null) 308 { 309 symInit = Integer.parseInt(str); 310 props.remove("sym_init"); 311 312 if (log.isInfoEnabled()) 313 log.info("Sym algo bits used is " + symInit); 314 } 315 316 str = props.getProperty("asym_algorithm"); 318 if (str != null) 319 { 320 asymAlgorithm = str; 321 props.remove("asym_algorithm"); 322 323 if (log.isInfoEnabled()) 324 log.info("Asym algo used is " + asymAlgorithm); 325 } 326 327 str = props.getProperty("sym_algorithm"); 329 if (str != null) 330 { 331 symAlgorithm = str; 332 props.remove("sym_algorithm"); 333 334 if (log.isInfoEnabled()) 335 log.info("Sym algo used is " + symAlgorithm); 336 } 337 338 str = props.getProperty("asym_provider"); 340 if (str != null) 341 { 342 asymProvider = str; 343 props.remove("asym_provider"); 344 345 if (log.isInfoEnabled()) 346 log.info("asymProvider used is " + asymProvider); 347 } 348 349 str = props.getProperty("key_store_name"); 351 if (str != null) 352 { 353 keyStoreName = str; 354 props.remove("key_store_name"); 355 356 if (log.isInfoEnabled()) 357 log.info("key_store_name used is " + keyStoreName); 358 } 359 360 str = props.getProperty("store_password"); 362 if (str != null) 363 { 364 storePassword = str; 365 props.remove("store_password"); 366 367 if (log.isInfoEnabled()) 368 log.info("store_password used is not null"); 369 } 370 371 str = props.getProperty("key_password"); 373 if (str != null) 374 { 375 keyPassword = str; 376 props.remove("key_password"); 377 378 if (log.isInfoEnabled()) 379 log.info("key_password used is not null"); 380 } else if (storePassword != null) 381 { 382 keyPassword = storePassword; 383 384 if (log.isInfoEnabled()) 385 log.info("key_password used is same as store_password"); 386 } 387 388 str = props.getProperty("alias"); 390 if (str != null) 391 { 392 alias = str; 393 props.remove("alias"); 394 395 if (log.isInfoEnabled()) 396 log.info("alias used is " + alias); 397 } 398 399 if (props.size() > 0) 400 { 401 402 if (log.isErrorEnabled()) 403 log.error("these properties are not recognized:" + props); 404 return false; 405 } 406 407 return true; 408 } 409 410 411 public void init() throws Exception 412 { 413 if (keyStoreName == null) 414 { 415 initSymKey(); 416 initKeyPair(); 417 } else 418 { 419 initConfiguredKey(); 420 } 421 initSymCiphers(symAlgorithm, getSecretKey()); 422 } 423 424 425 438 private void initConfiguredKey() throws KeyStoreException , Exception , 439 IOException , NoSuchAlgorithmException , CertificateException , 440 UnrecoverableKeyException 441 { 442 InputStream inputStream = null; 443 KeyStore store = KeyStore.getInstance("JCEKS"); 445 446 SecretKey tempKey = null; 447 try 448 { 449 inputStream = Thread.currentThread().getContextClassLoader() 451 .getResourceAsStream(keyStoreName); 452 if (inputStream == null) 454 { 455 throw new Exception ("Unable to load keystore " + keyStoreName + 456 " ensure file is on classpath"); 457 } 458 try{ 460 store.load(inputStream, storePassword.toCharArray()); 461 tempKey = (SecretKey) store 463 .getKey(alias, keyPassword.toCharArray()); 464 } catch (IOException e){ 465 throw new Exception ("Unable to load keystore "+ keyStoreName + ": " + e); 466 }catch (NoSuchAlgorithmException e){ 467 throw new Exception ("No Such algorithm "+ keyStoreName + ": " + e); 468 }catch(CertificateException e){ 469 throw new Exception ("Certificate exception "+ keyStoreName + ": " + e); 470 } 471 472 if (tempKey == null) 473 { 474 throw new Exception ("Unable to retrieve key '" + alias 475 + "' from keystore " + keyStoreName); 476 } 477 setSecretKey(tempKey); 479 symAlgorithm = tempKey.getAlgorithm(); 480 481 483 suppliedKey = true; 484 queue_down =false; 485 queue_up =false; 486 } finally 487 { 488 try 490 { 491 inputStream.close(); 492 } catch (Exception e) 493 { 494 495 } 496 } 497 498 } 499 500 501 505 public void initSymKey() throws Exception 506 { 507 KeyGenerator keyGen = null; 508 if (symProvider != null && symProvider.trim().length() > 0) 510 { 511 keyGen = KeyGenerator.getInstance(getAlgorithm(symAlgorithm), 512 symProvider); 513 } else 514 { 515 keyGen = KeyGenerator.getInstance(getAlgorithm(symAlgorithm)); 516 } 517 keyGen.init(symInit); 519 secretKey = keyGen.generateKey(); 520 521 setSecretKey(secretKey); 522 523 if (log.isInfoEnabled()) 524 log.info(" Symmetric key generated "); 525 } 526 527 528 536 private void initSymCiphers(String algorithm, SecretKey secret) throws Exception 537 { 538 539 if (log.isInfoEnabled()) 540 log.info(" Initializing symmetric ciphers"); 541 542 symEncodingCipher = Cipher.getInstance(algorithm); 543 symDecodingCipher = Cipher.getInstance(algorithm); 544 symEncodingCipher.init(Cipher.ENCRYPT_MODE, secret); 545 symDecodingCipher.init(Cipher.DECRYPT_MODE, secret); 546 547 MessageDigest digest = MessageDigest.getInstance("MD5"); 549 digest.reset(); 550 digest.update(secret.getEncoded()); 551 552 symVersion = new String (digest.digest()); 553 if (log.isInfoEnabled()) 554 log.info(" Initialized symmetric ciphers with secret key version " +symVersion); 555 } 556 557 558 562 public void initKeyPair() throws Exception 563 { 564 KeyPairGenerator KpairGen = null; 567 if (asymProvider != null && asymProvider.trim().length() > 0) 568 { 569 KpairGen = KeyPairGenerator.getInstance( 570 getAlgorithm(asymAlgorithm), asymProvider); 571 } else 572 { 573 KpairGen = KeyPairGenerator 574 .getInstance(getAlgorithm(asymAlgorithm)); 575 576 } 577 KpairGen.initialize(asymInit, new SecureRandom ()); 578 Kpair = KpairGen.generateKeyPair(); 579 580 582 asymCipher = Cipher.getInstance(asymAlgorithm); 583 asymCipher.init(Cipher.DECRYPT_MODE,Kpair.getPrivate()); 584 585 if (log.isInfoEnabled()) 586 log.info(" asym algo initialized"); 587 } 588 589 590 591 public void reset() 592 { 593 } 594 595 596 599 public void up(Event evt) 600 { 601 602 if (log.isDebugEnabled()) 603 { 604 log.debug("Event going up is " + evt); 605 } 606 607 switch (evt.getType()) { 608 609 case Event.SET_LOCAL_ADDRESS : 611 if (log.isDebugEnabled()) 612 log.debug("Set local address "); 613 local_addr = (Address) evt.getArg(); 614 break; 615 case Event.FIND_INITIAL_MBRS_OK : 617 if (!suppliedKey){ 618 if (!keyServer) 619 { 620 if (log.isInfoEnabled()) 621 log.info("FIND_INIT_MBRS_OK called - I am not the keyserver"); 622 } else 623 { 624 if (log.isInfoEnabled()) 625 log.info("FIND_INIT_MBRS_OK called -I am keyserver "); 626 } 627 } 628 break; 629 case Event.VIEW_CHANGE: 631 if (log.isInfoEnabled()) 632 log.info("handling view change event"); 633 if (!suppliedKey){ 634 handleViewChange(evt); 635 } 636 break; 637 case Event.MSG : 639 if (evt.getArg() == null || ((Message)evt.getArg()).getBuffer() == null){ 641 if (log.isDebugEnabled()) 642 log.debug("passing up message as is empty buffer "); 643 break; 644 } 645 try 647 { 648 649 handleUpMessage(evt); 650 651 } catch (Exception e) 652 { 653 log.warn("Exception occurred decrypting up message",e); 654 } 655 return; 656 default : 657 break; 658 } 659 660 if (log.isDebugEnabled()) 661 log.debug("passing event up " +evt); 662 passUp(evt); 663 return; 664 } 665 666 667 private synchronized void handleViewChange(Event evt){ 668 View view = (View)evt.getArg(); 669 670 if (view.getMembers() == null || view.getMembers().get(0) == null){ 672 becomeKeyServer(local_addr); 673 return; 674 } 675 Address tmpKeyServer = (Address)view.getMembers().get(0); 677 678 if (tmpKeyServer.equals(local_addr) && (keyServerAddr == null || (! tmpKeyServer.equals(keyServerAddr)))){ 681 becomeKeyServer(tmpKeyServer); 682 }else if (keyServerAddr == null || (! tmpKeyServer.equals(keyServerAddr))){ 684 handleNewKeyServer(tmpKeyServer); 685 } else{ 686 if (log.isDebugEnabled()) 687 log.debug("Membership has changed but I do not care"); 688 } 689 690 691 } 692 693 699 private void becomeKeyServer(Address tmpKeyServer){ 700 keyServerAddr = tmpKeyServer; 701 keyServer =true; 702 if (log.isInfoEnabled()) 703 log.info("I have become key server " + keyServerAddr); 704 queue_down = false; 705 queue_up = false; 706 } 707 708 716 private void handleNewKeyServer(Address newKeyServer){ 717 queue_up =true; 720 queue_down = true; 721 keyServerAddr = newKeyServer; 723 keyServer =false; 724 if (log.isInfoEnabled()) 725 log.info("Sending key request"); 726 727 sendKeyRequest(); 729 730 731 } 732 733 736 private void handleUpMessage(Event evt) throws Exception 737 { 738 if (log.isDebugEnabled()) 739 log.debug("Handling up message " + evt); 740 741 Message msg = (Message) evt.getArg(); 742 743 if (msg == null) 744 { 745 if (log.isDebugEnabled()) 746 log.debug("Null message - passing straight up"); 747 passUp(evt); 748 return; 749 } 750 751 Object obj = msg.getHeader(EncryptHeader.KEY); 752 753 if (obj == null || !(obj instanceof EncryptHeader)) 755 { 756 if (log.isInfoEnabled()) 757 log.info("Dropping message as ENCRYPT header is null or has not been recognized, msg will not be passed up"); 758 return; 759 } 760 761 EncryptHeader hdr = (EncryptHeader) obj; 762 763 764 if (log.isDebugEnabled()) 765 log.debug("Header received " + hdr); 766 767 if (hdr.getType() == EncryptHeader.ENCRYPT) 769 { 770 if (queue_up){ 772 if (log.isDebugEnabled()) 773 log.debug("queueing up message as no session key established"); 774 upMessageQueue.put(evt); 775 }else{ 776 if (!suppliedKey) { 780 drainUpQueue(); 781 } 782 Message tmpMsg =decryptMessage(symDecodingCipher, msg); 784 if (tmpMsg != null){ 785 passUp(evt); 787 } else { 788 log.warn("Unrecognised cipher discarding message"); 789 } 790 } 791 } else 792 { 793 if (suppliedKey) 797 { 798 if (log.isWarnEnabled()) 799 { 800 log.warn("We received an encrypt header of " 801 + hdr.getType() + " while in configured mode"); 802 } 803 } else{ 804 switch (hdr.getType()){ 807 case EncryptHeader.KEY_REQUEST: 809 if (log.isInfoEnabled()) { 810 log.info("received a key request from peer"); 811 } 812 813 try { 815 PublicKey tmpKey = generatePubKey(msg.getBuffer()); 817 sendSecretKey(getSecretKey(), tmpKey, msg.getSrc()); 819 } catch (Exception e){ 820 log.warn("unable to reconstitute peer's public key"); 821 } 822 break; 823 case EncryptHeader.SECRETKEY: 824 if (log.isInfoEnabled()) { 825 log.info("received a secretkey response from keyserver"); 826 } 827 828 try { 829 SecretKey tmp = decodeKey(msg.getBuffer()); 830 if (tmp == null) { 831 sendKeyRequest(); 834 }else{ 835 setKeys(tmp, hdr.getVersion()); 838 if (log.isInfoEnabled()) { 839 log.info("Decoded secretkey response"); 840 } 841 } 842 } catch (Exception e){ 843 log.warn("unable to process received public key"); 844 log.fatal(e); 845 } 846 break; 847 default: 848 log.warn("Received ignored encrypt header of "+hdr.getType()); 849 break; 850 } 851 } 852 } 853 } 854 855 856 862 private void drainUpQueue() throws QueueClosedException, Exception 863 { 864 Event tmp =null; 867 while ((tmp = (Event)upMessageQueue.poll(0L)) != null){ 868 if (tmp != null){ 872 Message msg = decryptMessage(symDecodingCipher, (Message)tmp.getArg()); 873 874 if (msg != null){ 875 if (log.isDebugEnabled()){ 876 log.debug("passing up message from drain " + new String (msg.getBuffer())); 877 } 878 passUp(tmp); 879 }else{ 880 log.warn("discarding message in queue up drain as cannot decode it"); 881 } 882 } 883 } 884 } 886 887 888 899 private void setKeys(SecretKey key, String version) throws Exception { 900 901 keyMap.put(getSymVersion(), getSymDecodingCipher()); 904 905 setSecretKey(key); 906 initSymCiphers(key.getAlgorithm(),key ); 907 setSymVersion(version); 908 909 log.info("setting queue up to false in setKeys"); 911 queue_up =false; 912 drainUpQueue(); 913 914 queue_down =false; 915 drainDownQueue(); 916 } 917 918 919 927 private Message decryptMessage(Cipher cipher, Message msg) throws Exception 928 { 929 930 if (log.isDebugEnabled()) 931 log.debug(" Starting to decypher message:" 932 + formatArray(msg.getBuffer())); 933 934 EncryptHeader hdr = (EncryptHeader)msg.getHeader(EncryptHeader.KEY); 935 936 if (!hdr.getVersion().equals(getSymVersion())){ 937 log.warn("attempting to use stored cipher as message does not uses current encryption version "); 938 Cipher temp = (Cipher)keyMap.get(hdr.getVersion()); 939 if (temp == null) { 940 log.warn("Unable to find a matching cipher in previous key map"); 941 return null; 942 } else{ 943 log.info("Decrypting using previous cipher version "+ hdr.getVersion()); 944 msg.setBuffer(temp.doFinal(msg.getBuffer())); 945 return msg; 946 } 947 } else { 948 949 msg.setBuffer(cipher.doFinal(msg.getBuffer())); 951 952 if (log.isDebugEnabled()) 955 log.debug(" Decyphered message:" + formatArray(msg.getBuffer())); 956 957 return msg; 958 } 959 } 960 961 962 970 private void sendSecretKey(SecretKey secret, PublicKey pubKey, Address source) 971 throws InvalidKeyException , IllegalStateException , 972 IllegalBlockSizeException, BadPaddingException, NoSuchPaddingException, 973 NoSuchAlgorithmException 974 { 975 Message newMsg; 976 977 if (log.isDebugEnabled()) 978 log.debug("encoding shared key "); 979 980 Cipher tmp = Cipher.getInstance(asymAlgorithm); 982 tmp.init(Cipher.ENCRYPT_MODE, pubKey); 983 984 byte[] encryptedKey = tmp.doFinal(secret.getEncoded()); 986 987 if (log.isDebugEnabled()) 990 log.debug(" Generated encoded key which only client can decode:" 991 + formatArray(encryptedKey)); 992 993 newMsg = new Message(source, local_addr, encryptedKey); 994 995 newMsg.putHeader(EncryptHeader.KEY, new EncryptHeader( 996 EncryptHeader.SECRETKEY, getSymVersion())); 997 998 if (log.isDebugEnabled()) 999 log.debug(" Sending version " + getSymVersion() 1000 + " encoded key to client"); 1001 passDown(new Event(Event.MSG, newMsg)); 1002 } 1003 1004 1005 1009 private PublicKey handleKeyRequest(Message msg) 1010 { 1011 Message newMsg; 1012 if (log.isDebugEnabled()) 1013 log.debug("Request for key recieved"); 1014 1015 if (log.isDebugEnabled()) 1018 log.debug("Got peer's encoded public key:" 1019 + formatArray(msg.getBuffer())); 1020 1021 PublicKey pubKey = generatePubKey(msg.getBuffer()); 1022 1023 if (log.isDebugEnabled()) 1026 log.debug("Generated requestors public key" + pubKey); 1027 1028 1033 newMsg = new Message(msg.getSrc(), local_addr, Kpair.getPublic() 1034 .getEncoded()); 1035 1036 if (log.isInfoEnabled()) 1040 log.debug("encoded key is " 1041 + formatArray(Kpair.getPublic().getEncoded())); 1042 1043 1044 newMsg.putHeader(EncryptHeader.KEY, new EncryptHeader( 1045 EncryptHeader.SERVER_PUBKEY, getSymVersion())); 1046 1047 1048 passDown(new Event(Event.MSG, newMsg)); 1049 return pubKey; 1050 } 1051 1052 1053 1056 1057 private Message sendKeyRequest() 1058 { 1059 1060 Message newMsg = new Message(keyServerAddr, local_addr, Kpair.getPublic() 1063 .getEncoded()); 1064 1065 newMsg.putHeader(EncryptHeader.KEY, new EncryptHeader( 1066 EncryptHeader.KEY_REQUEST, getSymVersion())); 1067 passDown(new Event(Event.MSG, newMsg)); 1068 return newMsg; 1069 } 1070 1071 1072 1075 public void down(Event evt) 1076 { 1077 1078 if (log.isDebugEnabled()) 1079 log.debug("down:evt is " + evt ); 1080 1081 switch (evt.getType()) { 1082 1083 case Event.MSG : 1084 if (evt.getArg() == null || ((Message)evt.getArg()).getBuffer() == null){ 1085 log.debug("passing down message as is empty message or buffer "); 1086 break; 1087 } 1088 try 1089 { 1090 1091 if (queue_down){ 1092 downMessageQueue.put(evt); 1094 }else{ 1095 handleDownEvent(evt); 1096 } 1097 1098 } catch (Exception e) 1099 { 1100 log.warn("Unable to send down event " + e.getMessage()); 1101 } 1102 return; 1103 default : 1104 break; 1105 } 1106 passDown(evt); 1107 1108 } 1109 1110 1111 1115 private void handleDownEvent(Event evt) throws Exception 1116 { 1117 if (log.isDebugEnabled()) 1118 log.debug("Handling down message"); 1119 if (!suppliedKey){ 1121 drainDownQueue(); 1122 } 1123 sendDown(evt); 1124 } 1125 1126 1127 1131 private void drainDownQueue() throws Exception , QueueClosedException 1132 { 1133 Event tmp =null; 1137 while((tmp = (Event)downMessageQueue.poll(0L) )!= null){ 1138 sendDown(tmp); 1139 } 1140 } 1142 1143 1144 1148 private void sendDown(Event evt) throws Exception 1149 { 1150 if (evt.getType() != Event.MSG) 1151 { 1152 return; 1153 } 1154 Message msg = (Message) evt.getArg(); 1155 1156 msg.putHeader(EncryptHeader.KEY, new EncryptHeader( 1158 EncryptHeader.ENCRYPT, getSymVersion())); 1159 1160 if (msg.getBuffer() != null) 1161 { 1162 encryptMessage(symEncodingCipher, msg); 1163 1164 } else 1165 { 1166 if (log.isInfoEnabled()) 1167 log.info("buffer is null not encrypting "); 1168 } 1169 passDown(evt); 1170 } 1171 1172 1173 1177 private Message encryptMessage(Cipher cipher, Message msg) throws Exception 1178 { 1179 1182 if (log.isDebugEnabled()) 1183 log.debug(" Starting to encypher message:" 1184 + formatArray(msg.getBuffer())); 1185 1186 msg.setBuffer(cipher.doFinal(msg.getBuffer())); 1187 1188 if (log.isDebugEnabled()) 1191 log.debug(" Encypher message:" + formatArray(msg.getBuffer())); 1192 1193 return msg; 1194 1195 } 1196 1197 1198 private SecretKeySpec decodeKey(byte[] encodedKey) throws Exception 1199 { 1200 byte[] keyBytes = asymCipher.doFinal(encodedKey); 1202 1203 SecretKeySpec keySpec = null; 1204 try 1205 { 1206 keySpec = new SecretKeySpec(keyBytes, getAlgorithm(symAlgorithm)); 1207 1208 Cipher temp = Cipher.getInstance(symAlgorithm); 1210 temp.init(Cipher.SECRET_KEY, keySpec); 1211 } catch (Exception e) 1212 { 1213 log.fatal(e); 1214 keySpec = null; 1215 } 1216 return keySpec; 1217 } 1218 1219 1220 1225 private PublicKey generatePubKey(byte[] encodedKey) 1226 { 1227 PublicKey pubKey = null; 1228 try 1229 { 1230 KeyFactory KeyFac = KeyFactory 1231 .getInstance(getAlgorithm(asymAlgorithm)); 1232 X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec (encodedKey); 1233 pubKey = KeyFac.generatePublic(x509KeySpec); 1234 } catch (Exception e) 1235 { 1236 e.printStackTrace(); 1237 } 1238 return pubKey; 1239 } 1240 1241 1242 1246 private String formatArray(byte[] array) 1247 { 1248 StringBuffer buf = new StringBuffer (); 1249 for (int i = 0; i < array.length; i++) 1250 { 1251 buf.append(Integer.toHexString(array[i])); 1252 } 1253 return buf.toString(); 1254 } 1255 1256 1257 1260 protected int getAsymInit() 1261 { 1262 return asymInit; 1263 } 1264 1265 1266 1269 protected String getAsymProvider() 1270 { 1271 return asymProvider; 1272 } 1273 1274 1275 1278 protected SecretKey getDesKey() 1279 { 1280 return secretKey; 1281 } 1282 1283 1284 1287 protected KeyPair getKpair() 1288 { 1289 return Kpair; 1290 } 1291 1292 1293 1296 protected Cipher getAsymCipher() 1297 { 1298 return asymCipher; 1299 } 1300 1301 1302 1305 protected PublicKey getServerPubKey() 1306 { 1307 return serverPubKey; 1308 } 1309 1310 1311 1314 protected String getSymAlgorithm() 1315 { 1316 return symAlgorithm; 1317 } 1318 1319 1320 1323 protected int getSymInit() 1324 { 1325 return symInit; 1326 } 1327 1328 1329 1332 protected String getSymProvider() 1333 { 1334 return symProvider; 1335 } 1336 1337 1338 1341 protected String getAsymAlgorithm() 1342 { 1343 return asymAlgorithm; 1344 } 1345 1346 1347 1350 private String getSymVersion() 1351 { 1352 1353 1354 return symVersion; 1355 1356 } 1357 1358 1359 1363 private void setSymVersion(String symVersion) 1364 { 1365 1366 this.symVersion = symVersion; 1367 1368 } 1369 1370 1371 1374 private SecretKey getSecretKey() 1375 { 1376 1377 return secretKey; 1378 1379 } 1380 1381 1382 1386 private void setSecretKey(SecretKey secretKey) 1387 { 1388 1389 this.secretKey = secretKey; 1390 1391 } 1392 1393 1394 1398 private void setServerPubKey(PublicKey serverPubKey) 1399 { 1400 1401 this.serverPubKey = serverPubKey; 1402 1403 } 1404 1407 protected String getKeyStoreName() 1408 { 1409 return keyStoreName; 1410 } 1411 1414 protected Cipher getSymDecodingCipher() 1415 { 1416 return symDecodingCipher; 1417 } 1418 1421 protected Cipher getSymEncodingCipher() 1422 { 1423 return symEncodingCipher; 1424 } 1425 1428 protected Address getLocal_addr() 1429 { 1430 return local_addr; 1431 } 1432 1435 protected void setLocal_addr(Address local_addr) 1436 { 1437 this.local_addr = local_addr; 1438 } 1439 1442 protected Address getKeyServerAddr() 1443 { 1444 return keyServerAddr; 1445 } 1446 1449 protected void setKeyServerAddr(Address keyServerAddr) 1450 { 1451 this.keyServerAddr = keyServerAddr; 1452 } 1453} | Popular Tags |