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

« back to all changes in this revision

Viewing changes to gnu/javax/net/ssl/provider/SSLRandom.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
/* SSLRandom.java -- SSLv3 pseudo-random function.
 
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.util.Map;
 
42
import gnu.java.security.hash.HashFactory;
 
43
import gnu.java.security.hash.IMessageDigest;
 
44
import gnu.java.security.prng.IRandom;
 
45
import gnu.java.security.prng.LimitReachedException;
 
46
 
 
47
class SSLRandom implements IRandom
 
48
{
 
49
 
 
50
  // Fields.
 
51
  // -------------------------------------------------------------------------
 
52
 
 
53
  static final String SECRET = "jessie.sslprng.secret";
 
54
  static final String SEED = "jessie.sslprng.seed";
 
55
 
 
56
  private final IMessageDigest md5, sha;
 
57
  private byte[] secret;
 
58
  private byte[] buffer;
 
59
  private byte pad;
 
60
  private byte[] seed;
 
61
  private int idx;
 
62
 
 
63
  // Constructor.
 
64
  // -------------------------------------------------------------------------
 
65
 
 
66
  SSLRandom()
 
67
  {
 
68
    md5 = HashFactory.getInstance("MD5");
 
69
    sha = HashFactory.getInstance("SHA-1");
 
70
  }
 
71
 
 
72
  // Instance methods.
 
73
  // -------------------------------------------------------------------------
 
74
 
 
75
  public void init(Map attrib)
 
76
  {
 
77
    secret = (byte[]) attrib.get(SECRET);
 
78
    seed = (byte[]) attrib.get(SEED);
 
79
 
 
80
    if (secret == null || seed == null)
 
81
      throw new NullPointerException();
 
82
 
 
83
    pad = (byte) 'A';
 
84
    try { buffer = nextBlock(); }
 
85
    catch (LimitReachedException cantHappen) { }
 
86
  }
 
87
 
 
88
  public String name()
 
89
  {
 
90
    return "SSLRandom";
 
91
  }
 
92
 
 
93
  public Object clone()
 
94
  {
 
95
    throw new UnsupportedOperationException();
 
96
  }
 
97
 
 
98
  public byte nextByte() throws LimitReachedException
 
99
  {
 
100
    if (buffer == null)
 
101
      throw new IllegalStateException();
 
102
    if (idx >= buffer.length)
 
103
      buffer = nextBlock();
 
104
    return buffer[idx++];
 
105
  }
 
106
 
 
107
  public void nextBytes(byte[] buf, int off, int len)
 
108
    throws LimitReachedException
 
109
  {
 
110
    if (buffer == null)
 
111
      throw new IllegalStateException();
 
112
    if (buf == null)
 
113
      throw new NullPointerException();
 
114
    if (off < 0 || len < 0 || off+len > buf.length)
 
115
      throw new IndexOutOfBoundsException();
 
116
    int count = 0;
 
117
    while (count < len)
 
118
      {
 
119
        if (idx >= buffer.length)
 
120
          buffer = nextBlock();
 
121
        int l = Math.min(buffer.length-idx, len-count);
 
122
        System.arraycopy(buffer, idx, buf, off+count, l);
 
123
        count += l;
 
124
        idx += l;
 
125
      }
 
126
  }
 
127
 
 
128
  public boolean selfTest()
 
129
  {
 
130
    return true; // XXX
 
131
  }
 
132
 
 
133
  // For future versions of GNU Crypto. No-ops.
 
134
  public void addRandomByte (byte b)
 
135
  {
 
136
  }
 
137
 
 
138
  public void addRandomBytes(byte[] buffer) {
 
139
    addRandomBytes(buffer, 0, buffer.length);
 
140
  }
 
141
 
 
142
  public void addRandomBytes (byte[] b, int i, int j)
 
143
  {
 
144
  }
 
145
 
 
146
  // Own methods.
 
147
  // -------------------------------------------------------------------------
 
148
 
 
149
  private byte[] nextBlock() throws LimitReachedException
 
150
  {
 
151
    int count = pad - 'A' + 1;
 
152
    if (count > 26)
 
153
      throw new LimitReachedException();
 
154
    for (int i = 0; i < count; i++)
 
155
      sha.update(pad);
 
156
    sha.update(secret, 0, secret.length);
 
157
    sha.update(seed, 0, seed.length);
 
158
    byte[] b = sha.digest();
 
159
    md5.update(secret, 0, secret.length);
 
160
    md5.update(b, 0, b.length);
 
161
    idx = 0;
 
162
    pad++;
 
163
    return md5.digest();
 
164
  }
 
165
}