| 1 |
// |
| 2 |
// Copyright 2020 gRPC authors. |
| 3 |
// |
| 4 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
// you may not use this file except in compliance with the License. |
| 6 |
// You may obtain a copy of the License at |
| 7 |
// |
| 8 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
// |
| 10 |
// Unless required by applicable law or agreed to in writing, software |
| 11 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
// See the License for the specific language governing permissions and |
| 14 |
// limitations under the License. |
| 15 |
// |
| 16 |
|
| 17 |
#include <memory> |
| 18 |
|
| 19 |
#include <gmock/gmock.h> |
| 20 |
#include <gtest/gtest.h> |
| 21 |
|
| 22 |
#include <grpc/grpc.h> |
| 23 |
#include <grpc/grpc_security.h> |
| 24 |
#include <grpcpp/security/server_credentials.h> |
| 25 |
#include <grpcpp/security/tls_credentials_options.h> |
| 26 |
|
| 27 |
#include "src/cpp/client/secure_credentials.h" |
| 28 |
#include "test/core/util/port.h" |
| 29 |
#include "test/core/util/test_config.h" |
| 30 |
#include "test/cpp/util/tls_test_utils.h" |
| 31 |
|
| 32 |
#define CA_CERT_PATH "src/core/tsi/test_creds/ca.pem" |
| 33 |
#define SERVER_CERT_PATH "src/core/tsi/test_creds/server1.pem" |
| 34 |
#define SERVER_KEY_PATH "src/core/tsi/test_creds/server1.key" |
| 35 |
#define CRL_DIR_PATH "test/core/tsi/test_creds/crl_data" |
| 36 |
|
| 37 |
namespace { |
| 38 |
|
| 39 |
constexpr const char* kRootCertName = "root_cert_name"; |
| 40 |
constexpr const char* kRootCertContents = "root_cert_contents"; |
| 41 |
constexpr const char* kIdentityCertName = "identity_cert_name"; |
| 42 |
constexpr const char* kIdentityCertPrivateKey = "identity_private_key"; |
| 43 |
constexpr const char* kIdentityCertContents = "identity_cert_contents"; |
| 44 |
|
| 45 |
using ::grpc::experimental::ExternalCertificateVerifier; |
| 46 |
using ::grpc::experimental::FileWatcherCertificateProvider; |
| 47 |
using ::grpc::experimental::StaticDataCertificateProvider; |
| 48 |
|
| 49 |
} // namespace |
| 50 |
|
| 51 |
namespace grpc { |
| 52 |
namespace testing { |
| 53 |
namespace { |
| 54 |
|
| 55 |
TEST( |
| 56 |
CredentialsTest, |
| 57 |
TlsServerCredentialsWithStaticDataCertificateProviderLoadingRootAndIdentity) { |
| 58 |
experimental::IdentityKeyCertPair key_cert_pair; |
| 59 |
key_cert_pair.private_key = kIdentityCertPrivateKey; |
| 60 |
key_cert_pair.certificate_chain = kIdentityCertContents; |
| 61 |
std::vector<experimental::IdentityKeyCertPair> identity_key_cert_pairs; |
| 62 |
identity_key_cert_pairs.emplace_back(key_cert_pair); |
| 63 |
auto certificate_provider = std::make_shared<StaticDataCertificateProvider>( |
| 64 |
kRootCertContents, identity_key_cert_pairs); |
| 65 |
grpc::experimental::TlsServerCredentialsOptions options(certificate_provider); |
| 66 |
options.watch_root_certs(); |
| 67 |
options.set_root_cert_name(kRootCertName); |
| 68 |
options.watch_identity_key_cert_pairs(); |
| 69 |
options.set_identity_cert_name(kIdentityCertName); |
| 70 |
options.set_cert_request_type( |
| 71 |
GRPC_SSL_REQUEST_AND_REQUIRE_CLIENT_CERTIFICATE_AND_VERIFY); |
| 72 |
auto server_credentials = grpc::experimental::TlsServerCredentials(options); |
| 73 |
GPR_ASSERT(server_credentials.get() != nullptr); |
| 74 |
} |
| 75 |
|
| 76 |
// ServerCredentials should always have identity credential presented. |
| 77 |
// Otherwise gRPC stack will fail. |
| 78 |
TEST(CredentialsTest, |
| 79 |
TlsServerCredentialsWithStaticDataCertificateProviderLoadingIdentityOnly) { |
| 80 |
experimental::IdentityKeyCertPair key_cert_pair; |
| 81 |
key_cert_pair.private_key = kIdentityCertPrivateKey; |
| 82 |
key_cert_pair.certificate_chain = kIdentityCertContents; |
| 83 |
std::vector<experimental::IdentityKeyCertPair> identity_key_cert_pairs; |
| 84 |
// Adding two key_cert_pair(s) should still work. |
| 85 |
identity_key_cert_pairs.emplace_back(key_cert_pair); |
| 86 |
identity_key_cert_pairs.emplace_back(key_cert_pair); |
| 87 |
auto certificate_provider = |
| 88 |
std::make_shared<StaticDataCertificateProvider>(identity_key_cert_pairs); |
| 89 |
grpc::experimental::TlsServerCredentialsOptions options(certificate_provider); |
| 90 |
options.watch_identity_key_cert_pairs(); |
| 91 |
options.set_identity_cert_name(kIdentityCertName); |
| 92 |
options.set_cert_request_type( |
| 93 |
GRPC_SSL_REQUEST_AND_REQUIRE_CLIENT_CERTIFICATE_AND_VERIFY); |
| 94 |
auto server_credentials = grpc::experimental::TlsServerCredentials(options); |
| 95 |
GPR_ASSERT(server_credentials.get() != nullptr); |
| 96 |
} |
| 97 |
|
| 98 |
TEST( |
| 99 |
CredentialsTest, |
| 100 |
TlsServerCredentialsWithFileWatcherCertificateProviderLoadingRootAndIdentity) { |
| 101 |
auto certificate_provider = std::make_shared<FileWatcherCertificateProvider>( |
| 102 |
SERVER_KEY_PATH, SERVER_CERT_PATH, CA_CERT_PATH, 1); |
| 103 |
grpc::experimental::TlsServerCredentialsOptions options(certificate_provider); |
| 104 |
options.watch_root_certs(); |
| 105 |
options.set_root_cert_name(kRootCertName); |
| 106 |
options.watch_identity_key_cert_pairs(); |
| 107 |
options.set_identity_cert_name(kIdentityCertName); |
| 108 |
options.set_cert_request_type( |
| 109 |
GRPC_SSL_REQUEST_AND_REQUIRE_CLIENT_CERTIFICATE_AND_VERIFY); |
| 110 |
auto server_credentials = grpc::experimental::TlsServerCredentials(options); |
| 111 |
GPR_ASSERT(server_credentials.get() != nullptr); |
| 112 |
} |
| 113 |
|
| 114 |
TEST(CredentialsTest, TlsServerCredentialsWithCrlChecking) { |
| 115 |
auto certificate_provider = std::make_shared<FileWatcherCertificateProvider>( |
| 116 |
SERVER_KEY_PATH, SERVER_CERT_PATH, CA_CERT_PATH, 1); |
| 117 |
grpc::experimental::TlsServerCredentialsOptions options(certificate_provider); |
| 118 |
options.watch_root_certs(); |
| 119 |
options.set_root_cert_name(kRootCertName); |
| 120 |
options.watch_identity_key_cert_pairs(); |
| 121 |
options.set_identity_cert_name(kIdentityCertName); |
| 122 |
options.set_cert_request_type( |
| 123 |
GRPC_SSL_REQUEST_AND_REQUIRE_CLIENT_CERTIFICATE_AND_VERIFY); |
| 124 |
options.set_crl_directory(CRL_DIR_PATH); |
| 125 |
auto server_credentials = grpc::experimental::TlsServerCredentials(options); |
| 126 |
GPR_ASSERT(server_credentials.get() != nullptr); |
| 127 |
} |
| 128 |
|
| 129 |
// ServerCredentials should always have identity credential presented. |
| 130 |
// Otherwise gRPC stack will fail. |
| 131 |
TEST( |
| 132 |
CredentialsTest, |
| 133 |
TlsServerCredentialsWithFileWatcherCertificateProviderLoadingIdentityOnly) { |
| 134 |
auto certificate_provider = std::make_shared<FileWatcherCertificateProvider>( |
| 135 |
SERVER_KEY_PATH, SERVER_CERT_PATH, 1); |
| 136 |
grpc::experimental::TlsServerCredentialsOptions options(certificate_provider); |
| 137 |
options.watch_identity_key_cert_pairs(); |
| 138 |
options.set_identity_cert_name(kIdentityCertName); |
| 139 |
options.set_cert_request_type( |
| 140 |
GRPC_SSL_REQUEST_AND_REQUIRE_CLIENT_CERTIFICATE_AND_VERIFY); |
| 141 |
auto server_credentials = grpc::experimental::TlsServerCredentials(options); |
| 142 |
GPR_ASSERT(server_credentials.get() != nullptr); |
| 143 |
} |
| 144 |
|
| 145 |
TEST(CredentialsTest, TlsServerCredentialsWithSyncExternalVerifier) { |
| 146 |
auto verifier = |
| 147 |
ExternalCertificateVerifier::Create<SyncCertificateVerifier>(true); |
| 148 |
auto certificate_provider = std::make_shared<FileWatcherCertificateProvider>( |
| 149 |
SERVER_KEY_PATH, SERVER_CERT_PATH, CA_CERT_PATH, 1); |
| 150 |
grpc::experimental::TlsServerCredentialsOptions options(certificate_provider); |
| 151 |
options.watch_root_certs(); |
| 152 |
options.set_root_cert_name(kRootCertName); |
| 153 |
options.watch_identity_key_cert_pairs(); |
| 154 |
options.set_identity_cert_name(kIdentityCertName); |
| 155 |
options.set_cert_request_type( |
| 156 |
GRPC_SSL_REQUEST_AND_REQUIRE_CLIENT_CERTIFICATE_AND_VERIFY); |
| 157 |
options.set_certificate_verifier(verifier); |
| 158 |
auto server_credentials = grpc::experimental::TlsServerCredentials(options); |
| 159 |
GPR_ASSERT(server_credentials.get() != nullptr); |
| 160 |
} |
| 161 |
|
| 162 |
TEST(CredentialsTest, TlsServerCredentialsWithAsyncExternalVerifier) { |
| 163 |
auto verifier = |
| 164 |
ExternalCertificateVerifier::Create<AsyncCertificateVerifier>(true); |
| 165 |
auto certificate_provider = std::make_shared<FileWatcherCertificateProvider>( |
| 166 |
SERVER_KEY_PATH, SERVER_CERT_PATH, CA_CERT_PATH, 1); |
| 167 |
grpc::experimental::TlsServerCredentialsOptions options(certificate_provider); |
| 168 |
options.watch_root_certs(); |
| 169 |
options.set_root_cert_name(kRootCertName); |
| 170 |
options.watch_identity_key_cert_pairs(); |
| 171 |
options.set_identity_cert_name(kIdentityCertName); |
| 172 |
options.set_cert_request_type( |
| 173 |
GRPC_SSL_REQUEST_AND_REQUIRE_CLIENT_CERTIFICATE_AND_VERIFY); |
| 174 |
options.set_certificate_verifier(verifier); |
| 175 |
auto server_credentials = grpc::experimental::TlsServerCredentials(options); |
| 176 |
GPR_ASSERT(server_credentials.get() != nullptr); |
| 177 |
} |
| 178 |
|
| 179 |
} // namespace |
| 180 |
} // namespace testing |
| 181 |
} // namespace grpc |
| 182 |
|
| 183 |
int main(int argc, char** argv) { |
| 184 |
::testing::InitGoogleTest(&argc, argv); |
| 185 |
grpc::testing::TestEnvironment env(&argc, argv); |
| 186 |
int ret = RUN_ALL_TESTS(); |
| 187 |
return ret; |
| 188 |
} |