Mail送信先確認プログラムβプロジェクト
Revision | 888b67347c880448e4d3c008562feca33edaead3 (tree) |
---|---|
Zeit | 2011-05-08 05:17:41 |
Autor | Takuya Ono <takuya-o@user...> |
Commiter | Takuya Ono |
ADD: Untrusted server connection waring dialog support. (Not test yet)
@@ -4,9 +4,6 @@ | ||
4 | 4 | */ |
5 | 5 | package org.jent.checksmtp.ssl; |
6 | 6 | |
7 | -import java.io.File; | |
8 | -import java.io.FileInputStream; | |
9 | -import java.io.FileNotFoundException; | |
10 | 7 | import java.security.KeyStore; |
11 | 8 | import java.security.cert.CertificateException; |
12 | 9 | import java.security.cert.X509Certificate; |
@@ -20,12 +17,17 @@ import javax.net.ssl.X509TrustManager; | ||
20 | 17 | * @author takuya-o@users.sourceforge.jp "Takuya Ono" |
21 | 18 | */ |
22 | 19 | public class RespondingX509TrustManager implements X509TrustManager { |
20 | + //Conform status. | |
21 | + | |
22 | + private final int RESULT_UNKNOWN = 0; | |
23 | + private final int RESULT_YES = 1; | |
24 | + private final int RESULT_NO = 2; | |
25 | + private int result = RESULT_UNKNOWN; | |
23 | 26 | /* |
24 | 27 | * The default PKIX X509TrustManager9. We'll delegate |
25 | 28 | * decisions to it, and fall back to the logic in this class if the |
26 | 29 | * default X509TrustManager doesn't trust it. |
27 | 30 | */ |
28 | - | |
29 | 31 | X509TrustManager pkixTrustManager; |
30 | 32 | |
31 | 33 | public RespondingX509TrustManager() throws Exception { |
@@ -33,7 +35,7 @@ public class RespondingX509TrustManager implements X509TrustManager { | ||
33 | 35 | //KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType()); |
34 | 36 | TrustManagerFactory tmf = |
35 | 37 | TrustManagerFactory.getInstance("PKIX"); |
36 | - tmf.init((KeyStore)null); //Default KeyStore by null argument. | |
38 | + tmf.init((KeyStore) null); //Default KeyStore by null argument. | |
37 | 39 | |
38 | 40 | TrustManager tms[] = tmf.getTrustManagers(); |
39 | 41 |
@@ -81,7 +83,7 @@ public class RespondingX509TrustManager implements X509TrustManager { | ||
81 | 83 | if (chain != null) { |
82 | 84 | System.out.println("Server certificate chain:"); |
83 | 85 | for (int i = 0; i < chain.length; i++) { |
84 | - System.out.println("X509Certificate[" + i + "]=" + chain[i].getSubjectDN() ); | |
86 | + System.out.println("X509Certificate[" + i + "]=" + chain[i].getSubjectDN()); | |
85 | 87 | } |
86 | 88 | } |
87 | 89 | try { |
@@ -92,7 +94,24 @@ public class RespondingX509TrustManager implements X509TrustManager { | ||
92 | 94 | * cert chain. |
93 | 95 | */ |
94 | 96 | //TODO: Pop up waring dialog. |
95 | - System.err.println("Connecting untrusted SMTP server." + chain[0].getSubjectDN() ); | |
97 | + result = RESULT_UNKNOWN; | |
98 | + new WarningServerTrustFrame(this, chain); | |
99 | + while (result == RESULT_UNKNOWN) { | |
100 | + try { | |
101 | + synchronized (this) { | |
102 | + wait(); | |
103 | + } | |
104 | + } catch (InterruptedException e) { | |
105 | + System.err.println("Waring dialog wait interrupted"); | |
106 | + e.printStackTrace(); | |
107 | + } | |
108 | + } | |
109 | + if ( result == RESULT_YES) { | |
110 | + //TODO: 何回もサーバの信頼を確認しないようにする。 | |
111 | + System.err.println("Connecting untrusted SMTP server." + chain[0].getSubjectDN()); | |
112 | + } else { | |
113 | + throw excep; | |
114 | + } | |
96 | 115 | } |
97 | 116 | } |
98 | 117 |
@@ -102,4 +121,18 @@ public class RespondingX509TrustManager implements X509TrustManager { | ||
102 | 121 | public X509Certificate[] getAcceptedIssuers() { |
103 | 122 | return pkixTrustManager.getAcceptedIssuers(); |
104 | 123 | } |
124 | + | |
125 | + public void pushYes() { | |
126 | + result = RESULT_YES; | |
127 | + notifyResult(); | |
128 | + } | |
129 | + | |
130 | + public void pushNo() { | |
131 | + result = RESULT_NO; | |
132 | + notifyResult(); | |
133 | + } | |
134 | + | |
135 | + private synchronized void notifyResult() { | |
136 | + notify(); | |
137 | + } | |
105 | 138 | } |
@@ -0,0 +1,115 @@ | ||
1 | +<?xml version="1.1" encoding="UTF-8" ?> | |
2 | + | |
3 | +<Form version="1.3" maxVersion="1.7" type="org.netbeans.modules.form.forminfo.JFrameFormInfo"> | |
4 | + <Properties> | |
5 | + <Property name="defaultCloseOperation" type="int" value="3"/> | |
6 | + </Properties> | |
7 | + <SyntheticProperties> | |
8 | + <SyntheticProperty name="formSizePolicy" type="int" value="1"/> | |
9 | + </SyntheticProperties> | |
10 | + <AuxValues> | |
11 | + <AuxValue name="FormSettings_autoResourcing" type="java.lang.Integer" value="0"/> | |
12 | + <AuxValue name="FormSettings_autoSetComponentName" type="java.lang.Boolean" value="false"/> | |
13 | + <AuxValue name="FormSettings_generateFQN" type="java.lang.Boolean" value="true"/> | |
14 | + <AuxValue name="FormSettings_generateMnemonicsCode" type="java.lang.Boolean" value="false"/> | |
15 | + <AuxValue name="FormSettings_i18nAutoMode" type="java.lang.Boolean" value="false"/> | |
16 | + <AuxValue name="FormSettings_layoutCodeTarget" type="java.lang.Integer" value="1"/> | |
17 | + <AuxValue name="FormSettings_listenerGenerationStyle" type="java.lang.Integer" value="0"/> | |
18 | + <AuxValue name="FormSettings_variablesLocal" type="java.lang.Boolean" value="false"/> | |
19 | + <AuxValue name="FormSettings_variablesModifier" type="java.lang.Integer" value="2"/> | |
20 | + </AuxValues> | |
21 | + | |
22 | + <Layout> | |
23 | + <DimensionLayout dim="0"> | |
24 | + <Group type="103" groupAlignment="0" attributes="0"> | |
25 | + <Group type="102" alignment="1" attributes="0"> | |
26 | + <Group type="103" groupAlignment="1" attributes="0"> | |
27 | + <Group type="102" alignment="1" attributes="0"> | |
28 | + <EmptySpace max="-2" attributes="0"/> | |
29 | + <Component id="jScrollPane1" pref="340" max="32767" attributes="0"/> | |
30 | + <EmptySpace min="12" pref="12" max="12" attributes="0"/> | |
31 | + </Group> | |
32 | + <Group type="102" alignment="0" attributes="0"> | |
33 | + <EmptySpace min="-2" pref="106" max="-2" attributes="0"/> | |
34 | + <Component id="jButtonYes" min="-2" max="-2" attributes="0"/> | |
35 | + <EmptySpace min="-2" pref="53" max="-2" attributes="0"/> | |
36 | + <Component id="jButtonNG" min="-2" max="-2" attributes="0"/> | |
37 | + </Group> | |
38 | + <Group type="102" alignment="0" attributes="0"> | |
39 | + <EmptySpace max="-2" attributes="0"/> | |
40 | + <Component id="jLabel1" min="-2" max="-2" attributes="0"/> | |
41 | + </Group> | |
42 | + <Group type="102" alignment="0" attributes="0"> | |
43 | + <EmptySpace max="-2" attributes="0"/> | |
44 | + <Component id="jTextField" pref="352" max="32767" attributes="0"/> | |
45 | + </Group> | |
46 | + </Group> | |
47 | + <EmptySpace min="-2" pref="0" max="-2" attributes="0"/> | |
48 | + </Group> | |
49 | + </Group> | |
50 | + </DimensionLayout> | |
51 | + <DimensionLayout dim="1"> | |
52 | + <Group type="103" groupAlignment="0" attributes="0"> | |
53 | + <Group type="102" alignment="1" attributes="0"> | |
54 | + <EmptySpace max="-2" attributes="0"/> | |
55 | + <Component id="jLabel1" min="-2" pref="15" max="-2" attributes="0"/> | |
56 | + <EmptySpace type="unrelated" max="-2" attributes="0"/> | |
57 | + <Component id="jTextField" min="-2" max="-2" attributes="0"/> | |
58 | + <EmptySpace max="-2" attributes="0"/> | |
59 | + <Component id="jScrollPane1" pref="219" max="32767" attributes="0"/> | |
60 | + <EmptySpace max="-2" attributes="0"/> | |
61 | + <Group type="103" groupAlignment="3" attributes="0"> | |
62 | + <Component id="jButtonNG" alignment="3" min="-2" max="-2" attributes="0"/> | |
63 | + <Component id="jButtonYes" alignment="3" min="-2" max="-2" attributes="0"/> | |
64 | + </Group> | |
65 | + <EmptySpace max="-2" attributes="0"/> | |
66 | + </Group> | |
67 | + </Group> | |
68 | + </DimensionLayout> | |
69 | + </Layout> | |
70 | + <SubComponents> | |
71 | + <Component class="javax.swing.JButton" name="jButtonYes"> | |
72 | + <Properties> | |
73 | + <Property name="text" type="java.lang.String" value="Yes"/> | |
74 | + </Properties> | |
75 | + <Events> | |
76 | + <EventHandler event="actionPerformed" listener="java.awt.event.ActionListener" parameters="java.awt.event.ActionEvent" handler="jButtonYesActionPerformed"/> | |
77 | + </Events> | |
78 | + </Component> | |
79 | + <Component class="javax.swing.JButton" name="jButtonNG"> | |
80 | + <Properties> | |
81 | + <Property name="text" type="java.lang.String" value="No"/> | |
82 | + </Properties> | |
83 | + <Events> | |
84 | + <EventHandler event="actionPerformed" listener="java.awt.event.ActionListener" parameters="java.awt.event.ActionEvent" handler="jButtonNGActionPerformed"/> | |
85 | + </Events> | |
86 | + </Component> | |
87 | + <Component class="javax.swing.JLabel" name="jLabel1"> | |
88 | + <Properties> | |
89 | + <Property name="text" type="java.lang.String" value="Warning: Untrusted Server Connection"/> | |
90 | + </Properties> | |
91 | + </Component> | |
92 | + <Container class="javax.swing.JScrollPane" name="jScrollPane1"> | |
93 | + <AuxValues> | |
94 | + <AuxValue name="autoScrollPane" type="java.lang.Boolean" value="true"/> | |
95 | + </AuxValues> | |
96 | + | |
97 | + <Layout class="org.netbeans.modules.form.compat2.layouts.support.JScrollPaneSupportLayout"/> | |
98 | + <SubComponents> | |
99 | + <Component class="javax.swing.JTextArea" name="jTextArea"> | |
100 | + <Properties> | |
101 | + <Property name="columns" type="int" value="20"/> | |
102 | + <Property name="editable" type="boolean" value="false"/> | |
103 | + <Property name="rows" type="int" value="5"/> | |
104 | + </Properties> | |
105 | + </Component> | |
106 | + </SubComponents> | |
107 | + </Container> | |
108 | + <Component class="javax.swing.JTextField" name="jTextField"> | |
109 | + <Properties> | |
110 | + <Property name="editable" type="boolean" value="false"/> | |
111 | + <Property name="text" type="java.lang.String" value="jTextField1"/> | |
112 | + </Properties> | |
113 | + </Component> | |
114 | + </SubComponents> | |
115 | +</Form> |
@@ -0,0 +1,162 @@ | ||
1 | +/* | |
2 | + * To change this template, choose Tools | Templates | |
3 | + * and open the template in the editor. | |
4 | + */ | |
5 | + | |
6 | +/* | |
7 | + * WarningServerTrustFrame.java | |
8 | + * | |
9 | + * Created on 2011/05/08, 4:36:36 | |
10 | + */ | |
11 | +package org.jent.checksmtp.ssl; | |
12 | + | |
13 | +import java.awt.Dimension; | |
14 | +import java.awt.Toolkit; | |
15 | +import java.security.cert.X509Certificate; | |
16 | + | |
17 | +/** | |
18 | + * | |
19 | + * @author Takuya Ono | |
20 | + */ | |
21 | +public class WarningServerTrustFrame extends javax.swing.JFrame { | |
22 | + RespondingX509TrustManager trustManager; | |
23 | + | |
24 | + /** Creates new form WarningServerTrustFrame */ | |
25 | + public WarningServerTrustFrame(RespondingX509TrustManager trustManager, X509Certificate[] chain) { | |
26 | + super(); //paranoia statement. | |
27 | + this.trustManager = trustManager; | |
28 | + initComponents(); | |
29 | + jTextField.setText(chain[0].getSubjectDN().toString()); | |
30 | + jTextArea.setText(chain.toString()); | |
31 | + | |
32 | + //Move to screen center. | |
33 | + Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); | |
34 | + Dimension frameSize = getSize(); | |
35 | + if (frameSize.height > screenSize.height) { | |
36 | + frameSize.height = screenSize.height; | |
37 | + } | |
38 | + if (frameSize.width > screenSize.width) { | |
39 | + frameSize.width = screenSize.width; | |
40 | + } | |
41 | + this.setBounds( | |
42 | + (screenSize.width - frameSize.width) / 2, | |
43 | + (screenSize.height - frameSize.height) / 2, | |
44 | + frameSize.width, frameSize.height); | |
45 | + | |
46 | + setVisible(true); | |
47 | + } | |
48 | + | |
49 | + /** This method is called from within the constructor to | |
50 | + * initialize the form. | |
51 | + * WARNING: Do NOT modify this code. The content of this method is | |
52 | + * always regenerated by the Form Editor. | |
53 | + */ | |
54 | + // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents | |
55 | + private void initComponents() { | |
56 | + | |
57 | + jButtonYes = new javax.swing.JButton(); | |
58 | + jButtonNG = new javax.swing.JButton(); | |
59 | + jLabel1 = new javax.swing.JLabel(); | |
60 | + jScrollPane1 = new javax.swing.JScrollPane(); | |
61 | + jTextArea = new javax.swing.JTextArea(); | |
62 | + jTextField = new javax.swing.JTextField(); | |
63 | + | |
64 | + setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); | |
65 | + | |
66 | + jButtonYes.setText("Yes"); | |
67 | + jButtonYes.addActionListener(new java.awt.event.ActionListener() { | |
68 | + public void actionPerformed(java.awt.event.ActionEvent evt) { | |
69 | + jButtonYesActionPerformed(evt); | |
70 | + } | |
71 | + }); | |
72 | + | |
73 | + jButtonNG.setText("No"); | |
74 | + jButtonNG.addActionListener(new java.awt.event.ActionListener() { | |
75 | + public void actionPerformed(java.awt.event.ActionEvent evt) { | |
76 | + jButtonNGActionPerformed(evt); | |
77 | + } | |
78 | + }); | |
79 | + | |
80 | + jLabel1.setText("Warning: Untrusted Server Connection"); | |
81 | + | |
82 | + jTextArea.setColumns(20); | |
83 | + jTextArea.setEditable(false); | |
84 | + jTextArea.setRows(5); | |
85 | + jScrollPane1.setViewportView(jTextArea); | |
86 | + | |
87 | + jTextField.setEditable(false); | |
88 | + jTextField.setText("jTextField1"); | |
89 | + | |
90 | + javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); | |
91 | + getContentPane().setLayout(layout); | |
92 | + layout.setHorizontalGroup( | |
93 | + layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) | |
94 | + .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() | |
95 | + .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) | |
96 | + .addGroup(layout.createSequentialGroup() | |
97 | + .addContainerGap() | |
98 | + .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 340, Short.MAX_VALUE) | |
99 | + .addGap(12, 12, 12)) | |
100 | + .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup() | |
101 | + .addGap(106, 106, 106) | |
102 | + .addComponent(jButtonYes) | |
103 | + .addGap(53, 53, 53) | |
104 | + .addComponent(jButtonNG)) | |
105 | + .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup() | |
106 | + .addContainerGap() | |
107 | + .addComponent(jLabel1)) | |
108 | + .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup() | |
109 | + .addContainerGap() | |
110 | + .addComponent(jTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 352, Short.MAX_VALUE))) | |
111 | + .addGap(0, 0, 0)) | |
112 | + ); | |
113 | + layout.setVerticalGroup( | |
114 | + layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) | |
115 | + .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() | |
116 | + .addContainerGap() | |
117 | + .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 15, javax.swing.GroupLayout.PREFERRED_SIZE) | |
118 | + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) | |
119 | + .addComponent(jTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) | |
120 | + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) | |
121 | + .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 219, Short.MAX_VALUE) | |
122 | + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) | |
123 | + .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) | |
124 | + .addComponent(jButtonNG) | |
125 | + .addComponent(jButtonYes)) | |
126 | + .addContainerGap()) | |
127 | + ); | |
128 | + | |
129 | + pack(); | |
130 | + }// </editor-fold>//GEN-END:initComponents | |
131 | + | |
132 | + private void jButtonYesActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonYesActionPerformed | |
133 | + trustManager.pushYes(); | |
134 | + dispose(); | |
135 | + }//GEN-LAST:event_jButtonYesActionPerformed | |
136 | + | |
137 | + private void jButtonNGActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonNGActionPerformed | |
138 | + trustManager.pushNo(); | |
139 | + dispose(); | |
140 | + }//GEN-LAST:event_jButtonNGActionPerformed | |
141 | + | |
142 | + /** | |
143 | + * @param args the command line arguments | |
144 | + * | |
145 | + public static void main(String args[]) { | |
146 | + java.awt.EventQueue.invokeLater(new Runnable() { | |
147 | + | |
148 | + public void run() { | |
149 | + new WarningServerTrustFrame().setVisible(true); | |
150 | + } | |
151 | + }); | |
152 | + }*/ | |
153 | + | |
154 | + // Variables declaration - do not modify//GEN-BEGIN:variables | |
155 | + private javax.swing.JButton jButtonNG; | |
156 | + private javax.swing.JButton jButtonYes; | |
157 | + private javax.swing.JLabel jLabel1; | |
158 | + private javax.swing.JScrollPane jScrollPane1; | |
159 | + private javax.swing.JTextArea jTextArea; | |
160 | + private javax.swing.JTextField jTextField; | |
161 | + // End of variables declaration//GEN-END:variables | |
162 | +} |