// --- 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.policy.constraints; import java.util.Date; import java.util.Locale; import java.util.Vector; import netscape.security.x509.CertificateValidity; import netscape.security.x509.X509CertInfo; import com.netscape.certsrv.apps.CMS; import com.netscape.certsrv.base.IConfigStore; import com.netscape.certsrv.base.IExtendedPluginInfo; import com.netscape.certsrv.base.ISubsystem; import com.netscape.certsrv.policy.EPolicyException; import com.netscape.certsrv.policy.IEnrollmentPolicy; import com.netscape.certsrv.request.IRequest; import com.netscape.certsrv.request.PolicyResult; import com.netscape.cms.policy.APolicyRule; /** * ValidityConstraints is a default rule for Enrollment and * Renewal that enforces minimum and maximum validity periods * and changes them if not met. * * Optionally the lead and lag times - i.e how far back into the * front or back the notBefore date could go in minutes can also * be specified. *
* *
* NOTE: The Policy Framework has been replaced by the Profile Framework. **
*
* @deprecated
* @version $Revision$, $Date$
*/
public class ValidityConstraints extends APolicyRule
implements IEnrollmentPolicy, IExtendedPluginInfo {
protected long mMinValidity;
protected long mMaxValidity;
protected long mLeadTime;
protected long mLagTime;
protected long mNotBeforeSkew;
private final static String PROP_MIN_VALIDITY = "minValidity";
private final static String PROP_MAX_VALIDITY = "maxValidity";
private final static String PROP_LEAD_TIME = "leadTime";
private final static String PROP_LAG_TIME = "lagTime";
private final static String PROP_NOT_BEFORE_SKEW = "notBeforeSkew";
public final static int DEF_MIN_VALIDITY = 180;
public final static int DEF_MAX_VALIDITY = 730;
public final static int DEF_LEAD_TIME = 10;
public final static int DEF_LAG_TIME = 10;
public final static int DEF_NOT_BEFORE_SKEW = 5;
public final static long DAYS_TO_MS_FACTOR = 24L * 3600 * 1000;
public final static long MINS_TO_MS_FACTOR = 60L * 1000;
private final static Vector
*
* The entries probably are of the form:
*
* ra.Policy.rule.
*
* @param req The request on which to apply policy.
* @return The policy result object.
*/
public PolicyResult apply(IRequest req) {
PolicyResult result = PolicyResult.ACCEPTED;
try {
// Get the certificate info from the request
//X509CertInfo certInfo[] = (X509CertInfo[])
// req.get(IRequest.CERT_INFO);
X509CertInfo certInfo[] = req.getExtDataInCertInfoArray(IRequest.CERT_INFO);
// There should be a certificate info set.
if (certInfo == null) {
setError(req, CMS.getUserMessage("CMS_POLICY_NO_CERT_INFO",
getInstanceName()), "");
return PolicyResult.REJECTED;
}
// Else check if validity is within the limit
for (int i = 0; i < certInfo.length; i++) {
CertificateValidity validity = (CertificateValidity)
certInfo[i].get(X509CertInfo.VALIDITY);
Date notBefore = null, notAfter = null;
if (validity != null) {
notBefore = (Date)
validity.get(CertificateValidity.NOT_BEFORE);
notAfter = (Date)
validity.get(CertificateValidity.NOT_AFTER);
}
// If no validity is supplied yet, make one. The default
// validity is supposed to pass the following checks, so
// bypass further checking.
// (date = 0 is hack for serialization)
if (validity == null ||
(notBefore.getTime() == 0 && notAfter.getTime() == 0)) {
certInfo[i].set(X509CertInfo.VALIDITY,
makeDefaultValidity(req));
continue;
}
Date now = CMS.getCurrentDate();
if (notBefore.getTime() > (now.getTime() + mLeadTime)) {
setError(req, CMS.getUserMessage("CMS_POLICY_INVALID_BEGIN_TIME",
getInstanceName()), "");
result = PolicyResult.REJECTED;
}
if ((notAfter.getTime() - notBefore.getTime()) > mMaxValidity) {
String params[] = { getInstanceName(),
String.valueOf(
((notAfter.getTime() - notBefore.getTime()) / DAYS_TO_MS_FACTOR)),
String.valueOf(mMaxValidity / DAYS_TO_MS_FACTOR) };
setError(req, CMS.getUserMessage("CMS_POLICY_MORE_THAN_MAX_VALIDITY", params), "");
result = PolicyResult.REJECTED;
}
if ((notAfter.getTime() - notBefore.getTime()) < mMinValidity) {
String params[] = { getInstanceName(),
String.valueOf(
((notAfter.getTime() - notBefore.getTime()) / DAYS_TO_MS_FACTOR)),
String.valueOf(mMinValidity / DAYS_TO_MS_FACTOR) };
setError(req, CMS.getUserMessage("CMS_POLICY_LESS_THAN_MIN_VALIDITY", params), "");
result = PolicyResult.REJECTED;
}
}
} catch (Exception e) {
// e.printStackTrace();
String params[] = { getInstanceName(), e.toString() };
setError(req, CMS.getUserMessage("CMS_POLICY_UNEXPECTED_POLICY_ERROR",
params), "");
result = PolicyResult.REJECTED;
}
return result;
}
/**
* Return configured parameters for a policy rule instance.
*
* @return nvPairs A Vector of name/value pairs.
*/
public Vector