1 |
769 |
jeremybenn |
/* Entry.java --
|
2 |
|
|
Copyright (C) 2003, 2006 Free Software Foundation, Inc.
|
3 |
|
|
|
4 |
|
|
This file is a part of GNU Classpath.
|
5 |
|
|
|
6 |
|
|
GNU Classpath is free software; you can redistribute it and/or modify
|
7 |
|
|
it under the terms of the GNU General Public License as published by
|
8 |
|
|
the Free Software Foundation; either version 2 of the License, or (at
|
9 |
|
|
your option) any later version.
|
10 |
|
|
|
11 |
|
|
GNU Classpath is distributed in the hope that it will be useful, but
|
12 |
|
|
WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
14 |
|
|
General Public License for more details.
|
15 |
|
|
|
16 |
|
|
You should have received a copy of the GNU General Public License
|
17 |
|
|
along with GNU Classpath; if not, write to the Free Software
|
18 |
|
|
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
|
19 |
|
|
USA
|
20 |
|
|
|
21 |
|
|
Linking this library statically or dynamically with other modules is
|
22 |
|
|
making a combined work based on this library. Thus, the terms and
|
23 |
|
|
conditions of the GNU General Public License cover the whole
|
24 |
|
|
combination.
|
25 |
|
|
|
26 |
|
|
As a special exception, the copyright holders of this library give you
|
27 |
|
|
permission to link this library with independent modules to produce an
|
28 |
|
|
executable, regardless of the license terms of these independent
|
29 |
|
|
modules, and to copy and distribute the resulting executable under
|
30 |
|
|
terms of your choice, provided that you also meet, for each linked
|
31 |
|
|
independent module, the terms and conditions of the license of that
|
32 |
|
|
module. An independent module is a module which is not derived from
|
33 |
|
|
or based on this library. If you modify this library, you may extend
|
34 |
|
|
this exception to your version of the library, but you are not
|
35 |
|
|
obligated to do so. If you do not wish to do so, delete this
|
36 |
|
|
exception statement from your version. */
|
37 |
|
|
|
38 |
|
|
|
39 |
|
|
package gnu.javax.crypto.keyring;
|
40 |
|
|
|
41 |
|
|
import gnu.java.security.Configuration;
|
42 |
|
|
|
43 |
|
|
import java.io.DataInputStream;
|
44 |
|
|
import java.io.DataOutputStream;
|
45 |
|
|
import java.io.IOException;
|
46 |
|
|
import java.util.logging.Logger;
|
47 |
|
|
|
48 |
|
|
/**
|
49 |
|
|
* An immutable class representing a single entry in a keyring.
|
50 |
|
|
*/
|
51 |
|
|
public abstract class Entry
|
52 |
|
|
{
|
53 |
|
|
private static final Logger log = Logger.getLogger(Entry.class.getName());
|
54 |
|
|
private static final String[] TYPES = new String[] {
|
55 |
|
|
"Encrypted",
|
56 |
|
|
"PasswordEncrypted",
|
57 |
|
|
"Authenticated",
|
58 |
|
|
"PasswordAuthenticated",
|
59 |
|
|
"Compressed",
|
60 |
|
|
"Certificate",
|
61 |
|
|
"PublicKey",
|
62 |
|
|
"PrivateKey",
|
63 |
|
|
"CertPath",
|
64 |
|
|
"BinaryData" };
|
65 |
|
|
/** This entry's type identifier. */
|
66 |
|
|
protected int type;
|
67 |
|
|
/** This entry's property set. */
|
68 |
|
|
protected Properties properties;
|
69 |
|
|
/** This entry's payload. */
|
70 |
|
|
protected byte[] payload;
|
71 |
|
|
|
72 |
|
|
/**
|
73 |
|
|
* Creates a new Entry.
|
74 |
|
|
*
|
75 |
|
|
* @param type This entry's type.
|
76 |
|
|
* @param properties This entry's properties.
|
77 |
|
|
* @throws IllegalArgumentException If the properties argument is null, or if
|
78 |
|
|
* the type is out of range.
|
79 |
|
|
*/
|
80 |
|
|
protected Entry(int type, Properties properties)
|
81 |
|
|
{
|
82 |
|
|
if (type < 0 || type > 255)
|
83 |
|
|
throw new IllegalArgumentException("invalid packet type");
|
84 |
|
|
if (properties == null)
|
85 |
|
|
throw new IllegalArgumentException("no properties");
|
86 |
|
|
this.type = type;
|
87 |
|
|
this.properties = (Properties) properties.clone();
|
88 |
|
|
}
|
89 |
|
|
|
90 |
|
|
/**
|
91 |
|
|
* Constructor for use by subclasses.
|
92 |
|
|
*/
|
93 |
|
|
protected Entry(final int type)
|
94 |
|
|
{
|
95 |
|
|
if (type < 0 || type > 255)
|
96 |
|
|
throw new IllegalArgumentException("invalid packet type");
|
97 |
|
|
this.type = type;
|
98 |
|
|
properties = new Properties();
|
99 |
|
|
}
|
100 |
|
|
|
101 |
|
|
/**
|
102 |
|
|
* Returns this entry's properties object. The properties are cloned before
|
103 |
|
|
* being returned.
|
104 |
|
|
*
|
105 |
|
|
* @return The properties.
|
106 |
|
|
*/
|
107 |
|
|
public Properties getProperties()
|
108 |
|
|
{
|
109 |
|
|
return (Properties) properties.clone();
|
110 |
|
|
}
|
111 |
|
|
|
112 |
|
|
/**
|
113 |
|
|
* Returns this entry's payload data, or null if
|
114 |
|
|
*/
|
115 |
|
|
public byte[] getPayload()
|
116 |
|
|
{
|
117 |
|
|
if (payload == null)
|
118 |
|
|
return null;
|
119 |
|
|
return (byte[]) payload.clone();
|
120 |
|
|
}
|
121 |
|
|
|
122 |
|
|
/**
|
123 |
|
|
* This method is called when this entry needs to be written to an output
|
124 |
|
|
* stream.
|
125 |
|
|
*
|
126 |
|
|
* @param out The stream to write to.
|
127 |
|
|
* @throws IOException If an I/O exception occurs.
|
128 |
|
|
*/
|
129 |
|
|
public void encode(DataOutputStream out) throws IOException
|
130 |
|
|
{
|
131 |
|
|
if (payload == null)
|
132 |
|
|
encodePayload();
|
133 |
|
|
if (out == null)
|
134 |
|
|
return;
|
135 |
|
|
out.write(type);
|
136 |
|
|
properties.encode(out);
|
137 |
|
|
out.writeInt(payload.length);
|
138 |
|
|
out.write(payload);
|
139 |
|
|
}
|
140 |
|
|
|
141 |
|
|
public String toString()
|
142 |
|
|
{
|
143 |
|
|
return new StringBuilder("Entry{")
|
144 |
|
|
.append("type=").append(TYPES[type])
|
145 |
|
|
.append(", properties=").append(properties)
|
146 |
|
|
.append(", payload=")
|
147 |
|
|
.append(payload == null ? "-" : "byte[" + payload.length + "]")
|
148 |
|
|
.append( "}")
|
149 |
|
|
.toString();
|
150 |
|
|
}
|
151 |
|
|
|
152 |
|
|
/**
|
153 |
|
|
* Generic decoding method, which simply decodes the properties field
|
154 |
|
|
* and reads the payload field.
|
155 |
|
|
*
|
156 |
|
|
* @param in The input data stream.
|
157 |
|
|
* @throws IOException If an I/O error occurs.
|
158 |
|
|
*/
|
159 |
|
|
protected void defaultDecode(DataInputStream in) throws IOException
|
160 |
|
|
{
|
161 |
|
|
properties = new Properties();
|
162 |
|
|
properties.decode(in);
|
163 |
|
|
int len = in.readInt();
|
164 |
|
|
if (len < 0)
|
165 |
|
|
throw new IOException("corrupt length");
|
166 |
|
|
if (Configuration.DEBUG)
|
167 |
|
|
log.fine("About to instantiate new payload byte array for " + this);
|
168 |
|
|
payload = new byte[len];
|
169 |
|
|
in.readFully(payload);
|
170 |
|
|
}
|
171 |
|
|
|
172 |
|
|
/**
|
173 |
|
|
* This method is called of subclasses when the payload data needs to be
|
174 |
|
|
* created.
|
175 |
|
|
*
|
176 |
|
|
* @throws IOException If an encoding error occurs.
|
177 |
|
|
*/
|
178 |
|
|
protected abstract void encodePayload() throws IOException;
|
179 |
|
|
}
|