转载

ca证书流程


出处:Blog.ChinaUnix.net
OSW:7.认证中心


  海龙讲PKI时,以算法和体系讲得很细,可是没讲怎么搭建。在一次讲座后去问另一个人,好象也没问出个结果。郝大说他自己搭了个认证服务器,很得意,又见他在研究应用密码学,便猜这东东一定不会容易。想向他打听吧,又怕需要的背景知识太多,只好作罢。


  有文说用OpenSSL可以做,过几天试试去。根据文章指点,step by step,:)


  目录
----------------------------



  1. 用户申请证书

  2. 服务方自频证书(只能一次)

  3. 服务方频发证书

  4. 用户使用证书

-----------------------------

  一些假定:我叫aliali,我有一朋友叫boxbox,当前目录是$home/candel。


  用户申请证书 top


1、生成私钥


$openssl genrsa -des3 -out aliali.key 1024


显示
Generating RSA private key, 1024 bit long modulus
.......++++++
.++++++
e is 65537 (0x10001)
Enter PEM pass phrase:
Verifying password - Enter PEM pass phrase:


生成名为aliali.key的一个文件,内容如下:
-----BEGIN RSA PRIVATE KEY-----
Proc-Type: 4,ENCRYPTED
DEK-Info: DES-EDE3-CBC,BB03146A74BE076E


HwsUT6Slujin4b7p7X3KLCxUSRMgK0WVHU1zRJwV+LeQQYOvW4YOvl1w1TJVuwtx
............
I5GbgPGfP7tb0dHKx6SMv/EOQitkx98SpPQlKwGvhimcUqg0QM+UJw==
-----END RSA PRIVATE KEY-----


  可以用下面的指令查看私钥内容
$openssl rsa -noout -text -in aliali.key
显示:
read RSA key
Enter PEM pass phrase:
Private-Key: (1024 bit)
modulus:
    00:d1:bd:28:78:e7:bb:17:c0:44:97:ec:43:b0:48:
.........
    6c:02:6c:35:fc:c0:97:16:eb
publicExponent: 65537 (0x10001)
privateExponent:
    22:c1:61:af:87:56:ed:7c:a2:e1:39:f9:8d:80:a9:
..........
    06:bd:e4:da:81:39:61:c1
prime1:
    00:f9:e4:ca:a5:3a:b2:da:27:69:e0:47:d3:80:e4:
..........
    08:ab:3c:fc:33
prime2:
    00:d6:dd:2d:a6:99:b6:11:2b:84:76:e1:af:eb:c0:
...........
    80:e7:b7:c2:69
exponent1:
    00:ef:6a:61:30:5e:85:9e:76:e6:7c:47:b4:7e:a8:
...........
    e6:b1:50:d3:7b
exponent2:
    00:a5:26:0a:01:a4:e2:09:5e:7e:89:75:bc:e7:23:
...........
    63:c7:37:3a:09
coefficient:
    00:c7:e0:04:e3:83:d7:9d:aa:55:d4:b2:ae:da:2b:
............
    4e:4f:64:f0:d4


  用下列命令可生成不需要口令访问的私钥
$openssl rsa -in tom.key -out tom.key.unsecure
显示,不知道,没试。


  2、用如上的私钥生成一个证书签名申请(CSR):
