OSDN Git Service

ADD: Untrusted server connection waring dialog support. (Not test yet)
authorTakuya Ono <takuya-o@users.sourceforge.jp>
Sat, 7 May 2011 20:17:41 +0000 (05:17 +0900)
committerTakuya Ono <takuya-o@users.sourceforge.jp>
Sat, 7 May 2011 20:17:41 +0000 (05:17 +0900)
src/org/jent/checksmtp/ssl/RespondingX509TrustManager.java
src/org/jent/checksmtp/ssl/WarningServerTrustFrame.form [new file with mode: 0644]
src/org/jent/checksmtp/ssl/WarningServerTrustFrame.java [new file with mode: 0644]

index 8858b64..f5810fa 100644 (file)
@@ -4,9 +4,6 @@
  */
 package org.jent.checksmtp.ssl;
 
-import java.io.File;
-import java.io.FileInputStream;
-import java.io.FileNotFoundException;
 import java.security.KeyStore;
 import java.security.cert.CertificateException;
 import java.security.cert.X509Certificate;
@@ -20,12 +17,17 @@ import javax.net.ssl.X509TrustManager;
  * @author takuya-o@users.sourceforge.jp "Takuya Ono"
  */
 public class RespondingX509TrustManager implements X509TrustManager {
+  //Conform status.
+
+  private final int RESULT_UNKNOWN = 0;
+  private final int RESULT_YES = 1;
+  private final int RESULT_NO = 2;
+  private int result = RESULT_UNKNOWN;
   /*
    * The default PKIX X509TrustManager9.  We'll delegate
    * decisions to it, and fall back to the logic in this class if the
    * default X509TrustManager doesn't trust it.
    */
-
   X509TrustManager pkixTrustManager;
 
   public RespondingX509TrustManager() throws Exception {
@@ -33,7 +35,7 @@ public class RespondingX509TrustManager implements X509TrustManager {
     //KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());
     TrustManagerFactory tmf =
             TrustManagerFactory.getInstance("PKIX");
-    tmf.init((KeyStore)null);  //Default KeyStore by null argument.
+    tmf.init((KeyStore) null);  //Default KeyStore by null argument.
 
     TrustManager tms[] = tmf.getTrustManagers();
 
@@ -81,7 +83,7 @@ public class RespondingX509TrustManager implements X509TrustManager {
     if (chain != null) {
       System.out.println("Server certificate chain:");
       for (int i = 0; i < chain.length; i++) {
-        System.out.println("X509Certificate[" + i + "]=" + chain[i].getSubjectDN() );
+        System.out.println("X509Certificate[" + i + "]=" + chain[i].getSubjectDN());
       }
     }
     try {
@@ -92,7 +94,24 @@ public class RespondingX509TrustManager implements X509TrustManager {
        * cert chain.
        */
       //TODO: Pop up waring dialog.
-      System.err.println("Connecting untrusted SMTP server." + chain[0].getSubjectDN() );
+      result = RESULT_UNKNOWN;
+      new WarningServerTrustFrame(this, chain);
+      while (result == RESULT_UNKNOWN) {
+        try {
+          synchronized (this) {
+            wait();
+          }
+        } catch (InterruptedException e) {
+          System.err.println("Waring dialog wait interrupted");
+          e.printStackTrace();
+        }
+      }
+      if ( result == RESULT_YES) {
+        //TODO: 何回もサーバの信頼を確認しないようにする。
+        System.err.println("Connecting untrusted SMTP server." + chain[0].getSubjectDN());
+      } else {
+        throw excep;
+      }
     }
   }
 
@@ -102,4 +121,18 @@ public class RespondingX509TrustManager implements X509TrustManager {
   public X509Certificate[] getAcceptedIssuers() {
     return pkixTrustManager.getAcceptedIssuers();
   }
+
+  public void pushYes() {
+    result = RESULT_YES;
+    notifyResult();
+  }
+
+  public void pushNo() {
+    result = RESULT_NO;
+    notifyResult();
+  }
+
+  private synchronized void notifyResult() {
+      notify();
+  }
 }
diff --git a/src/org/jent/checksmtp/ssl/WarningServerTrustFrame.form b/src/org/jent/checksmtp/ssl/WarningServerTrustFrame.form
new file mode 100644 (file)
index 0000000..02c1325
--- /dev/null
@@ -0,0 +1,115 @@
+<?xml version="1.1" encoding="UTF-8" ?>
+
+<Form version="1.3" maxVersion="1.7" type="org.netbeans.modules.form.forminfo.JFrameFormInfo">
+  <Properties>
+    <Property name="defaultCloseOperation" type="int" value="3"/>
+  </Properties>
+  <SyntheticProperties>
+    <SyntheticProperty name="formSizePolicy" type="int" value="1"/>
+  </SyntheticProperties>
+  <AuxValues>
+    <AuxValue name="FormSettings_autoResourcing" type="java.lang.Integer" value="0"/>
+    <AuxValue name="FormSettings_autoSetComponentName" type="java.lang.Boolean" value="false"/>
+    <AuxValue name="FormSettings_generateFQN" type="java.lang.Boolean" value="true"/>
+    <AuxValue name="FormSettings_generateMnemonicsCode" type="java.lang.Boolean" value="false"/>
+    <AuxValue name="FormSettings_i18nAutoMode" type="java.lang.Boolean" value="false"/>
+    <AuxValue name="FormSettings_layoutCodeTarget" type="java.lang.Integer" value="1"/>
+    <AuxValue name="FormSettings_listenerGenerationStyle" type="java.lang.Integer" value="0"/>
+    <AuxValue name="FormSettings_variablesLocal" type="java.lang.Boolean" value="false"/>
+    <AuxValue name="FormSettings_variablesModifier" type="java.lang.Integer" value="2"/>
+  </AuxValues>
+
+  <Layout>
+    <DimensionLayout dim="0">
+      <Group type="103" groupAlignment="0" attributes="0">
+          <Group type="102" alignment="1" attributes="0">
+              <Group type="103" groupAlignment="1" attributes="0">
+                  <Group type="102" alignment="1" attributes="0">
+                      <EmptySpace max="-2" attributes="0"/>
+                      <Component id="jScrollPane1" pref="340" max="32767" attributes="0"/>
+                      <EmptySpace min="12" pref="12" max="12" attributes="0"/>
+                  </Group>
+                  <Group type="102" alignment="0" attributes="0">
+                      <EmptySpace min="-2" pref="106" max="-2" attributes="0"/>
+                      <Component id="jButtonYes" min="-2" max="-2" attributes="0"/>
+                      <EmptySpace min="-2" pref="53" max="-2" attributes="0"/>
+                      <Component id="jButtonNG" min="-2" max="-2" attributes="0"/>
+                  </Group>
+                  <Group type="102" alignment="0" attributes="0">
+                      <EmptySpace max="-2" attributes="0"/>
+                      <Component id="jLabel1" min="-2" max="-2" attributes="0"/>
+                  </Group>
+                  <Group type="102" alignment="0" attributes="0">
+                      <EmptySpace max="-2" attributes="0"/>
+                      <Component id="jTextField" pref="352" max="32767" attributes="0"/>
+                  </Group>
+              </Group>
+              <EmptySpace min="-2" pref="0" max="-2" attributes="0"/>
+          </Group>
+      </Group>
+    </DimensionLayout>
+    <DimensionLayout dim="1">
+      <Group type="103" groupAlignment="0" attributes="0">
+          <Group type="102" alignment="1" attributes="0">
+              <EmptySpace max="-2" attributes="0"/>
+              <Component id="jLabel1" min="-2" pref="15" max="-2" attributes="0"/>
+              <EmptySpace type="unrelated" max="-2" attributes="0"/>
+              <Component id="jTextField" min="-2" max="-2" attributes="0"/>
+              <EmptySpace max="-2" attributes="0"/>
+              <Component id="jScrollPane1" pref="219" max="32767" attributes="0"/>
+              <EmptySpace max="-2" attributes="0"/>
+              <Group type="103" groupAlignment="3" attributes="0">
+                  <Component id="jButtonNG" alignment="3" min="-2" max="-2" attributes="0"/>
+                  <Component id="jButtonYes" alignment="3" min="-2" max="-2" attributes="0"/>
+              </Group>
+              <EmptySpace max="-2" attributes="0"/>
+          </Group>
+      </Group>
+    </DimensionLayout>
+  </Layout>
+  <SubComponents>
+    <Component class="javax.swing.JButton" name="jButtonYes">
+      <Properties>
+        <Property name="text" type="java.lang.String" value="Yes"/>
+      </Properties>
+      <Events>
+        <EventHandler event="actionPerformed" listener="java.awt.event.ActionListener" parameters="java.awt.event.ActionEvent" handler="jButtonYesActionPerformed"/>
+      </Events>
+    </Component>
+    <Component class="javax.swing.JButton" name="jButtonNG">
+      <Properties>
+        <Property name="text" type="java.lang.String" value="No"/>
+      </Properties>
+      <Events>
+        <EventHandler event="actionPerformed" listener="java.awt.event.ActionListener" parameters="java.awt.event.ActionEvent" handler="jButtonNGActionPerformed"/>
+      </Events>
+    </Component>
+    <Component class="javax.swing.JLabel" name="jLabel1">
+      <Properties>
+        <Property name="text" type="java.lang.String" value="Warning: Untrusted Server Connection"/>
+      </Properties>
+    </Component>
+    <Container class="javax.swing.JScrollPane" name="jScrollPane1">
+      <AuxValues>
+        <AuxValue name="autoScrollPane" type="java.lang.Boolean" value="true"/>
+      </AuxValues>
+
+      <Layout class="org.netbeans.modules.form.compat2.layouts.support.JScrollPaneSupportLayout"/>
+      <SubComponents>
+        <Component class="javax.swing.JTextArea" name="jTextArea">
+          <Properties>
+            <Property name="columns" type="int" value="20"/>
+            <Property name="editable" type="boolean" value="false"/>
+            <Property name="rows" type="int" value="5"/>
+          </Properties>
+        </Component>
+      </SubComponents>
+    </Container>
+    <Component class="javax.swing.JTextField" name="jTextField">
+      <Properties>
+        <Property name="editable" type="boolean" value="false"/>
+        <Property name="text" type="java.lang.String" value="jTextField1"/>
+      </Properties>
+    </Component>
+  </SubComponents>
+</Form>
diff --git a/src/org/jent/checksmtp/ssl/WarningServerTrustFrame.java b/src/org/jent/checksmtp/ssl/WarningServerTrustFrame.java
new file mode 100644 (file)
index 0000000..ef0b786
--- /dev/null
@@ -0,0 +1,162 @@
+/*
+ * To change this template, choose Tools | Templates
+ * and open the template in the editor.
+ */
+
+/*
+ * WarningServerTrustFrame.java
+ *
+ * Created on 2011/05/08, 4:36:36
+ */
+package org.jent.checksmtp.ssl;
+
+import java.awt.Dimension;
+import java.awt.Toolkit;
+import java.security.cert.X509Certificate;
+
+/**
+ *
+ * @author Takuya Ono
+ */
+public class WarningServerTrustFrame extends javax.swing.JFrame {
+  RespondingX509TrustManager trustManager;
+
+  /** Creates new form WarningServerTrustFrame */
+  public WarningServerTrustFrame(RespondingX509TrustManager trustManager, X509Certificate[] chain) {
+    super(); //paranoia statement.
+    this.trustManager = trustManager;
+    initComponents();
+    jTextField.setText(chain[0].getSubjectDN().toString());
+    jTextArea.setText(chain.toString());
+
+    //Move to screen center.
+    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
+    Dimension frameSize = getSize();
+    if (frameSize.height > screenSize.height) {
+      frameSize.height = screenSize.height;
+    }
+    if (frameSize.width > screenSize.width) {
+      frameSize.width = screenSize.width;
+    }
+    this.setBounds(
+            (screenSize.width - frameSize.width) / 2,
+            (screenSize.height - frameSize.height) / 2,
+            frameSize.width, frameSize.height);
+
+    setVisible(true);
+  }
+
+  /** This method is called from within the constructor to
+   * initialize the form.
+   * WARNING: Do NOT modify this code. The content of this method is
+   * always regenerated by the Form Editor.
+   */
+  // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
+  private void initComponents() {
+
+    jButtonYes = new javax.swing.JButton();
+    jButtonNG = new javax.swing.JButton();
+    jLabel1 = new javax.swing.JLabel();
+    jScrollPane1 = new javax.swing.JScrollPane();
+    jTextArea = new javax.swing.JTextArea();
+    jTextField = new javax.swing.JTextField();
+
+    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
+
+    jButtonYes.setText("Yes");
+    jButtonYes.addActionListener(new java.awt.event.ActionListener() {
+      public void actionPerformed(java.awt.event.ActionEvent evt) {
+        jButtonYesActionPerformed(evt);
+      }
+    });
+
+    jButtonNG.setText("No");
+    jButtonNG.addActionListener(new java.awt.event.ActionListener() {
+      public void actionPerformed(java.awt.event.ActionEvent evt) {
+        jButtonNGActionPerformed(evt);
+      }
+    });
+
+    jLabel1.setText("Warning: Untrusted Server Connection");
+
+    jTextArea.setColumns(20);
+    jTextArea.setEditable(false);
+    jTextArea.setRows(5);
+    jScrollPane1.setViewportView(jTextArea);
+
+    jTextField.setEditable(false);
+    jTextField.setText("jTextField1");
+
+    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
+    getContentPane().setLayout(layout);
+    layout.setHorizontalGroup(
+      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
+      .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
+        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
+          .addGroup(layout.createSequentialGroup()
+            .addContainerGap()
+            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 340, Short.MAX_VALUE)
+            .addGap(12, 12, 12))
+          .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
+            .addGap(106, 106, 106)
+            .addComponent(jButtonYes)
+            .addGap(53, 53, 53)
+            .addComponent(jButtonNG))
+          .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
+            .addContainerGap()
+            .addComponent(jLabel1))
+          .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
+            .addContainerGap()
+            .addComponent(jTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 352, Short.MAX_VALUE)))
+        .addGap(0, 0, 0))
+    );
+    layout.setVerticalGroup(
+      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
+      .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
+        .addContainerGap()
+        .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 15, javax.swing.GroupLayout.PREFERRED_SIZE)
+        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
+        .addComponent(jTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
+        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
+        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 219, Short.MAX_VALUE)
+        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
+        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
+          .addComponent(jButtonNG)
+          .addComponent(jButtonYes))
+        .addContainerGap())
+    );
+
+    pack();
+  }// </editor-fold>//GEN-END:initComponents
+
+  private void jButtonYesActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonYesActionPerformed
+    trustManager.pushYes();
+    dispose();
+  }//GEN-LAST:event_jButtonYesActionPerformed
+
+  private void jButtonNGActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonNGActionPerformed
+    trustManager.pushNo();
+    dispose();
+  }//GEN-LAST:event_jButtonNGActionPerformed
+
+  /**
+   * @param args the command line arguments
+   *
+  public static void main(String args[]) {
+    java.awt.EventQueue.invokeLater(new Runnable() {
+
+      public void run() {
+        new WarningServerTrustFrame().setVisible(true);
+      }
+    });
+  }*/
+  
+  // Variables declaration - do not modify//GEN-BEGIN:variables
+  private javax.swing.JButton jButtonNG;
+  private javax.swing.JButton jButtonYes;
+  private javax.swing.JLabel jLabel1;
+  private javax.swing.JScrollPane jScrollPane1;
+  private javax.swing.JTextArea jTextArea;
+  private javax.swing.JTextField jTextField;
+  // End of variables declaration//GEN-END:variables
+}