001: /**
002: * CertUtil.java
003: * @author ss133690
004: * @version 0.1
005: */package com.sun.portal.cli.cert;
006:
007: import java.io.*;
008: import com.sun.portal.log.common.PortalLogger;
009: import java.util.*;
010: import java.util.regex.Matcher;
011: import java.util.regex.Pattern;
012:
013: /**
014: * This class will help the administrator to perform SRA certificate related operations.
015: * @version
016: */
017:
018: public final class CertUtil {
019:
020: public static final String DEFAULT = "default";
021: public static final String CREATE_SELF_SIGN = "createselfsigncert";
022: public static final String VERIFY_CERT = "verifycert";
023: public static final String CREATE_LOG_USER_PASSWORD = "createloguserpassword";
024:
025: public static void main(String args[]) {
026: /*
027: System.out.println(CreateSelfSignedCertificate("/etc/opt/SUNWportal/cert/default", "testing123", "en_US", "sunone086.india.sun.com", "Sun", "IEC", "blr", "kar", "in", "", "install-cert", 6));
028: */
029: String option = DEFAULT;
030: int i = 0;
031: if (args[0].toLowerCase().startsWith("-option=")) {
032: option = args[0].substring(8, args[0].length());
033: i = i + 1;
034: }
035:
036: if ((option.equalsIgnoreCase(DEFAULT))
037: || (option.equalsIgnoreCase(CREATE_SELF_SIGN))) {
038:
039: String dir = args[i++];
040: String password = args[i++];
041: String locale = args[i++];
042: String other_info = args[i++];
043: String token = args[i++];
044: String nickname = args[i++];
045: int validity = 6;
046: try {
047: validity = Integer.parseInt(args[i++]);
048: } catch (Exception ex) {
049: }
050:
051: String cn = "";
052: String l = "";
053: String st = "";
054: String c = "";
055: String o = "";
056: String ou = "";
057:
058: cn = getDistinguishedNameComponent(other_info, "CN");
059: l = getDistinguishedNameComponent(other_info, "L");
060: st = getDistinguishedNameComponent(other_info, "ST");
061: c = getDistinguishedNameComponent(other_info, "C");
062: o = getDistinguishedNameComponent(other_info, "O");
063: ou = getDistinguishedNameComponent(other_info, "OU");
064:
065: CreateSelfSignedCertificate(dir, password, locale, cn, o,
066: ou, l, st, c, token, nickname, validity);
067: } else if (option.equalsIgnoreCase(VERIFY_CERT)) {
068: String dir = args[i++];
069: String locale = args[i++];
070: String nickname = args[i++];
071: CertAdminUtil.println(VerifyCertificate(dir, locale,
072: nickname));
073: } else if (option.equalsIgnoreCase(CREATE_LOG_USER_PASSWORD)) {
074: createLogUserPassword(args[i++], args[i++], args[i++]);
075: }
076: }
077:
078: public static String getDistinguishedNameComponent(
079: String searchString, String toFind) {
080: String match = "";
081: Matcher m;
082:
083: Pattern p_quotes = Pattern.compile(toFind + "=\"",
084: Pattern.CASE_INSENSITIVE);
085: Pattern p_noquotes = Pattern.compile(toFind + "=([^,]+),?",
086: Pattern.CASE_INSENSITIVE);
087:
088: m = p_quotes.matcher(searchString);
089:
090: if (m.find()) {
091: // Has a begining quote
092: Pattern p_find = Pattern.compile(toFind + "=\"([^\"]+)\"",
093: Pattern.CASE_INSENSITIVE);
094: m = p_find.matcher(searchString);
095:
096: if (m.find() && m.groupCount() > 0)
097: match = m.group(1);
098: } else {
099: m = p_noquotes.matcher(searchString);
100:
101: if (m.find() && m.groupCount() > 0)
102: match = m.group(1);
103: }
104: return match;
105: }
106:
107: public static void createLogUserPassword(String confFile,
108: String certDir, String plainPassword) {
109: try {
110: JSSUtil.setDefaultDecoder(certDir);
111: String password = JSSUtil.encryptPassword(plainPassword);
112: Properties prop = new Properties();
113: InputStream inpstrm = new FileInputStream(confFile);
114: prop.load(inpstrm);
115: inpstrm.close();
116: prop.put("gateway.logging.password", password);
117: OutputStream outstrm = new FileOutputStream(confFile);
118: prop.store(outstrm, null);
119: outstrm.close();
120: //CertAdminUtil.writeLine("gateway.logging.password="+password, confFile, true);
121: } catch (IOException ioex) {
122: ioex.printStackTrace();
123: } catch (SRADecoderException ex) {
124: ex.printStackTrace();
125: }
126: }
127:
128: public static boolean CreateSelfSignedCertificate(String certdir,
129: String jsspass, String locale, String fqdn, String o,
130: String ou, String l, String s, String c, String token,
131: String nick, int val) {
132: //JSSContext jsscntx = new JSSContextImpl(certdir, fqdn, locale, false, false,true);
133: JSSContext jsscntx = new JSSContextImpl(certdir, fqdn, locale);
134: PasswordContext passwdcntx = new InstallPasswordContextImpl(
135: jsspass);
136: jsscntx.setPasswordContext(passwdcntx);
137: if (!jsscntx.init()) {
138: CertAdminUtil.println(CertAdminLocale.getPFString("m1",
139: CertAdminConstants.m1));
140: return false;
141: }
142:
143: CertContext certcntx = CertAdminFactory
144: .CreateCertificateContext(fqdn, o, ou, l, s, c, token,
145: nick, val);
146: CreateSelfSignedCertificate cmd = new CreateSelfSignedCertificate();
147: return cmd.execute(jsscntx, certcntx);
148: }
149:
150: public static String VerifyCertificate(String certdir,
151: String locale, String nick) {
152: //JSSContext jsscntx = new JSSContextImpl(certdir, fqdn, locale, false, false,true);
153: JSSContext jsscntx = new JSSContextImpl(certdir, locale);
154: jsscntx.setPasswordContext(CertAdminFactory
155: .CreatePasswordContext());
156: if (!jsscntx.init()) {
157: return CertAdminLocale.getPFString("m58",
158: CertAdminConstants.m58)
159: + CertAdminConstants.SPACE
160: + nick
161: + CertAdminConstants.SPACE
162: + CertAdminLocale.getPFString("m1",
163: CertAdminConstants.m1);
164: }
165: VerifyCertificate cmd = new VerifyCertificate();
166: cmd.execute(jsscntx, nick);
167: return cmd.getMessage();
168: }
169:
170: }
|