A
download JCESecurityParameters.java
Language: Java
License: GPL
Copyright: (C) 2004 Casey Marshall
LOC: 227
Project Info
Jessie:A Free Implementation of the JSSE(jessie)
Server: Savannah NonGNU
Type: cvs
...metastatic\jessie\provider\
   Alert.java
   AlertException.java
   BitString.java
   Certificate.java
   CertificateRequest.java
   CertificateType.java
   CertificateVerify.java
   CipherSuite.java
   ClientHello.java
   ClientKeyExchange.java
   CompressionMethod.java
   Constructed.java
   ContentType.java
   Context.java
   CSPRNG.java
   CSPRNGSpi.java
   DER.java
   DEREncodingException.java
   DERReader.java
   DERValue.java
   DERWriter.java
   DiffieHellman.java
   DigestInputStream.java
   DigestOutputStream.java
   Enumerated.java
   Extension.java
   Extensions.java
   Extensions.java.in
   Finished.java
   GNUSecurityParameters.java
   Handshake.java
   JCESecurityParameters.java
   JDBCSessionContext.java
   Jessie.java
   Jessie.java.in
   JessieDHPrivateKey.java
   JessieDHPublicKey.java
   JessieRSAPrivateKey.java
   JessieRSAPublicKey.java
   KeyPool.java
   MacException.java
   OID.java
   OverflowException.java
   ProtocolVersion.java
   Random.java
   RecordingInputStream.java
   RecordInput.java
   RecordInputStream.java
   RecordOutputStream.java
   SecurityParameters.java
   ServerHello.java
   ServerKeyExchange.java
   Session.java
   SessionContext.java
   Signature.java
   SimpleList.java
   ...rustManagerFactory.java
   SSLHMac.java
   SSLRandom.java
   SSLRSASignature.java
   SSLServerSocket.java
   ...erverSocketFactory.java
   SSLSocket.java
   SSLSocket.java.in
   SSLSocketFactory.java
   SSLSocketInputStream.java
   SSLSocketOutputStream.java
   SynchronizedRandom.java
   TLSHMac.java
   TLSRandom.java
   Util.java
   X509KeyManagerFactory.java
   ...rustManagerFactory.java
   XMLSessionContext.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
/* JCESecurityParameters.java -- JCE-based security parameters.
   Copyright (C) 2004  Casey Marshall <csm@gnu.org>

This file is a part of Jessie.

Jessie is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
Free Software Foundation; either version 2 of the License, or (at your
option) any later version.

Jessie is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
for more details.

You should have received a copy of the GNU General Public License along
with Jessie; if not, write to the

   Free Software Foundation, Inc.,
   59 Temple Place, Suite 330,
   Boston, MA  02111-1307
   USA

Linking this library statically or dynamically with other modules is
making a combined work based on this library.  Thus, the terms and
conditions of the GNU General Public License cover the whole
combination.

As a special exception, the copyright holders of this library give you
permission to link this library with independent modules to produce an
executable, regardless of the license terms of these independent
modules, and to copy and distribute the resulting executable under terms
of your choice, provided that you also meet, for each linked independent
module, the terms and conditions of the license of that module.  An
independent module is a module which is not derived from or based on
this library.  If you modify this library, you may extend this exception
to your version of the library, but you are not obligated to do so.  If
you do not wish to do so, delete this exception statement from your
version.  */


package org.metastatic.jessie.provider;

import java.io.ByteArrayOutputStream;

import java.util.Arrays;
import java.util.zip.DataFormatException;
import java.util.zip.Deflater;
import java.util.zip.Inflater;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.Mac;

import javax.net.ssl.SSLException;

class JCESecurityParameters implements SecurityParameters
{

  // Fields.
  // -------------------------------------------------------------------------

  private Cipher inCipher, outCipher;
  private Mac inMac, outMac;
  private Inflater inflater;
  private Deflater deflater;
  private int fragmentLength;
  private long inSequence, outSequence;
  private ProtocolVersion version;

  // Constructors.
  // -------------------------------------------------------------------------

  JCESecurityParameters ()
  {
    fragmentLength = 16384;
    inSequence = 0L;
    outSequence = 0L;
  }

  // Instance methods.
  // -------------------------------------------------------------------------

  public void reset()
  {
    inCipher = null;
    outCipher = null;
    inMac = null;
    outMac = null;
    deflater = null;
    inflater = null;
  }

  public void setInCipher (Object inCipher)
  {
    this.inCipher = (Cipher) inCipher;
  }

  public void setOutCipher (Object outCipher)
  {
    this.outCipher = (Cipher) outCipher;
  }

  public void setInMac (Object inMac)
  {
    this.inMac = (Mac) inMac;
    inSequence = 0L;
  }

  public void setOutMac (Object outMac)
  {
    this.outMac = (Mac) outMac;
    outSequence = 0L;
  }

  public void setDeflating (boolean deflate)
  {
    if (deflate)
      {
        if (deflater == null)
          deflater = new Deflater();
      }
    else
      deflater = null;
  }

  public void setInflating (boolean inflate)
  {
    if (inflate)
      {
        if (inflater == null)
          inflater = new Inflater();
      }
    else
      inflater = null;
  }

  public int getFragmentLength()
  {
    return fragmentLength;
  }

