Source code: gnu/javax/crypto/key/OutgoingMessage.java
1 /* OutgoingMessage.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.key;
40
41 import gnu.java.security.Registry;
42 import gnu.java.security.key.dss.DSSKey;
43 import gnu.java.security.key.rsa.GnuRSAKey;
44 import gnu.java.security.util.FormatUtil;
45 import gnu.javax.crypto.key.dh.GnuDHKey;
46 import gnu.javax.crypto.key.srp6.SRPKey;
47
48 import java.io.ByteArrayOutputStream;
49 import java.io.UnsupportedEncodingException;
50 import java.security.Key;
51 import java.security.PrivateKey;
52 import java.security.PublicKey;
53 import java.math.BigInteger;
54
55 /**
56 * <p>An implementation of outgoing messages for use with key agreement
57 * protocols.</p>
58 */
59 public class OutgoingMessage
60 {
61
62 // Constants and variables
63 // -------------------------------------------------------------------------
64
65 /** The internal output stream. */
66 private ByteArrayOutputStream out;
67
68 // Constructor(s)
69 // -------------------------------------------------------------------------
70
71 public OutgoingMessage()
72 {
73 super();
74
75 out = new ByteArrayOutputStream();
76 }
77
78 // Class methods
79 // -------------------------------------------------------------------------
80
81 // Instance methods
82 // -------------------------------------------------------------------------
83
84 /**
85 * <p>Returns the encoded form of the current message including the 4-byte
86 * length header.</p>
87 *
88 * @throws KeyAgreementException if an encoding size constraint is violated.
89 */
90 public byte[] toByteArray() throws KeyAgreementException
91 {
92 byte[] buffer = wrap();
93 int length = buffer.length;
94 byte[] result = new byte[length + 4];
95 result[0] = (byte) (length >>> 24);
96 result[1] = (byte) (length >>> 16);
97 result[2] = (byte) (length >>> 8);
98 result[3] = (byte) length;
99 System.arraycopy(buffer, 0, result, 4, length);
100
101 return result;
102 }
103
104 /**
105 * <p>Returns the encoded form of the current message excluding the 4-byte
106 * length header.</p>
107 *
108 * @throws KeyAgreementException if an encoding size constraint is violated.
109 */
110 public byte[] wrap() throws KeyAgreementException
111 {
112 int length = out.size();
113 if (length > Registry.SASL_BUFFER_MAX_LIMIT || length < 0)
114 {
115 throw new KeyAgreementException("message content is too long");
116 }
117 return out.toByteArray();
118 }
119
120 /**
121 * Encodes a public key into the message.
122 * <p>
123 * When a public key is encoded into an outgoing message, the byte array of
124 * the encoded key --according to its encoding/decoding format specified when
125 * the key was first instantiated-- are put in the message (a) preceeded by
126 * one byte representing both the type of key (upper 4-bit) and the identifier
127 * of the format used (lower 4-bit), and (b) preceeed by a 4-byte entity
128 * representing the total length, excluding these 4 bytes, of the bytes
129 * representing the encoded key and the one-byte representing the key-type and
130 * format; i.e.
131 *
132 * <pre>
133 * key --> 4-byte-length || 1-byte-type-and-format || encoded-key-bytes
134 * </pre>
135 *
136 * @param k the public key to encode.
137 * @throws KeyAgreementException if an encoding size constraint is violated.
138 */
139 public void writePublicKey(PublicKey k) throws KeyAgreementException
140 {
141 writeKey(k);
142 }
143
144 /**
145 * Encodes a private key into the message.
146 * <p>
147 * When a private key is encoded into an outgoing message, the byte array of
148 * the encoded key --according to its encoding/decoding format specified when
149 * the key was first instantiated-- are put in the message (a) preceeded by
150 * one byte representing both the type of key (upper 4-bit) and the identifier
151 * of the format used (lower 4-bit), and (b) preceeed by a 4-byte entity
152 * representing the total length, excluding these 4 bytes, of the bytes
153 * representing the encoded key and the one-byte representing the key-type and
154 * format; i.e.
155 *
156 * <pre>
157 * key --> 4-byte-length || 1-byte-type-and-format || encoded-key-bytes
158 * </pre>
159 *
160 * @param k the private key to encode.
161 * @throws KeyAgreementException if an encoding size constraint is violated.
162 */
163 public void writePrivateKey(PrivateKey k) throws KeyAgreementException
164 {
165 writeKey(k);
166 }
167
168 /**
169 * <p>Encodes an MPI into the message.</p>
170 *
171 * @param val the MPI to encode.
172 * @throws KeyAgreementException if an encoding size constraint is violated.
173 */
174 public void writeMPI(BigInteger val) throws KeyAgreementException
175 {
176 byte[] b = val.toByteArray();
177 int length = b.length;
178 if (length > Registry.SASL_TWO_BYTE_MAX_LIMIT)
179 {
180 throw new KeyAgreementException("MPI is too long");
181 }
182 byte[] lengthBytes = { (byte) (length >>> 8), (byte) length };
183 out.write(lengthBytes, 0, 2);
184 out.write(b, 0, b.length);
185 }
186
187 /**
188 * <p>Encodes a string into the message.</p>
189 *
190 * @param s the string to encode.
191 * @throws KeyAgreementException if the UTF8 encoding is not supported on
192 * this platform, or if an encoding size constraint is violated.
193 */
194 public void writeString(String s) throws KeyAgreementException
195 {
196 byte[] b = null;
197 try
198 {
199 b = s.getBytes("UTF8");
200 }
201 catch (UnsupportedEncodingException x)
202 {
203 throw new KeyAgreementException("unxupported UTF8 encoding", x);
204 }
205 int length = b.length;
206 if (length > Registry.SASL_TWO_BYTE_MAX_LIMIT)
207 {
208 throw new KeyAgreementException("text too long");
209 }
210 byte[] lengthBytes = { (byte) (length >>> 8), (byte) length };
211 out.write(lengthBytes, 0, 2);
212 out.write(b, 0, b.length);
213 }
214
215 /**
216 * @param k the key to encode.
217 * @throws KeyAgreementException if an encoding size constraint is violated.
218 */
219 private void writeKey(Key k) throws KeyAgreementException
220 {
221 byte[] b = k.getEncoded();
222 int keyType = getKeyType(k);
223 int formatID = FormatUtil.getFormatID(k.getFormat());
224 int length = b.length + 1;
225 if (length > Registry.SASL_FOUR_BYTE_MAX_LIMIT)
226 throw new KeyAgreementException("Encoded key is too long");
227
228 byte[] lengthBytes = { (byte) (length >>> 24), (byte) (length >>> 16),
229 (byte) (length >>> 8), (byte) length };
230 out.write(lengthBytes, 0, 4);
231 out.write(((keyType & 0x0F) << 4) | (formatID & 0x0F));
232 out.write(b, 0, b.length);
233 }
234
235 /**
236 * @param k the key to find an identifier for.
237 * @return an integer from <code>0</code> to <code>3</code> identifying
238 * the type of key.
239 * @throws KeyAgreementException if the designated key is of unknown or
240 * unsupported type.
241 */
242 private int getKeyType(Key k) throws KeyAgreementException
243 {
244 if (k instanceof DSSKey)
245 return 0;
246 if (k instanceof GnuRSAKey)
247 return 1;
248 if (k instanceof GnuDHKey)
249 return 2;
250 if (k instanceof SRPKey)
251 return 3;
252 throw new KeyAgreementException("Unknown or unsupported key type: "
253 + k.getClass().getName());
254 }
255 }