# req: 生成证书签发申请文件CSR # -new: 生成新的证书签发申请文件CSR # -key: 指定私钥文件 # -out: 指定生成的证书签发申请文件CSR的路径 # -subj: 指定证书签发申请文件CSR中的信息,这里指定了国家、省份、城市、公司、部门、邮箱、域名等信息,如果不指定,则需要手动输入 # $ openssl req -new -key ca-key.pem -out ca.csr -subj "/C=CN/ST=beijing/L=beijing/O=test-on/OU=test-ou/CN=test-fqdn/emailAddress=test@example.com" $ openssl req -new -key ca-key.pem -out ca.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]:test-on Organizational Unit Name (eg, section) []:test-ou Common Name (e.g. server FQDN or YOUR name) []:test-fqdn Email Address []:test@example.com
Please enter the following 'extra' attributes to be sent with your certificate request A challenge password []: An optional company name []:
如果报错 Can't load /home/aaron/.rnd into RNG,则需要执行以下命令:
1 2
cd /home/aaron openssl rand -writerand .rnd
如果想查看证书签发申请文件CSR的内容,可以使用以下命令:
1 2 3 4 5 6 7 8 9
$ openssl req -in ca.csr -noout -text Certificate Request: Data: Version: 1 (0x0) Subject: C = CN, ST = beijing, L = beijing, O = test-on, OU = test-ou, CN = test-fqdn, emailAddress = test@example.com Subject Public Key Info: Public Key Algorithm: rsaEncryption RSA Public-Key: (8192 bit) ......
$ openssl x509 -in ca.cer -noout -text Certificate: Data: Version: 1 (0x0) Serial Number: 67:6b:09:ad:8e:e9:05:60:9d:d3:f5:c5:2a:27:20:14:b0:59:14:a3 Signature Algorithm: sha256WithRSAEncryption Issuer: C = CN, ST = beijing, L = beijing, O = test-on, OU = test-ou, CN = test-fqdn, emailAddress = test@example.com Validity Not Before: Oct 24 15:24:37 2023 GMT Not After : Oct 21 15:24:37 2033 GMT Subject: C = CN, ST = beijing, L = beijing, O = test-on, OU = test-ou, CN = test-fqdn, emailAddress = test@example.com Subject Public Key Info: ......
$ 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]:server-on Organizational Unit Name (eg, section) []:server-ou Common Name (e.g. server FQDN or YOUR name) []:cert.galaxy.com Email Address []:cert@galaxy.com
Please enter the following 'extra' attributes to be sent with your certificate request A challenge password []: An optional company name []:
准备配置文件
有时候,网站即使 Common Name 和域名是一样的,但是浏览器还是会报错 NET::ERR_CERT_COMMON_NAME_INVALID,这是因为证书中没有正确的嵌入域名信息,这时候就需要在配置文件中指定域名了。
# gencert: 生成证书 # -initca: 指定生成根证书 # 只使用cfssl命令会将证书输出到标准输出,因此需要重定向到文件中 $ cfssl gencert -initca ca-csr.json 2023/10/30 21:20:06 [INFO] generating a new CA key and certificate from CSR 2023/10/30 21:20:06 [INFO] generate received request 2023/10/30 21:20:06 [INFO] received CSR 2023/10/30 21:20:06 [INFO] generating key: rsa-2048 2023/10/30 21:20:07 [INFO] encoded CSR 2023/10/30 21:20:07 [INFO] signed certificate with serial number 664542684350169622832034206885696124480833745240 {"cert":"-----BEGIN CERTIFICATE-----\nMIIDzjCCAragAwIBAgIUdGcf5FWyqUAx3gID1+gF9KMAcVgwDQYJKoZIhvcNAQEL\nBQAwbTELMAkGA1UEBhMCQ04xEDAOBgNVBAgTB2JlaWppbmcxEDAOBgNVBAcTB2Jl\naWppbmcxEDAOBgNVBAoTB3Rlc3Qtb24xEDAOBgNVBAsTB3Rlc3Qtb3UxFjAUBgNV\nBAMTDWNhLmdhbGF4eS5jb20wHhcNMjMxMDMwMTMxNTAwWhcNMzMxMDI3MTMxNTAw\nWjBtMQswCQY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ky5va0CgYAk+hz+XXNbdYFZVkxbfwG5\nVMFmgYSkbG2wNXDUkzZZ0YOMm30BlTicqw+ifDwKoTJY32zzwl3GKDkcumugZSwS\nCjWl/brFuptrlzxXJv41RUpJ4yLZW4RJAvAGwSgl1W3n7HT8LYNLRDw+ssubEV68\ncd/4Cw6coa9dgrUYaTvJAwKBgQC1c4PaoI50HHLHi9TrqWEITH2JAeLCpTYQQGOw\nJWikYSVoCYhYvxPFGy/wbKZf+h8jsPWcPaHHW3nCBK3OfxPYBvfAR9LXMO3I6iKS\nhMQCIpUSH4xumTuzsLzJix85r8rJtM8t8C7hi7c3MVDCp6BzxTQs/qxB+velNrTI\nXXr/iQKBgQCNXdas7MwBxg5TUAbRbiMbQsJqeatLDc34ojckNxhT9896GLhXILHb\nTBRlOwd5mePRk9RxuCPzXWs3080QWDsMtEqCm2aTRcAtZpM0lAFGDInPVfjbvQ3n\nrr4Psi4TR0dcq2n6xwKDxHQHBw4mMwa+50lkE6l0foJ4LAgO0044ig==\n-----END RSA PRIVATE KEY-----\n"}
# 查看没有任何文件生成 $ ls ca-csr.json
# 通过cfssl-json命令将证书输出到文件中 # -bare: 指定输出文件的前缀 $ cfssl gencert -initca ca-csr.json | cfssl-json -bare ca 2023/10/30 21:20:31 [INFO] generating a new CA key and certificate from CSR 2023/10/30 21:20:31 [INFO] generate received request 2023/10/30 21:20:31 [INFO] received CSR 2023/10/30 21:20:31 [INFO] generating key: rsa-2048 2023/10/30 21:20:32 [INFO] encoded CSR 2023/10/30 21:20:32 [INFO] signed certificate with serial number 623569630703280871026003415413519558412232890993
# 此时查看就有证书文件了 $ ls ca.csr ca-csr.json ca-key.pem ca.pem