J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 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. Sun designates this |
| 7 | * particular file as subject to the "Classpath" exception as provided |
| 8 | * by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 21 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 22 | * have any 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.FileOutputStream; |
| 35 | import java.io.OutputStream; |
| 36 | import sun.security.krb5.internal.util.KrbDataOutputStream; |
| 37 | import sun.security.krb5.*; |
| 38 | import sun.security.krb5.internal.*; |
| 39 | |
| 40 | /** |
| 41 | * This class implements a buffered output stream. It provides functions to write FCC-format data to a disk file. |
| 42 | * |
| 43 | * @author Yanni Zhang |
| 44 | * |
| 45 | */ |
| 46 | public class CCacheOutputStream extends KrbDataOutputStream implements FileCCacheConstants { |
| 47 | public CCacheOutputStream(OutputStream os) { |
| 48 | super(os); |
| 49 | } |
| 50 | |
| 51 | public void writeHeader(PrincipalName p, int version) throws IOException { |
| 52 | write((version & 0xff00) >> 8); |
| 53 | write(version & 0x00ff); |
| 54 | p.writePrincipal(this); |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Writes a credentials in FCC format to this cache output stream. |
| 59 | * |
| 60 | * @param creds the credentials to be written to the output stream. |
| 61 | * @exception IOException if an I/O exception occurs. |
| 62 | * @exception Asn1Exception if an Asn1Exception occurs. |
| 63 | */ |
| 64 | /*For object data fields which themselves have multiple data fields, such as PrincipalName, EncryptionKey |
| 65 | HostAddresses, AuthorizationData, I created corresponding write methods (writePrincipal, |
| 66 | writeKey,...) in each class, since converting the object into FCC format data stream |
| 67 | should be encapsulated in object itself. |
| 68 | */ |
| 69 | public void addCreds(Credentials creds) throws IOException, Asn1Exception { |
| 70 | creds.cname.writePrincipal(this); |
| 71 | creds.sname.writePrincipal(this); |
| 72 | creds.key.writeKey(this); |
| 73 | write32((int)(creds.authtime.getTime()/1000)); |
| 74 | if (creds.starttime != null) |
| 75 | write32((int)(creds.starttime.getTime()/1000)); |
| 76 | else write32(0); |
| 77 | write32((int)(creds.endtime.getTime()/1000)); |
| 78 | if (creds.renewTill != null) |
| 79 | write32((int)(creds.renewTill.getTime()/1000)); |
| 80 | |
| 81 | else write32(0); |
| 82 | if (creds.isEncInSKey) { |
| 83 | write8(1); |
| 84 | } |
| 85 | else write8(0); |
| 86 | writeFlags(creds.flags); |
| 87 | if (creds.caddr == null) |
| 88 | write32(0); |
| 89 | else |
| 90 | creds.caddr.writeAddrs(this); |
| 91 | |
| 92 | if (creds.authorizationData == null) { |
| 93 | write32(0); |
| 94 | } |
| 95 | else |
| 96 | creds.authorizationData.writeAuth(this); |
| 97 | writeTicket(creds.ticket); |
| 98 | writeTicket(creds.secondTicket); |
| 99 | } |
| 100 | |
| 101 | void writeTicket(Ticket t) throws IOException, Asn1Exception { |
| 102 | if (t == null) { |
| 103 | write32(0); |
| 104 | } |
| 105 | else { |
| 106 | byte[] bytes = t.asn1Encode(); |
| 107 | write32(bytes.length); |
| 108 | write(bytes, 0, bytes.length); |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | void writeFlags(TicketFlags flags) throws IOException { |
| 113 | int tFlags = 0; |
| 114 | boolean[] f = flags.toBooleanArray(); |
| 115 | if (f[1] == true) { |
| 116 | tFlags |= TKT_FLG_FORWARDABLE; |
| 117 | } |
| 118 | if (f[2] == true) { |
| 119 | tFlags |= TKT_FLG_FORWARDED; |
| 120 | } |
| 121 | if (f[3] == true) { |
| 122 | tFlags |= TKT_FLG_PROXIABLE; |
| 123 | } |
| 124 | if (f[4] == true) { |
| 125 | tFlags |= TKT_FLG_PROXY; |
| 126 | } |
| 127 | if (f[5] == true) { |
| 128 | tFlags |= TKT_FLG_MAY_POSTDATE; |
| 129 | } |
| 130 | if (f[6] == true) { |
| 131 | tFlags |= TKT_FLG_POSTDATED; |
| 132 | } |
| 133 | if (f[7] == true) { |
| 134 | tFlags |= TKT_FLG_INVALID; |
| 135 | } |
| 136 | if (f[8] == true) { |
| 137 | tFlags |= TKT_FLG_RENEWABLE; |
| 138 | } |
| 139 | if (f[9] == true) { |
| 140 | tFlags |= TKT_FLG_INITIAL; |
| 141 | } |
| 142 | if (f[10] == true) { |
| 143 | tFlags |= TKT_FLG_PRE_AUTH; |
| 144 | } |
| 145 | if (f[11] == true) { |
| 146 | tFlags |= TKT_FLG_HW_AUTH; |
| 147 | } |
| 148 | write32(tFlags); |
| 149 | |
| 150 | } |
| 151 | } |