Create a self-signed SSL Certificate with OpenSSL

Creating a self-signed certificate with OpenSSL

by Mike Solomon

OpenSSL comes installed with Mac OS X (but see below), as well as many Linux and Unix distributions. Creating a certificate with it is very easy.

OpenSSL commands

openssl genrsa -out key.pem 2048
openssl req -new -sha256 -key key.pem -out csr.csr
openssl req -x509 -sha256 -days 365 -key key.pem -in csr.csr -out certificate.pem
openssl req -in csr.csr -text -noout | grep -i "Signature.*SHA256" && echo "All is well" || echo "This certificate will stop working in 2017! You must update OpenSSL to generate a widely-compatible certificate"

The first OpenSSL command generates a 2048-bit (recommended) RSA private key.

The second command generates a Certificate Signing Request, which you could instead use to generate a CA-signed certificate. This step will ask you questions; be as accurate as you like since you probably aren’t getting this signed by a CA.

The third command generates a self-signed x509 certificate suitable for use on web servers. This is the file you were after all along, congrats!

The check at the end ensures you will be able to use your certificate beyond 2016. OpenSSL on OS X is currently insufficient, and will silently generate a SHA-1 certificate that will be rejected by browsers in 2017. Update using your package manager, or with Homebrew on a Mac and start the process over.

More about self-signed SSL certificates

Self-signed SSL certificates provide all of the encryption benefits of a certificate signed by a Certificate Authority (CA), but essentially none of the authentication benefits. This is obviously still useful, and I find them particularly nice for staging sites, in the early stages of a project, and for use behind CloudFlare.

Due the the lack of authentication, web browsers will display a warning to users attempting to connect to your site. If this is a production site or you don’t want this warning, you must get a certificate signed by a CA. Google “free SSL certificate” and you’ll easily find a free 1-year certificate.

ECC certificates

While I would not recommend an ECC (elliptical curve) certificate, I have a guide to create a self-signed ECC certificate. ECC is a relatively new kind of key, and can be used as an alternative to RSA which we used above.

