1
#pragma once
2

            
3
#include <cstdint>
4
#include <memory>
5
#include <string>
6
#include <vector>
7

            
8
#include "envoy/common/pure.h"
9
#include "envoy/event/dispatcher.h"
10
#include "envoy/ssl/handshaker.h"
11

            
12
#include "absl/strings/string_view.h"
13

            
14
namespace Envoy {
15
namespace Ssl {
16

            
17
// Opaque type defined and used by the ``ServerContext``.
18
struct TlsContext;
19

            
20
enum class ClientValidationStatus { NotValidated, NoClientCertificate, Validated, Failed };
21

            
22
enum class ValidateStatus {
23
  NotStarted,
24
  Pending,
25
  Successful,
26
  Failed,
27
};
28

            
29
enum class CertificateSelectionStatus {
30
  NotStarted,
31
  Pending,
32
  Successful,
33
  Failed,
34
};
35

            
36
/**
37
 * Used to return the result from an asynchronous cert validation.
38
 */
39
class ValidateResultCallback {
40
public:
41
4149
  virtual ~ValidateResultCallback() = default;
42

            
43
  virtual Event::Dispatcher& dispatcher() PURE;
44

            
45
  /**
46
   * Called when the asynchronous cert validation completes.
47
   * @param succeeded true if the validation succeeds
48
   * @param detailed_status detailed status of the underlying validation. Depending on the
49
   *        validation configuration, `succeeded` may be true but `detailed_status` might
50
   *        indicate a failure. This detailed status can be used to inform routing
51
   *        decisions.
52
   * @param error_details failure details, only used if the validation fails.
53
   * @param tls_alert the TLS error related to the failure, only used if the validation fails.
54
   */
55
  virtual void onCertValidationResult(bool succeeded, ClientValidationStatus detailed_status,
56
                                      const std::string& error_details, uint8_t tls_alert) PURE;
57
};
58

            
59
using ValidateResultCallbackPtr = std::unique_ptr<ValidateResultCallback>;
60

            
61
class SslExtendedSocketInfo {
62
public:
63
2605
  virtual ~SslExtendedSocketInfo() = default;
64

            
65
  /**
66
   * Set the peer certificate validation status.
67
   **/
68
  virtual void setCertificateValidationStatus(ClientValidationStatus validated) PURE;
69

            
70
  /**
71
   * @return ClientValidationStatus The peer certificate validation status.
72
   **/
73
  virtual ClientValidationStatus certificateValidationStatus() const PURE;
74

            
75
  /**
76
   * @return ValidateResultCallbackPtr a callback used to return the validation result.
77
   */
78
  virtual ValidateResultCallbackPtr createValidateResultCallback() PURE;
79

            
80
  /**
81
   * Called after the cert validation completes either synchronously or asynchronously.
82
   * @param succeeded true if the validation succeeded.
83
   * @param async true if the validation is completed asynchronously.
84
   */
85
  virtual void onCertificateValidationCompleted(bool succeeded, bool async) PURE;
86

            
87
  /**
88
   * @return ValidateStatus the validation status.
89
   */
90
  virtual ValidateStatus certificateValidationResult() const PURE;
91

            
92
  /**
93
   * Called when doing asynchronous cert validation.
94
   * @return uint8_t represents the TLS alert populated by cert validator in
95
   * case of failure.
96
   */
97
  virtual uint8_t certificateValidationAlert() const PURE;
98

            
99
  /**
100
   * @return CertificateSelectionCallbackPtr a callback used to return the cert selection result.
101
   */
102
  virtual CertificateSelectionCallbackPtr createCertificateSelectionCallback() PURE;
103

            
104
  /**
105
   * Attach additional certificate selection data to the TLS socket connection.
106
   */
107
  virtual void
108
  setCertSelectionHandle(Ssl::SelectionHandleConstSharedPtr cert_selection_handle) PURE;
109

            
110
  /**
111
   * Called after the cert selection completes either synchronously or asynchronously.
112
   * @param selected_ctx selected Ssl::TlsContext, it's empty when selection failed.
113
   * @param async true if the validation is completed asynchronously.
114
   * @param staple true when need to set OCSP response.
115
   */
116
  virtual void onCertificateSelectionCompleted(OptRef<const Ssl::TlsContext> selected_ctx,
117
                                               bool staple, bool async) PURE;
118

            
119
  /**
120
   * @return CertificateSelectionStatus the cert selection status.
121
   */
122
  virtual CertificateSelectionStatus certificateSelectionResult() const PURE;
123

            
124
  /**
125
   * Set detailed certificate validation error information.
126
   * @param error_details the detailed error message from certificate validation.
127
   */
128
  virtual void setCertificateValidationError(absl::string_view error_details) PURE;
129

            
130
  /**
131
   * @return the detailed certificate validation error message, or empty if none.
132
   */
133
  virtual absl::string_view certificateValidationError() const PURE;
134
};
135

            
136
} // namespace Ssl
137
} // namespace Envoy