1 /* 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 it 5 * under the terms of the GNU General Public License version 2 only, as 6 * published by the Free Software Foundation. Oracle designates this 7 * particular file as subject to the "Classpath" exception as provided 8 * by Oracle in the LICENSE file that accompanied this code. 9 * 10 * This code is distributed in the hope that it will be useful, but WITHOUT 11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 13 * version 2 for more details (a copy is included in the LICENSE file that 14 * accompanied this code). 15 * 16 * You should have received a copy of the GNU General Public License version 17 * 2 along with this work; if not, write to the Free Software Foundation, 18 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 19 * 20 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 21 * or visit www.oracle.com if you need additional information or have any 22 * questions. 23 */ 24 25 /* 26 * 27 * (C) Copyright IBM Corp. 1999 All Rights Reserved. 28 * Copyright 1997 The Open Group Research Institute. All rights reserved. 29 */ 30 31 package sun.security.krb5.internal.ccache; 32 33 import java.io.IOException; 34 import java.io.OutputStream; 35 import sun.security.krb5.internal.util.KrbDataOutputStream; 36 import sun.security.krb5.*; 37 import sun.security.krb5.internal.*; 38 39 /** 40 * This class implements a buffered output stream. It provides functions to write FCC-format data to a disk file. 41 * 42 * @author Yanni Zhang 43 * 44 */ 45 public class CCacheOutputStream extends KrbDataOutputStream implements FileCCacheConstants { CCacheOutputStream(OutputStream os)46 public CCacheOutputStream(OutputStream os) { 47 super(os); 48 } 49 writeHeader(PrincipalName p, int version)50 public void writeHeader(PrincipalName p, int version) throws IOException { 51 write((version & 0xff00) >> 8); 52 write(version & 0x00ff); 53 p.writePrincipal(this); 54 } 55 56 /** 57 * Writes a credentials in FCC format to this cache output stream. 58 * 59 * @param creds the credentials to be written to the output stream. 60 * @exception IOException if an I/O exception occurs. 61 * @exception Asn1Exception if an Asn1Exception occurs. 62 */ 63 /*For object data fields which themselves have multiple data fields, such as PrincipalName, EncryptionKey 64 HostAddresses, AuthorizationData, I created corresponding write methods (writePrincipal, 65 writeKey,...) in each class, since converting the object into FCC format data stream 66 should be encapsulated in object itself. 67 */ addCreds(Credentials creds)68 public void addCreds(Credentials creds) throws IOException, Asn1Exception { 69 creds.cname.writePrincipal(this); 70 creds.sname.writePrincipal(this); 71 creds.key.writeKey(this); 72 write32((int)(creds.authtime.getTime()/1000)); 73 if (creds.starttime != null) 74 write32((int)(creds.starttime.getTime()/1000)); 75 else write32(0); 76 write32((int)(creds.endtime.getTime()/1000)); 77 if (creds.renewTill != null) 78 write32((int)(creds.renewTill.getTime()/1000)); 79 80 else write32(0); 81 if (creds.isEncInSKey) { 82 write8(1); 83 } 84 else write8(0); 85 writeFlags(creds.flags); 86 if (creds.caddr == null) 87 write32(0); 88 else 89 creds.caddr.writeAddrs(this); 90 91 if (creds.authorizationData == null) { 92 write32(0); 93 } 94 else 95 creds.authorizationData.writeAuth(this); 96 writeTicket(creds.ticket); 97 writeTicket(creds.secondTicket); 98 } 99 addConfigEntry(PrincipalName cname, CredentialsCache.ConfigEntry e)100 public void addConfigEntry(PrincipalName cname, CredentialsCache.ConfigEntry e) 101 throws IOException { 102 cname.writePrincipal(this); 103 e.getSName().writePrincipal(this); 104 write16(0); write16(0); write32(0); 105 write32(0); write32(0); write32(0); write32(0); 106 write8(0); 107 write32(0); 108 write32(0); 109 write32(0); 110 write32(e.getData().length); 111 write(e.getData()); 112 write32(0); 113 } 114 writeTicket(Ticket t)115 void writeTicket(Ticket t) throws IOException, Asn1Exception { 116 if (t == null) { 117 write32(0); 118 } 119 else { 120 byte[] bytes = t.asn1Encode(); 121 write32(bytes.length); 122 write(bytes, 0, bytes.length); 123 } 124 } 125 writeFlags(TicketFlags flags)126 void writeFlags(TicketFlags flags) throws IOException { 127 int tFlags = 0; 128 boolean[] f = flags.toBooleanArray(); 129 if (f[1] == true) { 130 tFlags |= TKT_FLG_FORWARDABLE; 131 } 132 if (f[2] == true) { 133 tFlags |= TKT_FLG_FORWARDED; 134 } 135 if (f[3] == true) { 136 tFlags |= TKT_FLG_PROXIABLE; 137 } 138 if (f[4] == true) { 139 tFlags |= TKT_FLG_PROXY; 140 } 141 if (f[5] == true) { 142 tFlags |= TKT_FLG_MAY_POSTDATE; 143 } 144 if (f[6] == true) { 145 tFlags |= TKT_FLG_POSTDATED; 146 } 147 if (f[7] == true) { 148 tFlags |= TKT_FLG_INVALID; 149 } 150 if (f[8] == true) { 151 tFlags |= TKT_FLG_RENEWABLE; 152 } 153 if (f[9] == true) { 154 tFlags |= TKT_FLG_INITIAL; 155 } 156 if (f[10] == true) { 157 tFlags |= TKT_FLG_PRE_AUTH; 158 } 159 if (f[11] == true) { 160 tFlags |= TKT_FLG_HW_AUTH; 161 } 162 write32(tFlags); 163 164 } 165 } 166