]> andersk Git - openssh.git/blame - ssh-dss.c
- (bal) Slight auth2-pam.c clean up.
[openssh.git] / ssh-dss.c
CommitLineData
4f8c6159 1/*
2 * Copyright (c) 2000 Markus Friedl. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
4f8c6159 12 *
13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
14 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
15 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
16 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
17 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
18 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
19 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
20 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
22 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 */
24
25#include "includes.h"
1e3b8b07 26RCSID("$OpenBSD: ssh-dss.c,v 1.2 2000/12/19 23:17:58 markus Exp $");
4f8c6159 27
28#include "ssh.h"
29#include "xmalloc.h"
30#include "buffer.h"
31#include "bufaux.h"
32#include "compat.h"
33
34#include <openssl/bn.h>
4f8c6159 35#include <openssl/rsa.h>
36#include <openssl/dsa.h>
37#include <openssl/evp.h>
4f8c6159 38
4f8c6159 39#include "key.h"
40
41#define INTBLOB_LEN 20
42#define SIGBLOB_LEN (2*INTBLOB_LEN)
43
4f8c6159 44int
fa08c86b 45ssh_dss_sign(
4f8c6159 46 Key *key,
1e3b8b07 47 u_char **sigp, int *lenp,
48 u_char *data, int datalen)
4f8c6159 49{
1e3b8b07 50 u_char *digest;
51 u_char *ret;
4f8c6159 52 DSA_SIG *sig;
53 EVP_MD *evp_md = EVP_sha1();
54 EVP_MD_CTX md;
1e3b8b07 55 u_int rlen;
56 u_int slen;
57 u_int len;
58 u_char sigblob[SIGBLOB_LEN];
4f8c6159 59 Buffer b;
60
61 if (key == NULL || key->type != KEY_DSA || key->dsa == NULL) {
fa08c86b 62 error("ssh_dss_sign: no DSA key");
4f8c6159 63 return -1;
64 }
65 digest = xmalloc(evp_md->md_size);
66 EVP_DigestInit(&md, evp_md);
a306f2dd 67 EVP_DigestUpdate(&md, data, datalen);
4f8c6159 68 EVP_DigestFinal(&md, digest, NULL);
69
70 sig = DSA_do_sign(digest, evp_md->md_size, key->dsa);
a306f2dd 71 if (sig == NULL) {
fa08c86b 72 fatal("ssh_dss_sign: cannot sign");
a306f2dd 73 }
4f8c6159 74
75 rlen = BN_num_bytes(sig->r);
76 slen = BN_num_bytes(sig->s);
77 if (rlen > INTBLOB_LEN || slen > INTBLOB_LEN) {
78 error("bad sig size %d %d", rlen, slen);
79 DSA_SIG_free(sig);
80 return -1;
81 }
82 debug("sig size %d %d", rlen, slen);
83
84 memset(sigblob, 0, SIGBLOB_LEN);
85 BN_bn2bin(sig->r, sigblob+ SIGBLOB_LEN - INTBLOB_LEN - rlen);
86 BN_bn2bin(sig->s, sigblob+ SIGBLOB_LEN - slen);
87 DSA_SIG_free(sig);
88
d0c832f3 89 if (datafellows & SSH_BUG_SIGBLOB) {
4f8c6159 90 debug("datafellows");
91 ret = xmalloc(SIGBLOB_LEN);
92 memcpy(ret, sigblob, SIGBLOB_LEN);
93 if (lenp != NULL)
94 *lenp = SIGBLOB_LEN;
95 if (sigp != NULL)
96 *sigp = ret;
97 } else {
98 /* ietf-drafts */
99 buffer_init(&b);
fa08c86b 100 buffer_put_cstring(&b, "ssh-dss");
4f8c6159 101 buffer_put_string(&b, sigblob, SIGBLOB_LEN);
102 len = buffer_len(&b);
103 ret = xmalloc(len);
104 memcpy(ret, buffer_ptr(&b), len);
105 buffer_free(&b);
106 if (lenp != NULL)
107 *lenp = len;
108 if (sigp != NULL)
109 *sigp = ret;
110 }
111 return 0;
112}
113int
fa08c86b 114ssh_dss_verify(
4f8c6159 115 Key *key,
1e3b8b07 116 u_char *signature, int signaturelen,
117 u_char *data, int datalen)
4f8c6159 118{
119 Buffer b;
1e3b8b07 120 u_char *digest;
4f8c6159 121 DSA_SIG *sig;
122 EVP_MD *evp_md = EVP_sha1();
123 EVP_MD_CTX md;
1e3b8b07 124 u_char *sigblob;
4f8c6159 125 char *txt;
1e3b8b07 126 u_int len, dlen;
4f8c6159 127 int rlen;
128 int ret;
129
130 if (key == NULL || key->type != KEY_DSA || key->dsa == NULL) {
fa08c86b 131 error("ssh_dss_verify: no DSA key");
4f8c6159 132 return -1;
133 }
134
d0c832f3 135 if (!(datafellows & SSH_BUG_SIGBLOB) &&
136 signaturelen == SIGBLOB_LEN) {
137 datafellows |= ~SSH_BUG_SIGBLOB;
138 log("autodetect SSH_BUG_SIGBLOB");
139 } else if ((datafellows & SSH_BUG_SIGBLOB) &&
140 signaturelen != SIGBLOB_LEN) {
141 log("autoremove SSH_BUG_SIGBLOB");
142 datafellows &= ~SSH_BUG_SIGBLOB;
4f8c6159 143 }
144
145 debug("len %d datafellows %d", signaturelen, datafellows);
146
147 /* fetch signature */
d0c832f3 148 if (datafellows & SSH_BUG_SIGBLOB) {
4f8c6159 149 sigblob = signature;
150 len = signaturelen;
151 } else {
152 /* ietf-drafts */
74fc9186 153 char *ktype;
4f8c6159 154 buffer_init(&b);
155 buffer_append(&b, (char *) signature, signaturelen);
156 ktype = buffer_get_string(&b, NULL);
fa08c86b 157 if (strcmp("ssh-dss", ktype) != 0) {
158 error("ssh_dss_verify: cannot handle type %s", ktype);
74fc9186 159 buffer_free(&b);
160 return -1;
161 }
1e3b8b07 162 sigblob = (u_char *)buffer_get_string(&b, &len);
4f8c6159 163 rlen = buffer_len(&b);
74fc9186 164 if(rlen != 0) {
4f8c6159 165 error("remaining bytes in signature %d", rlen);
74fc9186 166 buffer_free(&b);
167 return -1;
168 }
4f8c6159 169 buffer_free(&b);
74fc9186 170 xfree(ktype);
4f8c6159 171 }
172
173 if (len != SIGBLOB_LEN) {
174 fatal("bad sigbloblen %d != SIGBLOB_LEN", len);
175 }
176
177 /* parse signature */
178 sig = DSA_SIG_new();
179 sig->r = BN_new();
180 sig->s = BN_new();
181 BN_bin2bn(sigblob, INTBLOB_LEN, sig->r);
182 BN_bin2bn(sigblob+ INTBLOB_LEN, INTBLOB_LEN, sig->s);
d0c832f3 183
184 if (!(datafellows & SSH_BUG_SIGBLOB)) {
4f8c6159 185 memset(sigblob, 0, len);
186 xfree(sigblob);
187 }
188
a306f2dd 189 /* sha1 the data */
fa08c86b 190 dlen = evp_md->md_size;
191 digest = xmalloc(dlen);
4f8c6159 192 EVP_DigestInit(&md, evp_md);
a306f2dd 193 EVP_DigestUpdate(&md, data, datalen);
4f8c6159 194 EVP_DigestFinal(&md, digest, NULL);
195
fa08c86b 196 ret = DSA_do_verify(digest, dlen, sig, key->dsa);
4f8c6159 197
fa08c86b 198 memset(digest, 0, dlen);
4f8c6159 199 xfree(digest);
200 DSA_SIG_free(sig);
201
202 switch (ret) {
203 case 1:
204 txt = "correct";
205 break;
206 case 0:
207 txt = "incorrect";
208 break;
209 case -1:
210 default:
211 txt = "error";
212 break;
213 }
fa08c86b 214 debug("ssh_dss_verify: signature %s", txt);
4f8c6159 215 return ret;
216}
This page took 0.093691 seconds and 5 git commands to generate.