Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/security/x509/AVA/DomainComponentEncoding.java
38853 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*/2829import javax.security.auth.x500.X500Principal;30import sun.security.util.DerInputStream;31import sun.security.util.DerValue;32import sun.security.util.ObjectIdentifier;33import sun.security.x509.X500Name;3435public class DomainComponentEncoding {3637public static void main(String[] args) throws Exception {38// RFC 2253 String DN39testDN("cn=hello, dc=com, dc=example");40// RFC 1779 String DN with embedded quotes41testDN("cn=hello, dc=\"com\", dc=example");42}4344private static void testDN(String dn) throws Exception {45X500Principal p = new X500Principal(dn);46byte[] encoded = p.getEncoded();4748// name is a sequence of RDN's49DerInputStream dis = new DerInputStream(encoded);50DerValue[] nameseq = dis.getSequence(3);5152boolean passed = false;53for (int i = 0; i < nameseq.length; i++) {5455// each RDN is a set of AttributeTypeAndValue56DerInputStream is = new DerInputStream(nameseq[i].toByteArray());57DerValue[] ava = is.getSet(3);5859for (int j = 0; j < ava.length; j++) {6061ObjectIdentifier oid = ava[j].data.getOID();6263if (oid.equals(X500Name.DOMAIN_COMPONENT_OID)) {64DerValue value = ava[j].data.getDerValue();65if (value.getTag() == DerValue.tag_IA5String) {66passed = true;67break;68} else {69throw new SecurityException70("Test failed, expected DOMAIN_COMPONENT tag '" +71DerValue.tag_IA5String +72"', got '" +73value.getTag() + "'");74}75}76}7778if (passed) {79break;80}81}8283if (passed) {84System.out.println("Test passed");85} else {86throw new SecurityException("Test failed");87}88}89}909192