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
|
/* dsa.c
*
*/
/* lsh, an implementation of the ssh protocol
*
* Copyright (C) 1998 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
*/
#if HAVE_CONFIG_H
#include "config.h"
#endif
#include <assert.h>
#include <nettle/bignum.h>
#include <nettle/dsa-compat.h>
#include <nettle/sexp.h>
#include <nettle/sha.h>
#include "publickey_crypto.h"
#include "atoms.h"
#include "format.h"
#include "lsh_string.h"
#include "parse.h"
#include "randomness.h"
#include "sexp.h"
#include "ssh.h"
#include "werror.h"
#include "xalloc.h"
#include "dsa.c.x"
/* The standard says that DSA public keys are at most 1024 bits, i.e.
* 128 octets. We are a little more liberal than that. Note that
* allowing really large keys opens for Denial-of-service attacks. */
#define DSA_MAX_OCTETS 256
#define DSA_MAX_BITS (8 * DSA_MAX_OCTETS)
/* DSA signatures */
/* GABA:
(class
(name dsa_algorithm)
(super signature_algorithm)
(vars
(random object randomness)))
*/
/* GABA:
(class
(name dsa_verifier)
(super verifier)
(vars
(key indirect-special "struct dsa_public_key"
#f dsa_public_key_clear)))
*/
/* GABA:
(class
(name dsa_signer)
(super signer)
(vars
(verifier object dsa_verifier)
(random object randomness)
(key indirect-special "struct dsa_private_key"
#f dsa_private_key_clear)))
*/
static int
do_dsa_verify(struct verifier *c, int algorithm,
uint32_t length,
const uint8_t *msg,
uint32_t signature_length,
const uint8_t *signature_data)
{
CAST(dsa_verifier, self, c);
struct sha1_ctx hash;
struct simple_buffer buffer;
int res = 0;
struct dsa_signature sv;
trace("do_dsa_verify: Verifying %a signature\n", algorithm);
dsa_signature_init(&sv);
switch (algorithm)
{
case ATOM_SSH_DSS:
{
/* NOTE: draft-ietf-secsh-transport-X.txt (x <= 07) uses an extra
* length field, which should be removed in the next version. */
uint32_t buf_length;
const uint8_t *buf;
int atom;
simple_buffer_init(&buffer, signature_length, signature_data);
if (!(parse_atom(&buffer, &atom)
&& (atom == ATOM_SSH_DSS)
&& parse_string(&buffer, &buf_length, &buf)
&& buf_length == 2 * DSA_SHA1_Q_OCTETS
&& parse_eod(&buffer)))
goto fail;
nettle_mpz_set_str_256_u(sv.r, DSA_SHA1_Q_OCTETS, buf);
nettle_mpz_set_str_256_u(sv.s, DSA_SHA1_Q_OCTETS,
buf + DSA_SHA1_Q_OCTETS);
break;
}
/* It doesn't matter here which flavour of SPKI is used. */
case ATOM_SPKI_SIGN_RSA:
case ATOM_SPKI_SIGN_DSS:
case ATOM_SPKI:
{
struct sexp_iterator i;
const uint8_t *names[2] = { "r", "s" };
struct sexp_iterator values[2];
if (! (sexp_iterator_first(&i, signature_length, signature_data)
&& sexp_iterator_enter_list(&i)
&& sexp_iterator_assoc(&i, 2, names, values)
&& nettle_mpz_set_sexp(sv.r, DSA_SHA1_Q_BITS, &values[0])
&& nettle_mpz_set_sexp(sv.s, DSA_SHA1_Q_BITS, &values[1])) )
goto fail;
break;
}
default:
fatal("do_dsa_verify: Internal error!\n");
}
sha1_init(&hash);
sha1_update(&hash, length, msg);
res = dsa_sha1_verify(&self->key, &hash, &sv);
fail:
dsa_signature_clear(&sv);
return res;
}
static struct lsh_string *
do_dsa_public_key(struct verifier *s)
{
CAST(dsa_verifier, self, s);
return ssh_format("%a%n%n%n%n",
ATOM_SSH_DSS,
self->key.p, self->key.q,
self->key.g, self->key.y);
}
static struct lsh_string *
do_dsa_public_spki_key(struct verifier *s, int transport)
{
CAST(dsa_verifier, self, s);
return lsh_string_format_sexp(transport,
"(%0s(%0s(%0s%b)(%0s%b)(%0s%b)(%0s%b)))",
"public-key", "dsa",
"p", self->key.p,
"q", self->key.q,
"g", self->key.g,
"y", self->key.y);
}
static void
init_dsa_verifier(struct dsa_verifier *self)
{
/* FIXME: The allocator could do this kind of initialization
* automatically. */
dsa_public_key_init(&self->key);
self->super.verify = do_dsa_verify;
self->super.public_spki_key = do_dsa_public_spki_key;
self->super.public_key = do_dsa_public_key;
}
/* Alternative constructor using a key of type ssh-dss, when the atom
* "ssh-dss" is already read from the buffer. */
struct verifier *
parse_ssh_dss_public(struct simple_buffer *buffer)
{
NEW(dsa_verifier, res);
init_dsa_verifier(res);
if (parse_bignum(buffer, res->key.p, DSA_MAX_OCTETS)
&& (mpz_sgn(res->key.p) == 1)
&& parse_bignum(buffer, res->key.q, DSA_SHA1_Q_OCTETS)
&& (mpz_sgn(res->key.q) == 1)
&& mpz_sizeinbase(res->key.q, 2) == DSA_SHA1_Q_BITS
&& (mpz_cmp(res->key.q, res->key.p) < 0) /* q < p */
&& parse_bignum(buffer, res->key.g, DSA_MAX_OCTETS)
&& (mpz_sgn(res->key.g) == 1)
&& (mpz_cmp(res->key.g, res->key.p) < 0) /* g < p */
&& parse_bignum(buffer, res->key.y, DSA_MAX_OCTETS)
&& (mpz_sgn(res->key.y) == 1)
&& (mpz_cmp(res->key.y, res->key.p) < 0) /* y < p */
&& parse_eod(buffer))
return &res->super;
else
{
KILL(res);
return NULL;
}
}
/* Creating signatures */
static void
dsa_blob_write(struct lsh_string *buf, uint32_t pos,
const struct dsa_signature *signature)
{
lsh_string_write_bignum(buf, pos, DSA_SHA1_Q_OCTETS, signature->r);
lsh_string_write_bignum(buf, pos + DSA_SHA1_Q_OCTETS, DSA_SHA1_Q_OCTETS,
signature->s);
}
static struct lsh_string *
do_dsa_sign(struct signer *c,
int algorithm,
uint32_t msg_length,
const uint8_t *msg)
{
CAST(dsa_signer, self, c);
struct dsa_signature sv;
struct sha1_ctx hash;
struct lsh_string *signature;
trace("do_dsa_sign: Signing according to %a\n", algorithm);
dsa_signature_init(&sv);
sha1_init(&hash);
sha1_update(&hash, msg_length, msg);
if (dsa_sha1_sign(&self->verifier->key, &self->key,
self->random, lsh_random, &hash, &sv))
/* Build signature */
switch (algorithm)
{
case ATOM_SSH_DSS:
{
uint32_t blob_pos;
/* NOTE: draft-ietf-secsh-transport-X.txt (x <= 07) uses an extra
* length field, which should be removed in the next version. */
signature = ssh_format("%a%r", ATOM_SSH_DSS,
2 * DSA_SHA1_Q_OCTETS, &blob_pos);
dsa_blob_write(signature, blob_pos, &sv);
break;
}
/* It doesn't matter here which flavour of SPKI is used. */
case ATOM_SPKI_SIGN_RSA:
case ATOM_SPKI_SIGN_DSS:
case ATOM_SPKI:
/* Format: "((1:r20:<r>)(1:s20:<s>))". */
signature = lsh_string_format_sexp(0, "((r%b)(s%b))",
sv.r, sv.s);
break;
default:
fatal("do_dsa_sign: Internal error, unexpected algorithm %a.\n",
algorithm);
}
else
signature = NULL;
dsa_signature_clear(&sv);
return signature;
}
static struct verifier *
do_dsa_get_verifier(struct signer *s)
{
CAST(dsa_signer, self, s);
return &self->verifier->super;
}
static struct verifier *
make_dsa_verifier(struct signature_algorithm *self UNUSED,
struct sexp_iterator *i)
{
NEW(dsa_verifier, res);
init_dsa_verifier(res);
if (dsa_keypair_from_sexp_alist((struct dsa_params *)&res->key, res->key.y, NULL, DSA_MAX_BITS, DSA_SHA1_Q_BITS, i))
return &res->super;
KILL(res);
return NULL;
}
static struct signer *
make_dsa_signer(struct signature_algorithm *c,
struct sexp_iterator *i)
{
CAST(dsa_algorithm, self, c);
NEW(dsa_verifier, verifier);
NEW(dsa_signer, res);
init_dsa_verifier(verifier);
dsa_private_key_init(&res->key);
if (dsa_keypair_from_sexp_alist((struct dsa_params *)&verifier->key, verifier->key.y, res->key.x, DSA_MAX_BITS, DSA_SHA1_Q_BITS, i))
{
res->random = self->random;
res->verifier = verifier;
res->super.sign = do_dsa_sign;
res->super.get_verifier = do_dsa_get_verifier;
return &res->super;
}
KILL(res);
KILL(verifier);
return NULL;
}
struct signature_algorithm *
make_dsa_algorithm(struct randomness *random)
{
NEW(dsa_algorithm, dsa);
dsa->super.make_signer = make_dsa_signer;
dsa->super.make_verifier = make_dsa_verifier;
dsa->random = random;
return &dsa->super;
}
struct verifier *
make_ssh_dss_verifier(const struct lsh_string *public)
{
struct simple_buffer buffer;
int atom;
simple_buffer_init(&buffer, STRING_LD(public));
return ( (parse_atom(&buffer, &atom)
&& (atom == ATOM_SSH_DSS))
? parse_ssh_dss_public(&buffer)
: NULL);
}
|