PageRenderTime 36ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/contrib-gestalt/sipbc/jar/src/main/java/com/gestalt/jbi/sip/component/security/CryptoSecurityUtils.java

https://bitbucket.org/pymma/openesb-components
Java | 146 lines | 100 code | 24 blank | 22 comment | 9 complexity | 939451018f3d4ac27e6388e5cf8b7c16 MD5 | raw file
  1. /**
  2. * sip-binding-component - SIP Binding Component
  3. *
  4. * Copyright (C) 2007 Gestalt, LLC. All Rights Reserved.
  5. * http://www.gestalt-llc.com/
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License version 2.1 as published by the Free Software Foundation.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this library; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. package com.gestalt.jbi.sip.component.security;
  21. import com.sun.org.apache.xerces.internal.impl.dv.util.Base64;
  22. import com.sun.jbi.internationalization.Messages;
  23. import java.io.File;
  24. import java.io.FileInputStream;
  25. import java.io.FileOutputStream;
  26. import java.security.GeneralSecurityException;
  27. import java.security.KeyStore;
  28. import java.util.logging.Logger;
  29. import java.util.logging.Level;
  30. import javax.crypto.Cipher;
  31. import javax.crypto.KeyGenerator;
  32. import javax.crypto.SecretKey;
  33. import javax.crypto.spec.SecretKeySpec;
  34. public class CryptoSecurityUtils implements SecurityUtils {
  35. private static final Logger log = Messages.getLogger(CryptoSecurityUtils.class);
  36. private static Messages messages = Messages.getMessages(CryptoSecurityUtils.class);
  37. private Cipher encipher;
  38. private Cipher decipher;
  39. private String algorithm = "Blowfish";
  40. private File KEY_STORE_FILE = new File("sipbc.jks");
  41. private final String KEYSTORE_TYPE = "JCEKS";
  42. private final char[] STORE_PASSWD = "p4ssw0rd".toCharArray();
  43. private final char[] KEY_PASSWD = "k3yp4ssw0rd".toCharArray();
  44. private final String KEY_ALIAS = "SIPBC";
  45. public CryptoSecurityUtils(String algorithm) throws Exception {
  46. this.algorithm = algorithm;
  47. init();
  48. }
  49. public CryptoSecurityUtils() throws Exception {
  50. init();
  51. }
  52. private void init() throws Exception {
  53. if (log.isLoggable(Level.FINE)) {
  54. log.log(Level.FINE,"Using Algorithm: " + algorithm);
  55. }
  56. encipher = Cipher.getInstance(algorithm);
  57. decipher = Cipher.getInstance(algorithm);
  58. SecretKey skey;
  59. KeyStore ks = KeyStore.getInstance(KEYSTORE_TYPE);
  60. if (KEY_STORE_FILE.exists()) {
  61. if (log.isLoggable(Level.FINER)) {
  62. log.log(Level.FINER, "Using the existing key store file.");
  63. }
  64. ks.load(new FileInputStream(KEY_STORE_FILE), STORE_PASSWD);
  65. skey = (SecretKey) ks.getKey(KEY_ALIAS, KEY_PASSWD);
  66. } else {
  67. if (log.isLoggable(Level.FINER)) {
  68. log.log(Level.FINER, "Unable to locate an existing key store file. The file will be created.");
  69. }
  70. KeyGenerator kgen = KeyGenerator.getInstance(algorithm);
  71. skey = kgen.generateKey();
  72. ks.load(null, STORE_PASSWD);
  73. ks.setKeyEntry(KEY_ALIAS, skey, KEY_PASSWD, null);
  74. FileOutputStream fos = new FileOutputStream(KEY_STORE_FILE);
  75. ks.store(fos, STORE_PASSWD);
  76. fos.close();
  77. }
  78. byte[] raw = skey.getEncoded();
  79. SecretKeySpec skeySpec = new SecretKeySpec(raw, algorithm);
  80. encipher.init(Cipher.ENCRYPT_MODE, skeySpec);
  81. decipher.init(Cipher.DECRYPT_MODE, skeySpec);
  82. }
  83. public String decrypt(String password) throws GeneralSecurityException {
  84. byte[] encrypted = decipher.doFinal(Base64.decode(password));
  85. return getUTF8String(encrypted);
  86. }
  87. public String encrypt(String password) throws GeneralSecurityException {
  88. byte[] encrypted = encipher.doFinal(getUTF8Bytes(password));
  89. return Base64.encode(encrypted);
  90. }
  91. private byte[] getUTF8Bytes(String data) {
  92. byte[] bytes;
  93. if (data == null) {
  94. bytes = new byte[0];
  95. } else {
  96. try {
  97. bytes = data.getBytes("UTF-8");
  98. } catch (java.io.UnsupportedEncodingException uee) {
  99. log.log(Level.WARNING,messages.getString("SIPBC-W00500.exceptionPreparingForEncryption"),uee);
  100. bytes = data.getBytes();
  101. }
  102. }
  103. return bytes;
  104. }
  105. private String getUTF8String(byte[] data) {
  106. try {
  107. return new String(data, "UTF-8");
  108. } catch (java.io.UnsupportedEncodingException uee) {
  109. log.log(Level.WARNING,messages.getString("SIPBC-W00501.exceptionExtractingAUTF-8StringFromTheBytesProvided"),uee);
  110. return new String(data);
  111. }
  112. }
  113. /**
  114. * Deletes keystore file
  115. */
  116. public void cleanup() {
  117. if (log.isLoggable(Level.FINE)) {
  118. log.log(Level.FINE,"Removing Key Store File");
  119. }
  120. KEY_STORE_FILE.delete();
  121. }
  122. }