PageRenderTime 42ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/src/main/java/com/google/code/p/keytooliui/ktl/util/jarsigner/KTLShkAbs.java

http://keytool-iui.googlecode.com/
Java | 359 lines | 159 code | 68 blank | 132 comment | 12 complexity | 61cb36f847f17d1cb87e2d538f070461 MD5 | raw file
  1. package com.google.code.p.keytooliui.ktl.util.jarsigner;
  2. /**
  3. "Shk" for "Shared Key" (Secret Key)
  4. known subclasses:
  5. . KTLShkSaveAbs: pending
  6. . KTLShkOpenAbs
  7. **/
  8. import java.io.DataInputStream;
  9. import java.io.File;
  10. import java.io.FileInputStream;
  11. import java.io.FileOutputStream;
  12. import java.io.IOException;
  13. import java.io.InputStream;
  14. import java.io.OutputStream;
  15. import java.security.InvalidKeyException;
  16. import java.security.NoSuchAlgorithmException;
  17. import java.security.spec.InvalidKeySpecException;
  18. import java.security.spec.KeySpec;
  19. import javax.crypto.BadPaddingException;
  20. import javax.crypto.Cipher;
  21. import javax.crypto.CipherOutputStream;
  22. import javax.crypto.IllegalBlockSizeException;
  23. import javax.crypto.NoSuchPaddingException;
  24. import javax.crypto.SecretKey;
  25. import javax.crypto.SecretKeyFactory;
  26. import javax.crypto.spec.DESKeySpec;
  27. import javax.crypto.spec.DESedeKeySpec;
  28. import javax.crypto.spec.SecretKeySpec;
  29. import com.google.code.p.keytooliui.shared.lang.*;
  30. import com.google.code.p.keytooliui.shared.swing.optionpane.*;
  31. import com.google.code.p.keytooliui.shared.util.jarsigner.*;
  32. // ----
  33. import java.security.KeyStore;
  34. import java.security.PublicKey;
  35. import java.security.KeyStoreException;
  36. import java.security.Provider;
  37. // --
  38. import java.security.cert.Certificate;
  39. import java.security.cert.X509Certificate;
  40. // ----
  41. import java.awt.*;
  42. import java.util.*;
  43. public abstract class KTLShkAbs extends KTLAbs
  44. {
  45. // -------------
  46. // public static
  47. /* IN PROGRESS, TO BE USED BY keystoreManager
  48. public static boolean s_createKey(
  49. Frame frmOwner,
  50. String strPathAbsOpenKst, // existing keystore
  51. char[] chrsPasswdOpenKst,
  52. String strProviderKst,
  53. String strSignatureAlgo)
  54. {
  55. String strMethod = "s_createKey(...)";
  56. File fleOpen = null;
  57. // ---
  58. if (fleOpen == null)
  59. {
  60. return true; // maybe aborted by user
  61. }
  62. // ----
  63. SecretKey sky = KTLShkAbs._s_readKey_(fleOpen, strSignatureAlgo);
  64. if (sky == null)
  65. {
  66. return false;
  67. }
  68. char[] chrsPasswdShk = null;
  69. // ---
  70. if (chrsPasswdShk == null)
  71. {
  72. return true; // maybe aborted by user
  73. }
  74. // ----
  75. if (! UtilKstAbs.s_setKeyEntry(frmOwner,
  76. kstOpen, strAliasShk, ShkNew, chrsPasswdShk, null))
  77. {
  78. MySystem.s_printOutError(this, strMethod, "failed");
  79. return false;
  80. }
  81. // ending
  82. return true;
  83. }*/
  84. protected static SecretKey _s_readKey_(File fleOpen, String strSignatureAlgoCandidate)
  85. throws IOException, NoSuchAlgorithmException,
  86. InvalidKeyException, InvalidKeySpecException
  87. {
  88. String strMethod = "_s_readKey_(...)";
  89. String strSignatureAlgo = null;
  90. for (int i=0; i<KTLAbs.f_s_strsSigAlgoSKJceks.length; i++)
  91. {
  92. if (! strSignatureAlgoCandidate.equalsIgnoreCase(KTLAbs.f_s_strsSigAlgoSKJceks[i]))
  93. continue;
  94. strSignatureAlgo = KTLAbs.f_s_strsSigAlgoSKJceks[i];
  95. break;
  96. }
  97. if (strSignatureAlgo == null)
  98. {
  99. MySystem.s_printOutExit(strMethod, "uncaught strSignatureAlgoCandidate:" + strSignatureAlgoCandidate);
  100. }
  101. // Read the raw bytes from the keyfile
  102. DataInputStream dis = new DataInputStream(new FileInputStream(fleOpen));
  103. byte[] bytsRawKey = new byte[(int)fleOpen.length( )];
  104. dis.readFully(bytsRawKey);
  105. dis.close( );
  106. SecretKey sky = null;
  107. if (strSignatureAlgo.equalsIgnoreCase("DES"))
  108. {
  109. DESKeySpec obj = new DESKeySpec(bytsRawKey);
  110. SecretKeyFactory skf = SecretKeyFactory.getInstance(strSignatureAlgo);
  111. sky = skf.generateSecret(obj);
  112. }
  113. else if (strSignatureAlgo.equalsIgnoreCase("DESede"))
  114. {
  115. DESedeKeySpec obj = new DESedeKeySpec(bytsRawKey);
  116. SecretKeyFactory skf = SecretKeyFactory.getInstance(strSignatureAlgo);
  117. sky = skf.generateSecret(obj);
  118. }
  119. else
  120. {
  121. SecretKeySpec obj = new SecretKeySpec(bytsRawKey, strSignatureAlgo);
  122. // BUG !SecretKeyFactory skf = SecretKeyFactory.getInstance(strSignatureAlgo);
  123. sky = (SecretKey) obj; //skf.generateSecret(obj);
  124. }
  125. return sky;
  126. }
  127. // ----------------
  128. // PROTECTED STATIC
  129. /*
  130. * Memo: SecretKeyFactory for SunJCE, with JRE1.6
  131. DES
  132. DESede
  133. PBEWithMD5AndDES
  134. PBEWithMD5AndTripleDES
  135. PBEWithSHA1AndDESede
  136. PBEWithSHA1AndRC2_40
  137. PBKDF2WithHmacSHA1
  138. */
  139. protected static void _writeKey_(SecretKey sky, File fleSave)
  140. throws IOException, NoSuchAlgorithmException, InvalidKeySpecException, Exception
  141. {
  142. String strMethod = "KTLShkAbs._writeKey_(...)";
  143. String strAlgoSecretKey = sky.getAlgorithm();
  144. //KeySpec ksc = null;
  145. byte[] bytsRawKey = null;
  146. if (strAlgoSecretKey.equalsIgnoreCase("DES"))
  147. {
  148. SecretKeyFactory skf = SecretKeyFactory.getInstance(strAlgoSecretKey);
  149. DESKeySpec spec = (DESKeySpec) skf.getKeySpec(sky, DESKeySpec.class);
  150. bytsRawKey = spec.getKey();
  151. }
  152. else if (strAlgoSecretKey.equalsIgnoreCase("DESede"))
  153. {
  154. SecretKeyFactory skf = SecretKeyFactory.getInstance(strAlgoSecretKey);
  155. DESedeKeySpec spec = (DESedeKeySpec) skf.getKeySpec(sky, DESedeKeySpec.class);
  156. bytsRawKey = spec.getKey( );
  157. }
  158. else
  159. {
  160. // beg test
  161. //SecretKeyFactory skf = SecretKeyFactory.getInstance(strAlgoSecretKey/*, "BC"*/);
  162. //SecretKeySpec spec = (SecretKeySpec) skf.getKeySpec(sky, KeySpec.class);
  163. //SecretKeySpec spec = (SecretKeySpec) sky; // test, suggested by sean mullan
  164. //bytsRawKey = spec.getEncoded();
  165. /*
  166. byte[] raw = sky.getEncoded();
  167. SecretKeySpec skeySpec = new SecretKeySpec(raw, "Blowfish");
  168. */
  169. SecretKeySpec sks = (SecretKeySpec) sky; // test, suggested by sean mullan
  170. bytsRawKey = sks.getEncoded();
  171. // end test
  172. //String strBody = "DEV ERROR, unsupported algo, strAlgoSecretKey=" + strAlgoSecretKey;
  173. //throw new Exception(strBody);
  174. }
  175. // Write the raw key to the file
  176. FileOutputStream fos = new FileOutputStream(fleSave);
  177. fos.write(bytsRawKey);
  178. fos.close( );
  179. }
  180. /**
  181. * Use the specified key to decrypt bytes ready from the input
  182. * stream and write them to the output stream. This method
  183. * uses Cipher directly to show how it can be done without
  184. * CipherInputStream and CipherOutputStream.
  185. **/
  186. protected static void _s_decrypt_(
  187. SecretKey key,
  188. InputStream in,
  189. OutputStream out,
  190. String strInstanceCipher)
  191. throws
  192. NoSuchAlgorithmException,
  193. InvalidKeyException,
  194. IOException,
  195. IllegalBlockSizeException,
  196. NoSuchPaddingException,
  197. BadPaddingException
  198. {
  199. // Create and initialize the decryption engine
  200. Cipher cipher = Cipher.getInstance(strInstanceCipher);
  201. cipher.init(Cipher.DECRYPT_MODE, key);
  202. // Read bytes, decrypt, and write them out.
  203. byte[] buffer = new byte[2048];
  204. int bytesRead;
  205. while((bytesRead = in.read(buffer)) != -1) {
  206. out.write(cipher.update(buffer, 0, bytesRead));
  207. }
  208. // Write out the final bunch of decrypted bytes
  209. out.write(cipher.doFinal( ));
  210. out.flush( );
  211. }
  212. /**
  213. * Use the specified key to encrypt bytes from the input stream
  214. * and write them to the output stream. This method uses
  215. * CipherOutputStream to perform the encryption and write bytes at the
  216. * same time.
  217. **/
  218. protected static void _s_encrypt_(
  219. SecretKey sky,
  220. InputStream ism,
  221. OutputStream osm,
  222. String strInstanceCipher)
  223. throws
  224. NoSuchAlgorithmException,
  225. InvalidKeyException,
  226. NoSuchPaddingException,
  227. IOException
  228. {
  229. // Create and initialize the encryption engine
  230. Cipher cip = Cipher.getInstance(strInstanceCipher);
  231. cip.init(Cipher.ENCRYPT_MODE, sky);
  232. // Create a special output stream to do the work for us
  233. CipherOutputStream cos = new CipherOutputStream(osm, cip);
  234. // Read from the input and write to the encrypting output stream
  235. byte[] bytsBuffer = new byte[2048];
  236. int intBytesRead;
  237. while((intBytesRead = ism.read(bytsBuffer)) != -1)
  238. {
  239. cos.write(bytsBuffer, 0, intBytesRead);
  240. }
  241. cos.close( );
  242. // For extra security, don't leave any plaintext hanging around memory.
  243. java.util.Arrays.fill(bytsBuffer, (byte) 0);
  244. }
  245. /*
  246. *public static byte[] encrypt(Key key, String text) throws Exception {
  247. Cipher cipher = Cipher.getInstance("RSA");
  248. cipher.init(javax.crypto.Cipher.ENCRYPT_MODE, key);
  249. byte[] encrypted = cipher.doFinal(text.getBytes());
  250. return encrypted;
  251. }
  252. public static String decrypt(Key key, byte[] encrypted) throws Exception {
  253. Cipher cipher = Cipher.getInstance("RSA");
  254. cipher.init(javax.crypto.Cipher.DECRYPT_MODE, key);
  255. byte[] decrypted = cipher.doFinal(encrypted);
  256. return new String(decrypted);
  257. }
  258. */
  259. // ---------
  260. // PROTECTED
  261. protected KTLShkAbs(
  262. Frame frmOwner,
  263. // input
  264. String strPathAbsKst, // existing keystore of type [JKS-JCEKS-PKCS12]
  265. char[] chrsPasswdKst,
  266. String strProviderKst
  267. )
  268. {
  269. super(frmOwner, strPathAbsKst, chrsPasswdKst, strProviderKst);
  270. }
  271. }