/
storage_transaction.c
378 lines (311 loc) · 8.39 KB
/
storage_transaction.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
#include "bsdtar_platform.h"
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include "netpacket.h"
#include "netproto.h"
#include "warnp.h"
#include "storage.h"
#include "storage_internal.h"
struct transaction_start_internal {
/* General state information. */
uint64_t machinenum;
int done;
/* Parameters used in starting a transaction. */
uint8_t lastseq[32];
uint8_t type;
uint8_t snonce[32];
uint8_t cnonce[32];
uint8_t seqnum[32];
int status;
};
struct transaction_commit_internal {
/* General state information. */
uint64_t machinenum;
int done;
/* Parameters used in committing a transaction. */
uint8_t seqnum[32];
uint8_t whichkey;
};
static int key_lookup(int);
static int storage_transaction_start(NETPACKET_CONNECTION *, uint64_t,
const uint8_t[32], uint8_t[32], uint8_t);
static sendpacket_callback callback_getnonce_send;
static handlepacket_callback callback_getnonce_response;
static handlepacket_callback callback_start_response;
static sendpacket_callback callback_commit_send;
static handlepacket_callback callback_commit_response;
/**
* key_lookup(type):
* Look up the key number for the operation ${type}.
*/
static int
key_lookup(int type)
{
int key;
switch (type) {
case 0:
key = CRYPTO_KEY_AUTH_PUT;
break;
case 1:
case 2:
key = CRYPTO_KEY_AUTH_DELETE;
break;
default:
warn0("Programmer error: Invalid transaction type");
key = -1;
break;
}
return (key);
}
/**
* storage_transaction_start(NPC, machinenum, lastseq, seqnum, type):
* Start a transaction of type ${type}.
*/
static int
storage_transaction_start(NETPACKET_CONNECTION * NPC, uint64_t machinenum,
const uint8_t lastseq[32], uint8_t seqnum[32], uint8_t type)
{
struct transaction_start_internal C;
/* Initialize transaction structure. */
C.machinenum = machinenum;
if (type != 2)
memcpy(C.lastseq, lastseq, 32);
else
memset(C.lastseq, 0, 32);
C.type = type;
C.done = 0;
/* Ask the netpacket layer to send a request and get a response. */
if (netpacket_op(NPC, callback_getnonce_send, &C))
goto err0;
/* Wait until the transaction has been started or failed. */
if (network_spin(&C.done))
goto err0;
/* Report sequence number mismatch if necessary. */
switch (C.status) {
case 0:
/* Success. */
break;
case 1:
warn0("Sequence number mismatch: Run --fsck");
goto err0;
default:
netproto_printerr(NETPROTO_STATUS_PROTERR);
goto err0;
}
/* Store sequence number of new transaction. */
memcpy(seqnum, C.seqnum, 32);
/* Success! */
return (0);
err0:
/* Failure! */
return (-1);
}
static int
callback_getnonce_send(void * cookie, NETPACKET_CONNECTION * NPC)
{
struct transaction_start_internal * C = cookie;
/* Ask the server to provide a transaction server nonce. */
return (netpacket_transaction_getnonce(NPC, C->machinenum,
callback_getnonce_response));
}
static int
callback_getnonce_response(void * cookie, NETPACKET_CONNECTION * NPC,
int status, uint8_t packettype, const uint8_t * packetbuf,
size_t packetlen)
{
struct transaction_start_internal * C = cookie;
(void)packetlen; /* UNUSED */
/* Handle read errors. */
if (status != NETWORK_STATUS_OK) {
netproto_printerr(status);
goto err0;
}
/* Make sure we received the right type of packet. */
if (packettype != NETPACKET_TRANSACTION_GETNONCE_RESPONSE)
goto err1;
/* Store the provided server nonce. */
memcpy(C->snonce, packetbuf, 32);
/* Generate a random client nonce. */
if (crypto_entropy_read(C->cnonce, 32))
goto err0;
/* Send a transaction start request. */
if (netpacket_transaction_start(NPC, C->machinenum,
C->type, C->snonce, C->cnonce, C->lastseq,
callback_start_response))
goto err0;
/* Success! */
return (0);
err1:
netproto_printerr(NETPROTO_STATUS_PROTERR);
err0:
/* Failure! */
return (-1);
}
static int
callback_start_response(void * cookie, NETPACKET_CONNECTION * NPC,
int status, uint8_t packettype, const uint8_t * packetbuf,
size_t packetlen)
{
struct transaction_start_internal * C = cookie;
int key;
(void)packetlen; /* UNUSED */
(void)NPC; /* UNUSED */
/* Handle read errors. */
if (status != NETWORK_STATUS_OK) {
netproto_printerr(status);
goto err0;
}
/* Make sure we received the right type of packet. */
if (packettype != NETPACKET_TRANSACTION_START_RESPONSE)
goto err1;
/* Compute transaction nonce. */
if (crypto_hash_data_2(CRYPTO_KEY_HMAC_SHA256, C->snonce, 32,
C->cnonce, 32, C->seqnum)) {
warn0("Programmer error: "
"SHA256 should never fail");
goto err0;
}
/* Look up packet signing key. */
if ((key = key_lookup(C->type)) == -1)
goto err0;
/* Verify packet hmac. */
switch (netpacket_hmac_verify(packettype, C->seqnum,
packetbuf, 1, key)) {
case 1:
goto err1;
case -1:
goto err0;
}
/* Store response code from server. */
C->status = packetbuf[0];
/* We're done! */
C->done = 1;
/* Success! */
return (0);
err1:
netproto_printerr(NETPROTO_STATUS_PROTERR);
err0:
/* Failure! */
return (-1);
}
/**
* storage_transaction_start_write(NPC, machinenum, lastseq, seqnum):
* Start a write transaction, presuming that ${lastseq} is the sequence
* number of the last committed transaction; and return the sequence number
* of the new transaction in ${seqnum}.
*/
int
storage_transaction_start_write(NETPACKET_CONNECTION * NPC,
uint64_t machinenum, const uint8_t lastseq[32], uint8_t seqnum[32])
{
return (storage_transaction_start(NPC, machinenum,
lastseq, seqnum, 0));
}
/**
* storage_transaction_start_delete(NPC, machinenum, lastseq, seqnum):
* As storage_transaction_start_delete, but s/write/delete/.
*/
int
storage_transaction_start_delete(NETPACKET_CONNECTION * NPC,
uint64_t machinenum, const uint8_t lastseq[32], uint8_t seqnum[32])
{
return (storage_transaction_start(NPC, machinenum,
lastseq, seqnum, 1));
}
/**
* storage_transaction_start_fsck(NPC, machinenum, seqnum):
* Start a fsck transaction, and return the sequence number of the new
* transaction in ${seqnum}.
*/
int
storage_transaction_start_fsck(NETPACKET_CONNECTION * NPC,
uint64_t machinenum, uint8_t seqnum[32])
{
return (storage_transaction_start(NPC, machinenum,
NULL, seqnum, 2));
}
/**
* storage_transaction_commit(machinenum, seqnum, whichkey):
* Commit the transaction ${seqnum} if it is the most recent uncommitted
* transaction. The value ${whichkey} specifies a key which should be used
* to sign the commit request: 0 if the write key should be used, and 1 if
* the delete key should be used.
*/
int
storage_transaction_commit(uint64_t machinenum, const uint8_t seqnum[32],
uint8_t whichkey)
{
struct transaction_commit_internal C;
NETPACKET_CONNECTION * NPC;
/* Initialize transaction structure. */
C.machinenum = machinenum;
memcpy(C.seqnum, seqnum, 32);
C.whichkey = whichkey;
C.done = 0;
/* Open netpacket connection. */
if ((NPC = netpacket_open()) == NULL)
goto err0;
/* Ask the netpacket layer to send a request and get a response. */
if (netpacket_op(NPC, callback_commit_send, &C))
goto err1;
/* Wait until the transaction has been committed or failed. */
if (network_spin(&C.done))
goto err1;
/* Close netpacket connection. */
if (netpacket_close(NPC))
goto err0;
/* Success! */
return (0);
err1:
netpacket_close(NPC);
err0:
/* Failure! */
return (-1);
}
static int
callback_commit_send(void * cookie, NETPACKET_CONNECTION * NPC)
{
struct transaction_commit_internal * C = cookie;
/* Ask the server if the file exists. */
return (netpacket_transaction_commit(NPC, C->machinenum, C->whichkey,
C->seqnum, callback_commit_response));
}
static int
callback_commit_response(void * cookie, NETPACKET_CONNECTION * NPC,
int status, uint8_t packettype, const uint8_t * packetbuf,
size_t packetlen)
{
struct transaction_commit_internal * C = cookie;
int key;
(void)packetlen; /* UNUSED */
(void)NPC; /* UNUSED */
/* Handle read errors. */
if (status != NETWORK_STATUS_OK) {
netproto_printerr(status);
goto err0;
}
/* Make sure we received the right type of packet. */
if (packettype != NETPACKET_TRANSACTION_COMMIT_RESPONSE)
goto err1;
/* Look up packet signing key. */
if ((key = key_lookup(C->whichkey)) == -1)
goto err0;
/* Verify packet hmac. */
switch (netpacket_hmac_verify(packettype, C->seqnum,
packetbuf, 0, key)) {
case 1:
goto err1;
case -1:
goto err0;
}
/* We're done! */
C->done = 1;
/* Success! */
return (0);
err1:
netproto_printerr(NETPROTO_STATUS_PROTERR);
err0:
/* Failure! */
return (-1);
}