Step-by-Step Plan:
- Use OpenSSL. Install and link with the OpenSSL libraries.
- Initialize the OpenSSL library and create an SSL_CTX for the server (for example, using TLS_server_method()).
- Load your certificate and private key files.
- When accepting a new connection, wrap the socket with an SSL object and perform an SSL_accept before reading/writing data.
- Clean up the SSL_CTX when shutting down the server.
Below are sample code modifications.
Modify server.c to include OpenSSL headers and create initialization/cleanup functions:
Then, in your connection acceptance code (likely in a function handling new client connections), wrap the accepted socket with SSL:
Finally, in your main function, call InitializeSSL before opening the server and CleanupSSL during shutdown:
This approach keeps the design simple by integrating SSL initialization, connection wrapping, and cleanup into your current server code structure. Adjust the certificate and key file paths as needed.
Generate a self-signed certificate and key using OpenSSL. In your Terminal run:
openssl req -x509 -newkey rsa:2048 -nodes -keyout key.pem -out cert.pem -days 365 \
-subj "/C=US/ST=State/L=City/O=Organization/OU=Unit/CN=localhost"
This command creates a 2048-bit RSA key and a certificate valid for 365 days. The certificate will use "localhost" as the Common Name.Optionally, combine the certificate and key into a .p12 file (if needed):
openssl pkcs12 -export -inkey key.pem -in cert.pem -out cert.p12 -name "LocalTestCert"
You'll be prompted to set an export password.
Add the certificate to your Mac System Keychain:Open Keychain Access (from /Applications/Utilities/).
Drag and drop “cert.pem” into the “System” keychain (or “login” if preferred).
Double-click the certificate, expand “Trust,” and select “Always Trust” for when using this certificate.
Close and input your password if prompted.
Restart Brave Browser. Brave uses the system's trust store, so it should now trust your self-signed certificate when accessing your test server over HTTPS on "localhost".
- OpenSSL Command: Use the following OpenSSL command in the Terminal to generate a self-signed certificate:
- Explanation:
openssl req: Invokes the OpenSSL request tool.-x509: Specifies that this is an X.509 certificate.-newkey rsa:2048: Generates a new 2048-bit RSA key.-keyout localhost.key: Specifies the output file for the private key.-out localhost.pem: Specifies the output file for the self-signed certificate.-subj "/CN=localhost": Sets the Subject (Common Name) to "localhost". You can change this to your actual domain name if needed.-days 365: Sets the certificate validity to 365 days.
- Key File: The private key (
localhost.key) is crucial and should be kept secure.
- Launch the Keychain Access application (Applications > Utilities > Keychain Access).
- Choose "System" from the Keychains list on the left side.
- Choose "File" > "Import Items...".
- Navigate to the
localhost.pemfile and select it. - Click "Open".
- You'll be prompted to add the certificate to the System keychain. Confirm and choose "Add".
- In Keychain Access, navigate to the "Certificates" category and find the
localhostcertificate (or the name you assigned it). - Double-click the certificate to open its details.
- Expand the "Trust" section and change the "When using this certificate" option to "Always Trust".
- Close the certificate details window. You may be prompted for your password to save the changes.
- Restart Browser: Close and reopen your web browser (Chrome, Safari, Firefox, etc.).
- Access your Localhost: Navigate to your localhost server (e.g.,
https://localhost). - Verify Certificate: The browser should now trust the self-signed certificate and display a secure padlock icon in the address bar.
No comments:
Post a Comment