2 * Copyright (c) 2011-2012 The DragonFly Project. All rights reserved.
4 * This code is derived from software contributed to The DragonFly Project
5 * by Matthew Dillon <dillon@dragonflybsd.org>
6 * by Venkatesh Srinivas <vsrinivas@dragonflybsd.org>
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in
16 * the documentation and/or other materials provided with the
18 * 3. Neither the name of The DragonFly Project nor the names of its
19 * contributors may be used to endorse or promote products derived
20 * from this software without specific, prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
28 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
30 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
31 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
32 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 #include <openssl/rsa.h>
39 #include <openssl/pem.h>
40 #include <openssl/err.h>
43 * Synchronously negotiate crypto for a new session. This must occur
44 * within 10 seconds or the connection is error'd out.
46 * We work off the IP address and/or reverse DNS. The IP address is
47 * checked first, followed by the IP address at various levels of granularity,
48 * followed by the full domain name and domain names at various levels of
51 * /etc/hammer2/remote/<name>.pub - Contains a public key
52 * /etc/hammer2/remote/<name>.none - Indicates no encryption (empty file)
53 * (e.g. localhost.none).
55 * We first attempt to locate a public key file based on the peer address or
58 * <name>.none - No further negotiation is needed. We simply return.
59 * All communication proceeds without encryption.
60 * No public key handshake occurs in this situation.
61 * (both ends must match).
63 * <name>.pub - We have located the public key for the peer. Both
64 * sides transmit a block encrypted with their private
65 * keys and the peer's public key.
67 * Both sides receive a block and decrypt it.
69 * Both sides formulate a reply using the decrypted
70 * block and transmit it.
72 * communication proceeds with the negotiated session
73 * key (typically AES-256-CBC).
75 * If we fail to locate the appropriate file and no floating.db exists the
76 * connection is terminated without further action.
78 * If floating.db exists the connection proceeds with a floating negotiation.
82 struct sockaddr_in sa_in;
83 struct sockaddr_in6 sa_in6;
87 hammer2_crypto_negotiate(hammer2_iocom_t *iocom)
90 socklen_t salen = sizeof(sa);
93 hammer2_handshake_t handtx;
94 hammer2_handshake_t handrx;
95 char buf[sizeof(handtx)];
100 RSA *keys[3] = { NULL, NULL, NULL };
108 * Get the peer IP address for the connection as a string.
110 if (getpeername(iocom->sock_fd, &sa.sa, &salen) < 0) {
111 iocom->ioq_rx.error = HAMMER2_IOQ_ERROR_NOPEER;
112 iocom->flags |= HAMMER2_IOCOMF_EOF;
114 fprintf(stderr, "accept: getpeername() failed\n");
117 if (getnameinfo(&sa.sa, salen, peername, sizeof(peername),
118 NULL, 0, NI_NUMERICHOST) < 0) {
119 iocom->ioq_rx.error = HAMMER2_IOQ_ERROR_NOPEER;
120 iocom->flags |= HAMMER2_IOCOMF_EOF;
122 fprintf(stderr, "accept: cannot decode sockaddr\n");
126 if (realhostname_sa(realname, sizeof(realname),
127 &sa.sa, salen) == HOSTNAME_FOUND) {
128 fprintf(stderr, "accept from %s (%s)\n",
131 fprintf(stderr, "accept from %s\n", peername);
136 * Find the remote host's public key
138 asprintf(&path, "%s/%s.pub", HAMMER2_PATH_REMOTE, peername);
139 if ((fp = fopen(path, "r")) == NULL) {
141 asprintf(&path, "%s/%s.none",
142 HAMMER2_PATH_REMOTE, peername);
143 if (stat(path, &st) < 0) {
144 iocom->ioq_rx.error = HAMMER2_IOQ_ERROR_NORKEY;
145 iocom->flags |= HAMMER2_IOCOMF_EOF;
147 fprintf(stderr, "auth failure: unknown host\n");
151 fprintf(stderr, "auth succeeded, unencrypted link\n");
154 keys[0] = PEM_read_RSA_PUBKEY(fp, NULL, NULL, NULL);
156 if (keys[0] == NULL) {
157 iocom->ioq_rx.error = HAMMER2_IOQ_ERROR_KEYFMT;
158 iocom->flags |= HAMMER2_IOCOMF_EOF;
161 "auth failure: bad key format\n");
167 * Get our public and private keys
170 asprintf(&path, HAMMER2_DEFAULT_DIR "/rsa.pub");
171 if ((fp = fopen(path, "r")) == NULL) {
172 iocom->ioq_rx.error = HAMMER2_IOQ_ERROR_NOLKEY;
173 iocom->flags |= HAMMER2_IOCOMF_EOF;
176 keys[1] = PEM_read_RSA_PUBKEY(fp, NULL, NULL, NULL);
178 if (keys[1] == NULL) {
179 iocom->ioq_rx.error = HAMMER2_IOQ_ERROR_KEYFMT;
180 iocom->flags |= HAMMER2_IOCOMF_EOF;
182 fprintf(stderr, "auth failure: bad host key format\n");
187 asprintf(&path, HAMMER2_DEFAULT_DIR "/rsa.prv");
188 if ((fp = fopen(path, "r")) == NULL) {
189 iocom->ioq_rx.error = HAMMER2_IOQ_ERROR_NOLKEY;
190 iocom->flags |= HAMMER2_IOCOMF_EOF;
192 fprintf(stderr, "auth failure: bad host key format\n");
195 keys[2] = PEM_read_RSAPrivateKey(fp, NULL, NULL, NULL);
197 if (keys[2] == NULL) {
198 iocom->ioq_rx.error = HAMMER2_IOQ_ERROR_KEYFMT;
199 iocom->flags |= HAMMER2_IOCOMF_EOF;
201 fprintf(stderr, "auth failure: bad host key format\n");
208 * public key encrypt/decrypt block size.
211 blksize = (size_t)RSA_size(keys[0]);
212 if (blksize != (size_t)RSA_size(keys[1]) ||
213 blksize != (size_t)RSA_size(keys[2]) ||
214 sizeof(handtx) % blksize != 0) {
215 iocom->ioq_rx.error = HAMMER2_IOQ_ERROR_KEYFMT;
216 iocom->flags |= HAMMER2_IOCOMF_EOF;
218 fprintf(stderr, "auth failure: "
219 "key size mismatch\n");
223 blksize = sizeof(handtx);
225 blkmask = blksize - 1;
227 bzero(&handrx, sizeof(handrx));
228 bzero(&handtx, sizeof(handtx));
231 * Fill all unused fields (particular all junk fields) with random
232 * data, and also set the session key.
234 fd = open("/dev/urandom", O_RDONLY);
236 fstat(fd, &st) < 0 || /* something wrong */
237 S_ISREG(st.st_mode) || /* supposed to be a RNG dev! */
238 read(fd, &handtx, sizeof(handtx)) != sizeof(handtx)) {
242 iocom->ioq_rx.error = HAMMER2_IOQ_ERROR_BADURANDOM;
243 iocom->flags |= HAMMER2_IOCOMF_EOF;
245 fprintf(stderr, "auth failure: bad rng\n");
248 if (bcmp(&handrx, &handtx, sizeof(handtx)) == 0)
249 goto urandfail; /* read all zeros */
251 ERR_load_crypto_strings();
254 * Handshake with the remote.
256 * Encrypt with my private and remote's public
257 * Decrypt with my private and remote's public
259 * When encrypting we have to make sure our buffer fits within the
260 * modulus, which typically requires bit 7 o the first byte to be
261 * zero. To be safe make sure that bit 7 and bit 6 is zero.
263 snprintf(handtx.quickmsg, sizeof(handtx.quickmsg), "Testing 1 2 3");
264 handtx.magic = HAMMER2_MSGHDR_MAGIC;
267 assert(sizeof(handtx.verf) * 4 == sizeof(handtx.sess));
268 bzero(handtx.verf, sizeof(handtx.verf));
270 handtx.pad1[0] &= 0x3f; /* message must fit within modulus */
271 handtx.pad2[0] &= 0x3f; /* message must fit within modulus */
273 for (i = 0; i < sizeof(handtx.sess); ++i)
274 handtx.verf[i / 4] ^= handtx.sess[i];
277 * Write handshake buffer to remote
279 for (i = 0; i < sizeof(handtx); i += blksize) {
280 ptr = (char *)&handtx + i;
283 * Since we are double-encrypting we have to make
284 * sure that the result of the first stage does
285 * not blow out the modulus for the second stage.
287 * The pointer is pointing to the pad*[] area so
288 * we can mess with that until the first stage
293 if (RSA_private_encrypt(blksize, ptr, buf,
294 keys[2], RSA_NO_PADDING) < 0) {
295 iocom->ioq_rx.error =
296 HAMMER2_IOQ_ERROR_KEYXCHGFAIL;
298 } while (buf[0] & 0xC0);
300 if (RSA_public_encrypt(blksize, buf, ptr,
301 keys[0], RSA_NO_PADDING) < 0) {
302 iocom->ioq_rx.error =
303 HAMMER2_IOQ_ERROR_KEYXCHGFAIL;
306 if (write(iocom->sock_fd, ptr, blksize) != (ssize_t)blksize) {
307 fprintf(stderr, "WRITE ERROR\n");
310 if (iocom->ioq_rx.error) {
311 iocom->flags |= HAMMER2_IOCOMF_EOF;
313 fprintf(stderr, "auth failure: key exchange failure "
314 "during encryption\n");
319 * Read handshake buffer from remote
322 while (i < sizeof(handrx)) {
323 ptr = (char *)&handrx + i;
324 n = read(iocom->sock_fd, ptr, blksize - (i & blkmask));
327 ptr -= (i & blkmask);
329 if (keys[0] && (i & blkmask) == 0) {
330 if (RSA_private_decrypt(blksize, ptr, buf,
331 keys[2], RSA_NO_PADDING) < 0)
332 iocom->ioq_rx.error =
333 HAMMER2_IOQ_ERROR_KEYXCHGFAIL;
334 if (RSA_public_decrypt(blksize, buf, ptr,
335 keys[0], RSA_NO_PADDING) < 0)
336 iocom->ioq_rx.error =
337 HAMMER2_IOQ_ERROR_KEYXCHGFAIL;
340 if (iocom->ioq_rx.error) {
341 iocom->flags |= HAMMER2_IOCOMF_EOF;
343 fprintf(stderr, "auth failure: key exchange failure "
344 "during decryption\n");
349 * Validate the received data. Try to make this a constant-time
352 if (i != sizeof(handrx)) {
354 iocom->ioq_rx.error = HAMMER2_IOQ_ERROR_KEYXCHGFAIL;
355 iocom->flags |= HAMMER2_IOCOMF_EOF;
357 fprintf(stderr, "auth failure: key exchange failure\n");
361 if (handrx.magic == HAMMER2_MSGHDR_MAGIC_REV) {
362 handrx.version = bswap16(handrx.version);
363 handrx.flags = bswap32(handrx.flags);
365 for (i = 0; i < sizeof(handrx.sess); ++i)
366 handrx.verf[i / 4] ^= handrx.sess[i];
368 for (i = 0; i < sizeof(handrx.verf); ++i)
370 if (handrx.version != 1)
376 fprintf(stderr, "Remote data: %s\n", handrx.quickmsg);