Commit 944915ea authored by Franziskus Kiefer's avatar Franziskus Kiefer

Bug 1479787 - clang-format, r=mt,keeler

Differential Revision: https://phabricator.services.mozilla.com/D2721

--HG--
extra : rebase_source : 8b075cdf10c7864c532017d27785644446f4d33f
parent 53850b92
......@@ -40,10 +40,10 @@
#pragma warning(push, 3)
// C4224: Nonstandard extension used: formal parameter 'X' was previously
// defined as a type.
#pragma warning(disable: 4224)
#pragma warning(disable : 4224)
// C4826: Conversion from 'type1 ' to 'type_2' is sign - extended. This may
// cause unexpected runtime behavior.
#pragma warning(disable: 4826)
#pragma warning(disable : 4826)
#endif
#include "gtest/gtest.h"
......@@ -60,11 +60,10 @@
#include "mozpkix/test/pkixtestutil.h"
// PrintTo must be in the same namespace as the type we're overloading it for.
namespace mozilla { namespace pkix {
namespace mozilla {
namespace pkix {
inline void
PrintTo(const Result& result, ::std::ostream* os)
{
inline void PrintTo(const Result& result, ::std::ostream* os) {
const char* stringified = MapResultToName(result);
if (stringified) {
*os << stringified;
......@@ -72,10 +71,12 @@ PrintTo(const Result& result, ::std::ostream* os)
*os << "mozilla::pkix::Result(" << static_cast<unsigned int>(result) << ")";
}
}
}
} // namespace mozilla::pkix
} } // namespace mozilla::pkix
namespace mozilla { namespace pkix { namespace test {
namespace mozilla {
namespace pkix {
namespace test {
extern const std::time_t oneDayBeforeNow;
extern const std::time_t oneDayAfterNow;
......@@ -84,172 +85,145 @@ extern const std::time_t twoDaysAfterNow;
extern const std::time_t tenDaysBeforeNow;
extern const std::time_t tenDaysAfterNow;
class EverythingFailsByDefaultTrustDomain : public TrustDomain
{
public:
Result GetCertTrust(EndEntityOrCA, const CertPolicyId&,
Input, /*out*/ TrustLevel&) override
{
class EverythingFailsByDefaultTrustDomain : public TrustDomain {
public:
Result GetCertTrust(EndEntityOrCA, const CertPolicyId&, Input,
/*out*/ TrustLevel&) override {
ADD_FAILURE();
return NotReached("GetCertTrust should not be called",
Result::FATAL_ERROR_LIBRARY_FAILURE);
}
Result FindIssuer(Input, IssuerChecker&, Time) override
{
Result FindIssuer(Input, IssuerChecker&, Time) override {
ADD_FAILURE();
return NotReached("FindIssuer should not be called",
Result::FATAL_ERROR_LIBRARY_FAILURE);
}
Result CheckRevocation(EndEntityOrCA, const CertID&, Time, Duration,
/*optional*/ const Input*,
/*optional*/ const Input*) override
{
/*optional*/ const Input*,
/*optional*/ const Input*) override {
ADD_FAILURE();
return NotReached("CheckRevocation should not be called",
Result::FATAL_ERROR_LIBRARY_FAILURE);
}
Result IsChainValid(const DERArray&, Time, const CertPolicyId&) override
{
Result IsChainValid(const DERArray&, Time, const CertPolicyId&) override {
ADD_FAILURE();
return NotReached("IsChainValid should not be called",
Result::FATAL_ERROR_LIBRARY_FAILURE);
}
Result DigestBuf(Input, DigestAlgorithm, /*out*/ uint8_t*, size_t) override
{
Result DigestBuf(Input, DigestAlgorithm, /*out*/ uint8_t*, size_t) override {
ADD_FAILURE();
return NotReached("DigestBuf should not be called",
Result::FATAL_ERROR_LIBRARY_FAILURE);
}
Result CheckSignatureDigestAlgorithm(DigestAlgorithm,
EndEntityOrCA,
Time) override
{
Result CheckSignatureDigestAlgorithm(DigestAlgorithm, EndEntityOrCA,
Time) override {
ADD_FAILURE();
return NotReached("CheckSignatureDigestAlgorithm should not be called",
Result::FATAL_ERROR_LIBRARY_FAILURE);
}
Result CheckECDSACurveIsAcceptable(EndEntityOrCA, NamedCurve) override
{
Result CheckECDSACurveIsAcceptable(EndEntityOrCA, NamedCurve) override {
ADD_FAILURE();
return NotReached("CheckECDSACurveIsAcceptable should not be called",
Result::FATAL_ERROR_LIBRARY_FAILURE);
}
Result VerifyECDSASignedDigest(const SignedDigest&, Input) override
{
Result VerifyECDSASignedDigest(const SignedDigest&, Input) override {
ADD_FAILURE();
return NotReached("VerifyECDSASignedDigest should not be called",
Result::FATAL_ERROR_LIBRARY_FAILURE);
}
Result CheckRSAPublicKeyModulusSizeInBits(EndEntityOrCA, unsigned int)
override
{
Result CheckRSAPublicKeyModulusSizeInBits(EndEntityOrCA,
unsigned int) override {
ADD_FAILURE();
return NotReached("CheckRSAPublicKeyModulusSizeInBits should not be called",
Result::FATAL_ERROR_LIBRARY_FAILURE);
}
Result VerifyRSAPKCS1SignedDigest(const SignedDigest&, Input) override
{
Result VerifyRSAPKCS1SignedDigest(const SignedDigest&, Input) override {
ADD_FAILURE();
return NotReached("VerifyRSAPKCS1SignedDigest should not be called",
Result::FATAL_ERROR_LIBRARY_FAILURE);
}
Result CheckValidityIsAcceptable(Time, Time, EndEntityOrCA, KeyPurposeId)
override
{
Result CheckValidityIsAcceptable(Time, Time, EndEntityOrCA,
KeyPurposeId) override {
ADD_FAILURE();
return NotReached("CheckValidityIsAcceptable should not be called",
Result::FATAL_ERROR_LIBRARY_FAILURE);
}
Result NetscapeStepUpMatchesServerAuth(Time, bool&) override
{
Result NetscapeStepUpMatchesServerAuth(Time, bool&) override {
ADD_FAILURE();
return NotReached("NetscapeStepUpMatchesServerAuth should not be called",
Result::FATAL_ERROR_LIBRARY_FAILURE);
}
virtual void NoteAuxiliaryExtension(AuxiliaryExtension, Input) override
{
virtual void NoteAuxiliaryExtension(AuxiliaryExtension, Input) override {
ADD_FAILURE();
}
};
class DefaultCryptoTrustDomain : public EverythingFailsByDefaultTrustDomain
{
class DefaultCryptoTrustDomain : public EverythingFailsByDefaultTrustDomain {
Result DigestBuf(Input item, DigestAlgorithm digestAlg,
/*out*/ uint8_t* digestBuf, size_t digestBufLen) override
{
/*out*/ uint8_t* digestBuf, size_t digestBufLen) override {
return TestDigestBuf(item, digestAlg, digestBuf, digestBufLen);
}
Result CheckSignatureDigestAlgorithm(DigestAlgorithm, EndEntityOrCA, Time)
override
{
Result CheckSignatureDigestAlgorithm(DigestAlgorithm, EndEntityOrCA,
Time) override {
return Success;
}
Result CheckECDSACurveIsAcceptable(EndEntityOrCA, NamedCurve) override
{
Result CheckECDSACurveIsAcceptable(EndEntityOrCA, NamedCurve) override {
return Success;
}
Result VerifyECDSASignedDigest(const SignedDigest& signedDigest,
Input subjectPublicKeyInfo) override
{
Input subjectPublicKeyInfo) override {
return TestVerifyECDSASignedDigest(signedDigest, subjectPublicKeyInfo);
}
Result CheckRSAPublicKeyModulusSizeInBits(EndEntityOrCA, unsigned int)
override
{
Result CheckRSAPublicKeyModulusSizeInBits(EndEntityOrCA,
unsigned int) override {
return Success;
}
Result VerifyRSAPKCS1SignedDigest(const SignedDigest& signedDigest,
Input subjectPublicKeyInfo) override
{
Input subjectPublicKeyInfo) override {
return TestVerifyRSAPKCS1SignedDigest(signedDigest, subjectPublicKeyInfo);
}
Result CheckValidityIsAcceptable(Time, Time, EndEntityOrCA, KeyPurposeId)
override
{
Result CheckValidityIsAcceptable(Time, Time, EndEntityOrCA,
KeyPurposeId) override {
return Success;
}
Result NetscapeStepUpMatchesServerAuth(Time, /*out*/ bool& matches) override
{
Result NetscapeStepUpMatchesServerAuth(Time, /*out*/ bool& matches) override {
matches = true;
return Success;
}
void NoteAuxiliaryExtension(AuxiliaryExtension, Input) override
{
}
void NoteAuxiliaryExtension(AuxiliaryExtension, Input) override {}
};
class DefaultNameMatchingPolicy : public NameMatchingPolicy
{
public:
class DefaultNameMatchingPolicy : public NameMatchingPolicy {
public:
virtual Result FallBackToCommonName(
Time, /*out*/ FallBackToSearchWithinSubject& fallBackToCommonName) override
{
Time,
/*out*/ FallBackToSearchWithinSubject& fallBackToCommonName) override {
fallBackToCommonName = FallBackToSearchWithinSubject::Yes;
return Success;
}
};
}
}
} // namespace mozilla::pkix::test
} } } // namespace mozilla::pkix::test
#endif // mozilla_pkix_pkixgtest_h
#endif // mozilla_pkix_pkixgtest_h
......@@ -31,15 +31,18 @@
#include <keyhi.h>
#include <keythi.h>
#include "mozpkix/test/pkixtestutil.h"
#include "mozpkix/nss_scoped_ptrs.h"
#include "mozpkix/test/pkixtestutil.h"
namespace mozilla { namespace pkix { namespace test {
namespace mozilla {
namespace pkix {
namespace test {
TestKeyPair* CreateTestKeyPair(const TestPublicKeyAlgorithm publicKeyAlg,
const ScopedSECKEYPublicKey& publicKey,
const ScopedSECKEYPrivateKey& privateKey);
}
}
} // namespace mozilla::pkix::test
} } } // namespace mozilla::pkix::test
#endif // mozilla_pkix_test_pkixtestnss_h
#endif // mozilla_pkix_test_pkixtestnss_h
......@@ -30,7 +30,8 @@
#include "mozpkix/Result.h"
#include "stdint.h"
namespace mozilla { namespace pkix {
namespace mozilla {
namespace pkix {
class Reader;
......@@ -49,9 +50,8 @@ class Reader;
//
// Note that in the example, GoodExample has the same performance
// characteristics as WorseExample, but with much better safety guarantees.
class Input final
{
public:
class Input final {
public:
typedef uint16_t size_type;
// This constructor is useful for inputs that are statically known to be of a
......@@ -66,26 +66,17 @@ public:
// Input expected;
// Result rv = expected.Init(EXPECTED_BYTES, sizeof EXPECTED_BYTES);
template <size_type N>
explicit Input(const uint8_t (&aData)[N])
: data(aData)
, len(N)
{
}
explicit Input(const uint8_t (&aData)[N]) : data(aData), len(N) {}
// Construct a valid, empty, Init-able Input.
Input()
: data(nullptr)
, len(0u)
{
}
Input() : data(nullptr), len(0u) {}
// This is intentionally not explicit in order to allow value semantics.
Input(const Input&) = default;
// Initialize the input. data must be non-null and len must be less than
// 65536. Init may not be called more than once.
Result Init(const uint8_t* aData, size_t aLen)
{
Result Init(const uint8_t* aData, size_t aLen) {
if (this->data) {
// already initialized
return Result::FATAL_ERROR_INVALID_ARGS;
......@@ -107,10 +98,7 @@ public:
// This is basically operator=, but it wasn't given that name because
// normally callers do not check the result of operator=, and normally
// operator= can be used multiple times.
Result Init(Input other)
{
return Init(other.data, other.len);
}
Result Init(Input other) { return Init(other.data, other.len); }
// Returns the length of the input.
//
......@@ -122,18 +110,17 @@ public:
// don't want to declare in this header file.
const uint8_t* UnsafeGetData() const { return data; }
private:
private:
const uint8_t* data;
size_t len;
void operator=(const Input&) = delete; // Use Init instead.
void operator=(const Input&) = delete; // Use Init instead.
};
inline bool
InputsAreEqual(const Input& a, const Input& b)
{
inline bool InputsAreEqual(const Input& a, const Input& b) {
return a.GetLength() == b.GetLength() &&
std::equal(a.UnsafeGetData(), a.UnsafeGetData() + a.GetLength(), b.UnsafeGetData());
std::equal(a.UnsafeGetData(), a.UnsafeGetData() + a.GetLength(),
b.UnsafeGetData());
}
// An Reader is a cursor/iterator through the contents of an Input, designed to
......@@ -144,23 +131,15 @@ InputsAreEqual(const Input& a, const Input& b)
//
// In general, Reader allows for one byte of lookahead and no backtracking.
// However, the Match* functions internally may have more lookahead.
class Reader final
{
public:
Reader()
: input(nullptr)
, end(nullptr)
{
}
class Reader final {
public:
Reader() : input(nullptr), end(nullptr) {}
explicit Reader(Input aInput)
: input(aInput.UnsafeGetData())
, end(aInput.UnsafeGetData() + aInput.GetLength())
{
}
: input(aInput.UnsafeGetData()),
end(aInput.UnsafeGetData() + aInput.GetLength()) {}
Result Init(Input aInput)
{
Result Init(Input aInput) {
if (this->input) {
return Result::FATAL_ERROR_INVALID_ARGS;
}
......@@ -169,13 +148,11 @@ public:
return Success;
}
bool Peek(uint8_t expectedByte) const
{
bool Peek(uint8_t expectedByte) const {
return input < end && *input == expectedByte;
}
Result Read(uint8_t& out)
{
Result Read(uint8_t& out) {
Result rv = EnsureLength(1);
if (rv != Success) {
return rv;
......@@ -184,8 +161,7 @@ public:
return Success;
}
Result Read(uint16_t& out)
{
Result Read(uint16_t& out) {
Result rv = EnsureLength(2);
if (rv != Success) {
return rv;
......@@ -197,8 +173,7 @@ public:
}
template <Input::size_type N>
bool MatchRest(const uint8_t (&toMatch)[N])
{
bool MatchRest(const uint8_t (&toMatch)[N]) {
// Normally we use EnsureLength which compares (input + len < end), but
// here we want to be sure that there is nothing following the matched
// bytes
......@@ -212,8 +187,7 @@ public:
return true;
}
bool MatchRest(Input toMatch)
{
bool MatchRest(Input toMatch) {
// Normally we use EnsureLength which compares (input + len < end), but
// here we want to be sure that there is nothing following the matched
// bytes
......@@ -228,8 +202,7 @@ public:
return true;
}
Result Skip(Input::size_type len)
{
Result Skip(Input::size_type len) {
Result rv = EnsureLength(len);
if (rv != Success) {
return rv;
......@@ -238,8 +211,7 @@ public:
return Success;
}
Result Skip(Input::size_type len, Reader& skipped)
{
Result Skip(Input::size_type len, Reader& skipped) {
Result rv = EnsureLength(len);
if (rv != Success) {
return rv;
......@@ -252,8 +224,7 @@ public:
return Success;
}
Result Skip(Input::size_type len, /*out*/ Input& skipped)
{
Result Skip(Input::size_type len, /*out*/ Input& skipped) {
Result rv = EnsureLength(len);
if (rv != Success) {
return rv;
......@@ -266,18 +237,13 @@ public:
return Success;
}
void SkipToEnd()
{
input = end;
}
void SkipToEnd() { input = end; }
Result SkipToEnd(/*out*/ Input& skipped)
{
Result SkipToEnd(/*out*/ Input& skipped) {
return Skip(static_cast<Input::size_type>(end - input), skipped);
}
Result EnsureLength(Input::size_type len)
{
Result EnsureLength(Input::size_type len) {
if (static_cast<size_t>(end - input) < len) {
return Result::ERROR_BAD_DER;
}
......@@ -286,13 +252,13 @@ public:
bool AtEnd() const { return input == end; }
class Mark final
{
public:
Mark(const Mark&) = default; // Intentionally not explicit.
private:
class Mark final {
public:
Mark(const Mark&) = default; // Intentionally not explicit.
private:
friend class Reader;
Mark(const Reader& aInput, const uint8_t* aMark) : input(aInput), mark(aMark) { }
Mark(const Reader& aInput, const uint8_t* aMark)
: input(aInput), mark(aMark) {}
const Reader& input;
const uint8_t* const mark;
void operator=(const Mark&) = delete;
......@@ -300,8 +266,7 @@ public:
Mark GetMark() const { return Mark(*this, input); }
Result GetInput(const Mark& mark, /*out*/ Input& item)
{
Result GetInput(const Mark& mark, /*out*/ Input& item) {
if (&mark.input != this || mark.mark > input) {
return NotReached("invalid mark", Result::FATAL_ERROR_INVALID_ARGS);
}
......@@ -309,9 +274,8 @@ public:
static_cast<Input::size_type>(input - mark.mark));
}
private:
Result Init(const uint8_t* data, Input::size_type len)
{
private:
Result Init(const uint8_t* data, Input::size_type len) {
if (input) {
// already initialized
return Result::FATAL_ERROR_INVALID_ARGS;
......@@ -328,9 +292,7 @@ private:
void operator=(const Reader&) = delete;
};
inline bool
InputContains(const Input& input, uint8_t toFind)
{
inline bool InputContains(const Input& input, uint8_t toFind) {
Reader reader(input);
for (;;) {
uint8_t b;
......@@ -342,7 +304,7 @@ InputContains(const Input& input, uint8_t toFind)
}
}
}
}
} // namespace mozilla::pkix
} } // namespace mozilla::pkix
#endif // mozilla_pkix_Input_h
#endif // mozilla_pkix_Input_h
This diff is collapsed.
......@@ -25,22 +25,22 @@
#ifndef mozilla_pkix_Time_h
#define mozilla_pkix_Time_h
#include <stdint.h>
#include <ctime>
#include <limits>
#include <stdint.h>
#include "mozpkix/Result.h"
namespace mozilla { namespace pkix {
namespace mozilla {
namespace pkix {
// Time with a range from the first second of year 0 (AD) through at least the
// last second of year 9999, which is the range of legal times in X.509 and
// OCSP. This type has second-level precision. The time zone is always UTC.
//
// Pass by value, not by reference.
class Time final
{
public:
class Time final {
public:
// Construct an uninitialized instance.
//
// This will fail to compile because there is no default constructor:
......@@ -49,52 +49,44 @@ public:
// This will succeed, leaving the time uninitialized:
// Time x(Time::uninitialized);
enum Uninitialized { uninitialized };
explicit Time(Uninitialized) { }
explicit Time(Uninitialized) {}
bool operator==(const Time& other) const
{
bool operator==(const Time& other) const {
return elapsedSecondsAD == other.elapsedSecondsAD;
}
bool operator>(const Time& other) const
{
bool operator>(const Time& other) const {
return elapsedSecondsAD > other.elapsedSecondsAD;
}
bool operator>=(const Time& other) const
{
bool operator>=(const Time& other) const {
return elapsedSecondsAD >= other.elapsedSecondsAD;
}
bool operator<(const Time& other) const
{
bool operator<(const Time& other) const {
return elapsedSecondsAD < other.elapsedSecondsAD;
}
bool operator<=(const Time& other) const
{
bool operator<=(const Time& other) const {
return elapsedSecondsAD <= other.elapsedSecondsAD;
}
Result AddSeconds(uint64_t seconds)
{
if (std::numeric_limits<uint64_t>::max() - elapsedSecondsAD
< seconds) {
return Result::FATAL_ERROR_INVALID_ARGS; // integer overflow
Result AddSeconds(uint64_t seconds) {
if (std::numeric_limits<uint64_t>::max() - elapsedSecondsAD < seconds) {
return Result::FATAL_ERROR_INVALID_ARGS; // integer overflow
}
elapsedSecondsAD += seconds;
return Success;
}
Result SubtractSeconds(uint64_t seconds)
{
Result SubtractSeconds(uint64_t seconds) {
if (seconds > elapsedSecondsAD) {
return Result::FATAL_ERROR_INVALID_ARGS; // integer overflow
return Result::FATAL_ERROR_INVALID_ARGS; // integer overflow
}
elapsedSecondsAD -= seconds;
return Success;
}
static const uint64_t ONE_DAY_IN_SECONDS
= UINT64_C(24) * UINT64_C(60) * UINT64_C(60);
static const uint64_t ONE_DAY_IN_SECONDS =
UINT64_C(24) * UINT64_C(60) * UINT64_C(60);
private:
private:
// This constructor is hidden to prevent accidents like this:
//
// Time foo(time_t t)
......@@ -103,17 +95,14 @@ private:
// return Time(t);
// }
explicit Time(uint64_t aElapsedSecondsAD)
: elapsedSecondsAD(aElapsedSecondsAD)
{
}
: elapsedSecondsAD(aElapsedSecondsAD) {}
friend Time TimeFromElapsedSecondsAD(uint64_t);
friend class Duration;
uint64_t elapsedSecondsAD;
};
inline Time TimeFromElapsedSecondsAD(uint64_t aElapsedSecondsAD)
{
inline Time TimeFromElapsedSecondsAD(uint64_t aElapsedSecondsAD) {
return Time(aElapsedSecondsAD);
}
......@@ -122,34 +111,27 @@ Time Now();
// Note the epoch is the unix epoch (ie 00:00:00 UTC, 1 January 1970)
Time TimeFromEpochInSeconds(uint64_t secondsSinceEpoch);
class Duration final
{
public:
class Duration final {
public:
Duration(Time timeA, Time timeB)
: durationInSeconds(timeA < timeB
? timeB.elapsedSecondsAD - timeA.elapsedSecondsAD
: timeA.elapsedSecondsAD - timeB.elapsedSecondsAD)
{
}
: durationInSeconds(
timeA < timeB ? timeB.elapsedSecondsAD - timeA.elapsedSecondsAD
: timeA.elapsedSecondsAD - timeB.elapsedSecondsAD) {}
explicit Duration(uint64_t aDurationInSeconds)
: durationInSeconds(aDurationInSeconds)
{
}
: durationInSeconds(aDurationInSeconds) {}
bool operator>(const Duration& other) const
{
bool operator>(const Duration& other) const {
return durationInSeconds > other.durationInSeconds;
}
bool operator<(const Duration& other) const
{
bool operator<(const Duration& other) const {
return durationInSeconds < other.durationInSeconds;
}
private:
private:
uint64_t durationInSeconds;
};
}
} // namespace mozilla::pkix
} } // namespace mozilla::pkix
#endif // mozilla_pkix_Time_h
#endif // mozilla_pkix_Time_h
......@@ -27,7 +27,8 @@
#include "mozpkix/pkixtypes.h"
namespace mozilla { namespace pkix {
namespace mozilla {
namespace pkix {
// ----------------------------------------------------------------------------
// LIMITED SUPPORT FOR CERTIFICATE POLICIES
......@@ -103,8 +104,8 @@ namespace mozilla { namespace pkix {
// requiredPolicy:
// This is the policy to apply; typically included in EV certificates.
// If there is no policy, pass in CertPolicyId::anyPolicy.
Result BuildCertChain(TrustDomain& trustDomain, Input cert,
Time time, EndEntityOrCA endEntityOrCA,
Result BuildCertChain(TrustDomain& trustDomain, Input cert, Time time,
EndEntityOrCA endEntityOrCA,
KeyUsage requiredKeyUsageIfPresent,
KeyPurposeId requiredEKUIfPresent,
const CertPolicyId& requiredPolicy,
......@@ -122,8 +123,7 @@ Result CheckCertHostname(Input cert, Input hostname,
// Construct an RFC-6960-encoded OCSP request, ready for submission to a
// responder, for the provided CertID. The request has no extensions.
static const size_t OCSP_REQUEST_MAX_LENGTH = 127;