自签IP证书给IIS使用全流程,纯干货

[root@localhost app]#  openssl genrsa -out ca.key 2048
Generating RSA private key, 2048 bit long modulus (2 primes)
.......................+++++
..................+++++
e is 65537 (0x010001)
[root@localhost app]# openssl req -x509 -new -nodes -key ca.key -sha256 -days 1024 -out ca.crt
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [AU]:CN
State or Province Name (full name) [Some-State]:Beijing
Locality Name (eg, city) []:Beijing
Organization Name (eg, company) [Internet Widgits Pty Ltd]:mycompany
Organizational Unit Name (eg, section) []:it department
Common Name (e.g. server FQDN or YOUR name) []:10.237.34.62
Email Address []:[email protected]
[root@localhost app]# 
[root@localhost app]# 
[root@localhost app]# 
[root@localhost app]# ls 
ca.crt  ca.key
[root@localhost app]# 
[root@localhost app]# 
[root@localhost app]# openssl genrsa -out server.key 2048
Generating RSA private key, 2048 bit long modulus (2 primes)
.....................+++++
...............................................................+++++
e is 65537 (0x010001)
[root@localhost app]# openssl req -new -key server.key -out server.csr
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [AU]:CN
State or Province Name (full name) [Some-State]:Beijing
Locality Name (eg, city) []:Beijing
Organization Name (eg, company) [Internet Widgits Pty Ltd]:mycompany
Organizational Unit Name (eg, section) []:it department
Common Name (e.g. server FQDN or YOUR name) []:10.237.34.62
Email Address []:[email protected]

Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:zkhw2024
An optional company name []:department
[root@localhost app]# 
[root@localhost app]# 
[root@localhost app]# ls 
ca.crt  ca.key  server.csr  server.key
[root@localhost app]# pwd
/data/app
[root@localhost app]# openssl pkcs12 -export -out server.pfx -inkey server.key -in server.crt -certfile ca.crt -password pass:zkhw2024
Can't open server.crt for reading, No such file or directory
281469723429616:error:02001002:system library:fopen:No such file or directory:crypto/bio/bss_file.c:69:fopen('server.crt','r')
281469723429616:error:2006D080:BIO routines:BIO_new_file:no such file:crypto/bio/bss_file.c:76:
[root@localhost app]# ls 
ca.crt  ca.key  server.csr  server.key
[root@localhost app]# openssl x509 -req -days 365 -in server.csr -CA ca.crt -CAkey ca.key -CAcreateserial -out server.crt
Signature ok
subject=C = CN, ST = Beijing, L = Beijing, O = mycompany, OU = it department, CN = 10.237.34.62, emailAddress = 465544826@qq.com
Getting CA Private Key
[root@localhost app]# 
[root@localhost app]# 
[root@localhost app]# 
[root@localhost app]# ls 
ca.crt  ca.key  ca.srl  server.crt  server.csr  server.key
[root@localhost app]# openssl pkcs12 -export -out server.pfx -inkey server.key -in server.crt -certfile ca.crt -password pass:zkhw2024
[root@localhost app]# 
[root@localhost app]# 
[root@localhost app]# ls 
ca.crt  ca.key  ca.srl  server.crt  server.csr  server.key  server.pfx
[root@localhost app]# 

其中密码和IP自定义。

在你生成的文件中,公钥和私钥的文件用途如下:

私钥文件:

  • ca.key:这是证书颁发机构(CA)的私钥,用于签署其他证书(如服务器证书)。它非常重要,必须严格保密,只有CA的管理员才能访问。
  • server.key:这是服务器的私钥,用于加密和解密数据。它也必须保密,只有服务器的管理员才能访问。

公钥文件:

  • ca.crt:这是CA的公钥证书,包含了CA的公钥。它用于验证其他证书的真实性。客户端(如浏览器)会使用这个证书来验证服务器证书是否由可信的CA签发。
  • server.crt:这是服务器的公钥证书,包含了服务器的公钥。它用于让客户端验证服务器的身份,并用于加密数据。

其他文件:

  • server.csr:这是服务器的证书签名请求(CSR),它包含了服务器的公钥和一些身份信息,用于向CA请求证书。它本身不是公钥或私钥,但包含了公钥。
  • server.pfx:这是一个PKCS#12格式的文件,包含了服务器的私钥和证书(server.keyserver.crt),以及CA证书(ca.crt)。它通常用于Windows系统或需要私钥和证书捆绑在一起的场景。

你可以使用的文件:

  1. 服务器使用

    • server.key:服务器的私钥,用于解密客户端加密的数据。
    • server.crt:服务器的公钥证书,用于让客户端验证服务器的身份。
    • ca.crt:CA的公钥证书,用于让客户端验证服务器证书的真实性。
  2. 客户端使用

    • ca.crt:客户端需要这个证书来验证服务器证书是否可信。
    • server.crt:客户端会通过这个证书来验证服务器的身份。
  3. 导出用于其他系统

    • server.pfx:如果你需要将服务器证书和私钥导入到其他系统(如Windows),可以使用这个文件。

总结:

  • server.key 是服务器的私钥,保密使用。
  • server.crt 是服务器的公钥证书,公开使用。
  • ca.crt 是CA的公钥证书,公开使用。
  • server.pfx 是包含服务器私钥和证书的文件,用于特殊场景。

其他文件(server.csrca.srl)主要用于生成证书的过程,通常不需要直接使用。

你可能感兴趣的:(tcp/ip,前端,网络协议,ssl,证书)