aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/core/tsi/alts/zero_copy_frame_protector/alts_iovec_record_protocol.cc
blob: 6a548e50dd40a13e7e69ffefe5563576e17e68f6 (plain)
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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
/*
 *
 * Copyright 2018 gRPC authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License 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 <grpc/support/port_platform.h>

#include "src/core/tsi/alts/zero_copy_frame_protector/alts_iovec_record_protocol.h"

#include <stdlib.h>
#include <string.h>

#include <grpc/support/alloc.h>
#include <grpc/support/log.h>

#include "src/core/tsi/alts/frame_protector/alts_counter.h"

struct alts_iovec_record_protocol {
  alts_counter* ctr;
  gsec_aead_crypter* crypter;
  size_t tag_length;
  bool is_integrity_only;
  bool is_protect;
};

/* Copies error message to destination.  */
static void maybe_copy_error_msg(const char* src, char** dst) {
  if (dst != nullptr && src != nullptr) {
    *dst = static_cast<char*>(gpr_malloc(strlen(src) + 1));
    memcpy(*dst, src, strlen(src) + 1);
  }
}

/* Appends error message to destination.  */
static void maybe_append_error_msg(const char* appendix, char** dst) {
  if (dst != nullptr && appendix != nullptr) {
    int dst_len = static_cast<int>(strlen(*dst));
    *dst = static_cast<char*>(realloc(*dst, dst_len + strlen(appendix) + 1));
    assert(*dst != nullptr);
    memcpy(*dst + dst_len, appendix, strlen(appendix) + 1);
  }
}

/* Use little endian to interpret a string of bytes as uint32_t.  */
static uint32_t load_32_le(const unsigned char* buffer) {
  return (((uint32_t)buffer[3]) << 24) | (((uint32_t)buffer[2]) << 16) |
         (((uint32_t)buffer[1]) << 8) | ((uint32_t)buffer[0]);
}

/* Store uint32_t as a string of little endian bytes.  */
static void store_32_le(uint32_t value, unsigned char* buffer) {
  buffer[3] = (unsigned char)(value >> 24) & 0xFF;
  buffer[2] = (unsigned char)(value >> 16) & 0xFF;
  buffer[1] = (unsigned char)(value >> 8) & 0xFF;
  buffer[0] = (unsigned char)(value)&0xFF;
}

/* Ensures header and tag iovec have sufficient length.  */
static grpc_status_code ensure_header_and_tag_length(
    const alts_iovec_record_protocol* rp, iovec_t header, iovec_t tag,
    char** error_details) {
  if (rp == nullptr) {
    return GRPC_STATUS_FAILED_PRECONDITION;
  }
  if (header.iov_base == nullptr) {
    maybe_copy_error_msg("Header is nullptr.", error_details);
    return GRPC_STATUS_INVALID_ARGUMENT;
  }
  if (header.iov_len != alts_iovec_record_protocol_get_header_length()) {
    maybe_copy_error_msg("Header length is incorrect.", error_details);
    return GRPC_STATUS_INVALID_ARGUMENT;
  }
  if (tag.iov_base == nullptr) {
    maybe_copy_error_msg("Tag is nullptr.", error_details);
    return GRPC_STATUS_INVALID_ARGUMENT;
  }
  if (tag.iov_len != rp->tag_length) {
    maybe_copy_error_msg("Tag length is incorrect.", error_details);
    return GRPC_STATUS_INVALID_ARGUMENT;
  }
  return GRPC_STATUS_OK;
}

/* Increments crypter counter and checks overflow.  */
static grpc_status_code increment_counter(alts_counter* counter,
                                          char** error_details) {
  if (counter == nullptr) {
    return GRPC_STATUS_FAILED_PRECONDITION;
  }
  bool is_overflow = false;
  grpc_status_code status =
      alts_counter_increment(counter, &is_overflow, error_details);
  if (status != GRPC_STATUS_OK) {
    return status;
  }
  if (is_overflow) {
    maybe_copy_error_msg("Crypter counter is overflowed.", error_details);
    return GRPC_STATUS_INTERNAL;
  }
  return GRPC_STATUS_OK;
}

