1/*
2 * Copyright (c) 2006, 2007, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24/*
25 * @test
26 * @bug 6391482
27 * @summary incorrect ASN1 DER encoding of DomainComponent AttributeValue
28 * @modules java.base/sun.security.util
29 *          java.base/sun.security.x509
30 */
31
32import javax.security.auth.x500.X500Principal;
33import sun.security.util.DerInputStream;
34import sun.security.util.DerValue;
35import sun.security.util.ObjectIdentifier;
36import sun.security.x509.X500Name;
37
38public class DomainComponentEncoding {
39
40    public static void main(String[] args) throws Exception {
41        // RFC 2253 String DN
42        testDN("cn=hello, dc=com, dc=example");
43        // RFC 1779 String DN with embedded quotes
44        testDN("cn=hello, dc=\"com\", dc=example");
45    }
46
47    private static void testDN(String dn) throws Exception {
48        X500Principal p = new X500Principal(dn);
49        byte[] encoded = p.getEncoded();
50
51        // name is a sequence of RDN's
52        DerInputStream dis = new DerInputStream(encoded);
53        DerValue[] nameseq = dis.getSequence(3);
54
55        boolean passed = false;
56        for (int i = 0; i < nameseq.length; i++) {
57
58            // each RDN is a set of AttributeTypeAndValue
59            DerInputStream is = new DerInputStream(nameseq[i].toByteArray());
60            DerValue[] ava = is.getSet(3);
61
62            for (int j = 0; j < ava.length; j++) {
63
64                ObjectIdentifier oid = ava[j].data.getOID();
65
66                if (oid.equals(X500Name.DOMAIN_COMPONENT_OID)) {
67                    DerValue value = ava[j].data.getDerValue();
68                    if (value.getTag() == DerValue.tag_IA5String) {
69                        passed = true;
70                        break;
71                    } else {
72                        throw new SecurityException
73                                ("Test failed, expected DOMAIN_COMPONENT tag '" +
74                                DerValue.tag_IA5String +
75                                "', got '" +
76                                value.getTag() + "'");
77                    }
78                }
79            }
80
81            if (passed) {
82                break;
83            }
84        }
85
86        if (passed) {
87            System.out.println("Test passed");
88        } else {
89            throw new SecurityException("Test failed");
90        }
91    }
92}
93