src/share/classes/com/sun/org/apache/xml/internal/security/utils/RFC2253Parser.java

Print this page

        

*** 1,60 **** /* * reserved comment block * DO NOT REMOVE OR ALTER! */ ! /* ! * Copyright 1999-2004 The Apache Software Foundation. ! * ! * Licensed under the Apache License, Version 2.0 (the "License"); ! * you may not use this file except in compliance with the License. ! * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * ! * Unless required by applicable law or agreed to in writing, software ! * distributed under the License is distributed on an "AS IS" BASIS, ! * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. ! * See the License for the specific language governing permissions and ! * limitations under the License. ! * */ package com.sun.org.apache.xml.internal.security.utils; - - import java.io.IOException; import java.io.StringReader; - - /** - * - * @author $Author: mullan $ - */ public class RFC2253Parser { - - /** {@link java.util.logging} logging facility */ - /* static java.util.logging.Logger log = - java.util.logging.Logger.getLogger(RFC2253Parser.class.getName()); - */ - - static boolean _TOXML = true; - /** * Method rfc2253toXMLdsig * * @param dn * @return normalized string - * */ public static String rfc2253toXMLdsig(String dn) { - - _TOXML = true; - // Transform from RFC1779 to RFC2253 ! String normalized = normalize(dn); return rfctoXML(normalized); } /** --- 1,43 ---- /* * reserved comment block * DO NOT REMOVE OR ALTER! */ ! /** ! * Licensed to the Apache Software Foundation (ASF) under one ! * or more contributor license agreements. See the NOTICE file ! * distributed with this work for additional information ! * regarding copyright ownership. The ASF licenses this file ! * to you under the Apache License, Version 2.0 (the ! * "License"); you may not use this file except in compliance ! * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * ! * Unless required by applicable law or agreed to in writing, ! * software distributed under the License is distributed on an ! * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY ! * KIND, either express or implied. See the License for the ! * specific language governing permissions and limitations ! * under the License. */ package com.sun.org.apache.xml.internal.security.utils; import java.io.IOException; import java.io.StringReader; public class RFC2253Parser { /** * Method rfc2253toXMLdsig * * @param dn * @return normalized string */ public static String rfc2253toXMLdsig(String dn) { // Transform from RFC1779 to RFC2253 ! String normalized = normalize(dn, true); return rfctoXML(normalized); } /**
*** 62,76 **** * * @param dn * @return normalized string */ public static String xmldsigtoRFC2253(String dn) { - - _TOXML = false; - // Transform from RFC1779 to RFC2253 ! String normalized = normalize(dn); return xmltoRFC(normalized); } /** --- 45,56 ---- * * @param dn * @return normalized string */ public static String xmldsigtoRFC2253(String dn) { // Transform from RFC1779 to RFC2253 ! String normalized = normalize(dn, false); return xmltoRFC(normalized); } /**
*** 78,113 **** * * @param dn * @return normalized string */ public static String normalize(String dn) { //if empty string if ((dn == null) || dn.equals("")) { return ""; } try { ! String _DN = semicolonToComma(dn); ! StringBuffer sb = new StringBuffer(); int i = 0; int l = 0; int k; //for name component ! for (int j = 0; (k = _DN.indexOf(",", j)) >= 0; j = k + 1) { ! l += countQuotes(_DN, j, k); ! if ((k > 0) && (_DN.charAt(k - 1) != '\\') && (l % 2) != 1) { ! sb.append(parseRDN(_DN.substring(i, k).trim()) + ","); i = k + 1; l = 0; } } ! sb.append(parseRDN(trim(_DN.substring(i)))); return sb.toString(); } catch (IOException ex) { return dn; } --- 58,103 ---- * * @param dn * @return normalized string */ public static String normalize(String dn) { + return normalize(dn, true); + } + /** + * Method normalize + * + * @param dn + * @param toXml + * @return normalized string + */ + public static String normalize(String dn, boolean toXml) { //if empty string if ((dn == null) || dn.equals("")) { return ""; } try { ! String DN = semicolonToComma(dn); ! StringBuilder sb = new StringBuilder(); int i = 0; int l = 0; int k; //for name component ! for (int j = 0; (k = DN.indexOf(',', j)) >= 0; j = k + 1) { ! l += countQuotes(DN, j, k); ! if ((k > 0) && (DN.charAt(k - 1) != '\\') && (l % 2) == 0) { ! sb.append(parseRDN(DN.substring(i, k).trim(), toXml) + ","); i = k + 1; l = 0; } } ! sb.append(parseRDN(trim(DN.substring(i)), toXml)); return sb.toString(); } catch (IOException ex) { return dn; }
*** 115,171 **** /** * Method parseRDN * * @param str * @return normalized string * @throws IOException */ ! static String parseRDN(String str) throws IOException { ! ! StringBuffer sb = new StringBuffer(); int i = 0; int l = 0; int k; ! for (int j = 0; (k = str.indexOf("+", j)) >= 0; j = k + 1) { l += countQuotes(str, j, k); ! if ((k > 0) && (str.charAt(k - 1) != '\\') && (l % 2) != 1) { ! sb.append(parseATAV(trim(str.substring(i, k))) + "+"); i = k + 1; l = 0; } } ! sb.append(parseATAV(trim(str.substring(i)))); return sb.toString(); } /** * Method parseATAV * * @param str * @return normalized string * @throws IOException */ ! static String parseATAV(String str) throws IOException { ! ! int i = str.indexOf("="); if ((i == -1) || ((i > 0) && (str.charAt(i - 1) == '\\'))) { return str; } String attrType = normalizeAT(str.substring(0, i)); // only normalize if value is a String String attrValue = null; if (attrType.charAt(0) >= '0' && attrType.charAt(0) <= '9') { attrValue = str.substring(i + 1); } else { ! attrValue = normalizeV(str.substring(i + 1)); } return attrType + "=" + attrValue; } --- 105,161 ---- /** * Method parseRDN * * @param str + * @param toXml * @return normalized string * @throws IOException */ ! static String parseRDN(String str, boolean toXml) throws IOException { ! StringBuilder sb = new StringBuilder(); int i = 0; int l = 0; int k; ! for (int j = 0; (k = str.indexOf('+', j)) >= 0; j = k + 1) { l += countQuotes(str, j, k); ! if ((k > 0) && (str.charAt(k - 1) != '\\') && (l % 2) == 0) { ! sb.append(parseATAV(trim(str.substring(i, k)), toXml) + "+"); i = k + 1; l = 0; } } ! sb.append(parseATAV(trim(str.substring(i)), toXml)); return sb.toString(); } /** * Method parseATAV * * @param str + * @param toXml * @return normalized string * @throws IOException */ ! static String parseATAV(String str, boolean toXml) throws IOException { ! int i = str.indexOf('='); if ((i == -1) || ((i > 0) && (str.charAt(i - 1) == '\\'))) { return str; } String attrType = normalizeAT(str.substring(0, i)); // only normalize if value is a String String attrValue = null; if (attrType.charAt(0) >= '0' && attrType.charAt(0) <= '9') { attrValue = str.substring(i + 1); } else { ! attrValue = normalizeV(str.substring(i + 1), toXml); } return attrType + "=" + attrValue; }
*** 189,213 **** /** * Method normalizeV * * @param str * @return normalized string * @throws IOException */ ! static String normalizeV(String str) throws IOException { ! String value = trim(str); if (value.startsWith("\"")) { ! StringBuffer sb = new StringBuffer(); ! StringReader sr = new StringReader(value.substring(1, ! value.length() - 1)); int i = 0; char c; ! for (; (i = sr.read()) > -1; ) { c = (char) i; //the following char is defined at 4.Relationship with RFC1779 and LDAPv2 inrfc2253 if ((c == ',') || (c == '=') || (c == '+') || (c == '<') || (c == '>') || (c == '#') || (c == ';')) { --- 179,202 ---- /** * Method normalizeV * * @param str + * @param toXml * @return normalized string * @throws IOException */ ! static String normalizeV(String str, boolean toXml) throws IOException { String value = trim(str); if (value.startsWith("\"")) { ! StringBuilder sb = new StringBuilder(); ! StringReader sr = new StringReader(value.substring(1, value.length() - 1)); int i = 0; char c; ! while ((i = sr.read()) > -1) { c = (char) i; //the following char is defined at 4.Relationship with RFC1779 and LDAPv2 inrfc2253 if ((c == ',') || (c == '=') || (c == '+') || (c == '<') || (c == '>') || (c == '#') || (c == ';')) {
*** 218,228 **** } value = trim(sb.toString()); } ! if (_TOXML == true) { if (value.startsWith("#")) { value = '\\' + value; } } else { if (value.startsWith("\\#")) { --- 207,217 ---- } value = trim(sb.toString()); } ! if (toXml) { if (value.startsWith("#")) { value = '\\' + value; } } else { if (value.startsWith("\\#")) {
*** 238,248 **** * * @param string * @return normalized string */ static String rfctoXML(String string) { - try { String s = changeLess32toXML(string); return changeWStoXML(s); } catch (Exception e) { --- 227,236 ----
*** 255,265 **** * * @param string * @return normalized string */ static String xmltoRFC(String string) { - try { String s = changeLess32toRFC(string); return changeWStoRFC(s); } catch (Exception e) { --- 243,252 ----
*** 273,289 **** * @param string * @return normalized string * @throws IOException */ static String changeLess32toRFC(String string) throws IOException { ! ! StringBuffer sb = new StringBuffer(); StringReader sr = new StringReader(string); int i = 0; char c; ! for (; (i = sr.read()) > -1; ) { c = (char) i; if (c == '\\') { sb.append(c); --- 260,275 ---- * @param string * @return normalized string * @throws IOException */ static String changeLess32toRFC(String string) throws IOException { ! StringBuilder sb = new StringBuilder(); StringReader sr = new StringReader(string); int i = 0; char c; ! while ((i = sr.read()) > -1) { c = (char) i; if (c == '\\') { sb.append(c);
*** 316,331 **** * @param string * @return normalized string * @throws IOException */ static String changeLess32toXML(String string) throws IOException { ! ! StringBuffer sb = new StringBuffer(); StringReader sr = new StringReader(string); int i = 0; ! for (; (i = sr.read()) > -1; ) { if (i < 32) { sb.append('\\'); sb.append(Integer.toHexString(i)); } else { sb.append((char) i); --- 302,316 ---- * @param string * @return normalized string * @throws IOException */ static String changeLess32toXML(String string) throws IOException { ! StringBuilder sb = new StringBuilder(); StringReader sr = new StringReader(string); int i = 0; ! while ((i = sr.read()) > -1) { if (i < 32) { sb.append('\\'); sb.append(Integer.toHexString(i)); } else { sb.append((char) i);
*** 341,357 **** * @param string * @return normalized string * @throws IOException */ static String changeWStoXML(String string) throws IOException { ! ! StringBuffer sb = new StringBuffer(); StringReader sr = new StringReader(string); int i = 0; char c; ! for (; (i = sr.read()) > -1; ) { c = (char) i; if (c == '\\') { char c1 = (char) sr.read(); --- 326,341 ---- * @param string * @return normalized string * @throws IOException */ static String changeWStoXML(String string) throws IOException { ! StringBuilder sb = new StringBuilder(); StringReader sr = new StringReader(string); int i = 0; char c; ! while ((i = sr.read()) > -1) { c = (char) i; if (c == '\\') { char c1 = (char) sr.read();
*** 378,389 **** * * @param string * @return normalized string */ static String changeWStoRFC(String string) { ! ! StringBuffer sb = new StringBuffer(); int i = 0; int k; for (int j = 0; (k = string.indexOf("\\20", j)) >= 0; j = k + 3) { sb.append(trim(string.substring(i, k)) + "\\ "); --- 362,372 ---- * * @param string * @return normalized string */ static String changeWStoRFC(String string) { ! StringBuilder sb = new StringBuilder(); int i = 0; int k; for (int j = 0; (k = string.indexOf("\\20", j)) >= 0; j = k + 3) { sb.append(trim(string.substring(i, k)) + "\\ ");
*** 424,443 **** * @param symbol * @param replace * @return normalized string */ static String removeWSandReplace(String str, String symbol, String replace) { ! ! StringBuffer sb = new StringBuffer(); int i = 0; int l = 0; int k; for (int j = 0; (k = str.indexOf(symbol, j)) >= 0; j = k + 1) { l += countQuotes(str, j, k); ! if ((k > 0) && (str.charAt(k - 1) != '\\') && (l % 2) != 1) { sb.append(trim(str.substring(i, k)) + replace); i = k + 1; l = 0; } --- 407,425 ---- * @param symbol * @param replace * @return normalized string */ static String removeWSandReplace(String str, String symbol, String replace) { ! StringBuilder sb = new StringBuilder(); int i = 0; int l = 0; int k; for (int j = 0; (k = str.indexOf(symbol, j)) >= 0; j = k + 1) { l += countQuotes(str, j, k); ! if ((k > 0) && (str.charAt(k - 1) != '\\') && (l % 2) == 0) { sb.append(trim(str.substring(i, k)) + replace); i = k + 1; l = 0; }
*** 455,465 **** * @param i * @param j * @return number of quotes */ private static int countQuotes(String s, int i, int j) { - int k = 0; for (int l = i; l < j; l++) { if (s.charAt(l) == '"') { k++; --- 437,446 ----
*** 481,574 **** String trimed = str.trim(); int i = str.indexOf(trimed) + trimed.length(); if ((str.length() > i) && trimed.endsWith("\\") ! &&!trimed.endsWith("\\\\")) { ! if (str.charAt(i) == ' ') { trimed = trimed + " "; } - } return trimed; } - /** - * Method main - * - * @param args - * @throws Exception - */ - public static void main(String[] args) throws Exception { - - testToXML("CN=\"Steve, Kille\", O=Isode Limited, C=GB"); - testToXML("CN=Steve Kille , O=Isode Limited,C=GB"); - testToXML("\\ OU=Sales+CN=J. Smith,O=Widget Inc.,C=US\\ \\ "); - testToXML("CN=L. Eagle,O=Sue\\, Grabbit and Runn,C=GB"); - testToXML("CN=Before\\0DAfter,O=Test,C=GB"); - testToXML("CN=\"L. Eagle,O=Sue, = + < > # ;Grabbit and Runn\",C=GB"); - testToXML("1.3.6.1.4.1.1466.0=#04024869,O=Test,C=GB"); - - { - StringBuffer sb = new StringBuffer(); - - sb.append('L'); - sb.append('u'); - sb.append('\uc48d'); - sb.append('i'); - sb.append('\uc487'); - - String test7 = "SN=" + sb.toString(); - - testToXML(test7); - } - - testToRFC("CN=\"Steve, Kille\", O=Isode Limited, C=GB"); - testToRFC("CN=Steve Kille , O=Isode Limited,C=GB"); - testToRFC("\\20OU=Sales+CN=J. Smith,O=Widget Inc.,C=US\\20\\20 "); - testToRFC("CN=L. Eagle,O=Sue\\, Grabbit and Runn,C=GB"); - testToRFC("CN=Before\\12After,O=Test,C=GB"); - testToRFC("CN=\"L. Eagle,O=Sue, = + < > # ;Grabbit and Runn\",C=GB"); - testToRFC("1.3.6.1.4.1.1466.0=\\#04024869,O=Test,C=GB"); - - { - StringBuffer sb = new StringBuffer(); - - sb.append('L'); - sb.append('u'); - sb.append('\uc48d'); - sb.append('i'); - sb.append('\uc487'); - - String test7 = "SN=" + sb.toString(); - - testToRFC(test7); - } - } - - /** Field i */ - static int counter = 0; - - /** - * Method test - * - * @param st - */ - static void testToXML(String st) { - - System.out.println("start " + counter++ + ": " + st); - System.out.println(" " + rfc2253toXMLdsig(st)); - System.out.println(""); - } - - /** - * Method testToRFC - * - * @param st - */ - static void testToRFC(String st) { - - System.out.println("start " + counter++ + ": " + st); - System.out.println(" " + xmldsigtoRFC2253(st)); - System.out.println(""); - } } --- 462,474 ---- String trimed = str.trim(); int i = str.indexOf(trimed) + trimed.length(); if ((str.length() > i) && trimed.endsWith("\\") ! && !trimed.endsWith("\\\\") && (str.charAt(i) == ' ')) { trimed = trimed + " "; } return trimed; } }