xref: /openbsd-src/usr.bin/ssh/ssh-dss.c (revision aa997e528a848ca5596493c2a801bdd6fb26ae61)
1 /* $OpenBSD: ssh-dss.c,v 1.37 2018/02/07 02:06:51 jsing Exp $ */
2 /*
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.
13  *
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 <sys/types.h>
27 
28 #include <openssl/bn.h>
29 #include <openssl/evp.h>
30 
31 #include <string.h>
32 
33 #include "sshbuf.h"
34 #include "compat.h"
35 #include "ssherr.h"
36 #include "digest.h"
37 #define SSHKEY_INTERNAL
38 #include "sshkey.h"
39 
40 #define INTBLOB_LEN	20
41 #define SIGBLOB_LEN	(2*INTBLOB_LEN)
42 
43 int
44 ssh_dss_sign(const struct sshkey *key, u_char **sigp, size_t *lenp,
45     const u_char *data, size_t datalen, u_int compat)
46 {
47 	DSA_SIG *sig = NULL;
48 	u_char digest[SSH_DIGEST_MAX_LENGTH], sigblob[SIGBLOB_LEN];
49 	size_t rlen, slen, len, dlen = ssh_digest_bytes(SSH_DIGEST_SHA1);
50 	struct sshbuf *b = NULL;
51 	int ret = SSH_ERR_INVALID_ARGUMENT;
52 
53 	if (lenp != NULL)
54 		*lenp = 0;
55 	if (sigp != NULL)
56 		*sigp = NULL;
57 
58 	if (key == NULL || key->dsa == NULL ||
59 	    sshkey_type_plain(key->type) != KEY_DSA)
60 		return SSH_ERR_INVALID_ARGUMENT;
61 	if (dlen == 0)
62 		return SSH_ERR_INTERNAL_ERROR;
63 
64 	if ((ret = ssh_digest_memory(SSH_DIGEST_SHA1, data, datalen,
65 	    digest, sizeof(digest))) != 0)
66 		goto out;
67 
68 	if ((sig = DSA_do_sign(digest, dlen, key->dsa)) == NULL) {
69 		ret = SSH_ERR_LIBCRYPTO_ERROR;
70 		goto out;
71 	}
72 
73 	rlen = BN_num_bytes(sig->r);
74 	slen = BN_num_bytes(sig->s);
75 	if (rlen > INTBLOB_LEN || slen > INTBLOB_LEN) {
76 		ret = SSH_ERR_INTERNAL_ERROR;
77 		goto out;
78 	}
79 	explicit_bzero(sigblob, SIGBLOB_LEN);
80 	BN_bn2bin(sig->r, sigblob + SIGBLOB_LEN - INTBLOB_LEN - rlen);
81 	BN_bn2bin(sig->s, sigblob + SIGBLOB_LEN - slen);
82 
83 	if ((b = sshbuf_new()) == NULL) {
84 		ret = SSH_ERR_ALLOC_FAIL;
85 		goto out;
86 	}
87 	if ((ret = sshbuf_put_cstring(b, "ssh-dss")) != 0 ||
88 	    (ret = sshbuf_put_string(b, sigblob, SIGBLOB_LEN)) != 0)
89 		goto out;
90 
91 	len = sshbuf_len(b);
92 	if (sigp != NULL) {
93 		if ((*sigp = malloc(len)) == NULL) {
94 			ret = SSH_ERR_ALLOC_FAIL;
95 			goto out;
96 		}
97 		memcpy(*sigp, sshbuf_ptr(b), len);
98 	}
99 	if (lenp != NULL)
100 		*lenp = len;
101 	ret = 0;
102  out:
103 	explicit_bzero(digest, sizeof(digest));
104 	DSA_SIG_free(sig);
105 	sshbuf_free(b);
106 	return ret;
107 }
108 
109 int
110 ssh_dss_verify(const struct sshkey *key,
111     const u_char *signature, size_t signaturelen,
112     const u_char *data, size_t datalen, u_int compat)
113 {
114 	DSA_SIG *sig = NULL;
115 	u_char digest[SSH_DIGEST_MAX_LENGTH], *sigblob = NULL;
116 	size_t len, dlen = ssh_digest_bytes(SSH_DIGEST_SHA1);
117 	int ret = SSH_ERR_INTERNAL_ERROR;
118 	struct sshbuf *b = NULL;
119 	char *ktype = NULL;
120 
121 	if (key == NULL || key->dsa == NULL ||
122 	    sshkey_type_plain(key->type) != KEY_DSA ||
123 	    signature == NULL || signaturelen == 0)
124 		return SSH_ERR_INVALID_ARGUMENT;
125 	if (dlen == 0)
126 		return SSH_ERR_INTERNAL_ERROR;
127 
128 	/* fetch signature */
129 	if ((b = sshbuf_from(signature, signaturelen)) == NULL)
130 		return SSH_ERR_ALLOC_FAIL;
131 	if (sshbuf_get_cstring(b, &ktype, NULL) != 0 ||
132 	    sshbuf_get_string(b, &sigblob, &len) != 0) {
133 		ret = SSH_ERR_INVALID_FORMAT;
134 		goto out;
135 	}
136 	if (strcmp("ssh-dss", ktype) != 0) {
137 		ret = SSH_ERR_KEY_TYPE_MISMATCH;
138 		goto out;
139 	}
140 	if (sshbuf_len(b) != 0) {
141 		ret = SSH_ERR_UNEXPECTED_TRAILING_DATA;
142 		goto out;
143 	}
144 
145 	if (len != SIGBLOB_LEN) {
146 		ret = SSH_ERR_INVALID_FORMAT;
147 		goto out;
148 	}
149 
150 	/* parse signature */
151 	if ((sig = DSA_SIG_new()) == NULL ||
152 	    (sig->r = BN_new()) == NULL ||
153 	    (sig->s = BN_new()) == NULL) {
154 		ret = SSH_ERR_ALLOC_FAIL;
155 		goto out;
156 	}
157 	if ((BN_bin2bn(sigblob, INTBLOB_LEN, sig->r) == NULL) ||
158 	    (BN_bin2bn(sigblob+ INTBLOB_LEN, INTBLOB_LEN, sig->s) == NULL)) {
159 		ret = SSH_ERR_LIBCRYPTO_ERROR;
160 		goto out;
161 	}
162 
163 	/* sha1 the data */
164 	if ((ret = ssh_digest_memory(SSH_DIGEST_SHA1, data, datalen,
165 	    digest, sizeof(digest))) != 0)
166 		goto out;
167 
168 	switch (DSA_do_verify(digest, dlen, sig, key->dsa)) {
169 	case 1:
170 		ret = 0;
171 		break;
172 	case 0:
173 		ret = SSH_ERR_SIGNATURE_INVALID;
174 		goto out;
175 	default:
176 		ret = SSH_ERR_LIBCRYPTO_ERROR;
177 		goto out;
178 	}
179 
180  out:
181 	explicit_bzero(digest, sizeof(digest));
182 	DSA_SIG_free(sig);
183 	sshbuf_free(b);
184 	free(ktype);
185 	if (sigblob != NULL) {
186 		explicit_bzero(sigblob, len);
187 		free(sigblob);
188 	}
189 	return ret;
190 }
191