1/* 2 * Copyright (c) 2014, 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 * An implementation of the Subject class that provides basic functionality 26 * for the construction of Subject objects with null Principal elements. 27 * This is a helper class for serialization tests tied to bug 8015081 28 * (see SubjectNullTests.java). 29 */ 30package jjjjj.security.auth; 31 32import javax.management.remote.JMXPrincipal; 33import javax.security.auth.kerberos.KerberosPrincipal; 34import javax.security.auth.x500.X500Principal; 35import java.io.ByteArrayOutputStream; 36import java.io.ObjectOutputStream; 37import java.io.ObjectStreamField; 38import java.lang.Exception; 39import java.lang.RuntimeException; 40import java.security.Principal; 41import java.util.AbstractSet; 42import java.util.Collections; 43import java.util.HashSet; 44import java.util.Iterator; 45import java.util.LinkedList; 46import java.util.Set; 47 48import java.io.FileOutputStream; 49 50public class Subject implements java.io.Serializable { 51 52 private static final long serialVersionUID = -8308522755600156056L; 53 54 Set<Principal> principals; 55 private volatile boolean readOnly = false; 56 private static final int PRINCIPAL_SET = 1; 57 58 public Subject(Set<? extends Principal> principals) { 59 this.principals = Collections.synchronizedSet(new SecureSet<Principal> 60 (this, PRINCIPAL_SET, principals)); 61 } 62 63 public Set<Principal> getPrincipals() { 64 return principals; 65 } 66 67 private static class SecureSet<E> 68 extends AbstractSet<E> 69 implements java.io.Serializable { 70 71 private static final long serialVersionUID = 7911754171111800359L; 72 private static final ObjectStreamField[] serialPersistentFields = { 73 new ObjectStreamField("this$0", Subject.class), 74 new ObjectStreamField("elements", LinkedList.class), 75 new ObjectStreamField("which", int.class) 76 }; 77 78 Subject subject; 79 LinkedList<E> elements; 80 private int which; 81 82 SecureSet(Subject subject, int which, Set<? extends E> set) { 83 this.subject = subject; 84 this.which = which; 85 this.elements = new LinkedList<E>(set); 86 } 87 88 public Iterator<E> iterator() { 89 return elements.iterator(); 90 } 91 92 public int size() { 93 return elements.size(); 94 } 95 96 private void writeObject(java.io.ObjectOutputStream oos) 97 throws java.io.IOException { 98 99 ObjectOutputStream.PutField fields = oos.putFields(); 100 fields.put("this$0", subject); 101 fields.put("elements", elements); 102 fields.put("which", which); 103 oos.writeFields(); 104 } 105 } 106 107 public static byte[] enc(Object obj) { 108 try { 109 ByteArrayOutputStream bout; 110 bout = new ByteArrayOutputStream(); 111 new ObjectOutputStream(bout).writeObject(obj); 112 byte[] data = bout.toByteArray(); 113 for (int i = 0; i < data.length - 5; i++) { 114 if (data[i] == 'j' && data[i + 1] == 'j' && data[i + 2] == 'j' 115 && data[i + 3] == 'j' && data[i + 4] == 'j') { 116 System.arraycopy("javax".getBytes(), 0, data, i, 5); 117 } 118 } 119 return data; 120 } catch (Exception e) { 121 throw new RuntimeException(e); 122 } 123 } 124} 125