tls_mutators.cc 7.95 KB
Newer Older
1 2 3 4
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

5
#include <algorithm>
6 7 8 9
#include "shared.h"
#include "tls_parser.h"

#include "ssl.h"
10
extern "C" {
11
#include "sslimpl.h"
12
}
13 14 15

using namespace nss_test;

16 17 18 19
// Number of additional bytes in the TLS header.
// Used to properly skip DTLS seqnums.
static size_t gExtraHeaderBytes = 0;

20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
// Helper class to simplify TLS record manipulation.
class Record {
 public:
  static std::unique_ptr<Record> Create(const uint8_t *data, size_t size,
                                        size_t remaining) {
    return std::unique_ptr<Record>(new Record(data, size, remaining));
  }

  void insert_before(const std::unique_ptr<Record> &other) {
    assert(data_ && size_ > 0);

    // Copy data in case other == this.
    uint8_t buf[size_];
    memcpy(buf, data_, size_);

    uint8_t *dest = const_cast<uint8_t *>(other->data());
    // Make room for the record we want to insert.
    memmove(dest + size_, other->data(), other->size() + other->remaining());
    // Insert the record.
    memcpy(dest, buf, size_);
  }

  void truncate(size_t length) {
43
    assert(length >= 5 + gExtraHeaderBytes);
44
    uint8_t *dest = const_cast<uint8_t *>(data_);
45 46 47
    size_t l = length - (5 + gExtraHeaderBytes);
    dest[3] = (l >> 8) & 0xff;
    dest[4] = l & 0xff;
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
    memmove(dest + length, data_ + size_, remaining_);
  }

  void drop() {
    uint8_t *dest = const_cast<uint8_t *>(data_);
    memmove(dest, data_ + size_, remaining_);
  }

  const uint8_t *data() { return data_; }
  size_t remaining() { return remaining_; }
  size_t size() { return size_; }

 private:
  Record(const uint8_t *data, size_t size, size_t remaining)
      : data_(data), remaining_(remaining), size_(size) {}

  const uint8_t *data_;
  size_t remaining_;
  size_t size_;
};

// Parse records contained in a given TLS transcript.
std::vector<std::unique_ptr<Record>> ParseRecords(const uint8_t *data,
                                                  size_t size) {
  std::vector<std::unique_ptr<Record>> records;
  TlsParser parser(data, size);

  while (parser.remaining()) {
    size_t offset = parser.consumed();

78 79
    // Skip type, version, and DTLS seqnums.
    if (!parser.Skip(3 + gExtraHeaderBytes)) {
80 81 82 83 84 85 86 87
      break;
    }

    DataBuffer fragment;
    if (!parser.ReadVariable(&fragment, 2)) {
      break;
    }

88 89 90
    records.push_back(Record::Create(data + offset,
                                     fragment.len() + 5 + gExtraHeaderBytes,
                                     parser.remaining()));
91 92 93 94 95
  }

  return records;
}

