summaryrefslogtreecommitdiffstats
path: root/base/console/src/com/netscape/admin/certsrv/config/install/WIPasteCertPage.java
blob: 063382f3268b9ce65644a9161571ab04264483ca (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
// --- BEGIN COPYRIGHT BLOCK ---
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; version 2 of the License.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License along
// with this program; if not, write to the Free Software Foundation, Inc.,
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
//
// (C) 2007 Red Hat, Inc.
// All rights reserved.
// --- END COPYRIGHT BLOCK ---
package com.netscape.admin.certsrv.config.install;

import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
import javax.swing.*;
import javax.swing.text.*;
import com.netscape.admin.certsrv.*;
import com.netscape.admin.certsrv.connection.*;
import com.netscape.admin.certsrv.wizard.*;
import com.netscape.certsrv.common.*;
import com.netscape.admin.certsrv.task.*;
import com.netscape.management.client.console.*;
import com.netscape.management.client.util.*;

/**
 * This page is to install the certificate in the internal token. The user can 
 * import the cert from the file, paste the Base 64 encoded blob in the 
 * text area or get the cert from the CMS where the request was sent.
 *
 * @author Christine Ho
 * @version $Revision$, $Date$
 * @see com.netscape.admin.certsrv.config.install
 */
class WIPasteCertPage extends WizardBasePanel implements IWizardPanel {
	static private int transId = 1;
    private JRadioButton mFileBtn;
    private JRadioButton mBase64Btn;
    private JTextField mFileText;
    private JTextArea mBase64Text;
    private JButton mPaste;
    private String mCertContent = "";
    private String mCertFilePath = "";
    protected String mPanelName;
    protected String mHelpIndex;
    protected Color mActiveColor;
    protected JTextArea introLbl;
    
    protected JTextField mHostText, mPortText, mRIDText;
    protected JLabel mHostLbl, mPortLbl, mRIDLbl;
    protected String mHost, mPort, mRID;
    protected JLabel mSSLText;
    protected JCheckBox mSSL; // ssl or not
    protected JLabel mQueryText;
    protected JRadioButton mQueryBtn;    

    public static final int MAX_PORT = 65535;
    public static final int MIN_PORT = 1;

    WIPasteCertPage(String panelName) {
        super(panelName);
        mPanelName = panelName;
        init();
    }

    public boolean isLastPage() {
        return false;
    }

    public boolean initializePanel(WizardInfo info) {
        InstallWizardInfo wizardInfo = (InstallWizardInfo)info;
        if (mQueryBtn.isSelected())
            enableFields(true, mActiveColor);
        else
            enableFields(false, getBackground());

		String host = wizardInfo.getCMHost();
		if (host != null && !host.equals(""))
			mHostText.setText(host);
		String port = wizardInfo.getCMEEPort();
		if (port != null && !port.equals(""))
			mPortText.setText(port);
		
		String portType = wizardInfo.getCMEEType();
		if (portType != null && portType.equals("http"))
			mSSL.setSelected(false);

		String rid = wizardInfo.getRequestID();
		if (rid != null && !rid.equals(""))
			mRIDText.setText(rid);

        setBorder(makeTitledBorder(mPanelName));
        return true; 
    }

    public boolean validatePanel() {
        if (mBase64Btn.isSelected()) {
            mCertContent = mBase64Text.getText().trim();
            if (mCertContent.equals("")) {
                setErrorMessage("B64EEMPTY");
                return false;
            }
        } else if (mFileBtn.isSelected()) {
            mCertFilePath = mFileText.getText().trim();
            if (mCertFilePath.equals("")) {
                setErrorMessage("EMPTYFILE");
                return false;
            }
        } else if (mQueryBtn.isSelected()) {
			mHost = mHostText.getText().trim();
			mPort = mPortText.getText().trim();
			mRID = mRIDText.getText().trim();

			if (mRID.equals("")) {
				setErrorMessage("BLANKRID");
				return false;
			}
			try {
				int ridnumber = Integer.parseInt(mRID);
			} catch (NumberFormatException e) {
				setErrorMessage("INVALIDRID");
				return false;
			}

			if (mHost.equals("")) {
				setErrorMessage("BLANKHOST");
				return false;
			}
			if (mPort.equals("")) {
				setErrorMessage("BLANKPORT");
				return false;
			}

			try {
				int portnumber = Integer.parseInt(mPort);
				if (portnumber < MIN_PORT || portnumber > MAX_PORT) {
					setErrorMessage("OUTOFRANGE");
					return false;
				}
			} catch (NumberFormatException e) {
				setErrorMessage("INVALIDPORT");
				return false;
			}
			return true;
		}
		return true;

	}

    public boolean concludePanel(WizardInfo info) {
        InstallWizardInfo wizardInfo = (InstallWizardInfo)info;
        String rawData = ConfigConstants.TASKID+"="+TaskId.TASK_GET_CERT_CONTENT;
        rawData = rawData+"&"+ConfigConstants.OPTYPE+"="+OpDef.OP_READ;
		String reqType = wizardInfo.getCertType();
        rawData = rawData+"&"+Constants.PR_CERTIFICATE_TYPE+"="+reqType;
        if (mFileBtn.isSelected()) {
            rawData = rawData+"&"+Constants.PR_CERT_FILEPATH+"="+mCertFilePath;
            wizardInfo.setCertFilePath(mCertFilePath);
            wizardInfo.setPKCS10("");
        } else if (mBase64Btn.isSelected()) {
            rawData = rawData+"&"+Constants.PR_PKCS10+"="+mCertContent;
			//xxx It's not pkcs10, it's certificate.
            wizardInfo.setPKCS10(mCertContent);
            wizardInfo.setCertFilePath("");
        } else if (mQueryBtn.isSelected()) {
			if (mRID != null && !mRID.equals(""))
				wizardInfo.setRequestID(mRID);
			if (mHost != null && !mHost.equals(""))
				wizardInfo.setCMHost(mHost);
			if (mPort != null && !mPort.equals(""))
				wizardInfo.setCMEEPort(mPort);   
			if (mSSL.isSelected())
				wizardInfo.setCMEEType("https");
			else
				wizardInfo.setCMEEType("http");

            String rawData1 = "importCert=true";
            rawData1=rawData1+"&"+"requestId="+mRID;
/*
			CMSImportCert importCertCgi = new CMSImportCert();
			importCertCgi.initialize(wizardInfo);
			Hashtable data1 = new Hashtable();
			data1.put("importCert", "true");
			data1.put("requestId", mRID);
*/
			 
			startProgressStatus();
			boolean ready = send(mHost, Integer.parseInt(mPort), "/checkRequest",
              rawData1, wizardInfo);
        
			endProgressStatus();

			if (!ready) {
				String str = getErrorMessage();
				if (str.equals(""))
					setErrorMessage("Server Error");
				else
					setErrorMessage(str);
				return ready;
			}
			String certS= wizardInfo.getPKCS10();
			// Break the long single line:header,64 byte lines,trailer
			// Assuming this is the only format we generate.
			String CERT_NEW_HEADER = "-----BEGIN CERTIFICATE-----";
			String CERT_NEW_TRAILER = "-----END CERTIFICATE-----";
			String str = CERT_NEW_HEADER + "\n";
			int len = certS.length();
			for (int i = 0; i < len; i=i+64){
				if (i+64 < len)
					str = str + certS.substring(i,i+64) +"\n";
				else
					str = str + certS.substring(i,len) +"\n";
		    }
			str = str + CERT_NEW_TRAILER;
			certS = str;
            rawData = rawData+"&"+Constants.PR_PKCS10+"="+certS;
            wizardInfo.setPKCS10(certS);
            wizardInfo.setCertFilePath("");
        }

        startProgressStatus();
        boolean ready = send(rawData, wizardInfo);
        endProgressStatus();

        if (!ready) {
            String str = getErrorMessage();
            if (str.equals("")) {
                String errorMsg = mResource.getString(
                  mPanelName+"_ERRORMSG");
                setErrorMessage(errorMsg);
            } else
                setErrorMessage(str);
        }

        return ready;
    }

    public void callHelp() {
        CMSAdminUtil.help(mHelpIndex);
    }

    protected void init() {
        GridBagLayout gb = new GridBagLayout();
        GridBagConstraints gbc = new GridBagConstraints();
        setLayout(gb);

        introLbl = createTextArea(mResource.getString(
          mPanelName+"_LABEL_INTRO_LABEL"));
        CMSAdminUtil.resetGBC(gbc);
        gbc.anchor = gbc.NORTHWEST;
        gbc.weightx = 1.0;
        gbc.weighty = 0.0;
        gbc.insets = new Insets(COMPONENT_SPACE,COMPONENT_SPACE,
          COMPONENT_SPACE, COMPONENT_SPACE);
        gbc.gridwidth = gbc.REMAINDER;
        add(introLbl, gbc);
		
        mFileBtn = makeJRadioButton("FILE", true);
        CMSAdminUtil.resetGBC(gbc);
        gbc.anchor = gbc.NORTHWEST;
        gbc.weightx = 1.0;
        gbc.insets = new Insets(COMPONENT_SPACE,COMPONENT_SPACE,
          COMPONENT_SPACE, COMPONENT_SPACE);
        gbc.gridwidth = gbc.REMAINDER;
        add(mFileBtn, gbc);

        mFileText = makeJTextField(50);
        CMSAdminUtil.resetGBC(gbc);
        gbc.anchor = gbc.NORTHWEST;
        gbc.gridwidth = gbc.REMAINDER;
        gbc.insets = new Insets(0, 4*COMPONENT_SPACE, COMPONENT_SPACE, 0);
        add(mFileText, gbc);
        mActiveColor = mFileText.getBackground();

        mBase64Btn = makeJRadioButton("BASE64", false);
        CMSAdminUtil.resetGBC(gbc);
        gbc.anchor = gbc.NORTHWEST;
        gbc.weightx = 1.0;
        gbc.insets = new Insets(COMPONENT_SPACE,COMPONENT_SPACE,0,
          COMPONENT_SPACE);
        gbc.gridwidth = gbc.REMAINDER;
        add(mBase64Btn, gbc);

        JTextArea desc = createTextArea(mResource.getString(
          "PASTECERTWIZARD_TEXT_DESC_LABEL"));

        CMSAdminUtil.resetGBC(gbc);
        gbc.anchor = gbc.NORTHWEST;
        gbc.weightx = 1.0;
        gbc.weighty = 0.0;
        gbc.insets = new Insets(COMPONENT_SPACE,4*COMPONENT_SPACE,0,
          COMPONENT_SPACE);
        gbc.gridwidth = gbc.REMAINDER;
        add(desc, gbc);

        mBase64Text = new JTextArea(null, null, 6, 10);
        JScrollPane scrollPane = new JScrollPane(mBase64Text, 
          JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
          JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
        scrollPane.setPreferredSize(new Dimension(30, 50));
        CMSAdminUtil.resetGBC(gbc);
        gbc.anchor = gbc.NORTHWEST;
        gbc.weightx = 1.0;
        gbc.weighty = 0.5;
        gbc.insets = new Insets(0, COMPONENT_SPACE, 0, COMPONENT_SPACE);
        gbc.fill = gbc.BOTH;
        gbc.gridwidth = gbc.REMAINDER;
        add(scrollPane, gbc);

        mPaste = makeJButton("PASTE");
        CMSAdminUtil.resetGBC(gbc);
        gbc.anchor = gbc.NORTHEAST;
        gbc.fill = gbc.NONE;
        gbc.insets = new Insets(COMPONENT_SPACE,COMPONENT_SPACE,0,
          COMPONENT_SPACE);
        gbc.gridwidth = gbc.REMAINDER;
        add(mPaste, gbc);

        JLabel dummy = new JLabel(" ");
        gbc.gridwidth = gbc.REMAINDER;
        gbc.anchor = gbc.NORTHWEST;
        gbc.weighty = 1.0;
        gbc.fill = gbc.BOTH;
        add(dummy, gbc);

        mQueryBtn = makeJRadioButton("QUERY", false);
        CMSAdminUtil.resetGBC(gbc);
        gbc.anchor = gbc.NORTHWEST;
        gbc.weightx = 1.0;
        gbc.insets = new Insets(0,COMPONENT_SPACE,
          COMPONENT_SPACE,COMPONENT_SPACE);
        gbc.gridwidth = gbc.REMAINDER;
        add(mQueryBtn, gbc);

        mQueryText = new JLabel(mResource.getString(
            mPanelName + "_TEXT_QUERY_LABEL"));
        CMSAdminUtil.resetGBC(gbc);
        gbc.anchor = gbc.NORTHWEST;
        gbc.weightx = 1.0;
        gbc.insets = new Insets(0, 4*COMPONENT_SPACE,
          COMPONENT_SPACE,COMPONENT_SPACE);
        gbc.gridwidth = gbc.REMAINDER;
        add(mQueryText, gbc);

        mHostLbl = makeJLabel("HOST");
        CMSAdminUtil.resetGBC(gbc);
        gbc.fill = gbc.NONE;
        gbc.anchor = gbc.EAST;
        gbc.insets = new Insets(0, 4*COMPONENT_SPACE, 0,
          COMPONENT_SPACE);
        add(mHostLbl, gbc);

        mHostText = makeJTextField(23);
        CMSAdminUtil.resetGBC(gbc);
        gbc.anchor = gbc.WEST;
        gbc.gridwidth = gbc.REMAINDER;
        gbc.insets = new Insets(0, COMPONENT_SPACE, 0,
          COMPONENT_SPACE);
        add(mHostText, gbc);
        mActiveColor = mHostText.getBackground();

        mPortLbl = makeJLabel("PORT");
        CMSAdminUtil.resetGBC(gbc);
        gbc.anchor = gbc.EAST;
        gbc.fill = gbc.NONE;
        gbc.insets = new Insets(COMPONENT_SPACE, 4*COMPONENT_SPACE, 0,
          COMPONENT_SPACE);
        add(mPortLbl, gbc);

        mPortText = makeJTextField(23);
        CMSAdminUtil.resetGBC(gbc);
        gbc.anchor = gbc.WEST;
        gbc.gridwidth = gbc.REMAINDER;
        gbc.insets = new Insets(COMPONENT_SPACE, COMPONENT_SPACE, 0,
          COMPONENT_SPACE);
        add(mPortText, gbc);

        mSSLText = new JLabel(mResource.getString(
            mPanelName+"_TEXT_SSL_LABEL"));
        CMSAdminUtil.resetGBC(gbc);
        gbc.anchor = gbc.WEST;
        gbc.insets = new Insets(COMPONENT_SPACE, 4*COMPONENT_SPACE,
          COMPONENT_SPACE,COMPONENT_SPACE);
        //gbc.gridwidth = gbc.REMAINDER;
        add(mSSLText, gbc);

        mSSL = makeJCheckBox("SSL", true);
        CMSAdminUtil.resetGBC(gbc);
        gbc.anchor = gbc.WEST;
        gbc.insets = new Insets(COMPONENT_SPACE, COMPONENT_SPACE,
          COMPONENT_SPACE,COMPONENT_SPACE);
        gbc.gridwidth = gbc.REMAINDER;
        add(mSSL, gbc);

        mRIDLbl = makeJLabel("RID");
        CMSAdminUtil.resetGBC(gbc);
        gbc.fill = gbc.NONE;
        gbc.anchor = gbc.EAST;
        gbc.insets = new Insets(0, 4*COMPONENT_SPACE, 0,
          COMPONENT_SPACE);
        add(mRIDLbl, gbc);

        mRIDText = makeJTextField(23);
        CMSAdminUtil.resetGBC(gbc);
        gbc.anchor = gbc.WEST;
        gbc.gridwidth = gbc.REMAINDER;
        gbc.insets = new Insets(0, COMPONENT_SPACE, 0,
          COMPONENT_SPACE);
        add(mRIDText, gbc);

        JLabel label = new JLabel(" ");
        CMSAdminUtil.resetGBC(gbc);
        gbc.anchor = gbc.WEST;
        gbc.gridwidth = gbc.REMAINDER;
        gbc.gridheight = gbc.REMAINDER;
        gbc.weighty = 1.0;
        gbc.insets = new Insets(COMPONENT_SPACE, COMPONENT_SPACE, COMPONENT_SPACE,
          COMPONENT_SPACE);
        add(label, gbc);
		
        ButtonGroup buttonGrp = new ButtonGroup();
        buttonGrp.add(mFileBtn);
        buttonGrp.add(mBase64Btn);
        buttonGrp.add(mQueryBtn);

        enableFields(mFileText, true, mActiveColor);
        enableFields(mBase64Text, false, getBackground());
		enableFields(false,getBackground());

        super.init();
    }

    public void getUpdateInfo(WizardInfo info) {
    }

    public void actionPerformed(ActionEvent e) {
        if (e.getSource().equals(mPaste)) {
            mBase64Text.paste();
        } else if (e.getSource().equals(mFileBtn)) {
            enableFields(mFileText, true, mActiveColor);
            enableFields(mBase64Text, false, getBackground());
            enableFields(false, getBackground());
        } else if (e.getSource().equals(mBase64Btn)) {
            enableFields(mFileText, false, getBackground());
            enableFields(mBase64Text, true, mActiveColor);
            enableFields(false, getBackground());
        } else if (e.getSource().equals(mQueryBtn)) {
            enableFields(mFileText, false, getBackground());
            enableFields(mBase64Text, false, getBackground());
            enableFields(true, mActiveColor);
        }
    }

    private void enableFields(JTextComponent comp1, boolean enable, Color color) {
        comp1.setEnabled(enable);
        comp1.setEditable(enable);
        comp1.setBackground(color);
        CMSAdminUtil.repaintComp(comp1);
    }

    protected void enableFields(boolean enabled, Color color) {
        mQueryText.setEnabled(enabled);
        //mQueryText.setEditable(enabled);
        CMSAdminUtil.repaintComp(mQueryText);
        mHostLbl.setEnabled(enabled);
        mPortLbl.setEnabled(enabled);
        mRIDLbl.setEnabled(enabled);
        mHostText.setEnabled(enabled);
        mHostText.setEditable(enabled);
        mHostText.setBackground(color);
        mPortText.setEnabled(enabled);
        mPortText.setEditable(enabled);
        mPortText.setBackground(color);
        mRIDText.setEnabled(enabled);
        mRIDText.setEditable(enabled);
        mRIDText.setBackground(color);
        CMSAdminUtil.repaintComp(mHostLbl);
        CMSAdminUtil.repaintComp(mHostText);
        CMSAdminUtil.repaintComp(mPortLbl);
        CMSAdminUtil.repaintComp(mPortText);
        CMSAdminUtil.repaintComp(mRIDLbl);
        CMSAdminUtil.repaintComp(mRIDText);
		mSSLText.setEnabled(enabled);
		//mSSLText.setEditable(enabled);
        CMSAdminUtil.repaintComp(mSSLText);
		mSSL.setEnabled(enabled);
		//mSSL.setEditable(enabled);
		//mSSL.setBackground(color);
        CMSAdminUtil.repaintComp(mSSL);

    }
}