Path: blob/master/test/jdk/sun/security/x509/AVA/DomainComponentEncoding.java
41153 views
/*1* Copyright (c) 2006, 2007, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 639148226* @summary incorrect ASN1 DER encoding of DomainComponent AttributeValue27* @modules java.base/sun.security.util28* java.base/sun.security.x50929*/3031import javax.security.auth.x500.X500Principal;32import sun.security.util.DerInputStream;33import sun.security.util.DerValue;34import sun.security.util.ObjectIdentifier;35import sun.security.x509.X500Name;3637public class DomainComponentEncoding {3839public static void main(String[] args) throws Exception {40// RFC 2253 String DN41testDN("cn=hello, dc=com, dc=example");42// RFC 1779 String DN with embedded quotes43testDN("cn=hello, dc=\"com\", dc=example");44}4546private static void testDN(String dn) throws Exception {47X500Principal p = new X500Principal(dn);48byte[] encoded = p.getEncoded();4950// name is a sequence of RDN's51DerInputStream dis = new DerInputStream(encoded);52DerValue[] nameseq = dis.getSequence(3);5354boolean passed = false;55for (int i = 0; i < nameseq.length; i++) {5657// each RDN is a set of AttributeTypeAndValue58DerInputStream is = new DerInputStream(nameseq[i].toByteArray());59DerValue[] ava = is.getSet(3);6061for (int j = 0; j < ava.length; j++) {6263ObjectIdentifier oid = ava[j].data.getOID();6465if (oid.equals(X500Name.DOMAIN_COMPONENT_OID)) {66DerValue value = ava[j].data.getDerValue();67if (value.getTag() == DerValue.tag_IA5String) {68passed = true;69break;70} else {71throw new SecurityException72("Test failed, expected DOMAIN_COMPONENT tag '" +73DerValue.tag_IA5String +74"', got '" +75value.getTag() + "'");76}77}78}7980if (passed) {81break;82}83}8485if (passed) {86System.out.println("Test passed");87} else {88throw new SecurityException("Test failed");89}90}91}929394