/* Given an array of iovec, computes the total length of buffer.  */
static size_t get_total_length(const iovec_t* vec, size_t vec_length) {
  size_t total_length = 0;
  for (size_t i = 0; i < vec_length; ++i) {
    total_length += vec[i].iov_len;
  }
  return total_length;
}

/* Writes frame header given data and tag length.  */
static grpc_status_code write_frame_header(size_t data_length,
                                           unsigned char* header,
                                           char** error_details) {
  if (header == nullptr) {
    maybe_copy_error_msg("Header is nullptr.", error_details);
    return GRPC_STATUS_FAILED_PRECONDITION;
  }
  size_t frame_length = kZeroCopyFrameMessageTypeFieldSize + data_length;
  store_32_le(static_cast<uint32_t>(frame_length), header);
  store_32_le(kZeroCopyFrameMessageType,
              header + kZeroCopyFrameLengthFieldSize);
  return GRPC_STATUS_OK;
}

/* Verifies frame header given protected data length.  */
static grpc_status_code verify_frame_header(size_t data_length,
                                            unsigned char* header,
                                            char** error_details) {
  if (header == nullptr) {
    maybe_copy_error_msg("Header is nullptr.", error_details);
    return GRPC_STATUS_FAILED_PRECONDITION;
  }
  size_t frame_length = load_32_le(header);
  if (frame_length != kZeroCopyFrameMessageTypeFieldSize + data_length) {
    maybe_copy_error_msg("Bad frame length.", error_details);
    return GRPC_STATUS_INTERNAL;
  }
  size_t message_type = load_32_le(header + kZeroCopyFrameLengthFieldSize);
  if (message_type != kZeroCopyFrameMessageType) {
    maybe_copy_error_msg("Unsupported message type.", error_details);
    return GRPC_STATUS_INTERNAL;
  }
  return GRPC_STATUS_OK;
}

/* --- alts_iovec_record_protocol methods implementation. --- */

size_t alts_iovec_record_protocol_get_header_length() {
  return kZeroCopyFrameHeaderSize;
}

size_t alts_iovec_record_protocol_get_tag_length(
    const alts_iovec_record_protocol* rp) {
  if (rp != nullptr) {
    return rp->tag_length;
  }
  return 0;
}

size_t alts_iovec_record_protocol_max_unprotected_data_size(
    const alts_iovec_record_protocol* rp, size_t max_protected_frame_size) {
  if (rp == nullptr) {
    return 0;
  }
  size_t overhead_bytes_size =
      kZeroCopyFrameMessageTypeFieldSize + rp->tag_length;
  if (max_protected_frame_size <= overhead_bytes_size) return 0;
  return max_protected_frame_size - overhead_bytes_size;
}

