summaryrefslogtreecommitdiff
path: root/libjava/classpath/gnu/javax/crypto/sasl/srp/PasswordFile.java
blob: c13c2fa71c64c4d59c219a23103c9a808a71f517 (plain)
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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
/* PasswordFile.java --
   Copyright (C) 2003, 2006 Free Software Foundation, Inc.

This file is a part of GNU Classpath.

GNU Classpath 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.

GNU Classpath 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 GNU Classpath; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
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 gnu.javax.crypto.sasl.srp;

import gnu.java.lang.CPStringBuilder;

import gnu.java.security.Registry;
import gnu.java.security.util.Util;
import gnu.javax.crypto.key.srp6.SRPAlgorithm;
import gnu.javax.crypto.sasl.NoSuchUserException;
import gnu.javax.crypto.sasl.UserAlreadyExistsException;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.math.BigInteger;
import java.util.HashMap;
import java.util.Iterator;
import java.util.NoSuchElementException;
import java.util.StringTokenizer;

/**
 * The implementation of SRP password files.
 * <p>
 * For SRP, there are three (3) files:
 * <ol>
 * <li>The password configuration file: tpasswd.conf. It contains the pairs
 * &lt;N,g> indexed by a number for each pair used for a user. By default, this
 * file's pathname is constructed from the base password file pathname by
 * prepending it with the ".conf" suffix.</li>
 * <li>The base password file: tpasswd. It contains the related password
 * entries for all the users with values computed using SRP's default message
 * digest algorithm: SHA-1 (with 160-bit output block size).</li>
 * <li>The extended password file: tpasswd2. Its name, by default, is
 * constructed by adding the suffix "2" to the fully qualified pathname of the
 * base password file. It contains, in addition to the same fields as the base
 * password file, albeit with a different verifier value, an extra field
 * identifying the message digest algorithm used to compute this (verifier)
 * value.</li>
 * </ol>
 * <p>
 * This implementation assumes the following message digest algorithm codes:
 * <ul>
 * <li>0: the default hash algorithm, which is SHA-1 (or its alias SHA-160).</li>
 * <li>1: MD5.</li>
 * <li>2: RIPEMD-128.</li>
 * <li>3: RIPEMD-160.</li>
 * <li>4: SHA-256.</li>
 * <li>5: SHA-384.</li>
 * <li>6: SHA-512.</li>
 * </ul>
 * <p>
 * <b>IMPORTANT:</b> This method computes the verifiers as described in
 * RFC-2945, which differs from the description given on the web page for SRP-6.
 * <p>
 * Reference:
 * <ol>
 * <li><a href="http://srp.stanford.edu/design.html">SRP Protocol Design</a><br>
 * Thomas J. Wu.</li>
 * </ol>
 */
public class PasswordFile
{
  // names of property keys used in this class
  private static final String USER_FIELD = "user";
  private static final String VERIFIERS_FIELD = "verifier";
  private static final String SALT_FIELD = "salt";
  private static final String CONFIG_FIELD = "config";
  private static String DEFAULT_FILE;
  static
    {
      DEFAULT_FILE = System.getProperty(SRPRegistry.PASSWORD_FILE,
                                        SRPRegistry.DEFAULT_PASSWORD_FILE);
    }
  /** The SRP algorithm instances used by this object. */
  private static final HashMap srps;
  static
    {
      final HashMap map = new HashMap(SRPRegistry.SRP_ALGORITHMS.length);
      // The first entry MUST exist. The others are optional.
      map.put("0", SRP.instance(SRPRegistry.SRP_ALGORITHMS[0]));
      for (int i = 1; i < SRPRegistry.SRP_ALGORITHMS.length; i++)
        {
          try
            {
              map.put(String.valueOf(i),
                      SRP.instance(SRPRegistry.SRP_ALGORITHMS[i]));
            }
          catch (Exception x)
            {
              System.err.println("Ignored: " + x);
              x.printStackTrace(System.err);
            }
        }
      srps = map;
    }

  private String confName, pwName, pw2Name;
  private File configFile, passwdFile, passwd2File;
  private long lastmodPasswdFile, lastmodPasswd2File;
  private HashMap entries = new HashMap();
  private HashMap configurations = new HashMap();
  // default N values to use when creating a new password.conf file
  private static final BigInteger[] Nsrp = new BigInteger[] {
      SRPAlgorithm.N_2048,
      SRPAlgorithm.N_1536,
      SRPAlgorithm.N_1280,
      SRPAlgorithm.N_1024,
      SRPAlgorithm.N_768,
      SRPAlgorithm.N_640,
      SRPAlgorithm.N_512 };

  public PasswordFile() throws IOException
  {
    this(DEFAULT_FILE);
  }