$openssl req -new -key aliali.key -out aliali.csr
显示:
Using configuration from /usr/share/ssl/openssl.cnf
Enter PEM pass phrase:
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]:Beijing
Locality Name (eg, city) []:
Organization Name (eg, company) [Internet Widgits Pty Ltd]:aliali.com
Organizational Unit Name (eg, section) []:management center of network
Common Name (eg, your name or your server's hostname) []:aliali
Email Address []:hitonyang@yahoo.com.cn


Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:alia   
An optional company name []:istic


生成文件aliali.csr,内容如下:
-----BEGIN CERTIFICATE REQUEST-----
MIIB/zCCAWgCAQAwgZMxCzAJBgNVBAYTAkNOMRAwDgYDVQQIEwdCZWlqaW5nMRMw
...........
r6djmppuuS5beTZNASdzpC8FSEokg66a9Xgfz4K2joX/gK0=
-----END CERTIFICATE REQUEST-----


用$openssl req -noout -text -in aliali.csr看请求内容,显示如下:
Using configuration from /usr/share/ssl/openssl.cnf
Certificate Request:
    Data:
        Version: 0 (0x0)
        Subject: C=CN, ST=Beijing, O=aliali.com, OU=management center of network, CN=aliali/Email=hitonyang@yahoo.com.cn
        Subject Public Key Info:
            Public Key Algorithm: rsaEncryption
            RSA Public Key: (1024 bit)
                Modulus (1024 bit):
                    00:d1:bd:28:78:e7:bb:17:c0:44:97:ec:43:b0:48:
................
                    6c:02:6c:35:fc:c0:97:16:eb
                Exponent: 65537 (0x10001)
        Attributes:
            challengePassword        :alia
            unstructuredName         :istic
    Signature Algorithm: md5WithRSAEncryption
        a0:af:9f:9a:6e:48:a9:12:b5:ed:db:3a:8a:ec:ae:8f:cb:48:
.............


        80:ad


  服务方在发证书需要有自己的key和crt,如果没有则要生成 top
  生成服务方的私钥
$ openssl genrsa -des3 -out ca.key 1024
显示
Generating RSA private key, 1024 bit long modulus
.............................................++++++
...++++++
e is 65537 (0x10001)
Enter PEM pass phrase:
Verifying password - Enter PEM pass phrase:


  生成CA的x509自签名
$ openssl req -new -x509 -days 365 -key ca.key -out ca.crt 
显示
Using configuration from /usr/share/ssl/openssl.cnf
Enter PEM pass phrase:
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]:Beijing
Locality Name (eg, city) []:
Organization Name (eg, company) [Internet Widgits Pty Ltd]:ISTIC
Organizational Unit Name (eg, section) []:
Common Name (eg, your name or your server's hostname) []:
Email Address []:
  
  服务方颁发证书 top
$sign.sh aliali.csr
显示
CA signing: aliali.csr -> aliali.crt:
Using configuration from ca.config
Enter PEM pass phrase:
Check that the request matches the signature
Signature ok
The Subjects Distinguished Name is as follows
countryName            :P RINTABLE:'CN'
stateOrProvinceName    :P RINTABLE:'Beijing'
organizationName       :P RINTABLE:'aliali.com'
organizationalUnitName :P RINTABLE:'management center of network'
commonName             :P RINTABLE:'aliali'
emailAddress          :IA5STRING:'hitonyang@yahoo.com.cn'
Certificate is to be certified until Jan  2 10:22:08 2009 GMT (2002 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
CA verifying: aliali.crt <-> CA cert
aliali.crt: OK


所用的sign.sh可以从这下载
  OK!aliali的证书aliali.crt终于生成了,内容如下:
Certificate:
    Data:
        Version: 1 (0x0)
        Serial Number: 1 (0x1)
        Signature Algorithm: md5WithRSAEncryption
        Issuer: C=CN, ST=Beijing, O=ISTIC
        Validity
            Not Before: Jul 11 10:22:08 2003 GMT
            Not After : Jan  2 10:22:08 2009 GMT
        Subject: C=CN, ST=Beijing, O=aliali.com, OU=management center of network, CN=aliali/Email=hitonyang@yahoo.com.cn
        Subject Public Key Info:
            Public Key Algorithm: rsaEncryption
            RSA Public Key: (1024 bit)
                Modulus (1024 bit):
                    00:d1:bd:28:78:e7:bb:17:c0:44:97:ec:43:b0:48:
                    .............
                    6c:02:6c:35:fc:c0:97:16:eb
                Exponent: 65537 (0x10001)
    Signature Algorithm: md5WithRSAEncryption
        2a:2f:80:22:24:94:94:c5:c8:ec:f8:b4:5f:1c:89:e3:d9:5e:
        ....................
        91:7b
-----BEGIN CERTIFICATE-----
MIICMjCCAZsCAQEwDQYJKoZIhvcNAQEEBQAwLzELMAkGA1UEBhMCQ04xEDAOBgNV
.............................
tCMO03/DkL1dBTnwJdS5U7FONj3OW4cW0I1Sn84IbHMCL8u5kXs=
-----END CERTIFICATE-----



  不要修改"-----BEGIN CERTIFICATE-----"和"-----END CERTIFICATE-----"及两者中间的内容,其它内容可以去掉。


  对于那些不能识别PEM格式证书的程序,可以用下面的指令生成DER格式的证书:
$openssl x509 -in aliali.crt -out aliali.der -outform DER


  用户使用证书 top
  前期准备:按aliali的历程生成boxbox.crt,然后:
$mkdir usage
$cd usage
$mkdir ali
$mkdir box
$cd ali
$cp ../../*.crt .
$cp ../../aliali.key .
$echo "Hello World" >original.txt


$cd ../box
$cp ../../*.crt .
$cp ../../boxbox.key .


  1、数字签名
$cd ../ali
$ln -s original.txt txt_need_sign.txt
$openssl smime -sign -in txt_need_sign.txt -out txt_need_sign.2box.fromali.sign -inkey aliali.key -signer aliali.crt
$cp txt_need_sign.2box.fromali.sign ../box/txt_need_verify_sign.fromali.sign
  2、验证签名
$cd ../box
$openssl smime -verify -out org.txt2 -in txt_need_verify_sign.fromali.sign -certfile aliali.crt -CAfile ca.crt
显示:
Verification Successful


  3、数字信封(加密)
$cd ../ali
$ln -s original.txt txt_need_enc.txt
$openssl smime -encrypt -in txt_need_enc.txt -out txt_need_enc.4box.fromali.enc boxbox.crt
$cp txt_need_enc.4box.fromali.enc ../box/txt_need_dec.fromali.enc


  4、数字信封(解密)
$cd ../box
$openssl smime -decrypt -in txt_need_dec.fromali.enc -out org.txt3 -inkey boxbox.key -recip boxbox.crt


  5、签名后加密
$cd ../ali
$ln -s original.txt txt_need_sign_enc.txt
$openssl smime -sign -in txt_need_sign_enc.txt -out txt_signedbyali_need_enc.sign -inkey aliali.key -signer aliali.crt
$openssl smime -encrypt -in txt_signedbyali_need_enc.sign -out txt_signbyali_need_dec.4box.fromali.sign.enc boxbox.crt
$cp txt_signbyali_need_dec.4box.fromali.sign.enc ../box/txt_singed_need_dec.fromali.sign.enc


  6、生成一个包含私钥的数字证书  
  包含私角的数字证书用于证书导入、导出和保存,这样的证书标准为pcks12.
$openssl pkcs12 -export -in aliali.crt -out aliali.p12 -inkey aliali.key -name "ali's key and certificate"
显示:
Enter PEM pass phrase:
Enter Export Password:
Verifying password - Enter Export Password:

openssl简明使用手册

文章出处http://hi.baidu.com/52hack
简要介绍了使用openssl来生成CA证书、申请证书、颁发证书以及撤销证书的过程
1. 首先建立CA密钥:
openssl genrsa -des3 -out ca.key 1024 (创建密钥)
chmod 400 ca.key (修改权限为仅root能访问)
openssl rsa -noout -text -in ca.key (查看创建的证书)
2. 利用CA密钥自签署CA证书:
openssl req -new -x509 -days 3650 -key ca.key -out ca.crt
chmod 400 ca.crt (修改权限为仅root能访问)
openssl x509 -noout -text -in ca.crt (查看创建的证书)
3. 创建服务器证书签署申请:
openssl genrsa -des3 -out client.key 1024
chmod 400 server.key (修改权限为仅root能访问)
openssl rsa -noout -text -in client.key (查看创建的证书)
4. 利用证书签署申请生成请求:
openssl req -new -key client.key -out client.csr
openssl req -noout -text -in client.csr (查看创建的请求)
5.进行证书签署:
这时候需要先设置一下openssl的配置文件。
modify /usr/share/ssl/openssl.cnf并根据这个配置文件创建相应的目录和文件。
在创建了serial文件之后,还需要添加当前的八进制的serial number,如:01
然后执行:
openssl ca -keyfile ca.key -cert ca.crt -in client.csr -out client.pem
6.证书撤销:
openssl ca -keyfile ca.key -cert ca.crt -revoke client.pem
这时数据库被更新证书被标记上撤销的标志,需要生成新的证书撤销列表:
openssl ca -gencrl -keyfile ca.key -cert ca.crt -out crl/test.crl
查看证书撤销列表:
openssl crl -noout -text -in crl/test.crl
证书撤销列表文件要在WEB站点上可以使用,必须将crldays或crlhours和crlexts加到证书中:
openssl ca -gencrl -config /etc/openssl.cnf -crldays 7 -crlexts crl_ext -out crl/sopac-ca.crl

OpenVPN配置文件详解

OpenVPN配置文件详解

2009-06-24 16:24






Server使用的配置文件server.conf
—————————–
#申明本机使用的IP地址,也可以不说明
;local a.b.c.d
#申明使用的端口,默认1194
port 1194
#申明使用的协议,默认使用UDP,如果使用HTTP proxy,必须使用TCP协议
;proto tcp
proto udp
#申明使用的设备可选tap和tun,tap是二层设备,支持链路层协议。
#tun是ip层的点对点协议,限制稍微多一些,本人习惯使用TAP设备
dev tap
;dev tun
#OpenVPN使用的ROOT CA,使用build-ca生成的,用于验证客户是证书是否合法
ca ca.crt
#Server使用的证书文件
cert server.crt
#Server使用的证书对应的key,注意文件的权限,防止被盗
key server.key # This file should be kept secret
#CRL文件的申明,被吊销的证书链,这些证书将无法登录
crl-verify vpncrl.pem
#上面提到的生成的Diffie-Hellman文件
dh dh1024.pem
#这是一条命令的合集,如果你是OpenVPN的老用户,就知道这条命令的来由
#这条命令等效于:
# mode server #OpenVPN工作在Server模式,可以支持多client同时动态接入
# tls-server #使用TLS加密传输,本端为Server,Client端为tls-client
#
# if dev tun: #如果使用tun设备,等效于以下配置
# ifconfig 10.8.0.1 10.8.0.2 #设置本地tun设备的地址
# ifconfig-pool 10.8.0.4 10.8.0.251 #说明OpenVPN使用的地址池(用于分配给客户),分别是起始地址、结束地址
# route 10.8.0.0 255.255.255.0 #增加一条静态路由,省略下一跳地址,下一跳为对端地址,这里是: 10.8.0.2
# if client-to-client: #如果使用client-to-client这个选项
# push “route 10.8.0.0 255.255.255.0″ #把这条路由发送给客户端,客户连接成功后自动加入路由表,省略了下一跳地址: 10.8.0.1
# else
# push “route 10.8.0.1″ #否则发送本条路由,这是一个主机路由,省略了子网掩码和下一跳地址,分别为: 255.255.255.255 10.8.0.1
#
# if dev tap: #如果使用tap设备,则等效于以下命令
# ifconfig 10.8.0.1 255.255.255.0 #配置tap设备的地址
# ifconfig-pool 10.8.0.2 10.8.0.254 255.255.255.0 #客户端使用的地址池,分别是起始地址、结束地址、子网掩码
# push “route-gateway 10.8.0.1″ #把环境变量route-gateway传递给客户机
#
server 10.8.0.0 255.255.255.0 #等效于以上命令
#用于记录某个Client获得的IP地址,类似于dhcpd.lease文件,
#防止openvpn重新启动后“忘记”Client曾经使用过的IP地址
ifconfig-pool-persist ipp.txt
#Bridge状态下类似DHCPD的配置,为客户分配地址,由于这里工作在路由模式,所以不使用
;server-bridge 10.8.0.4 255.255.255.0 10.8.0.50 10.8.0.100
#通过VPN Server往Client push路由,client通过pull指令获得Server push的所有选项并应用
;push “route 192.168.10.0 255.255.255.0″
;push “route 192.168.20.0 255.255.255.0″
#VPN启动后,在VPN Server上增加的路由,VPN停止后自动删除
;route 10.9.0.0 255.255.255.252
#Run script or shell command cmd to validate client
#virtual addresses or routes. 具体查看manual
;learn-address ./script
#其他的一些需要PUSH给Client的选项
#
#使Client的默认网关指向VPN,让Client的所有Traffic都通过VPN走
;push “redirect-gateway”
#DHCP的一些选项,具体查看Manual
;push “dhcp-option DNS 10.8.0.1″
;push “dhcp-option WINS 10.8.0.1″
#如果可以让VPN Client之间相互访问直接通过openvpn程序转发,
#不用发送到tun或者tap设备后重新转发,优化Client to Client的访问效率
client-to-client
#如果Client使用的CA的Common Name有重复了,或者说客户都使用相同的CA
#和keys连接VPN,一定要打开这个选项,否则只允许一个人连接VPN
;duplicate-cn
#NAT后面使用VPN,如果VPN长时间不通信,NAT Session可能会失效,
#导致VPN连接丢失,为防止之类事情的发生,keepalive提供一个类似于ping的机制,
#下面表示每10秒通过VPN的Control通道ping对方,如果连续120秒无法ping通,
#认为连接丢失,并重新启动VPN,重新连接
#(对于mode server模式下的openvpn不会重新连接)。
keepalive 10 120
#上面提到的HMAC防火墙,防止DOS攻击,对于所有的控制信息,都使用HMAC signature,
#没有HMAC signature的控制信息不予处理,注意server端后面的数字肯定使用0,client使用1
tls-auth ta.key 0 # This file is secret
#对数据进行压缩,注意Server和Client一致
comp-lzo
#定义最大连接数
;max-clients 100
#定义运行openvpn的用户
user nobody
group nobody
#通过keepalive检测超时后,重新启动VPN,不重新读取keys,保留第一次使用的keys
persist-key
#通过keepalive检测超时后,重新启动VPN,一直保持tun或者tap设备是linkup的,
#否则网络连接会先linkdown然后linkup
persist-tun
#定期把openvpn的一些状态信息写到文件中,以便自己写程序计费或者进行其他操作
status openvpn-status.log
#记录日志,每次重新启动openvpn后删除原有的log信息
log /var/log/openvpn.log
#和log一致,每次重新启动openvpn后保留原有的log信息,新信息追加到文件最后
;log-append openvpn.log
#相当于debug level,具体查看manual
verb 3
——————————-
把server.conf文件保存到/etc/opennvpn目录中,并把使用easy-rsa下的脚本什成的key都复制到/etc/openvpn目录下,命令如下:
#cd /etc/openvpn
#cp easy-rsa/keys/ca.crt .
#cp easy-rsa/keys/server.crt .
#cp easy-rsa/keys/server.key .
#cp easy-rsa/keys/dh1024.pem .
#cp easy-rsa/keys/ta.key .
#cp easy-rsa/keys/vpncrl.pem .
创建OpenVPN启动脚本,可以在源代码目录中找到,在sample-scripts目录下的openvpn.init文件,将其复制到/etc/init.d/目录中,改名为openvpn
然后运行:
#chkconfig –add openvpn
#chkconfig openvpn on
立即启动openenvpn
#/etc/init.d/openvpn start


接下来配置客户端的配置文件client.conf:
Linux或Unix下使用扩展名为.conf Windows下使用的是.ovpn,并把需要使用的keys复制到配置文件所在目录ca.crt elm.crt elm.key ta.key
———————————-
# 申明我们是一个client,配置从server端pull过来,如IP地址,路由信息之类“Server使用push指令push过来的”
client


#指定接口的类型,严格和Server端一致
dev tap
;dev tun


# Windows needs the TAP-Win32 adapter name
# from the Network Connections panel
# if you have more than one. On XP SP2,
# you may need to disable the firewall
# for the TAP adapter.
;dev-node MyTap


# 使用的协议,与Server严格一致
;proto tcp
proto udp


#设置Server的IP地址和端口,如果有多台机器做负载均衡,可以多次出现remote关键字


remote 61.1.1.2 1194
;remote my-server-2 1194


# 随机选择一个Server连接,否则按照顺序从上到下依次连接
;remote-random


# 始终重新解析Server的IP地址(如果remote后面跟的是域名),
# 保证Server IP地址是动态的使用DDNS动态更新DNS后,Client在自动重新连接时重新解析Server的IP地址
# 这样无需人为重新启动,即可重新接入VPN
resolv-retry infinite


# 在本机不邦定任何端口监瑞脑消金兽听incoming数据,Client无需此操作,除非一对一的VPN有必要
nobind


# 运行openvpn用户的身份,旧版本在win下需要把这两行注释掉,新版本无需此操作
user nobody
group nobody


#在Client端增加路由,使得所有访问内网的流量都经过VPN出去
#当然也可以在Server的配置文件里头设置,Server配置里头使用的命令是
# push “route 192.168.0.0 255.255.255.0″
route 192.168.0.0 255.255.0.0


# 和Server配置上的功能一样如果使用了chroot或者su功能,最好打开下面2个选项,防止重新启动后找不到keys文件,或者nobody用户没有权限启动tun设备
persist-key
persist-tun


# 如果你使用HTTP代理连接VPN Server,把Proxy的IP地址和端口写到下面
# 如果代理需要验证,使用http-proxy server port [authfile] [auth-method]
# 其中authfile是一个2行的文本文件,用户名和密码各占一行,auth-method可以省略,详细信息查看Manual
;http-proxy-retry # retry on connection failures
;http-proxy [proxy server] [proxy port #]


# 对于无线设备使用VPN的配置,看看就明白了
# Wireless networks often produce a lot
# of duplicate packets. Set this flag
# to silence duplicate packet warnings.
;mute-replay-warnings


# Root CA 文件的文件名,用于验证Server CA证书合法性,通过easy-rsa/build-ca生成的ca.crt,和Server配置里的ca.crt是同一个文件
ca ca.crt
# easy-rsa/build-key生成的key pair文件,上面生成key部分中有提到,不同客户使用不同的keys修改以下两行配置并使用他们的keys即可。
cert elm.crt
key elm.key


# Server使用build-key-server脚本什成的,在x509 v3扩展中加入了ns-cert-type选项
# 防止VPN client使用他们的keys + DNS hack欺骗vpn client连接他们假冒的VPN Server
# 因为他们的CA里没有这个扩展
ns-cert-type server


# 和Server配置里一致,ta.key也一致,注意最后参数使用的是1
tls-auth ta.key 1


# 压缩选项,和Server严格一致
comp-lzo


# Set log file verbosity.
verb 4

原始文章地址

http://hi.baidu.com/chenyun2011/blog/item/87ab0112936c9c0b5baf53e3.html