Advertisement
Guest User

Untitled

a guest
Nov 20th, 2017
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 14.71 KB | None | 0 0
  1.  
  2.  
  3. import java.awt.EventQueue;
  4.  
  5. import javax.swing.JFrame;
  6. import javax.swing.JMenuBar;
  7. import javax.swing.JMenu;
  8. import javax.swing.JMenuItem;
  9. import javax.swing.JSeparator;
  10. import java.awt.event.MouseAdapter;
  11. import java.awt.event.MouseEvent;
  12. import javax.swing.JToolBar;
  13. import java.awt.BorderLayout;
  14. import javax.swing.JButton;
  15. import java.awt.event.ActionListener;
  16. import java.awt.event.ActionEvent;
  17. import java.util.Date;
  18.  
  19. import javax.swing.JTextArea;
  20.  
  21. import SecureBlackbox.Base.SBUtils;
  22. import SecureBlackbox.Base.TElMemoryCertStorage;
  23. import SecureBlackbox.Base.TElX509Certificate;
  24. import SecureBlackbox.Base.TElX509CertificateValidator;
  25. import SecureBlackbox.Base.TElX509CertificateValidateResult;
  26. import SecureBlackbox.Base.TSBCertificateValidateEvent;
  27. import SecureBlackbox.Base.TSBCertificateValidity;
  28. import SecureBlackbox.Base.TSBHostRole;
  29. import SecureBlackbox.Base.TSBInteger;
  30. import SecureBlackbox.SSLClient.TElSimpleSSLClient;
  31. import SecureBlackbox.SSLClient.TSBCertificateNeededExEvent;
  32. import SecureBlackbox.SSLCommon.SBSSLConstants;
  33. import SecureBlackbox.SSLCommon.TSBCloseConnectionEvent;
  34.  
  35. import javax.swing.JLabel;
  36.  
  37. import org.freepascal.rtl.TObject;
  38. import SecureBlackbox.HTTPClient.*;
  39. import javax.swing.ImageIcon;
  40.  
  41. public class Main {
  42.    
  43.     TElX509CertificateValidator certificateValidator;
  44.     private JFrame frmSslclientdemo;
  45.     private OptionsDialog odlg;
  46.     private JTextArea tbData;
  47.  
  48.     final int recvBufferSize = 16384;
  49.     private boolean bUseSSL2;
  50.     private boolean bUseSSL3;
  51.     private boolean bUseTLS1;
  52.     private boolean bUseTLS11;
  53.     private boolean bUseOnlyExportable;
  54.     private boolean bAllowAnonymous;
  55.     private boolean bSSLEnabled;
  56.     private boolean bStrictCertValidation;
  57.  
  58.     private TElMemoryCertStorage clientCerts;
  59.     private String host;
  60.     private int port;
  61.  
  62.     private int certIndex;
  63.     private TElSimpleSSLClient secureClient;
  64.     private JLabel lStatus;
  65.    
  66.     /**
  67.      * Launch the application.
  68.      */
  69.     public static void main(String[] args) {
  70.         EventQueue.invokeLater(new Runnable() {
  71.             public void run() {
  72.                 try {
  73.                     Main window = new Main();
  74.                     window.frmSslclientdemo.setVisible(true);
  75.                 } catch (Exception e) {
  76.                     e.printStackTrace();
  77.                 }
  78.             }
  79.         });
  80.     }
  81.  
  82.     /**
  83.      * Create the application.
  84.      */
  85.     public Main() {
  86.         initialize();
  87.     }
  88.  
  89.     /**
  90.      * Initialize the contents of the frame.
  91.      */
  92.     private void initialize() {
  93.         init();
  94.        
  95.         frmSslclientdemo = new JFrame();
  96.         frmSslclientdemo.setTitle("SSLClientDemo");
  97.         frmSslclientdemo.setBounds(100, 100, 526, 401);
  98.         frmSslclientdemo.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  99.        
  100.         JMenuBar menuBar = new JMenuBar();
  101.         frmSslclientdemo.setJMenuBar(menuBar);
  102.        
  103.         JMenu mnConnection = new JMenu("Connection");
  104.         menuBar.add(mnConnection);
  105.        
  106.         JMenuItem mntmConnect = new JMenuItem("Connect");
  107.         mntmConnect.addMouseListener(new MouseAdapter() {
  108.             @Override
  109.             public void mousePressed(MouseEvent arg0) {
  110.                 connect();
  111.             }
  112.         });
  113.         mnConnection.add(mntmConnect);
  114.        
  115.         JMenuItem mntmDisconnect = new JMenuItem("Disconnect");
  116.         mntmDisconnect.addMouseListener(new MouseAdapter() {
  117.             @Override
  118.             public void mousePressed(MouseEvent e) {
  119.                 disconnect();
  120.             }
  121.         });
  122.         mnConnection.add(mntmDisconnect);
  123.        
  124.         JSeparator separator = new JSeparator();
  125.         mnConnection.add(separator);
  126.        
  127.         JMenuItem mntmExit = new JMenuItem("Exit");
  128.         mntmExit.addMouseListener(new MouseAdapter() {
  129.             @Override
  130.             public void mousePressed(MouseEvent e) {
  131.                 frmSslclientdemo.dispose();
  132.             }
  133.         });
  134.         mnConnection.add(mntmExit);
  135.        
  136.         JMenu mnTools = new JMenu("Tools");
  137.         menuBar.add(mnTools);
  138.        
  139.         JMenuItem mntmSslOptions = new JMenuItem("SSL Options");
  140.         mntmSslOptions.addMouseListener(new MouseAdapter() {
  141.             @Override
  142.             public void mousePressed(MouseEvent e) {
  143.                 options();
  144.             }
  145.         });
  146.         mnTools.add(mntmSslOptions);
  147.        
  148.         JMenu mnHelp = new JMenu("Help");
  149.         menuBar.add(mnHelp);
  150.        
  151.         JMenuItem mntmAbout = new JMenuItem("About");
  152.         mntmAbout.addMouseListener(new MouseAdapter() {
  153.             @Override
  154.             public void mousePressed(MouseEvent e) {
  155.                 help();
  156.             }
  157.         });
  158.         mnHelp.add(mntmAbout);
  159.        
  160.         JToolBar toolBar = new JToolBar();
  161.         toolBar.setFloatable(false);
  162.         frmSslclientdemo.getContentPane().add(toolBar, BorderLayout.NORTH);
  163.        
  164.         JButton btnConnect = new JButton("");
  165.         btnConnect.setToolTipText("Connect");
  166.         btnConnect.setIcon(new ImageIcon(Main.class.getResource("/javax/swing/plaf/metal/icons/ocean/computer.gif")));
  167.         btnConnect.addActionListener(new ActionListener() {
  168.             public void actionPerformed(ActionEvent e) {
  169.                 connect();
  170.             }
  171.         });
  172.         toolBar.add(btnConnect);
  173.        
  174.         JButton btnDisconnect = new JButton("");
  175.         btnDisconnect.setToolTipText("Disconnect");
  176.         btnDisconnect.setIcon(new ImageIcon(Main.class.getResource("/javax/swing/plaf/metal/icons/ocean/close.gif")));
  177.         btnDisconnect.addActionListener(new ActionListener() {
  178.             public void actionPerformed(ActionEvent e) {
  179.                 disconnect();
  180.             }
  181.         });
  182.         toolBar.add(btnDisconnect);
  183.        
  184.         JButton btnOptions = new JButton("");
  185.         btnOptions.setToolTipText("Options");
  186.         btnOptions.setIcon(new ImageIcon(Main.class.getResource("/javax/swing/plaf/metal/icons/ocean/homeFolder.gif")));
  187.         btnOptions.addActionListener(new ActionListener() {
  188.             public void actionPerformed(ActionEvent e) {
  189.                 options();
  190.             }
  191.         });
  192.         toolBar.add(btnOptions);
  193.        
  194.         JSeparator separator_1 = new JSeparator();
  195.         toolBar.add(separator_1);
  196.        
  197.         tbData = new JTextArea();
  198.         frmSslclientdemo.getContentPane().add(tbData, BorderLayout.CENTER);
  199.        
  200.         lStatus = new JLabel("Ready");
  201.         frmSslclientdemo.getContentPane().add(lStatus, BorderLayout.SOUTH);
  202.     }
  203.  
  204.     protected void help() {
  205.         AboutDialog dlg = new AboutDialog();
  206.         dlg.setModal(true);
  207.         dlg.setVisible(true);      
  208.     }
  209.  
  210.     protected void options() {
  211.         putSSLOptionsToDialog();
  212.         odlg.setModal(true);
  213.         odlg.setVisible(true);
  214.        
  215.         if (odlg.isOk())
  216.             getSSLOptionsFromDialog();
  217.     }
  218.  
  219.     protected void disconnect() {
  220.         secureClient.close(true);
  221.         status("");
  222.     }
  223.  
  224.     protected void connect() {
  225.         ConnectionDialog dlg = new ConnectionDialog();
  226.        
  227.         dlg.setModal(true);
  228.         dlg.setVisible(true);
  229.        
  230.         if (dlg.isOk())
  231.         {
  232.             setupSSLOptions();
  233.             tbData.setText("");
  234.             host = dlg.getHost();
  235.             port = Integer.parseInt(dlg.getPort());
  236.             bSSLEnabled = dlg.getSSLEnabled();
  237.             status("Resolving host " + host + "...");
  238.             status("Connecting to " + host + "...");
  239.             certIndex = -1;
  240.             for(int i = 0; i < clientCerts.getChainCount(); i++) {
  241.                 int index = clientCerts.getChain(i);
  242.                 if (clientCerts.getCertificate(index).getPrivateKeyExists())
  243.                 {
  244.                     certIndex = index;
  245.                     break;
  246.                 }
  247.             }
  248.            
  249.             secureClient.setAddress(host);
  250.             secureClient.setPort(port);
  251.             secureClient.setEnabled(bSSLEnabled);
  252.             secureClient.open();
  253.            
  254.             java.lang.Thread t = new java.lang.Thread(new Runnable() {
  255.                 public void run() {
  256.                     doRequest(new byte [] {(byte)0xB3, (byte)0x74, (byte)0x12, (byte)0x34, (byte)0xFF, (byte)0x01, (byte)0x31, (byte)0x37, (byte)0x31, (byte)0x31, (byte)0x30, (byte)0x39, (byte)0x39, (byte)0x33, (byte)0x38, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x33, (byte)0x32, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x54, (byte)0x00, (byte)0x00, (byte)0x77});
  257. //                  doRequest(new byte[] {(byte)0xB3, (byte)0x88, (byte)0x1C, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x27});
  258.                    
  259.                     try {
  260.                         Thread.sleep(5000);
  261.                         disconnect();
  262.                     } catch (Exception e) {
  263.                         System.err.println(e.getMessage());
  264.                     }
  265.                    
  266.                 }
  267.             });
  268.            
  269.             t.start();
  270.         }      
  271.     }
  272.    
  273.     private void doRequest(byte[] message) {
  274.         String s;
  275.         byte[] Buffer;
  276.         if (secureClient.getActive())
  277.         {
  278.             status("Sending request ...");
  279.             secureClient.sendData(message, 0, message.length);
  280.             System.out.println("sending " + SBUtils.binaryToString(message));
  281.         }
  282.        
  283.         TSBInteger ToRead = new TSBInteger();
  284.         if (secureClient.getActive())
  285.         {
  286.             try
  287.             {
  288.                 ToRead.value = recvBufferSize;
  289.                 Buffer = new byte[ToRead.value];       
  290.                 secureClient.receiveData(Buffer, ToRead, false);
  291.                 s = SBUtils.binaryToString(Buffer);
  292.                
  293.                 tbData.setText(tbData.getText() + s + "\n");
  294.                 System.out.println("receiving default " + Buffer);
  295.                 System.out.println("receiving toString " + SBUtils.binaryToString(Buffer));
  296.             }
  297.             catch (Exception ex)
  298.             {
  299.                 System.out.println(ex.getMessage());
  300.             }
  301.         }
  302.     }
  303.  
  304.     private void status(String val) {
  305.         lStatus.setText(val);
  306.     }
  307.  
  308.     private void setupSSLOptions() {
  309.         secureClient.setVersions((short)0);
  310.        
  311.         if (bUseSSL2)
  312.         {
  313.             secureClient.setVersions((short) ((byte) secureClient.getVersions() | (byte)SBSSLConstants.sbSSL2));
  314.         }
  315.         if (bUseSSL3)
  316.         {
  317.             secureClient.setVersions((short) ((byte) secureClient.getVersions() | (byte)SBSSLConstants.sbSSL3));
  318.         }
  319.         if (bUseTLS1)
  320.         {
  321.             secureClient.setVersions((short) ((byte) secureClient.getVersions() | (byte)SBSSLConstants.sbTLS1));
  322.         }
  323.         if (bUseTLS11)
  324.         {
  325.             secureClient.setVersions((short) ((byte) secureClient.getVersions() | (byte)SBSSLConstants.sbTLS11));
  326.         }
  327.        
  328.         for(int i = SBSSLConstants.SB_SUITE_FIRST; i <= SBSSLConstants.SB_SUITE_LAST; i++)
  329.         {
  330.             secureClient.setCipherSuite((short)i, false);
  331.         }
  332.        
  333.         if (bUseOnlyExportable)
  334.         {
  335.             secureClient.setCipherSuite(SBSSLConstants.SB_SUITE_RSA_DES_SHA_EXPORT, true);
  336.             secureClient.setCipherSuite(SBSSLConstants.SB_SUITE_RSA_RC2_MD5_EXPORT, true);
  337.             secureClient.setCipherSuite(SBSSLConstants.SB_SUITE_RSA_RC4_MD5_EXPORT, true);
  338.             secureClient.setCipherSuite(SBSSLConstants.SB_SUITE_DHE_RSA_DES_SHA_EXPORT, true);
  339.             secureClient.setCipherSuite(SBSSLConstants.SB_SUITE_DH_DSS_DES_SHA_EXPORT, true);
  340.             secureClient.setCipherSuite(SBSSLConstants.SB_SUITE_DH_RSA_DES_SHA_EXPORT, true);
  341.             secureClient.setCipherSuite(SBSSLConstants.SB_SUITE_DHE_DSS_DES_SHA_EXPORT, true);
  342.             if (bAllowAnonymous)
  343.             {
  344.                 secureClient.setCipherSuite(SBSSLConstants.SB_SUITE_DH_ANON_DES_SHA_EXPORT, true);
  345.                 secureClient.setCipherSuite(SBSSLConstants.SB_SUITE_DH_ANON_RC4_MD5_EXPORT, true);
  346.             }
  347.         }
  348.         else
  349.         {
  350.             for(int i = SBSSLConstants.SB_SUITE_FIRST; i <= SBSSLConstants.SB_SUITE_LAST; i++)
  351.             {
  352.                 secureClient.setCipherSuite((short)i, true);
  353.             }
  354.             if (!bAllowAnonymous)
  355.             {
  356.                 secureClient.setCipherSuite(SBSSLConstants.SB_SUITE_DH_ANON_3DES_SHA, false);
  357.                 secureClient.setCipherSuite(SBSSLConstants.SB_SUITE_DH_ANON_AES128_SHA, false);
  358.                 secureClient.setCipherSuite(SBSSLConstants.SB_SUITE_DH_ANON_AES256_SHA, false);
  359.                 secureClient.setCipherSuite(SBSSLConstants.SB_SUITE_DH_ANON_DES_SHA, false);
  360.                 secureClient.setCipherSuite(SBSSLConstants.SB_SUITE_DH_ANON_DES_SHA_EXPORT, false);
  361.                 secureClient.setCipherSuite(SBSSLConstants.SB_SUITE_DH_ANON_RC4_MD5, false);
  362.                 secureClient.setCipherSuite(SBSSLConstants.SB_SUITE_DH_ANON_RC4_MD5_EXPORT, false);
  363.             }
  364.         }
  365.     }
  366.  
  367.     private void putSSLOptionsToDialog()
  368.     {
  369.         odlg.setCertificates(clientCerts);
  370.         odlg.setSSL2(bUseSSL2);
  371.         odlg.setSSL3(bUseSSL3);
  372.         odlg.setTLS1(bUseTLS1);
  373.         odlg.setTLS11(bUseTLS11);
  374.         odlg.setAllowAnon(bAllowAnonymous);
  375.         odlg.setExportableOnly(bUseOnlyExportable);
  376.         odlg.setStrictCertValidation(bStrictCertValidation);
  377.     }
  378.  
  379.     private void getSSLOptionsFromDialog()
  380.     {
  381.         odlg.getCertificates(clientCerts);
  382.         bUseSSL2 = odlg.getSSL2();
  383.         bUseSSL3 = odlg.getSSL3();
  384.         bUseTLS1 = odlg.getTLS1();
  385.         bUseTLS11 = odlg.getTLS11();
  386.         bAllowAnonymous = odlg.getAllowAnon();
  387.         bUseOnlyExportable = odlg.getExportableOnly();
  388.         bStrictCertValidation = odlg.getStrictCertValidation();
  389.     }
  390.    
  391.     protected void init()
  392.     {
  393.         bUseSSL2 = false;
  394.         bUseSSL3 = true;
  395.         bUseTLS1 = true;
  396.         bUseTLS11 = false;
  397.         bUseOnlyExportable = false;
  398.         bAllowAnonymous = false;
  399.         bStrictCertValidation = false;
  400.         clientCerts = new TElMemoryCertStorage();
  401.         odlg = new OptionsDialog();
  402.         secureClient = new TElSimpleSSLClient();        
  403.        
  404.         //#error Please pick the evaluation license key from <SecureBlackbox>\LicenseKey.txt file and place it here. If the evaluation key expires, you can request an extension using the form on http://www.secureblackbox.com/order/keyreq/
  405.         SBUtils.setLicenseKey("5CFA21A9AD04F5B5A2F0E6077875986733F142102F6077D39CD07AB0A7B994A49BFDF98A9C45DE669FA8C400EB6DF7733D0A02BF091E5C14AE92DA6FE25F2BB9E31EA93BE215A0EE717FF1D9E75CE408183B78715D15EEE726CE596348AFE5E713B03677ED46F01F91EFAEA9CD7E0CB1491F3F9DD711B28E6B6FCE422648AACD62E66E6B9C0F675BBA022C4A6B6B37595B901B36EE59C2171C8C38C9E1F73B9921A12FB0270E88FE19992FBCB0AB51B5E810397FF6E383586CB39ED9D63FC1897D9EBB8982162E5885DB993B772F2D352519C702CBC59E9DADC08A2E6DDC07E9E94A7CFDB193D2D420AE7FAC3F9DBD483327DCE4BDCC721B14BF17D05B20F7F8");
  406.  
  407.         // The following lines are required for HTTP retrieval of CRLs and OCSP in TElX509CertificateValidator to work
  408.         SBHTTPCRL.registerHTTPCRLRetrieverFactory();
  409.         SBHTTPOCSPClient.registerHTTPOCSPClientFactory();
  410.         events();
  411.     }
  412.    
  413.     protected void events() {
  414.         secureClient.setOnCertificateNeededEx(new TSBCertificateNeededExEvent(certNeed));
  415.         secureClient.setOnCertificateValidate(new TSBCertificateValidateEvent(certValid));
  416.         secureClient.setOnCloseConnection(new TSBCloseConnectionEvent(closeConn));
  417.     }
  418.    
  419.     TSBCertificateNeededExEvent.Callback certNeed = new TSBCertificateNeededExEvent.Callback() {       
  420.         public TElX509Certificate tsbCertificateNeededExEventCallback(TObject obj) {
  421.             if (certIndex >= 0)
  422.             {
  423.                 TElX509Certificate Certificate = clientCerts.getCertificate(certIndex);
  424.                 certIndex = clientCerts.getIssuerCertificate(Certificate);
  425.                 return Certificate;
  426.             }
  427.             return null;
  428.         }
  429.     };
  430.    
  431.     TSBCertificateValidateEvent.Callback certValid = new TSBCertificateValidateEvent.Callback() {      
  432.         public void tsbCertificateValidateEventCallback(TObject obj, TElX509Certificate Certificate, TElX509CertificateValidateResult Result) {
  433.             Result.Validity = TSBCertificateValidity.cvOk;
  434.             Result.Reason = 0;
  435.  
  436.             status("Certificate received");
  437.             if ((bStrictCertValidation) && ((Certificate.getChain() == null) || (Certificate.getChain().getCertificate(0) == Certificate)))
  438.             {
  439.                 certificateValidator.validateForSSL(Certificate, secureClient.getRemoteHost(), secureClient.getRemoteIP(), TSBHostRole.hrServer, null, false, new Date(), Result);
  440.             }
  441.         }
  442.     };
  443.    
  444.     TSBCloseConnectionEvent.Callback closeConn = new TSBCloseConnectionEvent.Callback() {
  445.         @Override
  446.         public void tsbCloseConnectionEventCallback(TObject arg0, SecureBlackbox.SSLCommon.TSBCloseReason arg1) {
  447.             status("SSL connection closed");   
  448.            
  449.         }
  450.     };
  451. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement