What is the certificate chain of trust for HTTPS?Can't you publish it yourself?

hebiwen95 2022-08-06 18:33:59 阅读数:601


Internet application of network communication is commonly by HTTP,但 HTTP 是明文传输的,Easy to leak information,So most applications will upgrade for HTTPS.

HTTP 底层是用 TCP 传输的,HTTPS 就是在 TCP 和 HTTP Add a layer of encryption and authentication protocol between,这一层叫做 SSL/TLS.


Because the earliest time is SSL 协议,But later discovered the hole,就改为 TLS 协议了,而且 TLS Agreement has been update,从 1.1、1.2 到了现在的 TLS 1.3.

不管叫 SSL 还是叫 TLS,This layer is refers to the.

This layer implements encryption、身份认证,And the function of tamper proof.

Encryption is easy to understand,Is through an encryption algorithm for processing the content,生成密文,Then the other end through the decryption algorithm to cipher processing as original content.

But only the encryption algorithm is not enough,Have to add some randomness,Let each time different,So there will be a key there,Every first randomly generated keys,Then communication process using the decryption keys with encryption algorithms to add.

This key is randomly generated,So can only be generated at one end later told the other end.

那么问题来了,How to tell the other end the key safe?

It takes a special kind of encryption algorithm --- 非对称加密了.

This kind of special encryption algorithm in two key,Data that is encrypted with one key can only be decrypted the other,So expose a key to go out,A key,Such data that is encrypted with the left key,The other people all can decrypt,But with exposed out key encrypted data,Only you can decrypt.

The exposed out, the key is known as public key,Left key is called the private key.

So when someone wants to give you some message,He went out through your exposure public key to encrypt information,Others can decrypt,Only you can decrypt the.This will make sure the safety of information transfer.

Key problems such as mentioned above,Can this asymmetric encryption mechanism of public and private keys to solve the.

有的同学可能会问,Now that the asymmetric encryption mechanism based on public and private key can ensure safety,Why we have to pass the other key?

Because of the asymmetric encryption is too slow,Several message ok,Frequent encryption efficiency of data in this way too low.So generally only use this way to transfer the session key,保证安全,And then back to use transfer the session key for data encryption.

前面提到 TLS Layer is mainly implements encryption、身份认证、The function of tamper proof.

Encryption is the way with symmetric encryption,Use the key based on asymmetric encryption mechanism of public and private keys to pass.

The identity authentication to do then?

Is by public and private key mechanism,Just mentioned that the only content of public-key encryption private key to decrypt the,This ensures that the information security transfer.

那反过来,私钥加密的数据,If with the public key can unlock,It is proved that the information is passed you?Because only you have the private key.

所以,The private key encryption is called the signature again,Can be used for authentication of.

That what is encrypted with the private key?

Generally is the transmission of information for a hash,生成数据指纹,And then the data is encrypted with the private key fingerprint,Also is to sign for it.

This data transfer to the other side,The data with the public key fingerprint out,Do the content again hash,Generate a fingerprint of the data,两者对比一下,如果一样,就说明没有被篡改.

这就是 TLS A third function of layer,防篡改,That is to ensure the integrity of the data.

至此,HTTPS 给 HTTP The extra encryption、身份认证、Tamper-proof function the principle we all know.

But don't know if the students have found that there is a hole,Asymmetric encryption algorithm is open,You can generate a public-private key,别人也可以,How to ensure that I get the public key is you?

One thousand I got the public key is someone else's,That I use it to encrypt the data,Don't be truncated yao?

To solve this problem involves a new concept,数字证书了:


The question now is how to verify the public key is one person.

If I have a trustworthy person,He said that the public key is the people of,我就相信.Based on the trust to verify can yao?

That is to say, I trust the people have their own private key,He used his private key to sign information,After I received information using his public key to decrypt the,Found to decrypt the information,States that this is been his signature,I believe I received the public key is reliable.

这样是可以的,But how to ensure that I received the trust of the public key is really?

It's an infinite loop up.

In reality won't such an infinite loop,The solution is the operating system built a batch of trust institutions public key,Through these institutions signature,To be in each other's public key.

This kind of trust institutions is called CA(Certification Authority),电子认证机构,经过 CA Certified public key and related information,Is known as a digital certificate.

Operating system built in all credible CA 的证书,也就是 CA The public key and related information,叫做根证书.

在 mac Can see in the key string system built-in all root certificates:

当你打开一个 https 的网站的时候,Will download site certificate,And see if it is through these systems of built-in CA Root certificate trust,如果是,On behalf of the site received the public key is credible.

Certificates are awarded to a domain name,Is the public key in the certificate of proof is the domain name.

比如打开 https://baidu.com ,View its certificate:

You will see the level 3 certificate,Root is the built-in system CA 的证书,It trust in the middle of a certificate,Then the intermediate certificate trust baidu.com 的证书.(Trust is refers to do it my own private key signature)