  public void setFragmentLength (int fragmentLength)
  {
    this.fragmentLength = fragmentLength;
  }

  public ProtocolVersion getVersion()
  {
    return version;
  }

  public void setVersion (ProtocolVersion version)
  {
    this.version = version;
  }

  public synchronized byte[] decrypt (byte[] fragment, ProtocolVersion version,
                                      ContentType type)
    throws MacException, OverflowException, SSLException
  {
    boolean badpad = false;
    if (inCipher != null)
      {
        // We imagine that the JCE would be used in cases where hardware
        // acceleration is available, since it isn't really that useful for
        // pure Java crypto. We decrypt (and encrypt, below) in one go
        // to minimize (potential) calls to native methods.
        try
          {
            fragment = inCipher.doFinal (fragment);
          }
        catch (BadPaddingException bpe)
          {
            badpad = true;
          }
        catch (IllegalBlockSizeException ibse)
          {
            badpad = true;
          }
      }

    if (inMac != null)
      {
        int macLen = inMac.getMacLength();
        int fragLen = fragment.length - macLen;
        byte[] mac = Util.trim (fragment, fragLen, macLen);
        fragment = Util.trim (fragment, fragLen);
        inMac.update ((byte) (inSequence >>> 56));
        inMac.update ((byte) (inSequence >>> 48));
        inMac.update ((byte) (inSequence >>> 40));
        inMac.update ((byte) (inSequence >>> 32));
        inMac.update ((byte) (inSequence >>> 24));
        inMac.update ((byte) (inSequence >>> 16));
        inMac.update ((byte) (inSequence >>>  8));
        inMac.update ((byte)  inSequence);
        inMac.update ((byte) type.getValue());
        if (version != ProtocolVersion.SSL_3)
          {
            inMac.update ((byte) version.getMajor());
            inMac.update ((byte) version.getMinor());
          }
        inMac.update ((byte) (fragLen >>> 8));
        inMac.update ((byte)  fragLen);
        inMac.update (fragment);
        if (!Arrays.equals (mac, inMac.doFinal()) || badpad)
          throw new MacException();
      }

    if (inflater != null)
      {
        byte[] buf = new byte[1024];
        ByteArrayOutputStream bout = new ByteArrayOutputStream (fragment.length << 1);
        inflater.setInput (fragment);
        int len;
        try
          {
            while ((len = inflater.inflate (buf)) > 0)
              {
                bout.write (buf, 0, len);
                if (bout.size() > fragmentLength + 1024)
                  throw new OverflowException ("inflated data too large");
              }
          }
        catch (DataFormatException dfe)
          {
            throw new SSLException (String.valueOf (dfe));
          }
        fragment = bout.toByteArray();
        inflater.reset();
      }

    inSequence++;
    return fragment;
  }

  public synchronized byte[] encrypt (byte[] fragment, int off, int len,
                                      ContentType type)
    throws OverflowException, SSLException
  {
    if (deflater != null)
      {
        byte[] buf = new byte[1024];
        ByteArrayOutputStream bout = new ByteArrayOutputStream (len >>> 1);
        deflater.setInput (fragment, off, len);
        deflater.finish();
        len = 0;
        while ((len = deflater.deflate (buf)) > 0)
          bout.write (buf, 0, len);
        // This should technically never happen for zlib.
        if (bout.size() > fragmentLength + 1024)
          throw new OverflowException ("deflated data too large");
        fragment = bout.toByteArray();
        off = 0;
        len = fragment.length;
        deflater.reset();
      }

    if (outMac != null)
      {
        outMac.update ((byte) (inSequence >>> 56));
        outMac.update ((byte) (inSequence >>> 48));
        outMac.update ((byte) (inSequence >>> 40));
        outMac.update ((byte) (inSequence >>> 32));
        outMac.update ((byte) (inSequence >>> 24));
        outMac.update ((byte) (inSequence >>> 16));
        outMac.update ((byte) (inSequence >>>  8));
        outMac.update ((byte)  inSequence);
        outMac.update ((byte) type.getValue());
        if (version != ProtocolVersion.SSL_3)
          {
            outMac.update ((byte) version.getMajor());
            outMac.update ((byte) version.getMinor());
          }
        outMac.update ((byte) (len >>> 8));
        outMac.update ((byte)  len);
        outMac.update (fragment, off, len);
        fragment = Util.concat (fragment, outMac.doFinal());
        off = 0;
        len = fragment.length;
      }

    if (outCipher != null)
      {
        try
          {
            fragment = outCipher.doFinal (fragment, off, len);
          }
        catch (BadPaddingException shouldNeverHappen)
          {
            // This is nonsensical. Don't even pretend that we can handle this.
            throw new RuntimeException ("bad padding thrown while encrypting");
          }
        catch (IllegalBlockSizeException ibse)
          {
            // Ditto.
            throw new RuntimeException ("illegal block size thrown while encrypting");
          }
        off = 0;
        len = fragment.length;
      }

    outSequence++;
    if (off == 0 && len == fragment.length)
      return fragment;
    else
      return Util.trim (fragment, off, len);
  }
}

About Koders | Resources | Downloads | Support | Black Duck | Terms of Service | DMCA | Privacy Policy | Contact Us