1 // 2 // 3 // Copyright 2015 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 GRPCPP_SECURITY_SERVER_CREDENTIALS_H 20 #define GRPCPP_SECURITY_SERVER_CREDENTIALS_H 21 22 #include <grpc/grpc_security_constants.h> 23 #include <grpcpp/impl/grpc_library.h> 24 #include <grpcpp/security/auth_metadata_processor.h> 25 #include <grpcpp/security/tls_credentials_options.h> 26 #include <grpcpp/support/config.h> 27 28 #include <memory> 29 #include <vector> 30 31 struct grpc_server; 32 33 namespace grpc { 34 35 class Server; 36 class ServerCredentials; 37 38 /// Options to create ServerCredentials with SSL 39 struct SslServerCredentialsOptions { 40 /// \warning Deprecated SslServerCredentialsOptionsSslServerCredentialsOptions41 SslServerCredentialsOptions() 42 : force_client_auth(false), 43 client_certificate_request(GRPC_SSL_DONT_REQUEST_CLIENT_CERTIFICATE) {} SslServerCredentialsOptionsSslServerCredentialsOptions44 explicit SslServerCredentialsOptions( 45 grpc_ssl_client_certificate_request_type request_type) 46 : force_client_auth(false), client_certificate_request(request_type) {} 47 48 struct PemKeyCertPair { 49 std::string private_key; 50 std::string cert_chain; 51 }; 52 std::string pem_root_certs; 53 std::vector<PemKeyCertPair> pem_key_cert_pairs; 54 /// \warning Deprecated 55 bool force_client_auth; 56 57 /// If both \a force_client_auth and \a client_certificate_request 58 /// fields are set, \a force_client_auth takes effect, i.e. 59 /// \a REQUEST_AND_REQUIRE_CLIENT_CERTIFICATE_AND_VERIFY 60 /// will be enforced. 61 grpc_ssl_client_certificate_request_type client_certificate_request; 62 }; 63 64 /// Builds Xds ServerCredentials given fallback credentials 65 std::shared_ptr<ServerCredentials> XdsServerCredentials( 66 const std::shared_ptr<ServerCredentials>& fallback_credentials); 67 68 /// Wrapper around \a grpc_server_credentials, a way to authenticate a server. 69 class ServerCredentials : private grpc::internal::GrpcLibrary { 70 public: 71 ~ServerCredentials() override; 72 73 /// This method is not thread-safe and has to be called before the server is 74 /// started. The last call to this function wins. 75 virtual void SetAuthMetadataProcessor( 76 const std::shared_ptr<grpc::AuthMetadataProcessor>& processor); 77 78 protected: 79 explicit ServerCredentials(grpc_server_credentials* creds); 80 c_creds()81 grpc_server_credentials* c_creds() const { return c_creds_; } 82 83 private: 84 // Needed for access to AddPortToServer. 85 friend class Server; 86 // Needed for access to c_creds_. 87 friend class ServerBuilder; 88 friend std::shared_ptr<ServerCredentials> grpc::XdsServerCredentials( 89 const std::shared_ptr<ServerCredentials>& fallback_credentials); 90 91 /// Tries to bind \a server to the given \a addr (eg, localhost:1234, 92 /// 192.168.1.1:31416, [::1]:27182, etc.) 93 /// 94 /// \return bound port number on success, 0 on failure. 95 // TODO(dgq): the "port" part seems to be a misnomer. 96 virtual int AddPortToServer(const std::string& addr, grpc_server* server); 97 98 grpc_server_credentials* c_creds_; 99 }; 100 101 /// Builds SSL ServerCredentials given SSL specific options 102 std::shared_ptr<ServerCredentials> SslServerCredentials( 103 const grpc::SslServerCredentialsOptions& options); 104 105 std::shared_ptr<ServerCredentials> InsecureServerCredentials(); 106 107 namespace experimental { 108 109 /// Options to create ServerCredentials with ALTS 110 struct AltsServerCredentialsOptions { 111 /// Add fields if needed. 112 }; 113 114 /// Builds ALTS ServerCredentials given ALTS specific options 115 std::shared_ptr<ServerCredentials> AltsServerCredentials( 116 const AltsServerCredentialsOptions& options); 117 118 /// Builds Local ServerCredentials. 119 std::shared_ptr<ServerCredentials> LocalServerCredentials( 120 grpc_local_connect_type type); 121 122 /// Builds TLS ServerCredentials given TLS options. 123 std::shared_ptr<ServerCredentials> TlsServerCredentials( 124 const experimental::TlsServerCredentialsOptions& options); 125 126 } // namespace experimental 127 } // namespace grpc 128 129 #endif // GRPCPP_SECURITY_SERVER_CREDENTIALS_H 130