From 10ce0b96ab64dc28d9553c3771c73e9782f0b2a4 Mon Sep 17 00:00:00 2001 From: mullan Date: Thu, 13 Mar 2008 13:29:27 -0400 Subject: [PATCH] 6611991: Add support for parsing RFC4514 DNs to X500Principal Summary: Added new test and made one code change to escape null characters. Reviewed-by: vinnie --- src/share/classes/sun/security/x509/AVA.java | 7 +- .../auth/x500/X500Principal/RFC4514.java | 92 +++++++++++++++++++ 2 files changed, 98 insertions(+), 1 deletion(-) create mode 100644 test/javax/security/auth/x500/X500Principal/RFC4514.java diff --git a/src/share/classes/sun/security/x509/AVA.java b/src/share/classes/sun/security/x509/AVA.java index 9a6b7a59d..43d914cba 100644 --- a/src/share/classes/sun/security/x509/AVA.java +++ b/src/share/classes/sun/security/x509/AVA.java @@ -780,7 +780,8 @@ public class AVA implements DerEncoder { * Implementations MAY escape other characters. * * NOTE: this implementation also recognizes "=" and "#" as - * characters which need escaping. + * characters which need escaping, and null which is escaped as + * '\00' (see RFC 4514). * * If a character to be escaped is one of the list shown above, then * it is prefixed by a backslash ('\' ASCII 92). @@ -805,6 +806,10 @@ public class AVA implements DerEncoder { // append printable/escaped char sbuffer.append(c); + } else if (c == '\u0000') { + // escape null character + sbuffer.append("\\00"); + } else if (debug != null && Debug.isOn("ava")) { // embed non-printable/non-escaped char diff --git a/test/javax/security/auth/x500/X500Principal/RFC4514.java b/test/javax/security/auth/x500/X500Principal/RFC4514.java new file mode 100644 index 000000000..6169314ab --- /dev/null +++ b/test/javax/security/auth/x500/X500Principal/RFC4514.java @@ -0,0 +1,92 @@ +/* + * Copyright 2008 Sun Microsystems, Inc. All Rights Reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code 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 + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, + * CA 95054 USA or visit www.sun.com if you need additional information or + * have any questions. + */ + +import javax.security.auth.x500.X500Principal; + +/** + * @test + * @bug 6611991 + * @summary Add support for parsing RFC 4514 DNs to X500Principal + * + * Ensure RFC 4514 Distinguished Name Strings can be parsed by X500Principal. + * RFC 4514 obsoleted RFC 2253 so we should make sure we can parse DNs of + * that form that contain subtle differences or clarifications in the grammar. + */ +public class RFC4514 { + + private int failed = 0; + + public static void main(String[] args) throws Exception { + new RFC4514().test(); + } + + private void test() throws Exception { + + /** + * RFC 4514 allows space to be escaped as '\ '. + */ + parse("CN=\\ Space\\ ,C=US"); + parse("CN=Sp\\ ace,C=US"); + /** + * RFC 4514 does not require escaping of '=' characters. + */ + parse("CN=Eq=uals,C=US"); + /** + * RFC 4514 requires the null character to be escaped. + */ + parse("CN=\\00,C=US"); + /** + * RFC 4514 does not require escaping of non-leading '#' characters. + */ + parse("CN=Num#ber,C=US"); + /** + * XMLDSig (http://www.w3.org/TR/2002/REC-xmldsig-core-20020212/) + * allows implementations to escape trailing whitespace as '\20'. + */ + parse("CN=Trailing \\20,C=US"); + /** + * XMLDSig allows implementations to escape ASCII control characters + * (Unicode range \x00 - \x1f) by replacing them with "\" followed by + * a two digit hex number showing its Unicode number. + */ + parse("CN=Con\\09trol,C=US"); + + if (failed != 0) { + throw new Exception("Some RFC4514 tests FAILED"); + } + } + + public void parse(String dnString) throws Exception { + + System.out.println("Parsing " + dnString); + X500Principal dn = new X500Principal(dnString); + String dnString2 = dn.getName(); + X500Principal dn2 = new X500Principal(dnString2); + if (dn.equals(dn2)) { + System.out.println("PASSED"); + } else { + System.out.println("FAILED"); + failed++; + } + } +} -- GitLab