SSL证书不适用于Ubuntu 16.04上的localhost

时间:2018-10-04 19:47:34

标签: ubuntu ssl networking certificate

我试图在本地主机上为我的vps添加证书,因此我可以使用https://localhost:1337从我的vps访问在端口1337上运行的节点服务器。

当前,当我的vps上被Firefox访问时,它给我一个“您的连接不安全”的信息。但是,访问https://my-website.example:1337可以正常工作。

我到目前为止所做的步骤:

  1. cd /usr/local/share/ca-certificates
  2. openssl req -x509 -sha256 -nodes -newkey rsa:2048 -days 365 -keyout localhost.key -out localhost.crt
    • 国家/地区名称:空
    • 州或省名称:空
    • 地点名称:空
    • 组织名称:空
    • 组织单位名称:空
    • 公用名:localhost
    • 电子邮件地址:空
  3. chmod 644 localhost.crt
  4. chmod 644 localhost.key
  5. sudo update-ca-certificates

1 个答案:

答案 0 :(得分:0)

还必须将证书添加到浏览器信任的存储中,您所做的步骤仅适用于CURL,WGET等工具。

在用于本地开发的docker容器中,我使用一组bash脚本来设置一个根证书颁发机构,该证书颁发机构将用于颁发开发所需的所有域证书。

浏览器仅需要了解根证书颁发机构,您的服务器将需要了解您颁发的所有域证书。

请按以下顺序尝试以下脚本集:

./setup-root-certificate.sh "root-ca.key" "root-ca.pem" "RootCertificateAuthority" && \
./create-domain-certificate.sh "localhost" "root-ca.key" "root-ca.pem" && \
./add-certificate-to-browser.sh "root-ca.pem" "RootCertificateAuthority"

现在只需将域证书添加到您的Apache,Nginx或您正在使用的任何其他服务器中,然后重新启动浏览器和服务器。

此解决方案不需要您在首次访问localhost域时添加例外。

  

ALERT:此脚本已在Docker容器内使用,我尚未在主机上直接对其进行过测试,但应以相同的方式工作。

要设置根证书:./setup-root-certificate.sh

#!/bin/bash

set -eu

###
# inspired https://fabianlee.org/2018/02/17/ubuntu-creating-a-trusted-ca-and-san-certificate-using-openssl-on-ubuntu/
###


ROOT_CA_KEY="${1?Missing Name for root certificate KEY file}"
ROOT_CA_PEM="${2?Missing Name for root certificate PEM file}"
ROOT_CA_NAME="${3?Missing Certificate Name}"
CONFIG_FILE="${4:-openssl.cnf}"

if [ ! -f ROOT_CA_PEM ]
    then
        printf "\n>>> CREATING A ROOT CERTIFICATE <<<\n"

        openssl req \
            -new \
            -newkey rsa:4096 \
            -days 3650 \
            -nodes \
            -x509 \
            -extensions v3_ca \
            -subj "/C=US/ST=CA/L=SF/O=${ROOT_CA_NAME}/CN=${ROOT_CA_NAME}" \
            -keyout ${ROOT_CA_KEY} \
            -out ${ROOT_CA_PEM} \
            -config ${CONFIG_FILE}

        printf "\n>>> ADDING ROOT CERTIFICATE TO THE TRUSTED STORE <<<\n"

        # add certificate to the trust store
        cp ${ROOT_CA_PEM} /usr/local/share/ca-certificates/self-signed-root-ca.crt
        update-ca-certificates

        # verifies the certificate
        openssl x509 -in ${ROOT_CA_PEM} -text -noout > "${ROOT_CA_NAME}.txt"

        printf "\n >>> ROOT CERTICATE CREATED SUCCESEFULY<<<\n"

    else
        printf "\n >>> ROOT CERTICATE ALREADY EXISTS <<<\n"
fi

