证书介绍及openssl生成证书和吊销列表

转自:http://www.mamicode.com/info-detail-1082882.html

一,先来讲讲基本概念。

证书分类:

按类型可以分为CA证书和用户用户证书,我们我说的root也是特殊的CA证书。

用户证书又可以根据用途分类,放在服务器端的称为服务器证书,放在客户端一般称为客户端证书(这种说法不是很准确,只是一种理解。实际应该是在对客户端认证时才会用到客户端证书且rootca证书都可以放在客户端),记住,这两种证书都应为用户证书。

一般可以理解为证书由key和证书(没有key的文件也称为证书)组成,谁拥有这两个东西才真正拥有这个证书。好比锁是有钥匙和锁头组成的,你得两都有。你只有锁头锁住东西,却没有钥匙打开,也没什么用。

如果你对证书不了解,那一定要知道证书这三点作用(纯个人认为比较重要三点):

1,签名:通过签名技术可以保证证书拥有者的唯一性,而且所有信息没有被篡改。想了解数字签名的自己百度一下。

2,提供公钥:通过签名技术知道证书拥有者是A,且所有信息都是A,就可以拿到A的公钥算法及公钥。所以有些人理解为证书就是一个公钥(个人认为这种理解与实际偏差较大)。

3,颁发者:找到颁发者很重要,每个证书都有一个颁发者。这个在证书认证时用得到。

对于用户(人)来说还是通过证书名称来区分证书,下面讲解几种常见的证书。

a)      .cert.crt文件:这种证书倒是可以理解为公钥证书,因为它最主要的作用就是来提供公钥的,只是一种理解,签名认证这些作用一样不会少。这种文件不含有key,就好像一个打开的锁头,可以发给任何人。所以拥有.cer.crt文件的不是真正的拥有者,因为你可以用证书里的公钥加密,但并没有私钥解密。

b)      .pfx文件:这种证书文件可以理解为.cer文件与key结合体,即拥有.pfx证书相当同时拥有公钥与私钥。即可以加密也可以解密。

c)      .key文件:就是钥匙啦。锁头可以给任何人,但是钥匙只能自己保留,所以这玩意一定要保存好。

d)      .p7b文件:为证书链文件,也不含私钥。证书链即证书的拥有者、颁发者、颁发者的颁发者、依次类推的证书合成一个文件。

         以上对证书概念基础的了解,有基础的可以不用看。实际证书分类根据编码方式来区分的,只是为了方便理解才这么分的。当我们需要向对方发送加密数据时,我们只需要对方的cercrt文件就可以了。而需要对方向自己发送加密数据时,自己得拥有key,并且对方要有自己公钥(从cer文件获得)。

二、环境搭建

安装openssl这个就不讲了,现在很多linux版本都默认安装了。先来看下配置文件中一段。

默认配置文件/usr/local/openssl/ssl/openssl.cnf或者/etc/pki/tls/openssl.cnf

dir             = ./demoCA              # Where everything is kept
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 allowcreation of
                                        #several ctificates with same subject.
new_certs_dir   = $dir/newcerts         # default place for new certs.
 
certificate     = $dir/cacert.pem       # The CA certificate
serial          = $dir/serial           # The current serial number
crlnumber       = $dir/crlnumber        # the current crl number
                                        # mustbe commented out to leave a V1 CRL
crl             = $dir/crl.pem          # The current CRL
private_key     = $dir/private/cakey.pem# The private key
RANDFILE        = $dir/private/.rand    # private random number file

根据上面配置文件就知道,我们得搭建相同的目录结构环境。先创建一个目录test,并且将1  配置文件cptest目录下。

[root@localhost ~]# mkdir test
[root@localhost ~]# cd test
[root@localhost test]# cp/usr/local/openssl/ssl/openssl.cnf ./
[root@localhost test]# ls
openssl.cnf

2  根据配置文件中目录结构可知有个demoCA目录,目录下有各种文件。

[root@localhost test]# mkdir ./demoCA./demoCA/newcerts  ./demoCA/private
[root@localhost test]# chmod 777./demoCA/private
[root@localhost test]# echo‘01‘>./demoCA/serial
[root@localhost test]# touch./demoCA/index.txt

生成证书暂时用到这么多,其他可以先不创建,用到时再作修改。环境目录结构如下:

[root@localhost test]# tree
.
├── demoCA
│   ├── certs
│   ├── index.txt
│   ├── private
│   └──newcerts
└── openssl.cnf

3  环境搭建很重要的东西就修改配置文件了。这里只是实现生成证书,不讲解配置文件。

所以只需要修改如下一句就可以了。

dir     = ./demoCA     # Where everything is kept

将路径改为实际绝对路径。我这里就是/root/test/demoCA,所以修改后就是:

dir    = /root/test/demoCA    # Whereeverything is kept

三、整体步骤

