001: /*
002: * JSSContextImpl.java
003: *
004: */
005:
006: /**
007: *
008: * @author ss133690
009: * @version 0.1
010: */package com.sun.portal.cli.cert;
011:
012: import org.mozilla.jss.crypto.*;
013: import com.sun.portal.log.common.PortalLogger;
014: import org.mozilla.jss.*;
015: import java.security.*;
016:
017: public class JSSContextImpl implements JSSContext {
018: protected String certdir;
019: protected String fqdn;
020: protected String locale = "en_US";
021: protected String inst;
022: protected CryptoManager cm;
023: //protected KeyStore ks;
024: protected boolean passfileExist = true;
025: protected boolean dbfileExist = true;
026: protected SignatureAlgorithm sigAlg = SignatureAlgorithm.RSASignatureWithMD5Digest;
027: protected boolean isPassEncrypted = true;
028: protected PasswordContext passwdcntx;
029:
030: public JSSContextImpl() {
031: }
032:
033: public JSSContextImpl(String certdir, String fqdn, String locale) {
034: this (certdir, locale);
035: this .fqdn = fqdn;
036: }
037:
038: public JSSContextImpl(String certdir, String locale) {
039: this .certdir = certdir;
040: this .locale = locale;
041: }
042:
043: public void setCertdir(String certdir) {
044: this .certdir = certdir;
045: }
046:
047: public void setHost(String fqdn) {
048: this .fqdn = fqdn;
049: }
050:
051: public void setLocale(String locale) {
052: this .locale = locale;
053: }
054:
055: public String getCertdir() {
056: return certdir;
057: }
058:
059: public String getHost() {
060: return fqdn;
061: }
062:
063: public String getLocale() {
064: return locale;
065: }
066:
067: public CryptoManager getCryptoManager() {
068: return cm;
069: }
070:
071: /*public KeyStore getKeyStore(){
072: return ks;
073: }*/
074: public SignatureAlgorithm getSigAlg() {
075: return sigAlg;
076: }
077:
078: public boolean isPassFileExist() {
079: return CertAdminUtil.fileExist(certdir
080: + CertAdminConstants.SEPERATOR
081: + CertAdminConstants.JSSPASSFILE);
082: }
083:
084: public boolean isDBFileExist() {
085: return CertAdminUtil.fileExist(certdir
086: + CertAdminConstants.SEPERATOR
087: + CertAdminConstants.KEYDB);
088: }
089:
090: public boolean isPasswordEcrypted() {
091: if (isPassFileExist()) {
092: String jsspass = CertAdminUtil.readLine(certdir
093: + CertAdminConstants.SEPERATOR
094: + CertAdminConstants.JSSPASSFILE);
095: isPassEncrypted = JSSUtil.isPasswordEncrypted(jsspass);
096: }
097: return isPassEncrypted;
098: }
099:
100: public void setPasswordMode(boolean encrypt) {
101: isPassEncrypted = encrypt;
102: }
103:
104: public void setPasswordContext(PasswordContext passwdcntx) {
105: this .passwdcntx = passwdcntx;
106: }
107:
108: public PasswordContext getPasswordContext() {
109: return passwdcntx;
110: }
111:
112: /**
113: * Initializes the certificate database.
114: * This method should be called first after the object creation.
115: */
116: public boolean init() {
117:
118: CertAdminLocale.createDefault(locale);
119: try {
120: //Initialize the CryptoManager
121: CryptoManager.InitializationValues vals = new CryptoManager.InitializationValues(
122: certdir);
123: CryptoManager.initialize(vals);
124: cm = CryptoManager.getInstance();
125: //Initialize the KeyStore (This will be used to perform some operations which CrytoManager cannot do)
126: Security.insertProviderAt(new sun.security.provider.Sun(),
127: 1);
128: //KeyStore ks = KeyStore.getInstance("Mozilla-JSS");
129: //ks.load(null,null);
130: //setPasswordMode(true);
131:
132: JSSUtil.setDefaultDecoder(certdir);
133: org.mozilla.jss.util.PasswordCallback password = new CertAdminPasswordCallback(
134: passwdcntx.generatePassphrase(this ));
135: cm.setPasswordCallback(password);
136:
137: } catch (KeyDatabaseException kdbe) {
138: //println("GWNSSInit: Couldn't open the key database." + kdbe);
139: CertAdminUtil.println(CertAdminLocale.getPFString("m3",
140: CertAdminConstants.m3)
141: + CertAdminConstants.newline + kdbe);
142: return false;
143: } catch (CertDatabaseException cdbe) {
144: //println("GWNSSInit: Couldn't open the certificate database." + cdbe);
145: CertAdminUtil.println(CertAdminLocale.getPFString("m4",
146: CertAdminConstants.m4)
147: + CertAdminConstants.newline + cdbe);
148: return false;
149: } catch (org.mozilla.jss.crypto.AlreadyInitializedException aie) {
150: //println("GWNSSInit: CryptoManager already initialized." + aie);
151: CertAdminUtil.println(CertAdminLocale.getPFString("m5",
152: CertAdminConstants.m5)
153: + CertAdminConstants.newline + aie);
154: } catch (SRADecoderException sde) {
155: //println("Error!, Could not initialize the JSS password engine");
156: CertAdminUtil.println(CertAdminLocale.getPFString("m54",
157: CertAdminConstants.m54)
158: + CertAdminConstants.newline + sde);
159: } catch (Exception e) {
160: //println("GWNSSInit: Exception occurred: "+e.getMessage());
161: CertAdminUtil.println(CertAdminLocale.getPFString("m6",
162: CertAdminConstants.m6));
163: e.printStackTrace();
164: return false;
165: }
166: return true;
167: }
168:
169: }
|