blob: 6b4abcb7de5d87d7787f5de75978ec6a5f511ef2 [file] [log] [blame]
Damien Millera5103f42014-02-04 11:20:14 +11001/* $OpenBSD: ssh-dss.c,v 1.31 2014/02/02 03:44:31 djm 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
Damien Millerd7834352006-08-05 12:39:39 +100028#include <sys/types.h>
29
Ben Lindstrom226cfa02001-01-22 05:34:40 +000030#include <openssl/bn.h>
31#include <openssl/evp.h>
32
Damien Millerded319c2006-09-01 15:38:36 +100033#include <stdarg.h>
Damien Millere3476ed2006-07-24 14:13:33 +100034#include <string.h>
35
Damien Millera664e872000-04-16 11:52:47 +100036#include "xmalloc.h"
37#include "buffer.h"
Damien Millera664e872000-04-16 11:52:47 +100038#include "compat.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000039#include "log.h"
Damien Millera664e872000-04-16 11:52:47 +100040#include "key.h"
Damien Millerb3051d02014-01-10 10:58:53 +110041#include "digest.h"
Damien Millera664e872000-04-16 11:52:47 +100042
43#define INTBLOB_LEN 20
44#define SIGBLOB_LEN (2*INTBLOB_LEN)
45
Damien Millera664e872000-04-16 11:52:47 +100046int
Damien Millerf58b58c2003-11-17 21:18:23 +110047ssh_dss_sign(const Key *key, u_char **sigp, u_int *lenp,
48 const u_char *data, u_int datalen)
Damien Millera664e872000-04-16 11:52:47 +100049{
Damien Millera664e872000-04-16 11:52:47 +100050 DSA_SIG *sig;
Damien Millerb3051d02014-01-10 10:58:53 +110051 u_char digest[SSH_DIGEST_MAX_LENGTH], sigblob[SIGBLOB_LEN];
52 u_int rlen, slen, len, dlen = ssh_digest_bytes(SSH_DIGEST_SHA1);
Damien Millera664e872000-04-16 11:52:47 +100053 Buffer b;
54
Damien Miller3e192952013-12-29 17:47:50 +110055 if (key == NULL || key_type_plain(key->type) != KEY_DSA ||
56 key->dsa == NULL) {
57 error("%s: no DSA key", __func__);
Damien Millera664e872000-04-16 11:52:47 +100058 return -1;
59 }
Damien Miller3e192952013-12-29 17:47:50 +110060
Damien Millerb3051d02014-01-10 10:58:53 +110061 if (ssh_digest_memory(SSH_DIGEST_SHA1, data, datalen,
62 digest, sizeof(digest)) != 0) {
63 error("%s: ssh_digest_memory failed", __func__);
64 return -1;
65 }
Damien Millera664e872000-04-16 11:52:47 +100066
Ben Lindstrom226cfa02001-01-22 05:34:40 +000067 sig = DSA_do_sign(digest, dlen, key->dsa);
Damien Millera5103f42014-02-04 11:20:14 +110068 explicit_bzero(digest, sizeof(digest));
Ben Lindstromc66d4362001-06-09 01:36:21 +000069
Ben Lindstromc66d4362001-06-09 01:36:21 +000070 if (sig == NULL) {
71 error("ssh_dss_sign: sign failed");
72 return -1;
73 }
Damien Millera664e872000-04-16 11:52:47 +100074
75 rlen = BN_num_bytes(sig->r);
76 slen = BN_num_bytes(sig->s);
77 if (rlen > INTBLOB_LEN || slen > INTBLOB_LEN) {
Ben Lindstrom5c385522002-06-23 21:23:20 +000078 error("bad sig size %u %u", rlen, slen);
Damien Millera664e872000-04-16 11:52:47 +100079 DSA_SIG_free(sig);
80 return -1;
81 }
Damien Millera5103f42014-02-04 11:20:14 +110082 explicit_bzero(sigblob, SIGBLOB_LEN);
Damien Millera664e872000-04-16 11:52:47 +100083 BN_bn2bin(sig->r, sigblob+ SIGBLOB_LEN - INTBLOB_LEN - rlen);
84 BN_bn2bin(sig->s, sigblob+ SIGBLOB_LEN - slen);
85 DSA_SIG_free(sig);
86
Damien Miller30c3d422000-05-09 11:02:59 +100087 if (datafellows & SSH_BUG_SIGBLOB) {
Damien Millera664e872000-04-16 11:52:47 +100088 if (lenp != NULL)
89 *lenp = SIGBLOB_LEN;
Ben Lindstrom2bf759c2002-07-07 22:13:31 +000090 if (sigp != NULL) {
91 *sigp = xmalloc(SIGBLOB_LEN);
92 memcpy(*sigp, sigblob, SIGBLOB_LEN);
93 }
Damien Millera664e872000-04-16 11:52:47 +100094 } else {
95 /* ietf-drafts */
96 buffer_init(&b);
Damien Miller0bc1bd82000-11-13 22:57:25 +110097 buffer_put_cstring(&b, "ssh-dss");
Damien Millera664e872000-04-16 11:52:47 +100098 buffer_put_string(&b, sigblob, SIGBLOB_LEN);
99 len = buffer_len(&b);
Damien Millera664e872000-04-16 11:52:47 +1000100 if (lenp != NULL)
101 *lenp = len;
Ben Lindstrom2bf759c2002-07-07 22:13:31 +0000102 if (sigp != NULL) {
103 *sigp = xmalloc(len);
104 memcpy(*sigp, buffer_ptr(&b), len);
105 }
106 buffer_free(&b);
Damien Millera664e872000-04-16 11:52:47 +1000107 }
108 return 0;
109}
110int
Damien Millerf58b58c2003-11-17 21:18:23 +1100111ssh_dss_verify(const Key *key, const u_char *signature, u_int signaturelen,
112 const u_char *data, u_int datalen)
Damien Millera664e872000-04-16 11:52:47 +1000113{
Damien Millera664e872000-04-16 11:52:47 +1000114 DSA_SIG *sig;
Damien Millerb3051d02014-01-10 10:58:53 +1100115 u_char digest[SSH_DIGEST_MAX_LENGTH], *sigblob;
116 u_int len, dlen = ssh_digest_bytes(SSH_DIGEST_SHA1);
Ben Lindstromc66d4362001-06-09 01:36:21 +0000117 int rlen, ret;
118 Buffer b;
Damien Millera664e872000-04-16 11:52:47 +1000119
Damien Miller3e192952013-12-29 17:47:50 +1100120 if (key == NULL || key_type_plain(key->type) != KEY_DSA ||
121 key->dsa == NULL) {
122 error("%s: no DSA key", __func__);
Damien Millera664e872000-04-16 11:52:47 +1000123 return -1;
124 }
125
Damien Millera664e872000-04-16 11:52:47 +1000126 /* fetch signature */
Damien Miller30c3d422000-05-09 11:02:59 +1000127 if (datafellows & SSH_BUG_SIGBLOB) {
Damien Millerf58b58c2003-11-17 21:18:23 +1100128 sigblob = xmalloc(signaturelen);
129 memcpy(sigblob, signature, signaturelen);
Damien Millera664e872000-04-16 11:52:47 +1000130 len = signaturelen;
131 } else {
132 /* ietf-drafts */
Damien Miller6536c7d2000-06-22 21:32:31 +1000133 char *ktype;
Damien Millera664e872000-04-16 11:52:47 +1000134 buffer_init(&b);
Ben Lindstrom9e0ddd42001-09-18 05:41:19 +0000135 buffer_append(&b, signature, signaturelen);
Damien Millerda108ec2010-08-31 22:36:39 +1000136 ktype = buffer_get_cstring(&b, NULL);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100137 if (strcmp("ssh-dss", ktype) != 0) {
Damien Miller3e192952013-12-29 17:47:50 +1100138 error("%s: cannot handle type %s", __func__, ktype);
Damien Miller6536c7d2000-06-22 21:32:31 +1000139 buffer_free(&b);
Darren Tuckera627d422013-06-02 07:31:17 +1000140 free(ktype);
Damien Miller6536c7d2000-06-22 21:32:31 +1000141 return -1;
142 }
Darren Tuckera627d422013-06-02 07:31:17 +1000143 free(ktype);
Ben Lindstrom9e0ddd42001-09-18 05:41:19 +0000144 sigblob = buffer_get_string(&b, &len);
Damien Millera664e872000-04-16 11:52:47 +1000145 rlen = buffer_len(&b);
Damien Miller36e603d2001-11-12 11:03:35 +1100146 buffer_free(&b);
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000147 if (rlen != 0) {
Damien Miller3e192952013-12-29 17:47:50 +1100148 error("%s: remaining bytes in signature %d",
149 __func__, rlen);
Darren Tuckera627d422013-06-02 07:31:17 +1000150 free(sigblob);
Damien Miller6536c7d2000-06-22 21:32:31 +1000151 return -1;
152 }
Damien Millera664e872000-04-16 11:52:47 +1000153 }
154
155 if (len != SIGBLOB_LEN) {
Ben Lindstrom5c385522002-06-23 21:23:20 +0000156 fatal("bad sigbloblen %u != SIGBLOB_LEN", len);
Damien Millera664e872000-04-16 11:52:47 +1000157 }
158
159 /* parse signature */
Damien Millerda755162002-01-22 23:09:22 +1100160 if ((sig = DSA_SIG_new()) == NULL)
Damien Miller3e192952013-12-29 17:47:50 +1100161 fatal("%s: DSA_SIG_new failed", __func__);
Damien Millerda755162002-01-22 23:09:22 +1100162 if ((sig->r = BN_new()) == NULL)
Damien Miller3e192952013-12-29 17:47:50 +1100163 fatal("%s: BN_new failed", __func__);
Damien Millerda755162002-01-22 23:09:22 +1100164 if ((sig->s = BN_new()) == NULL)
165 fatal("ssh_dss_verify: BN_new failed");
Darren Tucker0bc85572006-11-07 23:14:41 +1100166 if ((BN_bin2bn(sigblob, INTBLOB_LEN, sig->r) == NULL) ||
167 (BN_bin2bn(sigblob+ INTBLOB_LEN, INTBLOB_LEN, sig->s) == NULL))
Damien Miller3e192952013-12-29 17:47:50 +1100168 fatal("%s: BN_bin2bn failed", __func__);
Damien Miller30c3d422000-05-09 11:02:59 +1000169
Damien Millerf58b58c2003-11-17 21:18:23 +1100170 /* clean up */
Damien Millera5103f42014-02-04 11:20:14 +1100171 explicit_bzero(sigblob, len);
Darren Tuckera627d422013-06-02 07:31:17 +1000172 free(sigblob);
Kevin Stevesef4eea92001-02-05 12:42:17 +0000173
Damien Millereba71ba2000-04-29 23:57:08 +1000174 /* sha1 the data */
Damien Millerb3051d02014-01-10 10:58:53 +1100175 if (ssh_digest_memory(SSH_DIGEST_SHA1, data, datalen,
176 digest, sizeof(digest)) != 0) {
177 error("%s: digest_memory failed", __func__);
178 return -1;
179 }
Damien Millera664e872000-04-16 11:52:47 +1000180
Damien Miller0bc1bd82000-11-13 22:57:25 +1100181 ret = DSA_do_verify(digest, dlen, sig, key->dsa);
Damien Millera5103f42014-02-04 11:20:14 +1100182 explicit_bzero(digest, sizeof(digest));
Damien Millera664e872000-04-16 11:52:47 +1000183
Damien Millera664e872000-04-16 11:52:47 +1000184 DSA_SIG_free(sig);
185
Damien Miller3e192952013-12-29 17:47:50 +1100186 debug("%s: signature %s", __func__,
Ben Lindstromc66d4362001-06-09 01:36:21 +0000187 ret == 1 ? "correct" : ret == 0 ? "incorrect" : "error");
Damien Millera664e872000-04-16 11:52:47 +1000188 return ret;
189}