blob: a23c383dc805465a3dfa96722674ae10d36aa213 [file] [log] [blame]
jsing@openbsd.org7cd31632018-02-07 02:06:50 +00001/* $OpenBSD: ssh-dss.c,v 1.37 2018/02/07 02:06:51 jsing 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 Miller72ef7c12015-01-15 02:21:31 +110028#ifdef WITH_OPENSSL
29
Damien Millerd7834352006-08-05 12:39:39 +100030#include <sys/types.h>
31
Ben Lindstrom226cfa02001-01-22 05:34:40 +000032#include <openssl/bn.h>
Damien Miller3f022b52014-08-19 11:32:34 +100033#include <openssl/dsa.h>
Ben Lindstrom226cfa02001-01-22 05:34:40 +000034#include <openssl/evp.h>
35
Damien Millerded319c2006-09-01 15:38:36 +100036#include <stdarg.h>
Damien Millere3476ed2006-07-24 14:13:33 +100037#include <string.h>
38
Damien Miller86687062014-07-02 15:28:02 +100039#include "sshbuf.h"
Damien Millera664e872000-04-16 11:52:47 +100040#include "compat.h"
Damien Miller86687062014-07-02 15:28:02 +100041#include "ssherr.h"
Damien Millerb3051d02014-01-10 10:58:53 +110042#include "digest.h"
Damien Miller86687062014-07-02 15:28:02 +100043#define SSHKEY_INTERNAL
44#include "sshkey.h"
Damien Millera664e872000-04-16 11:52:47 +100045
Damien Miller48f54b92018-09-13 12:13:50 +100046#include "openbsd-compat/openssl-compat.h"
47
Damien Millera664e872000-04-16 11:52:47 +100048#define INTBLOB_LEN 20
49#define SIGBLOB_LEN (2*INTBLOB_LEN)
50
Damien Millera664e872000-04-16 11:52:47 +100051int
Damien Miller86687062014-07-02 15:28:02 +100052ssh_dss_sign(const struct sshkey *key, u_char **sigp, size_t *lenp,
53 const u_char *data, size_t datalen, u_int compat)
Damien Millera664e872000-04-16 11:52:47 +100054{
Damien Miller86687062014-07-02 15:28:02 +100055 DSA_SIG *sig = NULL;
djm@openbsd.org482d23b2018-09-13 02:08:33 +000056 const BIGNUM *sig_r, *sig_s;
Damien Millerb3051d02014-01-10 10:58:53 +110057 u_char digest[SSH_DIGEST_MAX_LENGTH], sigblob[SIGBLOB_LEN];
Damien Miller86687062014-07-02 15:28:02 +100058 size_t rlen, slen, len, dlen = ssh_digest_bytes(SSH_DIGEST_SHA1);
59 struct sshbuf *b = NULL;
60 int ret = SSH_ERR_INVALID_ARGUMENT;
Damien Millera664e872000-04-16 11:52:47 +100061
Damien Miller86687062014-07-02 15:28:02 +100062 if (lenp != NULL)
63 *lenp = 0;
64 if (sigp != NULL)
65 *sigp = NULL;
Damien Miller3e192952013-12-29 17:47:50 +110066
Damien Miller86687062014-07-02 15:28:02 +100067 if (key == NULL || key->dsa == NULL ||
68 sshkey_type_plain(key->type) != KEY_DSA)
69 return SSH_ERR_INVALID_ARGUMENT;
70 if (dlen == 0)
71 return SSH_ERR_INTERNAL_ERROR;
Damien Millera664e872000-04-16 11:52:47 +100072
Damien Miller86687062014-07-02 15:28:02 +100073 if ((ret = ssh_digest_memory(SSH_DIGEST_SHA1, data, datalen,
74 digest, sizeof(digest))) != 0)
75 goto out;
Ben Lindstromc66d4362001-06-09 01:36:21 +000076
Damien Miller86687062014-07-02 15:28:02 +100077 if ((sig = DSA_do_sign(digest, dlen, key->dsa)) == NULL) {
78 ret = SSH_ERR_LIBCRYPTO_ERROR;
79 goto out;
Ben Lindstromc66d4362001-06-09 01:36:21 +000080 }
Damien Millera664e872000-04-16 11:52:47 +100081
djm@openbsd.org482d23b2018-09-13 02:08:33 +000082 DSA_SIG_get0(sig, &sig_r, &sig_s);
83 rlen = BN_num_bytes(sig_r);
84 slen = BN_num_bytes(sig_s);
Damien Millera664e872000-04-16 11:52:47 +100085 if (rlen > INTBLOB_LEN || slen > INTBLOB_LEN) {
Damien Miller86687062014-07-02 15:28:02 +100086 ret = SSH_ERR_INTERNAL_ERROR;
87 goto out;
Damien Millera664e872000-04-16 11:52:47 +100088 }
Damien Millera5103f42014-02-04 11:20:14 +110089 explicit_bzero(sigblob, SIGBLOB_LEN);
djm@openbsd.org482d23b2018-09-13 02:08:33 +000090 BN_bn2bin(sig_r, sigblob + SIGBLOB_LEN - INTBLOB_LEN - rlen);
91 BN_bn2bin(sig_s, sigblob + SIGBLOB_LEN - slen);
Damien Millera664e872000-04-16 11:52:47 +100092
djm@openbsd.org14b5c632018-01-23 05:27:21 +000093 if ((b = sshbuf_new()) == NULL) {
94 ret = SSH_ERR_ALLOC_FAIL;
95 goto out;
96 }
97 if ((ret = sshbuf_put_cstring(b, "ssh-dss")) != 0 ||
98 (ret = sshbuf_put_string(b, sigblob, SIGBLOB_LEN)) != 0)
99 goto out;
100
101 len = sshbuf_len(b);
102 if (sigp != NULL) {
103 if ((*sigp = malloc(len)) == NULL) {
Damien Miller86687062014-07-02 15:28:02 +1000104 ret = SSH_ERR_ALLOC_FAIL;
105 goto out;
106 }
djm@openbsd.org14b5c632018-01-23 05:27:21 +0000107 memcpy(*sigp, sshbuf_ptr(b), len);
Damien Millera664e872000-04-16 11:52:47 +1000108 }
djm@openbsd.org14b5c632018-01-23 05:27:21 +0000109 if (lenp != NULL)
110 *lenp = len;
111 ret = 0;
Damien Miller86687062014-07-02 15:28:02 +1000112 out:
113 explicit_bzero(digest, sizeof(digest));
jsing@openbsd.org7cd31632018-02-07 02:06:50 +0000114 DSA_SIG_free(sig);
mmcc@openbsd.org52d70782015-12-11 04:21:11 +0000115 sshbuf_free(b);
Damien Miller86687062014-07-02 15:28:02 +1000116 return ret;
Damien Millera664e872000-04-16 11:52:47 +1000117}
Damien Millera664e872000-04-16 11:52:47 +1000118
Damien Miller86687062014-07-02 15:28:02 +1000119int
120ssh_dss_verify(const struct sshkey *key,
121 const u_char *signature, size_t signaturelen,
122 const u_char *data, size_t datalen, u_int compat)
123{
124 DSA_SIG *sig = NULL;
djm@openbsd.org482d23b2018-09-13 02:08:33 +0000125 BIGNUM *sig_r = NULL, *sig_s = NULL;
Damien Miller86687062014-07-02 15:28:02 +1000126 u_char digest[SSH_DIGEST_MAX_LENGTH], *sigblob = NULL;
127 size_t len, dlen = ssh_digest_bytes(SSH_DIGEST_SHA1);
128 int ret = SSH_ERR_INTERNAL_ERROR;
129 struct sshbuf *b = NULL;
130 char *ktype = NULL;
131
132 if (key == NULL || key->dsa == NULL ||
djm@openbsd.orgb6e01402016-04-21 06:08:02 +0000133 sshkey_type_plain(key->type) != KEY_DSA ||
134 signature == NULL || signaturelen == 0)
Damien Miller86687062014-07-02 15:28:02 +1000135 return SSH_ERR_INVALID_ARGUMENT;
136 if (dlen == 0)
137 return SSH_ERR_INTERNAL_ERROR;
Damien Millera664e872000-04-16 11:52:47 +1000138
Damien Millera664e872000-04-16 11:52:47 +1000139 /* fetch signature */
djm@openbsd.org14b5c632018-01-23 05:27:21 +0000140 if ((b = sshbuf_from(signature, signaturelen)) == NULL)
141 return SSH_ERR_ALLOC_FAIL;
142 if (sshbuf_get_cstring(b, &ktype, NULL) != 0 ||
143 sshbuf_get_string(b, &sigblob, &len) != 0) {
144 ret = SSH_ERR_INVALID_FORMAT;
145 goto out;
146 }
147 if (strcmp("ssh-dss", ktype) != 0) {
148 ret = SSH_ERR_KEY_TYPE_MISMATCH;
149 goto out;
150 }
151 if (sshbuf_len(b) != 0) {
152 ret = SSH_ERR_UNEXPECTED_TRAILING_DATA;
153 goto out;
Damien Millera664e872000-04-16 11:52:47 +1000154 }
155
156 if (len != SIGBLOB_LEN) {
Damien Miller86687062014-07-02 15:28:02 +1000157 ret = SSH_ERR_INVALID_FORMAT;
158 goto out;
Damien Millera664e872000-04-16 11:52:47 +1000159 }
160
161 /* parse signature */
Damien Miller86687062014-07-02 15:28:02 +1000162 if ((sig = DSA_SIG_new()) == NULL ||
djm@openbsd.org482d23b2018-09-13 02:08:33 +0000163 (sig_r = BN_new()) == NULL ||
164 (sig_s = BN_new()) == NULL) {
Damien Miller86687062014-07-02 15:28:02 +1000165 ret = SSH_ERR_ALLOC_FAIL;
166 goto out;
167 }
djm@openbsd.org482d23b2018-09-13 02:08:33 +0000168 if ((BN_bin2bn(sigblob, INTBLOB_LEN, sig_r) == NULL) ||
169 (BN_bin2bn(sigblob + INTBLOB_LEN, INTBLOB_LEN, sig_s) == NULL)) {
Damien Miller86687062014-07-02 15:28:02 +1000170 ret = SSH_ERR_LIBCRYPTO_ERROR;
171 goto out;
Damien Millerb3051d02014-01-10 10:58:53 +1100172 }
djm@openbsd.org482d23b2018-09-13 02:08:33 +0000173 if (!DSA_SIG_set0(sig, sig_r, sig_s)) {
174 ret = SSH_ERR_LIBCRYPTO_ERROR;
175 goto out;
176 }
177 sig_r = sig_s = NULL; /* transferred */
Damien Millera664e872000-04-16 11:52:47 +1000178
Damien Miller86687062014-07-02 15:28:02 +1000179 /* sha1 the data */
180 if ((ret = ssh_digest_memory(SSH_DIGEST_SHA1, data, datalen,
181 digest, sizeof(digest))) != 0)
182 goto out;
183
184 switch (DSA_do_verify(digest, dlen, sig, key->dsa)) {
185 case 1:
186 ret = 0;
187 break;
188 case 0:
189 ret = SSH_ERR_SIGNATURE_INVALID;
190 goto out;
191 default:
192 ret = SSH_ERR_LIBCRYPTO_ERROR;
193 goto out;
194 }
195
196 out:
Damien Millera5103f42014-02-04 11:20:14 +1100197 explicit_bzero(digest, sizeof(digest));
jsing@openbsd.org7cd31632018-02-07 02:06:50 +0000198 DSA_SIG_free(sig);
djm@openbsd.org482d23b2018-09-13 02:08:33 +0000199 BN_clear_free(sig_r);
200 BN_clear_free(sig_s);
mmcc@openbsd.org52d70782015-12-11 04:21:11 +0000201 sshbuf_free(b);
mmcc@openbsd.orgd59ce082015-12-10 17:08:40 +0000202 free(ktype);
Damien Miller86687062014-07-02 15:28:02 +1000203 if (sigblob != NULL) {
204 explicit_bzero(sigblob, len);
205 free(sigblob);
206 }
Damien Millera664e872000-04-16 11:52:47 +1000207 return ret;
208}
Damien Miller72ef7c12015-01-15 02:21:31 +1100209#endif /* WITH_OPENSSL */