96 97 98 99 100
namespace TlsMutators {

// Handle seqnums in DTLS transcripts.
void SetIsDTLS() { gExtraHeaderBytes = 8; }

101
// Mutator that drops whole TLS records.
102 103
size_t DropRecord(uint8_t *data, size_t size, size_t max_size,
                  unsigned int seed) {
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
  std::mt19937 rng(seed);

  // Find TLS records in the corpus.
  auto records = ParseRecords(data, size);
  if (records.empty()) {
    return 0;
  }

  // Pick a record to drop at random.
  std::uniform_int_distribution<size_t> dist(0, records.size() - 1);
  auto &rec = records.at(dist(rng));

  // Drop the record.
  rec->drop();

  // Return the new final size.
  return size - rec->size();
}

// Mutator that shuffles TLS records in a transcript.
124 125
size_t ShuffleRecords(uint8_t *data, size_t size, size_t max_size,
                      unsigned int seed) {
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
  std::mt19937 rng(seed);

  // Store the original corpus.
  uint8_t buf[size];
  memcpy(buf, data, size);

  // Find TLS records in the corpus.
  auto records = ParseRecords(buf, sizeof(buf));
  if (records.empty()) {
    return 0;
  }

  // Find offset of first record in target buffer.
  uint8_t *dest = const_cast<uint8_t *>(ParseRecords(data, size).at(0)->data());

  // Shuffle record order.
  std::shuffle(records.begin(), records.end(), rng);

  // Write records to their new positions.
  for (auto &rec : records) {
    memcpy(dest, rec->data(), rec->size());
    dest += rec->size();
  }

  // Final size hasn't changed.
  return size;
}

// Mutator that duplicates a single TLS record and randomly inserts it.
155 156
size_t DuplicateRecord(uint8_t *data, size_t size, size_t max_size,
                       unsigned int seed) {
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
  std::mt19937 rng(seed);

  // Find TLS records in the corpus.
  const auto records = ParseRecords(data, size);
  if (records.empty()) {
    return 0;
  }

  // Pick a record to duplicate at random.
  std::uniform_int_distribution<size_t> dist(0, records.size() - 1);
  auto &rec = records.at(dist(rng));
  if (size + rec->size() > max_size) {
    return 0;
  }

  // Insert before random record.
  rec->insert_before(records.at(dist(rng)));

  // Return the new final size.
  return size + rec->size();
}

// Mutator that truncates a TLS record.
180 181
size_t TruncateRecord(uint8_t *data, size_t size, size_t max_size,
                      unsigned int seed) {
182 183 184 185 186 187 188 189 190 191 192 193 194
  std::mt19937 rng(seed);

  // Find TLS records in the corpus.
  const auto records = ParseRecords(data, size);
  if (records.empty()) {
    return 0;
  }

  // Pick a record to truncate at random.
  std::uniform_int_distribution<size_t> dist(0, records.size() - 1);
  auto &rec = records.at(dist(rng));

  // Need a record with data.
195
  if (rec->size() <= 5 + gExtraHeaderBytes) {
196 197 198 199
    return 0;
  }

  // Truncate.
200 201
  std::uniform_int_distribution<size_t> dist2(5 + gExtraHeaderBytes,
                                              rec->size() - 1);
202 203 204 205 206 207 208 209
  size_t new_length = dist2(rng);
  rec->truncate(new_length);

  // Return the new final size.
  return size + new_length - rec->size();
}

// Mutator that splits a TLS record in two.
210 211
size_t FragmentRecord(uint8_t *data, size_t size, size_t max_size,
                      unsigned int seed) {
212 213
  std::mt19937 rng(seed);

214 215 216 217 218
  // We can't deal with DTLS yet.
  if (gExtraHeaderBytes > 0) {
    return 0;
  }

219 220 221 222 223 224 225 226 227 228 229
  if (size + 5 > max_size) {
    return 0;
  }

  // Find TLS records in the corpus.
  const auto records = ParseRecords(data, size);
  if (records.empty()) {
    return 0;
  }

  // Pick a record to fragment at random.
230 231
  std::uniform_int_distribution<size_t> rand_record(0, records.size() - 1);
  auto &rec = records.at(rand_record(rng));
232 233 234 235 236 237 238 239 240
  uint8_t *rdata = const_cast<uint8_t *>(rec->data());
  size_t length = rec->size();
  size_t content_length = length - 5;

  if (content_length < 2) {
    return 0;
  }

  // Assign a new length to the first fragment.
241 242 243 244 245 246
  std::uniform_int_distribution<size_t> rand_size(1, content_length - 1);
  size_t first_length = rand_size(rng);
  size_t second_length = content_length - first_length;
  rdata[3] = (first_length >> 8) & 0xff;
  rdata[4] = first_length & 0xff;
  uint8_t *second_record = rdata + 5 + first_length;
247

248 249 250
  // Make room for the header of the second record.
  memmove(second_record + 5, second_record,
          rec->remaining() + content_length - first_length);
251 252

  // Write second header.
253 254 255
  memcpy(second_record, rdata, 3);
  second_record[3] = (second_length >> 8) & 0xff;
  second_record[4] = second_length & 0xff;
256 257 258 259 260

  return size + 5;
}

// Cross-over function that merges and shuffles two transcripts.
261 262 263
size_t CrossOver(const uint8_t *data1, size_t size1, const uint8_t *data2,
                 size_t size2, uint8_t *out, size_t max_out_size,
                 unsigned int seed) {
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
  std::mt19937 rng(seed);

  // Find TLS records in the corpus.
  auto records1 = ParseRecords(data1, size1);
  if (records1.empty()) {
    return 0;
  }

  {  // Merge the two vectors.
    auto records2 = ParseRecords(data2, size2);
    if (records2.empty()) {
      return 0;
    }
    std::move(records2.begin(), records2.end(), std::back_inserter(records1));
  }

  // Shuffle record order.
  std::shuffle(records1.begin(), records1.end(), rng);

  size_t total = 0;
  for (auto &rec : records1) {
    size_t length = rec->size();
    if (total + length > max_out_size) {
      break;
    }

    // Write record to its new position.
    memcpy(out + total, rec->data(), length);
    total += length;
  }

  return total;
}
297 298

}  // namespace TlsMutators