// --- 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.cms.profile.def; import java.io.IOException; import java.util.Enumeration; import java.util.Hashtable; import java.util.Locale; import java.util.StringTokenizer; import java.util.Vector; import netscape.security.util.ObjectIdentifier; import netscape.security.x509.CPSuri; import netscape.security.x509.CertificatePoliciesExtension; import netscape.security.x509.CertificatePolicyId; import netscape.security.x509.CertificatePolicyInfo; import netscape.security.x509.DisplayText; import netscape.security.x509.NoticeReference; import netscape.security.x509.PKIXExtensions; import netscape.security.x509.PolicyQualifiers; import netscape.security.x509.Qualifier; import netscape.security.x509.UserNotice; import netscape.security.x509.X509CertInfo; import com.netscape.certsrv.apps.CMS; import com.netscape.certsrv.base.IConfigStore; import com.netscape.certsrv.profile.EProfileException; import com.netscape.certsrv.profile.IProfile; import com.netscape.certsrv.property.Descriptor; import com.netscape.certsrv.property.EPropertyException; import com.netscape.certsrv.property.IDescriptor; import com.netscape.certsrv.request.IRequest; /** * This class implements an enrollment default policy * that populates a policy mappings extension * into the certificate template. * * @version $Revision$, $Date$ */ public class CertificatePoliciesExtDefault extends EnrollExtDefault { public static final String CONFIG_CRITICAL = "Critical"; public static final String CONFIG_PREFIX = "PoliciesExt.certPolicy"; public static final String CONFIG_PREFIX1 = "PolicyQualifiers"; public static final String CONFIG_POLICY_ENABLE = "enable"; public static final String CONFIG_POLICY_NUM = "PoliciesExt.num"; public static final String CONFIG_POLICY_ID = "policyId"; public static final String CONFIG_POLICY_QUALIFIERS_NUM = "PolicyQualifiers.num"; public static final String CONFIG_CPSURI_ENABLE = "CPSURI.enable"; public static final String CONFIG_USERNOTICE_ENABLE = "usernotice.enable"; public static final String CONFIG_CPSURI_VALUE = "CPSURI.value"; public static final String CONFIG_USERNOTICE_ORG = "usernotice.noticeReference.organization"; public static final String CONFIG_USERNOTICE_NUMBERS = "usernotice.noticeReference.noticeNumbers"; public static final String CONFIG_USERNOTICE_TEXT = "usernotice.explicitText.value"; public static final String VAL_CRITICAL = "Critical"; public static final String VAL_POLICY_QUALIFIERS = "policyQualifiers"; private static final String SEPARATOR = "."; private static final int DEF_NUM_POLICIES = 5; private static final int DEF_NUM_QUALIFIERS = 1; private static final int MAX_NUM_POLICIES = 20; private static final String POLICY_ID_ENABLE = "Enable"; private static final String POLICY_ID = "Policy Id"; private static final String POLICY_QUALIFIER_CPSURI_ENABLE = "CPSuri Enable"; private static final String POLICY_QUALIFIER_USERNOTICE_ENABLE = "UserNotice Enable"; private static final String USERNOTICE_REF_ORG = "UserNoticeReference Organization"; private static final String USERNOTICE_REF_NUMBERS = "UserNoticeReference Numbers"; private static final String USERNOTICE_EXPLICIT_TEXT = "UserNoticeReference Explicit Text"; private static final String CPSURI = "CPS uri"; public CertificatePoliciesExtDefault() { super(); } protected int getNumPolicies() { int num = DEF_NUM_POLICIES; String numPolicies = getConfig(CONFIG_POLICY_NUM); if (numPolicies != null) { try { num = Integer.parseInt(numPolicies); } catch (NumberFormatException e) { // ignore } } if (num >= MAX_NUM_POLICIES) num = DEF_NUM_POLICIES; return num; } protected int getNumQualifiers() { int num = DEF_NUM_QUALIFIERS; String numQualifiers = getConfig(CONFIG_POLICY_QUALIFIERS_NUM); if (numQualifiers != null) { try { num = Integer.parseInt(numQualifiers); } catch (NumberFormatException e) { // ignore } } return num; } public void init(IProfile profile, IConfigStore config) throws EProfileException { super.init(profile, config); refreshConfigAndValueNames(); } public void setConfig(String name, String value) throws EPropertyException { int num = 0; if (name.equals(CONFIG_POLICY_NUM)) { try { num = Integer.parseInt(value); if (num >= MAX_NUM_POLICIES || num < 0) { throw new EPropertyException(CMS.getUserMessage( "CMS_INVALID_PROPERTY", CONFIG_POLICY_NUM)); } } catch (Exception e) { throw new EPropertyException(CMS.getUserMessage( "CMS_INVALID_PROPERTY", CONFIG_POLICY_NUM)); } } super.setConfig(name, value); } public Enumeration getConfigNames() { refreshConfigAndValueNames(); return super.getConfigNames(); } protected void refreshConfigAndValueNames() { super.refreshConfigAndValueNames(); addValueName(VAL_CRITICAL); addValueName(VAL_POLICY_QUALIFIERS); addConfigName(CONFIG_CRITICAL); int num = getNumPolicies(); int numQualifiers = getNumQualifiers(); addConfigName(CONFIG_POLICY_NUM); for (int i = 0; i < num; i++) { addConfigName(CONFIG_PREFIX+i+SEPARATOR+CONFIG_POLICY_ID); addConfigName(CONFIG_PREFIX+i+SEPARATOR+CONFIG_POLICY_ENABLE); for (int j=0; j= 0) { return new Descriptor(IDescriptor.STRING, null, null, CMS.getUserMessage(locale, "CMS_PROFILE_POLICY_ID")); } else if (name.indexOf(CONFIG_CPSURI_ENABLE) >= 0) { return new Descriptor(IDescriptor.BOOLEAN, null, "false", CMS.getUserMessage(locale, "CMS_PROFILE_POLICY_QUALIFIER_CPSURI_ENABLE")); } else if (name.indexOf(CONFIG_USERNOTICE_ENABLE) >= 0) { return new Descriptor(IDescriptor.BOOLEAN, null, "false", CMS.getUserMessage(locale, "CMS_PROFILE_POLICY_QUALIFIER_USERNOTICE_ENABLE")); } else if (name.indexOf(CONFIG_POLICY_ENABLE) >= 0) { return new Descriptor(IDescriptor.BOOLEAN, null, "false", CMS.getUserMessage(locale, "CMS_PROFILE_CERTIFICATE_POLICY_ENABLE")); } else if (name.indexOf(CONFIG_POLICY_QUALIFIERS_NUM) >= 0) { return new Descriptor(IDescriptor.INTEGER, null, "1", CMS.getUserMessage(locale, "CMS_PROFILE_POLICY_QUALIFIER_NUM")); } else if (name.indexOf(CONFIG_USERNOTICE_ORG) >= 0) { return new Descriptor(IDescriptor.STRING, null, null, CMS.getUserMessage(locale, "CMS_PROFILE_POLICY_USERNOTICE_REF_ORG")); } else if (name.indexOf(CONFIG_USERNOTICE_NUMBERS) >= 0) { return new Descriptor(IDescriptor.STRING, null, null, CMS.getUserMessage(locale, "CMS_PROFILE_POLICY_USERNOTICE_REF_NUMBERS")); } else if (name.indexOf(CONFIG_USERNOTICE_TEXT) >= 0) { return new Descriptor(IDescriptor.STRING, null, null, CMS.getUserMessage(locale, "CMS_PROFILE_POLICY_USERNOTICE_EXPLICIT_TEXT")); } else if (name.indexOf(CONFIG_CPSURI_VALUE) >= 0) { return new Descriptor(IDescriptor.STRING, null, null, CMS.getUserMessage(locale, "CMS_PROFILE_POLICY_CPSURI")); } else if (name.indexOf(CONFIG_POLICY_NUM) >= 0) { return new Descriptor(IDescriptor.INTEGER, null, "5", CMS.getUserMessage(locale, "CMS_PROFILE_NUM_POLICIES")); } return null; } public IDescriptor getValueDescriptor(Locale locale, String name) { if (name.equals(VAL_CRITICAL)) { return new Descriptor(IDescriptor.BOOLEAN, null, "false", CMS.getUserMessage(locale, "CMS_PROFILE_CRITICAL")); } else if (name.equals(VAL_POLICY_QUALIFIERS)) { return new Descriptor(IDescriptor.STRING_LIST, null, null, CMS.getUserMessage(locale, "CMS_PROFILE_POLICY_QUALIFIERS")); } return null; } private Hashtable buildRecords(String value) throws EPropertyException { StringTokenizer st = new StringTokenizer(value, "\r\n"); Hashtable table = new Hashtable(); while (st.hasMoreTokens()) { String token = (String) st.nextToken(); int index = token.indexOf(":"); if (index <= 0) throw new EPropertyException(CMS.getUserMessage( "CMS_INVALID_PROPERTY", token)); String name = token.substring(0, index); String val = ""; if ((token.length()-1) > index) { val = token.substring(index+1); } table.put(name, val); } return table; } public void setValue(String name, Locale locale, X509CertInfo info, String value) throws EPropertyException { try { CertificatePoliciesExtension ext = null; if (name == null) { throw new EPropertyException(CMS.getUserMessage( locale, "CMS_INVALID_PROPERTY", name)); } if (name.equals(VAL_CRITICAL)) { ext = (CertificatePoliciesExtension) getExtension(PKIXExtensions.CertificatePolicies_Id.toString(), info); boolean val = Boolean.valueOf(value).booleanValue(); ext.setCritical(val); } else if (name.equals(VAL_POLICY_QUALIFIERS)) { ext = (CertificatePoliciesExtension) getExtension(PKIXExtensions.CertificatePolicies_Id.toString(), info); Hashtable h = buildRecords(value); String numStr = (String)h.get(CONFIG_POLICY_NUM); int size = Integer.parseInt(numStr); Vector certificatePolicies = new Vector(); for (int i = 0; i < size; i++) { String enable = (String)h.get(CONFIG_PREFIX+i+SEPARATOR+CONFIG_POLICY_ENABLE); CertificatePolicyInfo cinfo = null; if (enable != null && enable.equals("true")) { String policyId = (String)h.get(CONFIG_PREFIX+i+SEPARATOR+CONFIG_POLICY_ID); if (policyId == null || policyId.length() == 0) throw new EPropertyException(CMS.getUserMessage( locale, "CMS_PROFILE_CERTIFICATE_POLICIES_EMPTY_POLICYID")); CertificatePolicyId cpolicyId = getPolicyId(policyId); String qualifersNum = (String)h.get(CONFIG_PREFIX+i+SEPARATOR+CONFIG_POLICY_QUALIFIERS_NUM); PolicyQualifiers policyQualifiers = new PolicyQualifiers(); int num = 0; if (qualifersNum != null && qualifersNum.length() > 0) num = Integer.parseInt(qualifersNum); for (int j=0; j 0) { CertificatePolicyInfo cinfo = (CertificatePolicyInfo) infos.elementAt(0); CertificatePolicyId id1 = cinfo.getPolicyIdentifier(); policyId = id1.getIdentifier().toString(); policyEnable = "true"; qualifiers = cinfo.getPolicyQualifiers(); if (qualifiers != null) qSize = qualifiers.size(); infos.removeElementAt(0); } sb.append(CONFIG_PREFIX+i+SEPARATOR+CONFIG_POLICY_ENABLE); sb.append(":"); sb.append(policyEnable); sb.append("\n"); sb.append(CONFIG_PREFIX+i+SEPARATOR+CONFIG_POLICY_ID); sb.append(":"); sb.append(policyId); sb.append("\n"); if (qSize == 0) { sb.append(CONFIG_PREFIX+i+SEPARATOR+CONFIG_POLICY_QUALIFIERS_NUM); sb.append(":"); sb.append(DEF_NUM_QUALIFIERS); sb.append("\n"); } else { sb.append(CONFIG_PREFIX+i+SEPARATOR+CONFIG_POLICY_QUALIFIERS_NUM); sb.append(":"); sb.append(qSize); sb.append("\n"); } if (qSize == 0) { sb.append(CONFIG_PREFIX+i+SEPARATOR+CONFIG_PREFIX1+"0"+SEPARATOR+CONFIG_CPSURI_ENABLE); sb.append(":"); sb.append("false"); sb.append("\n"); sb.append(CONFIG_PREFIX+i+SEPARATOR+CONFIG_PREFIX1+"0"+SEPARATOR+CONFIG_CPSURI_VALUE); sb.append(":"); sb.append(""); sb.append("\n"); sb.append(CONFIG_PREFIX+i+SEPARATOR+CONFIG_PREFIX1+"0"+SEPARATOR+CONFIG_USERNOTICE_ENABLE); sb.append(":"); sb.append("false"); sb.append("\n"); sb.append(CONFIG_PREFIX+i+SEPARATOR+CONFIG_PREFIX1+"0"+SEPARATOR+CONFIG_USERNOTICE_ORG); sb.append(":"); sb.append(""); sb.append("\n"); sb.append(CONFIG_PREFIX+i+SEPARATOR+CONFIG_PREFIX1+"0"+SEPARATOR+CONFIG_USERNOTICE_NUMBERS); sb.append(":"); sb.append(""); sb.append("\n"); sb.append(CONFIG_PREFIX+i+SEPARATOR+CONFIG_PREFIX1+"0"+SEPARATOR+CONFIG_USERNOTICE_TEXT); sb.append(":"); sb.append(""); sb.append("\n"); } for (int j=0; j 0) explicitText = new DisplayText(DisplayText.tag_VisibleString, noticeText); int nums[] = null; if (noticeNums != null && noticeNums.length() > 0) { Vector numsVector = new Vector(); StringTokenizer tokens = new StringTokenizer(noticeNums, ";"); while (tokens.hasMoreTokens()) { String num = tokens.nextToken().trim(); numsVector.addElement(num); } nums = new int[numsVector.size()]; try { for (int i = 0; i < numsVector.size(); i++) { Integer ii = new Integer((String) numsVector.elementAt(i)); nums[i] = ii.intValue(); } } catch (Exception e) { throw new EPropertyException("Wrong notice numbers"); } } DisplayText orgName = null; if (organization != null && organization.length() > 0) { orgName = new DisplayText(DisplayText.tag_VisibleString, organization); } NoticeReference noticeReference = null; if (orgName != null) noticeReference = new NoticeReference(orgName, nums); UserNotice userNotice = null; if (explicitText != null || noticeReference != null) { userNotice = new UserNotice (noticeReference, explicitText); netscape.security.x509.PolicyQualifierInfo policyQualifierInfo1 = new netscape.security.x509.PolicyQualifierInfo(netscape.security.x509.PolicyQualifierInfo.QT_UNOTICE, userNotice); return policyQualifierInfo1; } return null; } }