1、http、https、http2的關系
http:目前絕大多數是http1.1版本,最原始的web協議,默認80端口,基于TCP協議。
https:加密的http協議(http+SSL/TLS),默認443端口,基于TCP協議。
http2:第二代http協議,相較于HTTP1.x,大幅度的提升了web性能。在與HTTP/1.1完全語義兼容的基礎上,進一步減少了網絡延遲和傳輸的安全性,基于TCP協議。
2、HTTPS服務
2.1 自建CA
// 1.生成CA私匙,des3代表加密算法,還可以選擇aes256等
openssl genrsa -des3 -out ica.key 1024
// 2.生成CA證書請求
openssl req -new -key ica.key -out ssl.csr
// 3.生成CA根證書
openssl x509 -req -in ica.csr -extensions v3_ca -signkey ica.key -out ica.crt
2.2 server端證書
如果用戶想得到一份屬于自己的證書,他應先向 CA 提出申請。在 CA 判明申請者的網站信息后,便為他分配一個公鑰,并且 CA 將該公鑰與申請者的身份信息綁在一起,并為之簽字后,便形成證書發給申請者。自建CA生成的如下server.crt并不能讓服務器信任,實際開發中是第三方CA證書管理機構(付費的,可以理解為他們自建的CA被瀏覽器開發者添加到了可信任的證書頒發機構列表中)簽發證書給申請者,瀏覽器才信任。但是咱們可以把自建的CA證書添加到瀏覽器可信任列表中,具體見本文2.5。
// 1.生成server私匙
openssl genrsa -out server.key 1028
// 2.生成server證書請求
openssl req -new -key server.key -out server.csr
// 3.生成server證書
openssl x509 -days 365 -req -in server.csr -extensions v3_req -CAkey ica.key -CA ica.crt -CAcreateserial -out server.crt -extfile openssl.cnf
說明:
openssl.cnf文件內容
[req]
distinguished_name = req_distinguished_name
req_extensions = v3_req
[req_distinguished_name]
countryName = CN
countryName_default = CN
stateOrProvinceName = Beijing
stateOrProvinceName_default = Beijing
localityName = Beijing
localityName_default = Beijing
organizationalUnitName = HD
organizationalUnitName_default = HD
commonName = localhost
commonName_max = 64
[ v3_req ]
# Extensions to add to a certificate request
basicConstraints = CA:FALSE
keyUsage = nonRepudiation, digitalSignature, keyEncipherment
subjectAltName = @alt_names
[alt_names]
#注意這個IP.1的設置,IP地址需要和你的服務器的監聽地址一樣 DNS為server網址,可設置多個ip和dns
IP.1 = 127.0.0.1
DNS.1 = localhost
2.3 node.js搭建https服務器
//使用nodejs自帶的http、https模塊
const https = require('https');
const http = require('http');
const fs = require('fs');
const path = require('path');
const koa = require('koa');
const app = new koa();
app.on('error', (error, ctx) => {
console.log('something error ' + JSON.stringify(ctx.onerror));
});
app.use(async ctx => {
ctx.body = `This is ${ctx.protocol} visit`;
});
//根據項目的路徑導入生成的證書文件
const privateKey = fs.readFileSync(path.join(__dirname, './certificate/server.key'), 'utf8');
const certificate = fs.readFileSync(path.join(__dirname, './certificate/server.crt'), 'utf8');
const credentials = {key: privateKey, cert: certificate};
//創建http與HTTPS服務器
const httpServer = http.createServer(app.callback());
const httpsServer = https.createServer(credentials, app.callback());
//可以分別設置http、https的訪問端口號
const PORT = 8000;
const SSLPORT = 8001;
//創建http服務器
httpServer.listen(PORT, function() {
console.log('HTTP Server is running on: http://localhost:%s', PORT);
});
//創建https服務器
httpsServer.listen(SSLPORT, function() {
console.log('HTTPS Server is running on: https://localhost:%s', SSLPORT);
});
2.4 nginx配置https
server {
listen 80;
server_name localhost;
rewrite ^ https://$http_host$request_uri? permanent;
# force redirect http to https
}
server {
listen 443 ssl;
server_name localhost;
ssl_certificate server.crt;
ssl_certificate_key server.key;
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 5m;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
location / {
root D:\myProjects\hmall-pc\src\main\webapp;
try_files $uri $uri/ @router;
index hmall-base.html;
}
}
2.5 消除瀏覽器安全警告
因為我們不是官方的CA機構并沒有內置在瀏覽器或系統中,所以瀏覽器會認為該CA非法,此時我們需要將我們自建CA的根證書(ica.crt)加入到瀏覽器中。
Chrome :設置-設置-設置-高級-隱私設置和安全性-管理證書-導入
火狐:設置-選項-隱私與安全-證書-查看證書-證書機構-導入
親測火狐導入之后有效,chrome的依然有安全警告...
3、加密方式
3.1 對稱加密
-
對稱加密:加密和解密用同一個秘鑰的加密,特點是快。
對稱加密 -
對稱加密的算法存在的問題:
多個客戶端的時候,在一端生成一個秘鑰,傳輸秘鑰的過程,如果被中間人攔截,秘鑰也會被獲取。
多用戶對稱加密
3.2 非對稱加密
-
非對稱加密:會有一對秘鑰公鑰和私鑰,公鑰加密,私鑰解密,特點是安全,但是慢。
非對稱加密 -
非對稱加密存在的問題:
私鑰只保存在服務器端,公鑰可以發送給所有的客戶端。在傳輸公鑰的過程中,有被中間人獲取的風險,雖然中間人是無法破解(因為私鑰只保存在服務器端,只有私鑰可以破解公鑰加密的內容),但是公鑰被中間人拿到可篡改。
Man-in-the-MiddleAttack篡改公鑰
如果中間人篡改公鑰,客戶端和服務器端相互傳遞的消息容易都是假的。
3.3 第三方認證
- 公鑰被掉包,是因為客戶端無法分辨傳回公鑰的到底是中間人,還是服務器。在HTTPS中,使用
數字證書
(網站信息 + 數字簽名)來解決這個問題,中間人攔截后把服務器的公鑰替換為自己的公鑰,因為數字簽名的存在,會導致客戶端驗證簽名不匹配,這樣就防止了中間人替換公鑰的問題。
數字簽名:將網站的信息加密后通過第三方機構的私鑰再次進行加密,生成數字簽名。
第三方證書
4、https工作機制
- client使用公鑰A給秘鑰X加密,server使用私鑰B給秘鑰X解密,這一過程是非對稱加密。
- client和server利用對稱加密秘鑰X的快捷性來加密解密報文
參考文章:
https://blog.csdn.net/m0_37263637/article/details/80314093
https://www.kuacg.com/22672.html
http://www.lxweimin.com/p/b92d4c8cbe05