auto-generate-docker-tls-ca.sh 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. # !/bin/bash
  2. # 一键生成TLS和CA证书
  3. # Create : 2021-08-25
  4. # Update : 2021-08-25
  5. # @Autor : wuduoqiang
  6. # 服务器主机名
  7. SERVER="yibajiuling"
  8. # 密码
  9. PASSWORD="1890!Hd1"
  10. # 国家
  11. COUNTRY="CN"
  12. # 省份
  13. STATE="bj"
  14. # 城市
  15. CITY="hd"
  16. # 机构名称
  17. ORGANIZATION="ybjl"
  18. # 机构单位
  19. ORGANIZATIONAL_UNIT="ybjl"
  20. # 邮箱
  21. EMAIL="121"
  22. # 生成CA密钥
  23. # 生成CA密钥
  24. # 生成CA密钥
  25. openssl genrsa -aes256 -passout pass:$PASSWORD -out ca-key.pem 2048
  26. # 生成CA证书
  27. openssl req -new -x509 -passin "pass:$PASSWORD" -days 3650 -key ca-key.pem -sha256 -out ca-cert.pem -subj "/C=$COUNTRY/ST=$STATE/L=$CITY/O=$ORGANIZATION/OU=$ORGANIZATIONAL_UNIT/CN=$SERVER/emailAddress=$EMAIL"
  28. # 生成服务端密钥
  29. openssl genrsa -out server-key.pem 2048
  30. # 生成服务端证书签名的请求文件
  31. openssl req -subj "/CN=$SERVER" -new -key server-key.pem -out server-req.csr
  32. # 生成服务端证书
  33. openssl x509 -req -days 3650 -in server-req.csr -CA ca-cert.pem -CAkey ca-key.pem -passin "pass:$PASSWORD" -CAcreateserial -out server-cert.pem
  34. # 生成客户端密钥
  35. openssl genrsa -out client-key.pem 2048
  36. # 生成客户端证书签名的请求文件
  37. openssl req -subj '/CN=client' -new -key client-key.pem -out client-req.csr
  38. # 生成客户端证书
  39. sh -c 'echo "extendedKeyUsage=clientAuth" >> extfile.cnf'
  40. openssl x509 -req -days 3650 -in client-req.csr -CA ca-cert.pem -CAkey ca-key.pem -passin "pass:$PASSWORD" -CAcreateserial -out client-cert.pem -extfile extfile.cnf
  41. # 更改密钥权限
  42. chmod 0400 ca-key.pem server-key.pem client-key.pem
  43. # 更改证书权限
  44. chmod 0444 ca-cert.pem server-cert.pem client-cert.pem
  45. # 删除无用文件
  46. # rm ca-cert.srl client-req.csr server-req.csr extfile.cnf