Traceback (most recent call last): File "/usr/lib/python3/dist-packages/urllib3/connectionpool.py", line 665, in urlopen httplib_response = self._make_request( ^^^^^^^^^^^^^^^^^^^ File "/usr/lib/python3/dist-packages/urllib3/connectionpool.py", line 376, in _make_request self._validate_conn(conn) File "/usr/lib/python3/dist-packages/urllib3/connectionpool.py", line 996, in _validate_conn conn.connect() File "/usr/lib/python3/dist-packages/urllib3/connection.py", line 366, in connect self.sock = ssl_wrap_socket( ^^^^^^^^^^^^^^^^ File "/usr/lib/python3/dist-packages/urllib3/util/ssl_.py", line 370, in ssl_wrap_socket return context.wrap_socket(sock, server_hostname=server_hostname) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/lib/python3.11/ssl.py", line 517, in wrap_socket return self.sslsocket_class._create( ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/lib/python3.11/ssl.py", line 1104, in _create self.do_handshake() File "/usr/lib/python3.11/ssl.py", line 1382, in do_handshake self._sslobj.do_handshake() ssl.SSLCertVerificationError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: self signed certificate in certificate chain (_ssl.c:1016) During handling of the above exception, another exception occurred: Traceback (most recent call last): File "/usr/lib/python3/dist-packages/requests/adapters.py", line 439, in send resp = conn.urlopen( ^^^^^^^^^^^^^ File "/usr/lib/python3/dist-packages/urllib3/connectionpool.py", line 747, in urlopen return self.urlopen( ^^^^^^^^^^^^^ File "/usr/lib/python3/dist-packages/urllib3/connectionpool.py", line 747, in urlopen return self.urlopen( ^^^^^^^^^^^^^ File "/usr/lib/python3/dist-packages/urllib3/connectionpool.py", line 747, in urlopen return self.urlopen( ^^^^^^^^^^^^^ [Previous line repeated 2 more times] File "/usr/lib/python3/dist-packages/urllib3/connectionpool.py", line 719, in urlopen retries = retries.increment( ^^^^^^^^^^^^^^^^^^ File "/usr/lib/python3/dist-packages/urllib3/util/retry.py", line 436, in increment raise MaxRetryError(_pool, url, error or ResponseError(cause)) urllib3.exceptions.MaxRetryError: HTTPSConnectionPool(host='marketplace.dify.ai', port=443): Max retries exceeded with url: /api/v1/plugins/download?unique_identifier=langgenius/openai:0.0.22 (Caused by SSLError(SSLCertVerificationError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: self signed certificate in certificate chain (_ssl.c:1016)'))) During handling of the above exception, another exception occurred: Traceback (most recent call last): File "/home/gwm002/code/code/test.py", line 20, in <module> response = session.get("https://marketplace.dify.ai/api/v1/plugins/download?unique_identifier=langgenius/openai:0.0.22", timeout=(10, 30)) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/lib/python3/dist-packages/requests/sessions.py", line 546, in get return self.request('GET', url, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/lib/python3/dist-packages/requests/sessions.py", line 533, in request resp = self.send(prep, **send_kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/lib/python3/dist-packages/requests/sessions.py", line 646, in send r = adapter.send(request, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/lib/python3/dist-packages/requests/adapters.py", line 514, in send raise SSLError(e, request=request) requests.exceptions.SSLError: HTTPSConnectionPool(host='marketplace.dify.ai', port=443): Max retries exceeded with url: /api/v1/plugins/download?unique_identifier=langgenius/openai:0.0.22 (Caused by SSLError(SSLCertVerificationError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: self signed certificate in certificate chain (_ssl.c:1016)')))
最新发布
05-14
### 自签名证书导致的SSL验证失败解决方案 在使用Python的`requests`库进行HTTPS请求时,如果目标服务器使用的是自签名证书(self-signed certificate),可能会遇到类似于以下错误信息的情况: `SSLError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: self signed certificate in certificate chain (_ssl.c:XXXX)`[^1]。 这种错误的根本原因是Python默认会对HTTPS连接执行严格的SSL/TLS证书验证,而自签名证书未被信任根证书颁发机构(CA)认证,因此会被视为不安全。 以下是几种常见的解决方法: --- #### 方法一:禁用SSL验证 可以通过将`verify=False`传递给`requests`函数来跳过SSL验证。这种方法简单快捷,但在生产环境中应谨慎使用,因为它会使通信变得不安全,容易受到中间人攻击(MitM)[^2]。 ```python import requests url = "https://example.com" response = requests.get(url, verify=False) print(response.text) ``` 尽管如此,仍需注意此方式仅适用于测试或内部网络环境下的临时解决方案。 --- #### 方法二:导入自签名证书至信任存储 另一种更为推荐的安全做法是从目标站点导出自签名证书,并将其添加到系统的受信CA列表中。这样可以让Python识别并接受该证书作为合法凭证的一部分[^3]。 具体步骤如下: 1. 使用浏览器访问目标网站; 2. 导出其提供的公钥证书文件(.crt/.pem格式); 3. 将获得的`.crt`文件转换成PEM编码形式(如果必要的话),命令如下所示: ```bash openssl x509 -inform DER -in example.crt -out example.pem ``` 4. 配置Python项目加载这个新的可信证书路径: ```python import os import requests cert_path = '/path/to/example.pem' # 替换为实际保存位置 response = requests.get('https://example.com', verify=cert_path) print(response.text) ``` 通过这种方式能够保持完整的安全性同时解决了原生拒绝问题。 --- #### 方法三:更新系统内置的信任链 有时可能是操作系统自带的基础信任库版本老旧造成兼容性缺失所致,则考虑升级对应组件即可恢复正常运作状态[^4]。 对于Linux发行版而言一般只需运行包管理器刷新软件源即可完成自动修补工作;而在Windows平台上则建议定期安装微软发布的累积更新补丁集以维持最新防护等级标准。 --- ### 总结说明 综上所述,针对由“self signed certificate in certificate chain”引起的SSL验证失败情形提供了三种可行应对策略。其中前两者适合快速部署调试场景需求,而后一种长期维护视角下显得尤为重要。最终选择何种途径取决于具体的业务背景和技术约束条件等因素综合考量决定。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值