blob: a8d45a2b404ffc3861485c39f923025861117f0e [file] [log] [blame]
Damien Millere3476ed2006-07-24 14:13:33 +10001/* $OpenBSD: ssh-dss.c,v 1.22 2006/07/22 20:48:23 stevesk Exp $ */
Damien Millera664e872000-04-16 11:52:47 +10002/*
3 * Copyright (c) 2000 Markus Friedl. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
Damien Millera664e872000-04-16 11:52:47 +100013 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#include "includes.h"
Damien Millera664e872000-04-16 11:52:47 +100027
Ben Lindstrom226cfa02001-01-22 05:34:40 +000028#include <openssl/bn.h>
29#include <openssl/evp.h>
30
Damien Millere3476ed2006-07-24 14:13:33 +100031#include <string.h>
32
Damien Millera664e872000-04-16 11:52:47 +100033#include "xmalloc.h"
34#include "buffer.h"
35#include "bufaux.h"
36#include "compat.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000037#include "log.h"
Damien Millera664e872000-04-16 11:52:47 +100038#include "key.h"
39
40#define INTBLOB_LEN 20
41#define SIGBLOB_LEN (2*INTBLOB_LEN)
42
Damien Millera664e872000-04-16 11:52:47 +100043int
Damien Millerf58b58c2003-11-17 21:18:23 +110044ssh_dss_sign(const Key *key, u_char **sigp, u_int *lenp,
45 const u_char *data, u_int datalen)
Damien Millera664e872000-04-16 11:52:47 +100046{
Damien Millera664e872000-04-16 11:52:47 +100047 DSA_SIG *sig;
Ben Lindstrom80cb27d2002-03-05 01:33:36 +000048 const EVP_MD *evp_md = EVP_sha1();
Damien Millera664e872000-04-16 11:52:47 +100049 EVP_MD_CTX md;
Ben Lindstrom2bf759c2002-07-07 22:13:31 +000050 u_char digest[EVP_MAX_MD_SIZE], sigblob[SIGBLOB_LEN];
Ben Lindstromc66d4362001-06-09 01:36:21 +000051 u_int rlen, slen, len, dlen;
Damien Millera664e872000-04-16 11:52:47 +100052 Buffer b;
53
54 if (key == NULL || key->type != KEY_DSA || key->dsa == NULL) {
Damien Miller0bc1bd82000-11-13 22:57:25 +110055 error("ssh_dss_sign: no DSA key");
Damien Millera664e872000-04-16 11:52:47 +100056 return -1;
57 }
Damien Millera664e872000-04-16 11:52:47 +100058 EVP_DigestInit(&md, evp_md);
Damien Millereba71ba2000-04-29 23:57:08 +100059 EVP_DigestUpdate(&md, data, datalen);
Damien Millerc516e922002-02-05 11:53:43 +110060 EVP_DigestFinal(&md, digest, &dlen);
Damien Millera664e872000-04-16 11:52:47 +100061
Ben Lindstrom226cfa02001-01-22 05:34:40 +000062 sig = DSA_do_sign(digest, dlen, key->dsa);
Damien Millerc516e922002-02-05 11:53:43 +110063 memset(digest, 'd', sizeof(digest));
Ben Lindstromc66d4362001-06-09 01:36:21 +000064
Ben Lindstromc66d4362001-06-09 01:36:21 +000065 if (sig == NULL) {
66 error("ssh_dss_sign: sign failed");
67 return -1;
68 }
Damien Millera664e872000-04-16 11:52:47 +100069
70 rlen = BN_num_bytes(sig->r);
71 slen = BN_num_bytes(sig->s);
72 if (rlen > INTBLOB_LEN || slen > INTBLOB_LEN) {
Ben Lindstrom5c385522002-06-23 21:23:20 +000073 error("bad sig size %u %u", rlen, slen);
Damien Millera664e872000-04-16 11:52:47 +100074 DSA_SIG_free(sig);
75 return -1;
76 }
Damien Millera664e872000-04-16 11:52:47 +100077 memset(sigblob, 0, SIGBLOB_LEN);
78 BN_bn2bin(sig->r, sigblob+ SIGBLOB_LEN - INTBLOB_LEN - rlen);
79 BN_bn2bin(sig->s, sigblob+ SIGBLOB_LEN - slen);
80 DSA_SIG_free(sig);
81
Damien Miller30c3d422000-05-09 11:02:59 +100082 if (datafellows & SSH_BUG_SIGBLOB) {
Damien Millera664e872000-04-16 11:52:47 +100083 if (lenp != NULL)
84 *lenp = SIGBLOB_LEN;
Ben Lindstrom2bf759c2002-07-07 22:13:31 +000085 if (sigp != NULL) {
86 *sigp = xmalloc(SIGBLOB_LEN);
87 memcpy(*sigp, sigblob, SIGBLOB_LEN);
88 }
Damien Millera664e872000-04-16 11:52:47 +100089 } else {
90 /* ietf-drafts */
91 buffer_init(&b);
Damien Miller0bc1bd82000-11-13 22:57:25 +110092 buffer_put_cstring(&b, "ssh-dss");
Damien Millera664e872000-04-16 11:52:47 +100093 buffer_put_string(&b, sigblob, SIGBLOB_LEN);
94 len = buffer_len(&b);
Damien Millera664e872000-04-16 11:52:47 +100095 if (lenp != NULL)
96 *lenp = len;
Ben Lindstrom2bf759c2002-07-07 22:13:31 +000097 if (sigp != NULL) {
98 *sigp = xmalloc(len);
99 memcpy(*sigp, buffer_ptr(&b), len);
100 }
101 buffer_free(&b);
Damien Millera664e872000-04-16 11:52:47 +1000102 }
103 return 0;
104}
105int
Damien Millerf58b58c2003-11-17 21:18:23 +1100106ssh_dss_verify(const Key *key, const u_char *signature, u_int signaturelen,
107 const u_char *data, u_int datalen)
Damien Millera664e872000-04-16 11:52:47 +1000108{
Damien Millera664e872000-04-16 11:52:47 +1000109 DSA_SIG *sig;
Ben Lindstrom80cb27d2002-03-05 01:33:36 +0000110 const EVP_MD *evp_md = EVP_sha1();
Damien Millera664e872000-04-16 11:52:47 +1000111 EVP_MD_CTX md;
Damien Millerc516e922002-02-05 11:53:43 +1100112 u_char digest[EVP_MAX_MD_SIZE], *sigblob;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000113 u_int len, dlen;
Ben Lindstromc66d4362001-06-09 01:36:21 +0000114 int rlen, ret;
115 Buffer b;
Damien Millera664e872000-04-16 11:52:47 +1000116
117 if (key == NULL || key->type != KEY_DSA || key->dsa == NULL) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100118 error("ssh_dss_verify: no DSA key");
Damien Millera664e872000-04-16 11:52:47 +1000119 return -1;
120 }
121
Damien Millera664e872000-04-16 11:52:47 +1000122 /* fetch signature */
Damien Miller30c3d422000-05-09 11:02:59 +1000123 if (datafellows & SSH_BUG_SIGBLOB) {
Damien Millerf58b58c2003-11-17 21:18:23 +1100124 sigblob = xmalloc(signaturelen);
125 memcpy(sigblob, signature, signaturelen);
Damien Millera664e872000-04-16 11:52:47 +1000126 len = signaturelen;
127 } else {
128 /* ietf-drafts */
Damien Miller6536c7d2000-06-22 21:32:31 +1000129 char *ktype;
Damien Millera664e872000-04-16 11:52:47 +1000130 buffer_init(&b);
Ben Lindstrom9e0ddd42001-09-18 05:41:19 +0000131 buffer_append(&b, signature, signaturelen);
Damien Millera664e872000-04-16 11:52:47 +1000132 ktype = buffer_get_string(&b, NULL);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100133 if (strcmp("ssh-dss", ktype) != 0) {
134 error("ssh_dss_verify: cannot handle type %s", ktype);
Damien Miller6536c7d2000-06-22 21:32:31 +1000135 buffer_free(&b);
Damien Miller36e603d2001-11-12 11:03:35 +1100136 xfree(ktype);
Damien Miller6536c7d2000-06-22 21:32:31 +1000137 return -1;
138 }
Damien Miller36e603d2001-11-12 11:03:35 +1100139 xfree(ktype);
Ben Lindstrom9e0ddd42001-09-18 05:41:19 +0000140 sigblob = buffer_get_string(&b, &len);
Damien Millera664e872000-04-16 11:52:47 +1000141 rlen = buffer_len(&b);
Damien Miller36e603d2001-11-12 11:03:35 +1100142 buffer_free(&b);
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000143 if (rlen != 0) {
Damien Miller36e603d2001-11-12 11:03:35 +1100144 error("ssh_dss_verify: "
145 "remaining bytes in signature %d", rlen);
146 xfree(sigblob);
Damien Miller6536c7d2000-06-22 21:32:31 +1000147 return -1;
148 }
Damien Millera664e872000-04-16 11:52:47 +1000149 }
150
151 if (len != SIGBLOB_LEN) {
Ben Lindstrom5c385522002-06-23 21:23:20 +0000152 fatal("bad sigbloblen %u != SIGBLOB_LEN", len);
Damien Millera664e872000-04-16 11:52:47 +1000153 }
154
155 /* parse signature */
Damien Millerda755162002-01-22 23:09:22 +1100156 if ((sig = DSA_SIG_new()) == NULL)
157 fatal("ssh_dss_verify: DSA_SIG_new failed");
158 if ((sig->r = BN_new()) == NULL)
159 fatal("ssh_dss_verify: BN_new failed");
160 if ((sig->s = BN_new()) == NULL)
161 fatal("ssh_dss_verify: BN_new failed");
Damien Millera664e872000-04-16 11:52:47 +1000162 BN_bin2bn(sigblob, INTBLOB_LEN, sig->r);
163 BN_bin2bn(sigblob+ INTBLOB_LEN, INTBLOB_LEN, sig->s);
Damien Miller30c3d422000-05-09 11:02:59 +1000164
Damien Millerf58b58c2003-11-17 21:18:23 +1100165 /* clean up */
166 memset(sigblob, 0, len);
167 xfree(sigblob);
Kevin Stevesef4eea92001-02-05 12:42:17 +0000168
Damien Millereba71ba2000-04-29 23:57:08 +1000169 /* sha1 the data */
Damien Millera664e872000-04-16 11:52:47 +1000170 EVP_DigestInit(&md, evp_md);
Damien Millereba71ba2000-04-29 23:57:08 +1000171 EVP_DigestUpdate(&md, data, datalen);
Damien Millerc516e922002-02-05 11:53:43 +1100172 EVP_DigestFinal(&md, digest, &dlen);
Damien Millera664e872000-04-16 11:52:47 +1000173
Damien Miller0bc1bd82000-11-13 22:57:25 +1100174 ret = DSA_do_verify(digest, dlen, sig, key->dsa);
Damien Millerc516e922002-02-05 11:53:43 +1100175 memset(digest, 'd', sizeof(digest));
Damien Millera664e872000-04-16 11:52:47 +1000176
Damien Millera664e872000-04-16 11:52:47 +1000177 DSA_SIG_free(sig);
178
Ben Lindstromc66d4362001-06-09 01:36:21 +0000179 debug("ssh_dss_verify: signature %s",
180 ret == 1 ? "correct" : ret == 0 ? "incorrect" : "error");
Damien Millera664e872000-04-16 11:52:47 +1000181 return ret;
182}