grpc_status_code alts_iovec_record_protocol_integrity_only_protect(
    alts_iovec_record_protocol* rp, const iovec_t* unprotected_vec,
    size_t unprotected_vec_length, iovec_t header, iovec_t tag,
    char** error_details) {
  /* Input sanity checks.  */
  if (rp == nullptr) {
    maybe_copy_error_msg("Input iovec_record_protocol is nullptr.",
                         error_details);
    return GRPC_STATUS_INVALID_ARGUMENT;
  }
  if (!rp->is_integrity_only) {
    maybe_copy_error_msg(
        "Integrity-only operations are not allowed for this object.",
        error_details);
    return GRPC_STATUS_FAILED_PRECONDITION;
  }
  if (!rp->is_protect) {
    maybe_copy_error_msg("Protect operations are not allowed for this object.",
                         error_details);
    return GRPC_STATUS_FAILED_PRECONDITION;
  }
  grpc_status_code status =
      ensure_header_and_tag_length(rp, header, tag, error_details);
  if (status != GRPC_STATUS_OK) {
    return status;
  }
  /* Unprotected data should not be zero length.  */
  size_t data_length =
      get_total_length(unprotected_vec, unprotected_vec_length);
  /* Sets frame header.  */
  status = write_frame_header(data_length + rp->tag_length,
                              static_cast<unsigned char*>(header.iov_base),
                              error_details);
  if (status != GRPC_STATUS_OK) {
    return status;
  }
  /* Computes frame tag by calling AEAD crypter.  */
  size_t bytes_written = 0;
  status = gsec_aead_crypter_encrypt_iovec(
      rp->crypter, alts_counter_get_counter(rp->ctr),
      alts_counter_get_size(rp->ctr), unprotected_vec, unprotected_vec_length,
      /* plaintext_vec = */ nullptr, /* plaintext_vec_length = */ 0, tag,
      &bytes_written, error_details);
  if (status != GRPC_STATUS_OK) {
    return status;
  }
  if (bytes_written != rp->tag_length) {
    maybe_copy_error_msg("Bytes written expects to be the same as tag length.",
                         error_details);
    return GRPC_STATUS_INTERNAL;
  }
  /* Increments the crypter counter.  */
  return increment_counter(rp->ctr, error_details);
}

grpc_status_code alts_iovec_record_protocol_integrity_only_unprotect(
    alts_iovec_record_protocol* rp, const iovec_t* protected_vec,
    size_t protected_vec_length, iovec_t header, iovec_t tag,
    char** error_details) {
  /* Input sanity checks.  */
  if (rp == nullptr) {
    maybe_copy_error_msg("Input iovec_record_protocol is nullptr.",
                         error_details);
    return GRPC_STATUS_INVALID_ARGUMENT;
  }
  if (!rp->is_integrity_only) {
    maybe_copy_error_msg(
        "Integrity-only operations are not allowed for this object.",
        error_details);
    return GRPC_STATUS_FAILED_PRECONDITION;
  }
  if (rp->is_protect) {
    maybe_copy_error_msg(
        "Unprotect operations are not allowed for this object.", error_details);
    return GRPC_STATUS_FAILED_PRECONDITION;
  }
  grpc_status_code status =
      ensure_header_and_tag_length(rp, header, tag, error_details);
  if (status != GRPC_STATUS_OK) return status;
  /* Protected data should not be zero length.  */
  size_t data_length = get_total_length(protected_vec, protected_vec_length);
  /* Verifies frame header.  */
  status = verify_frame_header(data_length + rp->tag_length,
                               static_cast<unsigned char*>(header.iov_base),
                               error_details);
  if (status != GRPC_STATUS_OK) {
    return status;
  }
  /* Verifies frame tag by calling AEAD crypter.  */
  iovec_t plaintext = {nullptr, 0};
  size_t bytes_written = 0;
  status = gsec_aead_crypter_decrypt_iovec(
      rp->crypter, alts_counter_get_counter(rp->ctr),
      alts_counter_get_size(rp->ctr), protected_vec, protected_vec_length, &tag,
      1, plaintext, &bytes_written, error_details);
  if (status != GRPC_STATUS_OK || bytes_written != 0) {
    maybe_append_error_msg(" Frame tag verification failed.", error_details);
    return GRPC_STATUS_INTERNAL;
  }
  /* Increments the crypter counter.  */
  return increment_counter(rp->ctr, error_details);
}

