1
#pragma once
2

            
3
#include <array>
4
#include <cstdint>
5
#include <deque>
6
#include <functional>
7
#include <string>
8
#include <utility>
9
#include <vector>
10

            
11
#include "envoy/common/pure.h"
12
#include "envoy/network/transport_socket.h"
13
#include "envoy/registry/registry.h"
14
#include "envoy/ssl/context.h"
15
#include "envoy/ssl/context_config.h"
16
#include "envoy/ssl/private_key/private_key.h"
17
#include "envoy/ssl/ssl_socket_extended_info.h"
18

            
19
#include "source/common/common/logger.h"
20
#include "source/common/common/matchers.h"
21
#include "source/common/stats/symbol_table.h"
22
#include "source/common/tls/cert_validator/cert_validator.h"
23
#include "source/common/tls/cert_validator/san_matcher.h"
24
#include "source/common/tls/stats.h"
25

            
26
#include "absl/synchronization/mutex.h"
27
#include "openssl/ssl.h"
28
#include "openssl/x509v3.h"
29

            
30
namespace Envoy {
31
namespace Extensions {
32
namespace TransportSockets {
33
namespace Tls {
34

            
35
class DefaultCertValidator : public CertValidator, Logger::Loggable<Logger::Id::connection> {
36
public:
37
  DefaultCertValidator(const Envoy::Ssl::CertificateValidationContextConfig* config,
38
                       SslStats& stats, Server::Configuration::CommonFactoryContext& context);
39

            
40
7013
  ~DefaultCertValidator() override = default;
41

            
42
  // Tls::CertValidator
43
  absl::Status addClientValidationContext(SSL_CTX* context, bool require_client_cert) override;
44

            
45
  ValidationResults
46
  doVerifyCertChain(STACK_OF(X509)& cert_chain, Ssl::ValidateResultCallbackPtr callback,
47
                    const Network::TransportSocketOptionsConstSharedPtr& transport_socket_options,
48
                    SSL_CTX& ssl, const CertValidator::ExtraValidationContext& validation_context,
49
                    bool is_server, absl::string_view host_name) override;
50

            
51
  absl::StatusOr<int> initializeSslContexts(std::vector<SSL_CTX*> contexts,
52
                                            bool provides_certificates,
53
                                            Stats::Scope& scope) override;
54

            
55
  void updateDigestForSessionId(bssl::ScopedEVP_MD_CTX& md, uint8_t hash_buffer[EVP_MAX_MD_SIZE],
56
                                unsigned hash_length) override;
57

            
58
  absl::optional<uint32_t> daysUntilFirstCertExpires() const override;
59
6
  std::string getCaFileName() const override { return ca_file_path_; };
60
  Envoy::Ssl::CertificateDetailsPtr getCaCertInformation() const override;
61

            
62
  // Utility functions.
63
  Envoy::Ssl::ClientValidationStatus
64
  verifyCertificate(X509* cert, const std::vector<std::string>& verify_san_list,
65
                    const std::vector<SanMatcherPtr>& subject_alt_name_matchers,
66
                    OptRef<const StreamInfo::StreamInfo> stream_info, std::string* error_details,
67
                    uint8_t* out_alert);
68

            
69
  /**
70
   * Verifies certificate hash for pinning. The hash is a hex-encoded SHA-256 of the DER-encoded
71
   * certificate.
72
   *
73
   * @param ssl the certificate to verify
74
   * @param expected_hashes the configured list of certificate hashes to match
75
   * @return true if the verification succeeds
76
   */
77
  static bool verifyCertificateHashList(X509* cert,
78
                                        const std::vector<std::vector<uint8_t>>& expected_hashes);
79

            
80
  /**
81
   * Verifies certificate hash for pinning. The hash is a base64-encoded SHA-256 of the DER-encoded
82
   * Subject Public Key Information (SPKI) of the certificate.
83
   *
84
   * @param ssl the certificate to verify
85
   * @param expected_hashes the configured list of certificate hashes to match
86
   * @return true if the verification succeeds
87
   */
88
  static bool verifyCertificateSpkiList(X509* cert,
89
                                        const std::vector<std::vector<uint8_t>>& expected_hashes);
90

            
91
  /**
92
   * Performs subjectAltName verification
93
   * @param ssl the certificate to verify
94
   * @param subject_alt_names the configured subject_alt_names to match
95
   * @return true if the verification succeeds
96
   */
97
  static bool verifySubjectAltName(X509* cert, const std::vector<std::string>& subject_alt_names);
98

            
99
  /**
100
   * Performs subjectAltName matching with the provided matchers.
101
   * @param ssl the certificate to verify
102
   * @param subject_alt_name_matchers the configured matchers to match
103
   * @return true if the verification succeeds
104
   */
105
  static bool matchSubjectAltName(X509* cert, OptRef<const StreamInfo::StreamInfo> stream_info,
106
                                  const std::vector<SanMatcherPtr>& subject_alt_name_matchers);
107

            
108
private:
109
  bool verifyCertAndUpdateStatus(X509* leaf_cert, absl::string_view sni,
110
                                 const Network::TransportSocketOptions* transport_socket_options,
111
                                 const CertValidator::ExtraValidationContext& validation_context,
112
                                 Envoy::Ssl::ClientValidationStatus& detailed_status,
113
                                 std::string* error_details, uint8_t* out_alert);
114

            
115
  void initializeCertExpirationStats(Stats::Scope& scope);
116
  const Envoy::Ssl::CertificateValidationContextConfig* config_;
117
  SslStats& stats_;
118
  Server::Configuration::CommonFactoryContext& context_;
119
  bssl::UniquePtr<X509> ca_cert_;
120
  std::string ca_file_path_;
121
  std::vector<SanMatcherPtr> subject_alt_name_matchers_;
122
  std::vector<std::vector<uint8_t>> verify_certificate_hash_list_;
123
  std::vector<std::vector<uint8_t>> verify_certificate_spki_list_;
124
  bool allow_untrusted_certificate_{false};
125
  bool verify_trusted_ca_{false};
126
  const bool auto_sni_san_match_{false};
127
};
128

            
129
DECLARE_FACTORY(DefaultCertValidatorFactory);
130

            
131
} // namespace Tls
132
} // namespace TransportSockets
133
} // namespace Extensions
134
} // namespace Envoy