【https】Self-Signed SSL证书创建和使用 目录一、 创建 Self-Signed SSL Certificate(自签名证书)二、配置证书到服务器端5. 将证书添加到客户端TrustStoreSSl证书格式简介实践10.60.100.191上的cm8的server.key crt , pem的替换加下SAN信息如何将给apache使用的key、crt文件导入 到keystore只能通过命令行进行下面操作纯命令行制作 没有问题参考资料一、 创建 Self-Signed SSL Certificate(自签名证书)一般可用于个人测试使用和非域名https访问通常CA机构不支持颁发IP证书只有个别OV机构支持公网IP证书但只能用于大型机构组织的网站。When using the SSL for non-production applications or other experiments you can use a self-signed SSL certificate. Though the certificate implements full encryption, visitors to your site will see a browser warning indicating that the certificate should not be trusted.PrerequisitesThe openssl library is required to generate your own certificate. Run the following command in your local environment to see if you already have openssl installed installed.Generate private key and certificate signing requestA private key and certificate signing request are required to create an SSL certificate. These can be generated with a few simple commands.When theopenssl reqcommand asks for a “challenge password”, just press return, leaving the password empty. This password is used by Certificate Authorities to authenticate the certificate owner when they want to revoke their certificate. Since this is a self-signed certificate, there’s no way to revoke it via CRL (Certificate Revocation List).$ openssl genrsa -aes256 -passout pass:gsahdg -out server.pass.key 4096 ... $ openssl rsa -passin pass:gsahdg -in server.pass.key -out server.key writing RSA key $ rm server.pass.key $ openssl req -new -key server.key -out server.csr ... Country Name (2 letter code) [AU]:US State or Province Name (full name) [Some-State]:California ... A challenge password []: ...Generate SSL certificateThe self-signed SSL certificate is generated from theserver.keyprivate key andserver.csrfiles.$ openssl x509 -req -sha256 -days 365 -in server.csr -signkey server.key -out server.crtTheserver.crtfile is your site certificate suitable for use with SSL add-onalong with theserver.keyprivate key.其中server.key为服务端私钥文件用于后续传输加解密。server.crt为签好名的证书文件其中包含了服务描述信息和公钥用于发往客户端进行合法验证公钥用于后续传输加解密。二、配置证书到服务器端以最新版本nginx启用ssl配置为例将server.key和server.crt文件保存到 nginx主目录下cert目录中修改nginx主目录下default.conf配置文件包含以下内容server { listen 443 ssl; server_name localhost; root /usr/local/nginx/html; access_log /usr/local/nginx/logs/nginx.log main; index index.html index.htm; ssl_certificate /usr/local/nginx/cert/server.crt; ssl_certificate_key /usr/local/nginx/cert/server.key; ssl_session_timeout 5m; ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4; ssl_protocols TLSv1 TLSv1.1 TLSv1.2; ssl_prefer_server_ciphers on; ... }3. 重载nginx配置信息$ nginx -s reload4. 测试访问情况使用chrome浏览器https进行访问会有如下结果由于浏览器只信任通过公开的第三方权威CA机构颁发出来的证书通常这个证书采用证书链形式建立信任关系根证书可以对应到操作系统内置的信任根证书列表并采用层层签名的方式校验连接关系。此服务器回传证书为自签名证书在客户端操作系统找不到对应的根证书进行信任传递所以浏览会有安全是合情合理的不过就算我们把证书安装到操作系统根证书中也还会有提示此处可以临时让浏览器信任安全警告无法消除。java程序访问结果Caused by: javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target at sun.security.ssl.Alerts.getSSLException(Unknown Source) at sun.security.ssl.SSLSocketImpl.fatal(Unknown Source) at sun.security.ssl.Handshaker.fatalSE(Unknown Source) at sun.security.ssl.Handshaker.fatalSE(Unknown Source) at sun.security.ssl.ClientHandshaker.serverCertificate(Unknown Source) at sun.security.ssl.ClientHandshaker.processMessage(Unknown Source) at sun.security.ssl.Handshaker.processLoop(Unknown Source) at sun.security.ssl.Handshaker.process_record(Unknown Source) at sun.security.ssl.SSLSocketImpl.readRecord(Unknown Source) at sun.security.ssl.SSLSocketImpl.performInitialHandshake(Unknown Source) at sun.security.ssl.SSLSocketImpl.startHandshake(Unknown Source) at sun.security.ssl.SSLSocketImpl.startHandshake(Unknown Source) at sun.net.www.protocol.https.HttpsClient.afterConnect(Unknown Source) at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(Unknown Source) at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source) at java.net.HttpURLConnection.getResponseCode(Unknown Source) at sun.net.www.protocol.https.HttpsURLConnectionImpl.getResponseCode(Unknown Source) at twitter4j.internal.http.HttpResponseImpl.init(HttpResponseImpl.java:34) at twitter4j.internal.http.HttpClientImpl.request(HttpClientImpl.java:141) ... 5 more Caused by: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target at sun.security.validator.PKIXValidator.doBuild(Unknown Source) at sun.security.validator.PKIXValidator.engineValidate(Unknown Source) at sun.security.validator.Validator.validate(Unknown Source) at sun.security.ssl.X509TrustManagerImpl.validate(Unknown Source) at sun.security.ssl.X509TrustManagerImpl.checkTrusted(Unknown Source) at sun.security.ssl.X509TrustManagerImpl.checkServerTrusted(Unknown Source) ... 20 more Caused by: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target at sun.security.provider.certpath.SunCertPathBuilder.engineBuild(Unknown Source) at java.security.cert.CertPathBuilder.build(Unknown Source) ... 26 more Failed to search tweets: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target5. 将证书添加到客户端TrustStore通过jdk命令行工具keytool导入服务器证书到java信任证书库中:$ keytool -import -alias example -keystore $JAVA_HOME/lib/security/cacerts -file server.crt将会询问证书库的密码为默认值changeit。重启Jvm进程。参考文章Creating a Self-Signed SSL CertificateCreating a Self-Signed SSL Certificate | Heroku Dev CenterSSl证书格式简介(1)常见的格式有: PEM、JKS、CRT、PFX 等。PEM 它是 openssl 默认采用的信息存放方式。Openssl 中的 PEM 文件JKS keytool工具生成的的文件转换成的JKS格式证书。JKS证书主要用于java级系统应用如tomcat等.CRT 证书文件。可以是PEM格式。PFX 或 P12 – 公钥加密标准 #12 (PKCS#12) 可包含所有私钥、公钥和证书。其以二进制格式存储也称为 PFX 文件证书格式介绍PKCS 全称是 Public-Key Cryptography Standards 是由 RSA 实验室与其它安全系统开发商为促进公钥密码的发展而制订的一系列标准PKCS 目前共发布过 15 个标准。 常用的有1.PKCS#7 Cryptographic Message Syntax Standard2.PKCS#10 Certification Request Standard3.PKCS#12 Personal Information Exchange Syntax StandardX.509是常见通用的证书格式。所有的证书都符合为Public Key Infrastructure (PKI) 制定的 ITU-T X509 国际标准。1.PKCS#7常用的后缀是 .P7B .P7C .SPC2.PKCS#12常用的后缀有 .P12 .PFX3.X.509 DER编码(ASCII)的后缀是 .DER .CER .CRT4.X.509 PAM编码(Base64)的后缀是 .PEM .CER .CRT5..cer/.crt是用于存放证书它是2进制形式存放的不含私钥。6..pem跟crt/cer的区别是它以Ascii来表示。7.pfx/p12用于存放个人证书/私钥他通常包含保护密码2进制方式8.p10是证书请求9.p7r是CA对证书请求的回复只用于导入10.p7b以树状展示证书链(certificate chain)同时也支持单个证书不含私钥。实践10.60.100.191上的cm8的server.key crt , pem的替换为了创建一个配套的密钥key和证书crt文件您需要使用OpenSSL工具。这里我将为您提供一种生成自签名证书的简单方法。请按照以下步骤操作1. 下载并安装OpenSSL首先您需要下载并安装OpenSSL工具。您可以从OpenSSL官方网站下载预编译的二进制文件或者使用适用于Windows的OpenSSL发行版例如Win32 OpenSSL。确保将OpenSSL的可执行文件路径添加到系统环境变量中。2. 生成私钥key文件在命令提示符cmd中导航到希望保存证书和私钥的目录。然后运行以下命令来生成私钥文件openssl genpkey -algorithm RSA -out server.key该命令将生成一个RSA算法的私钥文件server.key。3. 生成自签名证书请求CSR接下来使用私钥生成一个自签名证书请求CSR文件。运行以下命令openssl req -new -key server.key -out server.csr在运行命令时您需要提供一些证书相关的信息例如Common Name通用名称通常为您的服务器的域名或IP地址。您可以按照提示输入这些信息。4. 生成自签名证书crt现在使用私钥和CSR文件生成自签名证书crt文件。运行以下命令openssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crt该命令将生成一个有效期为365天的自签名证书文件server.crt。5. 完成现在您已经成功生成了Apache所需的密钥server.key和证书server.crt文件。接下来将这两个文件放入Apache配置中并启用SSL模块和相关配置即可启用HTTPS服务。请注意自签名证书不会被浏览器和操作系统默认信任您在使用自签名证书时可能会收到安全警告。在生产环境中建议从受信任的证书颁发机构CA获取证书以确保安全性和可信度。自签名证书仅适用于测试、开发和本地环境。加下SAN信息为Apache 制作一个含有 Subject Alternative Name (SAN) 的自签名证书您需要使用 OpenSSL 工具。SAN 是一种允许在单个证书中包含多个域名或主机名的扩展。这样可以让同一个证书适用于多个域名或子域名。以下是制作含有 SAN 的自签名证书的步骤1. 安装 OpenSSL首先确保您已经安装了 OpenSSL 工具。您可以从 OpenSSL 官方网站下载预编译的二进制文件或者使用适用于您的操作系统的 OpenSSL 发行版。确保将 OpenSSL 的可执行文件路径添加到系统环境变量中。2. 创建证书配置文件创建一个用于配置证书生成的配置文件例如 san.cnf。在该文件中定义主体信息和 SAN 扩展。plaintext[req]distinguished_name req_distinguished_namereq_extensions v3_reqprompt no[req_distinguished_name]CN example.com # 通用名称[v3_req]subjectAltName alt_names[alt_names]DNS.1 example.com # 添加主机名或域名DNS.2 www.example.com # 添加其他域名每个域名递增数字在此示例中我们在 SAN 扩展中添加了两个主机名example.com 和 www.example.com。您可以根据需要添加更多的域名或主机名。3. 生成私钥和证书请求在命令提示符cmd中导航到包含配置文件的目录并使用以下命令生成私钥和证书请求CSRbashopenssl req -new -newkey rsa:2048 -nodes -keyout server.key -out server.csr -config san.cnf该命令将生成一个2048位的 RSA 私钥server.key和 CSR 文件server.csr。4. 生成含有 SAN 的自签名证书使用以下命令使用私钥和 CSR 生成含有 SAN 的自签名证书bashopenssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crt -extensions v3_req -extfile san.cnf该命令将生成一个有效期为 365 天的含有 SAN 的自签名证书server.crt。5. 完成现在您已经成功创建了含有 SAN 的自签名证书server.crt。您可以将该证书和私钥server.key配置到 Apache 上的 SSL/TLS 设置中以启用 HTTPS 服务。请注意自签名证书不会被浏览器和操作系统默认信任除非您手动将其添加到受信任的根证书机构。在生产环境中建议从受信任的证书颁发机构CA获得证书以确保安全性和可信度。自签名证书仅适用于测试、开发和本地环境。如何将给apache使用的key、crt文件导入 到keystore通过 keystore 导出的apache用不了通过 keystore导入key,crt 这个不成功。只能通过命令行进行下面操作10.60.100.192上制作。本机还不行。要将用于 Apache 的密钥key和证书crt文件导入到 Java Keystore.keystore您需要执行以下步骤1. 将 key 和 crt 文件放入同一目录首先将 Apache 使用的密钥文件例如 server.key和证书文件例如 server.crt放入同一个目录中。2. 生成 PKCS12 文件使用 OpenSSL 工具将密钥和证书合并为 PKCS12 文件。在命令提示符cmd中导航到存放 key 和 crt 文件的目录然后运行以下命令bashopenssl pkcs12 -export -in server.crt -inkey server.key -out server.p12运行该命令时您需要输入一个密码该密码将用于保护 PKCS12 文件。请记住这个密码稍后将在导入到 Java Keystore 时使用。3. 导入 PKCS12 文件到 Java Keystore使用 keytool 工具将 PKCS12 文件导入到 Java Keystore 中。运行以下命令bashkeytool -importkeystore -srckeystore server.p12 -srcstoretype PKCS12 -destkeystore your_keystore_file.keystore在运行命令时您需要提供目标 keystore 的密码以及确认是否要将 PKCS12 文件中的私钥密码保持与 keystore 文件相同。按照提示输入相关信息。4. 验证 Keystore可以使用以下命令来验证 Java Keystore 是否成功导入bashkeytool -list -keystore your_keystore_file.keystore在运行命令时您需要输入 keystore 的密码。如果一切正常将会显示导入的证书和密钥信息。现在您已经成功将 Apache 使用的密钥和证书导入到 Java Keystore 中。在使用 Keystore 时确保保护好 keystore 文件和相关密码以防止未经授权的访问。请注意Keystore 中的私钥和证书将仅用于 Java 应用程序和服务器而 Apache 仍然需要使用原始的密钥和证书文件用于 HTTPS 服务。纯命令行制作 没有问题通过keystore做好了*.keystore导出的crt ,key ,apache用不了将已经有的crt,key导入keystore , java程序用不了只能通过纯命令行制作 java,apache都能用。san.cnf[req] distinguished_name req_distinguished_name req_extensions v3_req prompt no [req_distinguished_name] CN vms.dualven.cn # 通用名称 [v3_req] subjectAltName alt_names [alt_names] DNS.1 vms.dualven.cn # 添加主机名或域名 DNS.2 localhost # 添加其他域名每个域名递增数字 IP.1 127.0.0.1 IP.2 180.168.191.195生成key ,crt , pemopenssl req -new -newkey rsa:2048 -nodes -keyout server.key -out server.csr -config san.cnf -subj /CGB/Odualven/OUgbcom/CNvms.dualven.cn openssl x509 -req -days 3650 -in server.csr -signkey server.key -out server.crt -extensions v3_req -extfile san.cnf cat server.key server.crt server.pem打包到keystore 里。openssl pkcs12 -export -in server.crt -inkey server.key -out server.p12 keytool -importkeystore -srckeystore server.p12 -srcstoretype PKCS12 -destkeystore server.keystore #keytool -list -keystore server.keystorehttp://10.30.40.10/dualvenDoc/sslkeys.git参考资料讲了为啥要用对称加密 https传输中https://blog.csdn.net/qzwzsl/article/details/125731575双向认证说明一般只是客户端浏览器或者程序校验服务端。 服务器一般欢迎所有客户端如果只对特定的客户端就要双向校验https://blog.csdn.net/lgxzzz/article/details/1246454892026-08-04wvp 8970emqx 10.60.2.199:8084990 20260715-11:57:19: docker cp ./key.pem emqx:/opt/emqx/etc/certs/ 991 20260715-11:57:28: docker cp ./cert.pem emqx:/opt/emqx/etc/certs/ 1030 20260804-19:45:42: history |grep emqx:/opt (base) [rootinsight-face zlm]# docker cp /home/dualven/wvp wvp/ wvp-20260804/ (base) [rootinsight-face zlm]# docker cp /home/dualven/wvp-20260804/forEmqx/key.pem emqx:/opt/emqx/etc/certs/ (base) [rootinsight-face zlm]# docker cp /home/dualven/wvp-20260804/forEmqx/cert.pem emqx:/opt/emqx/etc/certs/zlm 10443myshell 9198other:宝信