10 #include <openssl/sha1.h>
16 /* from %OpenBSD: skeylogin.c,v 1.32 1999/08/16 14:46:56 millert Exp % */
19 * try skey authentication,
20 * return 1 on success, 0 on failure, -1 if skey is not available
24 auth_skey_password(struct passwd * pw, const char *password)
26 if (strncasecmp(password, "s/key", 5) == 0) {
27 char *skeyinfo = skey_keyinfo(pw->pw_name);
28 if (skeyinfo == NULL) {
29 debug("generating fake skeyinfo for %.100s.",
31 skeyinfo = skey_fake_keyinfo(pw->pw_name);
34 packet_send_debug(skeyinfo);
37 } else if (skey_haskey(pw->pw_name) == 0 &&
38 skey_passcheck(pw->pw_name, (char *) password) != -1) {
39 /* Authentication succeeded. */
42 /* Fall back to ordinary passwd authentication. */
46 + /* from %OpenBSD: skeylogin.c,v 1.32 1999/08/16 14:46:56 millert Exp % */
48 #define ROUND(x) (((x)[0] << 24) + (((x)[1]) << 16) + (((x)[2]) << 8) + \
61 if ((strlen(s) % sizeof(u_int32_t)) == 0)
62 target = strlen(s); /* Multiple of 4 */
64 target = strlen(s) - (strlen(s) % sizeof(u_int32_t));
66 for (i = 0, len = 0; len < target; len += 4)
73 skey_fake_keyinfo(char *username)
77 u_char hseed[SKEY_MAX_SEED_LEN], flg = 1, *up;
78 char pbuf[SKEY_MAX_PW_LEN+1];
79 static char skeyprompt[SKEY_MAX_CHALLENGE+1];
86 * Base first 4 chars of seed on hostname.
87 * Add some filler for short hostnames if necessary.
89 if (gethostname(pbuf, sizeof(pbuf)) == -1)
92 for (p = pbuf; *p && isalnum(*p); p++)
93 if (isalpha(*p) && isupper(*p))
95 if (*p && pbuf - p < 4)
96 (void)strncpy(p, "asjd", 4 - (pbuf - p));
99 /* Hash the username if possible */
100 if ((up = SHA1Data(username, strlen(username), NULL)) != NULL) {
105 /* Collapse the hash */
106 ptr = hash_collapse(up);
107 memset(up, 0, strlen(up));
109 /* See if the random file's there, else use ctime */
110 if ((fd = open(_SKEY_RAND_FILE_PATH_, O_RDONLY)) != -1
111 && fstat(fd, &sb) == 0 &&
112 sb.st_size > (off_t)SKEY_MAX_SEED_LEN &&
113 lseek(fd, ptr % (sb.st_size - SKEY_MAX_SEED_LEN),
114 SEEK_SET) != -1 && read(fd, hseed,
115 SKEY_MAX_SEED_LEN) == SKEY_MAX_SEED_LEN) {
118 secretlen = SKEY_MAX_SEED_LEN;
120 } else if (!stat(_PATH_MEM, &sb) || !stat("/", &sb)) {
123 secretlen = strlen(secret);
128 /* Put that in your pipe and smoke it */
130 /* Hash secret value with username */
132 SHA1Update(&ctx, secret, secretlen);
133 SHA1Update(&ctx, username, strlen(username));
137 memset(secret, 0, secretlen);
139 /* Now hash the hash */
141 SHA1Update(&ctx, up, strlen(up));
144 ptr = hash_collapse(up + 4);
146 for (i = 4; i < 9; i++) {
147 pbuf[i] = (ptr % 10) + '0';
152 /* Sequence number */
153 ptr = ((up[2] + up[3]) % 99) + 1;
155 memset(up, 0, 20); /* SHA1 specific */
158 (void)snprintf(skeyprompt, sizeof skeyprompt,
160 SKEY_MAX_HASHNAME_LEN,
161 skey_get_algorithm(),
162 ptr, SKEY_MAX_SEED_LEN,
165 /* Base last 8 chars of seed on username */
171 /* Pad remainder with zeros */
177 *p++ = (*u++ % 10) + '0';
181 (void)snprintf(skeyprompt, sizeof skeyprompt,
183 SKEY_MAX_HASHNAME_LEN,
184 skey_get_algorithm(),
185 99, SKEY_MAX_SEED_LEN, pbuf);