openssl genrsa -des3 –out server.key 1024//生成key
openssl req -new –key server.key -outserver.csr -config openssl.cnf//生成csr文件
openssl req -new -x509 -keyoutca.key -outca.crt -config openssl.cnf//自生成CA(root)
openssl ca -in server.csr –out server.crt-cert ca.crt -keyfile ca.key -config openssl.cnf//签名
penssl pkcs12 -export -inkeyserver.key -inserver.crt -out server.pfx//合成pfx格式


四、具体生成证书步骤

将以生成服务器端证书为例讲解

1,生成私钥文件,保存为server.key。使用的3des算法,密钥长度为2048

[root@localhost test]# openssl genrsa -des3-out server.key 2048
Generating RSA private key, 2048 bit longmodulus
............................................................................+++
.....+++
e is 65537 (0x10001)
Enter pass phrase for server.key: #输入秘密
Verifying - Enter pass phrase forserver.key: #输入秘密
[root@localhost test]# ls
demoCA openssl.cnf  server.key

2,生成证书签名申请文件(csr)。保存为server.csr

[root@localhost test]# openssl req -new-key server.key -out server.csr -config openssl.cnf
Enter pass phrase for server.key:    #输入第1步输入的密码
You are about to be asked to enterinformation that will be incorporated
into your certificate request.
What you are about to enter is what iscalled a Distinguished Name or a DN.
There are quite a few fields but you canleave some blank
For some fields there will be a defaultvalue,
If you enter ‘.‘, the field will be leftblank.
-----
Country Name (2 letter code) [AU]:cn
State or Province Name (full name)[Some-State]:bj
Locality Name (eg, city) []:hd
Organization Name (eg, company) [InternetWidgits Pty Ltd]:www.test.com
Organizational Unit Name (eg, section)[]:test
Common Name (e.g. server FQDN or YOUR name)[]:www.test.com
Email Address []:test@test.com
 
Please enter the following ‘extra‘attributes
to be sent with your certificate request
A challenge password []: #这里可以不输入
An optional company name []: #这里可以不输入
[root@localhost test]# ls
demoCA openssl.cnf  server.csr  server.key

3,有申请文件,要有个机构来签名,实际是将server.csr文件提供给第三方可信任机构签名就可以。这里为了演示,将自生成CAroot)。证书保存为root.crtkey保存为root.key

[root@localhost test]# openssl req -new-x509 -keyout root.key -out root.crt -config openssl.cnf
Generating a 1024 bit RSA private key
...............................................++++++
..++++++
writing new private key to ‘root.key‘
Enter PEM pass phrase: #输入密码       
Verifying - Enter PEM pass phrase: #确认输入
-----
You are about to be asked to enterinformation that will be incorporated
into your certificate request.
What you are about to enter is what iscalled a Distinguished Name or a DN.
There are quite a few fields but you canleave some blank
For some fields there will be a defaultvalue,
If you enter ‘.‘, the field will be leftblank.
-----
Country Name (2 letter code) [AU]:cn #保持与csr文件信息一致,与配置文件有关。
State or Province Name (full name)[Some-State]:bj #保持与csr文件信息一致
Locality Name (eg, city) []:hd #保持与csr文件信息一致
Organization Name (eg, company) [InternetWidgits Pty Ltd]:www.test.com #保持与csr文件信息一致
Organizational Unit Name (eg, section)[]:test # 保持与csr文件信息一致
Common Name (e.g. server FQDN or YOUR name)[]:www.test.com
Email Address []:test@google.com
[root@localhost test]# ls
demoCA openssl.cnf  root.crt  root.key server.csr  server.key

4,有了CAroot)我们就可以用它来为第2步生的csr文件进行签名了。

[root@localhost test]# openssl ca -inserver.csr -out server.crt -cert root.crt -keyfile root.key -config openssl.cnf
Using configuration from openssl.cnf
Enter pass phrase for root.key:
Check that the request matches thesignature
Signature ok
Certificate Details:
       Serial Number: 1 (0x1)
       Validity
           Not Before: Oct  8 23:26:13 2015GMT
           Not After : Oct  7 23:26:13 2016GMT
       Subject:
           countryName               = cn
           stateOrProvinceName       = bj
           organizationName          =www.test.com
           organizationalUnitName    = test
           commonName                =www.test.com
           emailAddress              =test@test.com
       X509v3 extensions:
           X509v3 Basic Constraints:
                CA:FALSE
           Netscape Comment:
                OpenSSL Generated Certificate
           X509v3 Subject Key Identifier:
               35:2B:69:AE:42:6F:D7:93:CC:AC:C5:88:89:DE:F0:CC:4E:D9:EF:FD
           X509v3 Authority Key Identifier:
               keyid:0C:DA:95:AC:B9:BC:8E:F1:66:EC:DE:28:E3:01:66:D0:A4:82:1F:17
 
