1 /* $OpenBSD: ssh-dss.c,v 1.36 2018/01/23 05:27:21 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 "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 if (sig != NULL) 105 DSA_SIG_free(sig); 106 sshbuf_free(b); 107 return ret; 108 } 109 110 int 111 ssh_dss_verify(const struct sshkey *key, 112 const u_char *signature, size_t signaturelen, 113 const u_char *data, size_t datalen, u_int compat) 114 { 115 DSA_SIG *sig = NULL; 116 u_char digest[SSH_DIGEST_MAX_LENGTH], *sigblob = NULL; 117 size_t len, dlen = ssh_digest_bytes(SSH_DIGEST_SHA1); 118 int ret = SSH_ERR_INTERNAL_ERROR; 119 struct sshbuf *b = NULL; 120 char *ktype = NULL; 121 122 if (key == NULL || key->dsa == NULL || 123 sshkey_type_plain(key->type) != KEY_DSA || 124 signature == NULL || signaturelen == 0) 125 return SSH_ERR_INVALID_ARGUMENT; 126 if (dlen == 0) 127 return SSH_ERR_INTERNAL_ERROR; 128 129 /* fetch signature */ 130 if ((b = sshbuf_from(signature, signaturelen)) == NULL) 131 return SSH_ERR_ALLOC_FAIL; 132 if (sshbuf_get_cstring(b, &ktype, NULL) != 0 || 133 sshbuf_get_string(b, &sigblob, &len) != 0) { 134 ret = SSH_ERR_INVALID_FORMAT; 135 goto out; 136 } 137 if (strcmp("ssh-dss", ktype) != 0) { 138 ret = SSH_ERR_KEY_TYPE_MISMATCH; 139 goto out; 140 } 141 if (sshbuf_len(b) != 0) { 142 ret = SSH_ERR_UNEXPECTED_TRAILING_DATA; 143 goto out; 144 } 145 146 if (len != SIGBLOB_LEN) { 147 ret = SSH_ERR_INVALID_FORMAT; 148 goto out; 149 } 150 151 /* parse signature */ 152 if ((sig = DSA_SIG_new()) == NULL || 153 (sig->r = BN_new()) == NULL || 154 (sig->s = BN_new()) == NULL) { 155 ret = SSH_ERR_ALLOC_FAIL; 156 goto out; 157 } 158 if ((BN_bin2bn(sigblob, INTBLOB_LEN, sig->r) == NULL) || 159 (BN_bin2bn(sigblob+ INTBLOB_LEN, INTBLOB_LEN, sig->s) == NULL)) { 160 ret = SSH_ERR_LIBCRYPTO_ERROR; 161 goto out; 162 } 163 164 /* sha1 the data */ 165 if ((ret = ssh_digest_memory(SSH_DIGEST_SHA1, data, datalen, 166 digest, sizeof(digest))) != 0) 167 goto out; 168 169 switch (DSA_do_verify(digest, dlen, sig, key->dsa)) { 170 case 1: 171 ret = 0; 172 break; 173 case 0: 174 ret = SSH_ERR_SIGNATURE_INVALID; 175 goto out; 176 default: 177 ret = SSH_ERR_LIBCRYPTO_ERROR; 178 goto out; 179 } 180 181 out: 182 explicit_bzero(digest, sizeof(digest)); 183 if (sig != NULL) 184 DSA_SIG_free(sig); 185 sshbuf_free(b); 186 free(ktype); 187 if (sigblob != NULL) { 188 explicit_bzero(sigblob, len); 189 free(sigblob); 190 } 191 return ret; 192 } 193