MySslContextFactory.java 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. package com.persagy.iottransfer.communication.util;
  2. import javax.net.ssl.KeyManagerFactory;
  3. import javax.net.ssl.SSLContext;
  4. import javax.net.ssl.TrustManagerFactory;
  5. import java.io.ByteArrayInputStream;
  6. import java.io.FileInputStream;
  7. import java.io.IOException;
  8. import java.io.InputStream;
  9. import java.security.KeyStore;
  10. public final class MySslContextFactory {
  11. public static SSLContext getContext(String PROTOCOL, String KeyStoreType, String algorithm, String path_km,
  12. String path_tm, String password_km, String password_tm) throws Exception {
  13. InputStream is_km = null;
  14. InputStream is_tm = null;
  15. if (path_km != null) {
  16. is_km = new FileInputStream(path_km);
  17. }
  18. if (path_tm != null) {
  19. is_tm = new FileInputStream(path_tm);
  20. }
  21. SSLContext SSLContext = getContext(PROTOCOL, KeyStoreType, algorithm, is_km, is_tm, password_km, password_tm);
  22. return SSLContext;
  23. }
  24. public static SSLContext getContext(String PROTOCOL, String KeyStoreType, String algorithm, byte[] bytes_km,
  25. byte[] bytes_tm, String password_km, String password_tm) throws Exception {
  26. ByteArrayInputStream in = null;
  27. ByteArrayInputStream tIN = null;
  28. if (bytes_km != null) {
  29. in = new ByteArrayInputStream(bytes_km);
  30. }
  31. if (bytes_tm != null) {
  32. tIN = new ByteArrayInputStream(bytes_tm);
  33. }
  34. SSLContext SSLContext = getContext(PROTOCOL, KeyStoreType, algorithm, in, tIN, password_km, password_tm);
  35. return SSLContext;
  36. }
  37. private static SSLContext getContext(String PROTOCOL, String KeyStoreType, String algorithm, InputStream is_km,
  38. InputStream is_tm, String password_km, String password_tm) throws Exception {
  39. SSLContext context;
  40. try {
  41. // ��Կ������
  42. KeyManagerFactory kmf = null;
  43. if (is_km != null) {
  44. KeyStore ks = KeyStore.getInstance(KeyStoreType);
  45. ks.load(is_km, password_km.toCharArray());
  46. kmf = KeyManagerFactory.getInstance(algorithm);
  47. kmf.init(ks, password_km.toCharArray());
  48. }
  49. // ���ο�
  50. TrustManagerFactory tf = null;
  51. if (is_tm != null) {
  52. KeyStore tks = KeyStore.getInstance(KeyStoreType);
  53. tks.load(is_tm, password_tm.toCharArray());
  54. tf = TrustManagerFactory.getInstance(algorithm);
  55. tf.init(tks);
  56. }
  57. context = SSLContext.getInstance(PROTOCOL);
  58. // ��ʼ����������
  59. context.init(kmf == null ? null : kmf.getKeyManagers(), tf == null ? null : tf.getTrustManagers(), null);
  60. } catch (Exception e) {
  61. throw new Error("Failed to initialize the SSLContext", e);
  62. } finally {
  63. if (is_km != null) {
  64. try {
  65. is_km.close();
  66. } catch (IOException e) {
  67. e.printStackTrace();
  68. }
  69. is_km = null;
  70. }
  71. if (is_tm != null) {
  72. try {
  73. is_tm.close();
  74. } catch (IOException e) {
  75. e.printStackTrace();
  76. }
  77. is_tm = null;
  78. }
  79. }
  80. return context;
  81. }
  82. }