Path: blob/master/test/jdk/sun/security/provider/PolicyFile/EmailAddress.java
41153 views
/*1* Copyright (c) 2002, 2003, 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 X500Principal encodes EmailAddress incorrectly -27*28* fix has compatibility ramifications for policy.29*30* this test is related to the Alias.java test in the same directory.31* the email address encoding in EmailAddress.policy is the one32* taken from the persistent certificate stored in Alias.keystore,33* and which has the incorrect encoding. the alias is 'duke',34* and the DN is: "emailaddress=duke@sun". the cert was generated35* by a 1.4 JDK, so it has the wrong encoding for "duke@sun"36* (UTF-8 string instead of IA5String, i believe).37*38* administrators would have placed an incorrectly encoded DN entry39* like this in their policies. the fix for the above bug40* would have broken their policy because the incorrect41* encoding would be compared to a properly encoded DN from42* the current call thread. if you run this test without43* a fix for the compatibility issue, the debug output will44* show the differences in the encodings.45*46* so in addition to fixing the encoding,47* the policy implementation was updated to read the48* incorrectly encoded DN strings, generate new X500Principals,49* and dump out new DN strings that had the correct encoding.50* thus access control checks would no longer fail.51*52* @run main/othervm/policy=EmailAddress.policy -Djava.security.debug=policy EmailAddress53*/5455import java.security.*;56import java.util.*;5758public class EmailAddress {5960public static void main(String[] args) {6162Principal[] principals = new Principal[1];63principals[0] = new javax.security.auth.x500.X500Principal64("emailaddress=duke@sun");6566java.net.URL url = null;67try {68url = new java.net.URL("http://emailaddress");69} catch (java.net.MalformedURLException mue) {70System.out.println("test 1 failed");71throw new SecurityException(mue.getMessage());72}73CodeSource cs =74new CodeSource(url, (java.security.cert.Certificate[]) null);7576ProtectionDomain pd = new ProtectionDomain77(cs,78null,79null,80principals);8182PermissionCollection perms = Policy.getPolicy().getPermissions(pd);8384if (perms.implies(new SecurityPermission("EMAILADDRESS"))) {85System.out.println("test succeeded");86} else {87System.out.println("test 2 failed");88throw new SecurityException("test failed");89}90}91}929394