~ubuntu-branches/ubuntu/precise/classpath/precise

« back to all changes in this revision

Viewing changes to gnu/javax/net/ssl/provider/ClientKeyExchange.java

  • Committer: Bazaar Package Importer
  • Author(s): Michael Koch
  • Date: 2006-05-27 16:11:15 UTC
  • mfrom: (1.1.3 upstream)
  • Revision ID: james.westby@ubuntu.com-20060527161115-h6e39eposdt5snb6
Tags: 2:0.91-3
* Install header files to /usr/include/classpath.
* debian/control: classpath: Conflict with jamvm < 1.4.3 and
  cacao < 0.96 (Closes: #368172).

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* ClientKeyExchange.java -- SSL ClientKeyExchange message.
 
2
   Copyright (C) 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.net.ssl.provider;
 
40
 
 
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;
 
49
 
 
50
import java.math.BigInteger;
 
51
 
 
52
import java.security.PublicKey;
 
53
import java.security.interfaces.RSAKey;
 
54
import javax.crypto.interfaces.DHPublicKey;
 
55
 
 
56
final class ClientKeyExchange implements Handshake.Body
 
57
{
 
58
 
 
59
  // Fields.
 
60
  // -------------------------------------------------------------------------
 
61
 
 
62
  private final Object exObject;
 
63
 
 
64
  // Constructors.
 
65
  // -------------------------------------------------------------------------
 
66
 
 
67
  ClientKeyExchange(byte[] encryptedSecret)
 
68
  {
 
69
    exObject = encryptedSecret;
 
70
  }
 
71
 
 
72
  ClientKeyExchange(BigInteger bigint)
 
73
  {
 
74
    exObject = bigint;
 
75
  }
 
76
 
 
77
  // Class method.
 
78
  // -------------------------------------------------------------------------
 
79
 
 
80
  static ClientKeyExchange read(InputStream in, CipherSuite suite,
 
81
                                PublicKey key)
 
82
    throws IOException
 
83
  {
 
84
    DataInputStream din = new DataInputStream(in);
 
85
    if (suite.getKeyExchange().equals("RSA"))
 
86
      {
 
87
        int len = 0;
 
88
        if (suite.getVersion() == ProtocolVersion.SSL_3)
 
89
          {
 
90
            len = (((RSAKey) key).getModulus().bitLength()+7) / 8;
 
91
          }
 
92
        else
 
93
          {
 
94
            len = din.readUnsignedShort();
 
95
          }
 
96
        byte[] buf = new byte[len];
 
97
        din.readFully(buf);
 
98
        return new ClientKeyExchange(buf);
 
99
      }
 
100
    else if (suite.getKeyExchange().equals("SRP"))
 
101
      {
 
102
        byte[] buf = new byte[din.readUnsignedShort()];
 
103
        din.readFully(buf);
 
104
        return new ClientKeyExchange(new BigInteger(1, buf));
 
105
      }
 
106
    else if (key == null || !(key instanceof DHPublicKey))  // explicit.
 
107
      {
 
108
        byte[] buf = new byte[din.readUnsignedShort()];
 
109
        din.readFully(buf);
 
110
        return new ClientKeyExchange(new BigInteger(1, buf));
 
111
      }
 
112
    else
 
113
      {
 
114
        return new ClientKeyExchange(new byte[0]);
 
115
      }
 
116
  }
 
117
 
 
118
  // Instance methods.
 
119
  // -------------------------------------------------------------------------
 
120
 
 
121
  public void write(OutputStream out) throws IOException
 
122
  {
 
123
    throw new UnsupportedOperationException("use write(java.io.OutputStream,ProtocolVersion) instead");
 
124
  }
 
125
 
 
126
  public void write(OutputStream out, ProtocolVersion version) throws IOException
 
127
  {
 
128
    if (exObject instanceof byte[])
 
129
      {
 
130
        byte[] b = (byte[]) exObject;
 
131
        if (b.length > 0)
 
132
          {
 
133
            if (version != ProtocolVersion.SSL_3)
 
134
              {
 
135
                out.write(b.length >>> 8 & 0xFF);
 
136
                out.write(b.length & 0xFF);
 
137
              }
 
138
            out.write(b);
 
139
          }
 
140
      }
 
141
    else
 
142
      {
 
143
        byte[] bigint = ((BigInteger) exObject).toByteArray();
 
144
        if (bigint[0] == 0x00)
 
145
          {
 
146
            out.write(bigint.length - 1 >>> 8 & 0xFF);
 
147
            out.write(bigint.length - 1 & 0xFF);
 
148
            out.write(bigint, 1, bigint.length - 1);
 
149
          }
 
150
        else
 
151
          {
 
152
            out.write(bigint.length >>> 8 & 0xFF);
 
153
            out.write(bigint.length & 0xFF);
 
154
            out.write(bigint);
 
155
          }
 
156
      }
 
157
  }
 
158
 
 
159
  Object getExchangeObject()
 
160
  {
 
161
    return exObject;
 
162
  }
 
163
 
 
164
  public String toString()
 
165
  {
 
166
    StringWriter str = new StringWriter();
 
167
    PrintWriter out = new PrintWriter(str);
 
168
    out.println("struct {");
 
169
    if (exObject instanceof byte[] && ((byte[]) exObject).length > 0)
 
170
      {
 
171
        out.println("  encryptedPreMasterSecret =");
 
172
        out.print(Util.hexDump((byte[]) exObject, "    "));
 
173
      }
 
174
    else if (exObject instanceof BigInteger)
 
175
      {
 
176
        out.println("  clientPublic = " + ((BigInteger) exObject).toString(16) + ";");
 
177
      }
 
178
    out.println("} ClientKeyExchange;");
 
179
    return str.toString();
 
180
  }
 
181
}