grpc_status_code alts_iovec_record_protocol_privacy_integrity_protect(
    alts_iovec_record_protocol* rp, const iovec_t* unprotected_vec,
    size_t unprotected_vec_length, iovec_t protected_frame,
    char** error_details) {
  /* Input sanity checks.  */
  if (rp == nullptr) {
    maybe_copy_error_msg("Input iovec_record_protocol is nullptr.",
                         error_details);
    return GRPC_STATUS_INVALID_ARGUMENT;
  }
  if (rp->is_integrity_only) {
    maybe_copy_error_msg(
        "Privacy-integrity operations are not allowed for this object.",
        error_details);
    return GRPC_STATUS_FAILED_PRECONDITION;
  }
  if (!rp->is_protect) {
    maybe_copy_error_msg("Protect operations are not allowed for this object.",
                         error_details);
    return GRPC_STATUS_FAILED_PRECONDITION;
  }
  /* Unprotected data should not be zero length.  */
  size_t data_length =
      get_total_length(unprotected_vec, unprotected_vec_length);
  /* Ensures protected frame iovec has sufficient size.  */
  if (protected_frame.iov_base == nullptr) {
    maybe_copy_error_msg("Protected frame is nullptr.", error_details);
    return GRPC_STATUS_INVALID_ARGUMENT;
  }
  if (protected_frame.iov_len !=
      alts_iovec_record_protocol_get_header_length() + data_length +
          rp->tag_length) {
    maybe_copy_error_msg("Protected frame size is incorrect.", error_details);
    return GRPC_STATUS_INVALID_ARGUMENT;
  }
  /* Writer frame header.  */
  grpc_status_code status = write_frame_header(
      data_length + rp->tag_length,
      static_cast<unsigned char*>(protected_frame.iov_base), error_details);
  if (status != GRPC_STATUS_OK) {
    return status;
  }
  /* Encrypt unprotected data by calling AEAD crypter.  */
  unsigned char* ciphertext_buffer =
      static_cast<unsigned char*>(protected_frame.iov_base) +
      alts_iovec_record_protocol_get_header_length();
  iovec_t ciphertext = {ciphertext_buffer, data_length + rp->tag_length};
  size_t bytes_written = 0;
  status = gsec_aead_crypter_encrypt_iovec(
      rp->crypter, alts_counter_get_counter(rp->ctr),
      alts_counter_get_size(rp->ctr), /* aad_vec = */ nullptr,
      /* aad_vec_length = */ 0, unprotected_vec, unprotected_vec_length,
      ciphertext, &bytes_written, error_details);
  if (status != GRPC_STATUS_OK) {
    return status;
  }
  if (bytes_written != data_length + rp->tag_length) {
    maybe_copy_error_msg(
        "Bytes written expects to be data length plus tag length.",
        error_details);
    return GRPC_STATUS_INTERNAL;
  }
  /* Increments the crypter counter. */
  return increment_counter(rp->ctr, error_details);
}

