2018-02-06 11:46:26 +01:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
|
/* vim: set ts=2 et sw=2 tw=80: */
|
|
|
|
|
/* 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/. */
|
|
|
|
|
|
|
|
|
|
#ifndef tls_protection_h_
|
|
|
|
|
#define tls_protection_h_
|
|
|
|
|
|
|
|
|
|
#include <cstdint>
|
|
|
|
|
#include <memory>
|
|
|
|
|
|
|
|
|
|
#include "databuffer.h"
|
|
|
|
|
#include "pk11pub.h"
|
|
|
|
|
#include "sslt.h"
|
|
|
|
|
|
|
|
|
|
namespace nss_test {
|
|
|
|
|
class TlsRecordHeader;
|
|
|
|
|
|
|
|
|
|
class AeadCipher {
|
|
|
|
|
public:
|
|
|
|
|
AeadCipher(CK_MECHANISM_TYPE mech) : mech_(mech), key_(nullptr) {}
|
2018-02-23 11:04:39 +01:00
|
|
|
virtual ~AeadCipher();
|
2018-02-06 11:46:26 +01:00
|
|
|
|
|
|
|
|
bool Init(PK11SymKey *key, const uint8_t *iv);
|
2018-08-14 07:52:35 +02:00
|
|
|
virtual bool Aead(bool decrypt, const uint8_t *hdr, size_t hdr_len,
|
|
|
|
|
uint64_t seq, const uint8_t *in, size_t inlen, uint8_t *out,
|
|
|
|
|
size_t *outlen, size_t maxlen) = 0;
|
2018-02-06 11:46:26 +01:00
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
void FormatNonce(uint64_t seq, uint8_t *nonce);
|
|
|
|
|
bool AeadInner(bool decrypt, void *params, size_t param_length,
|
|
|
|
|
const uint8_t *in, size_t inlen, uint8_t *out, size_t *outlen,
|
|
|
|
|
size_t maxlen);
|
|
|
|
|
|
|
|
|
|
CK_MECHANISM_TYPE mech_;
|
|
|
|
|
PK11SymKey *key_;
|
|
|
|
|
uint8_t iv_[12];
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class AeadCipherChacha20Poly1305 : public AeadCipher {
|
|
|
|
|
public:
|
|
|
|
|
AeadCipherChacha20Poly1305() : AeadCipher(CKM_NSS_CHACHA20_POLY1305) {}
|
|
|
|
|
|
|
|
|
|
protected:
|
2018-08-14 07:52:35 +02:00
|
|
|
bool Aead(bool decrypt, const uint8_t *hdr, size_t hdr_len, uint64_t seq,
|
|
|
|
|
const uint8_t *in, size_t inlen, uint8_t *out, size_t *outlen,
|
|
|
|
|
size_t maxlen);
|
2018-02-06 11:46:26 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class AeadCipherAesGcm : public AeadCipher {
|
|
|
|
|
public:
|
|
|
|
|
AeadCipherAesGcm() : AeadCipher(CKM_AES_GCM) {}
|
|
|
|
|
|
|
|
|
|
protected:
|
2018-08-14 07:52:35 +02:00
|
|
|
bool Aead(bool decrypt, const uint8_t *hdr, size_t hdr_len, uint64_t seq,
|
|
|
|
|
const uint8_t *in, size_t inlen, uint8_t *out, size_t *outlen,
|
|
|
|
|
size_t maxlen);
|
2018-02-06 11:46:26 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Our analog of ssl3CipherSpec
|
|
|
|
|
class TlsCipherSpec {
|
|
|
|
|
public:
|
2018-02-23 11:04:39 +01:00
|
|
|
TlsCipherSpec() : epoch_(0), aead_() {}
|
2018-02-06 11:46:26 +01:00
|
|
|
|
2018-02-23 11:04:39 +01:00
|
|
|
bool Init(uint16_t epoch, SSLCipherAlgorithm cipher, PK11SymKey *key,
|
|
|
|
|
const uint8_t *iv);
|
2018-02-06 11:46:26 +01:00
|
|
|
|
|
|
|
|
bool Protect(const TlsRecordHeader &header, const DataBuffer &plaintext,
|
|
|
|
|
DataBuffer *ciphertext);
|
|
|
|
|
bool Unprotect(const TlsRecordHeader &header, const DataBuffer &ciphertext,
|
|
|
|
|
DataBuffer *plaintext);
|
2018-02-23 11:04:39 +01:00
|
|
|
uint16_t epoch() const { return epoch_; }
|
2018-02-06 11:46:26 +01:00
|
|
|
|
|
|
|
|
private:
|
2018-02-23 11:04:39 +01:00
|
|
|
uint16_t epoch_;
|
2018-02-06 11:46:26 +01:00
|
|
|
std::unique_ptr<AeadCipher> aead_;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
} // namespace nss_test
|
|
|
|
|
|
|
|
|
|
#endif
|