  public PasswordFile(final File pwFile) throws IOException
  {
    this(pwFile.getAbsolutePath());
  }

  public PasswordFile(final String pwName) throws IOException
  {
    this(pwName, pwName + "2", pwName + ".conf");
  }

  public PasswordFile(final String pwName, final String confName)
      throws IOException
  {
    this(pwName, pwName + "2", confName);
  }

  public PasswordFile(final String pwName, final String pw2Name,
                      final String confName) throws IOException
  {
    super();

    this.pwName = pwName;
    this.pw2Name = pw2Name;
    this.confName = confName;

    readOrCreateConf();
    update();
  }

  /**
   * Returns a string representing the decimal value of an integer identifying
   * the message digest algorithm to use for the SRP computations.
   *
   * @param mdName the canonical name of a message digest algorithm.
   * @return a string representing the decimal value of an ID for that
   *         algorithm.
   */
  private static final String nameToID(final String mdName)
  {
    if (Registry.SHA_HASH.equalsIgnoreCase(mdName)
        || Registry.SHA1_HASH.equalsIgnoreCase(mdName)
        || Registry.SHA160_HASH.equalsIgnoreCase(mdName))
      return "0";
    else if (Registry.MD5_HASH.equalsIgnoreCase(mdName))
      return "1";
    else if (Registry.RIPEMD128_HASH.equalsIgnoreCase(mdName))
      return "2";
    else if (Registry.RIPEMD160_HASH.equalsIgnoreCase(mdName))
      return "3";
    else if (Registry.SHA256_HASH.equalsIgnoreCase(mdName))
      return "4";
    else if (Registry.SHA384_HASH.equalsIgnoreCase(mdName))
      return "5";
    else if (Registry.SHA512_HASH.equalsIgnoreCase(mdName))
      return "6";
    return "0";
  }

  /**
   * Checks if the current configuration file contains the &lt;N, g> pair for
   * the designated <code>index</code>.
   *
   * @param index a string representing 1-digit identification of an &lt;N, g>
   *          pair used.
   * @return <code>true</code> if the designated <code>index</code> is that
   *         of a known &lt;N, g> pair, and <code>false</code> otherwise.
   * @throws IOException if an exception occurs during the process.
   * @see SRPRegistry#N_2048_BITS
   * @see SRPRegistry#N_1536_BITS
   * @see SRPRegistry#N_1280_BITS
   * @see SRPRegistry#N_1024_BITS
   * @see SRPRegistry#N_768_BITS
   * @see SRPRegistry#N_640_BITS
   * @see SRPRegistry#N_512_BITS
   */
  public synchronized boolean containsConfig(final String index)
      throws IOException
  {
    checkCurrent();
    return configurations.containsKey(index);
  }

  /**
   * Returns a pair of strings representing the pair of <code>N</code> and
   * <code>g</code> MPIs for the designated <code>index</code>.
   *
   * @param index a string representing 1-digit identification of an &lt;N, g>
   *          pair to look up.
   * @return a pair of strings, arranged in an array, where the first (at index
   *         position #0) is the repesentation of the MPI <code>N</code>, and
   *         the second (at index position #1) is the representation of the MPI
   *         <code>g</code>. If the <code>index</code> refers to an unknown
   *         pair, then an empty string array is returned.
   * @throws IOException if an exception occurs during the process.
   */
  public synchronized String[] lookupConfig(final String index)
      throws IOException
  {
    checkCurrent();
    String[] result = null;
    if (configurations.containsKey(index))
      result = (String[]) configurations.get(index);
    return result;
  }

  public synchronized boolean contains(final String user) throws IOException
  {
    checkCurrent();
    return entries.containsKey(user);
  }

  public synchronized void add(final String user, final String passwd,
                               final byte[] salt, final String index)
      throws IOException
  {
    checkCurrent();
    if (entries.containsKey(user))
      throw new UserAlreadyExistsException(user);
    final HashMap fields = new HashMap(4);
    fields.put(USER_FIELD, user); // 0
    fields.put(VERIFIERS_FIELD, newVerifiers(user, salt, passwd, index)); // 1
    fields.put(SALT_FIELD, Util.toBase64(salt)); // 2
    fields.put(CONFIG_FIELD, index); // 3
    entries.put(user, fields);
    savePasswd();
  }

  public synchronized void changePasswd(final String user, final String passwd)
      throws IOException
  {
    checkCurrent();
    if (! entries.containsKey(user))
      throw new NoSuchUserException(user);
    final HashMap fields = (HashMap) entries.get(user);
    final byte[] salt;
    try
      {
        salt = Util.fromBase64((String) fields.get(SALT_FIELD));
      }
    catch (NumberFormatException x)
      {
        throw new IOException("Password file corrupt");
      }
    final String index = (String) fields.get(CONFIG_FIELD);
    fields.put(VERIFIERS_FIELD, newVerifiers(user, salt, passwd, index));
    entries.put(user, fields);
    savePasswd();
  }

