Welcome back!
In the previous chapter, Service Contracts (Protobuf), we defined the "Menu" so our services speak the same language.
Now, we need to make sure that when Service A orders from that menu, no one else is listening in. We need to secure the connection.
Imagine you want to send a letter with top-secret instructions.
In computer programming, setting up that "locked briefcase" (TLS security) is usually very complicated. You have to configure ciphers, load certificates, check validities, and handle OpenSSL errors.
The Solution: We create a TLS Credentials Factory.
Instead of writing 50 lines of security code every time we start a server, we build a "Factory" (a helper function). We simply give the Factory our ID badge (Certificate), and it hands us back a fully secured, locked briefcase ready for travel.
Before the factory can work, it needs raw materials. In TLS (Transport Layer Security), these materials are files on your hard drive:
We group these file paths into a simple structure called CertConfig.
(Note: We will learn how to load these paths automatically in Certificate Loader, but for now, just know they exist.)
Our factory makes two types of products:
This setup creates mTLS (Mutual TLS), where both sides must prove who they are.
Let's look at how a developer uses this factory. The interface is defined in common/include/common/tls_config.h.
We want to start Service B securely. Instead of dealing with OpenSSL directly, we just call our factory.
namespace pqc_common {
// The Factory Function for Servers
std::shared_ptr<grpc::ServerCredentials> CreateServerCredentials(
const CertConfig& config,
bool pqc_enabled = true
);
}
Explanation:
config (Where are my keys?) and pqc_enabled (Should we use post-quantum crypto?).std::shared_ptr) to gRPC credentials.What happens inside the factory when we ask for credentials?
Let's peek inside common/src/tls_config.cpp to see how we implement this. We use a helper function LoadFileContents (which we will build in Certificate Loader) to read the text files.
// Inside CreateServerCredentials...
grpc::SslServerCredentialsOptions::PemKeyCertPair pair;
// 1. Load the secret key
pair.private_key = LoadFileContents(config.private_key_path);
// 2. Load the public certificate
pair.cert_chain = LoadFileContents(config.cert_chain_path);
Explanation:
PemKeyCertPair. Think of this as a folder holding your ID and your signature.// We force the server to CHECK the client's ID
grpc::SslServerCredentialsOptions opts(
GRPC_SSL_REQUEST_AND_REQUIRE_CLIENT_CERTIFICATE_AND_VERIFY
);
opts.pem_root_certs = LoadFileContents(config.ca_cert_path);
opts.pem_key_cert_pairs.push_back(std::move(pair));
Explanation:
GRPC_SSL_REQUEST...: This is the most strict setting. It means "If the client doesn't show a valid ID, hang up immediately."pem_root_certs: This is the "Master List" of valid IDs the server trusts.// Create the final object
return grpc::SslServerCredentials(opts);
// Note: (void)pqc_enabled is used to silence compiler warnings
// because OpenSSL 3.5 handles PQC defaults automatically!
Explanation:
SslServerCredentials and return it.
The client (Channel) side is very similar. It bundles the keys into a slightly different object called SslCredentialsOptions.
std::shared_ptr<grpc::ChannelCredentials> CreateChannelCredentials(...) {
grpc::SslCredentialsOptions opts;
// Load the Trusted Root so we know we are talking to the real Server
opts.pem_root_certs = LoadFileContents(config.ca_cert_path);
// Load our own ID to show the server
opts.pem_private_key = LoadFileContents(config.private_key_path);
opts.pem_cert_chain = LoadFileContents(config.cert_chain_path);
return grpc::SslCredentials(opts);
}
We have successfully isolated our security logic!
CreateServerCredentials.This factory ensures that every service in our PQC project uses the exact same high-security standards (TLS 1.3 with PQC) without the developer having to remember the details.
But waitβwhere does that LoadFileContents function come from? How do we actually read those certificates from the disk safely?
In the next chapter, we will build the utility that handles the file system.
Next Chapter: Certificate Loader
Generated by Code IQ