ubuntu创建自签名证书

本文详细介绍在Ubuntu17.04系统中,如何生成RSA私钥,利用私钥创建CA证书,以及如何生成自签名证书的全过程。文章包括创建私钥、生成证书请求、使用CA证书签名等步骤。

系统版本:

Ubuntu 17.04。首先生成一个RSA私钥:

$
$ mkdir private
$  
$ openssl genrsa -out private/cakey.pem 2048
Generating RSA private key, 2048 bit long modulus
.......................................................................................................................................+++
.......................................................+++
e is 65537 (0x10001)
$ 

接下来,使用以上的私钥生成CA证书:

$ 
$ openssl req -new -x509 -key private/cakey.pem -out cacert.pem
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]:BJ
Locality Name (eg, city) []:BJ
Organization Name (eg, company) [Internet Widgits Pty Ltd]:Test
Organizational Unit Name (eg, section) []:Security
Common Name (e.g. server FQDN or YOUR name) []:Development
Email Address []:sec@test.com
$ 
$ 
$ ls
cacert.pem  private
$ 

可使用以下命令查看证书内容:

$ openssl x509 -in cacert.pem -noout -text
Certificate:
    Data:
        Version: 3 (0x2)
        Serial Number: 9410302713013285385 (0x82981c75a60d3209)
    Signature Algorithm: sha256WithRSAEncryption
        Issuer: C=CN, ST=BJ, L=BJ, O=Test, OU=Security, CN=Development/emailAddress=sec@test.com
        Validity
            Not Before: Nov 18 06:20:55 2019 GMT
            Not After : Dec 18 06:20:55 2019 GMT
        Subject: C=CN, ST=BJ, L=BJ, O=Test, OU=Security, CN=Development/emailAddress=sec@test.com
        Subject Public Key Info:
            Public Key Algorithm: rsaEncryption
		...

以下操作用于生成名称为test.crt的自签名证书。首先生成test的私钥:

$  
$ openssl genrsa -out test.key 2048     
Generating RSA private key, 2048 bit long modulus
....+++
......+++
e is 65537 (0x10001)
$ 

接下来,使用test的私钥生成证书请求:

$ 
$ openssl req -new -key test.key -out test.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]:NJ
Locality Name (eg, city) []:^C
$ openssl req -new -key test.key -out test.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]:BJ
Locality Name (eg, city) []:BJ
Organization Name (eg, company) [Internet Widgits Pty Ltd]:Test
Organizational Unit Name (eg, section) []:Security
Common Name (e.g. server FQDN or YOUR name) []:Development
Email Address []:sec@test.com

Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:111111
An optional company name []:
$ 
$ 
$ ls
cacert.pem  private  test.csr  test.key
$ 

创建自签名需要用到的目录demoCA/newcerts,以及文件index.txt和serial。

$ 
$ mkdir -p  demoCA/newcerts
$ 
$ touch demoCA/index.txt
$ 
$ echo 01 > demoCA/serial
$ 

以下对test的证书请求,使用开始时生成的CA证书和私钥进行签名,生成test的自签名证书test.crt。

$ 
$ openssl ca -in test.csr -cert cacert.pem -keyfile private/cakey.pem -days 365 -out test.crt
Using configuration from /usr/lib/ssl/openssl.cnf
Check that the request matches the signature
Signature ok
Certificate Details:
        Serial Number: 1 (0x1)
        Validity
            Not Before: Nov 18 06:25:34 2019 GMT
            Not After : Nov 17 06:25:34 2020 GMT
        Subject:
            countryName               = CN
            stateOrProvinceName       = BJ
            organizationName          = Test
            organizationalUnitName    = Security
            commonName                = Development
            emailAddress              = sec@test.com
        X509v3 extensions:
            X509v3 Basic Constraints: 
                CA:FALSE
            Netscape Comment: 
                OpenSSL Generated Certificate
            X509v3 Subject Key Identifier: 
                B7:AF:39:DC:E2:05:DB:05:DA:D2:90:53:A8:34:A0:77:FD:80:5D:08
            X509v3 Authority Key Identifier: 
                keyid:67:BF:09:FC:58:01:FC:D7:D4:9D:47:93:07:00:8C:DF:FF:BE:36:B9

Certificate is to be certified until Nov 17 06:25:34 2020 GMT (365 days)
Sign the certificate? [y/n]:y


1 out of 1 certificate requests certified, commit? [y/n]y
Write out database with 1 new entries
Data Base Updated
$ 
$ 
$ ls
cacert.pem  demoCA  private  test.crt  test.csr  test.key
$ 

最后以及test的私钥生成test的公钥:

$ 
$ openssl rsa -in test.key -pubout -out test.pub.key    
writing RSA key
$ 

END

### 如何生成自签名SSL证书 #### 方法概述 为了在本地环境或测试环境中启用 HTTPS 功能,可以通过 OpenSSL 工具生成自签名 SSL 证书。这种方法无需支付费用给 CA(证书颁发机构),适用于开发和测试场景。 --- #### 步骤说明 1. **安装 OpenSSL** 如果尚未安装 OpenSSL,则需要先完成安装。对于 Windows 用户,可以从官方网站下载并配置 OpenSSL;而对于 Linux 系统(如 CentOS 或 Ubuntu),可以直接通过包管理器安装。 对于 CentOS 7,运行以下命令安装 OpenSSL: ```bash sudo yum install openssl ``` 2. **生成私钥** 使用 OpenSSL 命令生成一个 RSA 私钥文件。此文件应妥善保管,切勿泄露。 ```bash openssl genpkey -algorithm RSA -out private.key -aes256 ``` 这里 `-aes256` 参数表示对私钥进行 AES-256 加密保护[^1]。 3. **创建证书签署请求 (CSR)** 接下来生成 CSR 文件,其中包含有关您的身份的信息以及公钥。 ```bash openssl req -new -key private.key -out certificate.csr ``` 执行该命令后会提示输入一些信息,例如国家、省份、城市、组织名称等。这些字段可以根据实际需求填写。 4. **生成自签名证书** 利用之前生成的私钥和 CSR 来创建自签名证书。默认情况下有效期为一年,也可以指定更长时间。 ```bash openssl x509 -req -days 365 -in certificate.csr -signkey private.key -out selfsigned.crt ``` 上述命令中的 `selfsigned.crt` 即为目标自签名证书文件[^2]。 5. **验证证书** 可以使用如下命令查看新生成的证书详情: ```bash openssl x509 -in selfsigned.crt -text -noout ``` 6. **部署到 Web 服务器** 将生成好的 `.crt` 和 `.key` 文件复制至目标服务器目录,并按照所使用的 HTTP(S) 软件文档指引加载它们。比如,在 Nginx 中需修改站点配置文件加入 ssl_certificate 和 ssl_certificate_key 指向上述两个文件路径。 --- #### 注意事项 尽管自签名证书能够满足基本的安全传输需求,但由于未经过权威认证中心签发,在浏览器访问时可能会触发警告消息。这是正常现象,仅影响用户体验而不妨碍功能实现。 ```python import os def generate_ssl_cert(): """模拟生成自签名SSL证书过程""" key_command = 'openssl genpkey -algorithm RSA -out private.key -aes256' csr_command = 'openssl req -new -key private.key -out certificate.csr' cert_command = 'openssl x509 -req -days 365 -in certificate.csr -signkey private.key -out selfsigned.crt' try: os.system(key_command) os.system(csr_command) os.system(cert_command) print("Self-signed SSL Certificate generated successfully.") except Exception as e: print(f"Error during generation: {e}") generate_ssl_cert() ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值