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
36 #include <openssl/rsa.h> /* public/private key functions */
37 #include <openssl/pem.h> /* public/private key file load */
38 #include <openssl/err.h>
39 #include <openssl/evp.h> /* aes_256_cbc functions */
41 /***************************************************************************
43 ***************************************************************************
45 * The initial public-key exchange is implementing by transmitting a
46 * 512-byte buffer to the other side in a symmetrical fashion. This
47 * buffer contains the following:
49 * (1) A random session key. 512 bits is specified. We use aes_256_cbc()
50 * and initialize the key with the first 256 bits and the iv[] with
51 * the second. Note that the transmitted and received session
52 * keys are XOR'd together to create the session key used for
53 * communications (so even if the verifier is compromised the session
54 * will still be gobbly gook if the public key has not been completely
57 * (2) A verifier to determine that the decode was successful. It encodes
58 * an XOR of each group of 4 bytes from the session key.
60 * (3) Additional configuration and additional random data.
62 * - The hammer2 message header magic for endian detect
64 * - The hammer2 protocol version. The two sides agree on the
67 * - All unused fields (junk*) are filled with random data.
69 * This structure must be exactly 512 bytes and expects to use 256-byte
72 struct hammer2_handshake {
73 char pad1[8]; /* 000 */
74 uint16_t magic; /* 008 HAMMER2_MSGHDR_MAGIC for endian detect */
75 uint16_t version; /* 00A hammer2 protocol version */
76 uint32_t flags; /* 00C protocol extension flags */
77 uint8_t sess[64]; /* 010 512-bit session key */
78 uint8_t verf[16]; /* 050 verifier = ~sess */
79 char quickmsg[32]; /* 060 reason for connecting */
80 char junk080[128]; /* 080-0FF */
81 char pad2[8]; /* 100-107 */
82 char junk100[256-8]; /* 108-1FF */
85 typedef struct hammer2_handshake hammer2_handshake_t;
87 #define HAMMER2_AES_KEY_SIZE 32
88 #define HAMMER2_AES_KEY_MASK (HAMMER2_AES_KEY_SIZE - 1)
89 #define HAMMER2_AES_TYPE aes_256_cbc
90 #define HAMMER2_AES_TYPE_EVP EVP_aes_256_cbc()
91 #define HAMMER2_AES_TYPE_STR #HAMMER2_AES_TYPE
93 /***************************************************************************
94 * LOW LEVEL MESSAGING *
95 ***************************************************************************
97 * hammer2_msg - A standalone copy of a message, typically referenced by
98 * or embedded in other structures, or used with I/O queues.
100 * These structures are strictly temporary, so they do not have to be
101 * particularly optimized for size. All possible message headers are
102 * directly embedded (any), and the message may contain a reference
103 * to allocated auxillary data. The structure is recycled quite often
106 * This structure is typically not used for storing persistent message
107 * state (see hammer2_persist for that).
109 struct hammer2_iocom;
110 struct hammer2_persist;
111 struct hammre2_state;
114 TAILQ_HEAD(hammer2_msg_queue, hammer2_msg);
115 RB_HEAD(hammer2_state_tree, hammer2_state);
117 struct hammer2_state {
118 RB_ENTRY(hammer2_state) rbnode; /* indexed by msgid */
119 struct hammer2_iocom *iocom;
120 uint32_t txcmd; /* mostly for CMDF flags */
121 uint32_t rxcmd; /* mostly for CMDF flags */
122 uint16_t source; /* command originator */
123 uint16_t target; /* reply originator */
124 uint32_t msgid; /* {source,target,msgid} uniq */
127 struct hammer2_msg *msg;
128 int (*func)(struct hammer2_iocom *, struct hammer2_msg *);
131 #define HAMMER2_STATE_INSERTED 0x0001
132 #define HAMMER2_STATE_DYNAMIC 0x0002
135 TAILQ_ENTRY(hammer2_msg) qentry;
136 struct hammer2_state *state;
143 typedef struct hammer2_state hammer2_state_t;
144 typedef struct hammer2_msg hammer2_msg_t;
145 typedef struct hammer2_msg_queue hammer2_msg_queue_t;
147 int hammer2_state_cmp(hammer2_state_t *state1, hammer2_state_t *state2);
148 RB_PROTOTYPE(hammer2_state_tree, hammer2_state, rbnode, hammer2_state_cmp);
151 * hammer2_ioq - An embedded component of hammer2_connect, holds state
152 * for the buffering and parsing of incoming and outgoing messages.
155 enum { HAMMER2_MSGQ_STATE_HEADER1,
156 HAMMER2_MSGQ_STATE_HEADER2,
157 HAMMER2_MSGQ_STATE_AUXDATA1,
158 HAMMER2_MSGQ_STATE_AUXDATA2,
159 HAMMER2_MSGQ_STATE_ERROR } state;
160 int fifo_beg; /* buffered data */
161 int fifo_cdx; /* encrypt/decrypt index */
163 size_t hbytes; /* header size */
164 size_t abytes; /* aux_data size */
165 size_t already; /* aux_data already decrypted */
167 int seq; /* salt sequencer */
170 char iv[HAMMER2_AES_KEY_SIZE]; /* encrypt or decrypt iv[] */
172 hammer2_msg_queue_t msgq;
173 char buf[HAMMER2_MSGBUF_SIZE]; /* staging buffer */
176 typedef struct hammer2_ioq hammer2_ioq_t;
178 #define HAMMER2_IOQ_ERROR_SYNC 1 /* bad magic / out of sync */
179 #define HAMMER2_IOQ_ERROR_EOF 2 /* unexpected EOF */
180 #define HAMMER2_IOQ_ERROR_SOCK 3 /* read() error on socket */
181 #define HAMMER2_IOQ_ERROR_FIELD 4 /* invalid field */
182 #define HAMMER2_IOQ_ERROR_HCRC 5 /* core header crc bad */
183 #define HAMMER2_IOQ_ERROR_XCRC 6 /* ext header crc bad */
184 #define HAMMER2_IOQ_ERROR_ACRC 7 /* aux data crc bad */
185 #define HAMMER2_IOQ_ERROR_STATE 8 /* bad state */
186 #define HAMMER2_IOQ_ERROR_NOPEER 9 /* bad socket peer */
187 #define HAMMER2_IOQ_ERROR_NORKEY 10 /* no remote keyfile found */
188 #define HAMMER2_IOQ_ERROR_NOLKEY 11 /* no local keyfile found */
189 #define HAMMER2_IOQ_ERROR_KEYXCHGFAIL 12 /* key exchange failed */
190 #define HAMMER2_IOQ_ERROR_KEYFMT 13 /* key file format problem */
191 #define HAMMER2_IOQ_ERROR_BADURANDOM 14 /* /dev/urandom is bad */
192 #define HAMMER2_IOQ_ERROR_MSGSEQ 15 /* message sequence error */
193 #define HAMMER2_IOQ_ERROR_EALREADY 16 /* ignore this message */
194 #define HAMMER2_IOQ_ERROR_TRANS 17 /* state transaction issue */
196 #define HAMMER2_IOQ_MAXIOVEC 16
199 * hammer2_iocom - governs a messaging stream connection
201 struct hammer2_iocom {
202 hammer2_ioq_t ioq_rx;
203 hammer2_ioq_t ioq_tx;
204 hammer2_msg_queue_t freeq; /* free msgs hdr only */
205 hammer2_msg_queue_t freeq_aux; /* free msgs w/aux_data */
206 void (*recvmsg_callback)(struct hammer2_iocom *);
207 void (*sendmsg_callback)(struct hammer2_iocom *);
208 void (*altmsg_callback)(struct hammer2_iocom *);
209 int sock_fd; /* comm socket or pipe */
210 int alt_fd; /* thread signal, tty, etc */
214 char sess[HAMMER2_AES_KEY_SIZE]; /* aes_256_cbc key */
215 struct hammer2_state_tree staterd_tree; /* active messages */
216 struct hammer2_state_tree statewr_tree; /* active messages */
219 typedef struct hammer2_iocom hammer2_iocom_t;
221 #define HAMMER2_IOCOMF_EOF 0x00000001 /* EOF or ERROR on desc */
222 #define HAMMER2_IOCOMF_RREQ 0x00000002 /* request read-data event */
223 #define HAMMER2_IOCOMF_WREQ 0x00000004 /* request write-avail event */
224 #define HAMMER2_IOCOMF_WIDLE 0x00000008 /* request write-avail event */
225 #define HAMMER2_IOCOMF_SIGNAL 0x00000010
226 #define HAMMER2_IOCOMF_CRYPTED 0x00000020 /* encrypt enabled */