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