这个 CAThe root of the certificate is built into the system,受信任的,There was also trust for trust in the middle of the certificate,To trust the certificate trust among baidu.com 的证书,This is a chain of trust.

你打开 taobao.com Will see it is level 3 certificate chain:

Why are level 3?

Because this one thousand intermediate certificates cannot be trusted,Also can let the root certificate again to find a intermediate certificate,因为信任根证书,Also natural trust this new intermediate certificate,But if the root certificate directly to trust a website,One thousand root certificates have been breached can't trust,That can't find can trust.

所以,Level 3 certificate would be safer.

And so it was a chain of trust:

That is to say, want to upgrade your site HTTPS,得找 CA To apply for a certificate to just go.

Average cloud platform provides a service agent to apply for,But the cost of a year is quite expensive,比如阿里云:

If I just want to test the HTTPS,Take such a big cost to CA To apply for a certificate?

那倒不用,我们可以自己创建一个 CA 根证书,Then use it to their certificate,This is called a self-signed certificate:


当测试的时候,可以用 openssl The library yourself to create a CA 根证书.

第一步用 genrsa Command to generate the private key:

openssl genrsa -out ca-key.pem -des 1024

In the process of to input password,This is to protect the private key to use.

然后用 req Command to create certificate signing request,Enter the domain name and related information:

openssl req -new -key ca-key.pem -out ca-csr.pem

The process to enter some information,Domain name is the most important information,Because the certificate is to prove that the public key is the domain name:

然后用 x509 Command to generate the root certificate:

openssl x509 -req -in ca-csr.pem -signkey ca-key.pem -out ca-cert.pem

至此,Root certificate to create finished,产生了 ca-key.pem、ca-csr.pem、ca-cert.pem 三个文件,Are the private key、证书签名请求、根证书.

Then use the root certificate of creating web sites.

The same three steps:

用 genrsa 生成私钥:

openssl genrsa -out server-key.pem 1024

然后用 req Command to create certificate signing request.

openssl req -new -key server-key.pem -out server-csr.pem

The most important and domain information:

The final step to generate the certificate,But front to specify the generated here QA 的根证书:

openssl x509 -req -CA ca-cert.pem -CAkey ca-key.pem -CAcreateserial -in server-csr.pem -out server-cert.pem -extensions v3_req

这样就产生了 server-key.pem、server-csr.pem、server-cert.pem 三个文件,Respectively is a web site to the private key、证书签名请求、证书.

With the private key and certificate can create https 服务了,我们使用 nodejs 来创建:

const https = require('https');
const fs = require('fs');
let options = {
  key: fs.readFileSync('./server-key.pem'),
  cert: fs.readFileSync('./server-cert.pem')
    res.write('hello https');

然后浏览器访问,就可以看到 https 服务返回的内容:

chrome Will be marked as unsafe,It opens at we look at the certificate:

Can see only level 1 www.ggg.com 的证书,Has not been trust.

This is because the roots of his certificate issued no import key string,我们导入一下:

导入 ca-cert.pem,Can be found in the keys guangguangguang.com 的根证书,Has mark is self-signed certificates:

再访问网站,You will see the structure of the secondary:

But has not been trust,We trust the self-signed root certificate:

Then take a look at the website,Can see the certificate as a trusted,Because of his root certificate trusted:

But the website will still be marked as unsafe,这是 chrome 的策略,不支持自签名证书.

Normally we would to CA Agency to apply for a certificate of,But tests can create a CA 根证书,Then give yourself site certificate.


HTTPS 就是在 TCP 和 HTTP 之间加了一个 SSL 或者叫 TLS 层,实现了加密、身份认证、The function of tamper proof.

为了增加随机性,Every time to generate keys for encryption,Transfer the key to use asymmetric encryption private key mechanism.

Only the content of the public key encryption private key can unlock,防止被窃取.

私钥只有一个人有,So encryption can be used as the content of the identity authentication,也就是签名.

Do the content hash,And then the private key signature,Can do the integrity check,防止被篡改.

But how to ensure to get the public key must be each other's,这是个复杂的问题.

The plan now is a system built in some CA 的根证书,然后这些 CA Certificate presented some website,If access to the website to get the certificate is the CA 机构颁发的,That is a trusted.

But in reality are generally level 3 certificate trust chain,增加安全性.

向 CA To apply for the certificate can use ali cloud cloud computing providers such as the proxy service,But are quite expensive,如果测试的话,可以用 openssl 自己创建一个 CA 根证书,Signature of yourself,This is called a self-signed certificate.

Understand this certificate trust chain,也就理解了 HTTPS 的核心.

copyright:author[hebiwen95],Please bring the original link to reprint, thank you. https://en.javamana.com/2022/218/202208061814560605.html