How to build your own CA system
How to build your own CA (Certificate Authority).
CA:
- Key cakey.pem
- Certificate cacert.pem
Device:
- Key some_serverkey.pem
- Certificate Signing Request some_server.csr is signed by CA to become its Certificate
Overview
A CA is an entity that signs digital certificates. An example of a well-known CA is Verisign. Many websites on the Internet use certificates for their HTTPS connections that were signed by Verisign.Instead of paying companies like Verisign for all your digital certificates. It can be useful to build your own CA for some of your applications.
Step 1: Server Configuration
Before we configure OpenSSL, you should configure the hostname/FQDN (fully qualified domain name,完整網域名稱) and make sure that our time, date and timezone is correct.
You change /etc/hosts then verify it with
hostname -f
You can synchronize the time/date with this command:
sudo ntpdate pool.ntp.org
To synchronize periodically, install the NTP tools:
sudo apt-get install ntp
Check the NTP server pools by default:
cat /etc/ntp.conf | grep server
You can verify which servers it is currently using with the following command:
ntpq -p
If "ntpq: read: Connection refused" happens, run:
/etc/rc.d/init.d/ntpd restart
Step 2: OpenSSL Configuration
OpenSSL uses a configuration file that we will change in it:
/usr/lib/ssl/openssl.cnf
Change the following section:
[ CA_default ]
# The “/root/ca” folder is where we will store our private keys and certificates.
dir = /root/ca
[ policy_match ]
countryName = match
stateOrProvinceName = optional
organizationName = optional
organizationalUnitName = optional
commonName = supplied
emailAddress = optional
Root CA
Create the key and certificate for our CA.
mkdir /root/ca
chmod -R 600 /root/ca # Protecting your CA is important.
cd /root/ca
mkdir newcerts certs crl private requests
touch index.txt # where OpenSSL keeps track of all signed certificates
echo '01' > serial # Each signed certificate will have a serial number. I will start with 1
- Generate the root private key
openssl genrsa -aes256 -out private/cakey.pem 4096
openssl req -new -x509 -key /root/ca/private/cakey.pem -out cacert.pem -days 3650 -set_serial 0
Create a certificate for a device
If you want to install a certificate on a device (a web server for example), there are to ways to do it:- The device needs to generate a CSR (Certificate Signing Request). This CSR is created by using the private key of the device.
On our CA, we can then sign the device's CSR and create a digital certificate for the device. - We can do everything on our CA. We can generate a private key, CSR and then sign the certificate…everything “on behalf” of the device.
cd /root/ca/requests/ # use the requests folder
openssl genrsa -aes256 -out some_serverkey.pem 2048 # generate a private key
openssl req -new -key some_serverkey.pem -out some_server.csr # create a CSR
openssl ca -in some_server.csr -out some_server.pem # sign the CSR that we just created
rm some_server.csr # delete the CSR
mv some_serverkey.pem /root/ca/private/ # move the private key to the “private” folder
mv some_server.pem /root/ca/certs/ # move the new certificate to the “certs” folder:
Verification
cat /root/ca/index.txt
cat /root/ca/serial
openssl x509 -in cacert.pem -text # display the contents of a PEM formatted certificate
Install the certificate on your device
You need to install the root certificate and the server's certificate on the device you want to connect to the server.
- MS Windows Windows doesn’t recognize the .PEM file extension so you might want to rename your certificates to .CRT. Open the root certificate then hit the Install Certificate button and you will see this wizard. Make sure you select the Trusted Root Certification Authorities store and click Next and Finish. After the root certificate is installed and trusted, open the certificate that we assigned to the server.
How to create a self-signed SSL Certificate... which can be used for testing purposes or internal usage
http://www.akadia.com/services/ssh_test_certificate.html
Overview
The following is an extremely simplified view of how SSL is implemented and what part the certificate plays in the entire process.
Normal web traffic is sent unencrypted over the Internet. That is, anyone with access to the right tools can snoop all of that traffic. Obviously, this can lead to problems, especially where security and privacy is necessary, such as in credit card data and bank transactions. The Secure Socket Layer is used to encrypt the data stream between the web server and the web client (the browser).
SSL makes use of what is known as asymmetric cryptography, commonly referred to as public key cryptography (PKI). With public key cryptography, two keys are created, one public, one private. Anything encrypted with either key can only be decrypted with its corresponding key. Thus if a message or data stream were encrypted with the server's private key, it can be decrypted only using its corresponding public key, ensuring that the data only could have come from the server.
If SSL utilizes public key cryptography to encrypt the data stream traveling over the Internet, why is a certificate necessary? The technical answer to that question is that a certificate is not really necessary -the data is secure and cannot easily be decrypted by a third party. However, certificates do serve a crucial role in the communication process. The certificate, signed by a trusted Certificate Authority (CA), ensures that the certificate holder is really who he claims to be. Without a trusted signed certificate, your data may be encrypted, however, the party you are communicating with may not be whom you think. Without certificates, impersonation attacks would be much more common.
Step 1: Generate a Private Key
The openssl toolkit is used to generate an RSA Private Key and CSR (Certificate Signing Request). It can also be used to generate self-signed certificates which can be used for testing purposes or internal usage.
The first step is to create your RSA Private Key. This key is a 1024 bit RSA key which is encrypted using Triple-DES and stored in a PEM format so that it is readable as ASCII text.
openssl genrsa -des3 -out server.key 1024
Step 2: Generate a CSR (Certificate Signing Request)
Once the private key is generated a Certificate Signing Request(CSR) can be generated.
A CSR is an encoded file that provides you with a standardized way to send CA your public key as well as some information that identifies your company and domain name.
When you generate a CSR, most server software asks for the following information: common name (e.g., www.example.com), organization name and location (country, state/province, city/town), key type (typically RSA), and key size (2048-bit minimum).
During the generation of the CSR, you will be prompted for several pieces of information. These are the X.509 attributes of the certificate. One of the prompts will be for "Common Name (e.g., YOUR name)". It is important that this field be filled in with the fully qualified domain name of the server to be protected by SSL. If the website to be protected will be https://public.akadia.com, then enter public.akadia.com at this prompt. The command to generate the CSR is as follows:
openssl req -new -key server.key -out server.csr
What contained in a CSR:
- Common Name
- Organization
- Organizational Unit
- City/Locality
- State/County/Region
- Country
- Email address
- Public Key The public key that will go into the certificate. The public key is created automatically
Step 3: Remove Passphrase from Key
One unfortunate side-effect of the pass-phrased private key is that Apache will ask for the pass-phrase each time the web server is started. Obviously this is not necessarily convenient as someone will not always be around to type in the pass-phrase, such as after a reboot or crash. mod_ssl includes the ability to use an external program in place of the built-in pass-phrase dialog, however, this is not necessarily the most secure option either. It is possible to remove the Triple-DES encryption from the key, thereby no longer needing to type in a pass-phrase. If the private key is no longer encrypted, it is critical that this file only be readable by the root user! If your system is ever compromised and a third party obtains your unencrypted private key, the corresponding certificate will need to be revoked. With that being said, use the following command to remove the pass-phrase from the key:
cp server.key server.key.org
openssl rsa -in server.key.org -out server.key
The newly created server.key file has no more passphrase in it.
-rw-r--r-- 1 root root 745 Jun 29 12:19 server.csr
-rw-r--r-- 1 root root 891 Jun 29 13:22 server.key
-rw-r--r-- 1 root root 963 Jun 29 13:22 server.key.org
Step 4: Generating a Self-Signed Certificate
At this point you will need to generate a self-signed certificate because you either don't plan on having your certificate signed by a CA, or you wish to test your new SSL implementation while the CA is signing your certificate. This temporary certificate will generate an error in the client browser to the effect that the signing certificate authority is unknown and not trusted.
To generate a temporary certificate which is good for 365 days, issue the following command:
openssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crt
Step 5: Installing the Private Key and Certificate
When Apache with mod_ssl is installed, it creates several directories in the Apache config directory. The location of this directory will differ depending on how Apache was compiled.
cp server.crt /usr/local/apache/conf/ssl.crt
cp server.key /usr/local/apache/conf/ssl.key
Step 6: Configuring SSL Enabled Virtual Hosts
SSLEngine on
SSLCertificateFile /usr/local/apache/conf/ssl.crt/server.crt
SSLCertificateKeyFile /usr/local/apache/conf/ssl.key/server.key
SetEnvIf User-Agent ".*MSIE.*" nokeepalive ssl-unclean-shutdown
CustomLog logs/ssl_request_log \
"%t %h %{SSL_PROTOCOL}x %{SSL_CIPHER}x \"%r\" %b"
Step 7: Restart Apache and Test
/etc/init.d/httpd stop
/etc/init.d/httpd stop
https://public.akadia.com
Install Self-Signed Certificates on Mobile Device
iOS
Install Self-Signed Certificates as an iOS Configuration Profile
You can add an SSL certificate to the trusted list in iOS:
- Emailing the certificate file to yourself as an attachment.
- Open the certificate on the mobile device then select Install to add the certificate.
當您安裝透過電子郵件傳送給您或從網站下載的描述檔時,必須手動開啟 SSL 的信任。
若您想開啟該憑證的 SSL 信任,請前往「設定」>「一般」>「關於本機」>「憑證信任設定」。在「啟用根憑證的完整信任」下,開啟該憑證的信任。
留言