1 17 package com.sun.org.apache.xml.internal.security.utils; 18 19 20 21 import java.io.IOException ; 22 import java.io.StringReader ; 23 24 25 29 public class RFC2253Parser { 30 31 32 33 36 37 static boolean _TOXML = true; 38 39 46 public static String rfc2253toXMLdsig(String dn) { 47 48 _TOXML = true; 49 50 String normalized = normalize(dn); 52 53 return rfctoXML(normalized); 54 } 55 56 62 public static String xmldsigtoRFC2253(String dn) { 63 64 _TOXML = false; 65 66 String normalized = normalize(dn); 68 69 return xmltoRFC(normalized); 70 } 71 72 78 public static String normalize(String dn) { 79 80 if ((dn == null) || dn.equals("")) { 82 return ""; 83 } 84 85 try { 86 String _DN = semicolonToComma(dn); 87 StringBuffer sb = new StringBuffer (); 88 int i = 0; 89 int l = 0; 90 int k; 91 92 for (int j = 0; (k = _DN.indexOf(",", j)) >= 0; j = k + 1) { 94 l += countQuotes(_DN, j, k); 95 96 if ((k > 0) && (_DN.charAt(k - 1) != '\\') && (l % 2) != 1) { 97 sb.append(parseRDN(_DN.substring(i, k).trim()) + ","); 98 99 i = k + 1; 100 l = 0; 101 } 102 } 103 104 sb.append(parseRDN(trim(_DN.substring(i)))); 105 106 return sb.toString(); 107 } catch (IOException ex) { 108 return dn; 109 } 110 } 111 112 119 static String parseRDN(String str) throws IOException { 120 121 StringBuffer sb = new StringBuffer (); 122 int i = 0; 123 int l = 0; 124 int k; 125 126 for (int j = 0; (k = str.indexOf("+", j)) >= 0; j = k + 1) { 127 l += countQuotes(str, j, k); 128 129 if ((k > 0) && (str.charAt(k - 1) != '\\') && (l % 2) != 1) { 130 sb.append(parseATAV(trim(str.substring(i, k))) + "+"); 131 132 i = k + 1; 133 l = 0; 134 } 135 } 136 137 sb.append(parseATAV(trim(str.substring(i)))); 138 139 return sb.toString(); 140 } 141 142 149 static String parseATAV(String str) throws IOException { 150 151 int i = str.indexOf("="); 152 153 if ((i == -1) || ((i > 0) && (str.charAt(i - 1) == '\\'))) { 154 return str; 155 } 156 String attrType = normalizeAT(str.substring(0, i)); 157 String attrValue = normalizeV(str.substring(i + 1)); 158 159 return attrType + "=" + attrValue; 160 161 } 162 163 169 static String normalizeAT(String str) { 170 171 String at = str.toUpperCase().trim(); 172 173 if (at.startsWith("OID")) { 174 at = at.substring(3); 175 } 176 177 return at; 178 } 179 180 187 static String normalizeV(String str) throws IOException { 188 189 String value = trim(str); 190 191 if (value.startsWith("\"")) { 192 StringBuffer sb = new StringBuffer (); 193 StringReader sr = new StringReader (value.substring(1, 194 value.length() - 1)); 195 int i = 0; 196 char c; 197 198 for (; (i = sr.read()) > -1; ) { 199 c = (char) i; 200 201 if ((c == ',') || (c == '=') || (c == '+') || (c == '<') 203 || (c == '>') || (c == '#') || (c == ';')) { 204 sb.append('\\'); 205 } 206 207 sb.append(c); 208 } 209 210 value = trim(sb.toString()); 211 } 212 213 if (_TOXML == true) { 214 if (value.startsWith("#")) { 215 value = '\\' + value; 216 } 217 } else { 218 if (value.startsWith("\\#")) { 219 value = value.substring(1); 220 } 221 } 222 223 return value; 224 } 225 226 232 static String rfctoXML(String string) { 233 234 try { 235 String s = changeLess32toXML(string); 236 237 return changeWStoXML(s); 238 } catch (Exception e) { 239 return string; 240 } 241 } 242 243 249 static String xmltoRFC(String string) { 250 251 try { 252 String s = changeLess32toRFC(string); 253 254 return changeWStoRFC(s); 255 } catch (Exception e) { 256 return string; 257 } 258 } 259 260 267 static String changeLess32toRFC(String string) throws IOException { 268 269 StringBuffer sb = new StringBuffer (); 270 StringReader sr = new StringReader (string); 271 int i = 0; 272 char c; 273 274 for (; (i = sr.read()) > -1; ) { 275 c = (char) i; 276 277 if (c == '\\') { 278 sb.append(c); 279 280 char c1 = (char) sr.read(); 281 char c2 = (char) sr.read(); 282 283 if ((((c1 >= 48) && (c1 <= 57)) || ((c1 >= 65) && (c1 <= 70)) || ((c1 >= 97) && (c1 <= 102))) 285 && (((c2 >= 48) && (c2 <= 57)) 286 || ((c2 >= 65) && (c2 <= 70)) 287 || ((c2 >= 97) && (c2 <= 102)))) { 288 char ch = (char) Byte.parseByte("" + c1 + c2, 16); 289 290 sb.append(ch); 291 } else { 292 sb.append(c1); 293 sb.append(c2); 294 } 295 } else { 296 sb.append(c); 297 } 298 } 299 300 return sb.toString(); 301 } 302 303 310 static String changeLess32toXML(String string) throws IOException { 311 312 StringBuffer sb = new StringBuffer (); 313 StringReader sr = new StringReader (string); 314 int i = 0; 315 316 for (; (i = sr.read()) > -1; ) { 317 if (i < 32) { 318 sb.append('\\'); 319 sb.append(Integer.toHexString(i)); 320 } else { 321 sb.append((char) i); 322 } 323 } 324 325 return sb.toString(); 326 } 327 328 335 static String changeWStoXML(String string) throws IOException { 336 337 StringBuffer sb = new StringBuffer (); 338 StringReader sr = new StringReader (string); 339 int i = 0; 340 char c; 341 342 for (; (i = sr.read()) > -1; ) { 343 c = (char) i; 344 345 if (c == '\\') { 346 char c1 = (char) sr.read(); 347 348 if (c1 == ' ') { 349 sb.append('\\'); 350 351 String s = "20"; 352 353 sb.append(s); 354 } else { 355 sb.append('\\'); 356 sb.append(c1); 357 } 358 } else { 359 sb.append(c); 360 } 361 } 362 363 return sb.toString(); 364 } 365 366 372 static String changeWStoRFC(String string) { 373 374 StringBuffer sb = new StringBuffer (); 375 int i = 0; 376 int k; 377 378 for (int j = 0; (k = string.indexOf("\\20", j)) >= 0; j = k + 3) { 379 sb.append(trim(string.substring(i, k)) + "\\ "); 380 381 i = k + 3; 382 } 383 384 sb.append(string.substring(i)); 385 386 return sb.toString(); 387 } 388 389 395 static String semicolonToComma(String str) { 396 return removeWSandReplace(str, ";", ","); 397 } 398 399 406 static String removeWhiteSpace(String str, String symbol) { 407 return removeWSandReplace(str, symbol, symbol); 408 } 409 410 418 static String removeWSandReplace(String str, String symbol, String replace) { 419 420 StringBuffer sb = new StringBuffer (); 421 int i = 0; 422 int l = 0; 423 int k; 424 425 for (int j = 0; (k = str.indexOf(symbol, j)) >= 0; j = k + 1) { 426 l += countQuotes(str, j, k); 427 428 if ((k > 0) && (str.charAt(k - 1) != '\\') && (l % 2) != 1) { 429 sb.append(trim(str.substring(i, k)) + replace); 430 431 i = k + 1; 432 l = 0; 433 } 434 } 435 436 sb.append(trim(str.substring(i))); 437 438 return sb.toString(); 439 } 440 441 449 private static int countQuotes(String s, int i, int j) { 450 451 int k = 0; 452 453 for (int l = i; l < j; l++) { 454 if (s.charAt(l) == '"') { 455 k++; 456 } 457 } 458 459 return k; 460 } 461 462 464 470 static String trim(String str) { 471 472 String trimed = str.trim(); 473 int i = str.indexOf(trimed.substring(0)) + trimed.length(); 474 475 if ((str.length() > i) && trimed.endsWith("\\") 476 &&!trimed.endsWith("\\\\")) { 477 if (str.charAt(i) == ' ') { 478 trimed = trimed + " "; 479 } 480 } 481 482 return trimed; 483 } 484 485 491 public static void main(String [] args) throws Exception { 492 493 testToXML("CN=\"Steve, Kille\", O=Isode Limited, C=GB"); 494 testToXML("CN=Steve Kille , O=Isode Limited,C=GB"); 495 testToXML("\\ OU=Sales+CN=J. Smith,O=Widget Inc.,C=US\\ \\ "); 496 testToXML("CN=L. Eagle,O=Sue\\, Grabbit and Runn,C=GB"); 497 testToXML("CN=Before\\0DAfter,O=Test,C=GB"); 498 testToXML("CN=\"L. Eagle,O=Sue, = + < > # ;Grabbit and Runn\",C=GB"); 499 testToXML("1.3.6.1.4.1.1466.0=#04024869,O=Test,C=GB"); 500 501 { 502 StringBuffer sb = new StringBuffer (); 503 504 sb.append('L'); 505 sb.append('u'); 506 sb.append('\uc48d'); 507 sb.append('i'); 508 sb.append('\uc487'); 509 510 String test7 = "SN=" + sb.toString(); 511 512 testToXML(test7); 513 } 514 515 testToRFC("CN=\"Steve, Kille\", O=Isode Limited, C=GB"); 516 testToRFC("CN=Steve Kille , O=Isode Limited,C=GB"); 517 testToRFC("\\20OU=Sales+CN=J. Smith,O=Widget Inc.,C=US\\20\\20 "); 518 testToRFC("CN=L. Eagle,O=Sue\\, Grabbit and Runn,C=GB"); 519 testToRFC("CN=Before\\12After,O=Test,C=GB"); 520 testToRFC("CN=\"L. Eagle,O=Sue, = + < > # ;Grabbit and Runn\",C=GB"); 521 testToRFC("1.3.6.1.4.1.1466.0=\\#04024869,O=Test,C=GB"); 522 523 { 524 StringBuffer sb = new StringBuffer (); 525 526 sb.append('L'); 527 sb.append('u'); 528 sb.append('\uc48d'); 529 sb.append('i'); 530 sb.append('\uc487'); 531 532 String test7 = "SN=" + sb.toString(); 533 534 testToRFC(test7); 535 } 536 } 537 538 539 static int counter = 0; 540 541 546 static void testToXML(String st) { 547 548 System.out.println("start " + counter++ + ": " + st); 549 System.out.println(" " + rfc2253toXMLdsig(st)); 550 System.out.println(""); 551 } 552 553 558 static void testToRFC(String st) { 559 560 System.out.println("start " + counter++ + ": " + st); 561 System.out.println(" " + xmldsigtoRFC2253(st)); 562 System.out.println(""); 563 } 564 } 565 | Popular Tags |