blob: 9643d90d87ea41badc7b6a8195ba311464f3e428 [file] [log] [blame]
Damien Miller86687062014-07-02 15:28:02 +10001/* $OpenBSD: ssh-dss.c,v 1.32 2014/06/24 01:13:21 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>
Damien Miller3f022b52014-08-19 11:32:34 +100031#include <openssl/dsa.h>
Ben Lindstrom226cfa02001-01-22 05:34:40 +000032#include <openssl/evp.h>
33
Damien Millerded319c2006-09-01 15:38:36 +100034#include <stdarg.h>
Damien Millere3476ed2006-07-24 14:13:33 +100035#include <string.h>
36
Damien Miller86687062014-07-02 15:28:02 +100037#include "sshbuf.h"
Damien Millera664e872000-04-16 11:52:47 +100038#include "compat.h"
Damien Miller86687062014-07-02 15:28:02 +100039#include "ssherr.h"
Damien Millerb3051d02014-01-10 10:58:53 +110040#include "digest.h"
Damien Miller86687062014-07-02 15:28:02 +100041#define SSHKEY_INTERNAL
42#include "sshkey.h"
Damien Millera664e872000-04-16 11:52:47 +100043
44#define INTBLOB_LEN 20
45#define SIGBLOB_LEN (2*INTBLOB_LEN)
46
Damien Millera664e872000-04-16 11:52:47 +100047int
Damien Miller86687062014-07-02 15:28:02 +100048ssh_dss_sign(const struct sshkey *key, u_char **sigp, size_t *lenp,
49 const u_char *data, size_t datalen, u_int compat)
Damien Millera664e872000-04-16 11:52:47 +100050{
Damien Miller86687062014-07-02 15:28:02 +100051 DSA_SIG *sig = NULL;
Damien Millerb3051d02014-01-10 10:58:53 +110052 u_char digest[SSH_DIGEST_MAX_LENGTH], sigblob[SIGBLOB_LEN];
Damien Miller86687062014-07-02 15:28:02 +100053 size_t rlen, slen, len, dlen = ssh_digest_bytes(SSH_DIGEST_SHA1);
54 struct sshbuf *b = NULL;
55 int ret = SSH_ERR_INVALID_ARGUMENT;
Damien Millera664e872000-04-16 11:52:47 +100056
Damien Miller86687062014-07-02 15:28:02 +100057 if (lenp != NULL)
58 *lenp = 0;
59 if (sigp != NULL)
60 *sigp = NULL;
Damien Miller3e192952013-12-29 17:47:50 +110061
Damien Miller86687062014-07-02 15:28:02 +100062 if (key == NULL || key->dsa == NULL ||
63 sshkey_type_plain(key->type) != KEY_DSA)
64 return SSH_ERR_INVALID_ARGUMENT;
65 if (dlen == 0)
66 return SSH_ERR_INTERNAL_ERROR;
Damien Millera664e872000-04-16 11:52:47 +100067
Damien Miller86687062014-07-02 15:28:02 +100068 if ((ret = ssh_digest_memory(SSH_DIGEST_SHA1, data, datalen,
69 digest, sizeof(digest))) != 0)
70 goto out;
Ben Lindstromc66d4362001-06-09 01:36:21 +000071
Damien Miller86687062014-07-02 15:28:02 +100072 if ((sig = DSA_do_sign(digest, dlen, key->dsa)) == NULL) {
73 ret = SSH_ERR_LIBCRYPTO_ERROR;
74 goto out;
Ben Lindstromc66d4362001-06-09 01:36:21 +000075 }
Damien Millera664e872000-04-16 11:52:47 +100076
77 rlen = BN_num_bytes(sig->r);
78 slen = BN_num_bytes(sig->s);
79 if (rlen > INTBLOB_LEN || slen > INTBLOB_LEN) {
Damien Miller86687062014-07-02 15:28:02 +100080 ret = SSH_ERR_INTERNAL_ERROR;
81 goto out;
Damien Millera664e872000-04-16 11:52:47 +100082 }
Damien Millera5103f42014-02-04 11:20:14 +110083 explicit_bzero(sigblob, SIGBLOB_LEN);
Damien Miller86687062014-07-02 15:28:02 +100084 BN_bn2bin(sig->r, sigblob + SIGBLOB_LEN - INTBLOB_LEN - rlen);
85 BN_bn2bin(sig->s, sigblob + SIGBLOB_LEN - slen);
Damien Millera664e872000-04-16 11:52:47 +100086
Damien Miller86687062014-07-02 15:28:02 +100087 if (compat & SSH_BUG_SIGBLOB) {
Ben Lindstrom2bf759c2002-07-07 22:13:31 +000088 if (sigp != NULL) {
Damien Miller86687062014-07-02 15:28:02 +100089 if ((*sigp = malloc(SIGBLOB_LEN)) == NULL) {
90 ret = SSH_ERR_ALLOC_FAIL;
91 goto out;
92 }
Ben Lindstrom2bf759c2002-07-07 22:13:31 +000093 memcpy(*sigp, sigblob, SIGBLOB_LEN);
94 }
Damien Miller86687062014-07-02 15:28:02 +100095 if (lenp != NULL)
96 *lenp = SIGBLOB_LEN;
97 ret = 0;
Damien Millera664e872000-04-16 11:52:47 +100098 } else {
99 /* ietf-drafts */
Damien Miller86687062014-07-02 15:28:02 +1000100 if ((b = sshbuf_new()) == NULL) {
101 ret = SSH_ERR_ALLOC_FAIL;
102 goto out;
103 }
104 if ((ret = sshbuf_put_cstring(b, "ssh-dss")) != 0 ||
105 (ret = sshbuf_put_string(b, sigblob, SIGBLOB_LEN)) != 0)
106 goto out;
107 len = sshbuf_len(b);
108 if (sigp != NULL) {
109 if ((*sigp = malloc(len)) == NULL) {
110 ret = SSH_ERR_ALLOC_FAIL;
111 goto out;
112 }
113 memcpy(*sigp, sshbuf_ptr(b), len);
114 }
Damien Millera664e872000-04-16 11:52:47 +1000115 if (lenp != NULL)
116 *lenp = len;
Damien Miller86687062014-07-02 15:28:02 +1000117 ret = 0;
Damien Millera664e872000-04-16 11:52:47 +1000118 }
Damien Miller86687062014-07-02 15:28:02 +1000119 out:
120 explicit_bzero(digest, sizeof(digest));
121 if (sig != NULL)
122 DSA_SIG_free(sig);
123 if (b != NULL)
124 sshbuf_free(b);
125 return ret;
Damien Millera664e872000-04-16 11:52:47 +1000126}
Damien Millera664e872000-04-16 11:52:47 +1000127
Damien Miller86687062014-07-02 15:28:02 +1000128int
129ssh_dss_verify(const struct sshkey *key,
130 const u_char *signature, size_t signaturelen,
131 const u_char *data, size_t datalen, u_int compat)
132{
133 DSA_SIG *sig = NULL;
134 u_char digest[SSH_DIGEST_MAX_LENGTH], *sigblob = NULL;
135 size_t len, dlen = ssh_digest_bytes(SSH_DIGEST_SHA1);
136 int ret = SSH_ERR_INTERNAL_ERROR;
137 struct sshbuf *b = NULL;
138 char *ktype = NULL;
139
140 if (key == NULL || key->dsa == NULL ||
141 sshkey_type_plain(key->type) != KEY_DSA)
142 return SSH_ERR_INVALID_ARGUMENT;
143 if (dlen == 0)
144 return SSH_ERR_INTERNAL_ERROR;
Damien Millera664e872000-04-16 11:52:47 +1000145
Damien Millera664e872000-04-16 11:52:47 +1000146 /* fetch signature */
Damien Miller86687062014-07-02 15:28:02 +1000147 if (compat & SSH_BUG_SIGBLOB) {
148 if ((sigblob = malloc(signaturelen)) == NULL)
149 return SSH_ERR_ALLOC_FAIL;
Damien Millerf58b58c2003-11-17 21:18:23 +1100150 memcpy(sigblob, signature, signaturelen);
Damien Millera664e872000-04-16 11:52:47 +1000151 len = signaturelen;
152 } else {
153 /* ietf-drafts */
Damien Miller86687062014-07-02 15:28:02 +1000154 if ((b = sshbuf_from(signature, signaturelen)) == NULL)
155 return SSH_ERR_ALLOC_FAIL;
156 if (sshbuf_get_cstring(b, &ktype, NULL) != 0 ||
157 sshbuf_get_string(b, &sigblob, &len) != 0) {
158 ret = SSH_ERR_INVALID_FORMAT;
159 goto out;
Damien Miller6536c7d2000-06-22 21:32:31 +1000160 }
Damien Miller86687062014-07-02 15:28:02 +1000161 if (strcmp("ssh-dss", ktype) != 0) {
162 ret = SSH_ERR_KEY_TYPE_MISMATCH;
163 goto out;
164 }
165 if (sshbuf_len(b) != 0) {
166 ret = SSH_ERR_UNEXPECTED_TRAILING_DATA;
167 goto out;
Damien Miller6536c7d2000-06-22 21:32:31 +1000168 }
Damien Millera664e872000-04-16 11:52:47 +1000169 }
170
171 if (len != SIGBLOB_LEN) {
Damien Miller86687062014-07-02 15:28:02 +1000172 ret = SSH_ERR_INVALID_FORMAT;
173 goto out;
Damien Millera664e872000-04-16 11:52:47 +1000174 }
175
176 /* parse signature */
Damien Miller86687062014-07-02 15:28:02 +1000177 if ((sig = DSA_SIG_new()) == NULL ||
178 (sig->r = BN_new()) == NULL ||
179 (sig->s = BN_new()) == NULL) {
180 ret = SSH_ERR_ALLOC_FAIL;
181 goto out;
182 }
Darren Tucker0bc85572006-11-07 23:14:41 +1100183 if ((BN_bin2bn(sigblob, INTBLOB_LEN, sig->r) == NULL) ||
Damien Miller86687062014-07-02 15:28:02 +1000184 (BN_bin2bn(sigblob+ INTBLOB_LEN, INTBLOB_LEN, sig->s) == NULL)) {
185 ret = SSH_ERR_LIBCRYPTO_ERROR;
186 goto out;
Damien Millerb3051d02014-01-10 10:58:53 +1100187 }
Damien Millera664e872000-04-16 11:52:47 +1000188
Damien Miller86687062014-07-02 15:28:02 +1000189 /* sha1 the data */
190 if ((ret = ssh_digest_memory(SSH_DIGEST_SHA1, data, datalen,
191 digest, sizeof(digest))) != 0)
192 goto out;
193
194 switch (DSA_do_verify(digest, dlen, sig, key->dsa)) {
195 case 1:
196 ret = 0;
197 break;
198 case 0:
199 ret = SSH_ERR_SIGNATURE_INVALID;
200 goto out;
201 default:
202 ret = SSH_ERR_LIBCRYPTO_ERROR;
203 goto out;
204 }
205
206 out:
Damien Millera5103f42014-02-04 11:20:14 +1100207 explicit_bzero(digest, sizeof(digest));
Damien Miller86687062014-07-02 15:28:02 +1000208 if (sig != NULL)
209 DSA_SIG_free(sig);
210 if (b != NULL)
211 sshbuf_free(b);
212 if (ktype != NULL)
213 free(ktype);
214 if (sigblob != NULL) {
215 explicit_bzero(sigblob, len);
216 free(sigblob);
217 }
Damien Millera664e872000-04-16 11:52:47 +1000218 return ret;
219}