blob: f532485fc6beb4ec5e19d495fae73bd2bb8c03fc [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
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
31package sun.security.krb5.internal.ktab;
32
33import sun.security.krb5.internal.*;
34import sun.security.krb5.internal.util.KrbDataOutputStream;
35import java.io.IOException;
36import java.io.FileOutputStream;
37import java.io.OutputStream;
38import java.io.UnsupportedEncodingException;
39
40/**
41 * This class implements a buffered input stream. It is used for parsing key table
42 * data to memory.
43 *
44 * @author Yanni Zhang
45 *
46 */
47public class KeyTabOutputStream extends KrbDataOutputStream implements KeyTabConstants {
48 private KeyTabEntry entry;
49 private int keyType;
50 private byte[] keyValue;
51 public int version;
52
53 public KeyTabOutputStream(OutputStream os) {
54 super(os);
55 }
56
57 public void writeVersion(int num) throws IOException {
58 version = num;
59 write16(num); //we use the standard version.
60 }
61
62 public void writeEntry(KeyTabEntry entry) throws IOException {
63 write32(entry.entryLength());
64 String[] serviceNames = entry.service.getNameStrings();
65 int comp_num = serviceNames.length;
66 if (version == KRB5_KT_VNO_1) {
67 write16(comp_num + 1);
68 }
69 else write16(comp_num);
70
71 byte[] realm = null;
72 try {
73 realm = entry.service.getRealmString().getBytes("8859_1");
74 } catch (UnsupportedEncodingException exc) {
75 }
76
77 write16(realm.length);
78 write(realm);
79 for (int i = 0; i < comp_num; i++) {
80 try {
81 write16(serviceNames[i].getBytes("8859_1").length);
82 write(serviceNames[i].getBytes("8859_1"));
83 } catch (UnsupportedEncodingException exc) {
84 }
85 }
86 write32(entry.service.getNameType());
87 //time is long, but we only use 4 bytes to store the data.
88 write32((int)(entry.timestamp.getTime()/1000));
89
90 // the key version might be a 32 bit extended number.
91 write8(entry.keyVersion % 256 );
92 write16(entry.keyType);
93 write16(entry.keyblock.length);
94 write(entry.keyblock);
95
96 // if the key version isn't smaller than 256, it could be saved as
97 // extension key version number in 4 bytes. The nonzero extension
98 // key version number will be trusted. However, it isn't standardized
99 // yet, we won't support it.
100 // if (entry.keyVersion >= 256) {
101 // write32(entry.keyVersion);
102 //}
103 }
104}