]> andersk Git - openssh.git/blame - ssh-keysign.c
- markus@cvs.openbsd.org 2002/06/08 05:07:56
[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"
25RCSID("$OpenBSD: ssh-keysign.c,v 1.2 2002/05/31 10:30:33 markus Exp $");
26
27#include <openssl/evp.h>
28
29#include "log.h"
30#include "key.h"
31#include "ssh2.h"
32#include "misc.h"
33#include "xmalloc.h"
34#include "buffer.h"
35#include "bufaux.h"
36#include "authfile.h"
37#include "msg.h"
38#include "canohost.h"
39#include "pathnames.h"
40
95d5ebf7 41#ifdef HAVE___PROGNAME
42extern char *__progname;
43#else
44char *__progname;
45#endif
46
39c00dc2 47static int
48valid_request(struct passwd *pw, char *host, Key **ret, u_char *data,
49 u_int datalen)
50{
51 Buffer b;
52 Key *key;
53 u_char *pkblob;
54 u_int blen, len;
55 char *pkalg, *p;
56 int pktype, fail;
57
58 fail = 0;
59
60 buffer_init(&b);
61 buffer_append(&b, data, datalen);
62
63 /* session id */
64 buffer_skip_string(&b);
65 if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
66 fail++;
67
68 /* server user */
69 buffer_skip_string(&b);
70
71 /* service */
72 p = buffer_get_string(&b, NULL);
73 if (strcmp("ssh-connection", p) != 0)
74 fail++;
75 xfree(p);
76
77 /* method */
78 p = buffer_get_string(&b, NULL);
79 if (strcmp("hostbased", p) != 0)
80 fail++;
81 xfree(p);
82
83 /* pubkey */
84 pkalg = buffer_get_string(&b, NULL);
85 pkblob = buffer_get_string(&b, &blen);
86
87 pktype = key_type_from_name(pkalg);
88 if (pktype == KEY_UNSPEC)
89 fail++;
90 else if ((key = key_from_blob(pkblob, blen)) == NULL)
91 fail++;
92 else if (key->type != pktype)
93 fail++;
94 xfree(pkalg);
95 xfree(pkblob);
96
97 /* client host name, handle trailing dot */
98 p = buffer_get_string(&b, &len);
99 debug2("valid_request: check expect chost %s got %s", host, p);
100 if (strlen(host) != len - 1)
101 fail++;
102 else if (p[len - 1] != '.')
103 fail++;
104 else if (strncasecmp(host, p, len - 1) != 0)
105 fail++;
106 xfree(p);
107
108 /* local user */
109 p = buffer_get_string(&b, NULL);
110
111 if (strcmp(pw->pw_name, p) != 0)
112 fail++;
113 xfree(p);
114
115 /* end of message */
116 if (buffer_len(&b) != 0)
117 fail++;
118
119 debug3("valid_request: fail %d", fail);
120
121 if (fail && key != NULL)
122 key_free(key);
123 else
124 *ret = key;
125
126 return (fail ? -1 : 0);
127}
128
129int
130main(int argc, char **argv)
131{
132 Buffer b;
133 Key *keys[2], *key;
134 struct passwd *pw;
135 int key_fd[2], i, found, version = 2, fd;
136 u_char *signature, *data;
137 char *host;
138 u_int slen, dlen;
139
140 key_fd[0] = open(_PATH_HOST_RSA_KEY_FILE, O_RDONLY);
141 key_fd[1] = open(_PATH_HOST_DSA_KEY_FILE, O_RDONLY);
142
143 seteuid(getuid());
144 setuid(getuid());
145
875ec275 146 init_rng();
147 seed_rng();
148 arc4random_stir();
04eb391d 149
39c00dc2 150#ifdef DEBUG_SSH_KEYSIGN
151 log_init("ssh-keysign", SYSLOG_LEVEL_DEBUG3, SYSLOG_FACILITY_AUTH, 0);
152#endif
153
154 if (key_fd[0] == -1 && key_fd[1] == -1)
155 fatal("could not open any host key");
156
157 if ((pw = getpwuid(getuid())) == NULL)
158 fatal("getpwuid failed");
159 pw = pwcopy(pw);
160
161 SSLeay_add_all_algorithms();
162
163 found = 0;
164 for (i = 0; i < 2; i++) {
165 keys[i] = NULL;
166 if (key_fd[i] == -1)
167 continue;
168 keys[i] = key_load_private_pem(key_fd[i], KEY_UNSPEC,
169 NULL, NULL);
170 close(key_fd[i]);
171 if (keys[i] != NULL)
172 found = 1;
173 }
174 if (!found)
175 fatal("no hostkey found");
176
177 buffer_init(&b);
178 if (msg_recv(STDIN_FILENO, &b) < 0)
179 fatal("msg_recv failed");
180 if (buffer_get_char(&b) != version)
181 fatal("bad version");
182 fd = buffer_get_int(&b);
183 if ((fd == STDIN_FILENO) || (fd == STDOUT_FILENO))
184 fatal("bad fd");
185 if ((host = get_local_name(fd)) == NULL)
186 fatal("cannot get sockname for fd");
187
188 data = buffer_get_string(&b, &dlen);
189 if (valid_request(pw, host, &key, data, dlen) < 0)
190 fatal("not a valid request");
191 xfree(data);
192 xfree(host);
193
194 found = 0;
195 for (i = 0; i < 2; i++) {
196 if (keys[i] != NULL &&
197 key_equal(key, keys[i])) {
198 found = 1;
199 break;
200 }
201 }
202 if (!found)
203 fatal("no matching hostkey found");
204
205 if (key_sign(keys[i], &signature, &slen, data, dlen) != 0)
206 fatal("key_sign failed");
207
208 /* send reply */
209 buffer_clear(&b);
210 buffer_put_string(&b, signature, slen);
211 msg_send(STDOUT_FILENO, version, &b);
212
213 return (0);
214}
This page took 0.135934 seconds and 5 git commands to generate.