blob: 02d1ec2d663e3c1cf20f3b9e530dfa211a8b62cf [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>
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 Miller86687062014-07-02 15:28:02 +100036#include "sshbuf.h"
Damien Millera664e872000-04-16 11:52:47 +100037#include "compat.h"
Damien Miller86687062014-07-02 15:28:02 +100038#include "ssherr.h"
Damien Millerb3051d02014-01-10 10:58:53 +110039#include "digest.h"
Damien Miller86687062014-07-02 15:28:02 +100040#define SSHKEY_INTERNAL
41#include "sshkey.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 Miller86687062014-07-02 15:28:02 +100047ssh_dss_sign(const struct sshkey *key, u_char **sigp, size_t *lenp,
48 const u_char *data, size_t datalen, u_int compat)
Damien Millera664e872000-04-16 11:52:47 +100049{
Damien Miller86687062014-07-02 15:28:02 +100050 DSA_SIG *sig = NULL;
Damien Millerb3051d02014-01-10 10:58:53 +110051 u_char digest[SSH_DIGEST_MAX_LENGTH], sigblob[SIGBLOB_LEN];
Damien Miller86687062014-07-02 15:28:02 +100052 size_t rlen, slen, len, dlen = ssh_digest_bytes(SSH_DIGEST_SHA1);
53 struct sshbuf *b = NULL;
54 int ret = SSH_ERR_INVALID_ARGUMENT;
Damien Millera664e872000-04-16 11:52:47 +100055
Damien Miller86687062014-07-02 15:28:02 +100056 if (lenp != NULL)
57 *lenp = 0;
58 if (sigp != NULL)
59 *sigp = NULL;
Damien Miller3e192952013-12-29 17:47:50 +110060
Damien Miller86687062014-07-02 15:28:02 +100061 if (key == NULL || key->dsa == NULL ||
62 sshkey_type_plain(key->type) != KEY_DSA)
63 return SSH_ERR_INVALID_ARGUMENT;
64 if (dlen == 0)
65 return SSH_ERR_INTERNAL_ERROR;
Damien Millera664e872000-04-16 11:52:47 +100066
Damien Miller86687062014-07-02 15:28:02 +100067 if ((ret = ssh_digest_memory(SSH_DIGEST_SHA1, data, datalen,
68 digest, sizeof(digest))) != 0)
69 goto out;
Ben Lindstromc66d4362001-06-09 01:36:21 +000070
Damien Miller86687062014-07-02 15:28:02 +100071 if ((sig = DSA_do_sign(digest, dlen, key->dsa)) == NULL) {
72 ret = SSH_ERR_LIBCRYPTO_ERROR;
73 goto out;
Ben Lindstromc66d4362001-06-09 01:36:21 +000074 }
Damien Millera664e872000-04-16 11:52:47 +100075
76 rlen = BN_num_bytes(sig->r);
77 slen = BN_num_bytes(sig->s);
78 if (rlen > INTBLOB_LEN || slen > INTBLOB_LEN) {
Damien Miller86687062014-07-02 15:28:02 +100079 ret = SSH_ERR_INTERNAL_ERROR;
80 goto out;
Damien Millera664e872000-04-16 11:52:47 +100081 }
Damien Millera5103f42014-02-04 11:20:14 +110082 explicit_bzero(sigblob, SIGBLOB_LEN);
Damien Miller86687062014-07-02 15:28:02 +100083 BN_bn2bin(sig->r, sigblob + SIGBLOB_LEN - INTBLOB_LEN - rlen);
84 BN_bn2bin(sig->s, sigblob + SIGBLOB_LEN - slen);
Damien Millera664e872000-04-16 11:52:47 +100085
Damien Miller86687062014-07-02 15:28:02 +100086 if (compat & SSH_BUG_SIGBLOB) {
Ben Lindstrom2bf759c2002-07-07 22:13:31 +000087 if (sigp != NULL) {
Damien Miller86687062014-07-02 15:28:02 +100088 if ((*sigp = malloc(SIGBLOB_LEN)) == NULL) {
89 ret = SSH_ERR_ALLOC_FAIL;
90 goto out;
91 }
Ben Lindstrom2bf759c2002-07-07 22:13:31 +000092 memcpy(*sigp, sigblob, SIGBLOB_LEN);
93 }
Damien Miller86687062014-07-02 15:28:02 +100094 if (lenp != NULL)
95 *lenp = SIGBLOB_LEN;
96 ret = 0;
Damien Millera664e872000-04-16 11:52:47 +100097 } else {
98 /* ietf-drafts */
Damien Miller86687062014-07-02 15:28:02 +100099 if ((b = sshbuf_new()) == NULL) {
100 ret = SSH_ERR_ALLOC_FAIL;
101 goto out;
102 }
103 if ((ret = sshbuf_put_cstring(b, "ssh-dss")) != 0 ||
104 (ret = sshbuf_put_string(b, sigblob, SIGBLOB_LEN)) != 0)
105 goto out;
106 len = sshbuf_len(b);
107 if (sigp != NULL) {
108 if ((*sigp = malloc(len)) == NULL) {
109 ret = SSH_ERR_ALLOC_FAIL;
110 goto out;
111 }
112 memcpy(*sigp, sshbuf_ptr(b), len);
113 }
Damien Millera664e872000-04-16 11:52:47 +1000114 if (lenp != NULL)
115 *lenp = len;
Damien Miller86687062014-07-02 15:28:02 +1000116 ret = 0;
Damien Millera664e872000-04-16 11:52:47 +1000117 }
Damien Miller86687062014-07-02 15:28:02 +1000118 out:
119 explicit_bzero(digest, sizeof(digest));
120 if (sig != NULL)
121 DSA_SIG_free(sig);
122 if (b != NULL)
123 sshbuf_free(b);
124 return ret;
Damien Millera664e872000-04-16 11:52:47 +1000125}
Damien Millera664e872000-04-16 11:52:47 +1000126
Damien Miller86687062014-07-02 15:28:02 +1000127int
128ssh_dss_verify(const struct sshkey *key,
129 const u_char *signature, size_t signaturelen,
130 const u_char *data, size_t datalen, u_int compat)
131{
132 DSA_SIG *sig = NULL;
133 u_char digest[SSH_DIGEST_MAX_LENGTH], *sigblob = NULL;
134 size_t len, dlen = ssh_digest_bytes(SSH_DIGEST_SHA1);
135 int ret = SSH_ERR_INTERNAL_ERROR;
136 struct sshbuf *b = NULL;
137 char *ktype = NULL;
138
139 if (key == NULL || key->dsa == NULL ||
140 sshkey_type_plain(key->type) != KEY_DSA)
141 return SSH_ERR_INVALID_ARGUMENT;
142 if (dlen == 0)
143 return SSH_ERR_INTERNAL_ERROR;
Damien Millera664e872000-04-16 11:52:47 +1000144
Damien Millera664e872000-04-16 11:52:47 +1000145 /* fetch signature */
Damien Miller86687062014-07-02 15:28:02 +1000146 if (compat & SSH_BUG_SIGBLOB) {
147 if ((sigblob = malloc(signaturelen)) == NULL)
148 return SSH_ERR_ALLOC_FAIL;
Damien Millerf58b58c2003-11-17 21:18:23 +1100149 memcpy(sigblob, signature, signaturelen);
Damien Millera664e872000-04-16 11:52:47 +1000150 len = signaturelen;
151 } else {
152 /* ietf-drafts */
Damien Miller86687062014-07-02 15:28:02 +1000153 if ((b = sshbuf_from(signature, signaturelen)) == NULL)
154 return SSH_ERR_ALLOC_FAIL;
155 if (sshbuf_get_cstring(b, &ktype, NULL) != 0 ||
156 sshbuf_get_string(b, &sigblob, &len) != 0) {
157 ret = SSH_ERR_INVALID_FORMAT;
158 goto out;
Damien Miller6536c7d2000-06-22 21:32:31 +1000159 }
Damien Miller86687062014-07-02 15:28:02 +1000160 if (strcmp("ssh-dss", ktype) != 0) {
161 ret = SSH_ERR_KEY_TYPE_MISMATCH;
162 goto out;
163 }
164 if (sshbuf_len(b) != 0) {
165 ret = SSH_ERR_UNEXPECTED_TRAILING_DATA;
166 goto out;
Damien Miller6536c7d2000-06-22 21:32:31 +1000167 }
Damien Millera664e872000-04-16 11:52:47 +1000168 }
169
170 if (len != SIGBLOB_LEN) {
Damien Miller86687062014-07-02 15:28:02 +1000171 ret = SSH_ERR_INVALID_FORMAT;
172 goto out;
Damien Millera664e872000-04-16 11:52:47 +1000173 }
174
175 /* parse signature */
Damien Miller86687062014-07-02 15:28:02 +1000176 if ((sig = DSA_SIG_new()) == NULL ||
177 (sig->r = BN_new()) == NULL ||
178 (sig->s = BN_new()) == NULL) {
179 ret = SSH_ERR_ALLOC_FAIL;
180 goto out;
181 }
Darren Tucker0bc85572006-11-07 23:14:41 +1100182 if ((BN_bin2bn(sigblob, INTBLOB_LEN, sig->r) == NULL) ||
Damien Miller86687062014-07-02 15:28:02 +1000183 (BN_bin2bn(sigblob+ INTBLOB_LEN, INTBLOB_LEN, sig->s) == NULL)) {
184 ret = SSH_ERR_LIBCRYPTO_ERROR;
185 goto out;
Damien Millerb3051d02014-01-10 10:58:53 +1100186 }
Damien Millera664e872000-04-16 11:52:47 +1000187
Damien Miller86687062014-07-02 15:28:02 +1000188 /* sha1 the data */
189 if ((ret = ssh_digest_memory(SSH_DIGEST_SHA1, data, datalen,
190 digest, sizeof(digest))) != 0)
191 goto out;
192
193 switch (DSA_do_verify(digest, dlen, sig, key->dsa)) {
194 case 1:
195 ret = 0;
196 break;
197 case 0:
198 ret = SSH_ERR_SIGNATURE_INVALID;
199 goto out;
200 default:
201 ret = SSH_ERR_LIBCRYPTO_ERROR;
202 goto out;
203 }
204
205 out:
Damien Millera5103f42014-02-04 11:20:14 +1100206 explicit_bzero(digest, sizeof(digest));
Damien Miller86687062014-07-02 15:28:02 +1000207 if (sig != NULL)
208 DSA_SIG_free(sig);
209 if (b != NULL)
210 sshbuf_free(b);
211 if (ktype != NULL)
212 free(ktype);
213 if (sigblob != NULL) {
214 explicit_bzero(sigblob, len);
215 free(sigblob);
216 }
Damien Millera664e872000-04-16 11:52:47 +1000217 return ret;
218}