您的位置:首页 > 运维架构

openssl实现私有CA

2016-06-06 10:52 260 查看

openssl实现私有CA

查看系统是否安装openssl
命令:rpm -q openssl
查看openssl安装后生成的目录
命令:rpm -ql openssl

一.CA的配置

1.建立CA目录结构

Openssl的默认配置建立CA,需要在文件系统中建立相应的目录结构,相关的配置内容红帽系统一般在etc/pki/tls/openssl.cnf文件中,如果不知道,使用rpm -ql openssl
查看openssl安装后生成的目录。
打开openssl.cnf文件后可看到CA配置默认项

dir= /etc/pki/CA# Where everything is kept (CA工作目录)certs= $dir/certs# Where the issued certs are kept(客户端证书存放目录)crl_dir= $dir/crl# Where the issued crl are kept(证书吊销目录)database= $dir/index.txt# database index file.(所发证书检索)#unique_subject= no# Set to 'no' to allow creation of# several ctificates with same subject.new_certs_dir= $dir/newcerts# default place for new certs. (刚生成证书存放目录)certificate= $dir/cacert.pem # The CA certificate (CA自己的证书)serial= $dir/serial # The current serial number(生成证书序列号)crlnumber= $dir/crlnumber# the current crl number(证书吊销序列号)# must be commented out to leave a V1 CRLcrl= $dir/crl.pem # The current CRL (证书吊销列表文件)private_key= $dir/private/cakey.pem# The private key(CA自己的私钥)RANDFILE= $dir/private/.rand# private random number file(随机数文件)x509_extensions= usr_cert# The extentions to add to the certdefault_days= 365# how long to certify for(证书默认有效期限)default_crl_days= 30# how long before next CRL(吊销证书存放时间)
[ req_distinguished_name ]countryName= Country Name (2 letter code)countryName_default= CN (默认国家代码)countryName_min= 2countryName_max= 2
stateOrProvinceName= State or Province Name (full name)#stateOrProvinceName_default= Henan (默认省)
localityName= Locality Name (eg, city)localityName_default= Zhengzhou (默认城市)
0.organizationName= Organization Name (eg, company)0.organizationName_default= Default Company Ltd
# we can do this but it is not needed normally :-)#1.organizationName= Second Organization Name (eg, company)#1.organizationName_default= World Wide Web Pty Ltd
organizationalUnitName= Organizational Unit Name (eg, section)#organizationalUnitName_default= (默认组织)
commonName= Common Name (eg, your name or your server\'s hostname) (默认主机名) commonName_max= 64
emailAddress= Email AddressemailAddress_max= 64 (默认邮箱)

注意:以上dir 目录使用的是绝对路径,相对路径有时候会有问题。确保以上目录存在。

要能够给别人发证,自己必须先有证。先生成一对秘钥,把公钥做成证书,再生成自签署的证书,所以步骤如下:
1.生成一对秘钥
2.生成自签署证书

二.生成一对秘钥

生成秘钥我们一般用的是rsa的叫genrsa,私钥文件一般不让别人访问的,所以私钥的权限一般是600。
命令:openssl genrsa 或者openssl genrsa 2048(秘钥长度,默认512)

1.使用输出重定向生成私钥要文件
CA私钥所在目录
private_key= $dir/private/cakey.pem# The private key(CA自己的私钥)
在CA私钥所在目录下使用如下命令生成CA自己的私钥
Openssl genrsa > cakey.pem
或者
Openssl genrsa -out cakey.pem 2048
2.修改private.key文件的权限
Chmod 600 cakey.pem
3从私钥中生成公钥
Openssl rsa -in private.key -pubout > public.key
(生成公钥并保存到public.key文件中)

三.生成自签署的证书

命令:req -new -x509 -key private.key -out my.crt -days 365 1. 生成证书请求:
Req -new -x509 -key private.key -out my.crt -days 365
(发出请求后需要填写生成证书的详细信息)
Country Name (2 letter code) [GB]: #所在国家State or Province Name (full name) [Berkshire]: #州或省名Locality Name (eg, city) [Newbury]: #所在城市的名字Organization Name (eg, company) [My Company Ltd]: #组织或公司的名字Organizational Unit Name (eg, section) []: #公司所在部门Common Name (eg, your name or your server's hostname) []: #服务器名字或个人名字(域名必须正确)Email Address []: #Email地址Please enter the following 'extra' attributesto be sent with your certificate requestA challenge password []: #加密证书请求的密码An optional company name []: #以上信息填写完毕,证书生成成功。
在CA自己的证书存放目录下 certificate= $dir/cacert.pem # The CA certificate (CA自己的证书) 执行如下命令生成CA自签证书 Openssl -req -new -x509 -key private/cakey.pem -out cacert.pem2.确保CA工作目录/etc/pki/CA下 certs crl newcerts private 目录存在
3.确保CA工作目录/etc/pki/CA下 index.txt serial 两个文件存在。
4.在serial 文件中写入证书开始生成的序列号 echo 01 > serial

CA建立完毕

四.使用私有CA签发证书

每一种应用都有自己的证书,两种服务尽可能不使用一样的证书。

1.为应用服务器生成私钥
Openssl genrsa -out httpd.key 1024

2. 生成申请

Openssl req -new -key http.key -out httpd.csr

........填写申请信息

3.向CA请求签署
Openssl ca -in httpd.csr -out httpd.crt -days 365
4.确认信息后生成http.crt证书。

可以到etc/pki/CA/index.txt文件中查看生成证书

注意:填写信息时国家,省份,城市,公司,一定要和CA的一样。

本文出自 “德泽无忧” 博客,谢绝转载!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: