1 /* ClientKeyExchange.java -- SSL ClientKeyExchange message.
2 Copyright (C) 2006 Free Software Foundation, Inc.
4 This file is a part of GNU Classpath.
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.
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.
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
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
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. */
39 package gnu
.javax
.net
.ssl
.provider
;
41 import java
.io
.BufferedReader
;
42 import java
.io
.DataInputStream
;
43 import java
.io
.InputStream
;
44 import java
.io
.IOException
;
45 import java
.io
.OutputStream
;
46 import java
.io
.PrintWriter
;
47 import java
.io
.StringReader
;
48 import java
.io
.StringWriter
;
50 import java
.math
.BigInteger
;
52 import java
.security
.PublicKey
;
53 import java
.security
.interfaces
.RSAKey
;
54 import javax
.crypto
.interfaces
.DHPublicKey
;
56 final class ClientKeyExchange
implements Handshake
.Body
60 // -------------------------------------------------------------------------
62 private final Object exObject
;
65 // -------------------------------------------------------------------------
67 ClientKeyExchange(byte[] encryptedSecret
)
69 exObject
= encryptedSecret
;
72 ClientKeyExchange(BigInteger bigint
)
78 // -------------------------------------------------------------------------
80 static ClientKeyExchange
read(InputStream in
, CipherSuite suite
,
84 DataInputStream din
= new DataInputStream(in
);
85 if (suite
.getKeyExchange().equals("RSA"))
88 if (suite
.getVersion() == ProtocolVersion
.SSL_3
)
90 len
= (((RSAKey
) key
).getModulus().bitLength()+7) / 8;
94 len
= din
.readUnsignedShort();
96 byte[] buf
= new byte[len
];
98 return new ClientKeyExchange(buf
);
100 else if (suite
.getKeyExchange().equals("SRP"))
102 byte[] buf
= new byte[din
.readUnsignedShort()];
104 return new ClientKeyExchange(new BigInteger(1, buf
));
106 else if (key
== null || !(key
instanceof DHPublicKey
)) // explicit.
108 byte[] buf
= new byte[din
.readUnsignedShort()];
110 return new ClientKeyExchange(new BigInteger(1, buf
));
114 return new ClientKeyExchange(new byte[0]);
119 // -------------------------------------------------------------------------
121 public void write(OutputStream out
) throws IOException
123 throw new UnsupportedOperationException("use write(java.io.OutputStream,ProtocolVersion) instead");
126 public void write(OutputStream out
, ProtocolVersion version
) throws IOException
128 if (exObject
instanceof byte[])
130 byte[] b
= (byte[]) exObject
;
133 if (version
!= ProtocolVersion
.SSL_3
)
135 out
.write(b
.length
>>> 8 & 0xFF);
136 out
.write(b
.length
& 0xFF);
143 byte[] bigint
= ((BigInteger
) exObject
).toByteArray();
144 if (bigint
[0] == 0x00)
146 out
.write(bigint
.length
- 1 >>> 8 & 0xFF);
147 out
.write(bigint
.length
- 1 & 0xFF);
148 out
.write(bigint
, 1, bigint
.length
- 1);
152 out
.write(bigint
.length
>>> 8 & 0xFF);
153 out
.write(bigint
.length
& 0xFF);
159 Object
getExchangeObject()
164 public String
toString()
166 StringWriter str
= new StringWriter();
167 PrintWriter out
= new PrintWriter(str
);
168 out
.println("struct {");
169 if (exObject
instanceof byte[] && ((byte[]) exObject
).length
> 0)
171 out
.println(" encryptedPreMasterSecret =");
172 out
.print(Util
.hexDump((byte[]) exObject
, " "));
174 else if (exObject
instanceof BigInteger
)
176 out
.println(" clientPublic = " + ((BigInteger
) exObject
).toString(16) + ";");
178 out
.println("} ClientKeyExchange;");
179 return str
.toString();