001: /*
002: * InstallCertificate.java
003: *
004: */
005:
006: /**
007: *
008: * @author ss133690
009: * @version
010: */package com.sun.portal.cli.cert;
011:
012: import org.mozilla.jss.crypto.X509Certificate;
013: import com.sun.portal.log.common.PortalLogger;
014: import org.mozilla.jss.*;
015: import org.mozilla.jss.crypto.*;
016: import org.mozilla.jss.util.*;
017: import java.io.File;
018:
019: public class InstallCertificate implements Command {
020: public boolean execute(JSSContext cntx) {
021: CertAdminHelpText.getCertHelpText();
022: //String certfile = CertAdminUtil.question("What is the name (including path) of file that contains the certificate?");
023: String certfile = CertAdminUtil.question(CertAdminLocale
024: .getPFString("q15", CertAdminConstants.q15));
025: File file = new File(certfile);
026: if (!file.exists()) {
027: //println("Certificate file "+certfile+ " doesn't exists");
028: CertAdminUtil.println(CertAdminLocale.getPFString("m23",
029: CertAdminConstants.m23)
030: + CertAdminConstants.SPACE
031: + certfile
032: + CertAdminConstants.SPACE
033: + CertAdminLocale.getPFString("m231",
034: CertAdminConstants.m231));
035: return false;
036: }
037:
038: String tmpnick = "server-cert";
039: //String nick = CertAdminUtil.question("Enter the name you like for this certificate ["+tmpnick+"]");
040: String nick = CertAdminUtil.question(CertAdminLocale
041: .getPFString("q9", CertAdminConstants.q9)
042: + tmpnick
043: + CertAdminLocale.getPFString("q1",
044: CertAdminConstants.q1));
045: if (nick.trim().equals("")) {
046: nick = tmpnick;
047: }
048:
049: while (JSSUtil.certExist(cntx, nick)) {
050: //println("Certificate "+nick+" already exist. Choose another nickname");
051: CertAdminUtil.println(CertAdminLocale.getPFString("m26",
052: CertAdminConstants.m26)
053: + CertAdminConstants.SPACE
054: + nick
055: + CertAdminConstants.SPACE
056: + CertAdminLocale.getPFString("m261",
057: CertAdminConstants.m261));
058: //nick = CertAdminUtil.question("Enter the name you like for this certificate ["+tmpnick+"]");
059: nick = CertAdminUtil.question(CertAdminLocale.getPFString(
060: "q9", CertAdminConstants.q9)
061: + tmpnick
062: + CertAdminLocale.getPFString("q1",
063: CertAdminConstants.q1));
064: if (nick.trim().equals("")) {
065: nick = tmpnick;
066: }
067: }
068:
069: //String token = CertAdminUtil.question("Please enter the token name you used when creating CSR for this certificate []");
070: String token = CertAdminUtil.question(CertAdminLocale
071: .getPFString("q16", CertAdminConstants.q16));
072: try {
073: String passphrase = cntx.getPasswordContext()
074: .generatePassphrase(cntx);
075: PasswordCallback password;
076: CryptoToken tok = null;
077: if (token.trim().equals("")) {
078: tok = cntx.getCryptoManager()
079: .getInternalKeyStorageToken();
080: password = new CertAdminPasswordCallback(passphrase);
081: if (!tok.passwordIsInitialized()) {
082: tok.initPassword(new NullPasswordCallback(),
083: password);
084: }
085: } else {
086: tok = cntx.getCryptoManager().getTokenByName(token);
087: password = new ConsolePasswordCallback();
088: if (!tok.passwordIsInitialized()) {
089: tok.initPassword(new NullPasswordCallback(),
090: password);
091: }
092: }
093: //Login to the crypto token.
094: tok.login(password);
095:
096: //Install the certificate to the certificate databse.
097: X509Certificate cert = JSSUtil.addCertificate(cntx, file,
098: nick, false);
099: String nickfile = cntx.getCertdir()
100: + CertAdminConstants.SEPERATOR
101: + CertAdminConstants.JSSNICKNAMEFILE;
102: //Update the .nickname file.
103: if (CertAdminUtil.fileExist(nickfile)) {
104: if (!CertAdminUtil.delete(nickfile)) {
105: CertAdminUtil.println(CertAdminLocale.getPFString(
106: "m46", CertAdminConstants.m46));
107: }
108: }
109: if (!CertAdminUtil.writeLine(nick, nickfile)) {
110: CertAdminUtil.println(CertAdminLocale.getPFString(
111: "m47", CertAdminConstants.m47));
112: CertAdminUtil.println(CertAdminConstants.newline);
113: CertAdminUtil.println(CertAdminLocale.getPFString(
114: "m48", CertAdminConstants.m48));
115: }
116: CertAdminUtil.println(CertAdminConstants.newline);
117: //println("Certificate successfully installed");
118: CertAdminUtil.println(CertAdminLocale.getPFString("m27",
119: CertAdminConstants.m27));
120: //println("To use the new Certificate, you must (re)start the server.");
121: CertAdminUtil.println(CertAdminLocale.getPFString("m28",
122: CertAdminConstants.m28));
123: return true;
124: } catch (Exception ex) {
125: //println("Could not install certificate : "+ex.getMessage());
126: CertAdminUtil.println(CertAdminLocale.getPFString("m29",
127: CertAdminConstants.m29));
128: ex.printStackTrace();
129: return false;
130: }
131:
132: }
133:
134: }
|