From afea983a070fff830d273ca9ea640a975013b09c Mon Sep 17 00:00:00 2001
From: Endi Sukma Dewata <edewata@redhat.com>
Date: Tue, 18 Sep 2012 10:37:06 -0500
Subject: [PATCH] Removed duplicate DN escaping methods.

The duplicate methods to escape DN value have been removed. The
codes that used the duplicate methods have been modified to use
LDAPUtil.escapeDN().

Ticket #193
---
 .../netscape/cms/profile/def/EnrollDefault.java    | 40 ----------------------
 .../def/nsTokenUserKeySubjectNameDefault.java      |  5 +--
 .../com/netscape/cms/servlet/base/CMSServlet.java  | 40 ----------------------
 .../netscape/cms/servlet/cert/CertProcessor.java   |  4 +--
 .../cms/servlet/cert/EnrollmentProcessor.java      |  3 +-
 .../com/netscape/cms/servlet/cert/SrchCerts.java   |  7 ++--
 .../netscape/cms/servlet/processors/Processor.java | 39 ---------------------
 7 files changed, 11 insertions(+), 127 deletions(-)

diff --git a/base/common/src/com/netscape/cms/profile/def/EnrollDefault.java b/base/common/src/com/netscape/cms/profile/def/EnrollDefault.java
index 162d1647ce71481598de14a5039fa6b68182f9f1..53ec23b404d64a6b8525352b91fa2149a1c02ac9 100644
--- a/base/common/src/com/netscape/cms/profile/def/EnrollDefault.java
+++ b/base/common/src/com/netscape/cms/profile/def/EnrollDefault.java
@@ -772,44 +772,4 @@ public abstract class EnrollDefault implements IPolicyDefault, ICertInfoPolicyDe
         }
         return p.substitute2("request", attrSet);
     }
-
-    protected StringBuffer escapeValueRfc1779(String v, boolean doubleEscape) {
-        StringBuffer result = new StringBuffer();
-
-        // Do we need to escape any characters
-        for (int i = 0; i < v.length(); i++) {
-            int c = v.charAt(i);
-            if (c == ',' || c == '=' || c == '+' || c == '<' ||
-                    c == '>' || c == '#' || c == ';' || c == '\r' ||
-                    c == '\n' || c == '\\' || c == '"') {
-                if ((c == 0x5c) && ((i + 1) < v.length())) {
-                    int nextC = v.charAt(i + 1);
-                    if ((c == 0x5c) && (nextC == ',' || nextC == '=' || nextC == '+' ||
-                                        nextC == '<' || nextC == '>' || nextC == '#' ||
-                                        nextC == ';' || nextC == '\r' || nextC == '\n' ||
-                                        nextC == '\\' || nextC == '"')) {
-                        if (doubleEscape)
-                            result.append('\\');
-                    } else {
-                        result.append('\\');
-                        if (doubleEscape)
-                            result.append('\\');
-                    }
-                } else {
-                    result.append('\\');
-                    if (doubleEscape)
-                        result.append('\\');
-                }
-            }
-            if (c == '\r') {
-                result.append("0D");
-            } else if (c == '\n') {
-                result.append("0A");
-            } else {
-                result.append((char) c);
-            }
-        }
-        return result;
-    }
-
 }
diff --git a/base/common/src/com/netscape/cms/profile/def/nsTokenUserKeySubjectNameDefault.java b/base/common/src/com/netscape/cms/profile/def/nsTokenUserKeySubjectNameDefault.java
index dff36748c3530d875b0b3a32cb9323b353500e6e..01cc73aba2e9cf6f21812de50f5b2af96181dd11 100644
--- a/base/common/src/com/netscape/cms/profile/def/nsTokenUserKeySubjectNameDefault.java
+++ b/base/common/src/com/netscape/cms/profile/def/nsTokenUserKeySubjectNameDefault.java
@@ -40,6 +40,7 @@ import com.netscape.certsrv.property.Descriptor;
 import com.netscape.certsrv.property.EPropertyException;
 import com.netscape.certsrv.property.IDescriptor;
 import com.netscape.certsrv.request.IRequest;
