Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1 | #include "includes.h" |
Damien Miller | 8029775 | 1999-11-19 13:03:25 +1100 | [diff] [blame] | 2 | #ifdef SKEY |
Damien Miller | 6536c7d | 2000-06-22 21:32:31 +1000 | [diff] [blame] | 3 | RCSID("$OpenBSD: auth-skey.c,v 1.7 2000/06/20 01:39:38 markus Exp $"); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 4 | |
| 5 | #include "ssh.h" |
Damien Miller | aae6c61 | 1999-12-06 11:47:28 +1100 | [diff] [blame] | 6 | #include "packet.h" |
Damien Miller | 5f05637 | 2000-04-16 12:31:48 +1000 | [diff] [blame] | 7 | #include <sha1.h> |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 8 | |
| 9 | /* from %OpenBSD: skeylogin.c,v 1.32 1999/08/16 14:46:56 millert Exp % */ |
| 10 | |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 11 | /* |
Damien Miller | aae6c61 | 1999-12-06 11:47:28 +1100 | [diff] [blame] | 12 | * try skey authentication, |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 13 | * return 1 on success, 0 on failure, -1 if skey is not available |
Damien Miller | aae6c61 | 1999-12-06 11:47:28 +1100 | [diff] [blame] | 14 | */ |
| 15 | |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 16 | int |
Damien Miller | aae6c61 | 1999-12-06 11:47:28 +1100 | [diff] [blame] | 17 | auth_skey_password(struct passwd * pw, const char *password) |
| 18 | { |
| 19 | if (strncasecmp(password, "s/key", 5) == 0) { |
| 20 | char *skeyinfo = skey_keyinfo(pw->pw_name); |
| 21 | if (skeyinfo == NULL) { |
| 22 | debug("generating fake skeyinfo for %.100s.", |
| 23 | pw->pw_name); |
| 24 | skeyinfo = skey_fake_keyinfo(pw->pw_name); |
| 25 | } |
| 26 | if (skeyinfo != NULL) |
| 27 | packet_send_debug(skeyinfo); |
| 28 | /* Try again. */ |
| 29 | return 0; |
| 30 | } else if (skey_haskey(pw->pw_name) == 0 && |
| 31 | skey_passcheck(pw->pw_name, (char *) password) != -1) { |
| 32 | /* Authentication succeeded. */ |
| 33 | return 1; |
| 34 | } |
| 35 | /* Fall back to ordinary passwd authentication. */ |
| 36 | return -1; |
| 37 | } |
Damien Miller | 4e0dbd0 | 1999-12-09 10:57:00 +1100 | [diff] [blame] | 38 | |
| 39 | /* from %OpenBSD: skeylogin.c,v 1.32 1999/08/16 14:46:56 millert Exp % */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 40 | |
| 41 | #define ROUND(x) (((x)[0] << 24) + (((x)[1]) << 16) + (((x)[2]) << 8) + \ |
| 42 | ((x)[3])) |
| 43 | |
| 44 | /* |
| 45 | * hash_collapse() |
| 46 | */ |
| 47 | static u_int32_t |
| 48 | hash_collapse(s) |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 49 | u_char *s; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 50 | { |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 51 | int len, target; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 52 | u_int32_t i; |
| 53 | |
| 54 | if ((strlen(s) % sizeof(u_int32_t)) == 0) |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 55 | target = strlen(s); /* Multiple of 4 */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 56 | else |
| 57 | target = strlen(s) - (strlen(s) % sizeof(u_int32_t)); |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 58 | |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 59 | for (i = 0, len = 0; len < target; len += 4) |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 60 | i ^= ROUND(s + len); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 61 | |
| 62 | return i; |
| 63 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 64 | |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 65 | char * |
| 66 | skey_fake_keyinfo(char *username) |
| 67 | { |
| 68 | int i; |
| 69 | u_int ptr; |
| 70 | u_char hseed[SKEY_MAX_SEED_LEN], flg = 1, *up; |
| 71 | char pbuf[SKEY_MAX_PW_LEN+1]; |
| 72 | static char skeyprompt[SKEY_MAX_CHALLENGE+1]; |
| 73 | char *secret = NULL; |
| 74 | size_t secretlen = 0; |
Damien Miller | 25e4256 | 2000-01-11 10:59:47 +1100 | [diff] [blame] | 75 | SHA_CTX ctx; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 76 | char *p, *u; |
Damien Miller | 25e4256 | 2000-01-11 10:59:47 +1100 | [diff] [blame] | 77 | char md[SHA_DIGEST_LENGTH]; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 78 | |
| 79 | /* |
| 80 | * Base first 4 chars of seed on hostname. |
| 81 | * Add some filler for short hostnames if necessary. |
| 82 | */ |
| 83 | if (gethostname(pbuf, sizeof(pbuf)) == -1) |
| 84 | *(p = pbuf) = '.'; |
| 85 | else |
| 86 | for (p = pbuf; *p && isalnum(*p); p++) |
| 87 | if (isalpha(*p) && isupper(*p)) |
| 88 | *p = tolower(*p); |
| 89 | if (*p && pbuf - p < 4) |
| 90 | (void)strncpy(p, "asjd", 4 - (pbuf - p)); |
| 91 | pbuf[4] = '\0'; |
| 92 | |
| 93 | /* Hash the username if possible */ |
Damien Miller | 25e4256 | 2000-01-11 10:59:47 +1100 | [diff] [blame] | 94 | up = malloc(SHA_DIGEST_LENGTH); |
| 95 | if (up != NULL) { |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 96 | struct stat sb; |
| 97 | time_t t; |
| 98 | int fd; |
| 99 | |
Damien Miller | 25e4256 | 2000-01-11 10:59:47 +1100 | [diff] [blame] | 100 | SHA1_Init(&ctx); |
| 101 | SHA1_Update(&ctx, username, strlen(username)); |
| 102 | SHA1_End(&ctx, up); |
| 103 | |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 104 | /* Collapse the hash */ |
| 105 | ptr = hash_collapse(up); |
| 106 | memset(up, 0, strlen(up)); |
| 107 | |
| 108 | /* See if the random file's there, else use ctime */ |
| 109 | if ((fd = open(_SKEY_RAND_FILE_PATH_, O_RDONLY)) != -1 |
| 110 | && fstat(fd, &sb) == 0 && |
| 111 | sb.st_size > (off_t)SKEY_MAX_SEED_LEN && |
| 112 | lseek(fd, ptr % (sb.st_size - SKEY_MAX_SEED_LEN), |
| 113 | SEEK_SET) != -1 && read(fd, hseed, |
| 114 | SKEY_MAX_SEED_LEN) == SKEY_MAX_SEED_LEN) { |
| 115 | close(fd); |
Damien Miller | 037a0dc | 1999-12-07 15:38:31 +1100 | [diff] [blame] | 116 | fd = -1; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 117 | secret = hseed; |
| 118 | secretlen = SKEY_MAX_SEED_LEN; |
| 119 | flg = 0; |
| 120 | } else if (!stat(_PATH_MEM, &sb) || !stat("/", &sb)) { |
| 121 | t = sb.st_ctime; |
| 122 | secret = ctime(&t); |
| 123 | secretlen = strlen(secret); |
| 124 | flg = 0; |
| 125 | } |
Damien Miller | 037a0dc | 1999-12-07 15:38:31 +1100 | [diff] [blame] | 126 | if (fd != -1) |
| 127 | close(fd); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | /* Put that in your pipe and smoke it */ |
| 131 | if (flg == 0) { |
| 132 | /* Hash secret value with username */ |
Damien Miller | 25e4256 | 2000-01-11 10:59:47 +1100 | [diff] [blame] | 133 | SHA1_Init(&ctx); |
| 134 | SHA1_Update(&ctx, secret, secretlen); |
| 135 | SHA1_Update(&ctx, username, strlen(username)); |
| 136 | SHA1_End(&ctx, up); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 137 | |
| 138 | /* Zero out */ |
| 139 | memset(secret, 0, secretlen); |
| 140 | |
| 141 | /* Now hash the hash */ |
Damien Miller | 25e4256 | 2000-01-11 10:59:47 +1100 | [diff] [blame] | 142 | SHA1_Init(&ctx); |
| 143 | SHA1_Update(&ctx, up, strlen(up)); |
| 144 | SHA1_End(&ctx, up); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 145 | |
| 146 | ptr = hash_collapse(up + 4); |
| 147 | |
| 148 | for (i = 4; i < 9; i++) { |
| 149 | pbuf[i] = (ptr % 10) + '0'; |
| 150 | ptr /= 10; |
| 151 | } |
| 152 | pbuf[i] = '\0'; |
| 153 | |
| 154 | /* Sequence number */ |
| 155 | ptr = ((up[2] + up[3]) % 99) + 1; |
| 156 | |
Damien Miller | 25e4256 | 2000-01-11 10:59:47 +1100 | [diff] [blame] | 157 | memset(up, 0, SHA_DIGEST_LENGTH); /* SHA1 specific */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 158 | free(up); |
| 159 | |
| 160 | (void)snprintf(skeyprompt, sizeof skeyprompt, |
| 161 | "otp-%.*s %d %.*s", |
| 162 | SKEY_MAX_HASHNAME_LEN, |
| 163 | skey_get_algorithm(), |
| 164 | ptr, SKEY_MAX_SEED_LEN, |
| 165 | pbuf); |
| 166 | } else { |
| 167 | /* Base last 8 chars of seed on username */ |
| 168 | u = username; |
| 169 | i = 8; |
| 170 | p = &pbuf[4]; |
| 171 | do { |
| 172 | if (*u == 0) { |
| 173 | /* Pad remainder with zeros */ |
| 174 | while (--i >= 0) |
| 175 | *p++ = '0'; |
| 176 | break; |
| 177 | } |
| 178 | |
| 179 | *p++ = (*u++ % 10) + '0'; |
| 180 | } while (--i != 0); |
| 181 | pbuf[12] = '\0'; |
| 182 | |
| 183 | (void)snprintf(skeyprompt, sizeof skeyprompt, |
| 184 | "otp-%.*s %d %.*s", |
| 185 | SKEY_MAX_HASHNAME_LEN, |
| 186 | skey_get_algorithm(), |
| 187 | 99, SKEY_MAX_SEED_LEN, pbuf); |
| 188 | } |
| 189 | return skeyprompt; |
| 190 | } |
Damien Miller | 8029775 | 1999-11-19 13:03:25 +1100 | [diff] [blame] | 191 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 192 | #endif /* SKEY */ |