  public synchronized void savePasswd() throws IOException
  {
    final FileOutputStream f1 = new FileOutputStream(passwdFile);
    final FileOutputStream f2 = new FileOutputStream(passwd2File);
    PrintWriter pw1 = null;
    PrintWriter pw2 = null;
    try
      {
        pw1 = new PrintWriter(f1, true);
        pw2 = new PrintWriter(f2, true);
        this.writePasswd(pw1, pw2);
      }
    finally
      {
        if (pw1 != null)
          try
            {
              pw1.flush();
            }
          finally
            {
              pw1.close();
            }
        if (pw2 != null)
          try
            {
              pw2.flush();
            }
          finally
            {
              pw2.close();
            }
        try
          {
            f1.close();
          }
        catch (IOException ignored)
          {
          }
        try
          {
            f2.close();
          }
        catch (IOException ignored)
          {
          }
      }
    lastmodPasswdFile = passwdFile.lastModified();
    lastmodPasswd2File = passwd2File.lastModified();
  }

  /**
   * Returns the triplet: verifier, salt and configuration file index, of a
   * designated user, and a designated message digest algorithm name, as an
   * array of strings.
   *
   * @param user the username.
   * @param mdName the canonical name of the SRP's message digest algorithm.
   * @return a string array containing, in this order, the BASE-64 encodings of
   *         the verifier, the salt and the index in the password configuration
   *         file of the MPIs N and g of the designated user.
   */
  public synchronized String[] lookup(final String user, final String mdName)
      throws IOException
  {
    checkCurrent();
    if (! entries.containsKey(user))
      throw new NoSuchUserException(user);
    final HashMap fields = (HashMap) entries.get(user);
    final HashMap verifiers = (HashMap) fields.get(VERIFIERS_FIELD);
    final String salt = (String) fields.get(SALT_FIELD);
    final String index = (String) fields.get(CONFIG_FIELD);
    final String verifier = (String) verifiers.get(nameToID(mdName));
    return new String[] { verifier, salt, index };
  }

  private synchronized void readOrCreateConf() throws IOException
  {
    configurations.clear();
    final FileInputStream fis;
    configFile = new File(confName);
    try
      {
        fis = new FileInputStream(configFile);
        readConf(fis);
      }
    catch (FileNotFoundException x)
      { // create a default one
        final String g = Util.toBase64(Util.trim(new BigInteger("2")));
        String index, N;
        for (int i = 0; i < Nsrp.length; i++)
          {
            index = String.valueOf(i + 1);
            N = Util.toBase64(Util.trim(Nsrp[i]));
            configurations.put(index, new String[] { N, g });
          }
        FileOutputStream f0 = null;
        PrintWriter pw0 = null;
        try
          {
            f0 = new FileOutputStream(configFile);
            pw0 = new PrintWriter(f0, true);
            this.writeConf(pw0);
          }
        finally
          {
            if (pw0 != null)
              pw0.close();
            else if (f0 != null)
              f0.close();
          }
      }
  }

  private void readConf(final InputStream in) throws IOException
  {
    final BufferedReader din = new BufferedReader(new InputStreamReader(in));
    String line, index, N, g;
    StringTokenizer st;
    while ((line = din.readLine()) != null)
      {
        st = new StringTokenizer(line, ":");
        try
          {
            index = st.nextToken();
            N = st.nextToken();
            g = st.nextToken();
          }
        catch (NoSuchElementException x)
          {
            throw new IOException("SRP password configuration file corrupt");
          }
        configurations.put(index, new String[] { N, g });
      }
  }

  private void writeConf(final PrintWriter pw)
  {
    String ndx;
    String[] mpi;
    CPStringBuilder sb;
    for (Iterator it = configurations.keySet().iterator(); it.hasNext();)
      {
        ndx = (String) it.next();
        mpi = (String[]) configurations.get(ndx);
        sb = new CPStringBuilder(ndx)
            .append(":").append(mpi[0])
            .append(":").append(mpi[1]);
        pw.println(sb.toString());
      }
  }

