带有openssl的自签名证书,用于在家中的服务器并且没有域名

IMT*_*Man 5 https ssl openssl dynamic-dns

我在网上阅读的所有文章都讨论了为您拥有的域创建自签名证书。

我在家里的 Ubuntu 16.04 服务器上设置了 Apache2。我的 ISP 给了我一个动态 IP,所以我使用 No-IP。我在路由器上打开了端口以将流量重定向到我的服务器。

我也从家里(在网络内部)访问网络服务器。

因此,如果我在家庭网络之外,我将使用https://username.noip.me/,如果我在家,我将使用https://homeserver/

那么如何为这种情况创建自签名证书呢?我把什么作为通用名称?

jww*_*jww 3

我该将什么作为通用名称?

您使用友好名称作为通用名称 (CN)有两个原因。首先,它通过工具向用户显示,因此您需要诸如Example Widgets, LLC之类的东西。其次,主机名始终位于主题备用名称 (SAN)中。IETF 和 CA/B 论坛均不赞成在 CN 中放置主机名。

有关更多规则和原因,请参阅如何使用证书颁发机构签署证书签名请求以及如何使用 openssl 创建自签名证书?


那么如何针对这种情况创建自签名证书呢?

使用openssl带有自定义配置文件的实用程序。下面是一个示例。

你应该做两件事。首先,将 DNS 名称列表更改为username.noip.mehomeserver。其次,更改证书中所需的名称后,运行以下命令:

openssl req -config example-com.conf -new -x509 -sha256 -newkey rsa:2048 -nodes \
    -keyout example-com.key.pem -days 365 -out example-com.cert.pem
Run Code Online (Sandbox Code Playgroud)

显然,您可以将配置文件的名称更改为example-com.conf您想要的任何名称。

另外,我在家里运行自己的 PKI。我有一个根 CA,可以在需要时为网络上的设备颁发证书。所有设备都安装了根 CA。我的内部域称为home.pvt. 网络上的主机被命名为,pine64.home.pvtrpi3.home.pvtsolaris.home.pvtwindows10.home.pvt等。一切都按预期进行。


配置文件示例

# Self Signed (note the addition of -x509):
#     openssl req -config example-com.conf -new -x509 -sha256 -newkey rsa:2048 -nodes -keyout example-com.key.pem -days 365 -out example-com.cert.pem
# Signing Request (note the lack of -x509):
#     openssl req -config example-com.conf -new -newkey rsa:2048 -nodes -keyout example-com.key.pem -days 365 -out example-com.req.pem
# Print it:
#     openssl x509 -in example-com.cert.pem -text -noout
#     openssl req -in example-com.req.pem -text -noout

[ req ]
default_bits        = 2048
default_keyfile     = server-key.pem
distinguished_name  = subject
req_extensions      = req_ext
x509_extensions     = x509_ext
string_mask         = utf8only

# The Subject DN can be formed using X501 or RFC 4514 (see RFC 4519 for a description).
#   Its sort of a mashup. For example, RFC 4514 does not provide emailAddress.
[ subject ]
countryName         = Country Name (2 letter code)
countryName_default     = US

stateOrProvinceName     = State or Province Name (full name)
stateOrProvinceName_default = NY

localityName            = Locality Name (eg, city)
localityName_default        = New York

organizationName         = Organization Name (eg, company)
organizationName_default    = Example, LLC

# Use a friendly name here because its presented to the user. The server's DNS
#   names are placed in Subject Alternate Names. Plus, DNS names here is deprecated
#   by both IETF and CA/Browser Forums. If you place a DNS name here, then you 
#   must include the DNS name in the SAN too (otherwise, Chrome and others that
#   strictly follow the CA/Browser Baseline Requirements will fail).
commonName          = Common Name (e.g. server FQDN or YOUR name)
commonName_default      = Example Company

emailAddress            = Email Address
emailAddress_default        = test@example.com

# Section x509_ext is used when generating a self-signed certificate. I.e., openssl req -x509 ...
#  If RSA Key Transport bothers you, then remove keyEncipherment. TLS 1.3 is removing RSA
#  Key Transport in favor of exchanges with Forward Secrecy, like DHE and ECDHE.
[ x509_ext ]

subjectKeyIdentifier        = hash
authorityKeyIdentifier  = keyid,issuer

basicConstraints        = CA:FALSE
keyUsage            = digitalSignature, keyEncipherment
subjectAltName          = @alternate_names
nsComment           = "OpenSSL Generated Certificate"

# RFC 5280, Section 4.2.1.12 makes EKU optional
# CA/Browser Baseline Requirements, Appendix (B)(3)(G) makes me confused
# extendedKeyUsage  = serverAuth, clientAuth

# Section req_ext is used when generating a certificate signing request. I.e., openssl req ...
[ req_ext ]

subjectKeyIdentifier        = hash

basicConstraints        = CA:FALSE
keyUsage            = digitalSignature, keyEncipherment
subjectAltName          = @alternate_names
nsComment           = "OpenSSL Generated Certificate"

# RFC 5280, Section 4.2.1.12 makes EKU optional
# CA/Browser Baseline Requirements, Appendix (B)(3)(G) makes me confused
# extendedKeyUsage  = serverAuth, clientAuth

[ alternate_names ]

DNS.1       = example.com
DNS.2       = www.example.com
DNS.3       = mail.example.com
DNS.4       = ftp.example.com

# Add these if you need them. But usually you don't want them or
#   need them in production. You may need them for development.
# DNS.5       = localhost
# DNS.6       = localhost.localdomain
# DNS.7       = 127.0.0.1

# IPv6 localhost
# DNS.8     = ::1
# DNS.9     = fe80::1
Run Code Online (Sandbox Code Playgroud)