]> andersk Git - openssh.git/blame - ssh-keysign.c
- deraadt@cvs.openbsd.org 2006/03/19 18:51:18
[openssh.git] / ssh-keysign.c
CommitLineData
39c00dc2 1/*
2 * Copyright (c) 2002 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#include "includes.h"
a75f5360 25
b5b88c19 26#ifdef HAVE_PATHS_H
a75f5360 27#include <paths.h>
b5b88c19 28#endif
39c00dc2 29
30#include <openssl/evp.h>
261189cc 31#include <openssl/rand.h>
32#include <openssl/rsa.h>
39c00dc2 33
34#include "log.h"
35#include "key.h"
60cd0a97 36#include "ssh.h"
39c00dc2 37#include "ssh2.h"
38#include "misc.h"
39#include "xmalloc.h"
40#include "buffer.h"
41#include "bufaux.h"
42#include "authfile.h"
43#include "msg.h"
44#include "canohost.h"
45#include "pathnames.h"
60cd0a97 46#include "readconf.h"
6213295d 47#include "uidswap.h"
60cd0a97 48
f811e52a 49/* XXX readconf.c needs these */
50uid_t original_real_uid;
39c00dc2 51
95d5ebf7 52extern char *__progname;
95d5ebf7 53
39c00dc2 54static int
55valid_request(struct passwd *pw, char *host, Key **ret, u_char *data,
56 u_int datalen)
57{
58 Buffer b;
16f1b5ca 59 Key *key = NULL;
39c00dc2 60 u_char *pkblob;
61 u_int blen, len;
62 char *pkalg, *p;
63 int pktype, fail;
64
65 fail = 0;
66
67 buffer_init(&b);
68 buffer_append(&b, data, datalen);
7203d6bb 69
1f143cf2 70 /* session id, currently limited to SHA1 (20 bytes) */
71 p = buffer_get_string(&b, &len);
72 if (len != 20)
73 fail++;
74 xfree(p);
75
39c00dc2 76 if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
77 fail++;
78
79 /* server user */
80 buffer_skip_string(&b);
81
82 /* service */
83 p = buffer_get_string(&b, NULL);
84 if (strcmp("ssh-connection", p) != 0)
85 fail++;
86 xfree(p);
87
88 /* method */
89 p = buffer_get_string(&b, NULL);
90 if (strcmp("hostbased", p) != 0)
91 fail++;
92 xfree(p);
93
94 /* pubkey */
95 pkalg = buffer_get_string(&b, NULL);
96 pkblob = buffer_get_string(&b, &blen);
97
98 pktype = key_type_from_name(pkalg);
99 if (pktype == KEY_UNSPEC)
100 fail++;
101 else if ((key = key_from_blob(pkblob, blen)) == NULL)
102 fail++;
103 else if (key->type != pktype)
104 fail++;
105 xfree(pkalg);
106 xfree(pkblob);
107
108 /* client host name, handle trailing dot */
109 p = buffer_get_string(&b, &len);
110 debug2("valid_request: check expect chost %s got %s", host, p);
111 if (strlen(host) != len - 1)
112 fail++;
113 else if (p[len - 1] != '.')
7203d6bb 114 fail++;
39c00dc2 115 else if (strncasecmp(host, p, len - 1) != 0)
7203d6bb 116 fail++;
39c00dc2 117 xfree(p);
118
119 /* local user */
120 p = buffer_get_string(&b, NULL);
121
122 if (strcmp(pw->pw_name, p) != 0)
123 fail++;
124 xfree(p);
125
126 /* end of message */
127 if (buffer_len(&b) != 0)
128 fail++;
43f7a4b8 129 buffer_free(&b);
39c00dc2 130
131 debug3("valid_request: fail %d", fail);
132
133 if (fail && key != NULL)
134 key_free(key);
135 else
136 *ret = key;
137
138 return (fail ? -1 : 0);
139}
140
141int
142main(int argc, char **argv)
143{
144 Buffer b;
60cd0a97 145 Options options;
39c00dc2 146 Key *keys[2], *key;
147 struct passwd *pw;
148 int key_fd[2], i, found, version = 2, fd;
149 u_char *signature, *data;
150 char *host;
151 u_int slen, dlen;
261189cc 152 u_int32_t rnd[256];
39c00dc2 153
fd6168c1 154 /* Ensure that stdin and stdout are connected */
155 if ((fd = open(_PATH_DEVNULL, O_RDWR)) < 2)
156 exit(1);
157 /* Leave /dev/null fd iff it is attached to stderr */
158 if (fd > 2)
159 close(fd);
160
39c00dc2 161 key_fd[0] = open(_PATH_HOST_RSA_KEY_FILE, O_RDONLY);
162 key_fd[1] = open(_PATH_HOST_DSA_KEY_FILE, O_RDONLY);
163
baab9e74 164 original_real_uid = getuid(); /* XXX readconf.c needs this */
165 if ((pw = getpwuid(original_real_uid)) == NULL)
6213295d 166 fatal("getpwuid failed");
167 pw = pwcopy(pw);
168
169 permanently_set_uid(pw);
39c00dc2 170
875ec275 171 init_rng();
172 seed_rng();
173 arc4random_stir();
04eb391d 174
39c00dc2 175#ifdef DEBUG_SSH_KEYSIGN
176 log_init("ssh-keysign", SYSLOG_LEVEL_DEBUG3, SYSLOG_FACILITY_AUTH, 0);
7203d6bb 177#endif
39c00dc2 178
60cd0a97 179 /* verify that ssh-keysign is enabled by the admin */
60cd0a97 180 initialize_options(&options);
9f6cab4b 181 (void)read_config_file(_PATH_HOST_CONFIG_FILE, "", &options, 0);
60cd0a97 182 fill_default_options(&options);
cc46e2ee 183 if (options.enable_ssh_keysign != 1)
184 fatal("ssh-keysign not enabled in %s",
60cd0a97 185 _PATH_HOST_CONFIG_FILE);
186
39c00dc2 187 if (key_fd[0] == -1 && key_fd[1] == -1)
188 fatal("could not open any host key");
189
39c00dc2 190 SSLeay_add_all_algorithms();
261189cc 191 for (i = 0; i < 256; i++)
192 rnd[i] = arc4random();
193 RAND_seed(rnd, sizeof(rnd));
39c00dc2 194
195 found = 0;
196 for (i = 0; i < 2; i++) {
197 keys[i] = NULL;
198 if (key_fd[i] == -1)
199 continue;
200 keys[i] = key_load_private_pem(key_fd[i], KEY_UNSPEC,
201 NULL, NULL);
202 close(key_fd[i]);
203 if (keys[i] != NULL)
204 found = 1;
205 }
206 if (!found)
207 fatal("no hostkey found");
208
209 buffer_init(&b);
e987fdbe 210 if (ssh_msg_recv(STDIN_FILENO, &b) < 0)
211 fatal("ssh_msg_recv failed");
39c00dc2 212 if (buffer_get_char(&b) != version)
213 fatal("bad version");
214 fd = buffer_get_int(&b);
215 if ((fd == STDIN_FILENO) || (fd == STDOUT_FILENO))
216 fatal("bad fd");
217 if ((host = get_local_name(fd)) == NULL)
218 fatal("cannot get sockname for fd");
7203d6bb 219
39c00dc2 220 data = buffer_get_string(&b, &dlen);
221 if (valid_request(pw, host, &key, data, dlen) < 0)
222 fatal("not a valid request");
39c00dc2 223 xfree(host);
224
225 found = 0;
226 for (i = 0; i < 2; i++) {
227 if (keys[i] != NULL &&
228 key_equal(key, keys[i])) {
229 found = 1;
230 break;
231 }
232 }
233 if (!found)
234 fatal("no matching hostkey found");
235
236 if (key_sign(keys[i], &signature, &slen, data, dlen) != 0)
237 fatal("key_sign failed");
20e79e98 238 xfree(data);
7203d6bb 239
39c00dc2 240 /* send reply */
241 buffer_clear(&b);
242 buffer_put_string(&b, signature, slen);
d3cbe6f8 243 if (ssh_msg_send(STDOUT_FILENO, version, &b) == -1)
244 fatal("ssh_msg_send failed");
39c00dc2 245
246 return (0);
247}
This page took 0.242962 seconds and 5 git commands to generate.