diff options
| author | SkyperTHC | 2026-03-03 06:28:55 +0000 |
|---|---|---|
| committer | SkyperTHC | 2026-03-03 06:28:55 +0000 |
| commit | 5d3573ef7a109ee70416fe94db098fe6a769a798 (patch) | |
| tree | dc2d5b294c9db8ab2db7433511f94e1c4bb8b698 /other/ssharp/ssh-rsa.c | |
| parent | c6c59dc73cc4586357f93ab38ecf459e98675cc5 (diff) | |
packetstorm sync
Diffstat (limited to 'other/ssharp/ssh-rsa.c')
| -rw-r--r-- | other/ssharp/ssh-rsa.c | 174 |
1 files changed, 174 insertions, 0 deletions
diff --git a/other/ssharp/ssh-rsa.c b/other/ssharp/ssh-rsa.c new file mode 100644 index 0000000..b502ddb --- /dev/null +++ b/other/ssharp/ssh-rsa.c | |||
| @@ -0,0 +1,174 @@ | |||
| 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. | ||
| 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" | ||
| 26 | RCSID("$OpenBSD: ssh-rsa.c,v 1.8 2001/03/27 10:57:00 markus Exp $"); | ||
| 27 | |||
| 28 | #include <openssl/evp.h> | ||
| 29 | #include <openssl/err.h> | ||
| 30 | |||
| 31 | #include "xmalloc.h" | ||
| 32 | #include "log.h" | ||
| 33 | #include "buffer.h" | ||
| 34 | #include "bufaux.h" | ||
| 35 | #include "key.h" | ||
| 36 | #include "ssh-rsa.h" | ||
| 37 | #include "compat.h" | ||
| 38 | |||
| 39 | /* RSASSA-PKCS1-v1_5 (PKCS #1 v2.0 signature) with SHA1 */ | ||
| 40 | int | ||
| 41 | ssh_rsa_sign( | ||
| 42 | Key *key, | ||
| 43 | u_char **sigp, int *lenp, | ||
| 44 | u_char *data, int datalen) | ||
| 45 | { | ||
| 46 | const EVP_MD *evp_md; | ||
| 47 | EVP_MD_CTX md; | ||
| 48 | u_char *digest, *sig, *ret; | ||
| 49 | u_int slen, dlen, len; | ||
| 50 | int ok, nid; | ||
| 51 | Buffer b; | ||
| 52 | |||
| 53 | if (key == NULL || key->type != KEY_RSA || key->rsa == NULL) { | ||
| 54 | error("ssh_rsa_sign: no RSA key"); | ||
| 55 | return -1; | ||
| 56 | } | ||
| 57 | nid = (datafellows & SSH_BUG_RSASIGMD5) ? NID_md5 : NID_sha1; | ||
| 58 | if ((evp_md = EVP_get_digestbynid(nid)) == NULL) { | ||
| 59 | error("ssh_rsa_sign: EVP_get_digestbynid %d failed", nid); | ||
| 60 | return -1; | ||
| 61 | } | ||
| 62 | dlen = evp_md->md_size; | ||
| 63 | digest = xmalloc(dlen); | ||
| 64 | EVP_DigestInit(&md, evp_md); | ||
| 65 | EVP_DigestUpdate(&md, data, datalen); | ||
| 66 | EVP_DigestFinal(&md, digest, NULL); | ||
| 67 | |||
| 68 | slen = RSA_size(key->rsa); | ||
| 69 | sig = xmalloc(slen); | ||
| 70 | |||
| 71 | ok = RSA_sign(nid, digest, dlen, sig, &len, key->rsa); | ||
| 72 | memset(digest, 'd', dlen); | ||
| 73 | xfree(digest); | ||
| 74 | |||
| 75 | if (ok != 1) { | ||
| 76 | int ecode = ERR_get_error(); | ||
| 77 | error("ssh_rsa_sign: RSA_sign failed: %s", ERR_error_string(ecode, NULL)); | ||
| 78 | xfree(sig); | ||
| 79 | return -1; | ||
| 80 | } | ||
| 81 | if (len < slen) { | ||
| 82 | int diff = slen - len; | ||
| 83 | debug("slen %d > len %d", slen, len); | ||
| 84 | memmove(sig + diff, sig, len); | ||
| 85 | memset(sig, 0, diff); | ||
| 86 | } else if (len > slen) { | ||
| 87 | error("ssh_rsa_sign: slen %d slen2 %d", slen, len); | ||
| 88 | xfree(sig); | ||
| 89 | return -1; | ||
| 90 | } | ||
| 91 | /* encode signature */ | ||
| 92 | buffer_init(&b); | ||
| 93 | buffer_put_cstring(&b, "ssh-rsa"); | ||
| 94 | buffer_put_string(&b, sig, slen); | ||
| 95 | len = buffer_len(&b); | ||
| 96 | ret = xmalloc(len); | ||
| 97 | memcpy(ret, buffer_ptr(&b), len); | ||
| 98 | buffer_free(&b); | ||
| 99 | memset(sig, 's', slen); | ||
| 100 | xfree(sig); | ||
| 101 | |||
| 102 | if (lenp != NULL) | ||
| 103 | *lenp = len; | ||
| 104 | if (sigp != NULL) | ||
| 105 | *sigp = ret; | ||
| 106 | debug2("ssh_rsa_sign: done"); | ||
| 107 | return 0; | ||
| 108 | } | ||
| 109 | |||
| 110 | int | ||
| 111 | ssh_rsa_verify( | ||
| 112 | Key *key, | ||
| 113 | u_char *signature, int signaturelen, | ||
| 114 | u_char *data, int datalen) | ||
| 115 | { | ||
| 116 | Buffer b; | ||
| 117 | const EVP_MD *evp_md; | ||
| 118 | EVP_MD_CTX md; | ||
| 119 | char *ktype; | ||
| 120 | u_char *sigblob, *digest; | ||
| 121 | u_int len, dlen; | ||
| 122 | int rlen, ret, nid; | ||
| 123 | |||
| 124 | if (key == NULL || key->type != KEY_RSA || key->rsa == NULL) { | ||
| 125 | error("ssh_rsa_verify: no RSA key"); | ||
| 126 | return -1; | ||
| 127 | } | ||
| 128 | if (BN_num_bits(key->rsa->n) < 768) { | ||
| 129 | error("ssh_rsa_verify: n too small: %d bits", | ||
| 130 | BN_num_bits(key->rsa->n)); | ||
| 131 | return -1; | ||
| 132 | } | ||
| 133 | buffer_init(&b); | ||
| 134 | buffer_append(&b, (char *) signature, signaturelen); | ||
| 135 | ktype = buffer_get_string(&b, NULL); | ||
| 136 | if (strcmp("ssh-rsa", ktype) != 0) { | ||
| 137 | error("ssh_rsa_verify: cannot handle type %s", ktype); | ||
| 138 | buffer_free(&b); | ||
| 139 | xfree(ktype); | ||
| 140 | return -1; | ||
| 141 | } | ||
| 142 | xfree(ktype); | ||
| 143 | sigblob = (u_char *)buffer_get_string(&b, &len); | ||
| 144 | rlen = buffer_len(&b); | ||
| 145 | buffer_free(&b); | ||
| 146 | if(rlen != 0) { | ||
| 147 | xfree(sigblob); | ||
| 148 | error("ssh_rsa_verify: remaining bytes in signature %d", rlen); | ||
| 149 | return -1; | ||
| 150 | } | ||
| 151 | nid = (datafellows & SSH_BUG_RSASIGMD5) ? NID_md5 : NID_sha1; | ||
| 152 | if ((evp_md = EVP_get_digestbynid(nid)) == NULL) { | ||
| 153 | xfree(sigblob); | ||
| 154 | error("ssh_rsa_verify: EVP_get_digestbynid %d failed", nid); | ||
| 155 | return -1; | ||
| 156 | } | ||
| 157 | dlen = evp_md->md_size; | ||
| 158 | digest = xmalloc(dlen); | ||
| 159 | EVP_DigestInit(&md, evp_md); | ||
| 160 | EVP_DigestUpdate(&md, data, datalen); | ||
| 161 | EVP_DigestFinal(&md, digest, NULL); | ||
| 162 | |||
| 163 | ret = RSA_verify(nid, digest, dlen, sigblob, len, key->rsa); | ||
| 164 | memset(digest, 'd', dlen); | ||
| 165 | xfree(digest); | ||
| 166 | memset(sigblob, 's', len); | ||
| 167 | xfree(sigblob); | ||
| 168 | if (ret == 0) { | ||
| 169 | int ecode = ERR_get_error(); | ||
| 170 | error("ssh_rsa_verify: RSA_verify failed: %s", ERR_error_string(ecode, NULL)); | ||
| 171 | } | ||
| 172 | debug("ssh_rsa_verify: signature %scorrect", (ret==0) ? "in" : ""); | ||
| 173 | return ret; | ||
| 174 | } | ||
