使用OpenSSL自建CA及颁发证书、吊销证书

本文详细介绍了如何使用OpenSSL自建CA,包括创建私有CA服务器、颁发证书以及吊销证书的步骤。从生成私钥、自签名证书,到在需要证书的主机上生成私钥和证书请求,再到CA签署并颁发证书,最后讨论了吊销证书的过程,涉及到证书的serial和subject信息的验证。此外,还解析了CA生成自签名证书的命令和CA的配置文件详情。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、实验说明

       OpenSSL 是一个安全套接字层密码库,囊括主要的密码算法、常用的密钥和证书封装管理功能及SSL协议,并提供丰富的应用程序供测试或其它目的使用。
       OpenSSL是一个开源程序的套件、这个套件有三个部分组成:一是libcryto,这是一个具有通用功能的加密库,里面实现了众多的加密库;二是libssl,这个是实现ssl机制的,它是用于实现TLS/SSL的功能;三是openssl,是个多功能命令行工具,它可以实现加密解密,甚至还可以当CA来用,可以让你创建证书、吊销证书。

二、实验环境

    Centos 6.9 x86_64位(申请签名机器)、Centos 7.3 x86_64位(自建CA的机器)、VMware workstaton 12.

三、实验正文

1、查看自建CA的主机是否安装OpenSSL

[root@centos7 ~]# rpm -qa openssl  # 查看openssl是否安装
openssl-1.0.1e-60.el7.x86_64
[root@centos7 ~]# rpm -ql openssl  # 列出openssl安装包下有哪些文件,等下会用到下面的一些目录
/etc/pki/CA
/etc/pki/CA/certs 
/etc/pki/CA/crl    # 吊销的证书存放目录
/etc/pki/CA/newcerts # 存放CA签署(颁发)过的数字证书(证书备份目录)
/etc/pki/CA/private  # 用于存放CA的私钥
/etc/pki/tls/certs/Makefile
/etc/pki/tls/certs/make-dummy-cert
/etc/pki/tls/certs/renew-dummy-cert
/etc/pki/tls/misc/CA
/etc/pki/tls/misc/c_hash
/etc/pki/tls/misc/c_info
/etc/pki/tls/misc/c_issuer
/etc/pki/tls/misc/c_name
/usr/bin/openssl
...(以下省略)...
[root@centos7 ~]# yum install openssl -y  # 若没有安装使用此条命令安装

2、创建私有CA服务器

