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 <memory> 23 #include <vector> 24 25 #include <grpc/grpc_security_constants.h> 26 #include <grpcpp/impl/grpc_library.h> 27 #include <grpcpp/security/auth_metadata_processor.h> 28 #include <grpcpp/security/tls_credentials_options.h> 29 #include <grpcpp/support/config.h> 30 31 struct grpc_server; 32 33 namespace grpc { 34 35 class Server; 36 class ServerCredentials; 37 class SecureServerCredentials; 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 /// This method is not thread-safe and has to be called before the server is 72 /// started. The last call to this function wins. 73 virtual void SetAuthMetadataProcessor( 74 const std::shared_ptr<grpc::AuthMetadataProcessor>& processor) = 0; 75 76 private: 77 friend class Server; 78 79 // We need this friend declaration for access to Insecure() and 80 // AsSecureServerCredentials(). When these two functions are no longer 81 // necessary, this friend declaration can be removed too. 82 friend std::shared_ptr<ServerCredentials> grpc::XdsServerCredentials( 83 const std::shared_ptr<ServerCredentials>& fallback_credentials); 84 85 /// Tries to bind \a server to the given \a addr (eg, localhost:1234, 86 /// 192.168.1.1:31416, [::1]:27182, etc.) 87 /// 88 /// \return bound port number on success, 0 on failure. 89 // TODO(dgq): the "port" part seems to be a misnomer. 90 virtual int AddPortToServer(const std::string& addr, grpc_server* server) = 0; 91 92 // TODO(yashykt): This is a hack since InsecureServerCredentials() cannot use 93 // grpc_insecure_server_credentials_create() and should be removed after 94 // insecure builds are removed from gRPC. IsInsecure()95 virtual bool IsInsecure() const { return false; } 96 97 // TODO(yashkt): This is a hack that should be removed once we remove insecure 98 // builds and the indirect method of adding ports to a server. AsSecureServerCredentials()99 virtual SecureServerCredentials* AsSecureServerCredentials() { 100 return nullptr; 101 } 102 }; 103 104 /// Builds SSL ServerCredentials given SSL specific options 105 std::shared_ptr<ServerCredentials> SslServerCredentials( 106 const grpc::SslServerCredentialsOptions& options); 107 108 std::shared_ptr<ServerCredentials> InsecureServerCredentials(); 109 110 namespace experimental { 111 112 /// Options to create ServerCredentials with ALTS 113 struct AltsServerCredentialsOptions { 114 /// Add fields if needed. 115 }; 116 117 /// Builds ALTS ServerCredentials given ALTS specific options 118 std::shared_ptr<ServerCredentials> AltsServerCredentials( 119 const AltsServerCredentialsOptions& options); 120 121 /// Builds Local ServerCredentials. 122 std::shared_ptr<ServerCredentials> AltsServerCredentials( 123 const AltsServerCredentialsOptions& options); 124 125 std::shared_ptr<ServerCredentials> LocalServerCredentials( 126 grpc_local_connect_type type); 127 128 /// Builds TLS ServerCredentials given TLS options. 129 std::shared_ptr<ServerCredentials> TlsServerCredentials( 130 const experimental::TlsServerCredentialsOptions& options); 131 132 } // namespace experimental 133 } // namespace grpc 134 135 #endif // GRPCPP_SECURITY_SERVER_CREDENTIALS_H 136