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 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 std::shared_ptr<ServerCredentials> grpc::XdsServerCredentials( 88 const std::shared_ptr<ServerCredentials>& fallback_credentials); 89 90 /// Tries to bind \a server to the given \a addr (eg, localhost:1234, 91 /// 192.168.1.1:31416, [::1]:27182, etc.) 92 /// 93 /// \return bound port number on success, 0 on failure. 94 // TODO(dgq): the "port" part seems to be a misnomer. 95 virtual int AddPortToServer(const std::string& addr, grpc_server* server); 96 97 grpc_server_credentials* c_creds_; 98 }; 99 100 /// Builds SSL ServerCredentials given SSL specific options 101 std::shared_ptr<ServerCredentials> SslServerCredentials( 102 const grpc::SslServerCredentialsOptions& options); 103 104 std::shared_ptr<ServerCredentials> InsecureServerCredentials(); 105 106 namespace experimental { 107 108 /// Options to create ServerCredentials with ALTS 109 struct AltsServerCredentialsOptions { 110 /// Add fields if needed. 111 }; 112 113 /// Builds ALTS ServerCredentials given ALTS specific options 114 std::shared_ptr<ServerCredentials> AltsServerCredentials( 115 const AltsServerCredentialsOptions& options); 116 117 /// Builds Local ServerCredentials. 118 std::shared_ptr<ServerCredentials> AltsServerCredentials( 119 const AltsServerCredentialsOptions& options); 120 121 std::shared_ptr<ServerCredentials> LocalServerCredentials( 122 grpc_local_connect_type type); 123 124 /// Builds TLS ServerCredentials given TLS options. 125 std::shared_ptr<ServerCredentials> TlsServerCredentials( 126 const experimental::TlsServerCredentialsOptions& options); 127 128 } // namespace experimental 129 } // namespace grpc 130 131 #endif // GRPCPP_SECURITY_SERVER_CREDENTIALS_H 132