a、创建所需要的文件,只有第一次使用CA时才需要
[root@centos7 ~]# touch /etc/pki/CA/index.txt   # 生成证书索引数据库
[root@centos7 ~]# echo 01 > /etc/pki/CA/serial  # 指定第一个颁发证书的序列号
b、CA生成私钥
[root@centos7 ~]# cd /etc/pki/CA/  # 切换至此目录
[root@centos7 CA]# (umask 006; openssl genrsa -out /etc/pki/CA/private/cakey.pem 2048)  # 生成私钥
Generating RSA private key, 2048 bit long modulus  
.........+++
......................................+++
e is 65537 (0x10001)
[root@centos7 CA]# ls -l private/cakey.pem 
-rw-rw----. 1 root root 1675 Jul 17 17:22 cakey.pem
c、CA生成自签名证书
[root@centos7 CA]# openssl req -new -x509 -key /etc/pki/CA/private/cakey.pem -days \3650 -out /etc/pki/CA/cacert.pem   # CA生成自签名
...(中间省略)...
-----
Country Name (2 letter code) [XX]:CN
State or Province Name (full name) []:Beijing
Locality Name (eg, city) [Default City]:Beijing
Organization Name (eg, company) [Default Company Ltd]:Hxt
Organizational Unit Name (eg, section) []:Ops
Common Name (eg, your name or your server's hostname) []:www.hengxia.top
Email Address []:miouqi@qq.com
[root@centos7 CA]# ls -l
total 12
-rw-r--r--. 1 root root 1403 Jul 17 17:59 cacert.pem
drwxr-xr-x. 2 root root    6 Nov  6  2016 certs
drwxr-xr-x. 2 root root    6 Nov  6  2016 crl
-rw-r--r--. 1 root root    0 Jul 17 16:47 index.txt
drwxr-xr-x. 2 root root    6 Nov  6  2016 newcerts
drwx------. 2 root root   23 Jul 17 17:22 private
-rw-rw----. 1 root root 1675 Jul 17 17:18 privatecakey.pem
-rw-r--r--. 1 root root    3 Jul 17 16:48 serial

3、颁发证书

a、在需要使用证书的主机上给web服务器生成私钥
[root@centos6 ~]# (umask 066; openssl genrsa -out /etc/pki/tls/ private/test.key 2048)
Generating RSA private key, 2048 bit long modulus
.........+++
................................................+++
e is 65537 (0x10001)
b、在需要使用证书的主机上给web服务器生成证书请求
[root@centos6 ~]# openssl req -new -key /etc/pki/tls/private/test.key   -days 365 -out /etc/pki/tls/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) [XX]:CN  # 默认国家要与CA一致
State or Province Name (full name) []:Beijing  # 默认省要与CA一致
Locality Name (eg, city) [Default City]:Beijing 
Organization Name (eg, company) [Default Company Ltd]:Hxt # 公司名称默认要与CA一致
Organizational Unit Name (eg, section) []:Ops 
Common Name (eg, your name or your server's hostname) []:*.testweb.com
Email Address []:test@qq.com              

Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:
An optional company name []:
c、将证书文件传输给CA
[root@centos6 ~]# scp /etc/pki/tls/test.csr 172.16.251.124:/tmp
The authenticity of host '172.16.251.124 (172.16.251.124)' can't be established.
RSA key fingerprint is 8e:d7:ac:fd:71:70:22:e7:ff:98:ed:61:96:85:5f:b7.
Are you sure you want to continue connecting (yes/no)? yes 
Warning: Permanently added '172.16.251.124' (RSA) to the list of known hosts.
root@172.16.251.124's password: 
test.csr                                          100% 1050     1.0KB/s   00:00 
d、CA签署证书,并将证书颁发给请求者
[root@centos7 ~]# openssl ca -in  /tmp/test.csr -out /etc/pki/CA/certs/test.crt -days 365
Using configuration from /etc/pki/tls/openssl.cnf
Check that the request matches the signature
Signature ok
Certificate Details:
        Serial Number: 1 (0x1)
        Validity
           
以下是在Windows上配置IKEv2协议的Strongswan 5.3.5的步骤: 1. 安装Strongswan 5.3.5: 下载并安装Strongswan 5.3.5。Strongswan 5.3.5支持IKEv2协议,可以从Strongswan官网或者其他可靠的下载网站下载。 2. 生成证书和密钥: 在Strongswan服务器上生成CA证书和服务器证书和密钥。这可以使用openssl工具完成。将根证书和服务器证书和密钥导出到PFX格式,以便在Windows上使用。 3. 在Windows上导入证书和密钥: 双击PFX文件,按照向导完成导入过程。确保将证书和私钥都导入到计算机的“个人”证书存储中。 4. 配置Strongswan服务器: 在Strongswan服务器上,编辑/etc/ipsec.conf文件,添加以下内容: ``` conn ikev2 left=%any leftid=@your_server_ip_or_domain_name leftcert=server-cert.pem leftsendcert=always leftsubnet=0.0.0.0/0 right=%any rightid=%any rightauth=eap-mschapv2 rightsourceip=10.10.10.0/24 rightsendcert=never eap_identity=%identity auto=add ``` 其中,your_server_ip_or_domain_name应该替换为Strongswan服务器的IP地址或域名。 5. 配置Strongswan服务器的EAP: 在Strongswan服务器上,编辑/etc/ipsec.secrets文件,添加以下内容: ``` : RSA server-key.pem your_username : EAP "your_password" ``` 其中,your_username和your_password应该替换为你要使用的用户名和密码。 6. 在Windows上配置网络连接: 在Windows上,打开“网络和共享中心”,然后点击“设置新的连接或网络”。选择“连接到工作区”并按照向导继续。选择“使用我的Internet连接(VPN)”,输入Strongswan服务器的IP地址或域名,然后点击“创建”。 7. 配置强制加密: 在Windows上,打开“网络和共享中心”,然后点击“更改适配器设置”。右键单击Strongswan VPN连接并选择“属性”。在“安全”选项卡上,选择IKEv2协议并启用“加密”。点击“高级设置”,选择“使用预共享密钥”,并输入Strongswan服务器上的预共享密钥。 8. 连接: 在Windows上,点击Strongswan VPN连接并输入你的用户名和密码。点击“连接”以连接到Strongswan服务器。 以上是在Windows上配置IKEv2协议的Strongswan 5.3.5的步骤。如果你遇到任何问题,请查看Strongswan文档或在Strongswan社区寻求帮助。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值