// Copyright 2011 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef NET_CERT_CERT_VERIFY_RESULT_H_
#define NET_CERT_CERT_VERIFY_RESULT_H_

#include <optional>

#include "base/memory/scoped_refptr.h"
#include "base/values.h"
#include "build/build_config.h"
#include "net/base/hash_value.h"
#include "net/base/net_export.h"
#include "net/cert/cert_status_flags.h"
#include "net/cert/ct_policy_status.h"
#include "net/cert/signed_certificate_timestamp_and_status.h"
#include "net/net_buildflags.h"
#include "third_party/boringssl/src/include/openssl/pki/ocsp.h"

namespace ct {
enum class CTPolicyCompliance;
}  // namespace ct

namespace net {

class X509Certificate;

// The result of certificate verification.
// LINT.IfChange(CertVerifyResult)
class NET_EXPORT CertVerifyResult {
 public:
  CertVerifyResult();
  CertVerifyResult(const CertVerifyResult& other);
  ~CertVerifyResult();

  void Reset();

  // Creates NetLog parameter to describe the CertVerifyResult. |net_error| is
  // a net error code to include in the params, if non-zero. It must not be
  // ERR_IO_PENDING, as that is not a true error.
  base::DictValue NetLogParams(int net_error) const;

  // The certificate chain that was constructed during verification.
  //
  // Note: Although |verified_cert| will match the originally supplied
  // certificate to be validated, the results of intermediate_buffers()
  // may be substantially different, both in order and in content, then the
  // originally supplied intermediates.
  //
  // In the event of validation failures, this may contain the originally
  // supplied certificate chain or a partially constructed path, depending on
  // the implementation.
  //
  // In the event of validation success, the trust anchor will be
  // |verified_cert->intermediate_buffers().back()| if
  // there was a certificate chain to the trust anchor, and will
  // be |verified_cert->cert_buffer()| if the certificate was
  // the trust anchor.
  scoped_refptr<X509Certificate> verified_cert;

  // Bitmask of CERT_STATUS_* from net/cert/cert_status_flags.h. Note that
  // these status flags apply to the certificate chain returned in
  // |verified_cert|, rather than the originally supplied certificate
  // chain.
  CertStatus cert_status;

  // If the certificate was successfully verified then this contains the
  // hashes for all of the SubjectPublicKeyInfos of the chain (target,
  // intermediates, and trust anchor)
  //
  // The ordering of the hashes matches the order of the |verified_cert| chain
  // (leaf to root).
  std::vector<SHA256HashValue> public_key_hashes;

  // is_issued_by_known_root is true if we recognise the root CA as a standard
  // root.  If it isn't then it's probably the case that this certificate was
  // generated by a MITM proxy whose root has been installed locally. This is
  // meaningless if the certificate was not trusted.
  bool is_issued_by_known_root;

  // Verification of stapled OCSP response, if present.
  bssl::OCSPVerifyResult ocsp_result;

  // `scts` contains the result of verifying any provided or embedded SCTs for
  // this certificate against the set of known logs. Consumers should not simply
  // check this for the presence of a successfully verified SCT to determine CT
  // compliance. Instead look at `policy_compliance`.
  SignedCertificateTimestampAndStatusList scts;

  // The result of evaluating whether the certificate complies with the
  // Certificate Transparency policy.
  ct::CTPolicyCompliance policy_compliance;

  // The result of evaluating CT requirements.
  ct::CTRequirementsStatus ct_requirement_status;

#if BUILDFLAG(CHROME_ROOT_STORE_SUPPORTED)
  // Special values of crs_root_id. Values starting at 3 are actual ids, and
  // the source of truth is the root_store.textproto.
  // These values are recorded in histograms, do not change or re-order.
  enum CrsRootIdSpecialValues {
    // The value zero is never used, it is reserved to avoid the possible case
    // of an integer that was default initialized to zero slipping in
    // unnoticed.
    kReserved = 0,
    kCrsRootIdPrivatelyTrustedRoot = 1,
    kCrsRootIdUnknownId = 2,
  };
  // The stable identifier of the root of the chain of `verified_cert`, or one
  // of the special values in the `CrsRootIdSpecialValues` enum.
  // Will be nullopt in builds where CRS is optionally supported but was not
  // used.
  std::optional<int32_t> crs_root_id;
#endif
};
// LINT.ThenChange(/services/network/public/mojom/cert_verify_result.mojom:CertVerifyResult)

}  // namespace net

#endif  // NET_CERT_CERT_VERIFY_RESULT_H_
