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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
|
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/
#include "tls/s2n_async_pkey.h"
#include "crypto/s2n_hash.h"
#include "crypto/s2n_signature.h"
#include "error/s2n_errno.h"
#include "s2n.h"
#include "tls/s2n_connection.h"
#include "tls/s2n_handshake.h"
#include "utils/s2n_blob.h"
#include "utils/s2n_mem.h"
#include "utils/s2n_result.h"
#include "utils/s2n_safety.h"
typedef enum { S2N_ASYNC_DECRYPT, S2N_ASYNC_SIGN } s2n_async_pkey_op_type;
struct s2n_async_pkey_decrypt_data {
s2n_async_pkey_decrypt_complete on_complete;
struct s2n_blob encrypted;
struct s2n_blob decrypted;
unsigned rsa_failed : 1;
};
struct s2n_async_pkey_sign_data {
s2n_async_pkey_sign_complete on_complete;
struct s2n_hash_state digest;
s2n_signature_algorithm sig_alg;
struct s2n_blob signature;
};
struct s2n_async_pkey_op {
s2n_async_pkey_op_type type;
struct s2n_connection *conn;
unsigned complete : 1;
unsigned applied : 1;
union {
struct s2n_async_pkey_decrypt_data decrypt;
struct s2n_async_pkey_sign_data sign;
} op;
};
struct s2n_async_pkey_op_actions {
S2N_RESULT (*perform)(struct s2n_async_pkey_op *op, s2n_cert_private_key *pkey);
S2N_RESULT (*apply)(struct s2n_async_pkey_op *op, struct s2n_connection *conn);
S2N_RESULT (*free)(struct s2n_async_pkey_op *op);
};
static S2N_RESULT s2n_async_get_actions(s2n_async_pkey_op_type type, const struct s2n_async_pkey_op_actions **actions);
static S2N_RESULT s2n_async_pkey_op_allocate(struct s2n_async_pkey_op **op);
static S2N_RESULT s2n_async_pkey_sign_async(struct s2n_connection *conn, s2n_signature_algorithm sig_alg,
struct s2n_hash_state *digest, s2n_async_pkey_sign_complete on_complete);
static S2N_RESULT s2n_async_pkey_sign_sync(struct s2n_connection *conn, s2n_signature_algorithm sig_alg,
struct s2n_hash_state *digest, s2n_async_pkey_sign_complete on_complete);
static S2N_RESULT s2n_async_pkey_decrypt_async(struct s2n_connection *conn, struct s2n_blob *encrypted,
struct s2n_blob * init_decrypted,
s2n_async_pkey_decrypt_complete on_complete);
static S2N_RESULT s2n_async_pkey_decrypt_sync(struct s2n_connection *conn, struct s2n_blob *encrypted,
struct s2n_blob * init_decrypted,
s2n_async_pkey_decrypt_complete on_complete);
static S2N_RESULT s2n_async_pkey_decrypt_perform(struct s2n_async_pkey_op *op, s2n_cert_private_key *pkey);
static S2N_RESULT s2n_async_pkey_decrypt_apply(struct s2n_async_pkey_op *op, struct s2n_connection *conn);
static S2N_RESULT s2n_async_pkey_decrypt_free(struct s2n_async_pkey_op *op);
static S2N_RESULT s2n_async_pkey_sign_perform(struct s2n_async_pkey_op *op, s2n_cert_private_key *pkey);
static S2N_RESULT s2n_async_pkey_sign_apply(struct s2n_async_pkey_op *op, struct s2n_connection *conn);
static S2N_RESULT s2n_async_pkey_sign_free(struct s2n_async_pkey_op *op);
static const struct s2n_async_pkey_op_actions s2n_async_pkey_decrypt_op = { .perform = &s2n_async_pkey_decrypt_perform,
.apply = &s2n_async_pkey_decrypt_apply,
.free = &s2n_async_pkey_decrypt_free };
static const struct s2n_async_pkey_op_actions s2n_async_pkey_sign_op = { .perform = &s2n_async_pkey_sign_perform,
.apply = &s2n_async_pkey_sign_apply,
.free = &s2n_async_pkey_sign_free };
DEFINE_POINTER_CLEANUP_FUNC(struct s2n_async_pkey_op *, s2n_async_pkey_op_free);
static S2N_RESULT s2n_async_get_actions(s2n_async_pkey_op_type type, const struct s2n_async_pkey_op_actions **actions)
{
ENSURE_REF(actions);
switch (type) {
case S2N_ASYNC_DECRYPT:
*actions = &s2n_async_pkey_decrypt_op;
return S2N_RESULT_OK;
case S2N_ASYNC_SIGN:
*actions = &s2n_async_pkey_sign_op;
return S2N_RESULT_OK;
/* No default for compiler warnings */
}
return S2N_RESULT_ERROR;
}
static S2N_RESULT s2n_async_pkey_op_allocate(struct s2n_async_pkey_op **op)
{
ENSURE_REF(op);
ENSURE(*op == NULL, S2N_ERR_SAFETY);
/* allocate memory */
DEFER_CLEANUP(struct s2n_blob mem = {0}, s2n_free);
GUARD_AS_RESULT(s2n_alloc(&mem, sizeof(struct s2n_async_pkey_op)));
GUARD_AS_RESULT(s2n_blob_zero(&mem));
*op = (void *) mem.data;
if (s2n_blob_init(&mem, NULL, 0) != S2N_SUCCESS) {
*op = NULL;
return S2N_RESULT_ERROR;
}
return S2N_RESULT_OK;
}
S2N_RESULT s2n_async_pkey_decrypt(struct s2n_connection *conn, struct s2n_blob *encrypted,
struct s2n_blob *init_decrypted, s2n_async_pkey_decrypt_complete on_complete)
{
ENSURE_REF(conn);
ENSURE_REF(encrypted);
ENSURE_REF(init_decrypted);
ENSURE_REF(on_complete);
if (conn->config->async_pkey_cb) {
GUARD_RESULT(s2n_async_pkey_decrypt_async(conn, encrypted, init_decrypted, on_complete));
} else {
GUARD_RESULT(s2n_async_pkey_decrypt_sync(conn, encrypted, init_decrypted, on_complete));
}
return S2N_RESULT_OK;
}
S2N_RESULT s2n_async_pkey_decrypt_async(struct s2n_connection *conn, struct s2n_blob *encrypted,
struct s2n_blob *init_decrypted, s2n_async_pkey_decrypt_complete on_complete)
{
ENSURE_REF(conn);
ENSURE_REF(encrypted);
ENSURE_REF(init_decrypted);
ENSURE_REF(on_complete);
ENSURE(conn->handshake.async_state == S2N_ASYNC_NOT_INVOKED, S2N_ERR_ASYNC_MORE_THAN_ONE);
DEFER_CLEANUP(struct s2n_async_pkey_op *op = NULL, s2n_async_pkey_op_free_pointer);
GUARD_RESULT(s2n_async_pkey_op_allocate(&op));
op->type = S2N_ASYNC_DECRYPT;
op->conn = conn;
struct s2n_async_pkey_decrypt_data *decrypt = &op->op.decrypt;
decrypt->on_complete = on_complete;
GUARD_AS_RESULT(s2n_dup(encrypted, &decrypt->encrypted));
GUARD_AS_RESULT(s2n_dup(init_decrypted, &decrypt->decrypted));
/* Block the handshake and set async state to invoking to block async states */
GUARD_AS_RESULT(s2n_conn_set_handshake_read_block(conn));
conn->handshake.async_state = S2N_ASYNC_INVOKING_CALLBACK;
/* Move op to tmp to avoid DEFER_CLEANUP freeing the op, as it will be owned by callback */
struct s2n_async_pkey_op *tmp_op = op;
op = NULL;
ENSURE(conn->config->async_pkey_cb(conn, tmp_op) == S2N_SUCCESS, S2N_ERR_ASYNC_CALLBACK_FAILED);
/* Set state to waiting to allow op to be consumed by connection */
conn->handshake.async_state = S2N_ASYNC_INVOKED_WAITING;
/* Return an async blocked error to drop out of s2n_negotiate loop */
BAIL(S2N_ERR_ASYNC_BLOCKED);
}
S2N_RESULT s2n_async_pkey_decrypt_sync(struct s2n_connection *conn, struct s2n_blob *encrypted,
struct s2n_blob *init_decrypted, s2n_async_pkey_decrypt_complete on_complete)
{
ENSURE_REF(conn);
ENSURE_REF(encrypted);
ENSURE_REF(init_decrypted);
ENSURE_REF(on_complete);
const struct s2n_pkey *pkey = conn->handshake_params.our_chain_and_key->private_key;
bool rsa_failed = s2n_pkey_decrypt(pkey, encrypted, init_decrypted) != S2N_SUCCESS;
GUARD_AS_RESULT(on_complete(conn, rsa_failed, init_decrypted));
return S2N_RESULT_OK;
}
S2N_RESULT s2n_async_pkey_sign(struct s2n_connection *conn, s2n_signature_algorithm sig_alg,
struct s2n_hash_state *digest, s2n_async_pkey_sign_complete on_complete)
{
ENSURE_REF(conn);
ENSURE_REF(digest);
ENSURE_REF(on_complete);
if (conn->config->async_pkey_cb) {
GUARD_RESULT(s2n_async_pkey_sign_async(conn, sig_alg, digest, on_complete));
} else {
GUARD_RESULT(s2n_async_pkey_sign_sync(conn, sig_alg, digest, on_complete));
}
return S2N_RESULT_OK;
}
S2N_RESULT s2n_async_pkey_sign_async(struct s2n_connection *conn, s2n_signature_algorithm sig_alg,
struct s2n_hash_state *digest, s2n_async_pkey_sign_complete on_complete)
{
ENSURE_REF(conn);
ENSURE_REF(digest);
ENSURE_REF(on_complete);
ENSURE(conn->handshake.async_state == S2N_ASYNC_NOT_INVOKED, S2N_ERR_ASYNC_MORE_THAN_ONE);
DEFER_CLEANUP(struct s2n_async_pkey_op *op = NULL, s2n_async_pkey_op_free_pointer);
GUARD_RESULT(s2n_async_pkey_op_allocate(&op));
op->type = S2N_ASYNC_SIGN;
op->conn = conn;
struct s2n_async_pkey_sign_data *sign = &op->op.sign;
sign->on_complete = on_complete;
sign->sig_alg = sig_alg;
GUARD_AS_RESULT(s2n_hash_new(&sign->digest));
GUARD_AS_RESULT(s2n_hash_copy(&sign->digest, digest));
/* Block the handshake and set async state to invoking to block async states */
GUARD_AS_RESULT(s2n_conn_set_handshake_read_block(conn));
conn->handshake.async_state = S2N_ASYNC_INVOKING_CALLBACK;
/* Move op to tmp to avoid DEFER_CLEANUP freeing the op, as it will be owned by callback */
struct s2n_async_pkey_op *tmp_op = op;
op = NULL;
ENSURE(conn->config->async_pkey_cb(conn, tmp_op) == S2N_SUCCESS, S2N_ERR_ASYNC_CALLBACK_FAILED);
/* Set state to waiting to allow op to be consumed by connection */
conn->handshake.async_state = S2N_ASYNC_INVOKED_WAITING;
/* Return an async blocked error to drop out of s2n_negotiate loop */
BAIL(S2N_ERR_ASYNC_BLOCKED);
}
S2N_RESULT s2n_async_pkey_sign_sync(struct s2n_connection *conn, s2n_signature_algorithm sig_alg,
struct s2n_hash_state *digest, s2n_async_pkey_sign_complete on_complete)
{
ENSURE_REF(conn);
ENSURE_REF(digest);
ENSURE_REF(on_complete);
const struct s2n_pkey *pkey = conn->handshake_params.our_chain_and_key->private_key;
DEFER_CLEANUP(struct s2n_blob signed_content = { 0 }, s2n_free);
uint32_t maximum_signature_length = s2n_pkey_size(pkey);
GUARD_AS_RESULT(s2n_alloc(&signed_content, maximum_signature_length));
GUARD_AS_RESULT(s2n_pkey_sign(pkey, sig_alg, digest, &signed_content));
GUARD_AS_RESULT(on_complete(conn, &signed_content));
return S2N_RESULT_OK;
}
int s2n_async_pkey_op_perform(struct s2n_async_pkey_op *op, s2n_cert_private_key *key)
{
ENSURE_POSIX_REF(op);
ENSURE_POSIX_REF(key);
ENSURE_POSIX(!op->complete, S2N_ERR_ASYNC_ALREADY_PERFORMED);
const struct s2n_async_pkey_op_actions *actions = NULL;
GUARD_AS_POSIX(s2n_async_get_actions(op->type, &actions));
notnull_check(actions);
GUARD_AS_POSIX(actions->perform(op, key));
op->complete = true;
return S2N_SUCCESS;
}
int s2n_async_pkey_op_apply(struct s2n_async_pkey_op *op, struct s2n_connection *conn)
{
ENSURE_POSIX_REF(op);
ENSURE_POSIX_REF(conn);
ENSURE_POSIX(op->complete, S2N_ERR_ASYNC_NOT_PERFORMED);
ENSURE_POSIX(!op->applied, S2N_ERR_ASYNC_ALREADY_APPLIED);
/* We could have just used op->conn and removed a conn argument, but we want caller
* to be explicit about connection it wants to resume. Plus this gives more
* protections in cases if caller frees connection object and then tries to resume
* the connection. */
ENSURE_POSIX(op->conn == conn, S2N_ERR_ASYNC_WRONG_CONNECTION);
ENSURE_POSIX(conn->handshake.async_state != S2N_ASYNC_INVOKING_CALLBACK, S2N_ERR_ASYNC_APPLY_WHILE_INVOKING);
ENSURE_POSIX(conn->handshake.async_state == S2N_ASYNC_INVOKED_WAITING, S2N_ERR_ASYNC_WRONG_CONNECTION);
const struct s2n_async_pkey_op_actions *actions = NULL;
GUARD_AS_POSIX(s2n_async_get_actions(op->type, &actions));
notnull_check(actions);
GUARD_AS_POSIX(actions->apply(op, conn));
op->applied = true;
conn->handshake.async_state = S2N_ASYNC_INVOKED_COMPLETE;
/* Free up the decrypt/sign structs to avoid storing secrets for too long */
GUARD_AS_POSIX(actions->free(op));
return S2N_SUCCESS;
}
int s2n_async_pkey_op_free(struct s2n_async_pkey_op *op)
{
ENSURE_POSIX_REF(op);
const struct s2n_async_pkey_op_actions *actions = NULL;
GUARD_AS_POSIX(s2n_async_get_actions(op->type, &actions));
notnull_check(actions);
/* If applied the decrypt/sign structs were released in apply call */
if (!op->applied) { GUARD_AS_POSIX(actions->free(op)); }
GUARD_POSIX(s2n_free_object(( uint8_t ** )&op, sizeof(struct s2n_async_pkey_op)));
return S2N_SUCCESS;
}
S2N_RESULT s2n_async_pkey_decrypt_perform(struct s2n_async_pkey_op *op, s2n_cert_private_key *pkey)
{
ENSURE_REF(op);
ENSURE_REF(pkey);
struct s2n_async_pkey_decrypt_data *decrypt = &op->op.decrypt;
decrypt->rsa_failed = s2n_pkey_decrypt(pkey, &decrypt->encrypted, &decrypt->decrypted) != S2N_SUCCESS;
return S2N_RESULT_OK;
}
S2N_RESULT s2n_async_pkey_decrypt_apply(struct s2n_async_pkey_op *op, struct s2n_connection *conn)
{
ENSURE_REF(op);
ENSURE_REF(conn);
struct s2n_async_pkey_decrypt_data *decrypt = &op->op.decrypt;
GUARD_AS_RESULT(decrypt->on_complete(conn, decrypt->rsa_failed, &decrypt->decrypted));
return S2N_RESULT_OK;
}
S2N_RESULT s2n_async_pkey_decrypt_free(struct s2n_async_pkey_op *op)
{
ENSURE_REF(op);
struct s2n_async_pkey_decrypt_data *decrypt = &op->op.decrypt;
GUARD_AS_RESULT(s2n_blob_zero(&decrypt->decrypted));
GUARD_AS_RESULT(s2n_blob_zero(&decrypt->encrypted));
GUARD_AS_RESULT(s2n_free(&decrypt->decrypted));
GUARD_AS_RESULT(s2n_free(&decrypt->encrypted));
return S2N_RESULT_OK;
}
S2N_RESULT s2n_async_pkey_sign_perform(struct s2n_async_pkey_op *op, s2n_cert_private_key *pkey)
{
ENSURE_REF(op);
ENSURE_REF(pkey);
struct s2n_async_pkey_sign_data *sign = &op->op.sign;
uint32_t maximum_signature_length = s2n_pkey_size(pkey);
GUARD_AS_RESULT(s2n_alloc(&sign->signature, maximum_signature_length));
GUARD_AS_RESULT(s2n_pkey_sign(pkey, sign->sig_alg, &sign->digest, &sign->signature));
return S2N_RESULT_OK;
}
S2N_RESULT s2n_async_pkey_sign_apply(struct s2n_async_pkey_op *op, struct s2n_connection *conn)
{
ENSURE_REF(op);
ENSURE_REF(conn);
struct s2n_async_pkey_sign_data *sign = &op->op.sign;
GUARD_AS_RESULT(sign->on_complete(conn, &sign->signature));
return S2N_RESULT_OK;
}
S2N_RESULT s2n_async_pkey_sign_free(struct s2n_async_pkey_op *op)
{
ENSURE_REF(op);
struct s2n_async_pkey_sign_data *sign = &op->op.sign;
GUARD_AS_RESULT(s2n_hash_free(&sign->digest));
GUARD_AS_RESULT(s2n_free(&sign->signature));
return S2N_RESULT_OK;
}
|