Introduction
Using a command line window: cd C:OpenSSL-Win32bin (or path to where you installed SSL) if you are a linux user then open terminal with sudo su and start from step 2. Openssl genrsa -out iosdistribution.key 2048; openssl req -new -key iosdistribution.key -out CertificateSigningRequest.certSigningRequest -subj /emailAddress=YOUEMAILADDRESS.
Using openssl to generate HMAC using a binary key If you want to do a quick command-line generation of a HMAC, then the openssl command is useful. The last step in the process is to sign the request using a private key. In this example we are signing the certificate request with the same key that was used to create it. That's why it earns the name 'self-signed'. # Sign the certificate signing request openssl x509 -req -days 365 -in signreq.csr -signkey privkey.pem -out certificate.pem. Before adding a new SSH key to the ssh-agent to manage your keys, you should have checked for existing SSH keys and generated a new SSH key. When adding your SSH key to the agent, use the default macOS ssh-add command, and not an application installed by macports, homebrew, or some other external source. The next most common use case of OpenSSL is to create certificate signing requests for requesting a certificate from a certificate authority that is trusted. Openssl req -new -newkey rsa:2048 -nodes -out request.csr -keyout private.key Similar to the previous command to generate a self-signed certificate, this command generates a CSR.
If you are a system and network administrator, you have the issue of having multiple web-based applications that you need to access from within your LAN, or at least, accessible by certain employees of your organization. Generally, if these web applications are using HTTPS, the certificates are self-signed which causes the browser to issue a warning message every time you try to access such web apps. It can be a bit frustrating to actually accept the wrong the certificate every time. Besides, this can be exploited by malicious insiders who might intercept HTTPS traffic and forge fake certificates.
When HTTPS web sites are public – that is, they are accessible by anyone in the world, the digital certificate must be signed by an independent third-party trusted Certificate Authority (CA), such as, VeriSign, DigiCert, GoDaddy, GlobalSign, etc. However, if your websites are only accessible within your local LAN, it might not be worth the cost to have digital certificates issued by those trusted CAs.
The best secure solution in such a case is to implement your own local Certificate Authority (CA), which will sign the certificates installed on your LAN’s web servers. In this article, I will explain how you can implement such a procedure using the infamous OpenSSL tool – which can be installed on Linux, Mac, and Windows. And it comes pre-installed on Kali Linux. The demonstration below will be on Kali Linux distribution.
How Certificate Authority Works
A CA exists for one sole purpose, which is to testify that the “public key” of a certain entity – technically called a subject – really belongs to that subject. Just like in the real-world we have ID cards issued by the government by which we trust that a person is really who they are claiming to be, a website’s digital certificate signed by a trusted CA causes the web browser to trust the authenticity of that website. The browser is assured that it is communicating with the right website not with a fake one.
Certificate Authority and Digital Certificates are part of a cryptographic scheme known as Public Key Infrastructure (PKI) which utilizes a hybrid model of asymmetric and symmetric encryption, in addition to hashing functions, to guarantee data confidentiality, data integrity, and server authenticity. We can summarize the procedure as follows:
- The server (website) generates a pair of asymmetric keys, one public and one private. The private is kept secure at the server’s side and never exposed.
- The public key is encapsulated in what is called Certificate Signing Request (CSR) and sent to the CA.
- The CA signs the public key and generates a digital certificate. The digital certificate contains the server’s public key, but this time, it is signed by the CA. The certificate is returned to the server.
- When the client (a browser) connects to the server (website), it is presented with the certificate. The client is able to verify the signature issued by the CA; and thus, the client trusts the authenticity of the server.
It is important to mention here that in order for the CA to sign a CSR, it must already have generated its own pair of public and private keys. However, the CA public key is not signed by another CA, but rather, it is self-signed. In other words, the CA issues its own certificate and signs it locally with its own private key. CA certificates are always self-signed. The question now is, how can the browser trust the self-signed certificate of a CA? The answer is that it cannot trust it unless it is manually stored in the client’s machine in a secure way. That is the reason why global CAs pay OS vendors to have their root certificates pre-installed before the OS is shipped to the customer. Every Operating System, like Windows, MacOS, Linux, etc., comes with a certificate store, or Keychain, that have the root certificates of major CAs. The browser uses that pre-installed CA’s certificate to verify the signature of the server’s certificate.
Thus, if you are now implementing your own CA, the self-signed root certificate will be installed manually on each workstation that needs to connect to one of your local web application. Then, you will issue a separate certificate – signed by your CA – for each web server. The remainder of this article will discuss these two tasks: generating CA root certificate, and generating a server’s certificate which will be signed by the CA.
Generating the CA Root Certificate
The first thing you need to do in order to be a CA is to generate a self-signed root certificate with the value CA:TRUE. The presence of this value indicates that the certificate – and the associated private key – can actually issue and sign other certificates. Servers’ certificates, on the other hand, have the value CA:FALSE in them, which indicates that they are not allowed to sign other certificates. The following steps outline how to generate that root certificate.
Step 1: Preparing the Directory Structure
OpenSSL requires a certain directory structure in order to function properly. Basically, you need to create a directory that will be the main directory of the CA; then, you will create four subdirectories and two files.
The above commands create the directory /root/ca to be our main CA directory. Then, we created four subdirectories:
certs: it will contain our created certificates, *.crt.
crl: it will contain Certificate Revocation List (CRL).
newcerts: used by OpenSSL internally.
private: it will contain any generated private keys, *.key.
We also changed the permission of the private subdirectory so that only root can access it. Finally, we created two files, index.txt and serial. Each time a new certificate is created, OpenSSL writes an entry in index.txt. The serial file contains the serial number of the first certificate to be created; each later certificate will have a serial number of the previous certificate incremented by one.
Step 2: Preparing the Configuration File
OpenSSL comes with a template configuration file. And instead of creating a new configuration file, we will simply copy the template file into our CA directory (/root/ca). We will in later steps modify the file to reflect the changes we have made. However, all we need to do now is to copy the file:
The place of the configuration file (openssl.cnf) may change from OS to OS. In Kali Linux, it is located in /etc/ssl/.
Install Openssl On Mac
Step 3: Creating the CA Certificate and Private Key
Now, it is time to generate a pair of keys (public and private). The public will be issued in a digital certificate signed by the private key, hence, self-signed.
req: is a request subcommand; it is used to create a certificate signing request or simply a self-signed certificate.
-config openssl.cnf: tells OpenSSL which configuration file it should use.
-new: simply issues a new request.
-x509: generates a self-signed certificate with the X.509 structure. Omitting this option will generate a certificate signing request (CSR) instead.
-days 1825: this indicates the validity period in days; and in this case, it is 5 years.
-extensions v3_ca: extensions are additional attributes of the digital certificate. And in this case, the certificate is designated as a CA certificate; meaning, it can be used to issue (sign and verify) other certificates.
-keyout private/ca.key: this is the first output file, and it is the private key which will be stored in the private subdirectory with the extension: .key.
-out certs/ca.crt: this is the second output file, and it is the self-signed root certificate which will be stored in the certs subdirectory with the extension: .crt.
Step 4: Adjusting the Configuration File
We now need to make few modifications to the configuration file so it points to the right CA certificate and private keys.
Open the file /root/ca/openssl.cnf in your favorite text editor, and edit the following lines as follows:
The dir parameter should point to the CA directory, while the certificate and private_key parameters should point to the CA certificate and private key, respectively, created in the previous step.
Step 5: Verifying the Root Certificate
This step is not mandatory, but rather for you to check that everything is OK. You will check the created CA root certificate and make sure the values are correct.
x509: a subcommand to manage x.509 certificates. It will be used here to print out the CA certificate.
-noout: there is no output file. The actual output will be displayed on the terminal window.
-text: the certificate will be printed in a text format.
The certificate will be printed out. It is important to understand the following parameters:
Signature Algorithm | A signature is the hash of the certificate encrypted with the CA’s private key. Most often it is sha256WithRSAEncryption, which means the certificate is hashed using SHA256 function, and the 256-bit hash is encrypted using RSA private key. |
Issuer | This is the identity of the issuing CA, which sings the certificate. When the certificate is self-signed, the Issuer is identical to the Subject. |
Subject | This is the identity of the public key’s owner. |
Validity | This is the period within which the certificate is valid. |
Public Key Algorithm | The asymmetric algorithm that produced the pair of the public and private keys. The public key is always embedded inside the certificate itself. Generally, it is rsaEncryption. |
Public-Key | This is the actual public key in HEX representation. |
X509v3 Extensions | This parameter includes few values, one of them is the “Basic Constraints,” which indicates whether this certificate is allowed to sign other child certificates. Since here we are verifying a CA’s certificate, you should see CA:TRUE attribute. |
At this point, you need copy the file ca.crt and manually install it on your LAN’s workstations. This is very crucial so that later servers’ certificates will be verified automatically by the browsers.
Generating a Server’s Certificate
After creating the CA’s root certificate, you are now ready to issue a certificate for every website in your LAN. Those servers’ certificates will be signed by the CA’s private key; and they will be installed on their corresponding web servers.
Step 1: Generating a Certificate Request
Unlike the CA’s root certificate that is self-signed, a server certificate needs to be signed by the CA; and as such, we need first to issue a Certificate Signing Request containing a newly-created public key (of the server).
req: is a request subcommand; it is used to create a certificate signing request or simply a self-signed certificate.
-config openssl.cnf: tells OpenSSL which configuration file it should use.
-new: simply issues a new request.
-nodes: the private key will not be encrypted.
-days 365: this indicates the validity period in days; and in this case, it is one year.
-keyout private/server.key: this is the first output file, and it is the RSA private key that will be stored securely on the web server.
-out server.csr: this is the CSR, and it is a temporary file; after it is signed in the next step and normal certificate is generated, it will be removed.
Openssl Create Rsa Key
Step 2: Signing the Certificate Request
It is time now to sign the CSR:
The ca subcommand is used for various CA management tasks, one of which is signing CSRs. The policy argument states how the certificate attributes should look like; when policy_anything is used, attributes like Country Name, Organization Name, Email Address, etc., are optional.
The infiles argument takes the CSR file to be processed and the output will be a certificate (server.crt) stored in the certs subdirectory.
Step 3: Verifying the Certificate
You can check the certificate and all its attributes using the following command – which is similar to the one we used when verifying the CA certificate:
Using Openssl To Create Keys For Mac Catalina
Now you need to copy the two files certs/server.crt and private/server.key to the web server. Once correctly installed, any from your organization accessing the web server over HTTPS will be presented with the server’s certificate, which will be verified by the browser using the CA certificate (pre-installed manually).