Path: blob/master/test/jdk/sun/security/x509/AVA/EmailAddressEncoding.java
41153 views
/*1* Copyright (c) 2002, 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 470254326* @summary incorrect ASN1 DER encoding of EmailAddress Attribute27* @modules java.base/sun.security.pkcs28* java.base/sun.security.util29*/3031import java.io.*;32import javax.security.auth.x500.*;33import sun.security.util.*;34import sun.security.pkcs.*;3536public class EmailAddressEncoding {3738public static void main(String[] args) throws Exception {39X500Principal p = new X500Principal40("c=us, emailaddress=foo@bar, cn=foobar");41byte[] encoded = p.getEncoded();4243// name is a sequence of RDN's44DerInputStream dis = new DerInputStream(encoded);45DerValue[] nameseq = dis.getSequence(3);4647boolean passed = false;48for (int i = 0; i < nameseq.length; i++) {4950// each RDN is a set of AttributeTypeAndValue51DerInputStream is = new DerInputStream(nameseq[i].toByteArray());52DerValue[] ava = is.getSet(3);5354for (int j = 0; j < ava.length; j++) {5556ObjectIdentifier oid = ava[j].data.getOID();5758if (oid.equals(PKCS9Attribute.EMAIL_ADDRESS_OID)) {59DerValue value = ava[j].data.getDerValue();60if (value.getTag() == DerValue.tag_IA5String) {61passed = true;62break;63} else {64throw new SecurityException65("Test failed, expected EMAIL_ADDRESS tag '" +66DerValue.tag_IA5String +67"', got '" +68value.getTag() + "'");69}70}71}7273if (passed) {74break;75}76}7778if (passed) {79System.out.println("Test passed");80} else {81throw new SecurityException("Test failed");82}83}84}858687