+import com.netscape.cmsutil.ldap.LDAPUtil;
 
 /**
  * This class implements an enrollment default policy
@@ -429,8 +430,8 @@ public class nsTokenUserKeySubjectNameDefault extends EnrollDefault {
                     String[] sla = la.getStringValueArray();
                     CMS.debug("nsTokenUserKeySubjectNameDefault: getSubjectName(): got attribute: "
                             + mLdapStringAttrs[i] +
-                            "=" + escapeValueRfc1779(sla[0], false).toString());
-                    request.setExtData(mLdapStringAttrs[i], escapeValueRfc1779(sla[0], false).toString());
+                            "=" + LDAPUtil.escapeDN(sla[0]));
+                    request.setExtData(mLdapStringAttrs[i], LDAPUtil.escapeDN(sla[0]));
                 }
             }
             CMS.debug("pattern = " + pattern);
diff --git a/base/common/src/com/netscape/cms/servlet/base/CMSServlet.java b/base/common/src/com/netscape/cms/servlet/base/CMSServlet.java
index a4922ab25e38e607b4e0c26582b9899f13103c1c..72ced2c53309f7530ed7ab8c0fa93a2b279637fa 100644
--- a/base/common/src/com/netscape/cms/servlet/base/CMSServlet.java
+++ b/base/common/src/com/netscape/cms/servlet/base/CMSServlet.java
@@ -2257,44 +2257,4 @@ public abstract class CMSServlet extends HttpServlet {
                     CMS.getLogMessage("CMSGW_ERR_BAD_SERV_OUT_STREAM", "", ee.toString()));
         }
     }
-
-    protected StringBuffer escapeValueRfc1779(String v, boolean doubleEscape) {
-        StringBuffer result = new StringBuffer();
-
-        // Do we need to escape any characters
-        for (int i = 0; i < v.length(); i++) {
-            int c = v.charAt(i);
-            if (c == ',' || c == '=' || c == '+' || c == '<' ||
-                    c == '>' || c == '#' || c == ';' || c == '\r' ||
-                    c == '\n' || c == '\\' || c == '"') {
-                if ((c == 0x5c) && ((i + 1) < v.length())) {
-                    int nextC = v.charAt(i + 1);
-                    if ((c == 0x5c) && (nextC == ',' || nextC == '=' || nextC == '+' ||
-                                        nextC == '<' || nextC == '>' || nextC == '#' ||
-                                        nextC == ';' || nextC == '\r' || nextC == '\n' ||
-                                        nextC == '\\' || nextC == '"')) {
-                        if (doubleEscape)
-                            result.append('\\');
-                    } else {
-                        result.append('\\');
-                        if (doubleEscape)
-                            result.append('\\');
-                    }
-                } else {
-                    result.append('\\');
-                    if (doubleEscape)
-                        result.append('\\');
-                }
-            }
-            if (c == '\r') {
-                result.append("0D");
-            } else if (c == '\n') {
-                result.append("0A");
-            } else {
-                result.append((char) c);
-            }
-        }
-        return result;
-    }
-
 }
diff --git a/base/common/src/com/netscape/cms/servlet/cert/CertProcessor.java b/base/common/src/com/netscape/cms/servlet/cert/CertProcessor.java
index 4acc94d0720622ad9e0c340f764438fca44daaba..4e24b5862c866baec90a6dff534f5e8bb9aa4b2c 100644
--- a/base/common/src/com/netscape/cms/servlet/cert/CertProcessor.java
+++ b/base/common/src/com/netscape/cms/servlet/cert/CertProcessor.java
@@ -43,6 +43,7 @@ import com.netscape.certsrv.request.INotify;
 import com.netscape.certsrv.request.IRequest;
 import com.netscape.certsrv.request.RequestStatus;
 import com.netscape.cms.servlet.processors.Processor;
+import com.netscape.cmsutil.ldap.LDAPUtil;
 
 public class CertProcessor extends Processor {
 
@@ -98,8 +99,7 @@ public class CertProcessor extends Processor {
                             // special characters in subject names parameters must be escaped
                             if (inputName.matches("^sn_.*")) {
                                 req.setExtData(inputName,
-                                        escapeValueRfc1779(dataInputs.get(inputName), false)
-                                        .toString());
+                                        LDAPUtil.escapeDN(dataInputs.get(inputName)));
                             } else {
                                 req.setExtData(inputName, dataInputs.get(inputName));
                             }
diff --git a/base/common/src/com/netscape/cms/servlet/cert/EnrollmentProcessor.java b/base/common/src/com/netscape/cms/servlet/cert/EnrollmentProcessor.java
index 911b30fa3abb9afae6e2df69f1de9c4794c23ebd..03d0bb20c45d66ee7eee6c287169308571c3eed7 100644
--- a/base/common/src/com/netscape/cms/servlet/cert/EnrollmentProcessor.java
+++ b/base/common/src/com/netscape/cms/servlet/cert/EnrollmentProcessor.java
@@ -39,6 +39,7 @@ import com.netscape.certsrv.profile.ProfileInput;
 import com.netscape.certsrv.request.IRequest;
 import com.netscape.cms.servlet.common.CMSRequest;
 import com.netscape.cms.servlet.profile.SSLClientCertProvider;
+import com.netscape.cmsutil.ldap.LDAPUtil;
 
 public class EnrollmentProcessor extends CertProcessor {
 
@@ -69,7 +70,7 @@ public class EnrollmentProcessor extends CertProcessor {
                     if (dataInputs.containsKey(inputName)) {
                         // all subject name parameters start with sn_, no other input parameters do
                         if (inputName.matches("^sn_.*")) {
-                            ctx.set(inputName, escapeValueRfc1779(dataInputs.get(inputName), false).toString());
+                            ctx.set(inputName, LDAPUtil.escapeDN(dataInputs.get(inputName)));
                         } else {
                             ctx.set(inputName, dataInputs.get(inputName));
                         }
diff --git a/base/common/src/com/netscape/cms/servlet/cert/SrchCerts.java b/base/common/src/com/netscape/cms/servlet/cert/SrchCerts.java
index d538ce675fe41272fef435272bc67ad164830eb0..b35ca054c7d76ad3fcad838551e8192d103d6210 100644
--- a/base/common/src/com/netscape/cms/servlet/cert/SrchCerts.java
+++ b/base/common/src/com/netscape/cms/servlet/cert/SrchCerts.java
@@ -58,6 +58,7 @@ import com.netscape.cms.servlet.common.CMSRequest;
 import com.netscape.cms.servlet.common.CMSTemplate;
 import com.netscape.cms.servlet.common.CMSTemplateParams;
 import com.netscape.cms.servlet.common.ECMSGWException;
+import com.netscape.cmsutil.ldap.LDAPUtil;
 
 /**
  * Search for certificates matching complex query filter
@@ -224,12 +225,12 @@ public class SrchCerts extends CMSServlet {
                 lf.append("(x509cert.subject=*");
                 lf.append(avaName);
                 lf.append("=");
-                lf.append(escapeValueRfc1779(val, true));
+                lf.append(LDAPUtil.escapeFilter(LDAPUtil.escapeDN(val)));
                 lf.append(",*)");
                 lf.append("(x509cert.subject=*");
                 lf.append(avaName);
                 lf.append("=");
-                lf.append(escapeValueRfc1779(val, true));
+                lf.append(LDAPUtil.escapeFilter(LDAPUtil.escapeDN(val)));
                 lf.append(")");
                 lf.append(")");
             } else {
@@ -237,7 +238,7 @@ public class SrchCerts extends CMSServlet {
                 lf.append(avaName);
                 lf.append("=");
                 lf.append("*");
-                lf.append(escapeValueRfc1779(val, true));
+                lf.append(LDAPUtil.escapeFilter(LDAPUtil.escapeDN(val)));
                 lf.append("*)");
             }
         }
diff --git a/base/common/src/com/netscape/cms/servlet/processors/Processor.java b/base/common/src/com/netscape/cms/servlet/processors/Processor.java
index a39ba715bfdc92bef70b6bb5fc7858bca75bf1cb..fdbc85dd5de3389b1584aa35623d7e88a6d34783 100644
--- a/base/common/src/com/netscape/cms/servlet/processors/Processor.java
+++ b/base/common/src/com/netscape/cms/servlet/processors/Processor.java
@@ -294,45 +294,6 @@ public class Processor {
         CMS.debug("End of CertProcessor Input Parameters");
     }
 
-    protected StringBuffer escapeValueRfc1779(String v, boolean doubleEscape) {
-        StringBuffer result = new StringBuffer();
-
-        // Do we need to escape any characters
-        for (int i = 0; i < v.length(); i++) {
-            int c = v.charAt(i);
-            if (c == ',' || c == '=' || c == '+' || c == '<' ||
-                    c == '>' || c == '#' || c == ';' || c == '\r' ||
-                    c == '\n' || c == '\\' || c == '"') {
-                if ((c == 0x5c) && ((i + 1) < v.length())) {
-                    int nextC = v.charAt(i + 1);
-                    if ((c == 0x5c) && (nextC == ',' || nextC == '=' || nextC == '+' ||
-                            nextC == '<' || nextC == '>' || nextC == '#' ||
-                            nextC == ';' || nextC == '\r' || nextC == '\n' ||
-                            nextC == '\\' || nextC == '"')) {
-                        if (doubleEscape)
-                            result.append('\\');
-                    } else {
-                        result.append('\\');
-                        if (doubleEscape)
-                            result.append('\\');
-                    }
-                } else {
-                    result.append('\\');
-                    if (doubleEscape)
-                        result.append('\\');
-                }
-            }
-            if (c == '\r') {
-                result.append("0D");
-            } else if (c == '\n') {
-                result.append("0A");
-            } else {
-                result.append((char) c);
-            }
-        }
-        return result;
-    }
-
     /**
      * get ssl client authenticated certificate
      */
-- 
1.7.11.2