Certificate is to be certified untilOct  7 23:26:13 2016 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

5,至此所有文件都生成好了,查看下整个过程中需要的文件及生成的文件。

[root@localhost test]# tree
.
├── demoCA
│   ├── index.txt
│   ├── index.txt.attr
│   ├── index.txt.old
│   ├── newcerts
│   │   └── 01.pem
│   ├── private
│   ├── serial
│   └── serial.old
├── openssl.cnf
├── root.crt
├── root.key
├── server.crt #我们要生成的服务器端证书
├── server.csr
└── server.key #我们要生成的服务器端证书的key

五、经常也会碰到证书吊销列表CRL,现在来生成server.crl文件,即将刚才的证书吊销。

同样先根据配置文件配置环境,如下:

[root@localhost test]# cp root.key ./demoCA/private/cakey.pem
[root@localhost test]# cp root.crt./demoCA/cacert.pem
[root@localhost test]# echo ‘00‘ >./demoCA/crlnumber

吊销证书

[root@localhost test]# openssl ca -revokeserver.crt -config openssl.cnf
Using configuration from openssl.cnf
Enter pass phrase for/root/test/demoCA/private/cakey.pem:
Revoking Certificate 01.
Data Base Updated

生成吊销列表

[root@localhost test]# openssl ca-gencrl  -out server.crl  -config openssl.cnf
Using configuration from openssl.cnf
Enter pass phrase for/root/test/demoCA/private/cakey.pem:
[root@localhost test]# ls
demoCA openssl.cnf  root.crt  root.key server.crl  server.crt  server.csr server.key

查看所有生成的文件

[root@localhost test]# tree
.
├── demoCA
│   ├── cacert.pem
│   ├── crlnumber
│   ├── crlnumber.old
│   ├── index.txt
│   ├── index.txt.attr
│   ├── index.txt.attr.old
│   ├── index.txt.old
│   ├── newcerts
│   │   └── 01.pem #生成的证书
│   ├── private
│   │   └── cakey.pem
│   ├── serial
│   └── serial.old
├── openssl.cnf
├── root.crt #根证书
├── root.key #根key文件
├── server.crl #吊销列表
├── server.crt #服务端证书
├── server.csr #服务证书签名请求文件
└── server.key #服务器端证书key文件


五、总结

         以上只是简单讲解生成用户证书的过程,对于证书相关配置全部使用openssl.cnf默认配置。如:证书有效期、ca与用户证书信息验证等等。

         一开始说了,证书有很多格式,用的比较多的是pfx格式转换,转换命令如下:

pfx读取crtkey

opensslpkcs12 -in server.pfx -nodes -out server.pem # 生成明文所有内容
opensslrsa -in server.pem -out server.key # 取 key 文件
opensslx509 -in server.pem -out server.crt # 取证书


crtkey合成pfx

openssl pkcs12 -export -out server.pfx-inkey server.key -in server.crt


本文出自 “丁同学1990” 博客,请务必保留此出处http://dingtongxue1990.blog.51cto.com/4959501/1703964

  • 2
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
生成 SSL 证书和使用 SSL 证书一般需要以下步骤: 1. 安装 OpenSSL 首先需要安装 OpenSSL。如果你使用的是 Ubuntu 系统,可以通过以下命令来安装 OpenSSL: ``` sudo apt-get install openssl ``` 2. 生成私钥 使用 OpenSSL 生成私钥的命令如下: ``` openssl genrsa -out key.pem 2048 ``` 其中,`key.pem` 是生成的私钥文件名,`2048` 是私钥的长度,你也可以选择其他长度。 3. 生成证书签署请求(CSR) 使用 OpenSSL 生成证书签署请求的命令如下: ``` openssl req -new -key key.pem -out csr.pem ``` 其中,`key.pem` 是上一步生成的私钥文件名,`csr.pem` 是生成证书签署请求文件名。 在生成证书签署请求时,你需要提供一些信息,例如组织名称、国家、省份、城市、Email 等。 4. 签署证书 你可以使用自签名证书或者向认证机构请求签署证书。如果你想自己签署证书,可以使用以下命令: ``` openssl x509 -req -in csr.pem -signkey key.pem -out cert.pem ``` 其中,`csr.pem` 是上一步生成证书签署请求文件名,`key.pem` 是私钥文件名,`cert.pem` 是生成证书文件名。 5. 使用证书 生成证书后,你可以将其用于 SSL 加密通信、HTTPS 等场景中。例如,在 Node.js 中使用证书启动 HTTPS 服务器的代码如下: ``` const https = require('https'); const fs = require('fs'); const options = { key: fs.readFileSync('key.pem'), cert: fs.readFileSync('cert.pem') }; https.createServer(options, (req, res) => { res.writeHead(200); res.end('Hello, world!'); }).listen(443); ``` 其中,`key.pem` 是私钥文件名,`cert.pem` 是证书文件名。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值