rtconf.c

Go to the documentation of this file.
00001 /* @(#)root/srputils:$Id: rtconf.c 20882 2007-11-19 11:31:26Z rdm $ */
00002 /*
00003  * Copyright (c) 1997-1999  The Stanford SRP Authentication Project
00004  * All Rights Reserved.
00005  *
00006  * Permission is hereby granted, free of charge, to any person obtaining
00007  * a copy of this software and associated documentation files (the
00008  * "Software"), to deal in the Software without restriction, including
00009  * without limitation the rights to use, copy, modify, merge, publish,
00010  * distribute, sublicense, and/or sell copies of the Software, and to
00011  * permit persons to whom the Software is furnished to do so, subject to
00012  * the following conditions:
00013  *
00014  * The above copyright notice and this permission notice shall be
00015  * included in all copies or substantial portions of the Software.
00016  *
00017  * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
00018  * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
00019  * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
00020  *
00021  * IN NO EVENT SHALL STANFORD BE LIABLE FOR ANY SPECIAL, INCIDENTAL,
00022  * INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, OR ANY DAMAGES WHATSOEVER
00023  * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER OR NOT ADVISED OF
00024  * THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF LIABILITY, ARISING OUT
00025  * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
00026  *
00027  * In addition, the following conditions apply:
00028  *
00029  * 1. Any software that incorporates the SRP authentication technology
00030  *    must display the following acknowlegment:
00031  *    "This product uses the 'Secure Remote Password' cryptographic
00032  *     authentication system developed by Tom Wu (tjw@CS.Stanford.EDU)."
00033  *
00034  * 2. Any software that incorporates all or part of the SRP distribution
00035  *    itself must also display the following acknowledgment:
00036  *    "This product includes software developed by Tom Wu and Eugene
00037  *     Jhong for the SRP Distribution (http://srp.stanford.edu/srp/)."
00038  *
00039  * 3. Redistributions in source or binary form must retain an intact copy
00040  *    of this copyright notice and list of conditions.
00041  */
00042 
00043 #include <unistd.h>             /* close getlogin */
00044 #include <stdlib.h>             /* atexit exit */
00045 #include <stdio.h>
00046 #include <string.h>
00047 #include <sys/types.h>
00048 #include <sys/stat.h>
00049 #include <fcntl.h>
00050 #include <errno.h>
00051 
00052 #include "t_pwd.h"
00053 
00054 #define SROOTDCONF ".srootdpass.conf"
00055 
00056 #define MIN_BASIS_BITS 257
00057 #define BASIS_BITS 2048
00058 
00059 extern int optind;
00060 extern char *optarg;
00061 
00062 struct pre_struct {
00063    char *pre_mod;
00064    char *pre_gen;
00065    char *comment;
00066 } pre_params[] = {
00067 
00068    {
00069    "HMujfBWu4LfBFA0j3PpN7UbgUYfv.rMoMNuVRMoekpZ", "2", NULL}, {
00070       "W2KsCfRxb3/ELBvnVWufMA0gbdBlLXbJihgZkgp3xLTKwtPCUhSOHNZ5VLb9pBGR",
00071           "2", NULL}, {
00072       "3Kn/YYiomHkFkfM1x4kayR125MGkzpLUDy3y14FlTMwYnhZkjrMXnoC2TcFAecNlU5kFzgcpKYUbBOPZFRtyf3",
00073           "2", NULL}, {
00074       "CbDP.jR6YD6wAj2ByQWxQxQZ7.9J9xkn2.Uqb3zVm16vQyizprhBw9hi80psatZ8k54vwZfiIeEHZVsDnyqeWSSIpWso.wh5GD4OFgdhVI3",
00075           "2", NULL}, {
00076       "iqJ7nFZ4bGCRjE1F.FXEwL085Zb0kLM2TdHDaVVCdq0cKxvnH/0FLskJTKlDtt6sDl89dc//aEULTVFGtcbA/tDzc.bnFE.DWthQOu2n2JwKjgKfgCR2lZFWXdnWmoOh",
00077           "2", NULL}, {
00078       "///////////93zgY8MZ2DCJ6Oek0t1pHAG9E28fdp7G22xwcEnER8b5A27cED0JTxvKPiyqwGnimAmfjybyKDq/XDMrjKS95v8MrTc9UViRqJ4BffZes8F//////////",
00079           "7", "oakley prime 1"}, {
00080       "Ewl2hcjiutMd3Fu2lgFnUXWSc67TVyy2vwYCKoS9MLsrdJVT9RgWTCuEqWJrfB6uE3LsE9GkOlaZabS7M29sj5TnzUqOLJMjiwEzArfiLr9WbMRANlF68N5AVLcPWvNx6Zjl3m5Scp0BzJBz9TkgfhzKJZ.WtP3Mv/67I/0wmRZ",
00081           "2", NULL}, {
00082       "F//////////oG/QeY5emZJ4ncABWDmSqIa2JWYAPynq0Wk.fZiJco9HIWXvZZG4tU.L6RFDEaCRC2iARV9V53TFuJLjRL72HUI5jNPYNdx6z4n2wQOtxMiB/rosz0QtxUuuQ/jQYP.bhfya4NnB7.P9A6PHxEPJWV//////////",
00083           "5", "oakley prime 2"}, {
00084       "3NUKQ2Re4P5BEK0TLg2dX3gETNNNECPoe92h4OVMaDn3Xo/0QdjgG/EvM.hiVV1BdIGklSI14HA38Mpe5k04juR5/EXMU0r1WtsLhNXwKBlf2zEfoOh0zVmDvqInpU695f29Iy7sNW3U5RIogcs740oUp2Kdv5wuITwnIx84cnO.e467/IV1lPnvMCr0pd1dgS0a.RV5eBJr03Q65Xy61R",
00085           "2", NULL}, {
00086       "dUyyhxav9tgnyIg65wHxkzkb7VIPh4o0lkwfOKiPp4rVJrzLRYVBtb76gKlaO7ef5LYGEw3G.4E0jbMxcYBetDy2YdpiP/3GWJInoBbvYHIRO9uBuxgsFKTKWu7RnR7yTau/IrFTdQ4LY/q.AvoCzMxV0PKvD9Odso/LFIItn8PbTov3VMn/ZEH2SqhtpBUkWtmcIkEflhX/YY/fkBKfBbe27/zUaKUUZEUYZ2H2nlCL60.JIPeZJSzsu/xHDVcx",
00087           "2", NULL}, {
00088       "2iQzj1CagQc/5ctbuJYLWlhtAsPHc7xWVyCPAKFRLWKADpASkqe9djWPFWTNTdeJtL8nAhImCn3Sr/IAdQ1FrGw0WvQUstPx3FO9KNcXOwisOQ1VlL.gheAHYfbYyBaxXL.NcJx9TUwgWDT0hRzFzqSrdGGTN3FgSTA1v4QnHtEygNj3eZ.u0MThqWUaDiP87nqha7XnT66bkTCkQ8.7T8L4KZjIImrNrUftedTTBi.WCi.zlrBxDuOM0da0JbUkQlXqvp0yvJAPpC11nxmmZOAbQOywZGmu9nhZNuwTlxjfIro0FOdthaDTuZRL9VL7MRPUDo/DQEyW.d4H.UIlzp",
00089           "2", NULL}
00090 };
00091 
00092 #define NPARAMS (sizeof(pre_params) / sizeof(struct pre_struct))
00093 
00094 char *progName;
00095 
00096 int debug = 0;
00097 int verbose = 0;
00098 int composite = 0;
00099 
00100 int main(int argc, char *argv[])
00101 {
00102    char *chp;
00103    char configFile[256] = { 0 };
00104    char cbuf[256];
00105    char b64buf[MAXB64PARAMLEN];
00106    int c, ch, i, lastidx, keylen, yesno, fsize;
00107    FILE *efp;
00108 
00109    struct t_conf *tc = NULL;
00110    struct t_confent *tcent;
00111 
00112    progName = *argv;
00113    if ((chp = strrchr(progName, '/')) != (char *) 0)
00114       progName = chp + 1;
00115 
00116    while ((ch = getopt(argc, argv, "dv2c:")) != EOF)
00117       switch (ch) {
00118       case 'c':
00119          strcpy(configFile, optarg);
00120          break;
00121       case 'v':
00122          verbose++;
00123          break;
00124       case 'd':
00125          debug++;
00126          break;
00127       case '2':
00128          composite++;
00129          break;
00130       default:
00131          fprintf(stderr, "usage: %s [-dv2] [-c configfile]\n", progName);
00132          exit(1);
00133       }
00134 
00135    argc -= optind;
00136    argv += optind;
00137 
00138    if (configFile[0] == '\0' && getenv("HOME"))
00139       sprintf(configFile, "%s/%s", getenv("HOME"), SROOTDCONF);
00140 
00141    efp = fopen(configFile, "a+");
00142    if (efp == NULL) {
00143       if (creat(configFile, 0644) < 0
00144           || (efp = fopen(configFile, "a+")) == NULL) {
00145          fprintf(stderr, "%s: unable to create %s (errno = %d)\n",
00146                  progName, configFile, errno);
00147          exit(2);
00148       } else
00149          printf("%s: Creating new configuration file %s\n", progName,
00150                 configFile);
00151    }
00152 
00153    tc = t_openconf(efp);
00154    if (tc == NULL) {
00155       fprintf(stderr, "%s: unable to open configuration file %s\n",
00156               progName, configFile);
00157       exit(2);
00158    }
00159 
00160    tcent = t_getconflast(tc);
00161    if (tcent == NULL)
00162       lastidx = 0;
00163    else
00164       lastidx = tcent->index;
00165 
00166    if (lastidx > 0) {
00167       keylen = 8 * tcent->modulus.len;
00168       printf("Current field size is %d bits.\n", keylen);
00169       printf("\nIncrease the default field size? [y] ");
00170       yesno = 0;
00171       while ((c = getchar()) != '\n' && c != EOF) {
00172          if (yesno == 0) {
00173             if (c == 'n' || c == 'N')
00174                yesno = -1;
00175             else if (c == 'y' || c == 'Y')
00176                yesno = 1;
00177          }
00178       }
00179       if (c == EOF || yesno < 0)
00180          exit(0);
00181    } else {
00182       lastidx = 0;
00183       keylen = 0;
00184    }
00185 
00186    tcent = t_newconfent(tc);
00187 
00188    printf("\nGenerate a (n)ew field or use a (p)redefined field? [nP] ");
00189    fgets(cbuf, sizeof(cbuf), stdin);
00190    if (*cbuf != 'n' && *cbuf != 'N') {
00191       for (i = 0; i < (int)NPARAMS; ++i) {
00192          tcent->modulus.len = t_fromb64((char *)tcent->modulus.data,
00193                                         pre_params[i].pre_mod);
00194          printf("(%d) [%d bits]  %s\n    Modulus = %s\n  Generator = %s\n",
00195                 i + 1, 8 * tcent->modulus.len,
00196                 pre_params[i].comment ? pre_params[i].comment : "",
00197                 pre_params[i].pre_mod, pre_params[i].pre_gen);
00198       }
00199       printf("\nSelect a field (1-%d): ", NPARAMS);
00200       fgets(cbuf, sizeof(cbuf), stdin);
00201       i = atoi(cbuf);
00202       if (i <= 0 || i > (int)NPARAMS) {
00203          fprintf(stderr, "Index not in range\n");
00204          exit(1);
00205       }
00206       tcent->index = lastidx + 1;
00207       tcent->modulus.len = t_fromb64((char *)tcent->modulus.data,
00208                                      pre_params[i - 1].pre_mod);
00209       tcent->generator.len = t_fromb64((char *)tcent->generator.data,
00210                                        pre_params[i - 1].pre_gen);
00211       t_putconfent(tcent, efp);
00212       t_closeconf(tc);
00213       fclose(efp);
00214       printf("Configuration file updated.\n");
00215       exit(0);
00216    }
00217 
00218    printf("\nEnter the new field size, in bits.  Suggested sizes:\n\n");
00219    printf(" %3d (minimum, testing only)\n", MIN_BASIS_BITS);
00220    printf(" 384 (low security, but fast)\n");
00221    printf(" 512 (reasonable default)\n");
00222    printf(" 768 (better security)\n");
00223    printf("1024 (PGP-level security)\n");
00224    printf("1536 (extremely secure, possibly slow)\n");
00225    printf("2048 (maximum supported security level)\n");
00226    printf("\nField size (%d to %d): ", MIN_BASIS_BITS, BASIS_BITS);
00227 
00228    fgets(cbuf, sizeof(cbuf), stdin);
00229    fsize = atoi(cbuf);
00230    if (fsize < MIN_BASIS_BITS || fsize > BASIS_BITS) {
00231       fprintf(stderr, "%s: field size must be between %d and %d\n",
00232               progName, MIN_BASIS_BITS, BASIS_BITS);
00233       exit(1);
00234    }
00235 
00236    if (fsize <= keylen)
00237       fprintf(stderr, "Warning: new field size is not larger than old field size\n");
00238 
00239    printf("\nInitializing random number generator...");
00240    fflush(stdout);
00241    t_stronginitrand();
00242 
00243    if (composite)
00244       printf
00245           ("done.\n\nGenerating a %d-bit composite with safe prime factors.  This may take a while.\n",
00246            fsize);
00247    else
00248       printf
00249           ("done.\n\nGenerating a %d-bit safe prime.  This may take a while.\n",
00250            fsize);
00251 
00252    while (1) {
00253       while ((tcent = (composite ? t_makeconfent_c(tc, fsize) :
00254                        t_makeconfent(tc, fsize))) == NULL)
00255          printf("Parameter generation failed, retrying...\n");
00256       tcent->index = lastidx + 1;
00257 
00258       printf("\nParameters successfully generated.\n");
00259       printf("N = [%s]\n", t_tob64(b64buf,
00260                                    (char *)tcent->modulus.data,
00261                                    tcent->modulus.len));
00262       printf("g = [%s]\n",
00263              t_tob64(b64buf,
00264                      (char *)tcent->generator.data, tcent->generator.len));
00265       printf("\nUpdate the configuration file with these parameters? [Ynq] ");
00266 
00267       fgets(cbuf, sizeof(cbuf), stdin);
00268       switch (*cbuf) {
00269       case 'q':
00270       case 'Q':
00271          fclose(efp);
00272          exit(0);
00273       case 'n':
00274       case 'N':
00275          printf("\nGenerating another set of parameters, please wait...\n");
00276          break;
00277       default:
00278          t_putconfent(tcent, efp);
00279          t_closeconf(tc);
00280          fclose(efp);
00281          printf("Configuration file updated.\n");
00282          exit(0);
00283       }
00284    }
00285 }

Generated on Tue Jul 5 14:46:13 2011 for ROOT_528-00b_version by  doxygen 1.5.1