要创建域证书,请执行以下操作:./create-domain-certificate.sh

    #!/bin/bash

set -eu

###
# inspired https://fabianlee.org/2018/02/17/ubuntu-creating-a-trusted-ca-and-san-certificate-using-openssl-on-ubuntu/
###


DOMAIN="${1:-example.com}"
ROOT_CA_KEY="${2?Missing Name for root certificate KEY file}"
ROOT_CA_PEM="${3?Missing Name for root certificate PEM file}"

DOMAIN_CA_KEY="${DOMAIN}.key"
DOMAIN_CA_CSR="${DOMAIN}.csr"
DOMAIN_CA_CRT="${DOMAIN}.crt"
DOMAIN_CA_TXT="${DOMAIN}.txt"
CONFIG_FILE="${DOMAIN}.cnf" 


printf "\n>>> MERGINGING CONFIGURATION FROM ${DOMAIN_CA_TXT} INTO ${CONFIG_FILE} <<<\n"
cat openssl.cnf ${DOMAIN_CA_TXT} > ${CONFIG_FILE}


printf "\n>>> GENERATING KEY FOR DOMAIN CERTIFICATE: ${DOMAIN_CA_KEY} <<<\n"

# generate the private/public RSA key pair for the domain
openssl genrsa -out ${DOMAIN_CA_KEY} 4096

printf "\n>>> GENERATING CSR FOR DOMAIN CERTIFICATE: ${DOMAIN_CA_CSR} <<<\n"

# create the server certificate signing request:
openssl req \
    -subj "/CN=${DOMAIN}" \
    -extensions v3_req \
    -sha256 \
    -new \
    -key ${DOMAIN_CA_KEY} \
    -out ${DOMAIN_CA_CSR}

printf "\n>>> GENERATING CRT FOR DOMAIN CERTIFICATE: ${DOMAIN_CA_CRT} <<<\n"

# generate the server certificate using the: server signing request, the CA signing key, and CA cert.
openssl x509 \
            -req \
            -extensions v3_req \
            -days 3650 \
            -sha256 \
            -in ${DOMAIN_CA_CSR} \
            -CA ${ROOT_CA_PEM} \
            -CAkey ${ROOT_CA_KEY} \
            -CAcreateserial \
            -out ${DOMAIN_CA_CRT} \
            -extfile ${CONFIG_FILE}

# verifies the certificate
openssl x509 -in ${DOMAIN_CA_CRT} -text -noout > ${DOMAIN}.txt

printf "\n >>> CERTIFICATE CREATED FOR DOMAIN: ${DOMAIN} <<<\n"

要将根证书添加到浏览器信任的证书存储中:./add-certificate-to-browser.sh

#!/bin/bash

###
# https://thomas-leister.de/en/how-to-import-ca-root-certificate/
###


### Script installs root.cert.pem to certificate trust store of applications using NSS
### (e.g. Firefox, Thunderbird, Chromium)
### Mozilla uses cert8, Chromium and Chrome use cert9

###
### Requirement: apt install libnss3-tools
###


###
### CA file to install (CUSTOMIZE!)
###

CA_PEM="${1?Missing file name for the PEM certificate}"
CA_NAME="${2?Missing Certificate Name}"
BROWSER_CONFIG_DIR="${3:-/home}"

###
### For cert8 (legacy - DBM)
###

for certDB in $(find ${BROWSER_CONFIG_DIR} -name "cert8.db")
do
    certdir=$(dirname ${certDB});
    certutil -A -n "${CA_NAME}" -t "TCu,Cu,Tu" -i ${CA_PEM} -d dbm:${certdir}
done


###
### For cert9 (SQL)
###

for certDB in $(find ${BROWSER_CONFIG_DIR} -name "cert9.db")
do
    certdir=$(dirname ${certDB});
    certutil -A -n "${certname}" -t "TCu,Cu,Tu" -i ${CA_PEM} -d sql:${certdir}
done