blob: cda498a877608af3373a371436aeaec796fd87bd [file] [log] [blame]
djm@openbsd.org14b5c632018-01-23 05:27:21 +00001/* $OpenBSD: ssh-dss.c,v 1.36 2018/01/23 05:27: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 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
46#define INTBLOB_LEN 20
47#define SIGBLOB_LEN (2*INTBLOB_LEN)
48
Damien Millera664e872000-04-16 11:52:47 +100049int
Damien Miller86687062014-07-02 15:28:02 +100050ssh_dss_sign(const struct sshkey *key, u_char **sigp, size_t *lenp,
51 const u_char *data, size_t datalen, u_int compat)
Damien Millera664e872000-04-16 11:52:47 +100052{
Damien Miller86687062014-07-02 15:28:02 +100053 DSA_SIG *sig = NULL;
Damien Millerb3051d02014-01-10 10:58:53 +110054 u_char digest[SSH_DIGEST_MAX_LENGTH], sigblob[SIGBLOB_LEN];
Damien Miller86687062014-07-02 15:28:02 +100055 size_t rlen, slen, len, dlen = ssh_digest_bytes(SSH_DIGEST_SHA1);
56 struct sshbuf *b = NULL;
57 int ret = SSH_ERR_INVALID_ARGUMENT;
Damien Millera664e872000-04-16 11:52:47 +100058
Damien Miller86687062014-07-02 15:28:02 +100059 if (lenp != NULL)
60 *lenp = 0;
61 if (sigp != NULL)
62 *sigp = NULL;
Damien Miller3e192952013-12-29 17:47:50 +110063
Damien Miller86687062014-07-02 15:28:02 +100064 if (key == NULL || key->dsa == NULL ||
65 sshkey_type_plain(key->type) != KEY_DSA)
66 return SSH_ERR_INVALID_ARGUMENT;
67 if (dlen == 0)
68 return SSH_ERR_INTERNAL_ERROR;
Damien Millera664e872000-04-16 11:52:47 +100069
Damien Miller86687062014-07-02 15:28:02 +100070 if ((ret = ssh_digest_memory(SSH_DIGEST_SHA1, data, datalen,
71 digest, sizeof(digest))) != 0)
72 goto out;
Ben Lindstromc66d4362001-06-09 01:36:21 +000073
Damien Miller86687062014-07-02 15:28:02 +100074 if ((sig = DSA_do_sign(digest, dlen, key->dsa)) == NULL) {
75 ret = SSH_ERR_LIBCRYPTO_ERROR;
76 goto out;
Ben Lindstromc66d4362001-06-09 01:36:21 +000077 }
Damien Millera664e872000-04-16 11:52:47 +100078
79 rlen = BN_num_bytes(sig->r);
80 slen = BN_num_bytes(sig->s);
81 if (rlen > INTBLOB_LEN || slen > INTBLOB_LEN) {
Damien Miller86687062014-07-02 15:28:02 +100082 ret = SSH_ERR_INTERNAL_ERROR;
83 goto out;
Damien Millera664e872000-04-16 11:52:47 +100084 }
Damien Millera5103f42014-02-04 11:20:14 +110085 explicit_bzero(sigblob, SIGBLOB_LEN);
Damien Miller86687062014-07-02 15:28:02 +100086 BN_bn2bin(sig->r, sigblob + SIGBLOB_LEN - INTBLOB_LEN - rlen);
87 BN_bn2bin(sig->s, sigblob + SIGBLOB_LEN - slen);
Damien Millera664e872000-04-16 11:52:47 +100088
djm@openbsd.org14b5c632018-01-23 05:27:21 +000089 if ((b = sshbuf_new()) == NULL) {
90 ret = SSH_ERR_ALLOC_FAIL;
91 goto out;
92 }
93 if ((ret = sshbuf_put_cstring(b, "ssh-dss")) != 0 ||
94 (ret = sshbuf_put_string(b, sigblob, SIGBLOB_LEN)) != 0)
95 goto out;
96
97 len = sshbuf_len(b);
98 if (sigp != NULL) {
99 if ((*sigp = malloc(len)) == NULL) {
Damien Miller86687062014-07-02 15:28:02 +1000100 ret = SSH_ERR_ALLOC_FAIL;
101 goto out;
102 }
djm@openbsd.org14b5c632018-01-23 05:27:21 +0000103 memcpy(*sigp, sshbuf_ptr(b), len);
Damien Millera664e872000-04-16 11:52:47 +1000104 }
djm@openbsd.org14b5c632018-01-23 05:27:21 +0000105 if (lenp != NULL)
106 *lenp = len;
107 ret = 0;
Damien Miller86687062014-07-02 15:28:02 +1000108 out:
109 explicit_bzero(digest, sizeof(digest));
110 if (sig != NULL)
111 DSA_SIG_free(sig);
mmcc@openbsd.org52d70782015-12-11 04:21:11 +0000112 sshbuf_free(b);
Damien Miller86687062014-07-02 15:28:02 +1000113 return ret;
Damien Millera664e872000-04-16 11:52:47 +1000114}
Damien Millera664e872000-04-16 11:52:47 +1000115
Damien Miller86687062014-07-02 15:28:02 +1000116int
117ssh_dss_verify(const struct sshkey *key,
118 const u_char *signature, size_t signaturelen,
119 const u_char *data, size_t datalen, u_int compat)
120{
121 DSA_SIG *sig = NULL;
122 u_char digest[SSH_DIGEST_MAX_LENGTH], *sigblob = NULL;
123 size_t len, dlen = ssh_digest_bytes(SSH_DIGEST_SHA1);
124 int ret = SSH_ERR_INTERNAL_ERROR;
125 struct sshbuf *b = NULL;
126 char *ktype = NULL;
127
128 if (key == NULL || key->dsa == NULL ||
djm@openbsd.orgb6e01402016-04-21 06:08:02 +0000129 sshkey_type_plain(key->type) != KEY_DSA ||
130 signature == NULL || signaturelen == 0)
Damien Miller86687062014-07-02 15:28:02 +1000131 return SSH_ERR_INVALID_ARGUMENT;
132 if (dlen == 0)
133 return SSH_ERR_INTERNAL_ERROR;
Damien Millera664e872000-04-16 11:52:47 +1000134
Damien Millera664e872000-04-16 11:52:47 +1000135 /* fetch signature */
djm@openbsd.org14b5c632018-01-23 05:27:21 +0000136 if ((b = sshbuf_from(signature, signaturelen)) == NULL)
137 return SSH_ERR_ALLOC_FAIL;
138 if (sshbuf_get_cstring(b, &ktype, NULL) != 0 ||
139 sshbuf_get_string(b, &sigblob, &len) != 0) {
140 ret = SSH_ERR_INVALID_FORMAT;
141 goto out;
142 }
143 if (strcmp("ssh-dss", ktype) != 0) {
144 ret = SSH_ERR_KEY_TYPE_MISMATCH;
145 goto out;
146 }
147 if (sshbuf_len(b) != 0) {
148 ret = SSH_ERR_UNEXPECTED_TRAILING_DATA;
149 goto out;
Damien Millera664e872000-04-16 11:52:47 +1000150 }
151
152 if (len != SIGBLOB_LEN) {
Damien Miller86687062014-07-02 15:28:02 +1000153 ret = SSH_ERR_INVALID_FORMAT;
154 goto out;
Damien Millera664e872000-04-16 11:52:47 +1000155 }
156
157 /* parse signature */
Damien Miller86687062014-07-02 15:28:02 +1000158 if ((sig = DSA_SIG_new()) == NULL ||
159 (sig->r = BN_new()) == NULL ||
160 (sig->s = BN_new()) == NULL) {
161 ret = SSH_ERR_ALLOC_FAIL;
162 goto out;
163 }
Darren Tucker0bc85572006-11-07 23:14:41 +1100164 if ((BN_bin2bn(sigblob, INTBLOB_LEN, sig->r) == NULL) ||
Damien Miller86687062014-07-02 15:28:02 +1000165 (BN_bin2bn(sigblob+ INTBLOB_LEN, INTBLOB_LEN, sig->s) == NULL)) {
166 ret = SSH_ERR_LIBCRYPTO_ERROR;
167 goto out;
Damien Millerb3051d02014-01-10 10:58:53 +1100168 }
Damien Millera664e872000-04-16 11:52:47 +1000169
Damien Miller86687062014-07-02 15:28:02 +1000170 /* sha1 the data */
171 if ((ret = ssh_digest_memory(SSH_DIGEST_SHA1, data, datalen,
172 digest, sizeof(digest))) != 0)
173 goto out;
174
175 switch (DSA_do_verify(digest, dlen, sig, key->dsa)) {
176 case 1:
177 ret = 0;
178 break;
179 case 0:
180 ret = SSH_ERR_SIGNATURE_INVALID;
181 goto out;
182 default:
183 ret = SSH_ERR_LIBCRYPTO_ERROR;
184 goto out;
185 }
186
187 out:
Damien Millera5103f42014-02-04 11:20:14 +1100188 explicit_bzero(digest, sizeof(digest));
Damien Miller86687062014-07-02 15:28:02 +1000189 if (sig != NULL)
190 DSA_SIG_free(sig);
mmcc@openbsd.org52d70782015-12-11 04:21:11 +0000191 sshbuf_free(b);
mmcc@openbsd.orgd59ce082015-12-10 17:08:40 +0000192 free(ktype);
Damien Miller86687062014-07-02 15:28:02 +1000193 if (sigblob != NULL) {
194 explicit_bzero(sigblob, len);
195 free(sigblob);
196 }
Damien Millera664e872000-04-16 11:52:47 +1000197 return ret;
198}
Damien Miller72ef7c12015-01-15 02:21:31 +1100199#endif /* WITH_OPENSSL */