HTTPS bidirectional authentication Basic Configuration Learning

Source: Internet
Author: User
Tags base64 begin rsa private key openssl openssl library openssl rsa openssl x509 nginx server asymmetric encryption
HTTPS bidirectional authentication basic Configuration Learning a certificate key format 1.1 DER Format

The public key, the certificate file Information format, usually the suffix name is DER,CER,CRT, and the content is binary data. 1.2 PEM format

Private key, certificate, public key information format, usually suffix name is pem,cer,crt, content is binary data base64 encoded

The screenshot above is the content of the private key file in the traditional PEM format generated using the OpenSSL tool. (Note that the beginning end has the RSA letter, which distinguishes the PKCS8 format private key), in the image above

The OpenSSL command can convert PEM format to der Format

For example, PEM key conversion to DER format key

OpenSSL rsa-in key.pem–outform Der–outkey.der

PEM format certificate to der Format certificate conversion

OpenSSL x509-in Ca-cert.pem-outca-cert.der-outform der

The difference between PEM and Der formats is that PEM is the base64 encoding of the DER format content plus the end-to-end two lines (BEGIN ...). and end ...)

1.3 PKCS Format

The PKCS standard has now released 15 types of

Commonly used in several of the following: 1.3.1 pkcs#8 private key file Information format

Using OpenSSL to generate a traditional format (PEM) private key

Openss Genrsa–out PRIVATEKEY.PEM 2048

Convert to PKCS8 format (the traditional format private key in Figure 1.2 is converted to the PKCS8 format)

OpenSSL pkcs8–topk8–in privatekey.pem–out Privatekeypkcs8.pem–nocrypt

Note the private key methods that distinguish the traditional PEM and PKCS8 formats are:

1 The head of the traditional PEM format is begin RSA PRIVATE KEY

2 pkcs#8 Format private key file header is begin private key

Note that the PHP language can be used directly for both forms of the private key.

Note that the PKCS8 format conversion is PRIVATEKEYPKCS8.PEM again, with the contents unchanged (Diff–u comparison).

Note that only the private key exists in both formats (PKCS8 and legacy formats)

Pkcs#8 is a standard developed by RSA, which is more generic than the traditional PEM private key format.

In addition, the private key in the PKCS#8 format can be encrypted again (using a symmetric encryption algorithm), which is more secure. 1.3.2 PKCS#10 Certificate request file Information Format

The file includes a signature algorithm, public key information, requester information, and the use of the OpenSSL req command to generate a certificate request file, usually with a suffix called CSR

For example: OpenSSL req–new–key su-key.pem–outsu-req.csr–days 3650

Generate a PKCS#10-format certificate request file using a private key Su-key.pem

1.3.3 pkcs#12 User Personal certificate Information Format

The file includes the private key public key and the certificate, usually the suffix is pfx,p12, the Import browser personal certificate format (using OPENSSLPKCS12 can export the client personal certificate)

For example: OpenSSL pkcs12–export–clcerts–in qiyi-cert.pem–inkey qiyi-key.pem–out qiyi.p12

-clcerts indicates that only client certificates are exported

QIYI-CERT.PEM Client-Signed PEM format certificate (contains information such as public key and other domain names)

QIYI-KEY.PEM client PEM format private key file

QIYI.P12 is an exported personal certificate that contains information such as a certificate, private key, etc. (the suffix can also be PFX)

