xref: /openbsd-src/usr.bin/ssh/ssh-dss.c (revision 91f110e064cd7c194e59e019b83bb7496c1c84d4)
1 /* $OpenBSD: ssh-dss.c,v 1.31 2014/02/02 03:44:31 djm 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 "xmalloc.h"
34 #include "buffer.h"
35 #include "compat.h"
36 #include "log.h"
37 #include "key.h"
38 #include "digest.h"
39 
40 #define INTBLOB_LEN	20
41 #define SIGBLOB_LEN	(2*INTBLOB_LEN)
42 
43 int
44 ssh_dss_sign(const Key *key, u_char **sigp, u_int *lenp,
45     const u_char *data, u_int datalen)
46 {
47 	DSA_SIG *sig;
48 	u_char digest[SSH_DIGEST_MAX_LENGTH], sigblob[SIGBLOB_LEN];
49 	u_int rlen, slen, len, dlen = ssh_digest_bytes(SSH_DIGEST_SHA1);
50 	Buffer b;
51 
52 	if (key == NULL || key_type_plain(key->type) != KEY_DSA ||
53 	    key->dsa == NULL) {
54 		error("%s: no DSA key", __func__);
55 		return -1;
56 	}
57 
58 	if (ssh_digest_memory(SSH_DIGEST_SHA1, data, datalen,
59 	    digest, sizeof(digest)) != 0) {
60 		error("%s: ssh_digest_memory failed", __func__);
61 		return -1;
62 	}
63 
64 	sig = DSA_do_sign(digest, dlen, key->dsa);
65 	explicit_bzero(digest, sizeof(digest));
66 
67 	if (sig == NULL) {
68 		error("ssh_dss_sign: sign failed");
69 		return -1;
70 	}
71 
72 	rlen = BN_num_bytes(sig->r);
73 	slen = BN_num_bytes(sig->s);
74 	if (rlen > INTBLOB_LEN || slen > INTBLOB_LEN) {
75 		error("bad sig size %u %u", rlen, slen);
76 		DSA_SIG_free(sig);
77 		return -1;
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 	DSA_SIG_free(sig);
83 
84 	if (datafellows & SSH_BUG_SIGBLOB) {
85 		if (lenp != NULL)
86 			*lenp = SIGBLOB_LEN;
87 		if (sigp != NULL) {
88 			*sigp = xmalloc(SIGBLOB_LEN);
89 			memcpy(*sigp, sigblob, SIGBLOB_LEN);
90 		}
91 	} else {
92 		/* ietf-drafts */
93 		buffer_init(&b);
94 		buffer_put_cstring(&b, "ssh-dss");
95 		buffer_put_string(&b, sigblob, SIGBLOB_LEN);
96 		len = buffer_len(&b);
97 		if (lenp != NULL)
98 			*lenp = len;
99 		if (sigp != NULL) {
100 			*sigp = xmalloc(len);
101 			memcpy(*sigp, buffer_ptr(&b), len);
102 		}
103 		buffer_free(&b);
104 	}
105 	return 0;
106 }
107 int
108 ssh_dss_verify(const Key *key, const u_char *signature, u_int signaturelen,
109     const u_char *data, u_int datalen)
110 {
111 	DSA_SIG *sig;
112 	u_char digest[SSH_DIGEST_MAX_LENGTH], *sigblob;
113 	u_int len, dlen = ssh_digest_bytes(SSH_DIGEST_SHA1);
114 	int rlen, ret;
115 	Buffer b;
116 
117 	if (key == NULL || key_type_plain(key->type) != KEY_DSA ||
118 	    key->dsa == NULL) {
119 		error("%s: no DSA key", __func__);
120 		return -1;
121 	}
122 
123 	/* fetch signature */
124 	if (datafellows & SSH_BUG_SIGBLOB) {
125 		sigblob = xmalloc(signaturelen);
126 		memcpy(sigblob, signature, signaturelen);
127 		len = signaturelen;
128 	} else {
129 		/* ietf-drafts */
130 		char *ktype;
131 		buffer_init(&b);
132 		buffer_append(&b, signature, signaturelen);
133 		ktype = buffer_get_cstring(&b, NULL);
134 		if (strcmp("ssh-dss", ktype) != 0) {
135 			error("%s: cannot handle type %s", __func__, ktype);
136 			buffer_free(&b);
137 			free(ktype);
138 			return -1;
139 		}
140 		free(ktype);
141 		sigblob = buffer_get_string(&b, &len);
142 		rlen = buffer_len(&b);
143 		buffer_free(&b);
144 		if (rlen != 0) {
145 			error("%s: remaining bytes in signature %d",
146 			    __func__, rlen);
147 			free(sigblob);
148 			return -1;
149 		}
150 	}
151 
152 	if (len != SIGBLOB_LEN) {
153 		fatal("bad sigbloblen %u != SIGBLOB_LEN", len);
154 	}
155 
156 	/* parse signature */
157 	if ((sig = DSA_SIG_new()) == NULL)
158 		fatal("%s: DSA_SIG_new failed", __func__);
159 	if ((sig->r = BN_new()) == NULL)
160 		fatal("%s: BN_new failed", __func__);
161 	if ((sig->s = BN_new()) == NULL)
162 		fatal("ssh_dss_verify: BN_new failed");
163 	if ((BN_bin2bn(sigblob, INTBLOB_LEN, sig->r) == NULL) ||
164 	    (BN_bin2bn(sigblob+ INTBLOB_LEN, INTBLOB_LEN, sig->s) == NULL))
165 		fatal("%s: BN_bin2bn failed", __func__);
166 
167 	/* clean up */
168 	explicit_bzero(sigblob, len);
169 	free(sigblob);
170 
171 	/* sha1 the data */
172 	if (ssh_digest_memory(SSH_DIGEST_SHA1, data, datalen,
173 	    digest, sizeof(digest)) != 0) {
174 		error("%s: digest_memory failed", __func__);
175 		return -1;
176 	}
177 
178 	ret = DSA_do_verify(digest, dlen, sig, key->dsa);
179 	explicit_bzero(digest, sizeof(digest));
180 
181 	DSA_SIG_free(sig);
182 
183 	debug("%s: signature %s", __func__,
184 	    ret == 1 ? "correct" : ret == 0 ? "incorrect" : "error");
185 	return ret;
186 }
187