  /**
   * Compute the new verifiers for the designated username and password.
   * <p>
   * <b>IMPORTANT:</b> This method computes the verifiers as described in
   * RFC-2945, which differs from the description given on the web page for
   * SRP-6.
   *
   * @param user the user's name.
   * @param s the user's salt.
   * @param password the user's password
   * @param index the index of the &lt;N, g> pair to use for this user.
   * @return a {@link java.util.Map} of user verifiers.
   * @throws UnsupportedEncodingException if the US-ASCII decoder is not
   *           available on this platform.
   */
  private HashMap newVerifiers(final String user, final byte[] s,
                               final String password, final String index)
      throws UnsupportedEncodingException
  {
    // to ensure inter-operability with non-java tools
    final String[] mpi = (String[]) configurations.get(index);
    final BigInteger N = new BigInteger(1, Util.fromBase64(mpi[0]));
    final BigInteger g = new BigInteger(1, Util.fromBase64(mpi[1]));
    final HashMap result = new HashMap(srps.size());
    BigInteger x, v;
    SRP srp;
    for (int i = 0; i < srps.size(); i++)
      {
        final String digestID = String.valueOf(i);
        srp = (SRP) srps.get(digestID);
        x = new BigInteger(1, srp.computeX(s, user, password));
        v = g.modPow(x, N);
        final String verifier = Util.toBase64(v.toByteArray());
        result.put(digestID, verifier);
      }
    return result;
  }

  private synchronized void update() throws IOException
  {
    entries.clear();
    FileInputStream fis;
    passwdFile = new File(pwName);
    lastmodPasswdFile = passwdFile.lastModified();
    try
      {
        fis = new FileInputStream(passwdFile);
        readPasswd(fis);
      }
    catch (FileNotFoundException ignored)
      {
      }
    passwd2File = new File(pw2Name);
    lastmodPasswd2File = passwd2File.lastModified();
    try
      {
        fis = new FileInputStream(passwd2File);
        readPasswd2(fis);
      }
    catch (FileNotFoundException ignored)
      {
      }
  }

  private void checkCurrent() throws IOException
  {
    if (passwdFile.lastModified() > lastmodPasswdFile
        || passwd2File.lastModified() > lastmodPasswd2File)
      update();
  }

  private void readPasswd(final InputStream in) throws IOException
  {
    final BufferedReader din = new BufferedReader(new InputStreamReader(in));
    String line, user, verifier, salt, index;
    StringTokenizer st;
    while ((line = din.readLine()) != null)
      {
        st = new StringTokenizer(line, ":");
        try
          {
            user = st.nextToken();
            verifier = st.nextToken();
            salt = st.nextToken();
            index = st.nextToken();
          }
        catch (NoSuchElementException x)
          {
            throw new IOException("SRP base password file corrupt");
          }
        final HashMap verifiers = new HashMap(6);
        verifiers.put("0", verifier);
        final HashMap fields = new HashMap(4);
        fields.put(USER_FIELD, user);
        fields.put(VERIFIERS_FIELD, verifiers);
        fields.put(SALT_FIELD, salt);
        fields.put(CONFIG_FIELD, index);
        entries.put(user, fields);
      }
  }

  private void readPasswd2(final InputStream in) throws IOException
  {
    final BufferedReader din = new BufferedReader(new InputStreamReader(in));
    String line, digestID, user, verifier;
    StringTokenizer st;
    HashMap fields, verifiers;
    while ((line = din.readLine()) != null)
      {
        st = new StringTokenizer(line, ":");
        try
          {
            digestID = st.nextToken();
            user = st.nextToken();
            verifier = st.nextToken();
          }
        catch (NoSuchElementException x)
          {
            throw new IOException("SRP extended password file corrupt");
          }
        fields = (HashMap) entries.get(user);
        if (fields != null)
          {
            verifiers = (HashMap) fields.get(VERIFIERS_FIELD);
            verifiers.put(digestID, verifier);
          }
      }
  }

  private void writePasswd(final PrintWriter pw1, final PrintWriter pw2)
      throws IOException
  {
    String user, digestID;
    HashMap fields, verifiers;
    CPStringBuilder sb1, sb2;
    Iterator j;
    final Iterator i = entries.keySet().iterator();
    while (i.hasNext())
      {
        user = (String) i.next();
        fields = (HashMap) entries.get(user);
        if (! user.equals(fields.get(USER_FIELD)))
          throw new IOException("Inconsistent SRP password data");
        verifiers = (HashMap) fields.get(VERIFIERS_FIELD);
        sb1 = new CPStringBuilder(user)
            .append(":").append((String) verifiers.get("0"))
            .append(":").append((String) fields.get(SALT_FIELD))
            .append(":").append((String) fields.get(CONFIG_FIELD));
        pw1.println(sb1.toString());
        // write extended information
        j = verifiers.keySet().iterator();
        while (j.hasNext())
          {
            digestID = (String) j.next();
            if (! "0".equals(digestID))
              {
                // #0 is the default digest, already present in tpasswd!
                sb2 = new CPStringBuilder(digestID)
                    .append(":").append(user)
                    .append(":").append((String) verifiers.get(digestID));
                pw2.println(sb2.toString());
              }
          }
      }
  }
}