Other PKCS format standards have not been thoroughly understood, and the three types described here are used when configuring HTTPS bidirectional authentication (PKCS10,PKCS#12) and asymmetric Encryption decryption (PKCS8). 1.4 Summary

In this app fund supermarket, the server language is PHP, the server is Nginx. Both the server side and Android and iOS use the private key in the pkcs#8 format, and the public key is in PEM format, where the private key used on the server side is swapped for other formats, such as the traditional format PEM, which can be decrypted, and Android cannot use the traditional format private key, which must be pkcs#8, iOS must also be in pkcs#8 format.

two nginx HTTPS two-way authentication configuration 2.1 HTTPS bidirectional authentication principle

Bidirectional authentication The main work is to distribute symmetric encryption key information.

The specific distribution process is as follows:

1 browser sends connection request to HTTPS server

2 The server sends its own certificate information to the client

3 client verifies the legality of the server certificate

Verify that the server certificate is issued by a trusted root certification authority (CA) (primarily a validation signature, the CA certificate public key decrypts the signature result consistent with the signature generated by the signature algorithm and the server certificate information)

Verify that the certificate's period is expired

Verify that the issuing authority for the certificate is in the Revocation Authority list (this process is not studied in detail)

Verify that the domain name information in the certificate is consistent with the domain name accessed

If one of the above four conditions is not satisfied, the user is prompted for the server is not trusted and the user chooses whether to continue accessing

4 The client sends a personal certificate and a random number that produces a symmetric encryption key (encrypted with the public key from the server certificate) to the server, the server verifies the client's legitimacy (whether it is issued by a configured trust authority, whether it expires), and, if it is legitimate, obtains the client public key from the certificate

5 The Client Notification server will use the random number sent in step 4 to form a symmetric key to encrypt the next data for this connection and the end of client-to-server authentication

6 The server notifies the client to form a symmetric key using the random number accepted in step 4 to encrypt the next communication data for this connection and the end of the server-to-client authentication

2.2 Example of two-way authentication configuration based on Nginx (self as Certification authority)

Nginx to open the HTTPS service needs to install the OpenSSL library, this content is no longer described here.

The following example can try it on its own (the Nginx has tried multiple times without a problem, try again OK in Apache) 2.2.1 Generate a self-signed certification authority CA

L Generate CA private key (PEM format)

OpenSSL genrsa-out Ca-key.pem 2048

L Generate certificate Request file (pkcs#10 format)

OpenSSL req–new–key ca-key.pem–out ca-req.csr–days 3650

(-key specifies the private key file –new Specifies that a new certificate is generated –days the specified validity period)

L Signing CA Certificate request

OpenSSL x509–req–in ca-req.csr–out Ca-cert.pem–signkey ca-key.pem–days3650


2.2.2 Generating server-side certificates

A certificate is usually a file that includes a public key and some other information.

L Generate server-side private key file

OpenSSL genrsa–out Serverkey.pem 2048

L Generate server-side certificate request file

OpenSSL req–new–key serverkey.pem–out server.csr–days 3650 (will be prompted to enter some personal information, in which the attention is the domain name input, be sure to enter the correct domain name, Otherwise, cause the client to verify that the server certificate legality always prompt is not legal)

L Generate server-side certificates

OpenSSL x509–req–in server.csr–out server.pem–days 3650–caca-cert.pem–cakey–ca-key.pem–cacreateserial

(-cakey specifies the CA authority's private key –CA the CA authority's certificate –cacreateserial create the CA serial number)

2.2.3 Generating a client (browser) certificate

The browser client certificate has more important information than the server-side certificate for the private key.

Generate browser client certificate (including client private key certificate pfx/pkcs#12 format)

L Generate client private key (PEM)

OpenSSL genrsa–out clientkey.pem-2048

L Generate client certificate request (PKCS#10)

OpenSSL req–new–key clientkey.pem–out CLIENT-REQ.CSR days-3650 (users are prompted to enter personal information)

L Generate client certificate (PEM)

OpenSSL x509–req–in client-req.csr–out client-cert.pem–days 3650–caca-cert.pem–cakey ca-key.pem–cacreateserial

L Convert client certificate to pkcs#12 format (browser required format, including private key, public key certificate information)

OpenSSL pkcs12–export–in client-cert.pem–in clientkey.pem–out client.pfx

Sample client browser Import Certificate:

Ie-> tools->internet Options, Content--certificate--Personal

Import PFX format personal certificate client.pfx

Trusted Root certificate--certificate--ie-> Tools->internet Options

Import Ca-cert.pem

Nginx Server-side configuration (HTTP server module)

Ssl_certificate SERVER.PEM//Specify server certificate Server.pem

Ssl_certificate_key SERVERKEY.PEM//server private key Serverkey.pem

Ssl_client_certificate CA-CERT.PEM//CA Agency certificate Verifying the legality of the client certificate

Ssl_verify_client on//Enable Authentication Client certificate function

The actual authentication process, the client sends the request to the HTTPS server to specify the port, the server sends the certificate SERVER.PEM to the client browser, the browser exploits the import into the trusted root certificate

Group of Experiments:

1: Configure Nginx Server Two-way authentication, the browser does not import the trusted root certificate and personal certificate, first prompt the server is not trusted, whether to continue access, the user click Continue, after the prompt did not send the required certificate

2 server-side Delete ssl_verify_client on, 1 conditions can continue to access

3 Restore Ssl_verify_client on, as shown in Figure 2.1, import the client browser two certificates (CAS and individuals) to access them normally.

three appendices 3.1 OpenSSL common commands summary

Opensslgenrsa Generating the private key


OpenSSL RSA generates public key

OpenSSL rsa–in privatekey.pem–out–pubout–out Publickey.pem

OpenSSL rsa–in Privatekey.pem–out–pubout–outpublickey.der–outform der (Generate Der Format public key, note only public keys and certificates have der Format)

Asymmetric encryption algorithm and symmetric encryption algorithm main difference between non-symmetric encryption decryption key different (RSA), symmetric encryption decryption its key is the same, mainly Des, triple des

Convert private key to PKCS#8 format

OpenSSL pkcs8–topk8–in privatekey.pem–out Privatekeypkcs8.pem–nocrypt

(The conversion private key is PKCS8 format,-nocrypt means no encryption, otherwise the private key information will be encrypted once the symmetric algorithm)

OpenSSL req Command (

-new is used to generate a new certificate request if the-key is not specified as a private key

-key specifying the private key file

-out output file (default PEM format)

OpenSSL x509 Command (

Signing certificate request file, form certificate file and convert certificate format, display certificate contents

-REQ specifies that the input file is a certificate request file

-in Specifying input files

-out Output Certificate file

-cakey CA Agency private key file

-CA CA certificate file

-days Validity

-SIGNKEY Specifies the private key file to use when generating the self-signed CA certificate (note the cakey difference)

3.2 Related URLs

Common certificate Format conversions

OpenSSL Command Learning URLs

PKCS8 format differs from PEM format

Nginx HTTPS bidirectional authentication configuration



OpenSSL RSA Command

OpenSSL command classification

Http:// 2bcw-3nlavgbxhsxrx-ogmxe5lxvctundh9gn-0uybut37togpv8l6qt1hfimd-dyi

SSL bidirectional authentication principle


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.