grpc_status_code alts_iovec_record_protocol_privacy_integrity_unprotect(
    alts_iovec_record_protocol* rp, iovec_t header,
    const iovec_t* protected_vec, size_t protected_vec_length,
    iovec_t unprotected_data, char** error_details) {
  /* Input sanity checks.  */
  if (rp == nullptr) {
    maybe_copy_error_msg("Input iovec_record_protocol is nullptr.",
                         error_details);
    return GRPC_STATUS_INVALID_ARGUMENT;
  }
  if (rp->is_integrity_only) {
    maybe_copy_error_msg(
        "Privacy-integrity operations are not allowed for this object.",
        error_details);
    return GRPC_STATUS_FAILED_PRECONDITION;
  }
  if (rp->is_protect) {
    maybe_copy_error_msg(
        "Unprotect operations are not allowed for this object.", error_details);
    return GRPC_STATUS_FAILED_PRECONDITION;
  }
  /* Protected data size should be no less than tag size.  */
  size_t protected_data_length =
      get_total_length(protected_vec, protected_vec_length);
  if (protected_data_length < rp->tag_length) {
    maybe_copy_error_msg(
        "Protected data length should be more than the tag length.",
        error_details);
    return GRPC_STATUS_INVALID_ARGUMENT;
  }
  /* Ensures header has sufficient size.  */
  if (header.iov_base == nullptr) {
    maybe_copy_error_msg("Header is nullptr.", error_details);
    return GRPC_STATUS_INVALID_ARGUMENT;
  }
  if (header.iov_len != alts_iovec_record_protocol_get_header_length()) {
    maybe_copy_error_msg("Header length is incorrect.", error_details);
    return GRPC_STATUS_INVALID_ARGUMENT;
  }
  /* Ensures unprotected data iovec has sufficient size.  */
  if (unprotected_data.iov_len != protected_data_length - rp->tag_length) {
    maybe_copy_error_msg("Unprotected data size is incorrect.", error_details);
    return GRPC_STATUS_INVALID_ARGUMENT;
  }
  /* Verify frame header.  */
  grpc_status_code status = verify_frame_header(
      protected_data_length, static_cast<unsigned char*>(header.iov_base),
      error_details);
  if (status != GRPC_STATUS_OK) {
    return status;
  }
  /* Decrypt protected data by calling AEAD crypter.  */
  size_t bytes_written = 0;
  status = gsec_aead_crypter_decrypt_iovec(
      rp->crypter, alts_counter_get_counter(rp->ctr),
      alts_counter_get_size(rp->ctr), /* aad_vec = */ nullptr,
      /* aad_vec_length = */ 0, protected_vec, protected_vec_length,
      unprotected_data, &bytes_written, error_details);
  if (status != GRPC_STATUS_OK) {
    maybe_append_error_msg(" Frame decryption failed.", error_details);
    return GRPC_STATUS_INTERNAL;
  }
  if (bytes_written != protected_data_length - rp->tag_length) {
    maybe_copy_error_msg(
        "Bytes written expects to be protected data length minus tag length.",
        error_details);
    return GRPC_STATUS_INTERNAL;
  }
  /* Increments the crypter counter. */
  return increment_counter(rp->ctr, error_details);
}

grpc_status_code alts_iovec_record_protocol_create(
    gsec_aead_crypter* crypter, size_t overflow_size, bool is_client,
    bool is_integrity_only, bool is_protect, alts_iovec_record_protocol** rp,
    char** error_details) {
  if (crypter == nullptr || rp == nullptr) {
    maybe_copy_error_msg(
        "Invalid nullptr arguments to alts_iovec_record_protocol create.",
        error_details);
    return GRPC_STATUS_INVALID_ARGUMENT;
  }
  alts_iovec_record_protocol* impl = static_cast<alts_iovec_record_protocol*>(
      gpr_zalloc(sizeof(alts_iovec_record_protocol)));
  /* Gets counter length.  */
  size_t counter_length = 0;
  grpc_status_code status =
      gsec_aead_crypter_nonce_length(crypter, &counter_length, error_details);
  if (status != GRPC_STATUS_OK) {
    goto cleanup;
  }
  /* Creates counters.  */
  status =
      alts_counter_create(is_protect ? !is_client : is_client, counter_length,
                          overflow_size, &impl->ctr, error_details);
  if (status != GRPC_STATUS_OK) {
    goto cleanup;
  }
  /* Gets tag length.  */
  status =
      gsec_aead_crypter_tag_length(crypter, &impl->tag_length, error_details);
  if (status != GRPC_STATUS_OK) {
    goto cleanup;
  }
  impl->crypter = crypter;
  impl->is_integrity_only = is_integrity_only;
  impl->is_protect = is_protect;
  *rp = impl;
  return GRPC_STATUS_OK;
cleanup:
  alts_counter_destroy(impl->ctr);
  gpr_free(impl);
  return GRPC_STATUS_FAILED_PRECONDITION;
}

void alts_iovec_record_protocol_destroy(alts_iovec_record_protocol* rp) {
  if (rp != nullptr) {
    alts_counter_destroy(rp->ctr);
    gsec_aead_crypter_destroy(rp->crypter);
    gpr_free(rp);
  }
}