1 /* $OpenBSD: ssh-dss.c,v 1.27 2010/08/31 09:58:37 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 39 #define INTBLOB_LEN 20 40 #define SIGBLOB_LEN (2*INTBLOB_LEN) 41 42 int 43 ssh_dss_sign(const Key *key, u_char **sigp, u_int *lenp, 44 const u_char *data, u_int datalen) 45 { 46 DSA_SIG *sig; 47 const EVP_MD *evp_md = EVP_sha1(); 48 EVP_MD_CTX md; 49 u_char digest[EVP_MAX_MD_SIZE], sigblob[SIGBLOB_LEN]; 50 u_int rlen, slen, len, dlen; 51 Buffer b; 52 53 if (key == NULL || key->dsa == NULL || (key->type != KEY_DSA && 54 key->type != KEY_DSA_CERT && key->type != KEY_DSA_CERT_V00)) { 55 error("ssh_dss_sign: no DSA key"); 56 return -1; 57 } 58 EVP_DigestInit(&md, evp_md); 59 EVP_DigestUpdate(&md, data, datalen); 60 EVP_DigestFinal(&md, digest, &dlen); 61 62 sig = DSA_do_sign(digest, dlen, key->dsa); 63 memset(digest, 'd', sizeof(digest)); 64 65 if (sig == NULL) { 66 error("ssh_dss_sign: sign failed"); 67 return -1; 68 } 69 70 rlen = BN_num_bytes(sig->r); 71 slen = BN_num_bytes(sig->s); 72 if (rlen > INTBLOB_LEN || slen > INTBLOB_LEN) { 73 error("bad sig size %u %u", rlen, slen); 74 DSA_SIG_free(sig); 75 return -1; 76 } 77 memset(sigblob, 0, SIGBLOB_LEN); 78 BN_bn2bin(sig->r, sigblob+ SIGBLOB_LEN - INTBLOB_LEN - rlen); 79 BN_bn2bin(sig->s, sigblob+ SIGBLOB_LEN - slen); 80 DSA_SIG_free(sig); 81 82 if (datafellows & SSH_BUG_SIGBLOB) { 83 if (lenp != NULL) 84 *lenp = SIGBLOB_LEN; 85 if (sigp != NULL) { 86 *sigp = xmalloc(SIGBLOB_LEN); 87 memcpy(*sigp, sigblob, SIGBLOB_LEN); 88 } 89 } else { 90 /* ietf-drafts */ 91 buffer_init(&b); 92 buffer_put_cstring(&b, "ssh-dss"); 93 buffer_put_string(&b, sigblob, SIGBLOB_LEN); 94 len = buffer_len(&b); 95 if (lenp != NULL) 96 *lenp = len; 97 if (sigp != NULL) { 98 *sigp = xmalloc(len); 99 memcpy(*sigp, buffer_ptr(&b), len); 100 } 101 buffer_free(&b); 102 } 103 return 0; 104 } 105 int 106 ssh_dss_verify(const Key *key, const u_char *signature, u_int signaturelen, 107 const u_char *data, u_int datalen) 108 { 109 DSA_SIG *sig; 110 const EVP_MD *evp_md = EVP_sha1(); 111 EVP_MD_CTX md; 112 u_char digest[EVP_MAX_MD_SIZE], *sigblob; 113 u_int len, dlen; 114 int rlen, ret; 115 Buffer b; 116 117 if (key == NULL || key->dsa == NULL || (key->type != KEY_DSA && 118 key->type != KEY_DSA_CERT && key->type != KEY_DSA_CERT_V00)) { 119 error("ssh_dss_verify: no DSA key"); 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("ssh_dss_verify: cannot handle type %s", ktype); 136 buffer_free(&b); 137 xfree(ktype); 138 return -1; 139 } 140 xfree(ktype); 141 sigblob = buffer_get_string(&b, &len); 142 rlen = buffer_len(&b); 143 buffer_free(&b); 144 if (rlen != 0) { 145 error("ssh_dss_verify: " 146 "remaining bytes in signature %d", rlen); 147 xfree(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("ssh_dss_verify: DSA_SIG_new failed"); 159 if ((sig->r = BN_new()) == NULL) 160 fatal("ssh_dss_verify: BN_new failed"); 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("ssh_dss_verify: BN_bin2bn failed"); 166 167 /* clean up */ 168 memset(sigblob, 0, len); 169 xfree(sigblob); 170 171 /* sha1 the data */ 172 EVP_DigestInit(&md, evp_md); 173 EVP_DigestUpdate(&md, data, datalen); 174 EVP_DigestFinal(&md, digest, &dlen); 175 176 ret = DSA_do_verify(digest, dlen, sig, key->dsa); 177 memset(digest, 'd', sizeof(digest)); 178 179 DSA_SIG_free(sig); 180 181 debug("ssh_dss_verify: signature %s", 182 ret == 1 ? "correct" : ret == 0 ? "incorrect" : "error"); 183 return ret; 184 } 185