GRPC C++  1.80.0
private_key_signer.h
Go to the documentation of this file.
1 //
2 //
3 // Copyright 2025 gRPC authors.
4 //
5 // Licensed under the Apache License, Version 2.0 (the "License");
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17 //
18 
19 #ifndef GRPC_PRIVATE_KEY_SIGNER_H
20 #define GRPC_PRIVATE_KEY_SIGNER_H
21 
22 #include <grpc/credentials.h>
23 
24 #include <memory>
25 #include <string>
26 #include <variant>
27 
28 #include "absl/functional/any_invocable.h"
29 #include "absl/status/statusor.h"
30 #include "absl/strings/string_view.h"
31 
32 namespace grpc_core {
33 
34 // Implementations of this class must be thread-safe.
36  public:
37  // A handle for an asynchronous signing operation.
38  //
39  // When `PrivateKeySigner::Sign` is implemented asynchronously, it returns an
40  // instance of a concrete implementation of this class. This handle is used
41  // to manage the asynchronous signing operation and can be used to cancel the
42  // operation via `PrivateKeySigner::Cancel`.
43  //
44  // Users must provide their own concrete implementation of this class. The
45  // handle can store any state needed for the asynchronous operation.
47  public:
48  virtual ~AsyncSigningHandle() = default;
49  };
50 
51  // Enum class representing TLS signature algorithm identifiers from BoringSSL.
52  // The values correspond to the SSL_SIGN_* macros in <openssl/ssl.h>.
53  enum class SignatureAlgorithm {
63  };
64 
65  // A callback that is invoked when an asynchronous signing operation is
66  // complete. The argument should contain the signed bytes on success, or a
67  // non-OK status on failure.
68  using OnSignComplete = absl::AnyInvocable<void(absl::StatusOr<std::string>)>;
69 
70  virtual ~PrivateKeySigner() = default;
71 
72  // Signs data_to_sign.
73  // May return either synchronously or asynchronously.
74  // For synchronous returns, directly returns either the signed bytes
75  // or a failed status, and the callback will never be invoked.
76  // For asynchronous implementations, returns a handle for the asynchronous
77  // signing operation. The function argument on_sign_complete must be called by
78  // the implementer when the async signing operation is complete.
79  // on_sign_complete must not be invoked synchronously within Sign().
80  virtual std::variant<absl::StatusOr<std::string>,
81  std::shared_ptr<AsyncSigningHandle>>
82  Sign(absl::string_view data_to_sign, SignatureAlgorithm signature_algorithm,
83  OnSignComplete on_sign_complete) = 0;
84 
85  // Cancels an in-flight async signing operation using a handle returned
86  // from a previous call to Sign().
87  virtual void Cancel(std::shared_ptr<AsyncSigningHandle> handle) = 0;
88 };
89 } // namespace grpc_core
90 
101  std::shared_ptr<grpc_core::PrivateKeySigner> private_key_signer,
102  absl::string_view cert_chain);
103 
104 #endif /* GRPC_PRIVATE_KEY_SIGNER_H */
grpc_core::PrivateKeySigner::OnSignComplete
absl::AnyInvocable< void(absl::StatusOr< std::string >)> OnSignComplete
Definition: private_key_signer.h:68
grpc_core::PrivateKeySigner::SignatureAlgorithm::kRsaPssRsaeSha384
@ kRsaPssRsaeSha384
grpc_core::PrivateKeySigner::Cancel
virtual void Cancel(std::shared_ptr< AsyncSigningHandle > handle)=0
grpc_core::PrivateKeySigner::SignatureAlgorithm::kEcdsaSecp384r1Sha384
@ kEcdsaSecp384r1Sha384
grpc_core::PrivateKeySigner::Sign
virtual std::variant< absl::StatusOr< std::string >, std::shared_ptr< AsyncSigningHandle > > Sign(absl::string_view data_to_sign, SignatureAlgorithm signature_algorithm, OnSignComplete on_sign_complete)=0
grpc_core
Definition: grpc_audit_logging.h:31
credentials.h
grpc_core::PrivateKeySigner::~PrivateKeySigner
virtual ~PrivateKeySigner()=default
grpc_core::PrivateKeySigner
Definition: private_key_signer.h:35
grpc_core::PrivateKeySigner::SignatureAlgorithm::kRsaPkcs1Sha512
@ kRsaPkcs1Sha512
grpc_core::PrivateKeySigner::SignatureAlgorithm::kEcdsaSecp256r1Sha256
@ kEcdsaSecp256r1Sha256
grpc_core::PrivateKeySigner::SignatureAlgorithm::kRsaPkcs1Sha384
@ kRsaPkcs1Sha384
grpc_core::PrivateKeySigner::SignatureAlgorithm::kEcdsaSecp521r1Sha512
@ kEcdsaSecp521r1Sha512
grpc_core::PrivateKeySigner::SignatureAlgorithm
SignatureAlgorithm
Definition: private_key_signer.h:53
grpc_core::PrivateKeySigner::SignatureAlgorithm::kRsaPkcs1Sha256
@ kRsaPkcs1Sha256
grpc_core::PrivateKeySigner::SignatureAlgorithm::kRsaPssRsaeSha512
@ kRsaPssRsaeSha512
grpc_core::PrivateKeySigner::AsyncSigningHandle
Definition: private_key_signer.h:46
grpc_core::PrivateKeySigner::AsyncSigningHandle::~AsyncSigningHandle
virtual ~AsyncSigningHandle()=default
grpc_core::PrivateKeySigner::SignatureAlgorithm::kRsaPssRsaeSha256
@ kRsaPssRsaeSha256
grpc::protobuf::util::Status
::absl::Status Status
Definition: config_protobuf.h:107
grpc_tls_identity_pairs_add_pair_with_signer
absl::Status grpc_tls_identity_pairs_add_pair_with_signer(grpc_tls_identity_pairs *pairs, std::shared_ptr< grpc_core::PrivateKeySigner > private_key_signer, absl::string_view cert_chain)
EXPERIMENTAL API - Subject to change.
grpc_tls_identity_pairs
struct grpc_tls_identity_pairs grpc_tls_identity_pairs
EXPERIMENTAL API - Subject to change.
Definition: credentials.h:617