Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.EventQueue;
- import javax.swing.JFrame;
- import javax.swing.JMenuBar;
- import javax.swing.JMenu;
- import javax.swing.JMenuItem;
- import javax.swing.JSeparator;
- import java.awt.event.MouseAdapter;
- import java.awt.event.MouseEvent;
- import javax.swing.JToolBar;
- import java.awt.BorderLayout;
- import javax.swing.JButton;
- import java.awt.event.ActionListener;
- import java.awt.event.ActionEvent;
- import java.util.Date;
- import javax.swing.JTextArea;
- import SecureBlackbox.Base.SBUtils;
- import SecureBlackbox.Base.TElMemoryCertStorage;
- import SecureBlackbox.Base.TElX509Certificate;
- import SecureBlackbox.Base.TElX509CertificateValidator;
- import SecureBlackbox.Base.TElX509CertificateValidateResult;
- import SecureBlackbox.Base.TSBCertificateValidateEvent;
- import SecureBlackbox.Base.TSBCertificateValidity;
- import SecureBlackbox.Base.TSBHostRole;
- import SecureBlackbox.Base.TSBInteger;
- import SecureBlackbox.SSLClient.TElSimpleSSLClient;
- import SecureBlackbox.SSLClient.TSBCertificateNeededExEvent;
- import SecureBlackbox.SSLCommon.SBSSLConstants;
- import SecureBlackbox.SSLCommon.TSBCloseConnectionEvent;
- import javax.swing.JLabel;
- import org.freepascal.rtl.TObject;
- import SecureBlackbox.HTTPClient.*;
- import javax.swing.ImageIcon;
- public class Main {
- TElX509CertificateValidator certificateValidator;
- private JFrame frmSslclientdemo;
- private OptionsDialog odlg;
- private JTextArea tbData;
- final int recvBufferSize = 16384;
- private boolean bUseSSL2;
- private boolean bUseSSL3;
- private boolean bUseTLS1;
- private boolean bUseTLS11;
- private boolean bUseOnlyExportable;
- private boolean bAllowAnonymous;
- private boolean bSSLEnabled;
- private boolean bStrictCertValidation;
- private TElMemoryCertStorage clientCerts;
- private String host;
- private int port;
- private int certIndex;
- private TElSimpleSSLClient secureClient;
- private JLabel lStatus;
- /**
- * Launch the application.
- */
- public static void main(String[] args) {
- EventQueue.invokeLater(new Runnable() {
- public void run() {
- try {
- Main window = new Main();
- window.frmSslclientdemo.setVisible(true);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- });
- }
- /**
- * Create the application.
- */
- public Main() {
- initialize();
- }
- /**
- * Initialize the contents of the frame.
- */
- private void initialize() {
- init();
- frmSslclientdemo = new JFrame();
- frmSslclientdemo.setTitle("SSLClientDemo");
- frmSslclientdemo.setBounds(100, 100, 526, 401);
- frmSslclientdemo.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- JMenuBar menuBar = new JMenuBar();
- frmSslclientdemo.setJMenuBar(menuBar);
- JMenu mnConnection = new JMenu("Connection");
- menuBar.add(mnConnection);
- JMenuItem mntmConnect = new JMenuItem("Connect");
- mntmConnect.addMouseListener(new MouseAdapter() {
- @Override
- public void mousePressed(MouseEvent arg0) {
- connect();
- }
- });
- mnConnection.add(mntmConnect);
- JMenuItem mntmDisconnect = new JMenuItem("Disconnect");
- mntmDisconnect.addMouseListener(new MouseAdapter() {
- @Override
- public void mousePressed(MouseEvent e) {
- disconnect();
- }
- });
- mnConnection.add(mntmDisconnect);
- JSeparator separator = new JSeparator();
- mnConnection.add(separator);
- JMenuItem mntmExit = new JMenuItem("Exit");
- mntmExit.addMouseListener(new MouseAdapter() {
- @Override
- public void mousePressed(MouseEvent e) {
- frmSslclientdemo.dispose();
- }
- });
- mnConnection.add(mntmExit);
- JMenu mnTools = new JMenu("Tools");
- menuBar.add(mnTools);
- JMenuItem mntmSslOptions = new JMenuItem("SSL Options");
- mntmSslOptions.addMouseListener(new MouseAdapter() {
- @Override
- public void mousePressed(MouseEvent e) {
- options();
- }
- });
- mnTools.add(mntmSslOptions);
- JMenu mnHelp = new JMenu("Help");
- menuBar.add(mnHelp);
- JMenuItem mntmAbout = new JMenuItem("About");
- mntmAbout.addMouseListener(new MouseAdapter() {
- @Override
- public void mousePressed(MouseEvent e) {
- help();
- }
- });
- mnHelp.add(mntmAbout);
- JToolBar toolBar = new JToolBar();
- toolBar.setFloatable(false);
- frmSslclientdemo.getContentPane().add(toolBar, BorderLayout.NORTH);
- JButton btnConnect = new JButton("");
- btnConnect.setToolTipText("Connect");
- btnConnect.setIcon(new ImageIcon(Main.class.getResource("/javax/swing/plaf/metal/icons/ocean/computer.gif")));
- btnConnect.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- connect();
- }
- });
- toolBar.add(btnConnect);
- JButton btnDisconnect = new JButton("");
- btnDisconnect.setToolTipText("Disconnect");
- btnDisconnect.setIcon(new ImageIcon(Main.class.getResource("/javax/swing/plaf/metal/icons/ocean/close.gif")));
- btnDisconnect.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- disconnect();
- }
- });
- toolBar.add(btnDisconnect);
- JButton btnOptions = new JButton("");
- btnOptions.setToolTipText("Options");
- btnOptions.setIcon(new ImageIcon(Main.class.getResource("/javax/swing/plaf/metal/icons/ocean/homeFolder.gif")));
- btnOptions.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- options();
- }
- });
- toolBar.add(btnOptions);
- JSeparator separator_1 = new JSeparator();
- toolBar.add(separator_1);
- tbData = new JTextArea();
- frmSslclientdemo.getContentPane().add(tbData, BorderLayout.CENTER);
- lStatus = new JLabel("Ready");
- frmSslclientdemo.getContentPane().add(lStatus, BorderLayout.SOUTH);
- }
- protected void help() {
- AboutDialog dlg = new AboutDialog();
- dlg.setModal(true);
- dlg.setVisible(true);
- }
- protected void options() {
- putSSLOptionsToDialog();
- odlg.setModal(true);
- odlg.setVisible(true);
- if (odlg.isOk())
- getSSLOptionsFromDialog();
- }
- protected void disconnect() {
- secureClient.close(true);
- status("");
- }
- protected void connect() {
- ConnectionDialog dlg = new ConnectionDialog();
- dlg.setModal(true);
- dlg.setVisible(true);
- if (dlg.isOk())
- {
- setupSSLOptions();
- tbData.setText("");
- host = dlg.getHost();
- port = Integer.parseInt(dlg.getPort());
- bSSLEnabled = dlg.getSSLEnabled();
- status("Resolving host " + host + "...");
- status("Connecting to " + host + "...");
- certIndex = -1;
- for(int i = 0; i < clientCerts.getChainCount(); i++) {
- int index = clientCerts.getChain(i);
- if (clientCerts.getCertificate(index).getPrivateKeyExists())
- {
- certIndex = index;
- break;
- }
- }
- secureClient.setAddress(host);
- secureClient.setPort(port);
- secureClient.setEnabled(bSSLEnabled);
- secureClient.open();
- java.lang.Thread t = new java.lang.Thread(new Runnable() {
- public void run() {
- 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});
- // doRequest(new byte[] {(byte)0xB3, (byte)0x88, (byte)0x1C, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x27});
- try {
- Thread.sleep(5000);
- disconnect();
- } catch (Exception e) {
- System.err.println(e.getMessage());
- }
- }
- });
- t.start();
- }
- }
- private void doRequest(byte[] message) {
- String s;
- byte[] Buffer;
- if (secureClient.getActive())
- {
- status("Sending request ...");
- secureClient.sendData(message, 0, message.length);
- System.out.println("sending " + SBUtils.binaryToString(message));
- }
- TSBInteger ToRead = new TSBInteger();
- if (secureClient.getActive())
- {
- try
- {
- ToRead.value = recvBufferSize;
- Buffer = new byte[ToRead.value];
- secureClient.receiveData(Buffer, ToRead, false);
- s = SBUtils.binaryToString(Buffer);
- tbData.setText(tbData.getText() + s + "\n");
- System.out.println("receiving default " + Buffer);
- System.out.println("receiving toString " + SBUtils.binaryToString(Buffer));
- }
- catch (Exception ex)
- {
- System.out.println(ex.getMessage());
- }
- }
- }
- private void status(String val) {
- lStatus.setText(val);
- }
- private void setupSSLOptions() {
- secureClient.setVersions((short)0);
- if (bUseSSL2)
- {
- secureClient.setVersions((short) ((byte) secureClient.getVersions() | (byte)SBSSLConstants.sbSSL2));
- }
- if (bUseSSL3)
- {
- secureClient.setVersions((short) ((byte) secureClient.getVersions() | (byte)SBSSLConstants.sbSSL3));
- }
- if (bUseTLS1)
- {
- secureClient.setVersions((short) ((byte) secureClient.getVersions() | (byte)SBSSLConstants.sbTLS1));
- }
- if (bUseTLS11)
- {
- secureClient.setVersions((short) ((byte) secureClient.getVersions() | (byte)SBSSLConstants.sbTLS11));
- }
- for(int i = SBSSLConstants.SB_SUITE_FIRST; i <= SBSSLConstants.SB_SUITE_LAST; i++)
- {
- secureClient.setCipherSuite((short)i, false);
- }
- if (bUseOnlyExportable)
- {
- secureClient.setCipherSuite(SBSSLConstants.SB_SUITE_RSA_DES_SHA_EXPORT, true);
- secureClient.setCipherSuite(SBSSLConstants.SB_SUITE_RSA_RC2_MD5_EXPORT, true);
- secureClient.setCipherSuite(SBSSLConstants.SB_SUITE_RSA_RC4_MD5_EXPORT, true);
- secureClient.setCipherSuite(SBSSLConstants.SB_SUITE_DHE_RSA_DES_SHA_EXPORT, true);
- secureClient.setCipherSuite(SBSSLConstants.SB_SUITE_DH_DSS_DES_SHA_EXPORT, true);
- secureClient.setCipherSuite(SBSSLConstants.SB_SUITE_DH_RSA_DES_SHA_EXPORT, true);
- secureClient.setCipherSuite(SBSSLConstants.SB_SUITE_DHE_DSS_DES_SHA_EXPORT, true);
- if (bAllowAnonymous)
- {
- secureClient.setCipherSuite(SBSSLConstants.SB_SUITE_DH_ANON_DES_SHA_EXPORT, true);
- secureClient.setCipherSuite(SBSSLConstants.SB_SUITE_DH_ANON_RC4_MD5_EXPORT, true);
- }
- }
- else
- {
- for(int i = SBSSLConstants.SB_SUITE_FIRST; i <= SBSSLConstants.SB_SUITE_LAST; i++)
- {
- secureClient.setCipherSuite((short)i, true);
- }
- if (!bAllowAnonymous)
- {
- secureClient.setCipherSuite(SBSSLConstants.SB_SUITE_DH_ANON_3DES_SHA, false);
- secureClient.setCipherSuite(SBSSLConstants.SB_SUITE_DH_ANON_AES128_SHA, false);
- secureClient.setCipherSuite(SBSSLConstants.SB_SUITE_DH_ANON_AES256_SHA, false);
- secureClient.setCipherSuite(SBSSLConstants.SB_SUITE_DH_ANON_DES_SHA, false);
- secureClient.setCipherSuite(SBSSLConstants.SB_SUITE_DH_ANON_DES_SHA_EXPORT, false);
- secureClient.setCipherSuite(SBSSLConstants.SB_SUITE_DH_ANON_RC4_MD5, false);
- secureClient.setCipherSuite(SBSSLConstants.SB_SUITE_DH_ANON_RC4_MD5_EXPORT, false);
- }
- }
- }
- private void putSSLOptionsToDialog()
- {
- odlg.setCertificates(clientCerts);
- odlg.setSSL2(bUseSSL2);
- odlg.setSSL3(bUseSSL3);
- odlg.setTLS1(bUseTLS1);
- odlg.setTLS11(bUseTLS11);
- odlg.setAllowAnon(bAllowAnonymous);
- odlg.setExportableOnly(bUseOnlyExportable);
- odlg.setStrictCertValidation(bStrictCertValidation);
- }
- private void getSSLOptionsFromDialog()
- {
- odlg.getCertificates(clientCerts);
- bUseSSL2 = odlg.getSSL2();
- bUseSSL3 = odlg.getSSL3();
- bUseTLS1 = odlg.getTLS1();
- bUseTLS11 = odlg.getTLS11();
- bAllowAnonymous = odlg.getAllowAnon();
- bUseOnlyExportable = odlg.getExportableOnly();
- bStrictCertValidation = odlg.getStrictCertValidation();
- }
- protected void init()
- {
- bUseSSL2 = false;
- bUseSSL3 = true;
- bUseTLS1 = true;
- bUseTLS11 = false;
- bUseOnlyExportable = false;
- bAllowAnonymous = false;
- bStrictCertValidation = false;
- clientCerts = new TElMemoryCertStorage();
- odlg = new OptionsDialog();
- secureClient = new TElSimpleSSLClient();
- //#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/
- SBUtils.setLicenseKey("5CFA21A9AD04F5B5A2F0E6077875986733F142102F6077D39CD07AB0A7B994A49BFDF98A9C45DE669FA8C400EB6DF7733D0A02BF091E5C14AE92DA6FE25F2BB9E31EA93BE215A0EE717FF1D9E75CE408183B78715D15EEE726CE596348AFE5E713B03677ED46F01F91EFAEA9CD7E0CB1491F3F9DD711B28E6B6FCE422648AACD62E66E6B9C0F675BBA022C4A6B6B37595B901B36EE59C2171C8C38C9E1F73B9921A12FB0270E88FE19992FBCB0AB51B5E810397FF6E383586CB39ED9D63FC1897D9EBB8982162E5885DB993B772F2D352519C702CBC59E9DADC08A2E6DDC07E9E94A7CFDB193D2D420AE7FAC3F9DBD483327DCE4BDCC721B14BF17D05B20F7F8");
- // The following lines are required for HTTP retrieval of CRLs and OCSP in TElX509CertificateValidator to work
- SBHTTPCRL.registerHTTPCRLRetrieverFactory();
- SBHTTPOCSPClient.registerHTTPOCSPClientFactory();
- events();
- }
- protected void events() {
- secureClient.setOnCertificateNeededEx(new TSBCertificateNeededExEvent(certNeed));
- secureClient.setOnCertificateValidate(new TSBCertificateValidateEvent(certValid));
- secureClient.setOnCloseConnection(new TSBCloseConnectionEvent(closeConn));
- }
- TSBCertificateNeededExEvent.Callback certNeed = new TSBCertificateNeededExEvent.Callback() {
- public TElX509Certificate tsbCertificateNeededExEventCallback(TObject obj) {
- if (certIndex >= 0)
- {
- TElX509Certificate Certificate = clientCerts.getCertificate(certIndex);
- certIndex = clientCerts.getIssuerCertificate(Certificate);
- return Certificate;
- }
- return null;
- }
- };
- TSBCertificateValidateEvent.Callback certValid = new TSBCertificateValidateEvent.Callback() {
- public void tsbCertificateValidateEventCallback(TObject obj, TElX509Certificate Certificate, TElX509CertificateValidateResult Result) {
- Result.Validity = TSBCertificateValidity.cvOk;
- Result.Reason = 0;
- status("Certificate received");
- if ((bStrictCertValidation) && ((Certificate.getChain() == null) || (Certificate.getChain().getCertificate(0) == Certificate)))
- {
- certificateValidator.validateForSSL(Certificate, secureClient.getRemoteHost(), secureClient.getRemoteIP(), TSBHostRole.hrServer, null, false, new Date(), Result);
- }
- }
- };
- TSBCloseConnectionEvent.Callback closeConn = new TSBCloseConnectionEvent.Callback() {
- @Override
- public void tsbCloseConnectionEventCallback(TObject arg0, SecureBlackbox.SSLCommon.TSBCloseReason arg1) {
- status("SSL connection closed");
- }
- };
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement