srp-gen.c 5.51 KB
Newer Older
Niels Möller's avatar
Niels Möller committed
1
/* srp-gen.c
Niels Möller's avatar
Niels Möller committed
2
3
4
 *
 * Create an SRP verifier
 *
5
 */
Niels Möller's avatar
Niels Möller committed
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25

/* lsh, an implementation of the ssh protocol
 *
 * Copyright (C) 2000 Niels Mller
 *
 * This program 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.
 *
 * This program 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 this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#if HAVE_CONFIG_H
#include "config.h"
#endif

#include <assert.h>
#include <errno.h>

#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>

#if HAVE_UNISTD_H
#include <unistd.h>
#endif

Niels Möller's avatar
Niels Möller committed
41
42
43
#include "srp.h"

#include "crypto.h"
44
#include "environ.h"
Niels Möller's avatar
Niels Möller committed
45
#include "format.h"
46
#include "interact.h"
Niels Möller's avatar
Niels Möller committed
47
#include "io.h"
48
#include "lsh_string.h"
Niels Möller's avatar
Niels Möller committed
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include "randomness.h"
#include "srp.h"
#include "version.h"
#include "werror.h"
#include "xalloc.h"

#define BLOCK_SIZE 2000
#define SALT_SIZE 20

#include "srp-gen.c.x"

/* Option parsing */

const char *argp_program_version
= "srp-gen-" VERSION;

const char *argp_program_bug_address = BUG_ADDRESS;

/* GABA:
   (class
     (name srp_gen_options)
     (vars
71
72
       (tty object interact)
       
Niels Möller's avatar
Niels Möller committed
73
       (e object exception_handler)
74
       (G const object zn_group)
75
       (H const object hash_algorithm)
Niels Möller's avatar
Niels Möller committed
76
77
78
79
80
81
82
83
84
85
       
       (file string)
       (dest object abstract_write)

       (name . "const char *")
       (passwd string)
       (r object randomness)))
*/

static struct srp_gen_options *
86
make_srp_gen_options(struct exception_handler *e)
Niels Möller's avatar
Niels Möller committed
87
88
89
{
  NEW(srp_gen_options, self);

90
  /* We don't need window change tracking. */
91
  self->tty = make_unix_interact();
92
  
Niels Möller's avatar
Niels Möller committed
93
94
  self->e = e;

95
  self->G = make_ssh_ring_srp_1();
96
  self->H = &crypto_sha1_algorithm;
Niels Möller's avatar
Niels Möller committed
97
98
99
  self->file = NULL;
  self->dest = NULL;

100
  USER_NAME_FROM_ENV(self->name);
Niels Möller's avatar
Niels Möller committed
101
102
103
  self->passwd = NULL;

  /* We use this only for generating the salt. */
104
  self->r = make_user_random(getenv(ENV_HOME));
Niels Möller's avatar
Niels Möller committed
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

  return self;
}

static const struct argp_option
main_options[] =
{
  /* Name, key, arg-name, flags, doc, group */
  { "user", 'l', "User name", 0, NULL, 0 },
  { "password", 'p', "Password", 0, NULL, 0 },
  { "output-file", 'o', "Filename", 0, "Default is to write to stdout.", 0 },
  { NULL, 0, NULL, 0, NULL, 0 }
};

static const struct argp_child
main_argp_children[] =
{
  { &werror_argp, 0, "", 0 },
  { NULL, 0, NULL, 0}
};

static error_t
main_argp_parser(int key, char *arg, struct argp_state *state)
{
  CAST(srp_gen_options, self, state->input);

  switch(key)
    {
    default:
      return ARGP_ERR_UNKNOWN;

    case ARGP_KEY_INIT:
Niels Möller's avatar
Niels Möller committed
137
      state->child_inputs[0] = NULL;
Niels Möller's avatar
Niels Möller committed
138
139
140
      break;

    case ARGP_KEY_END:
141
142
143
      if (!self->name)
	argp_error(state, "No user name given. Use the -l option, or set LOGNAME in the environment.");

Niels Möller's avatar
Niels Möller committed
144
145
146
147
148
      {
	struct lsh_fd *fd;
	
	if (self->file)
	  {
149
150
	    const char *cfile = lsh_get_cstring(self->file);
	    fd = io_write_file(cfile,
Niels Möller's avatar
Niels Möller committed
151
152
153
154
			       O_CREAT | O_EXCL | O_WRONLY,
			       0600, BLOCK_SIZE,
			       NULL, self->e);
	    if (!fd)
155
156
	      argp_failure(state, EXIT_FAILURE, errno,
			   "Could not open '%s'.", cfile);
Niels Möller's avatar
Niels Möller committed
157
158
159
	  }
	else
	  {
160
	    fd = io_write(make_lsh_fd(STDOUT_FILENO, IO_STDIO, "stdout",
161
				      self->e),
Niels Möller's avatar
Niels Möller committed
162
163
164
165
			  BLOCK_SIZE, NULL);
	  }
	self->dest = &fd->write_buffer->super;
      }
166
      
Niels Möller's avatar
Niels Möller committed
167
168
169
170
171
      while (!self->passwd)
	{
	  struct lsh_string *pw;
	  struct lsh_string *again;

172
	  pw = INTERACT_READ_PASSWORD(self->tty, 500,
173
				      ssh_format("Enter new SRP password: "));
Niels Möller's avatar
Niels Möller committed
174
175
176
	  if (!pw)
	    argp_failure(state, EXIT_FAILURE, 0, "Aborted.");

177
	  again = INTERACT_READ_PASSWORD(self->tty, 500,
178
					 ssh_format("Again: "));
Niels Möller's avatar
Niels Möller committed
179
180
181
182
183
184
185
186
187
188
	  if (!again)
	    argp_failure(state, EXIT_FAILURE, 0, "Aborted.");

	  if (lsh_string_eq(pw, again))
	    self->passwd = pw;
	  else
	    lsh_string_free(pw);

	  lsh_string_free(again);
	}
189
      
Niels Möller's avatar
Niels Möller committed
190
191
192
      break;

    case 'o':
193
      self->file = make_string(arg);
Niels Möller's avatar
Niels Möller committed
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
      break;

    case 'p':
      self->passwd = ssh_format("%lz", arg);
      break;
    }
  return 0;
}

static const struct argp
main_argp =
{ main_options, main_argp_parser, 
  NULL,
  "Generates a password verifier for the Secure Remote Password protocol.",
  main_argp_children,
  NULL, NULL
};

Niels Möller's avatar
Niels Möller committed
212
static struct lsh_string *
Niels Möller's avatar
Niels Möller committed
213
214
215
216
srp_gen(struct srp_gen_options *options)
{
  struct lsh_string *salt;
  struct lsh_string *name;
Niels Möller's avatar
Niels Möller committed
217
  struct lsh_string *res;
Niels Möller's avatar
Niels Möller committed
218

219
  /* NOTE: Allows random to be of bad quality */
220
  salt = lsh_string_random(options->r, SALT_SIZE);
221

Niels Möller's avatar
Niels Möller committed
222
223
  name = ssh_format("%lz", options->name);

Niels Möller's avatar
Niels Möller committed
224
225
226
  /* FIXME: Leaks some strings. */
  res = srp_make_verifier(options->G, options->H,
			  salt, name, options->passwd);
Niels Möller's avatar
Niels Möller committed
227
228
  lsh_string_free(name);

Niels Möller's avatar
Niels Möller committed
229
  return res;
Niels Möller's avatar
Niels Möller committed
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
}

static void
do_srp_gen_handler(struct exception_handler *s UNUSED,
			const struct exception *e)
{
  werror("lsh_writekey: %z\n", e->msg);

  exit(EXIT_FAILURE);
}

static struct exception_handler exc_handler =
STATIC_EXCEPTION_HANDLER(do_srp_gen_handler, NULL);

int main(int argc, char **argv)
{
246
  struct srp_gen_options *options
247
    = make_srp_gen_options(&exc_handler);
248

249
250
  io_init();
  
Niels Möller's avatar
Niels Möller committed
251
252
  argp_parse(&main_argp, argc, argv, 0, NULL, options);

Niels Möller's avatar
Niels Möller committed
253
254
255
  /* FIXME: Use write_raw instead. */
  A_WRITE(options->dest, srp_gen(options));
  
256
257
258
  io_run();
  io_final();
  
Niels Möller's avatar
Niels Möller committed
259
260
  return EXIT_SUCCESS;
}