1264377Sdes/* $OpenBSD: ssh-dss.c,v 1.31 2014/02/02 03:44:31 djm Exp $ */
276259Sgreen/*
376259Sgreen * Copyright (c) 2000 Markus Friedl.  All rights reserved.
476259Sgreen *
576259Sgreen * Redistribution and use in source and binary forms, with or without
676259Sgreen * modification, are permitted provided that the following conditions
776259Sgreen * are met:
876259Sgreen * 1. Redistributions of source code must retain the above copyright
976259Sgreen *    notice, this list of conditions and the following disclaimer.
1076259Sgreen * 2. Redistributions in binary form must reproduce the above copyright
1176259Sgreen *    notice, this list of conditions and the following disclaimer in the
1276259Sgreen *    documentation and/or other materials provided with the distribution.
1376259Sgreen *
1476259Sgreen * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
1576259Sgreen * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
1676259Sgreen * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
1776259Sgreen * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
1876259Sgreen * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
1976259Sgreen * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2076259Sgreen * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2176259Sgreen * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2276259Sgreen * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
2376259Sgreen * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2476259Sgreen */
2576259Sgreen
2676259Sgreen#include "includes.h"
2776259Sgreen
28162852Sdes#include <sys/types.h>
29162852Sdes
3076259Sgreen#include <openssl/bn.h>
3176259Sgreen#include <openssl/evp.h>
3276259Sgreen
33162852Sdes#include <stdarg.h>
34162852Sdes#include <string.h>
35162852Sdes
3676259Sgreen#include "xmalloc.h"
3776259Sgreen#include "buffer.h"
3876259Sgreen#include "compat.h"
3976259Sgreen#include "log.h"
4076259Sgreen#include "key.h"
41262566Sdes#include "digest.h"
4276259Sgreen
4376259Sgreen#define INTBLOB_LEN	20
4476259Sgreen#define SIGBLOB_LEN	(2*INTBLOB_LEN)
4576259Sgreen
4676259Sgreenint
47126274Sdesssh_dss_sign(const Key *key, u_char **sigp, u_int *lenp,
48126274Sdes    const u_char *data, u_int datalen)
4976259Sgreen{
5076259Sgreen	DSA_SIG *sig;
51262566Sdes	u_char digest[SSH_DIGEST_MAX_LENGTH], sigblob[SIGBLOB_LEN];
52262566Sdes	u_int rlen, slen, len, dlen = ssh_digest_bytes(SSH_DIGEST_SHA1);
5376259Sgreen	Buffer b;
5476259Sgreen
55262566Sdes	if (key == NULL || key_type_plain(key->type) != KEY_DSA ||
56262566Sdes	    key->dsa == NULL) {
57262566Sdes		error("%s: no DSA key", __func__);
5876259Sgreen		return -1;
5976259Sgreen	}
6076259Sgreen
61262566Sdes	if (ssh_digest_memory(SSH_DIGEST_SHA1, data, datalen,
62262566Sdes	    digest, sizeof(digest)) != 0) {
63262566Sdes		error("%s: ssh_digest_memory failed", __func__);
64262566Sdes		return -1;
65262566Sdes	}
66262566Sdes
6776259Sgreen	sig = DSA_do_sign(digest, dlen, key->dsa);
68264377Sdes	explicit_bzero(digest, sizeof(digest));
6992555Sdes
7076259Sgreen	if (sig == NULL) {
7192555Sdes		error("ssh_dss_sign: sign failed");
7292555Sdes		return -1;
7376259Sgreen	}
7476259Sgreen
7576259Sgreen	rlen = BN_num_bytes(sig->r);
7676259Sgreen	slen = BN_num_bytes(sig->s);
7776259Sgreen	if (rlen > INTBLOB_LEN || slen > INTBLOB_LEN) {
7899060Sdes		error("bad sig size %u %u", rlen, slen);
7976259Sgreen		DSA_SIG_free(sig);
8076259Sgreen		return -1;
8176259Sgreen	}
82264377Sdes	explicit_bzero(sigblob, SIGBLOB_LEN);
8376259Sgreen	BN_bn2bin(sig->r, sigblob+ SIGBLOB_LEN - INTBLOB_LEN - rlen);
8476259Sgreen	BN_bn2bin(sig->s, sigblob+ SIGBLOB_LEN - slen);
8576259Sgreen	DSA_SIG_free(sig);
8676259Sgreen
8776259Sgreen	if (datafellows & SSH_BUG_SIGBLOB) {
8876259Sgreen		if (lenp != NULL)
8976259Sgreen			*lenp = SIGBLOB_LEN;
90106121Sdes		if (sigp != NULL) {
91106121Sdes			*sigp = xmalloc(SIGBLOB_LEN);
92106121Sdes			memcpy(*sigp, sigblob, SIGBLOB_LEN);
93106121Sdes		}
9476259Sgreen	} else {
9576259Sgreen		/* ietf-drafts */
9676259Sgreen		buffer_init(&b);
9776259Sgreen		buffer_put_cstring(&b, "ssh-dss");
9876259Sgreen		buffer_put_string(&b, sigblob, SIGBLOB_LEN);
9976259Sgreen		len = buffer_len(&b);
10076259Sgreen		if (lenp != NULL)
10176259Sgreen			*lenp = len;
102106121Sdes		if (sigp != NULL) {
103106121Sdes			*sigp = xmalloc(len);
104106121Sdes			memcpy(*sigp, buffer_ptr(&b), len);
105106121Sdes		}
106106121Sdes		buffer_free(&b);
10776259Sgreen	}
10876259Sgreen	return 0;
10976259Sgreen}
11076259Sgreenint
111126274Sdesssh_dss_verify(const Key *key, const u_char *signature, u_int signaturelen,
112126274Sdes    const u_char *data, u_int datalen)
11376259Sgreen{
11476259Sgreen	DSA_SIG *sig;
115262566Sdes	u_char digest[SSH_DIGEST_MAX_LENGTH], *sigblob;
116262566Sdes	u_int len, dlen = ssh_digest_bytes(SSH_DIGEST_SHA1);
11792555Sdes	int rlen, ret;
11892555Sdes	Buffer b;
11976259Sgreen
120262566Sdes	if (key == NULL || key_type_plain(key->type) != KEY_DSA ||
121262566Sdes	    key->dsa == NULL) {
122262566Sdes		error("%s: no DSA key", __func__);
12376259Sgreen		return -1;
12476259Sgreen	}
12576259Sgreen
12676259Sgreen	/* fetch signature */
12776259Sgreen	if (datafellows & SSH_BUG_SIGBLOB) {
128126274Sdes		sigblob = xmalloc(signaturelen);
129126274Sdes		memcpy(sigblob, signature, signaturelen);
13076259Sgreen		len = signaturelen;
13176259Sgreen	} else {
13276259Sgreen		/* ietf-drafts */
13376259Sgreen		char *ktype;
13476259Sgreen		buffer_init(&b);
13592555Sdes		buffer_append(&b, signature, signaturelen);
136221420Sdes		ktype = buffer_get_cstring(&b, NULL);
13776259Sgreen		if (strcmp("ssh-dss", ktype) != 0) {
138262566Sdes			error("%s: cannot handle type %s", __func__, ktype);
13976259Sgreen			buffer_free(&b);
140255767Sdes			free(ktype);
14176259Sgreen			return -1;
14276259Sgreen		}
143255767Sdes		free(ktype);
14492555Sdes		sigblob = buffer_get_string(&b, &len);
14576259Sgreen		rlen = buffer_len(&b);
14692555Sdes		buffer_free(&b);
14792555Sdes		if (rlen != 0) {
148262566Sdes			error("%s: remaining bytes in signature %d",
149262566Sdes			    __func__, rlen);
150255767Sdes			free(sigblob);
15176259Sgreen			return -1;
15276259Sgreen		}
15376259Sgreen	}
15476259Sgreen
15576259Sgreen	if (len != SIGBLOB_LEN) {
15699060Sdes		fatal("bad sigbloblen %u != SIGBLOB_LEN", len);
15776259Sgreen	}
15876259Sgreen
15976259Sgreen	/* parse signature */
16092555Sdes	if ((sig = DSA_SIG_new()) == NULL)
161262566Sdes		fatal("%s: DSA_SIG_new failed", __func__);
16292555Sdes	if ((sig->r = BN_new()) == NULL)
163262566Sdes		fatal("%s: BN_new failed", __func__);
16492555Sdes	if ((sig->s = BN_new()) == NULL)
16592555Sdes		fatal("ssh_dss_verify: BN_new failed");
166164146Sdes	if ((BN_bin2bn(sigblob, INTBLOB_LEN, sig->r) == NULL) ||
167164146Sdes	    (BN_bin2bn(sigblob+ INTBLOB_LEN, INTBLOB_LEN, sig->s) == NULL))
168262566Sdes		fatal("%s: BN_bin2bn failed", __func__);
16976259Sgreen
170126274Sdes	/* clean up */
171264377Sdes	explicit_bzero(sigblob, len);
172255767Sdes	free(sigblob);
17376259Sgreen
17476259Sgreen	/* sha1 the data */
175262566Sdes	if (ssh_digest_memory(SSH_DIGEST_SHA1, data, datalen,
176262566Sdes	    digest, sizeof(digest)) != 0) {
177262566Sdes		error("%s: digest_memory failed", __func__);
178262566Sdes		return -1;
179262566Sdes	}
18076259Sgreen
18176259Sgreen	ret = DSA_do_verify(digest, dlen, sig, key->dsa);
182264377Sdes	explicit_bzero(digest, sizeof(digest));
18376259Sgreen
18476259Sgreen	DSA_SIG_free(sig);
18576259Sgreen
186262566Sdes	debug("%s: signature %s", __func__,
18792555Sdes	    ret == 1 ? "correct" : ret == 0 ? "incorrect" : "error");
18876259Sgreen	return ret;
18976259Sgreen}
190